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