xref: /dflybsd-src/contrib/gdb-7/gdb/elfread.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Read ELF (Executable and Linking Format) object files for GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1991-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Written by Fred Fish at Cygnus Support.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "bfd.h"
245796c8dcSSimon Schubert #include "gdb_string.h"
255796c8dcSSimon Schubert #include "elf-bfd.h"
265796c8dcSSimon Schubert #include "elf/common.h"
275796c8dcSSimon Schubert #include "elf/internal.h"
285796c8dcSSimon Schubert #include "elf/mips.h"
295796c8dcSSimon Schubert #include "symtab.h"
305796c8dcSSimon Schubert #include "symfile.h"
315796c8dcSSimon Schubert #include "objfiles.h"
325796c8dcSSimon Schubert #include "buildsym.h"
335796c8dcSSimon Schubert #include "stabsread.h"
345796c8dcSSimon Schubert #include "gdb-stabs.h"
355796c8dcSSimon Schubert #include "complaints.h"
365796c8dcSSimon Schubert #include "demangle.h"
37cf7f2e2dSJohn Marino #include "psympriv.h"
38c50c785cSJohn Marino #include "filenames.h"
39*ef5ccd6cSJohn Marino #include "probe.h"
40*ef5ccd6cSJohn Marino #include "arch-utils.h"
41c50c785cSJohn Marino #include "gdbtypes.h"
42c50c785cSJohn Marino #include "value.h"
43c50c785cSJohn Marino #include "infcall.h"
44c50c785cSJohn Marino #include "gdbthread.h"
45c50c785cSJohn Marino #include "regcache.h"
46*ef5ccd6cSJohn Marino #include "bcache.h"
47*ef5ccd6cSJohn Marino #include "gdb_bfd.h"
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert extern void _initialize_elfread (void);
505796c8dcSSimon Schubert 
51c50c785cSJohn Marino /* Forward declarations.  */
52c50c785cSJohn Marino static const struct sym_fns elf_sym_fns_gdb_index;
53c50c785cSJohn Marino static const struct sym_fns elf_sym_fns_lazy_psyms;
54c50c785cSJohn Marino 
555796c8dcSSimon Schubert /* The struct elfinfo is available only during ELF symbol table and
565796c8dcSSimon Schubert    psymtab reading.  It is destroyed at the completion of psymtab-reading.
575796c8dcSSimon Schubert    It's local to elf_symfile_read.  */
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert struct elfinfo
605796c8dcSSimon Schubert   {
615796c8dcSSimon Schubert     asection *stabsect;		/* Section pointer for .stab section */
625796c8dcSSimon Schubert     asection *stabindexsect;	/* Section pointer for .stab.index section */
635796c8dcSSimon Schubert     asection *mdebugsect;	/* Section pointer for .mdebug section */
645796c8dcSSimon Schubert   };
655796c8dcSSimon Schubert 
66*ef5ccd6cSJohn Marino /* Per-objfile data for probe info.  */
67*ef5ccd6cSJohn Marino 
68*ef5ccd6cSJohn Marino static const struct objfile_data *probe_key = NULL;
69*ef5ccd6cSJohn Marino 
705796c8dcSSimon Schubert static void free_elfinfo (void *);
715796c8dcSSimon Schubert 
72c50c785cSJohn Marino /* Minimal symbols located at the GOT entries for .plt - that is the real
73c50c785cSJohn Marino    pointer where the given entry will jump to.  It gets updated by the real
74c50c785cSJohn Marino    function address during lazy ld.so resolving in the inferior.  These
75c50c785cSJohn Marino    minimal symbols are indexed for <tab>-completion.  */
76c50c785cSJohn Marino 
77c50c785cSJohn Marino #define SYMBOL_GOT_PLT_SUFFIX "@got.plt"
78c50c785cSJohn Marino 
795796c8dcSSimon Schubert /* Locate the segments in ABFD.  */
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert static struct symfile_segment_data *
elf_symfile_segments(bfd * abfd)825796c8dcSSimon Schubert elf_symfile_segments (bfd *abfd)
835796c8dcSSimon Schubert {
845796c8dcSSimon Schubert   Elf_Internal_Phdr *phdrs, **segments;
855796c8dcSSimon Schubert   long phdrs_size;
865796c8dcSSimon Schubert   int num_phdrs, num_segments, num_sections, i;
875796c8dcSSimon Schubert   asection *sect;
885796c8dcSSimon Schubert   struct symfile_segment_data *data;
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert   phdrs_size = bfd_get_elf_phdr_upper_bound (abfd);
915796c8dcSSimon Schubert   if (phdrs_size == -1)
925796c8dcSSimon Schubert     return NULL;
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert   phdrs = alloca (phdrs_size);
955796c8dcSSimon Schubert   num_phdrs = bfd_get_elf_phdrs (abfd, phdrs);
965796c8dcSSimon Schubert   if (num_phdrs == -1)
975796c8dcSSimon Schubert     return NULL;
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert   num_segments = 0;
1005796c8dcSSimon Schubert   segments = alloca (sizeof (Elf_Internal_Phdr *) * num_phdrs);
1015796c8dcSSimon Schubert   for (i = 0; i < num_phdrs; i++)
1025796c8dcSSimon Schubert     if (phdrs[i].p_type == PT_LOAD)
1035796c8dcSSimon Schubert       segments[num_segments++] = &phdrs[i];
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert   if (num_segments == 0)
1065796c8dcSSimon Schubert     return NULL;
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert   data = XZALLOC (struct symfile_segment_data);
1095796c8dcSSimon Schubert   data->num_segments = num_segments;
1105796c8dcSSimon Schubert   data->segment_bases = XCALLOC (num_segments, CORE_ADDR);
1115796c8dcSSimon Schubert   data->segment_sizes = XCALLOC (num_segments, CORE_ADDR);
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert   for (i = 0; i < num_segments; i++)
1145796c8dcSSimon Schubert     {
1155796c8dcSSimon Schubert       data->segment_bases[i] = segments[i]->p_vaddr;
1165796c8dcSSimon Schubert       data->segment_sizes[i] = segments[i]->p_memsz;
1175796c8dcSSimon Schubert     }
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert   num_sections = bfd_count_sections (abfd);
1205796c8dcSSimon Schubert   data->segment_info = XCALLOC (num_sections, int);
1215796c8dcSSimon Schubert 
1225796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
1235796c8dcSSimon Schubert     {
1245796c8dcSSimon Schubert       int j;
1255796c8dcSSimon Schubert       CORE_ADDR vma;
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
1285796c8dcSSimon Schubert 	continue;
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert       vma = bfd_get_section_vma (abfd, sect);
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert       for (j = 0; j < num_segments; j++)
1335796c8dcSSimon Schubert 	if (segments[j]->p_memsz > 0
1345796c8dcSSimon Schubert 	    && vma >= segments[j]->p_vaddr
1355796c8dcSSimon Schubert 	    && (vma - segments[j]->p_vaddr) < segments[j]->p_memsz)
1365796c8dcSSimon Schubert 	  {
1375796c8dcSSimon Schubert 	    data->segment_info[i] = j + 1;
1385796c8dcSSimon Schubert 	    break;
1395796c8dcSSimon Schubert 	  }
1405796c8dcSSimon Schubert 
141cf7f2e2dSJohn Marino       /* We should have found a segment for every non-empty section.
142cf7f2e2dSJohn Marino 	 If we haven't, we will not relocate this section by any
143cf7f2e2dSJohn Marino 	 offsets we apply to the segments.  As an exception, do not
144cf7f2e2dSJohn Marino 	 warn about SHT_NOBITS sections; in normal ELF execution
145cf7f2e2dSJohn Marino 	 environments, SHT_NOBITS means zero-initialized and belongs
146cf7f2e2dSJohn Marino 	 in a segment, but in no-OS environments some tools (e.g. ARM
147cf7f2e2dSJohn Marino 	 RealView) use SHT_NOBITS for uninitialized data.  Since it is
148cf7f2e2dSJohn Marino 	 uninitialized, it doesn't need a program header.  Such
149cf7f2e2dSJohn Marino 	 binaries are not relocatable.  */
150cf7f2e2dSJohn Marino       if (bfd_get_section_size (sect) > 0 && j == num_segments
151cf7f2e2dSJohn Marino 	  && (bfd_get_section_flags (abfd, sect) & SEC_LOAD) != 0)
152*ef5ccd6cSJohn Marino 	warning (_("Loadable section \"%s\" outside of ELF segments"),
1535796c8dcSSimon Schubert 		 bfd_section_name (abfd, sect));
1545796c8dcSSimon Schubert     }
1555796c8dcSSimon Schubert 
1565796c8dcSSimon Schubert   return data;
1575796c8dcSSimon Schubert }
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert /* We are called once per section from elf_symfile_read.  We
1605796c8dcSSimon Schubert    need to examine each section we are passed, check to see
1615796c8dcSSimon Schubert    if it is something we are interested in processing, and
1625796c8dcSSimon Schubert    if so, stash away some access information for the section.
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert    For now we recognize the dwarf debug information sections and
1655796c8dcSSimon Schubert    line number sections from matching their section names.  The
1665796c8dcSSimon Schubert    ELF definition is no real help here since it has no direct
1675796c8dcSSimon Schubert    knowledge of DWARF (by design, so any debugging format can be
1685796c8dcSSimon Schubert    used).
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert    We also recognize the ".stab" sections used by the Sun compilers
1715796c8dcSSimon Schubert    released with Solaris 2.
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert    FIXME: The section names should not be hardwired strings (what
1745796c8dcSSimon Schubert    should they be?  I don't think most object file formats have enough
175c50c785cSJohn Marino    section flags to specify what kind of debug section it is.
1765796c8dcSSimon Schubert    -kingdon).  */
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert static void
elf_locate_sections(bfd * ignore_abfd,asection * sectp,void * eip)1795796c8dcSSimon Schubert elf_locate_sections (bfd *ignore_abfd, asection *sectp, void *eip)
1805796c8dcSSimon Schubert {
1815796c8dcSSimon Schubert   struct elfinfo *ei;
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert   ei = (struct elfinfo *) eip;
1845796c8dcSSimon Schubert   if (strcmp (sectp->name, ".stab") == 0)
1855796c8dcSSimon Schubert     {
1865796c8dcSSimon Schubert       ei->stabsect = sectp;
1875796c8dcSSimon Schubert     }
1885796c8dcSSimon Schubert   else if (strcmp (sectp->name, ".stab.index") == 0)
1895796c8dcSSimon Schubert     {
1905796c8dcSSimon Schubert       ei->stabindexsect = sectp;
1915796c8dcSSimon Schubert     }
1925796c8dcSSimon Schubert   else if (strcmp (sectp->name, ".mdebug") == 0)
1935796c8dcSSimon Schubert     {
1945796c8dcSSimon Schubert       ei->mdebugsect = sectp;
1955796c8dcSSimon Schubert     }
1965796c8dcSSimon Schubert }
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert static struct minimal_symbol *
record_minimal_symbol(const char * name,int name_len,int copy_name,CORE_ADDR address,enum minimal_symbol_type ms_type,asection * bfd_section,struct objfile * objfile)199cf7f2e2dSJohn Marino record_minimal_symbol (const char *name, int name_len, int copy_name,
200cf7f2e2dSJohn Marino 		       CORE_ADDR address,
2015796c8dcSSimon Schubert 		       enum minimal_symbol_type ms_type,
2025796c8dcSSimon Schubert 		       asection *bfd_section, struct objfile *objfile)
2035796c8dcSSimon Schubert {
2045796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2055796c8dcSSimon Schubert 
206c50c785cSJohn Marino   if (ms_type == mst_text || ms_type == mst_file_text
207c50c785cSJohn Marino       || ms_type == mst_text_gnu_ifunc)
208*ef5ccd6cSJohn Marino     address = gdbarch_addr_bits_remove (gdbarch, address);
2095796c8dcSSimon Schubert 
210cf7f2e2dSJohn Marino   return prim_record_minimal_symbol_full (name, name_len, copy_name, address,
211cf7f2e2dSJohn Marino 					  ms_type, bfd_section->index,
212cf7f2e2dSJohn Marino 					  bfd_section, objfile);
2135796c8dcSSimon Schubert }
2145796c8dcSSimon Schubert 
215a45ae5f8SJohn Marino /* Read the symbol table of an ELF file.
2165796c8dcSSimon Schubert 
2175796c8dcSSimon Schubert    Given an objfile, a symbol table, and a flag indicating whether the
2185796c8dcSSimon Schubert    symbol table contains regular, dynamic, or synthetic symbols, add all
2195796c8dcSSimon Schubert    the global function and data symbols to the minimal symbol table.
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert    In stabs-in-ELF, as implemented by Sun, there are some local symbols
2225796c8dcSSimon Schubert    defined in the ELF symbol table, which can be used to locate
2235796c8dcSSimon Schubert    the beginnings of sections from each ".o" file that was linked to
2245796c8dcSSimon Schubert    form the executable objfile.  We gather any such info and record it
225a45ae5f8SJohn Marino    in data structures hung off the objfile's private data.  */
2265796c8dcSSimon Schubert 
2275796c8dcSSimon Schubert #define ST_REGULAR 0
2285796c8dcSSimon Schubert #define ST_DYNAMIC 1
2295796c8dcSSimon Schubert #define ST_SYNTHETIC 2
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert static void
elf_symtab_read(struct objfile * objfile,int type,long number_of_symbols,asymbol ** symbol_table,int copy_names)2325796c8dcSSimon Schubert elf_symtab_read (struct objfile *objfile, int type,
233cf7f2e2dSJohn Marino 		 long number_of_symbols, asymbol **symbol_table,
234cf7f2e2dSJohn Marino 		 int copy_names)
2355796c8dcSSimon Schubert {
2365796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2375796c8dcSSimon Schubert   asymbol *sym;
2385796c8dcSSimon Schubert   long i;
2395796c8dcSSimon Schubert   CORE_ADDR symaddr;
2405796c8dcSSimon Schubert   CORE_ADDR offset;
2415796c8dcSSimon Schubert   enum minimal_symbol_type ms_type;
2425796c8dcSSimon Schubert   /* If sectinfo is nonNULL, it contains section info that should end up
2435796c8dcSSimon Schubert      filed in the objfile.  */
2445796c8dcSSimon Schubert   struct stab_section_info *sectinfo = NULL;
2455796c8dcSSimon Schubert   /* If filesym is nonzero, it points to a file symbol, but we haven't
2465796c8dcSSimon Schubert      seen any section info for it yet.  */
2475796c8dcSSimon Schubert   asymbol *filesym = 0;
248cf7f2e2dSJohn Marino   /* Name of filesym.  This is either a constant string or is saved on
249*ef5ccd6cSJohn Marino      the objfile's filename cache.  */
250*ef5ccd6cSJohn Marino   const char *filesymname = "";
251*ef5ccd6cSJohn Marino   struct dbx_symfile_info *dbx = DBX_SYMFILE_INFO (objfile);
2525796c8dcSSimon Schubert   int stripped = (bfd_get_symcount (objfile->obfd) == 0);
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert   for (i = 0; i < number_of_symbols; i++)
2555796c8dcSSimon Schubert     {
2565796c8dcSSimon Schubert       sym = symbol_table[i];
2575796c8dcSSimon Schubert       if (sym->name == NULL || *sym->name == '\0')
2585796c8dcSSimon Schubert 	{
2595796c8dcSSimon Schubert 	  /* Skip names that don't exist (shouldn't happen), or names
2605796c8dcSSimon Schubert 	     that are null strings (may happen).  */
2615796c8dcSSimon Schubert 	  continue;
2625796c8dcSSimon Schubert 	}
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert       /* Skip "special" symbols, e.g. ARM mapping symbols.  These are
2655796c8dcSSimon Schubert 	 symbols which do not correspond to objects in the symbol table,
2665796c8dcSSimon Schubert 	 but have some other target-specific meaning.  */
2675796c8dcSSimon Schubert       if (bfd_is_target_special_symbol (objfile->obfd, sym))
2685796c8dcSSimon Schubert 	{
2695796c8dcSSimon Schubert 	  if (gdbarch_record_special_symbol_p (gdbarch))
2705796c8dcSSimon Schubert 	    gdbarch_record_special_symbol (gdbarch, objfile, sym);
2715796c8dcSSimon Schubert 	  continue;
2725796c8dcSSimon Schubert 	}
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert       offset = ANOFFSET (objfile->section_offsets, sym->section->index);
2755796c8dcSSimon Schubert       if (type == ST_DYNAMIC
276*ef5ccd6cSJohn Marino 	  && sym->section == bfd_und_section_ptr
2775796c8dcSSimon Schubert 	  && (sym->flags & BSF_FUNCTION))
2785796c8dcSSimon Schubert 	{
2795796c8dcSSimon Schubert 	  struct minimal_symbol *msym;
2805796c8dcSSimon Schubert 	  bfd *abfd = objfile->obfd;
2815796c8dcSSimon Schubert 	  asection *sect;
2825796c8dcSSimon Schubert 
2835796c8dcSSimon Schubert 	  /* Symbol is a reference to a function defined in
2845796c8dcSSimon Schubert 	     a shared library.
2855796c8dcSSimon Schubert 	     If its value is non zero then it is usually the address
2865796c8dcSSimon Schubert 	     of the corresponding entry in the procedure linkage table,
2875796c8dcSSimon Schubert 	     plus the desired section offset.
2885796c8dcSSimon Schubert 	     If its value is zero then the dynamic linker has to resolve
2895796c8dcSSimon Schubert 	     the symbol.  We are unable to find any meaningful address
2905796c8dcSSimon Schubert 	     for this symbol in the executable file, so we skip it.  */
2915796c8dcSSimon Schubert 	  symaddr = sym->value;
2925796c8dcSSimon Schubert 	  if (symaddr == 0)
2935796c8dcSSimon Schubert 	    continue;
2945796c8dcSSimon Schubert 
2955796c8dcSSimon Schubert 	  /* sym->section is the undefined section.  However, we want to
2965796c8dcSSimon Schubert 	     record the section where the PLT stub resides with the
2975796c8dcSSimon Schubert 	     minimal symbol.  Search the section table for the one that
2985796c8dcSSimon Schubert 	     covers the stub's address.  */
2995796c8dcSSimon Schubert 	  for (sect = abfd->sections; sect != NULL; sect = sect->next)
3005796c8dcSSimon Schubert 	    {
3015796c8dcSSimon Schubert 	      if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
3025796c8dcSSimon Schubert 		continue;
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert 	      if (symaddr >= bfd_get_section_vma (abfd, sect)
3055796c8dcSSimon Schubert 		  && symaddr < bfd_get_section_vma (abfd, sect)
3065796c8dcSSimon Schubert 			       + bfd_get_section_size (sect))
3075796c8dcSSimon Schubert 		break;
3085796c8dcSSimon Schubert 	    }
3095796c8dcSSimon Schubert 	  if (!sect)
3105796c8dcSSimon Schubert 	    continue;
3115796c8dcSSimon Schubert 
312*ef5ccd6cSJohn Marino 	  /* On ia64-hpux, we have discovered that the system linker
313*ef5ccd6cSJohn Marino 	     adds undefined symbols with nonzero addresses that cannot
314*ef5ccd6cSJohn Marino 	     be right (their address points inside the code of another
315*ef5ccd6cSJohn Marino 	     function in the .text section).  This creates problems
316*ef5ccd6cSJohn Marino 	     when trying to determine which symbol corresponds to
317*ef5ccd6cSJohn Marino 	     a given address.
318*ef5ccd6cSJohn Marino 
319*ef5ccd6cSJohn Marino 	     We try to detect those buggy symbols by checking which
320*ef5ccd6cSJohn Marino 	     section we think they correspond to.  Normally, PLT symbols
321*ef5ccd6cSJohn Marino 	     are stored inside their own section, and the typical name
322*ef5ccd6cSJohn Marino 	     for that section is ".plt".  So, if there is a ".plt"
323*ef5ccd6cSJohn Marino 	     section, and yet the section name of our symbol does not
324*ef5ccd6cSJohn Marino 	     start with ".plt", we ignore that symbol.  */
325*ef5ccd6cSJohn Marino 	  if (strncmp (sect->name, ".plt", 4) != 0
326*ef5ccd6cSJohn Marino 	      && bfd_get_section_by_name (abfd, ".plt") != NULL)
327*ef5ccd6cSJohn Marino 	    continue;
328*ef5ccd6cSJohn Marino 
3295796c8dcSSimon Schubert 	  symaddr += ANOFFSET (objfile->section_offsets, sect->index);
3305796c8dcSSimon Schubert 
3315796c8dcSSimon Schubert 	  msym = record_minimal_symbol
332cf7f2e2dSJohn Marino 	    (sym->name, strlen (sym->name), copy_names,
333cf7f2e2dSJohn Marino 	     symaddr, mst_solib_trampoline, sect, objfile);
3345796c8dcSSimon Schubert 	  if (msym != NULL)
3355796c8dcSSimon Schubert 	    msym->filename = filesymname;
3365796c8dcSSimon Schubert 	  continue;
3375796c8dcSSimon Schubert 	}
3385796c8dcSSimon Schubert 
3395796c8dcSSimon Schubert       /* If it is a nonstripped executable, do not enter dynamic
3405796c8dcSSimon Schubert 	 symbols, as the dynamic symbol table is usually a subset
3415796c8dcSSimon Schubert 	 of the main symbol table.  */
3425796c8dcSSimon Schubert       if (type == ST_DYNAMIC && !stripped)
3435796c8dcSSimon Schubert 	continue;
3445796c8dcSSimon Schubert       if (sym->flags & BSF_FILE)
3455796c8dcSSimon Schubert 	{
3465796c8dcSSimon Schubert 	  /* STT_FILE debugging symbol that helps stabs-in-elf debugging.
3475796c8dcSSimon Schubert 	     Chain any old one onto the objfile; remember new sym.  */
3485796c8dcSSimon Schubert 	  if (sectinfo != NULL)
3495796c8dcSSimon Schubert 	    {
3505796c8dcSSimon Schubert 	      sectinfo->next = dbx->stab_section_info;
3515796c8dcSSimon Schubert 	      dbx->stab_section_info = sectinfo;
3525796c8dcSSimon Schubert 	      sectinfo = NULL;
3535796c8dcSSimon Schubert 	    }
3545796c8dcSSimon Schubert 	  filesym = sym;
355*ef5ccd6cSJohn Marino 	  filesymname = bcache (filesym->name, strlen (filesym->name) + 1,
356*ef5ccd6cSJohn Marino 				objfile->per_bfd->filename_cache);
3575796c8dcSSimon Schubert 	}
3585796c8dcSSimon Schubert       else if (sym->flags & BSF_SECTION_SYM)
3595796c8dcSSimon Schubert 	continue;
360*ef5ccd6cSJohn Marino       else if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK
361*ef5ccd6cSJohn Marino 			     | BSF_GNU_UNIQUE))
3625796c8dcSSimon Schubert 	{
3635796c8dcSSimon Schubert 	  struct minimal_symbol *msym;
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert 	  /* Select global/local/weak symbols.  Note that bfd puts abs
3665796c8dcSSimon Schubert 	     symbols in their own section, so all symbols we are
3675796c8dcSSimon Schubert 	     interested in will have a section.  */
3685796c8dcSSimon Schubert 	  /* Bfd symbols are section relative.  */
3695796c8dcSSimon Schubert 	  symaddr = sym->value + sym->section->vma;
3705796c8dcSSimon Schubert 	  /* Relocate all non-absolute and non-TLS symbols by the
3715796c8dcSSimon Schubert 	     section offset.  */
372*ef5ccd6cSJohn Marino 	  if (sym->section != bfd_abs_section_ptr
3735796c8dcSSimon Schubert 	      && !(sym->section->flags & SEC_THREAD_LOCAL))
3745796c8dcSSimon Schubert 	    {
3755796c8dcSSimon Schubert 	      symaddr += offset;
3765796c8dcSSimon Schubert 	    }
3775796c8dcSSimon Schubert 	  /* For non-absolute symbols, use the type of the section
3785796c8dcSSimon Schubert 	     they are relative to, to intuit text/data.  Bfd provides
3795796c8dcSSimon Schubert 	     no way of figuring this out for absolute symbols.  */
380*ef5ccd6cSJohn Marino 	  if (sym->section == bfd_abs_section_ptr)
3815796c8dcSSimon Schubert 	    {
3825796c8dcSSimon Schubert 	      /* This is a hack to get the minimal symbol type
3835796c8dcSSimon Schubert 		 right for Irix 5, which has absolute addresses
3845796c8dcSSimon Schubert 		 with special section indices for dynamic symbols.
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert 		 NOTE: uweigand-20071112: Synthetic symbols do not
3875796c8dcSSimon Schubert 		 have an ELF-private part, so do not touch those.  */
3885796c8dcSSimon Schubert 	      unsigned int shndx = type == ST_SYNTHETIC ? 0 :
3895796c8dcSSimon Schubert 		((elf_symbol_type *) sym)->internal_elf_sym.st_shndx;
3905796c8dcSSimon Schubert 
3915796c8dcSSimon Schubert 	      switch (shndx)
3925796c8dcSSimon Schubert 		{
3935796c8dcSSimon Schubert 		case SHN_MIPS_TEXT:
3945796c8dcSSimon Schubert 		  ms_type = mst_text;
3955796c8dcSSimon Schubert 		  break;
3965796c8dcSSimon Schubert 		case SHN_MIPS_DATA:
3975796c8dcSSimon Schubert 		  ms_type = mst_data;
3985796c8dcSSimon Schubert 		  break;
3995796c8dcSSimon Schubert 		case SHN_MIPS_ACOMMON:
4005796c8dcSSimon Schubert 		  ms_type = mst_bss;
4015796c8dcSSimon Schubert 		  break;
4025796c8dcSSimon Schubert 		default:
4035796c8dcSSimon Schubert 		  ms_type = mst_abs;
4045796c8dcSSimon Schubert 		}
4055796c8dcSSimon Schubert 
4065796c8dcSSimon Schubert 	      /* If it is an Irix dynamic symbol, skip section name
4075796c8dcSSimon Schubert 		 symbols, relocate all others by section offset.  */
4085796c8dcSSimon Schubert 	      if (ms_type != mst_abs)
4095796c8dcSSimon Schubert 		{
4105796c8dcSSimon Schubert 		  if (sym->name[0] == '.')
4115796c8dcSSimon Schubert 		    continue;
4125796c8dcSSimon Schubert 		  symaddr += offset;
4135796c8dcSSimon Schubert 		}
4145796c8dcSSimon Schubert 	    }
4155796c8dcSSimon Schubert 	  else if (sym->section->flags & SEC_CODE)
4165796c8dcSSimon Schubert 	    {
417*ef5ccd6cSJohn Marino 	      if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
4185796c8dcSSimon Schubert 		{
419c50c785cSJohn Marino 		  if (sym->flags & BSF_GNU_INDIRECT_FUNCTION)
420c50c785cSJohn Marino 		    ms_type = mst_text_gnu_ifunc;
421c50c785cSJohn Marino 		  else
4225796c8dcSSimon Schubert 		    ms_type = mst_text;
4235796c8dcSSimon Schubert 		}
424a45ae5f8SJohn Marino 	      /* The BSF_SYNTHETIC check is there to omit ppc64 function
425a45ae5f8SJohn Marino 		 descriptors mistaken for static functions starting with 'L'.
426a45ae5f8SJohn Marino 		 */
427a45ae5f8SJohn Marino 	      else if ((sym->name[0] == '.' && sym->name[1] == 'L'
428a45ae5f8SJohn Marino 			&& (sym->flags & BSF_SYNTHETIC) == 0)
4295796c8dcSSimon Schubert 		       || ((sym->flags & BSF_LOCAL)
4305796c8dcSSimon Schubert 			   && sym->name[0] == '$'
4315796c8dcSSimon Schubert 			   && sym->name[1] == 'L'))
4325796c8dcSSimon Schubert 		/* Looks like a compiler-generated label.  Skip
4335796c8dcSSimon Schubert 		   it.  The assembler should be skipping these (to
4345796c8dcSSimon Schubert 		   keep executables small), but apparently with
4355796c8dcSSimon Schubert 		   gcc on the (deleted) delta m88k SVR4, it loses.
4365796c8dcSSimon Schubert 		   So to have us check too should be harmless (but
4375796c8dcSSimon Schubert 		   I encourage people to fix this in the assembler
4385796c8dcSSimon Schubert 		   instead of adding checks here).  */
4395796c8dcSSimon Schubert 		continue;
4405796c8dcSSimon Schubert 	      else
4415796c8dcSSimon Schubert 		{
4425796c8dcSSimon Schubert 		  ms_type = mst_file_text;
4435796c8dcSSimon Schubert 		}
4445796c8dcSSimon Schubert 	    }
4455796c8dcSSimon Schubert 	  else if (sym->section->flags & SEC_ALLOC)
4465796c8dcSSimon Schubert 	    {
447*ef5ccd6cSJohn Marino 	      if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
4485796c8dcSSimon Schubert 		{
4495796c8dcSSimon Schubert 		  if (sym->section->flags & SEC_LOAD)
4505796c8dcSSimon Schubert 		    {
4515796c8dcSSimon Schubert 		      ms_type = mst_data;
4525796c8dcSSimon Schubert 		    }
4535796c8dcSSimon Schubert 		  else
4545796c8dcSSimon Schubert 		    {
4555796c8dcSSimon Schubert 		      ms_type = mst_bss;
4565796c8dcSSimon Schubert 		    }
4575796c8dcSSimon Schubert 		}
4585796c8dcSSimon Schubert 	      else if (sym->flags & BSF_LOCAL)
4595796c8dcSSimon Schubert 		{
4605796c8dcSSimon Schubert 		  /* Named Local variable in a Data section.
4615796c8dcSSimon Schubert 		     Check its name for stabs-in-elf.  */
4625796c8dcSSimon Schubert 		  int special_local_sect;
463cf7f2e2dSJohn Marino 
4645796c8dcSSimon Schubert 		  if (strcmp ("Bbss.bss", sym->name) == 0)
4655796c8dcSSimon Schubert 		    special_local_sect = SECT_OFF_BSS (objfile);
4665796c8dcSSimon Schubert 		  else if (strcmp ("Ddata.data", sym->name) == 0)
4675796c8dcSSimon Schubert 		    special_local_sect = SECT_OFF_DATA (objfile);
4685796c8dcSSimon Schubert 		  else if (strcmp ("Drodata.rodata", sym->name) == 0)
4695796c8dcSSimon Schubert 		    special_local_sect = SECT_OFF_RODATA (objfile);
4705796c8dcSSimon Schubert 		  else
4715796c8dcSSimon Schubert 		    special_local_sect = -1;
4725796c8dcSSimon Schubert 		  if (special_local_sect >= 0)
4735796c8dcSSimon Schubert 		    {
4745796c8dcSSimon Schubert 		      /* Found a special local symbol.  Allocate a
4755796c8dcSSimon Schubert 			 sectinfo, if needed, and fill it in.  */
4765796c8dcSSimon Schubert 		      if (sectinfo == NULL)
4775796c8dcSSimon Schubert 			{
4785796c8dcSSimon Schubert 			  int max_index;
4795796c8dcSSimon Schubert 			  size_t size;
4805796c8dcSSimon Schubert 
4815796c8dcSSimon Schubert 			  max_index = SECT_OFF_BSS (objfile);
4825796c8dcSSimon Schubert 			  if (objfile->sect_index_data > max_index)
4835796c8dcSSimon Schubert 			    max_index = objfile->sect_index_data;
4845796c8dcSSimon Schubert 			  if (objfile->sect_index_rodata > max_index)
4855796c8dcSSimon Schubert 			    max_index = objfile->sect_index_rodata;
4865796c8dcSSimon Schubert 
4875796c8dcSSimon Schubert 			  /* max_index is the largest index we'll
4885796c8dcSSimon Schubert 			     use into this array, so we must
4895796c8dcSSimon Schubert 			     allocate max_index+1 elements for it.
4905796c8dcSSimon Schubert 			     However, 'struct stab_section_info'
4915796c8dcSSimon Schubert 			     already includes one element, so we
4925796c8dcSSimon Schubert 			     need to allocate max_index aadditional
4935796c8dcSSimon Schubert 			     elements.  */
4945796c8dcSSimon Schubert 			  size = (sizeof (struct stab_section_info)
495c50c785cSJohn Marino 				  + (sizeof (CORE_ADDR) * max_index));
4965796c8dcSSimon Schubert 			  sectinfo = (struct stab_section_info *)
4975796c8dcSSimon Schubert 			    xmalloc (size);
4985796c8dcSSimon Schubert 			  memset (sectinfo, 0, size);
4995796c8dcSSimon Schubert 			  sectinfo->num_sections = max_index;
5005796c8dcSSimon Schubert 			  if (filesym == NULL)
5015796c8dcSSimon Schubert 			    {
5025796c8dcSSimon Schubert 			      complaint (&symfile_complaints,
503c50c785cSJohn Marino 					 _("elf/stab section information %s "
504c50c785cSJohn Marino 					   "without a preceding file symbol"),
5055796c8dcSSimon Schubert 					 sym->name);
5065796c8dcSSimon Schubert 			    }
5075796c8dcSSimon Schubert 			  else
5085796c8dcSSimon Schubert 			    {
5095796c8dcSSimon Schubert 			      sectinfo->filename =
5105796c8dcSSimon Schubert 				(char *) filesym->name;
5115796c8dcSSimon Schubert 			    }
5125796c8dcSSimon Schubert 			}
5135796c8dcSSimon Schubert 		      if (sectinfo->sections[special_local_sect] != 0)
5145796c8dcSSimon Schubert 			complaint (&symfile_complaints,
515c50c785cSJohn Marino 				   _("duplicated elf/stab section "
516c50c785cSJohn Marino 				     "information for %s"),
5175796c8dcSSimon Schubert 				   sectinfo->filename);
5185796c8dcSSimon Schubert 		      /* BFD symbols are section relative.  */
5195796c8dcSSimon Schubert 		      symaddr = sym->value + sym->section->vma;
5205796c8dcSSimon Schubert 		      /* Relocate non-absolute symbols by the
5215796c8dcSSimon Schubert 			 section offset.  */
522*ef5ccd6cSJohn Marino 		      if (sym->section != bfd_abs_section_ptr)
5235796c8dcSSimon Schubert 			symaddr += offset;
5245796c8dcSSimon Schubert 		      sectinfo->sections[special_local_sect] = symaddr;
5255796c8dcSSimon Schubert 		      /* The special local symbols don't go in the
5265796c8dcSSimon Schubert 			 minimal symbol table, so ignore this one.  */
5275796c8dcSSimon Schubert 		      continue;
5285796c8dcSSimon Schubert 		    }
5295796c8dcSSimon Schubert 		  /* Not a special stabs-in-elf symbol, do regular
5305796c8dcSSimon Schubert 		     symbol processing.  */
5315796c8dcSSimon Schubert 		  if (sym->section->flags & SEC_LOAD)
5325796c8dcSSimon Schubert 		    {
5335796c8dcSSimon Schubert 		      ms_type = mst_file_data;
5345796c8dcSSimon Schubert 		    }
5355796c8dcSSimon Schubert 		  else
5365796c8dcSSimon Schubert 		    {
5375796c8dcSSimon Schubert 		      ms_type = mst_file_bss;
5385796c8dcSSimon Schubert 		    }
5395796c8dcSSimon Schubert 		}
5405796c8dcSSimon Schubert 	      else
5415796c8dcSSimon Schubert 		{
5425796c8dcSSimon Schubert 		  ms_type = mst_unknown;
5435796c8dcSSimon Schubert 		}
5445796c8dcSSimon Schubert 	    }
5455796c8dcSSimon Schubert 	  else
5465796c8dcSSimon Schubert 	    {
5475796c8dcSSimon Schubert 	      /* FIXME:  Solaris2 shared libraries include lots of
5485796c8dcSSimon Schubert 		 odd "absolute" and "undefined" symbols, that play
5495796c8dcSSimon Schubert 		 hob with actions like finding what function the PC
5505796c8dcSSimon Schubert 		 is in.  Ignore them if they aren't text, data, or bss.  */
5515796c8dcSSimon Schubert 	      /* ms_type = mst_unknown; */
5525796c8dcSSimon Schubert 	      continue;	/* Skip this symbol.  */
5535796c8dcSSimon Schubert 	    }
5545796c8dcSSimon Schubert 	  msym = record_minimal_symbol
555cf7f2e2dSJohn Marino 	    (sym->name, strlen (sym->name), copy_names, symaddr,
5565796c8dcSSimon Schubert 	     ms_type, sym->section, objfile);
5575796c8dcSSimon Schubert 
5585796c8dcSSimon Schubert 	  if (msym)
5595796c8dcSSimon Schubert 	    {
5605796c8dcSSimon Schubert 	      /* NOTE: uweigand-20071112: A synthetic symbol does not have an
561*ef5ccd6cSJohn Marino 		 ELF-private part.  */
5625796c8dcSSimon Schubert 	      if (type != ST_SYNTHETIC)
563*ef5ccd6cSJohn Marino 		{
564*ef5ccd6cSJohn Marino 		  /* Pass symbol size field in via BFD.  FIXME!!!  */
565*ef5ccd6cSJohn Marino 		  elf_symbol_type *elf_sym = (elf_symbol_type *) sym;
566*ef5ccd6cSJohn Marino 		  SET_MSYMBOL_SIZE (msym, elf_sym->internal_elf_sym.st_size);
567*ef5ccd6cSJohn Marino 		}
568cf7f2e2dSJohn Marino 
5695796c8dcSSimon Schubert 	      msym->filename = filesymname;
5705796c8dcSSimon Schubert 	      gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
571cf7f2e2dSJohn Marino 	    }
5725796c8dcSSimon Schubert 
5735796c8dcSSimon Schubert 	  /* For @plt symbols, also record a trampoline to the
5745796c8dcSSimon Schubert 	     destination symbol.  The @plt symbol will be used in
5755796c8dcSSimon Schubert 	     disassembly, and the trampoline will be used when we are
5765796c8dcSSimon Schubert 	     trying to find the target.  */
5775796c8dcSSimon Schubert 	  if (msym && ms_type == mst_text && type == ST_SYNTHETIC)
5785796c8dcSSimon Schubert 	    {
5795796c8dcSSimon Schubert 	      int len = strlen (sym->name);
5805796c8dcSSimon Schubert 
5815796c8dcSSimon Schubert 	      if (len > 4 && strcmp (sym->name + len - 4, "@plt") == 0)
5825796c8dcSSimon Schubert 		{
5835796c8dcSSimon Schubert 		  struct minimal_symbol *mtramp;
5845796c8dcSSimon Schubert 
585cf7f2e2dSJohn Marino 		  mtramp = record_minimal_symbol (sym->name, len - 4, 1,
586cf7f2e2dSJohn Marino 						  symaddr,
5875796c8dcSSimon Schubert 						  mst_solib_trampoline,
5885796c8dcSSimon Schubert 						  sym->section, objfile);
5895796c8dcSSimon Schubert 		  if (mtramp)
5905796c8dcSSimon Schubert 		    {
591*ef5ccd6cSJohn Marino 		      SET_MSYMBOL_SIZE (mtramp, MSYMBOL_SIZE (msym));
592*ef5ccd6cSJohn Marino 		      mtramp->created_by_gdb = 1;
5935796c8dcSSimon Schubert 		      mtramp->filename = filesymname;
5945796c8dcSSimon Schubert 		      gdbarch_elf_make_msymbol_special (gdbarch, sym, mtramp);
5955796c8dcSSimon Schubert 		    }
5965796c8dcSSimon Schubert 		}
5975796c8dcSSimon Schubert 	    }
5985796c8dcSSimon Schubert 	}
5995796c8dcSSimon Schubert     }
6005796c8dcSSimon Schubert }
6015796c8dcSSimon Schubert 
602c50c785cSJohn Marino /* Build minimal symbols named `function@got.plt' (see SYMBOL_GOT_PLT_SUFFIX)
603c50c785cSJohn Marino    for later look ups of which function to call when user requests
604c50c785cSJohn Marino    a STT_GNU_IFUNC function.  As the STT_GNU_IFUNC type is found at the target
605c50c785cSJohn Marino    library defining `function' we cannot yet know while reading OBJFILE which
606c50c785cSJohn Marino    of the SYMBOL_GOT_PLT_SUFFIX entries will be needed and later
607c50c785cSJohn Marino    DYN_SYMBOL_TABLE is no longer easily available for OBJFILE.  */
608c50c785cSJohn Marino 
609c50c785cSJohn Marino static void
elf_rel_plt_read(struct objfile * objfile,asymbol ** dyn_symbol_table)610c50c785cSJohn Marino elf_rel_plt_read (struct objfile *objfile, asymbol **dyn_symbol_table)
611c50c785cSJohn Marino {
612c50c785cSJohn Marino   bfd *obfd = objfile->obfd;
613c50c785cSJohn Marino   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
614c50c785cSJohn Marino   asection *plt, *relplt, *got_plt;
615c50c785cSJohn Marino   int plt_elf_idx;
616c50c785cSJohn Marino   bfd_size_type reloc_count, reloc;
617c50c785cSJohn Marino   char *string_buffer = NULL;
618c50c785cSJohn Marino   size_t string_buffer_size = 0;
619c50c785cSJohn Marino   struct cleanup *back_to;
620c50c785cSJohn Marino   struct gdbarch *gdbarch = objfile->gdbarch;
621c50c785cSJohn Marino   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
622c50c785cSJohn Marino   size_t ptr_size = TYPE_LENGTH (ptr_type);
623c50c785cSJohn Marino 
624c50c785cSJohn Marino   if (objfile->separate_debug_objfile_backlink)
625c50c785cSJohn Marino     return;
626c50c785cSJohn Marino 
627c50c785cSJohn Marino   plt = bfd_get_section_by_name (obfd, ".plt");
628c50c785cSJohn Marino   if (plt == NULL)
629c50c785cSJohn Marino     return;
630c50c785cSJohn Marino   plt_elf_idx = elf_section_data (plt)->this_idx;
631c50c785cSJohn Marino 
632c50c785cSJohn Marino   got_plt = bfd_get_section_by_name (obfd, ".got.plt");
633c50c785cSJohn Marino   if (got_plt == NULL)
634c50c785cSJohn Marino     return;
635c50c785cSJohn Marino 
636c50c785cSJohn Marino   /* This search algorithm is from _bfd_elf_canonicalize_dynamic_reloc.  */
637c50c785cSJohn Marino   for (relplt = obfd->sections; relplt != NULL; relplt = relplt->next)
638c50c785cSJohn Marino     if (elf_section_data (relplt)->this_hdr.sh_info == plt_elf_idx
639c50c785cSJohn Marino 	&& (elf_section_data (relplt)->this_hdr.sh_type == SHT_REL
640c50c785cSJohn Marino 	    || elf_section_data (relplt)->this_hdr.sh_type == SHT_RELA))
641c50c785cSJohn Marino       break;
642c50c785cSJohn Marino   if (relplt == NULL)
643c50c785cSJohn Marino     return;
644c50c785cSJohn Marino 
645c50c785cSJohn Marino   if (! bed->s->slurp_reloc_table (obfd, relplt, dyn_symbol_table, TRUE))
646c50c785cSJohn Marino     return;
647c50c785cSJohn Marino 
648c50c785cSJohn Marino   back_to = make_cleanup (free_current_contents, &string_buffer);
649c50c785cSJohn Marino 
650c50c785cSJohn Marino   reloc_count = relplt->size / elf_section_data (relplt)->this_hdr.sh_entsize;
651c50c785cSJohn Marino   for (reloc = 0; reloc < reloc_count; reloc++)
652c50c785cSJohn Marino     {
653*ef5ccd6cSJohn Marino       const char *name;
654c50c785cSJohn Marino       struct minimal_symbol *msym;
655c50c785cSJohn Marino       CORE_ADDR address;
656c50c785cSJohn Marino       const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
657c50c785cSJohn Marino       size_t name_len;
658c50c785cSJohn Marino 
659c50c785cSJohn Marino       name = bfd_asymbol_name (*relplt->relocation[reloc].sym_ptr_ptr);
660c50c785cSJohn Marino       name_len = strlen (name);
661c50c785cSJohn Marino       address = relplt->relocation[reloc].address;
662c50c785cSJohn Marino 
663c50c785cSJohn Marino       /* Does the pointer reside in the .got.plt section?  */
664c50c785cSJohn Marino       if (!(bfd_get_section_vma (obfd, got_plt) <= address
665c50c785cSJohn Marino             && address < bfd_get_section_vma (obfd, got_plt)
666c50c785cSJohn Marino 			 + bfd_get_section_size (got_plt)))
667c50c785cSJohn Marino 	continue;
668c50c785cSJohn Marino 
669c50c785cSJohn Marino       /* We cannot check if NAME is a reference to mst_text_gnu_ifunc as in
670c50c785cSJohn Marino 	 OBJFILE the symbol is undefined and the objfile having NAME defined
671c50c785cSJohn Marino 	 may not yet have been loaded.  */
672c50c785cSJohn Marino 
673a45ae5f8SJohn Marino       if (string_buffer_size < name_len + got_suffix_len + 1)
674c50c785cSJohn Marino 	{
675c50c785cSJohn Marino 	  string_buffer_size = 2 * (name_len + got_suffix_len);
676c50c785cSJohn Marino 	  string_buffer = xrealloc (string_buffer, string_buffer_size);
677c50c785cSJohn Marino 	}
678c50c785cSJohn Marino       memcpy (string_buffer, name, name_len);
679c50c785cSJohn Marino       memcpy (&string_buffer[name_len], SYMBOL_GOT_PLT_SUFFIX,
680a45ae5f8SJohn Marino 	      got_suffix_len + 1);
681c50c785cSJohn Marino 
682c50c785cSJohn Marino       msym = record_minimal_symbol (string_buffer, name_len + got_suffix_len,
683c50c785cSJohn Marino                                     1, address, mst_slot_got_plt, got_plt,
684c50c785cSJohn Marino 				    objfile);
685c50c785cSJohn Marino       if (msym)
686*ef5ccd6cSJohn Marino 	SET_MSYMBOL_SIZE (msym, ptr_size);
687c50c785cSJohn Marino     }
688c50c785cSJohn Marino 
689c50c785cSJohn Marino   do_cleanups (back_to);
690c50c785cSJohn Marino }
691c50c785cSJohn Marino 
692c50c785cSJohn Marino /* The data pointer is htab_t for gnu_ifunc_record_cache_unchecked.  */
693c50c785cSJohn Marino 
694c50c785cSJohn Marino static const struct objfile_data *elf_objfile_gnu_ifunc_cache_data;
695c50c785cSJohn Marino 
696c50c785cSJohn Marino /* Map function names to CORE_ADDR in elf_objfile_gnu_ifunc_cache_data.  */
697c50c785cSJohn Marino 
698c50c785cSJohn Marino struct elf_gnu_ifunc_cache
699c50c785cSJohn Marino {
700c50c785cSJohn Marino   /* This is always a function entry address, not a function descriptor.  */
701c50c785cSJohn Marino   CORE_ADDR addr;
702c50c785cSJohn Marino 
703c50c785cSJohn Marino   char name[1];
704c50c785cSJohn Marino };
705c50c785cSJohn Marino 
706c50c785cSJohn Marino /* htab_hash for elf_objfile_gnu_ifunc_cache_data.  */
707c50c785cSJohn Marino 
708c50c785cSJohn Marino static hashval_t
elf_gnu_ifunc_cache_hash(const void * a_voidp)709c50c785cSJohn Marino elf_gnu_ifunc_cache_hash (const void *a_voidp)
710c50c785cSJohn Marino {
711c50c785cSJohn Marino   const struct elf_gnu_ifunc_cache *a = a_voidp;
712c50c785cSJohn Marino 
713c50c785cSJohn Marino   return htab_hash_string (a->name);
714c50c785cSJohn Marino }
715c50c785cSJohn Marino 
716c50c785cSJohn Marino /* htab_eq for elf_objfile_gnu_ifunc_cache_data.  */
717c50c785cSJohn Marino 
718c50c785cSJohn Marino static int
elf_gnu_ifunc_cache_eq(const void * a_voidp,const void * b_voidp)719c50c785cSJohn Marino elf_gnu_ifunc_cache_eq (const void *a_voidp, const void *b_voidp)
720c50c785cSJohn Marino {
721c50c785cSJohn Marino   const struct elf_gnu_ifunc_cache *a = a_voidp;
722c50c785cSJohn Marino   const struct elf_gnu_ifunc_cache *b = b_voidp;
723c50c785cSJohn Marino 
724c50c785cSJohn Marino   return strcmp (a->name, b->name) == 0;
725c50c785cSJohn Marino }
726c50c785cSJohn Marino 
727c50c785cSJohn Marino /* Record the target function address of a STT_GNU_IFUNC function NAME is the
728c50c785cSJohn Marino    function entry address ADDR.  Return 1 if NAME and ADDR are considered as
729c50c785cSJohn Marino    valid and therefore they were successfully recorded, return 0 otherwise.
730c50c785cSJohn Marino 
731c50c785cSJohn Marino    Function does not expect a duplicate entry.  Use
732c50c785cSJohn Marino    elf_gnu_ifunc_resolve_by_cache first to check if the entry for NAME already
733c50c785cSJohn Marino    exists.  */
734c50c785cSJohn Marino 
735c50c785cSJohn Marino static int
elf_gnu_ifunc_record_cache(const char * name,CORE_ADDR addr)736c50c785cSJohn Marino elf_gnu_ifunc_record_cache (const char *name, CORE_ADDR addr)
737c50c785cSJohn Marino {
738c50c785cSJohn Marino   struct minimal_symbol *msym;
739c50c785cSJohn Marino   asection *sect;
740c50c785cSJohn Marino   struct objfile *objfile;
741c50c785cSJohn Marino   htab_t htab;
742c50c785cSJohn Marino   struct elf_gnu_ifunc_cache entry_local, *entry_p;
743c50c785cSJohn Marino   void **slot;
744c50c785cSJohn Marino 
745c50c785cSJohn Marino   msym = lookup_minimal_symbol_by_pc (addr);
746c50c785cSJohn Marino   if (msym == NULL)
747c50c785cSJohn Marino     return 0;
748c50c785cSJohn Marino   if (SYMBOL_VALUE_ADDRESS (msym) != addr)
749c50c785cSJohn Marino     return 0;
750c50c785cSJohn Marino   /* minimal symbols have always SYMBOL_OBJ_SECTION non-NULL.  */
751c50c785cSJohn Marino   sect = SYMBOL_OBJ_SECTION (msym)->the_bfd_section;
752c50c785cSJohn Marino   objfile = SYMBOL_OBJ_SECTION (msym)->objfile;
753c50c785cSJohn Marino 
754c50c785cSJohn Marino   /* If .plt jumps back to .plt the symbol is still deferred for later
755c50c785cSJohn Marino      resolution and it has no use for GDB.  Besides ".text" this symbol can
756c50c785cSJohn Marino      reside also in ".opd" for ppc64 function descriptor.  */
757c50c785cSJohn Marino   if (strcmp (bfd_get_section_name (objfile->obfd, sect), ".plt") == 0)
758c50c785cSJohn Marino     return 0;
759c50c785cSJohn Marino 
760c50c785cSJohn Marino   htab = objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data);
761c50c785cSJohn Marino   if (htab == NULL)
762c50c785cSJohn Marino     {
763c50c785cSJohn Marino       htab = htab_create_alloc_ex (1, elf_gnu_ifunc_cache_hash,
764c50c785cSJohn Marino 				   elf_gnu_ifunc_cache_eq,
765c50c785cSJohn Marino 				   NULL, &objfile->objfile_obstack,
766c50c785cSJohn Marino 				   hashtab_obstack_allocate,
767c50c785cSJohn Marino 				   dummy_obstack_deallocate);
768c50c785cSJohn Marino       set_objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data, htab);
769c50c785cSJohn Marino     }
770c50c785cSJohn Marino 
771c50c785cSJohn Marino   entry_local.addr = addr;
772c50c785cSJohn Marino   obstack_grow (&objfile->objfile_obstack, &entry_local,
773c50c785cSJohn Marino 		offsetof (struct elf_gnu_ifunc_cache, name));
774c50c785cSJohn Marino   obstack_grow_str0 (&objfile->objfile_obstack, name);
775c50c785cSJohn Marino   entry_p = obstack_finish (&objfile->objfile_obstack);
776c50c785cSJohn Marino 
777c50c785cSJohn Marino   slot = htab_find_slot (htab, entry_p, INSERT);
778c50c785cSJohn Marino   if (*slot != NULL)
779c50c785cSJohn Marino     {
780c50c785cSJohn Marino       struct elf_gnu_ifunc_cache *entry_found_p = *slot;
781c50c785cSJohn Marino       struct gdbarch *gdbarch = objfile->gdbarch;
782c50c785cSJohn Marino 
783c50c785cSJohn Marino       if (entry_found_p->addr != addr)
784c50c785cSJohn Marino 	{
785c50c785cSJohn Marino 	  /* This case indicates buggy inferior program, the resolved address
786c50c785cSJohn Marino 	     should never change.  */
787c50c785cSJohn Marino 
788c50c785cSJohn Marino 	    warning (_("gnu-indirect-function \"%s\" has changed its resolved "
789c50c785cSJohn Marino 		       "function_address from %s to %s"),
790c50c785cSJohn Marino 		     name, paddress (gdbarch, entry_found_p->addr),
791c50c785cSJohn Marino 		     paddress (gdbarch, addr));
792c50c785cSJohn Marino 	}
793c50c785cSJohn Marino 
794c50c785cSJohn Marino       /* New ENTRY_P is here leaked/duplicate in the OBJFILE obstack.  */
795c50c785cSJohn Marino     }
796c50c785cSJohn Marino   *slot = entry_p;
797c50c785cSJohn Marino 
798c50c785cSJohn Marino   return 1;
799c50c785cSJohn Marino }
800c50c785cSJohn Marino 
801c50c785cSJohn Marino /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
802c50c785cSJohn Marino    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
803c50c785cSJohn Marino    is not NULL) and the function returns 1.  It returns 0 otherwise.
804c50c785cSJohn Marino 
805c50c785cSJohn Marino    Only the elf_objfile_gnu_ifunc_cache_data hash table is searched by this
806c50c785cSJohn Marino    function.  */
807c50c785cSJohn Marino 
808c50c785cSJohn Marino static int
elf_gnu_ifunc_resolve_by_cache(const char * name,CORE_ADDR * addr_p)809c50c785cSJohn Marino elf_gnu_ifunc_resolve_by_cache (const char *name, CORE_ADDR *addr_p)
810c50c785cSJohn Marino {
811c50c785cSJohn Marino   struct objfile *objfile;
812c50c785cSJohn Marino 
813c50c785cSJohn Marino   ALL_PSPACE_OBJFILES (current_program_space, objfile)
814c50c785cSJohn Marino     {
815c50c785cSJohn Marino       htab_t htab;
816c50c785cSJohn Marino       struct elf_gnu_ifunc_cache *entry_p;
817c50c785cSJohn Marino       void **slot;
818c50c785cSJohn Marino 
819c50c785cSJohn Marino       htab = objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data);
820c50c785cSJohn Marino       if (htab == NULL)
821c50c785cSJohn Marino 	continue;
822c50c785cSJohn Marino 
823c50c785cSJohn Marino       entry_p = alloca (sizeof (*entry_p) + strlen (name));
824c50c785cSJohn Marino       strcpy (entry_p->name, name);
825c50c785cSJohn Marino 
826c50c785cSJohn Marino       slot = htab_find_slot (htab, entry_p, NO_INSERT);
827c50c785cSJohn Marino       if (slot == NULL)
828c50c785cSJohn Marino 	continue;
829c50c785cSJohn Marino       entry_p = *slot;
830c50c785cSJohn Marino       gdb_assert (entry_p != NULL);
831c50c785cSJohn Marino 
832c50c785cSJohn Marino       if (addr_p)
833c50c785cSJohn Marino 	*addr_p = entry_p->addr;
834c50c785cSJohn Marino       return 1;
835c50c785cSJohn Marino     }
836c50c785cSJohn Marino 
837c50c785cSJohn Marino   return 0;
838c50c785cSJohn Marino }
839c50c785cSJohn Marino 
840c50c785cSJohn Marino /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
841c50c785cSJohn Marino    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
842c50c785cSJohn Marino    is not NULL) and the function returns 1.  It returns 0 otherwise.
843c50c785cSJohn Marino 
844c50c785cSJohn Marino    Only the SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.
845c50c785cSJohn Marino    elf_gnu_ifunc_resolve_by_cache must have been already called for NAME to
846c50c785cSJohn Marino    prevent cache entries duplicates.  */
847c50c785cSJohn Marino 
848c50c785cSJohn Marino static int
elf_gnu_ifunc_resolve_by_got(const char * name,CORE_ADDR * addr_p)849c50c785cSJohn Marino elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
850c50c785cSJohn Marino {
851c50c785cSJohn Marino   char *name_got_plt;
852c50c785cSJohn Marino   struct objfile *objfile;
853c50c785cSJohn Marino   const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
854c50c785cSJohn Marino 
855c50c785cSJohn Marino   name_got_plt = alloca (strlen (name) + got_suffix_len + 1);
856c50c785cSJohn Marino   sprintf (name_got_plt, "%s" SYMBOL_GOT_PLT_SUFFIX, name);
857c50c785cSJohn Marino 
858c50c785cSJohn Marino   ALL_PSPACE_OBJFILES (current_program_space, objfile)
859c50c785cSJohn Marino     {
860c50c785cSJohn Marino       bfd *obfd = objfile->obfd;
861c50c785cSJohn Marino       struct gdbarch *gdbarch = objfile->gdbarch;
862c50c785cSJohn Marino       struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
863c50c785cSJohn Marino       size_t ptr_size = TYPE_LENGTH (ptr_type);
864c50c785cSJohn Marino       CORE_ADDR pointer_address, addr;
865c50c785cSJohn Marino       asection *plt;
866c50c785cSJohn Marino       gdb_byte *buf = alloca (ptr_size);
867c50c785cSJohn Marino       struct minimal_symbol *msym;
868c50c785cSJohn Marino 
869c50c785cSJohn Marino       msym = lookup_minimal_symbol (name_got_plt, NULL, objfile);
870c50c785cSJohn Marino       if (msym == NULL)
871c50c785cSJohn Marino 	continue;
872c50c785cSJohn Marino       if (MSYMBOL_TYPE (msym) != mst_slot_got_plt)
873c50c785cSJohn Marino 	continue;
874c50c785cSJohn Marino       pointer_address = SYMBOL_VALUE_ADDRESS (msym);
875c50c785cSJohn Marino 
876c50c785cSJohn Marino       plt = bfd_get_section_by_name (obfd, ".plt");
877c50c785cSJohn Marino       if (plt == NULL)
878c50c785cSJohn Marino 	continue;
879c50c785cSJohn Marino 
880c50c785cSJohn Marino       if (MSYMBOL_SIZE (msym) != ptr_size)
881c50c785cSJohn Marino 	continue;
882c50c785cSJohn Marino       if (target_read_memory (pointer_address, buf, ptr_size) != 0)
883c50c785cSJohn Marino 	continue;
884c50c785cSJohn Marino       addr = extract_typed_address (buf, ptr_type);
885c50c785cSJohn Marino       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
886c50c785cSJohn Marino 						 &current_target);
887c50c785cSJohn Marino 
888c50c785cSJohn Marino       if (addr_p)
889c50c785cSJohn Marino 	*addr_p = addr;
890c50c785cSJohn Marino       if (elf_gnu_ifunc_record_cache (name, addr))
891c50c785cSJohn Marino 	return 1;
892c50c785cSJohn Marino     }
893c50c785cSJohn Marino 
894c50c785cSJohn Marino   return 0;
895c50c785cSJohn Marino }
896c50c785cSJohn Marino 
897c50c785cSJohn Marino /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
898c50c785cSJohn Marino    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
899c50c785cSJohn Marino    is not NULL) and the function returns 1.  It returns 0 otherwise.
900c50c785cSJohn Marino 
901c50c785cSJohn Marino    Both the elf_objfile_gnu_ifunc_cache_data hash table and
902c50c785cSJohn Marino    SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.  */
903c50c785cSJohn Marino 
904c50c785cSJohn Marino static int
elf_gnu_ifunc_resolve_name(const char * name,CORE_ADDR * addr_p)905c50c785cSJohn Marino elf_gnu_ifunc_resolve_name (const char *name, CORE_ADDR *addr_p)
906c50c785cSJohn Marino {
907c50c785cSJohn Marino   if (elf_gnu_ifunc_resolve_by_cache (name, addr_p))
908c50c785cSJohn Marino     return 1;
909c50c785cSJohn Marino 
910c50c785cSJohn Marino   if (elf_gnu_ifunc_resolve_by_got (name, addr_p))
911c50c785cSJohn Marino     return 1;
912c50c785cSJohn Marino 
913c50c785cSJohn Marino   return 0;
914c50c785cSJohn Marino }
915c50c785cSJohn Marino 
916c50c785cSJohn Marino /* Call STT_GNU_IFUNC - a function returning addresss of a real function to
917c50c785cSJohn Marino    call.  PC is theSTT_GNU_IFUNC resolving function entry.  The value returned
918c50c785cSJohn Marino    is the entry point of the resolved STT_GNU_IFUNC target function to call.
919c50c785cSJohn Marino    */
920c50c785cSJohn Marino 
921c50c785cSJohn Marino static CORE_ADDR
elf_gnu_ifunc_resolve_addr(struct gdbarch * gdbarch,CORE_ADDR pc)922c50c785cSJohn Marino elf_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
923c50c785cSJohn Marino {
924*ef5ccd6cSJohn Marino   const char *name_at_pc;
925c50c785cSJohn Marino   CORE_ADDR start_at_pc, address;
926c50c785cSJohn Marino   struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
927c50c785cSJohn Marino   struct value *function, *address_val;
928c50c785cSJohn Marino 
929c50c785cSJohn Marino   /* Try first any non-intrusive methods without an inferior call.  */
930c50c785cSJohn Marino 
931c50c785cSJohn Marino   if (find_pc_partial_function (pc, &name_at_pc, &start_at_pc, NULL)
932c50c785cSJohn Marino       && start_at_pc == pc)
933c50c785cSJohn Marino     {
934c50c785cSJohn Marino       if (elf_gnu_ifunc_resolve_name (name_at_pc, &address))
935c50c785cSJohn Marino 	return address;
936c50c785cSJohn Marino     }
937c50c785cSJohn Marino   else
938c50c785cSJohn Marino     name_at_pc = NULL;
939c50c785cSJohn Marino 
940c50c785cSJohn Marino   function = allocate_value (func_func_type);
941c50c785cSJohn Marino   set_value_address (function, pc);
942c50c785cSJohn Marino 
943c50c785cSJohn Marino   /* STT_GNU_IFUNC resolver functions have no parameters.  FUNCTION is the
944c50c785cSJohn Marino      function entry address.  ADDRESS may be a function descriptor.  */
945c50c785cSJohn Marino 
946c50c785cSJohn Marino   address_val = call_function_by_hand (function, 0, NULL);
947c50c785cSJohn Marino   address = value_as_address (address_val);
948c50c785cSJohn Marino   address = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
949c50c785cSJohn Marino 						&current_target);
950c50c785cSJohn Marino 
951c50c785cSJohn Marino   if (name_at_pc)
952c50c785cSJohn Marino     elf_gnu_ifunc_record_cache (name_at_pc, address);
953c50c785cSJohn Marino 
954c50c785cSJohn Marino   return address;
955c50c785cSJohn Marino }
956c50c785cSJohn Marino 
957c50c785cSJohn Marino /* Handle inferior hit of bp_gnu_ifunc_resolver, see its definition.  */
958c50c785cSJohn Marino 
959c50c785cSJohn Marino static void
elf_gnu_ifunc_resolver_stop(struct breakpoint * b)960c50c785cSJohn Marino elf_gnu_ifunc_resolver_stop (struct breakpoint *b)
961c50c785cSJohn Marino {
962c50c785cSJohn Marino   struct breakpoint *b_return;
963c50c785cSJohn Marino   struct frame_info *prev_frame = get_prev_frame (get_current_frame ());
964c50c785cSJohn Marino   struct frame_id prev_frame_id = get_stack_frame_id (prev_frame);
965c50c785cSJohn Marino   CORE_ADDR prev_pc = get_frame_pc (prev_frame);
966c50c785cSJohn Marino   int thread_id = pid_to_thread_id (inferior_ptid);
967c50c785cSJohn Marino 
968c50c785cSJohn Marino   gdb_assert (b->type == bp_gnu_ifunc_resolver);
969c50c785cSJohn Marino 
970c50c785cSJohn Marino   for (b_return = b->related_breakpoint; b_return != b;
971c50c785cSJohn Marino        b_return = b_return->related_breakpoint)
972c50c785cSJohn Marino     {
973c50c785cSJohn Marino       gdb_assert (b_return->type == bp_gnu_ifunc_resolver_return);
974c50c785cSJohn Marino       gdb_assert (b_return->loc != NULL && b_return->loc->next == NULL);
975c50c785cSJohn Marino       gdb_assert (frame_id_p (b_return->frame_id));
976c50c785cSJohn Marino 
977c50c785cSJohn Marino       if (b_return->thread == thread_id
978c50c785cSJohn Marino 	  && b_return->loc->requested_address == prev_pc
979c50c785cSJohn Marino 	  && frame_id_eq (b_return->frame_id, prev_frame_id))
980c50c785cSJohn Marino 	break;
981c50c785cSJohn Marino     }
982c50c785cSJohn Marino 
983c50c785cSJohn Marino   if (b_return == b)
984c50c785cSJohn Marino     {
985c50c785cSJohn Marino       struct symtab_and_line sal;
986c50c785cSJohn Marino 
987c50c785cSJohn Marino       /* No need to call find_pc_line for symbols resolving as this is only
988c50c785cSJohn Marino 	 a helper breakpointer never shown to the user.  */
989c50c785cSJohn Marino 
990c50c785cSJohn Marino       init_sal (&sal);
991c50c785cSJohn Marino       sal.pspace = current_inferior ()->pspace;
992c50c785cSJohn Marino       sal.pc = prev_pc;
993c50c785cSJohn Marino       sal.section = find_pc_overlay (sal.pc);
994c50c785cSJohn Marino       sal.explicit_pc = 1;
995c50c785cSJohn Marino       b_return = set_momentary_breakpoint (get_frame_arch (prev_frame), sal,
996c50c785cSJohn Marino 					   prev_frame_id,
997c50c785cSJohn Marino 					   bp_gnu_ifunc_resolver_return);
998c50c785cSJohn Marino 
999*ef5ccd6cSJohn Marino       /* set_momentary_breakpoint invalidates PREV_FRAME.  */
1000*ef5ccd6cSJohn Marino       prev_frame = NULL;
1001*ef5ccd6cSJohn Marino 
1002c50c785cSJohn Marino       /* Add new b_return to the ring list b->related_breakpoint.  */
1003c50c785cSJohn Marino       gdb_assert (b_return->related_breakpoint == b_return);
1004c50c785cSJohn Marino       b_return->related_breakpoint = b->related_breakpoint;
1005c50c785cSJohn Marino       b->related_breakpoint = b_return;
1006c50c785cSJohn Marino     }
1007c50c785cSJohn Marino }
1008c50c785cSJohn Marino 
1009c50c785cSJohn Marino /* Handle inferior hit of bp_gnu_ifunc_resolver_return, see its definition.  */
1010c50c785cSJohn Marino 
1011c50c785cSJohn Marino static void
elf_gnu_ifunc_resolver_return_stop(struct breakpoint * b)1012c50c785cSJohn Marino elf_gnu_ifunc_resolver_return_stop (struct breakpoint *b)
1013c50c785cSJohn Marino {
1014c50c785cSJohn Marino   struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
1015c50c785cSJohn Marino   struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
1016c50c785cSJohn Marino   struct type *value_type = TYPE_TARGET_TYPE (func_func_type);
1017c50c785cSJohn Marino   struct regcache *regcache = get_thread_regcache (inferior_ptid);
1018*ef5ccd6cSJohn Marino   struct value *func_func;
1019c50c785cSJohn Marino   struct value *value;
1020c50c785cSJohn Marino   CORE_ADDR resolved_address, resolved_pc;
1021c50c785cSJohn Marino   struct symtab_and_line sal;
1022c50c785cSJohn Marino   struct symtabs_and_lines sals, sals_end;
1023c50c785cSJohn Marino 
1024c50c785cSJohn Marino   gdb_assert (b->type == bp_gnu_ifunc_resolver_return);
1025c50c785cSJohn Marino 
1026c50c785cSJohn Marino   while (b->related_breakpoint != b)
1027c50c785cSJohn Marino     {
1028c50c785cSJohn Marino       struct breakpoint *b_next = b->related_breakpoint;
1029c50c785cSJohn Marino 
1030c50c785cSJohn Marino       switch (b->type)
1031c50c785cSJohn Marino 	{
1032c50c785cSJohn Marino 	case bp_gnu_ifunc_resolver:
1033c50c785cSJohn Marino 	  break;
1034c50c785cSJohn Marino 	case bp_gnu_ifunc_resolver_return:
1035c50c785cSJohn Marino 	  delete_breakpoint (b);
1036c50c785cSJohn Marino 	  break;
1037c50c785cSJohn Marino 	default:
1038c50c785cSJohn Marino 	  internal_error (__FILE__, __LINE__,
1039c50c785cSJohn Marino 			  _("handle_inferior_event: Invalid "
1040c50c785cSJohn Marino 			    "gnu-indirect-function breakpoint type %d"),
1041c50c785cSJohn Marino 			  (int) b->type);
1042c50c785cSJohn Marino 	}
1043c50c785cSJohn Marino       b = b_next;
1044c50c785cSJohn Marino     }
1045c50c785cSJohn Marino   gdb_assert (b->type == bp_gnu_ifunc_resolver);
1046*ef5ccd6cSJohn Marino   gdb_assert (b->loc->next == NULL);
1047*ef5ccd6cSJohn Marino 
1048*ef5ccd6cSJohn Marino   func_func = allocate_value (func_func_type);
1049*ef5ccd6cSJohn Marino   set_value_address (func_func, b->loc->related_address);
1050*ef5ccd6cSJohn Marino 
1051*ef5ccd6cSJohn Marino   value = allocate_value (value_type);
1052*ef5ccd6cSJohn Marino   gdbarch_return_value (gdbarch, func_func, value_type, regcache,
1053*ef5ccd6cSJohn Marino 			value_contents_raw (value), NULL);
1054*ef5ccd6cSJohn Marino   resolved_address = value_as_address (value);
1055*ef5ccd6cSJohn Marino   resolved_pc = gdbarch_convert_from_func_ptr_addr (gdbarch,
1056*ef5ccd6cSJohn Marino 						    resolved_address,
1057*ef5ccd6cSJohn Marino 						    &current_target);
1058c50c785cSJohn Marino 
1059a45ae5f8SJohn Marino   gdb_assert (current_program_space == b->pspace || b->pspace == NULL);
1060c50c785cSJohn Marino   elf_gnu_ifunc_record_cache (b->addr_string, resolved_pc);
1061c50c785cSJohn Marino 
1062c50c785cSJohn Marino   sal = find_pc_line (resolved_pc, 0);
1063c50c785cSJohn Marino   sals.nelts = 1;
1064c50c785cSJohn Marino   sals.sals = &sal;
1065c50c785cSJohn Marino   sals_end.nelts = 0;
1066c50c785cSJohn Marino 
1067c50c785cSJohn Marino   b->type = bp_breakpoint;
1068c50c785cSJohn Marino   update_breakpoint_locations (b, sals, sals_end);
1069c50c785cSJohn Marino }
1070c50c785cSJohn Marino 
1071cf7f2e2dSJohn Marino /* Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
1072cf7f2e2dSJohn Marino 
1073*ef5ccd6cSJohn Marino static const struct elf_build_id *
build_id_bfd_get(bfd * abfd)1074cf7f2e2dSJohn Marino build_id_bfd_get (bfd *abfd)
1075cf7f2e2dSJohn Marino {
1076cf7f2e2dSJohn Marino   if (!bfd_check_format (abfd, bfd_object)
1077cf7f2e2dSJohn Marino       || bfd_get_flavour (abfd) != bfd_target_elf_flavour
1078cf7f2e2dSJohn Marino       || elf_tdata (abfd)->build_id == NULL)
1079cf7f2e2dSJohn Marino     return NULL;
1080cf7f2e2dSJohn Marino 
1081*ef5ccd6cSJohn Marino   return elf_tdata (abfd)->build_id;
1082cf7f2e2dSJohn Marino }
1083cf7f2e2dSJohn Marino 
1084cf7f2e2dSJohn Marino /* Return if FILENAME has NT_GNU_BUILD_ID matching the CHECK value.  */
1085cf7f2e2dSJohn Marino 
1086cf7f2e2dSJohn Marino static int
build_id_verify(const char * filename,const struct elf_build_id * check)1087*ef5ccd6cSJohn Marino build_id_verify (const char *filename, const struct elf_build_id *check)
1088cf7f2e2dSJohn Marino {
1089cf7f2e2dSJohn Marino   bfd *abfd;
1090*ef5ccd6cSJohn Marino   const struct elf_build_id *found;
1091cf7f2e2dSJohn Marino   int retval = 0;
1092cf7f2e2dSJohn Marino 
1093cf7f2e2dSJohn Marino   /* We expect to be silent on the non-existing files.  */
1094*ef5ccd6cSJohn Marino   abfd = gdb_bfd_open_maybe_remote (filename);
1095cf7f2e2dSJohn Marino   if (abfd == NULL)
1096cf7f2e2dSJohn Marino     return 0;
1097cf7f2e2dSJohn Marino 
1098cf7f2e2dSJohn Marino   found = build_id_bfd_get (abfd);
1099cf7f2e2dSJohn Marino 
1100cf7f2e2dSJohn Marino   if (found == NULL)
1101cf7f2e2dSJohn Marino     warning (_("File \"%s\" has no build-id, file skipped"), filename);
1102cf7f2e2dSJohn Marino   else if (found->size != check->size
1103cf7f2e2dSJohn Marino            || memcmp (found->data, check->data, found->size) != 0)
1104c50c785cSJohn Marino     warning (_("File \"%s\" has a different build-id, file skipped"),
1105c50c785cSJohn Marino 	     filename);
1106cf7f2e2dSJohn Marino   else
1107cf7f2e2dSJohn Marino     retval = 1;
1108cf7f2e2dSJohn Marino 
1109*ef5ccd6cSJohn Marino   gdb_bfd_unref (abfd);
1110cf7f2e2dSJohn Marino 
1111cf7f2e2dSJohn Marino   return retval;
1112cf7f2e2dSJohn Marino }
1113cf7f2e2dSJohn Marino 
1114cf7f2e2dSJohn Marino static char *
build_id_to_debug_filename(const struct elf_build_id * build_id)1115*ef5ccd6cSJohn Marino build_id_to_debug_filename (const struct elf_build_id *build_id)
1116cf7f2e2dSJohn Marino {
1117cf7f2e2dSJohn Marino   char *link, *debugdir, *retval = NULL;
1118*ef5ccd6cSJohn Marino   VEC (char_ptr) *debugdir_vec;
1119*ef5ccd6cSJohn Marino   struct cleanup *back_to;
1120*ef5ccd6cSJohn Marino   int ix;
1121cf7f2e2dSJohn Marino 
1122cf7f2e2dSJohn Marino   /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
1123cf7f2e2dSJohn Marino   link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
1124cf7f2e2dSJohn Marino 		 + 2 * build_id->size + (sizeof ".debug" - 1) + 1);
1125cf7f2e2dSJohn Marino 
1126cf7f2e2dSJohn Marino   /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
1127cf7f2e2dSJohn Marino      cause "/.build-id/..." lookups.  */
1128cf7f2e2dSJohn Marino 
1129*ef5ccd6cSJohn Marino   debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
1130*ef5ccd6cSJohn Marino   back_to = make_cleanup_free_char_ptr_vec (debugdir_vec);
1131*ef5ccd6cSJohn Marino 
1132*ef5ccd6cSJohn Marino   for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
1133cf7f2e2dSJohn Marino     {
1134*ef5ccd6cSJohn Marino       size_t debugdir_len = strlen (debugdir);
1135*ef5ccd6cSJohn Marino       const gdb_byte *data = build_id->data;
1136cf7f2e2dSJohn Marino       size_t size = build_id->size;
1137*ef5ccd6cSJohn Marino       char *s;
1138cf7f2e2dSJohn Marino 
1139*ef5ccd6cSJohn Marino       memcpy (link, debugdir, debugdir_len);
1140*ef5ccd6cSJohn Marino       s = &link[debugdir_len];
1141cf7f2e2dSJohn Marino       s += sprintf (s, "/.build-id/");
1142cf7f2e2dSJohn Marino       if (size > 0)
1143cf7f2e2dSJohn Marino 	{
1144cf7f2e2dSJohn Marino 	  size--;
1145cf7f2e2dSJohn Marino 	  s += sprintf (s, "%02x", (unsigned) *data++);
1146cf7f2e2dSJohn Marino 	}
1147cf7f2e2dSJohn Marino       if (size > 0)
1148cf7f2e2dSJohn Marino 	*s++ = '/';
1149cf7f2e2dSJohn Marino       while (size-- > 0)
1150cf7f2e2dSJohn Marino 	s += sprintf (s, "%02x", (unsigned) *data++);
1151cf7f2e2dSJohn Marino       strcpy (s, ".debug");
1152cf7f2e2dSJohn Marino 
1153cf7f2e2dSJohn Marino       /* lrealpath() is expensive even for the usually non-existent files.  */
1154cf7f2e2dSJohn Marino       if (access (link, F_OK) == 0)
1155cf7f2e2dSJohn Marino 	retval = lrealpath (link);
1156cf7f2e2dSJohn Marino 
1157cf7f2e2dSJohn Marino       if (retval != NULL && !build_id_verify (retval, build_id))
1158cf7f2e2dSJohn Marino 	{
1159cf7f2e2dSJohn Marino 	  xfree (retval);
1160cf7f2e2dSJohn Marino 	  retval = NULL;
1161cf7f2e2dSJohn Marino 	}
1162cf7f2e2dSJohn Marino 
1163cf7f2e2dSJohn Marino       if (retval != NULL)
1164cf7f2e2dSJohn Marino 	break;
1165cf7f2e2dSJohn Marino     }
1166cf7f2e2dSJohn Marino 
1167*ef5ccd6cSJohn Marino   do_cleanups (back_to);
1168cf7f2e2dSJohn Marino   return retval;
1169cf7f2e2dSJohn Marino }
1170cf7f2e2dSJohn Marino 
1171cf7f2e2dSJohn Marino static char *
find_separate_debug_file_by_buildid(struct objfile * objfile)1172cf7f2e2dSJohn Marino find_separate_debug_file_by_buildid (struct objfile *objfile)
1173cf7f2e2dSJohn Marino {
1174*ef5ccd6cSJohn Marino   const struct elf_build_id *build_id;
1175cf7f2e2dSJohn Marino 
1176cf7f2e2dSJohn Marino   build_id = build_id_bfd_get (objfile->obfd);
1177cf7f2e2dSJohn Marino   if (build_id != NULL)
1178cf7f2e2dSJohn Marino     {
1179cf7f2e2dSJohn Marino       char *build_id_name;
1180cf7f2e2dSJohn Marino 
1181cf7f2e2dSJohn Marino       build_id_name = build_id_to_debug_filename (build_id);
1182cf7f2e2dSJohn Marino       /* Prevent looping on a stripped .debug file.  */
1183c50c785cSJohn Marino       if (build_id_name != NULL
1184c50c785cSJohn Marino 	  && filename_cmp (build_id_name, objfile->name) == 0)
1185cf7f2e2dSJohn Marino         {
1186cf7f2e2dSJohn Marino 	  warning (_("\"%s\": separate debug info file has no debug info"),
1187cf7f2e2dSJohn Marino 		   build_id_name);
1188cf7f2e2dSJohn Marino 	  xfree (build_id_name);
1189cf7f2e2dSJohn Marino 	}
1190cf7f2e2dSJohn Marino       else if (build_id_name != NULL)
1191cf7f2e2dSJohn Marino         return build_id_name;
1192cf7f2e2dSJohn Marino     }
1193cf7f2e2dSJohn Marino   return NULL;
1194cf7f2e2dSJohn Marino }
1195cf7f2e2dSJohn Marino 
11965796c8dcSSimon Schubert /* Scan and build partial symbols for a symbol file.
11975796c8dcSSimon Schubert    We have been initialized by a call to elf_symfile_init, which
11985796c8dcSSimon Schubert    currently does nothing.
11995796c8dcSSimon Schubert 
12005796c8dcSSimon Schubert    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
12015796c8dcSSimon Schubert    in each section.  We simplify it down to a single offset for all
12025796c8dcSSimon Schubert    symbols.  FIXME.
12035796c8dcSSimon Schubert 
12045796c8dcSSimon Schubert    This function only does the minimum work necessary for letting the
12055796c8dcSSimon Schubert    user "name" things symbolically; it does not read the entire symtab.
12065796c8dcSSimon Schubert    Instead, it reads the external and static symbols and puts them in partial
12075796c8dcSSimon Schubert    symbol tables.  When more extensive information is requested of a
12085796c8dcSSimon Schubert    file, the corresponding partial symbol table is mutated into a full
12095796c8dcSSimon Schubert    fledged symbol table by going back and reading the symbols
12105796c8dcSSimon Schubert    for real.
12115796c8dcSSimon Schubert 
12125796c8dcSSimon Schubert    We look for sections with specific names, to tell us what debug
12135796c8dcSSimon Schubert    format to look for:  FIXME!!!
12145796c8dcSSimon Schubert 
12155796c8dcSSimon Schubert    elfstab_build_psymtabs() handles STABS symbols;
12165796c8dcSSimon Schubert    mdebug_build_psymtabs() handles ECOFF debugging information.
12175796c8dcSSimon Schubert 
12185796c8dcSSimon Schubert    Note that ELF files have a "minimal" symbol table, which looks a lot
12195796c8dcSSimon Schubert    like a COFF symbol table, but has only the minimal information necessary
12205796c8dcSSimon Schubert    for linking.  We process this also, and use the information to
12215796c8dcSSimon Schubert    build gdb's minimal symbol table.  This gives us some minimal debugging
12225796c8dcSSimon Schubert    capability even for files compiled without -g.  */
12235796c8dcSSimon Schubert 
12245796c8dcSSimon Schubert static void
elf_symfile_read(struct objfile * objfile,int symfile_flags)1225cf7f2e2dSJohn Marino elf_symfile_read (struct objfile *objfile, int symfile_flags)
12265796c8dcSSimon Schubert {
1227a45ae5f8SJohn Marino   bfd *synth_abfd, *abfd = objfile->obfd;
12285796c8dcSSimon Schubert   struct elfinfo ei;
12295796c8dcSSimon Schubert   struct cleanup *back_to;
12305796c8dcSSimon Schubert   long symcount = 0, dynsymcount = 0, synthcount, storage_needed;
12315796c8dcSSimon Schubert   asymbol **symbol_table = NULL, **dyn_symbol_table = NULL;
12325796c8dcSSimon Schubert   asymbol *synthsyms;
1233*ef5ccd6cSJohn Marino   struct dbx_symfile_info *dbx;
1234*ef5ccd6cSJohn Marino 
1235*ef5ccd6cSJohn Marino   if (symtab_create_debug)
1236*ef5ccd6cSJohn Marino     {
1237*ef5ccd6cSJohn Marino       fprintf_unfiltered (gdb_stdlog,
1238*ef5ccd6cSJohn Marino 			  "Reading minimal symbols of objfile %s ...\n",
1239*ef5ccd6cSJohn Marino 			  objfile->name);
1240*ef5ccd6cSJohn Marino     }
12415796c8dcSSimon Schubert 
12425796c8dcSSimon Schubert   init_minimal_symbol_collection ();
12435796c8dcSSimon Schubert   back_to = make_cleanup_discard_minimal_symbols ();
12445796c8dcSSimon Schubert 
12455796c8dcSSimon Schubert   memset ((char *) &ei, 0, sizeof (ei));
12465796c8dcSSimon Schubert 
1247c50c785cSJohn Marino   /* Allocate struct to keep track of the symfile.  */
1248*ef5ccd6cSJohn Marino   dbx = XCNEW (struct dbx_symfile_info);
1249*ef5ccd6cSJohn Marino   set_objfile_data (objfile, dbx_objfile_data_key, dbx);
12505796c8dcSSimon Schubert   make_cleanup (free_elfinfo, (void *) objfile);
12515796c8dcSSimon Schubert 
12525796c8dcSSimon Schubert   /* Process the normal ELF symbol table first.  This may write some
1253*ef5ccd6cSJohn Marino      chain of info into the dbx_symfile_info of the objfile, which can
1254*ef5ccd6cSJohn Marino      later be used by elfstab_offset_sections.  */
12555796c8dcSSimon Schubert 
12565796c8dcSSimon Schubert   storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
12575796c8dcSSimon Schubert   if (storage_needed < 0)
1258c50c785cSJohn Marino     error (_("Can't read symbols from %s: %s"),
1259c50c785cSJohn Marino 	   bfd_get_filename (objfile->obfd),
12605796c8dcSSimon Schubert 	   bfd_errmsg (bfd_get_error ()));
12615796c8dcSSimon Schubert 
12625796c8dcSSimon Schubert   if (storage_needed > 0)
12635796c8dcSSimon Schubert     {
12645796c8dcSSimon Schubert       symbol_table = (asymbol **) xmalloc (storage_needed);
12655796c8dcSSimon Schubert       make_cleanup (xfree, symbol_table);
12665796c8dcSSimon Schubert       symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
12675796c8dcSSimon Schubert 
12685796c8dcSSimon Schubert       if (symcount < 0)
1269c50c785cSJohn Marino 	error (_("Can't read symbols from %s: %s"),
1270c50c785cSJohn Marino 	       bfd_get_filename (objfile->obfd),
12715796c8dcSSimon Schubert 	       bfd_errmsg (bfd_get_error ()));
12725796c8dcSSimon Schubert 
1273cf7f2e2dSJohn Marino       elf_symtab_read (objfile, ST_REGULAR, symcount, symbol_table, 0);
12745796c8dcSSimon Schubert     }
12755796c8dcSSimon Schubert 
12765796c8dcSSimon Schubert   /* Add the dynamic symbols.  */
12775796c8dcSSimon Schubert 
12785796c8dcSSimon Schubert   storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd);
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert   if (storage_needed > 0)
12815796c8dcSSimon Schubert     {
1282c50c785cSJohn Marino       /* Memory gets permanently referenced from ABFD after
1283c50c785cSJohn Marino 	 bfd_get_synthetic_symtab so it must not get freed before ABFD gets.
1284c50c785cSJohn Marino 	 It happens only in the case when elf_slurp_reloc_table sees
1285c50c785cSJohn Marino 	 asection->relocation NULL.  Determining which section is asection is
1286c50c785cSJohn Marino 	 done by _bfd_elf_get_synthetic_symtab which is all a bfd
1287c50c785cSJohn Marino 	 implementation detail, though.  */
1288c50c785cSJohn Marino 
1289c50c785cSJohn Marino       dyn_symbol_table = bfd_alloc (abfd, storage_needed);
12905796c8dcSSimon Schubert       dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd,
12915796c8dcSSimon Schubert 						     dyn_symbol_table);
12925796c8dcSSimon Schubert 
12935796c8dcSSimon Schubert       if (dynsymcount < 0)
1294c50c785cSJohn Marino 	error (_("Can't read symbols from %s: %s"),
1295c50c785cSJohn Marino 	       bfd_get_filename (objfile->obfd),
12965796c8dcSSimon Schubert 	       bfd_errmsg (bfd_get_error ()));
12975796c8dcSSimon Schubert 
1298cf7f2e2dSJohn Marino       elf_symtab_read (objfile, ST_DYNAMIC, dynsymcount, dyn_symbol_table, 0);
1299c50c785cSJohn Marino 
1300c50c785cSJohn Marino       elf_rel_plt_read (objfile, dyn_symbol_table);
13015796c8dcSSimon Schubert     }
13025796c8dcSSimon Schubert 
1303a45ae5f8SJohn Marino   /* Contrary to binutils --strip-debug/--only-keep-debug the strip command from
1304a45ae5f8SJohn Marino      elfutils (eu-strip) moves even the .symtab section into the .debug file.
1305a45ae5f8SJohn Marino 
1306a45ae5f8SJohn Marino      bfd_get_synthetic_symtab on ppc64 for each function descriptor ELF symbol
1307a45ae5f8SJohn Marino      'name' creates a new BSF_SYNTHETIC ELF symbol '.name' with its code
1308a45ae5f8SJohn Marino      address.  But with eu-strip files bfd_get_synthetic_symtab would fail to
1309a45ae5f8SJohn Marino      read the code address from .opd while it reads the .symtab section from
1310a45ae5f8SJohn Marino      a separate debug info file as the .opd section is SHT_NOBITS there.
1311a45ae5f8SJohn Marino 
1312a45ae5f8SJohn Marino      With SYNTH_ABFD the .opd section will be read from the original
1313a45ae5f8SJohn Marino      backlinked binary where it is valid.  */
1314a45ae5f8SJohn Marino 
1315a45ae5f8SJohn Marino   if (objfile->separate_debug_objfile_backlink)
1316a45ae5f8SJohn Marino     synth_abfd = objfile->separate_debug_objfile_backlink->obfd;
1317a45ae5f8SJohn Marino   else
1318a45ae5f8SJohn Marino     synth_abfd = abfd;
1319a45ae5f8SJohn Marino 
13205796c8dcSSimon Schubert   /* Add synthetic symbols - for instance, names for any PLT entries.  */
13215796c8dcSSimon Schubert 
1322a45ae5f8SJohn Marino   synthcount = bfd_get_synthetic_symtab (synth_abfd, symcount, symbol_table,
13235796c8dcSSimon Schubert 					 dynsymcount, dyn_symbol_table,
13245796c8dcSSimon Schubert 					 &synthsyms);
13255796c8dcSSimon Schubert   if (synthcount > 0)
13265796c8dcSSimon Schubert     {
13275796c8dcSSimon Schubert       asymbol **synth_symbol_table;
13285796c8dcSSimon Schubert       long i;
13295796c8dcSSimon Schubert 
13305796c8dcSSimon Schubert       make_cleanup (xfree, synthsyms);
13315796c8dcSSimon Schubert       synth_symbol_table = xmalloc (sizeof (asymbol *) * synthcount);
13325796c8dcSSimon Schubert       for (i = 0; i < synthcount; i++)
13335796c8dcSSimon Schubert 	synth_symbol_table[i] = synthsyms + i;
13345796c8dcSSimon Schubert       make_cleanup (xfree, synth_symbol_table);
1335c50c785cSJohn Marino       elf_symtab_read (objfile, ST_SYNTHETIC, synthcount,
1336c50c785cSJohn Marino 		       synth_symbol_table, 1);
13375796c8dcSSimon Schubert     }
13385796c8dcSSimon Schubert 
13395796c8dcSSimon Schubert   /* Install any minimal symbols that have been collected as the current
13405796c8dcSSimon Schubert      minimal symbols for this objfile.  The debug readers below this point
13415796c8dcSSimon Schubert      should not generate new minimal symbols; if they do it's their
13425796c8dcSSimon Schubert      responsibility to install them.  "mdebug" appears to be the only one
13435796c8dcSSimon Schubert      which will do this.  */
13445796c8dcSSimon Schubert 
13455796c8dcSSimon Schubert   install_minimal_symbols (objfile);
13465796c8dcSSimon Schubert   do_cleanups (back_to);
13475796c8dcSSimon Schubert 
13485796c8dcSSimon Schubert   /* Now process debugging information, which is contained in
13495796c8dcSSimon Schubert      special ELF sections.  */
13505796c8dcSSimon Schubert 
13515796c8dcSSimon Schubert   /* We first have to find them...  */
13525796c8dcSSimon Schubert   bfd_map_over_sections (abfd, elf_locate_sections, (void *) & ei);
13535796c8dcSSimon Schubert 
13545796c8dcSSimon Schubert   /* ELF debugging information is inserted into the psymtab in the
13555796c8dcSSimon Schubert      order of least informative first - most informative last.  Since
13565796c8dcSSimon Schubert      the psymtab table is searched `most recent insertion first' this
13575796c8dcSSimon Schubert      increases the probability that more detailed debug information
13585796c8dcSSimon Schubert      for a section is found.
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert      For instance, an object file might contain both .mdebug (XCOFF)
13615796c8dcSSimon Schubert      and .debug_info (DWARF2) sections then .mdebug is inserted first
13625796c8dcSSimon Schubert      (searched last) and DWARF2 is inserted last (searched first).  If
13635796c8dcSSimon Schubert      we don't do this then the XCOFF info is found first - for code in
13645796c8dcSSimon Schubert      an included file XCOFF info is useless.  */
13655796c8dcSSimon Schubert 
13665796c8dcSSimon Schubert   if (ei.mdebugsect)
13675796c8dcSSimon Schubert     {
13685796c8dcSSimon Schubert       const struct ecoff_debug_swap *swap;
13695796c8dcSSimon Schubert 
13705796c8dcSSimon Schubert       /* .mdebug section, presumably holding ECOFF debugging
13715796c8dcSSimon Schubert          information.  */
13725796c8dcSSimon Schubert       swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13735796c8dcSSimon Schubert       if (swap)
13745796c8dcSSimon Schubert 	elfmdebug_build_psymtabs (objfile, swap, ei.mdebugsect);
13755796c8dcSSimon Schubert     }
13765796c8dcSSimon Schubert   if (ei.stabsect)
13775796c8dcSSimon Schubert     {
13785796c8dcSSimon Schubert       asection *str_sect;
13795796c8dcSSimon Schubert 
13805796c8dcSSimon Schubert       /* Stab sections have an associated string table that looks like
13815796c8dcSSimon Schubert          a separate section.  */
13825796c8dcSSimon Schubert       str_sect = bfd_get_section_by_name (abfd, ".stabstr");
13835796c8dcSSimon Schubert 
13845796c8dcSSimon Schubert       /* FIXME should probably warn about a stab section without a stabstr.  */
13855796c8dcSSimon Schubert       if (str_sect)
13865796c8dcSSimon Schubert 	elfstab_build_psymtabs (objfile,
13875796c8dcSSimon Schubert 				ei.stabsect,
13885796c8dcSSimon Schubert 				str_sect->filepos,
13895796c8dcSSimon Schubert 				bfd_section_size (abfd, str_sect));
13905796c8dcSSimon Schubert     }
1391c50c785cSJohn Marino 
1392*ef5ccd6cSJohn Marino   if (symtab_create_debug)
1393*ef5ccd6cSJohn Marino     fprintf_unfiltered (gdb_stdlog, "Done reading minimal symbols.\n");
1394*ef5ccd6cSJohn Marino 
1395a45ae5f8SJohn Marino   if (dwarf2_has_info (objfile, NULL))
13965796c8dcSSimon Schubert     {
1397c50c785cSJohn Marino       /* elf_sym_fns_gdb_index cannot handle simultaneous non-DWARF debug
1398c50c785cSJohn Marino 	 information present in OBJFILE.  If there is such debug info present
1399c50c785cSJohn Marino 	 never use .gdb_index.  */
14005796c8dcSSimon Schubert 
1401c50c785cSJohn Marino       if (!objfile_has_partial_symbols (objfile)
1402c50c785cSJohn Marino 	  && dwarf2_initialize_objfile (objfile))
1403c50c785cSJohn Marino 	objfile->sf = &elf_sym_fns_gdb_index;
1404c50c785cSJohn Marino       else
1405c50c785cSJohn Marino 	{
1406c50c785cSJohn Marino 	  /* It is ok to do this even if the stabs reader made some
1407c50c785cSJohn Marino 	     partial symbols, because OBJF_PSYMTABS_READ has not been
1408c50c785cSJohn Marino 	     set, and so our lazy reader function will still be called
1409c50c785cSJohn Marino 	     when needed.  */
1410c50c785cSJohn Marino 	  objfile->sf = &elf_sym_fns_lazy_psyms;
1411c50c785cSJohn Marino 	}
1412c50c785cSJohn Marino     }
1413c50c785cSJohn Marino   /* If the file has its own symbol tables it has no separate debug
1414c50c785cSJohn Marino      info.  `.dynsym'/`.symtab' go to MSYMBOLS, `.debug_info' goes to
1415c50c785cSJohn Marino      SYMTABS/PSYMTABS.  `.gnu_debuglink' may no longer be present with
1416*ef5ccd6cSJohn Marino      `.note.gnu.build-id'.
1417*ef5ccd6cSJohn Marino 
1418*ef5ccd6cSJohn Marino      .gnu_debugdata is !objfile_has_partial_symbols because it contains only
1419*ef5ccd6cSJohn Marino      .symtab, not .debug_* section.  But if we already added .gnu_debugdata as
1420*ef5ccd6cSJohn Marino      an objfile via find_separate_debug_file_in_section there was no separate
1421*ef5ccd6cSJohn Marino      debug info available.  Therefore do not attempt to search for another one,
1422*ef5ccd6cSJohn Marino      objfile->separate_debug_objfile->separate_debug_objfile GDB guarantees to
1423*ef5ccd6cSJohn Marino      be NULL and we would possibly violate it.  */
1424*ef5ccd6cSJohn Marino 
1425*ef5ccd6cSJohn Marino   else if (!objfile_has_partial_symbols (objfile)
1426*ef5ccd6cSJohn Marino 	   && objfile->separate_debug_objfile == NULL
1427*ef5ccd6cSJohn Marino 	   && objfile->separate_debug_objfile_backlink == NULL)
1428cf7f2e2dSJohn Marino     {
1429cf7f2e2dSJohn Marino       char *debugfile;
1430cf7f2e2dSJohn Marino 
1431cf7f2e2dSJohn Marino       debugfile = find_separate_debug_file_by_buildid (objfile);
1432cf7f2e2dSJohn Marino 
1433cf7f2e2dSJohn Marino       if (debugfile == NULL)
1434cf7f2e2dSJohn Marino 	debugfile = find_separate_debug_file_by_debuglink (objfile);
1435cf7f2e2dSJohn Marino 
1436cf7f2e2dSJohn Marino       if (debugfile)
1437cf7f2e2dSJohn Marino 	{
1438*ef5ccd6cSJohn Marino 	  struct cleanup *cleanup = make_cleanup (xfree, debugfile);
1439cf7f2e2dSJohn Marino 	  bfd *abfd = symfile_bfd_open (debugfile);
1440cf7f2e2dSJohn Marino 
1441*ef5ccd6cSJohn Marino 	  make_cleanup_bfd_unref (abfd);
1442cf7f2e2dSJohn Marino 	  symbol_file_add_separate (abfd, symfile_flags, objfile);
1443*ef5ccd6cSJohn Marino 	  do_cleanups (cleanup);
1444cf7f2e2dSJohn Marino 	}
1445cf7f2e2dSJohn Marino     }
14465796c8dcSSimon Schubert }
14475796c8dcSSimon Schubert 
1448c50c785cSJohn Marino /* Callback to lazily read psymtabs.  */
1449c50c785cSJohn Marino 
1450c50c785cSJohn Marino static void
read_psyms(struct objfile * objfile)1451c50c785cSJohn Marino read_psyms (struct objfile *objfile)
1452c50c785cSJohn Marino {
1453a45ae5f8SJohn Marino   if (dwarf2_has_info (objfile, NULL))
1454c50c785cSJohn Marino     dwarf2_build_psymtabs (objfile);
1455c50c785cSJohn Marino }
1456c50c785cSJohn Marino 
1457*ef5ccd6cSJohn Marino /* This cleans up the objfile's dbx symfile info, and the chain of
1458*ef5ccd6cSJohn Marino    stab_section_info's, that might be dangling from it.  */
14595796c8dcSSimon Schubert 
14605796c8dcSSimon Schubert static void
free_elfinfo(void * objp)14615796c8dcSSimon Schubert free_elfinfo (void *objp)
14625796c8dcSSimon Schubert {
14635796c8dcSSimon Schubert   struct objfile *objfile = (struct objfile *) objp;
1464*ef5ccd6cSJohn Marino   struct dbx_symfile_info *dbxinfo = DBX_SYMFILE_INFO (objfile);
14655796c8dcSSimon Schubert   struct stab_section_info *ssi, *nssi;
14665796c8dcSSimon Schubert 
14675796c8dcSSimon Schubert   ssi = dbxinfo->stab_section_info;
14685796c8dcSSimon Schubert   while (ssi)
14695796c8dcSSimon Schubert     {
14705796c8dcSSimon Schubert       nssi = ssi->next;
14715796c8dcSSimon Schubert       xfree (ssi);
14725796c8dcSSimon Schubert       ssi = nssi;
14735796c8dcSSimon Schubert     }
14745796c8dcSSimon Schubert 
14755796c8dcSSimon Schubert   dbxinfo->stab_section_info = 0;	/* Just say No mo info about this.  */
14765796c8dcSSimon Schubert }
14775796c8dcSSimon Schubert 
14785796c8dcSSimon Schubert 
14795796c8dcSSimon Schubert /* Initialize anything that needs initializing when a completely new symbol
14805796c8dcSSimon Schubert    file is specified (not just adding some symbols from another file, e.g. a
14815796c8dcSSimon Schubert    shared library).
14825796c8dcSSimon Schubert 
1483c50c785cSJohn Marino    We reinitialize buildsym, since we may be reading stabs from an ELF
1484c50c785cSJohn Marino    file.  */
14855796c8dcSSimon Schubert 
14865796c8dcSSimon Schubert static void
elf_new_init(struct objfile * ignore)14875796c8dcSSimon Schubert elf_new_init (struct objfile *ignore)
14885796c8dcSSimon Schubert {
14895796c8dcSSimon Schubert   stabsread_new_init ();
14905796c8dcSSimon Schubert   buildsym_new_init ();
14915796c8dcSSimon Schubert }
14925796c8dcSSimon Schubert 
14935796c8dcSSimon Schubert /* Perform any local cleanups required when we are done with a particular
14945796c8dcSSimon Schubert    objfile.  I.E, we are in the process of discarding all symbol information
14955796c8dcSSimon Schubert    for an objfile, freeing up all memory held for it, and unlinking the
14965796c8dcSSimon Schubert    objfile struct from the global list of known objfiles.  */
14975796c8dcSSimon Schubert 
14985796c8dcSSimon Schubert static void
elf_symfile_finish(struct objfile * objfile)14995796c8dcSSimon Schubert elf_symfile_finish (struct objfile *objfile)
15005796c8dcSSimon Schubert {
15015796c8dcSSimon Schubert   dwarf2_free_objfile (objfile);
15025796c8dcSSimon Schubert }
15035796c8dcSSimon Schubert 
15045796c8dcSSimon Schubert /* ELF specific initialization routine for reading symbols.
15055796c8dcSSimon Schubert 
15065796c8dcSSimon Schubert    It is passed a pointer to a struct sym_fns which contains, among other
15075796c8dcSSimon Schubert    things, the BFD for the file whose symbols are being read, and a slot for
15085796c8dcSSimon Schubert    a pointer to "private data" which we can fill with goodies.
15095796c8dcSSimon Schubert 
15105796c8dcSSimon Schubert    For now at least, we have nothing in particular to do, so this function is
15115796c8dcSSimon Schubert    just a stub.  */
15125796c8dcSSimon Schubert 
15135796c8dcSSimon Schubert static void
elf_symfile_init(struct objfile * objfile)15145796c8dcSSimon Schubert elf_symfile_init (struct objfile *objfile)
15155796c8dcSSimon Schubert {
15165796c8dcSSimon Schubert   /* ELF objects may be reordered, so set OBJF_REORDERED.  If we
15175796c8dcSSimon Schubert      find this causes a significant slowdown in gdb then we could
15185796c8dcSSimon Schubert      set it in the debug symbol readers only when necessary.  */
15195796c8dcSSimon Schubert   objfile->flags |= OBJF_REORDERED;
15205796c8dcSSimon Schubert }
15215796c8dcSSimon Schubert 
15225796c8dcSSimon Schubert /* When handling an ELF file that contains Sun STABS debug info,
15235796c8dcSSimon Schubert    some of the debug info is relative to the particular chunk of the
15245796c8dcSSimon Schubert    section that was generated in its individual .o file.  E.g.
15255796c8dcSSimon Schubert    offsets to static variables are relative to the start of the data
15265796c8dcSSimon Schubert    segment *for that module before linking*.  This information is
15275796c8dcSSimon Schubert    painfully squirreled away in the ELF symbol table as local symbols
15285796c8dcSSimon Schubert    with wierd names.  Go get 'em when needed.  */
15295796c8dcSSimon Schubert 
15305796c8dcSSimon Schubert void
elfstab_offset_sections(struct objfile * objfile,struct partial_symtab * pst)15315796c8dcSSimon Schubert elfstab_offset_sections (struct objfile *objfile, struct partial_symtab *pst)
15325796c8dcSSimon Schubert {
1533c50c785cSJohn Marino   const char *filename = pst->filename;
1534*ef5ccd6cSJohn Marino   struct dbx_symfile_info *dbx = DBX_SYMFILE_INFO (objfile);
15355796c8dcSSimon Schubert   struct stab_section_info *maybe = dbx->stab_section_info;
15365796c8dcSSimon Schubert   struct stab_section_info *questionable = 0;
15375796c8dcSSimon Schubert   int i;
15385796c8dcSSimon Schubert 
15395796c8dcSSimon Schubert   /* The ELF symbol info doesn't include path names, so strip the path
15405796c8dcSSimon Schubert      (if any) from the psymtab filename.  */
1541c50c785cSJohn Marino   filename = lbasename (filename);
15425796c8dcSSimon Schubert 
15435796c8dcSSimon Schubert   /* FIXME:  This linear search could speed up significantly
15445796c8dcSSimon Schubert      if it was chained in the right order to match how we search it,
15455796c8dcSSimon Schubert      and if we unchained when we found a match.  */
15465796c8dcSSimon Schubert   for (; maybe; maybe = maybe->next)
15475796c8dcSSimon Schubert     {
15485796c8dcSSimon Schubert       if (filename[0] == maybe->filename[0]
1549c50c785cSJohn Marino 	  && filename_cmp (filename, maybe->filename) == 0)
15505796c8dcSSimon Schubert 	{
15515796c8dcSSimon Schubert 	  /* We found a match.  But there might be several source files
15525796c8dcSSimon Schubert 	     (from different directories) with the same name.  */
15535796c8dcSSimon Schubert 	  if (0 == maybe->found)
15545796c8dcSSimon Schubert 	    break;
15555796c8dcSSimon Schubert 	  questionable = maybe;	/* Might use it later.  */
15565796c8dcSSimon Schubert 	}
15575796c8dcSSimon Schubert     }
15585796c8dcSSimon Schubert 
15595796c8dcSSimon Schubert   if (maybe == 0 && questionable != 0)
15605796c8dcSSimon Schubert     {
15615796c8dcSSimon Schubert       complaint (&symfile_complaints,
1562c50c785cSJohn Marino 		 _("elf/stab section information questionable for %s"),
1563c50c785cSJohn Marino 		 filename);
15645796c8dcSSimon Schubert       maybe = questionable;
15655796c8dcSSimon Schubert     }
15665796c8dcSSimon Schubert 
15675796c8dcSSimon Schubert   if (maybe)
15685796c8dcSSimon Schubert     {
15695796c8dcSSimon Schubert       /* Found it!  Allocate a new psymtab struct, and fill it in.  */
15705796c8dcSSimon Schubert       maybe->found++;
15715796c8dcSSimon Schubert       pst->section_offsets = (struct section_offsets *)
15725796c8dcSSimon Schubert 	obstack_alloc (&objfile->objfile_obstack,
15735796c8dcSSimon Schubert 		       SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
15745796c8dcSSimon Schubert       for (i = 0; i < maybe->num_sections; i++)
15755796c8dcSSimon Schubert 	(pst->section_offsets)->offsets[i] = maybe->sections[i];
15765796c8dcSSimon Schubert       return;
15775796c8dcSSimon Schubert     }
15785796c8dcSSimon Schubert 
15795796c8dcSSimon Schubert   /* We were unable to find any offsets for this file.  Complain.  */
15805796c8dcSSimon Schubert   if (dbx->stab_section_info)	/* If there *is* any info, */
15815796c8dcSSimon Schubert     complaint (&symfile_complaints,
15825796c8dcSSimon Schubert 	       _("elf/stab section information missing for %s"), filename);
15835796c8dcSSimon Schubert }
1584*ef5ccd6cSJohn Marino 
1585*ef5ccd6cSJohn Marino /* Implementation of `sym_get_probes', as documented in symfile.h.  */
1586*ef5ccd6cSJohn Marino 
VEC(probe_p)1587*ef5ccd6cSJohn Marino static VEC (probe_p) *
1588*ef5ccd6cSJohn Marino elf_get_probes (struct objfile *objfile)
1589*ef5ccd6cSJohn Marino {
1590*ef5ccd6cSJohn Marino   VEC (probe_p) *probes_per_objfile;
1591*ef5ccd6cSJohn Marino 
1592*ef5ccd6cSJohn Marino   /* Have we parsed this objfile's probes already?  */
1593*ef5ccd6cSJohn Marino   probes_per_objfile = objfile_data (objfile, probe_key);
1594*ef5ccd6cSJohn Marino 
1595*ef5ccd6cSJohn Marino   if (!probes_per_objfile)
1596*ef5ccd6cSJohn Marino     {
1597*ef5ccd6cSJohn Marino       int ix;
1598*ef5ccd6cSJohn Marino       const struct probe_ops *probe_ops;
1599*ef5ccd6cSJohn Marino 
1600*ef5ccd6cSJohn Marino       /* Here we try to gather information about all types of probes from the
1601*ef5ccd6cSJohn Marino 	 objfile.  */
1602*ef5ccd6cSJohn Marino       for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops);
1603*ef5ccd6cSJohn Marino 	   ix++)
1604*ef5ccd6cSJohn Marino 	probe_ops->get_probes (&probes_per_objfile, objfile);
1605*ef5ccd6cSJohn Marino 
1606*ef5ccd6cSJohn Marino       if (probes_per_objfile == NULL)
1607*ef5ccd6cSJohn Marino 	{
1608*ef5ccd6cSJohn Marino 	  VEC_reserve (probe_p, probes_per_objfile, 1);
1609*ef5ccd6cSJohn Marino 	  gdb_assert (probes_per_objfile != NULL);
1610*ef5ccd6cSJohn Marino 	}
1611*ef5ccd6cSJohn Marino 
1612*ef5ccd6cSJohn Marino       set_objfile_data (objfile, probe_key, probes_per_objfile);
1613*ef5ccd6cSJohn Marino     }
1614*ef5ccd6cSJohn Marino 
1615*ef5ccd6cSJohn Marino   return probes_per_objfile;
1616*ef5ccd6cSJohn Marino }
1617*ef5ccd6cSJohn Marino 
1618*ef5ccd6cSJohn Marino /* Implementation of `sym_get_probe_argument_count', as documented in
1619*ef5ccd6cSJohn Marino    symfile.h.  */
1620*ef5ccd6cSJohn Marino 
1621*ef5ccd6cSJohn Marino static unsigned
elf_get_probe_argument_count(struct probe * probe)1622*ef5ccd6cSJohn Marino elf_get_probe_argument_count (struct probe *probe)
1623*ef5ccd6cSJohn Marino {
1624*ef5ccd6cSJohn Marino   return probe->pops->get_probe_argument_count (probe);
1625*ef5ccd6cSJohn Marino }
1626*ef5ccd6cSJohn Marino 
1627*ef5ccd6cSJohn Marino /* Implementation of `sym_evaluate_probe_argument', as documented in
1628*ef5ccd6cSJohn Marino    symfile.h.  */
1629*ef5ccd6cSJohn Marino 
1630*ef5ccd6cSJohn Marino static struct value *
elf_evaluate_probe_argument(struct probe * probe,unsigned n)1631*ef5ccd6cSJohn Marino elf_evaluate_probe_argument (struct probe *probe, unsigned n)
1632*ef5ccd6cSJohn Marino {
1633*ef5ccd6cSJohn Marino   return probe->pops->evaluate_probe_argument (probe, n);
1634*ef5ccd6cSJohn Marino }
1635*ef5ccd6cSJohn Marino 
1636*ef5ccd6cSJohn Marino /* Implementation of `sym_compile_to_ax', as documented in symfile.h.  */
1637*ef5ccd6cSJohn Marino 
1638*ef5ccd6cSJohn Marino static void
elf_compile_to_ax(struct probe * probe,struct agent_expr * expr,struct axs_value * value,unsigned n)1639*ef5ccd6cSJohn Marino elf_compile_to_ax (struct probe *probe,
1640*ef5ccd6cSJohn Marino 		   struct agent_expr *expr,
1641*ef5ccd6cSJohn Marino 		   struct axs_value *value,
1642*ef5ccd6cSJohn Marino 		   unsigned n)
1643*ef5ccd6cSJohn Marino {
1644*ef5ccd6cSJohn Marino   probe->pops->compile_to_ax (probe, expr, value, n);
1645*ef5ccd6cSJohn Marino }
1646*ef5ccd6cSJohn Marino 
1647*ef5ccd6cSJohn Marino /* Implementation of `sym_relocate_probe', as documented in symfile.h.  */
1648*ef5ccd6cSJohn Marino 
1649*ef5ccd6cSJohn Marino static void
elf_symfile_relocate_probe(struct objfile * objfile,struct section_offsets * new_offsets,struct section_offsets * delta)1650*ef5ccd6cSJohn Marino elf_symfile_relocate_probe (struct objfile *objfile,
1651*ef5ccd6cSJohn Marino 			    struct section_offsets *new_offsets,
1652*ef5ccd6cSJohn Marino 			    struct section_offsets *delta)
1653*ef5ccd6cSJohn Marino {
1654*ef5ccd6cSJohn Marino   int ix;
1655*ef5ccd6cSJohn Marino   VEC (probe_p) *probes = objfile_data (objfile, probe_key);
1656*ef5ccd6cSJohn Marino   struct probe *probe;
1657*ef5ccd6cSJohn Marino 
1658*ef5ccd6cSJohn Marino   for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
1659*ef5ccd6cSJohn Marino     probe->pops->relocate (probe, ANOFFSET (delta, SECT_OFF_TEXT (objfile)));
1660*ef5ccd6cSJohn Marino }
1661*ef5ccd6cSJohn Marino 
1662*ef5ccd6cSJohn Marino /* Helper function used to free the space allocated for storing SystemTap
1663*ef5ccd6cSJohn Marino    probe information.  */
1664*ef5ccd6cSJohn Marino 
1665*ef5ccd6cSJohn Marino static void
probe_key_free(struct objfile * objfile,void * d)1666*ef5ccd6cSJohn Marino probe_key_free (struct objfile *objfile, void *d)
1667*ef5ccd6cSJohn Marino {
1668*ef5ccd6cSJohn Marino   int ix;
1669*ef5ccd6cSJohn Marino   VEC (probe_p) *probes = d;
1670*ef5ccd6cSJohn Marino   struct probe *probe;
1671*ef5ccd6cSJohn Marino 
1672*ef5ccd6cSJohn Marino   for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
1673*ef5ccd6cSJohn Marino     probe->pops->destroy (probe);
1674*ef5ccd6cSJohn Marino 
1675*ef5ccd6cSJohn Marino   VEC_free (probe_p, probes);
1676*ef5ccd6cSJohn Marino }
1677*ef5ccd6cSJohn Marino 
16785796c8dcSSimon Schubert 
1679*ef5ccd6cSJohn Marino 
1680*ef5ccd6cSJohn Marino /* Implementation `sym_probe_fns', as documented in symfile.h.  */
1681*ef5ccd6cSJohn Marino 
1682*ef5ccd6cSJohn Marino static const struct sym_probe_fns elf_probe_fns =
1683*ef5ccd6cSJohn Marino {
1684*ef5ccd6cSJohn Marino   elf_get_probes,		/* sym_get_probes */
1685*ef5ccd6cSJohn Marino   elf_get_probe_argument_count,	/* sym_get_probe_argument_count */
1686*ef5ccd6cSJohn Marino   elf_evaluate_probe_argument,	/* sym_evaluate_probe_argument */
1687*ef5ccd6cSJohn Marino   elf_compile_to_ax,		/* sym_compile_to_ax */
1688*ef5ccd6cSJohn Marino   elf_symfile_relocate_probe,	/* sym_relocate_probe */
1689*ef5ccd6cSJohn Marino };
1690*ef5ccd6cSJohn Marino 
16915796c8dcSSimon Schubert /* Register that we are able to handle ELF object file formats.  */
16925796c8dcSSimon Schubert 
1693c50c785cSJohn Marino static const struct sym_fns elf_sym_fns =
16945796c8dcSSimon Schubert {
16955796c8dcSSimon Schubert   bfd_target_elf_flavour,
1696c50c785cSJohn Marino   elf_new_init,			/* init anything gbl to entire symtab */
1697c50c785cSJohn Marino   elf_symfile_init,		/* read initial info, setup for sym_read() */
1698c50c785cSJohn Marino   elf_symfile_read,		/* read a symbol file into symtab */
1699c50c785cSJohn Marino   NULL,				/* sym_read_psymbols */
1700c50c785cSJohn Marino   elf_symfile_finish,		/* finished with file, cleanup */
1701c50c785cSJohn Marino   default_symfile_offsets,	/* Translate ext. to int. relocation */
1702c50c785cSJohn Marino   elf_symfile_segments,		/* Get segment information from a file.  */
1703c50c785cSJohn Marino   NULL,
1704c50c785cSJohn Marino   default_symfile_relocate,	/* Relocate a debug section.  */
1705*ef5ccd6cSJohn Marino   &elf_probe_fns,		/* sym_probe_fns */
1706c50c785cSJohn Marino   &psym_functions
1707c50c785cSJohn Marino };
1708c50c785cSJohn Marino 
1709c50c785cSJohn Marino /* The same as elf_sym_fns, but not registered and lazily reads
1710c50c785cSJohn Marino    psymbols.  */
1711c50c785cSJohn Marino 
1712c50c785cSJohn Marino static const struct sym_fns elf_sym_fns_lazy_psyms =
1713c50c785cSJohn Marino {
1714c50c785cSJohn Marino   bfd_target_elf_flavour,
1715c50c785cSJohn Marino   elf_new_init,			/* init anything gbl to entire symtab */
1716c50c785cSJohn Marino   elf_symfile_init,		/* read initial info, setup for sym_read() */
1717c50c785cSJohn Marino   elf_symfile_read,		/* read a symbol file into symtab */
1718c50c785cSJohn Marino   read_psyms,			/* sym_read_psymbols */
1719c50c785cSJohn Marino   elf_symfile_finish,		/* finished with file, cleanup */
1720c50c785cSJohn Marino   default_symfile_offsets,	/* Translate ext. to int. relocation */
1721c50c785cSJohn Marino   elf_symfile_segments,		/* Get segment information from a file.  */
1722c50c785cSJohn Marino   NULL,
1723c50c785cSJohn Marino   default_symfile_relocate,	/* Relocate a debug section.  */
1724*ef5ccd6cSJohn Marino   &elf_probe_fns,		/* sym_probe_fns */
1725c50c785cSJohn Marino   &psym_functions
1726c50c785cSJohn Marino };
1727c50c785cSJohn Marino 
1728c50c785cSJohn Marino /* The same as elf_sym_fns, but not registered and uses the
1729c50c785cSJohn Marino    DWARF-specific GNU index rather than psymtab.  */
1730c50c785cSJohn Marino static const struct sym_fns elf_sym_fns_gdb_index =
1731c50c785cSJohn Marino {
1732c50c785cSJohn Marino   bfd_target_elf_flavour,
1733c50c785cSJohn Marino   elf_new_init,			/* init anything gbl to entire symab */
1734c50c785cSJohn Marino   elf_symfile_init,		/* read initial info, setup for sym_red() */
1735c50c785cSJohn Marino   elf_symfile_read,		/* read a symbol file into symtab */
1736c50c785cSJohn Marino   NULL,				/* sym_read_psymbols */
1737c50c785cSJohn Marino   elf_symfile_finish,		/* finished with file, cleanup */
1738c50c785cSJohn Marino   default_symfile_offsets,	/* Translate ext. to int. relocatin */
1739c50c785cSJohn Marino   elf_symfile_segments,		/* Get segment information from a file.  */
1740c50c785cSJohn Marino   NULL,
1741c50c785cSJohn Marino   default_symfile_relocate,	/* Relocate a debug section.  */
1742*ef5ccd6cSJohn Marino   &elf_probe_fns,		/* sym_probe_fns */
1743c50c785cSJohn Marino   &dwarf2_gdb_index_functions
1744c50c785cSJohn Marino };
1745c50c785cSJohn Marino 
1746c50c785cSJohn Marino /* STT_GNU_IFUNC resolver vector to be installed to gnu_ifunc_fns_p.  */
1747c50c785cSJohn Marino 
1748c50c785cSJohn Marino static const struct gnu_ifunc_fns elf_gnu_ifunc_fns =
1749c50c785cSJohn Marino {
1750c50c785cSJohn Marino   elf_gnu_ifunc_resolve_addr,
1751c50c785cSJohn Marino   elf_gnu_ifunc_resolve_name,
1752c50c785cSJohn Marino   elf_gnu_ifunc_resolver_stop,
1753c50c785cSJohn Marino   elf_gnu_ifunc_resolver_return_stop
17545796c8dcSSimon Schubert };
17555796c8dcSSimon Schubert 
17565796c8dcSSimon Schubert void
_initialize_elfread(void)17575796c8dcSSimon Schubert _initialize_elfread (void)
17585796c8dcSSimon Schubert {
1759*ef5ccd6cSJohn Marino   probe_key = register_objfile_data_with_cleanup (NULL, probe_key_free);
17605796c8dcSSimon Schubert   add_symtab_fns (&elf_sym_fns);
1761c50c785cSJohn Marino 
1762c50c785cSJohn Marino   elf_objfile_gnu_ifunc_cache_data = register_objfile_data ();
1763c50c785cSJohn Marino   gnu_ifunc_fns_p = &elf_gnu_ifunc_fns;
17645796c8dcSSimon Schubert }
1765