Home | History | Annotate | Download | only in libdw
      1 /* Advance to next CU header.
      2    Copyright (C) 2002-2010 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 <libdwP.h>
     35 #include <dwarf.h>
     36 
     37 
     38 int
     39 dwarf_next_unit (dwarf, off, next_off, header_sizep, versionp, abbrev_offsetp,
     40 		 address_sizep, offset_sizep, type_signaturep, type_offsetp)
     41      Dwarf *dwarf;
     42      Dwarf_Off off;
     43      Dwarf_Off *next_off;
     44      size_t *header_sizep;
     45      Dwarf_Half *versionp;
     46      Dwarf_Off *abbrev_offsetp;
     47      uint8_t *address_sizep;
     48      uint8_t *offset_sizep;
     49      uint64_t *type_signaturep;
     50      Dwarf_Off *type_offsetp;
     51 {
     52   const bool debug_types = type_signaturep != NULL;
     53   const size_t sec_idx = debug_types ? IDX_debug_types : IDX_debug_info;
     54 
     55   /* Maybe there has been an error before.  */
     56   if (dwarf == NULL)
     57     return -1;
     58 
     59   /* If we reached the end before don't do anything.  */
     60   if (off == (Dwarf_Off) -1l
     61       || unlikely (dwarf->sectiondata[sec_idx] == NULL)
     62       /* Make sure there is enough space in the .debug_info section
     63 	 for at least the initial word.  We cannot test the rest since
     64 	 we don't know yet whether this is a 64-bit object or not.  */
     65       || unlikely (off + 4 >= dwarf->sectiondata[sec_idx]->d_size))
     66     {
     67       *next_off = (Dwarf_Off) -1l;
     68       return 1;
     69     }
     70 
     71   /* This points into the .debug_info section to the beginning of the
     72      CU entry.  */
     73   const unsigned char *data = dwarf->sectiondata[sec_idx]->d_buf;
     74   const unsigned char *bytes = data + off;
     75 
     76   /* The format of the CU header is described in dwarf2p1 7.5.1:
     77 
     78      1.  A 4-byte or 12-byte unsigned integer representing the length
     79 	 of the .debug_info contribution for that compilation unit, not
     80 	 including the length field itself. In the 32-bit DWARF format,
     81 	 this is a 4-byte unsigned integer (which must be less than
     82 	 0xfffffff0); in the 64-bit DWARF format, this consists of the
     83 	 4-byte value 0xffffffff followed by an 8-byte unsigned integer
     84 	 that gives the actual length (see Section 7.2.2).
     85 
     86       2. A 2-byte unsigned integer representing the version of the
     87 	 DWARF information for that compilation unit. For DWARF Version
     88 	 2.1, the value in this field is 2.
     89 
     90       3. A 4-byte or 8-byte unsigned offset into the .debug_abbrev
     91 	 section. This offset associates the compilation unit with a
     92 	 particular set of debugging information entry abbreviations. In
     93 	 the 32-bit DWARF format, this is a 4-byte unsigned length; in
     94 	 the 64-bit DWARF format, this is an 8-byte unsigned length (see
     95 	 Section 7.4).
     96 
     97       4. A 1-byte unsigned integer representing the size in bytes of
     98 	 an address on the target architecture. If the system uses
     99 	 segmented addressing, this value represents the size of the
    100 	 offset portion of an address.  */
    101   uint64_t length = read_4ubyte_unaligned_inc (dwarf, bytes);
    102   size_t offset_size = 4;
    103   /* Lengths of 0xfffffff0 - 0xffffffff are escape codes.  Oxffffffff is
    104      used to indicate that 64-bit dwarf information is being used, the
    105      other values are currently reserved.  */
    106   if (length == DWARF3_LENGTH_64_BIT)
    107     offset_size = 8;
    108   else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
    109 		     && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
    110     {
    111     invalid:
    112       __libdw_seterrno (DWARF_E_INVALID_DWARF);
    113       return -1;
    114     }
    115 
    116   /* Now we know how large the header is.  */
    117   if (unlikely (DIE_OFFSET_FROM_CU_OFFSET (off, offset_size, debug_types)
    118 		>= dwarf->sectiondata[sec_idx]->d_size))
    119     {
    120       *next_off = -1;
    121       return 1;
    122     }
    123 
    124   if (length == DWARF3_LENGTH_64_BIT)
    125     /* This is a 64-bit DWARF format.  */
    126     length = read_8ubyte_unaligned_inc (dwarf, bytes);
    127 
    128   /* Read the version stamp.  Always a 16-bit value.  */
    129   uint_fast16_t version = read_2ubyte_unaligned_inc (dwarf, bytes);
    130 
    131   /* Get offset in .debug_abbrev.  Note that the size of the entry
    132      depends on whether this is a 32-bit or 64-bit DWARF definition.  */
    133   uint64_t abbrev_offset;
    134   if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size,
    135 			       &abbrev_offset, IDX_debug_abbrev, 0))
    136     return -1;
    137 
    138   /* The address size.  Always an 8-bit value.  */
    139   uint8_t address_size = *bytes++;
    140 
    141   if (debug_types)
    142     {
    143       uint64_t type_sig8 = read_8ubyte_unaligned_inc (dwarf, bytes);
    144 
    145       Dwarf_Off type_offset;
    146       if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size,
    147 				   &type_offset, sec_idx, 0))
    148 	return -1;
    149 
    150       /* Validate that the TYPE_OFFSET points past the header.  */
    151       if (unlikely (type_offset < (size_t) (bytes - (data + off))))
    152 	goto invalid;
    153 
    154       *type_signaturep = type_sig8;
    155       if (type_offsetp != NULL)
    156 	*type_offsetp = type_offset;
    157     }
    158 
    159   /* Store the header length.  */
    160   if (header_sizep != NULL)
    161     *header_sizep = bytes - (data + off);
    162 
    163   if (versionp != NULL)
    164     *versionp = version;
    165 
    166   if (abbrev_offsetp != NULL)
    167     *abbrev_offsetp = abbrev_offset;
    168 
    169   if (address_sizep != NULL)
    170     *address_sizep = address_size;
    171 
    172   /* Store the offset size.  */
    173   if (offset_sizep != NULL)
    174     *offset_sizep = offset_size;
    175 
    176   /* See definition of DIE_OFFSET_FROM_CU_OFFSET macro
    177      for an explanation of the trick in this expression.  */
    178   *next_off = off + 2 * offset_size - 4 + length;
    179 
    180   return 0;
    181 }
    182 INTDEF(dwarf_next_unit)
    183 
    184 int
    185 dwarf_nextcu (dwarf, off, next_off, header_sizep, abbrev_offsetp,
    186 	      address_sizep, offset_sizep)
    187      Dwarf *dwarf;
    188      Dwarf_Off off;
    189      Dwarf_Off *next_off;
    190      size_t *header_sizep;
    191      Dwarf_Off *abbrev_offsetp;
    192      uint8_t *address_sizep;
    193      uint8_t *offset_sizep;
    194 {
    195   return INTUSE(dwarf_next_unit) (dwarf, off, next_off, header_sizep, NULL,
    196 				  abbrev_offsetp, address_sizep, offset_sizep,
    197 				  NULL, NULL);
    198 }
    199 INTDEF(dwarf_nextcu)
    200