* gdb.base/break.exp: Fix setting of $baz.
[deliverable/binutils-gdb.git] / gdb / symfile.c
CommitLineData
c906108c 1/* Generic symbol file reading for the GNU debugger, GDB.
8926118c 2
28e7fd62 3 Copyright (C) 1990-2013 Free Software Foundation, Inc.
8926118c 4
c906108c
SS
5 Contributed by Cygnus Support, using pieces from other GDB modules.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
e17c207e 23#include "arch-utils.h"
086df311 24#include "bfdlink.h"
c906108c
SS
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "gdbcore.h"
28#include "frame.h"
29#include "target.h"
30#include "value.h"
31#include "symfile.h"
32#include "objfiles.h"
0378c332 33#include "source.h"
c906108c
SS
34#include "gdbcmd.h"
35#include "breakpoint.h"
36#include "language.h"
37#include "complaints.h"
38#include "demangle.h"
fb14de7b
UW
39#include "inferior.h"
40#include "regcache.h"
5b5d99cf 41#include "filenames.h" /* for DOSish file names */
c906108c 42#include "gdb-stabs.h"
04ea0df1 43#include "gdb_obstack.h"
d75b5104 44#include "completer.h"
af5f3db6 45#include "bcache.h"
2de7ced7 46#include "hashtab.h"
dbda9972 47#include "readline/readline.h"
7e8580c1 48#include "gdb_assert.h"
fe898f56 49#include "block.h"
ea53e89f 50#include "observer.h"
c1bd25fd 51#include "exec.h"
9bdcbae7 52#include "parser-defs.h"
8756216b 53#include "varobj.h"
77069918 54#include "elf-bfd.h"
e85a822c 55#include "solib.h"
f1838a98 56#include "remote.h"
1bfeeb0f 57#include "stack.h"
cbb099e8 58#include "gdb_bfd.h"
529480d0 59#include "cli/cli-utils.h"
76ad5e1e 60#include "target.h"
c906108c 61
c906108c
SS
62#include <sys/types.h>
63#include <fcntl.h>
0e9f083f 64#include <string.h>
53ce3c39 65#include <sys/stat.h>
c906108c
SS
66#include <ctype.h>
67#include <time.h>
2b71414d 68#include <sys/time.h>
c906108c 69
ccefe4c4 70#include "psymtab.h"
c906108c 71
3e43a32a
MS
72int (*deprecated_ui_load_progress_hook) (const char *section,
73 unsigned long num);
9a4105ab 74void (*deprecated_show_load_progress) (const char *section,
5417f6dc
RM
75 unsigned long section_sent,
76 unsigned long section_size,
77 unsigned long total_sent,
c2d11a7d 78 unsigned long total_size);
769d7dc4
AC
79void (*deprecated_pre_add_symbol_hook) (const char *);
80void (*deprecated_post_add_symbol_hook) (void);
c906108c 81
74b7792f
AC
82static void clear_symtab_users_cleanup (void *ignore);
83
c378eb4e
MS
84/* Global variables owned by this file. */
85int readnow_symbol_files; /* Read full symbols immediately. */
c906108c 86
c378eb4e 87/* Functions this file defines. */
c906108c 88
a14ed312 89static void load_command (char *, int);
c906108c 90
69150c3d 91static void symbol_file_add_main_1 (const char *args, int from_tty, int flags);
d7db6da9 92
a14ed312 93static void add_symbol_file_command (char *, int);
c906108c 94
00b5771c 95static const struct sym_fns *find_sym_fns (bfd *);
c906108c 96
a14ed312 97static void decrement_reading_symtab (void *);
c906108c 98
a14ed312 99static void overlay_invalidate_all (void);
c906108c 100
a14ed312 101static void overlay_auto_command (char *, int);
c906108c 102
a14ed312 103static void overlay_manual_command (char *, int);
c906108c 104
a14ed312 105static void overlay_off_command (char *, int);
c906108c 106
a14ed312 107static void overlay_load_command (char *, int);
c906108c 108
a14ed312 109static void overlay_command (char *, int);
c906108c 110
a14ed312 111static void simple_free_overlay_table (void);
c906108c 112
e17a4113
UW
113static void read_target_long_array (CORE_ADDR, unsigned int *, int, int,
114 enum bfd_endian);
c906108c 115
a14ed312 116static int simple_read_overlay_table (void);
c906108c 117
a14ed312 118static int simple_overlay_update_1 (struct obj_section *);
c906108c 119
a14ed312 120static void add_filename_language (char *ext, enum language lang);
392a587b 121
a14ed312 122static void info_ext_lang_command (char *args, int from_tty);
392a587b 123
a14ed312 124static void init_filename_language_table (void);
392a587b 125
31d99776
DJ
126static void symfile_find_segment_sections (struct objfile *objfile);
127
a14ed312 128void _initialize_symfile (void);
c906108c
SS
129
130/* List of all available sym_fns. On gdb startup, each object file reader
131 calls add_symtab_fns() to register information on each format it is
c378eb4e 132 prepared to read. */
c906108c 133
c256e171
DE
134typedef struct
135{
136 /* BFD flavour that we handle. */
137 enum bfd_flavour sym_flavour;
138
139 /* The "vtable" of symbol functions. */
140 const struct sym_fns *sym_fns;
141} registered_sym_fns;
00b5771c 142
c256e171
DE
143DEF_VEC_O (registered_sym_fns);
144
145static VEC (registered_sym_fns) *symtab_fns = NULL;
c906108c 146
b7209cb4
FF
147/* If non-zero, shared library symbols will be added automatically
148 when the inferior is created, new libraries are loaded, or when
149 attaching to the inferior. This is almost always what users will
150 want to have happen; but for very large programs, the startup time
151 will be excessive, and so if this is a problem, the user can clear
152 this flag and then add the shared library symbols as needed. Note
153 that there is a potential for confusion, since if the shared
c906108c 154 library symbols are not loaded, commands like "info fun" will *not*
0d14a781 155 report all the functions that are actually present. */
c906108c
SS
156
157int auto_solib_add = 1;
c906108c 158\f
c5aa993b 159
0d14a781 160/* True if we are reading a symbol table. */
c906108c
SS
161
162int currently_reading_symtab = 0;
163
164static void
fba45db2 165decrement_reading_symtab (void *dummy)
c906108c
SS
166{
167 currently_reading_symtab--;
2cb9c859 168 gdb_assert (currently_reading_symtab >= 0);
c906108c
SS
169}
170
ccefe4c4
TT
171/* Increment currently_reading_symtab and return a cleanup that can be
172 used to decrement it. */
3b7bacac 173
ccefe4c4
TT
174struct cleanup *
175increment_reading_symtab (void)
c906108c 176{
ccefe4c4 177 ++currently_reading_symtab;
2cb9c859 178 gdb_assert (currently_reading_symtab > 0);
ccefe4c4 179 return make_cleanup (decrement_reading_symtab, NULL);
c906108c
SS
180}
181
5417f6dc
RM
182/* Remember the lowest-addressed loadable section we've seen.
183 This function is called via bfd_map_over_sections.
c906108c
SS
184
185 In case of equal vmas, the section with the largest size becomes the
186 lowest-addressed loadable section.
187
188 If the vmas and sizes are equal, the last section is considered the
189 lowest-addressed loadable section. */
190
191void
4efb68b1 192find_lowest_section (bfd *abfd, asection *sect, void *obj)
c906108c 193{
c5aa993b 194 asection **lowest = (asection **) obj;
c906108c 195
eb73e134 196 if (0 == (bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD)))
c906108c
SS
197 return;
198 if (!*lowest)
199 *lowest = sect; /* First loadable section */
200 else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
201 *lowest = sect; /* A lower loadable section */
202 else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
203 && (bfd_section_size (abfd, (*lowest))
204 <= bfd_section_size (abfd, sect)))
205 *lowest = sect;
206}
207
d76488d8
TT
208/* Create a new section_addr_info, with room for NUM_SECTIONS. The
209 new object's 'num_sections' field is set to 0; it must be updated
210 by the caller. */
a39a16c4
MM
211
212struct section_addr_info *
213alloc_section_addr_info (size_t num_sections)
214{
215 struct section_addr_info *sap;
216 size_t size;
217
218 size = (sizeof (struct section_addr_info)
219 + sizeof (struct other_sections) * (num_sections - 1));
220 sap = (struct section_addr_info *) xmalloc (size);
221 memset (sap, 0, size);
a39a16c4
MM
222
223 return sap;
224}
62557bbc
KB
225
226/* Build (allocate and populate) a section_addr_info struct from
c378eb4e 227 an existing section table. */
62557bbc
KB
228
229extern struct section_addr_info *
0542c86d
PA
230build_section_addr_info_from_section_table (const struct target_section *start,
231 const struct target_section *end)
62557bbc
KB
232{
233 struct section_addr_info *sap;
0542c86d 234 const struct target_section *stp;
62557bbc
KB
235 int oidx;
236
a39a16c4 237 sap = alloc_section_addr_info (end - start);
62557bbc
KB
238
239 for (stp = start, oidx = 0; stp != end; stp++)
240 {
2b2848e2
DE
241 struct bfd_section *asect = stp->the_bfd_section;
242 bfd *abfd = asect->owner;
243
244 if (bfd_get_section_flags (abfd, asect) & (SEC_ALLOC | SEC_LOAD)
a39a16c4 245 && oidx < end - start)
62557bbc
KB
246 {
247 sap->other[oidx].addr = stp->addr;
2b2848e2
DE
248 sap->other[oidx].name = xstrdup (bfd_section_name (abfd, asect));
249 sap->other[oidx].sectindex = gdb_bfd_section_index (abfd, asect);
62557bbc
KB
250 oidx++;
251 }
252 }
253
d76488d8
TT
254 sap->num_sections = oidx;
255
62557bbc
KB
256 return sap;
257}
258
82ccf5a5 259/* Create a section_addr_info from section offsets in ABFD. */
089b4803 260
82ccf5a5
JK
261static struct section_addr_info *
262build_section_addr_info_from_bfd (bfd *abfd)
089b4803
TG
263{
264 struct section_addr_info *sap;
265 int i;
266 struct bfd_section *sec;
267
82ccf5a5
JK
268 sap = alloc_section_addr_info (bfd_count_sections (abfd));
269 for (i = 0, sec = abfd->sections; sec != NULL; sec = sec->next)
270 if (bfd_get_section_flags (abfd, sec) & (SEC_ALLOC | SEC_LOAD))
012836ea 271 {
82ccf5a5
JK
272 sap->other[i].addr = bfd_get_section_vma (abfd, sec);
273 sap->other[i].name = xstrdup (bfd_get_section_name (abfd, sec));
65cf3563 274 sap->other[i].sectindex = gdb_bfd_section_index (abfd, sec);
012836ea
JK
275 i++;
276 }
d76488d8
TT
277
278 sap->num_sections = i;
279
089b4803
TG
280 return sap;
281}
282
82ccf5a5
JK
283/* Create a section_addr_info from section offsets in OBJFILE. */
284
285struct section_addr_info *
286build_section_addr_info_from_objfile (const struct objfile *objfile)
287{
288 struct section_addr_info *sap;
289 int i;
290
291 /* Before reread_symbols gets rewritten it is not safe to call:
292 gdb_assert (objfile->num_sections == bfd_count_sections (objfile->obfd));
293 */
294 sap = build_section_addr_info_from_bfd (objfile->obfd);
d76488d8 295 for (i = 0; i < sap->num_sections; i++)
82ccf5a5
JK
296 {
297 int sectindex = sap->other[i].sectindex;
298
299 sap->other[i].addr += objfile->section_offsets->offsets[sectindex];
300 }
301 return sap;
302}
62557bbc 303
c378eb4e 304/* Free all memory allocated by build_section_addr_info_from_section_table. */
62557bbc
KB
305
306extern void
307free_section_addr_info (struct section_addr_info *sap)
308{
309 int idx;
310
a39a16c4 311 for (idx = 0; idx < sap->num_sections; idx++)
d76488d8 312 xfree (sap->other[idx].name);
b8c9b27d 313 xfree (sap);
62557bbc
KB
314}
315
e8289572 316/* Initialize OBJFILE's sect_index_* members. */
3b7bacac 317
e8289572
JB
318static void
319init_objfile_sect_indices (struct objfile *objfile)
c906108c 320{
e8289572 321 asection *sect;
c906108c 322 int i;
5417f6dc 323
b8fbeb18 324 sect = bfd_get_section_by_name (objfile->obfd, ".text");
5417f6dc 325 if (sect)
b8fbeb18
EZ
326 objfile->sect_index_text = sect->index;
327
328 sect = bfd_get_section_by_name (objfile->obfd, ".data");
5417f6dc 329 if (sect)
b8fbeb18
EZ
330 objfile->sect_index_data = sect->index;
331
332 sect = bfd_get_section_by_name (objfile->obfd, ".bss");
5417f6dc 333 if (sect)
b8fbeb18
EZ
334 objfile->sect_index_bss = sect->index;
335
336 sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
5417f6dc 337 if (sect)
b8fbeb18
EZ
338 objfile->sect_index_rodata = sect->index;
339
bbcd32ad
FF
340 /* This is where things get really weird... We MUST have valid
341 indices for the various sect_index_* members or gdb will abort.
342 So if for example, there is no ".text" section, we have to
31d99776
DJ
343 accomodate that. First, check for a file with the standard
344 one or two segments. */
345
346 symfile_find_segment_sections (objfile);
347
348 /* Except when explicitly adding symbol files at some address,
349 section_offsets contains nothing but zeros, so it doesn't matter
350 which slot in section_offsets the individual sect_index_* members
351 index into. So if they are all zero, it is safe to just point
352 all the currently uninitialized indices to the first slot. But
353 beware: if this is the main executable, it may be relocated
354 later, e.g. by the remote qOffsets packet, and then this will
355 be wrong! That's why we try segments first. */
bbcd32ad
FF
356
357 for (i = 0; i < objfile->num_sections; i++)
358 {
359 if (ANOFFSET (objfile->section_offsets, i) != 0)
360 {
361 break;
362 }
363 }
364 if (i == objfile->num_sections)
365 {
366 if (objfile->sect_index_text == -1)
367 objfile->sect_index_text = 0;
368 if (objfile->sect_index_data == -1)
369 objfile->sect_index_data = 0;
370 if (objfile->sect_index_bss == -1)
371 objfile->sect_index_bss = 0;
372 if (objfile->sect_index_rodata == -1)
373 objfile->sect_index_rodata = 0;
374 }
b8fbeb18 375}
c906108c 376
c1bd25fd
DJ
377/* The arguments to place_section. */
378
379struct place_section_arg
380{
381 struct section_offsets *offsets;
382 CORE_ADDR lowest;
383};
384
385/* Find a unique offset to use for loadable section SECT if
386 the user did not provide an offset. */
387
2c0b251b 388static void
c1bd25fd
DJ
389place_section (bfd *abfd, asection *sect, void *obj)
390{
391 struct place_section_arg *arg = obj;
392 CORE_ADDR *offsets = arg->offsets->offsets, start_addr;
393 int done;
3bd72c6f 394 ULONGEST align = ((ULONGEST) 1) << bfd_get_section_alignment (abfd, sect);
c1bd25fd 395
2711e456
DJ
396 /* We are only interested in allocated sections. */
397 if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
c1bd25fd
DJ
398 return;
399
400 /* If the user specified an offset, honor it. */
65cf3563 401 if (offsets[gdb_bfd_section_index (abfd, sect)] != 0)
c1bd25fd
DJ
402 return;
403
404 /* Otherwise, let's try to find a place for the section. */
3bd72c6f
DJ
405 start_addr = (arg->lowest + align - 1) & -align;
406
c1bd25fd
DJ
407 do {
408 asection *cur_sec;
c1bd25fd 409
c1bd25fd
DJ
410 done = 1;
411
412 for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
413 {
414 int indx = cur_sec->index;
c1bd25fd
DJ
415
416 /* We don't need to compare against ourself. */
417 if (cur_sec == sect)
418 continue;
419
2711e456
DJ
420 /* We can only conflict with allocated sections. */
421 if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
c1bd25fd
DJ
422 continue;
423
424 /* If the section offset is 0, either the section has not been placed
425 yet, or it was the lowest section placed (in which case LOWEST
426 will be past its end). */
427 if (offsets[indx] == 0)
428 continue;
429
430 /* If this section would overlap us, then we must move up. */
431 if (start_addr + bfd_get_section_size (sect) > offsets[indx]
432 && start_addr < offsets[indx] + bfd_get_section_size (cur_sec))
433 {
434 start_addr = offsets[indx] + bfd_get_section_size (cur_sec);
435 start_addr = (start_addr + align - 1) & -align;
436 done = 0;
3bd72c6f 437 break;
c1bd25fd
DJ
438 }
439
440 /* Otherwise, we appear to be OK. So far. */
441 }
442 }
443 while (!done);
444
65cf3563 445 offsets[gdb_bfd_section_index (abfd, sect)] = start_addr;
c1bd25fd 446 arg->lowest = start_addr + bfd_get_section_size (sect);
c1bd25fd 447}
e8289572 448
75242ef4
JK
449/* Store struct section_addr_info as prepared (made relative and with SECTINDEX
450 filled-in) by addr_info_make_relative into SECTION_OFFSETS of NUM_SECTIONS
451 entries. */
e8289572
JB
452
453void
75242ef4
JK
454relative_addr_info_to_section_offsets (struct section_offsets *section_offsets,
455 int num_sections,
3189cb12 456 const struct section_addr_info *addrs)
e8289572
JB
457{
458 int i;
459
75242ef4 460 memset (section_offsets, 0, SIZEOF_N_SECTION_OFFSETS (num_sections));
e8289572 461
c378eb4e 462 /* Now calculate offsets for section that were specified by the caller. */
d76488d8 463 for (i = 0; i < addrs->num_sections; i++)
e8289572 464 {
3189cb12 465 const struct other_sections *osp;
e8289572 466
75242ef4 467 osp = &addrs->other[i];
5488dafb 468 if (osp->sectindex == -1)
e8289572
JB
469 continue;
470
c378eb4e 471 /* Record all sections in offsets. */
e8289572 472 /* The section_offsets in the objfile are here filled in using
c378eb4e 473 the BFD index. */
75242ef4
JK
474 section_offsets->offsets[osp->sectindex] = osp->addr;
475 }
476}
477
1276c759
JK
478/* Transform section name S for a name comparison. prelink can split section
479 `.bss' into two sections `.dynbss' and `.bss' (in this order). Similarly
480 prelink can split `.sbss' into `.sdynbss' and `.sbss'. Use virtual address
481 of the new `.dynbss' (`.sdynbss') section as the adjacent new `.bss'
482 (`.sbss') section has invalid (increased) virtual address. */
483
484static const char *
485addr_section_name (const char *s)
486{
487 if (strcmp (s, ".dynbss") == 0)
488 return ".bss";
489 if (strcmp (s, ".sdynbss") == 0)
490 return ".sbss";
491
492 return s;
493}
494
82ccf5a5
JK
495/* qsort comparator for addrs_section_sort. Sort entries in ascending order by
496 their (name, sectindex) pair. sectindex makes the sort by name stable. */
497
498static int
499addrs_section_compar (const void *ap, const void *bp)
500{
501 const struct other_sections *a = *((struct other_sections **) ap);
502 const struct other_sections *b = *((struct other_sections **) bp);
22e048c9 503 int retval;
82ccf5a5 504
1276c759 505 retval = strcmp (addr_section_name (a->name), addr_section_name (b->name));
82ccf5a5
JK
506 if (retval)
507 return retval;
508
5488dafb 509 return a->sectindex - b->sectindex;
82ccf5a5
JK
510}
511
512/* Provide sorted array of pointers to sections of ADDRS. The array is
513 terminated by NULL. Caller is responsible to call xfree for it. */
514
515static struct other_sections **
516addrs_section_sort (struct section_addr_info *addrs)
517{
518 struct other_sections **array;
519 int i;
520
521 /* `+ 1' for the NULL terminator. */
522 array = xmalloc (sizeof (*array) * (addrs->num_sections + 1));
d76488d8 523 for (i = 0; i < addrs->num_sections; i++)
82ccf5a5
JK
524 array[i] = &addrs->other[i];
525 array[i] = NULL;
526
527 qsort (array, i, sizeof (*array), addrs_section_compar);
528
529 return array;
530}
531
75242ef4 532/* Relativize absolute addresses in ADDRS into offsets based on ABFD. Fill-in
672d9c23
JK
533 also SECTINDEXes specific to ABFD there. This function can be used to
534 rebase ADDRS to start referencing different BFD than before. */
75242ef4
JK
535
536void
537addr_info_make_relative (struct section_addr_info *addrs, bfd *abfd)
538{
539 asection *lower_sect;
75242ef4
JK
540 CORE_ADDR lower_offset;
541 int i;
82ccf5a5
JK
542 struct cleanup *my_cleanup;
543 struct section_addr_info *abfd_addrs;
544 struct other_sections **addrs_sorted, **abfd_addrs_sorted;
545 struct other_sections **addrs_to_abfd_addrs;
75242ef4
JK
546
547 /* Find lowest loadable section to be used as starting point for
e76ab67f
DJ
548 continguous sections. */
549 lower_sect = NULL;
550 bfd_map_over_sections (abfd, find_lowest_section, &lower_sect);
75242ef4
JK
551 if (lower_sect == NULL)
552 {
553 warning (_("no loadable sections found in added symbol-file %s"),
554 bfd_get_filename (abfd));
555 lower_offset = 0;
e8289572 556 }
75242ef4
JK
557 else
558 lower_offset = bfd_section_vma (bfd_get_filename (abfd), lower_sect);
559
82ccf5a5
JK
560 /* Create ADDRS_TO_ABFD_ADDRS array to map the sections in ADDRS to sections
561 in ABFD. Section names are not unique - there can be multiple sections of
562 the same name. Also the sections of the same name do not have to be
563 adjacent to each other. Some sections may be present only in one of the
564 files. Even sections present in both files do not have to be in the same
565 order.
566
567 Use stable sort by name for the sections in both files. Then linearly
568 scan both lists matching as most of the entries as possible. */
569
570 addrs_sorted = addrs_section_sort (addrs);
571 my_cleanup = make_cleanup (xfree, addrs_sorted);
572
573 abfd_addrs = build_section_addr_info_from_bfd (abfd);
574 make_cleanup_free_section_addr_info (abfd_addrs);
575 abfd_addrs_sorted = addrs_section_sort (abfd_addrs);
576 make_cleanup (xfree, abfd_addrs_sorted);
577
c378eb4e
MS
578 /* Now create ADDRS_TO_ABFD_ADDRS from ADDRS_SORTED and
579 ABFD_ADDRS_SORTED. */
82ccf5a5
JK
580
581 addrs_to_abfd_addrs = xzalloc (sizeof (*addrs_to_abfd_addrs)
582 * addrs->num_sections);
583 make_cleanup (xfree, addrs_to_abfd_addrs);
584
585 while (*addrs_sorted)
586 {
1276c759 587 const char *sect_name = addr_section_name ((*addrs_sorted)->name);
82ccf5a5
JK
588
589 while (*abfd_addrs_sorted
1276c759
JK
590 && strcmp (addr_section_name ((*abfd_addrs_sorted)->name),
591 sect_name) < 0)
82ccf5a5
JK
592 abfd_addrs_sorted++;
593
594 if (*abfd_addrs_sorted
1276c759
JK
595 && strcmp (addr_section_name ((*abfd_addrs_sorted)->name),
596 sect_name) == 0)
82ccf5a5
JK
597 {
598 int index_in_addrs;
599
600 /* Make the found item directly addressable from ADDRS. */
601 index_in_addrs = *addrs_sorted - addrs->other;
602 gdb_assert (addrs_to_abfd_addrs[index_in_addrs] == NULL);
603 addrs_to_abfd_addrs[index_in_addrs] = *abfd_addrs_sorted;
604
605 /* Never use the same ABFD entry twice. */
606 abfd_addrs_sorted++;
607 }
608
609 addrs_sorted++;
610 }
611
75242ef4
JK
612 /* Calculate offsets for the loadable sections.
613 FIXME! Sections must be in order of increasing loadable section
614 so that contiguous sections can use the lower-offset!!!
615
616 Adjust offsets if the segments are not contiguous.
617 If the section is contiguous, its offset should be set to
618 the offset of the highest loadable section lower than it
619 (the loadable section directly below it in memory).
620 this_offset = lower_offset = lower_addr - lower_orig_addr */
621
d76488d8 622 for (i = 0; i < addrs->num_sections; i++)
75242ef4 623 {
82ccf5a5 624 struct other_sections *sect = addrs_to_abfd_addrs[i];
672d9c23
JK
625
626 if (sect)
75242ef4 627 {
c378eb4e 628 /* This is the index used by BFD. */
82ccf5a5 629 addrs->other[i].sectindex = sect->sectindex;
672d9c23
JK
630
631 if (addrs->other[i].addr != 0)
75242ef4 632 {
82ccf5a5 633 addrs->other[i].addr -= sect->addr;
75242ef4 634 lower_offset = addrs->other[i].addr;
75242ef4
JK
635 }
636 else
672d9c23 637 addrs->other[i].addr = lower_offset;
75242ef4
JK
638 }
639 else
672d9c23 640 {
1276c759
JK
641 /* addr_section_name transformation is not used for SECT_NAME. */
642 const char *sect_name = addrs->other[i].name;
643
b0fcb67f
JK
644 /* This section does not exist in ABFD, which is normally
645 unexpected and we want to issue a warning.
646
4d9743af
JK
647 However, the ELF prelinker does create a few sections which are
648 marked in the main executable as loadable (they are loaded in
649 memory from the DYNAMIC segment) and yet are not present in
650 separate debug info files. This is fine, and should not cause
651 a warning. Shared libraries contain just the section
652 ".gnu.liblist" but it is not marked as loadable there. There is
653 no other way to identify them than by their name as the sections
1276c759
JK
654 created by prelink have no special flags.
655
656 For the sections `.bss' and `.sbss' see addr_section_name. */
b0fcb67f
JK
657
658 if (!(strcmp (sect_name, ".gnu.liblist") == 0
4d9743af 659 || strcmp (sect_name, ".gnu.conflict") == 0
1276c759
JK
660 || (strcmp (sect_name, ".bss") == 0
661 && i > 0
662 && strcmp (addrs->other[i - 1].name, ".dynbss") == 0
663 && addrs_to_abfd_addrs[i - 1] != NULL)
664 || (strcmp (sect_name, ".sbss") == 0
665 && i > 0
666 && strcmp (addrs->other[i - 1].name, ".sdynbss") == 0
667 && addrs_to_abfd_addrs[i - 1] != NULL)))
b0fcb67f
JK
668 warning (_("section %s not found in %s"), sect_name,
669 bfd_get_filename (abfd));
670
672d9c23 671 addrs->other[i].addr = 0;
5488dafb 672 addrs->other[i].sectindex = -1;
672d9c23 673 }
75242ef4 674 }
82ccf5a5
JK
675
676 do_cleanups (my_cleanup);
75242ef4
JK
677}
678
679/* Parse the user's idea of an offset for dynamic linking, into our idea
680 of how to represent it for fast symbol reading. This is the default
681 version of the sym_fns.sym_offsets function for symbol readers that
682 don't need to do anything special. It allocates a section_offsets table
683 for the objectfile OBJFILE and stuffs ADDR into all of the offsets. */
684
685void
686default_symfile_offsets (struct objfile *objfile,
3189cb12 687 const struct section_addr_info *addrs)
75242ef4 688{
d445b2f6 689 objfile->num_sections = gdb_bfd_count_sections (objfile->obfd);
75242ef4
JK
690 objfile->section_offsets = (struct section_offsets *)
691 obstack_alloc (&objfile->objfile_obstack,
692 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
693 relative_addr_info_to_section_offsets (objfile->section_offsets,
694 objfile->num_sections, addrs);
e8289572 695
c1bd25fd
DJ
696 /* For relocatable files, all loadable sections will start at zero.
697 The zero is meaningless, so try to pick arbitrary addresses such
698 that no loadable sections overlap. This algorithm is quadratic,
699 but the number of sections in a single object file is generally
700 small. */
701 if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0)
702 {
703 struct place_section_arg arg;
2711e456
DJ
704 bfd *abfd = objfile->obfd;
705 asection *cur_sec;
2711e456
DJ
706
707 for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
708 /* We do not expect this to happen; just skip this step if the
709 relocatable file has a section with an assigned VMA. */
710 if (bfd_section_vma (abfd, cur_sec) != 0)
711 break;
712
713 if (cur_sec == NULL)
714 {
715 CORE_ADDR *offsets = objfile->section_offsets->offsets;
716
717 /* Pick non-overlapping offsets for sections the user did not
718 place explicitly. */
719 arg.offsets = objfile->section_offsets;
720 arg.lowest = 0;
721 bfd_map_over_sections (objfile->obfd, place_section, &arg);
722
723 /* Correctly filling in the section offsets is not quite
724 enough. Relocatable files have two properties that
725 (most) shared objects do not:
726
727 - Their debug information will contain relocations. Some
728 shared libraries do also, but many do not, so this can not
729 be assumed.
730
731 - If there are multiple code sections they will be loaded
732 at different relative addresses in memory than they are
733 in the objfile, since all sections in the file will start
734 at address zero.
735
736 Because GDB has very limited ability to map from an
737 address in debug info to the correct code section,
738 it relies on adding SECT_OFF_TEXT to things which might be
739 code. If we clear all the section offsets, and set the
740 section VMAs instead, then symfile_relocate_debug_section
741 will return meaningful debug information pointing at the
742 correct sections.
743
744 GDB has too many different data structures for section
745 addresses - a bfd, objfile, and so_list all have section
746 tables, as does exec_ops. Some of these could probably
747 be eliminated. */
748
749 for (cur_sec = abfd->sections; cur_sec != NULL;
750 cur_sec = cur_sec->next)
751 {
752 if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
753 continue;
754
755 bfd_set_section_vma (abfd, cur_sec, offsets[cur_sec->index]);
3e43a32a
MS
756 exec_set_section_address (bfd_get_filename (abfd),
757 cur_sec->index,
30510692 758 offsets[cur_sec->index]);
2711e456
DJ
759 offsets[cur_sec->index] = 0;
760 }
761 }
c1bd25fd
DJ
762 }
763
e8289572 764 /* Remember the bfd indexes for the .text, .data, .bss and
c378eb4e 765 .rodata sections. */
e8289572
JB
766 init_objfile_sect_indices (objfile);
767}
768
31d99776
DJ
769/* Divide the file into segments, which are individual relocatable units.
770 This is the default version of the sym_fns.sym_segments function for
771 symbol readers that do not have an explicit representation of segments.
772 It assumes that object files do not have segments, and fully linked
773 files have a single segment. */
774
775struct symfile_segment_data *
776default_symfile_segments (bfd *abfd)
777{
778 int num_sections, i;
779 asection *sect;
780 struct symfile_segment_data *data;
781 CORE_ADDR low, high;
782
783 /* Relocatable files contain enough information to position each
784 loadable section independently; they should not be relocated
785 in segments. */
786 if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) == 0)
787 return NULL;
788
789 /* Make sure there is at least one loadable section in the file. */
790 for (sect = abfd->sections; sect != NULL; sect = sect->next)
791 {
792 if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
793 continue;
794
795 break;
796 }
797 if (sect == NULL)
798 return NULL;
799
800 low = bfd_get_section_vma (abfd, sect);
801 high = low + bfd_get_section_size (sect);
802
803 data = XZALLOC (struct symfile_segment_data);
804 data->num_segments = 1;
805 data->segment_bases = XCALLOC (1, CORE_ADDR);
806 data->segment_sizes = XCALLOC (1, CORE_ADDR);
807
808 num_sections = bfd_count_sections (abfd);
809 data->segment_info = XCALLOC (num_sections, int);
810
811 for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
812 {
813 CORE_ADDR vma;
814
815 if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
816 continue;
817
818 vma = bfd_get_section_vma (abfd, sect);
819 if (vma < low)
820 low = vma;
821 if (vma + bfd_get_section_size (sect) > high)
822 high = vma + bfd_get_section_size (sect);
823
824 data->segment_info[i] = 1;
825 }
826
827 data->segment_bases[0] = low;
828 data->segment_sizes[0] = high - low;
829
830 return data;
831}
832
608e2dbb
TT
833/* This is a convenience function to call sym_read for OBJFILE and
834 possibly force the partial symbols to be read. */
835
836static void
837read_symbols (struct objfile *objfile, int add_flags)
838{
839 (*objfile->sf->sym_read) (objfile, add_flags);
8a92335b
JK
840
841 /* find_separate_debug_file_in_section should be called only if there is
842 single binary with no existing separate debug info file. */
843 if (!objfile_has_partial_symbols (objfile)
844 && objfile->separate_debug_objfile == NULL
845 && objfile->separate_debug_objfile_backlink == NULL)
608e2dbb
TT
846 {
847 bfd *abfd = find_separate_debug_file_in_section (objfile);
848 struct cleanup *cleanup = make_cleanup_bfd_unref (abfd);
849
850 if (abfd != NULL)
24ba069a
JK
851 {
852 /* find_separate_debug_file_in_section uses the same filename for the
853 virtual section-as-bfd like the bfd filename containing the
854 section. Therefore use also non-canonical name form for the same
855 file containing the section. */
856 symbol_file_add_separate (abfd, objfile->original_name, add_flags,
857 objfile);
858 }
608e2dbb
TT
859
860 do_cleanups (cleanup);
861 }
862 if ((add_flags & SYMFILE_NO_READ) == 0)
863 require_partial_symbols (objfile, 0);
864}
865
3d6e24f0
JB
866/* Initialize entry point information for this objfile. */
867
868static void
869init_entry_point_info (struct objfile *objfile)
870{
871 /* Save startup file's range of PC addresses to help blockframe.c
872 decide where the bottom of the stack is. */
873
874 if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
875 {
876 /* Executable file -- record its entry point so we'll recognize
877 the startup file because it contains the entry point. */
878 objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
879 objfile->ei.entry_point_p = 1;
880 }
881 else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
882 && bfd_get_start_address (objfile->obfd) != 0)
883 {
884 /* Some shared libraries may have entry points set and be
885 runnable. There's no clear way to indicate this, so just check
886 for values other than zero. */
887 objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
888 objfile->ei.entry_point_p = 1;
889 }
890 else
891 {
892 /* Examination of non-executable.o files. Short-circuit this stuff. */
893 objfile->ei.entry_point_p = 0;
894 }
895
896 if (objfile->ei.entry_point_p)
897 {
898 CORE_ADDR entry_point = objfile->ei.entry_point;
899
900 /* Make certain that the address points at real code, and not a
901 function descriptor. */
902 entry_point
df6d5441 903 = gdbarch_convert_from_func_ptr_addr (get_objfile_arch (objfile),
3d6e24f0
JB
904 entry_point,
905 &current_target);
906
907 /* Remove any ISA markers, so that this matches entries in the
908 symbol table. */
909 objfile->ei.entry_point
df6d5441 910 = gdbarch_addr_bits_remove (get_objfile_arch (objfile), entry_point);
3d6e24f0
JB
911 }
912}
913
c906108c
SS
914/* Process a symbol file, as either the main file or as a dynamically
915 loaded file.
916
36e4d068
JB
917 This function does not set the OBJFILE's entry-point info.
918
96baa820
JM
919 OBJFILE is where the symbols are to be read from.
920
7e8580c1
JB
921 ADDRS is the list of section load addresses. If the user has given
922 an 'add-symbol-file' command, then this is the list of offsets and
923 addresses he or she provided as arguments to the command; or, if
924 we're handling a shared library, these are the actual addresses the
925 sections are loaded at, according to the inferior's dynamic linker
926 (as gleaned by GDB's shared library code). We convert each address
927 into an offset from the section VMA's as it appears in the object
928 file, and then call the file's sym_offsets function to convert this
929 into a format-specific offset table --- a `struct section_offsets'.
96baa820 930
7eedccfa
PP
931 ADD_FLAGS encodes verbosity level, whether this is main symbol or
932 an extra symbol file such as dynamically loaded code, and wether
933 breakpoint reset should be deferred. */
c906108c 934
36e4d068
JB
935static void
936syms_from_objfile_1 (struct objfile *objfile,
937 struct section_addr_info *addrs,
36e4d068 938 int add_flags)
c906108c 939{
a39a16c4 940 struct section_addr_info *local_addr = NULL;
c906108c 941 struct cleanup *old_chain;
7eedccfa 942 const int mainline = add_flags & SYMFILE_MAINLINE;
2acceee2 943
8fb8eb5c 944 objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
c906108c 945
75245b24 946 if (objfile->sf == NULL)
36e4d068
JB
947 {
948 /* No symbols to load, but we still need to make sure
949 that the section_offsets table is allocated. */
d445b2f6 950 int num_sections = gdb_bfd_count_sections (objfile->obfd);
a7bfba49 951 size_t size = SIZEOF_N_SECTION_OFFSETS (num_sections);
36e4d068
JB
952
953 objfile->num_sections = num_sections;
954 objfile->section_offsets
955 = obstack_alloc (&objfile->objfile_obstack, size);
956 memset (objfile->section_offsets, 0, size);
957 return;
958 }
75245b24 959
c906108c
SS
960 /* Make sure that partially constructed symbol tables will be cleaned up
961 if an error occurs during symbol reading. */
74b7792f 962 old_chain = make_cleanup_free_objfile (objfile);
c906108c 963
6bf667bb
DE
964 /* If ADDRS is NULL, put together a dummy address list.
965 We now establish the convention that an addr of zero means
c378eb4e 966 no load address was specified. */
6bf667bb 967 if (! addrs)
a39a16c4 968 {
d445b2f6 969 local_addr = alloc_section_addr_info (1);
a39a16c4
MM
970 make_cleanup (xfree, local_addr);
971 addrs = local_addr;
972 }
973
c5aa993b 974 if (mainline)
c906108c
SS
975 {
976 /* We will modify the main symbol table, make sure that all its users
c5aa993b 977 will be cleaned up if an error occurs during symbol reading. */
74b7792f 978 make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
c906108c
SS
979
980 /* Since no error yet, throw away the old symbol table. */
981
982 if (symfile_objfile != NULL)
983 {
984 free_objfile (symfile_objfile);
adb7f338 985 gdb_assert (symfile_objfile == NULL);
c906108c
SS
986 }
987
988 /* Currently we keep symbols from the add-symbol-file command.
c5aa993b
JM
989 If the user wants to get rid of them, they should do "symbol-file"
990 without arguments first. Not sure this is the best behavior
991 (PR 2207). */
c906108c 992
c5aa993b 993 (*objfile->sf->sym_new_init) (objfile);
c906108c
SS
994 }
995
996 /* Convert addr into an offset rather than an absolute address.
997 We find the lowest address of a loaded segment in the objfile,
53a5351d 998 and assume that <addr> is where that got loaded.
c906108c 999
53a5351d
JM
1000 We no longer warn if the lowest section is not a text segment (as
1001 happens for the PA64 port. */
6bf667bb 1002 if (addrs->num_sections > 0)
75242ef4 1003 addr_info_make_relative (addrs, objfile->obfd);
c906108c
SS
1004
1005 /* Initialize symbol reading routines for this objfile, allow complaints to
1006 appear for this new file, and record how verbose to be, then do the
c378eb4e 1007 initial symbol reading for this file. */
c906108c 1008
c5aa993b 1009 (*objfile->sf->sym_init) (objfile);
7eedccfa 1010 clear_complaints (&symfile_complaints, 1, add_flags & SYMFILE_VERBOSE);
c906108c 1011
6bf667bb 1012 (*objfile->sf->sym_offsets) (objfile, addrs);
c906108c 1013
608e2dbb 1014 read_symbols (objfile, add_flags);
b11896a5 1015
c906108c
SS
1016 /* Discard cleanups as symbol reading was successful. */
1017
1018 discard_cleanups (old_chain);
f7545552 1019 xfree (local_addr);
c906108c
SS
1020}
1021
36e4d068
JB
1022/* Same as syms_from_objfile_1, but also initializes the objfile
1023 entry-point info. */
1024
6bf667bb 1025static void
36e4d068
JB
1026syms_from_objfile (struct objfile *objfile,
1027 struct section_addr_info *addrs,
36e4d068
JB
1028 int add_flags)
1029{
6bf667bb 1030 syms_from_objfile_1 (objfile, addrs, add_flags);
36e4d068
JB
1031 init_entry_point_info (objfile);
1032}
1033
c906108c
SS
1034/* Perform required actions after either reading in the initial
1035 symbols for a new objfile, or mapping in the symbols from a reusable
c1e56572 1036 objfile. ADD_FLAGS is a bitmask of enum symfile_add_flags. */
c5aa993b 1037
c906108c 1038void
7eedccfa 1039new_symfile_objfile (struct objfile *objfile, int add_flags)
c906108c 1040{
c906108c 1041 /* If this is the main symbol file we have to clean up all users of the
c378eb4e 1042 old main symbol file. Otherwise it is sufficient to fixup all the
c906108c 1043 breakpoints that may have been redefined by this symbol file. */
7eedccfa 1044 if (add_flags & SYMFILE_MAINLINE)
c906108c
SS
1045 {
1046 /* OK, make it the "real" symbol file. */
1047 symfile_objfile = objfile;
1048
c1e56572 1049 clear_symtab_users (add_flags);
c906108c 1050 }
7eedccfa 1051 else if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
c906108c 1052 {
69de3c6a 1053 breakpoint_re_set ();
c906108c
SS
1054 }
1055
1056 /* We're done reading the symbol file; finish off complaints. */
7eedccfa 1057 clear_complaints (&symfile_complaints, 0, add_flags & SYMFILE_VERBOSE);
c906108c
SS
1058}
1059
1060/* Process a symbol file, as either the main file or as a dynamically
1061 loaded file.
1062
5417f6dc 1063 ABFD is a BFD already open on the file, as from symfile_bfd_open.
8ac244b4 1064 A new reference is acquired by this function.
7904e09f 1065
24ba069a
JK
1066 For NAME description see allocate_objfile's definition.
1067
7eedccfa
PP
1068 ADD_FLAGS encodes verbosity, whether this is main symbol file or
1069 extra, such as dynamically loaded code, and what to do with breakpoins.
7904e09f 1070
6bf667bb 1071 ADDRS is as described for syms_from_objfile_1, above.
7eedccfa 1072 ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS.
c906108c 1073
63524580
JK
1074 PARENT is the original objfile if ABFD is a separate debug info file.
1075 Otherwise PARENT is NULL.
1076
c906108c 1077 Upon success, returns a pointer to the objfile that was added.
c378eb4e 1078 Upon failure, jumps back to command level (never returns). */
7eedccfa 1079
7904e09f 1080static struct objfile *
24ba069a 1081symbol_file_add_with_addrs (bfd *abfd, const char *name, int add_flags,
6bf667bb
DE
1082 struct section_addr_info *addrs,
1083 int flags, struct objfile *parent)
c906108c
SS
1084{
1085 struct objfile *objfile;
7eedccfa 1086 const int from_tty = add_flags & SYMFILE_VERBOSE;
0838fb57 1087 const int mainline = add_flags & SYMFILE_MAINLINE;
b11896a5
TT
1088 const int should_print = ((from_tty || info_verbose)
1089 && (readnow_symbol_files
1090 || (add_flags & SYMFILE_NO_READ) == 0));
c906108c 1091
9291a0cd 1092 if (readnow_symbol_files)
b11896a5
TT
1093 {
1094 flags |= OBJF_READNOW;
1095 add_flags &= ~SYMFILE_NO_READ;
1096 }
9291a0cd 1097
5417f6dc
RM
1098 /* Give user a chance to burp if we'd be
1099 interactively wiping out any existing symbols. */
c906108c
SS
1100
1101 if ((have_full_symbols () || have_partial_symbols ())
0838fb57 1102 && mainline
c906108c 1103 && from_tty
9e2f0ad4 1104 && !query (_("Load new symbol table from \"%s\"? "), name))
8a3fe4f8 1105 error (_("Not confirmed."));
c906108c 1106
24ba069a
JK
1107 objfile = allocate_objfile (abfd, name,
1108 flags | (mainline ? OBJF_MAINLINE : 0));
c906108c 1109
63524580
JK
1110 if (parent)
1111 add_separate_debug_objfile (objfile, parent);
1112
78a4a9b9
AC
1113 /* We either created a new mapped symbol table, mapped an existing
1114 symbol table file which has not had initial symbol reading
c378eb4e 1115 performed, or need to read an unmapped symbol table. */
b11896a5 1116 if (should_print)
c906108c 1117 {
769d7dc4
AC
1118 if (deprecated_pre_add_symbol_hook)
1119 deprecated_pre_add_symbol_hook (name);
78a4a9b9 1120 else
c906108c 1121 {
55333a84
DE
1122 printf_unfiltered (_("Reading symbols from %s..."), name);
1123 wrap_here ("");
1124 gdb_flush (gdb_stdout);
c906108c 1125 }
c906108c 1126 }
6bf667bb 1127 syms_from_objfile (objfile, addrs, add_flags);
c906108c
SS
1128
1129 /* We now have at least a partial symbol table. Check to see if the
1130 user requested that all symbols be read on initial access via either
1131 the gdb startup command line or on a per symbol file basis. Expand
c378eb4e 1132 all partial symbol tables for this objfile if so. */
c906108c 1133
9291a0cd 1134 if ((flags & OBJF_READNOW))
c906108c 1135 {
b11896a5 1136 if (should_print)
c906108c 1137 {
a3f17187 1138 printf_unfiltered (_("expanding to full symbols..."));
c906108c
SS
1139 wrap_here ("");
1140 gdb_flush (gdb_stdout);
1141 }
1142
ccefe4c4
TT
1143 if (objfile->sf)
1144 objfile->sf->qf->expand_all_symtabs (objfile);
c906108c
SS
1145 }
1146
b11896a5 1147 if (should_print && !objfile_has_symbols (objfile))
cb3c37b2
JB
1148 {
1149 wrap_here ("");
55333a84 1150 printf_unfiltered (_("(no debugging symbols found)..."));
cb3c37b2
JB
1151 wrap_here ("");
1152 }
1153
b11896a5 1154 if (should_print)
c906108c 1155 {
769d7dc4
AC
1156 if (deprecated_post_add_symbol_hook)
1157 deprecated_post_add_symbol_hook ();
c906108c 1158 else
55333a84 1159 printf_unfiltered (_("done.\n"));
c906108c
SS
1160 }
1161
481d0f41
JB
1162 /* We print some messages regardless of whether 'from_tty ||
1163 info_verbose' is true, so make sure they go out at the right
1164 time. */
1165 gdb_flush (gdb_stdout);
1166
109f874e 1167 if (objfile->sf == NULL)
8caee43b
PP
1168 {
1169 observer_notify_new_objfile (objfile);
c378eb4e 1170 return objfile; /* No symbols. */
8caee43b 1171 }
109f874e 1172
7eedccfa 1173 new_symfile_objfile (objfile, add_flags);
c906108c 1174
06d3b283 1175 observer_notify_new_objfile (objfile);
c906108c 1176
ce7d4522 1177 bfd_cache_close_all ();
c906108c
SS
1178 return (objfile);
1179}
1180
24ba069a
JK
1181/* Add BFD as a separate debug file for OBJFILE. For NAME description
1182 see allocate_objfile's definition. */
9cce227f
TG
1183
1184void
24ba069a
JK
1185symbol_file_add_separate (bfd *bfd, const char *name, int symfile_flags,
1186 struct objfile *objfile)
9cce227f 1187{
15d123c9 1188 struct objfile *new_objfile;
089b4803
TG
1189 struct section_addr_info *sap;
1190 struct cleanup *my_cleanup;
1191
1192 /* Create section_addr_info. We can't directly use offsets from OBJFILE
1193 because sections of BFD may not match sections of OBJFILE and because
1194 vma may have been modified by tools such as prelink. */
1195 sap = build_section_addr_info_from_objfile (objfile);
1196 my_cleanup = make_cleanup_free_section_addr_info (sap);
9cce227f 1197
6bf667bb 1198 new_objfile = symbol_file_add_with_addrs
24ba069a 1199 (bfd, name, symfile_flags, sap,
9cce227f 1200 objfile->flags & (OBJF_REORDERED | OBJF_SHARED | OBJF_READNOW
63524580
JK
1201 | OBJF_USERLOADED),
1202 objfile);
089b4803
TG
1203
1204 do_cleanups (my_cleanup);
9cce227f 1205}
7904e09f 1206
eb4556d7
JB
1207/* Process the symbol file ABFD, as either the main file or as a
1208 dynamically loaded file.
6bf667bb 1209 See symbol_file_add_with_addrs's comments for details. */
3b7bacac 1210
eb4556d7 1211struct objfile *
24ba069a 1212symbol_file_add_from_bfd (bfd *abfd, const char *name, int add_flags,
eb4556d7 1213 struct section_addr_info *addrs,
63524580 1214 int flags, struct objfile *parent)
eb4556d7 1215{
24ba069a
JK
1216 return symbol_file_add_with_addrs (abfd, name, add_flags, addrs, flags,
1217 parent);
eb4556d7
JB
1218}
1219
7904e09f 1220/* Process a symbol file, as either the main file or as a dynamically
6bf667bb 1221 loaded file. See symbol_file_add_with_addrs's comments for details. */
3b7bacac 1222
7904e09f 1223struct objfile *
69150c3d
JK
1224symbol_file_add (const char *name, int add_flags,
1225 struct section_addr_info *addrs, int flags)
7904e09f 1226{
8ac244b4
TT
1227 bfd *bfd = symfile_bfd_open (name);
1228 struct cleanup *cleanup = make_cleanup_bfd_unref (bfd);
1229 struct objfile *objf;
1230
24ba069a 1231 objf = symbol_file_add_from_bfd (bfd, name, add_flags, addrs, flags, NULL);
8ac244b4
TT
1232 do_cleanups (cleanup);
1233 return objf;
7904e09f
JB
1234}
1235
d7db6da9
FN
1236/* Call symbol_file_add() with default values and update whatever is
1237 affected by the loading of a new main().
1238 Used when the file is supplied in the gdb command line
1239 and by some targets with special loading requirements.
1240 The auxiliary function, symbol_file_add_main_1(), has the flags
1241 argument for the switches that can only be specified in the symbol_file
1242 command itself. */
5417f6dc 1243
1adeb98a 1244void
69150c3d 1245symbol_file_add_main (const char *args, int from_tty)
1adeb98a 1246{
d7db6da9
FN
1247 symbol_file_add_main_1 (args, from_tty, 0);
1248}
1249
1250static void
69150c3d 1251symbol_file_add_main_1 (const char *args, int from_tty, int flags)
d7db6da9 1252{
7dcd53a0
TT
1253 const int add_flags = (current_inferior ()->symfile_flags
1254 | SYMFILE_MAINLINE | (from_tty ? SYMFILE_VERBOSE : 0));
1255
7eedccfa 1256 symbol_file_add (args, add_flags, NULL, flags);
d7db6da9 1257
d7db6da9
FN
1258 /* Getting new symbols may change our opinion about
1259 what is frameless. */
1260 reinit_frame_cache ();
1261
7dcd53a0
TT
1262 if ((flags & SYMFILE_NO_READ) == 0)
1263 set_initial_language ();
1adeb98a
FN
1264}
1265
1266void
1267symbol_file_clear (int from_tty)
1268{
1269 if ((have_full_symbols () || have_partial_symbols ())
1270 && from_tty
0430b0d6
AS
1271 && (symfile_objfile
1272 ? !query (_("Discard symbol table from `%s'? "),
4262abfb 1273 objfile_name (symfile_objfile))
0430b0d6 1274 : !query (_("Discard symbol table? "))))
8a3fe4f8 1275 error (_("Not confirmed."));
1adeb98a 1276
0133421a
JK
1277 /* solib descriptors may have handles to objfiles. Wipe them before their
1278 objfiles get stale by free_all_objfiles. */
d10c338d
DE
1279 no_shared_libraries (NULL, from_tty);
1280
0133421a
JK
1281 free_all_objfiles ();
1282
adb7f338 1283 gdb_assert (symfile_objfile == NULL);
d10c338d
DE
1284 if (from_tty)
1285 printf_unfiltered (_("No symbol file now.\n"));
1adeb98a
FN
1286}
1287
5b5d99cf 1288static int
287ccc17 1289separate_debug_file_exists (const char *name, unsigned long crc,
32a0e547 1290 struct objfile *parent_objfile)
5b5d99cf 1291{
904578ed
JK
1292 unsigned long file_crc;
1293 int file_crc_p;
f1838a98 1294 bfd *abfd;
32a0e547 1295 struct stat parent_stat, abfd_stat;
904578ed 1296 int verified_as_different;
32a0e547
JK
1297
1298 /* Find a separate debug info file as if symbols would be present in
1299 PARENT_OBJFILE itself this function would not be called. .gnu_debuglink
1300 section can contain just the basename of PARENT_OBJFILE without any
1301 ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
c378eb4e 1302 the separate debug infos with the same basename can exist. */
32a0e547 1303
4262abfb 1304 if (filename_cmp (name, objfile_name (parent_objfile)) == 0)
32a0e547 1305 return 0;
5b5d99cf 1306
08d2cd74 1307 abfd = gdb_bfd_open_maybe_remote (name);
f1838a98
UW
1308
1309 if (!abfd)
5b5d99cf
JB
1310 return 0;
1311
0ba1096a 1312 /* Verify symlinks were not the cause of filename_cmp name difference above.
32a0e547
JK
1313
1314 Some operating systems, e.g. Windows, do not provide a meaningful
1315 st_ino; they always set it to zero. (Windows does provide a
1316 meaningful st_dev.) Do not indicate a duplicate library in that
1317 case. While there is no guarantee that a system that provides
1318 meaningful inode numbers will never set st_ino to zero, this is
1319 merely an optimization, so we do not need to worry about false
1320 negatives. */
1321
1322 if (bfd_stat (abfd, &abfd_stat) == 0
904578ed
JK
1323 && abfd_stat.st_ino != 0
1324 && bfd_stat (parent_objfile->obfd, &parent_stat) == 0)
32a0e547 1325 {
904578ed
JK
1326 if (abfd_stat.st_dev == parent_stat.st_dev
1327 && abfd_stat.st_ino == parent_stat.st_ino)
1328 {
cbb099e8 1329 gdb_bfd_unref (abfd);
904578ed
JK
1330 return 0;
1331 }
1332 verified_as_different = 1;
32a0e547 1333 }
904578ed
JK
1334 else
1335 verified_as_different = 0;
32a0e547 1336
dccee2de 1337 file_crc_p = gdb_bfd_crc (abfd, &file_crc);
5b5d99cf 1338
cbb099e8 1339 gdb_bfd_unref (abfd);
5b5d99cf 1340
904578ed
JK
1341 if (!file_crc_p)
1342 return 0;
1343
287ccc17
JK
1344 if (crc != file_crc)
1345 {
dccee2de
TT
1346 unsigned long parent_crc;
1347
904578ed
JK
1348 /* If one (or both) the files are accessed for example the via "remote:"
1349 gdbserver way it does not support the bfd_stat operation. Verify
1350 whether those two files are not the same manually. */
1351
dccee2de 1352 if (!verified_as_different)
904578ed 1353 {
dccee2de 1354 if (!gdb_bfd_crc (parent_objfile->obfd, &parent_crc))
904578ed
JK
1355 return 0;
1356 }
1357
dccee2de 1358 if (verified_as_different || parent_crc != file_crc)
904578ed
JK
1359 warning (_("the debug information found in \"%s\""
1360 " does not match \"%s\" (CRC mismatch).\n"),
4262abfb 1361 name, objfile_name (parent_objfile));
904578ed 1362
287ccc17
JK
1363 return 0;
1364 }
1365
1366 return 1;
5b5d99cf
JB
1367}
1368
aa28a74e 1369char *debug_file_directory = NULL;
920d2a44
AC
1370static void
1371show_debug_file_directory (struct ui_file *file, int from_tty,
1372 struct cmd_list_element *c, const char *value)
1373{
3e43a32a
MS
1374 fprintf_filtered (file,
1375 _("The directory where separate debug "
1376 "symbols are searched for is \"%s\".\n"),
920d2a44
AC
1377 value);
1378}
5b5d99cf
JB
1379
1380#if ! defined (DEBUG_SUBDIRECTORY)
1381#define DEBUG_SUBDIRECTORY ".debug"
1382#endif
1383
1db33378
PP
1384/* Find a separate debuginfo file for OBJFILE, using DIR as the directory
1385 where the original file resides (may not be the same as
1386 dirname(objfile->name) due to symlinks), and DEBUGLINK as the file we are
7edbb660
DE
1387 looking for. CANON_DIR is the "realpath" form of DIR.
1388 DIR must contain a trailing '/'.
1389 Returns the path of the file with separate debug info, of NULL. */
1db33378
PP
1390
1391static char *
1392find_separate_debug_file (const char *dir,
1393 const char *canon_dir,
1394 const char *debuglink,
1395 unsigned long crc32, struct objfile *objfile)
9cce227f 1396{
1db33378
PP
1397 char *debugdir;
1398 char *debugfile;
9cce227f 1399 int i;
e4ab2fad
JK
1400 VEC (char_ptr) *debugdir_vec;
1401 struct cleanup *back_to;
1402 int ix;
5b5d99cf 1403
1db33378 1404 /* Set I to max (strlen (canon_dir), strlen (dir)). */
1ffa32ee 1405 i = strlen (dir);
1db33378
PP
1406 if (canon_dir != NULL && strlen (canon_dir) > i)
1407 i = strlen (canon_dir);
1ffa32ee 1408
25522fae
JK
1409 debugfile = xmalloc (strlen (debug_file_directory) + 1
1410 + i
1411 + strlen (DEBUG_SUBDIRECTORY)
1412 + strlen ("/")
1db33378 1413 + strlen (debuglink)
25522fae 1414 + 1);
5b5d99cf
JB
1415
1416 /* First try in the same directory as the original file. */
1417 strcpy (debugfile, dir);
1db33378 1418 strcat (debugfile, debuglink);
5b5d99cf 1419
32a0e547 1420 if (separate_debug_file_exists (debugfile, crc32, objfile))
1db33378 1421 return debugfile;
5417f6dc 1422
5b5d99cf
JB
1423 /* Then try in the subdirectory named DEBUG_SUBDIRECTORY. */
1424 strcpy (debugfile, dir);
1425 strcat (debugfile, DEBUG_SUBDIRECTORY);
1426 strcat (debugfile, "/");
1db33378 1427 strcat (debugfile, debuglink);
5b5d99cf 1428
32a0e547 1429 if (separate_debug_file_exists (debugfile, crc32, objfile))
1db33378 1430 return debugfile;
5417f6dc 1431
24ddea62 1432 /* Then try in the global debugfile directories.
f888f159 1433
24ddea62
JK
1434 Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
1435 cause "/..." lookups. */
5417f6dc 1436
e4ab2fad
JK
1437 debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
1438 back_to = make_cleanup_free_char_ptr_vec (debugdir_vec);
24ddea62 1439
e4ab2fad
JK
1440 for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
1441 {
1442 strcpy (debugfile, debugdir);
aa28a74e 1443 strcat (debugfile, "/");
24ddea62 1444 strcat (debugfile, dir);
1db33378 1445 strcat (debugfile, debuglink);
aa28a74e 1446
32a0e547 1447 if (separate_debug_file_exists (debugfile, crc32, objfile))
a991ac28
TT
1448 {
1449 do_cleanups (back_to);
1450 return debugfile;
1451 }
24ddea62
JK
1452
1453 /* If the file is in the sysroot, try using its base path in the
1454 global debugfile directory. */
1db33378
PP
1455 if (canon_dir != NULL
1456 && filename_ncmp (canon_dir, gdb_sysroot,
0ba1096a 1457 strlen (gdb_sysroot)) == 0
1db33378 1458 && IS_DIR_SEPARATOR (canon_dir[strlen (gdb_sysroot)]))
24ddea62 1459 {
e4ab2fad 1460 strcpy (debugfile, debugdir);
1db33378 1461 strcat (debugfile, canon_dir + strlen (gdb_sysroot));
24ddea62 1462 strcat (debugfile, "/");
1db33378 1463 strcat (debugfile, debuglink);
24ddea62 1464
32a0e547 1465 if (separate_debug_file_exists (debugfile, crc32, objfile))
a991ac28
TT
1466 {
1467 do_cleanups (back_to);
1468 return debugfile;
1469 }
24ddea62 1470 }
aa28a74e 1471 }
f888f159 1472
e4ab2fad 1473 do_cleanups (back_to);
25522fae 1474 xfree (debugfile);
1db33378
PP
1475 return NULL;
1476}
1477
7edbb660 1478/* Modify PATH to contain only "[/]directory/" part of PATH.
1db33378
PP
1479 If there were no directory separators in PATH, PATH will be empty
1480 string on return. */
1481
1482static void
1483terminate_after_last_dir_separator (char *path)
1484{
1485 int i;
1486
1487 /* Strip off the final filename part, leaving the directory name,
1488 followed by a slash. The directory can be relative or absolute. */
1489 for (i = strlen(path) - 1; i >= 0; i--)
1490 if (IS_DIR_SEPARATOR (path[i]))
1491 break;
1492
1493 /* If I is -1 then no directory is present there and DIR will be "". */
1494 path[i + 1] = '\0';
1495}
1496
1497/* Find separate debuginfo for OBJFILE (using .gnu_debuglink section).
1498 Returns pathname, or NULL. */
1499
1500char *
1501find_separate_debug_file_by_debuglink (struct objfile *objfile)
1502{
1503 char *debuglink;
1504 char *dir, *canon_dir;
1505 char *debugfile;
1506 unsigned long crc32;
1507 struct cleanup *cleanups;
1508
cc0ea93c 1509 debuglink = bfd_get_debug_link_info (objfile->obfd, &crc32);
1db33378
PP
1510
1511 if (debuglink == NULL)
1512 {
1513 /* There's no separate debug info, hence there's no way we could
1514 load it => no warning. */
1515 return NULL;
1516 }
1517
71bdabee 1518 cleanups = make_cleanup (xfree, debuglink);
4262abfb 1519 dir = xstrdup (objfile_name (objfile));
71bdabee 1520 make_cleanup (xfree, dir);
1db33378
PP
1521 terminate_after_last_dir_separator (dir);
1522 canon_dir = lrealpath (dir);
1523
1524 debugfile = find_separate_debug_file (dir, canon_dir, debuglink,
1525 crc32, objfile);
1526 xfree (canon_dir);
1527
1528 if (debugfile == NULL)
1529 {
1530#ifdef HAVE_LSTAT
1531 /* For PR gdb/9538, try again with realpath (if different from the
1532 original). */
1533
1534 struct stat st_buf;
1535
4262abfb
JK
1536 if (lstat (objfile_name (objfile), &st_buf) == 0
1537 && S_ISLNK (st_buf.st_mode))
1db33378
PP
1538 {
1539 char *symlink_dir;
1540
4262abfb 1541 symlink_dir = lrealpath (objfile_name (objfile));
1db33378
PP
1542 if (symlink_dir != NULL)
1543 {
1544 make_cleanup (xfree, symlink_dir);
1545 terminate_after_last_dir_separator (symlink_dir);
1546 if (strcmp (dir, symlink_dir) != 0)
1547 {
1548 /* Different directory, so try using it. */
1549 debugfile = find_separate_debug_file (symlink_dir,
1550 symlink_dir,
1551 debuglink,
1552 crc32,
1553 objfile);
1554 }
1555 }
1556 }
1557#endif /* HAVE_LSTAT */
1558 }
aa28a74e 1559
1db33378 1560 do_cleanups (cleanups);
25522fae 1561 return debugfile;
5b5d99cf
JB
1562}
1563
c906108c
SS
1564/* This is the symbol-file command. Read the file, analyze its
1565 symbols, and add a struct symtab to a symtab list. The syntax of
cb2f3a29
MK
1566 the command is rather bizarre:
1567
1568 1. The function buildargv implements various quoting conventions
1569 which are undocumented and have little or nothing in common with
1570 the way things are quoted (or not quoted) elsewhere in GDB.
1571
1572 2. Options are used, which are not generally used in GDB (perhaps
1573 "set mapped on", "set readnow on" would be better)
1574
1575 3. The order of options matters, which is contrary to GNU
c906108c
SS
1576 conventions (because it is confusing and inconvenient). */
1577
1578void
fba45db2 1579symbol_file_command (char *args, int from_tty)
c906108c 1580{
c906108c
SS
1581 dont_repeat ();
1582
1583 if (args == NULL)
1584 {
1adeb98a 1585 symbol_file_clear (from_tty);
c906108c
SS
1586 }
1587 else
1588 {
d1a41061 1589 char **argv = gdb_buildargv (args);
cb2f3a29
MK
1590 int flags = OBJF_USERLOADED;
1591 struct cleanup *cleanups;
1592 char *name = NULL;
1593
7a292a7a 1594 cleanups = make_cleanup_freeargv (argv);
c906108c
SS
1595 while (*argv != NULL)
1596 {
78a4a9b9
AC
1597 if (strcmp (*argv, "-readnow") == 0)
1598 flags |= OBJF_READNOW;
1599 else if (**argv == '-')
8a3fe4f8 1600 error (_("unknown option `%s'"), *argv);
78a4a9b9
AC
1601 else
1602 {
cb2f3a29 1603 symbol_file_add_main_1 (*argv, from_tty, flags);
78a4a9b9 1604 name = *argv;
78a4a9b9 1605 }
cb2f3a29 1606
c906108c
SS
1607 argv++;
1608 }
1609
1610 if (name == NULL)
cb2f3a29
MK
1611 error (_("no symbol file name was specified"));
1612
c906108c
SS
1613 do_cleanups (cleanups);
1614 }
1615}
1616
1617/* Set the initial language.
1618
cb2f3a29
MK
1619 FIXME: A better solution would be to record the language in the
1620 psymtab when reading partial symbols, and then use it (if known) to
1621 set the language. This would be a win for formats that encode the
1622 language in an easily discoverable place, such as DWARF. For
1623 stabs, we can jump through hoops looking for specially named
1624 symbols or try to intuit the language from the specific type of
1625 stabs we find, but we can't do that until later when we read in
1626 full symbols. */
c906108c 1627
8b60591b 1628void
fba45db2 1629set_initial_language (void)
c906108c 1630{
c5aa993b 1631 enum language lang = language_unknown;
c906108c 1632
01f8c46d
JK
1633 if (language_of_main != language_unknown)
1634 lang = language_of_main;
1635 else
1636 {
bf6d8a91
TT
1637 char *name = main_name ();
1638 struct symbol *sym = lookup_symbol (name, NULL, VAR_DOMAIN, NULL);
f888f159 1639
bf6d8a91
TT
1640 if (sym != NULL)
1641 lang = SYMBOL_LANGUAGE (sym);
01f8c46d 1642 }
cb2f3a29 1643
ccefe4c4
TT
1644 if (lang == language_unknown)
1645 {
1646 /* Make C the default language */
1647 lang = language_c;
c906108c 1648 }
ccefe4c4
TT
1649
1650 set_language (lang);
1651 expected_language = current_language; /* Don't warn the user. */
c906108c
SS
1652}
1653
874f5765 1654/* If NAME is a remote name open the file using remote protocol, otherwise
cbb099e8
TT
1655 open it normally. Returns a new reference to the BFD. On error,
1656 returns NULL with the BFD error set. */
874f5765
TG
1657
1658bfd *
08d2cd74 1659gdb_bfd_open_maybe_remote (const char *name)
874f5765 1660{
520b0001
TT
1661 bfd *result;
1662
874f5765 1663 if (remote_filename_p (name))
520b0001 1664 result = remote_bfd_open (name, gnutarget);
874f5765 1665 else
1c00ec6b 1666 result = gdb_bfd_open (name, gnutarget, -1);
520b0001 1667
520b0001 1668 return result;
874f5765
TG
1669}
1670
cb2f3a29
MK
1671/* Open the file specified by NAME and hand it off to BFD for
1672 preliminary analysis. Return a newly initialized bfd *, which
1673 includes a newly malloc'd` copy of NAME (tilde-expanded and made
1674 absolute). In case of trouble, error() is called. */
c906108c
SS
1675
1676bfd *
69150c3d 1677symfile_bfd_open (const char *cname)
c906108c
SS
1678{
1679 bfd *sym_bfd;
1680 int desc;
69150c3d 1681 char *name, *absolute_name;
faab9922 1682 struct cleanup *back_to;
c906108c 1683
69150c3d 1684 if (remote_filename_p (cname))
f1838a98 1685 {
69150c3d 1686 sym_bfd = remote_bfd_open (cname, gnutarget);
f1838a98 1687 if (!sym_bfd)
69150c3d 1688 error (_("`%s': can't open to read symbols: %s."), cname,
a4453b7e 1689 bfd_errmsg (bfd_get_error ()));
f1838a98
UW
1690
1691 if (!bfd_check_format (sym_bfd, bfd_object))
1692 {
f9a062ff 1693 make_cleanup_bfd_unref (sym_bfd);
69150c3d 1694 error (_("`%s': can't read symbols: %s."), cname,
f1838a98
UW
1695 bfd_errmsg (bfd_get_error ()));
1696 }
1697
1698 return sym_bfd;
1699 }
1700
69150c3d 1701 name = tilde_expand (cname); /* Returns 1st new malloc'd copy. */
c906108c
SS
1702
1703 /* Look down path for it, allocate 2nd new malloc'd copy. */
492c0ab7 1704 desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, name,
fbdebf46 1705 O_RDONLY | O_BINARY, &absolute_name);
608506ed 1706#if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
c906108c
SS
1707 if (desc < 0)
1708 {
1709 char *exename = alloca (strlen (name) + 5);
433759f7 1710
c906108c 1711 strcat (strcpy (exename, name), ".exe");
492c0ab7
JK
1712 desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
1713 exename, O_RDONLY | O_BINARY, &absolute_name);
c906108c
SS
1714 }
1715#endif
1716 if (desc < 0)
1717 {
b8c9b27d 1718 make_cleanup (xfree, name);
c906108c
SS
1719 perror_with_name (name);
1720 }
cb2f3a29 1721
cb2f3a29
MK
1722 xfree (name);
1723 name = absolute_name;
faab9922 1724 back_to = make_cleanup (xfree, name);
c906108c 1725
1c00ec6b 1726 sym_bfd = gdb_bfd_open (name, gnutarget, desc);
c906108c 1727 if (!sym_bfd)
faab9922
JK
1728 error (_("`%s': can't open to read symbols: %s."), name,
1729 bfd_errmsg (bfd_get_error ()));
549c1eea 1730 bfd_set_cacheable (sym_bfd, 1);
c906108c
SS
1731
1732 if (!bfd_check_format (sym_bfd, bfd_object))
1733 {
f9a062ff 1734 make_cleanup_bfd_unref (sym_bfd);
f1838a98 1735 error (_("`%s': can't read symbols: %s."), name,
c906108c
SS
1736 bfd_errmsg (bfd_get_error ()));
1737 }
cb2f3a29 1738
faab9922
JK
1739 do_cleanups (back_to);
1740
cb2f3a29 1741 return sym_bfd;
c906108c
SS
1742}
1743
cb2f3a29
MK
1744/* Return the section index for SECTION_NAME on OBJFILE. Return -1 if
1745 the section was not found. */
1746
0e931cf0
JB
1747int
1748get_section_index (struct objfile *objfile, char *section_name)
1749{
1750 asection *sect = bfd_get_section_by_name (objfile->obfd, section_name);
cb2f3a29 1751
0e931cf0
JB
1752 if (sect)
1753 return sect->index;
1754 else
1755 return -1;
1756}
1757
c256e171
DE
1758/* Link SF into the global symtab_fns list.
1759 FLAVOUR is the file format that SF handles.
1760 Called on startup by the _initialize routine in each object file format
1761 reader, to register information about each format the reader is prepared
1762 to handle. */
c906108c
SS
1763
1764void
c256e171 1765add_symtab_fns (enum bfd_flavour flavour, const struct sym_fns *sf)
c906108c 1766{
c256e171
DE
1767 registered_sym_fns fns = { flavour, sf };
1768
1769 VEC_safe_push (registered_sym_fns, symtab_fns, &fns);
c906108c
SS
1770}
1771
cb2f3a29
MK
1772/* Initialize OBJFILE to read symbols from its associated BFD. It
1773 either returns or calls error(). The result is an initialized
1774 struct sym_fns in the objfile structure, that contains cached
1775 information about the symbol file. */
c906108c 1776
00b5771c 1777static const struct sym_fns *
31d99776 1778find_sym_fns (bfd *abfd)
c906108c 1779{
c256e171 1780 registered_sym_fns *rsf;
31d99776 1781 enum bfd_flavour our_flavour = bfd_get_flavour (abfd);
00b5771c 1782 int i;
c906108c 1783
75245b24
MS
1784 if (our_flavour == bfd_target_srec_flavour
1785 || our_flavour == bfd_target_ihex_flavour
1786 || our_flavour == bfd_target_tekhex_flavour)
31d99776 1787 return NULL; /* No symbols. */
75245b24 1788
c256e171
DE
1789 for (i = 0; VEC_iterate (registered_sym_fns, symtab_fns, i, rsf); ++i)
1790 if (our_flavour == rsf->sym_flavour)
1791 return rsf->sym_fns;
cb2f3a29 1792
8a3fe4f8 1793 error (_("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown."),
31d99776 1794 bfd_get_target (abfd));
c906108c
SS
1795}
1796\f
cb2f3a29 1797
c906108c
SS
1798/* This function runs the load command of our current target. */
1799
1800static void
fba45db2 1801load_command (char *arg, int from_tty)
c906108c 1802{
5b3fca71
TT
1803 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
1804
e5cc9f32
JB
1805 dont_repeat ();
1806
4487aabf
PA
1807 /* The user might be reloading because the binary has changed. Take
1808 this opportunity to check. */
1809 reopen_exec_file ();
1810 reread_symbols ();
1811
c906108c 1812 if (arg == NULL)
1986bccd
AS
1813 {
1814 char *parg;
1815 int count = 0;
1816
1817 parg = arg = get_exec_file (1);
1818
1819 /* Count how many \ " ' tab space there are in the name. */
1820 while ((parg = strpbrk (parg, "\\\"'\t ")))
1821 {
1822 parg++;
1823 count++;
1824 }
1825
1826 if (count)
1827 {
1828 /* We need to quote this string so buildargv can pull it apart. */
1829 char *temp = xmalloc (strlen (arg) + count + 1 );
1830 char *ptemp = temp;
1831 char *prev;
1832
1833 make_cleanup (xfree, temp);
1834
1835 prev = parg = arg;
1836 while ((parg = strpbrk (parg, "\\\"'\t ")))
1837 {
1838 strncpy (ptemp, prev, parg - prev);
1839 ptemp += parg - prev;
1840 prev = parg++;
1841 *ptemp++ = '\\';
1842 }
1843 strcpy (ptemp, prev);
1844
1845 arg = temp;
1846 }
1847 }
1848
c906108c 1849 target_load (arg, from_tty);
2889e661
JB
1850
1851 /* After re-loading the executable, we don't really know which
1852 overlays are mapped any more. */
1853 overlay_cache_invalid = 1;
5b3fca71
TT
1854
1855 do_cleanups (cleanup);
c906108c
SS
1856}
1857
1858/* This version of "load" should be usable for any target. Currently
1859 it is just used for remote targets, not inftarg.c or core files,
1860 on the theory that only in that case is it useful.
1861
1862 Avoiding xmodem and the like seems like a win (a) because we don't have
1863 to worry about finding it, and (b) On VMS, fork() is very slow and so
1864 we don't want to run a subprocess. On the other hand, I'm not sure how
1865 performance compares. */
917317f4 1866
917317f4
JM
1867static int validate_download = 0;
1868
e4f9b4d5
MS
1869/* Callback service function for generic_load (bfd_map_over_sections). */
1870
1871static void
1872add_section_size_callback (bfd *abfd, asection *asec, void *data)
1873{
1874 bfd_size_type *sum = data;
1875
2c500098 1876 *sum += bfd_get_section_size (asec);
e4f9b4d5
MS
1877}
1878
1879/* Opaque data for load_section_callback. */
1880struct load_section_data {
f698ca8e 1881 CORE_ADDR load_offset;
a76d924d
DJ
1882 struct load_progress_data *progress_data;
1883 VEC(memory_write_request_s) *requests;
1884};
1885
1886/* Opaque data for load_progress. */
1887struct load_progress_data {
1888 /* Cumulative data. */
e4f9b4d5
MS
1889 unsigned long write_count;
1890 unsigned long data_count;
1891 bfd_size_type total_size;
a76d924d
DJ
1892};
1893
1894/* Opaque data for load_progress for a single section. */
1895struct load_progress_section_data {
1896 struct load_progress_data *cumulative;
cf7a04e8 1897
a76d924d 1898 /* Per-section data. */
cf7a04e8
DJ
1899 const char *section_name;
1900 ULONGEST section_sent;
1901 ULONGEST section_size;
1902 CORE_ADDR lma;
1903 gdb_byte *buffer;
e4f9b4d5
MS
1904};
1905
a76d924d 1906/* Target write callback routine for progress reporting. */
cf7a04e8
DJ
1907
1908static void
1909load_progress (ULONGEST bytes, void *untyped_arg)
1910{
a76d924d
DJ
1911 struct load_progress_section_data *args = untyped_arg;
1912 struct load_progress_data *totals;
1913
1914 if (args == NULL)
1915 /* Writing padding data. No easy way to get at the cumulative
1916 stats, so just ignore this. */
1917 return;
1918
1919 totals = args->cumulative;
1920
1921 if (bytes == 0 && args->section_sent == 0)
1922 {
1923 /* The write is just starting. Let the user know we've started
1924 this section. */
79a45e25 1925 ui_out_message (current_uiout, 0, "Loading section %s, size %s lma %s\n",
5af949e3 1926 args->section_name, hex_string (args->section_size),
f5656ead 1927 paddress (target_gdbarch (), args->lma));
a76d924d
DJ
1928 return;
1929 }
cf7a04e8
DJ
1930
1931 if (validate_download)
1932 {
1933 /* Broken memories and broken monitors manifest themselves here
1934 when bring new computers to life. This doubles already slow
1935 downloads. */
1936 /* NOTE: cagney/1999-10-18: A more efficient implementation
1937 might add a verify_memory() method to the target vector and
1938 then use that. remote.c could implement that method using
1939 the ``qCRC'' packet. */
1940 gdb_byte *check = xmalloc (bytes);
1941 struct cleanup *verify_cleanups = make_cleanup (xfree, check);
1942
1943 if (target_read_memory (args->lma, check, bytes) != 0)
5af949e3 1944 error (_("Download verify read failed at %s"),
f5656ead 1945 paddress (target_gdbarch (), args->lma));
cf7a04e8 1946 if (memcmp (args->buffer, check, bytes) != 0)
5af949e3 1947 error (_("Download verify compare failed at %s"),
f5656ead 1948 paddress (target_gdbarch (), args->lma));
cf7a04e8
DJ
1949 do_cleanups (verify_cleanups);
1950 }
a76d924d 1951 totals->data_count += bytes;
cf7a04e8
DJ
1952 args->lma += bytes;
1953 args->buffer += bytes;
a76d924d 1954 totals->write_count += 1;
cf7a04e8 1955 args->section_sent += bytes;
522002f9 1956 if (check_quit_flag ()
cf7a04e8
DJ
1957 || (deprecated_ui_load_progress_hook != NULL
1958 && deprecated_ui_load_progress_hook (args->section_name,
1959 args->section_sent)))
1960 error (_("Canceled the download"));
1961
1962 if (deprecated_show_load_progress != NULL)
1963 deprecated_show_load_progress (args->section_name,
1964 args->section_sent,
1965 args->section_size,
a76d924d
DJ
1966 totals->data_count,
1967 totals->total_size);
cf7a04e8
DJ
1968}
1969
e4f9b4d5
MS
1970/* Callback service function for generic_load (bfd_map_over_sections). */
1971
1972static void
1973load_section_callback (bfd *abfd, asection *asec, void *data)
1974{
a76d924d 1975 struct memory_write_request *new_request;
e4f9b4d5 1976 struct load_section_data *args = data;
a76d924d 1977 struct load_progress_section_data *section_data;
cf7a04e8
DJ
1978 bfd_size_type size = bfd_get_section_size (asec);
1979 gdb_byte *buffer;
cf7a04e8 1980 const char *sect_name = bfd_get_section_name (abfd, asec);
e4f9b4d5 1981
cf7a04e8
DJ
1982 if ((bfd_get_section_flags (abfd, asec) & SEC_LOAD) == 0)
1983 return;
e4f9b4d5 1984
cf7a04e8
DJ
1985 if (size == 0)
1986 return;
e4f9b4d5 1987
a76d924d
DJ
1988 new_request = VEC_safe_push (memory_write_request_s,
1989 args->requests, NULL);
1990 memset (new_request, 0, sizeof (struct memory_write_request));
1991 section_data = xcalloc (1, sizeof (struct load_progress_section_data));
1992 new_request->begin = bfd_section_lma (abfd, asec) + args->load_offset;
3e43a32a
MS
1993 new_request->end = new_request->begin + size; /* FIXME Should size
1994 be in instead? */
a76d924d
DJ
1995 new_request->data = xmalloc (size);
1996 new_request->baton = section_data;
cf7a04e8 1997
a76d924d 1998 buffer = new_request->data;
cf7a04e8 1999
a76d924d
DJ
2000 section_data->cumulative = args->progress_data;
2001 section_data->section_name = sect_name;
2002 section_data->section_size = size;
2003 section_data->lma = new_request->begin;
2004 section_data->buffer = buffer;
cf7a04e8
DJ
2005
2006 bfd_get_section_contents (abfd, asec, buffer, 0, size);
a76d924d
DJ
2007}
2008
2009/* Clean up an entire memory request vector, including load
2010 data and progress records. */
cf7a04e8 2011
a76d924d
DJ
2012static void
2013clear_memory_write_data (void *arg)
2014{
2015 VEC(memory_write_request_s) **vec_p = arg;
2016 VEC(memory_write_request_s) *vec = *vec_p;
2017 int i;
2018 struct memory_write_request *mr;
cf7a04e8 2019
a76d924d
DJ
2020 for (i = 0; VEC_iterate (memory_write_request_s, vec, i, mr); ++i)
2021 {
2022 xfree (mr->data);
2023 xfree (mr->baton);
2024 }
2025 VEC_free (memory_write_request_s, vec);
e4f9b4d5
MS
2026}
2027
c906108c 2028void
917317f4 2029generic_load (char *args, int from_tty)
c906108c 2030{
c906108c 2031 bfd *loadfile_bfd;
2b71414d 2032 struct timeval start_time, end_time;
917317f4 2033 char *filename;
1986bccd 2034 struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0);
e4f9b4d5 2035 struct load_section_data cbdata;
a76d924d 2036 struct load_progress_data total_progress;
79a45e25 2037 struct ui_out *uiout = current_uiout;
a76d924d 2038
e4f9b4d5 2039 CORE_ADDR entry;
1986bccd 2040 char **argv;
e4f9b4d5 2041
a76d924d
DJ
2042 memset (&cbdata, 0, sizeof (cbdata));
2043 memset (&total_progress, 0, sizeof (total_progress));
2044 cbdata.progress_data = &total_progress;
2045
2046 make_cleanup (clear_memory_write_data, &cbdata.requests);
917317f4 2047
d1a41061
PP
2048 if (args == NULL)
2049 error_no_arg (_("file to load"));
1986bccd 2050
d1a41061 2051 argv = gdb_buildargv (args);
1986bccd
AS
2052 make_cleanup_freeargv (argv);
2053
2054 filename = tilde_expand (argv[0]);
2055 make_cleanup (xfree, filename);
2056
2057 if (argv[1] != NULL)
917317f4 2058 {
f698ca8e 2059 const char *endptr;
ba5f2f8a 2060
f698ca8e 2061 cbdata.load_offset = strtoulst (argv[1], &endptr, 0);
1986bccd
AS
2062
2063 /* If the last word was not a valid number then
2064 treat it as a file name with spaces in. */
2065 if (argv[1] == endptr)
2066 error (_("Invalid download offset:%s."), argv[1]);
2067
2068 if (argv[2] != NULL)
2069 error (_("Too many parameters."));
917317f4 2070 }
c906108c 2071
c378eb4e 2072 /* Open the file for loading. */
1c00ec6b 2073 loadfile_bfd = gdb_bfd_open (filename, gnutarget, -1);
c906108c
SS
2074 if (loadfile_bfd == NULL)
2075 {
2076 perror_with_name (filename);
2077 return;
2078 }
917317f4 2079
f9a062ff 2080 make_cleanup_bfd_unref (loadfile_bfd);
c906108c 2081
c5aa993b 2082 if (!bfd_check_format (loadfile_bfd, bfd_object))
c906108c 2083 {
8a3fe4f8 2084 error (_("\"%s\" is not an object file: %s"), filename,
c906108c
SS
2085 bfd_errmsg (bfd_get_error ()));
2086 }
c5aa993b 2087
5417f6dc 2088 bfd_map_over_sections (loadfile_bfd, add_section_size_callback,
a76d924d
DJ
2089 (void *) &total_progress.total_size);
2090
2091 bfd_map_over_sections (loadfile_bfd, load_section_callback, &cbdata);
c2d11a7d 2092
2b71414d 2093 gettimeofday (&start_time, NULL);
c906108c 2094
a76d924d
DJ
2095 if (target_write_memory_blocks (cbdata.requests, flash_discard,
2096 load_progress) != 0)
2097 error (_("Load failed"));
c906108c 2098
2b71414d 2099 gettimeofday (&end_time, NULL);
ba5f2f8a 2100
e4f9b4d5 2101 entry = bfd_get_start_address (loadfile_bfd);
8c2b9656 2102 entry = gdbarch_addr_bits_remove (target_gdbarch (), entry);
e4f9b4d5 2103 ui_out_text (uiout, "Start address ");
f5656ead 2104 ui_out_field_fmt (uiout, "address", "%s", paddress (target_gdbarch (), entry));
e4f9b4d5 2105 ui_out_text (uiout, ", load size ");
a76d924d 2106 ui_out_field_fmt (uiout, "load-size", "%lu", total_progress.data_count);
e4f9b4d5 2107 ui_out_text (uiout, "\n");
e4f9b4d5
MS
2108 /* We were doing this in remote-mips.c, I suspect it is right
2109 for other targets too. */
fb14de7b 2110 regcache_write_pc (get_current_regcache (), entry);
c906108c 2111
38963c97
DJ
2112 /* Reset breakpoints, now that we have changed the load image. For
2113 instance, breakpoints may have been set (or reset, by
2114 post_create_inferior) while connected to the target but before we
2115 loaded the program. In that case, the prologue analyzer could
2116 have read instructions from the target to find the right
2117 breakpoint locations. Loading has changed the contents of that
2118 memory. */
2119
2120 breakpoint_re_set ();
2121
7ca9f392
AC
2122 /* FIXME: are we supposed to call symbol_file_add or not? According
2123 to a comment from remote-mips.c (where a call to symbol_file_add
2124 was commented out), making the call confuses GDB if more than one
2125 file is loaded in. Some targets do (e.g., remote-vx.c) but
b2fa5097 2126 others don't (or didn't - perhaps they have all been deleted). */
c906108c 2127
a76d924d
DJ
2128 print_transfer_performance (gdb_stdout, total_progress.data_count,
2129 total_progress.write_count,
2130 &start_time, &end_time);
c906108c
SS
2131
2132 do_cleanups (old_cleanups);
2133}
2134
c378eb4e 2135/* Report how fast the transfer went. */
c906108c 2136
917317f4 2137void
d9fcf2fb 2138print_transfer_performance (struct ui_file *stream,
917317f4
JM
2139 unsigned long data_count,
2140 unsigned long write_count,
2b71414d
DJ
2141 const struct timeval *start_time,
2142 const struct timeval *end_time)
917317f4 2143{
9f43d28c 2144 ULONGEST time_count;
79a45e25 2145 struct ui_out *uiout = current_uiout;
2b71414d
DJ
2146
2147 /* Compute the elapsed time in milliseconds, as a tradeoff between
2148 accuracy and overflow. */
2149 time_count = (end_time->tv_sec - start_time->tv_sec) * 1000;
2150 time_count += (end_time->tv_usec - start_time->tv_usec) / 1000;
2151
8b93c638
JM
2152 ui_out_text (uiout, "Transfer rate: ");
2153 if (time_count > 0)
2154 {
9f43d28c
DJ
2155 unsigned long rate = ((ULONGEST) data_count * 1000) / time_count;
2156
2157 if (ui_out_is_mi_like_p (uiout))
2158 {
2159 ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate * 8);
2160 ui_out_text (uiout, " bits/sec");
2161 }
2162 else if (rate < 1024)
2163 {
2164 ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate);
2165 ui_out_text (uiout, " bytes/sec");
2166 }
2167 else
2168 {
2169 ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate / 1024);
2170 ui_out_text (uiout, " KB/sec");
2171 }
8b93c638
JM
2172 }
2173 else
2174 {
ba5f2f8a 2175 ui_out_field_fmt (uiout, "transferred-bits", "%lu", (data_count * 8));
5417f6dc 2176 ui_out_text (uiout, " bits in <1 sec");
8b93c638
JM
2177 }
2178 if (write_count > 0)
2179 {
2180 ui_out_text (uiout, ", ");
ba5f2f8a 2181 ui_out_field_fmt (uiout, "write-rate", "%lu", data_count / write_count);
8b93c638
JM
2182 ui_out_text (uiout, " bytes/write");
2183 }
2184 ui_out_text (uiout, ".\n");
c906108c
SS
2185}
2186
2187/* This function allows the addition of incrementally linked object files.
2188 It does not modify any state in the target, only in the debugger. */
db162d44
EZ
2189/* Note: ezannoni 2000-04-13 This function/command used to have a
2190 special case syntax for the rombug target (Rombug is the boot
2191 monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the
2192 rombug case, the user doesn't need to supply a text address,
2193 instead a call to target_link() (in target.c) would supply the
c378eb4e 2194 value to use. We are now discontinuing this type of ad hoc syntax. */
c906108c 2195
c906108c 2196static void
fba45db2 2197add_symbol_file_command (char *args, int from_tty)
c906108c 2198{
5af949e3 2199 struct gdbarch *gdbarch = get_current_arch ();
db162d44 2200 char *filename = NULL;
2df3850c 2201 int flags = OBJF_USERLOADED;
c906108c 2202 char *arg;
db162d44 2203 int section_index = 0;
2acceee2
JM
2204 int argcnt = 0;
2205 int sec_num = 0;
2206 int i;
db162d44
EZ
2207 int expecting_sec_name = 0;
2208 int expecting_sec_addr = 0;
5b96932b 2209 char **argv;
76ad5e1e 2210 struct objfile *objf;
db162d44 2211
a39a16c4 2212 struct sect_opt
2acceee2 2213 {
2acceee2
JM
2214 char *name;
2215 char *value;
a39a16c4 2216 };
db162d44 2217
a39a16c4
MM
2218 struct section_addr_info *section_addrs;
2219 struct sect_opt *sect_opts = NULL;
2220 size_t num_sect_opts = 0;
3017564a 2221 struct cleanup *my_cleanups = make_cleanup (null_cleanup, NULL);
c5aa993b 2222
a39a16c4 2223 num_sect_opts = 16;
5417f6dc 2224 sect_opts = (struct sect_opt *) xmalloc (num_sect_opts
a39a16c4
MM
2225 * sizeof (struct sect_opt));
2226
c906108c
SS
2227 dont_repeat ();
2228
2229 if (args == NULL)
8a3fe4f8 2230 error (_("add-symbol-file takes a file name and an address"));
c906108c 2231
d1a41061 2232 argv = gdb_buildargv (args);
5b96932b 2233 make_cleanup_freeargv (argv);
db162d44 2234
5b96932b
AS
2235 for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
2236 {
c378eb4e 2237 /* Process the argument. */
db162d44 2238 if (argcnt == 0)
c906108c 2239 {
c378eb4e 2240 /* The first argument is the file name. */
db162d44 2241 filename = tilde_expand (arg);
3017564a 2242 make_cleanup (xfree, filename);
c906108c 2243 }
41dc8db8
MB
2244 else if (argcnt == 1)
2245 {
2246 /* The second argument is always the text address at which
2247 to load the program. */
2248 sect_opts[section_index].name = ".text";
2249 sect_opts[section_index].value = arg;
2250 if (++section_index >= num_sect_opts)
2251 {
2252 num_sect_opts *= 2;
2253 sect_opts = ((struct sect_opt *)
2254 xrealloc (sect_opts,
2255 num_sect_opts
2256 * sizeof (struct sect_opt)));
2257 }
2258 }
db162d44 2259 else
41dc8db8
MB
2260 {
2261 /* It's an option (starting with '-') or it's an argument
2262 to an option. */
41dc8db8
MB
2263 if (expecting_sec_name)
2264 {
2265 sect_opts[section_index].name = arg;
2266 expecting_sec_name = 0;
2267 }
2268 else if (expecting_sec_addr)
2269 {
2270 sect_opts[section_index].value = arg;
2271 expecting_sec_addr = 0;
2272 if (++section_index >= num_sect_opts)
2273 {
2274 num_sect_opts *= 2;
2275 sect_opts = ((struct sect_opt *)
2276 xrealloc (sect_opts,
2277 num_sect_opts
2278 * sizeof (struct sect_opt)));
2279 }
2280 }
2281 else if (strcmp (arg, "-readnow") == 0)
2282 flags |= OBJF_READNOW;
2283 else if (strcmp (arg, "-s") == 0)
2284 {
2285 expecting_sec_name = 1;
2286 expecting_sec_addr = 1;
2287 }
2288 else
2289 error (_("USAGE: add-symbol-file <filename> <textaddress>"
2290 " [-readnow] [-s <secname> <addr>]*"));
2291 }
c906108c 2292 }
c906108c 2293
927890d0
JB
2294 /* This command takes at least two arguments. The first one is a
2295 filename, and the second is the address where this file has been
2296 loaded. Abort now if this address hasn't been provided by the
2297 user. */
2298 if (section_index < 1)
2299 error (_("The address where %s has been loaded is missing"), filename);
2300
c378eb4e 2301 /* Print the prompt for the query below. And save the arguments into
db162d44
EZ
2302 a sect_addr_info structure to be passed around to other
2303 functions. We have to split this up into separate print
bb599908 2304 statements because hex_string returns a local static
c378eb4e 2305 string. */
5417f6dc 2306
a3f17187 2307 printf_unfiltered (_("add symbol table from file \"%s\" at\n"), filename);
a39a16c4
MM
2308 section_addrs = alloc_section_addr_info (section_index);
2309 make_cleanup (xfree, section_addrs);
db162d44 2310 for (i = 0; i < section_index; i++)
c906108c 2311 {
db162d44
EZ
2312 CORE_ADDR addr;
2313 char *val = sect_opts[i].value;
2314 char *sec = sect_opts[i].name;
5417f6dc 2315
ae822768 2316 addr = parse_and_eval_address (val);
db162d44 2317
db162d44 2318 /* Here we store the section offsets in the order they were
c378eb4e 2319 entered on the command line. */
a39a16c4
MM
2320 section_addrs->other[sec_num].name = sec;
2321 section_addrs->other[sec_num].addr = addr;
5af949e3
UW
2322 printf_unfiltered ("\t%s_addr = %s\n", sec,
2323 paddress (gdbarch, addr));
db162d44
EZ
2324 sec_num++;
2325
5417f6dc 2326 /* The object's sections are initialized when a
db162d44 2327 call is made to build_objfile_section_table (objfile).
5417f6dc 2328 This happens in reread_symbols.
db162d44
EZ
2329 At this point, we don't know what file type this is,
2330 so we can't determine what section names are valid. */
2acceee2 2331 }
d76488d8 2332 section_addrs->num_sections = sec_num;
db162d44 2333
2acceee2 2334 if (from_tty && (!query ("%s", "")))
8a3fe4f8 2335 error (_("Not confirmed."));
c906108c 2336
76ad5e1e
NB
2337 objf = symbol_file_add (filename, from_tty ? SYMFILE_VERBOSE : 0,
2338 section_addrs, flags);
2339
2340 add_target_sections_of_objfile (objf);
c906108c
SS
2341
2342 /* Getting new symbols may change our opinion about what is
2343 frameless. */
2344 reinit_frame_cache ();
db162d44 2345 do_cleanups (my_cleanups);
c906108c
SS
2346}
2347\f
70992597 2348
63644780
NB
2349/* This function removes a symbol file that was added via add-symbol-file. */
2350
2351static void
2352remove_symbol_file_command (char *args, int from_tty)
2353{
2354 char **argv;
2355 struct objfile *objf = NULL;
2356 struct cleanup *my_cleanups;
2357 struct program_space *pspace = current_program_space;
2358 struct gdbarch *gdbarch = get_current_arch ();
2359
2360 dont_repeat ();
2361
2362 if (args == NULL)
2363 error (_("remove-symbol-file: no symbol file provided"));
2364
2365 my_cleanups = make_cleanup (null_cleanup, NULL);
2366
2367 argv = gdb_buildargv (args);
2368
2369 if (strcmp (argv[0], "-a") == 0)
2370 {
2371 /* Interpret the next argument as an address. */
2372 CORE_ADDR addr;
2373
2374 if (argv[1] == NULL)
2375 error (_("Missing address argument"));
2376
2377 if (argv[2] != NULL)
2378 error (_("Junk after %s"), argv[1]);
2379
2380 addr = parse_and_eval_address (argv[1]);
2381
2382 ALL_OBJFILES (objf)
2383 {
2384 if (objf != 0
2385 && objf->flags & OBJF_USERLOADED
2386 && objf->pspace == pspace && is_addr_in_objfile (addr, objf))
2387 break;
2388 }
2389 }
2390 else if (argv[0] != NULL)
2391 {
2392 /* Interpret the current argument as a file name. */
2393 char *filename;
2394
2395 if (argv[1] != NULL)
2396 error (_("Junk after %s"), argv[0]);
2397
2398 filename = tilde_expand (argv[0]);
2399 make_cleanup (xfree, filename);
2400
2401 ALL_OBJFILES (objf)
2402 {
2403 if (objf != 0
2404 && objf->flags & OBJF_USERLOADED
2405 && objf->pspace == pspace
2406 && filename_cmp (filename, objfile_name (objf)) == 0)
2407 break;
2408 }
2409 }
2410
2411 if (objf == NULL)
2412 error (_("No symbol file found"));
2413
2414 if (from_tty
2415 && !query (_("Remove symbol table from file \"%s\"? "),
2416 objfile_name (objf)))
2417 error (_("Not confirmed."));
2418
2419 free_objfile (objf);
2420 clear_symtab_users (0);
2421
2422 do_cleanups (my_cleanups);
2423}
2424
4ac39b97
JK
2425typedef struct objfile *objfilep;
2426
2427DEF_VEC_P (objfilep);
2428
c906108c 2429/* Re-read symbols if a symbol-file has changed. */
3b7bacac 2430
c906108c 2431void
fba45db2 2432reread_symbols (void)
c906108c
SS
2433{
2434 struct objfile *objfile;
2435 long new_modtime;
c906108c
SS
2436 struct stat new_statbuf;
2437 int res;
4ac39b97
JK
2438 VEC (objfilep) *new_objfiles = NULL;
2439 struct cleanup *all_cleanups;
2440
2441 all_cleanups = make_cleanup (VEC_cleanup (objfilep), &new_objfiles);
c906108c
SS
2442
2443 /* With the addition of shared libraries, this should be modified,
2444 the load time should be saved in the partial symbol tables, since
2445 different tables may come from different source files. FIXME.
2446 This routine should then walk down each partial symbol table
c378eb4e 2447 and see if the symbol table that it originates from has been changed. */
c906108c 2448
c5aa993b
JM
2449 for (objfile = object_files; objfile; objfile = objfile->next)
2450 {
9cce227f
TG
2451 if (objfile->obfd == NULL)
2452 continue;
2453
2454 /* Separate debug objfiles are handled in the main objfile. */
2455 if (objfile->separate_debug_objfile_backlink)
2456 continue;
2457
02aeec7b
JB
2458 /* If this object is from an archive (what you usually create with
2459 `ar', often called a `static library' on most systems, though
2460 a `shared library' on AIX is also an archive), then you should
2461 stat on the archive name, not member name. */
9cce227f
TG
2462 if (objfile->obfd->my_archive)
2463 res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
2464 else
4262abfb 2465 res = stat (objfile_name (objfile), &new_statbuf);
9cce227f
TG
2466 if (res != 0)
2467 {
c378eb4e 2468 /* FIXME, should use print_sys_errmsg but it's not filtered. */
9cce227f 2469 printf_unfiltered (_("`%s' has disappeared; keeping its symbols.\n"),
4262abfb 2470 objfile_name (objfile));
9cce227f
TG
2471 continue;
2472 }
2473 new_modtime = new_statbuf.st_mtime;
2474 if (new_modtime != objfile->mtime)
2475 {
2476 struct cleanup *old_cleanups;
2477 struct section_offsets *offsets;
2478 int num_offsets;
24ba069a 2479 char *original_name;
9cce227f
TG
2480
2481 printf_unfiltered (_("`%s' has changed; re-reading symbols.\n"),
4262abfb 2482 objfile_name (objfile));
9cce227f
TG
2483
2484 /* There are various functions like symbol_file_add,
2485 symfile_bfd_open, syms_from_objfile, etc., which might
2486 appear to do what we want. But they have various other
2487 effects which we *don't* want. So we just do stuff
2488 ourselves. We don't worry about mapped files (for one thing,
2489 any mapped file will be out of date). */
2490
2491 /* If we get an error, blow away this objfile (not sure if
2492 that is the correct response for things like shared
2493 libraries). */
2494 old_cleanups = make_cleanup_free_objfile (objfile);
2495 /* We need to do this whenever any symbols go away. */
2496 make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
2497
0ba1096a
KT
2498 if (exec_bfd != NULL
2499 && filename_cmp (bfd_get_filename (objfile->obfd),
2500 bfd_get_filename (exec_bfd)) == 0)
9cce227f
TG
2501 {
2502 /* Reload EXEC_BFD without asking anything. */
2503
2504 exec_file_attach (bfd_get_filename (objfile->obfd), 0);
2505 }
2506
f6eeced0
JK
2507 /* Keep the calls order approx. the same as in free_objfile. */
2508
2509 /* Free the separate debug objfiles. It will be
2510 automatically recreated by sym_read. */
2511 free_objfile_separate_debug (objfile);
2512
2513 /* Remove any references to this objfile in the global
2514 value lists. */
2515 preserve_values (objfile);
2516
2517 /* Nuke all the state that we will re-read. Much of the following
2518 code which sets things to NULL really is necessary to tell
2519 other parts of GDB that there is nothing currently there.
2520
2521 Try to keep the freeing order compatible with free_objfile. */
2522
2523 if (objfile->sf != NULL)
2524 {
2525 (*objfile->sf->sym_finish) (objfile);
2526 }
2527
2528 clear_objfile_data (objfile);
2529
e1507e95 2530 /* Clean up any state BFD has sitting around. */
a4453b7e
TT
2531 {
2532 struct bfd *obfd = objfile->obfd;
d3846e71 2533 char *obfd_filename;
a4453b7e
TT
2534
2535 obfd_filename = bfd_get_filename (objfile->obfd);
2536 /* Open the new BFD before freeing the old one, so that
2537 the filename remains live. */
08d2cd74 2538 objfile->obfd = gdb_bfd_open_maybe_remote (obfd_filename);
e1507e95
TT
2539 if (objfile->obfd == NULL)
2540 {
2541 /* We have to make a cleanup and error here, rather
2542 than erroring later, because once we unref OBFD,
2543 OBFD_FILENAME will be freed. */
2544 make_cleanup_bfd_unref (obfd);
2545 error (_("Can't open %s to read symbols."), obfd_filename);
2546 }
a4453b7e
TT
2547 gdb_bfd_unref (obfd);
2548 }
2549
24ba069a
JK
2550 original_name = xstrdup (objfile->original_name);
2551 make_cleanup (xfree, original_name);
2552
9cce227f
TG
2553 /* bfd_openr sets cacheable to true, which is what we want. */
2554 if (!bfd_check_format (objfile->obfd, bfd_object))
4262abfb 2555 error (_("Can't read symbols from %s: %s."), objfile_name (objfile),
9cce227f
TG
2556 bfd_errmsg (bfd_get_error ()));
2557
2558 /* Save the offsets, we will nuke them with the rest of the
2559 objfile_obstack. */
2560 num_offsets = objfile->num_sections;
2561 offsets = ((struct section_offsets *)
2562 alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets)));
2563 memcpy (offsets, objfile->section_offsets,
2564 SIZEOF_N_SECTION_OFFSETS (num_offsets));
2565
9cce227f
TG
2566 /* FIXME: Do we have to free a whole linked list, or is this
2567 enough? */
2568 if (objfile->global_psymbols.list)
2569 xfree (objfile->global_psymbols.list);
2570 memset (&objfile->global_psymbols, 0,
2571 sizeof (objfile->global_psymbols));
2572 if (objfile->static_psymbols.list)
2573 xfree (objfile->static_psymbols.list);
2574 memset (&objfile->static_psymbols, 0,
2575 sizeof (objfile->static_psymbols));
2576
c378eb4e 2577 /* Free the obstacks for non-reusable objfiles. */
710e1a31
SW
2578 psymbol_bcache_free (objfile->psymbol_cache);
2579 objfile->psymbol_cache = psymbol_bcache_init ();
9cce227f
TG
2580 obstack_free (&objfile->objfile_obstack, 0);
2581 objfile->sections = NULL;
2582 objfile->symtabs = NULL;
2583 objfile->psymtabs = NULL;
2584 objfile->psymtabs_addrmap = NULL;
2585 objfile->free_psymtabs = NULL;
34eaf542 2586 objfile->template_symbols = NULL;
9cce227f 2587 objfile->msymbols = NULL;
9cce227f
TG
2588 objfile->minimal_symbol_count = 0;
2589 memset (&objfile->msymbol_hash, 0,
2590 sizeof (objfile->msymbol_hash));
2591 memset (&objfile->msymbol_demangled_hash, 0,
2592 sizeof (objfile->msymbol_demangled_hash));
2593
9cce227f
TG
2594 /* obstack_init also initializes the obstack so it is
2595 empty. We could use obstack_specify_allocation but
d82ea6a8 2596 gdb_obstack.h specifies the alloc/dealloc functions. */
9cce227f 2597 obstack_init (&objfile->objfile_obstack);
779bd270 2598
846060df
JB
2599 /* set_objfile_per_bfd potentially allocates the per-bfd
2600 data on the objfile's obstack (if sharing data across
2601 multiple users is not possible), so it's important to
2602 do it *after* the obstack has been initialized. */
2603 set_objfile_per_bfd (objfile);
2604
24ba069a
JK
2605 objfile->original_name = obstack_copy0 (&objfile->objfile_obstack,
2606 original_name,
2607 strlen (original_name));
2608
779bd270
DE
2609 /* Reset the sym_fns pointer. The ELF reader can change it
2610 based on whether .gdb_index is present, and we need it to
2611 start over. PR symtab/15885 */
8fb8eb5c 2612 objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
779bd270 2613
d82ea6a8 2614 build_objfile_section_table (objfile);
9cce227f
TG
2615 terminate_minimal_symbol_table (objfile);
2616
2617 /* We use the same section offsets as from last time. I'm not
2618 sure whether that is always correct for shared libraries. */
2619 objfile->section_offsets = (struct section_offsets *)
2620 obstack_alloc (&objfile->objfile_obstack,
2621 SIZEOF_N_SECTION_OFFSETS (num_offsets));
2622 memcpy (objfile->section_offsets, offsets,
2623 SIZEOF_N_SECTION_OFFSETS (num_offsets));
2624 objfile->num_sections = num_offsets;
2625
2626 /* What the hell is sym_new_init for, anyway? The concept of
2627 distinguishing between the main file and additional files
2628 in this way seems rather dubious. */
2629 if (objfile == symfile_objfile)
c906108c 2630 {
9cce227f 2631 (*objfile->sf->sym_new_init) (objfile);
c906108c 2632 }
9cce227f
TG
2633
2634 (*objfile->sf->sym_init) (objfile);
2635 clear_complaints (&symfile_complaints, 1, 1);
608e2dbb
TT
2636
2637 objfile->flags &= ~OBJF_PSYMTABS_READ;
2638 read_symbols (objfile, 0);
b11896a5 2639
9cce227f 2640 if (!objfile_has_symbols (objfile))
c906108c 2641 {
9cce227f
TG
2642 wrap_here ("");
2643 printf_unfiltered (_("(no debugging symbols found)\n"));
2644 wrap_here ("");
c5aa993b 2645 }
9cce227f
TG
2646
2647 /* We're done reading the symbol file; finish off complaints. */
2648 clear_complaints (&symfile_complaints, 0, 1);
2649
2650 /* Getting new symbols may change our opinion about what is
2651 frameless. */
2652
2653 reinit_frame_cache ();
2654
2655 /* Discard cleanups as symbol reading was successful. */
2656 discard_cleanups (old_cleanups);
2657
2658 /* If the mtime has changed between the time we set new_modtime
2659 and now, we *want* this to be out of date, so don't call stat
2660 again now. */
2661 objfile->mtime = new_modtime;
9cce227f 2662 init_entry_point_info (objfile);
4ac39b97
JK
2663
2664 VEC_safe_push (objfilep, new_objfiles, objfile);
c906108c
SS
2665 }
2666 }
c906108c 2667
4ac39b97 2668 if (new_objfiles)
ea53e89f 2669 {
4ac39b97
JK
2670 int ix;
2671
ff3536bc
UW
2672 /* Notify objfiles that we've modified objfile sections. */
2673 objfiles_changed ();
2674
c1e56572 2675 clear_symtab_users (0);
4ac39b97
JK
2676
2677 /* clear_objfile_data for each objfile was called before freeing it and
2678 observer_notify_new_objfile (NULL) has been called by
2679 clear_symtab_users above. Notify the new files now. */
2680 for (ix = 0; VEC_iterate (objfilep, new_objfiles, ix, objfile); ix++)
2681 observer_notify_new_objfile (objfile);
2682
ea53e89f
JB
2683 /* At least one objfile has changed, so we can consider that
2684 the executable we're debugging has changed too. */
781b42b0 2685 observer_notify_executable_changed ();
ea53e89f 2686 }
4ac39b97
JK
2687
2688 do_cleanups (all_cleanups);
c906108c 2689}
c906108c
SS
2690\f
2691
c5aa993b
JM
2692typedef struct
2693{
2694 char *ext;
c906108c 2695 enum language lang;
c5aa993b
JM
2696}
2697filename_language;
c906108c 2698
c5aa993b 2699static filename_language *filename_language_table;
c906108c
SS
2700static int fl_table_size, fl_table_next;
2701
2702static void
fba45db2 2703add_filename_language (char *ext, enum language lang)
c906108c
SS
2704{
2705 if (fl_table_next >= fl_table_size)
2706 {
2707 fl_table_size += 10;
5417f6dc 2708 filename_language_table =
25bf3106
PM
2709 xrealloc (filename_language_table,
2710 fl_table_size * sizeof (*filename_language_table));
c906108c
SS
2711 }
2712
4fcf66da 2713 filename_language_table[fl_table_next].ext = xstrdup (ext);
c906108c
SS
2714 filename_language_table[fl_table_next].lang = lang;
2715 fl_table_next++;
2716}
2717
2718static char *ext_args;
920d2a44
AC
2719static void
2720show_ext_args (struct ui_file *file, int from_tty,
2721 struct cmd_list_element *c, const char *value)
2722{
3e43a32a
MS
2723 fprintf_filtered (file,
2724 _("Mapping between filename extension "
2725 "and source language is \"%s\".\n"),
920d2a44
AC
2726 value);
2727}
c906108c
SS
2728
2729static void
26c41df3 2730set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e)
c906108c
SS
2731{
2732 int i;
2733 char *cp = ext_args;
2734 enum language lang;
2735
c378eb4e 2736 /* First arg is filename extension, starting with '.' */
c906108c 2737 if (*cp != '.')
8a3fe4f8 2738 error (_("'%s': Filename extension must begin with '.'"), ext_args);
c906108c
SS
2739
2740 /* Find end of first arg. */
c5aa993b 2741 while (*cp && !isspace (*cp))
c906108c
SS
2742 cp++;
2743
2744 if (*cp == '\0')
3e43a32a
MS
2745 error (_("'%s': two arguments required -- "
2746 "filename extension and language"),
c906108c
SS
2747 ext_args);
2748
c378eb4e 2749 /* Null-terminate first arg. */
c5aa993b 2750 *cp++ = '\0';
c906108c
SS
2751
2752 /* Find beginning of second arg, which should be a source language. */
529480d0 2753 cp = skip_spaces (cp);
c906108c
SS
2754
2755 if (*cp == '\0')
3e43a32a
MS
2756 error (_("'%s': two arguments required -- "
2757 "filename extension and language"),
c906108c
SS
2758 ext_args);
2759
2760 /* Lookup the language from among those we know. */
2761 lang = language_enum (cp);
2762
2763 /* Now lookup the filename extension: do we already know it? */
2764 for (i = 0; i < fl_table_next; i++)
2765 if (0 == strcmp (ext_args, filename_language_table[i].ext))
2766 break;
2767
2768 if (i >= fl_table_next)
2769 {
c378eb4e 2770 /* New file extension. */
c906108c
SS
2771 add_filename_language (ext_args, lang);
2772 }
2773 else
2774 {
c378eb4e 2775 /* Redefining a previously known filename extension. */
c906108c
SS
2776
2777 /* if (from_tty) */
2778 /* query ("Really make files of type %s '%s'?", */
2779 /* ext_args, language_str (lang)); */
2780
b8c9b27d 2781 xfree (filename_language_table[i].ext);
4fcf66da 2782 filename_language_table[i].ext = xstrdup (ext_args);
c906108c
SS
2783 filename_language_table[i].lang = lang;
2784 }
2785}
2786
2787static void
fba45db2 2788info_ext_lang_command (char *args, int from_tty)
c906108c
SS
2789{
2790 int i;
2791
a3f17187 2792 printf_filtered (_("Filename extensions and the languages they represent:"));
c906108c
SS
2793 printf_filtered ("\n\n");
2794 for (i = 0; i < fl_table_next; i++)
c5aa993b
JM
2795 printf_filtered ("\t%s\t- %s\n",
2796 filename_language_table[i].ext,
c906108c
SS
2797 language_str (filename_language_table[i].lang));
2798}
2799
2800static void
fba45db2 2801init_filename_language_table (void)
c906108c 2802{
c378eb4e 2803 if (fl_table_size == 0) /* Protect against repetition. */
c906108c
SS
2804 {
2805 fl_table_size = 20;
2806 fl_table_next = 0;
c5aa993b 2807 filename_language_table =
c906108c 2808 xmalloc (fl_table_size * sizeof (*filename_language_table));
c5aa993b 2809 add_filename_language (".c", language_c);
6aecb9c2 2810 add_filename_language (".d", language_d);
c5aa993b
JM
2811 add_filename_language (".C", language_cplus);
2812 add_filename_language (".cc", language_cplus);
2813 add_filename_language (".cp", language_cplus);
2814 add_filename_language (".cpp", language_cplus);
2815 add_filename_language (".cxx", language_cplus);
2816 add_filename_language (".c++", language_cplus);
2817 add_filename_language (".java", language_java);
c906108c 2818 add_filename_language (".class", language_java);
da2cf7e0 2819 add_filename_language (".m", language_objc);
c5aa993b
JM
2820 add_filename_language (".f", language_fortran);
2821 add_filename_language (".F", language_fortran);
fd5700c7
JK
2822 add_filename_language (".for", language_fortran);
2823 add_filename_language (".FOR", language_fortran);
2824 add_filename_language (".ftn", language_fortran);
2825 add_filename_language (".FTN", language_fortran);
2826 add_filename_language (".fpp", language_fortran);
2827 add_filename_language (".FPP", language_fortran);
2828 add_filename_language (".f90", language_fortran);
2829 add_filename_language (".F90", language_fortran);
2830 add_filename_language (".f95", language_fortran);
2831 add_filename_language (".F95", language_fortran);
2832 add_filename_language (".f03", language_fortran);
2833 add_filename_language (".F03", language_fortran);
2834 add_filename_language (".f08", language_fortran);
2835 add_filename_language (".F08", language_fortran);
c5aa993b 2836 add_filename_language (".s", language_asm);
aa707ed0 2837 add_filename_language (".sx", language_asm);
c5aa993b 2838 add_filename_language (".S", language_asm);
c6fd39cd
PM
2839 add_filename_language (".pas", language_pascal);
2840 add_filename_language (".p", language_pascal);
2841 add_filename_language (".pp", language_pascal);
963a6417
PH
2842 add_filename_language (".adb", language_ada);
2843 add_filename_language (".ads", language_ada);
2844 add_filename_language (".a", language_ada);
2845 add_filename_language (".ada", language_ada);
dde59185 2846 add_filename_language (".dg", language_ada);
c906108c
SS
2847 }
2848}
2849
2850enum language
dd786858 2851deduce_language_from_filename (const char *filename)
c906108c
SS
2852{
2853 int i;
2854 char *cp;
2855
2856 if (filename != NULL)
2857 if ((cp = strrchr (filename, '.')) != NULL)
2858 for (i = 0; i < fl_table_next; i++)
2859 if (strcmp (cp, filename_language_table[i].ext) == 0)
2860 return filename_language_table[i].lang;
2861
2862 return language_unknown;
2863}
2864\f
2865/* allocate_symtab:
2866
2867 Allocate and partly initialize a new symbol table. Return a pointer
2868 to it. error() if no space.
2869
2870 Caller must set these fields:
c5aa993b
JM
2871 LINETABLE(symtab)
2872 symtab->blockvector
2873 symtab->dirname
2874 symtab->free_code
2875 symtab->free_ptr
c906108c
SS
2876 */
2877
2878struct symtab *
72b9f47f 2879allocate_symtab (const char *filename, struct objfile *objfile)
c906108c 2880{
52f0bd74 2881 struct symtab *symtab;
c906108c
SS
2882
2883 symtab = (struct symtab *)
4a146b47 2884 obstack_alloc (&objfile->objfile_obstack, sizeof (struct symtab));
c906108c 2885 memset (symtab, 0, sizeof (*symtab));
10abe6bf 2886 symtab->filename = (char *) bcache (filename, strlen (filename) + 1,
706e3705 2887 objfile->per_bfd->filename_cache);
c5aa993b
JM
2888 symtab->fullname = NULL;
2889 symtab->language = deduce_language_from_filename (filename);
1c9e8358 2890 symtab->debugformat = "unknown";
c906108c 2891
c378eb4e 2892 /* Hook it to the objfile it comes from. */
c906108c 2893
c5aa993b
JM
2894 symtab->objfile = objfile;
2895 symtab->next = objfile->symtabs;
2896 objfile->symtabs = symtab;
c906108c 2897
db0fec5c
DE
2898 /* This can be very verbose with lots of headers.
2899 Only print at higher debug levels. */
2900 if (symtab_create_debug >= 2)
45cfd468
DE
2901 {
2902 /* Be a bit clever with debugging messages, and don't print objfile
2903 every time, only when it changes. */
2904 static char *last_objfile_name = NULL;
2905
2906 if (last_objfile_name == NULL
4262abfb 2907 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
45cfd468
DE
2908 {
2909 xfree (last_objfile_name);
4262abfb 2910 last_objfile_name = xstrdup (objfile_name (objfile));
45cfd468
DE
2911 fprintf_unfiltered (gdb_stdlog,
2912 "Creating one or more symtabs for objfile %s ...\n",
2913 last_objfile_name);
2914 }
2915 fprintf_unfiltered (gdb_stdlog,
b3dbbd6f
PM
2916 "Created symtab %s for module %s.\n",
2917 host_address_to_string (symtab), filename);
45cfd468
DE
2918 }
2919
c906108c
SS
2920 return (symtab);
2921}
c906108c 2922\f
c5aa993b 2923
c906108c 2924/* Reset all data structures in gdb which may contain references to symbol
c1e56572 2925 table data. ADD_FLAGS is a bitmask of enum symfile_add_flags. */
c906108c
SS
2926
2927void
c1e56572 2928clear_symtab_users (int add_flags)
c906108c
SS
2929{
2930 /* Someday, we should do better than this, by only blowing away
2931 the things that really need to be blown. */
c0501be5
DJ
2932
2933 /* Clear the "current" symtab first, because it is no longer valid.
2934 breakpoint_re_set may try to access the current symtab. */
2935 clear_current_source_symtab_and_line ();
2936
c906108c 2937 clear_displays ();
c1e56572
JK
2938 if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
2939 breakpoint_re_set ();
1bfeeb0f 2940 clear_last_displayed_sal ();
c906108c 2941 clear_pc_function_cache ();
06d3b283 2942 observer_notify_new_objfile (NULL);
9bdcbae7
DJ
2943
2944 /* Clear globals which might have pointed into a removed objfile.
2945 FIXME: It's not clear which of these are supposed to persist
2946 between expressions and which ought to be reset each time. */
2947 expression_context_block = NULL;
2948 innermost_block = NULL;
8756216b
DP
2949
2950 /* Varobj may refer to old symbols, perform a cleanup. */
2951 varobj_invalidate ();
2952
c906108c
SS
2953}
2954
74b7792f
AC
2955static void
2956clear_symtab_users_cleanup (void *ignore)
2957{
c1e56572 2958 clear_symtab_users (0);
74b7792f 2959}
c906108c 2960\f
c906108c
SS
2961/* OVERLAYS:
2962 The following code implements an abstraction for debugging overlay sections.
2963
2964 The target model is as follows:
2965 1) The gnu linker will permit multiple sections to be mapped into the
c5aa993b 2966 same VMA, each with its own unique LMA (or load address).
c906108c 2967 2) It is assumed that some runtime mechanism exists for mapping the
c5aa993b 2968 sections, one by one, from the load address into the VMA address.
5417f6dc 2969 3) This code provides a mechanism for gdb to keep track of which
c5aa993b
JM
2970 sections should be considered to be mapped from the VMA to the LMA.
2971 This information is used for symbol lookup, and memory read/write.
5417f6dc 2972 For instance, if a section has been mapped then its contents
c5aa993b 2973 should be read from the VMA, otherwise from the LMA.
c906108c
SS
2974
2975 Two levels of debugger support for overlays are available. One is
2976 "manual", in which the debugger relies on the user to tell it which
2977 overlays are currently mapped. This level of support is
2978 implemented entirely in the core debugger, and the information about
2979 whether a section is mapped is kept in the objfile->obj_section table.
2980
2981 The second level of support is "automatic", and is only available if
2982 the target-specific code provides functionality to read the target's
2983 overlay mapping table, and translate its contents for the debugger
2984 (by updating the mapped state information in the obj_section tables).
2985
2986 The interface is as follows:
c5aa993b
JM
2987 User commands:
2988 overlay map <name> -- tell gdb to consider this section mapped
2989 overlay unmap <name> -- tell gdb to consider this section unmapped
2990 overlay list -- list the sections that GDB thinks are mapped
2991 overlay read-target -- get the target's state of what's mapped
2992 overlay off/manual/auto -- set overlay debugging state
2993 Functional interface:
2994 find_pc_mapped_section(pc): if the pc is in the range of a mapped
2995 section, return that section.
5417f6dc 2996 find_pc_overlay(pc): find any overlay section that contains
c5aa993b 2997 the pc, either in its VMA or its LMA
714835d5 2998 section_is_mapped(sect): true if overlay is marked as mapped
c5aa993b
JM
2999 section_is_overlay(sect): true if section's VMA != LMA
3000 pc_in_mapped_range(pc,sec): true if pc belongs to section's VMA
3001 pc_in_unmapped_range(...): true if pc belongs to section's LMA
9ec8e6a0 3002 sections_overlap(sec1, sec2): true if mapped sec1 and sec2 ranges overlap
c5aa993b
JM
3003 overlay_mapped_address(...): map an address from section's LMA to VMA
3004 overlay_unmapped_address(...): map an address from section's VMA to LMA
3005 symbol_overlayed_address(...): Return a "current" address for symbol:
3006 either in VMA or LMA depending on whether
c378eb4e 3007 the symbol's section is currently mapped. */
c906108c
SS
3008
3009/* Overlay debugging state: */
3010
d874f1e2 3011enum overlay_debugging_state overlay_debugging = ovly_off;
c378eb4e 3012int overlay_cache_invalid = 0; /* True if need to refresh mapped state. */
c906108c 3013
c906108c 3014/* Function: section_is_overlay (SECTION)
5417f6dc 3015 Returns true if SECTION has VMA not equal to LMA, ie.
c906108c
SS
3016 SECTION is loaded at an address different from where it will "run". */
3017
3018int
714835d5 3019section_is_overlay (struct obj_section *section)
c906108c 3020{
714835d5
UW
3021 if (overlay_debugging && section)
3022 {
3023 bfd *abfd = section->objfile->obfd;
3024 asection *bfd_section = section->the_bfd_section;
f888f159 3025
714835d5
UW
3026 if (bfd_section_lma (abfd, bfd_section) != 0
3027 && bfd_section_lma (abfd, bfd_section)
3028 != bfd_section_vma (abfd, bfd_section))
3029 return 1;
3030 }
c906108c
SS
3031
3032 return 0;
3033}
3034
3035/* Function: overlay_invalidate_all (void)
3036 Invalidate the mapped state of all overlay sections (mark it as stale). */
3037
3038static void
fba45db2 3039overlay_invalidate_all (void)
c906108c 3040{
c5aa993b 3041 struct objfile *objfile;
c906108c
SS
3042 struct obj_section *sect;
3043
3044 ALL_OBJSECTIONS (objfile, sect)
714835d5
UW
3045 if (section_is_overlay (sect))
3046 sect->ovly_mapped = -1;
c906108c
SS
3047}
3048
714835d5 3049/* Function: section_is_mapped (SECTION)
5417f6dc 3050 Returns true if section is an overlay, and is currently mapped.
c906108c
SS
3051
3052 Access to the ovly_mapped flag is restricted to this function, so
3053 that we can do automatic update. If the global flag
3054 OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
3055 overlay_invalidate_all. If the mapped state of the particular
3056 section is stale, then call TARGET_OVERLAY_UPDATE to refresh it. */
3057
714835d5
UW
3058int
3059section_is_mapped (struct obj_section *osect)
c906108c 3060{
9216df95
UW
3061 struct gdbarch *gdbarch;
3062
714835d5 3063 if (osect == 0 || !section_is_overlay (osect))
c906108c
SS
3064 return 0;
3065
c5aa993b 3066 switch (overlay_debugging)
c906108c
SS
3067 {
3068 default:
d874f1e2 3069 case ovly_off:
c5aa993b 3070 return 0; /* overlay debugging off */
d874f1e2 3071 case ovly_auto: /* overlay debugging automatic */
1c772458 3072 /* Unles there is a gdbarch_overlay_update function,
c378eb4e 3073 there's really nothing useful to do here (can't really go auto). */
9216df95
UW
3074 gdbarch = get_objfile_arch (osect->objfile);
3075 if (gdbarch_overlay_update_p (gdbarch))
c906108c
SS
3076 {
3077 if (overlay_cache_invalid)
3078 {
3079 overlay_invalidate_all ();
3080 overlay_cache_invalid = 0;
3081 }
3082 if (osect->ovly_mapped == -1)
9216df95 3083 gdbarch_overlay_update (gdbarch, osect);
c906108c
SS
3084 }
3085 /* fall thru to manual case */
d874f1e2 3086 case ovly_on: /* overlay debugging manual */
c906108c
SS
3087 return osect->ovly_mapped == 1;
3088 }
3089}
3090
c906108c
SS
3091/* Function: pc_in_unmapped_range
3092 If PC falls into the lma range of SECTION, return true, else false. */
3093
3094CORE_ADDR
714835d5 3095pc_in_unmapped_range (CORE_ADDR pc, struct obj_section *section)
c906108c 3096{
714835d5
UW
3097 if (section_is_overlay (section))
3098 {
3099 bfd *abfd = section->objfile->obfd;
3100 asection *bfd_section = section->the_bfd_section;
fbd35540 3101
714835d5
UW
3102 /* We assume the LMA is relocated by the same offset as the VMA. */
3103 bfd_vma size = bfd_get_section_size (bfd_section);
3104 CORE_ADDR offset = obj_section_offset (section);
3105
3106 if (bfd_get_section_lma (abfd, bfd_section) + offset <= pc
3107 && pc < bfd_get_section_lma (abfd, bfd_section) + offset + size)
3108 return 1;
3109 }
c906108c 3110
c906108c
SS
3111 return 0;
3112}
3113
3114/* Function: pc_in_mapped_range
3115 If PC falls into the vma range of SECTION, return true, else false. */
3116
3117CORE_ADDR
714835d5 3118pc_in_mapped_range (CORE_ADDR pc, struct obj_section *section)
c906108c 3119{
714835d5
UW
3120 if (section_is_overlay (section))
3121 {
3122 if (obj_section_addr (section) <= pc
3123 && pc < obj_section_endaddr (section))
3124 return 1;
3125 }
c906108c 3126
c906108c
SS
3127 return 0;
3128}
3129
9ec8e6a0
JB
3130/* Return true if the mapped ranges of sections A and B overlap, false
3131 otherwise. */
3b7bacac 3132
b9362cc7 3133static int
714835d5 3134sections_overlap (struct obj_section *a, struct obj_section *b)
9ec8e6a0 3135{
714835d5
UW
3136 CORE_ADDR a_start = obj_section_addr (a);
3137 CORE_ADDR a_end = obj_section_endaddr (a);
3138 CORE_ADDR b_start = obj_section_addr (b);
3139 CORE_ADDR b_end = obj_section_endaddr (b);
9ec8e6a0
JB
3140
3141 return (a_start < b_end && b_start < a_end);
3142}
3143
c906108c
SS
3144/* Function: overlay_unmapped_address (PC, SECTION)
3145 Returns the address corresponding to PC in the unmapped (load) range.
3146 May be the same as PC. */
3147
3148CORE_ADDR
714835d5 3149overlay_unmapped_address (CORE_ADDR pc, struct obj_section *section)
c906108c 3150{
714835d5
UW
3151 if (section_is_overlay (section) && pc_in_mapped_range (pc, section))
3152 {
3153 bfd *abfd = section->objfile->obfd;
3154 asection *bfd_section = section->the_bfd_section;
fbd35540 3155
714835d5
UW
3156 return pc + bfd_section_lma (abfd, bfd_section)
3157 - bfd_section_vma (abfd, bfd_section);
3158 }
c906108c
SS
3159
3160 return pc;
3161}
3162
3163/* Function: overlay_mapped_address (PC, SECTION)
3164 Returns the address corresponding to PC in the mapped (runtime) range.
3165 May be the same as PC. */
3166
3167CORE_ADDR
714835d5 3168overlay_mapped_address (CORE_ADDR pc, struct obj_section *section)
c906108c 3169{
714835d5
UW
3170 if (section_is_overlay (section) && pc_in_unmapped_range (pc, section))
3171 {
3172 bfd *abfd = section->objfile->obfd;
3173 asection *bfd_section = section->the_bfd_section;
fbd35540 3174
714835d5
UW
3175 return pc + bfd_section_vma (abfd, bfd_section)
3176 - bfd_section_lma (abfd, bfd_section);
3177 }
c906108c
SS
3178
3179 return pc;
3180}
3181
5417f6dc 3182/* Function: symbol_overlayed_address
c906108c
SS
3183 Return one of two addresses (relative to the VMA or to the LMA),
3184 depending on whether the section is mapped or not. */
3185
c5aa993b 3186CORE_ADDR
714835d5 3187symbol_overlayed_address (CORE_ADDR address, struct obj_section *section)
c906108c
SS
3188{
3189 if (overlay_debugging)
3190 {
c378eb4e 3191 /* If the symbol has no section, just return its regular address. */
c906108c
SS
3192 if (section == 0)
3193 return address;
c378eb4e
MS
3194 /* If the symbol's section is not an overlay, just return its
3195 address. */
c906108c
SS
3196 if (!section_is_overlay (section))
3197 return address;
c378eb4e 3198 /* If the symbol's section is mapped, just return its address. */
c906108c
SS
3199 if (section_is_mapped (section))
3200 return address;
3201 /*
3202 * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
3203 * then return its LOADED address rather than its vma address!!
3204 */
3205 return overlay_unmapped_address (address, section);
3206 }
3207 return address;
3208}
3209
5417f6dc 3210/* Function: find_pc_overlay (PC)
c906108c
SS
3211 Return the best-match overlay section for PC:
3212 If PC matches a mapped overlay section's VMA, return that section.
3213 Else if PC matches an unmapped section's VMA, return that section.
3214 Else if PC matches an unmapped section's LMA, return that section. */
3215
714835d5 3216struct obj_section *
fba45db2 3217find_pc_overlay (CORE_ADDR pc)
c906108c 3218{
c5aa993b 3219 struct objfile *objfile;
c906108c
SS
3220 struct obj_section *osect, *best_match = NULL;
3221
3222 if (overlay_debugging)
3223 ALL_OBJSECTIONS (objfile, osect)
714835d5 3224 if (section_is_overlay (osect))
c5aa993b 3225 {
714835d5 3226 if (pc_in_mapped_range (pc, osect))
c5aa993b 3227 {
714835d5
UW
3228 if (section_is_mapped (osect))
3229 return osect;
c5aa993b
JM
3230 else
3231 best_match = osect;
3232 }
714835d5 3233 else if (pc_in_unmapped_range (pc, osect))
c5aa993b
JM
3234 best_match = osect;
3235 }
714835d5 3236 return best_match;
c906108c
SS
3237}
3238
3239/* Function: find_pc_mapped_section (PC)
5417f6dc 3240 If PC falls into the VMA address range of an overlay section that is
c906108c
SS
3241 currently marked as MAPPED, return that section. Else return NULL. */
3242
714835d5 3243struct obj_section *
fba45db2 3244find_pc_mapped_section (CORE_ADDR pc)
c906108c 3245{
c5aa993b 3246 struct objfile *objfile;
c906108c
SS
3247 struct obj_section *osect;
3248
3249 if (overlay_debugging)
3250 ALL_OBJSECTIONS (objfile, osect)
714835d5
UW
3251 if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect))
3252 return osect;
c906108c
SS
3253
3254 return NULL;
3255}
3256
3257/* Function: list_overlays_command
c378eb4e 3258 Print a list of mapped sections and their PC ranges. */
c906108c 3259
5d3055ad 3260static void
fba45db2 3261list_overlays_command (char *args, int from_tty)
c906108c 3262{
c5aa993b
JM
3263 int nmapped = 0;
3264 struct objfile *objfile;
c906108c
SS
3265 struct obj_section *osect;
3266
3267 if (overlay_debugging)
3268 ALL_OBJSECTIONS (objfile, osect)
714835d5 3269 if (section_is_mapped (osect))
c5aa993b 3270 {
5af949e3 3271 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c5aa993b
JM
3272 const char *name;
3273 bfd_vma lma, vma;
3274 int size;
3275
3276 vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
3277 lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
2c500098 3278 size = bfd_get_section_size (osect->the_bfd_section);
c5aa993b
JM
3279 name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
3280
3281 printf_filtered ("Section %s, loaded at ", name);
5af949e3 3282 fputs_filtered (paddress (gdbarch, lma), gdb_stdout);
c5aa993b 3283 puts_filtered (" - ");
5af949e3 3284 fputs_filtered (paddress (gdbarch, lma + size), gdb_stdout);
c5aa993b 3285 printf_filtered (", mapped at ");
5af949e3 3286 fputs_filtered (paddress (gdbarch, vma), gdb_stdout);
c5aa993b 3287 puts_filtered (" - ");
5af949e3 3288 fputs_filtered (paddress (gdbarch, vma + size), gdb_stdout);
c5aa993b
JM
3289 puts_filtered ("\n");
3290
3291 nmapped++;
3292 }
c906108c 3293 if (nmapped == 0)
a3f17187 3294 printf_filtered (_("No sections are mapped.\n"));
c906108c
SS
3295}
3296
3297/* Function: map_overlay_command
3298 Mark the named section as mapped (ie. residing at its VMA address). */
3299
5d3055ad 3300static void
fba45db2 3301map_overlay_command (char *args, int from_tty)
c906108c 3302{
c5aa993b
JM
3303 struct objfile *objfile, *objfile2;
3304 struct obj_section *sec, *sec2;
c906108c
SS
3305
3306 if (!overlay_debugging)
3e43a32a
MS
3307 error (_("Overlay debugging not enabled. Use "
3308 "either the 'overlay auto' or\n"
3309 "the 'overlay manual' command."));
c906108c
SS
3310
3311 if (args == 0 || *args == 0)
8a3fe4f8 3312 error (_("Argument required: name of an overlay section"));
c906108c 3313
c378eb4e 3314 /* First, find a section matching the user supplied argument. */
c906108c
SS
3315 ALL_OBJSECTIONS (objfile, sec)
3316 if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
c5aa993b 3317 {
c378eb4e 3318 /* Now, check to see if the section is an overlay. */
714835d5 3319 if (!section_is_overlay (sec))
c5aa993b
JM
3320 continue; /* not an overlay section */
3321
c378eb4e 3322 /* Mark the overlay as "mapped". */
c5aa993b
JM
3323 sec->ovly_mapped = 1;
3324
3325 /* Next, make a pass and unmap any sections that are
3326 overlapped by this new section: */
3327 ALL_OBJSECTIONS (objfile2, sec2)
714835d5 3328 if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec, sec2))
c5aa993b
JM
3329 {
3330 if (info_verbose)
a3f17187 3331 printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
c5aa993b
JM
3332 bfd_section_name (objfile->obfd,
3333 sec2->the_bfd_section));
c378eb4e 3334 sec2->ovly_mapped = 0; /* sec2 overlaps sec: unmap sec2. */
c5aa993b
JM
3335 }
3336 return;
3337 }
8a3fe4f8 3338 error (_("No overlay section called %s"), args);
c906108c
SS
3339}
3340
3341/* Function: unmap_overlay_command
5417f6dc 3342 Mark the overlay section as unmapped
c906108c
SS
3343 (ie. resident in its LMA address range, rather than the VMA range). */
3344
5d3055ad 3345static void
fba45db2 3346unmap_overlay_command (char *args, int from_tty)
c906108c 3347{
c5aa993b 3348 struct objfile *objfile;
c906108c
SS
3349 struct obj_section *sec;
3350
3351 if (!overlay_debugging)
3e43a32a
MS
3352 error (_("Overlay debugging not enabled. "
3353 "Use either the 'overlay auto' or\n"
3354 "the 'overlay manual' command."));
c906108c
SS
3355
3356 if (args == 0 || *args == 0)
8a3fe4f8 3357 error (_("Argument required: name of an overlay section"));
c906108c 3358
c378eb4e 3359 /* First, find a section matching the user supplied argument. */
c906108c
SS
3360 ALL_OBJSECTIONS (objfile, sec)
3361 if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
c5aa993b
JM
3362 {
3363 if (!sec->ovly_mapped)
8a3fe4f8 3364 error (_("Section %s is not mapped"), args);
c5aa993b
JM
3365 sec->ovly_mapped = 0;
3366 return;
3367 }
8a3fe4f8 3368 error (_("No overlay section called %s"), args);
c906108c
SS
3369}
3370
3371/* Function: overlay_auto_command
3372 A utility command to turn on overlay debugging.
c378eb4e 3373 Possibly this should be done via a set/show command. */
c906108c
SS
3374
3375static void
fba45db2 3376overlay_auto_command (char *args, int from_tty)
c906108c 3377{
d874f1e2 3378 overlay_debugging = ovly_auto;
1900040c 3379 enable_overlay_breakpoints ();
c906108c 3380 if (info_verbose)
a3f17187 3381 printf_unfiltered (_("Automatic overlay debugging enabled."));
c906108c
SS
3382}
3383
3384/* Function: overlay_manual_command
3385 A utility command to turn on overlay debugging.
c378eb4e 3386 Possibly this should be done via a set/show command. */
c906108c
SS
3387
3388static void
fba45db2 3389overlay_manual_command (char *args, int from_tty)
c906108c 3390{
d874f1e2 3391 overlay_debugging = ovly_on;
1900040c 3392 disable_overlay_breakpoints ();
c906108c 3393 if (info_verbose)
a3f17187 3394 printf_unfiltered (_("Overlay debugging enabled."));
c906108c
SS
3395}
3396
3397/* Function: overlay_off_command
3398 A utility command to turn on overlay debugging.
c378eb4e 3399 Possibly this should be done via a set/show command. */
c906108c
SS
3400
3401static void
fba45db2 3402overlay_off_command (char *args, int from_tty)
c906108c 3403{
d874f1e2 3404 overlay_debugging = ovly_off;
1900040c 3405 disable_overlay_breakpoints ();
c906108c 3406 if (info_verbose)
a3f17187 3407 printf_unfiltered (_("Overlay debugging disabled."));
c906108c
SS
3408}
3409
3410static void
fba45db2 3411overlay_load_command (char *args, int from_tty)
c906108c 3412{
e17c207e
UW
3413 struct gdbarch *gdbarch = get_current_arch ();
3414
3415 if (gdbarch_overlay_update_p (gdbarch))
3416 gdbarch_overlay_update (gdbarch, NULL);
c906108c 3417 else
8a3fe4f8 3418 error (_("This target does not know how to read its overlay state."));
c906108c
SS
3419}
3420
3421/* Function: overlay_command
c378eb4e 3422 A place-holder for a mis-typed command. */
c906108c 3423
c378eb4e 3424/* Command list chain containing all defined "overlay" subcommands. */
28578e6b 3425static struct cmd_list_element *overlaylist;
c906108c
SS
3426
3427static void
fba45db2 3428overlay_command (char *args, int from_tty)
c906108c 3429{
c5aa993b 3430 printf_unfiltered
c906108c
SS
3431 ("\"overlay\" must be followed by the name of an overlay command.\n");
3432 help_list (overlaylist, "overlay ", -1, gdb_stdout);
3433}
3434
c906108c
SS
3435/* Target Overlays for the "Simplest" overlay manager:
3436
5417f6dc
RM
3437 This is GDB's default target overlay layer. It works with the
3438 minimal overlay manager supplied as an example by Cygnus. The
1c772458 3439 entry point is via a function pointer "gdbarch_overlay_update",
5417f6dc 3440 so targets that use a different runtime overlay manager can
c906108c
SS
3441 substitute their own overlay_update function and take over the
3442 function pointer.
3443
3444 The overlay_update function pokes around in the target's data structures
3445 to see what overlays are mapped, and updates GDB's overlay mapping with
3446 this information.
3447
3448 In this simple implementation, the target data structures are as follows:
c5aa993b
JM
3449 unsigned _novlys; /# number of overlay sections #/
3450 unsigned _ovly_table[_novlys][4] = {
3451 {VMA, SIZE, LMA, MAPPED}, /# one entry per overlay section #/
3452 {..., ..., ..., ...},
3453 }
3454 unsigned _novly_regions; /# number of overlay regions #/
3455 unsigned _ovly_region_table[_novly_regions][3] = {
3456 {VMA, SIZE, MAPPED_TO_LMA}, /# one entry per overlay region #/
3457 {..., ..., ...},
3458 }
c906108c
SS
3459 These functions will attempt to update GDB's mappedness state in the
3460 symbol section table, based on the target's mappedness state.
3461
3462 To do this, we keep a cached copy of the target's _ovly_table, and
3463 attempt to detect when the cached copy is invalidated. The main
3464 entry point is "simple_overlay_update(SECT), which looks up SECT in
3465 the cached table and re-reads only the entry for that section from
c378eb4e 3466 the target (whenever possible). */
c906108c
SS
3467
3468/* Cached, dynamically allocated copies of the target data structures: */
c5aa993b 3469static unsigned (*cache_ovly_table)[4] = 0;
c5aa993b 3470static unsigned cache_novlys = 0;
c906108c 3471static CORE_ADDR cache_ovly_table_base = 0;
c5aa993b
JM
3472enum ovly_index
3473 {
3474 VMA, SIZE, LMA, MAPPED
3475 };
c906108c 3476
c378eb4e 3477/* Throw away the cached copy of _ovly_table. */
3b7bacac 3478
c906108c 3479static void
fba45db2 3480simple_free_overlay_table (void)
c906108c
SS
3481{
3482 if (cache_ovly_table)
b8c9b27d 3483 xfree (cache_ovly_table);
c5aa993b 3484 cache_novlys = 0;
c906108c
SS
3485 cache_ovly_table = NULL;
3486 cache_ovly_table_base = 0;
3487}
3488
9216df95 3489/* Read an array of ints of size SIZE from the target into a local buffer.
c378eb4e 3490 Convert to host order. int LEN is number of ints. */
3b7bacac 3491
c906108c 3492static void
9216df95 3493read_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr,
e17a4113 3494 int len, int size, enum bfd_endian byte_order)
c906108c 3495{
c378eb4e 3496 /* FIXME (alloca): Not safe if array is very large. */
9216df95 3497 gdb_byte *buf = alloca (len * size);
c5aa993b 3498 int i;
c906108c 3499
9216df95 3500 read_memory (memaddr, buf, len * size);
c906108c 3501 for (i = 0; i < len; i++)
e17a4113 3502 myaddr[i] = extract_unsigned_integer (size * i + buf, size, byte_order);
c906108c
SS
3503}
3504
3505/* Find and grab a copy of the target _ovly_table
c378eb4e 3506 (and _novlys, which is needed for the table's size). */
3b7bacac 3507
c5aa993b 3508static int
fba45db2 3509simple_read_overlay_table (void)
c906108c 3510{
7c7b6655
TT
3511 struct minimal_symbol *novlys_msym;
3512 struct bound_minimal_symbol ovly_table_msym;
9216df95
UW
3513 struct gdbarch *gdbarch;
3514 int word_size;
e17a4113 3515 enum bfd_endian byte_order;
c906108c
SS
3516
3517 simple_free_overlay_table ();
9b27852e 3518 novlys_msym = lookup_minimal_symbol ("_novlys", NULL, NULL);
0d43edd1 3519 if (! novlys_msym)
c906108c 3520 {
8a3fe4f8 3521 error (_("Error reading inferior's overlay table: "
0d43edd1 3522 "couldn't find `_novlys' variable\n"
8a3fe4f8 3523 "in inferior. Use `overlay manual' mode."));
0d43edd1 3524 return 0;
c906108c 3525 }
0d43edd1 3526
7c7b6655
TT
3527 ovly_table_msym = lookup_bound_minimal_symbol ("_ovly_table");
3528 if (! ovly_table_msym.minsym)
0d43edd1 3529 {
8a3fe4f8 3530 error (_("Error reading inferior's overlay table: couldn't find "
0d43edd1 3531 "`_ovly_table' array\n"
8a3fe4f8 3532 "in inferior. Use `overlay manual' mode."));
0d43edd1
JB
3533 return 0;
3534 }
3535
7c7b6655 3536 gdbarch = get_objfile_arch (ovly_table_msym.objfile);
9216df95 3537 word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
e17a4113 3538 byte_order = gdbarch_byte_order (gdbarch);
9216df95 3539
e17a4113
UW
3540 cache_novlys = read_memory_integer (SYMBOL_VALUE_ADDRESS (novlys_msym),
3541 4, byte_order);
0d43edd1
JB
3542 cache_ovly_table
3543 = (void *) xmalloc (cache_novlys * sizeof (*cache_ovly_table));
7c7b6655 3544 cache_ovly_table_base = SYMBOL_VALUE_ADDRESS (ovly_table_msym.minsym);
0d43edd1 3545 read_target_long_array (cache_ovly_table_base,
777ea8f1 3546 (unsigned int *) cache_ovly_table,
e17a4113 3547 cache_novlys * 4, word_size, byte_order);
0d43edd1 3548
c5aa993b 3549 return 1; /* SUCCESS */
c906108c
SS
3550}
3551
5417f6dc 3552/* Function: simple_overlay_update_1
c906108c
SS
3553 A helper function for simple_overlay_update. Assuming a cached copy
3554 of _ovly_table exists, look through it to find an entry whose vma,
3555 lma and size match those of OSECT. Re-read the entry and make sure
3556 it still matches OSECT (else the table may no longer be valid).
3557 Set OSECT's mapped state to match the entry. Return: 1 for
3558 success, 0 for failure. */
3559
3560static int
fba45db2 3561simple_overlay_update_1 (struct obj_section *osect)
c906108c
SS
3562{
3563 int i, size;
fbd35540
MS
3564 bfd *obfd = osect->objfile->obfd;
3565 asection *bsect = osect->the_bfd_section;
9216df95
UW
3566 struct gdbarch *gdbarch = get_objfile_arch (osect->objfile);
3567 int word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
e17a4113 3568 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c906108c 3569
2c500098 3570 size = bfd_get_section_size (osect->the_bfd_section);
c906108c 3571 for (i = 0; i < cache_novlys; i++)
fbd35540
MS
3572 if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
3573 && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
3574 /* && cache_ovly_table[i][SIZE] == size */ )
c906108c 3575 {
9216df95
UW
3576 read_target_long_array (cache_ovly_table_base + i * word_size,
3577 (unsigned int *) cache_ovly_table[i],
e17a4113 3578 4, word_size, byte_order);
fbd35540
MS
3579 if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
3580 && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
3581 /* && cache_ovly_table[i][SIZE] == size */ )
c906108c
SS
3582 {
3583 osect->ovly_mapped = cache_ovly_table[i][MAPPED];
3584 return 1;
3585 }
c378eb4e 3586 else /* Warning! Warning! Target's ovly table has changed! */
c906108c
SS
3587 return 0;
3588 }
3589 return 0;
3590}
3591
3592/* Function: simple_overlay_update
5417f6dc
RM
3593 If OSECT is NULL, then update all sections' mapped state
3594 (after re-reading the entire target _ovly_table).
3595 If OSECT is non-NULL, then try to find a matching entry in the
c906108c 3596 cached ovly_table and update only OSECT's mapped state.
5417f6dc 3597 If a cached entry can't be found or the cache isn't valid, then
c906108c
SS
3598 re-read the entire cache, and go ahead and update all sections. */
3599
1c772458 3600void
fba45db2 3601simple_overlay_update (struct obj_section *osect)
c906108c 3602{
c5aa993b 3603 struct objfile *objfile;
c906108c 3604
c378eb4e 3605 /* Were we given an osect to look up? NULL means do all of them. */
c906108c 3606 if (osect)
c378eb4e 3607 /* Have we got a cached copy of the target's overlay table? */
c906108c 3608 if (cache_ovly_table != NULL)
9cc89665
MS
3609 {
3610 /* Does its cached location match what's currently in the
3611 symtab? */
3612 struct minimal_symbol *minsym
3613 = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
3614
3615 if (minsym == NULL)
3616 error (_("Error reading inferior's overlay table: couldn't "
3617 "find `_ovly_table' array\n"
3618 "in inferior. Use `overlay manual' mode."));
3619
3620 if (cache_ovly_table_base == SYMBOL_VALUE_ADDRESS (minsym))
3621 /* Then go ahead and try to look up this single section in
3622 the cache. */
3623 if (simple_overlay_update_1 (osect))
3624 /* Found it! We're done. */
3625 return;
3626 }
c906108c
SS
3627
3628 /* Cached table no good: need to read the entire table anew.
3629 Or else we want all the sections, in which case it's actually
3630 more efficient to read the whole table in one block anyway. */
3631
0d43edd1
JB
3632 if (! simple_read_overlay_table ())
3633 return;
3634
c378eb4e 3635 /* Now may as well update all sections, even if only one was requested. */
c906108c 3636 ALL_OBJSECTIONS (objfile, osect)
714835d5 3637 if (section_is_overlay (osect))
c5aa993b
JM
3638 {
3639 int i, size;
fbd35540
MS
3640 bfd *obfd = osect->objfile->obfd;
3641 asection *bsect = osect->the_bfd_section;
c5aa993b 3642
2c500098 3643 size = bfd_get_section_size (bsect);
c5aa993b 3644 for (i = 0; i < cache_novlys; i++)
fbd35540
MS
3645 if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
3646 && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
3647 /* && cache_ovly_table[i][SIZE] == size */ )
c378eb4e 3648 { /* obj_section matches i'th entry in ovly_table. */
c5aa993b 3649 osect->ovly_mapped = cache_ovly_table[i][MAPPED];
c378eb4e 3650 break; /* finished with inner for loop: break out. */
c5aa993b
JM
3651 }
3652 }
c906108c
SS
3653}
3654
086df311
DJ
3655/* Set the output sections and output offsets for section SECTP in
3656 ABFD. The relocation code in BFD will read these offsets, so we
3657 need to be sure they're initialized. We map each section to itself,
3658 with no offset; this means that SECTP->vma will be honored. */
3659
3660static void
3661symfile_dummy_outputs (bfd *abfd, asection *sectp, void *dummy)
3662{
3663 sectp->output_section = sectp;
3664 sectp->output_offset = 0;
3665}
3666
ac8035ab
TG
3667/* Default implementation for sym_relocate. */
3668
ac8035ab
TG
3669bfd_byte *
3670default_symfile_relocate (struct objfile *objfile, asection *sectp,
3671 bfd_byte *buf)
3672{
3019eac3
DE
3673 /* Use sectp->owner instead of objfile->obfd. sectp may point to a
3674 DWO file. */
3675 bfd *abfd = sectp->owner;
ac8035ab
TG
3676
3677 /* We're only interested in sections with relocation
3678 information. */
3679 if ((sectp->flags & SEC_RELOC) == 0)
3680 return NULL;
3681
3682 /* We will handle section offsets properly elsewhere, so relocate as if
3683 all sections begin at 0. */
3684 bfd_map_over_sections (abfd, symfile_dummy_outputs, NULL);
3685
3686 return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
3687}
3688
086df311
DJ
3689/* Relocate the contents of a debug section SECTP in ABFD. The
3690 contents are stored in BUF if it is non-NULL, or returned in a
3691 malloc'd buffer otherwise.
3692
3693 For some platforms and debug info formats, shared libraries contain
3694 relocations against the debug sections (particularly for DWARF-2;
3695 one affected platform is PowerPC GNU/Linux, although it depends on
3696 the version of the linker in use). Also, ELF object files naturally
3697 have unresolved relocations for their debug sections. We need to apply
065a2c74
PA
3698 the relocations in order to get the locations of symbols correct.
3699 Another example that may require relocation processing, is the
3700 DWARF-2 .eh_frame section in .o files, although it isn't strictly a
3701 debug section. */
086df311
DJ
3702
3703bfd_byte *
ac8035ab
TG
3704symfile_relocate_debug_section (struct objfile *objfile,
3705 asection *sectp, bfd_byte *buf)
086df311 3706{
ac8035ab 3707 gdb_assert (objfile->sf->sym_relocate);
086df311 3708
ac8035ab 3709 return (*objfile->sf->sym_relocate) (objfile, sectp, buf);
086df311 3710}
c906108c 3711
31d99776
DJ
3712struct symfile_segment_data *
3713get_symfile_segment_data (bfd *abfd)
3714{
00b5771c 3715 const struct sym_fns *sf = find_sym_fns (abfd);
31d99776
DJ
3716
3717 if (sf == NULL)
3718 return NULL;
3719
3720 return sf->sym_segments (abfd);
3721}
3722
3723void
3724free_symfile_segment_data (struct symfile_segment_data *data)
3725{
3726 xfree (data->segment_bases);
3727 xfree (data->segment_sizes);
3728 xfree (data->segment_info);
3729 xfree (data);
3730}
3731
28c32713
JB
3732/* Given:
3733 - DATA, containing segment addresses from the object file ABFD, and
3734 the mapping from ABFD's sections onto the segments that own them,
3735 and
3736 - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual
3737 segment addresses reported by the target,
3738 store the appropriate offsets for each section in OFFSETS.
3739
3740 If there are fewer entries in SEGMENT_BASES than there are segments
3741 in DATA, then apply SEGMENT_BASES' last entry to all the segments.
3742
8d385431
DJ
3743 If there are more entries, then ignore the extra. The target may
3744 not be able to distinguish between an empty data segment and a
3745 missing data segment; a missing text segment is less plausible. */
3b7bacac 3746
31d99776 3747int
3189cb12
DE
3748symfile_map_offsets_to_segments (bfd *abfd,
3749 const struct symfile_segment_data *data,
31d99776
DJ
3750 struct section_offsets *offsets,
3751 int num_segment_bases,
3752 const CORE_ADDR *segment_bases)
3753{
3754 int i;
3755 asection *sect;
3756
28c32713
JB
3757 /* It doesn't make sense to call this function unless you have some
3758 segment base addresses. */
202b96c1 3759 gdb_assert (num_segment_bases > 0);
28c32713 3760
31d99776
DJ
3761 /* If we do not have segment mappings for the object file, we
3762 can not relocate it by segments. */
3763 gdb_assert (data != NULL);
3764 gdb_assert (data->num_segments > 0);
3765
31d99776
DJ
3766 for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
3767 {
31d99776
DJ
3768 int which = data->segment_info[i];
3769
28c32713
JB
3770 gdb_assert (0 <= which && which <= data->num_segments);
3771
3772 /* Don't bother computing offsets for sections that aren't
3773 loaded as part of any segment. */
3774 if (! which)
3775 continue;
3776
3777 /* Use the last SEGMENT_BASES entry as the address of any extra
3778 segments mentioned in DATA->segment_info. */
31d99776 3779 if (which > num_segment_bases)
28c32713 3780 which = num_segment_bases;
31d99776 3781
28c32713
JB
3782 offsets->offsets[i] = (segment_bases[which - 1]
3783 - data->segment_bases[which - 1]);
31d99776
DJ
3784 }
3785
3786 return 1;
3787}
3788
3789static void
3790symfile_find_segment_sections (struct objfile *objfile)
3791{
3792 bfd *abfd = objfile->obfd;
3793 int i;
3794 asection *sect;
3795 struct symfile_segment_data *data;
3796
3797 data = get_symfile_segment_data (objfile->obfd);
3798 if (data == NULL)
3799 return;
3800
3801 if (data->num_segments != 1 && data->num_segments != 2)
3802 {
3803 free_symfile_segment_data (data);
3804 return;
3805 }
3806
3807 for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
3808 {
31d99776
DJ
3809 int which = data->segment_info[i];
3810
3811 if (which == 1)
3812 {
3813 if (objfile->sect_index_text == -1)
3814 objfile->sect_index_text = sect->index;
3815
3816 if (objfile->sect_index_rodata == -1)
3817 objfile->sect_index_rodata = sect->index;
3818 }
3819 else if (which == 2)
3820 {
3821 if (objfile->sect_index_data == -1)
3822 objfile->sect_index_data = sect->index;
3823
3824 if (objfile->sect_index_bss == -1)
3825 objfile->sect_index_bss = sect->index;
3826 }
3827 }
3828
3829 free_symfile_segment_data (data);
3830}
3831
76ad5e1e
NB
3832/* Listen for free_objfile events. */
3833
3834static void
3835symfile_free_objfile (struct objfile *objfile)
3836{
3837 /* Remove the target sections of user-added objfiles. */
3838 if (objfile != 0 && objfile->flags & OBJF_USERLOADED)
3839 remove_target_sections ((void *) objfile);
3840}
3841
c906108c 3842void
fba45db2 3843_initialize_symfile (void)
c906108c
SS
3844{
3845 struct cmd_list_element *c;
c5aa993b 3846
76ad5e1e
NB
3847 observer_attach_free_objfile (symfile_free_objfile);
3848
1a966eab
AC
3849 c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\
3850Load symbol table from executable file FILE.\n\
c906108c 3851The `file' command can also load symbol tables, as well as setting the file\n\
1a966eab 3852to execute."), &cmdlist);
5ba2abeb 3853 set_cmd_completer (c, filename_completer);
c906108c 3854
1a966eab 3855 c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\
5b96932b 3856Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
3e43a32a
MS
3857Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR>\
3858 ...]\nADDR is the starting address of the file's text.\n\
db162d44
EZ
3859The optional arguments are section-name section-address pairs and\n\
3860should be specified if the data and bss segments are not contiguous\n\
1a966eab 3861with the text. SECT is a section name to be loaded at SECT_ADDR."),
c906108c 3862 &cmdlist);
5ba2abeb 3863 set_cmd_completer (c, filename_completer);
c906108c 3864
63644780
NB
3865 c = add_cmd ("remove-symbol-file", class_files,
3866 remove_symbol_file_command, _("\
3867Remove a symbol file added via the add-symbol-file command.\n\
3868Usage: remove-symbol-file FILENAME\n\
3869 remove-symbol-file -a ADDRESS\n\
3870The file to remove can be identified by its filename or by an address\n\
3871that lies within the boundaries of this symbol file in memory."),
3872 &cmdlist);
3873
1a966eab
AC
3874 c = add_cmd ("load", class_files, load_command, _("\
3875Dynamically load FILE into the running program, and record its symbols\n\
1986bccd
AS
3876for access from GDB.\n\
3877A load OFFSET may also be given."), &cmdlist);
5ba2abeb 3878 set_cmd_completer (c, filename_completer);
c906108c 3879
c5aa993b 3880 add_prefix_cmd ("overlay", class_support, overlay_command,
1bedd215 3881 _("Commands for debugging overlays."), &overlaylist,
c906108c
SS
3882 "overlay ", 0, &cmdlist);
3883
3884 add_com_alias ("ovly", "overlay", class_alias, 1);
3885 add_com_alias ("ov", "overlay", class_alias, 1);
3886
c5aa993b 3887 add_cmd ("map-overlay", class_support, map_overlay_command,
1a966eab 3888 _("Assert that an overlay section is mapped."), &overlaylist);
c906108c 3889
c5aa993b 3890 add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
1a966eab 3891 _("Assert that an overlay section is unmapped."), &overlaylist);
c906108c 3892
c5aa993b 3893 add_cmd ("list-overlays", class_support, list_overlays_command,
1a966eab 3894 _("List mappings of overlay sections."), &overlaylist);
c906108c 3895
c5aa993b 3896 add_cmd ("manual", class_support, overlay_manual_command,
1a966eab 3897 _("Enable overlay debugging."), &overlaylist);
c5aa993b 3898 add_cmd ("off", class_support, overlay_off_command,
1a966eab 3899 _("Disable overlay debugging."), &overlaylist);
c5aa993b 3900 add_cmd ("auto", class_support, overlay_auto_command,
1a966eab 3901 _("Enable automatic overlay debugging."), &overlaylist);
c5aa993b 3902 add_cmd ("load-target", class_support, overlay_load_command,
1a966eab 3903 _("Read the overlay mapping state from the target."), &overlaylist);
c906108c
SS
3904
3905 /* Filename extension to source language lookup table: */
3906 init_filename_language_table ();
26c41df3
AC
3907 add_setshow_string_noescape_cmd ("extension-language", class_files,
3908 &ext_args, _("\
3909Set mapping between filename extension and source language."), _("\
3910Show mapping between filename extension and source language."), _("\
3911Usage: set extension-language .foo bar"),
3912 set_ext_lang_command,
920d2a44 3913 show_ext_args,
26c41df3 3914 &setlist, &showlist);
c906108c 3915
c5aa993b 3916 add_info ("extensions", info_ext_lang_command,
1bedd215 3917 _("All filename extensions associated with a source language."));
917317f4 3918
525226b5
AC
3919 add_setshow_optional_filename_cmd ("debug-file-directory", class_support,
3920 &debug_file_directory, _("\
24ddea62
JK
3921Set the directories where separate debug symbols are searched for."), _("\
3922Show the directories where separate debug symbols are searched for."), _("\
525226b5
AC
3923Separate debug symbols are first searched for in the same\n\
3924directory as the binary, then in the `" DEBUG_SUBDIRECTORY "' subdirectory,\n\
3925and lastly at the path of the directory of the binary with\n\
24ddea62 3926each global debug-file-directory component prepended."),
525226b5 3927 NULL,
920d2a44 3928 show_debug_file_directory,
525226b5 3929 &setlist, &showlist);
c906108c 3930}
This page took 1.994103 seconds and 4 git commands to generate.