Home | History | Annotate | Download | only in libelf
      1 /* Return section index of section header string table.
      2    Copyright (C) 2002, 2005, 2009 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      5 
      6    Red Hat elfutils is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by the
      8    Free Software Foundation; version 2 of the License.
      9 
     10    Red Hat elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License along
     16    with Red Hat elfutils; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     18 
     19    In addition, as a special exception, Red Hat, Inc. gives You the
     20    additional right to link the code of Red Hat elfutils with code licensed
     21    under any Open Source Initiative certified open source license
     22    (http://www.opensource.org/licenses/index.php) which requires the
     23    distribution of source code with any binary distribution and to
     24    distribute linked combinations of the two.  Non-GPL Code permitted under
     25    this exception must only link to the code of Red Hat elfutils through
     26    those well defined interfaces identified in the file named EXCEPTION
     27    found in the source code files (the "Approved Interfaces").  The files
     28    of Non-GPL Code may instantiate templates or use macros or inline
     29    functions from the Approved Interfaces without causing the resulting
     30    work to be covered by the GNU General Public License.  Only Red Hat,
     31    Inc. may make changes or additions to the list of Approved Interfaces.
     32    Red Hat's grant of this exception is conditioned upon your not adding
     33    any new exceptions.  If you wish to add a new Approved Interface or
     34    exception, please contact Red Hat.  You must obey the GNU General Public
     35    License in all respects for all of the Red Hat elfutils code and other
     36    code used in conjunction with Red Hat elfutils except the Non-GPL Code
     37    covered by this exception.  If you modify this file, you may extend this
     38    exception to your version of the file, but you are not obligated to do
     39    so.  If you do not wish to provide this exception without modification,
     40    you must delete this exception statement from your version and license
     41    this file solely under the GPL without exception.
     42 
     43    Red Hat elfutils is an included package of the Open Invention Network.
     44    An included package of the Open Invention Network is a package for which
     45    Open Invention Network licensees cross-license their patents.  No patent
     46    license is granted, either expressly or impliedly, by designation as an
     47    included package.  Should you wish to participate in the Open Invention
     48    Network licensing program, please visit www.openinventionnetwork.com
     49    <http://www.openinventionnetwork.com>.  */
     50 
     51 #ifdef HAVE_CONFIG_H
     52 # include <config.h>
     53 #endif
     54 
     55 #include <assert.h>
     56 #include <errno.h>
     57 #include <gelf.h>
     58 #include <stddef.h>
     59 #include <unistd.h>
     60 
     61 #include <system.h>
     62 #include "libelfP.h"
     63 #include "common.h"
     64 
     65 
     66 int
     67 elf_getshdrstrndx (elf, dst)
     68      Elf *elf;
     69      size_t *dst;
     70 {
     71   int result = 0;
     72 
     73   if (elf == NULL)
     74     return -1;
     75 
     76   if (unlikely (elf->kind != ELF_K_ELF))
     77     {
     78       __libelf_seterrno (ELF_E_INVALID_HANDLE);
     79       return -1;
     80     }
     81 
     82   rwlock_rdlock (elf->lock);
     83 
     84   /* We rely here on the fact that the `elf' element is a common prefix
     85      of `elf32' and `elf64'.  */
     86   assert (offsetof (struct Elf, state.elf.ehdr)
     87 	  == offsetof (struct Elf, state.elf32.ehdr));
     88   assert (sizeof (elf->state.elf.ehdr)
     89 	  == sizeof (elf->state.elf32.ehdr));
     90   assert (offsetof (struct Elf, state.elf.ehdr)
     91 	  == offsetof (struct Elf, state.elf64.ehdr));
     92   assert (sizeof (elf->state.elf.ehdr)
     93 	  == sizeof (elf->state.elf64.ehdr));
     94 
     95   if (unlikely (elf->state.elf.ehdr == NULL))
     96     {
     97       __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
     98       result = -1;
     99     }
    100   else
    101     {
    102       Elf32_Word num;
    103 
    104       num = (elf->class == ELFCLASS32
    105 	     ? elf->state.elf32.ehdr->e_shstrndx
    106 	     : elf->state.elf64.ehdr->e_shstrndx);
    107 
    108       /* Determine whether the index is too big to fit in the ELF
    109 	 header.  */
    110       if (unlikely (num == SHN_XINDEX))
    111 	{
    112 	  /* Yes.  Search the zeroth section header.  */
    113 	  if (elf->class == ELFCLASS32)
    114 	    {
    115 	      size_t offset;
    116 
    117 	      if (elf->state.elf32.scns.data[0].shdr.e32 != NULL)
    118 		{
    119 		  num = elf->state.elf32.scns.data[0].shdr.e32->sh_link;
    120 		  goto success;
    121 		}
    122 
    123 	      offset = elf->state.elf32.ehdr->e_shoff;
    124 
    125 	      if (elf->map_address != NULL
    126 		  && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
    127 		  && (ALLOW_UNALIGNED
    128 		      || (((size_t) ((char *) elf->map_address + offset))
    129 			  & (__alignof__ (Elf32_Shdr) - 1)) == 0))
    130 		/* We can directly access the memory.  */
    131 		num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
    132 	      else
    133 		{
    134 		  /* We avoid reading in all the section headers.  Just read
    135 		     the first one.  */
    136 		  Elf32_Shdr shdr_mem;
    137 
    138 		  if (unlikely (pread_retry (elf->fildes, &shdr_mem,
    139 					     sizeof (Elf32_Shdr), offset)
    140 				!= sizeof (Elf32_Shdr)))
    141 		    {
    142 		      /* We must be able to read this ELF section header.  */
    143 		      __libelf_seterrno (ELF_E_INVALID_FILE);
    144 		      result = -1;
    145 		      goto out;
    146 		    }
    147 
    148 		  if (elf->state.elf32.ehdr->e_ident[EI_DATA] != MY_ELFDATA)
    149 		    CONVERT (shdr_mem.sh_link);
    150 		  num = shdr_mem.sh_link;
    151 		}
    152 	    }
    153 	  else
    154 	    {
    155 	      if (elf->state.elf64.scns.data[0].shdr.e64 != NULL)
    156 		{
    157 		  num = elf->state.elf64.scns.data[0].shdr.e64->sh_link;
    158 		  goto success;
    159 		}
    160 
    161 	      size_t offset = elf->state.elf64.ehdr->e_shoff;
    162 
    163 	      if (elf->map_address != NULL
    164 		  && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
    165 		  && (ALLOW_UNALIGNED
    166 		      || (((size_t) ((char *) elf->map_address + offset))
    167 			  & (__alignof__ (Elf64_Shdr) - 1)) == 0))
    168 		/* We can directly access the memory.  */
    169 		num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
    170 	      else
    171 		{
    172 		  /* We avoid reading in all the section headers.  Just read
    173 		     the first one.  */
    174 		  Elf64_Shdr shdr_mem;
    175 
    176 		  if (unlikely (pread_retry (elf->fildes, &shdr_mem,
    177 					     sizeof (Elf64_Shdr), offset)
    178 				!= sizeof (Elf64_Shdr)))
    179 		    {
    180 		      /* We must be able to read this ELF section header.  */
    181 		      __libelf_seterrno (ELF_E_INVALID_FILE);
    182 		      result = -1;
    183 		      goto out;
    184 		    }
    185 
    186 		  if (elf->state.elf64.ehdr->e_ident[EI_DATA] != MY_ELFDATA)
    187 		    CONVERT (shdr_mem.sh_link);
    188 		  num = shdr_mem.sh_link;
    189 		}
    190 	    }
    191 	}
    192 
    193       /* Store the result.  */
    194     success:
    195       *dst = num;
    196     }
    197 
    198  out:
    199   rwlock_unlock (elf->lock);
    200 
    201   return result;
    202 }
    203 INTDEF(elf_getshdrstrndx)
    204 /* Alias for the deprecated name.  */
    205 #ifndef __APPLE__
    206 strong_alias (elf_getshdrstrndx, elf_getshstrndx)
    207 #endif
    208