]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/objfiles.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / objfiles.c
CommitLineData
c906108c 1/* GDB routines for manipulating objfiles.
af5f3db6 2
1d506c26 3 Copyright (C) 1992-2024 Free Software Foundation, Inc.
af5f3db6 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/* This file contains support routines for creating, manipulating, and
0df8b418 23 destroying objfile structures. */
c906108c 24
ef0f16cc 25#include "bfd.h"
c906108c
SS
26#include "symtab.h"
27#include "symfile.h"
28#include "objfiles.h"
c906108c 29#include "target.h"
9bdcbae7
DJ
30#include "expression.h"
31#include "parser-defs.h"
32
c906108c 33#include <sys/types.h>
53ce3c39 34#include <sys/stat.h>
c906108c 35#include <fcntl.h>
bf31fd38 36#include "gdbsupport/gdb_obstack.h"
2de7ced7 37#include "hashtab.h"
c906108c 38
7a292a7a 39#include "breakpoint.h"
fe898f56 40#include "block.h"
de4f826b 41#include "dictionary.h"
cb5d864f 42#include "source.h"
801e3a5b 43#include "addrmap.h"
5e2b427d 44#include "arch-utils.h"
30510692 45#include "exec.h"
76727919 46#include "observable.h"
6fbf07cd 47#include "complaints.h"
cbb099e8 48#include "gdb_bfd.h"
afedecd3 49#include "btrace.h"
268a13a5 50#include "gdbsupport/pathstuff.h"
7a292a7a 51
d0801dd8 52#include <algorithm>
cfe826d4 53
c906108c 54/* Externally visible variables that are owned by this module.
0df8b418 55 See declarations in objfile.h for more info. */
c906108c 56
6c95b8df
PA
57struct objfile_pspace_info
58{
f6aa7436
TT
59 objfile_pspace_info () = default;
60 ~objfile_pspace_info ();
61
62 struct obj_section **sections = nullptr;
63 int num_sections = 0;
607ece04
GB
64
65 /* Nonzero if object files have been added since the section map
66 was last updated. */
f6aa7436 67 int new_objfiles_available = 0;
607ece04
GB
68
69 /* Nonzero if the section map MUST be updated before use. */
f6aa7436 70 int section_map_dirty = 0;
607ece04
GB
71
72 /* Nonzero if section map updates should be inhibited if possible. */
f6aa7436 73 int inhibit_updates = 0;
6c95b8df
PA
74};
75
76/* Per-program-space data key. */
08b8a139 77static const registry<program_space>::key<objfile_pspace_info>
f6aa7436 78 objfiles_pspace_data;
6c95b8df 79
f6aa7436 80objfile_pspace_info::~objfile_pspace_info ()
6c95b8df 81{
f6aa7436 82 xfree (sections);
6c95b8df
PA
83}
84
85/* Get the current svr4 data. If none is found yet, add it now. This
86 function always returns a valid object. */
87
88static struct objfile_pspace_info *
89get_objfile_pspace_data (struct program_space *pspace)
90{
91 struct objfile_pspace_info *info;
92
f6aa7436 93 info = objfiles_pspace_data.get (pspace);
6c95b8df 94 if (info == NULL)
f6aa7436 95 info = objfiles_pspace_data.emplace (pspace);
6c95b8df
PA
96
97 return info;
98}
99
706e3705
TT
100\f
101
102/* Per-BFD data key. */
103
08b8a139 104static const registry<bfd>::key<objfile_per_bfd_storage> objfiles_bfd_data;
706e3705 105
d6797f46
TT
106objfile_per_bfd_storage::~objfile_per_bfd_storage ()
107{
d6797f46
TT
108}
109
706e3705
TT
110/* Create the per-BFD storage object for OBJFILE. If ABFD is not
111 NULL, and it already has a per-BFD storage object, use that.
9161c89a 112 Otherwise, allocate a new per-BFD storage object. */
706e3705 113
88c4cce8
TT
114void
115set_objfile_per_bfd (struct objfile *objfile)
706e3705 116{
88c4cce8 117 bfd *abfd = objfile->obfd.get ();
706e3705
TT
118 struct objfile_per_bfd_storage *storage = NULL;
119
120 if (abfd != NULL)
f6aa7436 121 storage = objfiles_bfd_data.get (abfd);
706e3705
TT
122
123 if (storage == NULL)
124 {
0072c873 125 storage = new objfile_per_bfd_storage (abfd);
1da77581
TT
126 /* If the object requires gdb to do relocations, we simply fall
127 back to not sharing data across users. These cases are rare
128 enough that this seems reasonable. */
129 if (abfd != NULL && !gdb_bfd_requires_relocations (abfd))
f6aa7436 130 objfiles_bfd_data.set (abfd, storage);
88c4cce8
TT
131 else
132 objfile->per_bfd_storage.reset (storage);
706e3705 133
1da77581
TT
134 /* Look up the gdbarch associated with the BFD. */
135 if (abfd != NULL)
136 storage->gdbarch = gdbarch_from_bfd (abfd);
706e3705
TT
137 }
138
88c4cce8 139 objfile->per_bfd = storage;
706e3705
TT
140}
141
3d548a53
TT
142/* Set the objfile's per-BFD notion of the "main" name and
143 language. */
144
145void
146set_objfile_main_name (struct objfile *objfile,
147 const char *name, enum language lang)
148{
149 if (objfile->per_bfd->name_of_main == NULL
150 || strcmp (objfile->per_bfd->name_of_main, name) != 0)
151 objfile->per_bfd->name_of_main
021887d8 152 = obstack_strdup (&objfile->per_bfd->storage_obstack, name);
3d548a53
TT
153 objfile->per_bfd->language_of_main = lang;
154}
155
63e43d3a
PMR
156/* Helper structure to map blocks to static link properties in hash tables. */
157
158struct static_link_htab_entry
159{
160 const struct block *block;
161 const struct dynamic_prop *static_link;
162};
163
164/* Return a hash code for struct static_link_htab_entry *P. */
165
166static hashval_t
167static_link_htab_entry_hash (const void *p)
168{
169 const struct static_link_htab_entry *e
170 = (const struct static_link_htab_entry *) p;
171
172 return htab_hash_pointer (e->block);
173}
174
175/* Return whether P1 an P2 (pointers to struct static_link_htab_entry) are
176 mappings for the same block. */
177
178static int
179static_link_htab_entry_eq (const void *p1, const void *p2)
180{
181 const struct static_link_htab_entry *e1
182 = (const struct static_link_htab_entry *) p1;
183 const struct static_link_htab_entry *e2
184 = (const struct static_link_htab_entry *) p2;
185
186 return e1->block == e2->block;
187}
188
189/* Register STATIC_LINK as the static link for BLOCK, which is part of OBJFILE.
190 Must not be called more than once for each BLOCK. */
191
192void
193objfile_register_static_link (struct objfile *objfile,
194 const struct block *block,
195 const struct dynamic_prop *static_link)
196{
197 void **slot;
198 struct static_link_htab_entry lookup_entry;
199 struct static_link_htab_entry *entry;
200
201 if (objfile->static_links == NULL)
cf250e36 202 objfile->static_links.reset (htab_create_alloc
63e43d3a 203 (1, &static_link_htab_entry_hash, static_link_htab_entry_eq, NULL,
cf250e36 204 xcalloc, xfree));
63e43d3a
PMR
205
206 /* Create a slot for the mapping, make sure it's the first mapping for this
207 block and then create the mapping itself. */
208 lookup_entry.block = block;
cf250e36 209 slot = htab_find_slot (objfile->static_links.get (), &lookup_entry, INSERT);
63e43d3a
PMR
210 gdb_assert (*slot == NULL);
211
e39db4db 212 entry = XOBNEW (&objfile->objfile_obstack, static_link_htab_entry);
63e43d3a
PMR
213 entry->block = block;
214 entry->static_link = static_link;
215 *slot = (void *) entry;
216}
217
218/* Look for a static link for BLOCK, which is part of OBJFILE. Return NULL if
219 none was found. */
220
221const struct dynamic_prop *
222objfile_lookup_static_link (struct objfile *objfile,
223 const struct block *block)
224{
225 struct static_link_htab_entry *entry;
226 struct static_link_htab_entry lookup_entry;
227
228 if (objfile->static_links == NULL)
229 return NULL;
230 lookup_entry.block = block;
cf250e36
TT
231 entry = ((struct static_link_htab_entry *)
232 htab_find (objfile->static_links.get (), &lookup_entry));
63e43d3a
PMR
233 if (entry == NULL)
234 return NULL;
235
236 gdb_assert (entry->block == block);
237 return entry->static_link;
238}
239
706e3705
TT
240\f
241
cb814f2e
TT
242/* Build up the section table that the objfile references. The
243 objfile contains pointers to the start of the table
244 (objfile->sections) and to the first location after the end of the
245 table (objfile->sections_end). */
96baa820 246
65cf3563 247static void
cb814f2e 248add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
65cf3563
TT
249 struct objfile *objfile, int force)
250{
251 struct obj_section *section;
252
253 if (!force)
254 {
255 flagword aflag;
256
fd361982 257 aflag = bfd_section_flags (asect);
65cf3563
TT
258 if (!(aflag & SEC_ALLOC))
259 return;
260 }
261
9ed8433a 262 section = &objfile->sections_start[gdb_bfd_section_index (abfd, asect)];
65cf3563
TT
263 section->objfile = objfile;
264 section->the_bfd_section = asect;
265 section->ovly_mapped = 0;
266}
267
c906108c 268/* Builds a section table for OBJFILE.
96baa820 269
65cf3563
TT
270 Note that the OFFSET and OVLY_MAPPED in each table entry are
271 initialized to zero. */
c906108c 272
d82ea6a8 273void
fba45db2 274build_objfile_section_table (struct objfile *objfile)
c906108c 275{
98badbfd 276 int count = gdb_bfd_count_sections (objfile->obfd.get ());
65cf3563 277
9ed8433a
TT
278 objfile->sections_start = OBSTACK_CALLOC (&objfile->objfile_obstack,
279 count,
280 struct obj_section);
281 objfile->sections_end = (objfile->sections_start + count);
cb814f2e 282 for (asection *sect : gdb_bfd_sections (objfile->obfd))
98badbfd 283 add_to_objfile_sections (objfile->obfd.get (), sect, objfile, 0);
65cf3563
TT
284
285 /* See gdb_bfd_section_index. */
98badbfd
TT
286 add_to_objfile_sections (objfile->obfd.get (), bfd_com_section_ptr,
287 objfile, 1);
288 add_to_objfile_sections (objfile->obfd.get (), bfd_und_section_ptr,
289 objfile, 1);
290 add_to_objfile_sections (objfile->obfd.get (), bfd_abs_section_ptr,
291 objfile, 1);
292 add_to_objfile_sections (objfile->obfd.get (), bfd_ind_section_ptr,
293 objfile, 1);
c906108c
SS
294}
295
9e86da07
TT
296/* Given a pointer to an initialized bfd (ABFD) and some flag bits,
297 initialize the new objfile as best we can and link it into the list
298 of all known objfiles.
c906108c 299
24ba069a
JK
300 NAME should contain original non-canonicalized filename or other
301 identifier as entered by user. If there is no better source use
302 bfd_get_filename (ABFD). NAME may be NULL only if ABFD is NULL.
303 NAME content is copied into returned objfile.
304
2df3850c 305 The FLAGS word contains various bits (OBJF_*) that can be taken as
78a4a9b9 306 requests for specific operations. Other bits like OBJF_SHARED are
0df8b418 307 simply copied through to the new objfile flags member. */
c906108c 308
98badbfd 309objfile::objfile (gdb_bfd_ref_ptr bfd_, const char *name, objfile_flags flags_)
9e86da07
TT
310 : flags (flags_),
311 pspace (current_program_space),
98badbfd 312 obfd (std::move (bfd_))
c906108c 313{
14278e1f 314 const char *expanded_name;
c906108c 315
7ab2607f 316 std::string name_holder;
24ba069a
JK
317 if (name == NULL)
318 {
98badbfd 319 gdb_assert (obfd == nullptr);
40135bb1 320 gdb_assert ((flags & OBJF_NOT_FILENAME) != 0);
e3e41d58 321 expanded_name = "<<anonymous objfile>>";
24ba069a 322 }
5fbae7d1
GB
323 else if ((flags & OBJF_NOT_FILENAME) != 0
324 || is_target_filename (name))
e3e41d58 325 expanded_name = name;
04affae3 326 else
e3e41d58
TT
327 {
328 name_holder = gdb_abspath (name);
7ab2607f 329 expanded_name = name_holder.c_str ();
e3e41d58 330 }
021887d8 331 original_name = obstack_strdup (&objfile_obstack, expanded_name);
04affae3 332
d3e81981
DE
333 /* Update the per-objfile information that comes from the bfd, ensuring
334 that any data that is reference is saved in the per-objfile data
335 region. */
336
98badbfd 337 if (obfd != nullptr)
c906108c 338 {
98badbfd 339 mtime = bfd_get_mtime (obfd.get ());
c906108c
SS
340
341 /* Build section table. */
9e86da07 342 build_objfile_section_table (this);
c906108c
SS
343 }
344
88c4cce8 345 set_objfile_per_bfd (this);
c906108c
SS
346}
347
abd0a5fa
JK
348/* If there is a valid and known entry point, function fills *ENTRY_P with it
349 and returns non-zero; otherwise it returns zero. */
9ab9195f 350
abd0a5fa
JK
351int
352entry_point_address_query (CORE_ADDR *entry_p)
9ab9195f 353{
a42d7dd8
TT
354 objfile *objf = current_program_space->symfile_object_file;
355 if (objf == NULL || !objf->per_bfd->ei.entry_point_p)
3612b192
DJ
356 return 0;
357
a42d7dd8
TT
358 int idx = objf->per_bfd->ei.the_bfd_section_index;
359 *entry_p = objf->per_bfd->ei.entry_point + objf->section_offsets[idx];
3612b192 360
abd0a5fa
JK
361 return 1;
362}
363
364/* Get current entry point address. Call error if it is not known. */
365
366CORE_ADDR
367entry_point_address (void)
368{
369 CORE_ADDR retval;
370
371 if (!entry_point_address_query (&retval))
372 error (_("Entry point address is not known."));
373
374 return retval;
9ab9195f 375}
15831452 376
e9ad22ee
TT
377separate_debug_iterator &
378separate_debug_iterator::operator++ ()
15d123c9 379{
e9ad22ee
TT
380 gdb_assert (m_objfile != nullptr);
381
15d123c9
TG
382 struct objfile *res;
383
399f313b 384 /* If any, return the first child. */
e9ad22ee
TT
385 res = m_objfile->separate_debug_objfile;
386 if (res != nullptr)
387 {
388 m_objfile = res;
389 return *this;
390 }
15d123c9 391
15d123c9 392 /* Common case where there is no separate debug objfile. */
e9ad22ee
TT
393 if (m_objfile == m_parent)
394 {
395 m_objfile = nullptr;
396 return *this;
397 }
15d123c9 398
399f313b
TG
399 /* Return the brother if any. Note that we don't iterate on brothers of
400 the parents. */
e9ad22ee
TT
401 res = m_objfile->separate_debug_objfile_link;
402 if (res != nullptr)
403 {
404 m_objfile = res;
405 return *this;
406 }
399f313b 407
e9ad22ee
TT
408 for (res = m_objfile->separate_debug_objfile_backlink;
409 res != m_parent;
15d123c9
TG
410 res = res->separate_debug_objfile_backlink)
411 {
e9ad22ee
TT
412 gdb_assert (res != nullptr);
413 if (res->separate_debug_objfile_link != nullptr)
414 {
415 m_objfile = res->separate_debug_objfile_link;
416 return *this;
417 }
15d123c9 418 }
e9ad22ee
TT
419 m_objfile = nullptr;
420 return *this;
15d123c9 421}
15831452 422
15d123c9
TG
423/* Add OBJFILE as a separate debug objfile of PARENT. */
424
f65fe570 425static void
15d123c9
TG
426add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
427{
428 gdb_assert (objfile && parent);
429
430 /* Must not be already in a list. */
431 gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
432 gdb_assert (objfile->separate_debug_objfile_link == NULL);
8a92335b
JK
433 gdb_assert (objfile->separate_debug_objfile == NULL);
434 gdb_assert (parent->separate_debug_objfile_backlink == NULL);
435 gdb_assert (parent->separate_debug_objfile_link == NULL);
15d123c9
TG
436
437 objfile->separate_debug_objfile_backlink = parent;
438 objfile->separate_debug_objfile_link = parent->separate_debug_objfile;
439 parent->separate_debug_objfile = objfile;
15d123c9
TG
440}
441
f65fe570
TT
442/* See objfiles.h. */
443
444objfile *
98badbfd 445objfile::make (gdb_bfd_ref_ptr bfd_, const char *name_, objfile_flags flags_,
f65fe570
TT
446 objfile *parent)
447{
98badbfd 448 objfile *result = new objfile (std::move (bfd_), name_, flags_);
f65fe570
TT
449 if (parent != nullptr)
450 add_separate_debug_objfile (result, parent);
7cac64af 451
e2904e1f 452 current_program_space->add_objfile (std::unique_ptr<objfile> (result),
7d7167ce 453 parent);
7cac64af
TT
454
455 /* Rebuild section map next time we need it. */
456 get_objfile_pspace_data (current_program_space)->new_objfiles_available = 1;
457
f65fe570
TT
458 return result;
459}
460
268e4f09
TT
461/* See objfiles.h. */
462
463void
464objfile::unlink ()
465{
23452926 466 current_program_space->remove_objfile (this);
268e4f09
TT
467}
468
15d123c9
TG
469/* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
470 itself. */
471
472void
473free_objfile_separate_debug (struct objfile *objfile)
474{
475 struct objfile *child;
476
477 for (child = objfile->separate_debug_objfile; child;)
478 {
479 struct objfile *next_child = child->separate_debug_objfile_link;
268e4f09 480 child->unlink ();
15d123c9
TG
481 child = next_child;
482 }
483}
c906108c 484
7580e917 485/* Destroy an objfile and all the symtabs and psymtabs under it. */
c906108c 486
9e86da07 487objfile::~objfile ()
c906108c 488{
63644780 489 /* First notify observers that this objfile is about to be freed. */
76727919 490 gdb::observers::free_objfile.notify (this);
63644780 491
15d123c9 492 /* Free all separate debug objfiles. */
9e86da07 493 free_objfile_separate_debug (this);
15d123c9 494
9e86da07 495 if (separate_debug_objfile_backlink)
5b5d99cf
JB
496 {
497 /* We freed the separate debug file, make sure the base objfile
498 doesn't reference it. */
15d123c9
TG
499 struct objfile *child;
500
9e86da07 501 child = separate_debug_objfile_backlink->separate_debug_objfile;
15d123c9 502
9e86da07 503 if (child == this)
dda83cd7
SM
504 {
505 /* THIS is the first child. */
506 separate_debug_objfile_backlink->separate_debug_objfile =
507 separate_debug_objfile_link;
508 }
15d123c9 509 else
dda83cd7
SM
510 {
511 /* Find THIS in the list. */
512 while (1)
513 {
514 if (child->separate_debug_objfile_link == this)
515 {
516 child->separate_debug_objfile_link =
517 separate_debug_objfile_link;
518 break;
519 }
520 child = child->separate_debug_objfile_link;
521 gdb_assert (child);
522 }
523 }
5b5d99cf 524 }
9e86da07 525
ae5a43e0
DJ
526 /* Remove any references to this objfile in the global value
527 lists. */
9e86da07 528 preserve_values (this);
ae5a43e0 529
9f743ef6
JK
530 /* It still may reference data modules have associated with the objfile and
531 the symbol file data. */
21f6be77 532 forget_cached_source_info ();
9f743ef6 533
9e86da07
TT
534 breakpoint_free_objfile (this);
535 btrace_free_objfile (this);
2f202fde 536
c906108c
SS
537 /* First do any symbol file specific actions required when we are
538 finished with a particular symbol file. Note that if the objfile
539 is using reusable symbol information (via mmalloc) then each of
540 these routines is responsible for doing the correct thing, either
541 freeing things which are valid only during this particular gdb
0df8b418 542 execution, or leaving them to be reused during the next one. */
c906108c 543
9e86da07
TT
544 if (sf != NULL)
545 (*sf->sym_finish) (this);
c906108c 546
c906108c
SS
547 /* Before the symbol table code was redone to make it easier to
548 selectively load and remove information particular to a specific
549 linkage unit, gdb used to do these things whenever the monolithic
550 symbol table was blown away. How much still needs to be done
551 is unknown, but we play it safe for now and keep each action until
0df8b418 552 it is shown to be no longer needed. */
c5aa993b 553
cb5d864f
FF
554 /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
555 for example), so we need to call this here. */
c906108c
SS
556 clear_pc_function_cache ();
557
cb5d864f 558 /* Check to see if the current_source_symtab belongs to this objfile,
0df8b418 559 and if so, call clear_current_source_symtab_and_line. */
cb5d864f
FF
560
561 {
562 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
cb5d864f 563
3c86fae3 564 if (cursal.symtab && cursal.symtab->compunit ()->objfile () == this)
00174a86 565 clear_current_source_symtab_and_line ();
cb5d864f
FF
566 }
567
6c95b8df 568 /* Rebuild section map next time we need it. */
9e86da07 569 get_objfile_pspace_data (pspace)->section_map_dirty = 1;
c906108c
SS
570}
571
c906108c 572\f
34eaf542
TT
573/* A helper function for objfile_relocate1 that relocates a single
574 symbol. */
575
576static void
577relocate_one_symbol (struct symbol *sym, struct objfile *objfile,
6a053cb1 578 const section_offsets &delta)
34eaf542 579{
34eaf542
TT
580 /* The RS6000 code from which this was taken skipped
581 any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
582 But I'm leaving out that test, on the theory that
583 they can't possibly pass the tests below. */
66d7f48f
SM
584 if ((sym->aclass () == LOC_LABEL
585 || sym->aclass () == LOC_STATIC)
a52d653e 586 && sym->section_index () >= 0)
4aeddc50
SM
587 sym->set_value_address (sym->value_address ()
588 + delta[sym->section_index ()]);
34eaf542
TT
589}
590
c906108c 591/* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
b260e109
JK
592 entries in new_offsets. SEPARATE_DEBUG_OBJFILE is not touched here.
593 Return non-zero iff any change happened. */
567995e1 594
b260e109 595static int
5cc80db3 596objfile_relocate1 (struct objfile *objfile,
6a053cb1 597 const section_offsets &new_offsets)
c906108c 598{
6a053cb1 599 section_offsets delta (objfile->section_offsets.size ());
c906108c 600
5cc80db3
MS
601 int something_changed = 0;
602
6a053cb1 603 for (int i = 0; i < objfile->section_offsets.size (); ++i)
5cc80db3 604 {
6a053cb1
TT
605 delta[i] = new_offsets[i] - objfile->section_offsets[i];
606 if (delta[i] != 0)
5cc80db3
MS
607 something_changed = 1;
608 }
609 if (!something_changed)
610 return 0;
c906108c
SS
611
612 /* OK, get all the symtabs. */
82c5090c
TT
613 for (compunit_symtab *cust : objfile->compunits ())
614 {
615 struct blockvector *bv = cust->blockvector ();
616 int block_line_section = SECT_OFF_TEXT (objfile);
c906108c 617
82c5090c
TT
618 if (bv->map () != nullptr)
619 bv->map ()->relocate (delta[block_line_section]);
34eaf542 620
82c5090c
TT
621 for (block *b : bv->blocks ())
622 {
82c5090c
TT
623 b->set_start (b->start () + delta[block_line_section]);
624 b->set_end (b->end () + delta[block_line_section]);
5cc80db3 625
82c5090c
TT
626 for (blockrange &r : b->ranges ())
627 {
628 r.set_start (r.start () + delta[block_line_section]);
629 r.set_end (r.end () + delta[block_line_section]);
630 }
631
632 /* We only want to iterate over the local symbols, not any
633 symbols in included symtabs. */
8e8d48f9
TT
634 for (struct symbol *sym : b->multidict_symbols ())
635 relocate_one_symbol (sym, objfile, delta);
82c5090c
TT
636 }
637 }
638
639 /* Relocate isolated symbols. */
640 for (symbol *iter = objfile->template_symbols; iter; iter = iter->hash_next)
641 relocate_one_symbol (iter, objfile, delta);
642
643 for (int i = 0; i < objfile->section_offsets.size (); ++i)
644 objfile->section_offsets[i] = new_offsets[i];
f1f2b5f4
PA
645
646 /* Rebuild section map next time we need it. */
607ece04 647 get_objfile_pspace_data (objfile->pspace)->section_map_dirty = 1;
f1f2b5f4 648
30510692 649 /* Update the table in exec_ops, used to read memory. */
5250cbc8 650 for (obj_section *s : objfile->sections ())
30510692 651 {
9ed8433a 652 int idx = s - objfile->sections_start;
30510692 653
98badbfd 654 exec_set_section_address (bfd_get_filename (objfile->obfd.get ()), idx,
0c1bcd23 655 s->addr ());
30510692 656 }
b260e109
JK
657
658 /* Data changed. */
659 return 1;
567995e1
JK
660}
661
662/* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
663 entries in new_offsets. Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
664
665 The number and ordering of sections does differ between the two objfiles.
666 Only their names match. Also the file offsets will differ (objfile being
667 possibly prelinked but separate_debug_objfile is probably not prelinked) but
668 the in-memory absolute address as specified by NEW_OFFSETS must match both
669 files. */
670
671void
3189cb12 672objfile_relocate (struct objfile *objfile,
6a053cb1 673 const section_offsets &new_offsets)
567995e1 674{
b260e109 675 int changed = 0;
567995e1 676
b260e109 677 changed |= objfile_relocate1 (objfile, new_offsets);
567995e1 678
bde09ab7 679 for (::objfile *debug_objfile : objfile->separate_debug_objfiles ())
567995e1 680 {
e9ad22ee
TT
681 if (debug_objfile == objfile)
682 continue;
683
37e136b1
TT
684 section_addr_info objfile_addrs
685 = build_section_addr_info_from_objfile (objfile);
567995e1
JK
686
687 /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
688 relative ones must be already created according to debug_objfile. */
689
98badbfd 690 addr_info_make_relative (&objfile_addrs, debug_objfile->obfd.get ());
567995e1 691
6a053cb1 692 gdb_assert (debug_objfile->section_offsets.size ()
98badbfd 693 == gdb_bfd_count_sections (debug_objfile->obfd.get ()));
6a053cb1
TT
694 section_offsets new_debug_offsets
695 (debug_objfile->section_offsets.size ());
696 relative_addr_info_to_section_offsets (new_debug_offsets, objfile_addrs);
567995e1 697
6a053cb1 698 changed |= objfile_relocate1 (debug_objfile, new_debug_offsets);
567995e1 699 }
30510692 700
0df8b418 701 /* Relocate breakpoints as necessary, after things are relocated. */
b260e109
JK
702 if (changed)
703 breakpoint_re_set ();
c906108c 704}
4141a416
JB
705
706/* Rebase (add to the offsets) OBJFILE by SLIDE. SEPARATE_DEBUG_OBJFILE is
707 not touched here.
708 Return non-zero iff any change happened. */
709
710static int
711objfile_rebase1 (struct objfile *objfile, CORE_ADDR slide)
712{
6a053cb1 713 section_offsets new_offsets (objfile->section_offsets.size (), slide);
4141a416
JB
714 return objfile_relocate1 (objfile, new_offsets);
715}
716
717/* Rebase (add to the offsets) OBJFILE by SLIDE. Process also OBJFILE's
718 SEPARATE_DEBUG_OBJFILEs. */
719
720void
721objfile_rebase (struct objfile *objfile, CORE_ADDR slide)
722{
4141a416
JB
723 int changed = 0;
724
bde09ab7 725 for (::objfile *debug_objfile : objfile->separate_debug_objfiles ())
4141a416
JB
726 changed |= objfile_rebase1 (debug_objfile, slide);
727
728 /* Relocate breakpoints as necessary, after things are relocated. */
729 if (changed)
730 breakpoint_re_set ();
731}
c906108c 732\f
55333a84
DE
733/* Return non-zero if OBJFILE has full symbols. */
734
735int
736objfile_has_full_symbols (struct objfile *objfile)
737{
43f3e411 738 return objfile->compunit_symtabs != NULL;
55333a84
DE
739}
740
e361b228 741/* Return non-zero if OBJFILE has full or partial symbols, either directly
15d123c9 742 or through a separate debug file. */
e361b228
TG
743
744int
745objfile_has_symbols (struct objfile *objfile)
746{
bde09ab7 747 for (::objfile *o : objfile->separate_debug_objfiles ())
a8ad4f3c 748 if (o->has_partial_symbols () || objfile_has_full_symbols (o))
15d123c9 749 return 1;
e361b228
TG
750 return 0;
751}
752
753
c906108c
SS
754/* Many places in gdb want to test just to see if we have any partial
755 symbols available. This function returns zero if none are currently
0df8b418 756 available, nonzero otherwise. */
c906108c
SS
757
758int
fba45db2 759have_partial_symbols (void)
c906108c 760{
2030c079 761 for (objfile *ofp : current_program_space->objfiles ())
aed57c53 762 {
a8ad4f3c 763 if (ofp->has_partial_symbols ())
aed57c53
TT
764 return 1;
765 }
c906108c
SS
766 return 0;
767}
768
769/* Many places in gdb want to test just to see if we have any full
770 symbols available. This function returns zero if none are currently
0df8b418 771 available, nonzero otherwise. */
c906108c
SS
772
773int
fba45db2 774have_full_symbols (void)
c906108c 775{
2030c079 776 for (objfile *ofp : current_program_space->objfiles ())
aed57c53
TT
777 {
778 if (objfile_has_full_symbols (ofp))
779 return 1;
780 }
c906108c
SS
781 return 0;
782}
783
784
785/* This operations deletes all objfile entries that represent solibs that
786 weren't explicitly loaded by the user, via e.g., the add-symbol-file
0df8b418
MS
787 command. */
788
c906108c 789void
fba45db2 790objfile_purge_solibs (void)
c906108c 791{
7e955d83 792 for (objfile *objf : current_program_space->objfiles_safe ())
cac85af2
TT
793 {
794 /* We assume that the solib package has been purged already, or will
795 be soon. */
0df8b418 796
cac85af2 797 if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
268e4f09 798 objf->unlink ();
cac85af2 799 }
c906108c
SS
800}
801
802
803/* Many places in gdb want to test just to see if we have any minimal
804 symbols available. This function returns zero if none are currently
0df8b418 805 available, nonzero otherwise. */
c906108c
SS
806
807int
fba45db2 808have_minimal_symbols (void)
c906108c 809{
2030c079 810 for (objfile *ofp : current_program_space->objfiles ())
aed57c53
TT
811 {
812 if (ofp->per_bfd->minimal_symbol_count > 0)
813 {
814 return 1;
815 }
816 }
c906108c
SS
817 return 0;
818}
819
a845f5cb
PP
820/* Qsort comparison function. */
821
39ef2f62
CB
822static bool
823sort_cmp (const struct obj_section *sect1, const obj_section *sect2)
a845f5cb 824{
0c1bcd23
SM
825 const CORE_ADDR sect1_addr = sect1->addr ();
826 const CORE_ADDR sect2_addr = sect2->addr ();
a845f5cb
PP
827
828 if (sect1_addr < sect2_addr)
39ef2f62 829 return true;
a845f5cb 830 else if (sect1_addr > sect2_addr)
39ef2f62 831 return false;
6fbf07cd 832 else
5cc80db3
MS
833 {
834 /* Sections are at the same address. This could happen if
835 A) we have an objfile and a separate debuginfo.
836 B) we are confused, and have added sections without proper relocation,
0df8b418 837 or something like that. */
5cc80db3
MS
838
839 const struct objfile *const objfile1 = sect1->objfile;
840 const struct objfile *const objfile2 = sect2->objfile;
841
842 if (objfile1->separate_debug_objfile == objfile2
843 || objfile2->separate_debug_objfile == objfile1)
844 {
845 /* Case A. The ordering doesn't matter: separate debuginfo files
846 will be filtered out later. */
847
39ef2f62 848 return false;
5cc80db3
MS
849 }
850
851 /* Case B. Maintain stable sort order, so bugs in GDB are easier to
852 triage. This section could be slow (since we iterate over all
39ef2f62 853 objfiles in each call to sort_cmp), but this shouldn't happen
5cc80db3
MS
854 very often (GDB is already in a confused state; one hopes this
855 doesn't happen at all). If you discover that significant time is
856 spent in the loops below, do 'set complaints 100' and examine the
857 resulting complaints. */
5cc80db3
MS
858 if (objfile1 == objfile2)
859 {
45f47c3a
AB
860 /* Both sections came from the same objfile. We are really
861 confused. Sort on sequence order of sections within the
862 objfile. The order of checks is important here, if we find a
863 match on SECT2 first then either SECT2 is before SECT1, or,
864 SECT2 == SECT1, in both cases we should return false. The
865 second case shouldn't occur during normal use, but std::sort
866 does check that '!(a < a)' when compiled in debug mode. */
5cc80db3 867
5250cbc8 868 for (const obj_section *osect : objfile1->sections ())
45f47c3a 869 if (osect == sect2)
39ef2f62 870 return false;
45f47c3a
AB
871 else if (osect == sect1)
872 return true;
5cc80db3
MS
873
874 /* We should have found one of the sections before getting here. */
f3574227 875 gdb_assert_not_reached ("section not found");
5cc80db3
MS
876 }
877 else
878 {
879 /* Sort on sequence number of the objfile in the chain. */
880
2030c079 881 for (objfile *objfile : current_program_space->objfiles ())
5cc80db3 882 if (objfile == objfile1)
39ef2f62 883 return true;
5cc80db3 884 else if (objfile == objfile2)
39ef2f62 885 return false;
5cc80db3
MS
886
887 /* We should have found one of the objfiles before getting here. */
f3574227 888 gdb_assert_not_reached ("objfile not found");
5cc80db3
MS
889 }
890 }
6fbf07cd
PP
891
892 /* Unreachable. */
f3574227 893 gdb_assert_not_reached ("unexpected code path");
39ef2f62 894 return false;
a845f5cb
PP
895}
896
3aad21cf
PP
897/* Select "better" obj_section to keep. We prefer the one that came from
898 the real object, rather than the one from separate debuginfo.
899 Most of the time the two sections are exactly identical, but with
900 prelinking the .rel.dyn section in the real object may have different
901 size. */
902
903static struct obj_section *
904preferred_obj_section (struct obj_section *a, struct obj_section *b)
905{
0c1bcd23 906 gdb_assert (a->addr () == b->addr ());
3aad21cf
PP
907 gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
908 || (b->objfile->separate_debug_objfile == a->objfile));
909 gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
910 || (b->objfile->separate_debug_objfile_backlink == a->objfile));
911
912 if (a->objfile->separate_debug_objfile != NULL)
913 return a;
914 return b;
915}
916
6fbf07cd 917/* Return 1 if SECTION should be inserted into the section map.
625b6eae 918 We want to insert only non-overlay non-TLS non-empty sections. */
6fbf07cd
PP
919
920static int
921insert_section_p (const struct bfd *abfd,
922 const struct bfd_section *section)
923{
fd361982 924 const bfd_vma lma = bfd_section_lma (section);
6fbf07cd 925
fd361982 926 if (overlay_debugging && lma != 0 && lma != bfd_section_vma (section)
6fbf07cd
PP
927 && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0)
928 /* This is an overlay section. IN_MEMORY check is needed to avoid
929 discarding sections from the "system supplied DSO" (aka vdso)
930 on some Linux systems (e.g. Fedora 11). */
931 return 0;
fd361982 932 if ((bfd_section_flags (section) & SEC_THREAD_LOCAL) != 0)
6fbf07cd
PP
933 /* This is a TLS section. */
934 return 0;
625b6eae
IL
935 if (bfd_section_size (section) == 0)
936 {
937 /* This is an empty section. It has no PCs for find_pc_section (), so
938 there is no reason to insert it into the section map. */
939 return 0;
940 }
6fbf07cd
PP
941
942 return 1;
943}
944
945/* Filter out overlapping sections where one section came from the real
946 objfile, and the other from a separate debuginfo file.
947 Return the size of table after redundant sections have been eliminated. */
948
949static int
950filter_debuginfo_sections (struct obj_section **map, int map_size)
951{
952 int i, j;
953
954 for (i = 0, j = 0; i < map_size - 1; i++)
955 {
956 struct obj_section *const sect1 = map[i];
957 struct obj_section *const sect2 = map[i + 1];
958 const struct objfile *const objfile1 = sect1->objfile;
959 const struct objfile *const objfile2 = sect2->objfile;
0c1bcd23
SM
960 const CORE_ADDR sect1_addr = sect1->addr ();
961 const CORE_ADDR sect2_addr = sect2->addr ();
6fbf07cd
PP
962
963 if (sect1_addr == sect2_addr
964 && (objfile1->separate_debug_objfile == objfile2
965 || objfile2->separate_debug_objfile == objfile1))
966 {
967 map[j++] = preferred_obj_section (sect1, sect2);
968 ++i;
969 }
970 else
971 map[j++] = sect1;
972 }
973
974 if (i < map_size)
975 {
976 gdb_assert (i == map_size - 1);
977 map[j++] = map[i];
978 }
979
980 /* The map should not have shrunk to less than half the original size. */
981 gdb_assert (map_size / 2 <= j);
982
983 return j;
984}
985
986/* Filter out overlapping sections, issuing a warning if any are found.
987 Overlapping sections could really be overlay sections which we didn't
988 classify as such in insert_section_p, or we could be dealing with a
989 corrupt binary. */
990
991static int
992filter_overlapping_sections (struct obj_section **map, int map_size)
993{
994 int i, j;
995
996 for (i = 0, j = 0; i < map_size - 1; )
997 {
998 int k;
999
1000 map[j++] = map[i];
1001 for (k = i + 1; k < map_size; k++)
1002 {
1003 struct obj_section *const sect1 = map[i];
1004 struct obj_section *const sect2 = map[k];
0c1bcd23
SM
1005 const CORE_ADDR sect1_addr = sect1->addr ();
1006 const CORE_ADDR sect2_addr = sect2->addr ();
1007 const CORE_ADDR sect1_endaddr = sect1->endaddr ();
6fbf07cd
PP
1008
1009 gdb_assert (sect1_addr <= sect2_addr);
1010
1011 if (sect1_endaddr <= sect2_addr)
1012 break;
1013 else
1014 {
1015 /* We have an overlap. Report it. */
1016
1017 struct objfile *const objf1 = sect1->objfile;
1018 struct objfile *const objf2 = sect2->objfile;
1019
6fbf07cd
PP
1020 const struct bfd_section *const bfds1 = sect1->the_bfd_section;
1021 const struct bfd_section *const bfds2 = sect2->the_bfd_section;
1022
0c1bcd23 1023 const CORE_ADDR sect2_endaddr = sect2->endaddr ();
6fbf07cd 1024
08feed99 1025 struct gdbarch *const gdbarch = objf1->arch ();
6fbf07cd 1026
b98664d3 1027 complaint (_("unexpected overlap between:\n"
6fbf07cd
PP
1028 " (A) section `%s' from `%s' [%s, %s)\n"
1029 " (B) section `%s' from `%s' [%s, %s).\n"
1030 "Will ignore section B"),
fd361982 1031 bfd_section_name (bfds1), objfile_name (objf1),
6fbf07cd
PP
1032 paddress (gdbarch, sect1_addr),
1033 paddress (gdbarch, sect1_endaddr),
fd361982 1034 bfd_section_name (bfds2), objfile_name (objf2),
6fbf07cd
PP
1035 paddress (gdbarch, sect2_addr),
1036 paddress (gdbarch, sect2_endaddr));
1037 }
1038 }
1039 i = k;
1040 }
1041
1042 if (i < map_size)
1043 {
1044 gdb_assert (i == map_size - 1);
1045 map[j++] = map[i];
1046 }
1047
1048 return j;
1049}
1050
1051
1052/* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
1053 TLS, overlay and overlapping sections. */
a845f5cb
PP
1054
1055static void
6c95b8df
PA
1056update_section_map (struct program_space *pspace,
1057 struct obj_section ***pmap, int *pmap_size)
a845f5cb 1058{
607ece04 1059 struct objfile_pspace_info *pspace_info;
6fbf07cd 1060 int alloc_size, map_size, i;
5250cbc8 1061 struct obj_section **map;
a845f5cb 1062
607ece04
GB
1063 pspace_info = get_objfile_pspace_data (pspace);
1064 gdb_assert (pspace_info->section_map_dirty != 0
1065 || pspace_info->new_objfiles_available != 0);
a845f5cb
PP
1066
1067 map = *pmap;
1068 xfree (map);
1069
6fbf07cd 1070 alloc_size = 0;
2030c079 1071 for (objfile *objfile : pspace->objfiles ())
5250cbc8 1072 for (obj_section *s : objfile->sections ())
98badbfd 1073 if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
6c95b8df 1074 alloc_size += 1;
a845f5cb 1075
65a97ab3
PP
1076 /* This happens on detach/attach (e.g. in gdb.base/attach.exp). */
1077 if (alloc_size == 0)
1078 {
1079 *pmap = NULL;
1080 *pmap_size = 0;
1081 return;
1082 }
1083
8d749320 1084 map = XNEWVEC (struct obj_section *, alloc_size);
a845f5cb 1085
3aad21cf 1086 i = 0;
2030c079 1087 for (objfile *objfile : pspace->objfiles ())
5250cbc8 1088 for (obj_section *s : objfile->sections ())
98badbfd 1089 if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
6c95b8df 1090 map[i++] = s;
a845f5cb 1091
39ef2f62 1092 std::sort (map, map + alloc_size, sort_cmp);
6fbf07cd
PP
1093 map_size = filter_debuginfo_sections(map, alloc_size);
1094 map_size = filter_overlapping_sections(map, map_size);
a845f5cb 1095
6fbf07cd
PP
1096 if (map_size < alloc_size)
1097 /* Some sections were eliminated. Trim excess space. */
224c3ddb 1098 map = XRESIZEVEC (struct obj_section *, map, map_size);
3aad21cf 1099 else
6fbf07cd 1100 gdb_assert (alloc_size == map_size);
3aad21cf 1101
a845f5cb
PP
1102 *pmap = map;
1103 *pmap_size = map_size;
1104}
1105
0df8b418 1106/* Bsearch comparison function. */
a845f5cb
PP
1107
1108static int
1109bsearch_cmp (const void *key, const void *elt)
1110{
1111 const CORE_ADDR pc = *(CORE_ADDR *) key;
1112 const struct obj_section *section = *(const struct obj_section **) elt;
1113
0c1bcd23 1114 if (pc < section->addr ())
a845f5cb 1115 return -1;
0c1bcd23 1116 if (pc < section->endaddr ())
a845f5cb
PP
1117 return 0;
1118 return 1;
1119}
1120
714835d5 1121/* Returns a section whose range includes PC or NULL if none found. */
c906108c
SS
1122
1123struct obj_section *
714835d5 1124find_pc_section (CORE_ADDR pc)
c906108c 1125{
6c95b8df 1126 struct objfile_pspace_info *pspace_info;
a845f5cb 1127 struct obj_section *s, **sp;
c5aa993b 1128
714835d5
UW
1129 /* Check for mapped overlay section first. */
1130 s = find_pc_mapped_section (pc);
1131 if (s)
1132 return s;
c906108c 1133
6c95b8df 1134 pspace_info = get_objfile_pspace_data (current_program_space);
607ece04
GB
1135 if (pspace_info->section_map_dirty
1136 || (pspace_info->new_objfiles_available
1137 && !pspace_info->inhibit_updates))
a845f5cb 1138 {
6c95b8df
PA
1139 update_section_map (current_program_space,
1140 &pspace_info->sections,
1141 &pspace_info->num_sections);
c906108c 1142
6c95b8df 1143 /* Don't need updates to section map until objfiles are added,
dda83cd7 1144 removed or relocated. */
607ece04
GB
1145 pspace_info->new_objfiles_available = 0;
1146 pspace_info->section_map_dirty = 0;
a845f5cb
PP
1147 }
1148
65a97ab3
PP
1149 /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1150 bsearch be non-NULL. */
1151 if (pspace_info->sections == NULL)
1152 {
1153 gdb_assert (pspace_info->num_sections == 0);
1154 return NULL;
1155 }
1156
6c95b8df
PA
1157 sp = (struct obj_section **) bsearch (&pc,
1158 pspace_info->sections,
1159 pspace_info->num_sections,
1160 sizeof (*pspace_info->sections),
1161 bsearch_cmp);
a845f5cb
PP
1162 if (sp != NULL)
1163 return *sp;
714835d5 1164 return NULL;
c906108c 1165}
c5aa993b 1166
c906108c 1167
3e5d3a5a 1168/* Return non-zero if PC is in a section called NAME. */
c906108c 1169
6ec27270 1170bool
a121b7c1 1171pc_in_section (CORE_ADDR pc, const char *name)
c906108c 1172{
6ec27270
TT
1173 struct obj_section *s = find_pc_section (pc);
1174 return (s != nullptr
1175 && s->the_bfd_section->name != nullptr
1176 && strcmp (s->the_bfd_section->name, name) == 0);
c906108c 1177}
0d0e1a63
MK
1178\f
1179
607ece04 1180/* Set section_map_dirty so section map will be rebuilt next time it
bb272892 1181 is used. Called by reread_symbols. */
a845f5cb
PP
1182
1183void
bb272892 1184objfiles_changed (void)
a845f5cb 1185{
6c95b8df 1186 /* Rebuild section map next time we need it. */
607ece04
GB
1187 get_objfile_pspace_data (current_program_space)->section_map_dirty = 1;
1188}
1189
1190/* See comments in objfiles.h. */
1191
06424eac 1192scoped_restore_tmpl<int>
607ece04
GB
1193inhibit_section_map_updates (struct program_space *pspace)
1194{
06424eac
TT
1195 return scoped_restore_tmpl<int>
1196 (&get_objfile_pspace_data (pspace)->inhibit_updates, 1);
a845f5cb 1197}
e3c69974 1198
02ff80c2 1199/* See objfiles.h. */
63644780 1200
02ff80c2 1201bool
63644780
NB
1202is_addr_in_objfile (CORE_ADDR addr, const struct objfile *objfile)
1203{
63644780 1204 if (objfile == NULL)
02ff80c2 1205 return false;
63644780 1206
5250cbc8 1207 for (obj_section *osect : objfile->sections ())
63644780
NB
1208 {
1209 if (section_is_overlay (osect) && !section_is_mapped (osect))
1210 continue;
1211
94a75b03 1212 if (osect->contains (addr))
02ff80c2 1213 return true;
63644780 1214 }
02ff80c2 1215 return false;
63644780
NB
1216}
1217
02ff80c2
SM
1218/* See objfiles.h. */
1219
1220bool
d03de421
PA
1221shared_objfile_contains_address_p (struct program_space *pspace,
1222 CORE_ADDR address)
08351840 1223{
2030c079 1224 for (objfile *objfile : pspace->objfiles ())
08351840 1225 {
d03de421 1226 if ((objfile->flags & OBJF_SHARED) != 0
08351840 1227 && is_addr_in_objfile (address, objfile))
02ff80c2 1228 return true;
08351840
PA
1229 }
1230
02ff80c2 1231 return false;
08351840
PA
1232}
1233
19630284 1234/* The default implementation for the "iterate_over_objfiles_in_search_order"
2030c079 1235 gdbarch method. It is equivalent to use the objfiles iterable,
19630284
JB
1236 searching the objfiles in the order they are stored internally,
1237 ignoring CURRENT_OBJFILE.
1238
85102364 1239 On most platforms, it should be close enough to doing the best
19630284
JB
1240 we can without some knowledge specific to the architecture. */
1241
1242void
1243default_iterate_over_objfiles_in_search_order
6e9cd73e
SM
1244 (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
1245 objfile *current_objfile)
19630284 1246{
2030c079 1247 for (objfile *objfile : current_program_space->objfiles ())
6e9cd73e
SM
1248 if (cb (objfile))
1249 return;
19630284
JB
1250}
1251
e02c96a7 1252/* See objfiles.h. */
4262abfb
JK
1253
1254const char *
1255objfile_name (const struct objfile *objfile)
1256{
24ba069a 1257 if (objfile->obfd != NULL)
98badbfd 1258 return bfd_get_filename (objfile->obfd.get ());
24ba069a 1259
4262abfb
JK
1260 return objfile->original_name;
1261}
1262
cc485e62
DE
1263/* See objfiles.h. */
1264
e02c96a7
DE
1265const char *
1266objfile_filename (const struct objfile *objfile)
1267{
1268 if (objfile->obfd != NULL)
98badbfd 1269 return bfd_get_filename (objfile->obfd.get ());
e02c96a7
DE
1270
1271 return NULL;
1272}
1273
1274/* See objfiles.h. */
1275
cc485e62
DE
1276const char *
1277objfile_debug_name (const struct objfile *objfile)
1278{
1279 return lbasename (objfile->original_name);
1280}
1281
015d2e7e
DE
1282/* See objfiles.h. */
1283
1284const char *
1285objfile_flavour_name (struct objfile *objfile)
1286{
1287 if (objfile->obfd != NULL)
98badbfd 1288 return bfd_flavour_name (bfd_get_flavour (objfile->obfd.get ()));
015d2e7e
DE
1289 return NULL;
1290}
b3a01ce2
WP
1291
1292/* See objfiles.h. */
1293
1294struct type *
1295objfile_int_type (struct objfile *of, int size_in_bytes, bool unsigned_p)
1296{
1297 struct type *int_type;
1298
1299 /* Helper macro to examine the various builtin types. */
1300#define TRY_TYPE(F) \
1301 int_type = (unsigned_p \
a8ed3dde
TT
1302 ? builtin_type (of)->builtin_unsigned_ ## F \
1303 : builtin_type (of)->builtin_ ## F); \
df86565b 1304 if (int_type != NULL && int_type->length () == size_in_bytes) \
b3a01ce2
WP
1305 return int_type
1306
1307 TRY_TYPE (char);
1308 TRY_TYPE (short);
1309 TRY_TYPE (int);
1310 TRY_TYPE (long);
1311 TRY_TYPE (long_long);
1312
1313#undef TRY_TYPE
1314
1315 gdb_assert_not_reached ("unable to find suitable integer type");
1316}