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