gdb/minsyms.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* GDB routines for manipulating the minimal symbol tables.
  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 minimal symbol tables.

  17.    Minimal symbol tables are used to hold some very basic information about
  18.    all defined global symbols (text, data, bss, abs, etc).  The only two
  19.    required pieces of information are the symbol's name and the address
  20.    associated with that symbol.

  21.    In many cases, even if a file was compiled with no special options for
  22.    debugging at all, as long as was not stripped it will contain sufficient
  23.    information to build useful minimal symbol tables using this structure.

  24.    Even when a file contains enough debugging information to build a full
  25.    symbol table, these minimal symbols are still useful for quickly mapping
  26.    between names and addresses, and vice versa.  They are also sometimes used
  27.    to figure out what full symbol table entries need to be read in.  */


  28. #include "defs.h"
  29. #include <ctype.h>
  30. #include "symtab.h"
  31. #include "bfd.h"
  32. #include "filenames.h"
  33. #include "symfile.h"
  34. #include "objfiles.h"
  35. #include "demangle.h"
  36. #include "value.h"
  37. #include "cp-abi.h"
  38. #include "target.h"
  39. #include "cp-support.h"
  40. #include "language.h"
  41. #include "cli/cli-utils.h"
  42. #include "symbol.h"

  43. /* Accumulate the minimal symbols for each objfile in bunches of BUNCH_SIZE.
  44.    At the end, copy them all into one newly allocated location on an objfile's
  45.    per-BFD storage obstack.  */

  46. #define BUNCH_SIZE 127

  47. struct msym_bunch
  48.   {
  49.     struct msym_bunch *next;
  50.     struct minimal_symbol contents[BUNCH_SIZE];
  51.   };

  52. /* Bunch currently being filled up.
  53.    The next field points to chain of filled bunches.  */

  54. static struct msym_bunch *msym_bunch;

  55. /* Number of slots filled in current bunch.  */

  56. static int msym_bunch_index;

  57. /* Total number of minimal symbols recorded so far for the objfile.  */

  58. static int msym_count;

  59. /* See minsyms.h.  */

  60. unsigned int
  61. msymbol_hash_iw (const char *string)
  62. {
  63.   unsigned int hash = 0;

  64.   while (*string && *string != '(')
  65.     {
  66.       string = skip_spaces_const (string);
  67.       if (*string && *string != '(')
  68.         {
  69.           hash = SYMBOL_HASH_NEXT (hash, *string);
  70.           ++string;
  71.         }
  72.     }
  73.   return hash;
  74. }

  75. /* See minsyms.h.  */

  76. unsigned int
  77. msymbol_hash (const char *string)
  78. {
  79.   unsigned int hash = 0;

  80.   for (; *string; ++string)
  81.     hash = SYMBOL_HASH_NEXT (hash, *string);
  82.   return hash;
  83. }

  84. /* Add the minimal symbol SYM to an objfile's minsym hash table, TABLE.  */
  85. static void
  86. add_minsym_to_hash_table (struct minimal_symbol *sym,
  87.                           struct minimal_symbol **table)
  88. {
  89.   if (sym->hash_next == NULL)
  90.     {
  91.       unsigned int hash
  92.         = msymbol_hash (MSYMBOL_LINKAGE_NAME (sym)) % MINIMAL_SYMBOL_HASH_SIZE;

  93.       sym->hash_next = table[hash];
  94.       table[hash] = sym;
  95.     }
  96. }

  97. /* Add the minimal symbol SYM to an objfile's minsym demangled hash table,
  98.    TABLE.  */
  99. static void
  100. add_minsym_to_demangled_hash_table (struct minimal_symbol *sym,
  101.                                   struct minimal_symbol **table)
  102. {
  103.   if (sym->demangled_hash_next == NULL)
  104.     {
  105.       unsigned int hash = msymbol_hash_iw (MSYMBOL_SEARCH_NAME (sym))
  106.         % MINIMAL_SYMBOL_HASH_SIZE;

  107.       sym->demangled_hash_next = table[hash];
  108.       table[hash] = sym;
  109.     }
  110. }

  111. /* Look through all the current minimal symbol tables and find the
  112.    first minimal symbol that matches NAME.  If OBJF is non-NULL, limit
  113.    the search to that objfile.  If SFILE is non-NULL, the only file-scope
  114.    symbols considered will be from that source file (global symbols are
  115.    still preferred).  Returns a pointer to the minimal symbol that
  116.    matches, or NULL if no match is found.

  117.    Note:  One instance where there may be duplicate minimal symbols with
  118.    the same name is when the symbol tables for a shared library and the
  119.    symbol tables for an executable contain global symbols with the same
  120.    names (the dynamic linker deals with the duplication).

  121.    It's also possible to have minimal symbols with different mangled
  122.    names, but identical demangled names.  For example, the GNU C++ v3
  123.    ABI requires the generation of two (or perhaps three) copies of
  124.    constructor functions --- "in-charge", "not-in-charge", and
  125.    "allocate" copies; destructors may be duplicated as well.
  126.    Obviously, there must be distinct mangled names for each of these,
  127.    but the demangled names are all the same: S::S or S::~S.  */

  128. struct bound_minimal_symbol
  129. lookup_minimal_symbol (const char *name, const char *sfile,
  130.                        struct objfile *objf)
  131. {
  132.   struct objfile *objfile;
  133.   struct bound_minimal_symbol found_symbol = { NULL, NULL };
  134.   struct bound_minimal_symbol found_file_symbol = { NULL, NULL };
  135.   struct bound_minimal_symbol trampoline_symbol = { NULL, NULL };

  136.   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
  137.   unsigned int dem_hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE;

  138.   int needtofreename = 0;
  139.   const char *modified_name;

  140.   if (sfile != NULL)
  141.     sfile = lbasename (sfile);

  142.   /* For C++, canonicalize the input name.  */
  143.   modified_name = name;
  144.   if (current_language->la_language == language_cplus)
  145.     {
  146.       char *cname = cp_canonicalize_string (name);

  147.       if (cname)
  148.         {
  149.           modified_name = cname;
  150.           needtofreename = 1;
  151.         }
  152.     }

  153.   for (objfile = object_files;
  154.        objfile != NULL && found_symbol.minsym == NULL;
  155.        objfile = objfile->next)
  156.     {
  157.       struct minimal_symbol *msymbol;

  158.       if (objf == NULL || objf == objfile
  159.           || objf == objfile->separate_debug_objfile_backlink)
  160.         {
  161.           /* Do two passes: the first over the ordinary hash table,
  162.              and the second over the demangled hash table.  */
  163.         int pass;

  164.         if (symbol_lookup_debug)
  165.           {
  166.             fprintf_unfiltered (gdb_stdlog,
  167.                                 "lookup_minimal_symbol (%s, %s, %s)\n",
  168.                                 name, sfile != NULL ? sfile : "NULL",
  169.                                 objfile_debug_name (objfile));
  170.           }

  171.         for (pass = 1; pass <= 2 && found_symbol.minsym == NULL; pass++)
  172.             {
  173.             /* Select hash list according to pass.  */
  174.             if (pass == 1)
  175.               msymbol = objfile->per_bfd->msymbol_hash[hash];
  176.             else
  177.               msymbol = objfile->per_bfd->msymbol_demangled_hash[dem_hash];

  178.             while (msymbol != NULL && found_symbol.minsym == NULL)
  179.                 {
  180.                   int match;

  181.                   if (pass == 1)
  182.                     {
  183.                       int (*cmp) (const char *, const char *);

  184.                       cmp = (case_sensitivity == case_sensitive_on
  185.                              ? strcmp : strcasecmp);
  186.                       match = cmp (MSYMBOL_LINKAGE_NAME (msymbol),
  187.                                    modified_name) == 0;
  188.                     }
  189.                   else
  190.                     {
  191.                       /* The function respects CASE_SENSITIVITY.  */
  192.                       match = MSYMBOL_MATCHES_SEARCH_NAME (msymbol,
  193.                                                           modified_name);
  194.                     }

  195.                   if (match)
  196.                     {
  197.                     switch (MSYMBOL_TYPE (msymbol))
  198.                       {
  199.                       case mst_file_text:
  200.                       case mst_file_data:
  201.                       case mst_file_bss:
  202.                         if (sfile == NULL
  203.                             || filename_cmp (msymbol->filename, sfile) == 0)
  204.                           {
  205.                             found_file_symbol.minsym = msymbol;
  206.                             found_file_symbol.objfile = objfile;
  207.                           }
  208.                         break;

  209.                       case mst_solib_trampoline:

  210.                         /* If a trampoline symbol is found, we prefer to
  211.                            keep looking for the *real* symbol.  If the
  212.                            actual symbol is not found, then we'll use the
  213.                            trampoline entry.  */
  214.                         if (trampoline_symbol.minsym == NULL)
  215.                           {
  216.                             trampoline_symbol.minsym = msymbol;
  217.                             trampoline_symbol.objfile = objfile;
  218.                           }
  219.                         break;

  220.                       case mst_unknown:
  221.                       default:
  222.                         found_symbol.minsym = msymbol;
  223.                         found_symbol.objfile = objfile;
  224.                         break;
  225.                       }
  226.                     }

  227.                 /* Find the next symbol on the hash chain.  */
  228.                 if (pass == 1)
  229.                   msymbol = msymbol->hash_next;
  230.                 else
  231.                   msymbol = msymbol->demangled_hash_next;
  232.                 }
  233.             }
  234.         }
  235.     }

  236.   if (needtofreename)
  237.     xfree ((void *) modified_name);

  238.   /* External symbols are best.  */
  239.   if (found_symbol.minsym != NULL)
  240.     {
  241.       if (symbol_lookup_debug)
  242.         {
  243.           fprintf_unfiltered (gdb_stdlog,
  244.                               "lookup_minimal_symbol (...) = %s"
  245.                               " (external)\n",
  246.                               host_address_to_string (found_symbol.minsym));
  247.         }
  248.       return found_symbol;
  249.     }

  250.   /* File-local symbols are next best.  */
  251.   if (found_file_symbol.minsym != NULL)
  252.     {
  253.       if (symbol_lookup_debug)
  254.         {
  255.           fprintf_unfiltered (gdb_stdlog,
  256.                               "lookup_minimal_symbol (...) = %s"
  257.                               " (file-local)\n",
  258.                               host_address_to_string
  259.                                 (found_file_symbol.minsym));
  260.         }
  261.       return found_file_symbol;
  262.     }

  263.   /* Symbols for shared library trampolines are next best.  */
  264.   if (symbol_lookup_debug)
  265.     {
  266.       fprintf_unfiltered (gdb_stdlog,
  267.                           "lookup_minimal_symbol (...) = %s%s\n",
  268.                           trampoline_symbol.minsym != NULL
  269.                           ? host_address_to_string (trampoline_symbol.minsym)
  270.                           : "NULL",
  271.                           trampoline_symbol.minsym != NULL
  272.                           ? " (trampoline)" : "");
  273.     }
  274.   return trampoline_symbol;
  275. }

  276. /* See minsyms.h.  */

  277. struct bound_minimal_symbol
  278. lookup_bound_minimal_symbol (const char *name)
  279. {
  280.   return lookup_minimal_symbol (name, NULL, NULL);
  281. }

  282. /* See common/symbol.h.  */

  283. int
  284. find_minimal_symbol_address (const char *name, CORE_ADDR *addr,
  285.                              struct objfile *objfile)
  286. {
  287.   struct bound_minimal_symbol sym
  288.     = lookup_minimal_symbol (name, NULL, objfile);

  289.   if (sym.minsym != NULL)
  290.     *addr = BMSYMBOL_VALUE_ADDRESS (sym);

  291.   return sym.minsym == NULL;
  292. }

  293. /* See minsyms.h.  */

  294. void
  295. iterate_over_minimal_symbols (struct objfile *objf, const char *name,
  296.                               void (*callback) (struct minimal_symbol *,
  297.                                                 void *),
  298.                               void *user_data)
  299. {
  300.   unsigned int hash;
  301.   struct minimal_symbol *iter;
  302.   int (*cmp) (const char *, const char *);

  303.   /* The first pass is over the ordinary hash table.  */
  304.   hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
  305.   iter = objf->per_bfd->msymbol_hash[hash];
  306.   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
  307.   while (iter)
  308.     {
  309.       if (cmp (MSYMBOL_LINKAGE_NAME (iter), name) == 0)
  310.         (*callback) (iter, user_data);
  311.       iter = iter->hash_next;
  312.     }

  313.   /* The second pass is over the demangled table.  */
  314.   hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE;
  315.   iter = objf->per_bfd->msymbol_demangled_hash[hash];
  316.   while (iter)
  317.     {
  318.       if (MSYMBOL_MATCHES_SEARCH_NAME (iter, name))
  319.         (*callback) (iter, user_data);
  320.       iter = iter->demangled_hash_next;
  321.     }
  322. }

  323. /* See minsyms.h.  */

  324. struct bound_minimal_symbol
  325. lookup_minimal_symbol_text (const char *name, struct objfile *objf)
  326. {
  327.   struct objfile *objfile;
  328.   struct minimal_symbol *msymbol;
  329.   struct bound_minimal_symbol found_symbol = { NULL, NULL };
  330.   struct bound_minimal_symbol found_file_symbol = { NULL, NULL };

  331.   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;

  332.   for (objfile = object_files;
  333.        objfile != NULL && found_symbol.minsym == NULL;
  334.        objfile = objfile->next)
  335.     {
  336.       if (objf == NULL || objf == objfile
  337.           || objf == objfile->separate_debug_objfile_backlink)
  338.         {
  339.           for (msymbol = objfile->per_bfd->msymbol_hash[hash];
  340.                msymbol != NULL && found_symbol.minsym == NULL;
  341.                msymbol = msymbol->hash_next)
  342.             {
  343.               if (strcmp (MSYMBOL_LINKAGE_NAME (msymbol), name) == 0 &&
  344.                   (MSYMBOL_TYPE (msymbol) == mst_text
  345.                    || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
  346.                    || MSYMBOL_TYPE (msymbol) == mst_file_text))
  347.                 {
  348.                   switch (MSYMBOL_TYPE (msymbol))
  349.                     {
  350.                     case mst_file_text:
  351.                       found_file_symbol.minsym = msymbol;
  352.                       found_file_symbol.objfile = objfile;
  353.                       break;
  354.                     default:
  355.                       found_symbol.minsym = msymbol;
  356.                       found_symbol.objfile = objfile;
  357.                       break;
  358.                     }
  359.                 }
  360.             }
  361.         }
  362.     }
  363.   /* External symbols are best.  */
  364.   if (found_symbol.minsym)
  365.     return found_symbol;

  366.   /* File-local symbols are next best.  */
  367.   return found_file_symbol;
  368. }

  369. /* See minsyms.h.  */

  370. struct minimal_symbol *
  371. lookup_minimal_symbol_by_pc_name (CORE_ADDR pc, const char *name,
  372.                                   struct objfile *objf)
  373. {
  374.   struct objfile *objfile;
  375.   struct minimal_symbol *msymbol;

  376.   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;

  377.   for (objfile = object_files;
  378.        objfile != NULL;
  379.        objfile = objfile->next)
  380.     {
  381.       if (objf == NULL || objf == objfile
  382.           || objf == objfile->separate_debug_objfile_backlink)
  383.         {
  384.           for (msymbol = objfile->per_bfd->msymbol_hash[hash];
  385.                msymbol != NULL;
  386.                msymbol = msymbol->hash_next)
  387.             {
  388.               if (MSYMBOL_VALUE_ADDRESS (objfile, msymbol) == pc
  389.                   && strcmp (MSYMBOL_LINKAGE_NAME (msymbol), name) == 0)
  390.                 return msymbol;
  391.             }
  392.         }
  393.     }

  394.   return NULL;
  395. }

  396. /* See minsyms.h.  */

  397. struct bound_minimal_symbol
  398. lookup_minimal_symbol_solib_trampoline (const char *name,
  399.                                         struct objfile *objf)
  400. {
  401.   struct objfile *objfile;
  402.   struct minimal_symbol *msymbol;
  403.   struct bound_minimal_symbol found_symbol = { NULL, NULL };

  404.   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;

  405.   for (objfile = object_files;
  406.        objfile != NULL;
  407.        objfile = objfile->next)
  408.     {
  409.       if (objf == NULL || objf == objfile
  410.           || objf == objfile->separate_debug_objfile_backlink)
  411.         {
  412.           for (msymbol = objfile->per_bfd->msymbol_hash[hash];
  413.                msymbol != NULL;
  414.                msymbol = msymbol->hash_next)
  415.             {
  416.               if (strcmp (MSYMBOL_LINKAGE_NAME (msymbol), name) == 0 &&
  417.                   MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
  418.                 {
  419.                   found_symbol.objfile = objfile;
  420.                   found_symbol.minsym = msymbol;
  421.                   return found_symbol;
  422.                 }
  423.             }
  424.         }
  425.     }

  426.   return found_symbol;
  427. }

  428. /* A helper function that makes *PC section-relative.  This searches
  429.    the sections of OBJFILE and if *PC is in a section, it subtracts
  430.    the section offset and returns true.  Otherwise it returns
  431.    false.  */

  432. static int
  433. frob_address (struct objfile *objfile, CORE_ADDR *pc)
  434. {
  435.   struct obj_section *iter;

  436.   ALL_OBJFILE_OSECTIONS (objfile, iter)
  437.     {
  438.       if (*pc >= obj_section_addr (iter) && *pc < obj_section_endaddr (iter))
  439.         {
  440.           *pc -= obj_section_offset (iter);
  441.           return 1;
  442.         }
  443.     }

  444.   return 0;
  445. }

  446. /* Search through the minimal symbol table for each objfile and find
  447.    the symbol whose address is the largest address that is still less
  448.    than or equal to PC, and matches SECTION (which is not NULL).
  449.    Returns a pointer to the minimal symbol if such a symbol is found,
  450.    or NULL if PC is not in a suitable range.
  451.    Note that we need to look through ALL the minimal symbol tables
  452.    before deciding on the symbol that comes closest to the specified PC.
  453.    This is because objfiles can overlap, for example objfile A has .text
  454.    at 0x100 and .data at 0x40000 and objfile B has .text at 0x234 and
  455.    .data at 0x40048.

  456.    If WANT_TRAMPOLINE is set, prefer mst_solib_trampoline symbols when
  457.    there are text and trampoline symbols at the same address.
  458.    Otherwise prefer mst_text symbols.  */

  459. static struct bound_minimal_symbol
  460. lookup_minimal_symbol_by_pc_section_1 (CORE_ADDR pc_in,
  461.                                        struct obj_section *section,
  462.                                        int want_trampoline)
  463. {
  464.   int lo;
  465.   int hi;
  466.   int new;
  467.   struct objfile *objfile;
  468.   struct minimal_symbol *msymbol;
  469.   struct minimal_symbol *best_symbol = NULL;
  470.   struct objfile *best_objfile = NULL;
  471.   struct bound_minimal_symbol result;
  472.   enum minimal_symbol_type want_type, other_type;

  473.   want_type = want_trampoline ? mst_solib_trampoline : mst_text;
  474.   other_type = want_trampoline ? mst_text : mst_solib_trampoline;

  475.   /* We can not require the symbol found to be in section, because
  476.      e.g. IRIX 6.5 mdebug relies on this code returning an absolute
  477.      symbol - but find_pc_section won't return an absolute section and
  478.      hence the code below would skip over absolute symbols.  We can
  479.      still take advantage of the call to find_pc_section, though - the
  480.      object file still must match.  In case we have separate debug
  481.      files, search both the file and its separate debug file.  There's
  482.      no telling which one will have the minimal symbols.  */

  483.   gdb_assert (section != NULL);

  484.   for (objfile = section->objfile;
  485.        objfile != NULL;
  486.        objfile = objfile_separate_debug_iterate (section->objfile, objfile))
  487.     {
  488.       CORE_ADDR pc = pc_in;

  489.       /* If this objfile has a minimal symbol table, go search it using
  490.          a binary search.  Note that a minimal symbol table always consists
  491.          of at least two symbols, a "real" symbol and the terminating
  492.          "null symbol".  If there are no real symbols, then there is no
  493.          minimal symbol table at all.  */

  494.       if (objfile->per_bfd->minimal_symbol_count > 0)
  495.         {
  496.           int best_zero_sized = -1;

  497.           msymbol = objfile->per_bfd->msymbols;
  498.           lo = 0;
  499.           hi = objfile->per_bfd->minimal_symbol_count - 1;

  500.           /* This code assumes that the minimal symbols are sorted by
  501.              ascending address values.  If the pc value is greater than or
  502.              equal to the first symbol's address, then some symbol in this
  503.              minimal symbol table is a suitable candidate for being the
  504.              "best" symbol.  This includes the last real symbol, for cases
  505.              where the pc value is larger than any address in this vector.

  506.              By iterating until the address associated with the current
  507.              hi index (the endpoint of the test interval) is less than
  508.              or equal to the desired pc value, we accomplish two things:
  509.              (1) the case where the pc value is larger than any minimal
  510.              symbol address is trivially solved, (2) the address associated
  511.              with the hi index is always the one we want when the interation
  512.              terminates.  In essence, we are iterating the test interval
  513.              down until the pc value is pushed out of it from the high end.

  514.              Warning: this code is trickier than it would appear at first.  */

  515.           if (frob_address (objfile, &pc)
  516.               && pc >= MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[lo]))
  517.             {
  518.               while (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi]) > pc)
  519.                 {
  520.                   /* pc is still strictly less than highest address.  */
  521.                   /* Note "new" will always be >= lo.  */
  522.                   new = (lo + hi) / 2;
  523.                   if ((MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[new]) >= pc)
  524.                       || (lo == new))
  525.                     {
  526.                       hi = new;
  527.                     }
  528.                   else
  529.                     {
  530.                       lo = new;
  531.                     }
  532.                 }

  533.               /* If we have multiple symbols at the same address, we want
  534.                  hi to point to the last one.  That way we can find the
  535.                  right symbol if it has an index greater than hi.  */
  536.               while (hi < objfile->per_bfd->minimal_symbol_count - 1
  537.                      && (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
  538.                          == MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi + 1])))
  539.                 hi++;

  540.               /* Skip various undesirable symbols.  */
  541.               while (hi >= 0)
  542.                 {
  543.                   /* Skip any absolute symbols.  This is apparently
  544.                      what adb and dbx do, and is needed for the CM-5.
  545.                      There are two known possible problems: (1) on
  546.                      ELF, apparently end, edata, etc. are absolute.
  547.                      Not sure ignoring them here is a big deal, but if
  548.                      we want to use them, the fix would go in
  549.                      elfread.c.  (2) I think shared library entry
  550.                      points on the NeXT are absolute.  If we want
  551.                      special handling for this it probably should be
  552.                      triggered by a special mst_abs_or_lib or some
  553.                      such.  */

  554.                   if (MSYMBOL_TYPE (&msymbol[hi]) == mst_abs)
  555.                     {
  556.                       hi--;
  557.                       continue;
  558.                     }

  559.                   /* If SECTION was specified, skip any symbol from
  560.                      wrong section.  */
  561.                   if (section
  562.                       /* Some types of debug info, such as COFF,
  563.                          don't fill the bfd_section member, so don't
  564.                          throw away symbols on those platforms.  */
  565.                       && MSYMBOL_OBJ_SECTION (objfile, &msymbol[hi]) != NULL
  566.                       && (!matching_obj_sections
  567.                           (MSYMBOL_OBJ_SECTION (objfile, &msymbol[hi]),
  568.                            section)))
  569.                     {
  570.                       hi--;
  571.                       continue;
  572.                     }

  573.                   /* If we are looking for a trampoline and this is a
  574.                      text symbol, or the other way around, check the
  575.                      preceding symbol too.  If they are otherwise
  576.                      identical prefer that one.  */
  577.                   if (hi > 0
  578.                       && MSYMBOL_TYPE (&msymbol[hi]) == other_type
  579.                       && MSYMBOL_TYPE (&msymbol[hi - 1]) == want_type
  580.                       && (MSYMBOL_SIZE (&msymbol[hi])
  581.                           == MSYMBOL_SIZE (&msymbol[hi - 1]))
  582.                       && (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
  583.                           == MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi - 1]))
  584.                       && (MSYMBOL_OBJ_SECTION (objfile, &msymbol[hi])
  585.                           == MSYMBOL_OBJ_SECTION (objfile, &msymbol[hi - 1])))
  586.                     {
  587.                       hi--;
  588.                       continue;
  589.                     }

  590.                   /* If the minimal symbol has a zero size, save it
  591.                      but keep scanning backwards looking for one with
  592.                      a non-zero size.  A zero size may mean that the
  593.                      symbol isn't an object or function (e.g. a
  594.                      label), or it may just mean that the size was not
  595.                      specified.  */
  596.                   if (MSYMBOL_SIZE (&msymbol[hi]) == 0
  597.                       && best_zero_sized == -1)
  598.                     {
  599.                       best_zero_sized = hi;
  600.                       hi--;
  601.                       continue;
  602.                     }

  603.                   /* If we are past the end of the current symbol, try
  604.                      the previous symbol if it has a larger overlapping
  605.                      size.  This happens on i686-pc-linux-gnu with glibc;
  606.                      the nocancel variants of system calls are inside
  607.                      the cancellable variants, but both have sizes.  */
  608.                   if (hi > 0
  609.                       && MSYMBOL_SIZE (&msymbol[hi]) != 0
  610.                       && pc >= (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
  611.                                 + MSYMBOL_SIZE (&msymbol[hi]))
  612.                       && pc < (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi - 1])
  613.                                + MSYMBOL_SIZE (&msymbol[hi - 1])))
  614.                     {
  615.                       hi--;
  616.                       continue;
  617.                     }

  618.                   /* Otherwise, this symbol must be as good as we're going
  619.                      to get.  */
  620.                   break;
  621.                 }

  622.               /* If HI has a zero size, and best_zero_sized is set,
  623.                  then we had two or more zero-sized symbols; prefer
  624.                  the first one we found (which may have a higher
  625.                  address).  Also, if we ran off the end, be sure
  626.                  to back up.  */
  627.               if (best_zero_sized != -1
  628.                   && (hi < 0 || MSYMBOL_SIZE (&msymbol[hi]) == 0))
  629.                 hi = best_zero_sized;

  630.               /* If the minimal symbol has a non-zero size, and this
  631.                  PC appears to be outside the symbol's contents, then
  632.                  refuse to use this symbol.  If we found a zero-sized
  633.                  symbol with an address greater than this symbol's,
  634.                  use that instead.  We assume that if symbols have
  635.                  specified sizes, they do not overlap.  */

  636.               if (hi >= 0
  637.                   && MSYMBOL_SIZE (&msymbol[hi]) != 0
  638.                   && pc >= (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
  639.                             + MSYMBOL_SIZE (&msymbol[hi])))
  640.                 {
  641.                   if (best_zero_sized != -1)
  642.                     hi = best_zero_sized;
  643.                   else
  644.                     /* Go on to the next object file.  */
  645.                     continue;
  646.                 }

  647.               /* The minimal symbol indexed by hi now is the best one in this
  648.                  objfile's minimal symbol table.  See if it is the best one
  649.                  overall.  */

  650.               if (hi >= 0
  651.                   && ((best_symbol == NULL) ||
  652.                       (MSYMBOL_VALUE_RAW_ADDRESS (best_symbol) <
  653.                        MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi]))))
  654.                 {
  655.                   best_symbol = &msymbol[hi];
  656.                   best_objfile = objfile;
  657.                 }
  658.             }
  659.         }
  660.     }

  661.   result.minsym = best_symbol;
  662.   result.objfile = best_objfile;
  663.   return result;
  664. }

  665. struct bound_minimal_symbol
  666. lookup_minimal_symbol_by_pc_section (CORE_ADDR pc, struct obj_section *section)
  667. {
  668.   if (section == NULL)
  669.     {
  670.       /* NOTE: cagney/2004-01-27: This was using find_pc_mapped_section to
  671.          force the section but that (well unless you're doing overlay
  672.          debugging) always returns NULL making the call somewhat useless.  */
  673.       section = find_pc_section (pc);
  674.       if (section == NULL)
  675.         {
  676.           struct bound_minimal_symbol result;

  677.           memset (&result, 0, sizeof (result));
  678.           return result;
  679.         }
  680.     }
  681.   return lookup_minimal_symbol_by_pc_section_1 (pc, section, 0);
  682. }

  683. /* See minsyms.h.  */

  684. struct bound_minimal_symbol
  685. lookup_minimal_symbol_by_pc (CORE_ADDR pc)
  686. {
  687.   struct obj_section *section = find_pc_section (pc);

  688.   if (section == NULL)
  689.     {
  690.       struct bound_minimal_symbol result;

  691.       memset (&result, 0, sizeof (result));
  692.       return result;
  693.     }
  694.   return lookup_minimal_symbol_by_pc_section_1 (pc, section, 0);
  695. }

  696. /* Return non-zero iff PC is in an STT_GNU_IFUNC function resolver.  */

  697. int
  698. in_gnu_ifunc_stub (CORE_ADDR pc)
  699. {
  700.   struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc);

  701.   return msymbol.minsym && MSYMBOL_TYPE (msymbol.minsym) == mst_text_gnu_ifunc;
  702. }

  703. /* See elf_gnu_ifunc_resolve_addr for its real implementation.  */

  704. static CORE_ADDR
  705. stub_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
  706. {
  707.   error (_("GDB cannot resolve STT_GNU_IFUNC symbol at address %s without "
  708.            "the ELF support compiled in."),
  709.          paddress (gdbarch, pc));
  710. }

  711. /* See elf_gnu_ifunc_resolve_name for its real implementation.  */

  712. static int
  713. stub_gnu_ifunc_resolve_name (const char *function_name,
  714.                              CORE_ADDR *function_address_p)
  715. {
  716.   error (_("GDB cannot resolve STT_GNU_IFUNC symbol \"%s\" without "
  717.            "the ELF support compiled in."),
  718.          function_name);
  719. }

  720. /* See elf_gnu_ifunc_resolver_stop for its real implementation.  */

  721. static void
  722. stub_gnu_ifunc_resolver_stop (struct breakpoint *b)
  723. {
  724.   internal_error (__FILE__, __LINE__,
  725.                   _("elf_gnu_ifunc_resolver_stop cannot be reached."));
  726. }

  727. /* See elf_gnu_ifunc_resolver_return_stop for its real implementation.  */

  728. static void
  729. stub_gnu_ifunc_resolver_return_stop (struct breakpoint *b)
  730. {
  731.   internal_error (__FILE__, __LINE__,
  732.                   _("elf_gnu_ifunc_resolver_return_stop cannot be reached."));
  733. }

  734. /* See elf_gnu_ifunc_fns for its real implementation.  */

  735. static const struct gnu_ifunc_fns stub_gnu_ifunc_fns =
  736. {
  737.   stub_gnu_ifunc_resolve_addr,
  738.   stub_gnu_ifunc_resolve_name,
  739.   stub_gnu_ifunc_resolver_stop,
  740.   stub_gnu_ifunc_resolver_return_stop,
  741. };

  742. /* A placeholder for &elf_gnu_ifunc_fns.  */

  743. const struct gnu_ifunc_fns *gnu_ifunc_fns_p = &stub_gnu_ifunc_fns;

  744. /* See minsyms.h.  */

  745. struct bound_minimal_symbol
  746. lookup_minimal_symbol_and_objfile (const char *name)
  747. {
  748.   struct bound_minimal_symbol result;
  749.   struct objfile *objfile;
  750.   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;

  751.   ALL_OBJFILES (objfile)
  752.     {
  753.       struct minimal_symbol *msym;

  754.       for (msym = objfile->per_bfd->msymbol_hash[hash];
  755.            msym != NULL;
  756.            msym = msym->hash_next)
  757.         {
  758.           if (strcmp (MSYMBOL_LINKAGE_NAME (msym), name) == 0)
  759.             {
  760.               result.minsym = msym;
  761.               result.objfile = objfile;
  762.               return result;
  763.             }
  764.         }
  765.     }

  766.   memset (&result, 0, sizeof (result));
  767.   return result;
  768. }


  769. /* Return leading symbol character for a BFD.  If BFD is NULL,
  770.    return the leading symbol character from the main objfile.  */

  771. static int
  772. get_symbol_leading_char (bfd *abfd)
  773. {
  774.   if (abfd != NULL)
  775.     return bfd_get_symbol_leading_char (abfd);
  776.   if (symfile_objfile != NULL && symfile_objfile->obfd != NULL)
  777.     return bfd_get_symbol_leading_char (symfile_objfile->obfd);
  778.   return 0;
  779. }

  780. /* See minsyms.h.  */

  781. void
  782. init_minimal_symbol_collection (void)
  783. {
  784.   msym_count = 0;
  785.   msym_bunch = NULL;
  786.   /* Note that presetting msym_bunch_index to BUNCH_SIZE causes the
  787.      first call to save a minimal symbol to allocate the memory for
  788.      the first bunch.  */
  789.   msym_bunch_index = BUNCH_SIZE;
  790. }

  791. /* See minsyms.h.  */

  792. void
  793. prim_record_minimal_symbol (const char *name, CORE_ADDR address,
  794.                             enum minimal_symbol_type ms_type,
  795.                             struct objfile *objfile)
  796. {
  797.   int section;

  798.   switch (ms_type)
  799.     {
  800.     case mst_text:
  801.     case mst_text_gnu_ifunc:
  802.     case mst_file_text:
  803.     case mst_solib_trampoline:
  804.       section = SECT_OFF_TEXT (objfile);
  805.       break;
  806.     case mst_data:
  807.     case mst_file_data:
  808.       section = SECT_OFF_DATA (objfile);
  809.       break;
  810.     case mst_bss:
  811.     case mst_file_bss:
  812.       section = SECT_OFF_BSS (objfile);
  813.       break;
  814.     default:
  815.       section = -1;
  816.     }

  817.   prim_record_minimal_symbol_and_info (name, address, ms_type,
  818.                                        section, objfile);
  819. }

  820. /* See minsyms.h.  */

  821. struct minimal_symbol *
  822. prim_record_minimal_symbol_full (const char *name, int name_len, int copy_name,
  823.                                  CORE_ADDR address,
  824.                                  enum minimal_symbol_type ms_type,
  825.                                  int section,
  826.                                  struct objfile *objfile)
  827. {
  828.   struct obj_section *obj_section;
  829.   struct msym_bunch *new;
  830.   struct minimal_symbol *msymbol;

  831.   /* Don't put gcc_compiled, __gnu_compiled_cplus, and friends into
  832.      the minimal symbols, because if there is also another symbol
  833.      at the same address (e.g. the first function of the file),
  834.      lookup_minimal_symbol_by_pc would have no way of getting the
  835.      right one.  */
  836.   if (ms_type == mst_file_text && name[0] == 'g'
  837.       && (strcmp (name, GCC_COMPILED_FLAG_SYMBOL) == 0
  838.           || strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0))
  839.     return (NULL);

  840.   /* It's safe to strip the leading char here once, since the name
  841.      is also stored stripped in the minimal symbol table.  */
  842.   if (name[0] == get_symbol_leading_char (objfile->obfd))
  843.     {
  844.       ++name;
  845.       --name_len;
  846.     }

  847.   if (ms_type == mst_file_text && strncmp (name, "__gnu_compiled", 14) == 0)
  848.     return (NULL);

  849.   if (msym_bunch_index == BUNCH_SIZE)
  850.     {
  851.       new = XCNEW (struct msym_bunch);
  852.       msym_bunch_index = 0;
  853.       new->next = msym_bunch;
  854.       msym_bunch = new;
  855.     }
  856.   msymbol = &msym_bunch->contents[msym_bunch_index];
  857.   MSYMBOL_SET_LANGUAGE (msymbol, language_auto,
  858.                         &objfile->per_bfd->storage_obstack);
  859.   MSYMBOL_SET_NAMES (msymbol, name, name_len, copy_name, objfile);

  860.   SET_MSYMBOL_VALUE_ADDRESS (msymbol, address);
  861.   MSYMBOL_SECTION (msymbol) = section;

  862.   MSYMBOL_TYPE (msymbol) = ms_type;
  863.   MSYMBOL_TARGET_FLAG_1 (msymbol) = 0;
  864.   MSYMBOL_TARGET_FLAG_2 (msymbol) = 0;
  865.   /* Do not use the SET_MSYMBOL_SIZE macro to initialize the size,
  866.      as it would also set the has_size flag.  */
  867.   msymbol->size = 0;

  868.   /* The hash pointers must be cleared! If they're not,
  869.      add_minsym_to_hash_table will NOT add this msymbol to the hash table.  */
  870.   msymbol->hash_next = NULL;
  871.   msymbol->demangled_hash_next = NULL;

  872.   /* If we already read minimal symbols for this objfile, then don't
  873.      ever allocate a new one.  */
  874.   if (!objfile->per_bfd->minsyms_read)
  875.     {
  876.       msym_bunch_index++;
  877.       objfile->per_bfd->n_minsyms++;
  878.     }
  879.   msym_count++;
  880.   return msymbol;
  881. }

  882. /* See minsyms.h.  */

  883. struct minimal_symbol *
  884. prim_record_minimal_symbol_and_info (const char *name, CORE_ADDR address,
  885.                                      enum minimal_symbol_type ms_type,
  886.                                      int section,
  887.                                      struct objfile *objfile)
  888. {
  889.   return prim_record_minimal_symbol_full (name, strlen (name), 1,
  890.                                           address, ms_type,
  891.                                           section, objfile);
  892. }

  893. /* Compare two minimal symbols by address and return a signed result based
  894.    on unsigned comparisons, so that we sort into unsigned numeric order.
  895.    Within groups with the same address, sort by name.  */

  896. static int
  897. compare_minimal_symbols (const void *fn1p, const void *fn2p)
  898. {
  899.   const struct minimal_symbol *fn1;
  900.   const struct minimal_symbol *fn2;

  901.   fn1 = (const struct minimal_symbol *) fn1p;
  902.   fn2 = (const struct minimal_symbol *) fn2p;

  903.   if (MSYMBOL_VALUE_RAW_ADDRESS (fn1) < MSYMBOL_VALUE_RAW_ADDRESS (fn2))
  904.     {
  905.       return (-1);                /* addr 1 is less than addr 2.  */
  906.     }
  907.   else if (MSYMBOL_VALUE_RAW_ADDRESS (fn1) > MSYMBOL_VALUE_RAW_ADDRESS (fn2))
  908.     {
  909.       return (1);                /* addr 1 is greater than addr 2.  */
  910.     }
  911.   else
  912.     /* addrs are equal: sort by name */
  913.     {
  914.       const char *name1 = MSYMBOL_LINKAGE_NAME (fn1);
  915.       const char *name2 = MSYMBOL_LINKAGE_NAME (fn2);

  916.       if (name1 && name2)        /* both have names */
  917.         return strcmp (name1, name2);
  918.       else if (name2)
  919.         return 1;                /* fn1 has no name, so it is "less".  */
  920.       else if (name1)                /* fn2 has no name, so it is "less".  */
  921.         return -1;
  922.       else
  923.         return (0);                /* Neither has a name, so they're equal.  */
  924.     }
  925. }

  926. /* Discard the currently collected minimal symbols, if any.  If we wish
  927.    to save them for later use, we must have already copied them somewhere
  928.    else before calling this function.

  929.    FIXME:  We could allocate the minimal symbol bunches on their own
  930.    obstack and then simply blow the obstack away when we are done with
  931.    it.  Is it worth the extra trouble though?  */

  932. static void
  933. do_discard_minimal_symbols_cleanup (void *arg)
  934. {
  935.   struct msym_bunch *next;

  936.   while (msym_bunch != NULL)
  937.     {
  938.       next = msym_bunch->next;
  939.       xfree (msym_bunch);
  940.       msym_bunch = next;
  941.     }
  942. }

  943. /* See minsyms.h.  */

  944. struct cleanup *
  945. make_cleanup_discard_minimal_symbols (void)
  946. {
  947.   return make_cleanup (do_discard_minimal_symbols_cleanup, 0);
  948. }



  949. /* Compact duplicate entries out of a minimal symbol table by walking
  950.    through the table and compacting out entries with duplicate addresses
  951.    and matching names.  Return the number of entries remaining.

  952.    On entry, the table resides between msymbol[0] and msymbol[mcount].
  953.    On exit, it resides between msymbol[0] and msymbol[result_count].

  954.    When files contain multiple sources of symbol information, it is
  955.    possible for the minimal symbol table to contain many duplicate entries.
  956.    As an example, SVR4 systems use ELF formatted object files, which
  957.    usually contain at least two different types of symbol tables (a
  958.    standard ELF one and a smaller dynamic linking table), as well as
  959.    DWARF debugging information for files compiled with -g.

  960.    Without compacting, the minimal symbol table for gdb itself contains
  961.    over a 1000 duplicates, about a third of the total table size.  Aside
  962.    from the potential trap of not noticing that two successive entries
  963.    identify the same location, this duplication impacts the time required
  964.    to linearly scan the table, which is done in a number of places.  So we
  965.    just do one linear scan here and toss out the duplicates.

  966.    Note that we are not concerned here about recovering the space that
  967.    is potentially freed up, because the strings themselves are allocated
  968.    on the storage_obstack, and will get automatically freed when the symbol
  969.    table is freed.  The caller can free up the unused minimal symbols at
  970.    the end of the compacted region if their allocation strategy allows it.

  971.    Also note we only go up to the next to last entry within the loop
  972.    and then copy the last entry explicitly after the loop terminates.

  973.    Since the different sources of information for each symbol may
  974.    have different levels of "completeness", we may have duplicates
  975.    that have one entry with type "mst_unknown" and the other with a
  976.    known type.  So if the one we are leaving alone has type mst_unknown,
  977.    overwrite its type with the type from the one we are compacting out.  */

  978. static int
  979. compact_minimal_symbols (struct minimal_symbol *msymbol, int mcount,
  980.                          struct objfile *objfile)
  981. {
  982.   struct minimal_symbol *copyfrom;
  983.   struct minimal_symbol *copyto;

  984.   if (mcount > 0)
  985.     {
  986.       copyfrom = copyto = msymbol;
  987.       while (copyfrom < msymbol + mcount - 1)
  988.         {
  989.           if (MSYMBOL_VALUE_RAW_ADDRESS (copyfrom)
  990.               == MSYMBOL_VALUE_RAW_ADDRESS ((copyfrom + 1))
  991.               && MSYMBOL_SECTION (copyfrom) == MSYMBOL_SECTION (copyfrom + 1)
  992.               && strcmp (MSYMBOL_LINKAGE_NAME (copyfrom),
  993.                          MSYMBOL_LINKAGE_NAME ((copyfrom + 1))) == 0)
  994.             {
  995.               if (MSYMBOL_TYPE ((copyfrom + 1)) == mst_unknown)
  996.                 {
  997.                   MSYMBOL_TYPE ((copyfrom + 1)) = MSYMBOL_TYPE (copyfrom);
  998.                 }
  999.               copyfrom++;
  1000.             }
  1001.           else
  1002.             *copyto++ = *copyfrom++;
  1003.         }
  1004.       *copyto++ = *copyfrom++;
  1005.       mcount = copyto - msymbol;
  1006.     }
  1007.   return (mcount);
  1008. }

  1009. /* Build (or rebuild) the minimal symbol hash tables.  This is necessary
  1010.    after compacting or sorting the table since the entries move around
  1011.    thus causing the internal minimal_symbol pointers to become jumbled.  */

  1012. static void
  1013. build_minimal_symbol_hash_tables (struct objfile *objfile)
  1014. {
  1015.   int i;
  1016.   struct minimal_symbol *msym;

  1017.   /* Clear the hash tables.  */
  1018.   for (i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++)
  1019.     {
  1020.       objfile->per_bfd->msymbol_hash[i] = 0;
  1021.       objfile->per_bfd->msymbol_demangled_hash[i] = 0;
  1022.     }

  1023.   /* Now, (re)insert the actual entries.  */
  1024.   for ((i = objfile->per_bfd->minimal_symbol_count,
  1025.         msym = objfile->per_bfd->msymbols);
  1026.        i > 0;
  1027.        i--, msym++)
  1028.     {
  1029.       msym->hash_next = 0;
  1030.       add_minsym_to_hash_table (msym, objfile->per_bfd->msymbol_hash);

  1031.       msym->demangled_hash_next = 0;
  1032.       if (MSYMBOL_SEARCH_NAME (msym) != MSYMBOL_LINKAGE_NAME (msym))
  1033.         add_minsym_to_demangled_hash_table (msym,
  1034.                                             objfile->per_bfd->msymbol_demangled_hash);
  1035.     }
  1036. }

  1037. /* Add the minimal symbols in the existing bunches to the objfile's official
  1038.    minimal symbol table.  In most cases there is no minimal symbol table yet
  1039.    for this objfile, and the existing bunches are used to create one.  Once
  1040.    in a while (for shared libraries for example), we add symbols (e.g. common
  1041.    symbols) to an existing objfile.

  1042.    Because of the way minimal symbols are collected, we generally have no way
  1043.    of knowing what source language applies to any particular minimal symbol.
  1044.    Specifically, we have no way of knowing if the minimal symbol comes from a
  1045.    C++ compilation unit or not.  So for the sake of supporting cached
  1046.    demangled C++ names, we have no choice but to try and demangle each new one
  1047.    that comes in.  If the demangling succeeds, then we assume it is a C++
  1048.    symbol and set the symbol's language and demangled name fields
  1049.    appropriately.  Note that in order to avoid unnecessary demanglings, and
  1050.    allocating obstack space that subsequently can't be freed for the demangled
  1051.    names, we mark all newly added symbols with language_auto.  After
  1052.    compaction of the minimal symbols, we go back and scan the entire minimal
  1053.    symbol table looking for these new symbols.  For each new symbol we attempt
  1054.    to demangle it, and if successful, record it as a language_cplus symbol
  1055.    and cache the demangled form on the symbol obstack.  Symbols which don't
  1056.    demangle are marked as language_unknown symbols, which inhibits future
  1057.    attempts to demangle them if we later add more minimal symbols.  */

  1058. void
  1059. install_minimal_symbols (struct objfile *objfile)
  1060. {
  1061.   int bindex;
  1062.   int mcount;
  1063.   struct msym_bunch *bunch;
  1064.   struct minimal_symbol *msymbols;
  1065.   int alloc_count;

  1066.   if (objfile->per_bfd->minsyms_read)
  1067.     return;

  1068.   if (msym_count > 0)
  1069.     {
  1070.       if (symtab_create_debug)
  1071.         {
  1072.           fprintf_unfiltered (gdb_stdlog,
  1073.                               "Installing %d minimal symbols of objfile %s.\n",
  1074.                               msym_count, objfile_name (objfile));
  1075.         }

  1076.       /* Allocate enough space in the obstack, into which we will gather the
  1077.          bunches of new and existing minimal symbols, sort them, and then
  1078.          compact out the duplicate entries.  Once we have a final table,
  1079.          we will give back the excess space.  */

  1080.       alloc_count = msym_count + objfile->per_bfd->minimal_symbol_count + 1;
  1081.       obstack_blank (&objfile->per_bfd->storage_obstack,
  1082.                      alloc_count * sizeof (struct minimal_symbol));
  1083.       msymbols = (struct minimal_symbol *)
  1084.         obstack_base (&objfile->per_bfd->storage_obstack);

  1085.       /* Copy in the existing minimal symbols, if there are any.  */

  1086.       if (objfile->per_bfd->minimal_symbol_count)
  1087.         memcpy ((char *) msymbols, (char *) objfile->per_bfd->msymbols,
  1088.             objfile->per_bfd->minimal_symbol_count * sizeof (struct minimal_symbol));

  1089.       /* Walk through the list of minimal symbol bunches, adding each symbol
  1090.          to the new contiguous array of symbols.  Note that we start with the
  1091.          current, possibly partially filled bunch (thus we use the current
  1092.          msym_bunch_index for the first bunch we copy over), and thereafter
  1093.          each bunch is full.  */

  1094.       mcount = objfile->per_bfd->minimal_symbol_count;

  1095.       for (bunch = msym_bunch; bunch != NULL; bunch = bunch->next)
  1096.         {
  1097.           for (bindex = 0; bindex < msym_bunch_index; bindex++, mcount++)
  1098.             msymbols[mcount] = bunch->contents[bindex];
  1099.           msym_bunch_index = BUNCH_SIZE;
  1100.         }

  1101.       /* Sort the minimal symbols by address.  */

  1102.       qsort (msymbols, mcount, sizeof (struct minimal_symbol),
  1103.              compare_minimal_symbols);

  1104.       /* Compact out any duplicates, and free up whatever space we are
  1105.          no longer using.  */

  1106.       mcount = compact_minimal_symbols (msymbols, mcount, objfile);

  1107.       obstack_blank_fast (&objfile->per_bfd->storage_obstack,
  1108.                (mcount + 1 - alloc_count) * sizeof (struct minimal_symbol));
  1109.       msymbols = (struct minimal_symbol *)
  1110.         obstack_finish (&objfile->per_bfd->storage_obstack);

  1111.       /* We also terminate the minimal symbol table with a "null symbol",
  1112.          which is *not* included in the size of the table.  This makes it
  1113.          easier to find the end of the table when we are handed a pointer
  1114.          to some symbol in the middle of it.  Zero out the fields in the
  1115.          "null symbol" allocated at the end of the array.  Note that the
  1116.          symbol count does *not* include this null symbol, which is why it
  1117.          is indexed by mcount and not mcount-1.  */

  1118.       memset (&msymbols[mcount], 0, sizeof (struct minimal_symbol));

  1119.       /* Attach the minimal symbol table to the specified objfile.
  1120.          The strings themselves are also located in the storage_obstack
  1121.          of this objfile.  */

  1122.       objfile->per_bfd->minimal_symbol_count = mcount;
  1123.       objfile->per_bfd->msymbols = msymbols;

  1124.       /* Now build the hash tables; we can't do this incrementally
  1125.          at an earlier point since we weren't finished with the obstack
  1126.          yet.  (And if the msymbol obstack gets moved, all the internal
  1127.          pointers to other msymbols need to be adjusted.)  */
  1128.       build_minimal_symbol_hash_tables (objfile);
  1129.     }
  1130. }

  1131. /* See minsyms.h.  */

  1132. void
  1133. terminate_minimal_symbol_table (struct objfile *objfile)
  1134. {
  1135.   if (! objfile->per_bfd->msymbols)
  1136.     objfile->per_bfd->msymbols
  1137.       = ((struct minimal_symbol *)
  1138.          obstack_alloc (&objfile->per_bfd->storage_obstack,
  1139.                         sizeof (struct minimal_symbol)));

  1140.   {
  1141.     struct minimal_symbol *m
  1142.       = &objfile->per_bfd->msymbols[objfile->per_bfd->minimal_symbol_count];

  1143.     memset (m, 0, sizeof (*m));
  1144.     /* Don't rely on these enumeration values being 0's.  */
  1145.     MSYMBOL_TYPE (m) = mst_unknown;
  1146.     MSYMBOL_SET_LANGUAGE (m, language_unknown,
  1147.                           &objfile->per_bfd->storage_obstack);
  1148.   }
  1149. }

  1150. /* Check if PC is in a shared library trampoline code stub.
  1151.    Return minimal symbol for the trampoline entry or NULL if PC is not
  1152.    in a trampoline code stub.  */

  1153. static struct minimal_symbol *
  1154. lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
  1155. {
  1156.   struct obj_section *section = find_pc_section (pc);
  1157.   struct bound_minimal_symbol msymbol;

  1158.   if (section == NULL)
  1159.     return NULL;
  1160.   msymbol = lookup_minimal_symbol_by_pc_section_1 (pc, section, 1);

  1161.   if (msymbol.minsym != NULL
  1162.       && MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
  1163.     return msymbol.minsym;
  1164.   return NULL;
  1165. }

  1166. /* If PC is in a shared library trampoline code stub, return the
  1167.    address of the `real' function belonging to the stub.
  1168.    Return 0 if PC is not in a trampoline code stub or if the real
  1169.    function is not found in the minimal symbol table.

  1170.    We may fail to find the right function if a function with the
  1171.    same name is defined in more than one shared library, but this
  1172.    is considered bad programming style.  We could return 0 if we find
  1173.    a duplicate function in case this matters someday.  */

  1174. CORE_ADDR
  1175. find_solib_trampoline_target (struct frame_info *frame, CORE_ADDR pc)
  1176. {
  1177.   struct objfile *objfile;
  1178.   struct minimal_symbol *msymbol;
  1179.   struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);

  1180.   if (tsymbol != NULL)
  1181.     {
  1182.       ALL_MSYMBOLS (objfile, msymbol)
  1183.       {
  1184.         if ((MSYMBOL_TYPE (msymbol) == mst_text
  1185.             || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc)
  1186.             && strcmp (MSYMBOL_LINKAGE_NAME (msymbol),
  1187.                        MSYMBOL_LINKAGE_NAME (tsymbol)) == 0)
  1188.           return MSYMBOL_VALUE_ADDRESS (objfile, msymbol);

  1189.         /* Also handle minimal symbols pointing to function descriptors.  */
  1190.         if (MSYMBOL_TYPE (msymbol) == mst_data
  1191.             && strcmp (MSYMBOL_LINKAGE_NAME (msymbol),
  1192.                        MSYMBOL_LINKAGE_NAME (tsymbol)) == 0)
  1193.           {
  1194.             CORE_ADDR func;

  1195.             func = gdbarch_convert_from_func_ptr_addr
  1196.                     (get_objfile_arch (objfile),
  1197.                      MSYMBOL_VALUE_ADDRESS (objfile, msymbol),
  1198.                      &current_target);

  1199.             /* Ignore data symbols that are not function descriptors.  */
  1200.             if (func != MSYMBOL_VALUE_ADDRESS (objfile, msymbol))
  1201.               return func;
  1202.           }
  1203.       }
  1204.     }
  1205.   return 0;
  1206. }

  1207. /* See minsyms.h.  */

  1208. CORE_ADDR
  1209. minimal_symbol_upper_bound (struct bound_minimal_symbol minsym)
  1210. {
  1211.   int i;
  1212.   short section;
  1213.   struct obj_section *obj_section;
  1214.   CORE_ADDR result;
  1215.   struct minimal_symbol *msymbol;

  1216.   gdb_assert (minsym.minsym != NULL);

  1217.   /* If the minimal symbol has a size, use it.  Otherwise use the
  1218.      lesser of the next minimal symbol in the same section, or the end
  1219.      of the section, as the end of the function.  */

  1220.   if (MSYMBOL_SIZE (minsym.minsym) != 0)
  1221.     return BMSYMBOL_VALUE_ADDRESS (minsym) + MSYMBOL_SIZE (minsym.minsym);

  1222.   /* Step over other symbols at this same address, and symbols in
  1223.      other sections, to find the next symbol in this section with a
  1224.      different address.  */

  1225.   msymbol = minsym.minsym;
  1226.   section = MSYMBOL_SECTION (msymbol);
  1227.   for (i = 1; MSYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
  1228.     {
  1229.       if ((MSYMBOL_VALUE_RAW_ADDRESS (msymbol + i)
  1230.            != MSYMBOL_VALUE_RAW_ADDRESS (msymbol))
  1231.           && MSYMBOL_SECTION (msymbol + i) == section)
  1232.         break;
  1233.     }

  1234.   obj_section = MSYMBOL_OBJ_SECTION (minsym.objfile, minsym.minsym);
  1235.   if (MSYMBOL_LINKAGE_NAME (msymbol + i) != NULL
  1236.       && (MSYMBOL_VALUE_ADDRESS (minsym.objfile, msymbol + i)
  1237.           < obj_section_endaddr (obj_section)))
  1238.     result = MSYMBOL_VALUE_ADDRESS (minsym.objfile, msymbol + i);
  1239.   else
  1240.     /* We got the start address from the last msymbol in the objfile.
  1241.        So the end address is the end of the section.  */
  1242.     result = obj_section_endaddr (obj_section);

  1243.   return result;
  1244. }