gdb/objfiles.c - gdb

Global variables defined

Functions defined

Source code

  1. /* GDB routines for manipulating objfiles.

  2.    Copyright (C) 1992-2015 Free Software Foundation, Inc.

  3.    Contributed by Cygnus Support, using pieces from other GDB modules.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. /* This file contains support routines for creating, manipulating, and
  16.    destroying objfile structures.  */

  17. #include "defs.h"
  18. #include "bfd.h"                /* Binary File Description */
  19. #include "symtab.h"
  20. #include "symfile.h"
  21. #include "objfiles.h"
  22. #include "gdb-stabs.h"
  23. #include "target.h"
  24. #include "bcache.h"
  25. #include "expression.h"
  26. #include "parser-defs.h"

  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include "gdb_obstack.h"
  31. #include "hashtab.h"

  32. #include "breakpoint.h"
  33. #include "block.h"
  34. #include "dictionary.h"
  35. #include "source.h"
  36. #include "addrmap.h"
  37. #include "arch-utils.h"
  38. #include "exec.h"
  39. #include "observer.h"
  40. #include "complaints.h"
  41. #include "psymtab.h"
  42. #include "solist.h"
  43. #include "gdb_bfd.h"
  44. #include "btrace.h"

  45. /* Keep a registry of per-objfile data-pointers required by other GDB
  46.    modules.  */

  47. DEFINE_REGISTRY (objfile, REGISTRY_ACCESS_FIELD)

  48. /* Externally visible variables that are owned by this module.
  49.    See declarations in objfile.h for more info.  */

  50. struct objfile_pspace_info
  51. {
  52.   struct obj_section **sections;
  53.   int num_sections;

  54.   /* Nonzero if object files have been added since the section map
  55.      was last updated.  */
  56.   int new_objfiles_available;

  57.   /* Nonzero if the section map MUST be updated before use.  */
  58.   int section_map_dirty;

  59.   /* Nonzero if section map updates should be inhibited if possible.  */
  60.   int inhibit_updates;
  61. };

  62. /* Per-program-space data key.  */
  63. static const struct program_space_data *objfiles_pspace_data;

  64. static void
  65. objfiles_pspace_data_cleanup (struct program_space *pspace, void *arg)
  66. {
  67.   struct objfile_pspace_info *info = arg;

  68.   xfree (info->sections);
  69.   xfree (info);
  70. }

  71. /* Get the current svr4 data.  If none is found yet, add it now.  This
  72.    function always returns a valid object.  */

  73. static struct objfile_pspace_info *
  74. get_objfile_pspace_data (struct program_space *pspace)
  75. {
  76.   struct objfile_pspace_info *info;

  77.   info = program_space_data (pspace, objfiles_pspace_data);
  78.   if (info == NULL)
  79.     {
  80.       info = XCNEW (struct objfile_pspace_info);
  81.       set_program_space_data (pspace, objfiles_pspace_data, info);
  82.     }

  83.   return info;
  84. }



  85. /* Per-BFD data key.  */

  86. static const struct bfd_data *objfiles_bfd_data;

  87. /* Create the per-BFD storage object for OBJFILE.  If ABFD is not
  88.    NULL, and it already has a per-BFD storage object, use that.
  89.    Otherwise, allocate a new per-BFD storage object.  If ABFD is not
  90.    NULL, the object is allocated on the BFD; otherwise it is allocated
  91.    on OBJFILE's obstack.  Note that it is not safe to call this
  92.    multiple times for a given OBJFILE -- it can only be called when
  93.    allocating or re-initializing OBJFILE.  */

  94. static struct objfile_per_bfd_storage *
  95. get_objfile_bfd_data (struct objfile *objfile, struct bfd *abfd)
  96. {
  97.   struct objfile_per_bfd_storage *storage = NULL;

  98.   if (abfd != NULL)
  99.     storage = bfd_data (abfd, objfiles_bfd_data);

  100.   if (storage == NULL)
  101.     {
  102.       /* If the object requires gdb to do relocations, we simply fall
  103.          back to not sharing data across users.  These cases are rare
  104.          enough that this seems reasonable.  */
  105.       if (abfd != NULL && !gdb_bfd_requires_relocations (abfd))
  106.         {
  107.           storage = bfd_zalloc (abfd, sizeof (struct objfile_per_bfd_storage));
  108.           set_bfd_data (abfd, objfiles_bfd_data, storage);
  109.         }
  110.       else
  111.         storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  112.                                   struct objfile_per_bfd_storage);

  113.       /* Look up the gdbarch associated with the BFD.  */
  114.       if (abfd != NULL)
  115.         storage->gdbarch = gdbarch_from_bfd (abfd);

  116.       obstack_init (&storage->storage_obstack);
  117.       storage->filename_cache = bcache_xmalloc (NULL, NULL);
  118.       storage->macro_cache = bcache_xmalloc (NULL, NULL);
  119.       storage->language_of_main = language_unknown;
  120.     }

  121.   return storage;
  122. }

  123. /* Free STORAGE.  */

  124. static void
  125. free_objfile_per_bfd_storage (struct objfile_per_bfd_storage *storage)
  126. {
  127.   bcache_xfree (storage->filename_cache);
  128.   bcache_xfree (storage->macro_cache);
  129.   if (storage->demangled_names_hash)
  130.     htab_delete (storage->demangled_names_hash);
  131.   obstack_free (&storage->storage_obstack, 0);
  132. }

  133. /* A wrapper for free_objfile_per_bfd_storage that can be passed as a
  134.    cleanup function to the BFD registry.  */

  135. static void
  136. objfile_bfd_data_free (struct bfd *unused, void *d)
  137. {
  138.   free_objfile_per_bfd_storage (d);
  139. }

  140. /* See objfiles.h.  */

  141. void
  142. set_objfile_per_bfd (struct objfile *objfile)
  143. {
  144.   objfile->per_bfd = get_objfile_bfd_data (objfile, objfile->obfd);
  145. }

  146. /* Set the objfile's per-BFD notion of the "main" name and
  147.    language.  */

  148. void
  149. set_objfile_main_name (struct objfile *objfile,
  150.                        const char *name, enum language lang)
  151. {
  152.   if (objfile->per_bfd->name_of_main == NULL
  153.       || strcmp (objfile->per_bfd->name_of_main, name) != 0)
  154.     objfile->per_bfd->name_of_main
  155.       = obstack_copy0 (&objfile->per_bfd->storage_obstack, name, strlen (name));
  156.   objfile->per_bfd->language_of_main = lang;
  157. }



  158. /* Called via bfd_map_over_sections to build up the section table that
  159.    the objfile references.  The objfile contains pointers to the start
  160.    of the table (objfile->sections) and to the first location after
  161.    the end of the table (objfile->sections_end).  */

  162. static void
  163. add_to_objfile_sections_full (struct bfd *abfd, struct bfd_section *asect,
  164.                               struct objfile *objfile, int force)
  165. {
  166.   struct obj_section *section;

  167.   if (!force)
  168.     {
  169.       flagword aflag;

  170.       aflag = bfd_get_section_flags (abfd, asect);
  171.       if (!(aflag & SEC_ALLOC))
  172.         return;
  173.     }

  174.   section = &objfile->sections[gdb_bfd_section_index (abfd, asect)];
  175.   section->objfile = objfile;
  176.   section->the_bfd_section = asect;
  177.   section->ovly_mapped = 0;
  178. }

  179. static void
  180. add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
  181.                          void *objfilep)
  182. {
  183.   add_to_objfile_sections_full (abfd, asect, objfilep, 0);
  184. }

  185. /* Builds a section table for OBJFILE.

  186.    Note that the OFFSET and OVLY_MAPPED in each table entry are
  187.    initialized to zero.  */

  188. void
  189. build_objfile_section_table (struct objfile *objfile)
  190. {
  191.   int count = gdb_bfd_count_sections (objfile->obfd);

  192.   objfile->sections = OBSTACK_CALLOC (&objfile->objfile_obstack,
  193.                                       count,
  194.                                       struct obj_section);
  195.   objfile->sections_end = (objfile->sections + count);
  196.   bfd_map_over_sections (objfile->obfd,
  197.                          add_to_objfile_sections, (void *) objfile);

  198.   /* See gdb_bfd_section_index.  */
  199.   add_to_objfile_sections_full (objfile->obfd, bfd_com_section_ptr, objfile, 1);
  200.   add_to_objfile_sections_full (objfile->obfd, bfd_und_section_ptr, objfile, 1);
  201.   add_to_objfile_sections_full (objfile->obfd, bfd_abs_section_ptr, objfile, 1);
  202.   add_to_objfile_sections_full (objfile->obfd, bfd_ind_section_ptr, objfile, 1);
  203. }

  204. /* Given a pointer to an initialized bfd (ABFD) and some flag bits
  205.    allocate a new objfile struct, fill it in as best we can, link it
  206.    into the list of all known objfiles, and return a pointer to the
  207.    new objfile struct.

  208.    NAME should contain original non-canonicalized filename or other
  209.    identifier as entered by user.  If there is no better source use
  210.    bfd_get_filename (ABFD).  NAME may be NULL only if ABFD is NULL.
  211.    NAME content is copied into returned objfile.

  212.    The FLAGS word contains various bits (OBJF_*) that can be taken as
  213.    requests for specific operations.  Other bits like OBJF_SHARED are
  214.    simply copied through to the new objfile flags member.  */

  215. /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0
  216.    by jv-lang.c, to create an artificial objfile used to hold
  217.    information about dynamically-loaded Java classes.  Unfortunately,
  218.    that branch of this function doesn't get tested very frequently, so
  219.    it's prone to breakage.  (E.g. at one time the name was set to NULL
  220.    in that situation, which broke a loop over all names in the dynamic
  221.    library loader.)  If you change this function, please try to leave
  222.    things in a consistent state even if abfd is NULL.  */

  223. struct objfile *
  224. allocate_objfile (bfd *abfd, const char *name, int flags)
  225. {
  226.   struct objfile *objfile;
  227.   char *expanded_name;

  228.   objfile = (struct objfile *) xzalloc (sizeof (struct objfile));
  229.   objfile->psymbol_cache = psymbol_bcache_init ();
  230.   /* We could use obstack_specify_allocation here instead, but
  231.      gdb_obstack.h specifies the alloc/dealloc functions.  */
  232.   obstack_init (&objfile->objfile_obstack);

  233.   objfile_alloc_data (objfile);

  234.   if (name == NULL)
  235.     {
  236.       gdb_assert (abfd == NULL);
  237.       gdb_assert ((flags & OBJF_NOT_FILENAME) != 0);
  238.       expanded_name = xstrdup ("<<anonymous objfile>>");
  239.     }
  240.   else if ((flags & OBJF_NOT_FILENAME) != 0)
  241.     expanded_name = xstrdup (name);
  242.   else
  243.     expanded_name = gdb_abspath (name);
  244.   objfile->original_name = obstack_copy0 (&objfile->objfile_obstack,
  245.                                           expanded_name,
  246.                                           strlen (expanded_name));
  247.   xfree (expanded_name);

  248.   /* Update the per-objfile information that comes from the bfd, ensuring
  249.      that any data that is reference is saved in the per-objfile data
  250.      region.  */

  251.   objfile->obfd = abfd;
  252.   gdb_bfd_ref (abfd);
  253.   if (abfd != NULL)
  254.     {
  255.       objfile->mtime = bfd_get_mtime (abfd);

  256.       /* Build section table.  */
  257.       build_objfile_section_table (objfile);
  258.     }

  259.   objfile->per_bfd = get_objfile_bfd_data (objfile, abfd);
  260.   objfile->pspace = current_program_space;

  261.   terminate_minimal_symbol_table (objfile);

  262.   /* Initialize the section indexes for this objfile, so that we can
  263.      later detect if they are used w/o being properly assigned to.  */

  264.   objfile->sect_index_text = -1;
  265.   objfile->sect_index_data = -1;
  266.   objfile->sect_index_bss = -1;
  267.   objfile->sect_index_rodata = -1;

  268.   /* Add this file onto the tail of the linked list of other such files.  */

  269.   objfile->next = NULL;
  270.   if (object_files == NULL)
  271.     object_files = objfile;
  272.   else
  273.     {
  274.       struct objfile *last_one;

  275.       for (last_one = object_files;
  276.            last_one->next;
  277.            last_one = last_one->next);
  278.       last_one->next = objfile;
  279.     }

  280.   /* Save passed in flag bits.  */
  281.   objfile->flags |= flags;

  282.   /* Rebuild section map next time we need it.  */
  283.   get_objfile_pspace_data (objfile->pspace)->new_objfiles_available = 1;

  284.   return objfile;
  285. }

  286. /* Retrieve the gdbarch associated with OBJFILE.  */

  287. struct gdbarch *
  288. get_objfile_arch (const struct objfile *objfile)
  289. {
  290.   return objfile->per_bfd->gdbarch;
  291. }

  292. /* If there is a valid and known entry point, function fills *ENTRY_P with it
  293.    and returns non-zero; otherwise it returns zero.  */

  294. int
  295. entry_point_address_query (CORE_ADDR *entry_p)
  296. {
  297.   if (symfile_objfile == NULL || !symfile_objfile->per_bfd->ei.entry_point_p)
  298.     return 0;

  299.   *entry_p = (symfile_objfile->per_bfd->ei.entry_point
  300.               + ANOFFSET (symfile_objfile->section_offsets,
  301.                           symfile_objfile->per_bfd->ei.the_bfd_section_index));

  302.   return 1;
  303. }

  304. /* Get current entry point address.  Call error if it is not known.  */

  305. CORE_ADDR
  306. entry_point_address (void)
  307. {
  308.   CORE_ADDR retval;

  309.   if (!entry_point_address_query (&retval))
  310.     error (_("Entry point address is not known."));

  311.   return retval;
  312. }

  313. /* Iterator on PARENT and every separate debug objfile of PARENT.
  314.    The usage pattern is:
  315.      for (objfile = parent;
  316.           objfile;
  317.           objfile = objfile_separate_debug_iterate (parent, objfile))
  318.        ...
  319. */

  320. struct objfile *
  321. objfile_separate_debug_iterate (const struct objfile *parent,
  322.                                 const struct objfile *objfile)
  323. {
  324.   struct objfile *res;

  325.   /* If any, return the first child.  */
  326.   res = objfile->separate_debug_objfile;
  327.   if (res)
  328.     return res;

  329.   /* Common case where there is no separate debug objfile.  */
  330.   if (objfile == parent)
  331.     return NULL;

  332.   /* Return the brother if any.  Note that we don't iterate on brothers of
  333.      the parents.  */
  334.   res = objfile->separate_debug_objfile_link;
  335.   if (res)
  336.     return res;

  337.   for (res = objfile->separate_debug_objfile_backlink;
  338.        res != parent;
  339.        res = res->separate_debug_objfile_backlink)
  340.     {
  341.       gdb_assert (res != NULL);
  342.       if (res->separate_debug_objfile_link)
  343.         return res->separate_debug_objfile_link;
  344.     }
  345.   return NULL;
  346. }

  347. /* Put one object file before a specified on in the global list.
  348.    This can be used to make sure an object file is destroyed before
  349.    another when using ALL_OBJFILES_SAFE to free all objfiles.  */
  350. void
  351. put_objfile_before (struct objfile *objfile, struct objfile *before_this)
  352. {
  353.   struct objfile **objp;

  354.   unlink_objfile (objfile);

  355.   for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
  356.     {
  357.       if (*objp == before_this)
  358.         {
  359.           objfile->next = *objp;
  360.           *objp = objfile;
  361.           return;
  362.         }
  363.     }

  364.   internal_error (__FILE__, __LINE__,
  365.                   _("put_objfile_before: before objfile not in list"));
  366. }

  367. /* Unlink OBJFILE from the list of known objfiles, if it is found in the
  368.    list.

  369.    It is not a bug, or error, to call this function if OBJFILE is not known
  370.    to be in the current list.  This is done in the case of mapped objfiles,
  371.    for example, just to ensure that the mapped objfile doesn't appear twice
  372.    in the list.  Since the list is threaded, linking in a mapped objfile
  373.    twice would create a circular list.

  374.    If OBJFILE turns out to be in the list, we zap it's NEXT pointer after
  375.    unlinking it, just to ensure that we have completely severed any linkages
  376.    between the OBJFILE and the list.  */

  377. void
  378. unlink_objfile (struct objfile *objfile)
  379. {
  380.   struct objfile **objpp;

  381.   for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next))
  382.     {
  383.       if (*objpp == objfile)
  384.         {
  385.           *objpp = (*objpp)->next;
  386.           objfile->next = NULL;
  387.           return;
  388.         }
  389.     }

  390.   internal_error (__FILE__, __LINE__,
  391.                   _("unlink_objfile: objfile already unlinked"));
  392. }

  393. /* Add OBJFILE as a separate debug objfile of PARENT.  */

  394. void
  395. add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
  396. {
  397.   gdb_assert (objfile && parent);

  398.   /* Must not be already in a list.  */
  399.   gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
  400.   gdb_assert (objfile->separate_debug_objfile_link == NULL);
  401.   gdb_assert (objfile->separate_debug_objfile == NULL);
  402.   gdb_assert (parent->separate_debug_objfile_backlink == NULL);
  403.   gdb_assert (parent->separate_debug_objfile_link == NULL);

  404.   objfile->separate_debug_objfile_backlink = parent;
  405.   objfile->separate_debug_objfile_link = parent->separate_debug_objfile;
  406.   parent->separate_debug_objfile = objfile;

  407.   /* Put the separate debug object before the normal one, this is so that
  408.      usage of the ALL_OBJFILES_SAFE macro will stay safe.  */
  409.   put_objfile_before (objfile, parent);
  410. }

  411. /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
  412.    itself.  */

  413. void
  414. free_objfile_separate_debug (struct objfile *objfile)
  415. {
  416.   struct objfile *child;

  417.   for (child = objfile->separate_debug_objfile; child;)
  418.     {
  419.       struct objfile *next_child = child->separate_debug_objfile_link;
  420.       free_objfile (child);
  421.       child = next_child;
  422.     }
  423. }

  424. /* Destroy an objfile and all the symtabs and psymtabs under it.  */

  425. void
  426. free_objfile (struct objfile *objfile)
  427. {
  428.   /* First notify observers that this objfile is about to be freed.  */
  429.   observer_notify_free_objfile (objfile);

  430.   /* Free all separate debug objfiles.  */
  431.   free_objfile_separate_debug (objfile);

  432.   if (objfile->separate_debug_objfile_backlink)
  433.     {
  434.       /* We freed the separate debug file, make sure the base objfile
  435.          doesn't reference it.  */
  436.       struct objfile *child;

  437.       child = objfile->separate_debug_objfile_backlink->separate_debug_objfile;

  438.       if (child == objfile)
  439.         {
  440.           /* OBJFILE is the first child.  */
  441.           objfile->separate_debug_objfile_backlink->separate_debug_objfile =
  442.             objfile->separate_debug_objfile_link;
  443.         }
  444.       else
  445.         {
  446.           /* Find OBJFILE in the list.  */
  447.           while (1)
  448.             {
  449.               if (child->separate_debug_objfile_link == objfile)
  450.                 {
  451.                   child->separate_debug_objfile_link =
  452.                     objfile->separate_debug_objfile_link;
  453.                   break;
  454.                 }
  455.               child = child->separate_debug_objfile_link;
  456.               gdb_assert (child);
  457.             }
  458.         }
  459.     }

  460.   /* Remove any references to this objfile in the global value
  461.      lists.  */
  462.   preserve_values (objfile);

  463.   /* It still may reference data modules have associated with the objfile and
  464.      the symbol file data.  */
  465.   forget_cached_source_info_for_objfile (objfile);

  466.   breakpoint_free_objfile (objfile);
  467.   btrace_free_objfile (objfile);

  468.   /* First do any symbol file specific actions required when we are
  469.      finished with a particular symbol file.  Note that if the objfile
  470.      is using reusable symbol information (via mmalloc) then each of
  471.      these routines is responsible for doing the correct thing, either
  472.      freeing things which are valid only during this particular gdb
  473.      execution, or leaving them to be reused during the next one.  */

  474.   if (objfile->sf != NULL)
  475.     {
  476.       (*objfile->sf->sym_finish) (objfile);
  477.     }

  478.   /* Discard any data modules have associated with the objfile.  The function
  479.      still may reference objfile->obfd.  */
  480.   objfile_free_data (objfile);

  481.   if (objfile->obfd)
  482.     gdb_bfd_unref (objfile->obfd);
  483.   else
  484.     free_objfile_per_bfd_storage (objfile->per_bfd);

  485.   /* Remove it from the chain of all objfiles.  */

  486.   unlink_objfile (objfile);

  487.   if (objfile == symfile_objfile)
  488.     symfile_objfile = NULL;

  489.   /* Before the symbol table code was redone to make it easier to
  490.      selectively load and remove information particular to a specific
  491.      linkage unit, gdb used to do these things whenever the monolithic
  492.      symbol table was blown away.  How much still needs to be done
  493.      is unknown, but we play it safe for now and keep each action until
  494.      it is shown to be no longer needed.  */

  495.   /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
  496.      for example), so we need to call this here.  */
  497.   clear_pc_function_cache ();

  498.   /* Clear globals which might have pointed into a removed objfile.
  499.      FIXME: It's not clear which of these are supposed to persist
  500.      between expressions and which ought to be reset each time.  */
  501.   expression_context_block = NULL;
  502.   innermost_block = NULL;

  503.   /* Check to see if the current_source_symtab belongs to this objfile,
  504.      and if so, call clear_current_source_symtab_and_line.  */

  505.   {
  506.     struct symtab_and_line cursal = get_current_source_symtab_and_line ();

  507.     if (cursal.symtab && SYMTAB_OBJFILE (cursal.symtab) == objfile)
  508.       clear_current_source_symtab_and_line ();
  509.   }

  510.   if (objfile->global_psymbols.list)
  511.     xfree (objfile->global_psymbols.list);
  512.   if (objfile->static_psymbols.list)
  513.     xfree (objfile->static_psymbols.list);
  514.   /* Free the obstacks for non-reusable objfiles.  */
  515.   psymbol_bcache_free (objfile->psymbol_cache);
  516.   obstack_free (&objfile->objfile_obstack, 0);

  517.   /* Rebuild section map next time we need it.  */
  518.   get_objfile_pspace_data (objfile->pspace)->section_map_dirty = 1;

  519.   /* The last thing we do is free the objfile struct itself.  */
  520.   xfree (objfile);
  521. }

  522. static void
  523. do_free_objfile_cleanup (void *obj)
  524. {
  525.   free_objfile (obj);
  526. }

  527. struct cleanup *
  528. make_cleanup_free_objfile (struct objfile *obj)
  529. {
  530.   return make_cleanup (do_free_objfile_cleanup, obj);
  531. }

  532. /* Free all the object files at once and clean up their users.  */

  533. void
  534. free_all_objfiles (void)
  535. {
  536.   struct objfile *objfile, *temp;
  537.   struct so_list *so;

  538.   /* Any objfile referencewould become stale.  */
  539.   for (so = master_so_list (); so; so = so->next)
  540.     gdb_assert (so->objfile == NULL);

  541.   ALL_OBJFILES_SAFE (objfile, temp)
  542.   {
  543.     free_objfile (objfile);
  544.   }
  545.   clear_symtab_users (0);
  546. }

  547. /* A helper function for objfile_relocate1 that relocates a single
  548.    symbol.  */

  549. static void
  550. relocate_one_symbol (struct symbol *sym, struct objfile *objfile,
  551.                      struct section_offsets *delta)
  552. {
  553.   fixup_symbol_section (sym, objfile);

  554.   /* The RS6000 code from which this was taken skipped
  555.      any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
  556.      But I'm leaving out that test, on the theory that
  557.      they can't possibly pass the tests below.  */
  558.   if ((SYMBOL_CLASS (sym) == LOC_LABEL
  559.        || SYMBOL_CLASS (sym) == LOC_STATIC)
  560.       && SYMBOL_SECTION (sym) >= 0)
  561.     {
  562.       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (delta, SYMBOL_SECTION (sym));
  563.     }
  564. }

  565. /* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
  566.    entries in new_offsets.  SEPARATE_DEBUG_OBJFILE is not touched here.
  567.    Return non-zero iff any change happened.  */

  568. static int
  569. objfile_relocate1 (struct objfile *objfile,
  570.                    const struct section_offsets *new_offsets)
  571. {
  572.   struct obj_section *s;
  573.   struct section_offsets *delta =
  574.     ((struct section_offsets *)
  575.      alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));

  576.   int i;
  577.   int something_changed = 0;

  578.   for (i = 0; i < objfile->num_sections; ++i)
  579.     {
  580.       delta->offsets[i] =
  581.         ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i);
  582.       if (ANOFFSET (delta, i) != 0)
  583.         something_changed = 1;
  584.     }
  585.   if (!something_changed)
  586.     return 0;

  587.   /* OK, get all the symtabs.  */
  588.   {
  589.     struct compunit_symtab *cust;
  590.     struct symtab *s;

  591.     ALL_OBJFILE_FILETABS (objfile, cust, s)
  592.     {
  593.       struct linetable *l;
  594.       int i;

  595.       /* First the line table.  */
  596.       l = SYMTAB_LINETABLE (s);
  597.       if (l)
  598.         {
  599.           for (i = 0; i < l->nitems; ++i)
  600.             l->item[i].pc += ANOFFSET (delta,
  601.                                        COMPUNIT_BLOCK_LINE_SECTION
  602.                                          (cust));
  603.         }
  604.     }

  605.     ALL_OBJFILE_COMPUNITS (objfile, cust)
  606.     {
  607.       const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (cust);
  608.       int block_line_section = COMPUNIT_BLOCK_LINE_SECTION (cust);

  609.       if (BLOCKVECTOR_MAP (bv))
  610.         addrmap_relocate (BLOCKVECTOR_MAP (bv),
  611.                           ANOFFSET (delta, block_line_section));

  612.       for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
  613.         {
  614.           struct block *b;
  615.           struct symbol *sym;
  616.           struct dict_iterator iter;

  617.           b = BLOCKVECTOR_BLOCK (bv, i);
  618.           BLOCK_START (b) += ANOFFSET (delta, block_line_section);
  619.           BLOCK_END (b) += ANOFFSET (delta, block_line_section);

  620.           /* We only want to iterate over the local symbols, not any
  621.              symbols in included symtabs.  */
  622.           ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
  623.             {
  624.               relocate_one_symbol (sym, objfile, delta);
  625.             }
  626.         }
  627.     }
  628.   }

  629.   /* Relocate isolated symbols.  */
  630.   {
  631.     struct symbol *iter;

  632.     for (iter = objfile->template_symbols; iter; iter = iter->hash_next)
  633.       relocate_one_symbol (iter, objfile, delta);
  634.   }

  635.   if (objfile->psymtabs_addrmap)
  636.     addrmap_relocate (objfile->psymtabs_addrmap,
  637.                       ANOFFSET (delta, SECT_OFF_TEXT (objfile)));

  638.   if (objfile->sf)
  639.     objfile->sf->qf->relocate (objfile, new_offsets, delta);

  640.   {
  641.     int i;

  642.     for (i = 0; i < objfile->num_sections; ++i)
  643.       (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i);
  644.   }

  645.   /* Rebuild section map next time we need it.  */
  646.   get_objfile_pspace_data (objfile->pspace)->section_map_dirty = 1;

  647.   /* Update the table in exec_ops, used to read memory.  */
  648.   ALL_OBJFILE_OSECTIONS (objfile, s)
  649.     {
  650.       int idx = s - objfile->sections;

  651.       exec_set_section_address (bfd_get_filename (objfile->obfd), idx,
  652.                                 obj_section_addr (s));
  653.     }

  654.   /* Data changed.  */
  655.   return 1;
  656. }

  657. /* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
  658.    entries in new_offsets.  Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.

  659.    The number and ordering of sections does differ between the two objfiles.
  660.    Only their names match.  Also the file offsets will differ (objfile being
  661.    possibly prelinked but separate_debug_objfile is probably not prelinked) but
  662.    the in-memory absolute address as specified by NEW_OFFSETS must match both
  663.    files.  */

  664. void
  665. objfile_relocate (struct objfile *objfile,
  666.                   const struct section_offsets *new_offsets)
  667. {
  668.   struct objfile *debug_objfile;
  669.   int changed = 0;

  670.   changed |= objfile_relocate1 (objfile, new_offsets);

  671.   for (debug_objfile = objfile->separate_debug_objfile;
  672.        debug_objfile;
  673.        debug_objfile = objfile_separate_debug_iterate (objfile, debug_objfile))
  674.     {
  675.       struct section_addr_info *objfile_addrs;
  676.       struct section_offsets *new_debug_offsets;
  677.       struct cleanup *my_cleanups;

  678.       objfile_addrs = build_section_addr_info_from_objfile (objfile);
  679.       my_cleanups = make_cleanup (xfree, objfile_addrs);

  680.       /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
  681.          relative ones must be already created according to debug_objfile.  */

  682.       addr_info_make_relative (objfile_addrs, debug_objfile->obfd);

  683.       gdb_assert (debug_objfile->num_sections
  684.                   == gdb_bfd_count_sections (debug_objfile->obfd));
  685.       new_debug_offsets =
  686.         xmalloc (SIZEOF_N_SECTION_OFFSETS (debug_objfile->num_sections));
  687.       make_cleanup (xfree, new_debug_offsets);
  688.       relative_addr_info_to_section_offsets (new_debug_offsets,
  689.                                              debug_objfile->num_sections,
  690.                                              objfile_addrs);

  691.       changed |= objfile_relocate1 (debug_objfile, new_debug_offsets);

  692.       do_cleanups (my_cleanups);
  693.     }

  694.   /* Relocate breakpoints as necessary, after things are relocated.  */
  695.   if (changed)
  696.     breakpoint_re_set ();
  697. }

  698. /* Rebase (add to the offsets) OBJFILE by SLIDE.  SEPARATE_DEBUG_OBJFILE is
  699.    not touched here.
  700.    Return non-zero iff any change happened.  */

  701. static int
  702. objfile_rebase1 (struct objfile *objfile, CORE_ADDR slide)
  703. {
  704.   struct section_offsets *new_offsets =
  705.     ((struct section_offsets *)
  706.      alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
  707.   int i;

  708.   for (i = 0; i < objfile->num_sections; ++i)
  709.     new_offsets->offsets[i] = slide;

  710.   return objfile_relocate1 (objfile, new_offsets);
  711. }

  712. /* Rebase (add to the offsets) OBJFILE by SLIDE.  Process also OBJFILE's
  713.    SEPARATE_DEBUG_OBJFILEs.  */

  714. void
  715. objfile_rebase (struct objfile *objfile, CORE_ADDR slide)
  716. {
  717.   struct objfile *debug_objfile;
  718.   int changed = 0;

  719.   changed |= objfile_rebase1 (objfile, slide);

  720.   for (debug_objfile = objfile->separate_debug_objfile;
  721.        debug_objfile;
  722.        debug_objfile = objfile_separate_debug_iterate (objfile, debug_objfile))
  723.     changed |= objfile_rebase1 (debug_objfile, slide);

  724.   /* Relocate breakpoints as necessary, after things are relocated.  */
  725.   if (changed)
  726.     breakpoint_re_set ();
  727. }

  728. /* Return non-zero if OBJFILE has partial symbols.  */

  729. int
  730. objfile_has_partial_symbols (struct objfile *objfile)
  731. {
  732.   if (!objfile->sf)
  733.     return 0;

  734.   /* If we have not read psymbols, but we have a function capable of reading
  735.      them, then that is an indication that they are in fact available.  Without
  736.      this function the symbols may have been already read in but they also may
  737.      not be present in this objfile.  */
  738.   if ((objfile->flags & OBJF_PSYMTABS_READ) == 0
  739.       && objfile->sf->sym_read_psymbols != NULL)
  740.     return 1;

  741.   return objfile->sf->qf->has_symbols (objfile);
  742. }

  743. /* Return non-zero if OBJFILE has full symbols.  */

  744. int
  745. objfile_has_full_symbols (struct objfile *objfile)
  746. {
  747.   return objfile->compunit_symtabs != NULL;
  748. }

  749. /* Return non-zero if OBJFILE has full or partial symbols, either directly
  750.    or through a separate debug file.  */

  751. int
  752. objfile_has_symbols (struct objfile *objfile)
  753. {
  754.   struct objfile *o;

  755.   for (o = objfile; o; o = objfile_separate_debug_iterate (objfile, o))
  756.     if (objfile_has_partial_symbols (o) || objfile_has_full_symbols (o))
  757.       return 1;
  758.   return 0;
  759. }


  760. /* Many places in gdb want to test just to see if we have any partial
  761.    symbols available.  This function returns zero if none are currently
  762.    available, nonzero otherwise.  */

  763. int
  764. have_partial_symbols (void)
  765. {
  766.   struct objfile *ofp;

  767.   ALL_OBJFILES (ofp)
  768.   {
  769.     if (objfile_has_partial_symbols (ofp))
  770.       return 1;
  771.   }
  772.   return 0;
  773. }

  774. /* Many places in gdb want to test just to see if we have any full
  775.    symbols available.  This function returns zero if none are currently
  776.    available, nonzero otherwise.  */

  777. int
  778. have_full_symbols (void)
  779. {
  780.   struct objfile *ofp;

  781.   ALL_OBJFILES (ofp)
  782.   {
  783.     if (objfile_has_full_symbols (ofp))
  784.       return 1;
  785.   }
  786.   return 0;
  787. }


  788. /* This operations deletes all objfile entries that represent solibs that
  789.    weren't explicitly loaded by the user, via e.g., the add-symbol-file
  790.    command.  */

  791. void
  792. objfile_purge_solibs (void)
  793. {
  794.   struct objfile *objf;
  795.   struct objfile *temp;

  796.   ALL_OBJFILES_SAFE (objf, temp)
  797.   {
  798.     /* We assume that the solib package has been purged already, or will
  799.        be soon.  */

  800.     if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
  801.       free_objfile (objf);
  802.   }
  803. }


  804. /* Many places in gdb want to test just to see if we have any minimal
  805.    symbols available.  This function returns zero if none are currently
  806.    available, nonzero otherwise.  */

  807. int
  808. have_minimal_symbols (void)
  809. {
  810.   struct objfile *ofp;

  811.   ALL_OBJFILES (ofp)
  812.   {
  813.     if (ofp->per_bfd->minimal_symbol_count > 0)
  814.       {
  815.         return 1;
  816.       }
  817.   }
  818.   return 0;
  819. }

  820. /* Qsort comparison function.  */

  821. static int
  822. qsort_cmp (const void *a, const void *b)
  823. {
  824.   const struct obj_section *sect1 = *(const struct obj_section **) a;
  825.   const struct obj_section *sect2 = *(const struct obj_section **) b;
  826.   const CORE_ADDR sect1_addr = obj_section_addr (sect1);
  827.   const CORE_ADDR sect2_addr = obj_section_addr (sect2);

  828.   if (sect1_addr < sect2_addr)
  829.     return -1;
  830.   else if (sect1_addr > sect2_addr)
  831.     return 1;
  832.   else
  833.     {
  834.       /* Sections are at the same address.  This could happen if
  835.          A) we have an objfile and a separate debuginfo.
  836.          B) we are confused, and have added sections without proper relocation,
  837.          or something like that.  */

  838.       const struct objfile *const objfile1 = sect1->objfile;
  839.       const struct objfile *const objfile2 = sect2->objfile;

  840.       if (objfile1->separate_debug_objfile == objfile2
  841.           || objfile2->separate_debug_objfile == objfile1)
  842.         {
  843.           /* Case A.  The ordering doesn't matter: separate debuginfo files
  844.              will be filtered out later.  */

  845.           return 0;
  846.         }

  847.       /* Case B.  Maintain stable sort order, so bugs in GDB are easier to
  848.          triage.  This section could be slow (since we iterate over all
  849.          objfiles in each call to qsort_cmp), but this shouldn't happen
  850.          very often (GDB is already in a confused state; one hopes this
  851.          doesn't happen at all).  If you discover that significant time is
  852.          spent in the loops below, do 'set complaints 100' and examine the
  853.          resulting complaints.  */

  854.       if (objfile1 == objfile2)
  855.         {
  856.           /* Both sections came from the same objfile.  We are really confused.
  857.              Sort on sequence order of sections within the objfile.  */

  858.           const struct obj_section *osect;

  859.           ALL_OBJFILE_OSECTIONS (objfile1, osect)
  860.             if (osect == sect1)
  861.               return -1;
  862.             else if (osect == sect2)
  863.               return 1;

  864.           /* We should have found one of the sections before getting here.  */
  865.           gdb_assert_not_reached ("section not found");
  866.         }
  867.       else
  868.         {
  869.           /* Sort on sequence number of the objfile in the chain.  */

  870.           const struct objfile *objfile;

  871.           ALL_OBJFILES (objfile)
  872.             if (objfile == objfile1)
  873.               return -1;
  874.             else if (objfile == objfile2)
  875.               return 1;

  876.           /* We should have found one of the objfiles before getting here.  */
  877.           gdb_assert_not_reached ("objfile not found");
  878.         }
  879.     }

  880.   /* Unreachable.  */
  881.   gdb_assert_not_reached ("unexpected code path");
  882.   return 0;
  883. }

  884. /* Select "better" obj_section to keep.  We prefer the one that came from
  885.    the real object, rather than the one from separate debuginfo.
  886.    Most of the time the two sections are exactly identical, but with
  887.    prelinking the .rel.dyn section in the real object may have different
  888.    size.  */

  889. static struct obj_section *
  890. preferred_obj_section (struct obj_section *a, struct obj_section *b)
  891. {
  892.   gdb_assert (obj_section_addr (a) == obj_section_addr (b));
  893.   gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
  894.               || (b->objfile->separate_debug_objfile == a->objfile));
  895.   gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
  896.               || (b->objfile->separate_debug_objfile_backlink == a->objfile));

  897.   if (a->objfile->separate_debug_objfile != NULL)
  898.     return a;
  899.   return b;
  900. }

  901. /* Return 1 if SECTION should be inserted into the section map.
  902.    We want to insert only non-overlay and non-TLS section.  */

  903. static int
  904. insert_section_p (const struct bfd *abfd,
  905.                   const struct bfd_section *section)
  906. {
  907.   const bfd_vma lma = bfd_section_lma (abfd, section);

  908.   if (overlay_debugging && lma != 0 && lma != bfd_section_vma (abfd, section)
  909.       && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0)
  910.     /* This is an overlay section.  IN_MEMORY check is needed to avoid
  911.        discarding sections from the "system supplied DSO" (aka vdso)
  912.        on some Linux systems (e.g. Fedora 11).  */
  913.     return 0;
  914.   if ((bfd_get_section_flags (abfd, section) & SEC_THREAD_LOCAL) != 0)
  915.     /* This is a TLS section.  */
  916.     return 0;

  917.   return 1;
  918. }

  919. /* Filter out overlapping sections where one section came from the real
  920.    objfile, and the other from a separate debuginfo file.
  921.    Return the size of table after redundant sections have been eliminated.  */

  922. static int
  923. filter_debuginfo_sections (struct obj_section **map, int map_size)
  924. {
  925.   int i, j;

  926.   for (i = 0, j = 0; i < map_size - 1; i++)
  927.     {
  928.       struct obj_section *const sect1 = map[i];
  929.       struct obj_section *const sect2 = map[i + 1];
  930.       const struct objfile *const objfile1 = sect1->objfile;
  931.       const struct objfile *const objfile2 = sect2->objfile;
  932.       const CORE_ADDR sect1_addr = obj_section_addr (sect1);
  933.       const CORE_ADDR sect2_addr = obj_section_addr (sect2);

  934.       if (sect1_addr == sect2_addr
  935.           && (objfile1->separate_debug_objfile == objfile2
  936.               || objfile2->separate_debug_objfile == objfile1))
  937.         {
  938.           map[j++] = preferred_obj_section (sect1, sect2);
  939.           ++i;
  940.         }
  941.       else
  942.         map[j++] = sect1;
  943.     }

  944.   if (i < map_size)
  945.     {
  946.       gdb_assert (i == map_size - 1);
  947.       map[j++] = map[i];
  948.     }

  949.   /* The map should not have shrunk to less than half the original size.  */
  950.   gdb_assert (map_size / 2 <= j);

  951.   return j;
  952. }

  953. /* Filter out overlapping sections, issuing a warning if any are found.
  954.    Overlapping sections could really be overlay sections which we didn't
  955.    classify as such in insert_section_p, or we could be dealing with a
  956.    corrupt binary.  */

  957. static int
  958. filter_overlapping_sections (struct obj_section **map, int map_size)
  959. {
  960.   int i, j;

  961.   for (i = 0, j = 0; i < map_size - 1; )
  962.     {
  963.       int k;

  964.       map[j++] = map[i];
  965.       for (k = i + 1; k < map_size; k++)
  966.         {
  967.           struct obj_section *const sect1 = map[i];
  968.           struct obj_section *const sect2 = map[k];
  969.           const CORE_ADDR sect1_addr = obj_section_addr (sect1);
  970.           const CORE_ADDR sect2_addr = obj_section_addr (sect2);
  971.           const CORE_ADDR sect1_endaddr = obj_section_endaddr (sect1);

  972.           gdb_assert (sect1_addr <= sect2_addr);

  973.           if (sect1_endaddr <= sect2_addr)
  974.             break;
  975.           else
  976.             {
  977.               /* We have an overlap.  Report it.  */

  978.               struct objfile *const objf1 = sect1->objfile;
  979.               struct objfile *const objf2 = sect2->objfile;

  980.               const struct bfd_section *const bfds1 = sect1->the_bfd_section;
  981.               const struct bfd_section *const bfds2 = sect2->the_bfd_section;

  982.               const CORE_ADDR sect2_endaddr = obj_section_endaddr (sect2);

  983.               struct gdbarch *const gdbarch = get_objfile_arch (objf1);

  984.               complaint (&symfile_complaints,
  985.                          _("unexpected overlap between:\n"
  986.                            " (A) section `%s' from `%s' [%s, %s)\n"
  987.                            " (B) section `%s' from `%s' [%s, %s).\n"
  988.                            "Will ignore section B"),
  989.                          bfd_section_name (abfd1, bfds1), objfile_name (objf1),
  990.                          paddress (gdbarch, sect1_addr),
  991.                          paddress (gdbarch, sect1_endaddr),
  992.                          bfd_section_name (abfd2, bfds2), objfile_name (objf2),
  993.                          paddress (gdbarch, sect2_addr),
  994.                          paddress (gdbarch, sect2_endaddr));
  995.             }
  996.         }
  997.       i = k;
  998.     }

  999.   if (i < map_size)
  1000.     {
  1001.       gdb_assert (i == map_size - 1);
  1002.       map[j++] = map[i];
  1003.     }

  1004.   return j;
  1005. }


  1006. /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
  1007.    TLS, overlay and overlapping sections.  */

  1008. static void
  1009. update_section_map (struct program_space *pspace,
  1010.                     struct obj_section ***pmap, int *pmap_size)
  1011. {
  1012.   struct objfile_pspace_info *pspace_info;
  1013.   int alloc_size, map_size, i;
  1014.   struct obj_section *s, **map;
  1015.   struct objfile *objfile;

  1016.   pspace_info = get_objfile_pspace_data (pspace);
  1017.   gdb_assert (pspace_info->section_map_dirty != 0
  1018.               || pspace_info->new_objfiles_available != 0);

  1019.   map = *pmap;
  1020.   xfree (map);

  1021.   alloc_size = 0;
  1022.   ALL_PSPACE_OBJFILES (pspace, objfile)
  1023.     ALL_OBJFILE_OSECTIONS (objfile, s)
  1024.       if (insert_section_p (objfile->obfd, s->the_bfd_section))
  1025.         alloc_size += 1;

  1026.   /* This happens on detach/attach (e.g. in gdb.base/attach.exp).  */
  1027.   if (alloc_size == 0)
  1028.     {
  1029.       *pmap = NULL;
  1030.       *pmap_size = 0;
  1031.       return;
  1032.     }

  1033.   map = xmalloc (alloc_size * sizeof (*map));

  1034.   i = 0;
  1035.   ALL_PSPACE_OBJFILES (pspace, objfile)
  1036.     ALL_OBJFILE_OSECTIONS (objfile, s)
  1037.       if (insert_section_p (objfile->obfd, s->the_bfd_section))
  1038.         map[i++] = s;

  1039.   qsort (map, alloc_size, sizeof (*map), qsort_cmp);
  1040.   map_size = filter_debuginfo_sections(map, alloc_size);
  1041.   map_size = filter_overlapping_sections(map, map_size);

  1042.   if (map_size < alloc_size)
  1043.     /* Some sections were eliminated.  Trim excess space.  */
  1044.     map = xrealloc (map, map_size * sizeof (*map));
  1045.   else
  1046.     gdb_assert (alloc_size == map_size);

  1047.   *pmap = map;
  1048.   *pmap_size = map_size;
  1049. }

  1050. /* Bsearch comparison function.  */

  1051. static int
  1052. bsearch_cmp (const void *key, const void *elt)
  1053. {
  1054.   const CORE_ADDR pc = *(CORE_ADDR *) key;
  1055.   const struct obj_section *section = *(const struct obj_section **) elt;

  1056.   if (pc < obj_section_addr (section))
  1057.     return -1;
  1058.   if (pc < obj_section_endaddr (section))
  1059.     return 0;
  1060.   return 1;
  1061. }

  1062. /* Returns a section whose range includes PC or NULL if none found.   */

  1063. struct obj_section *
  1064. find_pc_section (CORE_ADDR pc)
  1065. {
  1066.   struct objfile_pspace_info *pspace_info;
  1067.   struct obj_section *s, **sp;

  1068.   /* Check for mapped overlay section first.  */
  1069.   s = find_pc_mapped_section (pc);
  1070.   if (s)
  1071.     return s;

  1072.   pspace_info = get_objfile_pspace_data (current_program_space);
  1073.   if (pspace_info->section_map_dirty
  1074.       || (pspace_info->new_objfiles_available
  1075.           && !pspace_info->inhibit_updates))
  1076.     {
  1077.       update_section_map (current_program_space,
  1078.                           &pspace_info->sections,
  1079.                           &pspace_info->num_sections);

  1080.       /* Don't need updates to section map until objfiles are added,
  1081.          removed or relocated.  */
  1082.       pspace_info->new_objfiles_available = 0;
  1083.       pspace_info->section_map_dirty = 0;
  1084.     }

  1085.   /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
  1086.      bsearch be non-NULL.  */
  1087.   if (pspace_info->sections == NULL)
  1088.     {
  1089.       gdb_assert (pspace_info->num_sections == 0);
  1090.       return NULL;
  1091.     }

  1092.   sp = (struct obj_section **) bsearch (&pc,
  1093.                                         pspace_info->sections,
  1094.                                         pspace_info->num_sections,
  1095.                                         sizeof (*pspace_info->sections),
  1096.                                         bsearch_cmp);
  1097.   if (sp != NULL)
  1098.     return *sp;
  1099.   return NULL;
  1100. }


  1101. /* Return non-zero if PC is in a section called NAME.  */

  1102. int
  1103. pc_in_section (CORE_ADDR pc, char *name)
  1104. {
  1105.   struct obj_section *s;
  1106.   int retval = 0;

  1107.   s = find_pc_section (pc);

  1108.   retval = (s != NULL
  1109.             && s->the_bfd_section->name != NULL
  1110.             && strcmp (s->the_bfd_section->name, name) == 0);
  1111.   return (retval);
  1112. }


  1113. /* Set section_map_dirty so section map will be rebuilt next time it
  1114.    is used.  Called by reread_symbols.  */

  1115. void
  1116. objfiles_changed (void)
  1117. {
  1118.   /* Rebuild section map next time we need it.  */
  1119.   get_objfile_pspace_data (current_program_space)->section_map_dirty = 1;
  1120. }

  1121. /* See comments in objfiles.h.  */

  1122. void
  1123. inhibit_section_map_updates (struct program_space *pspace)
  1124. {
  1125.   get_objfile_pspace_data (pspace)->inhibit_updates = 1;
  1126. }

  1127. /* See comments in objfiles.h.  */

  1128. void
  1129. resume_section_map_updates (struct program_space *pspace)
  1130. {
  1131.   get_objfile_pspace_data (pspace)->inhibit_updates = 0;
  1132. }

  1133. /* See comments in objfiles.h.  */

  1134. void
  1135. resume_section_map_updates_cleanup (void *arg)
  1136. {
  1137.   resume_section_map_updates (arg);
  1138. }

  1139. /* Return 1 if ADDR maps into one of the sections of OBJFILE and 0
  1140.    otherwise.  */

  1141. int
  1142. is_addr_in_objfile (CORE_ADDR addr, const struct objfile *objfile)
  1143. {
  1144.   struct obj_section *osect;

  1145.   if (objfile == NULL)
  1146.     return 0;

  1147.   ALL_OBJFILE_OSECTIONS (objfile, osect)
  1148.     {
  1149.       if (section_is_overlay (osect) && !section_is_mapped (osect))
  1150.         continue;

  1151.       if (obj_section_addr (osect) <= addr
  1152.           && addr < obj_section_endaddr (osect))
  1153.         return 1;
  1154.     }
  1155.   return 0;
  1156. }

  1157. int
  1158. shared_objfile_contains_address_p (struct program_space *pspace,
  1159.                                    CORE_ADDR address)
  1160. {
  1161.   struct objfile *objfile;

  1162.   ALL_PSPACE_OBJFILES (pspace, objfile)
  1163.     {
  1164.       if ((objfile->flags & OBJF_SHARED) != 0
  1165.           && is_addr_in_objfile (address, objfile))
  1166.         return 1;
  1167.     }

  1168.   return 0;
  1169. }

  1170. /* The default implementation for the "iterate_over_objfiles_in_search_order"
  1171.    gdbarch method.  It is equivalent to use the ALL_OBJFILES macro,
  1172.    searching the objfiles in the order they are stored internally,
  1173.    ignoring CURRENT_OBJFILE.

  1174.    On most platorms, it should be close enough to doing the best
  1175.    we can without some knowledge specific to the architecture.  */

  1176. void
  1177. default_iterate_over_objfiles_in_search_order
  1178.   (struct gdbarch *gdbarch,
  1179.    iterate_over_objfiles_in_search_order_cb_ftype *cb,
  1180.    void *cb_data, struct objfile *current_objfile)
  1181. {
  1182.   int stop = 0;
  1183.   struct objfile *objfile;

  1184.   ALL_OBJFILES (objfile)
  1185.     {
  1186.        stop = cb (objfile, cb_data);
  1187.        if (stop)
  1188.          return;
  1189.     }
  1190. }

  1191. /* Return canonical name for OBJFILE.  */

  1192. const char *
  1193. objfile_name (const struct objfile *objfile)
  1194. {
  1195.   if (objfile->obfd != NULL)
  1196.     return bfd_get_filename (objfile->obfd);

  1197.   return objfile->original_name;
  1198. }

  1199. /* See objfiles.h.  */

  1200. const char *
  1201. objfile_debug_name (const struct objfile *objfile)
  1202. {
  1203.   return lbasename (objfile->original_name);
  1204. }

  1205. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1206. extern initialize_file_ftype _initialize_objfiles;

  1207. void
  1208. _initialize_objfiles (void)
  1209. {
  1210.   objfiles_pspace_data
  1211.     = register_program_space_data_with_cleanup (NULL,
  1212.                                                 objfiles_pspace_data_cleanup);

  1213.   objfiles_bfd_data = register_bfd_data_with_cleanup (NULL,
  1214.                                                       objfile_bfd_data_free);
  1215. }