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