Home | History | Annotate | Download | only in libdw
      1 /* Create descriptor from ELF descriptor for processing file.
      2    Copyright (C) 2002-2011, 2014 Red Hat, Inc.
      3    This file is part of elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      5 
      6    This file is free software; you can redistribute it and/or modify
      7    it under the terms of either
      8 
      9      * the GNU Lesser General Public License as published by the Free
     10        Software Foundation; either version 3 of the License, or (at
     11        your option) any later version
     12 
     13    or
     14 
     15      * the GNU General Public License as published by the Free
     16        Software Foundation; either version 2 of the License, or (at
     17        your option) any later version
     18 
     19    or both in parallel, as here.
     20 
     21    elfutils is distributed in the hope that it will be useful, but
     22    WITHOUT ANY WARRANTY; without even the implied warranty of
     23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     24    General Public License for more details.
     25 
     26    You should have received copies of the GNU General Public License and
     27    the GNU Lesser General Public License along with this program.  If
     28    not, see <http://www.gnu.org/licenses/>.  */
     29 
     30 #ifdef HAVE_CONFIG_H
     31 # include <config.h>
     32 #endif
     33 
     34 #include <assert.h>
     35 #include <stdbool.h>
     36 #include <stddef.h>
     37 #include <stdlib.h>
     38 #include <stdio.h>
     39 #include <string.h>
     40 #include <unistd.h>
     41 #include <sys/types.h>
     42 #include <sys/stat.h>
     43 #include <fcntl.h>
     44 
     45 #include "libdwP.h"
     46 
     47 #if USE_ZLIB
     48 # include <endian.h>
     49 # define crc32		loser_crc32
     50 # include <zlib.h>
     51 # undef crc32
     52 #endif
     53 
     54 
     55 /* Section names.  */
     56 static const char dwarf_scnnames[IDX_last][18] =
     57 {
     58   [IDX_debug_info] = ".debug_info",
     59   [IDX_debug_types] = ".debug_types",
     60   [IDX_debug_abbrev] = ".debug_abbrev",
     61   [IDX_debug_aranges] = ".debug_aranges",
     62   [IDX_debug_line] = ".debug_line",
     63   [IDX_debug_frame] = ".debug_frame",
     64   [IDX_debug_loc] = ".debug_loc",
     65   [IDX_debug_pubnames] = ".debug_pubnames",
     66   [IDX_debug_str] = ".debug_str",
     67   [IDX_debug_macinfo] = ".debug_macinfo",
     68   [IDX_debug_macro] = ".debug_macro",
     69   [IDX_debug_ranges] = ".debug_ranges",
     70   [IDX_gnu_debugaltlink] = ".gnu_debugaltlink"
     71 };
     72 #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
     73 
     74 static Dwarf *
     75 check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
     76 {
     77   GElf_Shdr shdr_mem;
     78   GElf_Shdr *shdr;
     79 
     80   /* Get the section header data.  */
     81   shdr = gelf_getshdr (scn, &shdr_mem);
     82   if (shdr == NULL)
     83     /* We may read /proc/PID/mem with only program headers mapped and section
     84        headers out of the mapped pages.  */
     85     goto err;
     86 
     87   /* Ignore any SHT_NOBITS sections.  Debugging sections should not
     88      have been stripped, but in case of a corrupt file we won't try
     89      to look at the missing data.  */
     90   if (unlikely (shdr->sh_type == SHT_NOBITS))
     91     return result;
     92 
     93   /* Make sure the section is part of a section group only iff we
     94      really need it.  If we are looking for the global (= non-section
     95      group debug info) we have to ignore all the info in section
     96      groups.  If we are looking into a section group we cannot look at
     97      a section which isn't part of the section group.  */
     98   if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0)
     99     /* Ignore the section.  */
    100     return result;
    101 
    102 
    103   /* We recognize the DWARF section by their names.  This is not very
    104      safe and stable but the best we can do.  */
    105   const char *scnname = elf_strptr (result->elf, ehdr->e_shstrndx,
    106 				    shdr->sh_name);
    107   if (scnname == NULL)
    108     {
    109       /* The section name must be valid.  Otherwise is the ELF file
    110 	 invalid.  */
    111     err:
    112       __libdw_free_zdata (result);
    113       Dwarf_Sig8_Hash_free (&result->sig8_hash);
    114       __libdw_seterrno (DWARF_E_INVALID_ELF);
    115       free (result);
    116       return NULL;
    117     }
    118 
    119   /* Recognize the various sections.  Most names start with .debug_.  */
    120   size_t cnt;
    121   for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
    122     if (strcmp (scnname, dwarf_scnnames[cnt]) == 0)
    123       {
    124 	/* Found it.  Remember where the data is.  */
    125 	if (unlikely (result->sectiondata[cnt] != NULL))
    126 	  /* A section appears twice.  That's bad.  We ignore the section.  */
    127 	  break;
    128 
    129 	/* Get the section data.  */
    130 	Elf_Data *data = elf_getdata (scn, NULL);
    131 	if (data != NULL && data->d_size != 0)
    132 	  /* Yep, there is actually data available.  */
    133 	  result->sectiondata[cnt] = data;
    134 
    135 	break;
    136       }
    137 #if USE_ZLIB
    138     else if (scnname[0] == '.' && scnname[1] == 'z'
    139 	     && strcmp (&scnname[2], &dwarf_scnnames[cnt][1]) == 0)
    140       {
    141 	/* A compressed section.  */
    142 
    143 	if (unlikely (result->sectiondata[cnt] != NULL))
    144 	  /* A section appears twice.  That's bad.  We ignore the section.  */
    145 	  break;
    146 
    147 	/* Get the section data.  */
    148 	Elf_Data *data = elf_getdata (scn, NULL);
    149 	if (data != NULL && data->d_size != 0)
    150 	  {
    151 	    /* There is a 12-byte header of "ZLIB" followed by
    152 	       an 8-byte big-endian size.  */
    153 
    154 	    if (unlikely (data->d_size < 4 + 8)
    155 		|| unlikely (memcmp (data->d_buf, "ZLIB", 4) != 0))
    156 	      break;
    157 
    158 	    uint64_t size;
    159 	    memcpy (&size, data->d_buf + 4, sizeof size);
    160 	    size = be64toh (size);
    161 
    162 	    /* Check for unsigned overflow so malloc always allocated
    163 	       enough memory for both the Elf_Data header and the
    164 	       uncompressed section data.  */
    165 	    if (unlikely (sizeof (Elf_Data) + size < size))
    166 	      break;
    167 
    168 	    Elf_Data *zdata = malloc (sizeof (Elf_Data) + size);
    169 	    if (unlikely (zdata == NULL))
    170 	      break;
    171 
    172 	    zdata->d_buf = &zdata[1];
    173 	    zdata->d_type = ELF_T_BYTE;
    174 	    zdata->d_version = EV_CURRENT;
    175 	    zdata->d_size = size;
    176 	    zdata->d_off = 0;
    177 	    zdata->d_align = 1;
    178 
    179 	    z_stream z =
    180 	      {
    181 		.next_in = data->d_buf + 4 + 8,
    182 		.avail_in = data->d_size - 4 - 8,
    183 		.next_out = zdata->d_buf,
    184 		.avail_out = zdata->d_size
    185 	      };
    186 	    int zrc = inflateInit (&z);
    187 	    while (z.avail_in > 0 && likely (zrc == Z_OK))
    188 	      {
    189 		z.next_out = zdata->d_buf + (zdata->d_size - z.avail_out);
    190 		zrc = inflate (&z, Z_FINISH);
    191 		if (unlikely (zrc != Z_STREAM_END))
    192 		  {
    193 		    zrc = Z_DATA_ERROR;
    194 		    break;
    195 		  }
    196 		zrc = inflateReset (&z);
    197 	      }
    198 	    if (likely (zrc == Z_OK))
    199 	      zrc = inflateEnd (&z);
    200 
    201 	    if (unlikely (zrc != Z_OK) || unlikely (z.avail_out != 0))
    202 	      free (zdata);
    203 	    else
    204 	      {
    205 		result->sectiondata[cnt] = zdata;
    206 		result->sectiondata_gzip_mask |= 1U << cnt;
    207 	      }
    208 	  }
    209 
    210 	break;
    211       }
    212 #endif
    213 
    214   return result;
    215 }
    216 
    217 
    218 /* Check whether all the necessary DWARF information is available.  */
    219 static Dwarf *
    220 valid_p (Dwarf *result)
    221 {
    222   /* We looked at all the sections.  Now determine whether all the
    223      sections with debugging information we need are there.
    224 
    225      XXX Which sections are absolutely necessary?  Add tests if
    226      necessary.  For now we require only .debug_info.  Hopefully this
    227      is correct.  */
    228   if (likely (result != NULL)
    229       && unlikely (result->sectiondata[IDX_debug_info] == NULL))
    230     {
    231       __libdw_free_zdata (result);
    232       Dwarf_Sig8_Hash_free (&result->sig8_hash);
    233       __libdw_seterrno (DWARF_E_NO_DWARF);
    234       free (result);
    235       result = NULL;
    236     }
    237 
    238   if (result != NULL && result->sectiondata[IDX_debug_loc] != NULL)
    239     {
    240       result->fake_loc_cu = (Dwarf_CU *) calloc (1, sizeof (Dwarf_CU));
    241       if (unlikely (result->fake_loc_cu == NULL))
    242 	{
    243 	  __libdw_free_zdata (result);
    244 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
    245 	  __libdw_seterrno (DWARF_E_NOMEM);
    246 	  free (result);
    247 	  result = NULL;
    248 	}
    249       else
    250 	{
    251 	  result->fake_loc_cu->dbg = result;
    252 	  result->fake_loc_cu->startp
    253 	    = result->sectiondata[IDX_debug_loc]->d_buf;
    254 	  result->fake_loc_cu->endp
    255 	    = (result->sectiondata[IDX_debug_loc]->d_buf
    256 	       + result->sectiondata[IDX_debug_loc]->d_size);
    257 	}
    258     }
    259 
    260   return result;
    261 }
    262 
    263 
    264 static Dwarf *
    265 global_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr)
    266 {
    267   Elf_Scn *scn = NULL;
    268 
    269   while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL)
    270     result = check_section (result, ehdr, scn, false);
    271 
    272   return valid_p (result);
    273 }
    274 
    275 
    276 static Dwarf *
    277 scngrp_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr, Elf_Scn *scngrp)
    278 {
    279   /* SCNGRP is the section descriptor for a section group which might
    280      contain debug sections.  */
    281   Elf_Data *data = elf_getdata (scngrp, NULL);
    282   if (data == NULL)
    283     {
    284       /* We cannot read the section content.  Fail!  */
    285       __libdw_free_zdata (result);
    286       Dwarf_Sig8_Hash_free (&result->sig8_hash);
    287       free (result);
    288       return NULL;
    289     }
    290 
    291   /* The content of the section is a number of 32-bit words which
    292      represent section indices.  The first word is a flag word.  */
    293   Elf32_Word *scnidx = (Elf32_Word *) data->d_buf;
    294   size_t cnt;
    295   for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt)
    296     {
    297       Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
    298       if (scn == NULL)
    299 	{
    300 	  /* A section group refers to a non-existing section.  Should
    301 	     never happen.  */
    302 	  __libdw_free_zdata (result);
    303 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
    304 	  __libdw_seterrno (DWARF_E_INVALID_ELF);
    305 	  free (result);
    306 	  return NULL;
    307 	}
    308 
    309       result = check_section (result, ehdr, scn, true);
    310       if (result == NULL)
    311 	break;
    312     }
    313 
    314   return valid_p (result);
    315 }
    316 
    317 
    318 Dwarf *
    319 dwarf_begin_elf (elf, cmd, scngrp)
    320      Elf *elf;
    321      Dwarf_Cmd cmd;
    322      Elf_Scn *scngrp;
    323 {
    324   GElf_Ehdr *ehdr;
    325   GElf_Ehdr ehdr_mem;
    326 
    327   /* Get the ELF header of the file.  We need various pieces of
    328      information from it.  */
    329   ehdr = gelf_getehdr (elf, &ehdr_mem);
    330   if (ehdr == NULL)
    331     {
    332       if (elf_kind (elf) != ELF_K_ELF)
    333 	__libdw_seterrno (DWARF_E_NOELF);
    334       else
    335 	__libdw_seterrno (DWARF_E_GETEHDR_ERROR);
    336 
    337       return NULL;
    338     }
    339 
    340 
    341   /* Default memory allocation size.  */
    342   size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
    343 
    344   /* Allocate the data structure.  */
    345   Dwarf *result = (Dwarf *) calloc (1, sizeof (Dwarf) + mem_default_size);
    346   if (unlikely (result == NULL)
    347       || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0))
    348     {
    349       free (result);
    350       __libdw_seterrno (DWARF_E_NOMEM);
    351       return NULL;
    352     }
    353 
    354   /* Fill in some values.  */
    355   if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
    356       || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB))
    357     result->other_byte_order = true;
    358 
    359   result->elf = elf;
    360 
    361   /* Initialize the memory handling.  */
    362   result->mem_default_size = mem_default_size;
    363   result->oom_handler = __libdw_oom;
    364   result->mem_tail = (struct libdw_memblock *) (result + 1);
    365   result->mem_tail->size = (result->mem_default_size
    366 			    - offsetof (struct libdw_memblock, mem));
    367   result->mem_tail->remaining = result->mem_tail->size;
    368   result->mem_tail->prev = NULL;
    369 
    370   if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
    371     {
    372       /* If the caller provides a section group we get the DWARF
    373 	 sections only from this setion group.  Otherwise we search
    374 	 for the first section with the required name.  Further
    375 	 sections with the name are ignored.  The DWARF specification
    376 	 does not really say this is allowed.  */
    377       if (scngrp == NULL)
    378 	return global_read (result, elf, ehdr);
    379       else
    380 	return scngrp_read (result, elf, ehdr, scngrp);
    381     }
    382   else if (cmd == DWARF_C_WRITE)
    383     {
    384       Dwarf_Sig8_Hash_free (&result->sig8_hash);
    385       __libdw_seterrno (DWARF_E_UNIMPL);
    386       free (result);
    387       return NULL;
    388     }
    389 
    390   Dwarf_Sig8_Hash_free (&result->sig8_hash);
    391   __libdw_seterrno (DWARF_E_INVALID_CMD);
    392   free (result);
    393   return NULL;
    394 }
    395 INTDEF(dwarf_begin_elf)
    396