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