xref: /dflybsd-src/contrib/gdb-7/gdb/objfiles.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* GDB routines for manipulating objfiles.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4*c50c785cSJohn Marino    2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011
5*c50c785cSJohn Marino    Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    Contributed by Cygnus Support, using pieces from other GDB modules.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This file is part of GDB.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
125796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
135796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
145796c8dcSSimon Schubert    (at your option) any later version.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
175796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
185796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
195796c8dcSSimon Schubert    GNU General Public License for more details.
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
225796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
235796c8dcSSimon Schubert 
245796c8dcSSimon Schubert /* This file contains support routines for creating, manipulating, and
255796c8dcSSimon Schubert    destroying objfile structures.  */
265796c8dcSSimon Schubert 
275796c8dcSSimon Schubert #include "defs.h"
285796c8dcSSimon Schubert #include "bfd.h"		/* Binary File Description */
295796c8dcSSimon Schubert #include "symtab.h"
305796c8dcSSimon Schubert #include "symfile.h"
315796c8dcSSimon Schubert #include "objfiles.h"
325796c8dcSSimon Schubert #include "gdb-stabs.h"
335796c8dcSSimon Schubert #include "target.h"
345796c8dcSSimon Schubert #include "bcache.h"
355796c8dcSSimon Schubert #include "mdebugread.h"
365796c8dcSSimon Schubert #include "expression.h"
375796c8dcSSimon Schubert #include "parser-defs.h"
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert #include "gdb_assert.h"
405796c8dcSSimon Schubert #include <sys/types.h>
415796c8dcSSimon Schubert #include "gdb_stat.h"
425796c8dcSSimon Schubert #include <fcntl.h>
435796c8dcSSimon Schubert #include "gdb_obstack.h"
445796c8dcSSimon Schubert #include "gdb_string.h"
455796c8dcSSimon Schubert #include "hashtab.h"
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert #include "breakpoint.h"
485796c8dcSSimon Schubert #include "block.h"
495796c8dcSSimon Schubert #include "dictionary.h"
505796c8dcSSimon Schubert #include "source.h"
515796c8dcSSimon Schubert #include "addrmap.h"
525796c8dcSSimon Schubert #include "arch-utils.h"
535796c8dcSSimon Schubert #include "exec.h"
545796c8dcSSimon Schubert #include "observer.h"
555796c8dcSSimon Schubert #include "complaints.h"
56cf7f2e2dSJohn Marino #include "psymtab.h"
57cf7f2e2dSJohn Marino #include "solist.h"
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert /* Prototypes for local functions */
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert static void objfile_alloc_data (struct objfile *objfile);
625796c8dcSSimon Schubert static void objfile_free_data (struct objfile *objfile);
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert /* Externally visible variables that are owned by this module.
655796c8dcSSimon Schubert    See declarations in objfile.h for more info.  */
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert struct objfile *current_objfile;	/* For symbol file being read in */
685796c8dcSSimon Schubert struct objfile *rt_common_objfile;	/* For runtime common symbols */
695796c8dcSSimon Schubert 
70cf7f2e2dSJohn Marino struct objfile_pspace_info
71cf7f2e2dSJohn Marino {
72cf7f2e2dSJohn Marino   int objfiles_changed_p;
73cf7f2e2dSJohn Marino   struct obj_section **sections;
74cf7f2e2dSJohn Marino   int num_sections;
75cf7f2e2dSJohn Marino };
76cf7f2e2dSJohn Marino 
77cf7f2e2dSJohn Marino /* Per-program-space data key.  */
78cf7f2e2dSJohn Marino static const struct program_space_data *objfiles_pspace_data;
79cf7f2e2dSJohn Marino 
80cf7f2e2dSJohn Marino static void
81cf7f2e2dSJohn Marino objfiles_pspace_data_cleanup (struct program_space *pspace, void *arg)
82cf7f2e2dSJohn Marino {
83cf7f2e2dSJohn Marino   struct objfile_pspace_info *info;
84cf7f2e2dSJohn Marino 
85cf7f2e2dSJohn Marino   info = program_space_data (pspace, objfiles_pspace_data);
86cf7f2e2dSJohn Marino   if (info != NULL)
87cf7f2e2dSJohn Marino     {
88cf7f2e2dSJohn Marino       xfree (info->sections);
89cf7f2e2dSJohn Marino       xfree (info);
90cf7f2e2dSJohn Marino     }
91cf7f2e2dSJohn Marino }
92cf7f2e2dSJohn Marino 
93cf7f2e2dSJohn Marino /* Get the current svr4 data.  If none is found yet, add it now.  This
94cf7f2e2dSJohn Marino    function always returns a valid object.  */
95cf7f2e2dSJohn Marino 
96cf7f2e2dSJohn Marino static struct objfile_pspace_info *
97cf7f2e2dSJohn Marino get_objfile_pspace_data (struct program_space *pspace)
98cf7f2e2dSJohn Marino {
99cf7f2e2dSJohn Marino   struct objfile_pspace_info *info;
100cf7f2e2dSJohn Marino 
101cf7f2e2dSJohn Marino   info = program_space_data (pspace, objfiles_pspace_data);
102cf7f2e2dSJohn Marino   if (info == NULL)
103cf7f2e2dSJohn Marino     {
104cf7f2e2dSJohn Marino       info = XZALLOC (struct objfile_pspace_info);
105cf7f2e2dSJohn Marino       set_program_space_data (pspace, objfiles_pspace_data, info);
106cf7f2e2dSJohn Marino     }
107cf7f2e2dSJohn Marino 
108cf7f2e2dSJohn Marino   return info;
109cf7f2e2dSJohn Marino }
110cf7f2e2dSJohn Marino 
1115796c8dcSSimon Schubert /* Records whether any objfiles appeared or disappeared since we last updated
1125796c8dcSSimon Schubert    address to obj section map.  */
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert /* Locate all mappable sections of a BFD file.
1155796c8dcSSimon Schubert    objfile_p_char is a char * to get it through
1165796c8dcSSimon Schubert    bfd_map_over_sections; we cast it back to its proper type.  */
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert /* Called via bfd_map_over_sections to build up the section table that
1195796c8dcSSimon Schubert    the objfile references.  The objfile contains pointers to the start
1205796c8dcSSimon Schubert    of the table (objfile->sections) and to the first location after
1215796c8dcSSimon Schubert    the end of the table (objfile->sections_end).  */
1225796c8dcSSimon Schubert 
1235796c8dcSSimon Schubert static void
1245796c8dcSSimon Schubert add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
1255796c8dcSSimon Schubert 			 void *objfile_p_char)
1265796c8dcSSimon Schubert {
1275796c8dcSSimon Schubert   struct objfile *objfile = (struct objfile *) objfile_p_char;
1285796c8dcSSimon Schubert   struct obj_section section;
1295796c8dcSSimon Schubert   flagword aflag;
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert   aflag = bfd_get_section_flags (abfd, asect);
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert   if (!(aflag & SEC_ALLOC))
1345796c8dcSSimon Schubert     return;
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert   if (0 == bfd_section_size (abfd, asect))
1375796c8dcSSimon Schubert     return;
1385796c8dcSSimon Schubert   section.objfile = objfile;
1395796c8dcSSimon Schubert   section.the_bfd_section = asect;
1405796c8dcSSimon Schubert   section.ovly_mapped = 0;
141*c50c785cSJohn Marino   obstack_grow (&objfile->objfile_obstack,
142*c50c785cSJohn Marino 		(char *) &section, sizeof (section));
1435796c8dcSSimon Schubert   objfile->sections_end
1445796c8dcSSimon Schubert     = (struct obj_section *) (((size_t) objfile->sections_end) + 1);
1455796c8dcSSimon Schubert }
1465796c8dcSSimon Schubert 
1475796c8dcSSimon Schubert /* Builds a section table for OBJFILE.
1485796c8dcSSimon Schubert    Returns 0 if OK, 1 on error (in which case bfd_error contains the
1495796c8dcSSimon Schubert    error).
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert    Note that while we are building the table, which goes into the
1525796c8dcSSimon Schubert    psymbol obstack, we hijack the sections_end pointer to instead hold
1535796c8dcSSimon Schubert    a count of the number of sections.  When bfd_map_over_sections
1545796c8dcSSimon Schubert    returns, this count is used to compute the pointer to the end of
1555796c8dcSSimon Schubert    the sections table, which then overwrites the count.
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert    Also note that the OFFSET and OVLY_MAPPED in each table entry
1585796c8dcSSimon Schubert    are initialized to zero.
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert    Also note that if anything else writes to the psymbol obstack while
1615796c8dcSSimon Schubert    we are building the table, we're pretty much hosed.  */
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert int
1645796c8dcSSimon Schubert build_objfile_section_table (struct objfile *objfile)
1655796c8dcSSimon Schubert {
1665796c8dcSSimon Schubert   /* objfile->sections can be already set when reading a mapped symbol
1675796c8dcSSimon Schubert      file.  I believe that we do need to rebuild the section table in
1685796c8dcSSimon Schubert      this case (we rebuild other things derived from the bfd), but we
1695796c8dcSSimon Schubert      can't free the old one (it's in the objfile_obstack).  So we just
1705796c8dcSSimon Schubert      waste some memory.  */
1715796c8dcSSimon Schubert 
1725796c8dcSSimon Schubert   objfile->sections_end = 0;
1735796c8dcSSimon Schubert   bfd_map_over_sections (objfile->obfd,
1745796c8dcSSimon Schubert 			 add_to_objfile_sections, (void *) objfile);
1755796c8dcSSimon Schubert   objfile->sections = obstack_finish (&objfile->objfile_obstack);
1765796c8dcSSimon Schubert   objfile->sections_end = objfile->sections + (size_t) objfile->sections_end;
1775796c8dcSSimon Schubert   return (0);
1785796c8dcSSimon Schubert }
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert /* Given a pointer to an initialized bfd (ABFD) and some flag bits
1815796c8dcSSimon Schubert    allocate a new objfile struct, fill it in as best we can, link it
1825796c8dcSSimon Schubert    into the list of all known objfiles, and return a pointer to the
1835796c8dcSSimon Schubert    new objfile struct.
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert    The FLAGS word contains various bits (OBJF_*) that can be taken as
1865796c8dcSSimon Schubert    requests for specific operations.  Other bits like OBJF_SHARED are
1875796c8dcSSimon Schubert    simply copied through to the new objfile flags member.  */
1885796c8dcSSimon Schubert 
1895796c8dcSSimon Schubert /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0
1905796c8dcSSimon Schubert    by jv-lang.c, to create an artificial objfile used to hold
1915796c8dcSSimon Schubert    information about dynamically-loaded Java classes.  Unfortunately,
1925796c8dcSSimon Schubert    that branch of this function doesn't get tested very frequently, so
1935796c8dcSSimon Schubert    it's prone to breakage.  (E.g. at one time the name was set to NULL
1945796c8dcSSimon Schubert    in that situation, which broke a loop over all names in the dynamic
1955796c8dcSSimon Schubert    library loader.)  If you change this function, please try to leave
1965796c8dcSSimon Schubert    things in a consistent state even if abfd is NULL.  */
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert struct objfile *
1995796c8dcSSimon Schubert allocate_objfile (bfd *abfd, int flags)
2005796c8dcSSimon Schubert {
201cf7f2e2dSJohn Marino   struct objfile *objfile;
2025796c8dcSSimon Schubert 
203cf7f2e2dSJohn Marino   objfile = (struct objfile *) xzalloc (sizeof (struct objfile));
204*c50c785cSJohn Marino   objfile->psymbol_cache = psymbol_bcache_init ();
205*c50c785cSJohn Marino   objfile->macro_cache = bcache_xmalloc (NULL, NULL);
206*c50c785cSJohn Marino   objfile->filename_cache = bcache_xmalloc (NULL, NULL);
2075796c8dcSSimon Schubert   /* We could use obstack_specify_allocation here instead, but
2085796c8dcSSimon Schubert      gdb_obstack.h specifies the alloc/dealloc functions.  */
2095796c8dcSSimon Schubert   obstack_init (&objfile->objfile_obstack);
2105796c8dcSSimon Schubert   terminate_minimal_symbol_table (objfile);
2115796c8dcSSimon Schubert 
2125796c8dcSSimon Schubert   objfile_alloc_data (objfile);
2135796c8dcSSimon Schubert 
2145796c8dcSSimon Schubert   /* Update the per-objfile information that comes from the bfd, ensuring
2155796c8dcSSimon Schubert      that any data that is reference is saved in the per-objfile data
2165796c8dcSSimon Schubert      region.  */
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert   objfile->obfd = gdb_bfd_ref (abfd);
2195796c8dcSSimon Schubert   if (abfd != NULL)
2205796c8dcSSimon Schubert     {
2215796c8dcSSimon Schubert       /* Look up the gdbarch associated with the BFD.  */
2225796c8dcSSimon Schubert       objfile->gdbarch = gdbarch_from_bfd (abfd);
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert       objfile->name = xstrdup (bfd_get_filename (abfd));
2255796c8dcSSimon Schubert       objfile->mtime = bfd_get_mtime (abfd);
2265796c8dcSSimon Schubert 
2275796c8dcSSimon Schubert       /* Build section table.  */
2285796c8dcSSimon Schubert 
2295796c8dcSSimon Schubert       if (build_objfile_section_table (objfile))
2305796c8dcSSimon Schubert 	{
2315796c8dcSSimon Schubert 	  error (_("Can't find the file sections in `%s': %s"),
2325796c8dcSSimon Schubert 		 objfile->name, bfd_errmsg (bfd_get_error ()));
2335796c8dcSSimon Schubert 	}
2345796c8dcSSimon Schubert     }
2355796c8dcSSimon Schubert   else
2365796c8dcSSimon Schubert     {
2375796c8dcSSimon Schubert       objfile->name = xstrdup ("<<anonymous objfile>>");
2385796c8dcSSimon Schubert     }
2395796c8dcSSimon Schubert 
240cf7f2e2dSJohn Marino   objfile->pspace = current_program_space;
241cf7f2e2dSJohn Marino 
2425796c8dcSSimon Schubert   /* Initialize the section indexes for this objfile, so that we can
2435796c8dcSSimon Schubert      later detect if they are used w/o being properly assigned to.  */
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert   objfile->sect_index_text = -1;
2465796c8dcSSimon Schubert   objfile->sect_index_data = -1;
2475796c8dcSSimon Schubert   objfile->sect_index_bss = -1;
2485796c8dcSSimon Schubert   objfile->sect_index_rodata = -1;
2495796c8dcSSimon Schubert 
2505796c8dcSSimon Schubert   /* We don't yet have a C++-specific namespace symtab.  */
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert   objfile->cp_namespace_symtab = NULL;
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert   /* Add this file onto the tail of the linked list of other such files.  */
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert   objfile->next = NULL;
2575796c8dcSSimon Schubert   if (object_files == NULL)
2585796c8dcSSimon Schubert     object_files = objfile;
2595796c8dcSSimon Schubert   else
2605796c8dcSSimon Schubert     {
261cf7f2e2dSJohn Marino       struct objfile *last_one;
262cf7f2e2dSJohn Marino 
2635796c8dcSSimon Schubert       for (last_one = object_files;
2645796c8dcSSimon Schubert 	   last_one->next;
2655796c8dcSSimon Schubert 	   last_one = last_one->next);
2665796c8dcSSimon Schubert       last_one->next = objfile;
2675796c8dcSSimon Schubert     }
2685796c8dcSSimon Schubert 
2695796c8dcSSimon Schubert   /* Save passed in flag bits.  */
2705796c8dcSSimon Schubert   objfile->flags |= flags;
2715796c8dcSSimon Schubert 
272cf7f2e2dSJohn Marino   /* Rebuild section map next time we need it.  */
273cf7f2e2dSJohn Marino   get_objfile_pspace_data (objfile->pspace)->objfiles_changed_p = 1;
2745796c8dcSSimon Schubert 
275cf7f2e2dSJohn Marino   return objfile;
2765796c8dcSSimon Schubert }
2775796c8dcSSimon Schubert 
2785796c8dcSSimon Schubert /* Retrieve the gdbarch associated with OBJFILE.  */
2795796c8dcSSimon Schubert struct gdbarch *
2805796c8dcSSimon Schubert get_objfile_arch (struct objfile *objfile)
2815796c8dcSSimon Schubert {
2825796c8dcSSimon Schubert   return objfile->gdbarch;
2835796c8dcSSimon Schubert }
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert /* Initialize entry point information for this objfile.  */
2865796c8dcSSimon Schubert 
2875796c8dcSSimon Schubert void
2885796c8dcSSimon Schubert init_entry_point_info (struct objfile *objfile)
2895796c8dcSSimon Schubert {
2905796c8dcSSimon Schubert   /* Save startup file's range of PC addresses to help blockframe.c
2915796c8dcSSimon Schubert      decide where the bottom of the stack is.  */
2925796c8dcSSimon Schubert 
2935796c8dcSSimon Schubert   if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
2945796c8dcSSimon Schubert     {
2955796c8dcSSimon Schubert       /* Executable file -- record its entry point so we'll recognize
2965796c8dcSSimon Schubert          the startup file because it contains the entry point.  */
2975796c8dcSSimon Schubert       objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
298cf7f2e2dSJohn Marino       objfile->ei.entry_point_p = 1;
2995796c8dcSSimon Schubert     }
3005796c8dcSSimon Schubert   else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
3015796c8dcSSimon Schubert 	   && bfd_get_start_address (objfile->obfd) != 0)
302cf7f2e2dSJohn Marino     {
3035796c8dcSSimon Schubert       /* Some shared libraries may have entry points set and be
3045796c8dcSSimon Schubert 	 runnable.  There's no clear way to indicate this, so just check
3055796c8dcSSimon Schubert 	 for values other than zero.  */
3065796c8dcSSimon Schubert       objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
307cf7f2e2dSJohn Marino       objfile->ei.entry_point_p = 1;
308cf7f2e2dSJohn Marino     }
3095796c8dcSSimon Schubert   else
3105796c8dcSSimon Schubert     {
3115796c8dcSSimon Schubert       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
312cf7f2e2dSJohn Marino       objfile->ei.entry_point_p = 0;
3135796c8dcSSimon Schubert     }
3145796c8dcSSimon Schubert }
3155796c8dcSSimon Schubert 
316cf7f2e2dSJohn Marino /* If there is a valid and known entry point, function fills *ENTRY_P with it
317cf7f2e2dSJohn Marino    and returns non-zero; otherwise it returns zero.  */
3185796c8dcSSimon Schubert 
319cf7f2e2dSJohn Marino int
320cf7f2e2dSJohn Marino entry_point_address_query (CORE_ADDR *entry_p)
3215796c8dcSSimon Schubert {
3225796c8dcSSimon Schubert   struct gdbarch *gdbarch;
3235796c8dcSSimon Schubert   CORE_ADDR entry_point;
3245796c8dcSSimon Schubert 
325cf7f2e2dSJohn Marino   if (symfile_objfile == NULL || !symfile_objfile->ei.entry_point_p)
3265796c8dcSSimon Schubert     return 0;
3275796c8dcSSimon Schubert 
3285796c8dcSSimon Schubert   gdbarch = get_objfile_arch (symfile_objfile);
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert   entry_point = symfile_objfile->ei.entry_point;
3315796c8dcSSimon Schubert 
3325796c8dcSSimon Schubert   /* Make certain that the address points at real code, and not a
3335796c8dcSSimon Schubert      function descriptor.  */
3345796c8dcSSimon Schubert   entry_point = gdbarch_convert_from_func_ptr_addr (gdbarch, entry_point,
3355796c8dcSSimon Schubert 						    &current_target);
3365796c8dcSSimon Schubert 
3375796c8dcSSimon Schubert   /* Remove any ISA markers, so that this matches entries in the
3385796c8dcSSimon Schubert      symbol table.  */
3395796c8dcSSimon Schubert   entry_point = gdbarch_addr_bits_remove (gdbarch, entry_point);
3405796c8dcSSimon Schubert 
341cf7f2e2dSJohn Marino   *entry_p = entry_point;
342cf7f2e2dSJohn Marino   return 1;
343cf7f2e2dSJohn Marino }
344cf7f2e2dSJohn Marino 
345cf7f2e2dSJohn Marino /* Get current entry point address.  Call error if it is not known.  */
346cf7f2e2dSJohn Marino 
347cf7f2e2dSJohn Marino CORE_ADDR
348cf7f2e2dSJohn Marino entry_point_address (void)
349cf7f2e2dSJohn Marino {
350cf7f2e2dSJohn Marino   CORE_ADDR retval;
351cf7f2e2dSJohn Marino 
352cf7f2e2dSJohn Marino   if (!entry_point_address_query (&retval))
353cf7f2e2dSJohn Marino     error (_("Entry point address is not known."));
354cf7f2e2dSJohn Marino 
355cf7f2e2dSJohn Marino   return retval;
3565796c8dcSSimon Schubert }
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert /* Create the terminating entry of OBJFILE's minimal symbol table.
3595796c8dcSSimon Schubert    If OBJFILE->msymbols is zero, allocate a single entry from
3605796c8dcSSimon Schubert    OBJFILE->objfile_obstack; otherwise, just initialize
3615796c8dcSSimon Schubert    OBJFILE->msymbols[OBJFILE->minimal_symbol_count].  */
3625796c8dcSSimon Schubert void
3635796c8dcSSimon Schubert terminate_minimal_symbol_table (struct objfile *objfile)
3645796c8dcSSimon Schubert {
3655796c8dcSSimon Schubert   if (! objfile->msymbols)
3665796c8dcSSimon Schubert     objfile->msymbols = ((struct minimal_symbol *)
3675796c8dcSSimon Schubert                          obstack_alloc (&objfile->objfile_obstack,
3685796c8dcSSimon Schubert                                         sizeof (objfile->msymbols[0])));
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert   {
3715796c8dcSSimon Schubert     struct minimal_symbol *m
3725796c8dcSSimon Schubert       = &objfile->msymbols[objfile->minimal_symbol_count];
3735796c8dcSSimon Schubert 
3745796c8dcSSimon Schubert     memset (m, 0, sizeof (*m));
3755796c8dcSSimon Schubert     /* Don't rely on these enumeration values being 0's.  */
3765796c8dcSSimon Schubert     MSYMBOL_TYPE (m) = mst_unknown;
377*c50c785cSJohn Marino     SYMBOL_SET_LANGUAGE (m, language_unknown);
3785796c8dcSSimon Schubert   }
3795796c8dcSSimon Schubert }
3805796c8dcSSimon Schubert 
381cf7f2e2dSJohn Marino /* Iterator on PARENT and every separate debug objfile of PARENT.
382cf7f2e2dSJohn Marino    The usage pattern is:
383cf7f2e2dSJohn Marino      for (objfile = parent;
384cf7f2e2dSJohn Marino           objfile;
385cf7f2e2dSJohn Marino           objfile = objfile_separate_debug_iterate (parent, objfile))
386cf7f2e2dSJohn Marino        ...
387cf7f2e2dSJohn Marino */
388cf7f2e2dSJohn Marino 
389cf7f2e2dSJohn Marino struct objfile *
390cf7f2e2dSJohn Marino objfile_separate_debug_iterate (const struct objfile *parent,
391cf7f2e2dSJohn Marino                                 const struct objfile *objfile)
392cf7f2e2dSJohn Marino {
393cf7f2e2dSJohn Marino   struct objfile *res;
394cf7f2e2dSJohn Marino 
395cf7f2e2dSJohn Marino   /* If any, return the first child.  */
396cf7f2e2dSJohn Marino   res = objfile->separate_debug_objfile;
397cf7f2e2dSJohn Marino   if (res)
398cf7f2e2dSJohn Marino     return res;
399cf7f2e2dSJohn Marino 
400cf7f2e2dSJohn Marino   /* Common case where there is no separate debug objfile.  */
401cf7f2e2dSJohn Marino   if (objfile == parent)
402cf7f2e2dSJohn Marino     return NULL;
403cf7f2e2dSJohn Marino 
404cf7f2e2dSJohn Marino   /* Return the brother if any.  Note that we don't iterate on brothers of
405cf7f2e2dSJohn Marino      the parents.  */
406cf7f2e2dSJohn Marino   res = objfile->separate_debug_objfile_link;
407cf7f2e2dSJohn Marino   if (res)
408cf7f2e2dSJohn Marino     return res;
409cf7f2e2dSJohn Marino 
410cf7f2e2dSJohn Marino   for (res = objfile->separate_debug_objfile_backlink;
411cf7f2e2dSJohn Marino        res != parent;
412cf7f2e2dSJohn Marino        res = res->separate_debug_objfile_backlink)
413cf7f2e2dSJohn Marino     {
414cf7f2e2dSJohn Marino       gdb_assert (res != NULL);
415cf7f2e2dSJohn Marino       if (res->separate_debug_objfile_link)
416cf7f2e2dSJohn Marino         return res->separate_debug_objfile_link;
417cf7f2e2dSJohn Marino     }
418cf7f2e2dSJohn Marino   return NULL;
419cf7f2e2dSJohn Marino }
4205796c8dcSSimon Schubert 
4215796c8dcSSimon Schubert /* Put one object file before a specified on in the global list.
4225796c8dcSSimon Schubert    This can be used to make sure an object file is destroyed before
4235796c8dcSSimon Schubert    another when using ALL_OBJFILES_SAFE to free all objfiles.  */
4245796c8dcSSimon Schubert void
4255796c8dcSSimon Schubert put_objfile_before (struct objfile *objfile, struct objfile *before_this)
4265796c8dcSSimon Schubert {
4275796c8dcSSimon Schubert   struct objfile **objp;
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert   unlink_objfile (objfile);
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert   for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
4325796c8dcSSimon Schubert     {
4335796c8dcSSimon Schubert       if (*objp == before_this)
4345796c8dcSSimon Schubert 	{
4355796c8dcSSimon Schubert 	  objfile->next = *objp;
4365796c8dcSSimon Schubert 	  *objp = objfile;
4375796c8dcSSimon Schubert 	  return;
4385796c8dcSSimon Schubert 	}
4395796c8dcSSimon Schubert     }
4405796c8dcSSimon Schubert 
4415796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
4425796c8dcSSimon Schubert 		  _("put_objfile_before: before objfile not in list"));
4435796c8dcSSimon Schubert }
4445796c8dcSSimon Schubert 
4455796c8dcSSimon Schubert /* Put OBJFILE at the front of the list.  */
4465796c8dcSSimon Schubert 
4475796c8dcSSimon Schubert void
4485796c8dcSSimon Schubert objfile_to_front (struct objfile *objfile)
4495796c8dcSSimon Schubert {
4505796c8dcSSimon Schubert   struct objfile **objp;
4515796c8dcSSimon Schubert   for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
4525796c8dcSSimon Schubert     {
4535796c8dcSSimon Schubert       if (*objp == objfile)
4545796c8dcSSimon Schubert 	{
4555796c8dcSSimon Schubert 	  /* Unhook it from where it is.  */
4565796c8dcSSimon Schubert 	  *objp = objfile->next;
4575796c8dcSSimon Schubert 	  /* Put it in the front.  */
4585796c8dcSSimon Schubert 	  objfile->next = object_files;
4595796c8dcSSimon Schubert 	  object_files = objfile;
4605796c8dcSSimon Schubert 	  break;
4615796c8dcSSimon Schubert 	}
4625796c8dcSSimon Schubert     }
4635796c8dcSSimon Schubert }
4645796c8dcSSimon Schubert 
4655796c8dcSSimon Schubert /* Unlink OBJFILE from the list of known objfiles, if it is found in the
4665796c8dcSSimon Schubert    list.
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert    It is not a bug, or error, to call this function if OBJFILE is not known
4695796c8dcSSimon Schubert    to be in the current list.  This is done in the case of mapped objfiles,
4705796c8dcSSimon Schubert    for example, just to ensure that the mapped objfile doesn't appear twice
4715796c8dcSSimon Schubert    in the list.  Since the list is threaded, linking in a mapped objfile
4725796c8dcSSimon Schubert    twice would create a circular list.
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert    If OBJFILE turns out to be in the list, we zap it's NEXT pointer after
4755796c8dcSSimon Schubert    unlinking it, just to ensure that we have completely severed any linkages
4765796c8dcSSimon Schubert    between the OBJFILE and the list.  */
4775796c8dcSSimon Schubert 
4785796c8dcSSimon Schubert void
4795796c8dcSSimon Schubert unlink_objfile (struct objfile *objfile)
4805796c8dcSSimon Schubert {
4815796c8dcSSimon Schubert   struct objfile **objpp;
4825796c8dcSSimon Schubert 
4835796c8dcSSimon Schubert   for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next))
4845796c8dcSSimon Schubert     {
4855796c8dcSSimon Schubert       if (*objpp == objfile)
4865796c8dcSSimon Schubert 	{
4875796c8dcSSimon Schubert 	  *objpp = (*objpp)->next;
4885796c8dcSSimon Schubert 	  objfile->next = NULL;
4895796c8dcSSimon Schubert 	  return;
4905796c8dcSSimon Schubert 	}
4915796c8dcSSimon Schubert     }
4925796c8dcSSimon Schubert 
4935796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
4945796c8dcSSimon Schubert 		  _("unlink_objfile: objfile already unlinked"));
4955796c8dcSSimon Schubert }
4965796c8dcSSimon Schubert 
497cf7f2e2dSJohn Marino /* Add OBJFILE as a separate debug objfile of PARENT.  */
498cf7f2e2dSJohn Marino 
499cf7f2e2dSJohn Marino void
500cf7f2e2dSJohn Marino add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
501cf7f2e2dSJohn Marino {
502cf7f2e2dSJohn Marino   gdb_assert (objfile && parent);
503cf7f2e2dSJohn Marino 
504cf7f2e2dSJohn Marino   /* Must not be already in a list.  */
505cf7f2e2dSJohn Marino   gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
506cf7f2e2dSJohn Marino   gdb_assert (objfile->separate_debug_objfile_link == NULL);
507cf7f2e2dSJohn Marino 
508cf7f2e2dSJohn Marino   objfile->separate_debug_objfile_backlink = parent;
509cf7f2e2dSJohn Marino   objfile->separate_debug_objfile_link = parent->separate_debug_objfile;
510cf7f2e2dSJohn Marino   parent->separate_debug_objfile = objfile;
511cf7f2e2dSJohn Marino 
512cf7f2e2dSJohn Marino   /* Put the separate debug object before the normal one, this is so that
513cf7f2e2dSJohn Marino      usage of the ALL_OBJFILES_SAFE macro will stay safe.  */
514cf7f2e2dSJohn Marino   put_objfile_before (objfile, parent);
515cf7f2e2dSJohn Marino }
516cf7f2e2dSJohn Marino 
517cf7f2e2dSJohn Marino /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
518cf7f2e2dSJohn Marino    itself.  */
519cf7f2e2dSJohn Marino 
520cf7f2e2dSJohn Marino void
521cf7f2e2dSJohn Marino free_objfile_separate_debug (struct objfile *objfile)
522cf7f2e2dSJohn Marino {
523cf7f2e2dSJohn Marino   struct objfile *child;
524cf7f2e2dSJohn Marino 
525cf7f2e2dSJohn Marino   for (child = objfile->separate_debug_objfile; child;)
526cf7f2e2dSJohn Marino     {
527cf7f2e2dSJohn Marino       struct objfile *next_child = child->separate_debug_objfile_link;
528cf7f2e2dSJohn Marino       free_objfile (child);
529cf7f2e2dSJohn Marino       child = next_child;
530cf7f2e2dSJohn Marino     }
531cf7f2e2dSJohn Marino }
5325796c8dcSSimon Schubert 
5335796c8dcSSimon Schubert /* Destroy an objfile and all the symtabs and psymtabs under it.  Note
5345796c8dcSSimon Schubert    that as much as possible is allocated on the objfile_obstack
5355796c8dcSSimon Schubert    so that the memory can be efficiently freed.
5365796c8dcSSimon Schubert 
5375796c8dcSSimon Schubert    Things which we do NOT free because they are not in malloc'd memory
5385796c8dcSSimon Schubert    or not in memory specific to the objfile include:
5395796c8dcSSimon Schubert 
5405796c8dcSSimon Schubert    objfile -> sf
5415796c8dcSSimon Schubert 
5425796c8dcSSimon Schubert    FIXME:  If the objfile is using reusable symbol information (via mmalloc),
5435796c8dcSSimon Schubert    then we need to take into account the fact that more than one process
5445796c8dcSSimon Schubert    may be using the symbol information at the same time (when mmalloc is
5455796c8dcSSimon Schubert    extended to support cooperative locking).  When more than one process
5465796c8dcSSimon Schubert    is using the mapped symbol info, we need to be more careful about when
5475796c8dcSSimon Schubert    we free objects in the reusable area.  */
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert void
5505796c8dcSSimon Schubert free_objfile (struct objfile *objfile)
5515796c8dcSSimon Schubert {
552cf7f2e2dSJohn Marino   /* Free all separate debug objfiles.  */
553cf7f2e2dSJohn Marino   free_objfile_separate_debug (objfile);
5545796c8dcSSimon Schubert 
5555796c8dcSSimon Schubert   if (objfile->separate_debug_objfile_backlink)
5565796c8dcSSimon Schubert     {
5575796c8dcSSimon Schubert       /* We freed the separate debug file, make sure the base objfile
5585796c8dcSSimon Schubert 	 doesn't reference it.  */
559cf7f2e2dSJohn Marino       struct objfile *child;
560cf7f2e2dSJohn Marino 
561cf7f2e2dSJohn Marino       child = objfile->separate_debug_objfile_backlink->separate_debug_objfile;
562cf7f2e2dSJohn Marino 
563cf7f2e2dSJohn Marino       if (child == objfile)
564cf7f2e2dSJohn Marino         {
565cf7f2e2dSJohn Marino           /* OBJFILE is the first child.  */
566cf7f2e2dSJohn Marino           objfile->separate_debug_objfile_backlink->separate_debug_objfile =
567cf7f2e2dSJohn Marino             objfile->separate_debug_objfile_link;
568cf7f2e2dSJohn Marino         }
569cf7f2e2dSJohn Marino       else
570cf7f2e2dSJohn Marino         {
571cf7f2e2dSJohn Marino           /* Find OBJFILE in the list.  */
572cf7f2e2dSJohn Marino           while (1)
573cf7f2e2dSJohn Marino             {
574cf7f2e2dSJohn Marino               if (child->separate_debug_objfile_link == objfile)
575cf7f2e2dSJohn Marino                 {
576cf7f2e2dSJohn Marino                   child->separate_debug_objfile_link =
577cf7f2e2dSJohn Marino                     objfile->separate_debug_objfile_link;
578cf7f2e2dSJohn Marino                   break;
579cf7f2e2dSJohn Marino                 }
580cf7f2e2dSJohn Marino               child = child->separate_debug_objfile_link;
581cf7f2e2dSJohn Marino               gdb_assert (child);
582cf7f2e2dSJohn Marino             }
583cf7f2e2dSJohn Marino         }
5845796c8dcSSimon Schubert     }
5855796c8dcSSimon Schubert 
5865796c8dcSSimon Schubert   /* Remove any references to this objfile in the global value
5875796c8dcSSimon Schubert      lists.  */
5885796c8dcSSimon Schubert   preserve_values (objfile);
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert   /* First do any symbol file specific actions required when we are
5915796c8dcSSimon Schubert      finished with a particular symbol file.  Note that if the objfile
5925796c8dcSSimon Schubert      is using reusable symbol information (via mmalloc) then each of
5935796c8dcSSimon Schubert      these routines is responsible for doing the correct thing, either
5945796c8dcSSimon Schubert      freeing things which are valid only during this particular gdb
5955796c8dcSSimon Schubert      execution, or leaving them to be reused during the next one.  */
5965796c8dcSSimon Schubert 
5975796c8dcSSimon Schubert   if (objfile->sf != NULL)
5985796c8dcSSimon Schubert     {
5995796c8dcSSimon Schubert       (*objfile->sf->sym_finish) (objfile);
6005796c8dcSSimon Schubert     }
6015796c8dcSSimon Schubert 
6025796c8dcSSimon Schubert   /* Discard any data modules have associated with the objfile.  */
6035796c8dcSSimon Schubert   objfile_free_data (objfile);
6045796c8dcSSimon Schubert 
6055796c8dcSSimon Schubert   gdb_bfd_unref (objfile->obfd);
6065796c8dcSSimon Schubert 
6075796c8dcSSimon Schubert   /* Remove it from the chain of all objfiles.  */
6085796c8dcSSimon Schubert 
6095796c8dcSSimon Schubert   unlink_objfile (objfile);
6105796c8dcSSimon Schubert 
6115796c8dcSSimon Schubert   if (objfile == symfile_objfile)
6125796c8dcSSimon Schubert     symfile_objfile = NULL;
6135796c8dcSSimon Schubert 
6145796c8dcSSimon Schubert   if (objfile == rt_common_objfile)
6155796c8dcSSimon Schubert     rt_common_objfile = NULL;
6165796c8dcSSimon Schubert 
6175796c8dcSSimon Schubert   /* Before the symbol table code was redone to make it easier to
6185796c8dcSSimon Schubert      selectively load and remove information particular to a specific
6195796c8dcSSimon Schubert      linkage unit, gdb used to do these things whenever the monolithic
6205796c8dcSSimon Schubert      symbol table was blown away.  How much still needs to be done
6215796c8dcSSimon Schubert      is unknown, but we play it safe for now and keep each action until
6225796c8dcSSimon Schubert      it is shown to be no longer needed.  */
6235796c8dcSSimon Schubert 
6245796c8dcSSimon Schubert   /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
6255796c8dcSSimon Schubert      for example), so we need to call this here.  */
6265796c8dcSSimon Schubert   clear_pc_function_cache ();
6275796c8dcSSimon Schubert 
6285796c8dcSSimon Schubert   /* Clear globals which might have pointed into a removed objfile.
6295796c8dcSSimon Schubert      FIXME: It's not clear which of these are supposed to persist
6305796c8dcSSimon Schubert      between expressions and which ought to be reset each time.  */
6315796c8dcSSimon Schubert   expression_context_block = NULL;
6325796c8dcSSimon Schubert   innermost_block = NULL;
6335796c8dcSSimon Schubert 
6345796c8dcSSimon Schubert   /* Check to see if the current_source_symtab belongs to this objfile,
6355796c8dcSSimon Schubert      and if so, call clear_current_source_symtab_and_line.  */
6365796c8dcSSimon Schubert 
6375796c8dcSSimon Schubert   {
6385796c8dcSSimon Schubert     struct symtab_and_line cursal = get_current_source_symtab_and_line ();
6395796c8dcSSimon Schubert     struct symtab *s;
6405796c8dcSSimon Schubert 
6415796c8dcSSimon Schubert     ALL_OBJFILE_SYMTABS (objfile, s)
6425796c8dcSSimon Schubert       {
6435796c8dcSSimon Schubert 	if (s == cursal.symtab)
6445796c8dcSSimon Schubert 	  clear_current_source_symtab_and_line ();
6455796c8dcSSimon Schubert       }
6465796c8dcSSimon Schubert   }
6475796c8dcSSimon Schubert 
6485796c8dcSSimon Schubert   /* The last thing we do is free the objfile struct itself.  */
6495796c8dcSSimon Schubert 
6505796c8dcSSimon Schubert   xfree (objfile->name);
6515796c8dcSSimon Schubert   if (objfile->global_psymbols.list)
6525796c8dcSSimon Schubert     xfree (objfile->global_psymbols.list);
6535796c8dcSSimon Schubert   if (objfile->static_psymbols.list)
6545796c8dcSSimon Schubert     xfree (objfile->static_psymbols.list);
655*c50c785cSJohn Marino   /* Free the obstacks for non-reusable objfiles.  */
656*c50c785cSJohn Marino   psymbol_bcache_free (objfile->psymbol_cache);
6575796c8dcSSimon Schubert   bcache_xfree (objfile->macro_cache);
658cf7f2e2dSJohn Marino   bcache_xfree (objfile->filename_cache);
6595796c8dcSSimon Schubert   if (objfile->demangled_names_hash)
6605796c8dcSSimon Schubert     htab_delete (objfile->demangled_names_hash);
6615796c8dcSSimon Schubert   obstack_free (&objfile->objfile_obstack, 0);
662cf7f2e2dSJohn Marino 
663cf7f2e2dSJohn Marino   /* Rebuild section map next time we need it.  */
664cf7f2e2dSJohn Marino   get_objfile_pspace_data (objfile->pspace)->objfiles_changed_p = 1;
665cf7f2e2dSJohn Marino 
6665796c8dcSSimon Schubert   xfree (objfile);
6675796c8dcSSimon Schubert }
6685796c8dcSSimon Schubert 
6695796c8dcSSimon Schubert static void
6705796c8dcSSimon Schubert do_free_objfile_cleanup (void *obj)
6715796c8dcSSimon Schubert {
6725796c8dcSSimon Schubert   free_objfile (obj);
6735796c8dcSSimon Schubert }
6745796c8dcSSimon Schubert 
6755796c8dcSSimon Schubert struct cleanup *
6765796c8dcSSimon Schubert make_cleanup_free_objfile (struct objfile *obj)
6775796c8dcSSimon Schubert {
6785796c8dcSSimon Schubert   return make_cleanup (do_free_objfile_cleanup, obj);
6795796c8dcSSimon Schubert }
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert /* Free all the object files at once and clean up their users.  */
6825796c8dcSSimon Schubert 
6835796c8dcSSimon Schubert void
6845796c8dcSSimon Schubert free_all_objfiles (void)
6855796c8dcSSimon Schubert {
6865796c8dcSSimon Schubert   struct objfile *objfile, *temp;
687cf7f2e2dSJohn Marino   struct so_list *so;
688cf7f2e2dSJohn Marino 
689cf7f2e2dSJohn Marino   /* Any objfile referencewould become stale.  */
690cf7f2e2dSJohn Marino   for (so = master_so_list (); so; so = so->next)
691cf7f2e2dSJohn Marino     gdb_assert (so->objfile == NULL);
6925796c8dcSSimon Schubert 
6935796c8dcSSimon Schubert   ALL_OBJFILES_SAFE (objfile, temp)
6945796c8dcSSimon Schubert   {
6955796c8dcSSimon Schubert     free_objfile (objfile);
6965796c8dcSSimon Schubert   }
697*c50c785cSJohn Marino   clear_symtab_users (0);
6985796c8dcSSimon Schubert }
6995796c8dcSSimon Schubert 
700*c50c785cSJohn Marino /* A helper function for objfile_relocate1 that relocates a single
701*c50c785cSJohn Marino    symbol.  */
702*c50c785cSJohn Marino 
703*c50c785cSJohn Marino static void
704*c50c785cSJohn Marino relocate_one_symbol (struct symbol *sym, struct objfile *objfile,
705*c50c785cSJohn Marino 		     struct section_offsets *delta)
706*c50c785cSJohn Marino {
707*c50c785cSJohn Marino   fixup_symbol_section (sym, objfile);
708*c50c785cSJohn Marino 
709*c50c785cSJohn Marino   /* The RS6000 code from which this was taken skipped
710*c50c785cSJohn Marino      any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
711*c50c785cSJohn Marino      But I'm leaving out that test, on the theory that
712*c50c785cSJohn Marino      they can't possibly pass the tests below.  */
713*c50c785cSJohn Marino   if ((SYMBOL_CLASS (sym) == LOC_LABEL
714*c50c785cSJohn Marino        || SYMBOL_CLASS (sym) == LOC_STATIC)
715*c50c785cSJohn Marino       && SYMBOL_SECTION (sym) >= 0)
716*c50c785cSJohn Marino     {
717*c50c785cSJohn Marino       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (delta, SYMBOL_SECTION (sym));
718*c50c785cSJohn Marino     }
719*c50c785cSJohn Marino }
720*c50c785cSJohn Marino 
7215796c8dcSSimon Schubert /* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
722cf7f2e2dSJohn Marino    entries in new_offsets.  SEPARATE_DEBUG_OBJFILE is not touched here.
723cf7f2e2dSJohn Marino    Return non-zero iff any change happened.  */
724cf7f2e2dSJohn Marino 
725cf7f2e2dSJohn Marino static int
726cf7f2e2dSJohn Marino objfile_relocate1 (struct objfile *objfile,
727cf7f2e2dSJohn Marino 		   struct section_offsets *new_offsets)
7285796c8dcSSimon Schubert {
7295796c8dcSSimon Schubert   struct obj_section *s;
7305796c8dcSSimon Schubert   struct section_offsets *delta =
7315796c8dcSSimon Schubert     ((struct section_offsets *)
7325796c8dcSSimon Schubert      alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
7335796c8dcSSimon Schubert 
7345796c8dcSSimon Schubert   int i;
7355796c8dcSSimon Schubert   int something_changed = 0;
736cf7f2e2dSJohn Marino 
7375796c8dcSSimon Schubert   for (i = 0; i < objfile->num_sections; ++i)
7385796c8dcSSimon Schubert     {
7395796c8dcSSimon Schubert       delta->offsets[i] =
7405796c8dcSSimon Schubert 	ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i);
7415796c8dcSSimon Schubert       if (ANOFFSET (delta, i) != 0)
7425796c8dcSSimon Schubert 	something_changed = 1;
7435796c8dcSSimon Schubert     }
7445796c8dcSSimon Schubert   if (!something_changed)
745cf7f2e2dSJohn Marino     return 0;
7465796c8dcSSimon Schubert 
7475796c8dcSSimon Schubert   /* OK, get all the symtabs.  */
7485796c8dcSSimon Schubert   {
7495796c8dcSSimon Schubert     struct symtab *s;
7505796c8dcSSimon Schubert 
7515796c8dcSSimon Schubert     ALL_OBJFILE_SYMTABS (objfile, s)
7525796c8dcSSimon Schubert     {
7535796c8dcSSimon Schubert       struct linetable *l;
7545796c8dcSSimon Schubert       struct blockvector *bv;
7555796c8dcSSimon Schubert       int i;
7565796c8dcSSimon Schubert 
7575796c8dcSSimon Schubert       /* First the line table.  */
7585796c8dcSSimon Schubert       l = LINETABLE (s);
7595796c8dcSSimon Schubert       if (l)
7605796c8dcSSimon Schubert 	{
7615796c8dcSSimon Schubert 	  for (i = 0; i < l->nitems; ++i)
7625796c8dcSSimon Schubert 	    l->item[i].pc += ANOFFSET (delta, s->block_line_section);
7635796c8dcSSimon Schubert 	}
7645796c8dcSSimon Schubert 
7655796c8dcSSimon Schubert       /* Don't relocate a shared blockvector more than once.  */
7665796c8dcSSimon Schubert       if (!s->primary)
7675796c8dcSSimon Schubert 	continue;
7685796c8dcSSimon Schubert 
7695796c8dcSSimon Schubert       bv = BLOCKVECTOR (s);
7705796c8dcSSimon Schubert       if (BLOCKVECTOR_MAP (bv))
7715796c8dcSSimon Schubert 	addrmap_relocate (BLOCKVECTOR_MAP (bv),
7725796c8dcSSimon Schubert 			  ANOFFSET (delta, s->block_line_section));
7735796c8dcSSimon Schubert 
7745796c8dcSSimon Schubert       for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
7755796c8dcSSimon Schubert 	{
7765796c8dcSSimon Schubert 	  struct block *b;
7775796c8dcSSimon Schubert 	  struct symbol *sym;
7785796c8dcSSimon Schubert 	  struct dict_iterator iter;
7795796c8dcSSimon Schubert 
7805796c8dcSSimon Schubert 	  b = BLOCKVECTOR_BLOCK (bv, i);
7815796c8dcSSimon Schubert 	  BLOCK_START (b) += ANOFFSET (delta, s->block_line_section);
7825796c8dcSSimon Schubert 	  BLOCK_END (b) += ANOFFSET (delta, s->block_line_section);
7835796c8dcSSimon Schubert 
7845796c8dcSSimon Schubert 	  ALL_BLOCK_SYMBOLS (b, iter, sym)
7855796c8dcSSimon Schubert 	    {
786*c50c785cSJohn Marino 	      relocate_one_symbol (sym, objfile, delta);
787*c50c785cSJohn Marino 	    }
788*c50c785cSJohn Marino 	}
789*c50c785cSJohn Marino     }
790*c50c785cSJohn Marino   }
7915796c8dcSSimon Schubert 
792*c50c785cSJohn Marino   /* Relocate isolated symbols.  */
7935796c8dcSSimon Schubert   {
794*c50c785cSJohn Marino     struct symbol *iter;
795*c50c785cSJohn Marino 
796*c50c785cSJohn Marino     for (iter = objfile->template_symbols; iter; iter = iter->hash_next)
797*c50c785cSJohn Marino       relocate_one_symbol (iter, objfile, delta);
7985796c8dcSSimon Schubert   }
7995796c8dcSSimon Schubert 
800cf7f2e2dSJohn Marino   if (objfile->psymtabs_addrmap)
801cf7f2e2dSJohn Marino     addrmap_relocate (objfile->psymtabs_addrmap,
802cf7f2e2dSJohn Marino 		      ANOFFSET (delta, SECT_OFF_TEXT (objfile)));
8035796c8dcSSimon Schubert 
804cf7f2e2dSJohn Marino   if (objfile->sf)
805cf7f2e2dSJohn Marino     objfile->sf->qf->relocate (objfile, new_offsets, delta);
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert   {
8085796c8dcSSimon Schubert     struct minimal_symbol *msym;
809cf7f2e2dSJohn Marino 
8105796c8dcSSimon Schubert     ALL_OBJFILE_MSYMBOLS (objfile, msym)
8115796c8dcSSimon Schubert       if (SYMBOL_SECTION (msym) >= 0)
8125796c8dcSSimon Schubert       SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym));
8135796c8dcSSimon Schubert   }
8145796c8dcSSimon Schubert   /* Relocating different sections by different amounts may cause the symbols
8155796c8dcSSimon Schubert      to be out of order.  */
8165796c8dcSSimon Schubert   msymbols_sort (objfile);
8175796c8dcSSimon Schubert 
818cf7f2e2dSJohn Marino   if (objfile->ei.entry_point_p)
8195796c8dcSSimon Schubert     {
8205796c8dcSSimon Schubert       /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT
8215796c8dcSSimon Schubert 	 only as a fallback.  */
8225796c8dcSSimon Schubert       struct obj_section *s;
8235796c8dcSSimon Schubert       s = find_pc_section (objfile->ei.entry_point);
8245796c8dcSSimon Schubert       if (s)
8255796c8dcSSimon Schubert         objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index);
8265796c8dcSSimon Schubert       else
8275796c8dcSSimon Schubert         objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
8285796c8dcSSimon Schubert     }
8295796c8dcSSimon Schubert 
8305796c8dcSSimon Schubert   {
8315796c8dcSSimon Schubert     int i;
832cf7f2e2dSJohn Marino 
8335796c8dcSSimon Schubert     for (i = 0; i < objfile->num_sections; ++i)
8345796c8dcSSimon Schubert       (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i);
8355796c8dcSSimon Schubert   }
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert   /* Rebuild section map next time we need it.  */
838cf7f2e2dSJohn Marino   get_objfile_pspace_data (objfile->pspace)->objfiles_changed_p = 1;
8395796c8dcSSimon Schubert 
8405796c8dcSSimon Schubert   /* Update the table in exec_ops, used to read memory.  */
8415796c8dcSSimon Schubert   ALL_OBJFILE_OSECTIONS (objfile, s)
8425796c8dcSSimon Schubert     {
8435796c8dcSSimon Schubert       int idx = s->the_bfd_section->index;
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert       exec_set_section_address (bfd_get_filename (objfile->obfd), idx,
8465796c8dcSSimon Schubert 				obj_section_addr (s));
8475796c8dcSSimon Schubert     }
8485796c8dcSSimon Schubert 
849cf7f2e2dSJohn Marino   /* Data changed.  */
850cf7f2e2dSJohn Marino   return 1;
851cf7f2e2dSJohn Marino }
852cf7f2e2dSJohn Marino 
853cf7f2e2dSJohn Marino /* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
854cf7f2e2dSJohn Marino    entries in new_offsets.  Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
855cf7f2e2dSJohn Marino 
856cf7f2e2dSJohn Marino    The number and ordering of sections does differ between the two objfiles.
857cf7f2e2dSJohn Marino    Only their names match.  Also the file offsets will differ (objfile being
858cf7f2e2dSJohn Marino    possibly prelinked but separate_debug_objfile is probably not prelinked) but
859cf7f2e2dSJohn Marino    the in-memory absolute address as specified by NEW_OFFSETS must match both
860cf7f2e2dSJohn Marino    files.  */
861cf7f2e2dSJohn Marino 
862cf7f2e2dSJohn Marino void
863cf7f2e2dSJohn Marino objfile_relocate (struct objfile *objfile, struct section_offsets *new_offsets)
864cf7f2e2dSJohn Marino {
865cf7f2e2dSJohn Marino   struct objfile *debug_objfile;
866cf7f2e2dSJohn Marino   int changed = 0;
867cf7f2e2dSJohn Marino 
868cf7f2e2dSJohn Marino   changed |= objfile_relocate1 (objfile, new_offsets);
869cf7f2e2dSJohn Marino 
870cf7f2e2dSJohn Marino   for (debug_objfile = objfile->separate_debug_objfile;
871cf7f2e2dSJohn Marino        debug_objfile;
872cf7f2e2dSJohn Marino        debug_objfile = objfile_separate_debug_iterate (objfile, debug_objfile))
873cf7f2e2dSJohn Marino     {
874cf7f2e2dSJohn Marino       struct section_addr_info *objfile_addrs;
875cf7f2e2dSJohn Marino       struct section_offsets *new_debug_offsets;
876cf7f2e2dSJohn Marino       struct cleanup *my_cleanups;
877cf7f2e2dSJohn Marino 
878cf7f2e2dSJohn Marino       objfile_addrs = build_section_addr_info_from_objfile (objfile);
879cf7f2e2dSJohn Marino       my_cleanups = make_cleanup (xfree, objfile_addrs);
880cf7f2e2dSJohn Marino 
881cf7f2e2dSJohn Marino       /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
882cf7f2e2dSJohn Marino 	 relative ones must be already created according to debug_objfile.  */
883cf7f2e2dSJohn Marino 
884cf7f2e2dSJohn Marino       addr_info_make_relative (objfile_addrs, debug_objfile->obfd);
885cf7f2e2dSJohn Marino 
886cf7f2e2dSJohn Marino       gdb_assert (debug_objfile->num_sections
887cf7f2e2dSJohn Marino 		  == bfd_count_sections (debug_objfile->obfd));
888cf7f2e2dSJohn Marino       new_debug_offsets =
889cf7f2e2dSJohn Marino 	xmalloc (SIZEOF_N_SECTION_OFFSETS (debug_objfile->num_sections));
890cf7f2e2dSJohn Marino       make_cleanup (xfree, new_debug_offsets);
891cf7f2e2dSJohn Marino       relative_addr_info_to_section_offsets (new_debug_offsets,
892cf7f2e2dSJohn Marino 					     debug_objfile->num_sections,
893cf7f2e2dSJohn Marino 					     objfile_addrs);
894cf7f2e2dSJohn Marino 
895cf7f2e2dSJohn Marino       changed |= objfile_relocate1 (debug_objfile, new_debug_offsets);
896cf7f2e2dSJohn Marino 
897cf7f2e2dSJohn Marino       do_cleanups (my_cleanups);
898cf7f2e2dSJohn Marino     }
899cf7f2e2dSJohn Marino 
9005796c8dcSSimon Schubert   /* Relocate breakpoints as necessary, after things are relocated.  */
901cf7f2e2dSJohn Marino   if (changed)
9025796c8dcSSimon Schubert     breakpoint_re_set ();
9035796c8dcSSimon Schubert }
9045796c8dcSSimon Schubert 
9055796c8dcSSimon Schubert /* Return non-zero if OBJFILE has partial symbols.  */
9065796c8dcSSimon Schubert 
9075796c8dcSSimon Schubert int
9085796c8dcSSimon Schubert objfile_has_partial_symbols (struct objfile *objfile)
9095796c8dcSSimon Schubert {
910*c50c785cSJohn Marino   if (!objfile->sf)
911*c50c785cSJohn Marino     return 0;
912*c50c785cSJohn Marino 
913*c50c785cSJohn Marino   /* If we have not read psymbols, but we have a function capable of reading
914*c50c785cSJohn Marino      them, then that is an indication that they are in fact available.  Without
915*c50c785cSJohn Marino      this function the symbols may have been already read in but they also may
916*c50c785cSJohn Marino      not be present in this objfile.  */
917*c50c785cSJohn Marino   if ((objfile->flags & OBJF_PSYMTABS_READ) == 0
918*c50c785cSJohn Marino       && objfile->sf->sym_read_psymbols != NULL)
919*c50c785cSJohn Marino     return 1;
920*c50c785cSJohn Marino 
921*c50c785cSJohn Marino   return objfile->sf->qf->has_symbols (objfile);
9225796c8dcSSimon Schubert }
9235796c8dcSSimon Schubert 
9245796c8dcSSimon Schubert /* Return non-zero if OBJFILE has full symbols.  */
9255796c8dcSSimon Schubert 
9265796c8dcSSimon Schubert int
9275796c8dcSSimon Schubert objfile_has_full_symbols (struct objfile *objfile)
9285796c8dcSSimon Schubert {
9295796c8dcSSimon Schubert   return objfile->symtabs != NULL;
9305796c8dcSSimon Schubert }
9315796c8dcSSimon Schubert 
932cf7f2e2dSJohn Marino /* Return non-zero if OBJFILE has full or partial symbols, either directly
933cf7f2e2dSJohn Marino    or through a separate debug file.  */
934cf7f2e2dSJohn Marino 
935cf7f2e2dSJohn Marino int
936cf7f2e2dSJohn Marino objfile_has_symbols (struct objfile *objfile)
937cf7f2e2dSJohn Marino {
938cf7f2e2dSJohn Marino   struct objfile *o;
939cf7f2e2dSJohn Marino 
940cf7f2e2dSJohn Marino   for (o = objfile; o; o = objfile_separate_debug_iterate (objfile, o))
941cf7f2e2dSJohn Marino     if (objfile_has_partial_symbols (o) || objfile_has_full_symbols (o))
942cf7f2e2dSJohn Marino       return 1;
943cf7f2e2dSJohn Marino   return 0;
944cf7f2e2dSJohn Marino }
945cf7f2e2dSJohn Marino 
946cf7f2e2dSJohn Marino 
9475796c8dcSSimon Schubert /* Many places in gdb want to test just to see if we have any partial
9485796c8dcSSimon Schubert    symbols available.  This function returns zero if none are currently
9495796c8dcSSimon Schubert    available, nonzero otherwise.  */
9505796c8dcSSimon Schubert 
9515796c8dcSSimon Schubert int
9525796c8dcSSimon Schubert have_partial_symbols (void)
9535796c8dcSSimon Schubert {
9545796c8dcSSimon Schubert   struct objfile *ofp;
9555796c8dcSSimon Schubert 
9565796c8dcSSimon Schubert   ALL_OBJFILES (ofp)
9575796c8dcSSimon Schubert   {
9585796c8dcSSimon Schubert     if (objfile_has_partial_symbols (ofp))
9595796c8dcSSimon Schubert       return 1;
9605796c8dcSSimon Schubert   }
9615796c8dcSSimon Schubert   return 0;
9625796c8dcSSimon Schubert }
9635796c8dcSSimon Schubert 
9645796c8dcSSimon Schubert /* Many places in gdb want to test just to see if we have any full
9655796c8dcSSimon Schubert    symbols available.  This function returns zero if none are currently
9665796c8dcSSimon Schubert    available, nonzero otherwise.  */
9675796c8dcSSimon Schubert 
9685796c8dcSSimon Schubert int
9695796c8dcSSimon Schubert have_full_symbols (void)
9705796c8dcSSimon Schubert {
9715796c8dcSSimon Schubert   struct objfile *ofp;
9725796c8dcSSimon Schubert 
9735796c8dcSSimon Schubert   ALL_OBJFILES (ofp)
9745796c8dcSSimon Schubert   {
9755796c8dcSSimon Schubert     if (objfile_has_full_symbols (ofp))
9765796c8dcSSimon Schubert       return 1;
9775796c8dcSSimon Schubert   }
9785796c8dcSSimon Schubert   return 0;
9795796c8dcSSimon Schubert }
9805796c8dcSSimon Schubert 
9815796c8dcSSimon Schubert 
9825796c8dcSSimon Schubert /* This operations deletes all objfile entries that represent solibs that
9835796c8dcSSimon Schubert    weren't explicitly loaded by the user, via e.g., the add-symbol-file
984*c50c785cSJohn Marino    command.  */
985*c50c785cSJohn Marino 
9865796c8dcSSimon Schubert void
9875796c8dcSSimon Schubert objfile_purge_solibs (void)
9885796c8dcSSimon Schubert {
9895796c8dcSSimon Schubert   struct objfile *objf;
9905796c8dcSSimon Schubert   struct objfile *temp;
9915796c8dcSSimon Schubert 
9925796c8dcSSimon Schubert   ALL_OBJFILES_SAFE (objf, temp)
9935796c8dcSSimon Schubert   {
9945796c8dcSSimon Schubert     /* We assume that the solib package has been purged already, or will
995*c50c785cSJohn Marino        be soon.  */
996*c50c785cSJohn Marino 
9975796c8dcSSimon Schubert     if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
9985796c8dcSSimon Schubert       free_objfile (objf);
9995796c8dcSSimon Schubert   }
10005796c8dcSSimon Schubert }
10015796c8dcSSimon Schubert 
10025796c8dcSSimon Schubert 
10035796c8dcSSimon Schubert /* Many places in gdb want to test just to see if we have any minimal
10045796c8dcSSimon Schubert    symbols available.  This function returns zero if none are currently
10055796c8dcSSimon Schubert    available, nonzero otherwise.  */
10065796c8dcSSimon Schubert 
10075796c8dcSSimon Schubert int
10085796c8dcSSimon Schubert have_minimal_symbols (void)
10095796c8dcSSimon Schubert {
10105796c8dcSSimon Schubert   struct objfile *ofp;
10115796c8dcSSimon Schubert 
10125796c8dcSSimon Schubert   ALL_OBJFILES (ofp)
10135796c8dcSSimon Schubert   {
10145796c8dcSSimon Schubert     if (ofp->minimal_symbol_count > 0)
10155796c8dcSSimon Schubert       {
10165796c8dcSSimon Schubert 	return 1;
10175796c8dcSSimon Schubert       }
10185796c8dcSSimon Schubert   }
10195796c8dcSSimon Schubert   return 0;
10205796c8dcSSimon Schubert }
10215796c8dcSSimon Schubert 
10225796c8dcSSimon Schubert /* Qsort comparison function.  */
10235796c8dcSSimon Schubert 
10245796c8dcSSimon Schubert static int
10255796c8dcSSimon Schubert qsort_cmp (const void *a, const void *b)
10265796c8dcSSimon Schubert {
10275796c8dcSSimon Schubert   const struct obj_section *sect1 = *(const struct obj_section **) a;
10285796c8dcSSimon Schubert   const struct obj_section *sect2 = *(const struct obj_section **) b;
10295796c8dcSSimon Schubert   const CORE_ADDR sect1_addr = obj_section_addr (sect1);
10305796c8dcSSimon Schubert   const CORE_ADDR sect2_addr = obj_section_addr (sect2);
10315796c8dcSSimon Schubert 
10325796c8dcSSimon Schubert   if (sect1_addr < sect2_addr)
10335796c8dcSSimon Schubert     return -1;
10345796c8dcSSimon Schubert   else if (sect1_addr > sect2_addr)
10355796c8dcSSimon Schubert     return 1;
10365796c8dcSSimon Schubert   else
10375796c8dcSSimon Schubert     {
10385796c8dcSSimon Schubert       /* Sections are at the same address.  This could happen if
10395796c8dcSSimon Schubert 	 A) we have an objfile and a separate debuginfo.
10405796c8dcSSimon Schubert 	 B) we are confused, and have added sections without proper relocation,
10415796c8dcSSimon Schubert 	 or something like that.  */
10425796c8dcSSimon Schubert 
10435796c8dcSSimon Schubert       const struct objfile *const objfile1 = sect1->objfile;
10445796c8dcSSimon Schubert       const struct objfile *const objfile2 = sect2->objfile;
10455796c8dcSSimon Schubert 
10465796c8dcSSimon Schubert       if (objfile1->separate_debug_objfile == objfile2
10475796c8dcSSimon Schubert 	  || objfile2->separate_debug_objfile == objfile1)
10485796c8dcSSimon Schubert 	{
10495796c8dcSSimon Schubert 	  /* Case A.  The ordering doesn't matter: separate debuginfo files
10505796c8dcSSimon Schubert 	     will be filtered out later.  */
10515796c8dcSSimon Schubert 
10525796c8dcSSimon Schubert 	  return 0;
10535796c8dcSSimon Schubert 	}
10545796c8dcSSimon Schubert 
10555796c8dcSSimon Schubert       /* Case B.  Maintain stable sort order, so bugs in GDB are easier to
10565796c8dcSSimon Schubert 	 triage.  This section could be slow (since we iterate over all
10575796c8dcSSimon Schubert 	 objfiles in each call to qsort_cmp), but this shouldn't happen
10585796c8dcSSimon Schubert 	 very often (GDB is already in a confused state; one hopes this
10595796c8dcSSimon Schubert 	 doesn't happen at all).  If you discover that significant time is
10605796c8dcSSimon Schubert 	 spent in the loops below, do 'set complaints 100' and examine the
10615796c8dcSSimon Schubert 	 resulting complaints.  */
10625796c8dcSSimon Schubert 
10635796c8dcSSimon Schubert       if (objfile1 == objfile2)
10645796c8dcSSimon Schubert 	{
10655796c8dcSSimon Schubert 	  /* Both sections came from the same objfile.  We are really confused.
10665796c8dcSSimon Schubert 	     Sort on sequence order of sections within the objfile.  */
10675796c8dcSSimon Schubert 
10685796c8dcSSimon Schubert 	  const struct obj_section *osect;
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert 	  ALL_OBJFILE_OSECTIONS (objfile1, osect)
10715796c8dcSSimon Schubert 	    if (osect == sect1)
10725796c8dcSSimon Schubert 	      return -1;
10735796c8dcSSimon Schubert 	    else if (osect == sect2)
10745796c8dcSSimon Schubert 	      return 1;
10755796c8dcSSimon Schubert 
10765796c8dcSSimon Schubert 	  /* We should have found one of the sections before getting here.  */
1077*c50c785cSJohn Marino 	  gdb_assert_not_reached ("section not found");
10785796c8dcSSimon Schubert 	}
10795796c8dcSSimon Schubert       else
10805796c8dcSSimon Schubert 	{
10815796c8dcSSimon Schubert 	  /* Sort on sequence number of the objfile in the chain.  */
10825796c8dcSSimon Schubert 
10835796c8dcSSimon Schubert 	  const struct objfile *objfile;
10845796c8dcSSimon Schubert 
10855796c8dcSSimon Schubert 	  ALL_OBJFILES (objfile)
10865796c8dcSSimon Schubert 	    if (objfile == objfile1)
10875796c8dcSSimon Schubert 	      return -1;
10885796c8dcSSimon Schubert 	    else if (objfile == objfile2)
10895796c8dcSSimon Schubert 	      return 1;
10905796c8dcSSimon Schubert 
10915796c8dcSSimon Schubert 	  /* We should have found one of the objfiles before getting here.  */
1092*c50c785cSJohn Marino 	  gdb_assert_not_reached ("objfile not found");
10935796c8dcSSimon Schubert 	}
10945796c8dcSSimon Schubert     }
10955796c8dcSSimon Schubert 
10965796c8dcSSimon Schubert   /* Unreachable.  */
1097*c50c785cSJohn Marino   gdb_assert_not_reached ("unexpected code path");
10985796c8dcSSimon Schubert   return 0;
10995796c8dcSSimon Schubert }
11005796c8dcSSimon Schubert 
11015796c8dcSSimon Schubert /* Select "better" obj_section to keep.  We prefer the one that came from
11025796c8dcSSimon Schubert    the real object, rather than the one from separate debuginfo.
11035796c8dcSSimon Schubert    Most of the time the two sections are exactly identical, but with
11045796c8dcSSimon Schubert    prelinking the .rel.dyn section in the real object may have different
11055796c8dcSSimon Schubert    size.  */
11065796c8dcSSimon Schubert 
11075796c8dcSSimon Schubert static struct obj_section *
11085796c8dcSSimon Schubert preferred_obj_section (struct obj_section *a, struct obj_section *b)
11095796c8dcSSimon Schubert {
11105796c8dcSSimon Schubert   gdb_assert (obj_section_addr (a) == obj_section_addr (b));
11115796c8dcSSimon Schubert   gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
11125796c8dcSSimon Schubert 	      || (b->objfile->separate_debug_objfile == a->objfile));
11135796c8dcSSimon Schubert   gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
11145796c8dcSSimon Schubert 	      || (b->objfile->separate_debug_objfile_backlink == a->objfile));
11155796c8dcSSimon Schubert 
11165796c8dcSSimon Schubert   if (a->objfile->separate_debug_objfile != NULL)
11175796c8dcSSimon Schubert     return a;
11185796c8dcSSimon Schubert   return b;
11195796c8dcSSimon Schubert }
11205796c8dcSSimon Schubert 
11215796c8dcSSimon Schubert /* Return 1 if SECTION should be inserted into the section map.
11225796c8dcSSimon Schubert    We want to insert only non-overlay and non-TLS section.  */
11235796c8dcSSimon Schubert 
11245796c8dcSSimon Schubert static int
11255796c8dcSSimon Schubert insert_section_p (const struct bfd *abfd,
11265796c8dcSSimon Schubert 		  const struct bfd_section *section)
11275796c8dcSSimon Schubert {
11285796c8dcSSimon Schubert   const bfd_vma lma = bfd_section_lma (abfd, section);
11295796c8dcSSimon Schubert 
11305796c8dcSSimon Schubert   if (lma != 0 && lma != bfd_section_vma (abfd, section)
11315796c8dcSSimon Schubert       && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0)
11325796c8dcSSimon Schubert     /* This is an overlay section.  IN_MEMORY check is needed to avoid
11335796c8dcSSimon Schubert        discarding sections from the "system supplied DSO" (aka vdso)
11345796c8dcSSimon Schubert        on some Linux systems (e.g. Fedora 11).  */
11355796c8dcSSimon Schubert     return 0;
11365796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, section) & SEC_THREAD_LOCAL) != 0)
11375796c8dcSSimon Schubert     /* This is a TLS section.  */
11385796c8dcSSimon Schubert     return 0;
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert   return 1;
11415796c8dcSSimon Schubert }
11425796c8dcSSimon Schubert 
11435796c8dcSSimon Schubert /* Filter out overlapping sections where one section came from the real
11445796c8dcSSimon Schubert    objfile, and the other from a separate debuginfo file.
11455796c8dcSSimon Schubert    Return the size of table after redundant sections have been eliminated.  */
11465796c8dcSSimon Schubert 
11475796c8dcSSimon Schubert static int
11485796c8dcSSimon Schubert filter_debuginfo_sections (struct obj_section **map, int map_size)
11495796c8dcSSimon Schubert {
11505796c8dcSSimon Schubert   int i, j;
11515796c8dcSSimon Schubert 
11525796c8dcSSimon Schubert   for (i = 0, j = 0; i < map_size - 1; i++)
11535796c8dcSSimon Schubert     {
11545796c8dcSSimon Schubert       struct obj_section *const sect1 = map[i];
11555796c8dcSSimon Schubert       struct obj_section *const sect2 = map[i + 1];
11565796c8dcSSimon Schubert       const struct objfile *const objfile1 = sect1->objfile;
11575796c8dcSSimon Schubert       const struct objfile *const objfile2 = sect2->objfile;
11585796c8dcSSimon Schubert       const CORE_ADDR sect1_addr = obj_section_addr (sect1);
11595796c8dcSSimon Schubert       const CORE_ADDR sect2_addr = obj_section_addr (sect2);
11605796c8dcSSimon Schubert 
11615796c8dcSSimon Schubert       if (sect1_addr == sect2_addr
11625796c8dcSSimon Schubert 	  && (objfile1->separate_debug_objfile == objfile2
11635796c8dcSSimon Schubert 	      || objfile2->separate_debug_objfile == objfile1))
11645796c8dcSSimon Schubert 	{
11655796c8dcSSimon Schubert 	  map[j++] = preferred_obj_section (sect1, sect2);
11665796c8dcSSimon Schubert 	  ++i;
11675796c8dcSSimon Schubert 	}
11685796c8dcSSimon Schubert       else
11695796c8dcSSimon Schubert 	map[j++] = sect1;
11705796c8dcSSimon Schubert     }
11715796c8dcSSimon Schubert 
11725796c8dcSSimon Schubert   if (i < map_size)
11735796c8dcSSimon Schubert     {
11745796c8dcSSimon Schubert       gdb_assert (i == map_size - 1);
11755796c8dcSSimon Schubert       map[j++] = map[i];
11765796c8dcSSimon Schubert     }
11775796c8dcSSimon Schubert 
11785796c8dcSSimon Schubert   /* The map should not have shrunk to less than half the original size.  */
11795796c8dcSSimon Schubert   gdb_assert (map_size / 2 <= j);
11805796c8dcSSimon Schubert 
11815796c8dcSSimon Schubert   return j;
11825796c8dcSSimon Schubert }
11835796c8dcSSimon Schubert 
11845796c8dcSSimon Schubert /* Filter out overlapping sections, issuing a warning if any are found.
11855796c8dcSSimon Schubert    Overlapping sections could really be overlay sections which we didn't
11865796c8dcSSimon Schubert    classify as such in insert_section_p, or we could be dealing with a
11875796c8dcSSimon Schubert    corrupt binary.  */
11885796c8dcSSimon Schubert 
11895796c8dcSSimon Schubert static int
11905796c8dcSSimon Schubert filter_overlapping_sections (struct obj_section **map, int map_size)
11915796c8dcSSimon Schubert {
11925796c8dcSSimon Schubert   int i, j;
11935796c8dcSSimon Schubert 
11945796c8dcSSimon Schubert   for (i = 0, j = 0; i < map_size - 1; )
11955796c8dcSSimon Schubert     {
11965796c8dcSSimon Schubert       int k;
11975796c8dcSSimon Schubert 
11985796c8dcSSimon Schubert       map[j++] = map[i];
11995796c8dcSSimon Schubert       for (k = i + 1; k < map_size; k++)
12005796c8dcSSimon Schubert 	{
12015796c8dcSSimon Schubert 	  struct obj_section *const sect1 = map[i];
12025796c8dcSSimon Schubert 	  struct obj_section *const sect2 = map[k];
12035796c8dcSSimon Schubert 	  const CORE_ADDR sect1_addr = obj_section_addr (sect1);
12045796c8dcSSimon Schubert 	  const CORE_ADDR sect2_addr = obj_section_addr (sect2);
12055796c8dcSSimon Schubert 	  const CORE_ADDR sect1_endaddr = obj_section_endaddr (sect1);
12065796c8dcSSimon Schubert 
12075796c8dcSSimon Schubert 	  gdb_assert (sect1_addr <= sect2_addr);
12085796c8dcSSimon Schubert 
12095796c8dcSSimon Schubert 	  if (sect1_endaddr <= sect2_addr)
12105796c8dcSSimon Schubert 	    break;
12115796c8dcSSimon Schubert 	  else
12125796c8dcSSimon Schubert 	    {
12135796c8dcSSimon Schubert 	      /* We have an overlap.  Report it.  */
12145796c8dcSSimon Schubert 
12155796c8dcSSimon Schubert 	      struct objfile *const objf1 = sect1->objfile;
12165796c8dcSSimon Schubert 	      struct objfile *const objf2 = sect2->objfile;
12175796c8dcSSimon Schubert 
12185796c8dcSSimon Schubert 	      const struct bfd *const abfd1 = objf1->obfd;
12195796c8dcSSimon Schubert 	      const struct bfd *const abfd2 = objf2->obfd;
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert 	      const struct bfd_section *const bfds1 = sect1->the_bfd_section;
12225796c8dcSSimon Schubert 	      const struct bfd_section *const bfds2 = sect2->the_bfd_section;
12235796c8dcSSimon Schubert 
12245796c8dcSSimon Schubert 	      const CORE_ADDR sect2_endaddr = obj_section_endaddr (sect2);
12255796c8dcSSimon Schubert 
12265796c8dcSSimon Schubert 	      struct gdbarch *const gdbarch = get_objfile_arch (objf1);
12275796c8dcSSimon Schubert 
12285796c8dcSSimon Schubert 	      complaint (&symfile_complaints,
12295796c8dcSSimon Schubert 			 _("unexpected overlap between:\n"
12305796c8dcSSimon Schubert 			   " (A) section `%s' from `%s' [%s, %s)\n"
12315796c8dcSSimon Schubert 			   " (B) section `%s' from `%s' [%s, %s).\n"
12325796c8dcSSimon Schubert 			   "Will ignore section B"),
12335796c8dcSSimon Schubert 			 bfd_section_name (abfd1, bfds1), objf1->name,
12345796c8dcSSimon Schubert 			 paddress (gdbarch, sect1_addr),
12355796c8dcSSimon Schubert 			 paddress (gdbarch, sect1_endaddr),
12365796c8dcSSimon Schubert 			 bfd_section_name (abfd2, bfds2), objf2->name,
12375796c8dcSSimon Schubert 			 paddress (gdbarch, sect2_addr),
12385796c8dcSSimon Schubert 			 paddress (gdbarch, sect2_endaddr));
12395796c8dcSSimon Schubert 	    }
12405796c8dcSSimon Schubert 	}
12415796c8dcSSimon Schubert       i = k;
12425796c8dcSSimon Schubert     }
12435796c8dcSSimon Schubert 
12445796c8dcSSimon Schubert   if (i < map_size)
12455796c8dcSSimon Schubert     {
12465796c8dcSSimon Schubert       gdb_assert (i == map_size - 1);
12475796c8dcSSimon Schubert       map[j++] = map[i];
12485796c8dcSSimon Schubert     }
12495796c8dcSSimon Schubert 
12505796c8dcSSimon Schubert   return j;
12515796c8dcSSimon Schubert }
12525796c8dcSSimon Schubert 
12535796c8dcSSimon Schubert 
12545796c8dcSSimon Schubert /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
12555796c8dcSSimon Schubert    TLS, overlay and overlapping sections.  */
12565796c8dcSSimon Schubert 
12575796c8dcSSimon Schubert static void
1258cf7f2e2dSJohn Marino update_section_map (struct program_space *pspace,
1259cf7f2e2dSJohn Marino 		    struct obj_section ***pmap, int *pmap_size)
12605796c8dcSSimon Schubert {
12615796c8dcSSimon Schubert   int alloc_size, map_size, i;
12625796c8dcSSimon Schubert   struct obj_section *s, **map;
12635796c8dcSSimon Schubert   struct objfile *objfile;
12645796c8dcSSimon Schubert 
1265cf7f2e2dSJohn Marino   gdb_assert (get_objfile_pspace_data (pspace)->objfiles_changed_p != 0);
12665796c8dcSSimon Schubert 
12675796c8dcSSimon Schubert   map = *pmap;
12685796c8dcSSimon Schubert   xfree (map);
12695796c8dcSSimon Schubert 
12705796c8dcSSimon Schubert   alloc_size = 0;
1271cf7f2e2dSJohn Marino   ALL_PSPACE_OBJFILES (pspace, objfile)
1272cf7f2e2dSJohn Marino     ALL_OBJFILE_OSECTIONS (objfile, s)
12735796c8dcSSimon Schubert       if (insert_section_p (objfile->obfd, s->the_bfd_section))
12745796c8dcSSimon Schubert 	alloc_size += 1;
12755796c8dcSSimon Schubert 
1276cf7f2e2dSJohn Marino   /* This happens on detach/attach (e.g. in gdb.base/attach.exp).  */
1277cf7f2e2dSJohn Marino   if (alloc_size == 0)
1278cf7f2e2dSJohn Marino     {
1279cf7f2e2dSJohn Marino       *pmap = NULL;
1280cf7f2e2dSJohn Marino       *pmap_size = 0;
1281cf7f2e2dSJohn Marino       return;
1282cf7f2e2dSJohn Marino     }
1283cf7f2e2dSJohn Marino 
12845796c8dcSSimon Schubert   map = xmalloc (alloc_size * sizeof (*map));
12855796c8dcSSimon Schubert 
12865796c8dcSSimon Schubert   i = 0;
1287cf7f2e2dSJohn Marino   ALL_PSPACE_OBJFILES (pspace, objfile)
1288cf7f2e2dSJohn Marino     ALL_OBJFILE_OSECTIONS (objfile, s)
12895796c8dcSSimon Schubert       if (insert_section_p (objfile->obfd, s->the_bfd_section))
12905796c8dcSSimon Schubert 	map[i++] = s;
12915796c8dcSSimon Schubert 
12925796c8dcSSimon Schubert   qsort (map, alloc_size, sizeof (*map), qsort_cmp);
12935796c8dcSSimon Schubert   map_size = filter_debuginfo_sections(map, alloc_size);
12945796c8dcSSimon Schubert   map_size = filter_overlapping_sections(map, map_size);
12955796c8dcSSimon Schubert 
12965796c8dcSSimon Schubert   if (map_size < alloc_size)
12975796c8dcSSimon Schubert     /* Some sections were eliminated.  Trim excess space.  */
12985796c8dcSSimon Schubert     map = xrealloc (map, map_size * sizeof (*map));
12995796c8dcSSimon Schubert   else
13005796c8dcSSimon Schubert     gdb_assert (alloc_size == map_size);
13015796c8dcSSimon Schubert 
13025796c8dcSSimon Schubert   *pmap = map;
13035796c8dcSSimon Schubert   *pmap_size = map_size;
13045796c8dcSSimon Schubert }
13055796c8dcSSimon Schubert 
13065796c8dcSSimon Schubert /* Bsearch comparison function.  */
13075796c8dcSSimon Schubert 
13085796c8dcSSimon Schubert static int
13095796c8dcSSimon Schubert bsearch_cmp (const void *key, const void *elt)
13105796c8dcSSimon Schubert {
13115796c8dcSSimon Schubert   const CORE_ADDR pc = *(CORE_ADDR *) key;
13125796c8dcSSimon Schubert   const struct obj_section *section = *(const struct obj_section **) elt;
13135796c8dcSSimon Schubert 
13145796c8dcSSimon Schubert   if (pc < obj_section_addr (section))
13155796c8dcSSimon Schubert     return -1;
13165796c8dcSSimon Schubert   if (pc < obj_section_endaddr (section))
13175796c8dcSSimon Schubert     return 0;
13185796c8dcSSimon Schubert   return 1;
13195796c8dcSSimon Schubert }
13205796c8dcSSimon Schubert 
13215796c8dcSSimon Schubert /* Returns a section whose range includes PC or NULL if none found.   */
13225796c8dcSSimon Schubert 
13235796c8dcSSimon Schubert struct obj_section *
13245796c8dcSSimon Schubert find_pc_section (CORE_ADDR pc)
13255796c8dcSSimon Schubert {
1326cf7f2e2dSJohn Marino   struct objfile_pspace_info *pspace_info;
13275796c8dcSSimon Schubert   struct obj_section *s, **sp;
13285796c8dcSSimon Schubert 
13295796c8dcSSimon Schubert   /* Check for mapped overlay section first.  */
13305796c8dcSSimon Schubert   s = find_pc_mapped_section (pc);
13315796c8dcSSimon Schubert   if (s)
13325796c8dcSSimon Schubert     return s;
13335796c8dcSSimon Schubert 
1334cf7f2e2dSJohn Marino   pspace_info = get_objfile_pspace_data (current_program_space);
1335cf7f2e2dSJohn Marino   if (pspace_info->objfiles_changed_p != 0)
13365796c8dcSSimon Schubert     {
1337cf7f2e2dSJohn Marino       update_section_map (current_program_space,
1338cf7f2e2dSJohn Marino 			  &pspace_info->sections,
1339cf7f2e2dSJohn Marino 			  &pspace_info->num_sections);
13405796c8dcSSimon Schubert 
1341cf7f2e2dSJohn Marino       /* Don't need updates to section map until objfiles are added,
1342cf7f2e2dSJohn Marino          removed or relocated.  */
1343cf7f2e2dSJohn Marino       pspace_info->objfiles_changed_p = 0;
13445796c8dcSSimon Schubert     }
13455796c8dcSSimon Schubert 
1346cf7f2e2dSJohn Marino   /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1347cf7f2e2dSJohn Marino      bsearch be non-NULL.  */
1348cf7f2e2dSJohn Marino   if (pspace_info->sections == NULL)
1349cf7f2e2dSJohn Marino     {
1350cf7f2e2dSJohn Marino       gdb_assert (pspace_info->num_sections == 0);
1351cf7f2e2dSJohn Marino       return NULL;
1352cf7f2e2dSJohn Marino     }
1353cf7f2e2dSJohn Marino 
1354cf7f2e2dSJohn Marino   sp = (struct obj_section **) bsearch (&pc,
1355cf7f2e2dSJohn Marino 					pspace_info->sections,
1356cf7f2e2dSJohn Marino 					pspace_info->num_sections,
1357cf7f2e2dSJohn Marino 					sizeof (*pspace_info->sections),
1358cf7f2e2dSJohn Marino 					bsearch_cmp);
13595796c8dcSSimon Schubert   if (sp != NULL)
13605796c8dcSSimon Schubert     return *sp;
13615796c8dcSSimon Schubert   return NULL;
13625796c8dcSSimon Schubert }
13635796c8dcSSimon Schubert 
13645796c8dcSSimon Schubert 
13655796c8dcSSimon Schubert /* In SVR4, we recognize a trampoline by it's section name.
13665796c8dcSSimon Schubert    That is, if the pc is in a section named ".plt" then we are in
13675796c8dcSSimon Schubert    a trampoline.  */
13685796c8dcSSimon Schubert 
13695796c8dcSSimon Schubert int
13705796c8dcSSimon Schubert in_plt_section (CORE_ADDR pc, char *name)
13715796c8dcSSimon Schubert {
13725796c8dcSSimon Schubert   struct obj_section *s;
13735796c8dcSSimon Schubert   int retval = 0;
13745796c8dcSSimon Schubert 
13755796c8dcSSimon Schubert   s = find_pc_section (pc);
13765796c8dcSSimon Schubert 
13775796c8dcSSimon Schubert   retval = (s != NULL
13785796c8dcSSimon Schubert 	    && s->the_bfd_section->name != NULL
13795796c8dcSSimon Schubert 	    && strcmp (s->the_bfd_section->name, ".plt") == 0);
13805796c8dcSSimon Schubert   return (retval);
13815796c8dcSSimon Schubert }
13825796c8dcSSimon Schubert 
13835796c8dcSSimon Schubert 
13845796c8dcSSimon Schubert /* Keep a registry of per-objfile data-pointers required by other GDB
13855796c8dcSSimon Schubert    modules.  */
13865796c8dcSSimon Schubert 
13875796c8dcSSimon Schubert struct objfile_data
13885796c8dcSSimon Schubert {
13895796c8dcSSimon Schubert   unsigned index;
13905796c8dcSSimon Schubert   void (*save) (struct objfile *, void *);
13915796c8dcSSimon Schubert   void (*free) (struct objfile *, void *);
13925796c8dcSSimon Schubert };
13935796c8dcSSimon Schubert 
13945796c8dcSSimon Schubert struct objfile_data_registration
13955796c8dcSSimon Schubert {
13965796c8dcSSimon Schubert   struct objfile_data *data;
13975796c8dcSSimon Schubert   struct objfile_data_registration *next;
13985796c8dcSSimon Schubert };
13995796c8dcSSimon Schubert 
14005796c8dcSSimon Schubert struct objfile_data_registry
14015796c8dcSSimon Schubert {
14025796c8dcSSimon Schubert   struct objfile_data_registration *registrations;
14035796c8dcSSimon Schubert   unsigned num_registrations;
14045796c8dcSSimon Schubert };
14055796c8dcSSimon Schubert 
14065796c8dcSSimon Schubert static struct objfile_data_registry objfile_data_registry = { NULL, 0 };
14075796c8dcSSimon Schubert 
14085796c8dcSSimon Schubert const struct objfile_data *
14095796c8dcSSimon Schubert register_objfile_data_with_cleanup (void (*save) (struct objfile *, void *),
14105796c8dcSSimon Schubert 				    void (*free) (struct objfile *, void *))
14115796c8dcSSimon Schubert {
14125796c8dcSSimon Schubert   struct objfile_data_registration **curr;
14135796c8dcSSimon Schubert 
14145796c8dcSSimon Schubert   /* Append new registration.  */
14155796c8dcSSimon Schubert   for (curr = &objfile_data_registry.registrations;
14165796c8dcSSimon Schubert        *curr != NULL; curr = &(*curr)->next);
14175796c8dcSSimon Schubert 
14185796c8dcSSimon Schubert   *curr = XMALLOC (struct objfile_data_registration);
14195796c8dcSSimon Schubert   (*curr)->next = NULL;
14205796c8dcSSimon Schubert   (*curr)->data = XMALLOC (struct objfile_data);
14215796c8dcSSimon Schubert   (*curr)->data->index = objfile_data_registry.num_registrations++;
14225796c8dcSSimon Schubert   (*curr)->data->save = save;
14235796c8dcSSimon Schubert   (*curr)->data->free = free;
14245796c8dcSSimon Schubert 
14255796c8dcSSimon Schubert   return (*curr)->data;
14265796c8dcSSimon Schubert }
14275796c8dcSSimon Schubert 
14285796c8dcSSimon Schubert const struct objfile_data *
14295796c8dcSSimon Schubert register_objfile_data (void)
14305796c8dcSSimon Schubert {
14315796c8dcSSimon Schubert   return register_objfile_data_with_cleanup (NULL, NULL);
14325796c8dcSSimon Schubert }
14335796c8dcSSimon Schubert 
14345796c8dcSSimon Schubert static void
14355796c8dcSSimon Schubert objfile_alloc_data (struct objfile *objfile)
14365796c8dcSSimon Schubert {
14375796c8dcSSimon Schubert   gdb_assert (objfile->data == NULL);
14385796c8dcSSimon Schubert   objfile->num_data = objfile_data_registry.num_registrations;
14395796c8dcSSimon Schubert   objfile->data = XCALLOC (objfile->num_data, void *);
14405796c8dcSSimon Schubert }
14415796c8dcSSimon Schubert 
14425796c8dcSSimon Schubert static void
14435796c8dcSSimon Schubert objfile_free_data (struct objfile *objfile)
14445796c8dcSSimon Schubert {
14455796c8dcSSimon Schubert   gdb_assert (objfile->data != NULL);
14465796c8dcSSimon Schubert   clear_objfile_data (objfile);
14475796c8dcSSimon Schubert   xfree (objfile->data);
14485796c8dcSSimon Schubert   objfile->data = NULL;
14495796c8dcSSimon Schubert }
14505796c8dcSSimon Schubert 
14515796c8dcSSimon Schubert void
14525796c8dcSSimon Schubert clear_objfile_data (struct objfile *objfile)
14535796c8dcSSimon Schubert {
14545796c8dcSSimon Schubert   struct objfile_data_registration *registration;
14555796c8dcSSimon Schubert   int i;
14565796c8dcSSimon Schubert 
14575796c8dcSSimon Schubert   gdb_assert (objfile->data != NULL);
14585796c8dcSSimon Schubert 
14595796c8dcSSimon Schubert   /* Process all the save handlers.  */
14605796c8dcSSimon Schubert 
14615796c8dcSSimon Schubert   for (registration = objfile_data_registry.registrations, i = 0;
14625796c8dcSSimon Schubert        i < objfile->num_data;
14635796c8dcSSimon Schubert        registration = registration->next, i++)
14645796c8dcSSimon Schubert     if (objfile->data[i] != NULL && registration->data->save != NULL)
14655796c8dcSSimon Schubert       registration->data->save (objfile, objfile->data[i]);
14665796c8dcSSimon Schubert 
14675796c8dcSSimon Schubert   /* Now process all the free handlers.  */
14685796c8dcSSimon Schubert 
14695796c8dcSSimon Schubert   for (registration = objfile_data_registry.registrations, i = 0;
14705796c8dcSSimon Schubert        i < objfile->num_data;
14715796c8dcSSimon Schubert        registration = registration->next, i++)
14725796c8dcSSimon Schubert     if (objfile->data[i] != NULL && registration->data->free != NULL)
14735796c8dcSSimon Schubert       registration->data->free (objfile, objfile->data[i]);
14745796c8dcSSimon Schubert 
14755796c8dcSSimon Schubert   memset (objfile->data, 0, objfile->num_data * sizeof (void *));
14765796c8dcSSimon Schubert }
14775796c8dcSSimon Schubert 
14785796c8dcSSimon Schubert void
14795796c8dcSSimon Schubert set_objfile_data (struct objfile *objfile, const struct objfile_data *data,
14805796c8dcSSimon Schubert 		  void *value)
14815796c8dcSSimon Schubert {
14825796c8dcSSimon Schubert   gdb_assert (data->index < objfile->num_data);
14835796c8dcSSimon Schubert   objfile->data[data->index] = value;
14845796c8dcSSimon Schubert }
14855796c8dcSSimon Schubert 
14865796c8dcSSimon Schubert void *
14875796c8dcSSimon Schubert objfile_data (struct objfile *objfile, const struct objfile_data *data)
14885796c8dcSSimon Schubert {
14895796c8dcSSimon Schubert   gdb_assert (data->index < objfile->num_data);
14905796c8dcSSimon Schubert   return objfile->data[data->index];
14915796c8dcSSimon Schubert }
14925796c8dcSSimon Schubert 
14935796c8dcSSimon Schubert /* Set objfiles_changed_p so section map will be rebuilt next time it
14945796c8dcSSimon Schubert    is used.  Called by reread_symbols.  */
14955796c8dcSSimon Schubert 
14965796c8dcSSimon Schubert void
14975796c8dcSSimon Schubert objfiles_changed (void)
14985796c8dcSSimon Schubert {
1499cf7f2e2dSJohn Marino   /* Rebuild section map next time we need it.  */
1500cf7f2e2dSJohn Marino   get_objfile_pspace_data (current_program_space)->objfiles_changed_p = 1;
1501cf7f2e2dSJohn Marino }
1502cf7f2e2dSJohn Marino 
1503cf7f2e2dSJohn Marino /* Close ABFD, and warn if that fails.  */
1504cf7f2e2dSJohn Marino 
1505cf7f2e2dSJohn Marino int
1506cf7f2e2dSJohn Marino gdb_bfd_close_or_warn (struct bfd *abfd)
1507cf7f2e2dSJohn Marino {
1508cf7f2e2dSJohn Marino   int ret;
1509cf7f2e2dSJohn Marino   char *name = bfd_get_filename (abfd);
1510cf7f2e2dSJohn Marino 
1511cf7f2e2dSJohn Marino   ret = bfd_close (abfd);
1512cf7f2e2dSJohn Marino 
1513cf7f2e2dSJohn Marino   if (!ret)
1514cf7f2e2dSJohn Marino     warning (_("cannot close \"%s\": %s"),
1515cf7f2e2dSJohn Marino 	     name, bfd_errmsg (bfd_get_error ()));
1516cf7f2e2dSJohn Marino 
1517cf7f2e2dSJohn Marino   return ret;
15185796c8dcSSimon Schubert }
15195796c8dcSSimon Schubert 
15205796c8dcSSimon Schubert /* Add reference to ABFD.  Returns ABFD.  */
15215796c8dcSSimon Schubert struct bfd *
15225796c8dcSSimon Schubert gdb_bfd_ref (struct bfd *abfd)
15235796c8dcSSimon Schubert {
1524cf7f2e2dSJohn Marino   int *p_refcount;
1525cf7f2e2dSJohn Marino 
1526cf7f2e2dSJohn Marino   if (abfd == NULL)
1527cf7f2e2dSJohn Marino     return NULL;
1528cf7f2e2dSJohn Marino 
1529cf7f2e2dSJohn Marino   p_refcount = bfd_usrdata (abfd);
15305796c8dcSSimon Schubert 
15315796c8dcSSimon Schubert   if (p_refcount != NULL)
15325796c8dcSSimon Schubert     {
15335796c8dcSSimon Schubert       *p_refcount += 1;
15345796c8dcSSimon Schubert       return abfd;
15355796c8dcSSimon Schubert     }
15365796c8dcSSimon Schubert 
15375796c8dcSSimon Schubert   p_refcount = xmalloc (sizeof (*p_refcount));
15385796c8dcSSimon Schubert   *p_refcount = 1;
15395796c8dcSSimon Schubert   bfd_usrdata (abfd) = p_refcount;
15405796c8dcSSimon Schubert 
15415796c8dcSSimon Schubert   return abfd;
15425796c8dcSSimon Schubert }
15435796c8dcSSimon Schubert 
15445796c8dcSSimon Schubert /* Unreference and possibly close ABFD.  */
15455796c8dcSSimon Schubert void
15465796c8dcSSimon Schubert gdb_bfd_unref (struct bfd *abfd)
15475796c8dcSSimon Schubert {
15485796c8dcSSimon Schubert   int *p_refcount;
15495796c8dcSSimon Schubert   char *name;
15505796c8dcSSimon Schubert 
15515796c8dcSSimon Schubert   if (abfd == NULL)
15525796c8dcSSimon Schubert     return;
15535796c8dcSSimon Schubert 
15545796c8dcSSimon Schubert   p_refcount = bfd_usrdata (abfd);
15555796c8dcSSimon Schubert 
15565796c8dcSSimon Schubert   /* Valid range for p_refcount: a pointer to int counter, which has a
15575796c8dcSSimon Schubert      value of 1 (single owner) or 2 (shared).  */
15585796c8dcSSimon Schubert   gdb_assert (*p_refcount == 1 || *p_refcount == 2);
15595796c8dcSSimon Schubert 
15605796c8dcSSimon Schubert   *p_refcount -= 1;
15615796c8dcSSimon Schubert   if (*p_refcount > 0)
15625796c8dcSSimon Schubert     return;
15635796c8dcSSimon Schubert 
15645796c8dcSSimon Schubert   xfree (p_refcount);
15655796c8dcSSimon Schubert   bfd_usrdata (abfd) = NULL;  /* Paranoia.  */
15665796c8dcSSimon Schubert 
15675796c8dcSSimon Schubert   name = bfd_get_filename (abfd);
1568cf7f2e2dSJohn Marino   gdb_bfd_close_or_warn (abfd);
15695796c8dcSSimon Schubert   xfree (name);
15705796c8dcSSimon Schubert }
1571cf7f2e2dSJohn Marino 
1572cf7f2e2dSJohn Marino /* Provide a prototype to silence -Wmissing-prototypes.  */
1573cf7f2e2dSJohn Marino extern initialize_file_ftype _initialize_objfiles;
1574cf7f2e2dSJohn Marino 
1575cf7f2e2dSJohn Marino void
1576cf7f2e2dSJohn Marino _initialize_objfiles (void)
1577cf7f2e2dSJohn Marino {
1578cf7f2e2dSJohn Marino   objfiles_pspace_data
1579cf7f2e2dSJohn Marino     = register_program_space_data_with_cleanup (objfiles_pspace_data_cleanup);
1580cf7f2e2dSJohn Marino }
1581