xref: /dflybsd-src/contrib/gdb-7/gdb/elfread.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* Read ELF (Executable and Linking Format) object files for GDB.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4*c50c785cSJohn Marino    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
55796c8dcSSimon Schubert    Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    Written by Fred Fish at Cygnus Support.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This file is part of GDB.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
125796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
135796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
145796c8dcSSimon Schubert    (at your option) any later version.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
175796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
185796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
195796c8dcSSimon Schubert    GNU General Public License for more details.
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
225796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
235796c8dcSSimon Schubert 
245796c8dcSSimon Schubert #include "defs.h"
255796c8dcSSimon Schubert #include "bfd.h"
265796c8dcSSimon Schubert #include "gdb_string.h"
275796c8dcSSimon Schubert #include "elf-bfd.h"
285796c8dcSSimon Schubert #include "elf/common.h"
295796c8dcSSimon Schubert #include "elf/internal.h"
305796c8dcSSimon Schubert #include "elf/mips.h"
315796c8dcSSimon Schubert #include "symtab.h"
325796c8dcSSimon Schubert #include "symfile.h"
335796c8dcSSimon Schubert #include "objfiles.h"
345796c8dcSSimon Schubert #include "buildsym.h"
355796c8dcSSimon Schubert #include "stabsread.h"
365796c8dcSSimon Schubert #include "gdb-stabs.h"
375796c8dcSSimon Schubert #include "complaints.h"
385796c8dcSSimon Schubert #include "demangle.h"
39cf7f2e2dSJohn Marino #include "psympriv.h"
40*c50c785cSJohn Marino #include "filenames.h"
41*c50c785cSJohn Marino #include "gdbtypes.h"
42*c50c785cSJohn Marino #include "value.h"
43*c50c785cSJohn Marino #include "infcall.h"
44*c50c785cSJohn Marino #include "gdbthread.h"
45*c50c785cSJohn Marino #include "regcache.h"
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert extern void _initialize_elfread (void);
485796c8dcSSimon Schubert 
49*c50c785cSJohn Marino /* Forward declarations.  */
50*c50c785cSJohn Marino static const struct sym_fns elf_sym_fns_gdb_index;
51*c50c785cSJohn Marino static const struct sym_fns elf_sym_fns_lazy_psyms;
52*c50c785cSJohn Marino 
535796c8dcSSimon Schubert /* The struct elfinfo is available only during ELF symbol table and
545796c8dcSSimon Schubert    psymtab reading.  It is destroyed at the completion of psymtab-reading.
555796c8dcSSimon Schubert    It's local to elf_symfile_read.  */
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert struct elfinfo
585796c8dcSSimon Schubert   {
595796c8dcSSimon Schubert     asection *stabsect;		/* Section pointer for .stab section */
605796c8dcSSimon Schubert     asection *stabindexsect;	/* Section pointer for .stab.index section */
615796c8dcSSimon Schubert     asection *mdebugsect;	/* Section pointer for .mdebug section */
625796c8dcSSimon Schubert   };
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert static void free_elfinfo (void *);
655796c8dcSSimon Schubert 
66*c50c785cSJohn Marino /* Minimal symbols located at the GOT entries for .plt - that is the real
67*c50c785cSJohn Marino    pointer where the given entry will jump to.  It gets updated by the real
68*c50c785cSJohn Marino    function address during lazy ld.so resolving in the inferior.  These
69*c50c785cSJohn Marino    minimal symbols are indexed for <tab>-completion.  */
70*c50c785cSJohn Marino 
71*c50c785cSJohn Marino #define SYMBOL_GOT_PLT_SUFFIX "@got.plt"
72*c50c785cSJohn Marino 
735796c8dcSSimon Schubert /* Locate the segments in ABFD.  */
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert static struct symfile_segment_data *
765796c8dcSSimon Schubert elf_symfile_segments (bfd *abfd)
775796c8dcSSimon Schubert {
785796c8dcSSimon Schubert   Elf_Internal_Phdr *phdrs, **segments;
795796c8dcSSimon Schubert   long phdrs_size;
805796c8dcSSimon Schubert   int num_phdrs, num_segments, num_sections, i;
815796c8dcSSimon Schubert   asection *sect;
825796c8dcSSimon Schubert   struct symfile_segment_data *data;
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert   phdrs_size = bfd_get_elf_phdr_upper_bound (abfd);
855796c8dcSSimon Schubert   if (phdrs_size == -1)
865796c8dcSSimon Schubert     return NULL;
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert   phdrs = alloca (phdrs_size);
895796c8dcSSimon Schubert   num_phdrs = bfd_get_elf_phdrs (abfd, phdrs);
905796c8dcSSimon Schubert   if (num_phdrs == -1)
915796c8dcSSimon Schubert     return NULL;
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert   num_segments = 0;
945796c8dcSSimon Schubert   segments = alloca (sizeof (Elf_Internal_Phdr *) * num_phdrs);
955796c8dcSSimon Schubert   for (i = 0; i < num_phdrs; i++)
965796c8dcSSimon Schubert     if (phdrs[i].p_type == PT_LOAD)
975796c8dcSSimon Schubert       segments[num_segments++] = &phdrs[i];
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert   if (num_segments == 0)
1005796c8dcSSimon Schubert     return NULL;
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert   data = XZALLOC (struct symfile_segment_data);
1035796c8dcSSimon Schubert   data->num_segments = num_segments;
1045796c8dcSSimon Schubert   data->segment_bases = XCALLOC (num_segments, CORE_ADDR);
1055796c8dcSSimon Schubert   data->segment_sizes = XCALLOC (num_segments, CORE_ADDR);
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert   for (i = 0; i < num_segments; i++)
1085796c8dcSSimon Schubert     {
1095796c8dcSSimon Schubert       data->segment_bases[i] = segments[i]->p_vaddr;
1105796c8dcSSimon Schubert       data->segment_sizes[i] = segments[i]->p_memsz;
1115796c8dcSSimon Schubert     }
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert   num_sections = bfd_count_sections (abfd);
1145796c8dcSSimon Schubert   data->segment_info = XCALLOC (num_sections, int);
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
1175796c8dcSSimon Schubert     {
1185796c8dcSSimon Schubert       int j;
1195796c8dcSSimon Schubert       CORE_ADDR vma;
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
1225796c8dcSSimon Schubert 	continue;
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert       vma = bfd_get_section_vma (abfd, sect);
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert       for (j = 0; j < num_segments; j++)
1275796c8dcSSimon Schubert 	if (segments[j]->p_memsz > 0
1285796c8dcSSimon Schubert 	    && vma >= segments[j]->p_vaddr
1295796c8dcSSimon Schubert 	    && (vma - segments[j]->p_vaddr) < segments[j]->p_memsz)
1305796c8dcSSimon Schubert 	  {
1315796c8dcSSimon Schubert 	    data->segment_info[i] = j + 1;
1325796c8dcSSimon Schubert 	    break;
1335796c8dcSSimon Schubert 	  }
1345796c8dcSSimon Schubert 
135cf7f2e2dSJohn Marino       /* We should have found a segment for every non-empty section.
136cf7f2e2dSJohn Marino 	 If we haven't, we will not relocate this section by any
137cf7f2e2dSJohn Marino 	 offsets we apply to the segments.  As an exception, do not
138cf7f2e2dSJohn Marino 	 warn about SHT_NOBITS sections; in normal ELF execution
139cf7f2e2dSJohn Marino 	 environments, SHT_NOBITS means zero-initialized and belongs
140cf7f2e2dSJohn Marino 	 in a segment, but in no-OS environments some tools (e.g. ARM
141cf7f2e2dSJohn Marino 	 RealView) use SHT_NOBITS for uninitialized data.  Since it is
142cf7f2e2dSJohn Marino 	 uninitialized, it doesn't need a program header.  Such
143cf7f2e2dSJohn Marino 	 binaries are not relocatable.  */
144cf7f2e2dSJohn Marino       if (bfd_get_section_size (sect) > 0 && j == num_segments
145cf7f2e2dSJohn Marino 	  && (bfd_get_section_flags (abfd, sect) & SEC_LOAD) != 0)
1465796c8dcSSimon Schubert 	warning (_("Loadable segment \"%s\" outside of ELF segments"),
1475796c8dcSSimon Schubert 		 bfd_section_name (abfd, sect));
1485796c8dcSSimon Schubert     }
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   return data;
1515796c8dcSSimon Schubert }
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert /* We are called once per section from elf_symfile_read.  We
1545796c8dcSSimon Schubert    need to examine each section we are passed, check to see
1555796c8dcSSimon Schubert    if it is something we are interested in processing, and
1565796c8dcSSimon Schubert    if so, stash away some access information for the section.
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert    For now we recognize the dwarf debug information sections and
1595796c8dcSSimon Schubert    line number sections from matching their section names.  The
1605796c8dcSSimon Schubert    ELF definition is no real help here since it has no direct
1615796c8dcSSimon Schubert    knowledge of DWARF (by design, so any debugging format can be
1625796c8dcSSimon Schubert    used).
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert    We also recognize the ".stab" sections used by the Sun compilers
1655796c8dcSSimon Schubert    released with Solaris 2.
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert    FIXME: The section names should not be hardwired strings (what
1685796c8dcSSimon Schubert    should they be?  I don't think most object file formats have enough
169*c50c785cSJohn Marino    section flags to specify what kind of debug section it is.
1705796c8dcSSimon Schubert    -kingdon).  */
1715796c8dcSSimon Schubert 
1725796c8dcSSimon Schubert static void
1735796c8dcSSimon Schubert elf_locate_sections (bfd *ignore_abfd, asection *sectp, void *eip)
1745796c8dcSSimon Schubert {
1755796c8dcSSimon Schubert   struct elfinfo *ei;
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert   ei = (struct elfinfo *) eip;
1785796c8dcSSimon Schubert   if (strcmp (sectp->name, ".stab") == 0)
1795796c8dcSSimon Schubert     {
1805796c8dcSSimon Schubert       ei->stabsect = sectp;
1815796c8dcSSimon Schubert     }
1825796c8dcSSimon Schubert   else if (strcmp (sectp->name, ".stab.index") == 0)
1835796c8dcSSimon Schubert     {
1845796c8dcSSimon Schubert       ei->stabindexsect = sectp;
1855796c8dcSSimon Schubert     }
1865796c8dcSSimon Schubert   else if (strcmp (sectp->name, ".mdebug") == 0)
1875796c8dcSSimon Schubert     {
1885796c8dcSSimon Schubert       ei->mdebugsect = sectp;
1895796c8dcSSimon Schubert     }
1905796c8dcSSimon Schubert }
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert static struct minimal_symbol *
193cf7f2e2dSJohn Marino record_minimal_symbol (const char *name, int name_len, int copy_name,
194cf7f2e2dSJohn Marino 		       CORE_ADDR address,
1955796c8dcSSimon Schubert 		       enum minimal_symbol_type ms_type,
1965796c8dcSSimon Schubert 		       asection *bfd_section, struct objfile *objfile)
1975796c8dcSSimon Schubert {
1985796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
1995796c8dcSSimon Schubert 
200*c50c785cSJohn Marino   if (ms_type == mst_text || ms_type == mst_file_text
201*c50c785cSJohn Marino       || ms_type == mst_text_gnu_ifunc)
2025796c8dcSSimon Schubert     address = gdbarch_smash_text_address (gdbarch, address);
2035796c8dcSSimon Schubert 
204cf7f2e2dSJohn Marino   return prim_record_minimal_symbol_full (name, name_len, copy_name, address,
205cf7f2e2dSJohn Marino 					  ms_type, bfd_section->index,
206cf7f2e2dSJohn Marino 					  bfd_section, objfile);
2075796c8dcSSimon Schubert }
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert /*
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert    LOCAL FUNCTION
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert    elf_symtab_read -- read the symbol table of an ELF file
2145796c8dcSSimon Schubert 
2155796c8dcSSimon Schubert    SYNOPSIS
2165796c8dcSSimon Schubert 
2175796c8dcSSimon Schubert    void elf_symtab_read (struct objfile *objfile, int type,
2185796c8dcSSimon Schubert 			 long number_of_symbols, asymbol **symbol_table)
2195796c8dcSSimon Schubert 
2205796c8dcSSimon Schubert    DESCRIPTION
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert    Given an objfile, a symbol table, and a flag indicating whether the
2235796c8dcSSimon Schubert    symbol table contains regular, dynamic, or synthetic symbols, add all
2245796c8dcSSimon Schubert    the global function and data symbols to the minimal symbol table.
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert    In stabs-in-ELF, as implemented by Sun, there are some local symbols
2275796c8dcSSimon Schubert    defined in the ELF symbol table, which can be used to locate
2285796c8dcSSimon Schubert    the beginnings of sections from each ".o" file that was linked to
2295796c8dcSSimon Schubert    form the executable objfile.  We gather any such info and record it
2305796c8dcSSimon Schubert    in data structures hung off the objfile's private data.
2315796c8dcSSimon Schubert 
2325796c8dcSSimon Schubert  */
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert #define ST_REGULAR 0
2355796c8dcSSimon Schubert #define ST_DYNAMIC 1
2365796c8dcSSimon Schubert #define ST_SYNTHETIC 2
2375796c8dcSSimon Schubert 
2385796c8dcSSimon Schubert static void
2395796c8dcSSimon Schubert elf_symtab_read (struct objfile *objfile, int type,
240cf7f2e2dSJohn Marino 		 long number_of_symbols, asymbol **symbol_table,
241cf7f2e2dSJohn Marino 		 int copy_names)
2425796c8dcSSimon Schubert {
2435796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2445796c8dcSSimon Schubert   asymbol *sym;
2455796c8dcSSimon Schubert   long i;
2465796c8dcSSimon Schubert   CORE_ADDR symaddr;
2475796c8dcSSimon Schubert   CORE_ADDR offset;
2485796c8dcSSimon Schubert   enum minimal_symbol_type ms_type;
2495796c8dcSSimon Schubert   /* If sectinfo is nonNULL, it contains section info that should end up
2505796c8dcSSimon Schubert      filed in the objfile.  */
2515796c8dcSSimon Schubert   struct stab_section_info *sectinfo = NULL;
2525796c8dcSSimon Schubert   /* If filesym is nonzero, it points to a file symbol, but we haven't
2535796c8dcSSimon Schubert      seen any section info for it yet.  */
2545796c8dcSSimon Schubert   asymbol *filesym = 0;
255cf7f2e2dSJohn Marino   /* Name of filesym.  This is either a constant string or is saved on
256cf7f2e2dSJohn Marino      the objfile's obstack.  */
257cf7f2e2dSJohn Marino   char *filesymname = "";
2585796c8dcSSimon Schubert   struct dbx_symfile_info *dbx = objfile->deprecated_sym_stab_info;
2595796c8dcSSimon Schubert   int stripped = (bfd_get_symcount (objfile->obfd) == 0);
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert   for (i = 0; i < number_of_symbols; i++)
2625796c8dcSSimon Schubert     {
2635796c8dcSSimon Schubert       sym = symbol_table[i];
2645796c8dcSSimon Schubert       if (sym->name == NULL || *sym->name == '\0')
2655796c8dcSSimon Schubert 	{
2665796c8dcSSimon Schubert 	  /* Skip names that don't exist (shouldn't happen), or names
2675796c8dcSSimon Schubert 	     that are null strings (may happen).  */
2685796c8dcSSimon Schubert 	  continue;
2695796c8dcSSimon Schubert 	}
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert       /* Skip "special" symbols, e.g. ARM mapping symbols.  These are
2725796c8dcSSimon Schubert 	 symbols which do not correspond to objects in the symbol table,
2735796c8dcSSimon Schubert 	 but have some other target-specific meaning.  */
2745796c8dcSSimon Schubert       if (bfd_is_target_special_symbol (objfile->obfd, sym))
2755796c8dcSSimon Schubert 	{
2765796c8dcSSimon Schubert 	  if (gdbarch_record_special_symbol_p (gdbarch))
2775796c8dcSSimon Schubert 	    gdbarch_record_special_symbol (gdbarch, objfile, sym);
2785796c8dcSSimon Schubert 	  continue;
2795796c8dcSSimon Schubert 	}
2805796c8dcSSimon Schubert 
2815796c8dcSSimon Schubert       offset = ANOFFSET (objfile->section_offsets, sym->section->index);
2825796c8dcSSimon Schubert       if (type == ST_DYNAMIC
2835796c8dcSSimon Schubert 	  && sym->section == &bfd_und_section
2845796c8dcSSimon Schubert 	  && (sym->flags & BSF_FUNCTION))
2855796c8dcSSimon Schubert 	{
2865796c8dcSSimon Schubert 	  struct minimal_symbol *msym;
2875796c8dcSSimon Schubert 	  bfd *abfd = objfile->obfd;
2885796c8dcSSimon Schubert 	  asection *sect;
2895796c8dcSSimon Schubert 
2905796c8dcSSimon Schubert 	  /* Symbol is a reference to a function defined in
2915796c8dcSSimon Schubert 	     a shared library.
2925796c8dcSSimon Schubert 	     If its value is non zero then it is usually the address
2935796c8dcSSimon Schubert 	     of the corresponding entry in the procedure linkage table,
2945796c8dcSSimon Schubert 	     plus the desired section offset.
2955796c8dcSSimon Schubert 	     If its value is zero then the dynamic linker has to resolve
2965796c8dcSSimon Schubert 	     the symbol.  We are unable to find any meaningful address
2975796c8dcSSimon Schubert 	     for this symbol in the executable file, so we skip it.  */
2985796c8dcSSimon Schubert 	  symaddr = sym->value;
2995796c8dcSSimon Schubert 	  if (symaddr == 0)
3005796c8dcSSimon Schubert 	    continue;
3015796c8dcSSimon Schubert 
3025796c8dcSSimon Schubert 	  /* sym->section is the undefined section.  However, we want to
3035796c8dcSSimon Schubert 	     record the section where the PLT stub resides with the
3045796c8dcSSimon Schubert 	     minimal symbol.  Search the section table for the one that
3055796c8dcSSimon Schubert 	     covers the stub's address.  */
3065796c8dcSSimon Schubert 	  for (sect = abfd->sections; sect != NULL; sect = sect->next)
3075796c8dcSSimon Schubert 	    {
3085796c8dcSSimon Schubert 	      if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
3095796c8dcSSimon Schubert 		continue;
3105796c8dcSSimon Schubert 
3115796c8dcSSimon Schubert 	      if (symaddr >= bfd_get_section_vma (abfd, sect)
3125796c8dcSSimon Schubert 		  && symaddr < bfd_get_section_vma (abfd, sect)
3135796c8dcSSimon Schubert 			       + bfd_get_section_size (sect))
3145796c8dcSSimon Schubert 		break;
3155796c8dcSSimon Schubert 	    }
3165796c8dcSSimon Schubert 	  if (!sect)
3175796c8dcSSimon Schubert 	    continue;
3185796c8dcSSimon Schubert 
3195796c8dcSSimon Schubert 	  symaddr += ANOFFSET (objfile->section_offsets, sect->index);
3205796c8dcSSimon Schubert 
3215796c8dcSSimon Schubert 	  msym = record_minimal_symbol
322cf7f2e2dSJohn Marino 	    (sym->name, strlen (sym->name), copy_names,
323cf7f2e2dSJohn Marino 	     symaddr, mst_solib_trampoline, sect, objfile);
3245796c8dcSSimon Schubert 	  if (msym != NULL)
3255796c8dcSSimon Schubert 	    msym->filename = filesymname;
3265796c8dcSSimon Schubert 	  continue;
3275796c8dcSSimon Schubert 	}
3285796c8dcSSimon Schubert 
3295796c8dcSSimon Schubert       /* If it is a nonstripped executable, do not enter dynamic
3305796c8dcSSimon Schubert 	 symbols, as the dynamic symbol table is usually a subset
3315796c8dcSSimon Schubert 	 of the main symbol table.  */
3325796c8dcSSimon Schubert       if (type == ST_DYNAMIC && !stripped)
3335796c8dcSSimon Schubert 	continue;
3345796c8dcSSimon Schubert       if (sym->flags & BSF_FILE)
3355796c8dcSSimon Schubert 	{
3365796c8dcSSimon Schubert 	  /* STT_FILE debugging symbol that helps stabs-in-elf debugging.
3375796c8dcSSimon Schubert 	     Chain any old one onto the objfile; remember new sym.  */
3385796c8dcSSimon Schubert 	  if (sectinfo != NULL)
3395796c8dcSSimon Schubert 	    {
3405796c8dcSSimon Schubert 	      sectinfo->next = dbx->stab_section_info;
3415796c8dcSSimon Schubert 	      dbx->stab_section_info = sectinfo;
3425796c8dcSSimon Schubert 	      sectinfo = NULL;
3435796c8dcSSimon Schubert 	    }
3445796c8dcSSimon Schubert 	  filesym = sym;
3455796c8dcSSimon Schubert 	  filesymname =
3465796c8dcSSimon Schubert 	    obsavestring ((char *) filesym->name, strlen (filesym->name),
3475796c8dcSSimon Schubert 			  &objfile->objfile_obstack);
3485796c8dcSSimon Schubert 	}
3495796c8dcSSimon Schubert       else if (sym->flags & BSF_SECTION_SYM)
3505796c8dcSSimon Schubert 	continue;
3515796c8dcSSimon Schubert       else if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
3525796c8dcSSimon Schubert 	{
3535796c8dcSSimon Schubert 	  struct minimal_symbol *msym;
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert 	  /* Select global/local/weak symbols.  Note that bfd puts abs
3565796c8dcSSimon Schubert 	     symbols in their own section, so all symbols we are
3575796c8dcSSimon Schubert 	     interested in will have a section.  */
3585796c8dcSSimon Schubert 	  /* Bfd symbols are section relative.  */
3595796c8dcSSimon Schubert 	  symaddr = sym->value + sym->section->vma;
3605796c8dcSSimon Schubert 	  /* Relocate all non-absolute and non-TLS symbols by the
3615796c8dcSSimon Schubert 	     section offset.  */
3625796c8dcSSimon Schubert 	  if (sym->section != &bfd_abs_section
3635796c8dcSSimon Schubert 	      && !(sym->section->flags & SEC_THREAD_LOCAL))
3645796c8dcSSimon Schubert 	    {
3655796c8dcSSimon Schubert 	      symaddr += offset;
3665796c8dcSSimon Schubert 	    }
3675796c8dcSSimon Schubert 	  /* For non-absolute symbols, use the type of the section
3685796c8dcSSimon Schubert 	     they are relative to, to intuit text/data.  Bfd provides
3695796c8dcSSimon Schubert 	     no way of figuring this out for absolute symbols.  */
3705796c8dcSSimon Schubert 	  if (sym->section == &bfd_abs_section)
3715796c8dcSSimon Schubert 	    {
3725796c8dcSSimon Schubert 	      /* This is a hack to get the minimal symbol type
3735796c8dcSSimon Schubert 		 right for Irix 5, which has absolute addresses
3745796c8dcSSimon Schubert 		 with special section indices for dynamic symbols.
3755796c8dcSSimon Schubert 
3765796c8dcSSimon Schubert 		 NOTE: uweigand-20071112: Synthetic symbols do not
3775796c8dcSSimon Schubert 		 have an ELF-private part, so do not touch those.  */
3785796c8dcSSimon Schubert 	      unsigned int shndx = type == ST_SYNTHETIC ? 0 :
3795796c8dcSSimon Schubert 		((elf_symbol_type *) sym)->internal_elf_sym.st_shndx;
3805796c8dcSSimon Schubert 
3815796c8dcSSimon Schubert 	      switch (shndx)
3825796c8dcSSimon Schubert 		{
3835796c8dcSSimon Schubert 		case SHN_MIPS_TEXT:
3845796c8dcSSimon Schubert 		  ms_type = mst_text;
3855796c8dcSSimon Schubert 		  break;
3865796c8dcSSimon Schubert 		case SHN_MIPS_DATA:
3875796c8dcSSimon Schubert 		  ms_type = mst_data;
3885796c8dcSSimon Schubert 		  break;
3895796c8dcSSimon Schubert 		case SHN_MIPS_ACOMMON:
3905796c8dcSSimon Schubert 		  ms_type = mst_bss;
3915796c8dcSSimon Schubert 		  break;
3925796c8dcSSimon Schubert 		default:
3935796c8dcSSimon Schubert 		  ms_type = mst_abs;
3945796c8dcSSimon Schubert 		}
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert 	      /* If it is an Irix dynamic symbol, skip section name
3975796c8dcSSimon Schubert 		 symbols, relocate all others by section offset.  */
3985796c8dcSSimon Schubert 	      if (ms_type != mst_abs)
3995796c8dcSSimon Schubert 		{
4005796c8dcSSimon Schubert 		  if (sym->name[0] == '.')
4015796c8dcSSimon Schubert 		    continue;
4025796c8dcSSimon Schubert 		  symaddr += offset;
4035796c8dcSSimon Schubert 		}
4045796c8dcSSimon Schubert 	    }
4055796c8dcSSimon Schubert 	  else if (sym->section->flags & SEC_CODE)
4065796c8dcSSimon Schubert 	    {
4075796c8dcSSimon Schubert 	      if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
4085796c8dcSSimon Schubert 		{
409*c50c785cSJohn Marino 		  if (sym->flags & BSF_GNU_INDIRECT_FUNCTION)
410*c50c785cSJohn Marino 		    ms_type = mst_text_gnu_ifunc;
411*c50c785cSJohn Marino 		  else
4125796c8dcSSimon Schubert 		    ms_type = mst_text;
4135796c8dcSSimon Schubert 		}
4145796c8dcSSimon Schubert 	      else if ((sym->name[0] == '.' && sym->name[1] == 'L')
4155796c8dcSSimon Schubert 		       || ((sym->flags & BSF_LOCAL)
4165796c8dcSSimon Schubert 			   && sym->name[0] == '$'
4175796c8dcSSimon Schubert 			   && sym->name[1] == 'L'))
4185796c8dcSSimon Schubert 		/* Looks like a compiler-generated label.  Skip
4195796c8dcSSimon Schubert 		   it.  The assembler should be skipping these (to
4205796c8dcSSimon Schubert 		   keep executables small), but apparently with
4215796c8dcSSimon Schubert 		   gcc on the (deleted) delta m88k SVR4, it loses.
4225796c8dcSSimon Schubert 		   So to have us check too should be harmless (but
4235796c8dcSSimon Schubert 		   I encourage people to fix this in the assembler
4245796c8dcSSimon Schubert 		   instead of adding checks here).  */
4255796c8dcSSimon Schubert 		continue;
4265796c8dcSSimon Schubert 	      else
4275796c8dcSSimon Schubert 		{
4285796c8dcSSimon Schubert 		  ms_type = mst_file_text;
4295796c8dcSSimon Schubert 		}
4305796c8dcSSimon Schubert 	    }
4315796c8dcSSimon Schubert 	  else if (sym->section->flags & SEC_ALLOC)
4325796c8dcSSimon Schubert 	    {
4335796c8dcSSimon Schubert 	      if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
4345796c8dcSSimon Schubert 		{
4355796c8dcSSimon Schubert 		  if (sym->section->flags & SEC_LOAD)
4365796c8dcSSimon Schubert 		    {
4375796c8dcSSimon Schubert 		      ms_type = mst_data;
4385796c8dcSSimon Schubert 		    }
4395796c8dcSSimon Schubert 		  else
4405796c8dcSSimon Schubert 		    {
4415796c8dcSSimon Schubert 		      ms_type = mst_bss;
4425796c8dcSSimon Schubert 		    }
4435796c8dcSSimon Schubert 		}
4445796c8dcSSimon Schubert 	      else if (sym->flags & BSF_LOCAL)
4455796c8dcSSimon Schubert 		{
4465796c8dcSSimon Schubert 		  /* Named Local variable in a Data section.
4475796c8dcSSimon Schubert 		     Check its name for stabs-in-elf.  */
4485796c8dcSSimon Schubert 		  int special_local_sect;
449cf7f2e2dSJohn Marino 
4505796c8dcSSimon Schubert 		  if (strcmp ("Bbss.bss", sym->name) == 0)
4515796c8dcSSimon Schubert 		    special_local_sect = SECT_OFF_BSS (objfile);
4525796c8dcSSimon Schubert 		  else if (strcmp ("Ddata.data", sym->name) == 0)
4535796c8dcSSimon Schubert 		    special_local_sect = SECT_OFF_DATA (objfile);
4545796c8dcSSimon Schubert 		  else if (strcmp ("Drodata.rodata", sym->name) == 0)
4555796c8dcSSimon Schubert 		    special_local_sect = SECT_OFF_RODATA (objfile);
4565796c8dcSSimon Schubert 		  else
4575796c8dcSSimon Schubert 		    special_local_sect = -1;
4585796c8dcSSimon Schubert 		  if (special_local_sect >= 0)
4595796c8dcSSimon Schubert 		    {
4605796c8dcSSimon Schubert 		      /* Found a special local symbol.  Allocate a
4615796c8dcSSimon Schubert 			 sectinfo, if needed, and fill it in.  */
4625796c8dcSSimon Schubert 		      if (sectinfo == NULL)
4635796c8dcSSimon Schubert 			{
4645796c8dcSSimon Schubert 			  int max_index;
4655796c8dcSSimon Schubert 			  size_t size;
4665796c8dcSSimon Schubert 
4675796c8dcSSimon Schubert 			  max_index = SECT_OFF_BSS (objfile);
4685796c8dcSSimon Schubert 			  if (objfile->sect_index_data > max_index)
4695796c8dcSSimon Schubert 			    max_index = objfile->sect_index_data;
4705796c8dcSSimon Schubert 			  if (objfile->sect_index_rodata > max_index)
4715796c8dcSSimon Schubert 			    max_index = objfile->sect_index_rodata;
4725796c8dcSSimon Schubert 
4735796c8dcSSimon Schubert 			  /* max_index is the largest index we'll
4745796c8dcSSimon Schubert 			     use into this array, so we must
4755796c8dcSSimon Schubert 			     allocate max_index+1 elements for it.
4765796c8dcSSimon Schubert 			     However, 'struct stab_section_info'
4775796c8dcSSimon Schubert 			     already includes one element, so we
4785796c8dcSSimon Schubert 			     need to allocate max_index aadditional
4795796c8dcSSimon Schubert 			     elements.  */
4805796c8dcSSimon Schubert 			  size = (sizeof (struct stab_section_info)
481*c50c785cSJohn Marino 				  + (sizeof (CORE_ADDR) * max_index));
4825796c8dcSSimon Schubert 			  sectinfo = (struct stab_section_info *)
4835796c8dcSSimon Schubert 			    xmalloc (size);
4845796c8dcSSimon Schubert 			  memset (sectinfo, 0, size);
4855796c8dcSSimon Schubert 			  sectinfo->num_sections = max_index;
4865796c8dcSSimon Schubert 			  if (filesym == NULL)
4875796c8dcSSimon Schubert 			    {
4885796c8dcSSimon Schubert 			      complaint (&symfile_complaints,
489*c50c785cSJohn Marino 					 _("elf/stab section information %s "
490*c50c785cSJohn Marino 					   "without a preceding file symbol"),
4915796c8dcSSimon Schubert 					 sym->name);
4925796c8dcSSimon Schubert 			    }
4935796c8dcSSimon Schubert 			  else
4945796c8dcSSimon Schubert 			    {
4955796c8dcSSimon Schubert 			      sectinfo->filename =
4965796c8dcSSimon Schubert 				(char *) filesym->name;
4975796c8dcSSimon Schubert 			    }
4985796c8dcSSimon Schubert 			}
4995796c8dcSSimon Schubert 		      if (sectinfo->sections[special_local_sect] != 0)
5005796c8dcSSimon Schubert 			complaint (&symfile_complaints,
501*c50c785cSJohn Marino 				   _("duplicated elf/stab section "
502*c50c785cSJohn Marino 				     "information for %s"),
5035796c8dcSSimon Schubert 				   sectinfo->filename);
5045796c8dcSSimon Schubert 		      /* BFD symbols are section relative.  */
5055796c8dcSSimon Schubert 		      symaddr = sym->value + sym->section->vma;
5065796c8dcSSimon Schubert 		      /* Relocate non-absolute symbols by the
5075796c8dcSSimon Schubert 			 section offset.  */
5085796c8dcSSimon Schubert 		      if (sym->section != &bfd_abs_section)
5095796c8dcSSimon Schubert 			symaddr += offset;
5105796c8dcSSimon Schubert 		      sectinfo->sections[special_local_sect] = symaddr;
5115796c8dcSSimon Schubert 		      /* The special local symbols don't go in the
5125796c8dcSSimon Schubert 			 minimal symbol table, so ignore this one.  */
5135796c8dcSSimon Schubert 		      continue;
5145796c8dcSSimon Schubert 		    }
5155796c8dcSSimon Schubert 		  /* Not a special stabs-in-elf symbol, do regular
5165796c8dcSSimon Schubert 		     symbol processing.  */
5175796c8dcSSimon Schubert 		  if (sym->section->flags & SEC_LOAD)
5185796c8dcSSimon Schubert 		    {
5195796c8dcSSimon Schubert 		      ms_type = mst_file_data;
5205796c8dcSSimon Schubert 		    }
5215796c8dcSSimon Schubert 		  else
5225796c8dcSSimon Schubert 		    {
5235796c8dcSSimon Schubert 		      ms_type = mst_file_bss;
5245796c8dcSSimon Schubert 		    }
5255796c8dcSSimon Schubert 		}
5265796c8dcSSimon Schubert 	      else
5275796c8dcSSimon Schubert 		{
5285796c8dcSSimon Schubert 		  ms_type = mst_unknown;
5295796c8dcSSimon Schubert 		}
5305796c8dcSSimon Schubert 	    }
5315796c8dcSSimon Schubert 	  else
5325796c8dcSSimon Schubert 	    {
5335796c8dcSSimon Schubert 	      /* FIXME:  Solaris2 shared libraries include lots of
5345796c8dcSSimon Schubert 		 odd "absolute" and "undefined" symbols, that play
5355796c8dcSSimon Schubert 		 hob with actions like finding what function the PC
5365796c8dcSSimon Schubert 		 is in.  Ignore them if they aren't text, data, or bss.  */
5375796c8dcSSimon Schubert 	      /* ms_type = mst_unknown; */
5385796c8dcSSimon Schubert 	      continue;	/* Skip this symbol.  */
5395796c8dcSSimon Schubert 	    }
5405796c8dcSSimon Schubert 	  msym = record_minimal_symbol
541cf7f2e2dSJohn Marino 	    (sym->name, strlen (sym->name), copy_names, symaddr,
5425796c8dcSSimon Schubert 	     ms_type, sym->section, objfile);
5435796c8dcSSimon Schubert 
5445796c8dcSSimon Schubert 	  if (msym)
5455796c8dcSSimon Schubert 	    {
5465796c8dcSSimon Schubert 	      /* Pass symbol size field in via BFD.  FIXME!!!  */
5475796c8dcSSimon Schubert 	      elf_symbol_type *elf_sym;
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert 	      /* NOTE: uweigand-20071112: A synthetic symbol does not have an
5505796c8dcSSimon Schubert 		 ELF-private part.  However, in some cases (e.g. synthetic
5515796c8dcSSimon Schubert 		 'dot' symbols on ppc64) the udata.p entry is set to point back
5525796c8dcSSimon Schubert 		 to the original ELF symbol it was derived from.  Get the size
5535796c8dcSSimon Schubert 		 from that symbol.  */
5545796c8dcSSimon Schubert 	      if (type != ST_SYNTHETIC)
5555796c8dcSSimon Schubert 		elf_sym = (elf_symbol_type *) sym;
5565796c8dcSSimon Schubert 	      else
5575796c8dcSSimon Schubert 		elf_sym = (elf_symbol_type *) sym->udata.p;
5585796c8dcSSimon Schubert 
5595796c8dcSSimon Schubert 	      if (elf_sym)
5605796c8dcSSimon Schubert 		MSYMBOL_SIZE(msym) = elf_sym->internal_elf_sym.st_size;
561cf7f2e2dSJohn Marino 
5625796c8dcSSimon Schubert 	      msym->filename = filesymname;
5635796c8dcSSimon Schubert 	      gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
564cf7f2e2dSJohn Marino 	    }
5655796c8dcSSimon Schubert 
5665796c8dcSSimon Schubert 	  /* For @plt symbols, also record a trampoline to the
5675796c8dcSSimon Schubert 	     destination symbol.  The @plt symbol will be used in
5685796c8dcSSimon Schubert 	     disassembly, and the trampoline will be used when we are
5695796c8dcSSimon Schubert 	     trying to find the target.  */
5705796c8dcSSimon Schubert 	  if (msym && ms_type == mst_text && type == ST_SYNTHETIC)
5715796c8dcSSimon Schubert 	    {
5725796c8dcSSimon Schubert 	      int len = strlen (sym->name);
5735796c8dcSSimon Schubert 
5745796c8dcSSimon Schubert 	      if (len > 4 && strcmp (sym->name + len - 4, "@plt") == 0)
5755796c8dcSSimon Schubert 		{
5765796c8dcSSimon Schubert 		  struct minimal_symbol *mtramp;
5775796c8dcSSimon Schubert 
578cf7f2e2dSJohn Marino 		  mtramp = record_minimal_symbol (sym->name, len - 4, 1,
579cf7f2e2dSJohn Marino 						  symaddr,
5805796c8dcSSimon Schubert 						  mst_solib_trampoline,
5815796c8dcSSimon Schubert 						  sym->section, objfile);
5825796c8dcSSimon Schubert 		  if (mtramp)
5835796c8dcSSimon Schubert 		    {
5845796c8dcSSimon Schubert 		      MSYMBOL_SIZE (mtramp) = MSYMBOL_SIZE (msym);
5855796c8dcSSimon Schubert 		      mtramp->filename = filesymname;
5865796c8dcSSimon Schubert 		      gdbarch_elf_make_msymbol_special (gdbarch, sym, mtramp);
5875796c8dcSSimon Schubert 		    }
5885796c8dcSSimon Schubert 		}
5895796c8dcSSimon Schubert 	    }
5905796c8dcSSimon Schubert 	}
5915796c8dcSSimon Schubert     }
5925796c8dcSSimon Schubert }
5935796c8dcSSimon Schubert 
594*c50c785cSJohn Marino /* Build minimal symbols named `function@got.plt' (see SYMBOL_GOT_PLT_SUFFIX)
595*c50c785cSJohn Marino    for later look ups of which function to call when user requests
596*c50c785cSJohn Marino    a STT_GNU_IFUNC function.  As the STT_GNU_IFUNC type is found at the target
597*c50c785cSJohn Marino    library defining `function' we cannot yet know while reading OBJFILE which
598*c50c785cSJohn Marino    of the SYMBOL_GOT_PLT_SUFFIX entries will be needed and later
599*c50c785cSJohn Marino    DYN_SYMBOL_TABLE is no longer easily available for OBJFILE.  */
600*c50c785cSJohn Marino 
601*c50c785cSJohn Marino static void
602*c50c785cSJohn Marino elf_rel_plt_read (struct objfile *objfile, asymbol **dyn_symbol_table)
603*c50c785cSJohn Marino {
604*c50c785cSJohn Marino   bfd *obfd = objfile->obfd;
605*c50c785cSJohn Marino   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
606*c50c785cSJohn Marino   asection *plt, *relplt, *got_plt;
607*c50c785cSJohn Marino   unsigned u;
608*c50c785cSJohn Marino   int plt_elf_idx;
609*c50c785cSJohn Marino   bfd_size_type reloc_count, reloc;
610*c50c785cSJohn Marino   char *string_buffer = NULL;
611*c50c785cSJohn Marino   size_t string_buffer_size = 0;
612*c50c785cSJohn Marino   struct cleanup *back_to;
613*c50c785cSJohn Marino   struct gdbarch *gdbarch = objfile->gdbarch;
614*c50c785cSJohn Marino   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
615*c50c785cSJohn Marino   size_t ptr_size = TYPE_LENGTH (ptr_type);
616*c50c785cSJohn Marino 
617*c50c785cSJohn Marino   if (objfile->separate_debug_objfile_backlink)
618*c50c785cSJohn Marino     return;
619*c50c785cSJohn Marino 
620*c50c785cSJohn Marino   plt = bfd_get_section_by_name (obfd, ".plt");
621*c50c785cSJohn Marino   if (plt == NULL)
622*c50c785cSJohn Marino     return;
623*c50c785cSJohn Marino   plt_elf_idx = elf_section_data (plt)->this_idx;
624*c50c785cSJohn Marino 
625*c50c785cSJohn Marino   got_plt = bfd_get_section_by_name (obfd, ".got.plt");
626*c50c785cSJohn Marino   if (got_plt == NULL)
627*c50c785cSJohn Marino     return;
628*c50c785cSJohn Marino 
629*c50c785cSJohn Marino   /* This search algorithm is from _bfd_elf_canonicalize_dynamic_reloc.  */
630*c50c785cSJohn Marino   for (relplt = obfd->sections; relplt != NULL; relplt = relplt->next)
631*c50c785cSJohn Marino     if (elf_section_data (relplt)->this_hdr.sh_info == plt_elf_idx
632*c50c785cSJohn Marino 	&& (elf_section_data (relplt)->this_hdr.sh_type == SHT_REL
633*c50c785cSJohn Marino 	    || elf_section_data (relplt)->this_hdr.sh_type == SHT_RELA))
634*c50c785cSJohn Marino       break;
635*c50c785cSJohn Marino   if (relplt == NULL)
636*c50c785cSJohn Marino     return;
637*c50c785cSJohn Marino 
638*c50c785cSJohn Marino   if (! bed->s->slurp_reloc_table (obfd, relplt, dyn_symbol_table, TRUE))
639*c50c785cSJohn Marino     return;
640*c50c785cSJohn Marino 
641*c50c785cSJohn Marino   back_to = make_cleanup (free_current_contents, &string_buffer);
642*c50c785cSJohn Marino 
643*c50c785cSJohn Marino   reloc_count = relplt->size / elf_section_data (relplt)->this_hdr.sh_entsize;
644*c50c785cSJohn Marino   for (reloc = 0; reloc < reloc_count; reloc++)
645*c50c785cSJohn Marino     {
646*c50c785cSJohn Marino       const char *name, *name_got_plt;
647*c50c785cSJohn Marino       struct minimal_symbol *msym;
648*c50c785cSJohn Marino       CORE_ADDR address;
649*c50c785cSJohn Marino       const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
650*c50c785cSJohn Marino       size_t name_len;
651*c50c785cSJohn Marino 
652*c50c785cSJohn Marino       name = bfd_asymbol_name (*relplt->relocation[reloc].sym_ptr_ptr);
653*c50c785cSJohn Marino       name_len = strlen (name);
654*c50c785cSJohn Marino       address = relplt->relocation[reloc].address;
655*c50c785cSJohn Marino 
656*c50c785cSJohn Marino       /* Does the pointer reside in the .got.plt section?  */
657*c50c785cSJohn Marino       if (!(bfd_get_section_vma (obfd, got_plt) <= address
658*c50c785cSJohn Marino             && address < bfd_get_section_vma (obfd, got_plt)
659*c50c785cSJohn Marino 			 + bfd_get_section_size (got_plt)))
660*c50c785cSJohn Marino 	continue;
661*c50c785cSJohn Marino 
662*c50c785cSJohn Marino       /* We cannot check if NAME is a reference to mst_text_gnu_ifunc as in
663*c50c785cSJohn Marino 	 OBJFILE the symbol is undefined and the objfile having NAME defined
664*c50c785cSJohn Marino 	 may not yet have been loaded.  */
665*c50c785cSJohn Marino 
666*c50c785cSJohn Marino       if (string_buffer_size < name_len + got_suffix_len)
667*c50c785cSJohn Marino 	{
668*c50c785cSJohn Marino 	  string_buffer_size = 2 * (name_len + got_suffix_len);
669*c50c785cSJohn Marino 	  string_buffer = xrealloc (string_buffer, string_buffer_size);
670*c50c785cSJohn Marino 	}
671*c50c785cSJohn Marino       memcpy (string_buffer, name, name_len);
672*c50c785cSJohn Marino       memcpy (&string_buffer[name_len], SYMBOL_GOT_PLT_SUFFIX,
673*c50c785cSJohn Marino 	      got_suffix_len);
674*c50c785cSJohn Marino 
675*c50c785cSJohn Marino       msym = record_minimal_symbol (string_buffer, name_len + got_suffix_len,
676*c50c785cSJohn Marino                                     1, address, mst_slot_got_plt, got_plt,
677*c50c785cSJohn Marino 				    objfile);
678*c50c785cSJohn Marino       if (msym)
679*c50c785cSJohn Marino 	MSYMBOL_SIZE (msym) = ptr_size;
680*c50c785cSJohn Marino     }
681*c50c785cSJohn Marino 
682*c50c785cSJohn Marino   do_cleanups (back_to);
683*c50c785cSJohn Marino }
684*c50c785cSJohn Marino 
685*c50c785cSJohn Marino /* The data pointer is htab_t for gnu_ifunc_record_cache_unchecked.  */
686*c50c785cSJohn Marino 
687*c50c785cSJohn Marino static const struct objfile_data *elf_objfile_gnu_ifunc_cache_data;
688*c50c785cSJohn Marino 
689*c50c785cSJohn Marino /* Map function names to CORE_ADDR in elf_objfile_gnu_ifunc_cache_data.  */
690*c50c785cSJohn Marino 
691*c50c785cSJohn Marino struct elf_gnu_ifunc_cache
692*c50c785cSJohn Marino {
693*c50c785cSJohn Marino   /* This is always a function entry address, not a function descriptor.  */
694*c50c785cSJohn Marino   CORE_ADDR addr;
695*c50c785cSJohn Marino 
696*c50c785cSJohn Marino   char name[1];
697*c50c785cSJohn Marino };
698*c50c785cSJohn Marino 
699*c50c785cSJohn Marino /* htab_hash for elf_objfile_gnu_ifunc_cache_data.  */
700*c50c785cSJohn Marino 
701*c50c785cSJohn Marino static hashval_t
702*c50c785cSJohn Marino elf_gnu_ifunc_cache_hash (const void *a_voidp)
703*c50c785cSJohn Marino {
704*c50c785cSJohn Marino   const struct elf_gnu_ifunc_cache *a = a_voidp;
705*c50c785cSJohn Marino 
706*c50c785cSJohn Marino   return htab_hash_string (a->name);
707*c50c785cSJohn Marino }
708*c50c785cSJohn Marino 
709*c50c785cSJohn Marino /* htab_eq for elf_objfile_gnu_ifunc_cache_data.  */
710*c50c785cSJohn Marino 
711*c50c785cSJohn Marino static int
712*c50c785cSJohn Marino elf_gnu_ifunc_cache_eq (const void *a_voidp, const void *b_voidp)
713*c50c785cSJohn Marino {
714*c50c785cSJohn Marino   const struct elf_gnu_ifunc_cache *a = a_voidp;
715*c50c785cSJohn Marino   const struct elf_gnu_ifunc_cache *b = b_voidp;
716*c50c785cSJohn Marino 
717*c50c785cSJohn Marino   return strcmp (a->name, b->name) == 0;
718*c50c785cSJohn Marino }
719*c50c785cSJohn Marino 
720*c50c785cSJohn Marino /* Record the target function address of a STT_GNU_IFUNC function NAME is the
721*c50c785cSJohn Marino    function entry address ADDR.  Return 1 if NAME and ADDR are considered as
722*c50c785cSJohn Marino    valid and therefore they were successfully recorded, return 0 otherwise.
723*c50c785cSJohn Marino 
724*c50c785cSJohn Marino    Function does not expect a duplicate entry.  Use
725*c50c785cSJohn Marino    elf_gnu_ifunc_resolve_by_cache first to check if the entry for NAME already
726*c50c785cSJohn Marino    exists.  */
727*c50c785cSJohn Marino 
728*c50c785cSJohn Marino static int
729*c50c785cSJohn Marino elf_gnu_ifunc_record_cache (const char *name, CORE_ADDR addr)
730*c50c785cSJohn Marino {
731*c50c785cSJohn Marino   struct minimal_symbol *msym;
732*c50c785cSJohn Marino   asection *sect;
733*c50c785cSJohn Marino   struct objfile *objfile;
734*c50c785cSJohn Marino   htab_t htab;
735*c50c785cSJohn Marino   struct elf_gnu_ifunc_cache entry_local, *entry_p;
736*c50c785cSJohn Marino   void **slot;
737*c50c785cSJohn Marino 
738*c50c785cSJohn Marino   msym = lookup_minimal_symbol_by_pc (addr);
739*c50c785cSJohn Marino   if (msym == NULL)
740*c50c785cSJohn Marino     return 0;
741*c50c785cSJohn Marino   if (SYMBOL_VALUE_ADDRESS (msym) != addr)
742*c50c785cSJohn Marino     return 0;
743*c50c785cSJohn Marino   /* minimal symbols have always SYMBOL_OBJ_SECTION non-NULL.  */
744*c50c785cSJohn Marino   sect = SYMBOL_OBJ_SECTION (msym)->the_bfd_section;
745*c50c785cSJohn Marino   objfile = SYMBOL_OBJ_SECTION (msym)->objfile;
746*c50c785cSJohn Marino 
747*c50c785cSJohn Marino   /* If .plt jumps back to .plt the symbol is still deferred for later
748*c50c785cSJohn Marino      resolution and it has no use for GDB.  Besides ".text" this symbol can
749*c50c785cSJohn Marino      reside also in ".opd" for ppc64 function descriptor.  */
750*c50c785cSJohn Marino   if (strcmp (bfd_get_section_name (objfile->obfd, sect), ".plt") == 0)
751*c50c785cSJohn Marino     return 0;
752*c50c785cSJohn Marino 
753*c50c785cSJohn Marino   htab = objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data);
754*c50c785cSJohn Marino   if (htab == NULL)
755*c50c785cSJohn Marino     {
756*c50c785cSJohn Marino       htab = htab_create_alloc_ex (1, elf_gnu_ifunc_cache_hash,
757*c50c785cSJohn Marino 				   elf_gnu_ifunc_cache_eq,
758*c50c785cSJohn Marino 				   NULL, &objfile->objfile_obstack,
759*c50c785cSJohn Marino 				   hashtab_obstack_allocate,
760*c50c785cSJohn Marino 				   dummy_obstack_deallocate);
761*c50c785cSJohn Marino       set_objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data, htab);
762*c50c785cSJohn Marino     }
763*c50c785cSJohn Marino 
764*c50c785cSJohn Marino   entry_local.addr = addr;
765*c50c785cSJohn Marino   obstack_grow (&objfile->objfile_obstack, &entry_local,
766*c50c785cSJohn Marino 		offsetof (struct elf_gnu_ifunc_cache, name));
767*c50c785cSJohn Marino   obstack_grow_str0 (&objfile->objfile_obstack, name);
768*c50c785cSJohn Marino   entry_p = obstack_finish (&objfile->objfile_obstack);
769*c50c785cSJohn Marino 
770*c50c785cSJohn Marino   slot = htab_find_slot (htab, entry_p, INSERT);
771*c50c785cSJohn Marino   if (*slot != NULL)
772*c50c785cSJohn Marino     {
773*c50c785cSJohn Marino       struct elf_gnu_ifunc_cache *entry_found_p = *slot;
774*c50c785cSJohn Marino       struct gdbarch *gdbarch = objfile->gdbarch;
775*c50c785cSJohn Marino 
776*c50c785cSJohn Marino       if (entry_found_p->addr != addr)
777*c50c785cSJohn Marino 	{
778*c50c785cSJohn Marino 	  /* This case indicates buggy inferior program, the resolved address
779*c50c785cSJohn Marino 	     should never change.  */
780*c50c785cSJohn Marino 
781*c50c785cSJohn Marino 	    warning (_("gnu-indirect-function \"%s\" has changed its resolved "
782*c50c785cSJohn Marino 		       "function_address from %s to %s"),
783*c50c785cSJohn Marino 		     name, paddress (gdbarch, entry_found_p->addr),
784*c50c785cSJohn Marino 		     paddress (gdbarch, addr));
785*c50c785cSJohn Marino 	}
786*c50c785cSJohn Marino 
787*c50c785cSJohn Marino       /* New ENTRY_P is here leaked/duplicate in the OBJFILE obstack.  */
788*c50c785cSJohn Marino     }
789*c50c785cSJohn Marino   *slot = entry_p;
790*c50c785cSJohn Marino 
791*c50c785cSJohn Marino   return 1;
792*c50c785cSJohn Marino }
793*c50c785cSJohn Marino 
794*c50c785cSJohn Marino /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
795*c50c785cSJohn Marino    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
796*c50c785cSJohn Marino    is not NULL) and the function returns 1.  It returns 0 otherwise.
797*c50c785cSJohn Marino 
798*c50c785cSJohn Marino    Only the elf_objfile_gnu_ifunc_cache_data hash table is searched by this
799*c50c785cSJohn Marino    function.  */
800*c50c785cSJohn Marino 
801*c50c785cSJohn Marino static int
802*c50c785cSJohn Marino elf_gnu_ifunc_resolve_by_cache (const char *name, CORE_ADDR *addr_p)
803*c50c785cSJohn Marino {
804*c50c785cSJohn Marino   struct objfile *objfile;
805*c50c785cSJohn Marino 
806*c50c785cSJohn Marino   ALL_PSPACE_OBJFILES (current_program_space, objfile)
807*c50c785cSJohn Marino     {
808*c50c785cSJohn Marino       htab_t htab;
809*c50c785cSJohn Marino       struct elf_gnu_ifunc_cache *entry_p;
810*c50c785cSJohn Marino       void **slot;
811*c50c785cSJohn Marino 
812*c50c785cSJohn Marino       htab = objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data);
813*c50c785cSJohn Marino       if (htab == NULL)
814*c50c785cSJohn Marino 	continue;
815*c50c785cSJohn Marino 
816*c50c785cSJohn Marino       entry_p = alloca (sizeof (*entry_p) + strlen (name));
817*c50c785cSJohn Marino       strcpy (entry_p->name, name);
818*c50c785cSJohn Marino 
819*c50c785cSJohn Marino       slot = htab_find_slot (htab, entry_p, NO_INSERT);
820*c50c785cSJohn Marino       if (slot == NULL)
821*c50c785cSJohn Marino 	continue;
822*c50c785cSJohn Marino       entry_p = *slot;
823*c50c785cSJohn Marino       gdb_assert (entry_p != NULL);
824*c50c785cSJohn Marino 
825*c50c785cSJohn Marino       if (addr_p)
826*c50c785cSJohn Marino 	*addr_p = entry_p->addr;
827*c50c785cSJohn Marino       return 1;
828*c50c785cSJohn Marino     }
829*c50c785cSJohn Marino 
830*c50c785cSJohn Marino   return 0;
831*c50c785cSJohn Marino }
832*c50c785cSJohn Marino 
833*c50c785cSJohn Marino /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
834*c50c785cSJohn Marino    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
835*c50c785cSJohn Marino    is not NULL) and the function returns 1.  It returns 0 otherwise.
836*c50c785cSJohn Marino 
837*c50c785cSJohn Marino    Only the SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.
838*c50c785cSJohn Marino    elf_gnu_ifunc_resolve_by_cache must have been already called for NAME to
839*c50c785cSJohn Marino    prevent cache entries duplicates.  */
840*c50c785cSJohn Marino 
841*c50c785cSJohn Marino static int
842*c50c785cSJohn Marino elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
843*c50c785cSJohn Marino {
844*c50c785cSJohn Marino   char *name_got_plt;
845*c50c785cSJohn Marino   struct objfile *objfile;
846*c50c785cSJohn Marino   const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
847*c50c785cSJohn Marino 
848*c50c785cSJohn Marino   name_got_plt = alloca (strlen (name) + got_suffix_len + 1);
849*c50c785cSJohn Marino   sprintf (name_got_plt, "%s" SYMBOL_GOT_PLT_SUFFIX, name);
850*c50c785cSJohn Marino 
851*c50c785cSJohn Marino   ALL_PSPACE_OBJFILES (current_program_space, objfile)
852*c50c785cSJohn Marino     {
853*c50c785cSJohn Marino       bfd *obfd = objfile->obfd;
854*c50c785cSJohn Marino       struct gdbarch *gdbarch = objfile->gdbarch;
855*c50c785cSJohn Marino       struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
856*c50c785cSJohn Marino       size_t ptr_size = TYPE_LENGTH (ptr_type);
857*c50c785cSJohn Marino       CORE_ADDR pointer_address, addr;
858*c50c785cSJohn Marino       asection *plt;
859*c50c785cSJohn Marino       gdb_byte *buf = alloca (ptr_size);
860*c50c785cSJohn Marino       struct minimal_symbol *msym;
861*c50c785cSJohn Marino 
862*c50c785cSJohn Marino       msym = lookup_minimal_symbol (name_got_plt, NULL, objfile);
863*c50c785cSJohn Marino       if (msym == NULL)
864*c50c785cSJohn Marino 	continue;
865*c50c785cSJohn Marino       if (MSYMBOL_TYPE (msym) != mst_slot_got_plt)
866*c50c785cSJohn Marino 	continue;
867*c50c785cSJohn Marino       pointer_address = SYMBOL_VALUE_ADDRESS (msym);
868*c50c785cSJohn Marino 
869*c50c785cSJohn Marino       plt = bfd_get_section_by_name (obfd, ".plt");
870*c50c785cSJohn Marino       if (plt == NULL)
871*c50c785cSJohn Marino 	continue;
872*c50c785cSJohn Marino 
873*c50c785cSJohn Marino       if (MSYMBOL_SIZE (msym) != ptr_size)
874*c50c785cSJohn Marino 	continue;
875*c50c785cSJohn Marino       if (target_read_memory (pointer_address, buf, ptr_size) != 0)
876*c50c785cSJohn Marino 	continue;
877*c50c785cSJohn Marino       addr = extract_typed_address (buf, ptr_type);
878*c50c785cSJohn Marino       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
879*c50c785cSJohn Marino 						 &current_target);
880*c50c785cSJohn Marino 
881*c50c785cSJohn Marino       if (addr_p)
882*c50c785cSJohn Marino 	*addr_p = addr;
883*c50c785cSJohn Marino       if (elf_gnu_ifunc_record_cache (name, addr))
884*c50c785cSJohn Marino 	return 1;
885*c50c785cSJohn Marino     }
886*c50c785cSJohn Marino 
887*c50c785cSJohn Marino   return 0;
888*c50c785cSJohn Marino }
889*c50c785cSJohn Marino 
890*c50c785cSJohn Marino /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
891*c50c785cSJohn Marino    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
892*c50c785cSJohn Marino    is not NULL) and the function returns 1.  It returns 0 otherwise.
893*c50c785cSJohn Marino 
894*c50c785cSJohn Marino    Both the elf_objfile_gnu_ifunc_cache_data hash table and
895*c50c785cSJohn Marino    SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.  */
896*c50c785cSJohn Marino 
897*c50c785cSJohn Marino static int
898*c50c785cSJohn Marino elf_gnu_ifunc_resolve_name (const char *name, CORE_ADDR *addr_p)
899*c50c785cSJohn Marino {
900*c50c785cSJohn Marino   if (elf_gnu_ifunc_resolve_by_cache (name, addr_p))
901*c50c785cSJohn Marino     return 1;
902*c50c785cSJohn Marino 
903*c50c785cSJohn Marino   if (elf_gnu_ifunc_resolve_by_got (name, addr_p))
904*c50c785cSJohn Marino     return 1;
905*c50c785cSJohn Marino 
906*c50c785cSJohn Marino   return 0;
907*c50c785cSJohn Marino }
908*c50c785cSJohn Marino 
909*c50c785cSJohn Marino /* Call STT_GNU_IFUNC - a function returning addresss of a real function to
910*c50c785cSJohn Marino    call.  PC is theSTT_GNU_IFUNC resolving function entry.  The value returned
911*c50c785cSJohn Marino    is the entry point of the resolved STT_GNU_IFUNC target function to call.
912*c50c785cSJohn Marino    */
913*c50c785cSJohn Marino 
914*c50c785cSJohn Marino static CORE_ADDR
915*c50c785cSJohn Marino elf_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
916*c50c785cSJohn Marino {
917*c50c785cSJohn Marino   char *name_at_pc;
918*c50c785cSJohn Marino   CORE_ADDR start_at_pc, address;
919*c50c785cSJohn Marino   struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
920*c50c785cSJohn Marino   struct value *function, *address_val;
921*c50c785cSJohn Marino 
922*c50c785cSJohn Marino   /* Try first any non-intrusive methods without an inferior call.  */
923*c50c785cSJohn Marino 
924*c50c785cSJohn Marino   if (find_pc_partial_function (pc, &name_at_pc, &start_at_pc, NULL)
925*c50c785cSJohn Marino       && start_at_pc == pc)
926*c50c785cSJohn Marino     {
927*c50c785cSJohn Marino       if (elf_gnu_ifunc_resolve_name (name_at_pc, &address))
928*c50c785cSJohn Marino 	return address;
929*c50c785cSJohn Marino     }
930*c50c785cSJohn Marino   else
931*c50c785cSJohn Marino     name_at_pc = NULL;
932*c50c785cSJohn Marino 
933*c50c785cSJohn Marino   function = allocate_value (func_func_type);
934*c50c785cSJohn Marino   set_value_address (function, pc);
935*c50c785cSJohn Marino 
936*c50c785cSJohn Marino   /* STT_GNU_IFUNC resolver functions have no parameters.  FUNCTION is the
937*c50c785cSJohn Marino      function entry address.  ADDRESS may be a function descriptor.  */
938*c50c785cSJohn Marino 
939*c50c785cSJohn Marino   address_val = call_function_by_hand (function, 0, NULL);
940*c50c785cSJohn Marino   address = value_as_address (address_val);
941*c50c785cSJohn Marino   address = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
942*c50c785cSJohn Marino 						&current_target);
943*c50c785cSJohn Marino 
944*c50c785cSJohn Marino   if (name_at_pc)
945*c50c785cSJohn Marino     elf_gnu_ifunc_record_cache (name_at_pc, address);
946*c50c785cSJohn Marino 
947*c50c785cSJohn Marino   return address;
948*c50c785cSJohn Marino }
949*c50c785cSJohn Marino 
950*c50c785cSJohn Marino /* Handle inferior hit of bp_gnu_ifunc_resolver, see its definition.  */
951*c50c785cSJohn Marino 
952*c50c785cSJohn Marino static void
953*c50c785cSJohn Marino elf_gnu_ifunc_resolver_stop (struct breakpoint *b)
954*c50c785cSJohn Marino {
955*c50c785cSJohn Marino   struct breakpoint *b_return;
956*c50c785cSJohn Marino   struct frame_info *prev_frame = get_prev_frame (get_current_frame ());
957*c50c785cSJohn Marino   struct frame_id prev_frame_id = get_stack_frame_id (prev_frame);
958*c50c785cSJohn Marino   CORE_ADDR prev_pc = get_frame_pc (prev_frame);
959*c50c785cSJohn Marino   int thread_id = pid_to_thread_id (inferior_ptid);
960*c50c785cSJohn Marino 
961*c50c785cSJohn Marino   gdb_assert (b->type == bp_gnu_ifunc_resolver);
962*c50c785cSJohn Marino 
963*c50c785cSJohn Marino   for (b_return = b->related_breakpoint; b_return != b;
964*c50c785cSJohn Marino        b_return = b_return->related_breakpoint)
965*c50c785cSJohn Marino     {
966*c50c785cSJohn Marino       gdb_assert (b_return->type == bp_gnu_ifunc_resolver_return);
967*c50c785cSJohn Marino       gdb_assert (b_return->loc != NULL && b_return->loc->next == NULL);
968*c50c785cSJohn Marino       gdb_assert (frame_id_p (b_return->frame_id));
969*c50c785cSJohn Marino 
970*c50c785cSJohn Marino       if (b_return->thread == thread_id
971*c50c785cSJohn Marino 	  && b_return->loc->requested_address == prev_pc
972*c50c785cSJohn Marino 	  && frame_id_eq (b_return->frame_id, prev_frame_id))
973*c50c785cSJohn Marino 	break;
974*c50c785cSJohn Marino     }
975*c50c785cSJohn Marino 
976*c50c785cSJohn Marino   if (b_return == b)
977*c50c785cSJohn Marino     {
978*c50c785cSJohn Marino       struct symtab_and_line sal;
979*c50c785cSJohn Marino 
980*c50c785cSJohn Marino       /* No need to call find_pc_line for symbols resolving as this is only
981*c50c785cSJohn Marino 	 a helper breakpointer never shown to the user.  */
982*c50c785cSJohn Marino 
983*c50c785cSJohn Marino       init_sal (&sal);
984*c50c785cSJohn Marino       sal.pspace = current_inferior ()->pspace;
985*c50c785cSJohn Marino       sal.pc = prev_pc;
986*c50c785cSJohn Marino       sal.section = find_pc_overlay (sal.pc);
987*c50c785cSJohn Marino       sal.explicit_pc = 1;
988*c50c785cSJohn Marino       b_return = set_momentary_breakpoint (get_frame_arch (prev_frame), sal,
989*c50c785cSJohn Marino 					   prev_frame_id,
990*c50c785cSJohn Marino 					   bp_gnu_ifunc_resolver_return);
991*c50c785cSJohn Marino 
992*c50c785cSJohn Marino       /* Add new b_return to the ring list b->related_breakpoint.  */
993*c50c785cSJohn Marino       gdb_assert (b_return->related_breakpoint == b_return);
994*c50c785cSJohn Marino       b_return->related_breakpoint = b->related_breakpoint;
995*c50c785cSJohn Marino       b->related_breakpoint = b_return;
996*c50c785cSJohn Marino     }
997*c50c785cSJohn Marino }
998*c50c785cSJohn Marino 
999*c50c785cSJohn Marino /* Handle inferior hit of bp_gnu_ifunc_resolver_return, see its definition.  */
1000*c50c785cSJohn Marino 
1001*c50c785cSJohn Marino static void
1002*c50c785cSJohn Marino elf_gnu_ifunc_resolver_return_stop (struct breakpoint *b)
1003*c50c785cSJohn Marino {
1004*c50c785cSJohn Marino   struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
1005*c50c785cSJohn Marino   struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
1006*c50c785cSJohn Marino   struct type *value_type = TYPE_TARGET_TYPE (func_func_type);
1007*c50c785cSJohn Marino   struct regcache *regcache = get_thread_regcache (inferior_ptid);
1008*c50c785cSJohn Marino   struct value *value;
1009*c50c785cSJohn Marino   CORE_ADDR resolved_address, resolved_pc;
1010*c50c785cSJohn Marino   struct symtab_and_line sal;
1011*c50c785cSJohn Marino   struct symtabs_and_lines sals, sals_end;
1012*c50c785cSJohn Marino 
1013*c50c785cSJohn Marino   gdb_assert (b->type == bp_gnu_ifunc_resolver_return);
1014*c50c785cSJohn Marino 
1015*c50c785cSJohn Marino   value = allocate_value (value_type);
1016*c50c785cSJohn Marino   gdbarch_return_value (gdbarch, func_func_type, value_type, regcache,
1017*c50c785cSJohn Marino 			value_contents_raw (value), NULL);
1018*c50c785cSJohn Marino   resolved_address = value_as_address (value);
1019*c50c785cSJohn Marino   resolved_pc = gdbarch_convert_from_func_ptr_addr (gdbarch,
1020*c50c785cSJohn Marino 						    resolved_address,
1021*c50c785cSJohn Marino 						    &current_target);
1022*c50c785cSJohn Marino 
1023*c50c785cSJohn Marino   while (b->related_breakpoint != b)
1024*c50c785cSJohn Marino     {
1025*c50c785cSJohn Marino       struct breakpoint *b_next = b->related_breakpoint;
1026*c50c785cSJohn Marino 
1027*c50c785cSJohn Marino       switch (b->type)
1028*c50c785cSJohn Marino 	{
1029*c50c785cSJohn Marino 	case bp_gnu_ifunc_resolver:
1030*c50c785cSJohn Marino 	  break;
1031*c50c785cSJohn Marino 	case bp_gnu_ifunc_resolver_return:
1032*c50c785cSJohn Marino 	  delete_breakpoint (b);
1033*c50c785cSJohn Marino 	  break;
1034*c50c785cSJohn Marino 	default:
1035*c50c785cSJohn Marino 	  internal_error (__FILE__, __LINE__,
1036*c50c785cSJohn Marino 			  _("handle_inferior_event: Invalid "
1037*c50c785cSJohn Marino 			    "gnu-indirect-function breakpoint type %d"),
1038*c50c785cSJohn Marino 			  (int) b->type);
1039*c50c785cSJohn Marino 	}
1040*c50c785cSJohn Marino       b = b_next;
1041*c50c785cSJohn Marino     }
1042*c50c785cSJohn Marino   gdb_assert (b->type == bp_gnu_ifunc_resolver);
1043*c50c785cSJohn Marino 
1044*c50c785cSJohn Marino   gdb_assert (current_program_space == b->pspace);
1045*c50c785cSJohn Marino   elf_gnu_ifunc_record_cache (b->addr_string, resolved_pc);
1046*c50c785cSJohn Marino 
1047*c50c785cSJohn Marino   sal = find_pc_line (resolved_pc, 0);
1048*c50c785cSJohn Marino   sals.nelts = 1;
1049*c50c785cSJohn Marino   sals.sals = &sal;
1050*c50c785cSJohn Marino   sals_end.nelts = 0;
1051*c50c785cSJohn Marino 
1052*c50c785cSJohn Marino   b->type = bp_breakpoint;
1053*c50c785cSJohn Marino   update_breakpoint_locations (b, sals, sals_end);
1054*c50c785cSJohn Marino }
1055*c50c785cSJohn Marino 
1056cf7f2e2dSJohn Marino struct build_id
1057cf7f2e2dSJohn Marino   {
1058cf7f2e2dSJohn Marino     size_t size;
1059cf7f2e2dSJohn Marino     gdb_byte data[1];
1060cf7f2e2dSJohn Marino   };
1061cf7f2e2dSJohn Marino 
1062cf7f2e2dSJohn Marino /* Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
1063cf7f2e2dSJohn Marino 
1064cf7f2e2dSJohn Marino static struct build_id *
1065cf7f2e2dSJohn Marino build_id_bfd_get (bfd *abfd)
1066cf7f2e2dSJohn Marino {
1067cf7f2e2dSJohn Marino   struct build_id *retval;
1068cf7f2e2dSJohn Marino 
1069cf7f2e2dSJohn Marino   if (!bfd_check_format (abfd, bfd_object)
1070cf7f2e2dSJohn Marino       || bfd_get_flavour (abfd) != bfd_target_elf_flavour
1071cf7f2e2dSJohn Marino       || elf_tdata (abfd)->build_id == NULL)
1072cf7f2e2dSJohn Marino     return NULL;
1073cf7f2e2dSJohn Marino 
1074cf7f2e2dSJohn Marino   retval = xmalloc (sizeof *retval - 1 + elf_tdata (abfd)->build_id_size);
1075cf7f2e2dSJohn Marino   retval->size = elf_tdata (abfd)->build_id_size;
1076cf7f2e2dSJohn Marino   memcpy (retval->data, elf_tdata (abfd)->build_id, retval->size);
1077cf7f2e2dSJohn Marino 
1078cf7f2e2dSJohn Marino   return retval;
1079cf7f2e2dSJohn Marino }
1080cf7f2e2dSJohn Marino 
1081cf7f2e2dSJohn Marino /* Return if FILENAME has NT_GNU_BUILD_ID matching the CHECK value.  */
1082cf7f2e2dSJohn Marino 
1083cf7f2e2dSJohn Marino static int
1084cf7f2e2dSJohn Marino build_id_verify (const char *filename, struct build_id *check)
1085cf7f2e2dSJohn Marino {
1086cf7f2e2dSJohn Marino   bfd *abfd;
1087cf7f2e2dSJohn Marino   struct build_id *found = NULL;
1088cf7f2e2dSJohn Marino   int retval = 0;
1089cf7f2e2dSJohn Marino 
1090cf7f2e2dSJohn Marino   /* We expect to be silent on the non-existing files.  */
1091cf7f2e2dSJohn Marino   abfd = bfd_open_maybe_remote (filename);
1092cf7f2e2dSJohn Marino   if (abfd == NULL)
1093cf7f2e2dSJohn Marino     return 0;
1094cf7f2e2dSJohn Marino 
1095cf7f2e2dSJohn Marino   found = build_id_bfd_get (abfd);
1096cf7f2e2dSJohn Marino 
1097cf7f2e2dSJohn Marino   if (found == NULL)
1098cf7f2e2dSJohn Marino     warning (_("File \"%s\" has no build-id, file skipped"), filename);
1099cf7f2e2dSJohn Marino   else if (found->size != check->size
1100cf7f2e2dSJohn Marino            || memcmp (found->data, check->data, found->size) != 0)
1101*c50c785cSJohn Marino     warning (_("File \"%s\" has a different build-id, file skipped"),
1102*c50c785cSJohn Marino 	     filename);
1103cf7f2e2dSJohn Marino   else
1104cf7f2e2dSJohn Marino     retval = 1;
1105cf7f2e2dSJohn Marino 
1106cf7f2e2dSJohn Marino   gdb_bfd_close_or_warn (abfd);
1107cf7f2e2dSJohn Marino 
1108cf7f2e2dSJohn Marino   xfree (found);
1109cf7f2e2dSJohn Marino 
1110cf7f2e2dSJohn Marino   return retval;
1111cf7f2e2dSJohn Marino }
1112cf7f2e2dSJohn Marino 
1113cf7f2e2dSJohn Marino static char *
1114cf7f2e2dSJohn Marino build_id_to_debug_filename (struct build_id *build_id)
1115cf7f2e2dSJohn Marino {
1116cf7f2e2dSJohn Marino   char *link, *debugdir, *retval = NULL;
1117cf7f2e2dSJohn Marino 
1118cf7f2e2dSJohn Marino   /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
1119cf7f2e2dSJohn Marino   link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
1120cf7f2e2dSJohn Marino 		 + 2 * build_id->size + (sizeof ".debug" - 1) + 1);
1121cf7f2e2dSJohn Marino 
1122cf7f2e2dSJohn Marino   /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
1123cf7f2e2dSJohn Marino      cause "/.build-id/..." lookups.  */
1124cf7f2e2dSJohn Marino 
1125cf7f2e2dSJohn Marino   debugdir = debug_file_directory;
1126cf7f2e2dSJohn Marino   do
1127cf7f2e2dSJohn Marino     {
1128cf7f2e2dSJohn Marino       char *s, *debugdir_end;
1129cf7f2e2dSJohn Marino       gdb_byte *data = build_id->data;
1130cf7f2e2dSJohn Marino       size_t size = build_id->size;
1131cf7f2e2dSJohn Marino 
1132cf7f2e2dSJohn Marino       while (*debugdir == DIRNAME_SEPARATOR)
1133cf7f2e2dSJohn Marino 	debugdir++;
1134cf7f2e2dSJohn Marino 
1135cf7f2e2dSJohn Marino       debugdir_end = strchr (debugdir, DIRNAME_SEPARATOR);
1136cf7f2e2dSJohn Marino       if (debugdir_end == NULL)
1137cf7f2e2dSJohn Marino 	debugdir_end = &debugdir[strlen (debugdir)];
1138cf7f2e2dSJohn Marino 
1139cf7f2e2dSJohn Marino       memcpy (link, debugdir, debugdir_end - debugdir);
1140cf7f2e2dSJohn Marino       s = &link[debugdir_end - debugdir];
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       debugdir = debugdir_end;
1167cf7f2e2dSJohn Marino     }
1168cf7f2e2dSJohn Marino   while (*debugdir != 0);
1169cf7f2e2dSJohn Marino 
1170cf7f2e2dSJohn Marino   return retval;
1171cf7f2e2dSJohn Marino }
1172cf7f2e2dSJohn Marino 
1173cf7f2e2dSJohn Marino static char *
1174cf7f2e2dSJohn Marino find_separate_debug_file_by_buildid (struct objfile *objfile)
1175cf7f2e2dSJohn Marino {
1176cf7f2e2dSJohn Marino   struct build_id *build_id;
1177cf7f2e2dSJohn Marino 
1178cf7f2e2dSJohn Marino   build_id = build_id_bfd_get (objfile->obfd);
1179cf7f2e2dSJohn Marino   if (build_id != NULL)
1180cf7f2e2dSJohn Marino     {
1181cf7f2e2dSJohn Marino       char *build_id_name;
1182cf7f2e2dSJohn Marino 
1183cf7f2e2dSJohn Marino       build_id_name = build_id_to_debug_filename (build_id);
1184cf7f2e2dSJohn Marino       xfree (build_id);
1185cf7f2e2dSJohn Marino       /* Prevent looping on a stripped .debug file.  */
1186*c50c785cSJohn Marino       if (build_id_name != NULL
1187*c50c785cSJohn Marino 	  && filename_cmp (build_id_name, objfile->name) == 0)
1188cf7f2e2dSJohn Marino         {
1189cf7f2e2dSJohn Marino 	  warning (_("\"%s\": separate debug info file has no debug info"),
1190cf7f2e2dSJohn Marino 		   build_id_name);
1191cf7f2e2dSJohn Marino 	  xfree (build_id_name);
1192cf7f2e2dSJohn Marino 	}
1193cf7f2e2dSJohn Marino       else if (build_id_name != NULL)
1194cf7f2e2dSJohn Marino         return build_id_name;
1195cf7f2e2dSJohn Marino     }
1196cf7f2e2dSJohn Marino   return NULL;
1197cf7f2e2dSJohn Marino }
1198cf7f2e2dSJohn Marino 
11995796c8dcSSimon Schubert /* Scan and build partial symbols for a symbol file.
12005796c8dcSSimon Schubert    We have been initialized by a call to elf_symfile_init, which
12015796c8dcSSimon Schubert    currently does nothing.
12025796c8dcSSimon Schubert 
12035796c8dcSSimon Schubert    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
12045796c8dcSSimon Schubert    in each section.  We simplify it down to a single offset for all
12055796c8dcSSimon Schubert    symbols.  FIXME.
12065796c8dcSSimon Schubert 
12075796c8dcSSimon Schubert    This function only does the minimum work necessary for letting the
12085796c8dcSSimon Schubert    user "name" things symbolically; it does not read the entire symtab.
12095796c8dcSSimon Schubert    Instead, it reads the external and static symbols and puts them in partial
12105796c8dcSSimon Schubert    symbol tables.  When more extensive information is requested of a
12115796c8dcSSimon Schubert    file, the corresponding partial symbol table is mutated into a full
12125796c8dcSSimon Schubert    fledged symbol table by going back and reading the symbols
12135796c8dcSSimon Schubert    for real.
12145796c8dcSSimon Schubert 
12155796c8dcSSimon Schubert    We look for sections with specific names, to tell us what debug
12165796c8dcSSimon Schubert    format to look for:  FIXME!!!
12175796c8dcSSimon Schubert 
12185796c8dcSSimon Schubert    elfstab_build_psymtabs() handles STABS symbols;
12195796c8dcSSimon Schubert    mdebug_build_psymtabs() handles ECOFF debugging information.
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert    Note that ELF files have a "minimal" symbol table, which looks a lot
12225796c8dcSSimon Schubert    like a COFF symbol table, but has only the minimal information necessary
12235796c8dcSSimon Schubert    for linking.  We process this also, and use the information to
12245796c8dcSSimon Schubert    build gdb's minimal symbol table.  This gives us some minimal debugging
12255796c8dcSSimon Schubert    capability even for files compiled without -g.  */
12265796c8dcSSimon Schubert 
12275796c8dcSSimon Schubert static void
1228cf7f2e2dSJohn Marino elf_symfile_read (struct objfile *objfile, int symfile_flags)
12295796c8dcSSimon Schubert {
12305796c8dcSSimon Schubert   bfd *abfd = objfile->obfd;
12315796c8dcSSimon Schubert   struct elfinfo ei;
12325796c8dcSSimon Schubert   struct cleanup *back_to;
12335796c8dcSSimon Schubert   long symcount = 0, dynsymcount = 0, synthcount, storage_needed;
12345796c8dcSSimon Schubert   asymbol **symbol_table = NULL, **dyn_symbol_table = NULL;
12355796c8dcSSimon Schubert   asymbol *synthsyms;
12365796c8dcSSimon Schubert 
12375796c8dcSSimon Schubert   init_minimal_symbol_collection ();
12385796c8dcSSimon Schubert   back_to = make_cleanup_discard_minimal_symbols ();
12395796c8dcSSimon Schubert 
12405796c8dcSSimon Schubert   memset ((char *) &ei, 0, sizeof (ei));
12415796c8dcSSimon Schubert 
1242*c50c785cSJohn Marino   /* Allocate struct to keep track of the symfile.  */
12435796c8dcSSimon Schubert   objfile->deprecated_sym_stab_info = (struct dbx_symfile_info *)
12445796c8dcSSimon Schubert     xmalloc (sizeof (struct dbx_symfile_info));
1245*c50c785cSJohn Marino   memset ((char *) objfile->deprecated_sym_stab_info,
1246*c50c785cSJohn Marino 	  0, sizeof (struct dbx_symfile_info));
12475796c8dcSSimon Schubert   make_cleanup (free_elfinfo, (void *) objfile);
12485796c8dcSSimon Schubert 
12495796c8dcSSimon Schubert   /* Process the normal ELF symbol table first.  This may write some
1250*c50c785cSJohn Marino      chain of info into the dbx_symfile_info in
1251*c50c785cSJohn Marino      objfile->deprecated_sym_stab_info, which can later be used by
1252*c50c785cSJohn Marino      elfstab_offset_sections.  */
12535796c8dcSSimon Schubert 
12545796c8dcSSimon Schubert   storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
12555796c8dcSSimon Schubert   if (storage_needed < 0)
1256*c50c785cSJohn Marino     error (_("Can't read symbols from %s: %s"),
1257*c50c785cSJohn Marino 	   bfd_get_filename (objfile->obfd),
12585796c8dcSSimon Schubert 	   bfd_errmsg (bfd_get_error ()));
12595796c8dcSSimon Schubert 
12605796c8dcSSimon Schubert   if (storage_needed > 0)
12615796c8dcSSimon Schubert     {
12625796c8dcSSimon Schubert       symbol_table = (asymbol **) xmalloc (storage_needed);
12635796c8dcSSimon Schubert       make_cleanup (xfree, symbol_table);
12645796c8dcSSimon Schubert       symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
12655796c8dcSSimon Schubert 
12665796c8dcSSimon Schubert       if (symcount < 0)
1267*c50c785cSJohn Marino 	error (_("Can't read symbols from %s: %s"),
1268*c50c785cSJohn Marino 	       bfd_get_filename (objfile->obfd),
12695796c8dcSSimon Schubert 	       bfd_errmsg (bfd_get_error ()));
12705796c8dcSSimon Schubert 
1271cf7f2e2dSJohn Marino       elf_symtab_read (objfile, ST_REGULAR, symcount, symbol_table, 0);
12725796c8dcSSimon Schubert     }
12735796c8dcSSimon Schubert 
12745796c8dcSSimon Schubert   /* Add the dynamic symbols.  */
12755796c8dcSSimon Schubert 
12765796c8dcSSimon Schubert   storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd);
12775796c8dcSSimon Schubert 
12785796c8dcSSimon Schubert   if (storage_needed > 0)
12795796c8dcSSimon Schubert     {
1280*c50c785cSJohn Marino       /* Memory gets permanently referenced from ABFD after
1281*c50c785cSJohn Marino 	 bfd_get_synthetic_symtab so it must not get freed before ABFD gets.
1282*c50c785cSJohn Marino 	 It happens only in the case when elf_slurp_reloc_table sees
1283*c50c785cSJohn Marino 	 asection->relocation NULL.  Determining which section is asection is
1284*c50c785cSJohn Marino 	 done by _bfd_elf_get_synthetic_symtab which is all a bfd
1285*c50c785cSJohn Marino 	 implementation detail, though.  */
1286*c50c785cSJohn Marino 
1287*c50c785cSJohn Marino       dyn_symbol_table = bfd_alloc (abfd, storage_needed);
12885796c8dcSSimon Schubert       dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd,
12895796c8dcSSimon Schubert 						     dyn_symbol_table);
12905796c8dcSSimon Schubert 
12915796c8dcSSimon Schubert       if (dynsymcount < 0)
1292*c50c785cSJohn Marino 	error (_("Can't read symbols from %s: %s"),
1293*c50c785cSJohn Marino 	       bfd_get_filename (objfile->obfd),
12945796c8dcSSimon Schubert 	       bfd_errmsg (bfd_get_error ()));
12955796c8dcSSimon Schubert 
1296cf7f2e2dSJohn Marino       elf_symtab_read (objfile, ST_DYNAMIC, dynsymcount, dyn_symbol_table, 0);
1297*c50c785cSJohn Marino 
1298*c50c785cSJohn Marino       elf_rel_plt_read (objfile, dyn_symbol_table);
12995796c8dcSSimon Schubert     }
13005796c8dcSSimon Schubert 
13015796c8dcSSimon Schubert   /* Add synthetic symbols - for instance, names for any PLT entries.  */
13025796c8dcSSimon Schubert 
13035796c8dcSSimon Schubert   synthcount = bfd_get_synthetic_symtab (abfd, symcount, symbol_table,
13045796c8dcSSimon Schubert 					 dynsymcount, dyn_symbol_table,
13055796c8dcSSimon Schubert 					 &synthsyms);
13065796c8dcSSimon Schubert   if (synthcount > 0)
13075796c8dcSSimon Schubert     {
13085796c8dcSSimon Schubert       asymbol **synth_symbol_table;
13095796c8dcSSimon Schubert       long i;
13105796c8dcSSimon Schubert 
13115796c8dcSSimon Schubert       make_cleanup (xfree, synthsyms);
13125796c8dcSSimon Schubert       synth_symbol_table = xmalloc (sizeof (asymbol *) * synthcount);
13135796c8dcSSimon Schubert       for (i = 0; i < synthcount; i++)
13145796c8dcSSimon Schubert 	synth_symbol_table[i] = synthsyms + i;
13155796c8dcSSimon Schubert       make_cleanup (xfree, synth_symbol_table);
1316*c50c785cSJohn Marino       elf_symtab_read (objfile, ST_SYNTHETIC, synthcount,
1317*c50c785cSJohn Marino 		       synth_symbol_table, 1);
13185796c8dcSSimon Schubert     }
13195796c8dcSSimon Schubert 
13205796c8dcSSimon Schubert   /* Install any minimal symbols that have been collected as the current
13215796c8dcSSimon Schubert      minimal symbols for this objfile.  The debug readers below this point
13225796c8dcSSimon Schubert      should not generate new minimal symbols; if they do it's their
13235796c8dcSSimon Schubert      responsibility to install them.  "mdebug" appears to be the only one
13245796c8dcSSimon Schubert      which will do this.  */
13255796c8dcSSimon Schubert 
13265796c8dcSSimon Schubert   install_minimal_symbols (objfile);
13275796c8dcSSimon Schubert   do_cleanups (back_to);
13285796c8dcSSimon Schubert 
13295796c8dcSSimon Schubert   /* Now process debugging information, which is contained in
13305796c8dcSSimon Schubert      special ELF sections.  */
13315796c8dcSSimon Schubert 
13325796c8dcSSimon Schubert   /* We first have to find them...  */
13335796c8dcSSimon Schubert   bfd_map_over_sections (abfd, elf_locate_sections, (void *) & ei);
13345796c8dcSSimon Schubert 
13355796c8dcSSimon Schubert   /* ELF debugging information is inserted into the psymtab in the
13365796c8dcSSimon Schubert      order of least informative first - most informative last.  Since
13375796c8dcSSimon Schubert      the psymtab table is searched `most recent insertion first' this
13385796c8dcSSimon Schubert      increases the probability that more detailed debug information
13395796c8dcSSimon Schubert      for a section is found.
13405796c8dcSSimon Schubert 
13415796c8dcSSimon Schubert      For instance, an object file might contain both .mdebug (XCOFF)
13425796c8dcSSimon Schubert      and .debug_info (DWARF2) sections then .mdebug is inserted first
13435796c8dcSSimon Schubert      (searched last) and DWARF2 is inserted last (searched first).  If
13445796c8dcSSimon Schubert      we don't do this then the XCOFF info is found first - for code in
13455796c8dcSSimon Schubert      an included file XCOFF info is useless.  */
13465796c8dcSSimon Schubert 
13475796c8dcSSimon Schubert   if (ei.mdebugsect)
13485796c8dcSSimon Schubert     {
13495796c8dcSSimon Schubert       const struct ecoff_debug_swap *swap;
13505796c8dcSSimon Schubert 
13515796c8dcSSimon Schubert       /* .mdebug section, presumably holding ECOFF debugging
13525796c8dcSSimon Schubert          information.  */
13535796c8dcSSimon Schubert       swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13545796c8dcSSimon Schubert       if (swap)
13555796c8dcSSimon Schubert 	elfmdebug_build_psymtabs (objfile, swap, ei.mdebugsect);
13565796c8dcSSimon Schubert     }
13575796c8dcSSimon Schubert   if (ei.stabsect)
13585796c8dcSSimon Schubert     {
13595796c8dcSSimon Schubert       asection *str_sect;
13605796c8dcSSimon Schubert 
13615796c8dcSSimon Schubert       /* Stab sections have an associated string table that looks like
13625796c8dcSSimon Schubert          a separate section.  */
13635796c8dcSSimon Schubert       str_sect = bfd_get_section_by_name (abfd, ".stabstr");
13645796c8dcSSimon Schubert 
13655796c8dcSSimon Schubert       /* FIXME should probably warn about a stab section without a stabstr.  */
13665796c8dcSSimon Schubert       if (str_sect)
13675796c8dcSSimon Schubert 	elfstab_build_psymtabs (objfile,
13685796c8dcSSimon Schubert 				ei.stabsect,
13695796c8dcSSimon Schubert 				str_sect->filepos,
13705796c8dcSSimon Schubert 				bfd_section_size (abfd, str_sect));
13715796c8dcSSimon Schubert     }
1372*c50c785cSJohn Marino 
13735796c8dcSSimon Schubert   if (dwarf2_has_info (objfile))
13745796c8dcSSimon Schubert     {
1375*c50c785cSJohn Marino       /* elf_sym_fns_gdb_index cannot handle simultaneous non-DWARF debug
1376*c50c785cSJohn Marino 	 information present in OBJFILE.  If there is such debug info present
1377*c50c785cSJohn Marino 	 never use .gdb_index.  */
13785796c8dcSSimon Schubert 
1379*c50c785cSJohn Marino       if (!objfile_has_partial_symbols (objfile)
1380*c50c785cSJohn Marino 	  && dwarf2_initialize_objfile (objfile))
1381*c50c785cSJohn Marino 	objfile->sf = &elf_sym_fns_gdb_index;
1382*c50c785cSJohn Marino       else
1383*c50c785cSJohn Marino 	{
1384*c50c785cSJohn Marino 	  /* It is ok to do this even if the stabs reader made some
1385*c50c785cSJohn Marino 	     partial symbols, because OBJF_PSYMTABS_READ has not been
1386*c50c785cSJohn Marino 	     set, and so our lazy reader function will still be called
1387*c50c785cSJohn Marino 	     when needed.  */
1388*c50c785cSJohn Marino 	  objfile->sf = &elf_sym_fns_lazy_psyms;
1389*c50c785cSJohn Marino 	}
1390*c50c785cSJohn Marino     }
1391*c50c785cSJohn Marino   /* If the file has its own symbol tables it has no separate debug
1392*c50c785cSJohn Marino      info.  `.dynsym'/`.symtab' go to MSYMBOLS, `.debug_info' goes to
1393*c50c785cSJohn Marino      SYMTABS/PSYMTABS.  `.gnu_debuglink' may no longer be present with
1394*c50c785cSJohn Marino      `.note.gnu.build-id'.  */
1395*c50c785cSJohn Marino   else if (!objfile_has_partial_symbols (objfile))
1396cf7f2e2dSJohn Marino     {
1397cf7f2e2dSJohn Marino       char *debugfile;
1398cf7f2e2dSJohn Marino 
1399cf7f2e2dSJohn Marino       debugfile = find_separate_debug_file_by_buildid (objfile);
1400cf7f2e2dSJohn Marino 
1401cf7f2e2dSJohn Marino       if (debugfile == NULL)
1402cf7f2e2dSJohn Marino 	debugfile = find_separate_debug_file_by_debuglink (objfile);
1403cf7f2e2dSJohn Marino 
1404cf7f2e2dSJohn Marino       if (debugfile)
1405cf7f2e2dSJohn Marino 	{
1406cf7f2e2dSJohn Marino 	  bfd *abfd = symfile_bfd_open (debugfile);
1407cf7f2e2dSJohn Marino 
1408cf7f2e2dSJohn Marino 	  symbol_file_add_separate (abfd, symfile_flags, objfile);
1409cf7f2e2dSJohn Marino 	  xfree (debugfile);
1410cf7f2e2dSJohn Marino 	}
1411cf7f2e2dSJohn Marino     }
14125796c8dcSSimon Schubert }
14135796c8dcSSimon Schubert 
1414*c50c785cSJohn Marino /* Callback to lazily read psymtabs.  */
1415*c50c785cSJohn Marino 
1416*c50c785cSJohn Marino static void
1417*c50c785cSJohn Marino read_psyms (struct objfile *objfile)
1418*c50c785cSJohn Marino {
1419*c50c785cSJohn Marino   if (dwarf2_has_info (objfile))
1420*c50c785cSJohn Marino     dwarf2_build_psymtabs (objfile);
1421*c50c785cSJohn Marino }
1422*c50c785cSJohn Marino 
14235796c8dcSSimon Schubert /* This cleans up the objfile's deprecated_sym_stab_info pointer, and
14245796c8dcSSimon Schubert    the chain of stab_section_info's, that might be dangling from
14255796c8dcSSimon Schubert    it.  */
14265796c8dcSSimon Schubert 
14275796c8dcSSimon Schubert static void
14285796c8dcSSimon Schubert free_elfinfo (void *objp)
14295796c8dcSSimon Schubert {
14305796c8dcSSimon Schubert   struct objfile *objfile = (struct objfile *) objp;
14315796c8dcSSimon Schubert   struct dbx_symfile_info *dbxinfo = objfile->deprecated_sym_stab_info;
14325796c8dcSSimon Schubert   struct stab_section_info *ssi, *nssi;
14335796c8dcSSimon Schubert 
14345796c8dcSSimon Schubert   ssi = dbxinfo->stab_section_info;
14355796c8dcSSimon Schubert   while (ssi)
14365796c8dcSSimon Schubert     {
14375796c8dcSSimon Schubert       nssi = ssi->next;
14385796c8dcSSimon Schubert       xfree (ssi);
14395796c8dcSSimon Schubert       ssi = nssi;
14405796c8dcSSimon Schubert     }
14415796c8dcSSimon Schubert 
14425796c8dcSSimon Schubert   dbxinfo->stab_section_info = 0;	/* Just say No mo info about this.  */
14435796c8dcSSimon Schubert }
14445796c8dcSSimon Schubert 
14455796c8dcSSimon Schubert 
14465796c8dcSSimon Schubert /* Initialize anything that needs initializing when a completely new symbol
14475796c8dcSSimon Schubert    file is specified (not just adding some symbols from another file, e.g. a
14485796c8dcSSimon Schubert    shared library).
14495796c8dcSSimon Schubert 
1450*c50c785cSJohn Marino    We reinitialize buildsym, since we may be reading stabs from an ELF
1451*c50c785cSJohn Marino    file.  */
14525796c8dcSSimon Schubert 
14535796c8dcSSimon Schubert static void
14545796c8dcSSimon Schubert elf_new_init (struct objfile *ignore)
14555796c8dcSSimon Schubert {
14565796c8dcSSimon Schubert   stabsread_new_init ();
14575796c8dcSSimon Schubert   buildsym_new_init ();
14585796c8dcSSimon Schubert }
14595796c8dcSSimon Schubert 
14605796c8dcSSimon Schubert /* Perform any local cleanups required when we are done with a particular
14615796c8dcSSimon Schubert    objfile.  I.E, we are in the process of discarding all symbol information
14625796c8dcSSimon Schubert    for an objfile, freeing up all memory held for it, and unlinking the
14635796c8dcSSimon Schubert    objfile struct from the global list of known objfiles.  */
14645796c8dcSSimon Schubert 
14655796c8dcSSimon Schubert static void
14665796c8dcSSimon Schubert elf_symfile_finish (struct objfile *objfile)
14675796c8dcSSimon Schubert {
14685796c8dcSSimon Schubert   if (objfile->deprecated_sym_stab_info != NULL)
14695796c8dcSSimon Schubert     {
14705796c8dcSSimon Schubert       xfree (objfile->deprecated_sym_stab_info);
14715796c8dcSSimon Schubert     }
14725796c8dcSSimon Schubert 
14735796c8dcSSimon Schubert   dwarf2_free_objfile (objfile);
14745796c8dcSSimon Schubert }
14755796c8dcSSimon Schubert 
14765796c8dcSSimon Schubert /* ELF specific initialization routine for reading symbols.
14775796c8dcSSimon Schubert 
14785796c8dcSSimon Schubert    It is passed a pointer to a struct sym_fns which contains, among other
14795796c8dcSSimon Schubert    things, the BFD for the file whose symbols are being read, and a slot for
14805796c8dcSSimon Schubert    a pointer to "private data" which we can fill with goodies.
14815796c8dcSSimon Schubert 
14825796c8dcSSimon Schubert    For now at least, we have nothing in particular to do, so this function is
14835796c8dcSSimon Schubert    just a stub.  */
14845796c8dcSSimon Schubert 
14855796c8dcSSimon Schubert static void
14865796c8dcSSimon Schubert elf_symfile_init (struct objfile *objfile)
14875796c8dcSSimon Schubert {
14885796c8dcSSimon Schubert   /* ELF objects may be reordered, so set OBJF_REORDERED.  If we
14895796c8dcSSimon Schubert      find this causes a significant slowdown in gdb then we could
14905796c8dcSSimon Schubert      set it in the debug symbol readers only when necessary.  */
14915796c8dcSSimon Schubert   objfile->flags |= OBJF_REORDERED;
14925796c8dcSSimon Schubert }
14935796c8dcSSimon Schubert 
14945796c8dcSSimon Schubert /* When handling an ELF file that contains Sun STABS debug info,
14955796c8dcSSimon Schubert    some of the debug info is relative to the particular chunk of the
14965796c8dcSSimon Schubert    section that was generated in its individual .o file.  E.g.
14975796c8dcSSimon Schubert    offsets to static variables are relative to the start of the data
14985796c8dcSSimon Schubert    segment *for that module before linking*.  This information is
14995796c8dcSSimon Schubert    painfully squirreled away in the ELF symbol table as local symbols
15005796c8dcSSimon Schubert    with wierd names.  Go get 'em when needed.  */
15015796c8dcSSimon Schubert 
15025796c8dcSSimon Schubert void
15035796c8dcSSimon Schubert elfstab_offset_sections (struct objfile *objfile, struct partial_symtab *pst)
15045796c8dcSSimon Schubert {
1505*c50c785cSJohn Marino   const char *filename = pst->filename;
15065796c8dcSSimon Schubert   struct dbx_symfile_info *dbx = objfile->deprecated_sym_stab_info;
15075796c8dcSSimon Schubert   struct stab_section_info *maybe = dbx->stab_section_info;
15085796c8dcSSimon Schubert   struct stab_section_info *questionable = 0;
15095796c8dcSSimon Schubert   int i;
15105796c8dcSSimon Schubert 
15115796c8dcSSimon Schubert   /* The ELF symbol info doesn't include path names, so strip the path
15125796c8dcSSimon Schubert      (if any) from the psymtab filename.  */
1513*c50c785cSJohn Marino   filename = lbasename (filename);
15145796c8dcSSimon Schubert 
15155796c8dcSSimon Schubert   /* FIXME:  This linear search could speed up significantly
15165796c8dcSSimon Schubert      if it was chained in the right order to match how we search it,
15175796c8dcSSimon Schubert      and if we unchained when we found a match.  */
15185796c8dcSSimon Schubert   for (; maybe; maybe = maybe->next)
15195796c8dcSSimon Schubert     {
15205796c8dcSSimon Schubert       if (filename[0] == maybe->filename[0]
1521*c50c785cSJohn Marino 	  && filename_cmp (filename, maybe->filename) == 0)
15225796c8dcSSimon Schubert 	{
15235796c8dcSSimon Schubert 	  /* We found a match.  But there might be several source files
15245796c8dcSSimon Schubert 	     (from different directories) with the same name.  */
15255796c8dcSSimon Schubert 	  if (0 == maybe->found)
15265796c8dcSSimon Schubert 	    break;
15275796c8dcSSimon Schubert 	  questionable = maybe;	/* Might use it later.  */
15285796c8dcSSimon Schubert 	}
15295796c8dcSSimon Schubert     }
15305796c8dcSSimon Schubert 
15315796c8dcSSimon Schubert   if (maybe == 0 && questionable != 0)
15325796c8dcSSimon Schubert     {
15335796c8dcSSimon Schubert       complaint (&symfile_complaints,
1534*c50c785cSJohn Marino 		 _("elf/stab section information questionable for %s"),
1535*c50c785cSJohn Marino 		 filename);
15365796c8dcSSimon Schubert       maybe = questionable;
15375796c8dcSSimon Schubert     }
15385796c8dcSSimon Schubert 
15395796c8dcSSimon Schubert   if (maybe)
15405796c8dcSSimon Schubert     {
15415796c8dcSSimon Schubert       /* Found it!  Allocate a new psymtab struct, and fill it in.  */
15425796c8dcSSimon Schubert       maybe->found++;
15435796c8dcSSimon Schubert       pst->section_offsets = (struct section_offsets *)
15445796c8dcSSimon Schubert 	obstack_alloc (&objfile->objfile_obstack,
15455796c8dcSSimon Schubert 		       SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
15465796c8dcSSimon Schubert       for (i = 0; i < maybe->num_sections; i++)
15475796c8dcSSimon Schubert 	(pst->section_offsets)->offsets[i] = maybe->sections[i];
15485796c8dcSSimon Schubert       return;
15495796c8dcSSimon Schubert     }
15505796c8dcSSimon Schubert 
15515796c8dcSSimon Schubert   /* We were unable to find any offsets for this file.  Complain.  */
15525796c8dcSSimon Schubert   if (dbx->stab_section_info)	/* If there *is* any info, */
15535796c8dcSSimon Schubert     complaint (&symfile_complaints,
15545796c8dcSSimon Schubert 	       _("elf/stab section information missing for %s"), filename);
15555796c8dcSSimon Schubert }
15565796c8dcSSimon Schubert 
15575796c8dcSSimon Schubert /* Register that we are able to handle ELF object file formats.  */
15585796c8dcSSimon Schubert 
1559*c50c785cSJohn Marino static const struct sym_fns elf_sym_fns =
15605796c8dcSSimon Schubert {
15615796c8dcSSimon Schubert   bfd_target_elf_flavour,
1562*c50c785cSJohn Marino   elf_new_init,			/* init anything gbl to entire symtab */
1563*c50c785cSJohn Marino   elf_symfile_init,		/* read initial info, setup for sym_read() */
1564*c50c785cSJohn Marino   elf_symfile_read,		/* read a symbol file into symtab */
1565*c50c785cSJohn Marino   NULL,				/* sym_read_psymbols */
1566*c50c785cSJohn Marino   elf_symfile_finish,		/* finished with file, cleanup */
1567*c50c785cSJohn Marino   default_symfile_offsets,	/* Translate ext. to int. relocation */
1568*c50c785cSJohn Marino   elf_symfile_segments,		/* Get segment information from a file.  */
1569*c50c785cSJohn Marino   NULL,
1570*c50c785cSJohn Marino   default_symfile_relocate,	/* Relocate a debug section.  */
1571*c50c785cSJohn Marino   &psym_functions
1572*c50c785cSJohn Marino };
1573*c50c785cSJohn Marino 
1574*c50c785cSJohn Marino /* The same as elf_sym_fns, but not registered and lazily reads
1575*c50c785cSJohn Marino    psymbols.  */
1576*c50c785cSJohn Marino 
1577*c50c785cSJohn Marino static const struct sym_fns elf_sym_fns_lazy_psyms =
1578*c50c785cSJohn Marino {
1579*c50c785cSJohn Marino   bfd_target_elf_flavour,
1580*c50c785cSJohn Marino   elf_new_init,			/* init anything gbl to entire symtab */
1581*c50c785cSJohn Marino   elf_symfile_init,		/* read initial info, setup for sym_read() */
1582*c50c785cSJohn Marino   elf_symfile_read,		/* read a symbol file into symtab */
1583*c50c785cSJohn Marino   read_psyms,			/* sym_read_psymbols */
1584*c50c785cSJohn Marino   elf_symfile_finish,		/* finished with file, cleanup */
1585*c50c785cSJohn Marino   default_symfile_offsets,	/* Translate ext. to int. relocation */
1586*c50c785cSJohn Marino   elf_symfile_segments,		/* Get segment information from a file.  */
1587*c50c785cSJohn Marino   NULL,
1588*c50c785cSJohn Marino   default_symfile_relocate,	/* Relocate a debug section.  */
1589*c50c785cSJohn Marino   &psym_functions
1590*c50c785cSJohn Marino };
1591*c50c785cSJohn Marino 
1592*c50c785cSJohn Marino /* The same as elf_sym_fns, but not registered and uses the
1593*c50c785cSJohn Marino    DWARF-specific GNU index rather than psymtab.  */
1594*c50c785cSJohn Marino static const struct sym_fns elf_sym_fns_gdb_index =
1595*c50c785cSJohn Marino {
1596*c50c785cSJohn Marino   bfd_target_elf_flavour,
1597*c50c785cSJohn Marino   elf_new_init,			/* init anything gbl to entire symab */
1598*c50c785cSJohn Marino   elf_symfile_init,		/* read initial info, setup for sym_red() */
1599*c50c785cSJohn Marino   elf_symfile_read,		/* read a symbol file into symtab */
1600*c50c785cSJohn Marino   NULL,				/* sym_read_psymbols */
1601*c50c785cSJohn Marino   elf_symfile_finish,		/* finished with file, cleanup */
1602*c50c785cSJohn Marino   default_symfile_offsets,	/* Translate ext. to int. relocatin */
1603*c50c785cSJohn Marino   elf_symfile_segments,		/* Get segment information from a file.  */
1604*c50c785cSJohn Marino   NULL,
1605*c50c785cSJohn Marino   default_symfile_relocate,	/* Relocate a debug section.  */
1606*c50c785cSJohn Marino   &dwarf2_gdb_index_functions
1607*c50c785cSJohn Marino };
1608*c50c785cSJohn Marino 
1609*c50c785cSJohn Marino /* STT_GNU_IFUNC resolver vector to be installed to gnu_ifunc_fns_p.  */
1610*c50c785cSJohn Marino 
1611*c50c785cSJohn Marino static const struct gnu_ifunc_fns elf_gnu_ifunc_fns =
1612*c50c785cSJohn Marino {
1613*c50c785cSJohn Marino   elf_gnu_ifunc_resolve_addr,
1614*c50c785cSJohn Marino   elf_gnu_ifunc_resolve_name,
1615*c50c785cSJohn Marino   elf_gnu_ifunc_resolver_stop,
1616*c50c785cSJohn Marino   elf_gnu_ifunc_resolver_return_stop
16175796c8dcSSimon Schubert };
16185796c8dcSSimon Schubert 
16195796c8dcSSimon Schubert void
16205796c8dcSSimon Schubert _initialize_elfread (void)
16215796c8dcSSimon Schubert {
16225796c8dcSSimon Schubert   add_symtab_fns (&elf_sym_fns);
1623*c50c785cSJohn Marino 
1624*c50c785cSJohn Marino   elf_objfile_gnu_ifunc_cache_data = register_objfile_data ();
1625*c50c785cSJohn Marino   gnu_ifunc_fns_p = &elf_gnu_ifunc_fns;
16265796c8dcSSimon Schubert }
1627