15796c8dcSSimon Schubert /* GDB routines for manipulating objfiles.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 1992-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert Contributed by Cygnus Support, using pieces from other GDB modules.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This file is part of GDB.
85796c8dcSSimon Schubert
95796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert (at your option) any later version.
135796c8dcSSimon Schubert
145796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
175796c8dcSSimon Schubert GNU General Public License for more details.
185796c8dcSSimon Schubert
195796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
215796c8dcSSimon Schubert
225796c8dcSSimon Schubert /* This file contains support routines for creating, manipulating, and
235796c8dcSSimon Schubert destroying objfile structures. */
245796c8dcSSimon Schubert
255796c8dcSSimon Schubert #include "defs.h"
265796c8dcSSimon Schubert #include "bfd.h" /* Binary File Description */
275796c8dcSSimon Schubert #include "symtab.h"
285796c8dcSSimon Schubert #include "symfile.h"
295796c8dcSSimon Schubert #include "objfiles.h"
305796c8dcSSimon Schubert #include "gdb-stabs.h"
315796c8dcSSimon Schubert #include "target.h"
325796c8dcSSimon Schubert #include "bcache.h"
335796c8dcSSimon Schubert #include "expression.h"
345796c8dcSSimon Schubert #include "parser-defs.h"
355796c8dcSSimon Schubert
365796c8dcSSimon Schubert #include "gdb_assert.h"
375796c8dcSSimon Schubert #include <sys/types.h>
385796c8dcSSimon Schubert #include "gdb_stat.h"
395796c8dcSSimon Schubert #include <fcntl.h>
405796c8dcSSimon Schubert #include "gdb_obstack.h"
415796c8dcSSimon Schubert #include "gdb_string.h"
425796c8dcSSimon Schubert #include "hashtab.h"
435796c8dcSSimon Schubert
445796c8dcSSimon Schubert #include "breakpoint.h"
455796c8dcSSimon Schubert #include "block.h"
465796c8dcSSimon Schubert #include "dictionary.h"
475796c8dcSSimon Schubert #include "source.h"
485796c8dcSSimon Schubert #include "addrmap.h"
495796c8dcSSimon Schubert #include "arch-utils.h"
505796c8dcSSimon Schubert #include "exec.h"
515796c8dcSSimon Schubert #include "observer.h"
525796c8dcSSimon Schubert #include "complaints.h"
53cf7f2e2dSJohn Marino #include "psymtab.h"
54cf7f2e2dSJohn Marino #include "solist.h"
55*ef5ccd6cSJohn Marino #include "gdb_bfd.h"
56*ef5ccd6cSJohn Marino #include "btrace.h"
575796c8dcSSimon Schubert
58*ef5ccd6cSJohn Marino /* Keep a registry of per-objfile data-pointers required by other GDB
59*ef5ccd6cSJohn Marino modules. */
605796c8dcSSimon Schubert
61*ef5ccd6cSJohn Marino DEFINE_REGISTRY (objfile, REGISTRY_ACCESS_FIELD)
625796c8dcSSimon Schubert
635796c8dcSSimon Schubert /* Externally visible variables that are owned by this module.
645796c8dcSSimon Schubert See declarations in objfile.h for more info. */
655796c8dcSSimon Schubert
665796c8dcSSimon Schubert struct objfile *rt_common_objfile; /* For runtime common symbols */
675796c8dcSSimon Schubert
68cf7f2e2dSJohn Marino struct objfile_pspace_info
69cf7f2e2dSJohn Marino {
70cf7f2e2dSJohn Marino int objfiles_changed_p;
71cf7f2e2dSJohn Marino struct obj_section **sections;
72cf7f2e2dSJohn Marino int num_sections;
73cf7f2e2dSJohn Marino };
74cf7f2e2dSJohn Marino
75cf7f2e2dSJohn Marino /* Per-program-space data key. */
76cf7f2e2dSJohn Marino static const struct program_space_data *objfiles_pspace_data;
77cf7f2e2dSJohn Marino
78cf7f2e2dSJohn Marino static void
objfiles_pspace_data_cleanup(struct program_space * pspace,void * arg)79cf7f2e2dSJohn Marino objfiles_pspace_data_cleanup (struct program_space *pspace, void *arg)
80cf7f2e2dSJohn Marino {
81cf7f2e2dSJohn Marino struct objfile_pspace_info *info;
82cf7f2e2dSJohn Marino
83cf7f2e2dSJohn Marino info = program_space_data (pspace, objfiles_pspace_data);
84cf7f2e2dSJohn Marino if (info != NULL)
85cf7f2e2dSJohn Marino {
86cf7f2e2dSJohn Marino xfree (info->sections);
87cf7f2e2dSJohn Marino xfree (info);
88cf7f2e2dSJohn Marino }
89cf7f2e2dSJohn Marino }
90cf7f2e2dSJohn Marino
91cf7f2e2dSJohn Marino /* Get the current svr4 data. If none is found yet, add it now. This
92cf7f2e2dSJohn Marino function always returns a valid object. */
93cf7f2e2dSJohn Marino
94cf7f2e2dSJohn Marino static struct objfile_pspace_info *
get_objfile_pspace_data(struct program_space * pspace)95cf7f2e2dSJohn Marino get_objfile_pspace_data (struct program_space *pspace)
96cf7f2e2dSJohn Marino {
97cf7f2e2dSJohn Marino struct objfile_pspace_info *info;
98cf7f2e2dSJohn Marino
99cf7f2e2dSJohn Marino info = program_space_data (pspace, objfiles_pspace_data);
100cf7f2e2dSJohn Marino if (info == NULL)
101cf7f2e2dSJohn Marino {
102cf7f2e2dSJohn Marino info = XZALLOC (struct objfile_pspace_info);
103cf7f2e2dSJohn Marino set_program_space_data (pspace, objfiles_pspace_data, info);
104cf7f2e2dSJohn Marino }
105cf7f2e2dSJohn Marino
106cf7f2e2dSJohn Marino return info;
107cf7f2e2dSJohn Marino }
108cf7f2e2dSJohn Marino
109*ef5ccd6cSJohn Marino
1105796c8dcSSimon Schubert
111*ef5ccd6cSJohn Marino /* Per-BFD data key. */
112*ef5ccd6cSJohn Marino
113*ef5ccd6cSJohn Marino static const struct bfd_data *objfiles_bfd_data;
114*ef5ccd6cSJohn Marino
115*ef5ccd6cSJohn Marino /* Create the per-BFD storage object for OBJFILE. If ABFD is not
116*ef5ccd6cSJohn Marino NULL, and it already has a per-BFD storage object, use that.
117*ef5ccd6cSJohn Marino Otherwise, allocate a new per-BFD storage object. If ABFD is not
118*ef5ccd6cSJohn Marino NULL, the object is allocated on the BFD; otherwise it is allocated
119*ef5ccd6cSJohn Marino on OBJFILE's obstack. Note that it is not safe to call this
120*ef5ccd6cSJohn Marino multiple times for a given OBJFILE -- it can only be called when
121*ef5ccd6cSJohn Marino allocating or re-initializing OBJFILE. */
122*ef5ccd6cSJohn Marino
123*ef5ccd6cSJohn Marino static struct objfile_per_bfd_storage *
get_objfile_bfd_data(struct objfile * objfile,struct bfd * abfd)124*ef5ccd6cSJohn Marino get_objfile_bfd_data (struct objfile *objfile, struct bfd *abfd)
125*ef5ccd6cSJohn Marino {
126*ef5ccd6cSJohn Marino struct objfile_per_bfd_storage *storage = NULL;
127*ef5ccd6cSJohn Marino
128*ef5ccd6cSJohn Marino if (abfd != NULL)
129*ef5ccd6cSJohn Marino storage = bfd_data (abfd, objfiles_bfd_data);
130*ef5ccd6cSJohn Marino
131*ef5ccd6cSJohn Marino if (storage == NULL)
132*ef5ccd6cSJohn Marino {
133*ef5ccd6cSJohn Marino if (abfd != NULL)
134*ef5ccd6cSJohn Marino {
135*ef5ccd6cSJohn Marino storage = bfd_zalloc (abfd, sizeof (struct objfile_per_bfd_storage));
136*ef5ccd6cSJohn Marino set_bfd_data (abfd, objfiles_bfd_data, storage);
137*ef5ccd6cSJohn Marino }
138*ef5ccd6cSJohn Marino else
139*ef5ccd6cSJohn Marino storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
140*ef5ccd6cSJohn Marino struct objfile_per_bfd_storage);
141*ef5ccd6cSJohn Marino
142*ef5ccd6cSJohn Marino obstack_init (&storage->storage_obstack);
143*ef5ccd6cSJohn Marino storage->filename_cache = bcache_xmalloc (NULL, NULL);
144*ef5ccd6cSJohn Marino storage->macro_cache = bcache_xmalloc (NULL, NULL);
145*ef5ccd6cSJohn Marino }
146*ef5ccd6cSJohn Marino
147*ef5ccd6cSJohn Marino return storage;
148*ef5ccd6cSJohn Marino }
149*ef5ccd6cSJohn Marino
150*ef5ccd6cSJohn Marino /* Free STORAGE. */
151*ef5ccd6cSJohn Marino
152*ef5ccd6cSJohn Marino static void
free_objfile_per_bfd_storage(struct objfile_per_bfd_storage * storage)153*ef5ccd6cSJohn Marino free_objfile_per_bfd_storage (struct objfile_per_bfd_storage *storage)
154*ef5ccd6cSJohn Marino {
155*ef5ccd6cSJohn Marino bcache_xfree (storage->filename_cache);
156*ef5ccd6cSJohn Marino bcache_xfree (storage->macro_cache);
157*ef5ccd6cSJohn Marino obstack_free (&storage->storage_obstack, 0);
158*ef5ccd6cSJohn Marino }
159*ef5ccd6cSJohn Marino
160*ef5ccd6cSJohn Marino /* A wrapper for free_objfile_per_bfd_storage that can be passed as a
161*ef5ccd6cSJohn Marino cleanup function to the BFD registry. */
162*ef5ccd6cSJohn Marino
163*ef5ccd6cSJohn Marino static void
objfile_bfd_data_free(struct bfd * unused,void * d)164*ef5ccd6cSJohn Marino objfile_bfd_data_free (struct bfd *unused, void *d)
165*ef5ccd6cSJohn Marino {
166*ef5ccd6cSJohn Marino free_objfile_per_bfd_storage (d);
167*ef5ccd6cSJohn Marino }
168*ef5ccd6cSJohn Marino
169*ef5ccd6cSJohn Marino /* See objfiles.h. */
170*ef5ccd6cSJohn Marino
171*ef5ccd6cSJohn Marino void
set_objfile_per_bfd(struct objfile * objfile)172*ef5ccd6cSJohn Marino set_objfile_per_bfd (struct objfile *objfile)
173*ef5ccd6cSJohn Marino {
174*ef5ccd6cSJohn Marino objfile->per_bfd = get_objfile_bfd_data (objfile, objfile->obfd);
175*ef5ccd6cSJohn Marino }
176*ef5ccd6cSJohn Marino
177*ef5ccd6cSJohn Marino
1785796c8dcSSimon Schubert
1795796c8dcSSimon Schubert /* Called via bfd_map_over_sections to build up the section table that
1805796c8dcSSimon Schubert the objfile references. The objfile contains pointers to the start
1815796c8dcSSimon Schubert of the table (objfile->sections) and to the first location after
1825796c8dcSSimon Schubert the end of the table (objfile->sections_end). */
1835796c8dcSSimon Schubert
1845796c8dcSSimon Schubert static void
add_to_objfile_sections(struct bfd * abfd,struct bfd_section * asect,void * objfilep)1855796c8dcSSimon Schubert add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
186*ef5ccd6cSJohn Marino void *objfilep)
1875796c8dcSSimon Schubert {
188*ef5ccd6cSJohn Marino struct objfile *objfile = (struct objfile *) objfilep;
1895796c8dcSSimon Schubert struct obj_section section;
1905796c8dcSSimon Schubert flagword aflag;
1915796c8dcSSimon Schubert
1925796c8dcSSimon Schubert aflag = bfd_get_section_flags (abfd, asect);
1935796c8dcSSimon Schubert if (!(aflag & SEC_ALLOC))
1945796c8dcSSimon Schubert return;
195*ef5ccd6cSJohn Marino if (bfd_section_size (abfd, asect) == 0)
1965796c8dcSSimon Schubert return;
197*ef5ccd6cSJohn Marino
1985796c8dcSSimon Schubert section.objfile = objfile;
1995796c8dcSSimon Schubert section.the_bfd_section = asect;
2005796c8dcSSimon Schubert section.ovly_mapped = 0;
201c50c785cSJohn Marino obstack_grow (&objfile->objfile_obstack,
202c50c785cSJohn Marino (char *) §ion, sizeof (section));
2035796c8dcSSimon Schubert objfile->sections_end
2045796c8dcSSimon Schubert = (struct obj_section *) (((size_t) objfile->sections_end) + 1);
2055796c8dcSSimon Schubert }
2065796c8dcSSimon Schubert
2075796c8dcSSimon Schubert /* Builds a section table for OBJFILE.
2085796c8dcSSimon Schubert
2095796c8dcSSimon Schubert Note that while we are building the table, which goes into the
210*ef5ccd6cSJohn Marino objfile obstack, we hijack the sections_end pointer to instead hold
2115796c8dcSSimon Schubert a count of the number of sections. When bfd_map_over_sections
2125796c8dcSSimon Schubert returns, this count is used to compute the pointer to the end of
2135796c8dcSSimon Schubert the sections table, which then overwrites the count.
2145796c8dcSSimon Schubert
2155796c8dcSSimon Schubert Also note that the OFFSET and OVLY_MAPPED in each table entry
2165796c8dcSSimon Schubert are initialized to zero.
2175796c8dcSSimon Schubert
218*ef5ccd6cSJohn Marino Also note that if anything else writes to the objfile obstack while
2195796c8dcSSimon Schubert we are building the table, we're pretty much hosed. */
2205796c8dcSSimon Schubert
221*ef5ccd6cSJohn Marino void
build_objfile_section_table(struct objfile * objfile)2225796c8dcSSimon Schubert build_objfile_section_table (struct objfile *objfile)
2235796c8dcSSimon Schubert {
2245796c8dcSSimon Schubert objfile->sections_end = 0;
2255796c8dcSSimon Schubert bfd_map_over_sections (objfile->obfd,
2265796c8dcSSimon Schubert add_to_objfile_sections, (void *) objfile);
2275796c8dcSSimon Schubert objfile->sections = obstack_finish (&objfile->objfile_obstack);
2285796c8dcSSimon Schubert objfile->sections_end = objfile->sections + (size_t) objfile->sections_end;
2295796c8dcSSimon Schubert }
2305796c8dcSSimon Schubert
2315796c8dcSSimon Schubert /* Given a pointer to an initialized bfd (ABFD) and some flag bits
2325796c8dcSSimon Schubert allocate a new objfile struct, fill it in as best we can, link it
2335796c8dcSSimon Schubert into the list of all known objfiles, and return a pointer to the
2345796c8dcSSimon Schubert new objfile struct.
2355796c8dcSSimon Schubert
2365796c8dcSSimon Schubert The FLAGS word contains various bits (OBJF_*) that can be taken as
2375796c8dcSSimon Schubert requests for specific operations. Other bits like OBJF_SHARED are
2385796c8dcSSimon Schubert simply copied through to the new objfile flags member. */
2395796c8dcSSimon Schubert
2405796c8dcSSimon Schubert /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0
2415796c8dcSSimon Schubert by jv-lang.c, to create an artificial objfile used to hold
2425796c8dcSSimon Schubert information about dynamically-loaded Java classes. Unfortunately,
2435796c8dcSSimon Schubert that branch of this function doesn't get tested very frequently, so
2445796c8dcSSimon Schubert it's prone to breakage. (E.g. at one time the name was set to NULL
2455796c8dcSSimon Schubert in that situation, which broke a loop over all names in the dynamic
2465796c8dcSSimon Schubert library loader.) If you change this function, please try to leave
2475796c8dcSSimon Schubert things in a consistent state even if abfd is NULL. */
2485796c8dcSSimon Schubert
2495796c8dcSSimon Schubert struct objfile *
allocate_objfile(bfd * abfd,int flags)2505796c8dcSSimon Schubert allocate_objfile (bfd *abfd, int flags)
2515796c8dcSSimon Schubert {
252cf7f2e2dSJohn Marino struct objfile *objfile;
2535796c8dcSSimon Schubert
254cf7f2e2dSJohn Marino objfile = (struct objfile *) xzalloc (sizeof (struct objfile));
255c50c785cSJohn Marino objfile->psymbol_cache = psymbol_bcache_init ();
2565796c8dcSSimon Schubert /* We could use obstack_specify_allocation here instead, but
2575796c8dcSSimon Schubert gdb_obstack.h specifies the alloc/dealloc functions. */
2585796c8dcSSimon Schubert obstack_init (&objfile->objfile_obstack);
2595796c8dcSSimon Schubert terminate_minimal_symbol_table (objfile);
2605796c8dcSSimon Schubert
2615796c8dcSSimon Schubert objfile_alloc_data (objfile);
2625796c8dcSSimon Schubert
2635796c8dcSSimon Schubert /* Update the per-objfile information that comes from the bfd, ensuring
2645796c8dcSSimon Schubert that any data that is reference is saved in the per-objfile data
2655796c8dcSSimon Schubert region. */
2665796c8dcSSimon Schubert
267*ef5ccd6cSJohn Marino objfile->obfd = abfd;
268*ef5ccd6cSJohn Marino gdb_bfd_ref (abfd);
2695796c8dcSSimon Schubert if (abfd != NULL)
2705796c8dcSSimon Schubert {
2715796c8dcSSimon Schubert /* Look up the gdbarch associated with the BFD. */
2725796c8dcSSimon Schubert objfile->gdbarch = gdbarch_from_bfd (abfd);
2735796c8dcSSimon Schubert
274*ef5ccd6cSJohn Marino objfile->name = bfd_get_filename (abfd);
2755796c8dcSSimon Schubert objfile->mtime = bfd_get_mtime (abfd);
2765796c8dcSSimon Schubert
2775796c8dcSSimon Schubert /* Build section table. */
278*ef5ccd6cSJohn Marino build_objfile_section_table (objfile);
2795796c8dcSSimon Schubert }
2805796c8dcSSimon Schubert else
2815796c8dcSSimon Schubert {
282*ef5ccd6cSJohn Marino objfile->name = "<<anonymous objfile>>";
2835796c8dcSSimon Schubert }
2845796c8dcSSimon Schubert
285*ef5ccd6cSJohn Marino objfile->per_bfd = get_objfile_bfd_data (objfile, abfd);
286cf7f2e2dSJohn Marino objfile->pspace = current_program_space;
287cf7f2e2dSJohn Marino
2885796c8dcSSimon Schubert /* Initialize the section indexes for this objfile, so that we can
2895796c8dcSSimon Schubert later detect if they are used w/o being properly assigned to. */
2905796c8dcSSimon Schubert
2915796c8dcSSimon Schubert objfile->sect_index_text = -1;
2925796c8dcSSimon Schubert objfile->sect_index_data = -1;
2935796c8dcSSimon Schubert objfile->sect_index_bss = -1;
2945796c8dcSSimon Schubert objfile->sect_index_rodata = -1;
2955796c8dcSSimon Schubert
2965796c8dcSSimon Schubert /* Add this file onto the tail of the linked list of other such files. */
2975796c8dcSSimon Schubert
2985796c8dcSSimon Schubert objfile->next = NULL;
2995796c8dcSSimon Schubert if (object_files == NULL)
3005796c8dcSSimon Schubert object_files = objfile;
3015796c8dcSSimon Schubert else
3025796c8dcSSimon Schubert {
303cf7f2e2dSJohn Marino struct objfile *last_one;
304cf7f2e2dSJohn Marino
3055796c8dcSSimon Schubert for (last_one = object_files;
3065796c8dcSSimon Schubert last_one->next;
3075796c8dcSSimon Schubert last_one = last_one->next);
3085796c8dcSSimon Schubert last_one->next = objfile;
3095796c8dcSSimon Schubert }
3105796c8dcSSimon Schubert
3115796c8dcSSimon Schubert /* Save passed in flag bits. */
3125796c8dcSSimon Schubert objfile->flags |= flags;
3135796c8dcSSimon Schubert
314cf7f2e2dSJohn Marino /* Rebuild section map next time we need it. */
315cf7f2e2dSJohn Marino get_objfile_pspace_data (objfile->pspace)->objfiles_changed_p = 1;
3165796c8dcSSimon Schubert
317cf7f2e2dSJohn Marino return objfile;
3185796c8dcSSimon Schubert }
3195796c8dcSSimon Schubert
3205796c8dcSSimon Schubert /* Retrieve the gdbarch associated with OBJFILE. */
3215796c8dcSSimon Schubert struct gdbarch *
get_objfile_arch(struct objfile * objfile)3225796c8dcSSimon Schubert get_objfile_arch (struct objfile *objfile)
3235796c8dcSSimon Schubert {
3245796c8dcSSimon Schubert return objfile->gdbarch;
3255796c8dcSSimon Schubert }
3265796c8dcSSimon Schubert
327cf7f2e2dSJohn Marino /* If there is a valid and known entry point, function fills *ENTRY_P with it
328cf7f2e2dSJohn Marino and returns non-zero; otherwise it returns zero. */
3295796c8dcSSimon Schubert
330cf7f2e2dSJohn Marino int
entry_point_address_query(CORE_ADDR * entry_p)331cf7f2e2dSJohn Marino entry_point_address_query (CORE_ADDR *entry_p)
3325796c8dcSSimon Schubert {
333cf7f2e2dSJohn Marino if (symfile_objfile == NULL || !symfile_objfile->ei.entry_point_p)
3345796c8dcSSimon Schubert return 0;
3355796c8dcSSimon Schubert
336*ef5ccd6cSJohn Marino *entry_p = symfile_objfile->ei.entry_point;
3375796c8dcSSimon Schubert
338cf7f2e2dSJohn Marino return 1;
339cf7f2e2dSJohn Marino }
340cf7f2e2dSJohn Marino
341cf7f2e2dSJohn Marino /* Get current entry point address. Call error if it is not known. */
342cf7f2e2dSJohn Marino
343cf7f2e2dSJohn Marino CORE_ADDR
entry_point_address(void)344cf7f2e2dSJohn Marino entry_point_address (void)
345cf7f2e2dSJohn Marino {
346cf7f2e2dSJohn Marino CORE_ADDR retval;
347cf7f2e2dSJohn Marino
348cf7f2e2dSJohn Marino if (!entry_point_address_query (&retval))
349cf7f2e2dSJohn Marino error (_("Entry point address is not known."));
350cf7f2e2dSJohn Marino
351cf7f2e2dSJohn Marino return retval;
3525796c8dcSSimon Schubert }
3535796c8dcSSimon Schubert
354cf7f2e2dSJohn Marino /* Iterator on PARENT and every separate debug objfile of PARENT.
355cf7f2e2dSJohn Marino The usage pattern is:
356cf7f2e2dSJohn Marino for (objfile = parent;
357cf7f2e2dSJohn Marino objfile;
358cf7f2e2dSJohn Marino objfile = objfile_separate_debug_iterate (parent, objfile))
359cf7f2e2dSJohn Marino ...
360cf7f2e2dSJohn Marino */
361cf7f2e2dSJohn Marino
362cf7f2e2dSJohn Marino struct objfile *
objfile_separate_debug_iterate(const struct objfile * parent,const struct objfile * objfile)363cf7f2e2dSJohn Marino objfile_separate_debug_iterate (const struct objfile *parent,
364cf7f2e2dSJohn Marino const struct objfile *objfile)
365cf7f2e2dSJohn Marino {
366cf7f2e2dSJohn Marino struct objfile *res;
367cf7f2e2dSJohn Marino
368cf7f2e2dSJohn Marino /* If any, return the first child. */
369cf7f2e2dSJohn Marino res = objfile->separate_debug_objfile;
370cf7f2e2dSJohn Marino if (res)
371cf7f2e2dSJohn Marino return res;
372cf7f2e2dSJohn Marino
373cf7f2e2dSJohn Marino /* Common case where there is no separate debug objfile. */
374cf7f2e2dSJohn Marino if (objfile == parent)
375cf7f2e2dSJohn Marino return NULL;
376cf7f2e2dSJohn Marino
377cf7f2e2dSJohn Marino /* Return the brother if any. Note that we don't iterate on brothers of
378cf7f2e2dSJohn Marino the parents. */
379cf7f2e2dSJohn Marino res = objfile->separate_debug_objfile_link;
380cf7f2e2dSJohn Marino if (res)
381cf7f2e2dSJohn Marino return res;
382cf7f2e2dSJohn Marino
383cf7f2e2dSJohn Marino for (res = objfile->separate_debug_objfile_backlink;
384cf7f2e2dSJohn Marino res != parent;
385cf7f2e2dSJohn Marino res = res->separate_debug_objfile_backlink)
386cf7f2e2dSJohn Marino {
387cf7f2e2dSJohn Marino gdb_assert (res != NULL);
388cf7f2e2dSJohn Marino if (res->separate_debug_objfile_link)
389cf7f2e2dSJohn Marino return res->separate_debug_objfile_link;
390cf7f2e2dSJohn Marino }
391cf7f2e2dSJohn Marino return NULL;
392cf7f2e2dSJohn Marino }
3935796c8dcSSimon Schubert
3945796c8dcSSimon Schubert /* Put one object file before a specified on in the global list.
3955796c8dcSSimon Schubert This can be used to make sure an object file is destroyed before
3965796c8dcSSimon Schubert another when using ALL_OBJFILES_SAFE to free all objfiles. */
3975796c8dcSSimon Schubert void
put_objfile_before(struct objfile * objfile,struct objfile * before_this)3985796c8dcSSimon Schubert put_objfile_before (struct objfile *objfile, struct objfile *before_this)
3995796c8dcSSimon Schubert {
4005796c8dcSSimon Schubert struct objfile **objp;
4015796c8dcSSimon Schubert
4025796c8dcSSimon Schubert unlink_objfile (objfile);
4035796c8dcSSimon Schubert
4045796c8dcSSimon Schubert for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
4055796c8dcSSimon Schubert {
4065796c8dcSSimon Schubert if (*objp == before_this)
4075796c8dcSSimon Schubert {
4085796c8dcSSimon Schubert objfile->next = *objp;
4095796c8dcSSimon Schubert *objp = objfile;
4105796c8dcSSimon Schubert return;
4115796c8dcSSimon Schubert }
4125796c8dcSSimon Schubert }
4135796c8dcSSimon Schubert
4145796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
4155796c8dcSSimon Schubert _("put_objfile_before: before objfile not in list"));
4165796c8dcSSimon Schubert }
4175796c8dcSSimon Schubert
4185796c8dcSSimon Schubert /* Put OBJFILE at the front of the list. */
4195796c8dcSSimon Schubert
4205796c8dcSSimon Schubert void
objfile_to_front(struct objfile * objfile)4215796c8dcSSimon Schubert objfile_to_front (struct objfile *objfile)
4225796c8dcSSimon Schubert {
4235796c8dcSSimon Schubert struct objfile **objp;
4245796c8dcSSimon Schubert for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
4255796c8dcSSimon Schubert {
4265796c8dcSSimon Schubert if (*objp == objfile)
4275796c8dcSSimon Schubert {
4285796c8dcSSimon Schubert /* Unhook it from where it is. */
4295796c8dcSSimon Schubert *objp = objfile->next;
4305796c8dcSSimon Schubert /* Put it in the front. */
4315796c8dcSSimon Schubert objfile->next = object_files;
4325796c8dcSSimon Schubert object_files = objfile;
4335796c8dcSSimon Schubert break;
4345796c8dcSSimon Schubert }
4355796c8dcSSimon Schubert }
4365796c8dcSSimon Schubert }
4375796c8dcSSimon Schubert
4385796c8dcSSimon Schubert /* Unlink OBJFILE from the list of known objfiles, if it is found in the
4395796c8dcSSimon Schubert list.
4405796c8dcSSimon Schubert
4415796c8dcSSimon Schubert It is not a bug, or error, to call this function if OBJFILE is not known
4425796c8dcSSimon Schubert to be in the current list. This is done in the case of mapped objfiles,
4435796c8dcSSimon Schubert for example, just to ensure that the mapped objfile doesn't appear twice
4445796c8dcSSimon Schubert in the list. Since the list is threaded, linking in a mapped objfile
4455796c8dcSSimon Schubert twice would create a circular list.
4465796c8dcSSimon Schubert
4475796c8dcSSimon Schubert If OBJFILE turns out to be in the list, we zap it's NEXT pointer after
4485796c8dcSSimon Schubert unlinking it, just to ensure that we have completely severed any linkages
4495796c8dcSSimon Schubert between the OBJFILE and the list. */
4505796c8dcSSimon Schubert
4515796c8dcSSimon Schubert void
unlink_objfile(struct objfile * objfile)4525796c8dcSSimon Schubert unlink_objfile (struct objfile *objfile)
4535796c8dcSSimon Schubert {
4545796c8dcSSimon Schubert struct objfile **objpp;
4555796c8dcSSimon Schubert
4565796c8dcSSimon Schubert for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next))
4575796c8dcSSimon Schubert {
4585796c8dcSSimon Schubert if (*objpp == objfile)
4595796c8dcSSimon Schubert {
4605796c8dcSSimon Schubert *objpp = (*objpp)->next;
4615796c8dcSSimon Schubert objfile->next = NULL;
4625796c8dcSSimon Schubert return;
4635796c8dcSSimon Schubert }
4645796c8dcSSimon Schubert }
4655796c8dcSSimon Schubert
4665796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
4675796c8dcSSimon Schubert _("unlink_objfile: objfile already unlinked"));
4685796c8dcSSimon Schubert }
4695796c8dcSSimon Schubert
470cf7f2e2dSJohn Marino /* Add OBJFILE as a separate debug objfile of PARENT. */
471cf7f2e2dSJohn Marino
472cf7f2e2dSJohn Marino void
add_separate_debug_objfile(struct objfile * objfile,struct objfile * parent)473cf7f2e2dSJohn Marino add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
474cf7f2e2dSJohn Marino {
475cf7f2e2dSJohn Marino gdb_assert (objfile && parent);
476cf7f2e2dSJohn Marino
477cf7f2e2dSJohn Marino /* Must not be already in a list. */
478cf7f2e2dSJohn Marino gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
479cf7f2e2dSJohn Marino gdb_assert (objfile->separate_debug_objfile_link == NULL);
480*ef5ccd6cSJohn Marino gdb_assert (objfile->separate_debug_objfile == NULL);
481*ef5ccd6cSJohn Marino gdb_assert (parent->separate_debug_objfile_backlink == NULL);
482*ef5ccd6cSJohn Marino gdb_assert (parent->separate_debug_objfile_link == NULL);
483cf7f2e2dSJohn Marino
484cf7f2e2dSJohn Marino objfile->separate_debug_objfile_backlink = parent;
485cf7f2e2dSJohn Marino objfile->separate_debug_objfile_link = parent->separate_debug_objfile;
486cf7f2e2dSJohn Marino parent->separate_debug_objfile = objfile;
487cf7f2e2dSJohn Marino
488cf7f2e2dSJohn Marino /* Put the separate debug object before the normal one, this is so that
489cf7f2e2dSJohn Marino usage of the ALL_OBJFILES_SAFE macro will stay safe. */
490cf7f2e2dSJohn Marino put_objfile_before (objfile, parent);
491cf7f2e2dSJohn Marino }
492cf7f2e2dSJohn Marino
493cf7f2e2dSJohn Marino /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
494cf7f2e2dSJohn Marino itself. */
495cf7f2e2dSJohn Marino
496cf7f2e2dSJohn Marino void
free_objfile_separate_debug(struct objfile * objfile)497cf7f2e2dSJohn Marino free_objfile_separate_debug (struct objfile *objfile)
498cf7f2e2dSJohn Marino {
499cf7f2e2dSJohn Marino struct objfile *child;
500cf7f2e2dSJohn Marino
501cf7f2e2dSJohn Marino for (child = objfile->separate_debug_objfile; child;)
502cf7f2e2dSJohn Marino {
503cf7f2e2dSJohn Marino struct objfile *next_child = child->separate_debug_objfile_link;
504cf7f2e2dSJohn Marino free_objfile (child);
505cf7f2e2dSJohn Marino child = next_child;
506cf7f2e2dSJohn Marino }
507cf7f2e2dSJohn Marino }
5085796c8dcSSimon Schubert
5095796c8dcSSimon Schubert /* Destroy an objfile and all the symtabs and psymtabs under it. Note
5105796c8dcSSimon Schubert that as much as possible is allocated on the objfile_obstack
5115796c8dcSSimon Schubert so that the memory can be efficiently freed.
5125796c8dcSSimon Schubert
5135796c8dcSSimon Schubert Things which we do NOT free because they are not in malloc'd memory
5145796c8dcSSimon Schubert or not in memory specific to the objfile include:
5155796c8dcSSimon Schubert
5165796c8dcSSimon Schubert objfile -> sf
5175796c8dcSSimon Schubert
5185796c8dcSSimon Schubert FIXME: If the objfile is using reusable symbol information (via mmalloc),
5195796c8dcSSimon Schubert then we need to take into account the fact that more than one process
5205796c8dcSSimon Schubert may be using the symbol information at the same time (when mmalloc is
5215796c8dcSSimon Schubert extended to support cooperative locking). When more than one process
5225796c8dcSSimon Schubert is using the mapped symbol info, we need to be more careful about when
5235796c8dcSSimon Schubert we free objects in the reusable area. */
5245796c8dcSSimon Schubert
5255796c8dcSSimon Schubert void
free_objfile(struct objfile * objfile)5265796c8dcSSimon Schubert free_objfile (struct objfile *objfile)
5275796c8dcSSimon Schubert {
528cf7f2e2dSJohn Marino /* Free all separate debug objfiles. */
529cf7f2e2dSJohn Marino free_objfile_separate_debug (objfile);
5305796c8dcSSimon Schubert
5315796c8dcSSimon Schubert if (objfile->separate_debug_objfile_backlink)
5325796c8dcSSimon Schubert {
5335796c8dcSSimon Schubert /* We freed the separate debug file, make sure the base objfile
5345796c8dcSSimon Schubert doesn't reference it. */
535cf7f2e2dSJohn Marino struct objfile *child;
536cf7f2e2dSJohn Marino
537cf7f2e2dSJohn Marino child = objfile->separate_debug_objfile_backlink->separate_debug_objfile;
538cf7f2e2dSJohn Marino
539cf7f2e2dSJohn Marino if (child == objfile)
540cf7f2e2dSJohn Marino {
541cf7f2e2dSJohn Marino /* OBJFILE is the first child. */
542cf7f2e2dSJohn Marino objfile->separate_debug_objfile_backlink->separate_debug_objfile =
543cf7f2e2dSJohn Marino objfile->separate_debug_objfile_link;
544cf7f2e2dSJohn Marino }
545cf7f2e2dSJohn Marino else
546cf7f2e2dSJohn Marino {
547cf7f2e2dSJohn Marino /* Find OBJFILE in the list. */
548cf7f2e2dSJohn Marino while (1)
549cf7f2e2dSJohn Marino {
550cf7f2e2dSJohn Marino if (child->separate_debug_objfile_link == objfile)
551cf7f2e2dSJohn Marino {
552cf7f2e2dSJohn Marino child->separate_debug_objfile_link =
553cf7f2e2dSJohn Marino objfile->separate_debug_objfile_link;
554cf7f2e2dSJohn Marino break;
555cf7f2e2dSJohn Marino }
556cf7f2e2dSJohn Marino child = child->separate_debug_objfile_link;
557cf7f2e2dSJohn Marino gdb_assert (child);
558cf7f2e2dSJohn Marino }
559cf7f2e2dSJohn Marino }
5605796c8dcSSimon Schubert }
5615796c8dcSSimon Schubert
5625796c8dcSSimon Schubert /* Remove any references to this objfile in the global value
5635796c8dcSSimon Schubert lists. */
5645796c8dcSSimon Schubert preserve_values (objfile);
5655796c8dcSSimon Schubert
566a45ae5f8SJohn Marino /* It still may reference data modules have associated with the objfile and
567a45ae5f8SJohn Marino the symbol file data. */
568a45ae5f8SJohn Marino forget_cached_source_info_for_objfile (objfile);
569a45ae5f8SJohn Marino
570*ef5ccd6cSJohn Marino breakpoint_free_objfile (objfile);
571*ef5ccd6cSJohn Marino btrace_free_objfile (objfile);
572*ef5ccd6cSJohn Marino
5735796c8dcSSimon Schubert /* First do any symbol file specific actions required when we are
5745796c8dcSSimon Schubert finished with a particular symbol file. Note that if the objfile
5755796c8dcSSimon Schubert is using reusable symbol information (via mmalloc) then each of
5765796c8dcSSimon Schubert these routines is responsible for doing the correct thing, either
5775796c8dcSSimon Schubert freeing things which are valid only during this particular gdb
5785796c8dcSSimon Schubert execution, or leaving them to be reused during the next one. */
5795796c8dcSSimon Schubert
5805796c8dcSSimon Schubert if (objfile->sf != NULL)
5815796c8dcSSimon Schubert {
5825796c8dcSSimon Schubert (*objfile->sf->sym_finish) (objfile);
5835796c8dcSSimon Schubert }
5845796c8dcSSimon Schubert
585a45ae5f8SJohn Marino /* Discard any data modules have associated with the objfile. The function
586a45ae5f8SJohn Marino still may reference objfile->obfd. */
5875796c8dcSSimon Schubert objfile_free_data (objfile);
5885796c8dcSSimon Schubert
589*ef5ccd6cSJohn Marino if (objfile->obfd)
5905796c8dcSSimon Schubert gdb_bfd_unref (objfile->obfd);
591*ef5ccd6cSJohn Marino else
592*ef5ccd6cSJohn Marino free_objfile_per_bfd_storage (objfile->per_bfd);
5935796c8dcSSimon Schubert
5945796c8dcSSimon Schubert /* Remove it from the chain of all objfiles. */
5955796c8dcSSimon Schubert
5965796c8dcSSimon Schubert unlink_objfile (objfile);
5975796c8dcSSimon Schubert
5985796c8dcSSimon Schubert if (objfile == symfile_objfile)
5995796c8dcSSimon Schubert symfile_objfile = NULL;
6005796c8dcSSimon Schubert
6015796c8dcSSimon Schubert if (objfile == rt_common_objfile)
6025796c8dcSSimon Schubert rt_common_objfile = NULL;
6035796c8dcSSimon Schubert
6045796c8dcSSimon Schubert /* Before the symbol table code was redone to make it easier to
6055796c8dcSSimon Schubert selectively load and remove information particular to a specific
6065796c8dcSSimon Schubert linkage unit, gdb used to do these things whenever the monolithic
6075796c8dcSSimon Schubert symbol table was blown away. How much still needs to be done
6085796c8dcSSimon Schubert is unknown, but we play it safe for now and keep each action until
6095796c8dcSSimon Schubert it is shown to be no longer needed. */
6105796c8dcSSimon Schubert
6115796c8dcSSimon Schubert /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
6125796c8dcSSimon Schubert for example), so we need to call this here. */
6135796c8dcSSimon Schubert clear_pc_function_cache ();
6145796c8dcSSimon Schubert
6155796c8dcSSimon Schubert /* Clear globals which might have pointed into a removed objfile.
6165796c8dcSSimon Schubert FIXME: It's not clear which of these are supposed to persist
6175796c8dcSSimon Schubert between expressions and which ought to be reset each time. */
6185796c8dcSSimon Schubert expression_context_block = NULL;
6195796c8dcSSimon Schubert innermost_block = NULL;
6205796c8dcSSimon Schubert
6215796c8dcSSimon Schubert /* Check to see if the current_source_symtab belongs to this objfile,
6225796c8dcSSimon Schubert and if so, call clear_current_source_symtab_and_line. */
6235796c8dcSSimon Schubert
6245796c8dcSSimon Schubert {
6255796c8dcSSimon Schubert struct symtab_and_line cursal = get_current_source_symtab_and_line ();
6265796c8dcSSimon Schubert
627a45ae5f8SJohn Marino if (cursal.symtab && cursal.symtab->objfile == objfile)
6285796c8dcSSimon Schubert clear_current_source_symtab_and_line ();
6295796c8dcSSimon Schubert }
6305796c8dcSSimon Schubert
6315796c8dcSSimon Schubert /* The last thing we do is free the objfile struct itself. */
6325796c8dcSSimon Schubert
6335796c8dcSSimon Schubert if (objfile->global_psymbols.list)
6345796c8dcSSimon Schubert xfree (objfile->global_psymbols.list);
6355796c8dcSSimon Schubert if (objfile->static_psymbols.list)
6365796c8dcSSimon Schubert xfree (objfile->static_psymbols.list);
637c50c785cSJohn Marino /* Free the obstacks for non-reusable objfiles. */
638c50c785cSJohn Marino psymbol_bcache_free (objfile->psymbol_cache);
6395796c8dcSSimon Schubert if (objfile->demangled_names_hash)
6405796c8dcSSimon Schubert htab_delete (objfile->demangled_names_hash);
6415796c8dcSSimon Schubert obstack_free (&objfile->objfile_obstack, 0);
642cf7f2e2dSJohn Marino
643cf7f2e2dSJohn Marino /* Rebuild section map next time we need it. */
644cf7f2e2dSJohn Marino get_objfile_pspace_data (objfile->pspace)->objfiles_changed_p = 1;
645cf7f2e2dSJohn Marino
6465796c8dcSSimon Schubert xfree (objfile);
6475796c8dcSSimon Schubert }
6485796c8dcSSimon Schubert
6495796c8dcSSimon Schubert static void
do_free_objfile_cleanup(void * obj)6505796c8dcSSimon Schubert do_free_objfile_cleanup (void *obj)
6515796c8dcSSimon Schubert {
6525796c8dcSSimon Schubert free_objfile (obj);
6535796c8dcSSimon Schubert }
6545796c8dcSSimon Schubert
6555796c8dcSSimon Schubert struct cleanup *
make_cleanup_free_objfile(struct objfile * obj)6565796c8dcSSimon Schubert make_cleanup_free_objfile (struct objfile *obj)
6575796c8dcSSimon Schubert {
6585796c8dcSSimon Schubert return make_cleanup (do_free_objfile_cleanup, obj);
6595796c8dcSSimon Schubert }
6605796c8dcSSimon Schubert
6615796c8dcSSimon Schubert /* Free all the object files at once and clean up their users. */
6625796c8dcSSimon Schubert
6635796c8dcSSimon Schubert void
free_all_objfiles(void)6645796c8dcSSimon Schubert free_all_objfiles (void)
6655796c8dcSSimon Schubert {
6665796c8dcSSimon Schubert struct objfile *objfile, *temp;
667cf7f2e2dSJohn Marino struct so_list *so;
668cf7f2e2dSJohn Marino
669cf7f2e2dSJohn Marino /* Any objfile referencewould become stale. */
670cf7f2e2dSJohn Marino for (so = master_so_list (); so; so = so->next)
671cf7f2e2dSJohn Marino gdb_assert (so->objfile == NULL);
6725796c8dcSSimon Schubert
6735796c8dcSSimon Schubert ALL_OBJFILES_SAFE (objfile, temp)
6745796c8dcSSimon Schubert {
6755796c8dcSSimon Schubert free_objfile (objfile);
6765796c8dcSSimon Schubert }
677c50c785cSJohn Marino clear_symtab_users (0);
6785796c8dcSSimon Schubert }
6795796c8dcSSimon Schubert
680c50c785cSJohn Marino /* A helper function for objfile_relocate1 that relocates a single
681c50c785cSJohn Marino symbol. */
682c50c785cSJohn Marino
683c50c785cSJohn Marino static void
relocate_one_symbol(struct symbol * sym,struct objfile * objfile,struct section_offsets * delta)684c50c785cSJohn Marino relocate_one_symbol (struct symbol *sym, struct objfile *objfile,
685c50c785cSJohn Marino struct section_offsets *delta)
686c50c785cSJohn Marino {
687c50c785cSJohn Marino fixup_symbol_section (sym, objfile);
688c50c785cSJohn Marino
689c50c785cSJohn Marino /* The RS6000 code from which this was taken skipped
690c50c785cSJohn Marino any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
691c50c785cSJohn Marino But I'm leaving out that test, on the theory that
692c50c785cSJohn Marino they can't possibly pass the tests below. */
693c50c785cSJohn Marino if ((SYMBOL_CLASS (sym) == LOC_LABEL
694c50c785cSJohn Marino || SYMBOL_CLASS (sym) == LOC_STATIC)
695c50c785cSJohn Marino && SYMBOL_SECTION (sym) >= 0)
696c50c785cSJohn Marino {
697c50c785cSJohn Marino SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (delta, SYMBOL_SECTION (sym));
698c50c785cSJohn Marino }
699c50c785cSJohn Marino }
700c50c785cSJohn Marino
7015796c8dcSSimon Schubert /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
702cf7f2e2dSJohn Marino entries in new_offsets. SEPARATE_DEBUG_OBJFILE is not touched here.
703cf7f2e2dSJohn Marino Return non-zero iff any change happened. */
704cf7f2e2dSJohn Marino
705cf7f2e2dSJohn Marino static int
objfile_relocate1(struct objfile * objfile,struct section_offsets * new_offsets)706cf7f2e2dSJohn Marino objfile_relocate1 (struct objfile *objfile,
707cf7f2e2dSJohn Marino struct section_offsets *new_offsets)
7085796c8dcSSimon Schubert {
7095796c8dcSSimon Schubert struct obj_section *s;
7105796c8dcSSimon Schubert struct section_offsets *delta =
7115796c8dcSSimon Schubert ((struct section_offsets *)
7125796c8dcSSimon Schubert alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
7135796c8dcSSimon Schubert
7145796c8dcSSimon Schubert int i;
7155796c8dcSSimon Schubert int something_changed = 0;
716cf7f2e2dSJohn Marino
7175796c8dcSSimon Schubert for (i = 0; i < objfile->num_sections; ++i)
7185796c8dcSSimon Schubert {
7195796c8dcSSimon Schubert delta->offsets[i] =
7205796c8dcSSimon Schubert ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i);
7215796c8dcSSimon Schubert if (ANOFFSET (delta, i) != 0)
7225796c8dcSSimon Schubert something_changed = 1;
7235796c8dcSSimon Schubert }
7245796c8dcSSimon Schubert if (!something_changed)
725cf7f2e2dSJohn Marino return 0;
7265796c8dcSSimon Schubert
7275796c8dcSSimon Schubert /* OK, get all the symtabs. */
7285796c8dcSSimon Schubert {
7295796c8dcSSimon Schubert struct symtab *s;
7305796c8dcSSimon Schubert
7315796c8dcSSimon Schubert ALL_OBJFILE_SYMTABS (objfile, s)
7325796c8dcSSimon Schubert {
7335796c8dcSSimon Schubert struct linetable *l;
7345796c8dcSSimon Schubert struct blockvector *bv;
7355796c8dcSSimon Schubert int i;
7365796c8dcSSimon Schubert
7375796c8dcSSimon Schubert /* First the line table. */
7385796c8dcSSimon Schubert l = LINETABLE (s);
7395796c8dcSSimon Schubert if (l)
7405796c8dcSSimon Schubert {
7415796c8dcSSimon Schubert for (i = 0; i < l->nitems; ++i)
7425796c8dcSSimon Schubert l->item[i].pc += ANOFFSET (delta, s->block_line_section);
7435796c8dcSSimon Schubert }
7445796c8dcSSimon Schubert
7455796c8dcSSimon Schubert /* Don't relocate a shared blockvector more than once. */
7465796c8dcSSimon Schubert if (!s->primary)
7475796c8dcSSimon Schubert continue;
7485796c8dcSSimon Schubert
7495796c8dcSSimon Schubert bv = BLOCKVECTOR (s);
7505796c8dcSSimon Schubert if (BLOCKVECTOR_MAP (bv))
7515796c8dcSSimon Schubert addrmap_relocate (BLOCKVECTOR_MAP (bv),
7525796c8dcSSimon Schubert ANOFFSET (delta, s->block_line_section));
7535796c8dcSSimon Schubert
7545796c8dcSSimon Schubert for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
7555796c8dcSSimon Schubert {
7565796c8dcSSimon Schubert struct block *b;
7575796c8dcSSimon Schubert struct symbol *sym;
7585796c8dcSSimon Schubert struct dict_iterator iter;
7595796c8dcSSimon Schubert
7605796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (bv, i);
7615796c8dcSSimon Schubert BLOCK_START (b) += ANOFFSET (delta, s->block_line_section);
7625796c8dcSSimon Schubert BLOCK_END (b) += ANOFFSET (delta, s->block_line_section);
7635796c8dcSSimon Schubert
764*ef5ccd6cSJohn Marino /* We only want to iterate over the local symbols, not any
765*ef5ccd6cSJohn Marino symbols in included symtabs. */
766*ef5ccd6cSJohn Marino ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
7675796c8dcSSimon Schubert {
768c50c785cSJohn Marino relocate_one_symbol (sym, objfile, delta);
769c50c785cSJohn Marino }
770c50c785cSJohn Marino }
771c50c785cSJohn Marino }
772c50c785cSJohn Marino }
7735796c8dcSSimon Schubert
774c50c785cSJohn Marino /* Relocate isolated symbols. */
7755796c8dcSSimon Schubert {
776c50c785cSJohn Marino struct symbol *iter;
777c50c785cSJohn Marino
778c50c785cSJohn Marino for (iter = objfile->template_symbols; iter; iter = iter->hash_next)
779c50c785cSJohn Marino relocate_one_symbol (iter, objfile, delta);
7805796c8dcSSimon Schubert }
7815796c8dcSSimon Schubert
782cf7f2e2dSJohn Marino if (objfile->psymtabs_addrmap)
783cf7f2e2dSJohn Marino addrmap_relocate (objfile->psymtabs_addrmap,
784cf7f2e2dSJohn Marino ANOFFSET (delta, SECT_OFF_TEXT (objfile)));
7855796c8dcSSimon Schubert
786cf7f2e2dSJohn Marino if (objfile->sf)
787cf7f2e2dSJohn Marino objfile->sf->qf->relocate (objfile, new_offsets, delta);
7885796c8dcSSimon Schubert
7895796c8dcSSimon Schubert {
7905796c8dcSSimon Schubert struct minimal_symbol *msym;
791cf7f2e2dSJohn Marino
7925796c8dcSSimon Schubert ALL_OBJFILE_MSYMBOLS (objfile, msym)
7935796c8dcSSimon Schubert if (SYMBOL_SECTION (msym) >= 0)
7945796c8dcSSimon Schubert SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym));
7955796c8dcSSimon Schubert }
7965796c8dcSSimon Schubert /* Relocating different sections by different amounts may cause the symbols
7975796c8dcSSimon Schubert to be out of order. */
7985796c8dcSSimon Schubert msymbols_sort (objfile);
7995796c8dcSSimon Schubert
800cf7f2e2dSJohn Marino if (objfile->ei.entry_point_p)
8015796c8dcSSimon Schubert {
8025796c8dcSSimon Schubert /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT
8035796c8dcSSimon Schubert only as a fallback. */
8045796c8dcSSimon Schubert struct obj_section *s;
8055796c8dcSSimon Schubert s = find_pc_section (objfile->ei.entry_point);
8065796c8dcSSimon Schubert if (s)
8075796c8dcSSimon Schubert objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index);
8085796c8dcSSimon Schubert else
8095796c8dcSSimon Schubert objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
8105796c8dcSSimon Schubert }
8115796c8dcSSimon Schubert
8125796c8dcSSimon Schubert {
8135796c8dcSSimon Schubert int i;
814cf7f2e2dSJohn Marino
8155796c8dcSSimon Schubert for (i = 0; i < objfile->num_sections; ++i)
8165796c8dcSSimon Schubert (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i);
8175796c8dcSSimon Schubert }
8185796c8dcSSimon Schubert
8195796c8dcSSimon Schubert /* Rebuild section map next time we need it. */
820cf7f2e2dSJohn Marino get_objfile_pspace_data (objfile->pspace)->objfiles_changed_p = 1;
8215796c8dcSSimon Schubert
8225796c8dcSSimon Schubert /* Update the table in exec_ops, used to read memory. */
8235796c8dcSSimon Schubert ALL_OBJFILE_OSECTIONS (objfile, s)
8245796c8dcSSimon Schubert {
8255796c8dcSSimon Schubert int idx = s->the_bfd_section->index;
8265796c8dcSSimon Schubert
8275796c8dcSSimon Schubert exec_set_section_address (bfd_get_filename (objfile->obfd), idx,
8285796c8dcSSimon Schubert obj_section_addr (s));
8295796c8dcSSimon Schubert }
8305796c8dcSSimon Schubert
831*ef5ccd6cSJohn Marino /* Relocating probes. */
832*ef5ccd6cSJohn Marino if (objfile->sf && objfile->sf->sym_probe_fns)
833*ef5ccd6cSJohn Marino objfile->sf->sym_probe_fns->sym_relocate_probe (objfile,
834*ef5ccd6cSJohn Marino new_offsets, delta);
835*ef5ccd6cSJohn Marino
836cf7f2e2dSJohn Marino /* Data changed. */
837cf7f2e2dSJohn Marino return 1;
838cf7f2e2dSJohn Marino }
839cf7f2e2dSJohn Marino
840cf7f2e2dSJohn Marino /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
841cf7f2e2dSJohn Marino entries in new_offsets. Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
842cf7f2e2dSJohn Marino
843cf7f2e2dSJohn Marino The number and ordering of sections does differ between the two objfiles.
844cf7f2e2dSJohn Marino Only their names match. Also the file offsets will differ (objfile being
845cf7f2e2dSJohn Marino possibly prelinked but separate_debug_objfile is probably not prelinked) but
846cf7f2e2dSJohn Marino the in-memory absolute address as specified by NEW_OFFSETS must match both
847cf7f2e2dSJohn Marino files. */
848cf7f2e2dSJohn Marino
849cf7f2e2dSJohn Marino void
objfile_relocate(struct objfile * objfile,struct section_offsets * new_offsets)850cf7f2e2dSJohn Marino objfile_relocate (struct objfile *objfile, struct section_offsets *new_offsets)
851cf7f2e2dSJohn Marino {
852cf7f2e2dSJohn Marino struct objfile *debug_objfile;
853cf7f2e2dSJohn Marino int changed = 0;
854cf7f2e2dSJohn Marino
855cf7f2e2dSJohn Marino changed |= objfile_relocate1 (objfile, new_offsets);
856cf7f2e2dSJohn Marino
857cf7f2e2dSJohn Marino for (debug_objfile = objfile->separate_debug_objfile;
858cf7f2e2dSJohn Marino debug_objfile;
859cf7f2e2dSJohn Marino debug_objfile = objfile_separate_debug_iterate (objfile, debug_objfile))
860cf7f2e2dSJohn Marino {
861cf7f2e2dSJohn Marino struct section_addr_info *objfile_addrs;
862cf7f2e2dSJohn Marino struct section_offsets *new_debug_offsets;
863cf7f2e2dSJohn Marino struct cleanup *my_cleanups;
864cf7f2e2dSJohn Marino
865cf7f2e2dSJohn Marino objfile_addrs = build_section_addr_info_from_objfile (objfile);
866cf7f2e2dSJohn Marino my_cleanups = make_cleanup (xfree, objfile_addrs);
867cf7f2e2dSJohn Marino
868cf7f2e2dSJohn Marino /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
869cf7f2e2dSJohn Marino relative ones must be already created according to debug_objfile. */
870cf7f2e2dSJohn Marino
871cf7f2e2dSJohn Marino addr_info_make_relative (objfile_addrs, debug_objfile->obfd);
872cf7f2e2dSJohn Marino
873cf7f2e2dSJohn Marino gdb_assert (debug_objfile->num_sections
874cf7f2e2dSJohn Marino == bfd_count_sections (debug_objfile->obfd));
875cf7f2e2dSJohn Marino new_debug_offsets =
876cf7f2e2dSJohn Marino xmalloc (SIZEOF_N_SECTION_OFFSETS (debug_objfile->num_sections));
877cf7f2e2dSJohn Marino make_cleanup (xfree, new_debug_offsets);
878cf7f2e2dSJohn Marino relative_addr_info_to_section_offsets (new_debug_offsets,
879cf7f2e2dSJohn Marino debug_objfile->num_sections,
880cf7f2e2dSJohn Marino objfile_addrs);
881cf7f2e2dSJohn Marino
882cf7f2e2dSJohn Marino changed |= objfile_relocate1 (debug_objfile, new_debug_offsets);
883cf7f2e2dSJohn Marino
884cf7f2e2dSJohn Marino do_cleanups (my_cleanups);
885cf7f2e2dSJohn Marino }
886cf7f2e2dSJohn Marino
8875796c8dcSSimon Schubert /* Relocate breakpoints as necessary, after things are relocated. */
888cf7f2e2dSJohn Marino if (changed)
8895796c8dcSSimon Schubert breakpoint_re_set ();
8905796c8dcSSimon Schubert }
891*ef5ccd6cSJohn Marino
892*ef5ccd6cSJohn Marino /* Rebase (add to the offsets) OBJFILE by SLIDE. SEPARATE_DEBUG_OBJFILE is
893*ef5ccd6cSJohn Marino not touched here.
894*ef5ccd6cSJohn Marino Return non-zero iff any change happened. */
895*ef5ccd6cSJohn Marino
896*ef5ccd6cSJohn Marino static int
objfile_rebase1(struct objfile * objfile,CORE_ADDR slide)897*ef5ccd6cSJohn Marino objfile_rebase1 (struct objfile *objfile, CORE_ADDR slide)
898*ef5ccd6cSJohn Marino {
899*ef5ccd6cSJohn Marino struct section_offsets *new_offsets =
900*ef5ccd6cSJohn Marino ((struct section_offsets *)
901*ef5ccd6cSJohn Marino alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
902*ef5ccd6cSJohn Marino int i;
903*ef5ccd6cSJohn Marino
904*ef5ccd6cSJohn Marino for (i = 0; i < objfile->num_sections; ++i)
905*ef5ccd6cSJohn Marino new_offsets->offsets[i] = slide;
906*ef5ccd6cSJohn Marino
907*ef5ccd6cSJohn Marino return objfile_relocate1 (objfile, new_offsets);
908*ef5ccd6cSJohn Marino }
909*ef5ccd6cSJohn Marino
910*ef5ccd6cSJohn Marino /* Rebase (add to the offsets) OBJFILE by SLIDE. Process also OBJFILE's
911*ef5ccd6cSJohn Marino SEPARATE_DEBUG_OBJFILEs. */
912*ef5ccd6cSJohn Marino
913*ef5ccd6cSJohn Marino void
objfile_rebase(struct objfile * objfile,CORE_ADDR slide)914*ef5ccd6cSJohn Marino objfile_rebase (struct objfile *objfile, CORE_ADDR slide)
915*ef5ccd6cSJohn Marino {
916*ef5ccd6cSJohn Marino struct objfile *debug_objfile;
917*ef5ccd6cSJohn Marino int changed = 0;
918*ef5ccd6cSJohn Marino
919*ef5ccd6cSJohn Marino changed |= objfile_rebase1 (objfile, slide);
920*ef5ccd6cSJohn Marino
921*ef5ccd6cSJohn Marino for (debug_objfile = objfile->separate_debug_objfile;
922*ef5ccd6cSJohn Marino debug_objfile;
923*ef5ccd6cSJohn Marino debug_objfile = objfile_separate_debug_iterate (objfile, debug_objfile))
924*ef5ccd6cSJohn Marino changed |= objfile_rebase1 (debug_objfile, slide);
925*ef5ccd6cSJohn Marino
926*ef5ccd6cSJohn Marino /* Relocate breakpoints as necessary, after things are relocated. */
927*ef5ccd6cSJohn Marino if (changed)
928*ef5ccd6cSJohn Marino breakpoint_re_set ();
929*ef5ccd6cSJohn Marino }
9305796c8dcSSimon Schubert
9315796c8dcSSimon Schubert /* Return non-zero if OBJFILE has partial symbols. */
9325796c8dcSSimon Schubert
9335796c8dcSSimon Schubert int
objfile_has_partial_symbols(struct objfile * objfile)9345796c8dcSSimon Schubert objfile_has_partial_symbols (struct objfile *objfile)
9355796c8dcSSimon Schubert {
936c50c785cSJohn Marino if (!objfile->sf)
937c50c785cSJohn Marino return 0;
938c50c785cSJohn Marino
939c50c785cSJohn Marino /* If we have not read psymbols, but we have a function capable of reading
940c50c785cSJohn Marino them, then that is an indication that they are in fact available. Without
941c50c785cSJohn Marino this function the symbols may have been already read in but they also may
942c50c785cSJohn Marino not be present in this objfile. */
943c50c785cSJohn Marino if ((objfile->flags & OBJF_PSYMTABS_READ) == 0
944c50c785cSJohn Marino && objfile->sf->sym_read_psymbols != NULL)
945c50c785cSJohn Marino return 1;
946c50c785cSJohn Marino
947c50c785cSJohn Marino return objfile->sf->qf->has_symbols (objfile);
9485796c8dcSSimon Schubert }
9495796c8dcSSimon Schubert
9505796c8dcSSimon Schubert /* Return non-zero if OBJFILE has full symbols. */
9515796c8dcSSimon Schubert
9525796c8dcSSimon Schubert int
objfile_has_full_symbols(struct objfile * objfile)9535796c8dcSSimon Schubert objfile_has_full_symbols (struct objfile *objfile)
9545796c8dcSSimon Schubert {
9555796c8dcSSimon Schubert return objfile->symtabs != NULL;
9565796c8dcSSimon Schubert }
9575796c8dcSSimon Schubert
958cf7f2e2dSJohn Marino /* Return non-zero if OBJFILE has full or partial symbols, either directly
959cf7f2e2dSJohn Marino or through a separate debug file. */
960cf7f2e2dSJohn Marino
961cf7f2e2dSJohn Marino int
objfile_has_symbols(struct objfile * objfile)962cf7f2e2dSJohn Marino objfile_has_symbols (struct objfile *objfile)
963cf7f2e2dSJohn Marino {
964cf7f2e2dSJohn Marino struct objfile *o;
965cf7f2e2dSJohn Marino
966cf7f2e2dSJohn Marino for (o = objfile; o; o = objfile_separate_debug_iterate (objfile, o))
967cf7f2e2dSJohn Marino if (objfile_has_partial_symbols (o) || objfile_has_full_symbols (o))
968cf7f2e2dSJohn Marino return 1;
969cf7f2e2dSJohn Marino return 0;
970cf7f2e2dSJohn Marino }
971cf7f2e2dSJohn Marino
972cf7f2e2dSJohn Marino
9735796c8dcSSimon Schubert /* Many places in gdb want to test just to see if we have any partial
9745796c8dcSSimon Schubert symbols available. This function returns zero if none are currently
9755796c8dcSSimon Schubert available, nonzero otherwise. */
9765796c8dcSSimon Schubert
9775796c8dcSSimon Schubert int
have_partial_symbols(void)9785796c8dcSSimon Schubert have_partial_symbols (void)
9795796c8dcSSimon Schubert {
9805796c8dcSSimon Schubert struct objfile *ofp;
9815796c8dcSSimon Schubert
9825796c8dcSSimon Schubert ALL_OBJFILES (ofp)
9835796c8dcSSimon Schubert {
9845796c8dcSSimon Schubert if (objfile_has_partial_symbols (ofp))
9855796c8dcSSimon Schubert return 1;
9865796c8dcSSimon Schubert }
9875796c8dcSSimon Schubert return 0;
9885796c8dcSSimon Schubert }
9895796c8dcSSimon Schubert
9905796c8dcSSimon Schubert /* Many places in gdb want to test just to see if we have any full
9915796c8dcSSimon Schubert symbols available. This function returns zero if none are currently
9925796c8dcSSimon Schubert available, nonzero otherwise. */
9935796c8dcSSimon Schubert
9945796c8dcSSimon Schubert int
have_full_symbols(void)9955796c8dcSSimon Schubert have_full_symbols (void)
9965796c8dcSSimon Schubert {
9975796c8dcSSimon Schubert struct objfile *ofp;
9985796c8dcSSimon Schubert
9995796c8dcSSimon Schubert ALL_OBJFILES (ofp)
10005796c8dcSSimon Schubert {
10015796c8dcSSimon Schubert if (objfile_has_full_symbols (ofp))
10025796c8dcSSimon Schubert return 1;
10035796c8dcSSimon Schubert }
10045796c8dcSSimon Schubert return 0;
10055796c8dcSSimon Schubert }
10065796c8dcSSimon Schubert
10075796c8dcSSimon Schubert
10085796c8dcSSimon Schubert /* This operations deletes all objfile entries that represent solibs that
10095796c8dcSSimon Schubert weren't explicitly loaded by the user, via e.g., the add-symbol-file
1010c50c785cSJohn Marino command. */
1011c50c785cSJohn Marino
10125796c8dcSSimon Schubert void
objfile_purge_solibs(void)10135796c8dcSSimon Schubert objfile_purge_solibs (void)
10145796c8dcSSimon Schubert {
10155796c8dcSSimon Schubert struct objfile *objf;
10165796c8dcSSimon Schubert struct objfile *temp;
10175796c8dcSSimon Schubert
10185796c8dcSSimon Schubert ALL_OBJFILES_SAFE (objf, temp)
10195796c8dcSSimon Schubert {
10205796c8dcSSimon Schubert /* We assume that the solib package has been purged already, or will
1021c50c785cSJohn Marino be soon. */
1022c50c785cSJohn Marino
10235796c8dcSSimon Schubert if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
10245796c8dcSSimon Schubert free_objfile (objf);
10255796c8dcSSimon Schubert }
10265796c8dcSSimon Schubert }
10275796c8dcSSimon Schubert
10285796c8dcSSimon Schubert
10295796c8dcSSimon Schubert /* Many places in gdb want to test just to see if we have any minimal
10305796c8dcSSimon Schubert symbols available. This function returns zero if none are currently
10315796c8dcSSimon Schubert available, nonzero otherwise. */
10325796c8dcSSimon Schubert
10335796c8dcSSimon Schubert int
have_minimal_symbols(void)10345796c8dcSSimon Schubert have_minimal_symbols (void)
10355796c8dcSSimon Schubert {
10365796c8dcSSimon Schubert struct objfile *ofp;
10375796c8dcSSimon Schubert
10385796c8dcSSimon Schubert ALL_OBJFILES (ofp)
10395796c8dcSSimon Schubert {
10405796c8dcSSimon Schubert if (ofp->minimal_symbol_count > 0)
10415796c8dcSSimon Schubert {
10425796c8dcSSimon Schubert return 1;
10435796c8dcSSimon Schubert }
10445796c8dcSSimon Schubert }
10455796c8dcSSimon Schubert return 0;
10465796c8dcSSimon Schubert }
10475796c8dcSSimon Schubert
10485796c8dcSSimon Schubert /* Qsort comparison function. */
10495796c8dcSSimon Schubert
10505796c8dcSSimon Schubert static int
qsort_cmp(const void * a,const void * b)10515796c8dcSSimon Schubert qsort_cmp (const void *a, const void *b)
10525796c8dcSSimon Schubert {
10535796c8dcSSimon Schubert const struct obj_section *sect1 = *(const struct obj_section **) a;
10545796c8dcSSimon Schubert const struct obj_section *sect2 = *(const struct obj_section **) b;
10555796c8dcSSimon Schubert const CORE_ADDR sect1_addr = obj_section_addr (sect1);
10565796c8dcSSimon Schubert const CORE_ADDR sect2_addr = obj_section_addr (sect2);
10575796c8dcSSimon Schubert
10585796c8dcSSimon Schubert if (sect1_addr < sect2_addr)
10595796c8dcSSimon Schubert return -1;
10605796c8dcSSimon Schubert else if (sect1_addr > sect2_addr)
10615796c8dcSSimon Schubert return 1;
10625796c8dcSSimon Schubert else
10635796c8dcSSimon Schubert {
10645796c8dcSSimon Schubert /* Sections are at the same address. This could happen if
10655796c8dcSSimon Schubert A) we have an objfile and a separate debuginfo.
10665796c8dcSSimon Schubert B) we are confused, and have added sections without proper relocation,
10675796c8dcSSimon Schubert or something like that. */
10685796c8dcSSimon Schubert
10695796c8dcSSimon Schubert const struct objfile *const objfile1 = sect1->objfile;
10705796c8dcSSimon Schubert const struct objfile *const objfile2 = sect2->objfile;
10715796c8dcSSimon Schubert
10725796c8dcSSimon Schubert if (objfile1->separate_debug_objfile == objfile2
10735796c8dcSSimon Schubert || objfile2->separate_debug_objfile == objfile1)
10745796c8dcSSimon Schubert {
10755796c8dcSSimon Schubert /* Case A. The ordering doesn't matter: separate debuginfo files
10765796c8dcSSimon Schubert will be filtered out later. */
10775796c8dcSSimon Schubert
10785796c8dcSSimon Schubert return 0;
10795796c8dcSSimon Schubert }
10805796c8dcSSimon Schubert
10815796c8dcSSimon Schubert /* Case B. Maintain stable sort order, so bugs in GDB are easier to
10825796c8dcSSimon Schubert triage. This section could be slow (since we iterate over all
10835796c8dcSSimon Schubert objfiles in each call to qsort_cmp), but this shouldn't happen
10845796c8dcSSimon Schubert very often (GDB is already in a confused state; one hopes this
10855796c8dcSSimon Schubert doesn't happen at all). If you discover that significant time is
10865796c8dcSSimon Schubert spent in the loops below, do 'set complaints 100' and examine the
10875796c8dcSSimon Schubert resulting complaints. */
10885796c8dcSSimon Schubert
10895796c8dcSSimon Schubert if (objfile1 == objfile2)
10905796c8dcSSimon Schubert {
10915796c8dcSSimon Schubert /* Both sections came from the same objfile. We are really confused.
10925796c8dcSSimon Schubert Sort on sequence order of sections within the objfile. */
10935796c8dcSSimon Schubert
10945796c8dcSSimon Schubert const struct obj_section *osect;
10955796c8dcSSimon Schubert
10965796c8dcSSimon Schubert ALL_OBJFILE_OSECTIONS (objfile1, osect)
10975796c8dcSSimon Schubert if (osect == sect1)
10985796c8dcSSimon Schubert return -1;
10995796c8dcSSimon Schubert else if (osect == sect2)
11005796c8dcSSimon Schubert return 1;
11015796c8dcSSimon Schubert
11025796c8dcSSimon Schubert /* We should have found one of the sections before getting here. */
1103c50c785cSJohn Marino gdb_assert_not_reached ("section not found");
11045796c8dcSSimon Schubert }
11055796c8dcSSimon Schubert else
11065796c8dcSSimon Schubert {
11075796c8dcSSimon Schubert /* Sort on sequence number of the objfile in the chain. */
11085796c8dcSSimon Schubert
11095796c8dcSSimon Schubert const struct objfile *objfile;
11105796c8dcSSimon Schubert
11115796c8dcSSimon Schubert ALL_OBJFILES (objfile)
11125796c8dcSSimon Schubert if (objfile == objfile1)
11135796c8dcSSimon Schubert return -1;
11145796c8dcSSimon Schubert else if (objfile == objfile2)
11155796c8dcSSimon Schubert return 1;
11165796c8dcSSimon Schubert
11175796c8dcSSimon Schubert /* We should have found one of the objfiles before getting here. */
1118c50c785cSJohn Marino gdb_assert_not_reached ("objfile not found");
11195796c8dcSSimon Schubert }
11205796c8dcSSimon Schubert }
11215796c8dcSSimon Schubert
11225796c8dcSSimon Schubert /* Unreachable. */
1123c50c785cSJohn Marino gdb_assert_not_reached ("unexpected code path");
11245796c8dcSSimon Schubert return 0;
11255796c8dcSSimon Schubert }
11265796c8dcSSimon Schubert
11275796c8dcSSimon Schubert /* Select "better" obj_section to keep. We prefer the one that came from
11285796c8dcSSimon Schubert the real object, rather than the one from separate debuginfo.
11295796c8dcSSimon Schubert Most of the time the two sections are exactly identical, but with
11305796c8dcSSimon Schubert prelinking the .rel.dyn section in the real object may have different
11315796c8dcSSimon Schubert size. */
11325796c8dcSSimon Schubert
11335796c8dcSSimon Schubert static struct obj_section *
preferred_obj_section(struct obj_section * a,struct obj_section * b)11345796c8dcSSimon Schubert preferred_obj_section (struct obj_section *a, struct obj_section *b)
11355796c8dcSSimon Schubert {
11365796c8dcSSimon Schubert gdb_assert (obj_section_addr (a) == obj_section_addr (b));
11375796c8dcSSimon Schubert gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
11385796c8dcSSimon Schubert || (b->objfile->separate_debug_objfile == a->objfile));
11395796c8dcSSimon Schubert gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
11405796c8dcSSimon Schubert || (b->objfile->separate_debug_objfile_backlink == a->objfile));
11415796c8dcSSimon Schubert
11425796c8dcSSimon Schubert if (a->objfile->separate_debug_objfile != NULL)
11435796c8dcSSimon Schubert return a;
11445796c8dcSSimon Schubert return b;
11455796c8dcSSimon Schubert }
11465796c8dcSSimon Schubert
11475796c8dcSSimon Schubert /* Return 1 if SECTION should be inserted into the section map.
11485796c8dcSSimon Schubert We want to insert only non-overlay and non-TLS section. */
11495796c8dcSSimon Schubert
11505796c8dcSSimon Schubert static int
insert_section_p(const struct bfd * abfd,const struct bfd_section * section)11515796c8dcSSimon Schubert insert_section_p (const struct bfd *abfd,
11525796c8dcSSimon Schubert const struct bfd_section *section)
11535796c8dcSSimon Schubert {
11545796c8dcSSimon Schubert const bfd_vma lma = bfd_section_lma (abfd, section);
11555796c8dcSSimon Schubert
1156*ef5ccd6cSJohn Marino if (overlay_debugging && lma != 0 && lma != bfd_section_vma (abfd, section)
11575796c8dcSSimon Schubert && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0)
11585796c8dcSSimon Schubert /* This is an overlay section. IN_MEMORY check is needed to avoid
11595796c8dcSSimon Schubert discarding sections from the "system supplied DSO" (aka vdso)
11605796c8dcSSimon Schubert on some Linux systems (e.g. Fedora 11). */
11615796c8dcSSimon Schubert return 0;
11625796c8dcSSimon Schubert if ((bfd_get_section_flags (abfd, section) & SEC_THREAD_LOCAL) != 0)
11635796c8dcSSimon Schubert /* This is a TLS section. */
11645796c8dcSSimon Schubert return 0;
11655796c8dcSSimon Schubert
11665796c8dcSSimon Schubert return 1;
11675796c8dcSSimon Schubert }
11685796c8dcSSimon Schubert
11695796c8dcSSimon Schubert /* Filter out overlapping sections where one section came from the real
11705796c8dcSSimon Schubert objfile, and the other from a separate debuginfo file.
11715796c8dcSSimon Schubert Return the size of table after redundant sections have been eliminated. */
11725796c8dcSSimon Schubert
11735796c8dcSSimon Schubert static int
filter_debuginfo_sections(struct obj_section ** map,int map_size)11745796c8dcSSimon Schubert filter_debuginfo_sections (struct obj_section **map, int map_size)
11755796c8dcSSimon Schubert {
11765796c8dcSSimon Schubert int i, j;
11775796c8dcSSimon Schubert
11785796c8dcSSimon Schubert for (i = 0, j = 0; i < map_size - 1; i++)
11795796c8dcSSimon Schubert {
11805796c8dcSSimon Schubert struct obj_section *const sect1 = map[i];
11815796c8dcSSimon Schubert struct obj_section *const sect2 = map[i + 1];
11825796c8dcSSimon Schubert const struct objfile *const objfile1 = sect1->objfile;
11835796c8dcSSimon Schubert const struct objfile *const objfile2 = sect2->objfile;
11845796c8dcSSimon Schubert const CORE_ADDR sect1_addr = obj_section_addr (sect1);
11855796c8dcSSimon Schubert const CORE_ADDR sect2_addr = obj_section_addr (sect2);
11865796c8dcSSimon Schubert
11875796c8dcSSimon Schubert if (sect1_addr == sect2_addr
11885796c8dcSSimon Schubert && (objfile1->separate_debug_objfile == objfile2
11895796c8dcSSimon Schubert || objfile2->separate_debug_objfile == objfile1))
11905796c8dcSSimon Schubert {
11915796c8dcSSimon Schubert map[j++] = preferred_obj_section (sect1, sect2);
11925796c8dcSSimon Schubert ++i;
11935796c8dcSSimon Schubert }
11945796c8dcSSimon Schubert else
11955796c8dcSSimon Schubert map[j++] = sect1;
11965796c8dcSSimon Schubert }
11975796c8dcSSimon Schubert
11985796c8dcSSimon Schubert if (i < map_size)
11995796c8dcSSimon Schubert {
12005796c8dcSSimon Schubert gdb_assert (i == map_size - 1);
12015796c8dcSSimon Schubert map[j++] = map[i];
12025796c8dcSSimon Schubert }
12035796c8dcSSimon Schubert
12045796c8dcSSimon Schubert /* The map should not have shrunk to less than half the original size. */
12055796c8dcSSimon Schubert gdb_assert (map_size / 2 <= j);
12065796c8dcSSimon Schubert
12075796c8dcSSimon Schubert return j;
12085796c8dcSSimon Schubert }
12095796c8dcSSimon Schubert
12105796c8dcSSimon Schubert /* Filter out overlapping sections, issuing a warning if any are found.
12115796c8dcSSimon Schubert Overlapping sections could really be overlay sections which we didn't
12125796c8dcSSimon Schubert classify as such in insert_section_p, or we could be dealing with a
12135796c8dcSSimon Schubert corrupt binary. */
12145796c8dcSSimon Schubert
12155796c8dcSSimon Schubert static int
filter_overlapping_sections(struct obj_section ** map,int map_size)12165796c8dcSSimon Schubert filter_overlapping_sections (struct obj_section **map, int map_size)
12175796c8dcSSimon Schubert {
12185796c8dcSSimon Schubert int i, j;
12195796c8dcSSimon Schubert
12205796c8dcSSimon Schubert for (i = 0, j = 0; i < map_size - 1; )
12215796c8dcSSimon Schubert {
12225796c8dcSSimon Schubert int k;
12235796c8dcSSimon Schubert
12245796c8dcSSimon Schubert map[j++] = map[i];
12255796c8dcSSimon Schubert for (k = i + 1; k < map_size; k++)
12265796c8dcSSimon Schubert {
12275796c8dcSSimon Schubert struct obj_section *const sect1 = map[i];
12285796c8dcSSimon Schubert struct obj_section *const sect2 = map[k];
12295796c8dcSSimon Schubert const CORE_ADDR sect1_addr = obj_section_addr (sect1);
12305796c8dcSSimon Schubert const CORE_ADDR sect2_addr = obj_section_addr (sect2);
12315796c8dcSSimon Schubert const CORE_ADDR sect1_endaddr = obj_section_endaddr (sect1);
12325796c8dcSSimon Schubert
12335796c8dcSSimon Schubert gdb_assert (sect1_addr <= sect2_addr);
12345796c8dcSSimon Schubert
12355796c8dcSSimon Schubert if (sect1_endaddr <= sect2_addr)
12365796c8dcSSimon Schubert break;
12375796c8dcSSimon Schubert else
12385796c8dcSSimon Schubert {
12395796c8dcSSimon Schubert /* We have an overlap. Report it. */
12405796c8dcSSimon Schubert
12415796c8dcSSimon Schubert struct objfile *const objf1 = sect1->objfile;
12425796c8dcSSimon Schubert struct objfile *const objf2 = sect2->objfile;
12435796c8dcSSimon Schubert
12445796c8dcSSimon Schubert const struct bfd_section *const bfds1 = sect1->the_bfd_section;
12455796c8dcSSimon Schubert const struct bfd_section *const bfds2 = sect2->the_bfd_section;
12465796c8dcSSimon Schubert
12475796c8dcSSimon Schubert const CORE_ADDR sect2_endaddr = obj_section_endaddr (sect2);
12485796c8dcSSimon Schubert
12495796c8dcSSimon Schubert struct gdbarch *const gdbarch = get_objfile_arch (objf1);
12505796c8dcSSimon Schubert
12515796c8dcSSimon Schubert complaint (&symfile_complaints,
12525796c8dcSSimon Schubert _("unexpected overlap between:\n"
12535796c8dcSSimon Schubert " (A) section `%s' from `%s' [%s, %s)\n"
12545796c8dcSSimon Schubert " (B) section `%s' from `%s' [%s, %s).\n"
12555796c8dcSSimon Schubert "Will ignore section B"),
12565796c8dcSSimon Schubert bfd_section_name (abfd1, bfds1), objf1->name,
12575796c8dcSSimon Schubert paddress (gdbarch, sect1_addr),
12585796c8dcSSimon Schubert paddress (gdbarch, sect1_endaddr),
12595796c8dcSSimon Schubert bfd_section_name (abfd2, bfds2), objf2->name,
12605796c8dcSSimon Schubert paddress (gdbarch, sect2_addr),
12615796c8dcSSimon Schubert paddress (gdbarch, sect2_endaddr));
12625796c8dcSSimon Schubert }
12635796c8dcSSimon Schubert }
12645796c8dcSSimon Schubert i = k;
12655796c8dcSSimon Schubert }
12665796c8dcSSimon Schubert
12675796c8dcSSimon Schubert if (i < map_size)
12685796c8dcSSimon Schubert {
12695796c8dcSSimon Schubert gdb_assert (i == map_size - 1);
12705796c8dcSSimon Schubert map[j++] = map[i];
12715796c8dcSSimon Schubert }
12725796c8dcSSimon Schubert
12735796c8dcSSimon Schubert return j;
12745796c8dcSSimon Schubert }
12755796c8dcSSimon Schubert
12765796c8dcSSimon Schubert
12775796c8dcSSimon Schubert /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
12785796c8dcSSimon Schubert TLS, overlay and overlapping sections. */
12795796c8dcSSimon Schubert
12805796c8dcSSimon Schubert static void
update_section_map(struct program_space * pspace,struct obj_section *** pmap,int * pmap_size)1281cf7f2e2dSJohn Marino update_section_map (struct program_space *pspace,
1282cf7f2e2dSJohn Marino struct obj_section ***pmap, int *pmap_size)
12835796c8dcSSimon Schubert {
12845796c8dcSSimon Schubert int alloc_size, map_size, i;
12855796c8dcSSimon Schubert struct obj_section *s, **map;
12865796c8dcSSimon Schubert struct objfile *objfile;
12875796c8dcSSimon Schubert
1288cf7f2e2dSJohn Marino gdb_assert (get_objfile_pspace_data (pspace)->objfiles_changed_p != 0);
12895796c8dcSSimon Schubert
12905796c8dcSSimon Schubert map = *pmap;
12915796c8dcSSimon Schubert xfree (map);
12925796c8dcSSimon Schubert
12935796c8dcSSimon Schubert alloc_size = 0;
1294cf7f2e2dSJohn Marino ALL_PSPACE_OBJFILES (pspace, objfile)
1295cf7f2e2dSJohn Marino ALL_OBJFILE_OSECTIONS (objfile, s)
12965796c8dcSSimon Schubert if (insert_section_p (objfile->obfd, s->the_bfd_section))
12975796c8dcSSimon Schubert alloc_size += 1;
12985796c8dcSSimon Schubert
1299cf7f2e2dSJohn Marino /* This happens on detach/attach (e.g. in gdb.base/attach.exp). */
1300cf7f2e2dSJohn Marino if (alloc_size == 0)
1301cf7f2e2dSJohn Marino {
1302cf7f2e2dSJohn Marino *pmap = NULL;
1303cf7f2e2dSJohn Marino *pmap_size = 0;
1304cf7f2e2dSJohn Marino return;
1305cf7f2e2dSJohn Marino }
1306cf7f2e2dSJohn Marino
13075796c8dcSSimon Schubert map = xmalloc (alloc_size * sizeof (*map));
13085796c8dcSSimon Schubert
13095796c8dcSSimon Schubert i = 0;
1310cf7f2e2dSJohn Marino ALL_PSPACE_OBJFILES (pspace, objfile)
1311cf7f2e2dSJohn Marino ALL_OBJFILE_OSECTIONS (objfile, s)
13125796c8dcSSimon Schubert if (insert_section_p (objfile->obfd, s->the_bfd_section))
13135796c8dcSSimon Schubert map[i++] = s;
13145796c8dcSSimon Schubert
13155796c8dcSSimon Schubert qsort (map, alloc_size, sizeof (*map), qsort_cmp);
13165796c8dcSSimon Schubert map_size = filter_debuginfo_sections(map, alloc_size);
13175796c8dcSSimon Schubert map_size = filter_overlapping_sections(map, map_size);
13185796c8dcSSimon Schubert
13195796c8dcSSimon Schubert if (map_size < alloc_size)
13205796c8dcSSimon Schubert /* Some sections were eliminated. Trim excess space. */
13215796c8dcSSimon Schubert map = xrealloc (map, map_size * sizeof (*map));
13225796c8dcSSimon Schubert else
13235796c8dcSSimon Schubert gdb_assert (alloc_size == map_size);
13245796c8dcSSimon Schubert
13255796c8dcSSimon Schubert *pmap = map;
13265796c8dcSSimon Schubert *pmap_size = map_size;
13275796c8dcSSimon Schubert }
13285796c8dcSSimon Schubert
13295796c8dcSSimon Schubert /* Bsearch comparison function. */
13305796c8dcSSimon Schubert
13315796c8dcSSimon Schubert static int
bsearch_cmp(const void * key,const void * elt)13325796c8dcSSimon Schubert bsearch_cmp (const void *key, const void *elt)
13335796c8dcSSimon Schubert {
13345796c8dcSSimon Schubert const CORE_ADDR pc = *(CORE_ADDR *) key;
13355796c8dcSSimon Schubert const struct obj_section *section = *(const struct obj_section **) elt;
13365796c8dcSSimon Schubert
13375796c8dcSSimon Schubert if (pc < obj_section_addr (section))
13385796c8dcSSimon Schubert return -1;
13395796c8dcSSimon Schubert if (pc < obj_section_endaddr (section))
13405796c8dcSSimon Schubert return 0;
13415796c8dcSSimon Schubert return 1;
13425796c8dcSSimon Schubert }
13435796c8dcSSimon Schubert
13445796c8dcSSimon Schubert /* Returns a section whose range includes PC or NULL if none found. */
13455796c8dcSSimon Schubert
13465796c8dcSSimon Schubert struct obj_section *
find_pc_section(CORE_ADDR pc)13475796c8dcSSimon Schubert find_pc_section (CORE_ADDR pc)
13485796c8dcSSimon Schubert {
1349cf7f2e2dSJohn Marino struct objfile_pspace_info *pspace_info;
13505796c8dcSSimon Schubert struct obj_section *s, **sp;
13515796c8dcSSimon Schubert
13525796c8dcSSimon Schubert /* Check for mapped overlay section first. */
13535796c8dcSSimon Schubert s = find_pc_mapped_section (pc);
13545796c8dcSSimon Schubert if (s)
13555796c8dcSSimon Schubert return s;
13565796c8dcSSimon Schubert
1357cf7f2e2dSJohn Marino pspace_info = get_objfile_pspace_data (current_program_space);
1358cf7f2e2dSJohn Marino if (pspace_info->objfiles_changed_p != 0)
13595796c8dcSSimon Schubert {
1360cf7f2e2dSJohn Marino update_section_map (current_program_space,
1361cf7f2e2dSJohn Marino &pspace_info->sections,
1362cf7f2e2dSJohn Marino &pspace_info->num_sections);
13635796c8dcSSimon Schubert
1364cf7f2e2dSJohn Marino /* Don't need updates to section map until objfiles are added,
1365cf7f2e2dSJohn Marino removed or relocated. */
1366cf7f2e2dSJohn Marino pspace_info->objfiles_changed_p = 0;
13675796c8dcSSimon Schubert }
13685796c8dcSSimon Schubert
1369cf7f2e2dSJohn Marino /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1370cf7f2e2dSJohn Marino bsearch be non-NULL. */
1371cf7f2e2dSJohn Marino if (pspace_info->sections == NULL)
1372cf7f2e2dSJohn Marino {
1373cf7f2e2dSJohn Marino gdb_assert (pspace_info->num_sections == 0);
1374cf7f2e2dSJohn Marino return NULL;
1375cf7f2e2dSJohn Marino }
1376cf7f2e2dSJohn Marino
1377cf7f2e2dSJohn Marino sp = (struct obj_section **) bsearch (&pc,
1378cf7f2e2dSJohn Marino pspace_info->sections,
1379cf7f2e2dSJohn Marino pspace_info->num_sections,
1380cf7f2e2dSJohn Marino sizeof (*pspace_info->sections),
1381cf7f2e2dSJohn Marino bsearch_cmp);
13825796c8dcSSimon Schubert if (sp != NULL)
13835796c8dcSSimon Schubert return *sp;
13845796c8dcSSimon Schubert return NULL;
13855796c8dcSSimon Schubert }
13865796c8dcSSimon Schubert
13875796c8dcSSimon Schubert
13885796c8dcSSimon Schubert /* In SVR4, we recognize a trampoline by it's section name.
13895796c8dcSSimon Schubert That is, if the pc is in a section named ".plt" then we are in
13905796c8dcSSimon Schubert a trampoline. */
13915796c8dcSSimon Schubert
13925796c8dcSSimon Schubert int
in_plt_section(CORE_ADDR pc,char * name)13935796c8dcSSimon Schubert in_plt_section (CORE_ADDR pc, char *name)
13945796c8dcSSimon Schubert {
13955796c8dcSSimon Schubert struct obj_section *s;
13965796c8dcSSimon Schubert int retval = 0;
13975796c8dcSSimon Schubert
13985796c8dcSSimon Schubert s = find_pc_section (pc);
13995796c8dcSSimon Schubert
14005796c8dcSSimon Schubert retval = (s != NULL
14015796c8dcSSimon Schubert && s->the_bfd_section->name != NULL
14025796c8dcSSimon Schubert && strcmp (s->the_bfd_section->name, ".plt") == 0);
14035796c8dcSSimon Schubert return (retval);
14045796c8dcSSimon Schubert }
14055796c8dcSSimon Schubert
14065796c8dcSSimon Schubert
14075796c8dcSSimon Schubert /* Set objfiles_changed_p so section map will be rebuilt next time it
14085796c8dcSSimon Schubert is used. Called by reread_symbols. */
14095796c8dcSSimon Schubert
14105796c8dcSSimon Schubert void
objfiles_changed(void)14115796c8dcSSimon Schubert objfiles_changed (void)
14125796c8dcSSimon Schubert {
1413cf7f2e2dSJohn Marino /* Rebuild section map next time we need it. */
1414cf7f2e2dSJohn Marino get_objfile_pspace_data (current_program_space)->objfiles_changed_p = 1;
1415cf7f2e2dSJohn Marino }
1416cf7f2e2dSJohn Marino
1417*ef5ccd6cSJohn Marino /* The default implementation for the "iterate_over_objfiles_in_search_order"
1418*ef5ccd6cSJohn Marino gdbarch method. It is equivalent to use the ALL_OBJFILES macro,
1419*ef5ccd6cSJohn Marino searching the objfiles in the order they are stored internally,
1420*ef5ccd6cSJohn Marino ignoring CURRENT_OBJFILE.
1421cf7f2e2dSJohn Marino
1422*ef5ccd6cSJohn Marino On most platorms, it should be close enough to doing the best
1423*ef5ccd6cSJohn Marino we can without some knowledge specific to the architecture. */
1424cf7f2e2dSJohn Marino
14255796c8dcSSimon Schubert void
default_iterate_over_objfiles_in_search_order(struct gdbarch * gdbarch,iterate_over_objfiles_in_search_order_cb_ftype * cb,void * cb_data,struct objfile * current_objfile)1426*ef5ccd6cSJohn Marino default_iterate_over_objfiles_in_search_order
1427*ef5ccd6cSJohn Marino (struct gdbarch *gdbarch,
1428*ef5ccd6cSJohn Marino iterate_over_objfiles_in_search_order_cb_ftype *cb,
1429*ef5ccd6cSJohn Marino void *cb_data, struct objfile *current_objfile)
14305796c8dcSSimon Schubert {
1431*ef5ccd6cSJohn Marino int stop = 0;
1432*ef5ccd6cSJohn Marino struct objfile *objfile;
14335796c8dcSSimon Schubert
1434*ef5ccd6cSJohn Marino ALL_OBJFILES (objfile)
1435*ef5ccd6cSJohn Marino {
1436*ef5ccd6cSJohn Marino stop = cb (objfile, cb_data);
1437*ef5ccd6cSJohn Marino if (stop)
14385796c8dcSSimon Schubert return;
1439*ef5ccd6cSJohn Marino }
14405796c8dcSSimon Schubert }
1441cf7f2e2dSJohn Marino
1442cf7f2e2dSJohn Marino /* Provide a prototype to silence -Wmissing-prototypes. */
1443cf7f2e2dSJohn Marino extern initialize_file_ftype _initialize_objfiles;
1444cf7f2e2dSJohn Marino
1445cf7f2e2dSJohn Marino void
_initialize_objfiles(void)1446cf7f2e2dSJohn Marino _initialize_objfiles (void)
1447cf7f2e2dSJohn Marino {
1448cf7f2e2dSJohn Marino objfiles_pspace_data
1449*ef5ccd6cSJohn Marino = register_program_space_data_with_cleanup (NULL,
1450*ef5ccd6cSJohn Marino objfiles_pspace_data_cleanup);
1451*ef5ccd6cSJohn Marino
1452*ef5ccd6cSJohn Marino objfiles_bfd_data = register_bfd_data_with_cleanup (NULL,
1453*ef5ccd6cSJohn Marino objfile_bfd_data_free);
1454cf7f2e2dSJohn Marino }
1455