GDB (API)
/home/stan/gdb/src/gdb/elfread.c
Go to the documentation of this file.
00001 /* Read ELF (Executable and Linking Format) object files for GDB.
00002 
00003    Copyright (C) 1991-2013 Free Software Foundation, Inc.
00004 
00005    Written by Fred Fish at Cygnus Support.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "bfd.h"
00024 #include "gdb_string.h"
00025 #include "elf-bfd.h"
00026 #include "elf/common.h"
00027 #include "elf/internal.h"
00028 #include "elf/mips.h"
00029 #include "symtab.h"
00030 #include "symfile.h"
00031 #include "objfiles.h"
00032 #include "buildsym.h"
00033 #include "stabsread.h"
00034 #include "gdb-stabs.h"
00035 #include "complaints.h"
00036 #include "demangle.h"
00037 #include "psympriv.h"
00038 #include "filenames.h"
00039 #include "probe.h"
00040 #include "arch-utils.h"
00041 #include "gdbtypes.h"
00042 #include "value.h"
00043 #include "infcall.h"
00044 #include "gdbthread.h"
00045 #include "regcache.h"
00046 #include "bcache.h"
00047 #include "gdb_bfd.h"
00048 #include "build-id.h"
00049 
00050 extern void _initialize_elfread (void);
00051 
00052 /* Forward declarations.  */
00053 static const struct sym_fns elf_sym_fns_gdb_index;
00054 static const struct sym_fns elf_sym_fns_lazy_psyms;
00055 
00056 /* The struct elfinfo is available only during ELF symbol table and
00057    psymtab reading.  It is destroyed at the completion of psymtab-reading.
00058    It's local to elf_symfile_read.  */
00059 
00060 struct elfinfo
00061   {
00062     asection *stabsect;         /* Section pointer for .stab section */
00063     asection *stabindexsect;    /* Section pointer for .stab.index section */
00064     asection *mdebugsect;       /* Section pointer for .mdebug section */
00065   };
00066 
00067 /* Per-objfile data for probe info.  */
00068 
00069 static const struct objfile_data *probe_key = NULL;
00070 
00071 static void free_elfinfo (void *);
00072 
00073 /* Minimal symbols located at the GOT entries for .plt - that is the real
00074    pointer where the given entry will jump to.  It gets updated by the real
00075    function address during lazy ld.so resolving in the inferior.  These
00076    minimal symbols are indexed for <tab>-completion.  */
00077 
00078 #define SYMBOL_GOT_PLT_SUFFIX "@got.plt"
00079 
00080 /* Locate the segments in ABFD.  */
00081 
00082 static struct symfile_segment_data *
00083 elf_symfile_segments (bfd *abfd)
00084 {
00085   Elf_Internal_Phdr *phdrs, **segments;
00086   long phdrs_size;
00087   int num_phdrs, num_segments, num_sections, i;
00088   asection *sect;
00089   struct symfile_segment_data *data;
00090 
00091   phdrs_size = bfd_get_elf_phdr_upper_bound (abfd);
00092   if (phdrs_size == -1)
00093     return NULL;
00094 
00095   phdrs = alloca (phdrs_size);
00096   num_phdrs = bfd_get_elf_phdrs (abfd, phdrs);
00097   if (num_phdrs == -1)
00098     return NULL;
00099 
00100   num_segments = 0;
00101   segments = alloca (sizeof (Elf_Internal_Phdr *) * num_phdrs);
00102   for (i = 0; i < num_phdrs; i++)
00103     if (phdrs[i].p_type == PT_LOAD)
00104       segments[num_segments++] = &phdrs[i];
00105 
00106   if (num_segments == 0)
00107     return NULL;
00108 
00109   data = XZALLOC (struct symfile_segment_data);
00110   data->num_segments = num_segments;
00111   data->segment_bases = XCALLOC (num_segments, CORE_ADDR);
00112   data->segment_sizes = XCALLOC (num_segments, CORE_ADDR);
00113 
00114   for (i = 0; i < num_segments; i++)
00115     {
00116       data->segment_bases[i] = segments[i]->p_vaddr;
00117       data->segment_sizes[i] = segments[i]->p_memsz;
00118     }
00119 
00120   num_sections = bfd_count_sections (abfd);
00121   data->segment_info = XCALLOC (num_sections, int);
00122 
00123   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
00124     {
00125       int j;
00126       CORE_ADDR vma;
00127 
00128       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
00129         continue;
00130 
00131       vma = bfd_get_section_vma (abfd, sect);
00132 
00133       for (j = 0; j < num_segments; j++)
00134         if (segments[j]->p_memsz > 0
00135             && vma >= segments[j]->p_vaddr
00136             && (vma - segments[j]->p_vaddr) < segments[j]->p_memsz)
00137           {
00138             data->segment_info[i] = j + 1;
00139             break;
00140           }
00141 
00142       /* We should have found a segment for every non-empty section.
00143          If we haven't, we will not relocate this section by any
00144          offsets we apply to the segments.  As an exception, do not
00145          warn about SHT_NOBITS sections; in normal ELF execution
00146          environments, SHT_NOBITS means zero-initialized and belongs
00147          in a segment, but in no-OS environments some tools (e.g. ARM
00148          RealView) use SHT_NOBITS for uninitialized data.  Since it is
00149          uninitialized, it doesn't need a program header.  Such
00150          binaries are not relocatable.  */
00151       if (bfd_get_section_size (sect) > 0 && j == num_segments
00152           && (bfd_get_section_flags (abfd, sect) & SEC_LOAD) != 0)
00153         warning (_("Loadable section \"%s\" outside of ELF segments"),
00154                  bfd_section_name (abfd, sect));
00155     }
00156 
00157   return data;
00158 }
00159 
00160 /* We are called once per section from elf_symfile_read.  We
00161    need to examine each section we are passed, check to see
00162    if it is something we are interested in processing, and
00163    if so, stash away some access information for the section.
00164 
00165    For now we recognize the dwarf debug information sections and
00166    line number sections from matching their section names.  The
00167    ELF definition is no real help here since it has no direct
00168    knowledge of DWARF (by design, so any debugging format can be
00169    used).
00170 
00171    We also recognize the ".stab" sections used by the Sun compilers
00172    released with Solaris 2.
00173 
00174    FIXME: The section names should not be hardwired strings (what
00175    should they be?  I don't think most object file formats have enough
00176    section flags to specify what kind of debug section it is.
00177    -kingdon).  */
00178 
00179 static void
00180 elf_locate_sections (bfd *ignore_abfd, asection *sectp, void *eip)
00181 {
00182   struct elfinfo *ei;
00183 
00184   ei = (struct elfinfo *) eip;
00185   if (strcmp (sectp->name, ".stab") == 0)
00186     {
00187       ei->stabsect = sectp;
00188     }
00189   else if (strcmp (sectp->name, ".stab.index") == 0)
00190     {
00191       ei->stabindexsect = sectp;
00192     }
00193   else if (strcmp (sectp->name, ".mdebug") == 0)
00194     {
00195       ei->mdebugsect = sectp;
00196     }
00197 }
00198 
00199 static struct minimal_symbol *
00200 record_minimal_symbol (const char *name, int name_len, int copy_name,
00201                        CORE_ADDR address,
00202                        enum minimal_symbol_type ms_type,
00203                        asection *bfd_section, struct objfile *objfile)
00204 {
00205   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00206 
00207   if (ms_type == mst_text || ms_type == mst_file_text
00208       || ms_type == mst_text_gnu_ifunc)
00209     address = gdbarch_addr_bits_remove (gdbarch, address);
00210 
00211   return prim_record_minimal_symbol_full (name, name_len, copy_name, address,
00212                                           ms_type,
00213                                           gdb_bfd_section_index (objfile->obfd,
00214                                                                  bfd_section),
00215                                           objfile);
00216 }
00217 
00218 /* Read the symbol table of an ELF file.
00219 
00220    Given an objfile, a symbol table, and a flag indicating whether the
00221    symbol table contains regular, dynamic, or synthetic symbols, add all
00222    the global function and data symbols to the minimal symbol table.
00223 
00224    In stabs-in-ELF, as implemented by Sun, there are some local symbols
00225    defined in the ELF symbol table, which can be used to locate
00226    the beginnings of sections from each ".o" file that was linked to
00227    form the executable objfile.  We gather any such info and record it
00228    in data structures hung off the objfile's private data.  */
00229 
00230 #define ST_REGULAR 0
00231 #define ST_DYNAMIC 1
00232 #define ST_SYNTHETIC 2
00233 
00234 static void
00235 elf_symtab_read (struct objfile *objfile, int type,
00236                  long number_of_symbols, asymbol **symbol_table,
00237                  int copy_names)
00238 {
00239   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00240   asymbol *sym;
00241   long i;
00242   CORE_ADDR symaddr;
00243   CORE_ADDR offset;
00244   enum minimal_symbol_type ms_type;
00245   /* If sectinfo is nonNULL, it contains section info that should end up
00246      filed in the objfile.  */
00247   struct stab_section_info *sectinfo = NULL;
00248   /* If filesym is nonzero, it points to a file symbol, but we haven't
00249      seen any section info for it yet.  */
00250   asymbol *filesym = 0;
00251   /* Name of filesym.  This is either a constant string or is saved on
00252      the objfile's filename cache.  */
00253   const char *filesymname = "";
00254   struct dbx_symfile_info *dbx = DBX_SYMFILE_INFO (objfile);
00255   int stripped = (bfd_get_symcount (objfile->obfd) == 0);
00256 
00257   for (i = 0; i < number_of_symbols; i++)
00258     {
00259       sym = symbol_table[i];
00260       if (sym->name == NULL || *sym->name == '\0')
00261         {
00262           /* Skip names that don't exist (shouldn't happen), or names
00263              that are null strings (may happen).  */
00264           continue;
00265         }
00266 
00267       /* Skip "special" symbols, e.g. ARM mapping symbols.  These are
00268          symbols which do not correspond to objects in the symbol table,
00269          but have some other target-specific meaning.  */
00270       if (bfd_is_target_special_symbol (objfile->obfd, sym))
00271         {
00272           if (gdbarch_record_special_symbol_p (gdbarch))
00273             gdbarch_record_special_symbol (gdbarch, objfile, sym);
00274           continue;
00275         }
00276 
00277       offset = ANOFFSET (objfile->section_offsets,
00278                          gdb_bfd_section_index (objfile->obfd, sym->section));
00279       if (type == ST_DYNAMIC
00280           && sym->section == bfd_und_section_ptr
00281           && (sym->flags & BSF_FUNCTION))
00282         {
00283           struct minimal_symbol *msym;
00284           bfd *abfd = objfile->obfd;
00285           asection *sect;
00286 
00287           /* Symbol is a reference to a function defined in
00288              a shared library.
00289              If its value is non zero then it is usually the address
00290              of the corresponding entry in the procedure linkage table,
00291              plus the desired section offset.
00292              If its value is zero then the dynamic linker has to resolve
00293              the symbol.  We are unable to find any meaningful address
00294              for this symbol in the executable file, so we skip it.  */
00295           symaddr = sym->value;
00296           if (symaddr == 0)
00297             continue;
00298 
00299           /* sym->section is the undefined section.  However, we want to
00300              record the section where the PLT stub resides with the
00301              minimal symbol.  Search the section table for the one that
00302              covers the stub's address.  */
00303           for (sect = abfd->sections; sect != NULL; sect = sect->next)
00304             {
00305               if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
00306                 continue;
00307 
00308               if (symaddr >= bfd_get_section_vma (abfd, sect)
00309                   && symaddr < bfd_get_section_vma (abfd, sect)
00310                                + bfd_get_section_size (sect))
00311                 break;
00312             }
00313           if (!sect)
00314             continue;
00315 
00316           /* On ia64-hpux, we have discovered that the system linker
00317              adds undefined symbols with nonzero addresses that cannot
00318              be right (their address points inside the code of another
00319              function in the .text section).  This creates problems
00320              when trying to determine which symbol corresponds to
00321              a given address.
00322 
00323              We try to detect those buggy symbols by checking which
00324              section we think they correspond to.  Normally, PLT symbols
00325              are stored inside their own section, and the typical name
00326              for that section is ".plt".  So, if there is a ".plt"
00327              section, and yet the section name of our symbol does not
00328              start with ".plt", we ignore that symbol.  */
00329           if (strncmp (sect->name, ".plt", 4) != 0
00330               && bfd_get_section_by_name (abfd, ".plt") != NULL)
00331             continue;
00332 
00333           symaddr += ANOFFSET (objfile->section_offsets,
00334                                gdb_bfd_section_index (objfile->obfd, sect));
00335 
00336           msym = record_minimal_symbol
00337             (sym->name, strlen (sym->name), copy_names,
00338              symaddr, mst_solib_trampoline, sect, objfile);
00339           if (msym != NULL)
00340             msym->filename = filesymname;
00341           continue;
00342         }
00343 
00344       /* If it is a nonstripped executable, do not enter dynamic
00345          symbols, as the dynamic symbol table is usually a subset
00346          of the main symbol table.  */
00347       if (type == ST_DYNAMIC && !stripped)
00348         continue;
00349       if (sym->flags & BSF_FILE)
00350         {
00351           /* STT_FILE debugging symbol that helps stabs-in-elf debugging.
00352              Chain any old one onto the objfile; remember new sym.  */
00353           if (sectinfo != NULL)
00354             {
00355               sectinfo->next = dbx->stab_section_info;
00356               dbx->stab_section_info = sectinfo;
00357               sectinfo = NULL;
00358             }
00359           filesym = sym;
00360           filesymname = bcache (filesym->name, strlen (filesym->name) + 1,
00361                                 objfile->per_bfd->filename_cache);
00362         }
00363       else if (sym->flags & BSF_SECTION_SYM)
00364         continue;
00365       else if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK
00366                              | BSF_GNU_UNIQUE))
00367         {
00368           struct minimal_symbol *msym;
00369 
00370           /* Select global/local/weak symbols.  Note that bfd puts abs
00371              symbols in their own section, so all symbols we are
00372              interested in will have a section.  */
00373           /* Bfd symbols are section relative.  */
00374           symaddr = sym->value + sym->section->vma;
00375           /* Relocate all non-absolute and non-TLS symbols by the
00376              section offset.  */
00377           if (sym->section != bfd_abs_section_ptr
00378               && !(sym->section->flags & SEC_THREAD_LOCAL))
00379             {
00380               symaddr += offset;
00381             }
00382           /* For non-absolute symbols, use the type of the section
00383              they are relative to, to intuit text/data.  Bfd provides
00384              no way of figuring this out for absolute symbols.  */
00385           if (sym->section == bfd_abs_section_ptr)
00386             {
00387               /* This is a hack to get the minimal symbol type
00388                  right for Irix 5, which has absolute addresses
00389                  with special section indices for dynamic symbols.
00390 
00391                  NOTE: uweigand-20071112: Synthetic symbols do not
00392                  have an ELF-private part, so do not touch those.  */
00393               unsigned int shndx = type == ST_SYNTHETIC ? 0 :
00394                 ((elf_symbol_type *) sym)->internal_elf_sym.st_shndx;
00395 
00396               switch (shndx)
00397                 {
00398                 case SHN_MIPS_TEXT:
00399                   ms_type = mst_text;
00400                   break;
00401                 case SHN_MIPS_DATA:
00402                   ms_type = mst_data;
00403                   break;
00404                 case SHN_MIPS_ACOMMON:
00405                   ms_type = mst_bss;
00406                   break;
00407                 default:
00408                   ms_type = mst_abs;
00409                 }
00410 
00411               /* If it is an Irix dynamic symbol, skip section name
00412                  symbols, relocate all others by section offset.  */
00413               if (ms_type != mst_abs)
00414                 {
00415                   if (sym->name[0] == '.')
00416                     continue;
00417                   symaddr += offset;
00418                 }
00419             }
00420           else if (sym->section->flags & SEC_CODE)
00421             {
00422               if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
00423                 {
00424                   if (sym->flags & BSF_GNU_INDIRECT_FUNCTION)
00425                     ms_type = mst_text_gnu_ifunc;
00426                   else
00427                     ms_type = mst_text;
00428                 }
00429               /* The BSF_SYNTHETIC check is there to omit ppc64 function
00430                  descriptors mistaken for static functions starting with 'L'.
00431                  */
00432               else if ((sym->name[0] == '.' && sym->name[1] == 'L'
00433                         && (sym->flags & BSF_SYNTHETIC) == 0)
00434                        || ((sym->flags & BSF_LOCAL)
00435                            && sym->name[0] == '$'
00436                            && sym->name[1] == 'L'))
00437                 /* Looks like a compiler-generated label.  Skip
00438                    it.  The assembler should be skipping these (to
00439                    keep executables small), but apparently with
00440                    gcc on the (deleted) delta m88k SVR4, it loses.
00441                    So to have us check too should be harmless (but
00442                    I encourage people to fix this in the assembler
00443                    instead of adding checks here).  */
00444                 continue;
00445               else
00446                 {
00447                   ms_type = mst_file_text;
00448                 }
00449             }
00450           else if (sym->section->flags & SEC_ALLOC)
00451             {
00452               if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
00453                 {
00454                   if (sym->section->flags & SEC_LOAD)
00455                     {
00456                       ms_type = mst_data;
00457                     }
00458                   else
00459                     {
00460                       ms_type = mst_bss;
00461                     }
00462                 }
00463               else if (sym->flags & BSF_LOCAL)
00464                 {
00465                   /* Named Local variable in a Data section.
00466                      Check its name for stabs-in-elf.  */
00467                   int special_local_sect;
00468 
00469                   if (strcmp ("Bbss.bss", sym->name) == 0)
00470                     special_local_sect = SECT_OFF_BSS (objfile);
00471                   else if (strcmp ("Ddata.data", sym->name) == 0)
00472                     special_local_sect = SECT_OFF_DATA (objfile);
00473                   else if (strcmp ("Drodata.rodata", sym->name) == 0)
00474                     special_local_sect = SECT_OFF_RODATA (objfile);
00475                   else
00476                     special_local_sect = -1;
00477                   if (special_local_sect >= 0)
00478                     {
00479                       /* Found a special local symbol.  Allocate a
00480                          sectinfo, if needed, and fill it in.  */
00481                       if (sectinfo == NULL)
00482                         {
00483                           int max_index;
00484                           size_t size;
00485 
00486                           max_index = SECT_OFF_BSS (objfile);
00487                           if (objfile->sect_index_data > max_index)
00488                             max_index = objfile->sect_index_data;
00489                           if (objfile->sect_index_rodata > max_index)
00490                             max_index = objfile->sect_index_rodata;
00491 
00492                           /* max_index is the largest index we'll
00493                              use into this array, so we must
00494                              allocate max_index+1 elements for it.
00495                              However, 'struct stab_section_info'
00496                              already includes one element, so we
00497                              need to allocate max_index aadditional
00498                              elements.  */
00499                           size = (sizeof (struct stab_section_info)
00500                                   + (sizeof (CORE_ADDR) * max_index));
00501                           sectinfo = (struct stab_section_info *)
00502                             xmalloc (size);
00503                           memset (sectinfo, 0, size);
00504                           sectinfo->num_sections = max_index;
00505                           if (filesym == NULL)
00506                             {
00507                               complaint (&symfile_complaints,
00508                                          _("elf/stab section information %s "
00509                                            "without a preceding file symbol"),
00510                                          sym->name);
00511                             }
00512                           else
00513                             {
00514                               sectinfo->filename =
00515                                 (char *) filesym->name;
00516                             }
00517                         }
00518                       if (sectinfo->sections[special_local_sect] != 0)
00519                         complaint (&symfile_complaints,
00520                                    _("duplicated elf/stab section "
00521                                      "information for %s"),
00522                                    sectinfo->filename);
00523                       /* BFD symbols are section relative.  */
00524                       symaddr = sym->value + sym->section->vma;
00525                       /* Relocate non-absolute symbols by the
00526                          section offset.  */
00527                       if (sym->section != bfd_abs_section_ptr)
00528                         symaddr += offset;
00529                       sectinfo->sections[special_local_sect] = symaddr;
00530                       /* The special local symbols don't go in the
00531                          minimal symbol table, so ignore this one.  */
00532                       continue;
00533                     }
00534                   /* Not a special stabs-in-elf symbol, do regular
00535                      symbol processing.  */
00536                   if (sym->section->flags & SEC_LOAD)
00537                     {
00538                       ms_type = mst_file_data;
00539                     }
00540                   else
00541                     {
00542                       ms_type = mst_file_bss;
00543                     }
00544                 }
00545               else
00546                 {
00547                   ms_type = mst_unknown;
00548                 }
00549             }
00550           else
00551             {
00552               /* FIXME:  Solaris2 shared libraries include lots of
00553                  odd "absolute" and "undefined" symbols, that play
00554                  hob with actions like finding what function the PC
00555                  is in.  Ignore them if they aren't text, data, or bss.  */
00556               /* ms_type = mst_unknown; */
00557               continue; /* Skip this symbol.  */
00558             }
00559           msym = record_minimal_symbol
00560             (sym->name, strlen (sym->name), copy_names, symaddr,
00561              ms_type, sym->section, objfile);
00562 
00563           if (msym)
00564             {
00565               /* NOTE: uweigand-20071112: A synthetic symbol does not have an
00566                  ELF-private part.  */
00567               if (type != ST_SYNTHETIC)
00568                 {
00569                   /* Pass symbol size field in via BFD.  FIXME!!!  */
00570                   elf_symbol_type *elf_sym = (elf_symbol_type *) sym;
00571                   SET_MSYMBOL_SIZE (msym, elf_sym->internal_elf_sym.st_size);
00572                 }
00573 
00574               msym->filename = filesymname;
00575               gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
00576             }
00577 
00578           /* If we see a default versioned symbol, install it under
00579              its version-less name.  */
00580           if (msym != NULL)
00581             {
00582               const char *atsign = strchr (sym->name, '@');
00583 
00584               if (atsign != NULL && atsign[1] == '@' && atsign > sym->name)
00585                 {
00586                   int len = atsign - sym->name;
00587 
00588                   record_minimal_symbol (sym->name, len, 1, symaddr,
00589                                          ms_type, sym->section, objfile);
00590                 }
00591             }
00592 
00593           /* For @plt symbols, also record a trampoline to the
00594              destination symbol.  The @plt symbol will be used in
00595              disassembly, and the trampoline will be used when we are
00596              trying to find the target.  */
00597           if (msym && ms_type == mst_text && type == ST_SYNTHETIC)
00598             {
00599               int len = strlen (sym->name);
00600 
00601               if (len > 4 && strcmp (sym->name + len - 4, "@plt") == 0)
00602                 {
00603                   struct minimal_symbol *mtramp;
00604 
00605                   mtramp = record_minimal_symbol (sym->name, len - 4, 1,
00606                                                   symaddr,
00607                                                   mst_solib_trampoline,
00608                                                   sym->section, objfile);
00609                   if (mtramp)
00610                     {
00611                       SET_MSYMBOL_SIZE (mtramp, MSYMBOL_SIZE (msym));
00612                       mtramp->created_by_gdb = 1;
00613                       mtramp->filename = filesymname;
00614                       gdbarch_elf_make_msymbol_special (gdbarch, sym, mtramp);
00615                     }
00616                 }
00617             }
00618         }
00619     }
00620 }
00621 
00622 /* Build minimal symbols named `function@got.plt' (see SYMBOL_GOT_PLT_SUFFIX)
00623    for later look ups of which function to call when user requests
00624    a STT_GNU_IFUNC function.  As the STT_GNU_IFUNC type is found at the target
00625    library defining `function' we cannot yet know while reading OBJFILE which
00626    of the SYMBOL_GOT_PLT_SUFFIX entries will be needed and later
00627    DYN_SYMBOL_TABLE is no longer easily available for OBJFILE.  */
00628 
00629 static void
00630 elf_rel_plt_read (struct objfile *objfile, asymbol **dyn_symbol_table)
00631 {
00632   bfd *obfd = objfile->obfd;
00633   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
00634   asection *plt, *relplt, *got_plt;
00635   int plt_elf_idx;
00636   bfd_size_type reloc_count, reloc;
00637   char *string_buffer = NULL;
00638   size_t string_buffer_size = 0;
00639   struct cleanup *back_to;
00640   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00641   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
00642   size_t ptr_size = TYPE_LENGTH (ptr_type);
00643 
00644   if (objfile->separate_debug_objfile_backlink)
00645     return;
00646 
00647   plt = bfd_get_section_by_name (obfd, ".plt");
00648   if (plt == NULL)
00649     return;
00650   plt_elf_idx = elf_section_data (plt)->this_idx;
00651 
00652   got_plt = bfd_get_section_by_name (obfd, ".got.plt");
00653   if (got_plt == NULL)
00654     return;
00655 
00656   /* This search algorithm is from _bfd_elf_canonicalize_dynamic_reloc.  */
00657   for (relplt = obfd->sections; relplt != NULL; relplt = relplt->next)
00658     if (elf_section_data (relplt)->this_hdr.sh_info == plt_elf_idx
00659         && (elf_section_data (relplt)->this_hdr.sh_type == SHT_REL
00660             || elf_section_data (relplt)->this_hdr.sh_type == SHT_RELA))
00661       break;
00662   if (relplt == NULL)
00663     return;
00664 
00665   if (! bed->s->slurp_reloc_table (obfd, relplt, dyn_symbol_table, TRUE))
00666     return;
00667 
00668   back_to = make_cleanup (free_current_contents, &string_buffer);
00669 
00670   reloc_count = relplt->size / elf_section_data (relplt)->this_hdr.sh_entsize;
00671   for (reloc = 0; reloc < reloc_count; reloc++)
00672     {
00673       const char *name;
00674       struct minimal_symbol *msym;
00675       CORE_ADDR address;
00676       const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
00677       size_t name_len;
00678 
00679       name = bfd_asymbol_name (*relplt->relocation[reloc].sym_ptr_ptr);
00680       name_len = strlen (name);
00681       address = relplt->relocation[reloc].address;
00682 
00683       /* Does the pointer reside in the .got.plt section?  */
00684       if (!(bfd_get_section_vma (obfd, got_plt) <= address
00685             && address < bfd_get_section_vma (obfd, got_plt)
00686                          + bfd_get_section_size (got_plt)))
00687         continue;
00688 
00689       /* We cannot check if NAME is a reference to mst_text_gnu_ifunc as in
00690          OBJFILE the symbol is undefined and the objfile having NAME defined
00691          may not yet have been loaded.  */
00692 
00693       if (string_buffer_size < name_len + got_suffix_len + 1)
00694         {
00695           string_buffer_size = 2 * (name_len + got_suffix_len);
00696           string_buffer = xrealloc (string_buffer, string_buffer_size);
00697         }
00698       memcpy (string_buffer, name, name_len);
00699       memcpy (&string_buffer[name_len], SYMBOL_GOT_PLT_SUFFIX,
00700               got_suffix_len + 1);
00701 
00702       msym = record_minimal_symbol (string_buffer, name_len + got_suffix_len,
00703                                     1, address, mst_slot_got_plt, got_plt,
00704                                     objfile);
00705       if (msym)
00706         SET_MSYMBOL_SIZE (msym, ptr_size);
00707     }
00708 
00709   do_cleanups (back_to);
00710 }
00711 
00712 /* The data pointer is htab_t for gnu_ifunc_record_cache_unchecked.  */
00713 
00714 static const struct objfile_data *elf_objfile_gnu_ifunc_cache_data;
00715 
00716 /* Map function names to CORE_ADDR in elf_objfile_gnu_ifunc_cache_data.  */
00717 
00718 struct elf_gnu_ifunc_cache
00719 {
00720   /* This is always a function entry address, not a function descriptor.  */
00721   CORE_ADDR addr;
00722 
00723   char name[1];
00724 };
00725 
00726 /* htab_hash for elf_objfile_gnu_ifunc_cache_data.  */
00727 
00728 static hashval_t
00729 elf_gnu_ifunc_cache_hash (const void *a_voidp)
00730 {
00731   const struct elf_gnu_ifunc_cache *a = a_voidp;
00732 
00733   return htab_hash_string (a->name);
00734 }
00735 
00736 /* htab_eq for elf_objfile_gnu_ifunc_cache_data.  */
00737 
00738 static int
00739 elf_gnu_ifunc_cache_eq (const void *a_voidp, const void *b_voidp)
00740 {
00741   const struct elf_gnu_ifunc_cache *a = a_voidp;
00742   const struct elf_gnu_ifunc_cache *b = b_voidp;
00743 
00744   return strcmp (a->name, b->name) == 0;
00745 }
00746 
00747 /* Record the target function address of a STT_GNU_IFUNC function NAME is the
00748    function entry address ADDR.  Return 1 if NAME and ADDR are considered as
00749    valid and therefore they were successfully recorded, return 0 otherwise.
00750 
00751    Function does not expect a duplicate entry.  Use
00752    elf_gnu_ifunc_resolve_by_cache first to check if the entry for NAME already
00753    exists.  */
00754 
00755 static int
00756 elf_gnu_ifunc_record_cache (const char *name, CORE_ADDR addr)
00757 {
00758   struct bound_minimal_symbol msym;
00759   asection *sect;
00760   struct objfile *objfile;
00761   htab_t htab;
00762   struct elf_gnu_ifunc_cache entry_local, *entry_p;
00763   void **slot;
00764 
00765   msym = lookup_minimal_symbol_by_pc (addr);
00766   if (msym.minsym == NULL)
00767     return 0;
00768   if (SYMBOL_VALUE_ADDRESS (msym.minsym) != addr)
00769     return 0;
00770   /* minimal symbols have always SYMBOL_OBJ_SECTION non-NULL.  */
00771   sect = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym)->the_bfd_section;
00772   objfile = msym.objfile;
00773 
00774   /* If .plt jumps back to .plt the symbol is still deferred for later
00775      resolution and it has no use for GDB.  Besides ".text" this symbol can
00776      reside also in ".opd" for ppc64 function descriptor.  */
00777   if (strcmp (bfd_get_section_name (objfile->obfd, sect), ".plt") == 0)
00778     return 0;
00779 
00780   htab = objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data);
00781   if (htab == NULL)
00782     {
00783       htab = htab_create_alloc_ex (1, elf_gnu_ifunc_cache_hash,
00784                                    elf_gnu_ifunc_cache_eq,
00785                                    NULL, &objfile->objfile_obstack,
00786                                    hashtab_obstack_allocate,
00787                                    dummy_obstack_deallocate);
00788       set_objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data, htab);
00789     }
00790 
00791   entry_local.addr = addr;
00792   obstack_grow (&objfile->objfile_obstack, &entry_local,
00793                 offsetof (struct elf_gnu_ifunc_cache, name));
00794   obstack_grow_str0 (&objfile->objfile_obstack, name);
00795   entry_p = obstack_finish (&objfile->objfile_obstack);
00796 
00797   slot = htab_find_slot (htab, entry_p, INSERT);
00798   if (*slot != NULL)
00799     {
00800       struct elf_gnu_ifunc_cache *entry_found_p = *slot;
00801       struct gdbarch *gdbarch = get_objfile_arch (objfile);
00802 
00803       if (entry_found_p->addr != addr)
00804         {
00805           /* This case indicates buggy inferior program, the resolved address
00806              should never change.  */
00807 
00808             warning (_("gnu-indirect-function \"%s\" has changed its resolved "
00809                        "function_address from %s to %s"),
00810                      name, paddress (gdbarch, entry_found_p->addr),
00811                      paddress (gdbarch, addr));
00812         }
00813 
00814       /* New ENTRY_P is here leaked/duplicate in the OBJFILE obstack.  */
00815     }
00816   *slot = entry_p;
00817 
00818   return 1;
00819 }
00820 
00821 /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
00822    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
00823    is not NULL) and the function returns 1.  It returns 0 otherwise.
00824 
00825    Only the elf_objfile_gnu_ifunc_cache_data hash table is searched by this
00826    function.  */
00827 
00828 static int
00829 elf_gnu_ifunc_resolve_by_cache (const char *name, CORE_ADDR *addr_p)
00830 {
00831   struct objfile *objfile;
00832 
00833   ALL_PSPACE_OBJFILES (current_program_space, objfile)
00834     {
00835       htab_t htab;
00836       struct elf_gnu_ifunc_cache *entry_p;
00837       void **slot;
00838 
00839       htab = objfile_data (objfile, elf_objfile_gnu_ifunc_cache_data);
00840       if (htab == NULL)
00841         continue;
00842 
00843       entry_p = alloca (sizeof (*entry_p) + strlen (name));
00844       strcpy (entry_p->name, name);
00845 
00846       slot = htab_find_slot (htab, entry_p, NO_INSERT);
00847       if (slot == NULL)
00848         continue;
00849       entry_p = *slot;
00850       gdb_assert (entry_p != NULL);
00851 
00852       if (addr_p)
00853         *addr_p = entry_p->addr;
00854       return 1;
00855     }
00856 
00857   return 0;
00858 }
00859 
00860 /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
00861    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
00862    is not NULL) and the function returns 1.  It returns 0 otherwise.
00863 
00864    Only the SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.
00865    elf_gnu_ifunc_resolve_by_cache must have been already called for NAME to
00866    prevent cache entries duplicates.  */
00867 
00868 static int
00869 elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
00870 {
00871   char *name_got_plt;
00872   struct objfile *objfile;
00873   const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
00874 
00875   name_got_plt = alloca (strlen (name) + got_suffix_len + 1);
00876   sprintf (name_got_plt, "%s" SYMBOL_GOT_PLT_SUFFIX, name);
00877 
00878   ALL_PSPACE_OBJFILES (current_program_space, objfile)
00879     {
00880       bfd *obfd = objfile->obfd;
00881       struct gdbarch *gdbarch = get_objfile_arch (objfile);
00882       struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
00883       size_t ptr_size = TYPE_LENGTH (ptr_type);
00884       CORE_ADDR pointer_address, addr;
00885       asection *plt;
00886       gdb_byte *buf = alloca (ptr_size);
00887       struct minimal_symbol *msym;
00888 
00889       msym = lookup_minimal_symbol (name_got_plt, NULL, objfile);
00890       if (msym == NULL)
00891         continue;
00892       if (MSYMBOL_TYPE (msym) != mst_slot_got_plt)
00893         continue;
00894       pointer_address = SYMBOL_VALUE_ADDRESS (msym);
00895 
00896       plt = bfd_get_section_by_name (obfd, ".plt");
00897       if (plt == NULL)
00898         continue;
00899 
00900       if (MSYMBOL_SIZE (msym) != ptr_size)
00901         continue;
00902       if (target_read_memory (pointer_address, buf, ptr_size) != 0)
00903         continue;
00904       addr = extract_typed_address (buf, ptr_type);
00905       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
00906                                                  &current_target);
00907 
00908       if (addr_p)
00909         *addr_p = addr;
00910       if (elf_gnu_ifunc_record_cache (name, addr))
00911         return 1;
00912     }
00913 
00914   return 0;
00915 }
00916 
00917 /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
00918    function NAME.  If the address is found it is stored to *ADDR_P (if ADDR_P
00919    is not NULL) and the function returns 1.  It returns 0 otherwise.
00920 
00921    Both the elf_objfile_gnu_ifunc_cache_data hash table and
00922    SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.  */
00923 
00924 static int
00925 elf_gnu_ifunc_resolve_name (const char *name, CORE_ADDR *addr_p)
00926 {
00927   if (elf_gnu_ifunc_resolve_by_cache (name, addr_p))
00928     return 1;
00929 
00930   if (elf_gnu_ifunc_resolve_by_got (name, addr_p))
00931     return 1;
00932 
00933   return 0;
00934 }
00935 
00936 /* Call STT_GNU_IFUNC - a function returning addresss of a real function to
00937    call.  PC is theSTT_GNU_IFUNC resolving function entry.  The value returned
00938    is the entry point of the resolved STT_GNU_IFUNC target function to call.
00939    */
00940 
00941 static CORE_ADDR
00942 elf_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
00943 {
00944   const char *name_at_pc;
00945   CORE_ADDR start_at_pc, address;
00946   struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
00947   struct value *function, *address_val;
00948 
00949   /* Try first any non-intrusive methods without an inferior call.  */
00950 
00951   if (find_pc_partial_function (pc, &name_at_pc, &start_at_pc, NULL)
00952       && start_at_pc == pc)
00953     {
00954       if (elf_gnu_ifunc_resolve_name (name_at_pc, &address))
00955         return address;
00956     }
00957   else
00958     name_at_pc = NULL;
00959 
00960   function = allocate_value (func_func_type);
00961   set_value_address (function, pc);
00962 
00963   /* STT_GNU_IFUNC resolver functions have no parameters.  FUNCTION is the
00964      function entry address.  ADDRESS may be a function descriptor.  */
00965 
00966   address_val = call_function_by_hand (function, 0, NULL);
00967   address = value_as_address (address_val);
00968   address = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
00969                                                 &current_target);
00970 
00971   if (name_at_pc)
00972     elf_gnu_ifunc_record_cache (name_at_pc, address);
00973 
00974   return address;
00975 }
00976 
00977 /* Handle inferior hit of bp_gnu_ifunc_resolver, see its definition.  */
00978 
00979 static void
00980 elf_gnu_ifunc_resolver_stop (struct breakpoint *b)
00981 {
00982   struct breakpoint *b_return;
00983   struct frame_info *prev_frame = get_prev_frame (get_current_frame ());
00984   struct frame_id prev_frame_id = get_stack_frame_id (prev_frame);
00985   CORE_ADDR prev_pc = get_frame_pc (prev_frame);
00986   int thread_id = pid_to_thread_id (inferior_ptid);
00987 
00988   gdb_assert (b->type == bp_gnu_ifunc_resolver);
00989 
00990   for (b_return = b->related_breakpoint; b_return != b;
00991        b_return = b_return->related_breakpoint)
00992     {
00993       gdb_assert (b_return->type == bp_gnu_ifunc_resolver_return);
00994       gdb_assert (b_return->loc != NULL && b_return->loc->next == NULL);
00995       gdb_assert (frame_id_p (b_return->frame_id));
00996 
00997       if (b_return->thread == thread_id
00998           && b_return->loc->requested_address == prev_pc
00999           && frame_id_eq (b_return->frame_id, prev_frame_id))
01000         break;
01001     }
01002 
01003   if (b_return == b)
01004     {
01005       struct symtab_and_line sal;
01006 
01007       /* No need to call find_pc_line for symbols resolving as this is only
01008          a helper breakpointer never shown to the user.  */
01009 
01010       init_sal (&sal);
01011       sal.pspace = current_inferior ()->pspace;
01012       sal.pc = prev_pc;
01013       sal.section = find_pc_overlay (sal.pc);
01014       sal.explicit_pc = 1;
01015       b_return = set_momentary_breakpoint (get_frame_arch (prev_frame), sal,
01016                                            prev_frame_id,
01017                                            bp_gnu_ifunc_resolver_return);
01018 
01019       /* set_momentary_breakpoint invalidates PREV_FRAME.  */
01020       prev_frame = NULL;
01021 
01022       /* Add new b_return to the ring list b->related_breakpoint.  */
01023       gdb_assert (b_return->related_breakpoint == b_return);
01024       b_return->related_breakpoint = b->related_breakpoint;
01025       b->related_breakpoint = b_return;
01026     }
01027 }
01028 
01029 /* Handle inferior hit of bp_gnu_ifunc_resolver_return, see its definition.  */
01030 
01031 static void
01032 elf_gnu_ifunc_resolver_return_stop (struct breakpoint *b)
01033 {
01034   struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
01035   struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
01036   struct type *value_type = TYPE_TARGET_TYPE (func_func_type);
01037   struct regcache *regcache = get_thread_regcache (inferior_ptid);
01038   struct value *func_func;
01039   struct value *value;
01040   CORE_ADDR resolved_address, resolved_pc;
01041   struct symtab_and_line sal;
01042   struct symtabs_and_lines sals, sals_end;
01043 
01044   gdb_assert (b->type == bp_gnu_ifunc_resolver_return);
01045 
01046   while (b->related_breakpoint != b)
01047     {
01048       struct breakpoint *b_next = b->related_breakpoint;
01049 
01050       switch (b->type)
01051         {
01052         case bp_gnu_ifunc_resolver:
01053           break;
01054         case bp_gnu_ifunc_resolver_return:
01055           delete_breakpoint (b);
01056           break;
01057         default:
01058           internal_error (__FILE__, __LINE__,
01059                           _("handle_inferior_event: Invalid "
01060                             "gnu-indirect-function breakpoint type %d"),
01061                           (int) b->type);
01062         }
01063       b = b_next;
01064     }
01065   gdb_assert (b->type == bp_gnu_ifunc_resolver);
01066   gdb_assert (b->loc->next == NULL);
01067 
01068   func_func = allocate_value (func_func_type);
01069   set_value_address (func_func, b->loc->related_address);
01070 
01071   value = allocate_value (value_type);
01072   gdbarch_return_value (gdbarch, func_func, value_type, regcache,
01073                         value_contents_raw (value), NULL);
01074   resolved_address = value_as_address (value);
01075   resolved_pc = gdbarch_convert_from_func_ptr_addr (gdbarch,
01076                                                     resolved_address,
01077                                                     &current_target);
01078 
01079   gdb_assert (current_program_space == b->pspace || b->pspace == NULL);
01080   elf_gnu_ifunc_record_cache (b->addr_string, resolved_pc);
01081 
01082   sal = find_pc_line (resolved_pc, 0);
01083   sals.nelts = 1;
01084   sals.sals = &sal;
01085   sals_end.nelts = 0;
01086 
01087   b->type = bp_breakpoint;
01088   update_breakpoint_locations (b, sals, sals_end);
01089 }
01090 
01091 /* Scan and build partial symbols for a symbol file.
01092    We have been initialized by a call to elf_symfile_init, which
01093    currently does nothing.
01094 
01095    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
01096    in each section.  We simplify it down to a single offset for all
01097    symbols.  FIXME.
01098 
01099    This function only does the minimum work necessary for letting the
01100    user "name" things symbolically; it does not read the entire symtab.
01101    Instead, it reads the external and static symbols and puts them in partial
01102    symbol tables.  When more extensive information is requested of a
01103    file, the corresponding partial symbol table is mutated into a full
01104    fledged symbol table by going back and reading the symbols
01105    for real.
01106 
01107    We look for sections with specific names, to tell us what debug
01108    format to look for:  FIXME!!!
01109 
01110    elfstab_build_psymtabs() handles STABS symbols;
01111    mdebug_build_psymtabs() handles ECOFF debugging information.
01112 
01113    Note that ELF files have a "minimal" symbol table, which looks a lot
01114    like a COFF symbol table, but has only the minimal information necessary
01115    for linking.  We process this also, and use the information to
01116    build gdb's minimal symbol table.  This gives us some minimal debugging
01117    capability even for files compiled without -g.  */
01118 
01119 static void
01120 elf_symfile_read (struct objfile *objfile, int symfile_flags)
01121 {
01122   bfd *synth_abfd, *abfd = objfile->obfd;
01123   struct elfinfo ei;
01124   struct cleanup *back_to;
01125   long symcount = 0, dynsymcount = 0, synthcount, storage_needed;
01126   asymbol **symbol_table = NULL, **dyn_symbol_table = NULL;
01127   asymbol *synthsyms;
01128   struct dbx_symfile_info *dbx;
01129 
01130   if (symtab_create_debug)
01131     {
01132       fprintf_unfiltered (gdb_stdlog,
01133                           "Reading minimal symbols of objfile %s ...\n",
01134                           objfile_name (objfile));
01135     }
01136 
01137   init_minimal_symbol_collection ();
01138   back_to = make_cleanup_discard_minimal_symbols ();
01139 
01140   memset ((char *) &ei, 0, sizeof (ei));
01141 
01142   /* Allocate struct to keep track of the symfile.  */
01143   dbx = XCNEW (struct dbx_symfile_info);
01144   set_objfile_data (objfile, dbx_objfile_data_key, dbx);
01145   make_cleanup (free_elfinfo, (void *) objfile);
01146 
01147   /* Process the normal ELF symbol table first.  This may write some
01148      chain of info into the dbx_symfile_info of the objfile, which can
01149      later be used by elfstab_offset_sections.  */
01150 
01151   storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
01152   if (storage_needed < 0)
01153     error (_("Can't read symbols from %s: %s"),
01154            bfd_get_filename (objfile->obfd),
01155            bfd_errmsg (bfd_get_error ()));
01156 
01157   if (storage_needed > 0)
01158     {
01159       symbol_table = (asymbol **) xmalloc (storage_needed);
01160       make_cleanup (xfree, symbol_table);
01161       symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
01162 
01163       if (symcount < 0)
01164         error (_("Can't read symbols from %s: %s"),
01165                bfd_get_filename (objfile->obfd),
01166                bfd_errmsg (bfd_get_error ()));
01167 
01168       elf_symtab_read (objfile, ST_REGULAR, symcount, symbol_table, 0);
01169     }
01170 
01171   /* Add the dynamic symbols.  */
01172 
01173   storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd);
01174 
01175   if (storage_needed > 0)
01176     {
01177       /* Memory gets permanently referenced from ABFD after
01178          bfd_get_synthetic_symtab so it must not get freed before ABFD gets.
01179          It happens only in the case when elf_slurp_reloc_table sees
01180          asection->relocation NULL.  Determining which section is asection is
01181          done by _bfd_elf_get_synthetic_symtab which is all a bfd
01182          implementation detail, though.  */
01183 
01184       dyn_symbol_table = bfd_alloc (abfd, storage_needed);
01185       dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd,
01186                                                      dyn_symbol_table);
01187 
01188       if (dynsymcount < 0)
01189         error (_("Can't read symbols from %s: %s"),
01190                bfd_get_filename (objfile->obfd),
01191                bfd_errmsg (bfd_get_error ()));
01192 
01193       elf_symtab_read (objfile, ST_DYNAMIC, dynsymcount, dyn_symbol_table, 0);
01194 
01195       elf_rel_plt_read (objfile, dyn_symbol_table);
01196     }
01197 
01198   /* Contrary to binutils --strip-debug/--only-keep-debug the strip command from
01199      elfutils (eu-strip) moves even the .symtab section into the .debug file.
01200 
01201      bfd_get_synthetic_symtab on ppc64 for each function descriptor ELF symbol
01202      'name' creates a new BSF_SYNTHETIC ELF symbol '.name' with its code
01203      address.  But with eu-strip files bfd_get_synthetic_symtab would fail to
01204      read the code address from .opd while it reads the .symtab section from
01205      a separate debug info file as the .opd section is SHT_NOBITS there.
01206 
01207      With SYNTH_ABFD the .opd section will be read from the original
01208      backlinked binary where it is valid.  */
01209 
01210   if (objfile->separate_debug_objfile_backlink)
01211     synth_abfd = objfile->separate_debug_objfile_backlink->obfd;
01212   else
01213     synth_abfd = abfd;
01214 
01215   /* Add synthetic symbols - for instance, names for any PLT entries.  */
01216 
01217   synthcount = bfd_get_synthetic_symtab (synth_abfd, symcount, symbol_table,
01218                                          dynsymcount, dyn_symbol_table,
01219                                          &synthsyms);
01220   if (synthcount > 0)
01221     {
01222       asymbol **synth_symbol_table;
01223       long i;
01224 
01225       make_cleanup (xfree, synthsyms);
01226       synth_symbol_table = xmalloc (sizeof (asymbol *) * synthcount);
01227       for (i = 0; i < synthcount; i++)
01228         synth_symbol_table[i] = synthsyms + i;
01229       make_cleanup (xfree, synth_symbol_table);
01230       elf_symtab_read (objfile, ST_SYNTHETIC, synthcount,
01231                        synth_symbol_table, 1);
01232     }
01233 
01234   /* Install any minimal symbols that have been collected as the current
01235      minimal symbols for this objfile.  The debug readers below this point
01236      should not generate new minimal symbols; if they do it's their
01237      responsibility to install them.  "mdebug" appears to be the only one
01238      which will do this.  */
01239 
01240   install_minimal_symbols (objfile);
01241   do_cleanups (back_to);
01242 
01243   if (symtab_create_debug)
01244     fprintf_unfiltered (gdb_stdlog, "Done reading minimal symbols.\n");
01245 
01246   /* Now process debugging information, which is contained in
01247      special ELF sections.  */
01248 
01249   /* We first have to find them...  */
01250   bfd_map_over_sections (abfd, elf_locate_sections, (void *) & ei);
01251 
01252   /* ELF debugging information is inserted into the psymtab in the
01253      order of least informative first - most informative last.  Since
01254      the psymtab table is searched `most recent insertion first' this
01255      increases the probability that more detailed debug information
01256      for a section is found.
01257 
01258      For instance, an object file might contain both .mdebug (XCOFF)
01259      and .debug_info (DWARF2) sections then .mdebug is inserted first
01260      (searched last) and DWARF2 is inserted last (searched first).  If
01261      we don't do this then the XCOFF info is found first - for code in
01262      an included file XCOFF info is useless.  */
01263 
01264   if (ei.mdebugsect)
01265     {
01266       const struct ecoff_debug_swap *swap;
01267 
01268       /* .mdebug section, presumably holding ECOFF debugging
01269          information.  */
01270       swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
01271       if (swap)
01272         elfmdebug_build_psymtabs (objfile, swap, ei.mdebugsect);
01273     }
01274   if (ei.stabsect)
01275     {
01276       asection *str_sect;
01277 
01278       /* Stab sections have an associated string table that looks like
01279          a separate section.  */
01280       str_sect = bfd_get_section_by_name (abfd, ".stabstr");
01281 
01282       /* FIXME should probably warn about a stab section without a stabstr.  */
01283       if (str_sect)
01284         elfstab_build_psymtabs (objfile,
01285                                 ei.stabsect,
01286                                 str_sect->filepos,
01287                                 bfd_section_size (abfd, str_sect));
01288     }
01289 
01290   if (dwarf2_has_info (objfile, NULL))
01291     {
01292       /* elf_sym_fns_gdb_index cannot handle simultaneous non-DWARF debug
01293          information present in OBJFILE.  If there is such debug info present
01294          never use .gdb_index.  */
01295 
01296       if (!objfile_has_partial_symbols (objfile)
01297           && dwarf2_initialize_objfile (objfile))
01298         objfile_set_sym_fns (objfile, &elf_sym_fns_gdb_index);
01299       else
01300         {
01301           /* It is ok to do this even if the stabs reader made some
01302              partial symbols, because OBJF_PSYMTABS_READ has not been
01303              set, and so our lazy reader function will still be called
01304              when needed.  */
01305           objfile_set_sym_fns (objfile, &elf_sym_fns_lazy_psyms);
01306         }
01307     }
01308   /* If the file has its own symbol tables it has no separate debug
01309      info.  `.dynsym'/`.symtab' go to MSYMBOLS, `.debug_info' goes to
01310      SYMTABS/PSYMTABS.  `.gnu_debuglink' may no longer be present with
01311      `.note.gnu.build-id'.
01312 
01313      .gnu_debugdata is !objfile_has_partial_symbols because it contains only
01314      .symtab, not .debug_* section.  But if we already added .gnu_debugdata as
01315      an objfile via find_separate_debug_file_in_section there was no separate
01316      debug info available.  Therefore do not attempt to search for another one,
01317      objfile->separate_debug_objfile->separate_debug_objfile GDB guarantees to
01318      be NULL and we would possibly violate it.  */
01319 
01320   else if (!objfile_has_partial_symbols (objfile)
01321            && objfile->separate_debug_objfile == NULL
01322            && objfile->separate_debug_objfile_backlink == NULL)
01323     {
01324       char *debugfile;
01325 
01326       debugfile = find_separate_debug_file_by_buildid (objfile);
01327 
01328       if (debugfile == NULL)
01329         debugfile = find_separate_debug_file_by_debuglink (objfile);
01330 
01331       if (debugfile)
01332         {
01333           struct cleanup *cleanup = make_cleanup (xfree, debugfile);
01334           bfd *abfd = symfile_bfd_open (debugfile);
01335 
01336           make_cleanup_bfd_unref (abfd);
01337           symbol_file_add_separate (abfd, debugfile, symfile_flags, objfile);
01338           do_cleanups (cleanup);
01339         }
01340     }
01341 }
01342 
01343 /* Callback to lazily read psymtabs.  */
01344 
01345 static void
01346 read_psyms (struct objfile *objfile)
01347 {
01348   if (dwarf2_has_info (objfile, NULL))
01349     dwarf2_build_psymtabs (objfile);
01350 }
01351 
01352 /* This cleans up the objfile's dbx symfile info, and the chain of
01353    stab_section_info's, that might be dangling from it.  */
01354 
01355 static void
01356 free_elfinfo (void *objp)
01357 {
01358   struct objfile *objfile = (struct objfile *) objp;
01359   struct dbx_symfile_info *dbxinfo = DBX_SYMFILE_INFO (objfile);
01360   struct stab_section_info *ssi, *nssi;
01361 
01362   ssi = dbxinfo->stab_section_info;
01363   while (ssi)
01364     {
01365       nssi = ssi->next;
01366       xfree (ssi);
01367       ssi = nssi;
01368     }
01369 
01370   dbxinfo->stab_section_info = 0;       /* Just say No mo info about this.  */
01371 }
01372 
01373 
01374 /* Initialize anything that needs initializing when a completely new symbol
01375    file is specified (not just adding some symbols from another file, e.g. a
01376    shared library).
01377 
01378    We reinitialize buildsym, since we may be reading stabs from an ELF
01379    file.  */
01380 
01381 static void
01382 elf_new_init (struct objfile *ignore)
01383 {
01384   stabsread_new_init ();
01385   buildsym_new_init ();
01386 }
01387 
01388 /* Perform any local cleanups required when we are done with a particular
01389    objfile.  I.E, we are in the process of discarding all symbol information
01390    for an objfile, freeing up all memory held for it, and unlinking the
01391    objfile struct from the global list of known objfiles.  */
01392 
01393 static void
01394 elf_symfile_finish (struct objfile *objfile)
01395 {
01396   dwarf2_free_objfile (objfile);
01397 }
01398 
01399 /* ELF specific initialization routine for reading symbols.
01400 
01401    It is passed a pointer to a struct sym_fns which contains, among other
01402    things, the BFD for the file whose symbols are being read, and a slot for
01403    a pointer to "private data" which we can fill with goodies.
01404 
01405    For now at least, we have nothing in particular to do, so this function is
01406    just a stub.  */
01407 
01408 static void
01409 elf_symfile_init (struct objfile *objfile)
01410 {
01411   /* ELF objects may be reordered, so set OBJF_REORDERED.  If we
01412      find this causes a significant slowdown in gdb then we could
01413      set it in the debug symbol readers only when necessary.  */
01414   objfile->flags |= OBJF_REORDERED;
01415 }
01416 
01417 /* When handling an ELF file that contains Sun STABS debug info,
01418    some of the debug info is relative to the particular chunk of the
01419    section that was generated in its individual .o file.  E.g.
01420    offsets to static variables are relative to the start of the data
01421    segment *for that module before linking*.  This information is
01422    painfully squirreled away in the ELF symbol table as local symbols
01423    with wierd names.  Go get 'em when needed.  */
01424 
01425 void
01426 elfstab_offset_sections (struct objfile *objfile, struct partial_symtab *pst)
01427 {
01428   const char *filename = pst->filename;
01429   struct dbx_symfile_info *dbx = DBX_SYMFILE_INFO (objfile);
01430   struct stab_section_info *maybe = dbx->stab_section_info;
01431   struct stab_section_info *questionable = 0;
01432   int i;
01433 
01434   /* The ELF symbol info doesn't include path names, so strip the path
01435      (if any) from the psymtab filename.  */
01436   filename = lbasename (filename);
01437 
01438   /* FIXME:  This linear search could speed up significantly
01439      if it was chained in the right order to match how we search it,
01440      and if we unchained when we found a match.  */
01441   for (; maybe; maybe = maybe->next)
01442     {
01443       if (filename[0] == maybe->filename[0]
01444           && filename_cmp (filename, maybe->filename) == 0)
01445         {
01446           /* We found a match.  But there might be several source files
01447              (from different directories) with the same name.  */
01448           if (0 == maybe->found)
01449             break;
01450           questionable = maybe; /* Might use it later.  */
01451         }
01452     }
01453 
01454   if (maybe == 0 && questionable != 0)
01455     {
01456       complaint (&symfile_complaints,
01457                  _("elf/stab section information questionable for %s"),
01458                  filename);
01459       maybe = questionable;
01460     }
01461 
01462   if (maybe)
01463     {
01464       /* Found it!  Allocate a new psymtab struct, and fill it in.  */
01465       maybe->found++;
01466       pst->section_offsets = (struct section_offsets *)
01467         obstack_alloc (&objfile->objfile_obstack,
01468                        SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
01469       for (i = 0; i < maybe->num_sections; i++)
01470         (pst->section_offsets)->offsets[i] = maybe->sections[i];
01471       return;
01472     }
01473 
01474   /* We were unable to find any offsets for this file.  Complain.  */
01475   if (dbx->stab_section_info)   /* If there *is* any info, */
01476     complaint (&symfile_complaints,
01477                _("elf/stab section information missing for %s"), filename);
01478 }
01479 
01480 /* Implementation of `sym_get_probes', as documented in symfile.h.  */
01481 
01482 static VEC (probe_p) *
01483 elf_get_probes (struct objfile *objfile)
01484 {
01485   VEC (probe_p) *probes_per_objfile;
01486 
01487   /* Have we parsed this objfile's probes already?  */
01488   probes_per_objfile = objfile_data (objfile, probe_key);
01489 
01490   if (!probes_per_objfile)
01491     {
01492       int ix;
01493       const struct probe_ops *probe_ops;
01494 
01495       /* Here we try to gather information about all types of probes from the
01496          objfile.  */
01497       for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops);
01498            ix++)
01499         probe_ops->get_probes (&probes_per_objfile, objfile);
01500 
01501       if (probes_per_objfile == NULL)
01502         {
01503           VEC_reserve (probe_p, probes_per_objfile, 1);
01504           gdb_assert (probes_per_objfile != NULL);
01505         }
01506 
01507       set_objfile_data (objfile, probe_key, probes_per_objfile);
01508     }
01509 
01510   return probes_per_objfile;
01511 }
01512 
01513 /* Implementation of `sym_get_probe_argument_count', as documented in
01514    symfile.h.  */
01515 
01516 static unsigned
01517 elf_get_probe_argument_count (struct probe *probe)
01518 {
01519   return probe->pops->get_probe_argument_count (probe);
01520 }
01521 
01522 /* Implementation of `sym_can_evaluate_probe_arguments', as documented in
01523    symfile.h.  */
01524 
01525 static int
01526 elf_can_evaluate_probe_arguments (struct probe *probe)
01527 {
01528   return probe->pops->can_evaluate_probe_arguments (probe);
01529 }
01530 
01531 /* Implementation of `sym_evaluate_probe_argument', as documented in
01532    symfile.h.  */
01533 
01534 static struct value *
01535 elf_evaluate_probe_argument (struct probe *probe, unsigned n)
01536 {
01537   return probe->pops->evaluate_probe_argument (probe, n);
01538 }
01539 
01540 /* Implementation of `sym_compile_to_ax', as documented in symfile.h.  */
01541 
01542 static void
01543 elf_compile_to_ax (struct probe *probe,
01544                    struct agent_expr *expr,
01545                    struct axs_value *value,
01546                    unsigned n)
01547 {
01548   probe->pops->compile_to_ax (probe, expr, value, n);
01549 }
01550 
01551 /* Implementation of `sym_relocate_probe', as documented in symfile.h.  */
01552 
01553 static void
01554 elf_symfile_relocate_probe (struct objfile *objfile,
01555                             const struct section_offsets *new_offsets,
01556                             const struct section_offsets *delta)
01557 {
01558   int ix;
01559   VEC (probe_p) *probes = objfile_data (objfile, probe_key);
01560   struct probe *probe;
01561 
01562   for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
01563     probe->pops->relocate (probe, ANOFFSET (delta, SECT_OFF_TEXT (objfile)));
01564 }
01565 
01566 /* Helper function used to free the space allocated for storing SystemTap
01567    probe information.  */
01568 
01569 static void
01570 probe_key_free (struct objfile *objfile, void *d)
01571 {
01572   int ix;
01573   VEC (probe_p) *probes = d;
01574   struct probe *probe;
01575 
01576   for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
01577     probe->pops->destroy (probe);
01578 
01579   VEC_free (probe_p, probes);
01580 }
01581 
01582 
01583 
01584 /* Implementation `sym_probe_fns', as documented in symfile.h.  */
01585 
01586 static const struct sym_probe_fns elf_probe_fns =
01587 {
01588   elf_get_probes,                   /* sym_get_probes */
01589   elf_get_probe_argument_count,     /* sym_get_probe_argument_count */
01590   elf_can_evaluate_probe_arguments, /* sym_can_evaluate_probe_arguments */
01591   elf_evaluate_probe_argument,      /* sym_evaluate_probe_argument */
01592   elf_compile_to_ax,                /* sym_compile_to_ax */
01593   elf_symfile_relocate_probe,       /* sym_relocate_probe */
01594 };
01595 
01596 /* Register that we are able to handle ELF object file formats.  */
01597 
01598 static const struct sym_fns elf_sym_fns =
01599 {
01600   elf_new_init,                 /* init anything gbl to entire symtab */
01601   elf_symfile_init,             /* read initial info, setup for sym_read() */
01602   elf_symfile_read,             /* read a symbol file into symtab */
01603   NULL,                         /* sym_read_psymbols */
01604   elf_symfile_finish,           /* finished with file, cleanup */
01605   default_symfile_offsets,      /* Translate ext. to int. relocation */
01606   elf_symfile_segments,         /* Get segment information from a file.  */
01607   NULL,
01608   default_symfile_relocate,     /* Relocate a debug section.  */
01609   &elf_probe_fns,               /* sym_probe_fns */
01610   &psym_functions
01611 };
01612 
01613 /* The same as elf_sym_fns, but not registered and lazily reads
01614    psymbols.  */
01615 
01616 static const struct sym_fns elf_sym_fns_lazy_psyms =
01617 {
01618   elf_new_init,                 /* init anything gbl to entire symtab */
01619   elf_symfile_init,             /* read initial info, setup for sym_read() */
01620   elf_symfile_read,             /* read a symbol file into symtab */
01621   read_psyms,                   /* sym_read_psymbols */
01622   elf_symfile_finish,           /* finished with file, cleanup */
01623   default_symfile_offsets,      /* Translate ext. to int. relocation */
01624   elf_symfile_segments,         /* Get segment information from a file.  */
01625   NULL,
01626   default_symfile_relocate,     /* Relocate a debug section.  */
01627   &elf_probe_fns,               /* sym_probe_fns */
01628   &psym_functions
01629 };
01630 
01631 /* The same as elf_sym_fns, but not registered and uses the
01632    DWARF-specific GNU index rather than psymtab.  */
01633 static const struct sym_fns elf_sym_fns_gdb_index =
01634 {
01635   elf_new_init,                 /* init anything gbl to entire symab */
01636   elf_symfile_init,             /* read initial info, setup for sym_red() */
01637   elf_symfile_read,             /* read a symbol file into symtab */
01638   NULL,                         /* sym_read_psymbols */
01639   elf_symfile_finish,           /* finished with file, cleanup */
01640   default_symfile_offsets,      /* Translate ext. to int. relocatin */
01641   elf_symfile_segments,         /* Get segment information from a file.  */
01642   NULL,
01643   default_symfile_relocate,     /* Relocate a debug section.  */
01644   &elf_probe_fns,               /* sym_probe_fns */
01645   &dwarf2_gdb_index_functions
01646 };
01647 
01648 /* STT_GNU_IFUNC resolver vector to be installed to gnu_ifunc_fns_p.  */
01649 
01650 static const struct gnu_ifunc_fns elf_gnu_ifunc_fns =
01651 {
01652   elf_gnu_ifunc_resolve_addr,
01653   elf_gnu_ifunc_resolve_name,
01654   elf_gnu_ifunc_resolver_stop,
01655   elf_gnu_ifunc_resolver_return_stop
01656 };
01657 
01658 void
01659 _initialize_elfread (void)
01660 {
01661   probe_key = register_objfile_data_with_cleanup (NULL, probe_key_free);
01662   add_symtab_fns (bfd_target_elf_flavour, &elf_sym_fns);
01663 
01664   elf_objfile_gnu_ifunc_cache_data = register_objfile_data ();
01665   gnu_ifunc_fns_p = &elf_gnu_ifunc_fns;
01666 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines