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