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