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, |
9b254dd1 | 4 | 2002, 2003, 2004, 2007, 2008 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" |
7a292a7a | 53 | |
c906108c SS |
54 | /* Prototypes for local functions */ |
55 | ||
0d0e1a63 MK |
56 | static void objfile_alloc_data (struct objfile *objfile); |
57 | static void objfile_free_data (struct objfile *objfile); | |
58 | ||
c906108c SS |
59 | /* Externally visible variables that are owned by this module. |
60 | See declarations in objfile.h for more info. */ | |
61 | ||
c5aa993b | 62 | struct objfile *object_files; /* Linked list of all objfiles */ |
c906108c SS |
63 | struct objfile *current_objfile; /* For symbol file being read in */ |
64 | struct objfile *symfile_objfile; /* Main symbol table loaded from */ | |
65 | struct objfile *rt_common_objfile; /* For runtime common symbols */ | |
66 | ||
c906108c SS |
67 | /* Locate all mappable sections of a BFD file. |
68 | objfile_p_char is a char * to get it through | |
69 | bfd_map_over_sections; we cast it back to its proper type. */ | |
70 | ||
96baa820 JM |
71 | /* Called via bfd_map_over_sections to build up the section table that |
72 | the objfile references. The objfile contains pointers to the start | |
73 | of the table (objfile->sections) and to the first location after | |
74 | the end of the table (objfile->sections_end). */ | |
75 | ||
c906108c | 76 | static void |
7be0c536 AC |
77 | add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect, |
78 | void *objfile_p_char) | |
c906108c SS |
79 | { |
80 | struct objfile *objfile = (struct objfile *) objfile_p_char; | |
81 | struct obj_section section; | |
82 | flagword aflag; | |
83 | ||
84 | aflag = bfd_get_section_flags (abfd, asect); | |
85 | ||
ed7c5e43 | 86 | if (!(aflag & SEC_ALLOC)) |
c906108c SS |
87 | return; |
88 | ||
89 | if (0 == bfd_section_size (abfd, asect)) | |
90 | return; | |
91 | section.offset = 0; | |
92 | section.objfile = objfile; | |
93 | section.the_bfd_section = asect; | |
94 | section.ovly_mapped = 0; | |
95 | section.addr = bfd_section_vma (abfd, asect); | |
96 | section.endaddr = section.addr + bfd_section_size (abfd, asect); | |
8b92e4d5 | 97 | obstack_grow (&objfile->objfile_obstack, (char *) §ion, sizeof (section)); |
c906108c SS |
98 | objfile->sections_end = (struct obj_section *) (((unsigned long) objfile->sections_end) + 1); |
99 | } | |
100 | ||
101 | /* Builds a section table for OBJFILE. | |
102 | Returns 0 if OK, 1 on error (in which case bfd_error contains the | |
96baa820 JM |
103 | error). |
104 | ||
105 | Note that while we are building the table, which goes into the | |
106 | psymbol obstack, we hijack the sections_end pointer to instead hold | |
107 | a count of the number of sections. When bfd_map_over_sections | |
108 | returns, this count is used to compute the pointer to the end of | |
109 | the sections table, which then overwrites the count. | |
110 | ||
111 | Also note that the OFFSET and OVLY_MAPPED in each table entry | |
112 | are initialized to zero. | |
113 | ||
114 | Also note that if anything else writes to the psymbol obstack while | |
115 | we are building the table, we're pretty much hosed. */ | |
c906108c SS |
116 | |
117 | int | |
fba45db2 | 118 | build_objfile_section_table (struct objfile *objfile) |
c906108c SS |
119 | { |
120 | /* objfile->sections can be already set when reading a mapped symbol | |
121 | file. I believe that we do need to rebuild the section table in | |
122 | this case (we rebuild other things derived from the bfd), but we | |
8b92e4d5 | 123 | can't free the old one (it's in the objfile_obstack). So we just |
c906108c SS |
124 | waste some memory. */ |
125 | ||
126 | objfile->sections_end = 0; | |
c5aa993b | 127 | bfd_map_over_sections (objfile->obfd, add_to_objfile_sections, (char *) objfile); |
c906108c | 128 | objfile->sections = (struct obj_section *) |
8b92e4d5 | 129 | obstack_finish (&objfile->objfile_obstack); |
c906108c | 130 | objfile->sections_end = objfile->sections + (unsigned long) objfile->sections_end; |
c5aa993b | 131 | return (0); |
c906108c SS |
132 | } |
133 | ||
2df3850c JM |
134 | /* Given a pointer to an initialized bfd (ABFD) and some flag bits |
135 | allocate a new objfile struct, fill it in as best we can, link it | |
136 | into the list of all known objfiles, and return a pointer to the | |
137 | new objfile struct. | |
c906108c | 138 | |
2df3850c | 139 | The FLAGS word contains various bits (OBJF_*) that can be taken as |
78a4a9b9 AC |
140 | requests for specific operations. Other bits like OBJF_SHARED are |
141 | simply copied through to the new objfile flags member. */ | |
c906108c | 142 | |
eb9a305d DC |
143 | /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0 |
144 | by jv-lang.c, to create an artificial objfile used to hold | |
145 | information about dynamically-loaded Java classes. Unfortunately, | |
146 | that branch of this function doesn't get tested very frequently, so | |
147 | it's prone to breakage. (E.g. at one time the name was set to NULL | |
148 | in that situation, which broke a loop over all names in the dynamic | |
149 | library loader.) If you change this function, please try to leave | |
150 | things in a consistent state even if abfd is NULL. */ | |
151 | ||
c906108c | 152 | struct objfile * |
fba45db2 | 153 | allocate_objfile (bfd *abfd, int flags) |
c906108c SS |
154 | { |
155 | struct objfile *objfile = NULL; | |
156 | struct objfile *last_one = NULL; | |
157 | ||
c906108c SS |
158 | /* If we don't support mapped symbol files, didn't ask for the file to be |
159 | mapped, or failed to open the mapped file for some reason, then revert | |
160 | back to an unmapped objfile. */ | |
161 | ||
162 | if (objfile == NULL) | |
163 | { | |
164 | objfile = (struct objfile *) xmalloc (sizeof (struct objfile)); | |
165 | memset (objfile, 0, sizeof (struct objfile)); | |
c5aa993b | 166 | objfile->md = NULL; |
af5f3db6 AC |
167 | objfile->psymbol_cache = bcache_xmalloc (); |
168 | objfile->macro_cache = bcache_xmalloc (); | |
1ab21617 EZ |
169 | /* We could use obstack_specify_allocation here instead, but |
170 | gdb_obstack.h specifies the alloc/dealloc functions. */ | |
171 | obstack_init (&objfile->objfile_obstack); | |
15831452 | 172 | terminate_minimal_symbol_table (objfile); |
c906108c SS |
173 | } |
174 | ||
0d0e1a63 MK |
175 | objfile_alloc_data (objfile); |
176 | ||
c906108c SS |
177 | /* Update the per-objfile information that comes from the bfd, ensuring |
178 | that any data that is reference is saved in the per-objfile data | |
179 | region. */ | |
180 | ||
c5aa993b JM |
181 | objfile->obfd = abfd; |
182 | if (objfile->name != NULL) | |
c906108c | 183 | { |
2dc74dc1 | 184 | xfree (objfile->name); |
c906108c SS |
185 | } |
186 | if (abfd != NULL) | |
187 | { | |
5e2b427d UW |
188 | /* Look up the gdbarch associated with the BFD. */ |
189 | objfile->gdbarch = gdbarch_from_bfd (abfd); | |
190 | ||
982526a1 | 191 | objfile->name = xstrdup (bfd_get_filename (abfd)); |
c5aa993b | 192 | objfile->mtime = bfd_get_mtime (abfd); |
c906108c SS |
193 | |
194 | /* Build section table. */ | |
195 | ||
196 | if (build_objfile_section_table (objfile)) | |
197 | { | |
8a3fe4f8 | 198 | error (_("Can't find the file sections in `%s': %s"), |
c5aa993b | 199 | objfile->name, bfd_errmsg (bfd_get_error ())); |
c906108c SS |
200 | } |
201 | } | |
eb9a305d DC |
202 | else |
203 | { | |
982526a1 | 204 | objfile->name = xstrdup ("<<anonymous objfile>>"); |
eb9a305d | 205 | } |
c906108c | 206 | |
b8fbeb18 EZ |
207 | /* Initialize the section indexes for this objfile, so that we can |
208 | later detect if they are used w/o being properly assigned to. */ | |
209 | ||
5c4e30ca DC |
210 | objfile->sect_index_text = -1; |
211 | objfile->sect_index_data = -1; | |
212 | objfile->sect_index_bss = -1; | |
213 | objfile->sect_index_rodata = -1; | |
214 | ||
215 | /* We don't yet have a C++-specific namespace symtab. */ | |
216 | ||
217 | objfile->cp_namespace_symtab = NULL; | |
b8fbeb18 | 218 | |
c906108c SS |
219 | /* Add this file onto the tail of the linked list of other such files. */ |
220 | ||
c5aa993b | 221 | objfile->next = NULL; |
c906108c SS |
222 | if (object_files == NULL) |
223 | object_files = objfile; | |
224 | else | |
225 | { | |
226 | for (last_one = object_files; | |
c5aa993b JM |
227 | last_one->next; |
228 | last_one = last_one->next); | |
229 | last_one->next = objfile; | |
c906108c SS |
230 | } |
231 | ||
2df3850c JM |
232 | /* Save passed in flag bits. */ |
233 | objfile->flags |= flags; | |
c906108c SS |
234 | |
235 | return (objfile); | |
236 | } | |
237 | ||
5e2b427d UW |
238 | /* Retrieve the gdbarch associated with OBJFILE. */ |
239 | struct gdbarch * | |
240 | get_objfile_arch (struct objfile *objfile) | |
241 | { | |
242 | return objfile->gdbarch; | |
243 | } | |
244 | ||
9ab9195f EZ |
245 | /* Initialize entry point information for this objfile. */ |
246 | ||
247 | void | |
248 | init_entry_point_info (struct objfile *objfile) | |
249 | { | |
250 | /* Save startup file's range of PC addresses to help blockframe.c | |
251 | decide where the bottom of the stack is. */ | |
252 | ||
253 | if (bfd_get_file_flags (objfile->obfd) & EXEC_P) | |
254 | { | |
255 | /* Executable file -- record its entry point so we'll recognize | |
256 | the startup file because it contains the entry point. */ | |
257 | objfile->ei.entry_point = bfd_get_start_address (objfile->obfd); | |
258 | } | |
574dffa2 DJ |
259 | else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC |
260 | && bfd_get_start_address (objfile->obfd) != 0) | |
261 | /* Some shared libraries may have entry points set and be | |
262 | runnable. There's no clear way to indicate this, so just check | |
263 | for values other than zero. */ | |
264 | objfile->ei.entry_point = bfd_get_start_address (objfile->obfd); | |
9ab9195f EZ |
265 | else |
266 | { | |
267 | /* Examination of non-executable.o files. Short-circuit this stuff. */ | |
268 | objfile->ei.entry_point = INVALID_ENTRY_POINT; | |
269 | } | |
9ab9195f EZ |
270 | } |
271 | ||
272 | /* Get current entry point address. */ | |
273 | ||
274 | CORE_ADDR | |
275 | entry_point_address (void) | |
276 | { | |
277 | return symfile_objfile ? symfile_objfile->ei.entry_point : 0; | |
278 | } | |
15831452 JB |
279 | |
280 | /* Create the terminating entry of OBJFILE's minimal symbol table. | |
281 | If OBJFILE->msymbols is zero, allocate a single entry from | |
4a146b47 | 282 | OBJFILE->objfile_obstack; otherwise, just initialize |
15831452 JB |
283 | OBJFILE->msymbols[OBJFILE->minimal_symbol_count]. */ |
284 | void | |
285 | terminate_minimal_symbol_table (struct objfile *objfile) | |
286 | { | |
287 | if (! objfile->msymbols) | |
288 | objfile->msymbols = ((struct minimal_symbol *) | |
4a146b47 | 289 | obstack_alloc (&objfile->objfile_obstack, |
15831452 JB |
290 | sizeof (objfile->msymbols[0]))); |
291 | ||
292 | { | |
293 | struct minimal_symbol *m | |
294 | = &objfile->msymbols[objfile->minimal_symbol_count]; | |
295 | ||
296 | memset (m, 0, sizeof (*m)); | |
5bf0017e EZ |
297 | /* Don't rely on these enumeration values being 0's. */ |
298 | MSYMBOL_TYPE (m) = mst_unknown; | |
15831452 JB |
299 | SYMBOL_INIT_LANGUAGE_SPECIFIC (m, language_unknown); |
300 | } | |
301 | } | |
302 | ||
303 | ||
5b5d99cf JB |
304 | /* Put one object file before a specified on in the global list. |
305 | This can be used to make sure an object file is destroyed before | |
306 | another when using ALL_OBJFILES_SAFE to free all objfiles. */ | |
307 | void | |
308 | put_objfile_before (struct objfile *objfile, struct objfile *before_this) | |
309 | { | |
310 | struct objfile **objp; | |
311 | ||
312 | unlink_objfile (objfile); | |
313 | ||
314 | for (objp = &object_files; *objp != NULL; objp = &((*objp)->next)) | |
315 | { | |
316 | if (*objp == before_this) | |
317 | { | |
318 | objfile->next = *objp; | |
319 | *objp = objfile; | |
320 | return; | |
321 | } | |
322 | } | |
323 | ||
324 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 325 | _("put_objfile_before: before objfile not in list")); |
5b5d99cf JB |
326 | } |
327 | ||
c906108c SS |
328 | /* Put OBJFILE at the front of the list. */ |
329 | ||
330 | void | |
fba45db2 | 331 | objfile_to_front (struct objfile *objfile) |
c906108c SS |
332 | { |
333 | struct objfile **objp; | |
334 | for (objp = &object_files; *objp != NULL; objp = &((*objp)->next)) | |
335 | { | |
336 | if (*objp == objfile) | |
337 | { | |
338 | /* Unhook it from where it is. */ | |
339 | *objp = objfile->next; | |
340 | /* Put it in the front. */ | |
341 | objfile->next = object_files; | |
342 | object_files = objfile; | |
343 | break; | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
348 | /* Unlink OBJFILE from the list of known objfiles, if it is found in the | |
349 | list. | |
350 | ||
351 | It is not a bug, or error, to call this function if OBJFILE is not known | |
352 | to be in the current list. This is done in the case of mapped objfiles, | |
353 | for example, just to ensure that the mapped objfile doesn't appear twice | |
354 | in the list. Since the list is threaded, linking in a mapped objfile | |
355 | twice would create a circular list. | |
356 | ||
357 | If OBJFILE turns out to be in the list, we zap it's NEXT pointer after | |
358 | unlinking it, just to ensure that we have completely severed any linkages | |
359 | between the OBJFILE and the list. */ | |
360 | ||
361 | void | |
fba45db2 | 362 | unlink_objfile (struct objfile *objfile) |
c906108c | 363 | { |
c5aa993b | 364 | struct objfile **objpp; |
c906108c | 365 | |
c5aa993b | 366 | for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next)) |
c906108c | 367 | { |
c5aa993b | 368 | if (*objpp == objfile) |
c906108c | 369 | { |
c5aa993b JM |
370 | *objpp = (*objpp)->next; |
371 | objfile->next = NULL; | |
07cd4b97 | 372 | return; |
c906108c SS |
373 | } |
374 | } | |
07cd4b97 | 375 | |
8e65ff28 | 376 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 377 | _("unlink_objfile: objfile already unlinked")); |
c906108c SS |
378 | } |
379 | ||
380 | ||
381 | /* Destroy an objfile and all the symtabs and psymtabs under it. Note | |
4a146b47 EZ |
382 | that as much as possible is allocated on the objfile_obstack |
383 | so that the memory can be efficiently freed. | |
c906108c SS |
384 | |
385 | Things which we do NOT free because they are not in malloc'd memory | |
386 | or not in memory specific to the objfile include: | |
387 | ||
c5aa993b | 388 | objfile -> sf |
c906108c SS |
389 | |
390 | FIXME: If the objfile is using reusable symbol information (via mmalloc), | |
391 | then we need to take into account the fact that more than one process | |
392 | may be using the symbol information at the same time (when mmalloc is | |
393 | extended to support cooperative locking). When more than one process | |
394 | is using the mapped symbol info, we need to be more careful about when | |
395 | we free objects in the reusable area. */ | |
396 | ||
397 | void | |
fba45db2 | 398 | free_objfile (struct objfile *objfile) |
c906108c | 399 | { |
5b5d99cf JB |
400 | if (objfile->separate_debug_objfile) |
401 | { | |
402 | free_objfile (objfile->separate_debug_objfile); | |
403 | } | |
404 | ||
405 | if (objfile->separate_debug_objfile_backlink) | |
406 | { | |
407 | /* We freed the separate debug file, make sure the base objfile | |
408 | doesn't reference it. */ | |
409 | objfile->separate_debug_objfile_backlink->separate_debug_objfile = NULL; | |
410 | } | |
411 | ||
ae5a43e0 DJ |
412 | /* Remove any references to this objfile in the global value |
413 | lists. */ | |
414 | preserve_values (objfile); | |
415 | ||
c906108c SS |
416 | /* First do any symbol file specific actions required when we are |
417 | finished with a particular symbol file. Note that if the objfile | |
418 | is using reusable symbol information (via mmalloc) then each of | |
419 | these routines is responsible for doing the correct thing, either | |
420 | freeing things which are valid only during this particular gdb | |
421 | execution, or leaving them to be reused during the next one. */ | |
422 | ||
c5aa993b | 423 | if (objfile->sf != NULL) |
c906108c | 424 | { |
c5aa993b | 425 | (*objfile->sf->sym_finish) (objfile); |
c906108c SS |
426 | } |
427 | ||
428 | /* We always close the bfd. */ | |
429 | ||
c5aa993b | 430 | if (objfile->obfd != NULL) |
c906108c SS |
431 | { |
432 | char *name = bfd_get_filename (objfile->obfd); | |
c5aa993b | 433 | if (!bfd_close (objfile->obfd)) |
8a3fe4f8 | 434 | warning (_("cannot close \"%s\": %s"), |
c906108c | 435 | name, bfd_errmsg (bfd_get_error ())); |
b8c9b27d | 436 | xfree (name); |
c906108c SS |
437 | } |
438 | ||
439 | /* Remove it from the chain of all objfiles. */ | |
440 | ||
441 | unlink_objfile (objfile); | |
442 | ||
443 | /* If we are going to free the runtime common objfile, mark it | |
444 | as unallocated. */ | |
445 | ||
446 | if (objfile == rt_common_objfile) | |
447 | rt_common_objfile = NULL; | |
448 | ||
449 | /* Before the symbol table code was redone to make it easier to | |
450 | selectively load and remove information particular to a specific | |
451 | linkage unit, gdb used to do these things whenever the monolithic | |
452 | symbol table was blown away. How much still needs to be done | |
453 | is unknown, but we play it safe for now and keep each action until | |
454 | it is shown to be no longer needed. */ | |
c5aa993b | 455 | |
cb5d864f FF |
456 | /* Not all our callers call clear_symtab_users (objfile_purge_solibs, |
457 | for example), so we need to call this here. */ | |
c906108c SS |
458 | clear_pc_function_cache (); |
459 | ||
9bdcbae7 DJ |
460 | /* Clear globals which might have pointed into a removed objfile. |
461 | FIXME: It's not clear which of these are supposed to persist | |
462 | between expressions and which ought to be reset each time. */ | |
463 | expression_context_block = NULL; | |
464 | innermost_block = NULL; | |
465 | ||
cb5d864f FF |
466 | /* Check to see if the current_source_symtab belongs to this objfile, |
467 | and if so, call clear_current_source_symtab_and_line. */ | |
468 | ||
469 | { | |
470 | struct symtab_and_line cursal = get_current_source_symtab_and_line (); | |
471 | struct symtab *s; | |
472 | ||
473 | ALL_OBJFILE_SYMTABS (objfile, s) | |
474 | { | |
475 | if (s == cursal.symtab) | |
476 | clear_current_source_symtab_and_line (); | |
477 | } | |
478 | } | |
479 | ||
78a4a9b9 | 480 | /* The last thing we do is free the objfile struct itself. */ |
c906108c | 481 | |
78a4a9b9 AC |
482 | objfile_free_data (objfile); |
483 | if (objfile->name != NULL) | |
c906108c | 484 | { |
2dc74dc1 | 485 | xfree (objfile->name); |
c906108c | 486 | } |
78a4a9b9 | 487 | if (objfile->global_psymbols.list) |
2dc74dc1 | 488 | xfree (objfile->global_psymbols.list); |
78a4a9b9 | 489 | if (objfile->static_psymbols.list) |
2dc74dc1 | 490 | xfree (objfile->static_psymbols.list); |
78a4a9b9 AC |
491 | /* Free the obstacks for non-reusable objfiles */ |
492 | bcache_xfree (objfile->psymbol_cache); | |
493 | bcache_xfree (objfile->macro_cache); | |
494 | if (objfile->demangled_names_hash) | |
495 | htab_delete (objfile->demangled_names_hash); | |
b99607ea | 496 | obstack_free (&objfile->objfile_obstack, 0); |
2dc74dc1 | 497 | xfree (objfile); |
78a4a9b9 | 498 | objfile = NULL; |
c906108c SS |
499 | } |
500 | ||
74b7792f AC |
501 | static void |
502 | do_free_objfile_cleanup (void *obj) | |
503 | { | |
504 | free_objfile (obj); | |
505 | } | |
506 | ||
507 | struct cleanup * | |
508 | make_cleanup_free_objfile (struct objfile *obj) | |
509 | { | |
510 | return make_cleanup (do_free_objfile_cleanup, obj); | |
511 | } | |
c906108c SS |
512 | |
513 | /* Free all the object files at once and clean up their users. */ | |
514 | ||
515 | void | |
fba45db2 | 516 | free_all_objfiles (void) |
c906108c SS |
517 | { |
518 | struct objfile *objfile, *temp; | |
519 | ||
520 | ALL_OBJFILES_SAFE (objfile, temp) | |
c5aa993b JM |
521 | { |
522 | free_objfile (objfile); | |
523 | } | |
c906108c SS |
524 | clear_symtab_users (); |
525 | } | |
526 | \f | |
527 | /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS | |
528 | entries in new_offsets. */ | |
529 | void | |
fba45db2 | 530 | objfile_relocate (struct objfile *objfile, struct section_offsets *new_offsets) |
c906108c | 531 | { |
30510692 | 532 | struct obj_section *s; |
d4f3574e | 533 | struct section_offsets *delta = |
a39a16c4 MM |
534 | ((struct section_offsets *) |
535 | alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections))); | |
c906108c SS |
536 | |
537 | { | |
538 | int i; | |
539 | int something_changed = 0; | |
540 | for (i = 0; i < objfile->num_sections; ++i) | |
541 | { | |
a4c8257b | 542 | delta->offsets[i] = |
c906108c SS |
543 | ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i); |
544 | if (ANOFFSET (delta, i) != 0) | |
545 | something_changed = 1; | |
546 | } | |
547 | if (!something_changed) | |
548 | return; | |
549 | } | |
550 | ||
551 | /* OK, get all the symtabs. */ | |
552 | { | |
553 | struct symtab *s; | |
554 | ||
555 | ALL_OBJFILE_SYMTABS (objfile, s) | |
c5aa993b JM |
556 | { |
557 | struct linetable *l; | |
558 | struct blockvector *bv; | |
559 | int i; | |
560 | ||
561 | /* First the line table. */ | |
562 | l = LINETABLE (s); | |
563 | if (l) | |
564 | { | |
565 | for (i = 0; i < l->nitems; ++i) | |
566 | l->item[i].pc += ANOFFSET (delta, s->block_line_section); | |
567 | } | |
c906108c | 568 | |
c5aa993b JM |
569 | /* Don't relocate a shared blockvector more than once. */ |
570 | if (!s->primary) | |
571 | continue; | |
c906108c | 572 | |
c5aa993b JM |
573 | bv = BLOCKVECTOR (s); |
574 | for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i) | |
575 | { | |
576 | struct block *b; | |
e88c90f2 | 577 | struct symbol *sym; |
de4f826b | 578 | struct dict_iterator iter; |
c5aa993b JM |
579 | |
580 | b = BLOCKVECTOR_BLOCK (bv, i); | |
581 | BLOCK_START (b) += ANOFFSET (delta, s->block_line_section); | |
582 | BLOCK_END (b) += ANOFFSET (delta, s->block_line_section); | |
801e3a5b JB |
583 | if (BLOCKVECTOR_MAP (bv)) |
584 | addrmap_relocate (BLOCKVECTOR_MAP (bv), | |
585 | ANOFFSET (delta, s->block_line_section)); | |
c5aa993b | 586 | |
de4f826b | 587 | ALL_BLOCK_SYMBOLS (b, iter, sym) |
c5aa993b | 588 | { |
7a78d0ee KB |
589 | fixup_symbol_section (sym, objfile); |
590 | ||
c5aa993b | 591 | /* The RS6000 code from which this was taken skipped |
176620f1 | 592 | any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN. |
c5aa993b JM |
593 | But I'm leaving out that test, on the theory that |
594 | they can't possibly pass the tests below. */ | |
595 | if ((SYMBOL_CLASS (sym) == LOC_LABEL | |
0bb4e8c4 | 596 | || SYMBOL_CLASS (sym) == LOC_STATIC) |
c5aa993b JM |
597 | && SYMBOL_SECTION (sym) >= 0) |
598 | { | |
599 | SYMBOL_VALUE_ADDRESS (sym) += | |
600 | ANOFFSET (delta, SYMBOL_SECTION (sym)); | |
601 | } | |
c5aa993b JM |
602 | } |
603 | } | |
604 | } | |
c906108c SS |
605 | } |
606 | ||
607 | { | |
608 | struct partial_symtab *p; | |
609 | ||
610 | ALL_OBJFILE_PSYMTABS (objfile, p) | |
c5aa993b | 611 | { |
b8fbeb18 EZ |
612 | p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile)); |
613 | p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile)); | |
c5aa993b | 614 | } |
c906108c SS |
615 | } |
616 | ||
617 | { | |
618 | struct partial_symbol **psym; | |
619 | ||
620 | for (psym = objfile->global_psymbols.list; | |
621 | psym < objfile->global_psymbols.next; | |
622 | psym++) | |
7a78d0ee KB |
623 | { |
624 | fixup_psymbol_section (*psym, objfile); | |
625 | if (SYMBOL_SECTION (*psym) >= 0) | |
626 | SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta, | |
627 | SYMBOL_SECTION (*psym)); | |
628 | } | |
c906108c SS |
629 | for (psym = objfile->static_psymbols.list; |
630 | psym < objfile->static_psymbols.next; | |
631 | psym++) | |
7a78d0ee KB |
632 | { |
633 | fixup_psymbol_section (*psym, objfile); | |
634 | if (SYMBOL_SECTION (*psym) >= 0) | |
635 | SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta, | |
636 | SYMBOL_SECTION (*psym)); | |
637 | } | |
c906108c SS |
638 | } |
639 | ||
640 | { | |
641 | struct minimal_symbol *msym; | |
642 | ALL_OBJFILE_MSYMBOLS (objfile, msym) | |
643 | if (SYMBOL_SECTION (msym) >= 0) | |
c5aa993b | 644 | SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym)); |
c906108c SS |
645 | } |
646 | /* Relocating different sections by different amounts may cause the symbols | |
647 | to be out of order. */ | |
648 | msymbols_sort (objfile); | |
649 | ||
650 | { | |
651 | int i; | |
652 | for (i = 0; i < objfile->num_sections; ++i) | |
a4c8257b | 653 | (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i); |
c906108c SS |
654 | } |
655 | ||
36b0c0e0 PS |
656 | if (objfile->ei.entry_point != ~(CORE_ADDR) 0) |
657 | { | |
658 | /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT | |
659 | only as a fallback. */ | |
660 | struct obj_section *s; | |
661 | s = find_pc_section (objfile->ei.entry_point); | |
662 | if (s) | |
663 | objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index); | |
664 | else | |
665 | objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile)); | |
666 | } | |
667 | ||
c906108c SS |
668 | { |
669 | struct obj_section *s; | |
670 | bfd *abfd; | |
671 | ||
672 | abfd = objfile->obfd; | |
673 | ||
96baa820 | 674 | ALL_OBJFILE_OSECTIONS (objfile, s) |
c906108c | 675 | { |
78f0949b KB |
676 | int idx = s->the_bfd_section->index; |
677 | ||
678 | s->addr += ANOFFSET (delta, idx); | |
679 | s->endaddr += ANOFFSET (delta, idx); | |
c906108c SS |
680 | } |
681 | } | |
682 | ||
30510692 DJ |
683 | /* Update the table in exec_ops, used to read memory. */ |
684 | ALL_OBJFILE_OSECTIONS (objfile, s) | |
685 | { | |
686 | int idx = s->the_bfd_section->index; | |
687 | ||
688 | exec_set_section_address (bfd_get_filename (objfile->obfd), idx, | |
689 | s->addr); | |
690 | } | |
691 | ||
c906108c SS |
692 | /* Relocate breakpoints as necessary, after things are relocated. */ |
693 | breakpoint_re_set (); | |
694 | } | |
695 | \f | |
696 | /* Many places in gdb want to test just to see if we have any partial | |
697 | symbols available. This function returns zero if none are currently | |
698 | available, nonzero otherwise. */ | |
699 | ||
700 | int | |
fba45db2 | 701 | have_partial_symbols (void) |
c906108c SS |
702 | { |
703 | struct objfile *ofp; | |
704 | ||
705 | ALL_OBJFILES (ofp) | |
c5aa993b JM |
706 | { |
707 | if (ofp->psymtabs != NULL) | |
708 | { | |
709 | return 1; | |
710 | } | |
711 | } | |
c906108c SS |
712 | return 0; |
713 | } | |
714 | ||
715 | /* Many places in gdb want to test just to see if we have any full | |
716 | symbols available. This function returns zero if none are currently | |
717 | available, nonzero otherwise. */ | |
718 | ||
719 | int | |
fba45db2 | 720 | have_full_symbols (void) |
c906108c SS |
721 | { |
722 | struct objfile *ofp; | |
723 | ||
724 | ALL_OBJFILES (ofp) | |
c5aa993b JM |
725 | { |
726 | if (ofp->symtabs != NULL) | |
727 | { | |
728 | return 1; | |
729 | } | |
730 | } | |
c906108c SS |
731 | return 0; |
732 | } | |
733 | ||
734 | ||
735 | /* This operations deletes all objfile entries that represent solibs that | |
736 | weren't explicitly loaded by the user, via e.g., the add-symbol-file | |
737 | command. | |
c5aa993b | 738 | */ |
c906108c | 739 | void |
fba45db2 | 740 | objfile_purge_solibs (void) |
c906108c | 741 | { |
c5aa993b JM |
742 | struct objfile *objf; |
743 | struct objfile *temp; | |
c906108c SS |
744 | |
745 | ALL_OBJFILES_SAFE (objf, temp) | |
746 | { | |
747 | /* We assume that the solib package has been purged already, or will | |
748 | be soon. | |
c5aa993b | 749 | */ |
2df3850c | 750 | if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED)) |
c906108c SS |
751 | free_objfile (objf); |
752 | } | |
753 | } | |
754 | ||
755 | ||
756 | /* Many places in gdb want to test just to see if we have any minimal | |
757 | symbols available. This function returns zero if none are currently | |
758 | available, nonzero otherwise. */ | |
759 | ||
760 | int | |
fba45db2 | 761 | have_minimal_symbols (void) |
c906108c SS |
762 | { |
763 | struct objfile *ofp; | |
764 | ||
765 | ALL_OBJFILES (ofp) | |
c5aa993b | 766 | { |
15831452 | 767 | if (ofp->minimal_symbol_count > 0) |
c5aa993b JM |
768 | { |
769 | return 1; | |
770 | } | |
771 | } | |
c906108c SS |
772 | return 0; |
773 | } | |
774 | ||
198beae2 AC |
775 | /* Returns a section whose range includes PC and SECTION, or NULL if |
776 | none found. Note the distinction between the return type, struct | |
777 | obj_section (which is defined in gdb), and the input type "struct | |
778 | bfd_section" (which is a bfd-defined data type). The obj_section | |
779 | contains a pointer to the "struct bfd_section". */ | |
c906108c SS |
780 | |
781 | struct obj_section * | |
198beae2 | 782 | find_pc_sect_section (CORE_ADDR pc, struct bfd_section *section) |
c906108c SS |
783 | { |
784 | struct obj_section *s; | |
785 | struct objfile *objfile; | |
c5aa993b | 786 | |
96baa820 | 787 | ALL_OBJSECTIONS (objfile, s) |
c5aa993b JM |
788 | if ((section == 0 || section == s->the_bfd_section) && |
789 | s->addr <= pc && pc < s->endaddr) | |
c5aa993b | 790 | return (s); |
c906108c | 791 | |
c5aa993b | 792 | return (NULL); |
c906108c SS |
793 | } |
794 | ||
795 | /* Returns a section whose range includes PC or NULL if none found. | |
796 | Backward compatibility, no section. */ | |
797 | ||
798 | struct obj_section * | |
fba45db2 | 799 | find_pc_section (CORE_ADDR pc) |
c906108c SS |
800 | { |
801 | return find_pc_sect_section (pc, find_pc_mapped_section (pc)); | |
802 | } | |
c5aa993b | 803 | |
c906108c SS |
804 | |
805 | /* In SVR4, we recognize a trampoline by it's section name. | |
806 | That is, if the pc is in a section named ".plt" then we are in | |
807 | a trampoline. */ | |
808 | ||
809 | int | |
fba45db2 | 810 | in_plt_section (CORE_ADDR pc, char *name) |
c906108c SS |
811 | { |
812 | struct obj_section *s; | |
813 | int retval = 0; | |
c5aa993b JM |
814 | |
815 | s = find_pc_section (pc); | |
816 | ||
c906108c SS |
817 | retval = (s != NULL |
818 | && s->the_bfd_section->name != NULL | |
6314a349 | 819 | && strcmp (s->the_bfd_section->name, ".plt") == 0); |
c5aa993b | 820 | return (retval); |
c906108c | 821 | } |
0d0e1a63 MK |
822 | \f |
823 | ||
824 | /* Keep a registry of per-objfile data-pointers required by other GDB | |
825 | modules. */ | |
826 | ||
827 | struct objfile_data | |
828 | { | |
829 | unsigned index; | |
60c5725c | 830 | void (*cleanup) (struct objfile *, void *); |
0d0e1a63 MK |
831 | }; |
832 | ||
833 | struct objfile_data_registration | |
834 | { | |
835 | struct objfile_data *data; | |
836 | struct objfile_data_registration *next; | |
837 | }; | |
838 | ||
839 | struct objfile_data_registry | |
840 | { | |
841 | struct objfile_data_registration *registrations; | |
842 | unsigned num_registrations; | |
843 | }; | |
844 | ||
845 | static struct objfile_data_registry objfile_data_registry = { NULL, 0 }; | |
846 | ||
847 | const struct objfile_data * | |
60c5725c | 848 | register_objfile_data_with_cleanup (void (*cleanup) (struct objfile *, void *)) |
0d0e1a63 MK |
849 | { |
850 | struct objfile_data_registration **curr; | |
851 | ||
852 | /* Append new registration. */ | |
853 | for (curr = &objfile_data_registry.registrations; | |
854 | *curr != NULL; curr = &(*curr)->next); | |
7be570e7 | 855 | |
0d0e1a63 MK |
856 | *curr = XMALLOC (struct objfile_data_registration); |
857 | (*curr)->next = NULL; | |
858 | (*curr)->data = XMALLOC (struct objfile_data); | |
859 | (*curr)->data->index = objfile_data_registry.num_registrations++; | |
60c5725c | 860 | (*curr)->data->cleanup = cleanup; |
0d0e1a63 MK |
861 | |
862 | return (*curr)->data; | |
863 | } | |
864 | ||
60c5725c DJ |
865 | const struct objfile_data * |
866 | register_objfile_data (void) | |
867 | { | |
868 | return register_objfile_data_with_cleanup (NULL); | |
869 | } | |
870 | ||
0d0e1a63 MK |
871 | static void |
872 | objfile_alloc_data (struct objfile *objfile) | |
873 | { | |
874 | gdb_assert (objfile->data == NULL); | |
875 | objfile->num_data = objfile_data_registry.num_registrations; | |
876 | objfile->data = XCALLOC (objfile->num_data, void *); | |
877 | } | |
878 | ||
879 | static void | |
880 | objfile_free_data (struct objfile *objfile) | |
881 | { | |
882 | gdb_assert (objfile->data != NULL); | |
60c5725c | 883 | clear_objfile_data (objfile); |
0d0e1a63 MK |
884 | xfree (objfile->data); |
885 | objfile->data = NULL; | |
886 | } | |
887 | ||
7b097ae3 MK |
888 | void |
889 | clear_objfile_data (struct objfile *objfile) | |
890 | { | |
60c5725c DJ |
891 | struct objfile_data_registration *registration; |
892 | int i; | |
893 | ||
7b097ae3 | 894 | gdb_assert (objfile->data != NULL); |
60c5725c DJ |
895 | |
896 | for (registration = objfile_data_registry.registrations, i = 0; | |
897 | i < objfile->num_data; | |
898 | registration = registration->next, i++) | |
899 | if (objfile->data[i] != NULL && registration->data->cleanup) | |
900 | registration->data->cleanup (objfile, objfile->data[i]); | |
901 | ||
7b097ae3 MK |
902 | memset (objfile->data, 0, objfile->num_data * sizeof (void *)); |
903 | } | |
904 | ||
0d0e1a63 MK |
905 | void |
906 | set_objfile_data (struct objfile *objfile, const struct objfile_data *data, | |
907 | void *value) | |
908 | { | |
909 | gdb_assert (data->index < objfile->num_data); | |
910 | objfile->data[data->index] = value; | |
911 | } | |
912 | ||
913 | void * | |
914 | objfile_data (struct objfile *objfile, const struct objfile_data *data) | |
915 | { | |
916 | gdb_assert (data->index < objfile->num_data); | |
917 | return objfile->data[data->index]; | |
918 | } |