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