Home | History | Annotate | Download | only in libelf
      1 /* Get symbol information and separate section index from symbol table
      2    at the given index.
      3    Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
      4    This file is part of Red Hat elfutils.
      5    Written by Ulrich Drepper <drepper (at) redhat.com>, 2000.
      6 
      7    Red Hat elfutils is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by the
      9    Free Software Foundation; version 2 of the License.
     10 
     11    Red Hat elfutils is distributed in the hope that it will be useful, but
     12    WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14    General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License along
     17    with Red Hat elfutils; if not, write to the Free Software Foundation,
     18    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     19 
     20    In addition, as a special exception, Red Hat, Inc. gives You the
     21    additional right to link the code of Red Hat elfutils with code licensed
     22    under any Open Source Initiative certified open source license
     23    (http://www.opensource.org/licenses/index.php) which requires the
     24    distribution of source code with any binary distribution and to
     25    distribute linked combinations of the two.  Non-GPL Code permitted under
     26    this exception must only link to the code of Red Hat elfutils through
     27    those well defined interfaces identified in the file named EXCEPTION
     28    found in the source code files (the "Approved Interfaces").  The files
     29    of Non-GPL Code may instantiate templates or use macros or inline
     30    functions from the Approved Interfaces without causing the resulting
     31    work to be covered by the GNU General Public License.  Only Red Hat,
     32    Inc. may make changes or additions to the list of Approved Interfaces.
     33    Red Hat's grant of this exception is conditioned upon your not adding
     34    any new exceptions.  If you wish to add a new Approved Interface or
     35    exception, please contact Red Hat.  You must obey the GNU General Public
     36    License in all respects for all of the Red Hat elfutils code and other
     37    code used in conjunction with Red Hat elfutils except the Non-GPL Code
     38    covered by this exception.  If you modify this file, you may extend this
     39    exception to your version of the file, but you are not obligated to do
     40    so.  If you do not wish to provide this exception without modification,
     41    you must delete this exception statement from your version and license
     42    this file solely under the GPL without exception.
     43 
     44    Red Hat elfutils is an included package of the Open Invention Network.
     45    An included package of the Open Invention Network is a package for which
     46    Open Invention Network licensees cross-license their patents.  No patent
     47    license is granted, either expressly or impliedly, by designation as an
     48    included package.  Should you wish to participate in the Open Invention
     49    Network licensing program, please visit www.openinventionnetwork.com
     50    <http://www.openinventionnetwork.com>.  */
     51 
     52 #ifdef HAVE_CONFIG_H
     53 # include <config.h>
     54 #endif
     55 
     56 #include <assert.h>
     57 #include <gelf.h>
     58 #include <string.h>
     59 
     60 #include "libelfP.h"
     61 
     62 
     63 GElf_Sym *
     64 gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
     65      Elf_Data *symdata;
     66      Elf_Data *shndxdata;
     67      int ndx;
     68      GElf_Sym *dst;
     69      Elf32_Word *dstshndx;
     70 {
     71   Elf_Data_Scn *symdata_scn = (Elf_Data_Scn *) symdata;
     72   Elf_Data_Scn *shndxdata_scn = (Elf_Data_Scn *) shndxdata;
     73   GElf_Sym *result = NULL;
     74   Elf32_Word shndx = 0;
     75 
     76   if (symdata == NULL)
     77     return NULL;
     78 
     79   if (unlikely (symdata->d_type != ELF_T_SYM)
     80       || (likely (shndxdata_scn != NULL)
     81 	  && unlikely (shndxdata->d_type != ELF_T_WORD)))
     82     {
     83       __libelf_seterrno (ELF_E_INVALID_HANDLE);
     84       return NULL;
     85     }
     86 
     87   rwlock_rdlock (symdata_scn->s->elf->lock);
     88 
     89   /* The user is not required to pass a data descriptor for an extended
     90      section index table.  */
     91   if (likely (shndxdata_scn != NULL))
     92     {
     93       if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
     94 	{
     95 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
     96 	  goto out;
     97 	}
     98 
     99       shndx = ((Elf32_Word *) shndxdata_scn->d.d_buf)[ndx];
    100     }
    101 
    102   /* This is the one place where we have to take advantage of the fact
    103      that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
    104      The interface is broken so that it requires this hack.  */
    105   if (symdata_scn->s->elf->class == ELFCLASS32)
    106     {
    107       Elf32_Sym *src;
    108 
    109       /* Here it gets a bit more complicated.  The format of the symbol
    110 	 table entries has to be adopted.  The user better has provided
    111 	 a buffer where we can store the information.  While copying the
    112 	 data we are converting the format.  */
    113       if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
    114 	{
    115 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
    116 	  goto out;
    117 	}
    118 
    119       src = &((Elf32_Sym *) symdata->d_buf)[ndx];
    120 
    121       /* This might look like a simple copy operation but it's
    122 	 not.  There are zero- and sign-extensions going on.  */
    123 #define COPY(name) \
    124       dst->name = src->name
    125       COPY (st_name);
    126       /* Please note that we can simply copy the `st_info' element since
    127 	 the definitions of ELFxx_ST_BIND and ELFxx_ST_TYPE are the same
    128 	 for the 64 bit variant.  */
    129       COPY (st_info);
    130       COPY (st_other);
    131       COPY (st_shndx);
    132       COPY (st_value);
    133       COPY (st_size);
    134     }
    135   else
    136     {
    137       /* If this is a 64 bit object it's easy.  */
    138       assert (sizeof (GElf_Sym) == sizeof (Elf64_Sym));
    139 
    140       /* The data is already in the correct form.  Just make sure the
    141 	 index is OK.  */
    142       if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
    143 	{
    144 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
    145 	  goto out;
    146 	}
    147 
    148       *dst = ((GElf_Sym *) symdata->d_buf)[ndx];
    149     }
    150 
    151   /* Now we can store the section index.  */
    152   if (dstshndx != NULL)
    153     *dstshndx = shndx;
    154 
    155   result = dst;
    156 
    157  out:
    158   rwlock_unlock (symdata_scn->s->elf->lock);
    159 
    160   return result;
    161 }
    162