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