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