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