GDB (API)
/home/stan/gdb/src/gdb/objfiles.c
Go to the documentation of this file.
00001 /* GDB routines for manipulating objfiles.
00002 
00003    Copyright (C) 1992-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Cygnus Support, using pieces from other GDB modules.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 /* This file contains support routines for creating, manipulating, and
00023    destroying objfile structures.  */
00024 
00025 #include "defs.h"
00026 #include "bfd.h"                /* Binary File Description */
00027 #include "symtab.h"
00028 #include "symfile.h"
00029 #include "objfiles.h"
00030 #include "gdb-stabs.h"
00031 #include "target.h"
00032 #include "bcache.h"
00033 #include "expression.h"
00034 #include "parser-defs.h"
00035 
00036 #include "gdb_assert.h"
00037 #include <sys/types.h>
00038 #include "gdb_stat.h"
00039 #include <fcntl.h>
00040 #include "gdb_obstack.h"
00041 #include "gdb_string.h"
00042 #include "hashtab.h"
00043 
00044 #include "breakpoint.h"
00045 #include "block.h"
00046 #include "dictionary.h"
00047 #include "source.h"
00048 #include "addrmap.h"
00049 #include "arch-utils.h"
00050 #include "exec.h"
00051 #include "observer.h"
00052 #include "complaints.h"
00053 #include "psymtab.h"
00054 #include "solist.h"
00055 #include "gdb_bfd.h"
00056 #include "btrace.h"
00057 
00058 /* Keep a registry of per-objfile data-pointers required by other GDB
00059    modules.  */
00060 
00061 DEFINE_REGISTRY (objfile, REGISTRY_ACCESS_FIELD)
00062 
00063 /* Externally visible variables that are owned by this module.
00064    See declarations in objfile.h for more info.  */
00065 
00066 struct objfile_pspace_info
00067 {
00068   struct obj_section **sections;
00069   int num_sections;
00070 
00071   /* Nonzero if object files have been added since the section map
00072      was last updated.  */
00073   int new_objfiles_available;
00074 
00075   /* Nonzero if the section map MUST be updated before use.  */
00076   int section_map_dirty;
00077 
00078   /* Nonzero if section map updates should be inhibited if possible.  */
00079   int inhibit_updates;
00080 };
00081 
00082 /* Per-program-space data key.  */
00083 static const struct program_space_data *objfiles_pspace_data;
00084 
00085 static void
00086 objfiles_pspace_data_cleanup (struct program_space *pspace, void *arg)
00087 {
00088   struct objfile_pspace_info *info;
00089 
00090   info = program_space_data (pspace, objfiles_pspace_data);
00091   if (info != NULL)
00092     {
00093       xfree (info->sections);
00094       xfree (info);
00095     }
00096 }
00097 
00098 /* Get the current svr4 data.  If none is found yet, add it now.  This
00099    function always returns a valid object.  */
00100 
00101 static struct objfile_pspace_info *
00102 get_objfile_pspace_data (struct program_space *pspace)
00103 {
00104   struct objfile_pspace_info *info;
00105 
00106   info = program_space_data (pspace, objfiles_pspace_data);
00107   if (info == NULL)
00108     {
00109       info = XZALLOC (struct objfile_pspace_info);
00110       set_program_space_data (pspace, objfiles_pspace_data, info);
00111     }
00112 
00113   return info;
00114 }
00115 
00116 
00117 
00118 /* Per-BFD data key.  */
00119 
00120 static const struct bfd_data *objfiles_bfd_data;
00121 
00122 /* Create the per-BFD storage object for OBJFILE.  If ABFD is not
00123    NULL, and it already has a per-BFD storage object, use that.
00124    Otherwise, allocate a new per-BFD storage object.  If ABFD is not
00125    NULL, the object is allocated on the BFD; otherwise it is allocated
00126    on OBJFILE's obstack.  Note that it is not safe to call this
00127    multiple times for a given OBJFILE -- it can only be called when
00128    allocating or re-initializing OBJFILE.  */
00129 
00130 static struct objfile_per_bfd_storage *
00131 get_objfile_bfd_data (struct objfile *objfile, struct bfd *abfd)
00132 {
00133   struct objfile_per_bfd_storage *storage = NULL;
00134 
00135   if (abfd != NULL)
00136     storage = bfd_data (abfd, objfiles_bfd_data);
00137 
00138   if (storage == NULL)
00139     {
00140       /* If the object requires gdb to do relocations, we simply fall
00141          back to not sharing data across users.  These cases are rare
00142          enough that this seems reasonable.  */
00143       if (abfd != NULL && !gdb_bfd_requires_relocations (abfd))
00144         {
00145           storage = bfd_zalloc (abfd, sizeof (struct objfile_per_bfd_storage));
00146           set_bfd_data (abfd, objfiles_bfd_data, storage);
00147         }
00148       else
00149         storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
00150                                   struct objfile_per_bfd_storage);
00151 
00152       /* Look up the gdbarch associated with the BFD.  */
00153       if (abfd != NULL)
00154         storage->gdbarch = gdbarch_from_bfd (abfd);
00155 
00156       obstack_init (&storage->storage_obstack);
00157       storage->filename_cache = bcache_xmalloc (NULL, NULL);
00158       storage->macro_cache = bcache_xmalloc (NULL, NULL);
00159     }
00160 
00161   return storage;
00162 }
00163 
00164 /* Free STORAGE.  */
00165 
00166 static void
00167 free_objfile_per_bfd_storage (struct objfile_per_bfd_storage *storage)
00168 {
00169   bcache_xfree (storage->filename_cache);
00170   bcache_xfree (storage->macro_cache);
00171   if (storage->demangled_names_hash)
00172     htab_delete (storage->demangled_names_hash);
00173   obstack_free (&storage->storage_obstack, 0);
00174 }
00175 
00176 /* A wrapper for free_objfile_per_bfd_storage that can be passed as a
00177    cleanup function to the BFD registry.  */
00178 
00179 static void
00180 objfile_bfd_data_free (struct bfd *unused, void *d)
00181 {
00182   free_objfile_per_bfd_storage (d);
00183 }
00184 
00185 /* See objfiles.h.  */
00186 
00187 void
00188 set_objfile_per_bfd (struct objfile *objfile)
00189 {
00190   objfile->per_bfd = get_objfile_bfd_data (objfile, objfile->obfd);
00191 }
00192 
00193 
00194 
00195 /* Called via bfd_map_over_sections to build up the section table that
00196    the objfile references.  The objfile contains pointers to the start
00197    of the table (objfile->sections) and to the first location after
00198    the end of the table (objfile->sections_end).  */
00199 
00200 static void
00201 add_to_objfile_sections_full (struct bfd *abfd, struct bfd_section *asect,
00202                               struct objfile *objfile, int force)
00203 {
00204   struct obj_section *section;
00205 
00206   if (!force)
00207     {
00208       flagword aflag;
00209 
00210       aflag = bfd_get_section_flags (abfd, asect);
00211       if (!(aflag & SEC_ALLOC))
00212         return;
00213     }
00214 
00215   section = &objfile->sections[gdb_bfd_section_index (abfd, asect)];
00216   section->objfile = objfile;
00217   section->the_bfd_section = asect;
00218   section->ovly_mapped = 0;
00219 }
00220 
00221 static void
00222 add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
00223                          void *objfilep)
00224 {
00225   add_to_objfile_sections_full (abfd, asect, objfilep, 0);
00226 }
00227 
00228 /* Builds a section table for OBJFILE.
00229 
00230    Note that the OFFSET and OVLY_MAPPED in each table entry are
00231    initialized to zero.  */
00232 
00233 void
00234 build_objfile_section_table (struct objfile *objfile)
00235 {
00236   int count = gdb_bfd_count_sections (objfile->obfd);
00237 
00238   objfile->sections = OBSTACK_CALLOC (&objfile->objfile_obstack,
00239                                       count,
00240                                       struct obj_section);
00241   objfile->sections_end = (objfile->sections + count);
00242   bfd_map_over_sections (objfile->obfd,
00243                          add_to_objfile_sections, (void *) objfile);
00244 
00245   /* See gdb_bfd_section_index.  */
00246   add_to_objfile_sections_full (objfile->obfd, bfd_com_section_ptr, objfile, 1);
00247   add_to_objfile_sections_full (objfile->obfd, bfd_und_section_ptr, objfile, 1);
00248   add_to_objfile_sections_full (objfile->obfd, bfd_abs_section_ptr, objfile, 1);
00249   add_to_objfile_sections_full (objfile->obfd, bfd_ind_section_ptr, objfile, 1);
00250 }
00251 
00252 /* Given a pointer to an initialized bfd (ABFD) and some flag bits
00253    allocate a new objfile struct, fill it in as best we can, link it
00254    into the list of all known objfiles, and return a pointer to the
00255    new objfile struct.
00256 
00257    NAME should contain original non-canonicalized filename or other
00258    identifier as entered by user.  If there is no better source use
00259    bfd_get_filename (ABFD).  NAME may be NULL only if ABFD is NULL.
00260    NAME content is copied into returned objfile.
00261 
00262    The FLAGS word contains various bits (OBJF_*) that can be taken as
00263    requests for specific operations.  Other bits like OBJF_SHARED are
00264    simply copied through to the new objfile flags member.  */
00265 
00266 /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0
00267    by jv-lang.c, to create an artificial objfile used to hold
00268    information about dynamically-loaded Java classes.  Unfortunately,
00269    that branch of this function doesn't get tested very frequently, so
00270    it's prone to breakage.  (E.g. at one time the name was set to NULL
00271    in that situation, which broke a loop over all names in the dynamic
00272    library loader.)  If you change this function, please try to leave
00273    things in a consistent state even if abfd is NULL.  */
00274 
00275 struct objfile *
00276 allocate_objfile (bfd *abfd, const char *name, int flags)
00277 {
00278   struct objfile *objfile;
00279 
00280   objfile = (struct objfile *) xzalloc (sizeof (struct objfile));
00281   objfile->psymbol_cache = psymbol_bcache_init ();
00282   /* We could use obstack_specify_allocation here instead, but
00283      gdb_obstack.h specifies the alloc/dealloc functions.  */
00284   obstack_init (&objfile->objfile_obstack);
00285   terminate_minimal_symbol_table (objfile);
00286 
00287   objfile_alloc_data (objfile);
00288 
00289   if (name == NULL)
00290     {
00291       gdb_assert (abfd == NULL);
00292       gdb_assert ((flags & OBJF_NOT_FILENAME) != 0);
00293       name = "<<anonymous objfile>>";
00294     }
00295   objfile->original_name = obstack_copy0 (&objfile->objfile_obstack, name,
00296                                           strlen (name));
00297 
00298   /* Update the per-objfile information that comes from the bfd, ensuring
00299      that any data that is reference is saved in the per-objfile data
00300      region.  */
00301 
00302   objfile->obfd = abfd;
00303   gdb_bfd_ref (abfd);
00304   if (abfd != NULL)
00305     {
00306       objfile->mtime = bfd_get_mtime (abfd);
00307 
00308       /* Build section table.  */
00309       build_objfile_section_table (objfile);
00310     }
00311 
00312   objfile->per_bfd = get_objfile_bfd_data (objfile, abfd);
00313   objfile->pspace = current_program_space;
00314 
00315   /* Initialize the section indexes for this objfile, so that we can
00316      later detect if they are used w/o being properly assigned to.  */
00317 
00318   objfile->sect_index_text = -1;
00319   objfile->sect_index_data = -1;
00320   objfile->sect_index_bss = -1;
00321   objfile->sect_index_rodata = -1;
00322 
00323   /* Add this file onto the tail of the linked list of other such files.  */
00324 
00325   objfile->next = NULL;
00326   if (object_files == NULL)
00327     object_files = objfile;
00328   else
00329     {
00330       struct objfile *last_one;
00331 
00332       for (last_one = object_files;
00333            last_one->next;
00334            last_one = last_one->next);
00335       last_one->next = objfile;
00336     }
00337 
00338   /* Save passed in flag bits.  */
00339   objfile->flags |= flags;
00340 
00341   /* Rebuild section map next time we need it.  */
00342   get_objfile_pspace_data (objfile->pspace)->new_objfiles_available = 1;
00343 
00344   return objfile;
00345 }
00346 
00347 /* Retrieve the gdbarch associated with OBJFILE.  */
00348 struct gdbarch *
00349 get_objfile_arch (struct objfile *objfile)
00350 {
00351   return objfile->per_bfd->gdbarch;
00352 }
00353 
00354 /* If there is a valid and known entry point, function fills *ENTRY_P with it
00355    and returns non-zero; otherwise it returns zero.  */
00356 
00357 int
00358 entry_point_address_query (CORE_ADDR *entry_p)
00359 {
00360   if (symfile_objfile == NULL || !symfile_objfile->ei.entry_point_p)
00361     return 0;
00362 
00363   *entry_p = symfile_objfile->ei.entry_point;
00364 
00365   return 1;
00366 }
00367 
00368 /* Get current entry point address.  Call error if it is not known.  */
00369 
00370 CORE_ADDR
00371 entry_point_address (void)
00372 {
00373   CORE_ADDR retval;
00374 
00375   if (!entry_point_address_query (&retval))
00376     error (_("Entry point address is not known."));
00377 
00378   return retval;
00379 }
00380 
00381 /* Iterator on PARENT and every separate debug objfile of PARENT.
00382    The usage pattern is:
00383      for (objfile = parent;
00384           objfile;
00385           objfile = objfile_separate_debug_iterate (parent, objfile))
00386        ...
00387 */
00388 
00389 struct objfile *
00390 objfile_separate_debug_iterate (const struct objfile *parent,
00391                                 const struct objfile *objfile)
00392 {
00393   struct objfile *res;
00394 
00395   /* If any, return the first child.  */
00396   res = objfile->separate_debug_objfile;
00397   if (res)
00398     return res;
00399 
00400   /* Common case where there is no separate debug objfile.  */
00401   if (objfile == parent)
00402     return NULL;
00403 
00404   /* Return the brother if any.  Note that we don't iterate on brothers of
00405      the parents.  */
00406   res = objfile->separate_debug_objfile_link;
00407   if (res)
00408     return res;
00409 
00410   for (res = objfile->separate_debug_objfile_backlink;
00411        res != parent;
00412        res = res->separate_debug_objfile_backlink)
00413     {
00414       gdb_assert (res != NULL);
00415       if (res->separate_debug_objfile_link)
00416         return res->separate_debug_objfile_link;
00417     }
00418   return NULL;
00419 }
00420 
00421 /* Put one object file before a specified on in the global list.
00422    This can be used to make sure an object file is destroyed before
00423    another when using ALL_OBJFILES_SAFE to free all objfiles.  */
00424 void
00425 put_objfile_before (struct objfile *objfile, struct objfile *before_this)
00426 {
00427   struct objfile **objp;
00428 
00429   unlink_objfile (objfile);
00430   
00431   for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
00432     {
00433       if (*objp == before_this)
00434         {
00435           objfile->next = *objp;
00436           *objp = objfile;
00437           return;
00438         }
00439     }
00440   
00441   internal_error (__FILE__, __LINE__,
00442                   _("put_objfile_before: before objfile not in list"));
00443 }
00444 
00445 /* Put OBJFILE at the front of the list.  */
00446 
00447 void
00448 objfile_to_front (struct objfile *objfile)
00449 {
00450   struct objfile **objp;
00451   for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
00452     {
00453       if (*objp == objfile)
00454         {
00455           /* Unhook it from where it is.  */
00456           *objp = objfile->next;
00457           /* Put it in the front.  */
00458           objfile->next = object_files;
00459           object_files = objfile;
00460           break;
00461         }
00462     }
00463 }
00464 
00465 /* Unlink OBJFILE from the list of known objfiles, if it is found in the
00466    list.
00467 
00468    It is not a bug, or error, to call this function if OBJFILE is not known
00469    to be in the current list.  This is done in the case of mapped objfiles,
00470    for example, just to ensure that the mapped objfile doesn't appear twice
00471    in the list.  Since the list is threaded, linking in a mapped objfile
00472    twice would create a circular list.
00473 
00474    If OBJFILE turns out to be in the list, we zap it's NEXT pointer after
00475    unlinking it, just to ensure that we have completely severed any linkages
00476    between the OBJFILE and the list.  */
00477 
00478 void
00479 unlink_objfile (struct objfile *objfile)
00480 {
00481   struct objfile **objpp;
00482 
00483   for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next))
00484     {
00485       if (*objpp == objfile)
00486         {
00487           *objpp = (*objpp)->next;
00488           objfile->next = NULL;
00489           return;
00490         }
00491     }
00492 
00493   internal_error (__FILE__, __LINE__,
00494                   _("unlink_objfile: objfile already unlinked"));
00495 }
00496 
00497 /* Add OBJFILE as a separate debug objfile of PARENT.  */
00498 
00499 void
00500 add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
00501 {
00502   gdb_assert (objfile && parent);
00503 
00504   /* Must not be already in a list.  */
00505   gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
00506   gdb_assert (objfile->separate_debug_objfile_link == NULL);
00507   gdb_assert (objfile->separate_debug_objfile == NULL);
00508   gdb_assert (parent->separate_debug_objfile_backlink == NULL);
00509   gdb_assert (parent->separate_debug_objfile_link == NULL);
00510 
00511   objfile->separate_debug_objfile_backlink = parent;
00512   objfile->separate_debug_objfile_link = parent->separate_debug_objfile;
00513   parent->separate_debug_objfile = objfile;
00514 
00515   /* Put the separate debug object before the normal one, this is so that
00516      usage of the ALL_OBJFILES_SAFE macro will stay safe.  */
00517   put_objfile_before (objfile, parent);
00518 }
00519 
00520 /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
00521    itself.  */
00522 
00523 void
00524 free_objfile_separate_debug (struct objfile *objfile)
00525 {
00526   struct objfile *child;
00527 
00528   for (child = objfile->separate_debug_objfile; child;)
00529     {
00530       struct objfile *next_child = child->separate_debug_objfile_link;
00531       free_objfile (child);
00532       child = next_child;
00533     }
00534 }
00535 
00536 /* Destroy an objfile and all the symtabs and psymtabs under it.  Note
00537    that as much as possible is allocated on the objfile_obstack 
00538    so that the memory can be efficiently freed.
00539 
00540    Things which we do NOT free because they are not in malloc'd memory
00541    or not in memory specific to the objfile include:
00542 
00543    objfile -> sf
00544 
00545    FIXME:  If the objfile is using reusable symbol information (via mmalloc),
00546    then we need to take into account the fact that more than one process
00547    may be using the symbol information at the same time (when mmalloc is
00548    extended to support cooperative locking).  When more than one process
00549    is using the mapped symbol info, we need to be more careful about when
00550    we free objects in the reusable area.  */
00551 
00552 void
00553 free_objfile (struct objfile *objfile)
00554 {
00555   /* Free all separate debug objfiles.  */
00556   free_objfile_separate_debug (objfile);
00557 
00558   if (objfile->separate_debug_objfile_backlink)
00559     {
00560       /* We freed the separate debug file, make sure the base objfile
00561          doesn't reference it.  */
00562       struct objfile *child;
00563 
00564       child = objfile->separate_debug_objfile_backlink->separate_debug_objfile;
00565 
00566       if (child == objfile)
00567         {
00568           /* OBJFILE is the first child.  */
00569           objfile->separate_debug_objfile_backlink->separate_debug_objfile =
00570             objfile->separate_debug_objfile_link;
00571         }
00572       else
00573         {
00574           /* Find OBJFILE in the list.  */
00575           while (1)
00576             {
00577               if (child->separate_debug_objfile_link == objfile)
00578                 {
00579                   child->separate_debug_objfile_link =
00580                     objfile->separate_debug_objfile_link;
00581                   break;
00582                 }
00583               child = child->separate_debug_objfile_link;
00584               gdb_assert (child);
00585             }
00586         }
00587     }
00588   
00589   /* Remove any references to this objfile in the global value
00590      lists.  */
00591   preserve_values (objfile);
00592 
00593   /* It still may reference data modules have associated with the objfile and
00594      the symbol file data.  */
00595   forget_cached_source_info_for_objfile (objfile);
00596 
00597   breakpoint_free_objfile (objfile);
00598   btrace_free_objfile (objfile);
00599 
00600   /* First do any symbol file specific actions required when we are
00601      finished with a particular symbol file.  Note that if the objfile
00602      is using reusable symbol information (via mmalloc) then each of
00603      these routines is responsible for doing the correct thing, either
00604      freeing things which are valid only during this particular gdb
00605      execution, or leaving them to be reused during the next one.  */
00606 
00607   if (objfile->sf != NULL)
00608     {
00609       (*objfile->sf->sym_finish) (objfile);
00610     }
00611 
00612   /* Discard any data modules have associated with the objfile.  The function
00613      still may reference objfile->obfd.  */
00614   objfile_free_data (objfile);
00615 
00616   if (objfile->obfd)
00617     gdb_bfd_unref (objfile->obfd);
00618   else
00619     free_objfile_per_bfd_storage (objfile->per_bfd);
00620 
00621   /* Remove it from the chain of all objfiles.  */
00622 
00623   unlink_objfile (objfile);
00624 
00625   if (objfile == symfile_objfile)
00626     symfile_objfile = NULL;
00627 
00628   /* Before the symbol table code was redone to make it easier to
00629      selectively load and remove information particular to a specific
00630      linkage unit, gdb used to do these things whenever the monolithic
00631      symbol table was blown away.  How much still needs to be done
00632      is unknown, but we play it safe for now and keep each action until
00633      it is shown to be no longer needed.  */
00634 
00635   /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
00636      for example), so we need to call this here.  */
00637   clear_pc_function_cache ();
00638 
00639   /* Clear globals which might have pointed into a removed objfile.
00640      FIXME: It's not clear which of these are supposed to persist
00641      between expressions and which ought to be reset each time.  */
00642   expression_context_block = NULL;
00643   innermost_block = NULL;
00644 
00645   /* Check to see if the current_source_symtab belongs to this objfile,
00646      and if so, call clear_current_source_symtab_and_line.  */
00647 
00648   {
00649     struct symtab_and_line cursal = get_current_source_symtab_and_line ();
00650 
00651     if (cursal.symtab && cursal.symtab->objfile == objfile)
00652       clear_current_source_symtab_and_line ();
00653   }
00654 
00655   if (objfile->global_psymbols.list)
00656     xfree (objfile->global_psymbols.list);
00657   if (objfile->static_psymbols.list)
00658     xfree (objfile->static_psymbols.list);
00659   /* Free the obstacks for non-reusable objfiles.  */
00660   psymbol_bcache_free (objfile->psymbol_cache);
00661   obstack_free (&objfile->objfile_obstack, 0);
00662 
00663   /* Rebuild section map next time we need it.  */
00664   get_objfile_pspace_data (objfile->pspace)->section_map_dirty = 1;
00665 
00666   /* The last thing we do is free the objfile struct itself.  */
00667   xfree (objfile);
00668 }
00669 
00670 static void
00671 do_free_objfile_cleanup (void *obj)
00672 {
00673   free_objfile (obj);
00674 }
00675 
00676 struct cleanup *
00677 make_cleanup_free_objfile (struct objfile *obj)
00678 {
00679   return make_cleanup (do_free_objfile_cleanup, obj);
00680 }
00681 
00682 /* Free all the object files at once and clean up their users.  */
00683 
00684 void
00685 free_all_objfiles (void)
00686 {
00687   struct objfile *objfile, *temp;
00688   struct so_list *so;
00689 
00690   /* Any objfile referencewould become stale.  */
00691   for (so = master_so_list (); so; so = so->next)
00692     gdb_assert (so->objfile == NULL);
00693 
00694   ALL_OBJFILES_SAFE (objfile, temp)
00695   {
00696     free_objfile (objfile);
00697   }
00698   clear_symtab_users (0);
00699 }
00700 
00701 /* A helper function for objfile_relocate1 that relocates a single
00702    symbol.  */
00703 
00704 static void
00705 relocate_one_symbol (struct symbol *sym, struct objfile *objfile,
00706                      struct section_offsets *delta)
00707 {
00708   fixup_symbol_section (sym, objfile);
00709 
00710   /* The RS6000 code from which this was taken skipped
00711      any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
00712      But I'm leaving out that test, on the theory that
00713      they can't possibly pass the tests below.  */
00714   if ((SYMBOL_CLASS (sym) == LOC_LABEL
00715        || SYMBOL_CLASS (sym) == LOC_STATIC)
00716       && SYMBOL_SECTION (sym) >= 0)
00717     {
00718       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (delta, SYMBOL_SECTION (sym));
00719     }
00720 }
00721 
00722 /* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
00723    entries in new_offsets.  SEPARATE_DEBUG_OBJFILE is not touched here.
00724    Return non-zero iff any change happened.  */
00725 
00726 static int
00727 objfile_relocate1 (struct objfile *objfile, 
00728                    const struct section_offsets *new_offsets)
00729 {
00730   struct obj_section *s;
00731   struct section_offsets *delta =
00732     ((struct section_offsets *) 
00733      alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
00734 
00735   int i;
00736   int something_changed = 0;
00737 
00738   for (i = 0; i < objfile->num_sections; ++i)
00739     {
00740       delta->offsets[i] =
00741         ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i);
00742       if (ANOFFSET (delta, i) != 0)
00743         something_changed = 1;
00744     }
00745   if (!something_changed)
00746     return 0;
00747 
00748   /* OK, get all the symtabs.  */
00749   {
00750     struct symtab *s;
00751 
00752     ALL_OBJFILE_SYMTABS (objfile, s)
00753     {
00754       struct linetable *l;
00755       struct blockvector *bv;
00756       int i;
00757 
00758       /* First the line table.  */
00759       l = LINETABLE (s);
00760       if (l)
00761         {
00762           for (i = 0; i < l->nitems; ++i)
00763             l->item[i].pc += ANOFFSET (delta, s->block_line_section);
00764         }
00765 
00766       /* Don't relocate a shared blockvector more than once.  */
00767       if (!s->primary)
00768         continue;
00769 
00770       bv = BLOCKVECTOR (s);
00771       if (BLOCKVECTOR_MAP (bv))
00772         addrmap_relocate (BLOCKVECTOR_MAP (bv),
00773                           ANOFFSET (delta, s->block_line_section));
00774 
00775       for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
00776         {
00777           struct block *b;
00778           struct symbol *sym;
00779           struct dict_iterator iter;
00780 
00781           b = BLOCKVECTOR_BLOCK (bv, i);
00782           BLOCK_START (b) += ANOFFSET (delta, s->block_line_section);
00783           BLOCK_END (b) += ANOFFSET (delta, s->block_line_section);
00784 
00785           /* We only want to iterate over the local symbols, not any
00786              symbols in included symtabs.  */
00787           ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
00788             {
00789               relocate_one_symbol (sym, objfile, delta);
00790             }
00791         }
00792     }
00793   }
00794 
00795   /* Relocate isolated symbols.  */
00796   {
00797     struct symbol *iter;
00798 
00799     for (iter = objfile->template_symbols; iter; iter = iter->hash_next)
00800       relocate_one_symbol (iter, objfile, delta);
00801   }
00802 
00803   if (objfile->psymtabs_addrmap)
00804     addrmap_relocate (objfile->psymtabs_addrmap,
00805                       ANOFFSET (delta, SECT_OFF_TEXT (objfile)));
00806 
00807   if (objfile->sf)
00808     objfile->sf->qf->relocate (objfile, new_offsets, delta);
00809 
00810   {
00811     struct minimal_symbol *msym;
00812 
00813     ALL_OBJFILE_MSYMBOLS (objfile, msym)
00814       if (SYMBOL_SECTION (msym) >= 0)
00815       SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym));
00816   }
00817   /* Relocating different sections by different amounts may cause the symbols
00818      to be out of order.  */
00819   msymbols_sort (objfile);
00820 
00821   if (objfile->ei.entry_point_p)
00822     {
00823       /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT
00824          only as a fallback.  */
00825       struct obj_section *s;
00826       s = find_pc_section (objfile->ei.entry_point);
00827       if (s)
00828         {
00829           int idx = gdb_bfd_section_index (objfile->obfd, s->the_bfd_section);
00830 
00831           objfile->ei.entry_point += ANOFFSET (delta, idx);
00832         }
00833       else
00834         objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
00835     }
00836 
00837   {
00838     int i;
00839 
00840     for (i = 0; i < objfile->num_sections; ++i)
00841       (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i);
00842   }
00843 
00844   /* Rebuild section map next time we need it.  */
00845   get_objfile_pspace_data (objfile->pspace)->section_map_dirty = 1;
00846 
00847   /* Update the table in exec_ops, used to read memory.  */
00848   ALL_OBJFILE_OSECTIONS (objfile, s)
00849     {
00850       int idx = s - objfile->sections;
00851 
00852       exec_set_section_address (bfd_get_filename (objfile->obfd), idx,
00853                                 obj_section_addr (s));
00854     }
00855 
00856   /* Relocating probes.  */
00857   if (objfile->sf && objfile->sf->sym_probe_fns)
00858     objfile->sf->sym_probe_fns->sym_relocate_probe (objfile,
00859                                                     new_offsets, delta);
00860 
00861   /* Data changed.  */
00862   return 1;
00863 }
00864 
00865 /* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
00866    entries in new_offsets.  Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
00867 
00868    The number and ordering of sections does differ between the two objfiles.
00869    Only their names match.  Also the file offsets will differ (objfile being
00870    possibly prelinked but separate_debug_objfile is probably not prelinked) but
00871    the in-memory absolute address as specified by NEW_OFFSETS must match both
00872    files.  */
00873 
00874 void
00875 objfile_relocate (struct objfile *objfile,
00876                   const struct section_offsets *new_offsets)
00877 {
00878   struct objfile *debug_objfile;
00879   int changed = 0;
00880 
00881   changed |= objfile_relocate1 (objfile, new_offsets);
00882 
00883   for (debug_objfile = objfile->separate_debug_objfile;
00884        debug_objfile;
00885        debug_objfile = objfile_separate_debug_iterate (objfile, debug_objfile))
00886     {
00887       struct section_addr_info *objfile_addrs;
00888       struct section_offsets *new_debug_offsets;
00889       struct cleanup *my_cleanups;
00890 
00891       objfile_addrs = build_section_addr_info_from_objfile (objfile);
00892       my_cleanups = make_cleanup (xfree, objfile_addrs);
00893 
00894       /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
00895          relative ones must be already created according to debug_objfile.  */
00896 
00897       addr_info_make_relative (objfile_addrs, debug_objfile->obfd);
00898 
00899       gdb_assert (debug_objfile->num_sections
00900                   == gdb_bfd_count_sections (debug_objfile->obfd));
00901       new_debug_offsets = 
00902         xmalloc (SIZEOF_N_SECTION_OFFSETS (debug_objfile->num_sections));
00903       make_cleanup (xfree, new_debug_offsets);
00904       relative_addr_info_to_section_offsets (new_debug_offsets,
00905                                              debug_objfile->num_sections,
00906                                              objfile_addrs);
00907 
00908       changed |= objfile_relocate1 (debug_objfile, new_debug_offsets);
00909 
00910       do_cleanups (my_cleanups);
00911     }
00912 
00913   /* Relocate breakpoints as necessary, after things are relocated.  */
00914   if (changed)
00915     breakpoint_re_set ();
00916 }
00917 
00918 /* Rebase (add to the offsets) OBJFILE by SLIDE.  SEPARATE_DEBUG_OBJFILE is
00919    not touched here.
00920    Return non-zero iff any change happened.  */
00921 
00922 static int
00923 objfile_rebase1 (struct objfile *objfile, CORE_ADDR slide)
00924 {
00925   struct section_offsets *new_offsets =
00926     ((struct section_offsets *)
00927      alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
00928   int i;
00929 
00930   for (i = 0; i < objfile->num_sections; ++i)
00931     new_offsets->offsets[i] = slide;
00932 
00933   return objfile_relocate1 (objfile, new_offsets);
00934 }
00935 
00936 /* Rebase (add to the offsets) OBJFILE by SLIDE.  Process also OBJFILE's
00937    SEPARATE_DEBUG_OBJFILEs.  */
00938 
00939 void
00940 objfile_rebase (struct objfile *objfile, CORE_ADDR slide)
00941 {
00942   struct objfile *debug_objfile;
00943   int changed = 0;
00944 
00945   changed |= objfile_rebase1 (objfile, slide);
00946 
00947   for (debug_objfile = objfile->separate_debug_objfile;
00948        debug_objfile;
00949        debug_objfile = objfile_separate_debug_iterate (objfile, debug_objfile))
00950     changed |= objfile_rebase1 (debug_objfile, slide);
00951 
00952   /* Relocate breakpoints as necessary, after things are relocated.  */
00953   if (changed)
00954     breakpoint_re_set ();
00955 }
00956 
00957 /* Return non-zero if OBJFILE has partial symbols.  */
00958 
00959 int
00960 objfile_has_partial_symbols (struct objfile *objfile)
00961 {
00962   if (!objfile->sf)
00963     return 0;
00964 
00965   /* If we have not read psymbols, but we have a function capable of reading
00966      them, then that is an indication that they are in fact available.  Without
00967      this function the symbols may have been already read in but they also may
00968      not be present in this objfile.  */
00969   if ((objfile->flags & OBJF_PSYMTABS_READ) == 0
00970       && objfile->sf->sym_read_psymbols != NULL)
00971     return 1;
00972 
00973   return objfile->sf->qf->has_symbols (objfile);
00974 }
00975 
00976 /* Return non-zero if OBJFILE has full symbols.  */
00977 
00978 int
00979 objfile_has_full_symbols (struct objfile *objfile)
00980 {
00981   return objfile->symtabs != NULL;
00982 }
00983 
00984 /* Return non-zero if OBJFILE has full or partial symbols, either directly
00985    or through a separate debug file.  */
00986 
00987 int
00988 objfile_has_symbols (struct objfile *objfile)
00989 {
00990   struct objfile *o;
00991 
00992   for (o = objfile; o; o = objfile_separate_debug_iterate (objfile, o))
00993     if (objfile_has_partial_symbols (o) || objfile_has_full_symbols (o))
00994       return 1;
00995   return 0;
00996 }
00997 
00998 
00999 /* Many places in gdb want to test just to see if we have any partial
01000    symbols available.  This function returns zero if none are currently
01001    available, nonzero otherwise.  */
01002 
01003 int
01004 have_partial_symbols (void)
01005 {
01006   struct objfile *ofp;
01007 
01008   ALL_OBJFILES (ofp)
01009   {
01010     if (objfile_has_partial_symbols (ofp))
01011       return 1;
01012   }
01013   return 0;
01014 }
01015 
01016 /* Many places in gdb want to test just to see if we have any full
01017    symbols available.  This function returns zero if none are currently
01018    available, nonzero otherwise.  */
01019 
01020 int
01021 have_full_symbols (void)
01022 {
01023   struct objfile *ofp;
01024 
01025   ALL_OBJFILES (ofp)
01026   {
01027     if (objfile_has_full_symbols (ofp))
01028       return 1;
01029   }
01030   return 0;
01031 }
01032 
01033 
01034 /* This operations deletes all objfile entries that represent solibs that
01035    weren't explicitly loaded by the user, via e.g., the add-symbol-file
01036    command.  */
01037 
01038 void
01039 objfile_purge_solibs (void)
01040 {
01041   struct objfile *objf;
01042   struct objfile *temp;
01043 
01044   ALL_OBJFILES_SAFE (objf, temp)
01045   {
01046     /* We assume that the solib package has been purged already, or will
01047        be soon.  */
01048 
01049     if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
01050       free_objfile (objf);
01051   }
01052 }
01053 
01054 
01055 /* Many places in gdb want to test just to see if we have any minimal
01056    symbols available.  This function returns zero if none are currently
01057    available, nonzero otherwise.  */
01058 
01059 int
01060 have_minimal_symbols (void)
01061 {
01062   struct objfile *ofp;
01063 
01064   ALL_OBJFILES (ofp)
01065   {
01066     if (ofp->minimal_symbol_count > 0)
01067       {
01068         return 1;
01069       }
01070   }
01071   return 0;
01072 }
01073 
01074 /* Qsort comparison function.  */
01075 
01076 static int
01077 qsort_cmp (const void *a, const void *b)
01078 {
01079   const struct obj_section *sect1 = *(const struct obj_section **) a;
01080   const struct obj_section *sect2 = *(const struct obj_section **) b;
01081   const CORE_ADDR sect1_addr = obj_section_addr (sect1);
01082   const CORE_ADDR sect2_addr = obj_section_addr (sect2);
01083 
01084   if (sect1_addr < sect2_addr)
01085     return -1;
01086   else if (sect1_addr > sect2_addr)
01087     return 1;
01088   else
01089     {
01090       /* Sections are at the same address.  This could happen if
01091          A) we have an objfile and a separate debuginfo.
01092          B) we are confused, and have added sections without proper relocation,
01093          or something like that.  */
01094 
01095       const struct objfile *const objfile1 = sect1->objfile;
01096       const struct objfile *const objfile2 = sect2->objfile;
01097 
01098       if (objfile1->separate_debug_objfile == objfile2
01099           || objfile2->separate_debug_objfile == objfile1)
01100         {
01101           /* Case A.  The ordering doesn't matter: separate debuginfo files
01102              will be filtered out later.  */
01103 
01104           return 0;
01105         }
01106 
01107       /* Case B.  Maintain stable sort order, so bugs in GDB are easier to
01108          triage.  This section could be slow (since we iterate over all
01109          objfiles in each call to qsort_cmp), but this shouldn't happen
01110          very often (GDB is already in a confused state; one hopes this
01111          doesn't happen at all).  If you discover that significant time is
01112          spent in the loops below, do 'set complaints 100' and examine the
01113          resulting complaints.  */
01114 
01115       if (objfile1 == objfile2)
01116         {
01117           /* Both sections came from the same objfile.  We are really confused.
01118              Sort on sequence order of sections within the objfile.  */
01119 
01120           const struct obj_section *osect;
01121 
01122           ALL_OBJFILE_OSECTIONS (objfile1, osect)
01123             if (osect == sect1)
01124               return -1;
01125             else if (osect == sect2)
01126               return 1;
01127 
01128           /* We should have found one of the sections before getting here.  */
01129           gdb_assert_not_reached ("section not found");
01130         }
01131       else
01132         {
01133           /* Sort on sequence number of the objfile in the chain.  */
01134 
01135           const struct objfile *objfile;
01136 
01137           ALL_OBJFILES (objfile)
01138             if (objfile == objfile1)
01139               return -1;
01140             else if (objfile == objfile2)
01141               return 1;
01142 
01143           /* We should have found one of the objfiles before getting here.  */
01144           gdb_assert_not_reached ("objfile not found");
01145         }
01146     }
01147 
01148   /* Unreachable.  */
01149   gdb_assert_not_reached ("unexpected code path");
01150   return 0;
01151 }
01152 
01153 /* Select "better" obj_section to keep.  We prefer the one that came from
01154    the real object, rather than the one from separate debuginfo.
01155    Most of the time the two sections are exactly identical, but with
01156    prelinking the .rel.dyn section in the real object may have different
01157    size.  */
01158 
01159 static struct obj_section *
01160 preferred_obj_section (struct obj_section *a, struct obj_section *b)
01161 {
01162   gdb_assert (obj_section_addr (a) == obj_section_addr (b));
01163   gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
01164               || (b->objfile->separate_debug_objfile == a->objfile));
01165   gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
01166               || (b->objfile->separate_debug_objfile_backlink == a->objfile));
01167 
01168   if (a->objfile->separate_debug_objfile != NULL)
01169     return a;
01170   return b;
01171 }
01172 
01173 /* Return 1 if SECTION should be inserted into the section map.
01174    We want to insert only non-overlay and non-TLS section.  */
01175 
01176 static int
01177 insert_section_p (const struct bfd *abfd,
01178                   const struct bfd_section *section)
01179 {
01180   const bfd_vma lma = bfd_section_lma (abfd, section);
01181 
01182   if (overlay_debugging && lma != 0 && lma != bfd_section_vma (abfd, section)
01183       && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0)
01184     /* This is an overlay section.  IN_MEMORY check is needed to avoid
01185        discarding sections from the "system supplied DSO" (aka vdso)
01186        on some Linux systems (e.g. Fedora 11).  */
01187     return 0;
01188   if ((bfd_get_section_flags (abfd, section) & SEC_THREAD_LOCAL) != 0)
01189     /* This is a TLS section.  */
01190     return 0;
01191 
01192   return 1;
01193 }
01194 
01195 /* Filter out overlapping sections where one section came from the real
01196    objfile, and the other from a separate debuginfo file.
01197    Return the size of table after redundant sections have been eliminated.  */
01198 
01199 static int
01200 filter_debuginfo_sections (struct obj_section **map, int map_size)
01201 {
01202   int i, j;
01203 
01204   for (i = 0, j = 0; i < map_size - 1; i++)
01205     {
01206       struct obj_section *const sect1 = map[i];
01207       struct obj_section *const sect2 = map[i + 1];
01208       const struct objfile *const objfile1 = sect1->objfile;
01209       const struct objfile *const objfile2 = sect2->objfile;
01210       const CORE_ADDR sect1_addr = obj_section_addr (sect1);
01211       const CORE_ADDR sect2_addr = obj_section_addr (sect2);
01212 
01213       if (sect1_addr == sect2_addr
01214           && (objfile1->separate_debug_objfile == objfile2
01215               || objfile2->separate_debug_objfile == objfile1))
01216         {
01217           map[j++] = preferred_obj_section (sect1, sect2);
01218           ++i;
01219         }
01220       else
01221         map[j++] = sect1;
01222     }
01223 
01224   if (i < map_size)
01225     {
01226       gdb_assert (i == map_size - 1);
01227       map[j++] = map[i];
01228     }
01229 
01230   /* The map should not have shrunk to less than half the original size.  */
01231   gdb_assert (map_size / 2 <= j);
01232 
01233   return j;
01234 }
01235 
01236 /* Filter out overlapping sections, issuing a warning if any are found.
01237    Overlapping sections could really be overlay sections which we didn't
01238    classify as such in insert_section_p, or we could be dealing with a
01239    corrupt binary.  */
01240 
01241 static int
01242 filter_overlapping_sections (struct obj_section **map, int map_size)
01243 {
01244   int i, j;
01245 
01246   for (i = 0, j = 0; i < map_size - 1; )
01247     {
01248       int k;
01249 
01250       map[j++] = map[i];
01251       for (k = i + 1; k < map_size; k++)
01252         {
01253           struct obj_section *const sect1 = map[i];
01254           struct obj_section *const sect2 = map[k];
01255           const CORE_ADDR sect1_addr = obj_section_addr (sect1);
01256           const CORE_ADDR sect2_addr = obj_section_addr (sect2);
01257           const CORE_ADDR sect1_endaddr = obj_section_endaddr (sect1);
01258 
01259           gdb_assert (sect1_addr <= sect2_addr);
01260 
01261           if (sect1_endaddr <= sect2_addr)
01262             break;
01263           else
01264             {
01265               /* We have an overlap.  Report it.  */
01266 
01267               struct objfile *const objf1 = sect1->objfile;
01268               struct objfile *const objf2 = sect2->objfile;
01269 
01270               const struct bfd_section *const bfds1 = sect1->the_bfd_section;
01271               const struct bfd_section *const bfds2 = sect2->the_bfd_section;
01272 
01273               const CORE_ADDR sect2_endaddr = obj_section_endaddr (sect2);
01274 
01275               struct gdbarch *const gdbarch = get_objfile_arch (objf1);
01276 
01277               complaint (&symfile_complaints,
01278                          _("unexpected overlap between:\n"
01279                            " (A) section `%s' from `%s' [%s, %s)\n"
01280                            " (B) section `%s' from `%s' [%s, %s).\n"
01281                            "Will ignore section B"),
01282                          bfd_section_name (abfd1, bfds1), objfile_name (objf1),
01283                          paddress (gdbarch, sect1_addr),
01284                          paddress (gdbarch, sect1_endaddr),
01285                          bfd_section_name (abfd2, bfds2), objfile_name (objf2),
01286                          paddress (gdbarch, sect2_addr),
01287                          paddress (gdbarch, sect2_endaddr));
01288             }
01289         }
01290       i = k;
01291     }
01292 
01293   if (i < map_size)
01294     {
01295       gdb_assert (i == map_size - 1);
01296       map[j++] = map[i];
01297     }
01298 
01299   return j;
01300 }
01301 
01302 
01303 /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
01304    TLS, overlay and overlapping sections.  */
01305 
01306 static void
01307 update_section_map (struct program_space *pspace,
01308                     struct obj_section ***pmap, int *pmap_size)
01309 {
01310   struct objfile_pspace_info *pspace_info;
01311   int alloc_size, map_size, i;
01312   struct obj_section *s, **map;
01313   struct objfile *objfile;
01314 
01315   pspace_info = get_objfile_pspace_data (pspace);
01316   gdb_assert (pspace_info->section_map_dirty != 0
01317               || pspace_info->new_objfiles_available != 0);
01318 
01319   map = *pmap;
01320   xfree (map);
01321 
01322   alloc_size = 0;
01323   ALL_PSPACE_OBJFILES (pspace, objfile)
01324     ALL_OBJFILE_OSECTIONS (objfile, s)
01325       if (insert_section_p (objfile->obfd, s->the_bfd_section))
01326         alloc_size += 1;
01327 
01328   /* This happens on detach/attach (e.g. in gdb.base/attach.exp).  */
01329   if (alloc_size == 0)
01330     {
01331       *pmap = NULL;
01332       *pmap_size = 0;
01333       return;
01334     }
01335 
01336   map = xmalloc (alloc_size * sizeof (*map));
01337 
01338   i = 0;
01339   ALL_PSPACE_OBJFILES (pspace, objfile)
01340     ALL_OBJFILE_OSECTIONS (objfile, s)
01341       if (insert_section_p (objfile->obfd, s->the_bfd_section))
01342         map[i++] = s;
01343 
01344   qsort (map, alloc_size, sizeof (*map), qsort_cmp);
01345   map_size = filter_debuginfo_sections(map, alloc_size);
01346   map_size = filter_overlapping_sections(map, map_size);
01347 
01348   if (map_size < alloc_size)
01349     /* Some sections were eliminated.  Trim excess space.  */
01350     map = xrealloc (map, map_size * sizeof (*map));
01351   else
01352     gdb_assert (alloc_size == map_size);
01353 
01354   *pmap = map;
01355   *pmap_size = map_size;
01356 }
01357 
01358 /* Bsearch comparison function.  */
01359 
01360 static int
01361 bsearch_cmp (const void *key, const void *elt)
01362 {
01363   const CORE_ADDR pc = *(CORE_ADDR *) key;
01364   const struct obj_section *section = *(const struct obj_section **) elt;
01365 
01366   if (pc < obj_section_addr (section))
01367     return -1;
01368   if (pc < obj_section_endaddr (section))
01369     return 0;
01370   return 1;
01371 }
01372 
01373 /* Returns a section whose range includes PC or NULL if none found.   */
01374 
01375 struct obj_section *
01376 find_pc_section (CORE_ADDR pc)
01377 {
01378   struct objfile_pspace_info *pspace_info;
01379   struct obj_section *s, **sp;
01380 
01381   /* Check for mapped overlay section first.  */
01382   s = find_pc_mapped_section (pc);
01383   if (s)
01384     return s;
01385 
01386   pspace_info = get_objfile_pspace_data (current_program_space);
01387   if (pspace_info->section_map_dirty
01388       || (pspace_info->new_objfiles_available
01389           && !pspace_info->inhibit_updates))
01390     {
01391       update_section_map (current_program_space,
01392                           &pspace_info->sections,
01393                           &pspace_info->num_sections);
01394 
01395       /* Don't need updates to section map until objfiles are added,
01396          removed or relocated.  */
01397       pspace_info->new_objfiles_available = 0;
01398       pspace_info->section_map_dirty = 0;
01399     }
01400 
01401   /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
01402      bsearch be non-NULL.  */
01403   if (pspace_info->sections == NULL)
01404     {
01405       gdb_assert (pspace_info->num_sections == 0);
01406       return NULL;
01407     }
01408 
01409   sp = (struct obj_section **) bsearch (&pc,
01410                                         pspace_info->sections,
01411                                         pspace_info->num_sections,
01412                                         sizeof (*pspace_info->sections),
01413                                         bsearch_cmp);
01414   if (sp != NULL)
01415     return *sp;
01416   return NULL;
01417 }
01418 
01419 
01420 /* Return non-zero if PC is in a section called NAME.  */
01421 
01422 int
01423 pc_in_section (CORE_ADDR pc, char *name)
01424 {
01425   struct obj_section *s;
01426   int retval = 0;
01427 
01428   s = find_pc_section (pc);
01429 
01430   retval = (s != NULL
01431             && s->the_bfd_section->name != NULL
01432             && strcmp (s->the_bfd_section->name, name) == 0);
01433   return (retval);
01434 }
01435 
01436 
01437 /* Set section_map_dirty so section map will be rebuilt next time it
01438    is used.  Called by reread_symbols.  */
01439 
01440 void
01441 objfiles_changed (void)
01442 {
01443   /* Rebuild section map next time we need it.  */
01444   get_objfile_pspace_data (current_program_space)->section_map_dirty = 1;
01445 }
01446 
01447 /* See comments in objfiles.h.  */
01448 
01449 void
01450 inhibit_section_map_updates (struct program_space *pspace)
01451 {
01452   get_objfile_pspace_data (pspace)->inhibit_updates = 1;
01453 }
01454 
01455 /* See comments in objfiles.h.  */
01456 
01457 void
01458 resume_section_map_updates (struct program_space *pspace)
01459 {
01460   get_objfile_pspace_data (pspace)->inhibit_updates = 0;
01461 }
01462 
01463 /* See comments in objfiles.h.  */
01464 
01465 void
01466 resume_section_map_updates_cleanup (void *arg)
01467 {
01468   resume_section_map_updates (arg);
01469 }
01470 
01471 /* The default implementation for the "iterate_over_objfiles_in_search_order"
01472    gdbarch method.  It is equivalent to use the ALL_OBJFILES macro,
01473    searching the objfiles in the order they are stored internally,
01474    ignoring CURRENT_OBJFILE.
01475 
01476    On most platorms, it should be close enough to doing the best
01477    we can without some knowledge specific to the architecture.  */
01478 
01479 void
01480 default_iterate_over_objfiles_in_search_order
01481   (struct gdbarch *gdbarch,
01482    iterate_over_objfiles_in_search_order_cb_ftype *cb,
01483    void *cb_data, struct objfile *current_objfile)
01484 {
01485   int stop = 0;
01486   struct objfile *objfile;
01487 
01488   ALL_OBJFILES (objfile)
01489     {
01490        stop = cb (objfile, cb_data);
01491        if (stop)
01492          return;
01493     }
01494 }
01495 
01496 /* Return canonical name for OBJFILE.  */
01497 
01498 const char *
01499 objfile_name (const struct objfile *objfile)
01500 {
01501   if (objfile->obfd != NULL)
01502     return bfd_get_filename (objfile->obfd);
01503 
01504   return objfile->original_name;
01505 }
01506 
01507 /* Provide a prototype to silence -Wmissing-prototypes.  */
01508 extern initialize_file_ftype _initialize_objfiles;
01509 
01510 void
01511 _initialize_objfiles (void)
01512 {
01513   objfiles_pspace_data
01514     = register_program_space_data_with_cleanup (NULL,
01515                                                 objfiles_pspace_data_cleanup);
01516 
01517   objfiles_bfd_data = register_bfd_data_with_cleanup (NULL,
01518                                                       objfile_bfd_data_free);
01519 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines