119370Spst/* GDB routines for manipulating objfiles.
2130809Smarcel
3130809Smarcel   Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4130809Smarcel   2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5130809Smarcel
619370Spst   Contributed by Cygnus Support, using pieces from other GDB modules.
719370Spst
898948Sobrien   This file is part of GDB.
919370Spst
1098948Sobrien   This program is free software; you can redistribute it and/or modify
1198948Sobrien   it under the terms of the GNU General Public License as published by
1298948Sobrien   the Free Software Foundation; either version 2 of the License, or
1398948Sobrien   (at your option) any later version.
1419370Spst
1598948Sobrien   This program is distributed in the hope that it will be useful,
1698948Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1798948Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1898948Sobrien   GNU General Public License for more details.
1919370Spst
2098948Sobrien   You should have received a copy of the GNU General Public License
2198948Sobrien   along with this program; if not, write to the Free Software
2298948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2398948Sobrien   Boston, MA 02111-1307, USA.  */
2419370Spst
2519370Spst/* This file contains support routines for creating, manipulating, and
2619370Spst   destroying objfile structures. */
2719370Spst
2819370Spst#include "defs.h"
2919370Spst#include "bfd.h"		/* Binary File Description */
3019370Spst#include "symtab.h"
3119370Spst#include "symfile.h"
3219370Spst#include "objfiles.h"
3319370Spst#include "gdb-stabs.h"
3419370Spst#include "target.h"
35130809Smarcel#include "bcache.h"
3619370Spst
37130809Smarcel#include "gdb_assert.h"
3819370Spst#include <sys/types.h>
3919370Spst#include "gdb_stat.h"
4019370Spst#include <fcntl.h>
41130809Smarcel#include "gdb_obstack.h"
4219370Spst#include "gdb_string.h"
43130809Smarcel#include "hashtab.h"
4419370Spst
4598948Sobrien#include "breakpoint.h"
46130809Smarcel#include "block.h"
47130809Smarcel#include "dictionary.h"
4898948Sobrien
4919370Spst/* Prototypes for local functions */
5019370Spst
51130809Smarcelstatic void objfile_alloc_data (struct objfile *objfile);
52130809Smarcelstatic void objfile_free_data (struct objfile *objfile);
5319370Spst
5419370Spst/* Externally visible variables that are owned by this module.
5519370Spst   See declarations in objfile.h for more info. */
5619370Spst
5798948Sobrienstruct objfile *object_files;	/* Linked list of all objfiles */
5819370Spststruct objfile *current_objfile;	/* For symbol file being read in */
5919370Spststruct objfile *symfile_objfile;	/* Main symbol table loaded from */
6019370Spststruct objfile *rt_common_objfile;	/* For runtime common symbols */
6119370Spst
6219370Spst/* Locate all mappable sections of a BFD file.
6319370Spst   objfile_p_char is a char * to get it through
6419370Spst   bfd_map_over_sections; we cast it back to its proper type.  */
6519370Spst
6646289Sdfr#ifndef TARGET_KEEP_SECTION
6746289Sdfr#define TARGET_KEEP_SECTION(ASECT)	0
6846289Sdfr#endif
6946289Sdfr
7098948Sobrien/* Called via bfd_map_over_sections to build up the section table that
7198948Sobrien   the objfile references.  The objfile contains pointers to the start
7298948Sobrien   of the table (objfile->sections) and to the first location after
7398948Sobrien   the end of the table (objfile->sections_end). */
7498948Sobrien
7519370Spststatic void
76130809Smarceladd_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
77130809Smarcel			 void *objfile_p_char)
7819370Spst{
7919370Spst  struct objfile *objfile = (struct objfile *) objfile_p_char;
8019370Spst  struct obj_section section;
8119370Spst  flagword aflag;
8219370Spst
8319370Spst  aflag = bfd_get_section_flags (abfd, asect);
8446289Sdfr
8598948Sobrien  if (!(aflag & SEC_ALLOC) && !(TARGET_KEEP_SECTION (asect)))
8619370Spst    return;
8746289Sdfr
8819370Spst  if (0 == bfd_section_size (abfd, asect))
8919370Spst    return;
9019370Spst  section.offset = 0;
9119370Spst  section.objfile = objfile;
9219370Spst  section.the_bfd_section = asect;
9346289Sdfr  section.ovly_mapped = 0;
9419370Spst  section.addr = bfd_section_vma (abfd, asect);
9519370Spst  section.endaddr = section.addr + bfd_section_size (abfd, asect);
96130809Smarcel  obstack_grow (&objfile->objfile_obstack, (char *) &section, sizeof (section));
9719370Spst  objfile->sections_end = (struct obj_section *) (((unsigned long) objfile->sections_end) + 1);
9819370Spst}
9919370Spst
10019370Spst/* Builds a section table for OBJFILE.
10119370Spst   Returns 0 if OK, 1 on error (in which case bfd_error contains the
10298948Sobrien   error).
10319370Spst
10498948Sobrien   Note that while we are building the table, which goes into the
10598948Sobrien   psymbol obstack, we hijack the sections_end pointer to instead hold
10698948Sobrien   a count of the number of sections.  When bfd_map_over_sections
10798948Sobrien   returns, this count is used to compute the pointer to the end of
10898948Sobrien   the sections table, which then overwrites the count.
10998948Sobrien
11098948Sobrien   Also note that the OFFSET and OVLY_MAPPED in each table entry
11198948Sobrien   are initialized to zero.
11298948Sobrien
11398948Sobrien   Also note that if anything else writes to the psymbol obstack while
11498948Sobrien   we are building the table, we're pretty much hosed. */
11598948Sobrien
11619370Spstint
11798948Sobrienbuild_objfile_section_table (struct objfile *objfile)
11819370Spst{
11919370Spst  /* objfile->sections can be already set when reading a mapped symbol
12019370Spst     file.  I believe that we do need to rebuild the section table in
12119370Spst     this case (we rebuild other things derived from the bfd), but we
122130809Smarcel     can't free the old one (it's in the objfile_obstack).  So we just
12319370Spst     waste some memory.  */
12419370Spst
12519370Spst  objfile->sections_end = 0;
12698948Sobrien  bfd_map_over_sections (objfile->obfd, add_to_objfile_sections, (char *) objfile);
12719370Spst  objfile->sections = (struct obj_section *)
128130809Smarcel    obstack_finish (&objfile->objfile_obstack);
12919370Spst  objfile->sections_end = objfile->sections + (unsigned long) objfile->sections_end;
13098948Sobrien  return (0);
13119370Spst}
13219370Spst
13398948Sobrien/* Given a pointer to an initialized bfd (ABFD) and some flag bits
13498948Sobrien   allocate a new objfile struct, fill it in as best we can, link it
13598948Sobrien   into the list of all known objfiles, and return a pointer to the
13698948Sobrien   new objfile struct.
13719370Spst
13898948Sobrien   The FLAGS word contains various bits (OBJF_*) that can be taken as
139130809Smarcel   requests for specific operations.  Other bits like OBJF_SHARED are
140130809Smarcel   simply copied through to the new objfile flags member. */
14146289Sdfr
142130809Smarcel/* NOTE: carlton/2003-02-04: This function is called with args NULL, 0
143130809Smarcel   by jv-lang.c, to create an artificial objfile used to hold
144130809Smarcel   information about dynamically-loaded Java classes.  Unfortunately,
145130809Smarcel   that branch of this function doesn't get tested very frequently, so
146130809Smarcel   it's prone to breakage.  (E.g. at one time the name was set to NULL
147130809Smarcel   in that situation, which broke a loop over all names in the dynamic
148130809Smarcel   library loader.)  If you change this function, please try to leave
149130809Smarcel   things in a consistent state even if abfd is NULL.  */
150130809Smarcel
15119370Spststruct objfile *
15298948Sobrienallocate_objfile (bfd *abfd, int flags)
15319370Spst{
15419370Spst  struct objfile *objfile = NULL;
15519370Spst  struct objfile *last_one = NULL;
15619370Spst
15719370Spst  /* If we don't support mapped symbol files, didn't ask for the file to be
15819370Spst     mapped, or failed to open the mapped file for some reason, then revert
15919370Spst     back to an unmapped objfile. */
16019370Spst
16119370Spst  if (objfile == NULL)
16219370Spst    {
16319370Spst      objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
16419370Spst      memset (objfile, 0, sizeof (struct objfile));
16598948Sobrien      objfile->md = NULL;
166130809Smarcel      objfile->psymbol_cache = bcache_xmalloc ();
167130809Smarcel      objfile->macro_cache = bcache_xmalloc ();
168130809Smarcel      /* We could use obstack_specify_allocation here instead, but
169130809Smarcel	 gdb_obstack.h specifies the alloc/dealloc functions.  */
170130809Smarcel      obstack_init (&objfile->objfile_obstack);
171130809Smarcel      terminate_minimal_symbol_table (objfile);
17219370Spst    }
17319370Spst
174130809Smarcel  objfile_alloc_data (objfile);
175130809Smarcel
17619370Spst  /* Update the per-objfile information that comes from the bfd, ensuring
17719370Spst     that any data that is reference is saved in the per-objfile data
17819370Spst     region. */
17919370Spst
18098948Sobrien  objfile->obfd = abfd;
18198948Sobrien  if (objfile->name != NULL)
18219370Spst    {
18398948Sobrien      xmfree (objfile->md, objfile->name);
18419370Spst    }
18546289Sdfr  if (abfd != NULL)
18646289Sdfr    {
18798948Sobrien      objfile->name = mstrsave (objfile->md, bfd_get_filename (abfd));
18898948Sobrien      objfile->mtime = bfd_get_mtime (abfd);
18919370Spst
19046289Sdfr      /* Build section table.  */
19119370Spst
19246289Sdfr      if (build_objfile_section_table (objfile))
19346289Sdfr	{
19498948Sobrien	  error ("Can't find the file sections in `%s': %s",
19598948Sobrien		 objfile->name, bfd_errmsg (bfd_get_error ()));
19646289Sdfr	}
19719370Spst    }
198130809Smarcel  else
199130809Smarcel    {
200130809Smarcel      objfile->name = mstrsave (objfile->md, "<<anonymous objfile>>");
201130809Smarcel    }
20219370Spst
20398948Sobrien  /* Initialize the section indexes for this objfile, so that we can
20498948Sobrien     later detect if they are used w/o being properly assigned to. */
20598948Sobrien
206130809Smarcel  objfile->sect_index_text = -1;
207130809Smarcel  objfile->sect_index_data = -1;
208130809Smarcel  objfile->sect_index_bss = -1;
209130809Smarcel  objfile->sect_index_rodata = -1;
21098948Sobrien
211130809Smarcel  /* We don't yet have a C++-specific namespace symtab.  */
212130809Smarcel
213130809Smarcel  objfile->cp_namespace_symtab = NULL;
214130809Smarcel
21519370Spst  /* Add this file onto the tail of the linked list of other such files. */
21619370Spst
21798948Sobrien  objfile->next = NULL;
21819370Spst  if (object_files == NULL)
21919370Spst    object_files = objfile;
22019370Spst  else
22119370Spst    {
22219370Spst      for (last_one = object_files;
22398948Sobrien	   last_one->next;
22498948Sobrien	   last_one = last_one->next);
22598948Sobrien      last_one->next = objfile;
22619370Spst    }
22746289Sdfr
22898948Sobrien  /* Save passed in flag bits. */
22998948Sobrien  objfile->flags |= flags;
23046289Sdfr
23119370Spst  return (objfile);
23219370Spst}
23319370Spst
234130809Smarcel/* Initialize entry point information for this objfile. */
235130809Smarcel
236130809Smarcelvoid
237130809Smarcelinit_entry_point_info (struct objfile *objfile)
238130809Smarcel{
239130809Smarcel  /* Save startup file's range of PC addresses to help blockframe.c
240130809Smarcel     decide where the bottom of the stack is.  */
241130809Smarcel
242130809Smarcel  if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
243130809Smarcel    {
244130809Smarcel      /* Executable file -- record its entry point so we'll recognize
245130809Smarcel         the startup file because it contains the entry point.  */
246130809Smarcel      objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
247130809Smarcel    }
248130809Smarcel  else
249130809Smarcel    {
250130809Smarcel      /* Examination of non-executable.o files.  Short-circuit this stuff.  */
251130809Smarcel      objfile->ei.entry_point = INVALID_ENTRY_POINT;
252130809Smarcel    }
253130809Smarcel  objfile->ei.deprecated_entry_file_lowpc = INVALID_ENTRY_LOWPC;
254130809Smarcel  objfile->ei.deprecated_entry_file_highpc = INVALID_ENTRY_HIGHPC;
255130809Smarcel  objfile->ei.entry_func_lowpc = INVALID_ENTRY_LOWPC;
256130809Smarcel  objfile->ei.entry_func_highpc = INVALID_ENTRY_HIGHPC;
257130809Smarcel  objfile->ei.main_func_lowpc = INVALID_ENTRY_LOWPC;
258130809Smarcel  objfile->ei.main_func_highpc = INVALID_ENTRY_HIGHPC;
259130809Smarcel}
260130809Smarcel
261130809Smarcel/* Get current entry point address.  */
262130809Smarcel
263130809SmarcelCORE_ADDR
264130809Smarcelentry_point_address (void)
265130809Smarcel{
266130809Smarcel  return symfile_objfile ? symfile_objfile->ei.entry_point : 0;
267130809Smarcel}
268130809Smarcel
269130809Smarcel/* Create the terminating entry of OBJFILE's minimal symbol table.
270130809Smarcel   If OBJFILE->msymbols is zero, allocate a single entry from
271130809Smarcel   OBJFILE->objfile_obstack; otherwise, just initialize
272130809Smarcel   OBJFILE->msymbols[OBJFILE->minimal_symbol_count].  */
273130809Smarcelvoid
274130809Smarcelterminate_minimal_symbol_table (struct objfile *objfile)
275130809Smarcel{
276130809Smarcel  if (! objfile->msymbols)
277130809Smarcel    objfile->msymbols = ((struct minimal_symbol *)
278130809Smarcel                         obstack_alloc (&objfile->objfile_obstack,
279130809Smarcel                                        sizeof (objfile->msymbols[0])));
280130809Smarcel
281130809Smarcel  {
282130809Smarcel    struct minimal_symbol *m
283130809Smarcel      = &objfile->msymbols[objfile->minimal_symbol_count];
284130809Smarcel
285130809Smarcel    memset (m, 0, sizeof (*m));
286130809Smarcel    /* Don't rely on these enumeration values being 0's.  */
287130809Smarcel    MSYMBOL_TYPE (m) = mst_unknown;
288130809Smarcel    SYMBOL_INIT_LANGUAGE_SPECIFIC (m, language_unknown);
289130809Smarcel  }
290130809Smarcel}
291130809Smarcel
292130809Smarcel
293130809Smarcel/* Put one object file before a specified on in the global list.
294130809Smarcel   This can be used to make sure an object file is destroyed before
295130809Smarcel   another when using ALL_OBJFILES_SAFE to free all objfiles. */
296130809Smarcelvoid
297130809Smarcelput_objfile_before (struct objfile *objfile, struct objfile *before_this)
298130809Smarcel{
299130809Smarcel  struct objfile **objp;
300130809Smarcel
301130809Smarcel  unlink_objfile (objfile);
302130809Smarcel
303130809Smarcel  for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
304130809Smarcel    {
305130809Smarcel      if (*objp == before_this)
306130809Smarcel	{
307130809Smarcel	  objfile->next = *objp;
308130809Smarcel	  *objp = objfile;
309130809Smarcel	  return;
310130809Smarcel	}
311130809Smarcel    }
312130809Smarcel
313130809Smarcel  internal_error (__FILE__, __LINE__,
314130809Smarcel		  "put_objfile_before: before objfile not in list");
315130809Smarcel}
316130809Smarcel
31719370Spst/* Put OBJFILE at the front of the list.  */
31819370Spst
31919370Spstvoid
32098948Sobrienobjfile_to_front (struct objfile *objfile)
32119370Spst{
32219370Spst  struct objfile **objp;
32319370Spst  for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
32419370Spst    {
32519370Spst      if (*objp == objfile)
32619370Spst	{
32719370Spst	  /* Unhook it from where it is.  */
32819370Spst	  *objp = objfile->next;
32919370Spst	  /* Put it in the front.  */
33019370Spst	  objfile->next = object_files;
33119370Spst	  object_files = objfile;
33219370Spst	  break;
33319370Spst	}
33419370Spst    }
33519370Spst}
33619370Spst
33719370Spst/* Unlink OBJFILE from the list of known objfiles, if it is found in the
33819370Spst   list.
33919370Spst
34019370Spst   It is not a bug, or error, to call this function if OBJFILE is not known
34119370Spst   to be in the current list.  This is done in the case of mapped objfiles,
34219370Spst   for example, just to ensure that the mapped objfile doesn't appear twice
34319370Spst   in the list.  Since the list is threaded, linking in a mapped objfile
34419370Spst   twice would create a circular list.
34519370Spst
34619370Spst   If OBJFILE turns out to be in the list, we zap it's NEXT pointer after
34719370Spst   unlinking it, just to ensure that we have completely severed any linkages
34819370Spst   between the OBJFILE and the list. */
34919370Spst
35019370Spstvoid
35198948Sobrienunlink_objfile (struct objfile *objfile)
35219370Spst{
35398948Sobrien  struct objfile **objpp;
35419370Spst
35598948Sobrien  for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next))
35619370Spst    {
35798948Sobrien      if (*objpp == objfile)
35819370Spst	{
35998948Sobrien	  *objpp = (*objpp)->next;
36098948Sobrien	  objfile->next = NULL;
36198948Sobrien	  return;
36219370Spst	}
36319370Spst    }
36498948Sobrien
36598948Sobrien  internal_error (__FILE__, __LINE__,
36698948Sobrien		  "unlink_objfile: objfile already unlinked");
36719370Spst}
36819370Spst
36919370Spst
37019370Spst/* Destroy an objfile and all the symtabs and psymtabs under it.  Note
371130809Smarcel   that as much as possible is allocated on the objfile_obstack
372130809Smarcel   so that the memory can be efficiently freed.
37319370Spst
37419370Spst   Things which we do NOT free because they are not in malloc'd memory
37519370Spst   or not in memory specific to the objfile include:
37619370Spst
37798948Sobrien   objfile -> sf
37819370Spst
37919370Spst   FIXME:  If the objfile is using reusable symbol information (via mmalloc),
38019370Spst   then we need to take into account the fact that more than one process
38119370Spst   may be using the symbol information at the same time (when mmalloc is
38219370Spst   extended to support cooperative locking).  When more than one process
38319370Spst   is using the mapped symbol info, we need to be more careful about when
38419370Spst   we free objects in the reusable area. */
38519370Spst
38619370Spstvoid
38798948Sobrienfree_objfile (struct objfile *objfile)
38819370Spst{
389130809Smarcel  if (objfile->separate_debug_objfile)
390130809Smarcel    {
391130809Smarcel      free_objfile (objfile->separate_debug_objfile);
392130809Smarcel    }
393130809Smarcel
394130809Smarcel  if (objfile->separate_debug_objfile_backlink)
395130809Smarcel    {
396130809Smarcel      /* We freed the separate debug file, make sure the base objfile
397130809Smarcel	 doesn't reference it.  */
398130809Smarcel      objfile->separate_debug_objfile_backlink->separate_debug_objfile = NULL;
399130809Smarcel    }
400130809Smarcel
40119370Spst  /* First do any symbol file specific actions required when we are
40219370Spst     finished with a particular symbol file.  Note that if the objfile
40319370Spst     is using reusable symbol information (via mmalloc) then each of
40419370Spst     these routines is responsible for doing the correct thing, either
40519370Spst     freeing things which are valid only during this particular gdb
40619370Spst     execution, or leaving them to be reused during the next one. */
40719370Spst
40898948Sobrien  if (objfile->sf != NULL)
40919370Spst    {
41098948Sobrien      (*objfile->sf->sym_finish) (objfile);
41119370Spst    }
41219370Spst
41319370Spst  /* We always close the bfd. */
41419370Spst
41598948Sobrien  if (objfile->obfd != NULL)
41619370Spst    {
41719370Spst      char *name = bfd_get_filename (objfile->obfd);
41898948Sobrien      if (!bfd_close (objfile->obfd))
41919370Spst	warning ("cannot close \"%s\": %s",
42019370Spst		 name, bfd_errmsg (bfd_get_error ()));
42198948Sobrien      xfree (name);
42219370Spst    }
42319370Spst
42419370Spst  /* Remove it from the chain of all objfiles. */
42519370Spst
42619370Spst  unlink_objfile (objfile);
42719370Spst
42819370Spst  /* If we are going to free the runtime common objfile, mark it
42919370Spst     as unallocated.  */
43019370Spst
43119370Spst  if (objfile == rt_common_objfile)
43219370Spst    rt_common_objfile = NULL;
43319370Spst
43419370Spst  /* Before the symbol table code was redone to make it easier to
43519370Spst     selectively load and remove information particular to a specific
43619370Spst     linkage unit, gdb used to do these things whenever the monolithic
43719370Spst     symbol table was blown away.  How much still needs to be done
43819370Spst     is unknown, but we play it safe for now and keep each action until
43919370Spst     it is shown to be no longer needed. */
44098948Sobrien
44119370Spst  /* I *think* all our callers call clear_symtab_users.  If so, no need
44219370Spst     to call this here.  */
44319370Spst  clear_pc_function_cache ();
44419370Spst
445130809Smarcel  /* The last thing we do is free the objfile struct itself. */
44619370Spst
447130809Smarcel  objfile_free_data (objfile);
448130809Smarcel  if (objfile->name != NULL)
44919370Spst    {
450130809Smarcel      xmfree (objfile->md, objfile->name);
45119370Spst    }
452130809Smarcel  if (objfile->global_psymbols.list)
453130809Smarcel    xmfree (objfile->md, objfile->global_psymbols.list);
454130809Smarcel  if (objfile->static_psymbols.list)
455130809Smarcel    xmfree (objfile->md, objfile->static_psymbols.list);
456130809Smarcel  /* Free the obstacks for non-reusable objfiles */
457130809Smarcel  bcache_xfree (objfile->psymbol_cache);
458130809Smarcel  bcache_xfree (objfile->macro_cache);
459130809Smarcel  if (objfile->demangled_names_hash)
460130809Smarcel    htab_delete (objfile->demangled_names_hash);
461130809Smarcel  obstack_free (&objfile->objfile_obstack, 0);
462130809Smarcel  xmfree (objfile->md, objfile);
463130809Smarcel  objfile = NULL;
46419370Spst}
46519370Spst
46698948Sobrienstatic void
46798948Sobriendo_free_objfile_cleanup (void *obj)
46898948Sobrien{
46998948Sobrien  free_objfile (obj);
47098948Sobrien}
47119370Spst
47298948Sobrienstruct cleanup *
47398948Sobrienmake_cleanup_free_objfile (struct objfile *obj)
47498948Sobrien{
47598948Sobrien  return make_cleanup (do_free_objfile_cleanup, obj);
47698948Sobrien}
47798948Sobrien
47819370Spst/* Free all the object files at once and clean up their users.  */
47919370Spst
48019370Spstvoid
48198948Sobrienfree_all_objfiles (void)
48219370Spst{
48319370Spst  struct objfile *objfile, *temp;
48419370Spst
48519370Spst  ALL_OBJFILES_SAFE (objfile, temp)
48698948Sobrien  {
48798948Sobrien    free_objfile (objfile);
48898948Sobrien  }
48919370Spst  clear_symtab_users ();
49019370Spst}
49119370Spst
49219370Spst/* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
49319370Spst   entries in new_offsets.  */
49419370Spstvoid
49598948Sobrienobjfile_relocate (struct objfile *objfile, struct section_offsets *new_offsets)
49619370Spst{
49798948Sobrien  struct section_offsets *delta =
498130809Smarcel    ((struct section_offsets *)
499130809Smarcel     alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
50019370Spst
50119370Spst  {
50219370Spst    int i;
50319370Spst    int something_changed = 0;
50419370Spst    for (i = 0; i < objfile->num_sections; ++i)
50519370Spst      {
50698948Sobrien	delta->offsets[i] =
50719370Spst	  ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i);
50819370Spst	if (ANOFFSET (delta, i) != 0)
50919370Spst	  something_changed = 1;
51019370Spst      }
51119370Spst    if (!something_changed)
51219370Spst      return;
51319370Spst  }
51419370Spst
51519370Spst  /* OK, get all the symtabs.  */
51619370Spst  {
51719370Spst    struct symtab *s;
51819370Spst
51919370Spst    ALL_OBJFILE_SYMTABS (objfile, s)
52098948Sobrien    {
52198948Sobrien      struct linetable *l;
52298948Sobrien      struct blockvector *bv;
52398948Sobrien      int i;
52419370Spst
52598948Sobrien      /* First the line table.  */
52698948Sobrien      l = LINETABLE (s);
52798948Sobrien      if (l)
52898948Sobrien	{
52998948Sobrien	  for (i = 0; i < l->nitems; ++i)
53098948Sobrien	    l->item[i].pc += ANOFFSET (delta, s->block_line_section);
53198948Sobrien	}
53219370Spst
53398948Sobrien      /* Don't relocate a shared blockvector more than once.  */
53498948Sobrien      if (!s->primary)
53598948Sobrien	continue;
53619370Spst
53798948Sobrien      bv = BLOCKVECTOR (s);
53898948Sobrien      for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
53998948Sobrien	{
54098948Sobrien	  struct block *b;
54198948Sobrien	  struct symbol *sym;
542130809Smarcel	  struct dict_iterator iter;
54398948Sobrien
54498948Sobrien	  b = BLOCKVECTOR_BLOCK (bv, i);
54598948Sobrien	  BLOCK_START (b) += ANOFFSET (delta, s->block_line_section);
54698948Sobrien	  BLOCK_END (b) += ANOFFSET (delta, s->block_line_section);
54798948Sobrien
548130809Smarcel	  ALL_BLOCK_SYMBOLS (b, iter, sym)
54998948Sobrien	    {
55098948Sobrien	      fixup_symbol_section (sym, objfile);
55198948Sobrien
55298948Sobrien	      /* The RS6000 code from which this was taken skipped
553130809Smarcel	         any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
55498948Sobrien	         But I'm leaving out that test, on the theory that
55598948Sobrien	         they can't possibly pass the tests below.  */
55698948Sobrien	      if ((SYMBOL_CLASS (sym) == LOC_LABEL
55798948Sobrien		   || SYMBOL_CLASS (sym) == LOC_STATIC
55898948Sobrien		   || SYMBOL_CLASS (sym) == LOC_INDIRECT)
55998948Sobrien		  && SYMBOL_SECTION (sym) >= 0)
56098948Sobrien		{
56198948Sobrien		  SYMBOL_VALUE_ADDRESS (sym) +=
56298948Sobrien		    ANOFFSET (delta, SYMBOL_SECTION (sym));
56398948Sobrien		}
56419370Spst#ifdef MIPS_EFI_SYMBOL_NAME
56598948Sobrien	      /* Relocate Extra Function Info for ecoff.  */
56619370Spst
56798948Sobrien	      else if (SYMBOL_CLASS (sym) == LOC_CONST
568130809Smarcel		       && SYMBOL_DOMAIN (sym) == LABEL_DOMAIN
569130809Smarcel		       && strcmp (DEPRECATED_SYMBOL_NAME (sym), MIPS_EFI_SYMBOL_NAME) == 0)
57098948Sobrien		ecoff_relocate_efi (sym, ANOFFSET (delta,
57146289Sdfr						   s->block_line_section));
57219370Spst#endif
57398948Sobrien	    }
57498948Sobrien	}
57598948Sobrien    }
57619370Spst  }
57719370Spst
57819370Spst  {
57919370Spst    struct partial_symtab *p;
58019370Spst
58119370Spst    ALL_OBJFILE_PSYMTABS (objfile, p)
58298948Sobrien    {
58398948Sobrien      p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
58498948Sobrien      p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
58598948Sobrien    }
58619370Spst  }
58719370Spst
58819370Spst  {
58919370Spst    struct partial_symbol **psym;
59019370Spst
59119370Spst    for (psym = objfile->global_psymbols.list;
59219370Spst	 psym < objfile->global_psymbols.next;
59319370Spst	 psym++)
59498948Sobrien      {
59598948Sobrien	fixup_psymbol_section (*psym, objfile);
59698948Sobrien	if (SYMBOL_SECTION (*psym) >= 0)
59798948Sobrien	  SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
59898948Sobrien						    SYMBOL_SECTION (*psym));
59998948Sobrien      }
60019370Spst    for (psym = objfile->static_psymbols.list;
60119370Spst	 psym < objfile->static_psymbols.next;
60219370Spst	 psym++)
60398948Sobrien      {
60498948Sobrien	fixup_psymbol_section (*psym, objfile);
60598948Sobrien	if (SYMBOL_SECTION (*psym) >= 0)
60698948Sobrien	  SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
60798948Sobrien						    SYMBOL_SECTION (*psym));
60898948Sobrien      }
60919370Spst  }
61019370Spst
61119370Spst  {
61219370Spst    struct minimal_symbol *msym;
61319370Spst    ALL_OBJFILE_MSYMBOLS (objfile, msym)
61419370Spst      if (SYMBOL_SECTION (msym) >= 0)
61598948Sobrien      SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym));
61619370Spst  }
61719370Spst  /* Relocating different sections by different amounts may cause the symbols
61819370Spst     to be out of order.  */
61919370Spst  msymbols_sort (objfile);
62019370Spst
62119370Spst  {
62219370Spst    int i;
62319370Spst    for (i = 0; i < objfile->num_sections; ++i)
62498948Sobrien      (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i);
62519370Spst  }
62619370Spst
62798948Sobrien  if (objfile->ei.entry_point != ~(CORE_ADDR) 0)
62898948Sobrien    {
62998948Sobrien      /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT
63098948Sobrien	 only as a fallback.  */
63198948Sobrien      struct obj_section *s;
63298948Sobrien      s = find_pc_section (objfile->ei.entry_point);
63398948Sobrien      if (s)
63498948Sobrien        objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index);
63598948Sobrien      else
63698948Sobrien        objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
63798948Sobrien    }
63898948Sobrien
63919370Spst  {
64019370Spst    struct obj_section *s;
64119370Spst    bfd *abfd;
64219370Spst
64319370Spst    abfd = objfile->obfd;
64419370Spst
64598948Sobrien    ALL_OBJFILE_OSECTIONS (objfile, s)
64619370Spst      {
64798948Sobrien      	int idx = s->the_bfd_section->index;
64898948Sobrien
64998948Sobrien	s->addr += ANOFFSET (delta, idx);
65098948Sobrien	s->endaddr += ANOFFSET (delta, idx);
65119370Spst      }
65219370Spst  }
65319370Spst
65419370Spst  if (objfile->ei.entry_func_lowpc != INVALID_ENTRY_LOWPC)
65519370Spst    {
65698948Sobrien      objfile->ei.entry_func_lowpc += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
65798948Sobrien      objfile->ei.entry_func_highpc += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
65819370Spst    }
65919370Spst
660130809Smarcel  if (objfile->ei.deprecated_entry_file_lowpc != INVALID_ENTRY_LOWPC)
66119370Spst    {
662130809Smarcel      objfile->ei.deprecated_entry_file_lowpc += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
663130809Smarcel      objfile->ei.deprecated_entry_file_highpc += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
66419370Spst    }
66519370Spst
66619370Spst  if (objfile->ei.main_func_lowpc != INVALID_ENTRY_LOWPC)
66719370Spst    {
66898948Sobrien      objfile->ei.main_func_lowpc += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
66998948Sobrien      objfile->ei.main_func_highpc += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
67019370Spst    }
67146289Sdfr
67246289Sdfr  /* Relocate breakpoints as necessary, after things are relocated. */
67346289Sdfr  breakpoint_re_set ();
67419370Spst}
67519370Spst
67619370Spst/* Many places in gdb want to test just to see if we have any partial
67719370Spst   symbols available.  This function returns zero if none are currently
67819370Spst   available, nonzero otherwise. */
67919370Spst
68019370Spstint
68198948Sobrienhave_partial_symbols (void)
68219370Spst{
68319370Spst  struct objfile *ofp;
68419370Spst
68519370Spst  ALL_OBJFILES (ofp)
68698948Sobrien  {
68798948Sobrien    if (ofp->psymtabs != NULL)
68898948Sobrien      {
68998948Sobrien	return 1;
69098948Sobrien      }
69198948Sobrien  }
69219370Spst  return 0;
69319370Spst}
69419370Spst
69519370Spst/* Many places in gdb want to test just to see if we have any full
69619370Spst   symbols available.  This function returns zero if none are currently
69719370Spst   available, nonzero otherwise. */
69819370Spst
69919370Spstint
70098948Sobrienhave_full_symbols (void)
70119370Spst{
70219370Spst  struct objfile *ofp;
70319370Spst
70419370Spst  ALL_OBJFILES (ofp)
70598948Sobrien  {
70698948Sobrien    if (ofp->symtabs != NULL)
70798948Sobrien      {
70898948Sobrien	return 1;
70998948Sobrien      }
71098948Sobrien  }
71119370Spst  return 0;
71219370Spst}
71319370Spst
71446289Sdfr
71546289Sdfr/* This operations deletes all objfile entries that represent solibs that
71646289Sdfr   weren't explicitly loaded by the user, via e.g., the add-symbol-file
71746289Sdfr   command.
71898948Sobrien */
71946289Sdfrvoid
72098948Sobrienobjfile_purge_solibs (void)
72146289Sdfr{
72298948Sobrien  struct objfile *objf;
72398948Sobrien  struct objfile *temp;
72446289Sdfr
72546289Sdfr  ALL_OBJFILES_SAFE (objf, temp)
72646289Sdfr  {
72746289Sdfr    /* We assume that the solib package has been purged already, or will
72846289Sdfr       be soon.
72998948Sobrien     */
73098948Sobrien    if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
73146289Sdfr      free_objfile (objf);
73246289Sdfr  }
73346289Sdfr}
73446289Sdfr
73546289Sdfr
73619370Spst/* Many places in gdb want to test just to see if we have any minimal
73719370Spst   symbols available.  This function returns zero if none are currently
73819370Spst   available, nonzero otherwise. */
73919370Spst
74019370Spstint
74198948Sobrienhave_minimal_symbols (void)
74219370Spst{
74319370Spst  struct objfile *ofp;
74419370Spst
74519370Spst  ALL_OBJFILES (ofp)
74698948Sobrien  {
747130809Smarcel    if (ofp->minimal_symbol_count > 0)
74898948Sobrien      {
74998948Sobrien	return 1;
75098948Sobrien      }
75198948Sobrien  }
75219370Spst  return 0;
75319370Spst}
75419370Spst
755130809Smarcel/* Returns a section whose range includes PC and SECTION, or NULL if
756130809Smarcel   none found.  Note the distinction between the return type, struct
757130809Smarcel   obj_section (which is defined in gdb), and the input type "struct
758130809Smarcel   bfd_section" (which is a bfd-defined data type).  The obj_section
759130809Smarcel   contains a pointer to the "struct bfd_section".  */
76019370Spst
76119370Spststruct obj_section *
762130809Smarcelfind_pc_sect_section (CORE_ADDR pc, struct bfd_section *section)
76319370Spst{
76419370Spst  struct obj_section *s;
76519370Spst  struct objfile *objfile;
76619370Spst
76798948Sobrien  ALL_OBJSECTIONS (objfile, s)
76898948Sobrien    if ((section == 0 || section == s->the_bfd_section) &&
76998948Sobrien	s->addr <= pc && pc < s->endaddr)
77098948Sobrien      return (s);
77198948Sobrien
77298948Sobrien  return (NULL);
77319370Spst}
77419370Spst
77546289Sdfr/* Returns a section whose range includes PC or NULL if none found.
77646289Sdfr   Backward compatibility, no section.  */
77746289Sdfr
77846289Sdfrstruct obj_section *
77998948Sobrienfind_pc_section (CORE_ADDR pc)
78046289Sdfr{
78146289Sdfr  return find_pc_sect_section (pc, find_pc_mapped_section (pc));
78246289Sdfr}
78346289Sdfr
78498948Sobrien
78519370Spst/* In SVR4, we recognize a trampoline by it's section name.
78619370Spst   That is, if the pc is in a section named ".plt" then we are in
78719370Spst   a trampoline.  */
78819370Spst
78919370Spstint
79098948Sobrienin_plt_section (CORE_ADDR pc, char *name)
79119370Spst{
79219370Spst  struct obj_section *s;
79319370Spst  int retval = 0;
79498948Sobrien
79598948Sobrien  s = find_pc_section (pc);
79698948Sobrien
79719370Spst  retval = (s != NULL
79819370Spst	    && s->the_bfd_section->name != NULL
799130809Smarcel	    && strcmp (s->the_bfd_section->name, ".plt") == 0);
80098948Sobrien  return (retval);
80119370Spst}
80298948Sobrien
80398948Sobrien/* Return nonzero if NAME is in the import list of OBJFILE.  Else
80498948Sobrien   return zero.  */
80598948Sobrien
80698948Sobrienint
80798948Sobrienis_in_import_list (char *name, struct objfile *objfile)
80898948Sobrien{
809130809Smarcel  int i;
81098948Sobrien
81198948Sobrien  if (!objfile || !name || !*name)
81298948Sobrien    return 0;
81398948Sobrien
81498948Sobrien  for (i = 0; i < objfile->import_list_size; i++)
815130809Smarcel    if (objfile->import_list[i] && DEPRECATED_STREQ (name, objfile->import_list[i]))
81698948Sobrien      return 1;
81798948Sobrien  return 0;
81898948Sobrien}
819130809Smarcel
82098948Sobrien
821130809Smarcel/* Keep a registry of per-objfile data-pointers required by other GDB
822130809Smarcel   modules.  */
823130809Smarcel
824130809Smarcelstruct objfile_data
825130809Smarcel{
826130809Smarcel  unsigned index;
827130809Smarcel};
828130809Smarcel
829130809Smarcelstruct objfile_data_registration
830130809Smarcel{
831130809Smarcel  struct objfile_data *data;
832130809Smarcel  struct objfile_data_registration *next;
833130809Smarcel};
834130809Smarcel
835130809Smarcelstruct objfile_data_registry
836130809Smarcel{
837130809Smarcel  struct objfile_data_registration *registrations;
838130809Smarcel  unsigned num_registrations;
839130809Smarcel};
840130809Smarcel
841130809Smarcelstatic struct objfile_data_registry objfile_data_registry = { NULL, 0 };
842130809Smarcel
843130809Smarcelconst struct objfile_data *
844130809Smarcelregister_objfile_data (void)
845130809Smarcel{
846130809Smarcel  struct objfile_data_registration **curr;
847130809Smarcel
848130809Smarcel  /* Append new registration.  */
849130809Smarcel  for (curr = &objfile_data_registry.registrations;
850130809Smarcel       *curr != NULL; curr = &(*curr)->next);
851130809Smarcel
852130809Smarcel  *curr = XMALLOC (struct objfile_data_registration);
853130809Smarcel  (*curr)->next = NULL;
854130809Smarcel  (*curr)->data = XMALLOC (struct objfile_data);
855130809Smarcel  (*curr)->data->index = objfile_data_registry.num_registrations++;
856130809Smarcel
857130809Smarcel  return (*curr)->data;
858130809Smarcel}
859130809Smarcel
860130809Smarcelstatic void
861130809Smarcelobjfile_alloc_data (struct objfile *objfile)
862130809Smarcel{
863130809Smarcel  gdb_assert (objfile->data == NULL);
864130809Smarcel  objfile->num_data = objfile_data_registry.num_registrations;
865130809Smarcel  objfile->data = XCALLOC (objfile->num_data, void *);
866130809Smarcel}
867130809Smarcel
868130809Smarcelstatic void
869130809Smarcelobjfile_free_data (struct objfile *objfile)
870130809Smarcel{
871130809Smarcel  gdb_assert (objfile->data != NULL);
872130809Smarcel  xfree (objfile->data);
873130809Smarcel  objfile->data = NULL;
874130809Smarcel}
875130809Smarcel
876130809Smarcelvoid
877130809Smarcelclear_objfile_data (struct objfile *objfile)
878130809Smarcel{
879130809Smarcel  gdb_assert (objfile->data != NULL);
880130809Smarcel  memset (objfile->data, 0, objfile->num_data * sizeof (void *));
881130809Smarcel}
882130809Smarcel
883130809Smarcelvoid
884130809Smarcelset_objfile_data (struct objfile *objfile, const struct objfile_data *data,
885130809Smarcel		  void *value)
886130809Smarcel{
887130809Smarcel  gdb_assert (data->index < objfile->num_data);
888130809Smarcel  objfile->data[data->index] = value;
889130809Smarcel}
890130809Smarcel
891130809Smarcelvoid *
892130809Smarcelobjfile_data (struct objfile *objfile, const struct objfile_data *data)
893130809Smarcel{
894130809Smarcel  gdb_assert (data->index < objfile->num_data);
895130809Smarcel  return objfile->data[data->index];
896130809Smarcel}
897