Home | History | Annotate | Download | only in libelf
      1 /* Create descriptor for processing file.
      2    Copyright (C) 1998-2005, 2006, 2007, 2008 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 <ctype.h>
     57 #include <errno.h>
     58 #include <fcntl.h>
     59 #include <stdbool.h>
     60 #include <stddef.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 #include <sys/mman.h>
     64 #include <sys/param.h>
     65 #include <sys/stat.h>
     66 
     67 #include <system.h>
     68 #include "libelfP.h"
     69 #include "common.h"
     70 
     71 /* Create descriptor for archive in memory.  */
     72 static inline Elf *
     73 file_read_ar (int fildes, void *map_address, off_t offset, size_t maxsize,
     74 	      Elf_Cmd cmd, Elf *parent)
     75 {
     76   Elf *elf;
     77 
     78   /* Create a descriptor.  */
     79   elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
     80                       ELF_K_AR, 0);
     81   if (elf != NULL)
     82     {
     83       /* We don't read all the symbol tables in advance.  All this will
     84 	 happen on demand.  */
     85       elf->state.ar.offset = offset + SARMAG;
     86 
     87       elf->state.ar.elf_ar_hdr.ar_rawname = elf->state.ar.raw_name;
     88     }
     89 
     90   return elf;
     91 }
     92 
     93 
     94 static size_t
     95 get_shnum (void *map_address, unsigned char *e_ident, int fildes, off_t offset,
     96 	   size_t maxsize)
     97 {
     98   size_t result;
     99   union
    100   {
    101     Elf32_Ehdr *e32;
    102     Elf64_Ehdr *e64;
    103     void *p;
    104   } ehdr;
    105   union
    106   {
    107     Elf32_Ehdr e32;
    108     Elf64_Ehdr e64;
    109   } ehdr_mem;
    110   bool is32 = e_ident[EI_CLASS] == ELFCLASS32;
    111 
    112   /* Make the ELF header available.  */
    113   if (e_ident[EI_DATA] == MY_ELFDATA
    114       && (ALLOW_UNALIGNED
    115 	  || (((size_t) e_ident
    116 	       & ((is32 ? __alignof__ (Elf32_Ehdr) : __alignof__ (Elf64_Ehdr))
    117 		  - 1)) == 0)))
    118     ehdr.p = e_ident;
    119   else
    120     {
    121       /* We already read the ELF header.  We have to copy the header
    122 	 since we possibly modify the data here and the caller
    123 	 expects the memory it passes in to be preserved.  */
    124       ehdr.p = &ehdr_mem;
    125 
    126       if (is32)
    127 	{
    128 	  if (ALLOW_UNALIGNED)
    129 	    {
    130 	      ehdr_mem.e32.e_shnum = ((Elf32_Ehdr *) e_ident)->e_shnum;
    131 	      ehdr_mem.e32.e_shoff = ((Elf32_Ehdr *) e_ident)->e_shoff;
    132 	    }
    133 	  else
    134 	    memcpy (&ehdr_mem, e_ident, sizeof (Elf32_Ehdr));
    135 
    136 	  if (e_ident[EI_DATA] != MY_ELFDATA)
    137 	    {
    138 	      CONVERT (ehdr_mem.e32.e_shnum);
    139 	      CONVERT (ehdr_mem.e32.e_shoff);
    140 	    }
    141 	}
    142       else
    143 	{
    144 	  if (ALLOW_UNALIGNED)
    145 	    {
    146 	      ehdr_mem.e64.e_shnum = ((Elf64_Ehdr *) e_ident)->e_shnum;
    147 	      ehdr_mem.e64.e_shoff = ((Elf64_Ehdr *) e_ident)->e_shoff;
    148 	    }
    149 	  else
    150 	    memcpy (&ehdr_mem, e_ident, sizeof (Elf64_Ehdr));
    151 
    152 	  if (e_ident[EI_DATA] != MY_ELFDATA)
    153 	    {
    154 	      CONVERT (ehdr_mem.e64.e_shnum);
    155 	      CONVERT (ehdr_mem.e64.e_shoff);
    156 	    }
    157 	}
    158     }
    159 
    160   if (is32)
    161     {
    162       /* Get the number of sections from the ELF header.  */
    163       result = ehdr.e32->e_shnum;
    164 
    165       if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
    166 	{
    167 	  if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
    168 	    /* Cannot read the first section header.  */
    169 	    return 0;
    170 
    171 	  if (likely (map_address != NULL) && e_ident[EI_DATA] == MY_ELFDATA
    172 	      && (ALLOW_UNALIGNED
    173 		  || (((size_t) ((char *) map_address + offset))
    174 		      & (__alignof__ (Elf32_Ehdr) - 1)) == 0))
    175 	    /* We can directly access the memory.  */
    176 	    result = ((Elf32_Shdr *) ((char *) map_address + ehdr.e32->e_shoff
    177 				      + offset))->sh_size;
    178 	  else
    179 	    {
    180 	      Elf32_Word size;
    181 
    182 	      if (likely (map_address != NULL))
    183 		/* gcc will optimize the memcpy to a simple memory
    184 		   access while taking care of alignment issues.  */
    185 		memcpy (&size, &((Elf32_Shdr *) ((char *) map_address
    186 						 + ehdr.e32->e_shoff
    187 						 + offset))->sh_size,
    188 			sizeof (Elf32_Word));
    189 	      else
    190 		if (unlikely (pread_retry (fildes, &size, sizeof (Elf32_Word),
    191 					   offset + ehdr.e32->e_shoff
    192 					   + offsetof (Elf32_Shdr, sh_size))
    193 			      != sizeof (Elf32_Word)))
    194 		  return (size_t) -1l;
    195 
    196 	      if (e_ident[EI_DATA] != MY_ELFDATA)
    197 		CONVERT (size);
    198 
    199 	      result = size;
    200 	    }
    201 	}
    202 
    203       /* If the section headers were truncated, pretend none were there.  */
    204       if (ehdr.e32->e_shoff > maxsize
    205 	  || maxsize - ehdr.e32->e_shoff < sizeof (Elf32_Shdr) * result)
    206 	result = 0;
    207     }
    208   else
    209     {
    210       /* Get the number of sections from the ELF header.  */
    211       result = ehdr.e64->e_shnum;
    212 
    213       if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
    214 	{
    215 	  if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
    216 	    /* Cannot read the first section header.  */
    217 	    return 0;
    218 
    219 	  Elf64_Xword size;
    220 	  if (likely (map_address != NULL) && e_ident[EI_DATA] == MY_ELFDATA
    221 	      && (ALLOW_UNALIGNED
    222 		  || (((size_t) ((char *) map_address + offset))
    223 		      & (__alignof__ (Elf64_Ehdr) - 1)) == 0))
    224 	    /* We can directly access the memory.  */
    225 	    size = ((Elf64_Shdr *) ((char *) map_address + ehdr.e64->e_shoff
    226 				    + offset))->sh_size;
    227 	  else
    228 	    {
    229 	      if (likely (map_address != NULL))
    230 		/* gcc will optimize the memcpy to a simple memory
    231 		   access while taking care of alignment issues.  */
    232 		memcpy (&size, &((Elf64_Shdr *) ((char *) map_address
    233 						 + ehdr.e64->e_shoff
    234 						 + offset))->sh_size,
    235 			sizeof (Elf64_Xword));
    236 	      else
    237 		if (unlikely (pread_retry (fildes, &size, sizeof (Elf64_Word),
    238 					   offset + ehdr.e64->e_shoff
    239 					   + offsetof (Elf64_Shdr, sh_size))
    240 			      != sizeof (Elf64_Xword)))
    241 		  return (size_t) -1l;
    242 
    243 	      if (e_ident[EI_DATA] != MY_ELFDATA)
    244 		CONVERT (size);
    245 	    }
    246 
    247 	  if (size > ~((GElf_Word) 0))
    248 	    /* Invalid value, it is too large.  */
    249 	    return (size_t) -1l;
    250 
    251 	  result = size;
    252 	}
    253 
    254       /* If the section headers were truncated, pretend none were there.  */
    255       if (ehdr.e64->e_shoff > maxsize
    256 	  || maxsize - ehdr.e64->e_shoff < sizeof (Elf64_Shdr) * result)
    257 	result = 0;
    258     }
    259 
    260   return result;
    261 }
    262 
    263 
    264 /* Create descriptor for ELF file in memory.  */
    265 static Elf *
    266 file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
    267 	       off_t offset, size_t maxsize, Elf_Cmd cmd, Elf *parent)
    268 {
    269   /* Verify the binary is of the class we can handle.  */
    270   if (unlikely ((e_ident[EI_CLASS] != ELFCLASS32
    271 		 && e_ident[EI_CLASS] != ELFCLASS64)
    272 		/* We also can only handle two encodings.  */
    273 		|| (e_ident[EI_DATA] != ELFDATA2LSB
    274 		    && e_ident[EI_DATA] != ELFDATA2MSB)))
    275     {
    276       /* Cannot handle this.  */
    277       __libelf_seterrno (ELF_E_INVALID_FILE);
    278       return NULL;
    279     }
    280 
    281   /* Determine the number of sections.  */
    282   size_t scncnt = get_shnum (map_address, e_ident, fildes, offset, maxsize);
    283   if (scncnt == (size_t) -1l)
    284     /* Could not determine the number of sections.  */
    285     return NULL;
    286 
    287   /* We can now allocate the memory.  */
    288   Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
    289 			   ELF_K_ELF, scncnt * sizeof (Elf_Scn));
    290   if (elf == NULL)
    291     /* Not enough memory.  */
    292     return NULL;
    293 
    294   /* Some more or less arbitrary value.  */
    295   elf->state.elf.scnincr = 10;
    296 
    297   /* Make the class easily available.  */
    298   elf->class = e_ident[EI_CLASS];
    299 
    300   if (e_ident[EI_CLASS] == ELFCLASS32)
    301     {
    302       /* This pointer might not be directly usable if the alignment is
    303 	 not sufficient for the architecture.  */
    304       Elf32_Ehdr *ehdr = (Elf32_Ehdr *) ((char *) map_address + offset);
    305 
    306       assert ((unsigned int) scncnt == scncnt);
    307       elf->state.elf32.scns.cnt = elf->state.elf32.scns.max = scncnt;
    308 
    309       /* This is a 32-bit binary.  */
    310       if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
    311 	  && (ALLOW_UNALIGNED
    312 	      || ((((uintptr_t) ehdr) & (__alignof__ (Elf32_Ehdr) - 1)) == 0
    313 		  && ((uintptr_t) ((char *) ehdr + ehdr->e_shoff)
    314 		      & (__alignof__ (Elf32_Shdr) - 1)) == 0
    315 		  && ((uintptr_t) ((char *) ehdr + ehdr->e_phoff)
    316 		      & (__alignof__ (Elf32_Phdr) - 1)) == 0)))
    317 	{
    318 	  /* We can use the mmapped memory.  */
    319 	  elf->state.elf32.ehdr = ehdr;
    320 	  elf->state.elf32.shdr
    321 	    = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
    322 	  if (ehdr->e_phnum > 0)
    323 	    /* Assign a value only if there really is a program
    324 	       header.  Otherwise the value remains NULL.  */
    325 	    elf->state.elf32.phdr
    326 	      = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
    327 
    328 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
    329 	    {
    330 	      elf->state.elf32.scns.data[cnt].index = cnt;
    331 	      elf->state.elf32.scns.data[cnt].elf = elf;
    332 	      elf->state.elf32.scns.data[cnt].shdr.e32 =
    333 		&elf->state.elf32.shdr[cnt];
    334 	      elf->state.elf32.scns.data[cnt].rawdata_base =
    335 		elf->state.elf32.scns.data[cnt].data_base =
    336 		((char *) map_address + offset
    337 		 + elf->state.elf32.shdr[cnt].sh_offset);
    338 	      elf->state.elf32.scns.data[cnt].list = &elf->state.elf32.scns;
    339 
    340 	      /* If this is a section with an extended index add a
    341 		 reference in the section which uses the extended
    342 		 index.  */
    343 	      if (elf->state.elf32.shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
    344 		  && elf->state.elf32.shdr[cnt].sh_link < scncnt)
    345 		elf->state.elf32.scns.data[elf->state.elf32.shdr[cnt].sh_link].shndx_index
    346 		  = cnt;
    347 
    348 	      /* Set the own shndx_index field in case it has not yet
    349 		 been set.  */
    350 	      if (elf->state.elf32.scns.data[cnt].shndx_index == 0)
    351 		elf->state.elf32.scns.data[cnt].shndx_index = -1;
    352 	    }
    353 	}
    354       else
    355 	{
    356 	  /* Copy the ELF header.  */
    357 	  elf->state.elf32.ehdr = memcpy (&elf->state.elf32.ehdr_mem, e_ident,
    358 					  sizeof (Elf32_Ehdr));
    359 
    360 	  if (e_ident[EI_DATA] != MY_ELFDATA)
    361 	    {
    362 	      CONVERT (elf->state.elf32.ehdr_mem.e_type);
    363 	      CONVERT (elf->state.elf32.ehdr_mem.e_machine);
    364 	      CONVERT (elf->state.elf32.ehdr_mem.e_version);
    365 	      CONVERT (elf->state.elf32.ehdr_mem.e_entry);
    366 	      CONVERT (elf->state.elf32.ehdr_mem.e_phoff);
    367 	      CONVERT (elf->state.elf32.ehdr_mem.e_shoff);
    368 	      CONVERT (elf->state.elf32.ehdr_mem.e_flags);
    369 	      CONVERT (elf->state.elf32.ehdr_mem.e_ehsize);
    370 	      CONVERT (elf->state.elf32.ehdr_mem.e_phentsize);
    371 	      CONVERT (elf->state.elf32.ehdr_mem.e_phnum);
    372 	      CONVERT (elf->state.elf32.ehdr_mem.e_shentsize);
    373 	      CONVERT (elf->state.elf32.ehdr_mem.e_shnum);
    374 	      CONVERT (elf->state.elf32.ehdr_mem.e_shstrndx);
    375 	    }
    376 
    377 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
    378 	    {
    379 	      elf->state.elf32.scns.data[cnt].index = cnt;
    380 	      elf->state.elf32.scns.data[cnt].elf = elf;
    381 	      elf->state.elf32.scns.data[cnt].list = &elf->state.elf32.scns;
    382 	    }
    383 	}
    384 
    385       /* So far only one block with sections.  */
    386       elf->state.elf32.scns_last = &elf->state.elf32.scns;
    387     }
    388   else
    389     {
    390       /* This pointer might not be directly usable if the alignment is
    391 	 not sufficient for the architecture.  */
    392       Elf64_Ehdr *ehdr = (Elf64_Ehdr *) ((char *) map_address + offset);
    393 
    394       assert ((unsigned int) scncnt == scncnt);
    395       elf->state.elf64.scns.cnt = elf->state.elf64.scns.max = scncnt;
    396 
    397       /* This is a 64-bit binary.  */
    398       if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
    399 	  && (ALLOW_UNALIGNED
    400 	      || ((((uintptr_t) ehdr) & (__alignof__ (Elf64_Ehdr) - 1)) == 0
    401 		  && ((uintptr_t) ((char *) ehdr + ehdr->e_shoff)
    402 		      & (__alignof__ (Elf64_Shdr) - 1)) == 0
    403 		  && ((uintptr_t) ((char *) ehdr + ehdr->e_phoff)
    404 		      & (__alignof__ (Elf64_Phdr) - 1)) == 0)))
    405 	{
    406 	  /* We can use the mmapped memory.  */
    407 	  elf->state.elf64.ehdr = ehdr;
    408 	  elf->state.elf64.shdr
    409 	    = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
    410 	  if (ehdr->e_phnum > 0)
    411 	    /* Assign a value only if there really is a program
    412 	       header.  Otherwise the value remains NULL.  */
    413 	    elf->state.elf64.phdr
    414 	      = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
    415 
    416 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
    417 	    {
    418 	      elf->state.elf64.scns.data[cnt].index = cnt;
    419 	      elf->state.elf64.scns.data[cnt].elf = elf;
    420 	      elf->state.elf64.scns.data[cnt].shdr.e64 =
    421 		&elf->state.elf64.shdr[cnt];
    422 	      elf->state.elf64.scns.data[cnt].rawdata_base =
    423 		elf->state.elf64.scns.data[cnt].data_base =
    424 		((char *) map_address + offset
    425 		 + elf->state.elf64.shdr[cnt].sh_offset);
    426 	      elf->state.elf64.scns.data[cnt].list = &elf->state.elf64.scns;
    427 
    428 	      /* If this is a section with an extended index add a
    429 		 reference in the section which uses the extended
    430 		 index.  */
    431 	      if (elf->state.elf64.shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
    432 		  && elf->state.elf64.shdr[cnt].sh_link < scncnt)
    433 		elf->state.elf64.scns.data[elf->state.elf64.shdr[cnt].sh_link].shndx_index
    434 		  = cnt;
    435 
    436 	      /* Set the own shndx_index field in case it has not yet
    437 		 been set.  */
    438 	      if (elf->state.elf64.scns.data[cnt].shndx_index == 0)
    439 		elf->state.elf64.scns.data[cnt].shndx_index = -1;
    440 	    }
    441 	}
    442       else
    443 	{
    444 	  /* Copy the ELF header.  */
    445 	  elf->state.elf64.ehdr = memcpy (&elf->state.elf64.ehdr_mem, e_ident,
    446 					  sizeof (Elf64_Ehdr));
    447 
    448 	  if (e_ident[EI_DATA] != MY_ELFDATA)
    449 	    {
    450 	      CONVERT (elf->state.elf64.ehdr_mem.e_type);
    451 	      CONVERT (elf->state.elf64.ehdr_mem.e_machine);
    452 	      CONVERT (elf->state.elf64.ehdr_mem.e_version);
    453 	      CONVERT (elf->state.elf64.ehdr_mem.e_entry);
    454 	      CONVERT (elf->state.elf64.ehdr_mem.e_phoff);
    455 	      CONVERT (elf->state.elf64.ehdr_mem.e_shoff);
    456 	      CONVERT (elf->state.elf64.ehdr_mem.e_flags);
    457 	      CONVERT (elf->state.elf64.ehdr_mem.e_ehsize);
    458 	      CONVERT (elf->state.elf64.ehdr_mem.e_phentsize);
    459 	      CONVERT (elf->state.elf64.ehdr_mem.e_phnum);
    460 	      CONVERT (elf->state.elf64.ehdr_mem.e_shentsize);
    461 	      CONVERT (elf->state.elf64.ehdr_mem.e_shnum);
    462 	      CONVERT (elf->state.elf64.ehdr_mem.e_shstrndx);
    463 	    }
    464 
    465 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
    466 	    {
    467 	      elf->state.elf64.scns.data[cnt].index = cnt;
    468 	      elf->state.elf64.scns.data[cnt].elf = elf;
    469 	      elf->state.elf64.scns.data[cnt].list = &elf->state.elf64.scns;
    470 	    }
    471 	}
    472 
    473       /* So far only one block with sections.  */
    474       elf->state.elf64.scns_last = &elf->state.elf64.scns;
    475     }
    476 
    477   return elf;
    478 }
    479 
    480 
    481 Elf *
    482 internal_function
    483 __libelf_read_mmaped_file (int fildes, void *map_address,  off_t offset,
    484 			   size_t maxsize, Elf_Cmd cmd, Elf *parent)
    485 {
    486   /* We have to find out what kind of file this is.  We handle ELF
    487      files and archives.  To find out what we have we must look at the
    488      header.  The header for an ELF file is EI_NIDENT bytes in size,
    489      the header for an archive file SARMAG bytes long.  */
    490   unsigned char *e_ident = (unsigned char *) map_address + offset;
    491 
    492   /* See what kind of object we have here.  */
    493   Elf_Kind kind = determine_kind (e_ident, maxsize);
    494 
    495   switch (kind)
    496     {
    497     case ELF_K_ELF:
    498       return file_read_elf (fildes, map_address, e_ident, offset, maxsize,
    499 			    cmd, parent);
    500 
    501     case ELF_K_AR:
    502       return file_read_ar (fildes, map_address, offset, maxsize, cmd, parent);
    503 
    504     default:
    505       break;
    506     }
    507 
    508   /* This case is easy.  Since we cannot do anything with this file
    509      create a dummy descriptor.  */
    510   return allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
    511 		       ELF_K_NONE, 0);
    512 }
    513 
    514 
    515 static Elf *
    516 read_unmmaped_file (int fildes, off_t offset, size_t maxsize, Elf_Cmd cmd,
    517 		    Elf *parent)
    518 {
    519   /* We have to find out what kind of file this is.  We handle ELF
    520      files and archives.  To find out what we have we must read the
    521      header.  The identification header for an ELF file is EI_NIDENT
    522      bytes in size, but we read the whole ELF header since we will
    523      need it anyway later.  For archives the header in SARMAG bytes
    524      long.  Read the maximum of these numbers.
    525 
    526      XXX We have to change this for the extended `ar' format some day.
    527 
    528      Use a union to ensure alignment.  We might later access the
    529      memory as a ElfXX_Ehdr.  */
    530   union
    531   {
    532     Elf64_Ehdr ehdr;
    533     unsigned char header[MAX (sizeof (Elf64_Ehdr), SARMAG)];
    534   } mem;
    535 
    536   /* Read the head of the file.  */
    537   ssize_t nread = pread_retry (fildes, mem.header,
    538 			       MIN (MAX (sizeof (Elf64_Ehdr), SARMAG),
    539 				    maxsize),
    540 			       offset);
    541   if (unlikely (nread == -1))
    542     /* We cannot even read the head of the file.  Maybe FILDES is associated
    543        with an unseekable device.  This is nothing we can handle.  */
    544     return NULL;
    545 
    546   /* See what kind of object we have here.  */
    547   Elf_Kind kind = determine_kind (mem.header, nread);
    548 
    549   switch (kind)
    550     {
    551     case ELF_K_AR:
    552       return file_read_ar (fildes, NULL, offset, maxsize, cmd, parent);
    553 
    554     case ELF_K_ELF:
    555       /* Make sure at least the ELF header is contained in the file.  */
    556       if ((size_t) nread >= (mem.header[EI_CLASS] == ELFCLASS32
    557 			     ? sizeof (Elf32_Ehdr) : sizeof (Elf64_Ehdr)))
    558 	return file_read_elf (fildes, NULL, mem.header, offset, maxsize, cmd,
    559 			      parent);
    560       /* FALLTHROUGH */
    561 
    562     default:
    563       break;
    564     }
    565 
    566   /* This case is easy.  Since we cannot do anything with this file
    567      create a dummy descriptor.  */
    568   return allocate_elf (fildes, NULL, offset, maxsize, cmd, parent,
    569 		       ELF_K_NONE, 0);
    570 }
    571 
    572 
    573 /* Open a file for reading.  If possible we will try to mmap() the file.  */
    574 static struct Elf *
    575 read_file (int fildes, off_t offset, size_t maxsize,
    576 	   Elf_Cmd cmd, Elf *parent)
    577 {
    578   void *map_address = NULL;
    579   int use_mmap = (cmd == ELF_C_READ_MMAP || cmd == ELF_C_RDWR_MMAP
    580 		  || cmd == ELF_C_WRITE_MMAP
    581 		  || cmd == ELF_C_READ_MMAP_PRIVATE);
    582 
    583 #if _MUDFLAP
    584   /* Mudflap doesn't grok that our mmap'd data is ok.  */
    585   use_mmap = 0;
    586 #endif
    587 
    588   if (use_mmap)
    589     {
    590       if (parent == NULL)
    591 	{
    592 	  if (maxsize == ~((size_t) 0))
    593 	    {
    594 	      /* We don't know in the moment how large the file is.
    595 		 Determine it now.  */
    596 	      struct stat st;
    597 
    598 	      if (fstat (fildes, &st) == 0
    599 		  && (sizeof (size_t) >= sizeof (st.st_size)
    600 		      || st.st_size <= ~((size_t) 0)))
    601 		maxsize = (size_t) st.st_size;
    602 	    }
    603 
    604 	  /* We try to map the file ourself.  */
    605 	  map_address = mmap (NULL, maxsize, (cmd == ELF_C_READ_MMAP
    606 					      ? PROT_READ
    607 					      : PROT_READ|PROT_WRITE),
    608 			      cmd == ELF_C_READ_MMAP_PRIVATE
    609 			      || cmd == ELF_C_READ_MMAP
    610 			      ? MAP_PRIVATE : MAP_SHARED,
    611 			      fildes, offset);
    612 
    613 	  if (map_address == MAP_FAILED)
    614 	    map_address = NULL;
    615 	}
    616       else
    617 	{
    618 	  /* The parent is already loaded.  Use it.  */
    619 	  assert (maxsize != ~((size_t) 0));
    620 
    621 	  map_address = parent->map_address;
    622 	}
    623     }
    624 
    625   /* If we have the file in memory optimize the access.  */
    626   if (map_address != NULL)
    627     {
    628       assert (map_address != MAP_FAILED);
    629 
    630       struct Elf *result = __libelf_read_mmaped_file (fildes, map_address,
    631 						      offset, maxsize, cmd,
    632 						      parent);
    633 
    634       /* If something went wrong during the initialization unmap the
    635 	 memory if we mmaped here.  */
    636       if (result == NULL
    637 	  && (parent == NULL
    638 	      || parent->map_address != map_address))
    639 	munmap (map_address, maxsize);
    640       else if (parent == NULL)
    641 	/* Remember that we mmap()ed the memory.  */
    642 	result->flags |= ELF_F_MMAPPED;
    643 
    644       return result;
    645     }
    646 
    647   /* Otherwise we have to do it the hard way.  We read as much as necessary
    648      from the file whenever we need information which is not available.  */
    649   return read_unmmaped_file (fildes, offset, maxsize, cmd, parent);
    650 }
    651 
    652 
    653 /* Find the entry with the long names for the content of this archive.  */
    654 static const char *
    655 read_long_names (Elf *elf)
    656 {
    657   off_t offset = SARMAG;	/* This is the first entry.  */
    658   struct ar_hdr hdrm;
    659   struct ar_hdr *hdr;
    660   char *newp;
    661   size_t len;
    662 
    663   while (1)
    664     {
    665       if (elf->map_address != NULL)
    666 	{
    667 	  if (offset + sizeof (struct ar_hdr) > elf->maximum_size)
    668 	    return NULL;
    669 
    670 	  /* The data is mapped.  */
    671 	  hdr = (struct ar_hdr *) (elf->map_address + offset);
    672 	}
    673       else
    674 	{
    675 	  /* Read the header from the file.  */
    676 	  if (unlikely (pread_retry (elf->fildes, &hdrm, sizeof (hdrm),
    677 				     elf->start_offset + offset)
    678 			!= sizeof (hdrm)))
    679 	    return NULL;
    680 
    681 	  hdr = &hdrm;
    682 	}
    683 
    684       len = atol (hdr->ar_size);
    685 
    686       if (memcmp (hdr->ar_name, "//              ", 16) == 0)
    687 	break;
    688 
    689       offset += sizeof (struct ar_hdr) + ((len + 1) & ~1l);
    690     }
    691 
    692   /* Due to the stupid format of the long name table entry (which are not
    693      NUL terminted) we have to provide an appropriate representation anyhow.
    694      Therefore we always make a copy which has the appropriate form.  */
    695   newp = (char *) malloc (len);
    696   if (newp != NULL)
    697     {
    698       char *runp;
    699 
    700       if (elf->map_address != NULL)
    701 	/* Simply copy it over.  */
    702 	elf->state.ar.long_names = (char *) memcpy (newp,
    703 						    elf->map_address + offset
    704 						    + sizeof (struct ar_hdr),
    705 						    len);
    706       else
    707 	{
    708 	  if (unlikely ((size_t) pread_retry (elf->fildes, newp, len,
    709 					      elf->start_offset + offset
    710 					      + sizeof (struct ar_hdr))
    711 			!= len))
    712 	    {
    713 	      /* We were not able to read all data.  */
    714 	      free (newp);
    715 	      elf->state.ar.long_names = NULL;
    716 	      return NULL;
    717 	    }
    718 	  elf->state.ar.long_names = newp;
    719 	}
    720 
    721       elf->state.ar.long_names_len = len;
    722 
    723       /* Now NUL-terminate the strings.  */
    724       runp = newp;
    725       while (1)
    726         {
    727 	  runp = (char *) memchr (runp, '/', newp + len - runp);
    728 	  if (runp == NULL)
    729 	    /* This was the last entry.  */
    730 	    break;
    731 
    732 	  /* NUL-terminate the string.  */
    733 	  *runp = '\0';
    734 
    735 	  /* Skip the NUL byte and the \012.  */
    736 	  runp += 2;
    737 
    738 	  /* A sanity check.  Somebody might have generated invalid
    739 	     archive.  */
    740 	  if (runp >= newp + len)
    741 	    break;
    742 	}
    743     }
    744 
    745   return newp;
    746 }
    747 
    748 
    749 /* Read the next archive header.  */
    750 int
    751 internal_function
    752 __libelf_next_arhdr_wrlock (elf)
    753      Elf *elf;
    754 {
    755   struct ar_hdr *ar_hdr;
    756   Elf_Arhdr *elf_ar_hdr;
    757 
    758   if (elf->map_address != NULL)
    759     {
    760       /* See whether this entry is in the file.  */
    761       if (unlikely (elf->state.ar.offset + sizeof (struct ar_hdr)
    762 		    > elf->start_offset + elf->maximum_size))
    763 	{
    764 	  /* This record is not anymore in the file.  */
    765 	  __libelf_seterrno (ELF_E_RANGE);
    766 	  return -1;
    767 	}
    768       ar_hdr = (struct ar_hdr *) (elf->map_address + elf->state.ar.offset);
    769     }
    770   else
    771     {
    772       ar_hdr = &elf->state.ar.ar_hdr;
    773 
    774       if (unlikely (pread_retry (elf->fildes, ar_hdr, sizeof (struct ar_hdr),
    775 				 elf->state.ar.offset)
    776 		    != sizeof (struct ar_hdr)))
    777 	{
    778 	  /* Something went wrong while reading the file.  */
    779 	  __libelf_seterrno (ELF_E_RANGE);
    780 	  return -1;
    781 	}
    782     }
    783 
    784   /* One little consistency check.  */
    785   if (unlikely (memcmp (ar_hdr->ar_fmag, ARFMAG, 2) != 0))
    786     {
    787       /* This is no valid archive.  */
    788       __libelf_seterrno (ELF_E_ARCHIVE_FMAG);
    789       return -1;
    790     }
    791 
    792   /* Copy the raw name over to a NUL terminated buffer.  */
    793   *((char *) __mempcpy (elf->state.ar.raw_name, ar_hdr->ar_name, 16)) = '\0';
    794 
    795   elf_ar_hdr = &elf->state.ar.elf_ar_hdr;
    796 
    797   /* Now convert the `struct ar_hdr' into `Elf_Arhdr'.
    798      Determine whether this is a special entry.  */
    799   if (ar_hdr->ar_name[0] == '/')
    800     {
    801       if (ar_hdr->ar_name[1] == ' '
    802 	  && memcmp (ar_hdr->ar_name, "/               ", 16) == 0)
    803 	/* This is the index.  */
    804 	elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "/", 2);
    805       else if (ar_hdr->ar_name[1] == '/'
    806 	       && memcmp (ar_hdr->ar_name, "//              ", 16) == 0)
    807 	/* This is the array with the long names.  */
    808 	elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "//", 3);
    809       else if (likely  (isdigit (ar_hdr->ar_name[1])))
    810 	{
    811 	  size_t offset;
    812 
    813 	  /* This is a long name.  First we have to read the long name
    814 	     table, if this hasn't happened already.  */
    815 	  if (unlikely (elf->state.ar.long_names == NULL
    816 			&& read_long_names (elf) == NULL))
    817 	    {
    818 	      /* No long name table although it is reference.  The archive is
    819 		 broken.  */
    820 	      __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
    821 	      return -1;
    822 	    }
    823 
    824 	  offset = atol (ar_hdr->ar_name + 1);
    825 	  if (unlikely (offset >= elf->state.ar.long_names_len))
    826 	    {
    827 	      /* The index in the long name table is larger than the table.  */
    828 	      __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
    829 	      return -1;
    830 	    }
    831 	  elf_ar_hdr->ar_name = elf->state.ar.long_names + offset;
    832 	}
    833       else
    834 	{
    835 	  /* This is none of the known special entries.  */
    836 	  __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
    837 	  return -1;
    838 	}
    839     }
    840   else
    841     {
    842       char *endp;
    843 
    844       /* It is a normal entry.  Copy over the name.  */
    845       endp = (char *) memccpy (elf->state.ar.ar_name, ar_hdr->ar_name,
    846 			       '/', 16);
    847       if (endp != NULL)
    848 	endp[-1] = '\0';
    849       else
    850 	{
    851 	  /* In the old BSD style of archive, there is no / terminator.
    852 	     Instead, there is space padding at the end of the name.  */
    853 	  size_t i = 15;
    854 	  do
    855 	    elf->state.ar.ar_name[i] = '\0';
    856 	  while (i > 0 && elf->state.ar.ar_name[--i] == ' ');
    857 	}
    858 
    859       elf_ar_hdr->ar_name = elf->state.ar.ar_name;
    860     }
    861 
    862   if (unlikely (ar_hdr->ar_size[0] == ' '))
    863     /* Something is really wrong.  We cannot live without a size for
    864        the member since it will not be possible to find the next
    865        archive member.  */
    866     {
    867       __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
    868       return -1;
    869     }
    870 
    871   /* Since there are no specialized functions to convert ASCII to
    872      time_t, uid_t, gid_t, mode_t, and off_t we use either atol or
    873      atoll depending on the size of the types.  We are also prepared
    874      for the case where the whole field in the `struct ar_hdr' is
    875      filled in which case we cannot simply use atol/l but instead have
    876      to create a temporary copy.  */
    877 
    878 #define INT_FIELD(FIELD)						      \
    879   do									      \
    880     {									      \
    881       char buf[sizeof (ar_hdr->FIELD) + 1];				      \
    882       const char *string = ar_hdr->FIELD;				      \
    883       if (ar_hdr->FIELD[sizeof (ar_hdr->FIELD) - 1] != ' ')		      \
    884 	{								      \
    885 	  *((char *) __mempcpy (buf, ar_hdr->FIELD, sizeof (ar_hdr->FIELD)))  \
    886 	    = '\0';							      \
    887 	  string = buf;							      \
    888 	}								      \
    889       if (sizeof (elf_ar_hdr->FIELD) <= sizeof (long int))		      \
    890 	elf_ar_hdr->FIELD = (__typeof (elf_ar_hdr->FIELD)) atol (string);     \
    891       else								      \
    892 	elf_ar_hdr->FIELD = (__typeof (elf_ar_hdr->FIELD)) atoll (string);    \
    893     }									      \
    894   while (0)
    895 
    896   INT_FIELD (ar_date);
    897   INT_FIELD (ar_uid);
    898   INT_FIELD (ar_gid);
    899   INT_FIELD (ar_mode);
    900   INT_FIELD (ar_size);
    901 
    902   return 0;
    903 }
    904 
    905 
    906 /* We were asked to return a clone of an existing descriptor.  This
    907    function must be called with the lock on the parent descriptor
    908    being held. */
    909 static Elf *
    910 dup_elf (int fildes, Elf_Cmd cmd, Elf *ref)
    911 {
    912   struct Elf *result;
    913 
    914   if (fildes == -1)
    915     /* Allow the user to pass -1 as the file descriptor for the new file.  */
    916     fildes = ref->fildes;
    917   /* The file descriptor better should be the same.  If it was disconnected
    918      already (using `elf_cntl') we do not test it.  */
    919   else if (unlikely (ref->fildes != -1 && fildes != ref->fildes))
    920     {
    921       __libelf_seterrno (ELF_E_FD_MISMATCH);
    922       return NULL;
    923     }
    924 
    925   /* The mode must allow reading.  I.e., a descriptor creating with a
    926      command different then ELF_C_READ, ELF_C_WRITE and ELF_C_RDWR is
    927      not allowed.  */
    928   if (unlikely (ref->cmd != ELF_C_READ && ref->cmd != ELF_C_READ_MMAP
    929 		&& ref->cmd != ELF_C_WRITE && ref->cmd != ELF_C_WRITE_MMAP
    930 		&& ref->cmd != ELF_C_RDWR && ref->cmd != ELF_C_RDWR_MMAP
    931 		&& ref->cmd != ELF_C_READ_MMAP_PRIVATE))
    932     {
    933       __libelf_seterrno (ELF_E_INVALID_OP);
    934       return NULL;
    935     }
    936 
    937   /* Now it is time to distinguish between reading normal files and
    938      archives.  Normal files can easily be handled be incrementing the
    939      reference counter and return the same descriptor.  */
    940   if (ref->kind != ELF_K_AR)
    941     {
    942       ++ref->ref_count;
    943       return ref;
    944     }
    945 
    946   /* This is an archive.  We must create a descriptor for the archive
    947      member the internal pointer of the archive file desriptor is
    948      pointing to.  First read the header of the next member if this
    949      has not happened already.  */
    950   if (ref->state.ar.elf_ar_hdr.ar_name == NULL
    951       && __libelf_next_arhdr_wrlock (ref) != 0)
    952     /* Something went wrong.  Maybe there is no member left.  */
    953     return NULL;
    954 
    955   /* We have all the information we need about the next archive member.
    956      Now create a descriptor for it.  */
    957   result = read_file (fildes, ref->state.ar.offset + sizeof (struct ar_hdr),
    958 		      ref->state.ar.elf_ar_hdr.ar_size, cmd, ref);
    959 
    960   /* Enlist this new descriptor in the list of children.  */
    961   if (result != NULL)
    962     {
    963       result->next = ref->state.ar.children;
    964       ref->state.ar.children = result;
    965     }
    966 
    967   return result;
    968 }
    969 
    970 
    971 /* Return desriptor for empty file ready for writing.  */
    972 static struct Elf *
    973 write_file (int fd, Elf_Cmd cmd)
    974 {
    975   /* We simply create an empty `Elf' structure.  */
    976 #define NSCNSALLOC	10
    977   Elf *result = allocate_elf (fd, NULL, 0, 0, cmd, NULL, ELF_K_ELF,
    978 			      NSCNSALLOC * sizeof (Elf_Scn));
    979 
    980   if (result != NULL)
    981     {
    982       /* We have to write to the file in any case.  */
    983       result->flags = ELF_F_DIRTY;
    984 
    985       /* Some more or less arbitrary value.  */
    986       result->state.elf.scnincr = NSCNSALLOC;
    987 
    988       /* We have allocated room for some sections.  */
    989       assert (offsetof (struct Elf, state.elf32.scns)
    990 	      == offsetof (struct Elf, state.elf64.scns));
    991       result->state.elf.scns_last = &result->state.elf32.scns;
    992       result->state.elf32.scns.max = NSCNSALLOC;
    993     }
    994 
    995   return result;
    996 }
    997 
    998 
    999 /* Return a descriptor for the file belonging to FILDES.  */
   1000 Elf *
   1001 elf_begin (fildes, cmd, ref)
   1002      int fildes;
   1003      Elf_Cmd cmd;
   1004      Elf *ref;
   1005 {
   1006   Elf *retval;
   1007 
   1008   if (unlikely (! __libelf_version_initialized))
   1009     {
   1010       /* Version wasn't set so far.  */
   1011       __libelf_seterrno (ELF_E_NO_VERSION);
   1012       return NULL;
   1013     }
   1014 
   1015   if (ref != NULL)
   1016     /* Make sure the descriptor is not suddenly going away.  */
   1017     rwlock_rdlock (ref->lock);
   1018   else if (unlikely (fcntl (fildes, F_GETFL) == -1 && errno == EBADF))
   1019     {
   1020       /* We cannot do anything productive without a file descriptor.  */
   1021       __libelf_seterrno (ELF_E_INVALID_FILE);
   1022       return NULL;
   1023     }
   1024 
   1025   Elf *lock_dup_elf ()
   1026   {
   1027     /* We need wrlock to dup an archive.  */
   1028     if (ref->kind == ELF_K_AR)
   1029       {
   1030 	rwlock_unlock (ref->lock);
   1031 	rwlock_wrlock (ref->lock);
   1032       }
   1033 
   1034     /* Duplicate the descriptor.  */
   1035     return dup_elf (fildes, cmd, ref);
   1036   }
   1037 
   1038   switch (cmd)
   1039     {
   1040     case ELF_C_NULL:
   1041       /* We simply return a NULL pointer.  */
   1042       retval = NULL;
   1043       break;
   1044 
   1045     case ELF_C_READ_MMAP_PRIVATE:
   1046       /* If we have a reference it must also be opened this way.  */
   1047       if (unlikely (ref != NULL && ref->cmd != ELF_C_READ_MMAP_PRIVATE))
   1048 	{
   1049 	  __libelf_seterrno (ELF_E_INVALID_CMD);
   1050 	  retval = NULL;
   1051 	  break;
   1052 	}
   1053       /* FALLTHROUGH */
   1054 
   1055     case ELF_C_READ:
   1056     case ELF_C_READ_MMAP:
   1057       if (ref != NULL)
   1058 	retval = lock_dup_elf ();
   1059       else
   1060 	/* Create descriptor for existing file.  */
   1061 	retval = read_file (fildes, 0, ~((size_t) 0), cmd, NULL);
   1062       break;
   1063 
   1064     case ELF_C_RDWR:
   1065     case ELF_C_RDWR_MMAP:
   1066       /* If we have a REF object it must also be opened using this
   1067 	 command.  */
   1068       if (ref != NULL)
   1069 	{
   1070 	  if (unlikely (ref->cmd != ELF_C_RDWR && ref->cmd != ELF_C_RDWR_MMAP
   1071 			&& ref->cmd != ELF_C_WRITE
   1072 			&& ref->cmd != ELF_C_WRITE_MMAP))
   1073 	    {
   1074 	      /* This is not ok.  REF must also be opened for writing.  */
   1075 	      __libelf_seterrno (ELF_E_INVALID_CMD);
   1076 	      retval = NULL;
   1077 	    }
   1078 	  else
   1079 	    retval = lock_dup_elf ();
   1080 	}
   1081       else
   1082 	/* Create descriptor for existing file.  */
   1083 	retval = read_file (fildes, 0, ~((size_t) 0), cmd, NULL);
   1084       break;
   1085 
   1086     case ELF_C_WRITE:
   1087     case ELF_C_WRITE_MMAP:
   1088       /* We ignore REF and prepare a descriptor to write a new file.  */
   1089       retval = write_file (fildes, cmd);
   1090       break;
   1091 
   1092     default:
   1093       __libelf_seterrno (ELF_E_INVALID_CMD);
   1094       retval = NULL;
   1095       break;
   1096     }
   1097 
   1098   /* Release the lock.  */
   1099   if (ref != NULL)
   1100     rwlock_unlock (ref->lock);
   1101 
   1102   return retval;
   1103 }
   1104 INTDEF(elf_begin)
   1105