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