xref: /dflybsd-src/contrib/gdb-7/gdb/objfiles.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* GDB routines for manipulating objfiles.
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 1992-2004, 2007-2012 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Contributed by Cygnus Support, using pieces from other GDB modules.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert /* This file contains support routines for creating, manipulating, and
235796c8dcSSimon Schubert    destroying objfile structures.  */
245796c8dcSSimon Schubert 
255796c8dcSSimon Schubert #include "defs.h"
265796c8dcSSimon Schubert #include "bfd.h"		/* Binary File Description */
275796c8dcSSimon Schubert #include "symtab.h"
285796c8dcSSimon Schubert #include "symfile.h"
295796c8dcSSimon Schubert #include "objfiles.h"
305796c8dcSSimon Schubert #include "gdb-stabs.h"
315796c8dcSSimon Schubert #include "target.h"
325796c8dcSSimon Schubert #include "bcache.h"
335796c8dcSSimon Schubert #include "mdebugread.h"
345796c8dcSSimon Schubert #include "expression.h"
355796c8dcSSimon Schubert #include "parser-defs.h"
365796c8dcSSimon Schubert 
375796c8dcSSimon Schubert #include "gdb_assert.h"
385796c8dcSSimon Schubert #include <sys/types.h>
395796c8dcSSimon Schubert #include "gdb_stat.h"
405796c8dcSSimon Schubert #include <fcntl.h>
415796c8dcSSimon Schubert #include "gdb_obstack.h"
425796c8dcSSimon Schubert #include "gdb_string.h"
435796c8dcSSimon Schubert #include "hashtab.h"
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert #include "breakpoint.h"
465796c8dcSSimon Schubert #include "block.h"
475796c8dcSSimon Schubert #include "dictionary.h"
485796c8dcSSimon Schubert #include "source.h"
495796c8dcSSimon Schubert #include "addrmap.h"
505796c8dcSSimon Schubert #include "arch-utils.h"
515796c8dcSSimon Schubert #include "exec.h"
525796c8dcSSimon Schubert #include "observer.h"
535796c8dcSSimon Schubert #include "complaints.h"
54cf7f2e2dSJohn Marino #include "psymtab.h"
55cf7f2e2dSJohn Marino #include "solist.h"
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert /* Prototypes for local functions */
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert static void objfile_alloc_data (struct objfile *objfile);
605796c8dcSSimon Schubert static void objfile_free_data (struct objfile *objfile);
615796c8dcSSimon Schubert 
625796c8dcSSimon Schubert /* Externally visible variables that are owned by this module.
635796c8dcSSimon Schubert    See declarations in objfile.h for more info.  */
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert struct objfile *rt_common_objfile;	/* For runtime common symbols */
665796c8dcSSimon Schubert 
67cf7f2e2dSJohn Marino struct objfile_pspace_info
68cf7f2e2dSJohn Marino {
69cf7f2e2dSJohn Marino   int objfiles_changed_p;
70cf7f2e2dSJohn Marino   struct obj_section **sections;
71cf7f2e2dSJohn Marino   int num_sections;
72cf7f2e2dSJohn Marino };
73cf7f2e2dSJohn Marino 
74cf7f2e2dSJohn Marino /* Per-program-space data key.  */
75cf7f2e2dSJohn Marino static const struct program_space_data *objfiles_pspace_data;
76cf7f2e2dSJohn Marino 
77cf7f2e2dSJohn Marino static void
78cf7f2e2dSJohn Marino objfiles_pspace_data_cleanup (struct program_space *pspace, void *arg)
79cf7f2e2dSJohn Marino {
80cf7f2e2dSJohn Marino   struct objfile_pspace_info *info;
81cf7f2e2dSJohn Marino 
82cf7f2e2dSJohn Marino   info = program_space_data (pspace, objfiles_pspace_data);
83cf7f2e2dSJohn Marino   if (info != NULL)
84cf7f2e2dSJohn Marino     {
85cf7f2e2dSJohn Marino       xfree (info->sections);
86cf7f2e2dSJohn Marino       xfree (info);
87cf7f2e2dSJohn Marino     }
88cf7f2e2dSJohn Marino }
89cf7f2e2dSJohn Marino 
90cf7f2e2dSJohn Marino /* Get the current svr4 data.  If none is found yet, add it now.  This
91cf7f2e2dSJohn Marino    function always returns a valid object.  */
92cf7f2e2dSJohn Marino 
93cf7f2e2dSJohn Marino static struct objfile_pspace_info *
94cf7f2e2dSJohn Marino get_objfile_pspace_data (struct program_space *pspace)
95cf7f2e2dSJohn Marino {
96cf7f2e2dSJohn Marino   struct objfile_pspace_info *info;
97cf7f2e2dSJohn Marino 
98cf7f2e2dSJohn Marino   info = program_space_data (pspace, objfiles_pspace_data);
99cf7f2e2dSJohn Marino   if (info == NULL)
100cf7f2e2dSJohn Marino     {
101cf7f2e2dSJohn Marino       info = XZALLOC (struct objfile_pspace_info);
102cf7f2e2dSJohn Marino       set_program_space_data (pspace, objfiles_pspace_data, info);
103cf7f2e2dSJohn Marino     }
104cf7f2e2dSJohn Marino 
105cf7f2e2dSJohn Marino   return info;
106cf7f2e2dSJohn Marino }
107cf7f2e2dSJohn Marino 
1085796c8dcSSimon Schubert /* Records whether any objfiles appeared or disappeared since we last updated
1095796c8dcSSimon Schubert    address to obj section map.  */
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert /* Locate all mappable sections of a BFD file.
1125796c8dcSSimon Schubert    objfile_p_char is a char * to get it through
1135796c8dcSSimon Schubert    bfd_map_over_sections; we cast it back to its proper type.  */
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert /* Called via bfd_map_over_sections to build up the section table that
1165796c8dcSSimon Schubert    the objfile references.  The objfile contains pointers to the start
1175796c8dcSSimon Schubert    of the table (objfile->sections) and to the first location after
1185796c8dcSSimon Schubert    the end of the table (objfile->sections_end).  */
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert static void
1215796c8dcSSimon Schubert add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
1225796c8dcSSimon Schubert 			 void *objfile_p_char)
1235796c8dcSSimon Schubert {
1245796c8dcSSimon Schubert   struct objfile *objfile = (struct objfile *) objfile_p_char;
1255796c8dcSSimon Schubert   struct obj_section section;
1265796c8dcSSimon Schubert   flagword aflag;
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert   aflag = bfd_get_section_flags (abfd, asect);
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert   if (!(aflag & SEC_ALLOC))
1315796c8dcSSimon Schubert     return;
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert   if (0 == bfd_section_size (abfd, asect))
1345796c8dcSSimon Schubert     return;
1355796c8dcSSimon Schubert   section.objfile = objfile;
1365796c8dcSSimon Schubert   section.the_bfd_section = asect;
1375796c8dcSSimon Schubert   section.ovly_mapped = 0;
138c50c785cSJohn Marino   obstack_grow (&objfile->objfile_obstack,
139c50c785cSJohn Marino 		(char *) &section, sizeof (section));
1405796c8dcSSimon Schubert   objfile->sections_end
1415796c8dcSSimon Schubert     = (struct obj_section *) (((size_t) objfile->sections_end) + 1);
1425796c8dcSSimon Schubert }
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert /* Builds a section table for OBJFILE.
1455796c8dcSSimon Schubert    Returns 0 if OK, 1 on error (in which case bfd_error contains the
1465796c8dcSSimon Schubert    error).
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert    Note that while we are building the table, which goes into the
1495796c8dcSSimon Schubert    psymbol obstack, we hijack the sections_end pointer to instead hold
1505796c8dcSSimon Schubert    a count of the number of sections.  When bfd_map_over_sections
1515796c8dcSSimon Schubert    returns, this count is used to compute the pointer to the end of
1525796c8dcSSimon Schubert    the sections table, which then overwrites the count.
1535796c8dcSSimon Schubert 
1545796c8dcSSimon Schubert    Also note that the OFFSET and OVLY_MAPPED in each table entry
1555796c8dcSSimon Schubert    are initialized to zero.
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert    Also note that if anything else writes to the psymbol obstack while
1585796c8dcSSimon Schubert    we are building the table, we're pretty much hosed.  */
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert int
1615796c8dcSSimon Schubert build_objfile_section_table (struct objfile *objfile)
1625796c8dcSSimon Schubert {
1635796c8dcSSimon Schubert   objfile->sections_end = 0;
1645796c8dcSSimon Schubert   bfd_map_over_sections (objfile->obfd,
1655796c8dcSSimon Schubert 			 add_to_objfile_sections, (void *) objfile);
1665796c8dcSSimon Schubert   objfile->sections = obstack_finish (&objfile->objfile_obstack);
1675796c8dcSSimon Schubert   objfile->sections_end = objfile->sections + (size_t) objfile->sections_end;
1685796c8dcSSimon Schubert   return (0);
1695796c8dcSSimon Schubert }
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert /* Given a pointer to an initialized bfd (ABFD) and some flag bits
1725796c8dcSSimon Schubert    allocate a new objfile struct, fill it in as best we can, link it
1735796c8dcSSimon Schubert    into the list of all known objfiles, and return a pointer to the
1745796c8dcSSimon Schubert    new objfile struct.
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert    The FLAGS word contains various bits (OBJF_*) that can be taken as
1775796c8dcSSimon Schubert    requests for specific operations.  Other bits like OBJF_SHARED are
1785796c8dcSSimon Schubert    simply copied through to the new objfile flags member.  */
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0
1815796c8dcSSimon Schubert    by jv-lang.c, to create an artificial objfile used to hold
1825796c8dcSSimon Schubert    information about dynamically-loaded Java classes.  Unfortunately,
1835796c8dcSSimon Schubert    that branch of this function doesn't get tested very frequently, so
1845796c8dcSSimon Schubert    it's prone to breakage.  (E.g. at one time the name was set to NULL
1855796c8dcSSimon Schubert    in that situation, which broke a loop over all names in the dynamic
1865796c8dcSSimon Schubert    library loader.)  If you change this function, please try to leave
1875796c8dcSSimon Schubert    things in a consistent state even if abfd is NULL.  */
1885796c8dcSSimon Schubert 
1895796c8dcSSimon Schubert struct objfile *
1905796c8dcSSimon Schubert allocate_objfile (bfd *abfd, int flags)
1915796c8dcSSimon Schubert {
192cf7f2e2dSJohn Marino   struct objfile *objfile;
1935796c8dcSSimon Schubert 
194cf7f2e2dSJohn Marino   objfile = (struct objfile *) xzalloc (sizeof (struct objfile));
195c50c785cSJohn Marino   objfile->psymbol_cache = psymbol_bcache_init ();
196c50c785cSJohn Marino   objfile->macro_cache = bcache_xmalloc (NULL, NULL);
197c50c785cSJohn Marino   objfile->filename_cache = bcache_xmalloc (NULL, NULL);
1985796c8dcSSimon Schubert   /* We could use obstack_specify_allocation here instead, but
1995796c8dcSSimon Schubert      gdb_obstack.h specifies the alloc/dealloc functions.  */
2005796c8dcSSimon Schubert   obstack_init (&objfile->objfile_obstack);
2015796c8dcSSimon Schubert   terminate_minimal_symbol_table (objfile);
2025796c8dcSSimon Schubert 
2035796c8dcSSimon Schubert   objfile_alloc_data (objfile);
2045796c8dcSSimon Schubert 
2055796c8dcSSimon Schubert   /* Update the per-objfile information that comes from the bfd, ensuring
2065796c8dcSSimon Schubert      that any data that is reference is saved in the per-objfile data
2075796c8dcSSimon Schubert      region.  */
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert   objfile->obfd = gdb_bfd_ref (abfd);
2105796c8dcSSimon Schubert   if (abfd != NULL)
2115796c8dcSSimon Schubert     {
2125796c8dcSSimon Schubert       /* Look up the gdbarch associated with the BFD.  */
2135796c8dcSSimon Schubert       objfile->gdbarch = gdbarch_from_bfd (abfd);
2145796c8dcSSimon Schubert 
2155796c8dcSSimon Schubert       objfile->name = xstrdup (bfd_get_filename (abfd));
2165796c8dcSSimon Schubert       objfile->mtime = bfd_get_mtime (abfd);
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert       /* Build section table.  */
2195796c8dcSSimon Schubert 
2205796c8dcSSimon Schubert       if (build_objfile_section_table (objfile))
2215796c8dcSSimon Schubert 	{
2225796c8dcSSimon Schubert 	  error (_("Can't find the file sections in `%s': %s"),
2235796c8dcSSimon Schubert 		 objfile->name, bfd_errmsg (bfd_get_error ()));
2245796c8dcSSimon Schubert 	}
2255796c8dcSSimon Schubert     }
2265796c8dcSSimon Schubert   else
2275796c8dcSSimon Schubert     {
2285796c8dcSSimon Schubert       objfile->name = xstrdup ("<<anonymous objfile>>");
2295796c8dcSSimon Schubert     }
2305796c8dcSSimon Schubert 
231cf7f2e2dSJohn Marino   objfile->pspace = current_program_space;
232cf7f2e2dSJohn Marino 
2335796c8dcSSimon Schubert   /* Initialize the section indexes for this objfile, so that we can
2345796c8dcSSimon Schubert      later detect if they are used w/o being properly assigned to.  */
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert   objfile->sect_index_text = -1;
2375796c8dcSSimon Schubert   objfile->sect_index_data = -1;
2385796c8dcSSimon Schubert   objfile->sect_index_bss = -1;
2395796c8dcSSimon Schubert   objfile->sect_index_rodata = -1;
2405796c8dcSSimon Schubert 
2415796c8dcSSimon Schubert   /* Add this file onto the tail of the linked list of other such files.  */
2425796c8dcSSimon Schubert 
2435796c8dcSSimon Schubert   objfile->next = NULL;
2445796c8dcSSimon Schubert   if (object_files == NULL)
2455796c8dcSSimon Schubert     object_files = objfile;
2465796c8dcSSimon Schubert   else
2475796c8dcSSimon Schubert     {
248cf7f2e2dSJohn Marino       struct objfile *last_one;
249cf7f2e2dSJohn Marino 
2505796c8dcSSimon Schubert       for (last_one = object_files;
2515796c8dcSSimon Schubert 	   last_one->next;
2525796c8dcSSimon Schubert 	   last_one = last_one->next);
2535796c8dcSSimon Schubert       last_one->next = objfile;
2545796c8dcSSimon Schubert     }
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert   /* Save passed in flag bits.  */
2575796c8dcSSimon Schubert   objfile->flags |= flags;
2585796c8dcSSimon Schubert 
259cf7f2e2dSJohn Marino   /* Rebuild section map next time we need it.  */
260cf7f2e2dSJohn Marino   get_objfile_pspace_data (objfile->pspace)->objfiles_changed_p = 1;
2615796c8dcSSimon Schubert 
262cf7f2e2dSJohn Marino   return objfile;
2635796c8dcSSimon Schubert }
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert /* Retrieve the gdbarch associated with OBJFILE.  */
2665796c8dcSSimon Schubert struct gdbarch *
2675796c8dcSSimon Schubert get_objfile_arch (struct objfile *objfile)
2685796c8dcSSimon Schubert {
2695796c8dcSSimon Schubert   return objfile->gdbarch;
2705796c8dcSSimon Schubert }
2715796c8dcSSimon Schubert 
2725796c8dcSSimon Schubert /* Initialize entry point information for this objfile.  */
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert void
2755796c8dcSSimon Schubert init_entry_point_info (struct objfile *objfile)
2765796c8dcSSimon Schubert {
2775796c8dcSSimon Schubert   /* Save startup file's range of PC addresses to help blockframe.c
2785796c8dcSSimon Schubert      decide where the bottom of the stack is.  */
2795796c8dcSSimon Schubert 
2805796c8dcSSimon Schubert   if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
2815796c8dcSSimon Schubert     {
2825796c8dcSSimon Schubert       /* Executable file -- record its entry point so we'll recognize
2835796c8dcSSimon Schubert          the startup file because it contains the entry point.  */
2845796c8dcSSimon Schubert       objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
285cf7f2e2dSJohn Marino       objfile->ei.entry_point_p = 1;
2865796c8dcSSimon Schubert     }
2875796c8dcSSimon Schubert   else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
2885796c8dcSSimon Schubert 	   && bfd_get_start_address (objfile->obfd) != 0)
289cf7f2e2dSJohn Marino     {
2905796c8dcSSimon Schubert       /* Some shared libraries may have entry points set and be
2915796c8dcSSimon Schubert 	 runnable.  There's no clear way to indicate this, so just check
2925796c8dcSSimon Schubert 	 for values other than zero.  */
2935796c8dcSSimon Schubert       objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
294cf7f2e2dSJohn Marino       objfile->ei.entry_point_p = 1;
295cf7f2e2dSJohn Marino     }
2965796c8dcSSimon Schubert   else
2975796c8dcSSimon Schubert     {
2985796c8dcSSimon Schubert       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
299cf7f2e2dSJohn Marino       objfile->ei.entry_point_p = 0;
3005796c8dcSSimon Schubert     }
3015796c8dcSSimon Schubert }
3025796c8dcSSimon Schubert 
303cf7f2e2dSJohn Marino /* If there is a valid and known entry point, function fills *ENTRY_P with it
304cf7f2e2dSJohn Marino    and returns non-zero; otherwise it returns zero.  */
3055796c8dcSSimon Schubert 
306cf7f2e2dSJohn Marino int
307cf7f2e2dSJohn Marino entry_point_address_query (CORE_ADDR *entry_p)
3085796c8dcSSimon Schubert {
3095796c8dcSSimon Schubert   struct gdbarch *gdbarch;
3105796c8dcSSimon Schubert   CORE_ADDR entry_point;
3115796c8dcSSimon Schubert 
312cf7f2e2dSJohn Marino   if (symfile_objfile == NULL || !symfile_objfile->ei.entry_point_p)
3135796c8dcSSimon Schubert     return 0;
3145796c8dcSSimon Schubert 
3155796c8dcSSimon Schubert   gdbarch = get_objfile_arch (symfile_objfile);
3165796c8dcSSimon Schubert 
3175796c8dcSSimon Schubert   entry_point = symfile_objfile->ei.entry_point;
3185796c8dcSSimon Schubert 
3195796c8dcSSimon Schubert   /* Make certain that the address points at real code, and not a
3205796c8dcSSimon Schubert      function descriptor.  */
3215796c8dcSSimon Schubert   entry_point = gdbarch_convert_from_func_ptr_addr (gdbarch, entry_point,
3225796c8dcSSimon Schubert 						    &current_target);
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert   /* Remove any ISA markers, so that this matches entries in the
3255796c8dcSSimon Schubert      symbol table.  */
3265796c8dcSSimon Schubert   entry_point = gdbarch_addr_bits_remove (gdbarch, entry_point);
3275796c8dcSSimon Schubert 
328cf7f2e2dSJohn Marino   *entry_p = entry_point;
329cf7f2e2dSJohn Marino   return 1;
330cf7f2e2dSJohn Marino }
331cf7f2e2dSJohn Marino 
332cf7f2e2dSJohn Marino /* Get current entry point address.  Call error if it is not known.  */
333cf7f2e2dSJohn Marino 
334cf7f2e2dSJohn Marino CORE_ADDR
335cf7f2e2dSJohn Marino entry_point_address (void)
336cf7f2e2dSJohn Marino {
337cf7f2e2dSJohn Marino   CORE_ADDR retval;
338cf7f2e2dSJohn Marino 
339cf7f2e2dSJohn Marino   if (!entry_point_address_query (&retval))
340cf7f2e2dSJohn Marino     error (_("Entry point address is not known."));
341cf7f2e2dSJohn Marino 
342cf7f2e2dSJohn Marino   return retval;
3435796c8dcSSimon Schubert }
3445796c8dcSSimon Schubert 
3455796c8dcSSimon Schubert /* Create the terminating entry of OBJFILE's minimal symbol table.
3465796c8dcSSimon Schubert    If OBJFILE->msymbols is zero, allocate a single entry from
3475796c8dcSSimon Schubert    OBJFILE->objfile_obstack; otherwise, just initialize
3485796c8dcSSimon Schubert    OBJFILE->msymbols[OBJFILE->minimal_symbol_count].  */
3495796c8dcSSimon Schubert void
3505796c8dcSSimon Schubert terminate_minimal_symbol_table (struct objfile *objfile)
3515796c8dcSSimon Schubert {
3525796c8dcSSimon Schubert   if (! objfile->msymbols)
3535796c8dcSSimon Schubert     objfile->msymbols = ((struct minimal_symbol *)
3545796c8dcSSimon Schubert                          obstack_alloc (&objfile->objfile_obstack,
3555796c8dcSSimon Schubert                                         sizeof (objfile->msymbols[0])));
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert   {
3585796c8dcSSimon Schubert     struct minimal_symbol *m
3595796c8dcSSimon Schubert       = &objfile->msymbols[objfile->minimal_symbol_count];
3605796c8dcSSimon Schubert 
3615796c8dcSSimon Schubert     memset (m, 0, sizeof (*m));
3625796c8dcSSimon Schubert     /* Don't rely on these enumeration values being 0's.  */
3635796c8dcSSimon Schubert     MSYMBOL_TYPE (m) = mst_unknown;
364c50c785cSJohn Marino     SYMBOL_SET_LANGUAGE (m, language_unknown);
3655796c8dcSSimon Schubert   }
3665796c8dcSSimon Schubert }
3675796c8dcSSimon Schubert 
368cf7f2e2dSJohn Marino /* Iterator on PARENT and every separate debug objfile of PARENT.
369cf7f2e2dSJohn Marino    The usage pattern is:
370cf7f2e2dSJohn Marino      for (objfile = parent;
371cf7f2e2dSJohn Marino           objfile;
372cf7f2e2dSJohn Marino           objfile = objfile_separate_debug_iterate (parent, objfile))
373cf7f2e2dSJohn Marino        ...
374cf7f2e2dSJohn Marino */
375cf7f2e2dSJohn Marino 
376cf7f2e2dSJohn Marino struct objfile *
377cf7f2e2dSJohn Marino objfile_separate_debug_iterate (const struct objfile *parent,
378cf7f2e2dSJohn Marino                                 const struct objfile *objfile)
379cf7f2e2dSJohn Marino {
380cf7f2e2dSJohn Marino   struct objfile *res;
381cf7f2e2dSJohn Marino 
382cf7f2e2dSJohn Marino   /* If any, return the first child.  */
383cf7f2e2dSJohn Marino   res = objfile->separate_debug_objfile;
384cf7f2e2dSJohn Marino   if (res)
385cf7f2e2dSJohn Marino     return res;
386cf7f2e2dSJohn Marino 
387cf7f2e2dSJohn Marino   /* Common case where there is no separate debug objfile.  */
388cf7f2e2dSJohn Marino   if (objfile == parent)
389cf7f2e2dSJohn Marino     return NULL;
390cf7f2e2dSJohn Marino 
391cf7f2e2dSJohn Marino   /* Return the brother if any.  Note that we don't iterate on brothers of
392cf7f2e2dSJohn Marino      the parents.  */
393cf7f2e2dSJohn Marino   res = objfile->separate_debug_objfile_link;
394cf7f2e2dSJohn Marino   if (res)
395cf7f2e2dSJohn Marino     return res;
396cf7f2e2dSJohn Marino 
397cf7f2e2dSJohn Marino   for (res = objfile->separate_debug_objfile_backlink;
398cf7f2e2dSJohn Marino        res != parent;
399cf7f2e2dSJohn Marino        res = res->separate_debug_objfile_backlink)
400cf7f2e2dSJohn Marino     {
401cf7f2e2dSJohn Marino       gdb_assert (res != NULL);
402cf7f2e2dSJohn Marino       if (res->separate_debug_objfile_link)
403cf7f2e2dSJohn Marino         return res->separate_debug_objfile_link;
404cf7f2e2dSJohn Marino     }
405cf7f2e2dSJohn Marino   return NULL;
406cf7f2e2dSJohn Marino }
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert /* Put one object file before a specified on in the global list.
4095796c8dcSSimon Schubert    This can be used to make sure an object file is destroyed before
4105796c8dcSSimon Schubert    another when using ALL_OBJFILES_SAFE to free all objfiles.  */
4115796c8dcSSimon Schubert void
4125796c8dcSSimon Schubert put_objfile_before (struct objfile *objfile, struct objfile *before_this)
4135796c8dcSSimon Schubert {
4145796c8dcSSimon Schubert   struct objfile **objp;
4155796c8dcSSimon Schubert 
4165796c8dcSSimon Schubert   unlink_objfile (objfile);
4175796c8dcSSimon Schubert 
4185796c8dcSSimon Schubert   for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
4195796c8dcSSimon Schubert     {
4205796c8dcSSimon Schubert       if (*objp == before_this)
4215796c8dcSSimon Schubert 	{
4225796c8dcSSimon Schubert 	  objfile->next = *objp;
4235796c8dcSSimon Schubert 	  *objp = objfile;
4245796c8dcSSimon Schubert 	  return;
4255796c8dcSSimon Schubert 	}
4265796c8dcSSimon Schubert     }
4275796c8dcSSimon Schubert 
4285796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
4295796c8dcSSimon Schubert 		  _("put_objfile_before: before objfile not in list"));
4305796c8dcSSimon Schubert }
4315796c8dcSSimon Schubert 
4325796c8dcSSimon Schubert /* Put OBJFILE at the front of the list.  */
4335796c8dcSSimon Schubert 
4345796c8dcSSimon Schubert void
4355796c8dcSSimon Schubert objfile_to_front (struct objfile *objfile)
4365796c8dcSSimon Schubert {
4375796c8dcSSimon Schubert   struct objfile **objp;
4385796c8dcSSimon Schubert   for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
4395796c8dcSSimon Schubert     {
4405796c8dcSSimon Schubert       if (*objp == objfile)
4415796c8dcSSimon Schubert 	{
4425796c8dcSSimon Schubert 	  /* Unhook it from where it is.  */
4435796c8dcSSimon Schubert 	  *objp = objfile->next;
4445796c8dcSSimon Schubert 	  /* Put it in the front.  */
4455796c8dcSSimon Schubert 	  objfile->next = object_files;
4465796c8dcSSimon Schubert 	  object_files = objfile;
4475796c8dcSSimon Schubert 	  break;
4485796c8dcSSimon Schubert 	}
4495796c8dcSSimon Schubert     }
4505796c8dcSSimon Schubert }
4515796c8dcSSimon Schubert 
4525796c8dcSSimon Schubert /* Unlink OBJFILE from the list of known objfiles, if it is found in the
4535796c8dcSSimon Schubert    list.
4545796c8dcSSimon Schubert 
4555796c8dcSSimon Schubert    It is not a bug, or error, to call this function if OBJFILE is not known
4565796c8dcSSimon Schubert    to be in the current list.  This is done in the case of mapped objfiles,
4575796c8dcSSimon Schubert    for example, just to ensure that the mapped objfile doesn't appear twice
4585796c8dcSSimon Schubert    in the list.  Since the list is threaded, linking in a mapped objfile
4595796c8dcSSimon Schubert    twice would create a circular list.
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert    If OBJFILE turns out to be in the list, we zap it's NEXT pointer after
4625796c8dcSSimon Schubert    unlinking it, just to ensure that we have completely severed any linkages
4635796c8dcSSimon Schubert    between the OBJFILE and the list.  */
4645796c8dcSSimon Schubert 
4655796c8dcSSimon Schubert void
4665796c8dcSSimon Schubert unlink_objfile (struct objfile *objfile)
4675796c8dcSSimon Schubert {
4685796c8dcSSimon Schubert   struct objfile **objpp;
4695796c8dcSSimon Schubert 
4705796c8dcSSimon Schubert   for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next))
4715796c8dcSSimon Schubert     {
4725796c8dcSSimon Schubert       if (*objpp == objfile)
4735796c8dcSSimon Schubert 	{
4745796c8dcSSimon Schubert 	  *objpp = (*objpp)->next;
4755796c8dcSSimon Schubert 	  objfile->next = NULL;
4765796c8dcSSimon Schubert 	  return;
4775796c8dcSSimon Schubert 	}
4785796c8dcSSimon Schubert     }
4795796c8dcSSimon Schubert 
4805796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
4815796c8dcSSimon Schubert 		  _("unlink_objfile: objfile already unlinked"));
4825796c8dcSSimon Schubert }
4835796c8dcSSimon Schubert 
484cf7f2e2dSJohn Marino /* Add OBJFILE as a separate debug objfile of PARENT.  */
485cf7f2e2dSJohn Marino 
486cf7f2e2dSJohn Marino void
487cf7f2e2dSJohn Marino add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
488cf7f2e2dSJohn Marino {
489cf7f2e2dSJohn Marino   gdb_assert (objfile && parent);
490cf7f2e2dSJohn Marino 
491cf7f2e2dSJohn Marino   /* Must not be already in a list.  */
492cf7f2e2dSJohn Marino   gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
493cf7f2e2dSJohn Marino   gdb_assert (objfile->separate_debug_objfile_link == NULL);
494cf7f2e2dSJohn Marino 
495cf7f2e2dSJohn Marino   objfile->separate_debug_objfile_backlink = parent;
496cf7f2e2dSJohn Marino   objfile->separate_debug_objfile_link = parent->separate_debug_objfile;
497cf7f2e2dSJohn Marino   parent->separate_debug_objfile = objfile;
498cf7f2e2dSJohn Marino 
499cf7f2e2dSJohn Marino   /* Put the separate debug object before the normal one, this is so that
500cf7f2e2dSJohn Marino      usage of the ALL_OBJFILES_SAFE macro will stay safe.  */
501cf7f2e2dSJohn Marino   put_objfile_before (objfile, parent);
502cf7f2e2dSJohn Marino }
503cf7f2e2dSJohn Marino 
504cf7f2e2dSJohn Marino /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
505cf7f2e2dSJohn Marino    itself.  */
506cf7f2e2dSJohn Marino 
507cf7f2e2dSJohn Marino void
508cf7f2e2dSJohn Marino free_objfile_separate_debug (struct objfile *objfile)
509cf7f2e2dSJohn Marino {
510cf7f2e2dSJohn Marino   struct objfile *child;
511cf7f2e2dSJohn Marino 
512cf7f2e2dSJohn Marino   for (child = objfile->separate_debug_objfile; child;)
513cf7f2e2dSJohn Marino     {
514cf7f2e2dSJohn Marino       struct objfile *next_child = child->separate_debug_objfile_link;
515cf7f2e2dSJohn Marino       free_objfile (child);
516cf7f2e2dSJohn Marino       child = next_child;
517cf7f2e2dSJohn Marino     }
518cf7f2e2dSJohn Marino }
5195796c8dcSSimon Schubert 
5205796c8dcSSimon Schubert /* Destroy an objfile and all the symtabs and psymtabs under it.  Note
5215796c8dcSSimon Schubert    that as much as possible is allocated on the objfile_obstack
5225796c8dcSSimon Schubert    so that the memory can be efficiently freed.
5235796c8dcSSimon Schubert 
5245796c8dcSSimon Schubert    Things which we do NOT free because they are not in malloc'd memory
5255796c8dcSSimon Schubert    or not in memory specific to the objfile include:
5265796c8dcSSimon Schubert 
5275796c8dcSSimon Schubert    objfile -> sf
5285796c8dcSSimon Schubert 
5295796c8dcSSimon Schubert    FIXME:  If the objfile is using reusable symbol information (via mmalloc),
5305796c8dcSSimon Schubert    then we need to take into account the fact that more than one process
5315796c8dcSSimon Schubert    may be using the symbol information at the same time (when mmalloc is
5325796c8dcSSimon Schubert    extended to support cooperative locking).  When more than one process
5335796c8dcSSimon Schubert    is using the mapped symbol info, we need to be more careful about when
5345796c8dcSSimon Schubert    we free objects in the reusable area.  */
5355796c8dcSSimon Schubert 
5365796c8dcSSimon Schubert void
5375796c8dcSSimon Schubert free_objfile (struct objfile *objfile)
5385796c8dcSSimon Schubert {
539cf7f2e2dSJohn Marino   /* Free all separate debug objfiles.  */
540cf7f2e2dSJohn Marino   free_objfile_separate_debug (objfile);
5415796c8dcSSimon Schubert 
5425796c8dcSSimon Schubert   if (objfile->separate_debug_objfile_backlink)
5435796c8dcSSimon Schubert     {
5445796c8dcSSimon Schubert       /* We freed the separate debug file, make sure the base objfile
5455796c8dcSSimon Schubert 	 doesn't reference it.  */
546cf7f2e2dSJohn Marino       struct objfile *child;
547cf7f2e2dSJohn Marino 
548cf7f2e2dSJohn Marino       child = objfile->separate_debug_objfile_backlink->separate_debug_objfile;
549cf7f2e2dSJohn Marino 
550cf7f2e2dSJohn Marino       if (child == objfile)
551cf7f2e2dSJohn Marino         {
552cf7f2e2dSJohn Marino           /* OBJFILE is the first child.  */
553cf7f2e2dSJohn Marino           objfile->separate_debug_objfile_backlink->separate_debug_objfile =
554cf7f2e2dSJohn Marino             objfile->separate_debug_objfile_link;
555cf7f2e2dSJohn Marino         }
556cf7f2e2dSJohn Marino       else
557cf7f2e2dSJohn Marino         {
558cf7f2e2dSJohn Marino           /* Find OBJFILE in the list.  */
559cf7f2e2dSJohn Marino           while (1)
560cf7f2e2dSJohn Marino             {
561cf7f2e2dSJohn Marino               if (child->separate_debug_objfile_link == objfile)
562cf7f2e2dSJohn Marino                 {
563cf7f2e2dSJohn Marino                   child->separate_debug_objfile_link =
564cf7f2e2dSJohn Marino                     objfile->separate_debug_objfile_link;
565cf7f2e2dSJohn Marino                   break;
566cf7f2e2dSJohn Marino                 }
567cf7f2e2dSJohn Marino               child = child->separate_debug_objfile_link;
568cf7f2e2dSJohn Marino               gdb_assert (child);
569cf7f2e2dSJohn Marino             }
570cf7f2e2dSJohn Marino         }
5715796c8dcSSimon Schubert     }
5725796c8dcSSimon Schubert 
5735796c8dcSSimon Schubert   /* Remove any references to this objfile in the global value
5745796c8dcSSimon Schubert      lists.  */
5755796c8dcSSimon Schubert   preserve_values (objfile);
5765796c8dcSSimon Schubert 
577*a45ae5f8SJohn Marino   /* It still may reference data modules have associated with the objfile and
578*a45ae5f8SJohn Marino      the symbol file data.  */
579*a45ae5f8SJohn Marino   forget_cached_source_info_for_objfile (objfile);
580*a45ae5f8SJohn Marino 
5815796c8dcSSimon Schubert   /* First do any symbol file specific actions required when we are
5825796c8dcSSimon Schubert      finished with a particular symbol file.  Note that if the objfile
5835796c8dcSSimon Schubert      is using reusable symbol information (via mmalloc) then each of
5845796c8dcSSimon Schubert      these routines is responsible for doing the correct thing, either
5855796c8dcSSimon Schubert      freeing things which are valid only during this particular gdb
5865796c8dcSSimon Schubert      execution, or leaving them to be reused during the next one.  */
5875796c8dcSSimon Schubert 
5885796c8dcSSimon Schubert   if (objfile->sf != NULL)
5895796c8dcSSimon Schubert     {
5905796c8dcSSimon Schubert       (*objfile->sf->sym_finish) (objfile);
5915796c8dcSSimon Schubert     }
5925796c8dcSSimon Schubert 
593*a45ae5f8SJohn Marino   /* Discard any data modules have associated with the objfile.  The function
594*a45ae5f8SJohn Marino      still may reference objfile->obfd.  */
5955796c8dcSSimon Schubert   objfile_free_data (objfile);
5965796c8dcSSimon Schubert 
5975796c8dcSSimon Schubert   gdb_bfd_unref (objfile->obfd);
5985796c8dcSSimon Schubert 
5995796c8dcSSimon Schubert   /* Remove it from the chain of all objfiles.  */
6005796c8dcSSimon Schubert 
6015796c8dcSSimon Schubert   unlink_objfile (objfile);
6025796c8dcSSimon Schubert 
6035796c8dcSSimon Schubert   if (objfile == symfile_objfile)
6045796c8dcSSimon Schubert     symfile_objfile = NULL;
6055796c8dcSSimon Schubert 
6065796c8dcSSimon Schubert   if (objfile == rt_common_objfile)
6075796c8dcSSimon Schubert     rt_common_objfile = NULL;
6085796c8dcSSimon Schubert 
6095796c8dcSSimon Schubert   /* Before the symbol table code was redone to make it easier to
6105796c8dcSSimon Schubert      selectively load and remove information particular to a specific
6115796c8dcSSimon Schubert      linkage unit, gdb used to do these things whenever the monolithic
6125796c8dcSSimon Schubert      symbol table was blown away.  How much still needs to be done
6135796c8dcSSimon Schubert      is unknown, but we play it safe for now and keep each action until
6145796c8dcSSimon Schubert      it is shown to be no longer needed.  */
6155796c8dcSSimon Schubert 
6165796c8dcSSimon Schubert   /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
6175796c8dcSSimon Schubert      for example), so we need to call this here.  */
6185796c8dcSSimon Schubert   clear_pc_function_cache ();
6195796c8dcSSimon Schubert 
6205796c8dcSSimon Schubert   /* Clear globals which might have pointed into a removed objfile.
6215796c8dcSSimon Schubert      FIXME: It's not clear which of these are supposed to persist
6225796c8dcSSimon Schubert      between expressions and which ought to be reset each time.  */
6235796c8dcSSimon Schubert   expression_context_block = NULL;
6245796c8dcSSimon Schubert   innermost_block = NULL;
6255796c8dcSSimon Schubert 
6265796c8dcSSimon Schubert   /* Check to see if the current_source_symtab belongs to this objfile,
6275796c8dcSSimon Schubert      and if so, call clear_current_source_symtab_and_line.  */
6285796c8dcSSimon Schubert 
6295796c8dcSSimon Schubert   {
6305796c8dcSSimon Schubert     struct symtab_and_line cursal = get_current_source_symtab_and_line ();
6315796c8dcSSimon Schubert 
632*a45ae5f8SJohn Marino     if (cursal.symtab && cursal.symtab->objfile == objfile)
6335796c8dcSSimon Schubert       clear_current_source_symtab_and_line ();
6345796c8dcSSimon Schubert   }
6355796c8dcSSimon Schubert 
6365796c8dcSSimon Schubert   /* The last thing we do is free the objfile struct itself.  */
6375796c8dcSSimon Schubert 
6385796c8dcSSimon Schubert   xfree (objfile->name);
6395796c8dcSSimon Schubert   if (objfile->global_psymbols.list)
6405796c8dcSSimon Schubert     xfree (objfile->global_psymbols.list);
6415796c8dcSSimon Schubert   if (objfile->static_psymbols.list)
6425796c8dcSSimon Schubert     xfree (objfile->static_psymbols.list);
643c50c785cSJohn Marino   /* Free the obstacks for non-reusable objfiles.  */
644c50c785cSJohn Marino   psymbol_bcache_free (objfile->psymbol_cache);
6455796c8dcSSimon Schubert   bcache_xfree (objfile->macro_cache);
646cf7f2e2dSJohn Marino   bcache_xfree (objfile->filename_cache);
6475796c8dcSSimon Schubert   if (objfile->demangled_names_hash)
6485796c8dcSSimon Schubert     htab_delete (objfile->demangled_names_hash);
6495796c8dcSSimon Schubert   obstack_free (&objfile->objfile_obstack, 0);
650cf7f2e2dSJohn Marino 
651cf7f2e2dSJohn Marino   /* Rebuild section map next time we need it.  */
652cf7f2e2dSJohn Marino   get_objfile_pspace_data (objfile->pspace)->objfiles_changed_p = 1;
653cf7f2e2dSJohn Marino 
6545796c8dcSSimon Schubert   xfree (objfile);
6555796c8dcSSimon Schubert }
6565796c8dcSSimon Schubert 
6575796c8dcSSimon Schubert static void
6585796c8dcSSimon Schubert do_free_objfile_cleanup (void *obj)
6595796c8dcSSimon Schubert {
6605796c8dcSSimon Schubert   free_objfile (obj);
6615796c8dcSSimon Schubert }
6625796c8dcSSimon Schubert 
6635796c8dcSSimon Schubert struct cleanup *
6645796c8dcSSimon Schubert make_cleanup_free_objfile (struct objfile *obj)
6655796c8dcSSimon Schubert {
6665796c8dcSSimon Schubert   return make_cleanup (do_free_objfile_cleanup, obj);
6675796c8dcSSimon Schubert }
6685796c8dcSSimon Schubert 
6695796c8dcSSimon Schubert /* Free all the object files at once and clean up their users.  */
6705796c8dcSSimon Schubert 
6715796c8dcSSimon Schubert void
6725796c8dcSSimon Schubert free_all_objfiles (void)
6735796c8dcSSimon Schubert {
6745796c8dcSSimon Schubert   struct objfile *objfile, *temp;
675cf7f2e2dSJohn Marino   struct so_list *so;
676cf7f2e2dSJohn Marino 
677cf7f2e2dSJohn Marino   /* Any objfile referencewould become stale.  */
678cf7f2e2dSJohn Marino   for (so = master_so_list (); so; so = so->next)
679cf7f2e2dSJohn Marino     gdb_assert (so->objfile == NULL);
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert   ALL_OBJFILES_SAFE (objfile, temp)
6825796c8dcSSimon Schubert   {
6835796c8dcSSimon Schubert     free_objfile (objfile);
6845796c8dcSSimon Schubert   }
685c50c785cSJohn Marino   clear_symtab_users (0);
6865796c8dcSSimon Schubert }
6875796c8dcSSimon Schubert 
688c50c785cSJohn Marino /* A helper function for objfile_relocate1 that relocates a single
689c50c785cSJohn Marino    symbol.  */
690c50c785cSJohn Marino 
691c50c785cSJohn Marino static void
692c50c785cSJohn Marino relocate_one_symbol (struct symbol *sym, struct objfile *objfile,
693c50c785cSJohn Marino 		     struct section_offsets *delta)
694c50c785cSJohn Marino {
695c50c785cSJohn Marino   fixup_symbol_section (sym, objfile);
696c50c785cSJohn Marino 
697c50c785cSJohn Marino   /* The RS6000 code from which this was taken skipped
698c50c785cSJohn Marino      any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
699c50c785cSJohn Marino      But I'm leaving out that test, on the theory that
700c50c785cSJohn Marino      they can't possibly pass the tests below.  */
701c50c785cSJohn Marino   if ((SYMBOL_CLASS (sym) == LOC_LABEL
702c50c785cSJohn Marino        || SYMBOL_CLASS (sym) == LOC_STATIC)
703c50c785cSJohn Marino       && SYMBOL_SECTION (sym) >= 0)
704c50c785cSJohn Marino     {
705c50c785cSJohn Marino       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (delta, SYMBOL_SECTION (sym));
706c50c785cSJohn Marino     }
707c50c785cSJohn Marino }
708c50c785cSJohn Marino 
7095796c8dcSSimon Schubert /* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
710cf7f2e2dSJohn Marino    entries in new_offsets.  SEPARATE_DEBUG_OBJFILE is not touched here.
711cf7f2e2dSJohn Marino    Return non-zero iff any change happened.  */
712cf7f2e2dSJohn Marino 
713cf7f2e2dSJohn Marino static int
714cf7f2e2dSJohn Marino objfile_relocate1 (struct objfile *objfile,
715cf7f2e2dSJohn Marino 		   struct section_offsets *new_offsets)
7165796c8dcSSimon Schubert {
7175796c8dcSSimon Schubert   struct obj_section *s;
7185796c8dcSSimon Schubert   struct section_offsets *delta =
7195796c8dcSSimon Schubert     ((struct section_offsets *)
7205796c8dcSSimon Schubert      alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
7215796c8dcSSimon Schubert 
7225796c8dcSSimon Schubert   int i;
7235796c8dcSSimon Schubert   int something_changed = 0;
724cf7f2e2dSJohn Marino 
7255796c8dcSSimon Schubert   for (i = 0; i < objfile->num_sections; ++i)
7265796c8dcSSimon Schubert     {
7275796c8dcSSimon Schubert       delta->offsets[i] =
7285796c8dcSSimon Schubert 	ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i);
7295796c8dcSSimon Schubert       if (ANOFFSET (delta, i) != 0)
7305796c8dcSSimon Schubert 	something_changed = 1;
7315796c8dcSSimon Schubert     }
7325796c8dcSSimon Schubert   if (!something_changed)
733cf7f2e2dSJohn Marino     return 0;
7345796c8dcSSimon Schubert 
7355796c8dcSSimon Schubert   /* OK, get all the symtabs.  */
7365796c8dcSSimon Schubert   {
7375796c8dcSSimon Schubert     struct symtab *s;
7385796c8dcSSimon Schubert 
7395796c8dcSSimon Schubert     ALL_OBJFILE_SYMTABS (objfile, s)
7405796c8dcSSimon Schubert     {
7415796c8dcSSimon Schubert       struct linetable *l;
7425796c8dcSSimon Schubert       struct blockvector *bv;
7435796c8dcSSimon Schubert       int i;
7445796c8dcSSimon Schubert 
7455796c8dcSSimon Schubert       /* First the line table.  */
7465796c8dcSSimon Schubert       l = LINETABLE (s);
7475796c8dcSSimon Schubert       if (l)
7485796c8dcSSimon Schubert 	{
7495796c8dcSSimon Schubert 	  for (i = 0; i < l->nitems; ++i)
7505796c8dcSSimon Schubert 	    l->item[i].pc += ANOFFSET (delta, s->block_line_section);
7515796c8dcSSimon Schubert 	}
7525796c8dcSSimon Schubert 
7535796c8dcSSimon Schubert       /* Don't relocate a shared blockvector more than once.  */
7545796c8dcSSimon Schubert       if (!s->primary)
7555796c8dcSSimon Schubert 	continue;
7565796c8dcSSimon Schubert 
7575796c8dcSSimon Schubert       bv = BLOCKVECTOR (s);
7585796c8dcSSimon Schubert       if (BLOCKVECTOR_MAP (bv))
7595796c8dcSSimon Schubert 	addrmap_relocate (BLOCKVECTOR_MAP (bv),
7605796c8dcSSimon Schubert 			  ANOFFSET (delta, s->block_line_section));
7615796c8dcSSimon Schubert 
7625796c8dcSSimon Schubert       for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
7635796c8dcSSimon Schubert 	{
7645796c8dcSSimon Schubert 	  struct block *b;
7655796c8dcSSimon Schubert 	  struct symbol *sym;
7665796c8dcSSimon Schubert 	  struct dict_iterator iter;
7675796c8dcSSimon Schubert 
7685796c8dcSSimon Schubert 	  b = BLOCKVECTOR_BLOCK (bv, i);
7695796c8dcSSimon Schubert 	  BLOCK_START (b) += ANOFFSET (delta, s->block_line_section);
7705796c8dcSSimon Schubert 	  BLOCK_END (b) += ANOFFSET (delta, s->block_line_section);
7715796c8dcSSimon Schubert 
7725796c8dcSSimon Schubert 	  ALL_BLOCK_SYMBOLS (b, iter, sym)
7735796c8dcSSimon Schubert 	    {
774c50c785cSJohn Marino 	      relocate_one_symbol (sym, objfile, delta);
775c50c785cSJohn Marino 	    }
776c50c785cSJohn Marino 	}
777c50c785cSJohn Marino     }
778c50c785cSJohn Marino   }
7795796c8dcSSimon Schubert 
780c50c785cSJohn Marino   /* Relocate isolated symbols.  */
7815796c8dcSSimon Schubert   {
782c50c785cSJohn Marino     struct symbol *iter;
783c50c785cSJohn Marino 
784c50c785cSJohn Marino     for (iter = objfile->template_symbols; iter; iter = iter->hash_next)
785c50c785cSJohn Marino       relocate_one_symbol (iter, objfile, delta);
7865796c8dcSSimon Schubert   }
7875796c8dcSSimon Schubert 
788cf7f2e2dSJohn Marino   if (objfile->psymtabs_addrmap)
789cf7f2e2dSJohn Marino     addrmap_relocate (objfile->psymtabs_addrmap,
790cf7f2e2dSJohn Marino 		      ANOFFSET (delta, SECT_OFF_TEXT (objfile)));
7915796c8dcSSimon Schubert 
792cf7f2e2dSJohn Marino   if (objfile->sf)
793cf7f2e2dSJohn Marino     objfile->sf->qf->relocate (objfile, new_offsets, delta);
7945796c8dcSSimon Schubert 
7955796c8dcSSimon Schubert   {
7965796c8dcSSimon Schubert     struct minimal_symbol *msym;
797cf7f2e2dSJohn Marino 
7985796c8dcSSimon Schubert     ALL_OBJFILE_MSYMBOLS (objfile, msym)
7995796c8dcSSimon Schubert       if (SYMBOL_SECTION (msym) >= 0)
8005796c8dcSSimon Schubert       SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym));
8015796c8dcSSimon Schubert   }
8025796c8dcSSimon Schubert   /* Relocating different sections by different amounts may cause the symbols
8035796c8dcSSimon Schubert      to be out of order.  */
8045796c8dcSSimon Schubert   msymbols_sort (objfile);
8055796c8dcSSimon Schubert 
806cf7f2e2dSJohn Marino   if (objfile->ei.entry_point_p)
8075796c8dcSSimon Schubert     {
8085796c8dcSSimon Schubert       /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT
8095796c8dcSSimon Schubert 	 only as a fallback.  */
8105796c8dcSSimon Schubert       struct obj_section *s;
8115796c8dcSSimon Schubert       s = find_pc_section (objfile->ei.entry_point);
8125796c8dcSSimon Schubert       if (s)
8135796c8dcSSimon Schubert         objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index);
8145796c8dcSSimon Schubert       else
8155796c8dcSSimon Schubert         objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
8165796c8dcSSimon Schubert     }
8175796c8dcSSimon Schubert 
8185796c8dcSSimon Schubert   {
8195796c8dcSSimon Schubert     int i;
820cf7f2e2dSJohn Marino 
8215796c8dcSSimon Schubert     for (i = 0; i < objfile->num_sections; ++i)
8225796c8dcSSimon Schubert       (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i);
8235796c8dcSSimon Schubert   }
8245796c8dcSSimon Schubert 
8255796c8dcSSimon Schubert   /* Rebuild section map next time we need it.  */
826cf7f2e2dSJohn Marino   get_objfile_pspace_data (objfile->pspace)->objfiles_changed_p = 1;
8275796c8dcSSimon Schubert 
8285796c8dcSSimon Schubert   /* Update the table in exec_ops, used to read memory.  */
8295796c8dcSSimon Schubert   ALL_OBJFILE_OSECTIONS (objfile, s)
8305796c8dcSSimon Schubert     {
8315796c8dcSSimon Schubert       int idx = s->the_bfd_section->index;
8325796c8dcSSimon Schubert 
8335796c8dcSSimon Schubert       exec_set_section_address (bfd_get_filename (objfile->obfd), idx,
8345796c8dcSSimon Schubert 				obj_section_addr (s));
8355796c8dcSSimon Schubert     }
8365796c8dcSSimon Schubert 
837cf7f2e2dSJohn Marino   /* Data changed.  */
838cf7f2e2dSJohn Marino   return 1;
839cf7f2e2dSJohn Marino }
840cf7f2e2dSJohn Marino 
841cf7f2e2dSJohn Marino /* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
842cf7f2e2dSJohn Marino    entries in new_offsets.  Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
843cf7f2e2dSJohn Marino 
844cf7f2e2dSJohn Marino    The number and ordering of sections does differ between the two objfiles.
845cf7f2e2dSJohn Marino    Only their names match.  Also the file offsets will differ (objfile being
846cf7f2e2dSJohn Marino    possibly prelinked but separate_debug_objfile is probably not prelinked) but
847cf7f2e2dSJohn Marino    the in-memory absolute address as specified by NEW_OFFSETS must match both
848cf7f2e2dSJohn Marino    files.  */
849cf7f2e2dSJohn Marino 
850cf7f2e2dSJohn Marino void
851cf7f2e2dSJohn Marino objfile_relocate (struct objfile *objfile, struct section_offsets *new_offsets)
852cf7f2e2dSJohn Marino {
853cf7f2e2dSJohn Marino   struct objfile *debug_objfile;
854cf7f2e2dSJohn Marino   int changed = 0;
855cf7f2e2dSJohn Marino 
856cf7f2e2dSJohn Marino   changed |= objfile_relocate1 (objfile, new_offsets);
857cf7f2e2dSJohn Marino 
858cf7f2e2dSJohn Marino   for (debug_objfile = objfile->separate_debug_objfile;
859cf7f2e2dSJohn Marino        debug_objfile;
860cf7f2e2dSJohn Marino        debug_objfile = objfile_separate_debug_iterate (objfile, debug_objfile))
861cf7f2e2dSJohn Marino     {
862cf7f2e2dSJohn Marino       struct section_addr_info *objfile_addrs;
863cf7f2e2dSJohn Marino       struct section_offsets *new_debug_offsets;
864cf7f2e2dSJohn Marino       struct cleanup *my_cleanups;
865cf7f2e2dSJohn Marino 
866cf7f2e2dSJohn Marino       objfile_addrs = build_section_addr_info_from_objfile (objfile);
867cf7f2e2dSJohn Marino       my_cleanups = make_cleanup (xfree, objfile_addrs);
868cf7f2e2dSJohn Marino 
869cf7f2e2dSJohn Marino       /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
870cf7f2e2dSJohn Marino 	 relative ones must be already created according to debug_objfile.  */
871cf7f2e2dSJohn Marino 
872cf7f2e2dSJohn Marino       addr_info_make_relative (objfile_addrs, debug_objfile->obfd);
873cf7f2e2dSJohn Marino 
874cf7f2e2dSJohn Marino       gdb_assert (debug_objfile->num_sections
875cf7f2e2dSJohn Marino 		  == bfd_count_sections (debug_objfile->obfd));
876cf7f2e2dSJohn Marino       new_debug_offsets =
877cf7f2e2dSJohn Marino 	xmalloc (SIZEOF_N_SECTION_OFFSETS (debug_objfile->num_sections));
878cf7f2e2dSJohn Marino       make_cleanup (xfree, new_debug_offsets);
879cf7f2e2dSJohn Marino       relative_addr_info_to_section_offsets (new_debug_offsets,
880cf7f2e2dSJohn Marino 					     debug_objfile->num_sections,
881cf7f2e2dSJohn Marino 					     objfile_addrs);
882cf7f2e2dSJohn Marino 
883cf7f2e2dSJohn Marino       changed |= objfile_relocate1 (debug_objfile, new_debug_offsets);
884cf7f2e2dSJohn Marino 
885cf7f2e2dSJohn Marino       do_cleanups (my_cleanups);
886cf7f2e2dSJohn Marino     }
887cf7f2e2dSJohn Marino 
8885796c8dcSSimon Schubert   /* Relocate breakpoints as necessary, after things are relocated.  */
889cf7f2e2dSJohn Marino   if (changed)
8905796c8dcSSimon Schubert     breakpoint_re_set ();
8915796c8dcSSimon Schubert }
8925796c8dcSSimon Schubert 
8935796c8dcSSimon Schubert /* Return non-zero if OBJFILE has partial symbols.  */
8945796c8dcSSimon Schubert 
8955796c8dcSSimon Schubert int
8965796c8dcSSimon Schubert objfile_has_partial_symbols (struct objfile *objfile)
8975796c8dcSSimon Schubert {
898c50c785cSJohn Marino   if (!objfile->sf)
899c50c785cSJohn Marino     return 0;
900c50c785cSJohn Marino 
901c50c785cSJohn Marino   /* If we have not read psymbols, but we have a function capable of reading
902c50c785cSJohn Marino      them, then that is an indication that they are in fact available.  Without
903c50c785cSJohn Marino      this function the symbols may have been already read in but they also may
904c50c785cSJohn Marino      not be present in this objfile.  */
905c50c785cSJohn Marino   if ((objfile->flags & OBJF_PSYMTABS_READ) == 0
906c50c785cSJohn Marino       && objfile->sf->sym_read_psymbols != NULL)
907c50c785cSJohn Marino     return 1;
908c50c785cSJohn Marino 
909c50c785cSJohn Marino   return objfile->sf->qf->has_symbols (objfile);
9105796c8dcSSimon Schubert }
9115796c8dcSSimon Schubert 
9125796c8dcSSimon Schubert /* Return non-zero if OBJFILE has full symbols.  */
9135796c8dcSSimon Schubert 
9145796c8dcSSimon Schubert int
9155796c8dcSSimon Schubert objfile_has_full_symbols (struct objfile *objfile)
9165796c8dcSSimon Schubert {
9175796c8dcSSimon Schubert   return objfile->symtabs != NULL;
9185796c8dcSSimon Schubert }
9195796c8dcSSimon Schubert 
920cf7f2e2dSJohn Marino /* Return non-zero if OBJFILE has full or partial symbols, either directly
921cf7f2e2dSJohn Marino    or through a separate debug file.  */
922cf7f2e2dSJohn Marino 
923cf7f2e2dSJohn Marino int
924cf7f2e2dSJohn Marino objfile_has_symbols (struct objfile *objfile)
925cf7f2e2dSJohn Marino {
926cf7f2e2dSJohn Marino   struct objfile *o;
927cf7f2e2dSJohn Marino 
928cf7f2e2dSJohn Marino   for (o = objfile; o; o = objfile_separate_debug_iterate (objfile, o))
929cf7f2e2dSJohn Marino     if (objfile_has_partial_symbols (o) || objfile_has_full_symbols (o))
930cf7f2e2dSJohn Marino       return 1;
931cf7f2e2dSJohn Marino   return 0;
932cf7f2e2dSJohn Marino }
933cf7f2e2dSJohn Marino 
934cf7f2e2dSJohn Marino 
9355796c8dcSSimon Schubert /* Many places in gdb want to test just to see if we have any partial
9365796c8dcSSimon Schubert    symbols available.  This function returns zero if none are currently
9375796c8dcSSimon Schubert    available, nonzero otherwise.  */
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert int
9405796c8dcSSimon Schubert have_partial_symbols (void)
9415796c8dcSSimon Schubert {
9425796c8dcSSimon Schubert   struct objfile *ofp;
9435796c8dcSSimon Schubert 
9445796c8dcSSimon Schubert   ALL_OBJFILES (ofp)
9455796c8dcSSimon Schubert   {
9465796c8dcSSimon Schubert     if (objfile_has_partial_symbols (ofp))
9475796c8dcSSimon Schubert       return 1;
9485796c8dcSSimon Schubert   }
9495796c8dcSSimon Schubert   return 0;
9505796c8dcSSimon Schubert }
9515796c8dcSSimon Schubert 
9525796c8dcSSimon Schubert /* Many places in gdb want to test just to see if we have any full
9535796c8dcSSimon Schubert    symbols available.  This function returns zero if none are currently
9545796c8dcSSimon Schubert    available, nonzero otherwise.  */
9555796c8dcSSimon Schubert 
9565796c8dcSSimon Schubert int
9575796c8dcSSimon Schubert have_full_symbols (void)
9585796c8dcSSimon Schubert {
9595796c8dcSSimon Schubert   struct objfile *ofp;
9605796c8dcSSimon Schubert 
9615796c8dcSSimon Schubert   ALL_OBJFILES (ofp)
9625796c8dcSSimon Schubert   {
9635796c8dcSSimon Schubert     if (objfile_has_full_symbols (ofp))
9645796c8dcSSimon Schubert       return 1;
9655796c8dcSSimon Schubert   }
9665796c8dcSSimon Schubert   return 0;
9675796c8dcSSimon Schubert }
9685796c8dcSSimon Schubert 
9695796c8dcSSimon Schubert 
9705796c8dcSSimon Schubert /* This operations deletes all objfile entries that represent solibs that
9715796c8dcSSimon Schubert    weren't explicitly loaded by the user, via e.g., the add-symbol-file
972c50c785cSJohn Marino    command.  */
973c50c785cSJohn Marino 
9745796c8dcSSimon Schubert void
9755796c8dcSSimon Schubert objfile_purge_solibs (void)
9765796c8dcSSimon Schubert {
9775796c8dcSSimon Schubert   struct objfile *objf;
9785796c8dcSSimon Schubert   struct objfile *temp;
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert   ALL_OBJFILES_SAFE (objf, temp)
9815796c8dcSSimon Schubert   {
9825796c8dcSSimon Schubert     /* We assume that the solib package has been purged already, or will
983c50c785cSJohn Marino        be soon.  */
984c50c785cSJohn Marino 
9855796c8dcSSimon Schubert     if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
9865796c8dcSSimon Schubert       free_objfile (objf);
9875796c8dcSSimon Schubert   }
9885796c8dcSSimon Schubert }
9895796c8dcSSimon Schubert 
9905796c8dcSSimon Schubert 
9915796c8dcSSimon Schubert /* Many places in gdb want to test just to see if we have any minimal
9925796c8dcSSimon Schubert    symbols available.  This function returns zero if none are currently
9935796c8dcSSimon Schubert    available, nonzero otherwise.  */
9945796c8dcSSimon Schubert 
9955796c8dcSSimon Schubert int
9965796c8dcSSimon Schubert have_minimal_symbols (void)
9975796c8dcSSimon Schubert {
9985796c8dcSSimon Schubert   struct objfile *ofp;
9995796c8dcSSimon Schubert 
10005796c8dcSSimon Schubert   ALL_OBJFILES (ofp)
10015796c8dcSSimon Schubert   {
10025796c8dcSSimon Schubert     if (ofp->minimal_symbol_count > 0)
10035796c8dcSSimon Schubert       {
10045796c8dcSSimon Schubert 	return 1;
10055796c8dcSSimon Schubert       }
10065796c8dcSSimon Schubert   }
10075796c8dcSSimon Schubert   return 0;
10085796c8dcSSimon Schubert }
10095796c8dcSSimon Schubert 
10105796c8dcSSimon Schubert /* Qsort comparison function.  */
10115796c8dcSSimon Schubert 
10125796c8dcSSimon Schubert static int
10135796c8dcSSimon Schubert qsort_cmp (const void *a, const void *b)
10145796c8dcSSimon Schubert {
10155796c8dcSSimon Schubert   const struct obj_section *sect1 = *(const struct obj_section **) a;
10165796c8dcSSimon Schubert   const struct obj_section *sect2 = *(const struct obj_section **) b;
10175796c8dcSSimon Schubert   const CORE_ADDR sect1_addr = obj_section_addr (sect1);
10185796c8dcSSimon Schubert   const CORE_ADDR sect2_addr = obj_section_addr (sect2);
10195796c8dcSSimon Schubert 
10205796c8dcSSimon Schubert   if (sect1_addr < sect2_addr)
10215796c8dcSSimon Schubert     return -1;
10225796c8dcSSimon Schubert   else if (sect1_addr > sect2_addr)
10235796c8dcSSimon Schubert     return 1;
10245796c8dcSSimon Schubert   else
10255796c8dcSSimon Schubert     {
10265796c8dcSSimon Schubert       /* Sections are at the same address.  This could happen if
10275796c8dcSSimon Schubert 	 A) we have an objfile and a separate debuginfo.
10285796c8dcSSimon Schubert 	 B) we are confused, and have added sections without proper relocation,
10295796c8dcSSimon Schubert 	 or something like that.  */
10305796c8dcSSimon Schubert 
10315796c8dcSSimon Schubert       const struct objfile *const objfile1 = sect1->objfile;
10325796c8dcSSimon Schubert       const struct objfile *const objfile2 = sect2->objfile;
10335796c8dcSSimon Schubert 
10345796c8dcSSimon Schubert       if (objfile1->separate_debug_objfile == objfile2
10355796c8dcSSimon Schubert 	  || objfile2->separate_debug_objfile == objfile1)
10365796c8dcSSimon Schubert 	{
10375796c8dcSSimon Schubert 	  /* Case A.  The ordering doesn't matter: separate debuginfo files
10385796c8dcSSimon Schubert 	     will be filtered out later.  */
10395796c8dcSSimon Schubert 
10405796c8dcSSimon Schubert 	  return 0;
10415796c8dcSSimon Schubert 	}
10425796c8dcSSimon Schubert 
10435796c8dcSSimon Schubert       /* Case B.  Maintain stable sort order, so bugs in GDB are easier to
10445796c8dcSSimon Schubert 	 triage.  This section could be slow (since we iterate over all
10455796c8dcSSimon Schubert 	 objfiles in each call to qsort_cmp), but this shouldn't happen
10465796c8dcSSimon Schubert 	 very often (GDB is already in a confused state; one hopes this
10475796c8dcSSimon Schubert 	 doesn't happen at all).  If you discover that significant time is
10485796c8dcSSimon Schubert 	 spent in the loops below, do 'set complaints 100' and examine the
10495796c8dcSSimon Schubert 	 resulting complaints.  */
10505796c8dcSSimon Schubert 
10515796c8dcSSimon Schubert       if (objfile1 == objfile2)
10525796c8dcSSimon Schubert 	{
10535796c8dcSSimon Schubert 	  /* Both sections came from the same objfile.  We are really confused.
10545796c8dcSSimon Schubert 	     Sort on sequence order of sections within the objfile.  */
10555796c8dcSSimon Schubert 
10565796c8dcSSimon Schubert 	  const struct obj_section *osect;
10575796c8dcSSimon Schubert 
10585796c8dcSSimon Schubert 	  ALL_OBJFILE_OSECTIONS (objfile1, osect)
10595796c8dcSSimon Schubert 	    if (osect == sect1)
10605796c8dcSSimon Schubert 	      return -1;
10615796c8dcSSimon Schubert 	    else if (osect == sect2)
10625796c8dcSSimon Schubert 	      return 1;
10635796c8dcSSimon Schubert 
10645796c8dcSSimon Schubert 	  /* We should have found one of the sections before getting here.  */
1065c50c785cSJohn Marino 	  gdb_assert_not_reached ("section not found");
10665796c8dcSSimon Schubert 	}
10675796c8dcSSimon Schubert       else
10685796c8dcSSimon Schubert 	{
10695796c8dcSSimon Schubert 	  /* Sort on sequence number of the objfile in the chain.  */
10705796c8dcSSimon Schubert 
10715796c8dcSSimon Schubert 	  const struct objfile *objfile;
10725796c8dcSSimon Schubert 
10735796c8dcSSimon Schubert 	  ALL_OBJFILES (objfile)
10745796c8dcSSimon Schubert 	    if (objfile == objfile1)
10755796c8dcSSimon Schubert 	      return -1;
10765796c8dcSSimon Schubert 	    else if (objfile == objfile2)
10775796c8dcSSimon Schubert 	      return 1;
10785796c8dcSSimon Schubert 
10795796c8dcSSimon Schubert 	  /* We should have found one of the objfiles before getting here.  */
1080c50c785cSJohn Marino 	  gdb_assert_not_reached ("objfile not found");
10815796c8dcSSimon Schubert 	}
10825796c8dcSSimon Schubert     }
10835796c8dcSSimon Schubert 
10845796c8dcSSimon Schubert   /* Unreachable.  */
1085c50c785cSJohn Marino   gdb_assert_not_reached ("unexpected code path");
10865796c8dcSSimon Schubert   return 0;
10875796c8dcSSimon Schubert }
10885796c8dcSSimon Schubert 
10895796c8dcSSimon Schubert /* Select "better" obj_section to keep.  We prefer the one that came from
10905796c8dcSSimon Schubert    the real object, rather than the one from separate debuginfo.
10915796c8dcSSimon Schubert    Most of the time the two sections are exactly identical, but with
10925796c8dcSSimon Schubert    prelinking the .rel.dyn section in the real object may have different
10935796c8dcSSimon Schubert    size.  */
10945796c8dcSSimon Schubert 
10955796c8dcSSimon Schubert static struct obj_section *
10965796c8dcSSimon Schubert preferred_obj_section (struct obj_section *a, struct obj_section *b)
10975796c8dcSSimon Schubert {
10985796c8dcSSimon Schubert   gdb_assert (obj_section_addr (a) == obj_section_addr (b));
10995796c8dcSSimon Schubert   gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
11005796c8dcSSimon Schubert 	      || (b->objfile->separate_debug_objfile == a->objfile));
11015796c8dcSSimon Schubert   gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
11025796c8dcSSimon Schubert 	      || (b->objfile->separate_debug_objfile_backlink == a->objfile));
11035796c8dcSSimon Schubert 
11045796c8dcSSimon Schubert   if (a->objfile->separate_debug_objfile != NULL)
11055796c8dcSSimon Schubert     return a;
11065796c8dcSSimon Schubert   return b;
11075796c8dcSSimon Schubert }
11085796c8dcSSimon Schubert 
11095796c8dcSSimon Schubert /* Return 1 if SECTION should be inserted into the section map.
11105796c8dcSSimon Schubert    We want to insert only non-overlay and non-TLS section.  */
11115796c8dcSSimon Schubert 
11125796c8dcSSimon Schubert static int
11135796c8dcSSimon Schubert insert_section_p (const struct bfd *abfd,
11145796c8dcSSimon Schubert 		  const struct bfd_section *section)
11155796c8dcSSimon Schubert {
11165796c8dcSSimon Schubert   const bfd_vma lma = bfd_section_lma (abfd, section);
11175796c8dcSSimon Schubert 
11185796c8dcSSimon Schubert   if (lma != 0 && lma != bfd_section_vma (abfd, section)
11195796c8dcSSimon Schubert       && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0)
11205796c8dcSSimon Schubert     /* This is an overlay section.  IN_MEMORY check is needed to avoid
11215796c8dcSSimon Schubert        discarding sections from the "system supplied DSO" (aka vdso)
11225796c8dcSSimon Schubert        on some Linux systems (e.g. Fedora 11).  */
11235796c8dcSSimon Schubert     return 0;
11245796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, section) & SEC_THREAD_LOCAL) != 0)
11255796c8dcSSimon Schubert     /* This is a TLS section.  */
11265796c8dcSSimon Schubert     return 0;
11275796c8dcSSimon Schubert 
11285796c8dcSSimon Schubert   return 1;
11295796c8dcSSimon Schubert }
11305796c8dcSSimon Schubert 
11315796c8dcSSimon Schubert /* Filter out overlapping sections where one section came from the real
11325796c8dcSSimon Schubert    objfile, and the other from a separate debuginfo file.
11335796c8dcSSimon Schubert    Return the size of table after redundant sections have been eliminated.  */
11345796c8dcSSimon Schubert 
11355796c8dcSSimon Schubert static int
11365796c8dcSSimon Schubert filter_debuginfo_sections (struct obj_section **map, int map_size)
11375796c8dcSSimon Schubert {
11385796c8dcSSimon Schubert   int i, j;
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert   for (i = 0, j = 0; i < map_size - 1; i++)
11415796c8dcSSimon Schubert     {
11425796c8dcSSimon Schubert       struct obj_section *const sect1 = map[i];
11435796c8dcSSimon Schubert       struct obj_section *const sect2 = map[i + 1];
11445796c8dcSSimon Schubert       const struct objfile *const objfile1 = sect1->objfile;
11455796c8dcSSimon Schubert       const struct objfile *const objfile2 = sect2->objfile;
11465796c8dcSSimon Schubert       const CORE_ADDR sect1_addr = obj_section_addr (sect1);
11475796c8dcSSimon Schubert       const CORE_ADDR sect2_addr = obj_section_addr (sect2);
11485796c8dcSSimon Schubert 
11495796c8dcSSimon Schubert       if (sect1_addr == sect2_addr
11505796c8dcSSimon Schubert 	  && (objfile1->separate_debug_objfile == objfile2
11515796c8dcSSimon Schubert 	      || objfile2->separate_debug_objfile == objfile1))
11525796c8dcSSimon Schubert 	{
11535796c8dcSSimon Schubert 	  map[j++] = preferred_obj_section (sect1, sect2);
11545796c8dcSSimon Schubert 	  ++i;
11555796c8dcSSimon Schubert 	}
11565796c8dcSSimon Schubert       else
11575796c8dcSSimon Schubert 	map[j++] = sect1;
11585796c8dcSSimon Schubert     }
11595796c8dcSSimon Schubert 
11605796c8dcSSimon Schubert   if (i < map_size)
11615796c8dcSSimon Schubert     {
11625796c8dcSSimon Schubert       gdb_assert (i == map_size - 1);
11635796c8dcSSimon Schubert       map[j++] = map[i];
11645796c8dcSSimon Schubert     }
11655796c8dcSSimon Schubert 
11665796c8dcSSimon Schubert   /* The map should not have shrunk to less than half the original size.  */
11675796c8dcSSimon Schubert   gdb_assert (map_size / 2 <= j);
11685796c8dcSSimon Schubert 
11695796c8dcSSimon Schubert   return j;
11705796c8dcSSimon Schubert }
11715796c8dcSSimon Schubert 
11725796c8dcSSimon Schubert /* Filter out overlapping sections, issuing a warning if any are found.
11735796c8dcSSimon Schubert    Overlapping sections could really be overlay sections which we didn't
11745796c8dcSSimon Schubert    classify as such in insert_section_p, or we could be dealing with a
11755796c8dcSSimon Schubert    corrupt binary.  */
11765796c8dcSSimon Schubert 
11775796c8dcSSimon Schubert static int
11785796c8dcSSimon Schubert filter_overlapping_sections (struct obj_section **map, int map_size)
11795796c8dcSSimon Schubert {
11805796c8dcSSimon Schubert   int i, j;
11815796c8dcSSimon Schubert 
11825796c8dcSSimon Schubert   for (i = 0, j = 0; i < map_size - 1; )
11835796c8dcSSimon Schubert     {
11845796c8dcSSimon Schubert       int k;
11855796c8dcSSimon Schubert 
11865796c8dcSSimon Schubert       map[j++] = map[i];
11875796c8dcSSimon Schubert       for (k = i + 1; k < map_size; k++)
11885796c8dcSSimon Schubert 	{
11895796c8dcSSimon Schubert 	  struct obj_section *const sect1 = map[i];
11905796c8dcSSimon Schubert 	  struct obj_section *const sect2 = map[k];
11915796c8dcSSimon Schubert 	  const CORE_ADDR sect1_addr = obj_section_addr (sect1);
11925796c8dcSSimon Schubert 	  const CORE_ADDR sect2_addr = obj_section_addr (sect2);
11935796c8dcSSimon Schubert 	  const CORE_ADDR sect1_endaddr = obj_section_endaddr (sect1);
11945796c8dcSSimon Schubert 
11955796c8dcSSimon Schubert 	  gdb_assert (sect1_addr <= sect2_addr);
11965796c8dcSSimon Schubert 
11975796c8dcSSimon Schubert 	  if (sect1_endaddr <= sect2_addr)
11985796c8dcSSimon Schubert 	    break;
11995796c8dcSSimon Schubert 	  else
12005796c8dcSSimon Schubert 	    {
12015796c8dcSSimon Schubert 	      /* We have an overlap.  Report it.  */
12025796c8dcSSimon Schubert 
12035796c8dcSSimon Schubert 	      struct objfile *const objf1 = sect1->objfile;
12045796c8dcSSimon Schubert 	      struct objfile *const objf2 = sect2->objfile;
12055796c8dcSSimon Schubert 
12065796c8dcSSimon Schubert 	      const struct bfd *const abfd1 = objf1->obfd;
12075796c8dcSSimon Schubert 	      const struct bfd *const abfd2 = objf2->obfd;
12085796c8dcSSimon Schubert 
12095796c8dcSSimon Schubert 	      const struct bfd_section *const bfds1 = sect1->the_bfd_section;
12105796c8dcSSimon Schubert 	      const struct bfd_section *const bfds2 = sect2->the_bfd_section;
12115796c8dcSSimon Schubert 
12125796c8dcSSimon Schubert 	      const CORE_ADDR sect2_endaddr = obj_section_endaddr (sect2);
12135796c8dcSSimon Schubert 
12145796c8dcSSimon Schubert 	      struct gdbarch *const gdbarch = get_objfile_arch (objf1);
12155796c8dcSSimon Schubert 
12165796c8dcSSimon Schubert 	      complaint (&symfile_complaints,
12175796c8dcSSimon Schubert 			 _("unexpected overlap between:\n"
12185796c8dcSSimon Schubert 			   " (A) section `%s' from `%s' [%s, %s)\n"
12195796c8dcSSimon Schubert 			   " (B) section `%s' from `%s' [%s, %s).\n"
12205796c8dcSSimon Schubert 			   "Will ignore section B"),
12215796c8dcSSimon Schubert 			 bfd_section_name (abfd1, bfds1), objf1->name,
12225796c8dcSSimon Schubert 			 paddress (gdbarch, sect1_addr),
12235796c8dcSSimon Schubert 			 paddress (gdbarch, sect1_endaddr),
12245796c8dcSSimon Schubert 			 bfd_section_name (abfd2, bfds2), objf2->name,
12255796c8dcSSimon Schubert 			 paddress (gdbarch, sect2_addr),
12265796c8dcSSimon Schubert 			 paddress (gdbarch, sect2_endaddr));
12275796c8dcSSimon Schubert 	    }
12285796c8dcSSimon Schubert 	}
12295796c8dcSSimon Schubert       i = k;
12305796c8dcSSimon Schubert     }
12315796c8dcSSimon Schubert 
12325796c8dcSSimon Schubert   if (i < map_size)
12335796c8dcSSimon Schubert     {
12345796c8dcSSimon Schubert       gdb_assert (i == map_size - 1);
12355796c8dcSSimon Schubert       map[j++] = map[i];
12365796c8dcSSimon Schubert     }
12375796c8dcSSimon Schubert 
12385796c8dcSSimon Schubert   return j;
12395796c8dcSSimon Schubert }
12405796c8dcSSimon Schubert 
12415796c8dcSSimon Schubert 
12425796c8dcSSimon Schubert /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
12435796c8dcSSimon Schubert    TLS, overlay and overlapping sections.  */
12445796c8dcSSimon Schubert 
12455796c8dcSSimon Schubert static void
1246cf7f2e2dSJohn Marino update_section_map (struct program_space *pspace,
1247cf7f2e2dSJohn Marino 		    struct obj_section ***pmap, int *pmap_size)
12485796c8dcSSimon Schubert {
12495796c8dcSSimon Schubert   int alloc_size, map_size, i;
12505796c8dcSSimon Schubert   struct obj_section *s, **map;
12515796c8dcSSimon Schubert   struct objfile *objfile;
12525796c8dcSSimon Schubert 
1253cf7f2e2dSJohn Marino   gdb_assert (get_objfile_pspace_data (pspace)->objfiles_changed_p != 0);
12545796c8dcSSimon Schubert 
12555796c8dcSSimon Schubert   map = *pmap;
12565796c8dcSSimon Schubert   xfree (map);
12575796c8dcSSimon Schubert 
12585796c8dcSSimon Schubert   alloc_size = 0;
1259cf7f2e2dSJohn Marino   ALL_PSPACE_OBJFILES (pspace, objfile)
1260cf7f2e2dSJohn Marino     ALL_OBJFILE_OSECTIONS (objfile, s)
12615796c8dcSSimon Schubert       if (insert_section_p (objfile->obfd, s->the_bfd_section))
12625796c8dcSSimon Schubert 	alloc_size += 1;
12635796c8dcSSimon Schubert 
1264cf7f2e2dSJohn Marino   /* This happens on detach/attach (e.g. in gdb.base/attach.exp).  */
1265cf7f2e2dSJohn Marino   if (alloc_size == 0)
1266cf7f2e2dSJohn Marino     {
1267cf7f2e2dSJohn Marino       *pmap = NULL;
1268cf7f2e2dSJohn Marino       *pmap_size = 0;
1269cf7f2e2dSJohn Marino       return;
1270cf7f2e2dSJohn Marino     }
1271cf7f2e2dSJohn Marino 
12725796c8dcSSimon Schubert   map = xmalloc (alloc_size * sizeof (*map));
12735796c8dcSSimon Schubert 
12745796c8dcSSimon Schubert   i = 0;
1275cf7f2e2dSJohn Marino   ALL_PSPACE_OBJFILES (pspace, objfile)
1276cf7f2e2dSJohn Marino     ALL_OBJFILE_OSECTIONS (objfile, s)
12775796c8dcSSimon Schubert       if (insert_section_p (objfile->obfd, s->the_bfd_section))
12785796c8dcSSimon Schubert 	map[i++] = s;
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert   qsort (map, alloc_size, sizeof (*map), qsort_cmp);
12815796c8dcSSimon Schubert   map_size = filter_debuginfo_sections(map, alloc_size);
12825796c8dcSSimon Schubert   map_size = filter_overlapping_sections(map, map_size);
12835796c8dcSSimon Schubert 
12845796c8dcSSimon Schubert   if (map_size < alloc_size)
12855796c8dcSSimon Schubert     /* Some sections were eliminated.  Trim excess space.  */
12865796c8dcSSimon Schubert     map = xrealloc (map, map_size * sizeof (*map));
12875796c8dcSSimon Schubert   else
12885796c8dcSSimon Schubert     gdb_assert (alloc_size == map_size);
12895796c8dcSSimon Schubert 
12905796c8dcSSimon Schubert   *pmap = map;
12915796c8dcSSimon Schubert   *pmap_size = map_size;
12925796c8dcSSimon Schubert }
12935796c8dcSSimon Schubert 
12945796c8dcSSimon Schubert /* Bsearch comparison function.  */
12955796c8dcSSimon Schubert 
12965796c8dcSSimon Schubert static int
12975796c8dcSSimon Schubert bsearch_cmp (const void *key, const void *elt)
12985796c8dcSSimon Schubert {
12995796c8dcSSimon Schubert   const CORE_ADDR pc = *(CORE_ADDR *) key;
13005796c8dcSSimon Schubert   const struct obj_section *section = *(const struct obj_section **) elt;
13015796c8dcSSimon Schubert 
13025796c8dcSSimon Schubert   if (pc < obj_section_addr (section))
13035796c8dcSSimon Schubert     return -1;
13045796c8dcSSimon Schubert   if (pc < obj_section_endaddr (section))
13055796c8dcSSimon Schubert     return 0;
13065796c8dcSSimon Schubert   return 1;
13075796c8dcSSimon Schubert }
13085796c8dcSSimon Schubert 
13095796c8dcSSimon Schubert /* Returns a section whose range includes PC or NULL if none found.   */
13105796c8dcSSimon Schubert 
13115796c8dcSSimon Schubert struct obj_section *
13125796c8dcSSimon Schubert find_pc_section (CORE_ADDR pc)
13135796c8dcSSimon Schubert {
1314cf7f2e2dSJohn Marino   struct objfile_pspace_info *pspace_info;
13155796c8dcSSimon Schubert   struct obj_section *s, **sp;
13165796c8dcSSimon Schubert 
13175796c8dcSSimon Schubert   /* Check for mapped overlay section first.  */
13185796c8dcSSimon Schubert   s = find_pc_mapped_section (pc);
13195796c8dcSSimon Schubert   if (s)
13205796c8dcSSimon Schubert     return s;
13215796c8dcSSimon Schubert 
1322cf7f2e2dSJohn Marino   pspace_info = get_objfile_pspace_data (current_program_space);
1323cf7f2e2dSJohn Marino   if (pspace_info->objfiles_changed_p != 0)
13245796c8dcSSimon Schubert     {
1325cf7f2e2dSJohn Marino       update_section_map (current_program_space,
1326cf7f2e2dSJohn Marino 			  &pspace_info->sections,
1327cf7f2e2dSJohn Marino 			  &pspace_info->num_sections);
13285796c8dcSSimon Schubert 
1329cf7f2e2dSJohn Marino       /* Don't need updates to section map until objfiles are added,
1330cf7f2e2dSJohn Marino          removed or relocated.  */
1331cf7f2e2dSJohn Marino       pspace_info->objfiles_changed_p = 0;
13325796c8dcSSimon Schubert     }
13335796c8dcSSimon Schubert 
1334cf7f2e2dSJohn Marino   /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1335cf7f2e2dSJohn Marino      bsearch be non-NULL.  */
1336cf7f2e2dSJohn Marino   if (pspace_info->sections == NULL)
1337cf7f2e2dSJohn Marino     {
1338cf7f2e2dSJohn Marino       gdb_assert (pspace_info->num_sections == 0);
1339cf7f2e2dSJohn Marino       return NULL;
1340cf7f2e2dSJohn Marino     }
1341cf7f2e2dSJohn Marino 
1342cf7f2e2dSJohn Marino   sp = (struct obj_section **) bsearch (&pc,
1343cf7f2e2dSJohn Marino 					pspace_info->sections,
1344cf7f2e2dSJohn Marino 					pspace_info->num_sections,
1345cf7f2e2dSJohn Marino 					sizeof (*pspace_info->sections),
1346cf7f2e2dSJohn Marino 					bsearch_cmp);
13475796c8dcSSimon Schubert   if (sp != NULL)
13485796c8dcSSimon Schubert     return *sp;
13495796c8dcSSimon Schubert   return NULL;
13505796c8dcSSimon Schubert }
13515796c8dcSSimon Schubert 
13525796c8dcSSimon Schubert 
13535796c8dcSSimon Schubert /* In SVR4, we recognize a trampoline by it's section name.
13545796c8dcSSimon Schubert    That is, if the pc is in a section named ".plt" then we are in
13555796c8dcSSimon Schubert    a trampoline.  */
13565796c8dcSSimon Schubert 
13575796c8dcSSimon Schubert int
13585796c8dcSSimon Schubert in_plt_section (CORE_ADDR pc, char *name)
13595796c8dcSSimon Schubert {
13605796c8dcSSimon Schubert   struct obj_section *s;
13615796c8dcSSimon Schubert   int retval = 0;
13625796c8dcSSimon Schubert 
13635796c8dcSSimon Schubert   s = find_pc_section (pc);
13645796c8dcSSimon Schubert 
13655796c8dcSSimon Schubert   retval = (s != NULL
13665796c8dcSSimon Schubert 	    && s->the_bfd_section->name != NULL
13675796c8dcSSimon Schubert 	    && strcmp (s->the_bfd_section->name, ".plt") == 0);
13685796c8dcSSimon Schubert   return (retval);
13695796c8dcSSimon Schubert }
13705796c8dcSSimon Schubert 
13715796c8dcSSimon Schubert 
13725796c8dcSSimon Schubert /* Keep a registry of per-objfile data-pointers required by other GDB
13735796c8dcSSimon Schubert    modules.  */
13745796c8dcSSimon Schubert 
13755796c8dcSSimon Schubert struct objfile_data
13765796c8dcSSimon Schubert {
13775796c8dcSSimon Schubert   unsigned index;
13785796c8dcSSimon Schubert   void (*save) (struct objfile *, void *);
13795796c8dcSSimon Schubert   void (*free) (struct objfile *, void *);
13805796c8dcSSimon Schubert };
13815796c8dcSSimon Schubert 
13825796c8dcSSimon Schubert struct objfile_data_registration
13835796c8dcSSimon Schubert {
13845796c8dcSSimon Schubert   struct objfile_data *data;
13855796c8dcSSimon Schubert   struct objfile_data_registration *next;
13865796c8dcSSimon Schubert };
13875796c8dcSSimon Schubert 
13885796c8dcSSimon Schubert struct objfile_data_registry
13895796c8dcSSimon Schubert {
13905796c8dcSSimon Schubert   struct objfile_data_registration *registrations;
13915796c8dcSSimon Schubert   unsigned num_registrations;
13925796c8dcSSimon Schubert };
13935796c8dcSSimon Schubert 
13945796c8dcSSimon Schubert static struct objfile_data_registry objfile_data_registry = { NULL, 0 };
13955796c8dcSSimon Schubert 
13965796c8dcSSimon Schubert const struct objfile_data *
13975796c8dcSSimon Schubert register_objfile_data_with_cleanup (void (*save) (struct objfile *, void *),
13985796c8dcSSimon Schubert 				    void (*free) (struct objfile *, void *))
13995796c8dcSSimon Schubert {
14005796c8dcSSimon Schubert   struct objfile_data_registration **curr;
14015796c8dcSSimon Schubert 
14025796c8dcSSimon Schubert   /* Append new registration.  */
14035796c8dcSSimon Schubert   for (curr = &objfile_data_registry.registrations;
14045796c8dcSSimon Schubert        *curr != NULL; curr = &(*curr)->next);
14055796c8dcSSimon Schubert 
14065796c8dcSSimon Schubert   *curr = XMALLOC (struct objfile_data_registration);
14075796c8dcSSimon Schubert   (*curr)->next = NULL;
14085796c8dcSSimon Schubert   (*curr)->data = XMALLOC (struct objfile_data);
14095796c8dcSSimon Schubert   (*curr)->data->index = objfile_data_registry.num_registrations++;
14105796c8dcSSimon Schubert   (*curr)->data->save = save;
14115796c8dcSSimon Schubert   (*curr)->data->free = free;
14125796c8dcSSimon Schubert 
14135796c8dcSSimon Schubert   return (*curr)->data;
14145796c8dcSSimon Schubert }
14155796c8dcSSimon Schubert 
14165796c8dcSSimon Schubert const struct objfile_data *
14175796c8dcSSimon Schubert register_objfile_data (void)
14185796c8dcSSimon Schubert {
14195796c8dcSSimon Schubert   return register_objfile_data_with_cleanup (NULL, NULL);
14205796c8dcSSimon Schubert }
14215796c8dcSSimon Schubert 
14225796c8dcSSimon Schubert static void
14235796c8dcSSimon Schubert objfile_alloc_data (struct objfile *objfile)
14245796c8dcSSimon Schubert {
14255796c8dcSSimon Schubert   gdb_assert (objfile->data == NULL);
14265796c8dcSSimon Schubert   objfile->num_data = objfile_data_registry.num_registrations;
14275796c8dcSSimon Schubert   objfile->data = XCALLOC (objfile->num_data, void *);
14285796c8dcSSimon Schubert }
14295796c8dcSSimon Schubert 
14305796c8dcSSimon Schubert static void
14315796c8dcSSimon Schubert objfile_free_data (struct objfile *objfile)
14325796c8dcSSimon Schubert {
14335796c8dcSSimon Schubert   gdb_assert (objfile->data != NULL);
14345796c8dcSSimon Schubert   clear_objfile_data (objfile);
14355796c8dcSSimon Schubert   xfree (objfile->data);
14365796c8dcSSimon Schubert   objfile->data = NULL;
14375796c8dcSSimon Schubert }
14385796c8dcSSimon Schubert 
14395796c8dcSSimon Schubert void
14405796c8dcSSimon Schubert clear_objfile_data (struct objfile *objfile)
14415796c8dcSSimon Schubert {
14425796c8dcSSimon Schubert   struct objfile_data_registration *registration;
14435796c8dcSSimon Schubert   int i;
14445796c8dcSSimon Schubert 
14455796c8dcSSimon Schubert   gdb_assert (objfile->data != NULL);
14465796c8dcSSimon Schubert 
14475796c8dcSSimon Schubert   /* Process all the save handlers.  */
14485796c8dcSSimon Schubert 
14495796c8dcSSimon Schubert   for (registration = objfile_data_registry.registrations, i = 0;
14505796c8dcSSimon Schubert        i < objfile->num_data;
14515796c8dcSSimon Schubert        registration = registration->next, i++)
14525796c8dcSSimon Schubert     if (objfile->data[i] != NULL && registration->data->save != NULL)
14535796c8dcSSimon Schubert       registration->data->save (objfile, objfile->data[i]);
14545796c8dcSSimon Schubert 
14555796c8dcSSimon Schubert   /* Now process all the free handlers.  */
14565796c8dcSSimon Schubert 
14575796c8dcSSimon Schubert   for (registration = objfile_data_registry.registrations, i = 0;
14585796c8dcSSimon Schubert        i < objfile->num_data;
14595796c8dcSSimon Schubert        registration = registration->next, i++)
14605796c8dcSSimon Schubert     if (objfile->data[i] != NULL && registration->data->free != NULL)
14615796c8dcSSimon Schubert       registration->data->free (objfile, objfile->data[i]);
14625796c8dcSSimon Schubert 
14635796c8dcSSimon Schubert   memset (objfile->data, 0, objfile->num_data * sizeof (void *));
14645796c8dcSSimon Schubert }
14655796c8dcSSimon Schubert 
14665796c8dcSSimon Schubert void
14675796c8dcSSimon Schubert set_objfile_data (struct objfile *objfile, const struct objfile_data *data,
14685796c8dcSSimon Schubert 		  void *value)
14695796c8dcSSimon Schubert {
14705796c8dcSSimon Schubert   gdb_assert (data->index < objfile->num_data);
14715796c8dcSSimon Schubert   objfile->data[data->index] = value;
14725796c8dcSSimon Schubert }
14735796c8dcSSimon Schubert 
14745796c8dcSSimon Schubert void *
14755796c8dcSSimon Schubert objfile_data (struct objfile *objfile, const struct objfile_data *data)
14765796c8dcSSimon Schubert {
14775796c8dcSSimon Schubert   gdb_assert (data->index < objfile->num_data);
14785796c8dcSSimon Schubert   return objfile->data[data->index];
14795796c8dcSSimon Schubert }
14805796c8dcSSimon Schubert 
14815796c8dcSSimon Schubert /* Set objfiles_changed_p so section map will be rebuilt next time it
14825796c8dcSSimon Schubert    is used.  Called by reread_symbols.  */
14835796c8dcSSimon Schubert 
14845796c8dcSSimon Schubert void
14855796c8dcSSimon Schubert objfiles_changed (void)
14865796c8dcSSimon Schubert {
1487cf7f2e2dSJohn Marino   /* Rebuild section map next time we need it.  */
1488cf7f2e2dSJohn Marino   get_objfile_pspace_data (current_program_space)->objfiles_changed_p = 1;
1489cf7f2e2dSJohn Marino }
1490cf7f2e2dSJohn Marino 
1491cf7f2e2dSJohn Marino /* Close ABFD, and warn if that fails.  */
1492cf7f2e2dSJohn Marino 
1493cf7f2e2dSJohn Marino int
1494cf7f2e2dSJohn Marino gdb_bfd_close_or_warn (struct bfd *abfd)
1495cf7f2e2dSJohn Marino {
1496cf7f2e2dSJohn Marino   int ret;
1497cf7f2e2dSJohn Marino   char *name = bfd_get_filename (abfd);
1498cf7f2e2dSJohn Marino 
1499cf7f2e2dSJohn Marino   ret = bfd_close (abfd);
1500cf7f2e2dSJohn Marino 
1501cf7f2e2dSJohn Marino   if (!ret)
1502cf7f2e2dSJohn Marino     warning (_("cannot close \"%s\": %s"),
1503cf7f2e2dSJohn Marino 	     name, bfd_errmsg (bfd_get_error ()));
1504cf7f2e2dSJohn Marino 
1505cf7f2e2dSJohn Marino   return ret;
15065796c8dcSSimon Schubert }
15075796c8dcSSimon Schubert 
15085796c8dcSSimon Schubert /* Add reference to ABFD.  Returns ABFD.  */
15095796c8dcSSimon Schubert struct bfd *
15105796c8dcSSimon Schubert gdb_bfd_ref (struct bfd *abfd)
15115796c8dcSSimon Schubert {
1512cf7f2e2dSJohn Marino   int *p_refcount;
1513cf7f2e2dSJohn Marino 
1514cf7f2e2dSJohn Marino   if (abfd == NULL)
1515cf7f2e2dSJohn Marino     return NULL;
1516cf7f2e2dSJohn Marino 
1517cf7f2e2dSJohn Marino   p_refcount = bfd_usrdata (abfd);
15185796c8dcSSimon Schubert 
15195796c8dcSSimon Schubert   if (p_refcount != NULL)
15205796c8dcSSimon Schubert     {
15215796c8dcSSimon Schubert       *p_refcount += 1;
15225796c8dcSSimon Schubert       return abfd;
15235796c8dcSSimon Schubert     }
15245796c8dcSSimon Schubert 
15255796c8dcSSimon Schubert   p_refcount = xmalloc (sizeof (*p_refcount));
15265796c8dcSSimon Schubert   *p_refcount = 1;
15275796c8dcSSimon Schubert   bfd_usrdata (abfd) = p_refcount;
15285796c8dcSSimon Schubert 
15295796c8dcSSimon Schubert   return abfd;
15305796c8dcSSimon Schubert }
15315796c8dcSSimon Schubert 
15325796c8dcSSimon Schubert /* Unreference and possibly close ABFD.  */
15335796c8dcSSimon Schubert void
15345796c8dcSSimon Schubert gdb_bfd_unref (struct bfd *abfd)
15355796c8dcSSimon Schubert {
15365796c8dcSSimon Schubert   int *p_refcount;
15375796c8dcSSimon Schubert   char *name;
15385796c8dcSSimon Schubert 
15395796c8dcSSimon Schubert   if (abfd == NULL)
15405796c8dcSSimon Schubert     return;
15415796c8dcSSimon Schubert 
15425796c8dcSSimon Schubert   p_refcount = bfd_usrdata (abfd);
15435796c8dcSSimon Schubert 
15445796c8dcSSimon Schubert   /* Valid range for p_refcount: a pointer to int counter, which has a
15455796c8dcSSimon Schubert      value of 1 (single owner) or 2 (shared).  */
15465796c8dcSSimon Schubert   gdb_assert (*p_refcount == 1 || *p_refcount == 2);
15475796c8dcSSimon Schubert 
15485796c8dcSSimon Schubert   *p_refcount -= 1;
15495796c8dcSSimon Schubert   if (*p_refcount > 0)
15505796c8dcSSimon Schubert     return;
15515796c8dcSSimon Schubert 
15525796c8dcSSimon Schubert   xfree (p_refcount);
15535796c8dcSSimon Schubert   bfd_usrdata (abfd) = NULL;  /* Paranoia.  */
15545796c8dcSSimon Schubert 
15555796c8dcSSimon Schubert   name = bfd_get_filename (abfd);
1556cf7f2e2dSJohn Marino   gdb_bfd_close_or_warn (abfd);
15575796c8dcSSimon Schubert   xfree (name);
15585796c8dcSSimon Schubert }
1559cf7f2e2dSJohn Marino 
1560cf7f2e2dSJohn Marino /* Provide a prototype to silence -Wmissing-prototypes.  */
1561cf7f2e2dSJohn Marino extern initialize_file_ftype _initialize_objfiles;
1562cf7f2e2dSJohn Marino 
1563cf7f2e2dSJohn Marino void
1564cf7f2e2dSJohn Marino _initialize_objfiles (void)
1565cf7f2e2dSJohn Marino {
1566cf7f2e2dSJohn Marino   objfiles_pspace_data
1567cf7f2e2dSJohn Marino     = register_program_space_data_with_cleanup (objfiles_pspace_data_cleanup);
1568cf7f2e2dSJohn Marino }
1569