gdb/elfread.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Read ELF (Executable and Linking Format) object files for GDB.

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

  3.    Written by Fred Fish at Cygnus Support.

  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. #include "defs.h"
  16. #include "bfd.h"
  17. #include "elf-bfd.h"
  18. #include "elf/common.h"
  19. #include "elf/internal.h"
  20. #include "elf/mips.h"
  21. #include "symtab.h"
  22. #include "symfile.h"
  23. #include "objfiles.h"
  24. #include "buildsym.h"
  25. #include "stabsread.h"
  26. #include "gdb-stabs.h"
  27. #include "complaints.h"
  28. #include "demangle.h"
  29. #include "psympriv.h"
  30. #include "filenames.h"
  31. #include "probe.h"
  32. #include "arch-utils.h"
  33. #include "gdbtypes.h"
  34. #include "value.h"
  35. #include "infcall.h"
  36. #include "gdbthread.h"
  37. #include "regcache.h"
  38. #include "bcache.h"
  39. #include "gdb_bfd.h"
  40. #include "build-id.h"

  41. extern void _initialize_elfread (void);

  42. /* Forward declarations.  */
  43. static const struct sym_fns elf_sym_fns_gdb_index;
  44. static const struct sym_fns elf_sym_fns_lazy_psyms;

  45. /* The struct elfinfo is available only during ELF symbol table and
  46.    psymtab reading.  It is destroyed at the completion of psymtab-reading.
  47.    It's local to elf_symfile_read.  */

  48. struct elfinfo
  49.   {
  50.     asection *stabsect;                /* Section pointer for .stab section */
  51.     asection *mdebugsect;        /* Section pointer for .mdebug section */
  52.   };

  53. /* Per-BFD data for probe info.  */

  54. static const struct bfd_data *probe_key = NULL;

  55. static void free_elfinfo (void *);

  56. /* Minimal symbols located at the GOT entries for .plt - that is the real
  57.    pointer where the given entry will jump to.  It gets updated by the real
  58.    function address during lazy ld.so resolving in the inferior.  These
  59.    minimal symbols are indexed for <tab>-completion.  */

  60. #define SYMBOL_GOT_PLT_SUFFIX "@got.plt"

  61. /* Locate the segments in ABFD.  */

  62. static struct symfile_segment_data *
  63. elf_symfile_segments (bfd *abfd)
  64. {
  65.   Elf_Internal_Phdr *phdrs, **segments;
  66.   long phdrs_size;
  67.   int num_phdrs, num_segments, num_sections, i;
  68.   asection *sect;
  69.   struct symfile_segment_data *data;

  70.   phdrs_size = bfd_get_elf_phdr_upper_bound (abfd);
  71.   if (phdrs_size == -1)
  72.     return NULL;

  73.   phdrs = alloca (phdrs_size);
  74.   num_phdrs = bfd_get_elf_phdrs (abfd, phdrs);
  75.   if (num_phdrs == -1)
  76.     return NULL;

  77.   num_segments = 0;
  78.   segments = alloca (sizeof (Elf_Internal_Phdr *) * num_phdrs);
  79.   for (i = 0; i < num_phdrs; i++)
  80.     if (phdrs[i].p_type == PT_LOAD)
  81.       segments[num_segments++] = &phdrs[i];

  82.   if (num_segments == 0)
  83.     return NULL;

  84.   data = XCNEW (struct symfile_segment_data);
  85.   data->num_segments = num_segments;
  86.   data->segment_bases = XCNEWVEC (CORE_ADDR, num_segments);
  87.   data->segment_sizes = XCNEWVEC (CORE_ADDR, num_segments);

  88.   for (i = 0; i < num_segments; i++)
  89.     {
  90.       data->segment_bases[i] = segments[i]->p_vaddr;
  91.       data->segment_sizes[i] = segments[i]->p_memsz;
  92.     }

  93.   num_sections = bfd_count_sections (abfd);
  94.   data->segment_info = XCNEWVEC (int, num_sections);

  95.   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
  96.     {
  97.       int j;
  98.       CORE_ADDR vma;

  99.       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
  100.         continue;

  101.       vma = bfd_get_section_vma (abfd, sect);

  102.       for (j = 0; j < num_segments; j++)
  103.         if (segments[j]->p_memsz > 0
  104.             && vma >= segments[j]->p_vaddr
  105.             && (vma - segments[j]->p_vaddr) < segments[j]->p_memsz)
  106.           {
  107.             data->segment_info[i] = j + 1;
  108.             break;
  109.           }

  110.       /* We should have found a segment for every non-empty section.
  111.          If we haven't, we will not relocate this section by any
  112.          offsets we apply to the segments.  As an exception, do not
  113.          warn about SHT_NOBITS sections; in normal ELF execution
  114.          environments, SHT_NOBITS means zero-initialized and belongs
  115.          in a segment, but in no-OS environments some tools (e.g. ARM
  116.          RealView) use SHT_NOBITS for uninitialized data.  Since it is
  117.          uninitialized, it doesn't need a program header.  Such
  118.          binaries are not relocatable.  */
  119.       if (bfd_get_section_size (sect) > 0 && j == num_segments
  120.           && (bfd_get_section_flags (abfd, sect) & SEC_LOAD) != 0)
  121.         warning (_("Loadable section \"%s\" outside of ELF segments"),
  122.                  bfd_section_name (abfd, sect));
  123.     }

  124.   return data;
  125. }

  126. /* We are called once per section from elf_symfile_read.  We
  127.    need to examine each section we are passed, check to see
  128.    if it is something we are interested in processing, and
  129.    if so, stash away some access information for the section.

  130.    For now we recognize the dwarf debug information sections and
  131.    line number sections from matching their section names.  The
  132.    ELF definition is no real help here since it has no direct
  133.    knowledge of DWARF (by design, so any debugging format can be
  134.    used).

  135.    We also recognize the ".stab" sections used by the Sun compilers
  136.    released with Solaris 2.

  137.    FIXME: The section names should not be hardwired strings (what
  138.    should they be?  I don't think most object file formats have enough
  139.    section flags to specify what kind of debug section it is.
  140.    -kingdon).  */

  141. static void
  142. elf_locate_sections (bfd *ignore_abfd, asection *sectp, void *eip)
  143. {
  144.   struct elfinfo *ei;

  145.   ei = (struct elfinfo *) eip;
  146.   if (strcmp (sectp->name, ".stab") == 0)
  147.     {
  148.       ei->stabsect = sectp;
  149.     }
  150.   else if (strcmp (sectp->name, ".mdebug") == 0)
  151.     {
  152.       ei->mdebugsect = sectp;
  153.     }
  154. }

  155. static struct minimal_symbol *
  156. record_minimal_symbol (const char *name, int name_len, int copy_name,
  157.                        CORE_ADDR address,
  158.                        enum minimal_symbol_type ms_type,
  159.                        asection *bfd_section, struct objfile *objfile)
  160. {
  161.   struct gdbarch *gdbarch = get_objfile_arch (objfile);

  162.   if (ms_type == mst_text || ms_type == mst_file_text
  163.       || ms_type == mst_text_gnu_ifunc)
  164.     address = gdbarch_addr_bits_remove (gdbarch, address);

  165.   return prim_record_minimal_symbol_full (name, name_len, copy_name, address,
  166.                                           ms_type,
  167.                                           gdb_bfd_section_index (objfile->obfd,
  168.                                                                  bfd_section),
  169.                                           objfile);
  170. }

  171. /* Read the symbol table of an ELF file.

  172.    Given an objfile, a symbol table, and a flag indicating whether the
  173.    symbol table contains regular, dynamic, or synthetic symbols, add all
  174.    the global function and data symbols to the minimal symbol table.

  175.    In stabs-in-ELF, as implemented by Sun, there are some local symbols
  176.    defined in the ELF symbol table, which can be used to locate
  177.    the beginnings of sections from each ".o" file that was linked to
  178.    form the executable objfile.  We gather any such info and record it
  179.    in data structures hung off the objfile's private data.  */

  180. #define ST_REGULAR 0
  181. #define ST_DYNAMIC 1
  182. #define ST_SYNTHETIC 2

  183. static void
  184. elf_symtab_read (struct objfile *objfile, int type,
  185.                  long number_of_symbols, asymbol **symbol_table,
  186.                  int copy_names)
  187. {
  188.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  189.   asymbol *sym;
  190.   long i;
  191.   CORE_ADDR symaddr;
  192.   CORE_ADDR offset;
  193.   enum minimal_symbol_type ms_type;
  194.   /* If sectinfo is nonNULL, it contains section info that should end up
  195.      filed in the objfile.  */
  196.   struct stab_section_info *sectinfo = NULL;
  197.   /* If filesym is nonzero, it points to a file symbol, but we haven't
  198.      seen any section info for it yet.  */
  199.   asymbol *filesym = 0;
  200.   /* Name of filesym.  This is either a constant string or is saved on
  201.      the objfile's filename cache.  */
  202.   const char *filesymname = "";
  203.   struct dbx_symfile_info *dbx = DBX_SYMFILE_INFO (objfile);
  204.   int stripped = (bfd_get_symcount (objfile->obfd) == 0);
  205.   int elf_make_msymbol_special_p
  206.     = gdbarch_elf_make_msymbol_special_p (gdbarch);

  207.   for (i = 0; i < number_of_symbols; i++)
  208.     {
  209.       sym = symbol_table[i];
  210.       if (sym->name == NULL || *sym->name == '\0')
  211.         {
  212.           /* Skip names that don't exist (shouldn't happen), or names
  213.              that are null strings (may happen).  */
  214.           continue;
  215.         }

  216.       /* Skip "special" symbols, e.g. ARM mapping symbols.  These are
  217.          symbols which do not correspond to objects in the symbol table,
  218.          but have some other target-specific meaning.  */
  219.       if (bfd_is_target_special_symbol (objfile->obfd, sym))
  220.         {
  221.           if (gdbarch_record_special_symbol_p (gdbarch))
  222.             gdbarch_record_special_symbol (gdbarch, objfile, sym);
  223.           continue;
  224.         }

  225.       offset = ANOFFSET (objfile->section_offsets,
  226.                          gdb_bfd_section_index (objfile->obfd, sym->section));
  227.       if (type == ST_DYNAMIC
  228.           && sym->section == bfd_und_section_ptr
  229.           && (sym->flags & BSF_FUNCTION))
  230.         {
  231.           struct minimal_symbol *msym;
  232.           bfd *abfd = objfile->obfd;
  233.           asection *sect;

  234.           /* Symbol is a reference to a function defined in
  235.              a shared library.
  236.              If its value is non zero then it is usually the address
  237.              of the corresponding entry in the procedure linkage table,
  238.              plus the desired section offset.
  239.              If its value is zero then the dynamic linker has to resolve
  240.              the symbol.  We are unable to find any meaningful address
  241.              for this symbol in the executable file, so we skip it.  */
  242.           symaddr = sym->value;
  243.           if (symaddr == 0)
  244.             continue;

  245.           /* sym->section is the undefined section.  However, we want to
  246.              record the section where the PLT stub resides with the
  247.              minimal symbol.  Search the section table for the one that
  248.              covers the stub's address.  */
  249.           for (sect = abfd->sections; sect != NULL; sect = sect->next)
  250.             {
  251.               if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
  252.                 continue;

  253.               if (symaddr >= bfd_get_section_vma (abfd, sect)
  254.                   && symaddr < bfd_get_section_vma (abfd, sect)
  255.                                + bfd_get_section_size (sect))
  256.                 break;
  257.             }
  258.           if (!sect)
  259.             continue;

  260.           /* On ia64-hpux, we have discovered that the system linker
  261.              adds undefined symbols with nonzero addresses that cannot
  262.              be right (their address points inside the code of another
  263.              function in the .text section).  This creates problems
  264.              when trying to determine which symbol corresponds to
  265.              a given address.

  266.              We try to detect those buggy symbols by checking which
  267.              section we think they correspond to.  Normally, PLT symbols
  268.              are stored inside their own section, and the typical name
  269.              for that section is ".plt".  So, if there is a ".plt"
  270.              section, and yet the section name of our symbol does not
  271.              start with ".plt", we ignore that symbol.  */
  272.           if (strncmp (sect->name, ".plt", 4) != 0
  273.               && bfd_get_section_by_name (abfd, ".plt") != NULL)
  274.             continue;

  275.           msym = record_minimal_symbol
  276.             (sym->name, strlen (sym->name), copy_names,
  277.              symaddr, mst_solib_trampoline, sect, objfile);
  278.           if (msym != NULL)
  279.             {
  280.               msym->filename = filesymname;
  281.               if (elf_make_msymbol_special_p)
  282.                 gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
  283.             }
  284.           continue;
  285.         }

  286.       /* If it is a nonstripped executable, do not enter dynamic
  287.          symbols, as the dynamic symbol table is usually a subset
  288.          of the main symbol table.  */
  289.       if (type == ST_DYNAMIC && !stripped)
  290.         continue;
  291.       if (sym->flags & BSF_FILE)
  292.         {
  293.           /* STT_FILE debugging symbol that helps stabs-in-elf debugging.
  294.              Chain any old one onto the objfile; remember new sym.  */
  295.           if (sectinfo != NULL)
  296.             {
  297.               sectinfo->next = dbx->stab_section_info;
  298.               dbx->stab_section_info = sectinfo;
  299.               sectinfo = NULL;
  300.             }
  301.           filesym = sym;
  302.           filesymname = bcache (filesym->name, strlen (filesym->name) + 1,
  303.                                 objfile->per_bfd->filename_cache);
  304.         }
  305.       else if (sym->flags & BSF_SECTION_SYM)
  306.         continue;
  307.       else if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK
  308.                              | BSF_GNU_UNIQUE))
  309.         {
  310.           struct minimal_symbol *msym;

  311.           /* Select global/local/weak symbols.  Note that bfd puts abs
  312.              symbols in their own section, so all symbols we are
  313.              interested in will have a section.  */
  314.           /* Bfd symbols are section relative.  */
  315.           symaddr = sym->value + sym->section->vma;
  316.           /* For non-absolute symbols, use the type of the section
  317.              they are relative to, to intuit text/data.  Bfd provides
  318.              no way of figuring this out for absolute symbols.  */
  319.           if (sym->section == bfd_abs_section_ptr)
  320.             {
  321.               /* This is a hack to get the minimal symbol type
  322.                  right for Irix 5, which has absolute addresses
  323.                  with special section indices for dynamic symbols.

  324.                  NOTE: uweigand-20071112: Synthetic symbols do not
  325.                  have an ELF-private part, so do not touch those.  */
  326.               unsigned int shndx = type == ST_SYNTHETIC ? 0 :
  327.                 ((elf_symbol_type *) sym)->internal_elf_sym.st_shndx;

  328.               switch (shndx)
  329.                 {
  330.                 case SHN_MIPS_TEXT:
  331.                   ms_type = mst_text;
  332.                   break;
  333.                 case SHN_MIPS_DATA:
  334.                   ms_type = mst_data;
  335.                   break;
  336.                 case SHN_MIPS_ACOMMON:
  337.                   ms_type = mst_bss;
  338.                   break;
  339.                 default:
  340.                   ms_type = mst_abs;
  341.                 }

  342.               /* If it is an Irix dynamic symbol, skip section name
  343.                  symbols, relocate all others by section offset.  */
  344.               if (ms_type != mst_abs)
  345.                 {
  346.                   if (sym->name[0] == '.')
  347.                     continue;
  348.                 }
  349.             }
  350.           else if (sym->section->flags & SEC_CODE)
  351.             {
  352.               if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
  353.                 {
  354.                   if (sym->flags & BSF_GNU_INDIRECT_FUNCTION)
  355.                     ms_type = mst_text_gnu_ifunc;
  356.                   else
  357.                     ms_type = mst_text;
  358.                 }
  359.               /* The BSF_SYNTHETIC check is there to omit ppc64 function
  360.                  descriptors mistaken for static functions starting with 'L'.
  361.                  */
  362.               else if ((sym->name[0] == '.' && sym->name[1] == 'L'
  363.                         && (sym->flags & BSF_SYNTHETIC) == 0)
  364.                        || ((sym->flags & BSF_LOCAL)
  365.                            && sym->name[0] == '$'
  366.                            && sym->name[1] == 'L'))
  367.                 /* Looks like a compiler-generated label.  Skip
  368.                    it.  The assembler should be skipping these (to
  369.                    keep executables small), but apparently with
  370.                    gcc on the (deleted) delta m88k SVR4, it loses.
  371.                    So to have us check too should be harmless (but
  372.                    I encourage people to fix this in the assembler
  373.                    instead of adding checks here).  */
  374.                 continue;
  375.               else
  376.                 {
  377.                   ms_type = mst_file_text;
  378.                 }
  379.             }
  380.           else if (sym->section->flags & SEC_ALLOC)
  381.             {
  382.               if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
  383.                 {
  384.                   if (sym->section->flags & SEC_LOAD)
  385.                     {
  386.                       ms_type = mst_data;
  387.                     }
  388.                   else
  389.                     {
  390.                       ms_type = mst_bss;
  391.                     }
  392.                 }
  393.               else if (sym->flags & BSF_LOCAL)
  394.                 {
  395.                   /* Named Local variable in a Data section.
  396.                      Check its name for stabs-in-elf.  */
  397.                   int special_local_sect;

  398.                   if (strcmp ("Bbss.bss", sym->name) == 0)
  399.                     special_local_sect = SECT_OFF_BSS (objfile);
  400.                   else if (strcmp ("Ddata.data", sym->name) == 0)
  401.                     special_local_sect = SECT_OFF_DATA (objfile);
  402.                   else if (strcmp ("Drodata.rodata", sym->name) == 0)
  403.                     special_local_sect = SECT_OFF_RODATA (objfile);
  404.                   else
  405.                     special_local_sect = -1;
  406.                   if (special_local_sect >= 0)
  407.                     {
  408.                       /* Found a special local symbol.  Allocate a
  409.                          sectinfo, if needed, and fill it in.  */
  410.                       if (sectinfo == NULL)
  411.                         {
  412.                           int max_index;
  413.                           size_t size;

  414.                           max_index = SECT_OFF_BSS (objfile);
  415.                           if (objfile->sect_index_data > max_index)
  416.                             max_index = objfile->sect_index_data;
  417.                           if (objfile->sect_index_rodata > max_index)
  418.                             max_index = objfile->sect_index_rodata;

  419.                           /* max_index is the largest index we'll
  420.                              use into this array, so we must
  421.                              allocate max_index+1 elements for it.
  422.                              However, 'struct stab_section_info'
  423.                              already includes one element, so we
  424.                              need to allocate max_index aadditional
  425.                              elements.  */
  426.                           size = (sizeof (struct stab_section_info)
  427.                                   + (sizeof (CORE_ADDR) * max_index));
  428.                           sectinfo = (struct stab_section_info *)
  429.                             xmalloc (size);
  430.                           memset (sectinfo, 0, size);
  431.                           sectinfo->num_sections = max_index;
  432.                           if (filesym == NULL)
  433.                             {
  434.                               complaint (&symfile_complaints,
  435.                                          _("elf/stab section information %s "
  436.                                            "without a preceding file symbol"),
  437.                                          sym->name);
  438.                             }
  439.                           else
  440.                             {
  441.                               sectinfo->filename =
  442.                                 (char *) filesym->name;
  443.                             }
  444.                         }
  445.                       if (sectinfo->sections[special_local_sect] != 0)
  446.                         complaint (&symfile_complaints,
  447.                                    _("duplicated elf/stab section "
  448.                                      "information for %s"),
  449.                                    sectinfo->filename);
  450.                       /* BFD symbols are section relative.  */
  451.                       symaddr = sym->value + sym->section->vma;
  452.                       /* Relocate non-absolute symbols by the
  453.                          section offset.  */
  454.                       if (sym->section != bfd_abs_section_ptr)
  455.                         symaddr += offset;
  456.                       sectinfo->sections[special_local_sect] = symaddr;
  457.                       /* The special local symbols don't go in the
  458.                          minimal symbol table, so ignore this one.  */
  459.                       continue;
  460.                     }
  461.                   /* Not a special stabs-in-elf symbol, do regular
  462.                      symbol processing.  */
  463.                   if (sym->section->flags & SEC_LOAD)
  464.                     {
  465.                       ms_type = mst_file_data;
  466.                     }
  467.                   else
  468.                     {
  469.                       ms_type = mst_file_bss;
  470.                     }
  471.                 }
  472.               else
  473.                 {
  474.                   ms_type = mst_unknown;
  475.                 }
  476.             }
  477.           else
  478.             {
  479.               /* FIXME:  Solaris2 shared libraries include lots of
  480.                  odd "absolute" and "undefined" symbols, that play
  481.                  hob with actions like finding what function the PC
  482.                  is in.  Ignore them if they aren't text, data, or bss.  */
  483.               /* ms_type = mst_unknown; */
  484.               continue;        /* Skip this symbol.  */
  485.             }
  486.           msym = record_minimal_symbol
  487.             (sym->name, strlen (sym->name), copy_names, symaddr,
  488.              ms_type, sym->section, objfile);

  489.           if (msym)
  490.             {
  491.               /* NOTE: uweigand-20071112: A synthetic symbol does not have an
  492.                  ELF-private part.  */
  493.               if (type != ST_SYNTHETIC)
  494.                 {
  495.                   /* Pass symbol size field in via BFD.  FIXME!!!  */
  496.                   elf_symbol_type *elf_sym = (elf_symbol_type *) sym;
  497.                   SET_MSYMBOL_SIZE (msym, elf_sym->internal_elf_sym.st_size);
  498.                 }

  499.               msym->filename = filesymname;
  500.               if (elf_make_msymbol_special_p)
  501.                 gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
  502.             }

  503.           /* If we see a default versioned symbol, install it under
  504.              its version-less name.  */
  505.           if (msym != NULL)
  506.             {
  507.               const char *atsign = strchr (sym->name, '@');

  508.               if (atsign != NULL && atsign[1] == '@' && atsign > sym->name)
  509.                 {
  510.                   int len = atsign - sym->name;

  511.                   record_minimal_symbol (sym->name, len, 1, symaddr,
  512.                                          ms_type, sym->section, objfile);
  513.                 }
  514.             }

  515.           /* For @plt symbols, also record a trampoline to the
  516.              destination symbol.  The @plt symbol will be used in
  517.              disassembly, and the trampoline will be used when we are
  518.              trying to find the target.  */
  519.           if (msym && ms_type == mst_text && type == ST_SYNTHETIC)
  520.             {
  521.               int len = strlen (sym->name);

  522.               if (len > 4 && strcmp (sym->name + len - 4, "@plt") == 0)
  523.                 {
  524.                   struct minimal_symbol *mtramp;

  525.                   mtramp = record_minimal_symbol (sym->name, len - 4, 1,
  526.                                                   symaddr,
  527.                                                   mst_solib_trampoline,
  528.                                                   sym->section, objfile);
  529.                   if (mtramp)
  530.                     {
  531.                       SET_MSYMBOL_SIZE (mtramp, MSYMBOL_SIZE (msym));
  532.                       mtramp->created_by_gdb = 1;
  533.                       mtramp->filename = filesymname;
  534.                       if (elf_make_msymbol_special_p)
  535.                         gdbarch_elf_make_msymbol_special (gdbarch,
  536.                                                           sym, mtramp);
  537.                     }
  538.                 }
  539.             }
  540.         }
  541.     }
  542. }

  543. /* Build minimal symbols named `function@got.plt' (see SYMBOL_GOT_PLT_SUFFIX)
  544.    for later look ups of which function to call when user requests
  545.    a STT_GNU_IFUNC function.  As the STT_GNU_IFUNC type is found at the target
  546.    library defining `function' we cannot yet know while reading OBJFILE which
  547.    of the SYMBOL_GOT_PLT_SUFFIX entries will be needed and later
  548.    DYN_SYMBOL_TABLE is no longer easily available for OBJFILE.  */

  549. static void
  550. elf_rel_plt_read (struct objfile *objfile, asymbol **dyn_symbol_table)
  551. {
  552.   bfd *obfd = objfile->obfd;
  553.   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
  554.   asection *plt, *relplt, *got_plt;
  555.   int plt_elf_idx;
  556.   bfd_size_type reloc_count, reloc;
  557.   char *string_buffer = NULL;
  558.   size_t string_buffer_size = 0;
  559.   struct cleanup *back_to;
  560.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  561.   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
  562.   size_t ptr_size = TYPE_LENGTH (ptr_type);

  563.   if (objfile->separate_debug_objfile_backlink)
  564.     return;

  565.   plt = bfd_get_section_by_name (obfd, ".plt");
  566.   if (plt == NULL)
  567.     return;
  568.   plt_elf_idx = elf_section_data (plt)->this_idx;

  569.   got_plt = bfd_get_section_by_name (obfd, ".got.plt");
  570.   if (got_plt == NULL)
  571.     {
  572.       /* For platforms where there is no separate .got.plt.  */
  573.       got_plt = bfd_get_section_by_name (obfd, ".got");
  574.       if (got_plt == NULL)
  575.         return;
  576.     }

  577.   /* This search algorithm is from _bfd_elf_canonicalize_dynamic_reloc.  */
  578.   for (relplt = obfd->sections; relplt != NULL; relplt = relplt->next)
  579.     if (elf_section_data (relplt)->this_hdr.sh_info == plt_elf_idx
  580.         && (elf_section_data (relplt)->this_hdr.sh_type == SHT_REL
  581.             || elf_section_data (relplt)->this_hdr.sh_type == SHT_RELA))
  582.       break;
  583.   if (relplt == NULL)
  584.     return;

  585.   if (! bed->s->slurp_reloc_table (obfd, relplt, dyn_symbol_table, TRUE))
  586.     return;

  587.   back_to = make_cleanup (free_current_contents, &string_buffer);

  588.   reloc_count = relplt->size / elf_section_data (relplt)->this_hdr.sh_entsize;
  589.   for (reloc = 0; reloc < reloc_count; reloc++)
  590.     {
  591.       const char *name;
  592.       struct minimal_symbol *msym;
  593.       CORE_ADDR address;
  594.       const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
  595.       size_t name_len;

  596.       name = bfd_asymbol_name (*relplt->relocation[reloc].sym_ptr_ptr);
  597.       name_len = strlen (name);
  598.       address = relplt->relocation[reloc].address;

  599.       /* Does the pointer reside in the .got.plt section?  */
  600.       if (!(bfd_get_section_vma (obfd, got_plt) <= address
  601.             && address < bfd_get_section_vma (obfd, got_plt)
  602.                          + bfd_get_section_size (got_plt)))
  603.         continue;

  604.       /* We cannot check if NAME is a reference to mst_text_gnu_ifunc as in
  605.          OBJFILE the symbol is undefined and the objfile having NAME defined
  606.          may not yet have been loaded.  */

  607.       if (string_buffer_size < name_len + got_suffix_len + 1)
  608.         {
  609.           string_buffer_size = 2 * (name_len + got_suffix_len);
  610.           string_buffer = xrealloc (string_buffer, string_buffer_size);
  611.         }
  612.       memcpy (string_buffer, name, name_len);
  613.       memcpy (&string_buffer[name_len], SYMBOL_GOT_PLT_SUFFIX,
  614.               got_suffix_len + 1);

  615.       msym = record_minimal_symbol (string_buffer, name_len + got_suffix_len,
  616.                                     1, address, mst_slot_got_plt, got_plt,
  617.                                     objfile);
  618.       if (msym)
  619.         SET_MSYMBOL_SIZE (msym, ptr_size);
  620.     }

  621.   do_cleanups (back_to);
  622. }

  623. /* The data pointer is htab_t for gnu_ifunc_record_cache_unchecked.  */

  624. static const struct objfile_data *elf_objfile_gnu_ifunc_cache_data;

  625. /* Map function names to CORE_ADDR in elf_objfile_gnu_ifunc_cache_data.  */

  626. struct elf_gnu_ifunc_cache
  627. {
  628.   /* This is always a function entry address, not a function descriptor.  */
  629.   CORE_ADDR addr;

  630.   char name[1];
  631. };

  632. /* htab_hash for elf_objfile_gnu_ifunc_cache_data.  */

  633. static hashval_t
  634. elf_gnu_ifunc_cache_hash (const void *a_voidp)
  635. {
  636.   const struct elf_gnu_ifunc_cache *a = a_voidp;

  637.   return htab_hash_string (a->name);
  638. }

  639. /* htab_eq for elf_objfile_gnu_ifunc_cache_data.  */

  640. static int
  641. elf_gnu_ifunc_cache_eq (const void *a_voidp, const void *b_voidp)
  642. {
  643.   const struct elf_gnu_ifunc_cache *a = a_voidp;
  644.   const struct elf_gnu_ifunc_cache *b = b_voidp;

  645.   return strcmp (a->name, b->name) == 0;
  646. }

  647. /* Record the target function address of a STT_GNU_IFUNC function NAME is the
  648.    function entry address ADDR.  Return 1 if NAME and ADDR are considered as
  649.    valid and therefore they were successfully recorded, return 0 otherwise.

  650.    Function does not expect a duplicate entry.  Use
  651.    elf_gnu_ifunc_resolve_by_cache first to check if the entry for NAME already
  652.    exists.  */

  653. static int
  654. elf_gnu_ifunc_record_cache (const char *name, CORE_ADDR addr)
  655. {
  656.   struct bound_minimal_symbol msym;
  657.   asection *sect;
  658.   struct objfile *objfile;
  659.   htab_t htab;
  660.   struct elf_gnu_ifunc_cache entry_local, *entry_p;
  661.   void **slot;

  662.   msym = lookup_minimal_symbol_by_pc (addr);
  663.   if (msym.minsym == NULL)
  664.     return 0;
  665.   if (BMSYMBOL_VALUE_ADDRESS (msym) != addr)
  666.     return 0;
  667.   /* minimal symbols have always SYMBOL_OBJ_SECTION non-NULL.  */
  668.   sect = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym)->the_bfd_section;
  669.   objfile = msym.objfile;

  670.   /* If .plt jumps back to .plt the symbol is still deferred for later
  671.      resolution and it has no use for GDB.  Besides ".text" this symbol can
  672.      reside also in ".opd" for ppc64 function descriptor.  */
  673.   if (strcmp (bfd_get_section_name (objfile->obfd, sect), ".plt") == 0)
  674.     return 0;

  675.   htab = objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data);
  676.   if (htab == NULL)
  677.     {
  678.       htab = htab_create_alloc_ex (1, elf_gnu_ifunc_cache_hash,
  679.                                    elf_gnu_ifunc_cache_eq,
  680.                                    NULL, &objfile->objfile_obstack,
  681.                                    hashtab_obstack_allocate,
  682.                                    dummy_obstack_deallocate);
  683.       set_objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data, htab);
  684.     }

  685.   entry_local.addr = addr;
  686.   obstack_grow (&objfile->objfile_obstack, &entry_local,
  687.                 offsetof (struct elf_gnu_ifunc_cache, name));
  688.   obstack_grow_str0 (&objfile->objfile_obstack, name);
  689.   entry_p = obstack_finish (&objfile->objfile_obstack);

  690.   slot = htab_find_slot (htab, entry_p, INSERT);
  691.   if (*slot != NULL)
  692.     {
  693.       struct elf_gnu_ifunc_cache *entry_found_p = *slot;
  694.       struct gdbarch *gdbarch = get_objfile_arch (objfile);

  695.       if (entry_found_p->addr != addr)
  696.         {
  697.           /* This case indicates buggy inferior program, the resolved address
  698.              should never change.  */

  699.             warning (_("gnu-indirect-function \"%s\" has changed its resolved "
  700.                        "function_address from %s to %s"),
  701.                      name, paddress (gdbarch, entry_found_p->addr),
  702.                      paddress (gdbarch, addr));
  703.         }

  704.       /* New ENTRY_P is here leaked/duplicate in the OBJFILE obstack.  */
  705.     }
  706.   *slot = entry_p;

  707.   return 1;
  708. }

  709. /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
  710.    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
  711.    is not NULL) and the function returns 1.  It returns 0 otherwise.

  712.    Only the elf_objfile_gnu_ifunc_cache_data hash table is searched by this
  713.    function.  */

  714. static int
  715. elf_gnu_ifunc_resolve_by_cache (const char *name, CORE_ADDR *addr_p)
  716. {
  717.   struct objfile *objfile;

  718.   ALL_PSPACE_OBJFILES (current_program_space, objfile)
  719.     {
  720.       htab_t htab;
  721.       struct elf_gnu_ifunc_cache *entry_p;
  722.       void **slot;

  723.       htab = objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data);
  724.       if (htab == NULL)
  725.         continue;

  726.       entry_p = alloca (sizeof (*entry_p) + strlen (name));
  727.       strcpy (entry_p->name, name);

  728.       slot = htab_find_slot (htab, entry_p, NO_INSERT);
  729.       if (slot == NULL)
  730.         continue;
  731.       entry_p = *slot;
  732.       gdb_assert (entry_p != NULL);

  733.       if (addr_p)
  734.         *addr_p = entry_p->addr;
  735.       return 1;
  736.     }

  737.   return 0;
  738. }

  739. /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
  740.    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
  741.    is not NULL) and the function returns 1.  It returns 0 otherwise.

  742.    Only the SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.
  743.    elf_gnu_ifunc_resolve_by_cache must have been already called for NAME to
  744.    prevent cache entries duplicates.  */

  745. static int
  746. elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
  747. {
  748.   char *name_got_plt;
  749.   struct objfile *objfile;
  750.   const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);

  751.   name_got_plt = alloca (strlen (name) + got_suffix_len + 1);
  752.   sprintf (name_got_plt, "%s" SYMBOL_GOT_PLT_SUFFIX, name);

  753.   ALL_PSPACE_OBJFILES (current_program_space, objfile)
  754.     {
  755.       bfd *obfd = objfile->obfd;
  756.       struct gdbarch *gdbarch = get_objfile_arch (objfile);
  757.       struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
  758.       size_t ptr_size = TYPE_LENGTH (ptr_type);
  759.       CORE_ADDR pointer_address, addr;
  760.       asection *plt;
  761.       gdb_byte *buf = alloca (ptr_size);
  762.       struct bound_minimal_symbol msym;

  763.       msym = lookup_minimal_symbol (name_got_plt, NULL, objfile);
  764.       if (msym.minsym == NULL)
  765.         continue;
  766.       if (MSYMBOL_TYPE (msym.minsym) != mst_slot_got_plt)
  767.         continue;
  768.       pointer_address = BMSYMBOL_VALUE_ADDRESS (msym);

  769.       plt = bfd_get_section_by_name (obfd, ".plt");
  770.       if (plt == NULL)
  771.         continue;

  772.       if (MSYMBOL_SIZE (msym.minsym) != ptr_size)
  773.         continue;
  774.       if (target_read_memory (pointer_address, buf, ptr_size) != 0)
  775.         continue;
  776.       addr = extract_typed_address (buf, ptr_type);
  777.       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
  778.                                                  &current_target);
  779.       addr = gdbarch_addr_bits_remove (gdbarch, addr);

  780.       if (addr_p)
  781.         *addr_p = addr;
  782.       if (elf_gnu_ifunc_record_cache (name, addr))
  783.         return 1;
  784.     }

  785.   return 0;
  786. }

  787. /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
  788.    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
  789.    is not NULL) and the function returns 1.  It returns 0 otherwise.

  790.    Both the elf_objfile_gnu_ifunc_cache_data hash table and
  791.    SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.  */

  792. static int
  793. elf_gnu_ifunc_resolve_name (const char *name, CORE_ADDR *addr_p)
  794. {
  795.   if (elf_gnu_ifunc_resolve_by_cache (name, addr_p))
  796.     return 1;

  797.   if (elf_gnu_ifunc_resolve_by_got (name, addr_p))
  798.     return 1;

  799.   return 0;
  800. }

  801. /* Call STT_GNU_IFUNC - a function returning addresss of a real function to
  802.    call.  PC is theSTT_GNU_IFUNC resolving function entry.  The value returned
  803.    is the entry point of the resolved STT_GNU_IFUNC target function to call.
  804.    */

  805. static CORE_ADDR
  806. elf_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
  807. {
  808.   const char *name_at_pc;
  809.   CORE_ADDR start_at_pc, address;
  810.   struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
  811.   struct value *function, *address_val;

  812.   /* Try first any non-intrusive methods without an inferior call.  */

  813.   if (find_pc_partial_function (pc, &name_at_pc, &start_at_pc, NULL)
  814.       && start_at_pc == pc)
  815.     {
  816.       if (elf_gnu_ifunc_resolve_name (name_at_pc, &address))
  817.         return address;
  818.     }
  819.   else
  820.     name_at_pc = NULL;

  821.   function = allocate_value (func_func_type);
  822.   set_value_address (function, pc);

  823.   /* STT_GNU_IFUNC resolver functions have no parameters.  FUNCTION is the
  824.      function entry address.  ADDRESS may be a function descriptor.  */

  825.   address_val = call_function_by_hand (function, 0, NULL);
  826.   address = value_as_address (address_val);
  827.   address = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
  828.                                                 &current_target);
  829.   address = gdbarch_addr_bits_remove (gdbarch, address);

  830.   if (name_at_pc)
  831.     elf_gnu_ifunc_record_cache (name_at_pc, address);

  832.   return address;
  833. }

  834. /* Handle inferior hit of bp_gnu_ifunc_resolver, see its definition.  */

  835. static void
  836. elf_gnu_ifunc_resolver_stop (struct breakpoint *b)
  837. {
  838.   struct breakpoint *b_return;
  839.   struct frame_info *prev_frame = get_prev_frame (get_current_frame ());
  840.   struct frame_id prev_frame_id = get_stack_frame_id (prev_frame);
  841.   CORE_ADDR prev_pc = get_frame_pc (prev_frame);
  842.   int thread_id = pid_to_thread_id (inferior_ptid);

  843.   gdb_assert (b->type == bp_gnu_ifunc_resolver);

  844.   for (b_return = b->related_breakpoint; b_return != b;
  845.        b_return = b_return->related_breakpoint)
  846.     {
  847.       gdb_assert (b_return->type == bp_gnu_ifunc_resolver_return);
  848.       gdb_assert (b_return->loc != NULL && b_return->loc->next == NULL);
  849.       gdb_assert (frame_id_p (b_return->frame_id));

  850.       if (b_return->thread == thread_id
  851.           && b_return->loc->requested_address == prev_pc
  852.           && frame_id_eq (b_return->frame_id, prev_frame_id))
  853.         break;
  854.     }

  855.   if (b_return == b)
  856.     {
  857.       struct symtab_and_line sal;

  858.       /* No need to call find_pc_line for symbols resolving as this is only
  859.          a helper breakpointer never shown to the user.  */

  860.       init_sal (&sal);
  861.       sal.pspace = current_inferior ()->pspace;
  862.       sal.pc = prev_pc;
  863.       sal.section = find_pc_overlay (sal.pc);
  864.       sal.explicit_pc = 1;
  865.       b_return = set_momentary_breakpoint (get_frame_arch (prev_frame), sal,
  866.                                            prev_frame_id,
  867.                                            bp_gnu_ifunc_resolver_return);

  868.       /* set_momentary_breakpoint invalidates PREV_FRAME.  */
  869.       prev_frame = NULL;

  870.       /* Add new b_return to the ring list b->related_breakpoint.  */
  871.       gdb_assert (b_return->related_breakpoint == b_return);
  872.       b_return->related_breakpoint = b->related_breakpoint;
  873.       b->related_breakpoint = b_return;
  874.     }
  875. }

  876. /* Handle inferior hit of bp_gnu_ifunc_resolver_return, see its definition.  */

  877. static void
  878. elf_gnu_ifunc_resolver_return_stop (struct breakpoint *b)
  879. {
  880.   struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
  881.   struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
  882.   struct type *value_type = TYPE_TARGET_TYPE (func_func_type);
  883.   struct regcache *regcache = get_thread_regcache (inferior_ptid);
  884.   struct value *func_func;
  885.   struct value *value;
  886.   CORE_ADDR resolved_address, resolved_pc;
  887.   struct symtab_and_line sal;
  888.   struct symtabs_and_lines sals, sals_end;

  889.   gdb_assert (b->type == bp_gnu_ifunc_resolver_return);

  890.   while (b->related_breakpoint != b)
  891.     {
  892.       struct breakpoint *b_next = b->related_breakpoint;

  893.       switch (b->type)
  894.         {
  895.         case bp_gnu_ifunc_resolver:
  896.           break;
  897.         case bp_gnu_ifunc_resolver_return:
  898.           delete_breakpoint (b);
  899.           break;
  900.         default:
  901.           internal_error (__FILE__, __LINE__,
  902.                           _("handle_inferior_event: Invalid "
  903.                             "gnu-indirect-function breakpoint type %d"),
  904.                           (int) b->type);
  905.         }
  906.       b = b_next;
  907.     }
  908.   gdb_assert (b->type == bp_gnu_ifunc_resolver);
  909.   gdb_assert (b->loc->next == NULL);

  910.   func_func = allocate_value (func_func_type);
  911.   set_value_address (func_func, b->loc->related_address);

  912.   value = allocate_value (value_type);
  913.   gdbarch_return_value (gdbarch, func_func, value_type, regcache,
  914.                         value_contents_raw (value), NULL);
  915.   resolved_address = value_as_address (value);
  916.   resolved_pc = gdbarch_convert_from_func_ptr_addr (gdbarch,
  917.                                                     resolved_address,
  918.                                                     &current_target);
  919.   resolved_pc = gdbarch_addr_bits_remove (gdbarch, resolved_pc);

  920.   gdb_assert (current_program_space == b->pspace || b->pspace == NULL);
  921.   elf_gnu_ifunc_record_cache (b->addr_string, resolved_pc);

  922.   sal = find_pc_line (resolved_pc, 0);
  923.   sals.nelts = 1;
  924.   sals.sals = &sal;
  925.   sals_end.nelts = 0;

  926.   b->type = bp_breakpoint;
  927.   update_breakpoint_locations (b, sals, sals_end);
  928. }

  929. /* A helper function for elf_symfile_read that reads the minimal
  930.    symbols.  */

  931. static void
  932. elf_read_minimal_symbols (struct objfile *objfile, int symfile_flags,
  933.                           const struct elfinfo *ei)
  934. {
  935.   bfd *synth_abfd, *abfd = objfile->obfd;
  936.   struct cleanup *back_to;
  937.   long symcount = 0, dynsymcount = 0, synthcount, storage_needed;
  938.   asymbol **symbol_table = NULL, **dyn_symbol_table = NULL;
  939.   asymbol *synthsyms;
  940.   struct dbx_symfile_info *dbx;

  941.   if (symtab_create_debug)
  942.     {
  943.       fprintf_unfiltered (gdb_stdlog,
  944.                           "Reading minimal symbols of objfile %s ...\n",
  945.                           objfile_name (objfile));
  946.     }

  947.   /* If we already have minsyms, then we can skip some work here.
  948.      However, if there were stabs or mdebug sections, we go ahead and
  949.      redo all the work anyway, because the psym readers for those
  950.      kinds of debuginfo need extra information found here.  This can
  951.      go away once all types of symbols are in the per-BFD object.  */
  952.   if (objfile->per_bfd->minsyms_read
  953.       && ei->stabsect == NULL
  954.       && ei->mdebugsect == NULL)
  955.     {
  956.       if (symtab_create_debug)
  957.         fprintf_unfiltered (gdb_stdlog,
  958.                             "... minimal symbols previously read\n");
  959.       return;
  960.     }

  961.   init_minimal_symbol_collection ();
  962.   back_to = make_cleanup_discard_minimal_symbols ();

  963.   /* Allocate struct to keep track of the symfile.  */
  964.   dbx = XCNEW (struct dbx_symfile_info);
  965.   set_objfile_data (objfile, dbx_objfile_data_key, dbx);
  966.   make_cleanup (free_elfinfo, (void *) objfile);

  967.   /* Process the normal ELF symbol table first.  This may write some
  968.      chain of info into the dbx_symfile_info of the objfile, which can
  969.      later be used by elfstab_offset_sections.  */

  970.   storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
  971.   if (storage_needed < 0)
  972.     error (_("Can't read symbols from %s: %s"),
  973.            bfd_get_filename (objfile->obfd),
  974.            bfd_errmsg (bfd_get_error ()));

  975.   if (storage_needed > 0)
  976.     {
  977.       symbol_table = (asymbol **) xmalloc (storage_needed);
  978.       make_cleanup (xfree, symbol_table);
  979.       symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);

  980.       if (symcount < 0)
  981.         error (_("Can't read symbols from %s: %s"),
  982.                bfd_get_filename (objfile->obfd),
  983.                bfd_errmsg (bfd_get_error ()));

  984.       elf_symtab_read (objfile, ST_REGULAR, symcount, symbol_table, 0);
  985.     }

  986.   /* Add the dynamic symbols.  */

  987.   storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd);

  988.   if (storage_needed > 0)
  989.     {
  990.       /* Memory gets permanently referenced from ABFD after
  991.          bfd_get_synthetic_symtab so it must not get freed before ABFD gets.
  992.          It happens only in the case when elf_slurp_reloc_table sees
  993.          asection->relocation NULL.  Determining which section is asection is
  994.          done by _bfd_elf_get_synthetic_symtab which is all a bfd
  995.          implementation detail, though.  */

  996.       dyn_symbol_table = bfd_alloc (abfd, storage_needed);
  997.       dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd,
  998.                                                      dyn_symbol_table);

  999.       if (dynsymcount < 0)
  1000.         error (_("Can't read symbols from %s: %s"),
  1001.                bfd_get_filename (objfile->obfd),
  1002.                bfd_errmsg (bfd_get_error ()));

  1003.       elf_symtab_read (objfile, ST_DYNAMIC, dynsymcount, dyn_symbol_table, 0);

  1004.       elf_rel_plt_read (objfile, dyn_symbol_table);
  1005.     }

  1006.   /* Contrary to binutils --strip-debug/--only-keep-debug the strip command from
  1007.      elfutils (eu-strip) moves even the .symtab section into the .debug file.

  1008.      bfd_get_synthetic_symtab on ppc64 for each function descriptor ELF symbol
  1009.      'name' creates a new BSF_SYNTHETIC ELF symbol '.name' with its code
  1010.      address.  But with eu-strip files bfd_get_synthetic_symtab would fail to
  1011.      read the code address from .opd while it reads the .symtab section from
  1012.      a separate debug info file as the .opd section is SHT_NOBITS there.

  1013.      With SYNTH_ABFD the .opd section will be read from the original
  1014.      backlinked binary where it is valid.  */

  1015.   if (objfile->separate_debug_objfile_backlink)
  1016.     synth_abfd = objfile->separate_debug_objfile_backlink->obfd;
  1017.   else
  1018.     synth_abfd = abfd;

  1019.   /* Add synthetic symbols - for instance, names for any PLT entries.  */

  1020.   synthcount = bfd_get_synthetic_symtab (synth_abfd, symcount, symbol_table,
  1021.                                          dynsymcount, dyn_symbol_table,
  1022.                                          &synthsyms);
  1023.   if (synthcount > 0)
  1024.     {
  1025.       asymbol **synth_symbol_table;
  1026.       long i;

  1027.       make_cleanup (xfree, synthsyms);
  1028.       synth_symbol_table = xmalloc (sizeof (asymbol *) * synthcount);
  1029.       for (i = 0; i < synthcount; i++)
  1030.         synth_symbol_table[i] = synthsyms + i;
  1031.       make_cleanup (xfree, synth_symbol_table);
  1032.       elf_symtab_read (objfile, ST_SYNTHETIC, synthcount,
  1033.                        synth_symbol_table, 1);
  1034.     }

  1035.   /* Install any minimal symbols that have been collected as the current
  1036.      minimal symbols for this objfile.  The debug readers below this point
  1037.      should not generate new minimal symbols; if they do it's their
  1038.      responsibility to install them.  "mdebug" appears to be the only one
  1039.      which will do this.  */

  1040.   install_minimal_symbols (objfile);
  1041.   do_cleanups (back_to);

  1042.   if (symtab_create_debug)
  1043.     fprintf_unfiltered (gdb_stdlog, "Done reading minimal symbols.\n");
  1044. }

  1045. /* Scan and build partial symbols for a symbol file.
  1046.    We have been initialized by a call to elf_symfile_init, which
  1047.    currently does nothing.

  1048.    This function only does the minimum work necessary for letting the
  1049.    user "name" things symbolically; it does not read the entire symtab.
  1050.    Instead, it reads the external and static symbols and puts them in partial
  1051.    symbol tables.  When more extensive information is requested of a
  1052.    file, the corresponding partial symbol table is mutated into a full
  1053.    fledged symbol table by going back and reading the symbols
  1054.    for real.

  1055.    We look for sections with specific names, to tell us what debug
  1056.    format to look for:  FIXME!!!

  1057.    elfstab_build_psymtabs() handles STABS symbols;
  1058.    mdebug_build_psymtabs() handles ECOFF debugging information.

  1059.    Note that ELF files have a "minimal" symbol table, which looks a lot
  1060.    like a COFF symbol table, but has only the minimal information necessary
  1061.    for linking.  We process this also, and use the information to
  1062.    build gdb's minimal symbol table.  This gives us some minimal debugging
  1063.    capability even for files compiled without -g.  */

  1064. static void
  1065. elf_symfile_read (struct objfile *objfile, int symfile_flags)
  1066. {
  1067.   bfd *abfd = objfile->obfd;
  1068.   struct elfinfo ei;

  1069.   memset ((char *) &ei, 0, sizeof (ei));
  1070.   bfd_map_over_sections (abfd, elf_locate_sections, (void *) & ei);

  1071.   elf_read_minimal_symbols (objfile, symfile_flags, &ei);

  1072.   /* ELF debugging information is inserted into the psymtab in the
  1073.      order of least informative first - most informative last.  Since
  1074.      the psymtab table is searched `most recent insertion first' this
  1075.      increases the probability that more detailed debug information
  1076.      for a section is found.

  1077.      For instance, an object file might contain both .mdebug (XCOFF)
  1078.      and .debug_info (DWARF2) sections then .mdebug is inserted first
  1079.      (searched last) and DWARF2 is inserted last (searched first).  If
  1080.      we don't do this then the XCOFF info is found first - for code in
  1081.      an included file XCOFF info is useless.  */

  1082.   if (ei.mdebugsect)
  1083.     {
  1084.       const struct ecoff_debug_swap *swap;

  1085.       /* .mdebug section, presumably holding ECOFF debugging
  1086.          information.  */
  1087.       swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1088.       if (swap)
  1089.         elfmdebug_build_psymtabs (objfile, swap, ei.mdebugsect);
  1090.     }
  1091.   if (ei.stabsect)
  1092.     {
  1093.       asection *str_sect;

  1094.       /* Stab sections have an associated string table that looks like
  1095.          a separate section.  */
  1096.       str_sect = bfd_get_section_by_name (abfd, ".stabstr");

  1097.       /* FIXME should probably warn about a stab section without a stabstr.  */
  1098.       if (str_sect)
  1099.         elfstab_build_psymtabs (objfile,
  1100.                                 ei.stabsect,
  1101.                                 str_sect->filepos,
  1102.                                 bfd_section_size (abfd, str_sect));
  1103.     }

  1104.   if (dwarf2_has_info (objfile, NULL))
  1105.     {
  1106.       /* elf_sym_fns_gdb_index cannot handle simultaneous non-DWARF debug
  1107.          information present in OBJFILE.  If there is such debug info present
  1108.          never use .gdb_index.  */

  1109.       if (!objfile_has_partial_symbols (objfile)
  1110.           && dwarf2_initialize_objfile (objfile))
  1111.         objfile_set_sym_fns (objfile, &elf_sym_fns_gdb_index);
  1112.       else
  1113.         {
  1114.           /* It is ok to do this even if the stabs reader made some
  1115.              partial symbols, because OBJF_PSYMTABS_READ has not been
  1116.              set, and so our lazy reader function will still be called
  1117.              when needed.  */
  1118.           objfile_set_sym_fns (objfile, &elf_sym_fns_lazy_psyms);
  1119.         }
  1120.     }
  1121.   /* If the file has its own symbol tables it has no separate debug
  1122.      info.  `.dynsym'/`.symtab' go to MSYMBOLS, `.debug_info' goes to
  1123.      SYMTABS/PSYMTABS.  `.gnu_debuglink' may no longer be present with
  1124.      `.note.gnu.build-id'.

  1125.      .gnu_debugdata is !objfile_has_partial_symbols because it contains only
  1126.      .symtab, not .debug_* section.  But if we already added .gnu_debugdata as
  1127.      an objfile via find_separate_debug_file_in_section there was no separate
  1128.      debug info available.  Therefore do not attempt to search for another one,
  1129.      objfile->separate_debug_objfile->separate_debug_objfile GDB guarantees to
  1130.      be NULL and we would possibly violate it.  */

  1131.   else if (!objfile_has_partial_symbols (objfile)
  1132.            && objfile->separate_debug_objfile == NULL
  1133.            && objfile->separate_debug_objfile_backlink == NULL)
  1134.     {
  1135.       char *debugfile;

  1136.       debugfile = find_separate_debug_file_by_buildid (objfile);

  1137.       if (debugfile == NULL)
  1138.         debugfile = find_separate_debug_file_by_debuglink (objfile);

  1139.       if (debugfile)
  1140.         {
  1141.           struct cleanup *cleanup = make_cleanup (xfree, debugfile);
  1142.           bfd *abfd = symfile_bfd_open (debugfile);

  1143.           make_cleanup_bfd_unref (abfd);
  1144.           symbol_file_add_separate (abfd, debugfile, symfile_flags, objfile);
  1145.           do_cleanups (cleanup);
  1146.         }
  1147.     }
  1148. }

  1149. /* Callback to lazily read psymtabs.  */

  1150. static void
  1151. read_psyms (struct objfile *objfile)
  1152. {
  1153.   if (dwarf2_has_info (objfile, NULL))
  1154.     dwarf2_build_psymtabs (objfile);
  1155. }

  1156. /* This cleans up the objfile's dbx symfile info, and the chain of
  1157.    stab_section_info's, that might be dangling from it.  */

  1158. static void
  1159. free_elfinfo (void *objp)
  1160. {
  1161.   struct objfile *objfile = (struct objfile *) objp;
  1162.   struct dbx_symfile_info *dbxinfo = DBX_SYMFILE_INFO (objfile);
  1163.   struct stab_section_info *ssi, *nssi;

  1164.   ssi = dbxinfo->stab_section_info;
  1165.   while (ssi)
  1166.     {
  1167.       nssi = ssi->next;
  1168.       xfree (ssi);
  1169.       ssi = nssi;
  1170.     }

  1171.   dbxinfo->stab_section_info = 0;        /* Just say No mo info about this.  */
  1172. }


  1173. /* Initialize anything that needs initializing when a completely new symbol
  1174.    file is specified (not just adding some symbols from another file, e.g. a
  1175.    shared library).

  1176.    We reinitialize buildsym, since we may be reading stabs from an ELF
  1177.    file.  */

  1178. static void
  1179. elf_new_init (struct objfile *ignore)
  1180. {
  1181.   stabsread_new_init ();
  1182.   buildsym_new_init ();
  1183. }

  1184. /* Perform any local cleanups required when we are done with a particular
  1185.    objfileI.E, we are in the process of discarding all symbol information
  1186.    for an objfile, freeing up all memory held for it, and unlinking the
  1187.    objfile struct from the global list of known objfiles.  */

  1188. static void
  1189. elf_symfile_finish (struct objfile *objfile)
  1190. {
  1191.   dwarf2_free_objfile (objfile);
  1192. }

  1193. /* ELF specific initialization routine for reading symbols.  */

  1194. static void
  1195. elf_symfile_init (struct objfile *objfile)
  1196. {
  1197.   /* ELF objects may be reordered, so set OBJF_REORDERED.  If we
  1198.      find this causes a significant slowdown in gdb then we could
  1199.      set it in the debug symbol readers only when necessary.  */
  1200.   objfile->flags |= OBJF_REORDERED;
  1201. }

  1202. /* When handling an ELF file that contains Sun STABS debug info,
  1203.    some of the debug info is relative to the particular chunk of the
  1204.    section that was generated in its individual .o file.  E.g.
  1205.    offsets to static variables are relative to the start of the data
  1206.    segment *for that module before linking*.  This information is
  1207.    painfully squirreled away in the ELF symbol table as local symbols
  1208.    with wierd names.  Go get 'em when needed.  */

  1209. void
  1210. elfstab_offset_sections (struct objfile *objfile, struct partial_symtab *pst)
  1211. {
  1212.   const char *filename = pst->filename;
  1213.   struct dbx_symfile_info *dbx = DBX_SYMFILE_INFO (objfile);
  1214.   struct stab_section_info *maybe = dbx->stab_section_info;
  1215.   struct stab_section_info *questionable = 0;
  1216.   int i;

  1217.   /* The ELF symbol info doesn't include path names, so strip the path
  1218.      (if any) from the psymtab filename.  */
  1219.   filename = lbasename (filename);

  1220.   /* FIXME:  This linear search could speed up significantly
  1221.      if it was chained in the right order to match how we search it,
  1222.      and if we unchained when we found a match.  */
  1223.   for (; maybe; maybe = maybe->next)
  1224.     {
  1225.       if (filename[0] == maybe->filename[0]
  1226.           && filename_cmp (filename, maybe->filename) == 0)
  1227.         {
  1228.           /* We found a match.  But there might be several source files
  1229.              (from different directories) with the same name.  */
  1230.           if (0 == maybe->found)
  1231.             break;
  1232.           questionable = maybe;        /* Might use it later.  */
  1233.         }
  1234.     }

  1235.   if (maybe == 0 && questionable != 0)
  1236.     {
  1237.       complaint (&symfile_complaints,
  1238.                  _("elf/stab section information questionable for %s"),
  1239.                  filename);
  1240.       maybe = questionable;
  1241.     }

  1242.   if (maybe)
  1243.     {
  1244.       /* Found it!  Allocate a new psymtab struct, and fill it in.  */
  1245.       maybe->found++;
  1246.       pst->section_offsets = (struct section_offsets *)
  1247.         obstack_alloc (&objfile->objfile_obstack,
  1248.                        SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
  1249.       for (i = 0; i < maybe->num_sections; i++)
  1250.         (pst->section_offsets)->offsets[i] = maybe->sections[i];
  1251.       return;
  1252.     }

  1253.   /* We were unable to find any offsets for this file.  Complain.  */
  1254.   if (dbx->stab_section_info)        /* If there *is* any info, */
  1255.     complaint (&symfile_complaints,
  1256.                _("elf/stab section information missing for %s"), filename);
  1257. }

  1258. /* Implementation of `sym_get_probes', as documented in symfile.h.  */

  1259. static VEC (probe_p) *
  1260. elf_get_probes (struct objfile *objfile)
  1261. {
  1262.   VEC (probe_p) *probes_per_bfd;

  1263.   /* Have we parsed this objfile's probes already?  */
  1264.   probes_per_bfd = bfd_data (objfile->obfd, probe_key);

  1265.   if (!probes_per_bfd)
  1266.     {
  1267.       int ix;
  1268.       const struct probe_ops *probe_ops;

  1269.       /* Here we try to gather information about all types of probes from the
  1270.          objfile.  */
  1271.       for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops);
  1272.            ix++)
  1273.         probe_ops->get_probes (&probes_per_bfd, objfile);

  1274.       if (probes_per_bfd == NULL)
  1275.         {
  1276.           VEC_reserve (probe_p, probes_per_bfd, 1);
  1277.           gdb_assert (probes_per_bfd != NULL);
  1278.         }

  1279.       set_bfd_data (objfile->obfd, probe_key, probes_per_bfd);
  1280.     }

  1281.   return probes_per_bfd;
  1282. }

  1283. /* Helper function used to free the space allocated for storing SystemTap
  1284.    probe information.  */

  1285. static void
  1286. probe_key_free (bfd *abfd, void *d)
  1287. {
  1288.   int ix;
  1289.   VEC (probe_p) *probes = d;
  1290.   struct probe *probe;

  1291.   for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
  1292.     probe->pops->destroy (probe);

  1293.   VEC_free (probe_p, probes);
  1294. }



  1295. /* Implementation `sym_probe_fns', as documented in symfile.h.  */

  1296. static const struct sym_probe_fns elf_probe_fns =
  1297. {
  1298.   elf_get_probes,                    /* sym_get_probes */
  1299. };

  1300. /* Register that we are able to handle ELF object file formats.  */

  1301. static const struct sym_fns elf_sym_fns =
  1302. {
  1303.   elf_new_init,                        /* init anything gbl to entire symtab */
  1304.   elf_symfile_init,                /* read initial info, setup for sym_read() */
  1305.   elf_symfile_read,                /* read a symbol file into symtab */
  1306.   NULL,                                /* sym_read_psymbols */
  1307.   elf_symfile_finish,                /* finished with file, cleanup */
  1308.   default_symfile_offsets,        /* Translate ext. to int. relocation */
  1309.   elf_symfile_segments,                /* Get segment information from a file.  */
  1310.   NULL,
  1311.   default_symfile_relocate,        /* Relocate a debug section.  */
  1312.   &elf_probe_fns,                /* sym_probe_fns */
  1313.   &psym_functions
  1314. };

  1315. /* The same as elf_sym_fns, but not registered and lazily reads
  1316.    psymbols.  */

  1317. static const struct sym_fns elf_sym_fns_lazy_psyms =
  1318. {
  1319.   elf_new_init,                        /* init anything gbl to entire symtab */
  1320.   elf_symfile_init,                /* read initial info, setup for sym_read() */
  1321.   elf_symfile_read,                /* read a symbol file into symtab */
  1322.   read_psyms,                        /* sym_read_psymbols */
  1323.   elf_symfile_finish,                /* finished with file, cleanup */
  1324.   default_symfile_offsets,        /* Translate ext. to int. relocation */
  1325.   elf_symfile_segments,                /* Get segment information from a file.  */
  1326.   NULL,
  1327.   default_symfile_relocate,        /* Relocate a debug section.  */
  1328.   &elf_probe_fns,                /* sym_probe_fns */
  1329.   &psym_functions
  1330. };

  1331. /* The same as elf_sym_fns, but not registered and uses the
  1332.    DWARF-specific GNU index rather than psymtab.  */
  1333. static const struct sym_fns elf_sym_fns_gdb_index =
  1334. {
  1335.   elf_new_init,                        /* init anything gbl to entire symab */
  1336.   elf_symfile_init,                /* read initial info, setup for sym_red() */
  1337.   elf_symfile_read,                /* read a symbol file into symtab */
  1338.   NULL,                                /* sym_read_psymbols */
  1339.   elf_symfile_finish,                /* finished with file, cleanup */
  1340.   default_symfile_offsets,        /* Translate ext. to int. relocatin */
  1341.   elf_symfile_segments,                /* Get segment information from a file.  */
  1342.   NULL,
  1343.   default_symfile_relocate,        /* Relocate a debug section.  */
  1344.   &elf_probe_fns,                /* sym_probe_fns */
  1345.   &dwarf2_gdb_index_functions
  1346. };

  1347. /* STT_GNU_IFUNC resolver vector to be installed to gnu_ifunc_fns_p.  */

  1348. static const struct gnu_ifunc_fns elf_gnu_ifunc_fns =
  1349. {
  1350.   elf_gnu_ifunc_resolve_addr,
  1351.   elf_gnu_ifunc_resolve_name,
  1352.   elf_gnu_ifunc_resolver_stop,
  1353.   elf_gnu_ifunc_resolver_return_stop
  1354. };

  1355. void
  1356. _initialize_elfread (void)
  1357. {
  1358.   probe_key = register_bfd_data_with_cleanup (NULL, probe_key_free);
  1359.   add_symtab_fns (bfd_target_elf_flavour, &elf_sym_fns);

  1360.   elf_objfile_gnu_ifunc_cache_data = register_objfile_data ();
  1361.   gnu_ifunc_fns_p = &elf_gnu_ifunc_fns;
  1362. }