Home | History | Annotate | Download | only in libdwfl
      1 /* Decompression support for libdwfl: zlib (gzip) and/or bzlib (bzip2).
      2    Copyright (C) 2009 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4 
      5    Red Hat elfutils is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by the
      7    Free Software Foundation; version 2 of the License.
      8 
      9    Red Hat elfutils is distributed in the hope that it will be useful, but
     10    WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12    General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License along
     15    with Red Hat elfutils; if not, write to the Free Software Foundation,
     16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     17 
     18    In addition, as a special exception, Red Hat, Inc. gives You the
     19    additional right to link the code of Red Hat elfutils with code licensed
     20    under any Open Source Initiative certified open source license
     21    (http://www.opensource.org/licenses/index.php) which requires the
     22    distribution of source code with any binary distribution and to
     23    distribute linked combinations of the two.  Non-GPL Code permitted under
     24    this exception must only link to the code of Red Hat elfutils through
     25    those well defined interfaces identified in the file named EXCEPTION
     26    found in the source code files (the "Approved Interfaces").  The files
     27    of Non-GPL Code may instantiate templates or use macros or inline
     28    functions from the Approved Interfaces without causing the resulting
     29    work to be covered by the GNU General Public License.  Only Red Hat,
     30    Inc. may make changes or additions to the list of Approved Interfaces.
     31    Red Hat's grant of this exception is conditioned upon your not adding
     32    any new exceptions.  If you wish to add a new Approved Interface or
     33    exception, please contact Red Hat.  You must obey the GNU General Public
     34    License in all respects for all of the Red Hat elfutils code and other
     35    code used in conjunction with Red Hat elfutils except the Non-GPL Code
     36    covered by this exception.  If you modify this file, you may extend this
     37    exception to your version of the file, but you are not obligated to do
     38    so.  If you do not wish to provide this exception without modification,
     39    you must delete this exception statement from your version and license
     40    this file solely under the GPL without exception.
     41 
     42    Red Hat elfutils is an included package of the Open Invention Network.
     43    An included package of the Open Invention Network is a package for which
     44    Open Invention Network licensees cross-license their patents.  No patent
     45    license is granted, either expressly or impliedly, by designation as an
     46    included package.  Should you wish to participate in the Open Invention
     47    Network licensing program, please visit www.openinventionnetwork.com
     48    <http://www.openinventionnetwork.com>.  */
     49 
     50 #include "../libelf/libelfP.h"
     51 #undef	_
     52 #include "libdwflP.h"
     53 
     54 #include <unistd.h>
     55 
     56 #if !USE_ZLIB
     57 # define __libdw_gunzip(...)	false
     58 #endif
     59 
     60 #if !USE_BZLIB
     61 # define __libdw_bunzip2(...)	false
     62 #endif
     63 
     64 #if !USE_LZMA
     65 # define __libdw_unlzma(...)	false
     66 #endif
     67 
     68 /* Consumes and replaces *ELF only on success.  */
     69 static Dwfl_Error
     70 decompress (int fd __attribute__ ((unused)), Elf **elf)
     71 {
     72   Dwfl_Error error = DWFL_E_BADELF;
     73   void *buffer = NULL;
     74   size_t size = 0;
     75 
     76 #if USE_ZLIB || USE_BZLIB || USE_LZMA
     77   const off64_t offset = (*elf)->start_offset;
     78   void *const mapped = ((*elf)->map_address == NULL ? NULL
     79 			: (*elf)->map_address + offset);
     80   const size_t mapped_size = (*elf)->maximum_size;
     81   if (mapped_size == 0)
     82     return error;
     83 
     84   error = __libdw_gunzip (fd, offset, mapped, mapped_size, &buffer, &size);
     85   if (error == DWFL_E_BADELF)
     86     error = __libdw_bunzip2 (fd, offset, mapped, mapped_size, &buffer, &size);
     87   if (error == DWFL_E_BADELF)
     88     error = __libdw_unlzma (fd, offset, mapped, mapped_size, &buffer, &size);
     89 #endif
     90 
     91   if (error == DWFL_E_NOERROR)
     92     {
     93       if (unlikely (size == 0))
     94 	{
     95 	  error = DWFL_E_BADELF;
     96 	  free (buffer);
     97 	}
     98       else
     99 	{
    100 	  Elf *memelf = elf_memory (buffer, size);
    101 	  if (memelf == NULL)
    102 	    {
    103 	      error = DWFL_E_LIBELF;
    104 	      free (buffer);
    105 	    }
    106 	  else
    107 	    {
    108 	      memelf->flags |= ELF_F_MALLOCED;
    109 	      elf_end (*elf);
    110 	      *elf = memelf;
    111 	    }
    112 	}
    113     }
    114   else
    115     free (buffer);
    116 
    117   return error;
    118 }
    119 
    120 static Dwfl_Error
    121 what_kind (int fd, Elf **elfp, Elf_Kind *kind, bool *close_fd)
    122 {
    123   Dwfl_Error error = DWFL_E_NOERROR;
    124   *kind = elf_kind (*elfp);
    125   if (unlikely (*kind == ELF_K_NONE))
    126     {
    127       if (unlikely (*elfp == NULL))
    128 	error = DWFL_E_LIBELF;
    129       else
    130 	{
    131 	  error = decompress (fd, elfp);
    132 	  if (error == DWFL_E_NOERROR)
    133 	    {
    134 	      *close_fd = true;
    135 	      *kind = elf_kind (*elfp);
    136 	    }
    137 	}
    138     }
    139   return error;
    140 }
    141 
    142 Dwfl_Error internal_function
    143 __libdw_open_file (int *fdp, Elf **elfp, bool close_on_fail, bool archive_ok)
    144 {
    145   bool close_fd = false;
    146 
    147   Elf *elf = elf_begin (*fdp, ELF_C_READ_MMAP_PRIVATE, NULL);
    148 
    149   Elf_Kind kind;
    150   Dwfl_Error error = what_kind (*fdp, &elf, &kind, &close_fd);
    151   if (error == DWFL_E_BADELF)
    152     {
    153       /* It's not an ELF file or a compressed file.
    154 	 See if it's an image with a header preceding the real file.  */
    155 
    156       off64_t offset = elf->start_offset;
    157       error = __libdw_image_header (*fdp, &offset,
    158 				    (elf->map_address == NULL ? NULL
    159 				     : elf->map_address + offset),
    160 				    elf->maximum_size);
    161       if (error == DWFL_E_NOERROR)
    162 	{
    163 	  /* Pure evil.  libelf needs some better interfaces.  */
    164 	  elf->kind = ELF_K_AR;
    165 	  elf->state.ar.elf_ar_hdr.ar_name = "libdwfl is faking you out";
    166 	  elf->state.ar.elf_ar_hdr.ar_size = elf->maximum_size - offset;
    167 	  elf->state.ar.offset = offset - sizeof (struct ar_hdr);
    168 	  Elf *subelf = elf_begin (-1, ELF_C_READ_MMAP_PRIVATE, elf);
    169 	  elf->kind = ELF_K_NONE;
    170 	  if (unlikely (subelf == NULL))
    171 	    error = DWFL_E_LIBELF;
    172 	  else
    173 	    {
    174 	      subelf->parent = NULL;
    175 	      subelf->flags |= elf->flags & (ELF_F_MMAPPED | ELF_F_MALLOCED);
    176 	      elf->flags &= ~(ELF_F_MMAPPED | ELF_F_MALLOCED);
    177 	      elf_end (elf);
    178 	      elf = subelf;
    179 	      error = what_kind (*fdp, &elf, &kind, &close_fd);
    180 	    }
    181 	}
    182     }
    183 
    184   if (error == DWFL_E_NOERROR
    185       && kind != ELF_K_ELF
    186       && !(archive_ok && kind == ELF_K_AR))
    187     error = DWFL_E_BADELF;
    188 
    189   if (error != DWFL_E_NOERROR)
    190     {
    191       elf_end (elf);
    192       elf = NULL;
    193     }
    194 
    195   if (error == DWFL_E_NOERROR ? close_fd : close_on_fail)
    196     {
    197       close (*fdp);
    198       *fdp = -1;
    199     }
    200 
    201   *elfp = elf;
    202   return error;
    203 }
    204