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