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