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