Home | History | Annotate | Download | only in libelf
      1 /* Return section header.
      2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2007, 2009 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 1998.
      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 <stdbool.h>
     58 #include <unistd.h>
     59 
     60 #include <system.h>
     61 #include "libelfP.h"
     62 #include "common.h"
     63 
     64 #ifndef LIBELFBITS
     65 # define LIBELFBITS 32
     66 #endif
     67 
     68 
     69 static ElfW2(LIBELFBITS,Shdr) *
     70 load_shdr_wrlock (Elf_Scn *scn)
     71 {
     72   ElfW2(LIBELFBITS,Shdr) *result;
     73 
     74   /* Read the section header table.  */
     75   Elf *elf = scn->elf;
     76   ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
     77 
     78   /* Try again, maybe the data is there now.  */
     79   result = scn->shdr.ELFW(e,LIBELFBITS);
     80   if (result != NULL)
     81     goto out;
     82 
     83   size_t shnum;
     84   if (__elf_getshdrnum_rdlock (elf, &shnum) != 0)
     85     goto out;
     86   size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
     87 
     88   /* Allocate memory for the section headers.  We know the number
     89      of entries from the ELF header.  */
     90   ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
     91     (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
     92   if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
     93     {
     94       __libelf_seterrno (ELF_E_NOMEM);
     95       goto out;
     96     }
     97   elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
     98 
     99   if (elf->map_address != NULL)
    100     {
    101       ElfW2(LIBELFBITS,Shdr) *notcvt;
    102 
    103       /* All the data is already mapped.  If we could use it
    104 	 directly this would already have happened.  */
    105       void *file_shdr = ((char *) elf->map_address
    106 			 + elf->start_offset + ehdr->e_shoff);
    107 
    108       assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
    109 	      || (! ALLOW_UNALIGNED
    110 		  && ((uintptr_t) file_shdr
    111 		      & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
    112 
    113       /* Now copy the data and at the same time convert the byte order.  */
    114       if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
    115 	{
    116 	  assert (! ALLOW_UNALIGNED);
    117 	  memcpy (shdr, file_shdr, size);
    118 	}
    119       else
    120 	{
    121 	  if (ALLOW_UNALIGNED
    122 	      || ((uintptr_t) file_shdr
    123 		  & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) == 0)
    124 	    notcvt = (ElfW2(LIBELFBITS,Shdr) *)
    125 	      ((char *) elf->map_address
    126 	       + elf->start_offset + ehdr->e_shoff);
    127 	  else
    128 	    {
    129 	      notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
    130 	      memcpy (notcvt, ((char *) elf->map_address
    131 			       + elf->start_offset + ehdr->e_shoff),
    132 		      size);
    133 	    }
    134 
    135 	  for (size_t cnt = 0; cnt < shnum; ++cnt)
    136 	    {
    137 	      CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
    138 	      CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
    139 	      CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
    140 	      CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
    141 	      CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
    142 	      CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
    143 	      CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
    144 	      CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
    145 	      CONVERT_TO (shdr[cnt].sh_addralign,
    146 			  notcvt[cnt].sh_addralign);
    147 	      CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
    148 
    149 	      /* If this is a section with an extended index add a
    150 		 reference in the section which uses the extended
    151 		 index.  */
    152 	      if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
    153 		  && shdr[cnt].sh_link < shnum)
    154 		elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
    155 		  = cnt;
    156 
    157 	      /* Set the own shndx_index field in case it has not yet
    158 		 been set.  */
    159 	      if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
    160 		elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
    161 		  = -1;
    162 	    }
    163 	}
    164     }
    165   else if (likely (elf->fildes != -1))
    166     {
    167       /* Read the header.  */
    168       ssize_t n = pread_retry (elf->fildes,
    169 			       elf->state.ELFW(elf,LIBELFBITS).shdr, size,
    170 			       elf->start_offset + ehdr->e_shoff);
    171       if (unlikely ((size_t) n != size))
    172 	{
    173 	  /* Severe problems.  We cannot read the data.  */
    174 	  __libelf_seterrno (ELF_E_READ_ERROR);
    175 	  goto free_and_out;
    176 	}
    177 
    178       /* If the byte order of the file is not the same as the one
    179 	 of the host convert the data now.  */
    180       if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
    181 	for (size_t cnt = 0; cnt < shnum; ++cnt)
    182 	  {
    183 	    CONVERT (shdr[cnt].sh_name);
    184 	    CONVERT (shdr[cnt].sh_type);
    185 	    CONVERT (shdr[cnt].sh_flags);
    186 	    CONVERT (shdr[cnt].sh_addr);
    187 	    CONVERT (shdr[cnt].sh_offset);
    188 	    CONVERT (shdr[cnt].sh_size);
    189 	    CONVERT (shdr[cnt].sh_link);
    190 	    CONVERT (shdr[cnt].sh_info);
    191 	    CONVERT (shdr[cnt].sh_addralign);
    192 	    CONVERT (shdr[cnt].sh_entsize);
    193 	  }
    194     }
    195   else
    196     {
    197       /* The file descriptor was already enabled and not all data was
    198 	 read.  Undo the allocation.  */
    199       __libelf_seterrno (ELF_E_FD_DISABLED);
    200 
    201     free_and_out:
    202       free (shdr);
    203       elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
    204       elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
    205 
    206       goto out;
    207     }
    208 
    209   /* Set the pointers in the `scn's.  */
    210   for (size_t cnt = 0; cnt < shnum; ++cnt)
    211     elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
    212       = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
    213 
    214   result = scn->shdr.ELFW(e,LIBELFBITS);
    215   assert (result != NULL);
    216 
    217 out:
    218   return result;
    219 }
    220 
    221 static bool
    222 scn_valid (Elf_Scn *scn)
    223 {
    224   if (scn == NULL)
    225     return false;
    226 
    227   if (unlikely (scn->elf->state.elf.ehdr == NULL))
    228     {
    229       __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
    230       return false;
    231     }
    232 
    233   if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
    234     {
    235       __libelf_seterrno (ELF_E_INVALID_CLASS);
    236       return false;
    237     }
    238 
    239   return true;
    240 }
    241 
    242 ElfW2(LIBELFBITS,Shdr) *
    243 __elfw2(LIBELFBITS,getshdr_rdlock) (scn)
    244      Elf_Scn *scn;
    245 {
    246   ElfW2(LIBELFBITS,Shdr) *result;
    247 
    248   if (!scn_valid (scn))
    249     return NULL;
    250 
    251   result = scn->shdr.ELFW(e,LIBELFBITS);
    252   if (result == NULL)
    253     {
    254       rwlock_unlock (scn->elf->lock);
    255       rwlock_wrlock (scn->elf->lock);
    256       result = scn->shdr.ELFW(e,LIBELFBITS);
    257       if (result == NULL)
    258 	result = load_shdr_wrlock (scn);
    259     }
    260 
    261   return result;
    262 }
    263 
    264 ElfW2(LIBELFBITS,Shdr) *
    265 __elfw2(LIBELFBITS,getshdr_wrlock) (scn)
    266      Elf_Scn *scn;
    267 {
    268   ElfW2(LIBELFBITS,Shdr) *result;
    269 
    270   if (!scn_valid (scn))
    271     return NULL;
    272 
    273   result = scn->shdr.ELFW(e,LIBELFBITS);
    274   if (result == NULL)
    275     result = load_shdr_wrlock (scn);
    276 
    277   return result;
    278 }
    279 
    280 ElfW2(LIBELFBITS,Shdr) *
    281 elfw2(LIBELFBITS,getshdr) (scn)
    282      Elf_Scn *scn;
    283 {
    284   ElfW2(LIBELFBITS,Shdr) *result;
    285 
    286   if (!scn_valid (scn))
    287     return NULL;
    288 
    289   rwlock_rdlock (scn->elf->lock);
    290   result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
    291   rwlock_unlock (scn->elf->lock);
    292 
    293   return result;
    294 }
    295