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