Commit | Line | Data |
---|---|---|
c906108c | 1 | /* GDB routines for manipulating objfiles. |
af5f3db6 | 2 | |
6aba47ca | 3 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
0fb0cc75 | 4 | 2002, 2003, 2004, 2007, 2008, 2009 Free Software Foundation, Inc. |
af5f3db6 | 5 | |
c906108c SS |
6 | Contributed by Cygnus Support, using pieces from other GDB modules. |
7 | ||
c5aa993b | 8 | This file is part of GDB. |
c906108c | 9 | |
c5aa993b JM |
10 | This program is free software; you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 13 | (at your option) any later version. |
c906108c | 14 | |
c5aa993b JM |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
c906108c | 19 | |
c5aa993b | 20 | You should have received a copy of the GNU General Public License |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
22 | |
23 | /* This file contains support routines for creating, manipulating, and | |
24 | destroying objfile structures. */ | |
25 | ||
26 | #include "defs.h" | |
27 | #include "bfd.h" /* Binary File Description */ | |
28 | #include "symtab.h" | |
29 | #include "symfile.h" | |
30 | #include "objfiles.h" | |
31 | #include "gdb-stabs.h" | |
32 | #include "target.h" | |
af5f3db6 | 33 | #include "bcache.h" |
5b123146 | 34 | #include "mdebugread.h" |
9bdcbae7 DJ |
35 | #include "expression.h" |
36 | #include "parser-defs.h" | |
37 | ||
0d0e1a63 | 38 | #include "gdb_assert.h" |
c906108c SS |
39 | #include <sys/types.h> |
40 | #include "gdb_stat.h" | |
41 | #include <fcntl.h> | |
04ea0df1 | 42 | #include "gdb_obstack.h" |
c906108c | 43 | #include "gdb_string.h" |
2de7ced7 | 44 | #include "hashtab.h" |
c906108c | 45 | |
7a292a7a | 46 | #include "breakpoint.h" |
fe898f56 | 47 | #include "block.h" |
de4f826b | 48 | #include "dictionary.h" |
cb5d864f | 49 | #include "source.h" |
801e3a5b | 50 | #include "addrmap.h" |
5e2b427d | 51 | #include "arch-utils.h" |
30510692 | 52 | #include "exec.h" |
a845f5cb | 53 | #include "observer.h" |
6fbf07cd | 54 | #include "complaints.h" |
7a292a7a | 55 | |
c906108c SS |
56 | /* Prototypes for local functions */ |
57 | ||
0d0e1a63 MK |
58 | static void objfile_alloc_data (struct objfile *objfile); |
59 | static void objfile_free_data (struct objfile *objfile); | |
60 | ||
c906108c SS |
61 | /* Externally visible variables that are owned by this module. |
62 | See declarations in objfile.h for more info. */ | |
63 | ||
c5aa993b | 64 | struct objfile *object_files; /* Linked list of all objfiles */ |
c906108c SS |
65 | struct objfile *current_objfile; /* For symbol file being read in */ |
66 | struct objfile *symfile_objfile; /* Main symbol table loaded from */ | |
67 | struct objfile *rt_common_objfile; /* For runtime common symbols */ | |
68 | ||
a845f5cb PP |
69 | /* Records whether any objfiles appeared or disappeared since we last updated |
70 | address to obj section map. */ | |
71 | ||
bb272892 | 72 | static int objfiles_changed_p; |
a845f5cb | 73 | |
c906108c SS |
74 | /* Locate all mappable sections of a BFD file. |
75 | objfile_p_char is a char * to get it through | |
76 | bfd_map_over_sections; we cast it back to its proper type. */ | |
77 | ||
96baa820 JM |
78 | /* Called via bfd_map_over_sections to build up the section table that |
79 | the objfile references. The objfile contains pointers to the start | |
80 | of the table (objfile->sections) and to the first location after | |
81 | the end of the table (objfile->sections_end). */ | |
82 | ||
c906108c | 83 | static void |
7be0c536 AC |
84 | add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect, |
85 | void *objfile_p_char) | |
c906108c SS |
86 | { |
87 | struct objfile *objfile = (struct objfile *) objfile_p_char; | |
88 | struct obj_section section; | |
89 | flagword aflag; | |
90 | ||
91 | aflag = bfd_get_section_flags (abfd, asect); | |
92 | ||
ed7c5e43 | 93 | if (!(aflag & SEC_ALLOC)) |
c906108c SS |
94 | return; |
95 | ||
96 | if (0 == bfd_section_size (abfd, asect)) | |
97 | return; | |
c906108c SS |
98 | section.objfile = objfile; |
99 | section.the_bfd_section = asect; | |
100 | section.ovly_mapped = 0; | |
8b92e4d5 | 101 | obstack_grow (&objfile->objfile_obstack, (char *) §ion, sizeof (section)); |
f1f6aadf PA |
102 | objfile->sections_end |
103 | = (struct obj_section *) (((size_t) objfile->sections_end) + 1); | |
c906108c SS |
104 | } |
105 | ||
106 | /* Builds a section table for OBJFILE. | |
107 | Returns 0 if OK, 1 on error (in which case bfd_error contains the | |
96baa820 JM |
108 | error). |
109 | ||
110 | Note that while we are building the table, which goes into the | |
111 | psymbol obstack, we hijack the sections_end pointer to instead hold | |
112 | a count of the number of sections. When bfd_map_over_sections | |
113 | returns, this count is used to compute the pointer to the end of | |
114 | the sections table, which then overwrites the count. | |
115 | ||
116 | Also note that the OFFSET and OVLY_MAPPED in each table entry | |
117 | are initialized to zero. | |
118 | ||
119 | Also note that if anything else writes to the psymbol obstack while | |
120 | we are building the table, we're pretty much hosed. */ | |
c906108c SS |
121 | |
122 | int | |
fba45db2 | 123 | build_objfile_section_table (struct objfile *objfile) |
c906108c SS |
124 | { |
125 | /* objfile->sections can be already set when reading a mapped symbol | |
126 | file. I believe that we do need to rebuild the section table in | |
127 | this case (we rebuild other things derived from the bfd), but we | |
8b92e4d5 | 128 | can't free the old one (it's in the objfile_obstack). So we just |
c906108c SS |
129 | waste some memory. */ |
130 | ||
131 | objfile->sections_end = 0; | |
f1f6aadf PA |
132 | bfd_map_over_sections (objfile->obfd, |
133 | add_to_objfile_sections, (void *) objfile); | |
134 | objfile->sections = obstack_finish (&objfile->objfile_obstack); | |
135 | objfile->sections_end = objfile->sections + (size_t) objfile->sections_end; | |
c5aa993b | 136 | return (0); |
c906108c SS |
137 | } |
138 | ||
2df3850c JM |
139 | /* Given a pointer to an initialized bfd (ABFD) and some flag bits |
140 | allocate a new objfile struct, fill it in as best we can, link it | |
141 | into the list of all known objfiles, and return a pointer to the | |
142 | new objfile struct. | |
c906108c | 143 | |
2df3850c | 144 | The FLAGS word contains various bits (OBJF_*) that can be taken as |
78a4a9b9 AC |
145 | requests for specific operations. Other bits like OBJF_SHARED are |
146 | simply copied through to the new objfile flags member. */ | |
c906108c | 147 | |
eb9a305d DC |
148 | /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0 |
149 | by jv-lang.c, to create an artificial objfile used to hold | |
150 | information about dynamically-loaded Java classes. Unfortunately, | |
151 | that branch of this function doesn't get tested very frequently, so | |
152 | it's prone to breakage. (E.g. at one time the name was set to NULL | |
153 | in that situation, which broke a loop over all names in the dynamic | |
154 | library loader.) If you change this function, please try to leave | |
155 | things in a consistent state even if abfd is NULL. */ | |
156 | ||
c906108c | 157 | struct objfile * |
fba45db2 | 158 | allocate_objfile (bfd *abfd, int flags) |
c906108c SS |
159 | { |
160 | struct objfile *objfile = NULL; | |
161 | struct objfile *last_one = NULL; | |
162 | ||
c906108c SS |
163 | /* If we don't support mapped symbol files, didn't ask for the file to be |
164 | mapped, or failed to open the mapped file for some reason, then revert | |
165 | back to an unmapped objfile. */ | |
166 | ||
167 | if (objfile == NULL) | |
168 | { | |
169 | objfile = (struct objfile *) xmalloc (sizeof (struct objfile)); | |
170 | memset (objfile, 0, sizeof (struct objfile)); | |
af5f3db6 AC |
171 | objfile->psymbol_cache = bcache_xmalloc (); |
172 | objfile->macro_cache = bcache_xmalloc (); | |
1ab21617 EZ |
173 | /* We could use obstack_specify_allocation here instead, but |
174 | gdb_obstack.h specifies the alloc/dealloc functions. */ | |
175 | obstack_init (&objfile->objfile_obstack); | |
15831452 | 176 | terminate_minimal_symbol_table (objfile); |
c906108c SS |
177 | } |
178 | ||
0d0e1a63 MK |
179 | objfile_alloc_data (objfile); |
180 | ||
c906108c SS |
181 | /* Update the per-objfile information that comes from the bfd, ensuring |
182 | that any data that is reference is saved in the per-objfile data | |
183 | region. */ | |
184 | ||
3db741ef | 185 | objfile->obfd = gdb_bfd_ref (abfd); |
c5aa993b | 186 | if (objfile->name != NULL) |
c906108c | 187 | { |
2dc74dc1 | 188 | xfree (objfile->name); |
c906108c SS |
189 | } |
190 | if (abfd != NULL) | |
191 | { | |
5e2b427d UW |
192 | /* Look up the gdbarch associated with the BFD. */ |
193 | objfile->gdbarch = gdbarch_from_bfd (abfd); | |
194 | ||
982526a1 | 195 | objfile->name = xstrdup (bfd_get_filename (abfd)); |
c5aa993b | 196 | objfile->mtime = bfd_get_mtime (abfd); |
c906108c SS |
197 | |
198 | /* Build section table. */ | |
199 | ||
200 | if (build_objfile_section_table (objfile)) | |
201 | { | |
8a3fe4f8 | 202 | error (_("Can't find the file sections in `%s': %s"), |
c5aa993b | 203 | objfile->name, bfd_errmsg (bfd_get_error ())); |
c906108c SS |
204 | } |
205 | } | |
eb9a305d DC |
206 | else |
207 | { | |
982526a1 | 208 | objfile->name = xstrdup ("<<anonymous objfile>>"); |
eb9a305d | 209 | } |
c906108c | 210 | |
b8fbeb18 EZ |
211 | /* Initialize the section indexes for this objfile, so that we can |
212 | later detect if they are used w/o being properly assigned to. */ | |
213 | ||
5c4e30ca DC |
214 | objfile->sect_index_text = -1; |
215 | objfile->sect_index_data = -1; | |
216 | objfile->sect_index_bss = -1; | |
217 | objfile->sect_index_rodata = -1; | |
218 | ||
219 | /* We don't yet have a C++-specific namespace symtab. */ | |
220 | ||
221 | objfile->cp_namespace_symtab = NULL; | |
b8fbeb18 | 222 | |
c906108c SS |
223 | /* Add this file onto the tail of the linked list of other such files. */ |
224 | ||
c5aa993b | 225 | objfile->next = NULL; |
c906108c SS |
226 | if (object_files == NULL) |
227 | object_files = objfile; | |
228 | else | |
229 | { | |
230 | for (last_one = object_files; | |
c5aa993b JM |
231 | last_one->next; |
232 | last_one = last_one->next); | |
233 | last_one->next = objfile; | |
c906108c SS |
234 | } |
235 | ||
2df3850c JM |
236 | /* Save passed in flag bits. */ |
237 | objfile->flags |= flags; | |
c906108c | 238 | |
bb272892 PP |
239 | objfiles_changed_p = 1; /* Rebuild section map next time we need it. */ |
240 | ||
c906108c SS |
241 | return (objfile); |
242 | } | |
243 | ||
5e2b427d UW |
244 | /* Retrieve the gdbarch associated with OBJFILE. */ |
245 | struct gdbarch * | |
246 | get_objfile_arch (struct objfile *objfile) | |
247 | { | |
248 | return objfile->gdbarch; | |
249 | } | |
250 | ||
9ab9195f EZ |
251 | /* Initialize entry point information for this objfile. */ |
252 | ||
253 | void | |
254 | init_entry_point_info (struct objfile *objfile) | |
255 | { | |
256 | /* Save startup file's range of PC addresses to help blockframe.c | |
257 | decide where the bottom of the stack is. */ | |
258 | ||
259 | if (bfd_get_file_flags (objfile->obfd) & EXEC_P) | |
260 | { | |
261 | /* Executable file -- record its entry point so we'll recognize | |
262 | the startup file because it contains the entry point. */ | |
263 | objfile->ei.entry_point = bfd_get_start_address (objfile->obfd); | |
264 | } | |
574dffa2 DJ |
265 | else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC |
266 | && bfd_get_start_address (objfile->obfd) != 0) | |
267 | /* Some shared libraries may have entry points set and be | |
268 | runnable. There's no clear way to indicate this, so just check | |
269 | for values other than zero. */ | |
270 | objfile->ei.entry_point = bfd_get_start_address (objfile->obfd); | |
9ab9195f EZ |
271 | else |
272 | { | |
273 | /* Examination of non-executable.o files. Short-circuit this stuff. */ | |
274 | objfile->ei.entry_point = INVALID_ENTRY_POINT; | |
275 | } | |
9ab9195f EZ |
276 | } |
277 | ||
278 | /* Get current entry point address. */ | |
279 | ||
280 | CORE_ADDR | |
281 | entry_point_address (void) | |
282 | { | |
3612b192 DJ |
283 | struct gdbarch *gdbarch; |
284 | CORE_ADDR entry_point; | |
285 | ||
286 | if (symfile_objfile == NULL) | |
287 | return 0; | |
288 | ||
289 | gdbarch = get_objfile_arch (symfile_objfile); | |
290 | ||
291 | entry_point = symfile_objfile->ei.entry_point; | |
292 | ||
293 | /* Make certain that the address points at real code, and not a | |
294 | function descriptor. */ | |
295 | entry_point = gdbarch_convert_from_func_ptr_addr (gdbarch, entry_point, | |
296 | ¤t_target); | |
297 | ||
298 | /* Remove any ISA markers, so that this matches entries in the | |
299 | symbol table. */ | |
300 | entry_point = gdbarch_addr_bits_remove (gdbarch, entry_point); | |
301 | ||
302 | return entry_point; | |
9ab9195f | 303 | } |
15831452 JB |
304 | |
305 | /* Create the terminating entry of OBJFILE's minimal symbol table. | |
306 | If OBJFILE->msymbols is zero, allocate a single entry from | |
4a146b47 | 307 | OBJFILE->objfile_obstack; otherwise, just initialize |
15831452 JB |
308 | OBJFILE->msymbols[OBJFILE->minimal_symbol_count]. */ |
309 | void | |
310 | terminate_minimal_symbol_table (struct objfile *objfile) | |
311 | { | |
312 | if (! objfile->msymbols) | |
313 | objfile->msymbols = ((struct minimal_symbol *) | |
4a146b47 | 314 | obstack_alloc (&objfile->objfile_obstack, |
15831452 JB |
315 | sizeof (objfile->msymbols[0]))); |
316 | ||
317 | { | |
318 | struct minimal_symbol *m | |
319 | = &objfile->msymbols[objfile->minimal_symbol_count]; | |
320 | ||
321 | memset (m, 0, sizeof (*m)); | |
5bf0017e EZ |
322 | /* Don't rely on these enumeration values being 0's. */ |
323 | MSYMBOL_TYPE (m) = mst_unknown; | |
15831452 JB |
324 | SYMBOL_INIT_LANGUAGE_SPECIFIC (m, language_unknown); |
325 | } | |
326 | } | |
327 | ||
328 | ||
5b5d99cf JB |
329 | /* Put one object file before a specified on in the global list. |
330 | This can be used to make sure an object file is destroyed before | |
331 | another when using ALL_OBJFILES_SAFE to free all objfiles. */ | |
332 | void | |
333 | put_objfile_before (struct objfile *objfile, struct objfile *before_this) | |
334 | { | |
335 | struct objfile **objp; | |
336 | ||
337 | unlink_objfile (objfile); | |
338 | ||
339 | for (objp = &object_files; *objp != NULL; objp = &((*objp)->next)) | |
340 | { | |
341 | if (*objp == before_this) | |
342 | { | |
343 | objfile->next = *objp; | |
344 | *objp = objfile; | |
345 | return; | |
346 | } | |
347 | } | |
348 | ||
349 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 350 | _("put_objfile_before: before objfile not in list")); |
5b5d99cf JB |
351 | } |
352 | ||
c906108c SS |
353 | /* Put OBJFILE at the front of the list. */ |
354 | ||
355 | void | |
fba45db2 | 356 | objfile_to_front (struct objfile *objfile) |
c906108c SS |
357 | { |
358 | struct objfile **objp; | |
359 | for (objp = &object_files; *objp != NULL; objp = &((*objp)->next)) | |
360 | { | |
361 | if (*objp == objfile) | |
362 | { | |
363 | /* Unhook it from where it is. */ | |
364 | *objp = objfile->next; | |
365 | /* Put it in the front. */ | |
366 | objfile->next = object_files; | |
367 | object_files = objfile; | |
368 | break; | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
373 | /* Unlink OBJFILE from the list of known objfiles, if it is found in the | |
374 | list. | |
375 | ||
376 | It is not a bug, or error, to call this function if OBJFILE is not known | |
377 | to be in the current list. This is done in the case of mapped objfiles, | |
378 | for example, just to ensure that the mapped objfile doesn't appear twice | |
379 | in the list. Since the list is threaded, linking in a mapped objfile | |
380 | twice would create a circular list. | |
381 | ||
382 | If OBJFILE turns out to be in the list, we zap it's NEXT pointer after | |
383 | unlinking it, just to ensure that we have completely severed any linkages | |
384 | between the OBJFILE and the list. */ | |
385 | ||
386 | void | |
fba45db2 | 387 | unlink_objfile (struct objfile *objfile) |
c906108c | 388 | { |
c5aa993b | 389 | struct objfile **objpp; |
c906108c | 390 | |
c5aa993b | 391 | for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next)) |
c906108c | 392 | { |
c5aa993b | 393 | if (*objpp == objfile) |
c906108c | 394 | { |
c5aa993b JM |
395 | *objpp = (*objpp)->next; |
396 | objfile->next = NULL; | |
07cd4b97 | 397 | return; |
c906108c SS |
398 | } |
399 | } | |
07cd4b97 | 400 | |
8e65ff28 | 401 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 402 | _("unlink_objfile: objfile already unlinked")); |
c906108c SS |
403 | } |
404 | ||
405 | ||
406 | /* Destroy an objfile and all the symtabs and psymtabs under it. Note | |
4a146b47 EZ |
407 | that as much as possible is allocated on the objfile_obstack |
408 | so that the memory can be efficiently freed. | |
c906108c SS |
409 | |
410 | Things which we do NOT free because they are not in malloc'd memory | |
411 | or not in memory specific to the objfile include: | |
412 | ||
c5aa993b | 413 | objfile -> sf |
c906108c SS |
414 | |
415 | FIXME: If the objfile is using reusable symbol information (via mmalloc), | |
416 | then we need to take into account the fact that more than one process | |
417 | may be using the symbol information at the same time (when mmalloc is | |
418 | extended to support cooperative locking). When more than one process | |
419 | is using the mapped symbol info, we need to be more careful about when | |
420 | we free objects in the reusable area. */ | |
421 | ||
422 | void | |
fba45db2 | 423 | free_objfile (struct objfile *objfile) |
c906108c | 424 | { |
5b5d99cf JB |
425 | if (objfile->separate_debug_objfile) |
426 | { | |
427 | free_objfile (objfile->separate_debug_objfile); | |
428 | } | |
429 | ||
430 | if (objfile->separate_debug_objfile_backlink) | |
431 | { | |
432 | /* We freed the separate debug file, make sure the base objfile | |
433 | doesn't reference it. */ | |
434 | objfile->separate_debug_objfile_backlink->separate_debug_objfile = NULL; | |
435 | } | |
436 | ||
ae5a43e0 DJ |
437 | /* Remove any references to this objfile in the global value |
438 | lists. */ | |
439 | preserve_values (objfile); | |
440 | ||
c906108c SS |
441 | /* First do any symbol file specific actions required when we are |
442 | finished with a particular symbol file. Note that if the objfile | |
443 | is using reusable symbol information (via mmalloc) then each of | |
444 | these routines is responsible for doing the correct thing, either | |
445 | freeing things which are valid only during this particular gdb | |
446 | execution, or leaving them to be reused during the next one. */ | |
447 | ||
c5aa993b | 448 | if (objfile->sf != NULL) |
c906108c | 449 | { |
c5aa993b | 450 | (*objfile->sf->sym_finish) (objfile); |
c906108c SS |
451 | } |
452 | ||
c5bc3a77 DJ |
453 | /* Discard any data modules have associated with the objfile. */ |
454 | objfile_free_data (objfile); | |
455 | ||
e3c69974 | 456 | gdb_bfd_unref (objfile->obfd); |
c906108c SS |
457 | |
458 | /* Remove it from the chain of all objfiles. */ | |
459 | ||
460 | unlink_objfile (objfile); | |
461 | ||
adb7f338 JK |
462 | if (objfile == symfile_objfile) |
463 | symfile_objfile = NULL; | |
c906108c SS |
464 | |
465 | if (objfile == rt_common_objfile) | |
466 | rt_common_objfile = NULL; | |
467 | ||
468 | /* Before the symbol table code was redone to make it easier to | |
469 | selectively load and remove information particular to a specific | |
470 | linkage unit, gdb used to do these things whenever the monolithic | |
471 | symbol table was blown away. How much still needs to be done | |
472 | is unknown, but we play it safe for now and keep each action until | |
473 | it is shown to be no longer needed. */ | |
c5aa993b | 474 | |
cb5d864f FF |
475 | /* Not all our callers call clear_symtab_users (objfile_purge_solibs, |
476 | for example), so we need to call this here. */ | |
c906108c SS |
477 | clear_pc_function_cache (); |
478 | ||
9bdcbae7 DJ |
479 | /* Clear globals which might have pointed into a removed objfile. |
480 | FIXME: It's not clear which of these are supposed to persist | |
481 | between expressions and which ought to be reset each time. */ | |
482 | expression_context_block = NULL; | |
483 | innermost_block = NULL; | |
484 | ||
cb5d864f FF |
485 | /* Check to see if the current_source_symtab belongs to this objfile, |
486 | and if so, call clear_current_source_symtab_and_line. */ | |
487 | ||
488 | { | |
489 | struct symtab_and_line cursal = get_current_source_symtab_and_line (); | |
490 | struct symtab *s; | |
491 | ||
492 | ALL_OBJFILE_SYMTABS (objfile, s) | |
493 | { | |
494 | if (s == cursal.symtab) | |
495 | clear_current_source_symtab_and_line (); | |
496 | } | |
497 | } | |
498 | ||
78a4a9b9 | 499 | /* The last thing we do is free the objfile struct itself. */ |
c906108c | 500 | |
78a4a9b9 | 501 | if (objfile->name != NULL) |
c906108c | 502 | { |
2dc74dc1 | 503 | xfree (objfile->name); |
c906108c | 504 | } |
78a4a9b9 | 505 | if (objfile->global_psymbols.list) |
2dc74dc1 | 506 | xfree (objfile->global_psymbols.list); |
78a4a9b9 | 507 | if (objfile->static_psymbols.list) |
2dc74dc1 | 508 | xfree (objfile->static_psymbols.list); |
78a4a9b9 AC |
509 | /* Free the obstacks for non-reusable objfiles */ |
510 | bcache_xfree (objfile->psymbol_cache); | |
511 | bcache_xfree (objfile->macro_cache); | |
512 | if (objfile->demangled_names_hash) | |
513 | htab_delete (objfile->demangled_names_hash); | |
b99607ea | 514 | obstack_free (&objfile->objfile_obstack, 0); |
2dc74dc1 | 515 | xfree (objfile); |
78a4a9b9 | 516 | objfile = NULL; |
bb272892 | 517 | objfiles_changed_p = 1; /* Rebuild section map next time we need it. */ |
c906108c SS |
518 | } |
519 | ||
74b7792f AC |
520 | static void |
521 | do_free_objfile_cleanup (void *obj) | |
522 | { | |
523 | free_objfile (obj); | |
524 | } | |
525 | ||
526 | struct cleanup * | |
527 | make_cleanup_free_objfile (struct objfile *obj) | |
528 | { | |
529 | return make_cleanup (do_free_objfile_cleanup, obj); | |
530 | } | |
c906108c SS |
531 | |
532 | /* Free all the object files at once and clean up their users. */ | |
533 | ||
534 | void | |
fba45db2 | 535 | free_all_objfiles (void) |
c906108c SS |
536 | { |
537 | struct objfile *objfile, *temp; | |
538 | ||
539 | ALL_OBJFILES_SAFE (objfile, temp) | |
c5aa993b JM |
540 | { |
541 | free_objfile (objfile); | |
542 | } | |
c906108c SS |
543 | clear_symtab_users (); |
544 | } | |
545 | \f | |
546 | /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS | |
547 | entries in new_offsets. */ | |
548 | void | |
fba45db2 | 549 | objfile_relocate (struct objfile *objfile, struct section_offsets *new_offsets) |
c906108c | 550 | { |
30510692 | 551 | struct obj_section *s; |
d4f3574e | 552 | struct section_offsets *delta = |
a39a16c4 MM |
553 | ((struct section_offsets *) |
554 | alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections))); | |
c906108c SS |
555 | |
556 | { | |
557 | int i; | |
558 | int something_changed = 0; | |
559 | for (i = 0; i < objfile->num_sections; ++i) | |
560 | { | |
a4c8257b | 561 | delta->offsets[i] = |
c906108c SS |
562 | ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i); |
563 | if (ANOFFSET (delta, i) != 0) | |
564 | something_changed = 1; | |
565 | } | |
566 | if (!something_changed) | |
567 | return; | |
568 | } | |
569 | ||
570 | /* OK, get all the symtabs. */ | |
571 | { | |
572 | struct symtab *s; | |
573 | ||
574 | ALL_OBJFILE_SYMTABS (objfile, s) | |
c5aa993b JM |
575 | { |
576 | struct linetable *l; | |
577 | struct blockvector *bv; | |
578 | int i; | |
579 | ||
580 | /* First the line table. */ | |
581 | l = LINETABLE (s); | |
582 | if (l) | |
583 | { | |
584 | for (i = 0; i < l->nitems; ++i) | |
585 | l->item[i].pc += ANOFFSET (delta, s->block_line_section); | |
586 | } | |
c906108c | 587 | |
c5aa993b JM |
588 | /* Don't relocate a shared blockvector more than once. */ |
589 | if (!s->primary) | |
590 | continue; | |
c906108c | 591 | |
c5aa993b | 592 | bv = BLOCKVECTOR (s); |
b101f7a1 UW |
593 | if (BLOCKVECTOR_MAP (bv)) |
594 | addrmap_relocate (BLOCKVECTOR_MAP (bv), | |
595 | ANOFFSET (delta, s->block_line_section)); | |
596 | ||
c5aa993b JM |
597 | for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i) |
598 | { | |
599 | struct block *b; | |
e88c90f2 | 600 | struct symbol *sym; |
de4f826b | 601 | struct dict_iterator iter; |
c5aa993b JM |
602 | |
603 | b = BLOCKVECTOR_BLOCK (bv, i); | |
604 | BLOCK_START (b) += ANOFFSET (delta, s->block_line_section); | |
605 | BLOCK_END (b) += ANOFFSET (delta, s->block_line_section); | |
606 | ||
de4f826b | 607 | ALL_BLOCK_SYMBOLS (b, iter, sym) |
c5aa993b | 608 | { |
7a78d0ee KB |
609 | fixup_symbol_section (sym, objfile); |
610 | ||
c5aa993b | 611 | /* The RS6000 code from which this was taken skipped |
176620f1 | 612 | any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN. |
c5aa993b JM |
613 | But I'm leaving out that test, on the theory that |
614 | they can't possibly pass the tests below. */ | |
615 | if ((SYMBOL_CLASS (sym) == LOC_LABEL | |
0bb4e8c4 | 616 | || SYMBOL_CLASS (sym) == LOC_STATIC) |
c5aa993b JM |
617 | && SYMBOL_SECTION (sym) >= 0) |
618 | { | |
619 | SYMBOL_VALUE_ADDRESS (sym) += | |
620 | ANOFFSET (delta, SYMBOL_SECTION (sym)); | |
621 | } | |
c5aa993b JM |
622 | } |
623 | } | |
624 | } | |
c906108c SS |
625 | } |
626 | ||
627 | { | |
628 | struct partial_symtab *p; | |
629 | ||
630 | ALL_OBJFILE_PSYMTABS (objfile, p) | |
c5aa993b | 631 | { |
b8fbeb18 EZ |
632 | p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile)); |
633 | p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile)); | |
c5aa993b | 634 | } |
c906108c SS |
635 | } |
636 | ||
637 | { | |
638 | struct partial_symbol **psym; | |
639 | ||
640 | for (psym = objfile->global_psymbols.list; | |
641 | psym < objfile->global_psymbols.next; | |
642 | psym++) | |
7a78d0ee KB |
643 | { |
644 | fixup_psymbol_section (*psym, objfile); | |
645 | if (SYMBOL_SECTION (*psym) >= 0) | |
646 | SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta, | |
647 | SYMBOL_SECTION (*psym)); | |
648 | } | |
c906108c SS |
649 | for (psym = objfile->static_psymbols.list; |
650 | psym < objfile->static_psymbols.next; | |
651 | psym++) | |
7a78d0ee KB |
652 | { |
653 | fixup_psymbol_section (*psym, objfile); | |
654 | if (SYMBOL_SECTION (*psym) >= 0) | |
655 | SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta, | |
656 | SYMBOL_SECTION (*psym)); | |
657 | } | |
c906108c SS |
658 | } |
659 | ||
660 | { | |
661 | struct minimal_symbol *msym; | |
662 | ALL_OBJFILE_MSYMBOLS (objfile, msym) | |
663 | if (SYMBOL_SECTION (msym) >= 0) | |
c5aa993b | 664 | SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym)); |
c906108c SS |
665 | } |
666 | /* Relocating different sections by different amounts may cause the symbols | |
667 | to be out of order. */ | |
668 | msymbols_sort (objfile); | |
669 | ||
36b0c0e0 PS |
670 | if (objfile->ei.entry_point != ~(CORE_ADDR) 0) |
671 | { | |
672 | /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT | |
673 | only as a fallback. */ | |
674 | struct obj_section *s; | |
675 | s = find_pc_section (objfile->ei.entry_point); | |
676 | if (s) | |
677 | objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index); | |
678 | else | |
679 | objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile)); | |
680 | } | |
681 | ||
f1f2b5f4 PA |
682 | { |
683 | int i; | |
684 | for (i = 0; i < objfile->num_sections; ++i) | |
685 | (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i); | |
686 | } | |
687 | ||
688 | /* Rebuild section map next time we need it. */ | |
689 | objfiles_changed_p = 1; | |
690 | ||
30510692 DJ |
691 | /* Update the table in exec_ops, used to read memory. */ |
692 | ALL_OBJFILE_OSECTIONS (objfile, s) | |
693 | { | |
694 | int idx = s->the_bfd_section->index; | |
695 | ||
696 | exec_set_section_address (bfd_get_filename (objfile->obfd), idx, | |
f1f6aadf | 697 | obj_section_addr (s)); |
30510692 DJ |
698 | } |
699 | ||
c906108c | 700 | /* Relocate breakpoints as necessary, after things are relocated. */ |
69de3c6a | 701 | breakpoint_re_set (); |
c906108c SS |
702 | } |
703 | \f | |
55333a84 DE |
704 | /* Return non-zero if OBJFILE has partial symbols. */ |
705 | ||
706 | int | |
707 | objfile_has_partial_symbols (struct objfile *objfile) | |
708 | { | |
709 | return objfile->psymtabs != NULL; | |
710 | } | |
711 | ||
712 | /* Return non-zero if OBJFILE has full symbols. */ | |
713 | ||
714 | int | |
715 | objfile_has_full_symbols (struct objfile *objfile) | |
716 | { | |
717 | return objfile->symtabs != NULL; | |
718 | } | |
719 | ||
e361b228 TG |
720 | /* Return non-zero if OBJFILE has full or partial symbols, either directly |
721 | or throught its separate debug file. */ | |
722 | ||
723 | int | |
724 | objfile_has_symbols (struct objfile *objfile) | |
725 | { | |
726 | struct objfile *separate_objfile; | |
727 | ||
728 | if (objfile_has_partial_symbols (objfile) | |
729 | || objfile_has_full_symbols (objfile)) | |
730 | return 1; | |
731 | ||
732 | separate_objfile = objfile->separate_debug_objfile; | |
733 | if (separate_objfile == NULL) | |
734 | return 0; | |
735 | ||
736 | if (objfile_has_partial_symbols (separate_objfile) | |
737 | || objfile_has_full_symbols (separate_objfile)) | |
738 | return 1; | |
739 | ||
740 | return 0; | |
741 | } | |
742 | ||
743 | ||
c906108c SS |
744 | /* Many places in gdb want to test just to see if we have any partial |
745 | symbols available. This function returns zero if none are currently | |
746 | available, nonzero otherwise. */ | |
747 | ||
748 | int | |
fba45db2 | 749 | have_partial_symbols (void) |
c906108c SS |
750 | { |
751 | struct objfile *ofp; | |
752 | ||
753 | ALL_OBJFILES (ofp) | |
c5aa993b | 754 | { |
55333a84 DE |
755 | if (objfile_has_partial_symbols (ofp)) |
756 | return 1; | |
c5aa993b | 757 | } |
c906108c SS |
758 | return 0; |
759 | } | |
760 | ||
761 | /* Many places in gdb want to test just to see if we have any full | |
762 | symbols available. This function returns zero if none are currently | |
763 | available, nonzero otherwise. */ | |
764 | ||
765 | int | |
fba45db2 | 766 | have_full_symbols (void) |
c906108c SS |
767 | { |
768 | struct objfile *ofp; | |
769 | ||
770 | ALL_OBJFILES (ofp) | |
c5aa993b | 771 | { |
55333a84 DE |
772 | if (objfile_has_full_symbols (ofp)) |
773 | return 1; | |
c5aa993b | 774 | } |
c906108c SS |
775 | return 0; |
776 | } | |
777 | ||
778 | ||
779 | /* This operations deletes all objfile entries that represent solibs that | |
780 | weren't explicitly loaded by the user, via e.g., the add-symbol-file | |
781 | command. | |
c5aa993b | 782 | */ |
c906108c | 783 | void |
fba45db2 | 784 | objfile_purge_solibs (void) |
c906108c | 785 | { |
c5aa993b JM |
786 | struct objfile *objf; |
787 | struct objfile *temp; | |
c906108c SS |
788 | |
789 | ALL_OBJFILES_SAFE (objf, temp) | |
790 | { | |
791 | /* We assume that the solib package has been purged already, or will | |
792 | be soon. | |
c5aa993b | 793 | */ |
2df3850c | 794 | if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED)) |
c906108c SS |
795 | free_objfile (objf); |
796 | } | |
797 | } | |
798 | ||
799 | ||
800 | /* Many places in gdb want to test just to see if we have any minimal | |
801 | symbols available. This function returns zero if none are currently | |
802 | available, nonzero otherwise. */ | |
803 | ||
804 | int | |
fba45db2 | 805 | have_minimal_symbols (void) |
c906108c SS |
806 | { |
807 | struct objfile *ofp; | |
808 | ||
809 | ALL_OBJFILES (ofp) | |
c5aa993b | 810 | { |
15831452 | 811 | if (ofp->minimal_symbol_count > 0) |
c5aa993b JM |
812 | { |
813 | return 1; | |
814 | } | |
815 | } | |
c906108c SS |
816 | return 0; |
817 | } | |
818 | ||
a845f5cb PP |
819 | /* Qsort comparison function. */ |
820 | ||
821 | static int | |
822 | qsort_cmp (const void *a, const void *b) | |
823 | { | |
824 | const struct obj_section *sect1 = *(const struct obj_section **) a; | |
825 | const struct obj_section *sect2 = *(const struct obj_section **) b; | |
826 | const CORE_ADDR sect1_addr = obj_section_addr (sect1); | |
827 | const CORE_ADDR sect2_addr = obj_section_addr (sect2); | |
828 | ||
829 | if (sect1_addr < sect2_addr) | |
6fbf07cd | 830 | return -1; |
a845f5cb | 831 | else if (sect1_addr > sect2_addr) |
6fbf07cd PP |
832 | return 1; |
833 | else | |
834 | { | |
835 | /* Sections are at the same address. This could happen if | |
836 | A) we have an objfile and a separate debuginfo. | |
837 | B) we are confused, and have added sections without proper relocation, | |
838 | or something like that. */ | |
839 | ||
840 | const struct objfile *const objfile1 = sect1->objfile; | |
841 | const struct objfile *const objfile2 = sect2->objfile; | |
842 | ||
843 | if (objfile1->separate_debug_objfile == objfile2 | |
844 | || objfile2->separate_debug_objfile == objfile1) | |
845 | { | |
846 | /* Case A. The ordering doesn't matter: separate debuginfo files | |
847 | will be filtered out later. */ | |
848 | ||
849 | return 0; | |
850 | } | |
851 | ||
852 | /* Case B. Maintain stable sort order, so bugs in GDB are easier to | |
853 | triage. This section could be slow (since we iterate over all | |
854 | objfiles in each call to qsort_cmp), but this shouldn't happen | |
855 | very often (GDB is already in a confused state; one hopes this | |
856 | doesn't happen at all). If you discover that significant time is | |
857 | spent in the loops below, do 'set complaints 100' and examine the | |
858 | resulting complaints. */ | |
859 | ||
860 | if (objfile1 == objfile2) | |
861 | { | |
862 | /* Both sections came from the same objfile. We are really confused. | |
863 | Sort on sequence order of sections within the objfile. */ | |
864 | ||
865 | const struct obj_section *osect; | |
866 | ||
867 | ALL_OBJFILE_OSECTIONS (objfile1, osect) | |
868 | if (osect == sect1) | |
869 | return -1; | |
870 | else if (osect == sect2) | |
871 | return 1; | |
872 | ||
873 | /* We should have found one of the sections before getting here. */ | |
874 | gdb_assert (0); | |
875 | } | |
876 | else | |
877 | { | |
878 | /* Sort on sequence number of the objfile in the chain. */ | |
879 | ||
880 | const struct objfile *objfile; | |
881 | ||
882 | ALL_OBJFILES (objfile) | |
883 | if (objfile == objfile1) | |
884 | return -1; | |
885 | else if (objfile == objfile2) | |
886 | return 1; | |
887 | ||
888 | /* We should have found one of the objfiles before getting here. */ | |
889 | gdb_assert (0); | |
890 | } | |
891 | ||
892 | } | |
893 | ||
894 | /* Unreachable. */ | |
895 | gdb_assert (0); | |
a845f5cb PP |
896 | return 0; |
897 | } | |
898 | ||
3aad21cf PP |
899 | /* Select "better" obj_section to keep. We prefer the one that came from |
900 | the real object, rather than the one from separate debuginfo. | |
901 | Most of the time the two sections are exactly identical, but with | |
902 | prelinking the .rel.dyn section in the real object may have different | |
903 | size. */ | |
904 | ||
905 | static struct obj_section * | |
906 | preferred_obj_section (struct obj_section *a, struct obj_section *b) | |
907 | { | |
908 | gdb_assert (obj_section_addr (a) == obj_section_addr (b)); | |
909 | gdb_assert ((a->objfile->separate_debug_objfile == b->objfile) | |
910 | || (b->objfile->separate_debug_objfile == a->objfile)); | |
911 | gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile) | |
912 | || (b->objfile->separate_debug_objfile_backlink == a->objfile)); | |
913 | ||
914 | if (a->objfile->separate_debug_objfile != NULL) | |
915 | return a; | |
916 | return b; | |
917 | } | |
918 | ||
6fbf07cd PP |
919 | /* Return 1 if SECTION should be inserted into the section map. |
920 | We want to insert only non-overlay and non-TLS section. */ | |
921 | ||
922 | static int | |
923 | insert_section_p (const struct bfd *abfd, | |
924 | const struct bfd_section *section) | |
925 | { | |
926 | const bfd_vma lma = bfd_section_lma (abfd, section); | |
927 | ||
928 | if (lma != 0 && lma != bfd_section_vma (abfd, section) | |
929 | && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0) | |
930 | /* This is an overlay section. IN_MEMORY check is needed to avoid | |
931 | discarding sections from the "system supplied DSO" (aka vdso) | |
932 | on some Linux systems (e.g. Fedora 11). */ | |
933 | return 0; | |
934 | if ((bfd_get_section_flags (abfd, section) & SEC_THREAD_LOCAL) != 0) | |
935 | /* This is a TLS section. */ | |
936 | return 0; | |
937 | ||
938 | return 1; | |
939 | } | |
940 | ||
941 | /* Filter out overlapping sections where one section came from the real | |
942 | objfile, and the other from a separate debuginfo file. | |
943 | Return the size of table after redundant sections have been eliminated. */ | |
944 | ||
945 | static int | |
946 | filter_debuginfo_sections (struct obj_section **map, int map_size) | |
947 | { | |
948 | int i, j; | |
949 | ||
950 | for (i = 0, j = 0; i < map_size - 1; i++) | |
951 | { | |
952 | struct obj_section *const sect1 = map[i]; | |
953 | struct obj_section *const sect2 = map[i + 1]; | |
954 | const struct objfile *const objfile1 = sect1->objfile; | |
955 | const struct objfile *const objfile2 = sect2->objfile; | |
956 | const CORE_ADDR sect1_addr = obj_section_addr (sect1); | |
957 | const CORE_ADDR sect2_addr = obj_section_addr (sect2); | |
958 | ||
959 | if (sect1_addr == sect2_addr | |
960 | && (objfile1->separate_debug_objfile == objfile2 | |
961 | || objfile2->separate_debug_objfile == objfile1)) | |
962 | { | |
963 | map[j++] = preferred_obj_section (sect1, sect2); | |
964 | ++i; | |
965 | } | |
966 | else | |
967 | map[j++] = sect1; | |
968 | } | |
969 | ||
970 | if (i < map_size) | |
971 | { | |
972 | gdb_assert (i == map_size - 1); | |
973 | map[j++] = map[i]; | |
974 | } | |
975 | ||
976 | /* The map should not have shrunk to less than half the original size. */ | |
977 | gdb_assert (map_size / 2 <= j); | |
978 | ||
979 | return j; | |
980 | } | |
981 | ||
982 | /* Filter out overlapping sections, issuing a warning if any are found. | |
983 | Overlapping sections could really be overlay sections which we didn't | |
984 | classify as such in insert_section_p, or we could be dealing with a | |
985 | corrupt binary. */ | |
986 | ||
987 | static int | |
988 | filter_overlapping_sections (struct obj_section **map, int map_size) | |
989 | { | |
990 | int i, j; | |
991 | ||
992 | for (i = 0, j = 0; i < map_size - 1; ) | |
993 | { | |
994 | int k; | |
995 | ||
996 | map[j++] = map[i]; | |
997 | for (k = i + 1; k < map_size; k++) | |
998 | { | |
999 | struct obj_section *const sect1 = map[i]; | |
1000 | struct obj_section *const sect2 = map[k]; | |
1001 | const CORE_ADDR sect1_addr = obj_section_addr (sect1); | |
1002 | const CORE_ADDR sect2_addr = obj_section_addr (sect2); | |
1003 | const CORE_ADDR sect1_endaddr = obj_section_endaddr (sect1); | |
1004 | ||
1005 | gdb_assert (sect1_addr <= sect2_addr); | |
1006 | ||
1007 | if (sect1_endaddr <= sect2_addr) | |
1008 | break; | |
1009 | else | |
1010 | { | |
1011 | /* We have an overlap. Report it. */ | |
1012 | ||
1013 | struct objfile *const objf1 = sect1->objfile; | |
1014 | struct objfile *const objf2 = sect2->objfile; | |
1015 | ||
1016 | const struct bfd *const abfd1 = objf1->obfd; | |
1017 | const struct bfd *const abfd2 = objf2->obfd; | |
1018 | ||
1019 | const struct bfd_section *const bfds1 = sect1->the_bfd_section; | |
1020 | const struct bfd_section *const bfds2 = sect2->the_bfd_section; | |
1021 | ||
1022 | const CORE_ADDR sect2_endaddr = obj_section_endaddr (sect2); | |
1023 | ||
1024 | struct gdbarch *const gdbarch = get_objfile_arch (objf1); | |
1025 | ||
1026 | complaint (&symfile_complaints, | |
1027 | _("unexpected overlap between:\n" | |
1028 | " (A) section `%s' from `%s' [%s, %s)\n" | |
1029 | " (B) section `%s' from `%s' [%s, %s).\n" | |
1030 | "Will ignore section B"), | |
1031 | bfd_section_name (abfd1, bfds1), objf1->name, | |
1032 | paddress (gdbarch, sect1_addr), | |
1033 | paddress (gdbarch, sect1_endaddr), | |
1034 | bfd_section_name (abfd2, bfds2), objf2->name, | |
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 | |
1055 | static void | |
1056 | update_section_map (struct obj_section ***pmap, int *pmap_size) | |
1057 | { | |
6fbf07cd | 1058 | int alloc_size, map_size, i; |
a845f5cb PP |
1059 | struct obj_section *s, **map; |
1060 | struct objfile *objfile; | |
1061 | ||
bb272892 | 1062 | gdb_assert (objfiles_changed_p != 0); |
a845f5cb PP |
1063 | |
1064 | map = *pmap; | |
1065 | xfree (map); | |
1066 | ||
6fbf07cd | 1067 | alloc_size = 0; |
a845f5cb | 1068 | ALL_OBJSECTIONS (objfile, s) |
6fbf07cd PP |
1069 | if (insert_section_p (objfile->obfd, s->the_bfd_section)) |
1070 | alloc_size += 1; | |
a845f5cb | 1071 | |
6fbf07cd | 1072 | map = xmalloc (alloc_size * sizeof (*map)); |
a845f5cb | 1073 | |
3aad21cf | 1074 | i = 0; |
a845f5cb | 1075 | ALL_OBJSECTIONS (objfile, s) |
6fbf07cd | 1076 | if (insert_section_p (objfile->obfd, s->the_bfd_section)) |
3aad21cf | 1077 | map[i++] = s; |
a845f5cb | 1078 | |
6fbf07cd PP |
1079 | qsort (map, alloc_size, sizeof (*map), qsort_cmp); |
1080 | map_size = filter_debuginfo_sections(map, alloc_size); | |
1081 | map_size = filter_overlapping_sections(map, map_size); | |
a845f5cb | 1082 | |
6fbf07cd PP |
1083 | if (map_size < alloc_size) |
1084 | /* Some sections were eliminated. Trim excess space. */ | |
1085 | map = xrealloc (map, map_size * sizeof (*map)); | |
3aad21cf | 1086 | else |
6fbf07cd | 1087 | gdb_assert (alloc_size == map_size); |
3aad21cf | 1088 | |
a845f5cb PP |
1089 | *pmap = map; |
1090 | *pmap_size = map_size; | |
1091 | } | |
1092 | ||
1093 | /* Bsearch comparison function. */ | |
1094 | ||
1095 | static int | |
1096 | bsearch_cmp (const void *key, const void *elt) | |
1097 | { | |
1098 | const CORE_ADDR pc = *(CORE_ADDR *) key; | |
1099 | const struct obj_section *section = *(const struct obj_section **) elt; | |
1100 | ||
1101 | if (pc < obj_section_addr (section)) | |
1102 | return -1; | |
1103 | if (pc < obj_section_endaddr (section)) | |
1104 | return 0; | |
1105 | return 1; | |
1106 | } | |
1107 | ||
714835d5 | 1108 | /* Returns a section whose range includes PC or NULL if none found. */ |
c906108c SS |
1109 | |
1110 | struct obj_section * | |
714835d5 | 1111 | find_pc_section (CORE_ADDR pc) |
c906108c | 1112 | { |
a845f5cb PP |
1113 | static struct obj_section **sections; |
1114 | static int num_sections; | |
1115 | ||
1116 | struct obj_section *s, **sp; | |
c5aa993b | 1117 | |
714835d5 UW |
1118 | /* Check for mapped overlay section first. */ |
1119 | s = find_pc_mapped_section (pc); | |
1120 | if (s) | |
1121 | return s; | |
c906108c | 1122 | |
bb272892 | 1123 | if (objfiles_changed_p != 0) |
a845f5cb PP |
1124 | { |
1125 | update_section_map (§ions, &num_sections); | |
c906108c | 1126 | |
a845f5cb PP |
1127 | /* Don't need updates to section map until objfiles are added |
1128 | or removed. */ | |
bb272892 | 1129 | objfiles_changed_p = 0; |
a845f5cb PP |
1130 | } |
1131 | ||
1132 | sp = (struct obj_section **) bsearch (&pc, sections, num_sections, | |
1133 | sizeof (*sections), bsearch_cmp); | |
1134 | if (sp != NULL) | |
1135 | return *sp; | |
714835d5 | 1136 | return NULL; |
c906108c | 1137 | } |
c5aa993b | 1138 | |
c906108c SS |
1139 | |
1140 | /* In SVR4, we recognize a trampoline by it's section name. | |
1141 | That is, if the pc is in a section named ".plt" then we are in | |
1142 | a trampoline. */ | |
1143 | ||
1144 | int | |
fba45db2 | 1145 | in_plt_section (CORE_ADDR pc, char *name) |
c906108c SS |
1146 | { |
1147 | struct obj_section *s; | |
1148 | int retval = 0; | |
c5aa993b JM |
1149 | |
1150 | s = find_pc_section (pc); | |
1151 | ||
c906108c SS |
1152 | retval = (s != NULL |
1153 | && s->the_bfd_section->name != NULL | |
6314a349 | 1154 | && strcmp (s->the_bfd_section->name, ".plt") == 0); |
c5aa993b | 1155 | return (retval); |
c906108c | 1156 | } |
0d0e1a63 MK |
1157 | \f |
1158 | ||
1159 | /* Keep a registry of per-objfile data-pointers required by other GDB | |
1160 | modules. */ | |
1161 | ||
1162 | struct objfile_data | |
1163 | { | |
1164 | unsigned index; | |
c1bd65d0 DE |
1165 | void (*save) (struct objfile *, void *); |
1166 | void (*free) (struct objfile *, void *); | |
0d0e1a63 MK |
1167 | }; |
1168 | ||
1169 | struct objfile_data_registration | |
1170 | { | |
1171 | struct objfile_data *data; | |
1172 | struct objfile_data_registration *next; | |
1173 | }; | |
1174 | ||
1175 | struct objfile_data_registry | |
1176 | { | |
1177 | struct objfile_data_registration *registrations; | |
1178 | unsigned num_registrations; | |
1179 | }; | |
1180 | ||
1181 | static struct objfile_data_registry objfile_data_registry = { NULL, 0 }; | |
1182 | ||
1183 | const struct objfile_data * | |
c1bd65d0 DE |
1184 | register_objfile_data_with_cleanup (void (*save) (struct objfile *, void *), |
1185 | void (*free) (struct objfile *, void *)) | |
0d0e1a63 MK |
1186 | { |
1187 | struct objfile_data_registration **curr; | |
1188 | ||
1189 | /* Append new registration. */ | |
1190 | for (curr = &objfile_data_registry.registrations; | |
1191 | *curr != NULL; curr = &(*curr)->next); | |
7be570e7 | 1192 | |
0d0e1a63 MK |
1193 | *curr = XMALLOC (struct objfile_data_registration); |
1194 | (*curr)->next = NULL; | |
1195 | (*curr)->data = XMALLOC (struct objfile_data); | |
1196 | (*curr)->data->index = objfile_data_registry.num_registrations++; | |
c1bd65d0 DE |
1197 | (*curr)->data->save = save; |
1198 | (*curr)->data->free = free; | |
0d0e1a63 MK |
1199 | |
1200 | return (*curr)->data; | |
1201 | } | |
1202 | ||
60c5725c DJ |
1203 | const struct objfile_data * |
1204 | register_objfile_data (void) | |
1205 | { | |
c1bd65d0 | 1206 | return register_objfile_data_with_cleanup (NULL, NULL); |
60c5725c DJ |
1207 | } |
1208 | ||
0d0e1a63 MK |
1209 | static void |
1210 | objfile_alloc_data (struct objfile *objfile) | |
1211 | { | |
1212 | gdb_assert (objfile->data == NULL); | |
1213 | objfile->num_data = objfile_data_registry.num_registrations; | |
1214 | objfile->data = XCALLOC (objfile->num_data, void *); | |
1215 | } | |
1216 | ||
1217 | static void | |
1218 | objfile_free_data (struct objfile *objfile) | |
1219 | { | |
1220 | gdb_assert (objfile->data != NULL); | |
60c5725c | 1221 | clear_objfile_data (objfile); |
0d0e1a63 MK |
1222 | xfree (objfile->data); |
1223 | objfile->data = NULL; | |
1224 | } | |
1225 | ||
7b097ae3 MK |
1226 | void |
1227 | clear_objfile_data (struct objfile *objfile) | |
1228 | { | |
60c5725c DJ |
1229 | struct objfile_data_registration *registration; |
1230 | int i; | |
1231 | ||
7b097ae3 | 1232 | gdb_assert (objfile->data != NULL); |
60c5725c | 1233 | |
c1bd65d0 DE |
1234 | /* Process all the save handlers. */ |
1235 | ||
1236 | for (registration = objfile_data_registry.registrations, i = 0; | |
1237 | i < objfile->num_data; | |
1238 | registration = registration->next, i++) | |
1239 | if (objfile->data[i] != NULL && registration->data->save != NULL) | |
1240 | registration->data->save (objfile, objfile->data[i]); | |
1241 | ||
1242 | /* Now process all the free handlers. */ | |
1243 | ||
60c5725c DJ |
1244 | for (registration = objfile_data_registry.registrations, i = 0; |
1245 | i < objfile->num_data; | |
1246 | registration = registration->next, i++) | |
c1bd65d0 DE |
1247 | if (objfile->data[i] != NULL && registration->data->free != NULL) |
1248 | registration->data->free (objfile, objfile->data[i]); | |
60c5725c | 1249 | |
7b097ae3 MK |
1250 | memset (objfile->data, 0, objfile->num_data * sizeof (void *)); |
1251 | } | |
1252 | ||
0d0e1a63 MK |
1253 | void |
1254 | set_objfile_data (struct objfile *objfile, const struct objfile_data *data, | |
1255 | void *value) | |
1256 | { | |
1257 | gdb_assert (data->index < objfile->num_data); | |
1258 | objfile->data[data->index] = value; | |
1259 | } | |
1260 | ||
1261 | void * | |
1262 | objfile_data (struct objfile *objfile, const struct objfile_data *data) | |
1263 | { | |
1264 | gdb_assert (data->index < objfile->num_data); | |
1265 | return objfile->data[data->index]; | |
1266 | } | |
a845f5cb | 1267 | |
bb272892 PP |
1268 | /* Set objfiles_changed_p so section map will be rebuilt next time it |
1269 | is used. Called by reread_symbols. */ | |
a845f5cb PP |
1270 | |
1271 | void | |
bb272892 | 1272 | objfiles_changed (void) |
a845f5cb | 1273 | { |
bb272892 | 1274 | objfiles_changed_p = 1; /* Rebuild section map next time we need it. */ |
a845f5cb | 1275 | } |
e3c69974 | 1276 | |
3db741ef PP |
1277 | /* Add reference to ABFD. Returns ABFD. */ |
1278 | struct bfd * | |
1279 | gdb_bfd_ref (struct bfd *abfd) | |
1280 | { | |
1281 | int *p_refcount = bfd_usrdata (abfd); | |
1282 | ||
1283 | if (p_refcount != NULL) | |
1284 | { | |
1285 | *p_refcount += 1; | |
1286 | return abfd; | |
1287 | } | |
1288 | ||
1289 | p_refcount = xmalloc (sizeof (*p_refcount)); | |
1290 | *p_refcount = 1; | |
1291 | bfd_usrdata (abfd) = p_refcount; | |
1292 | ||
1293 | return abfd; | |
1294 | } | |
1295 | ||
1296 | /* Unreference and possibly close ABFD. */ | |
e3c69974 PP |
1297 | void |
1298 | gdb_bfd_unref (struct bfd *abfd) | |
1299 | { | |
1300 | int *p_refcount; | |
1301 | char *name; | |
1302 | ||
1303 | if (abfd == NULL) | |
1304 | return; | |
1305 | ||
4f6f9936 | 1306 | p_refcount = bfd_usrdata (abfd); |
e3c69974 | 1307 | |
3db741ef PP |
1308 | /* Valid range for p_refcount: a pointer to int counter, which has a |
1309 | value of 1 (single owner) or 2 (shared). */ | |
1310 | gdb_assert (*p_refcount == 1 || *p_refcount == 2); | |
1311 | ||
1312 | *p_refcount -= 1; | |
1313 | if (*p_refcount > 0) | |
1314 | return; | |
e3c69974 | 1315 | |
e3c69974 | 1316 | xfree (p_refcount); |
4f6f9936 | 1317 | bfd_usrdata (abfd) = NULL; /* Paranoia. */ |
e3c69974 PP |
1318 | |
1319 | name = bfd_get_filename (abfd); | |
1320 | if (!bfd_close (abfd)) | |
1321 | warning (_("cannot close \"%s\": %s"), | |
1322 | name, bfd_errmsg (bfd_get_error ())); | |
1323 | xfree (name); | |
1324 | } |