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