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