Blame libdw/dwarf_nextcu.c

Packit 032894
/* Advance to next CU header.
Packit 032894
   Copyright (C) 2002-2010, 2016, 2017 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <libdwP.h>
Packit 032894
#include <dwarf.h>
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
dwarf_next_unit (Dwarf *dwarf, Dwarf_Off off, Dwarf_Off *next_off,
Packit 032894
		 size_t *header_sizep, Dwarf_Half *versionp,
Packit 032894
		 Dwarf_Off *abbrev_offsetp, uint8_t *address_sizep,
Packit 032894
		 uint8_t *offset_sizep, uint64_t *v4_type_signaturep,
Packit 032894
		 Dwarf_Off *v4_type_offsetp)
Packit 032894
{
Packit 032894
  const bool v4_debug_types = v4_type_signaturep != NULL;
Packit 032894
  return __libdw_next_unit (dwarf, v4_debug_types, off, next_off,
Packit 032894
			     header_sizep, versionp, NULL,
Packit 032894
			     abbrev_offsetp, address_sizep, offset_sizep,
Packit 032894
			     v4_type_signaturep, v4_type_offsetp);
Packit 032894
}
Packit 032894
INTDEF(dwarf_next_unit)
Packit 032894
Packit 032894
int
Packit 032894
internal_function
Packit 032894
__libdw_next_unit (Dwarf *dwarf, bool v4_debug_types, Dwarf_Off off,
Packit 032894
		   Dwarf_Off *next_off, size_t *header_sizep,
Packit 032894
		   Dwarf_Half *versionp, uint8_t *unit_typep,
Packit 032894
		   Dwarf_Off *abbrev_offsetp, uint8_t *address_sizep,
Packit 032894
		   uint8_t *offset_sizep, uint64_t *unit_id8p,
Packit 032894
		   Dwarf_Off *subdie_offsetp)
Packit 032894
{
Packit 032894
  /* Note that debug_type units come from .debug_types in DWARF < 5 and
Packit 032894
     from .debug_info in DWARF >= 5.  If the user requested the
Packit 032894
     v4_type_signature we return from .debug_types always.  If no signature
Packit 032894
     is requested we return units (any type) from .debug_info.  */
Packit 032894
  const size_t sec_idx = v4_debug_types ? IDX_debug_types : IDX_debug_info;
Packit 032894
Packit 032894
  /* Maybe there has been an error before.  */
Packit 032894
  if (dwarf == NULL)
Packit 032894
    return -1;
Packit 032894
Packit 032894
  /* If we reached the end before don't do anything.  */
Packit 032894
  if (off == (Dwarf_Off) -1l
Packit 032894
      || unlikely (dwarf->sectiondata[sec_idx] == NULL)
Packit 032894
      /* Make sure there is enough space in the .debug_info section
Packit 032894
	 for at least the initial word.  We cannot test the rest since
Packit 032894
	 we don't know yet whether this is a 64-bit object or not.  */
Packit 032894
      || unlikely (off + 4 >= dwarf->sectiondata[sec_idx]->d_size))
Packit 032894
    {
Packit 032894
      *next_off = (Dwarf_Off) -1l;
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* This points into the .debug_info or .debug_types section to the
Packit 032894
     beginning of the CU entry.  */
Packit 032894
  const unsigned char *data = dwarf->sectiondata[sec_idx]->d_buf;
Packit 032894
  const unsigned char *bytes = data + off;
Packit 032894
  const unsigned char *bytes_end = data + dwarf->sectiondata[sec_idx]->d_size;
Packit 032894
Packit 032894
  /* The format of the CU header is described in dwarf2p1 7.5.1 and
Packit 032894
     changed in DWARFv5 (to include unit type, switch location of some
Packit 032894
     fields and add some optional fields).
Packit 032894
Packit 032894
     1.  A 4-byte or 12-byte unsigned integer representing the length
Packit 032894
	 of the .debug_info contribution for that compilation unit, not
Packit 032894
	 including the length field itself. In the 32-bit DWARF format,
Packit 032894
	 this is a 4-byte unsigned integer (which must be less than
Packit 032894
	 0xfffffff0); in the 64-bit DWARF format, this consists of the
Packit 032894
	 4-byte value 0xffffffff followed by an 8-byte unsigned integer
Packit 032894
	 that gives the actual length (see Section 7.2.2). This field
Packit 032894
	 indicates whether this unit is 32-bit of 64-bit DWARF, which
Packit 032894
	 affects all other offset fields in this header.
Packit 032894
Packit 032894
      2. A 2-byte unsigned integer representing the version of the
Packit 032894
	 DWARF information for that compilation unit. For DWARF Version
Packit 032894
	 2.1, the value in this field is 2 (3 for v3, 4 for v4, 5 for v5).
Packit 032894
	 This fields determines the order of the next fields and whether
Packit 032894
	 there are any optional fields in this header.
Packit 032894
Packit 032894
      3. For DWARF 2, 3 and 4 (including v4 type units):
Packit 032894
         A 4-byte or 8-byte unsigned offset into the .debug_abbrev
Packit 032894
	 section. This offset associates the compilation unit with a
Packit 032894
	 particular set of debugging information entry abbreviations. In
Packit 032894
	 the 32-bit DWARF format, this is a 4-byte unsigned length; in
Packit 032894
	 the 64-bit DWARF format, this is an 8-byte unsigned length (see
Packit 032894
	 Section 7.4).
Packit 032894
Packit 032894
	 For DWARF 5:
Packit 032894
	 A 1-byte unsigned integer representing the unit (header) type.
Packit 032894
	 This field determines what the optional fields in the header
Packit 032894
	 represent.  If this is an unknown unit type then we cannot
Packit 032894
	 assume anything about the rest of the unit (header).
Packit 032894
Packit 032894
      4. For all DWARF versions (including v4 type units):
Packit 032894
         A 1-byte unsigned integer representing the size in bytes of
Packit 032894
	 an address on the target architecture. If the system uses
Packit 032894
	 segmented addressing, this value represents the size of the
Packit 032894
	 offset portion of an address. This is the last field in the header
Packit 032894
	 for DWARF versions 2, 3 and 4 (except for v4 type units).
Packit 032894
Packit 032894
      5. For DWARF 5 only (this is field 3 for DWARF 2, 3, 4 and v4 types):
Packit 032894
         A 4-byte or 8-byte unsigned offset into the .debug_abbrev
Packit 032894
	 section. This offset associates the compilation unit with a
Packit 032894
	 particular set of debugging information entry abbreviations. In
Packit 032894
	 the 32-bit DWARF format, this is a 4-byte unsigned length; in
Packit 032894
	 the 64-bit DWARF format, this is an 8-byte unsigned length.
Packit 032894
Packit 032894
      6. For v4 type units (this is really field 5 for v4 types) and
Packit 032894
         DWARF 5 optional (skeleton, split_compile, type and
Packit 032894
         split_type): An 8 byte (opaque) integer constant value. For
Packit 032894
         v4 and v5 type units this is the type signature. For skeleton
Packit 032894
         and split compile units this is the compilation ID.
Packit 032894
Packit 032894
      7. For v4 type units (this is really field 6 for v4 types) and
Packit 032894
         DWARF 5 optional (type and split_type) and v4 type units:
Packit 032894
         A 4-byte or 8-byte unsigned offset. In the 32-bit DWARF format,
Packit 032894
         this is a 4-byte unsigned length; in the 64-bit DWARF format,
Packit 032894
         this is an 8-byte unsigned length. This is the type DIE offset
Packit 032894
	 (which is not necessarily the first DIE in the unit).
Packit 032894
  */
Packit 032894
Packit 032894
  uint64_t length = read_4ubyte_unaligned_inc (dwarf, bytes);
Packit 032894
  size_t offset_size = 4;
Packit 032894
  /* Lengths of 0xfffffff0 - 0xffffffff are escape codes.  Oxffffffff is
Packit 032894
     used to indicate that 64-bit dwarf information is being used, the
Packit 032894
     other values are currently reserved.  */
Packit 032894
  if (length == DWARF3_LENGTH_64_BIT)
Packit 032894
    offset_size = 8;
Packit 032894
  else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
Packit 032894
		     && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
Packit 032894
    {
Packit 032894
    invalid:
Packit 032894
      __libdw_seterrno (DWARF_E_INVALID_DWARF);
Packit 032894
      return -1;
Packit 032894
    }
Packit 032894
Packit 032894
  if (length == DWARF3_LENGTH_64_BIT)
Packit 032894
    {
Packit 032894
      /* This is a 64-bit DWARF format.  */
Packit 032894
      if (bytes_end - bytes < 8)
Packit 032894
	goto invalid;
Packit 032894
      length = read_8ubyte_unaligned_inc (dwarf, bytes);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Read the version stamp.  Always a 16-bit value.  */
Packit 032894
  if (bytes_end - bytes < 2)
Packit 032894
    goto invalid;
Packit 032894
  uint_fast16_t version = read_2ubyte_unaligned_inc (dwarf, bytes);
Packit 032894
Packit 032894
  /* We keep unit_type at zero for older DWARF since we cannot
Packit 032894
     easily guess whether it is a compile or partial unit.  */
Packit 032894
  uint8_t unit_type = 0;
Packit 032894
  if (version >= 5)
Packit 032894
    {
Packit 032894
      if (bytes_end - bytes < 1)
Packit 032894
	goto invalid;
Packit 032894
      unit_type = *bytes++;
Packit 032894
    }
Packit 032894
Packit 032894
  /* All these are optional.  */
Packit 032894
  Dwarf_Off subdie_off = 0;
Packit 032894
  uint64_t sig_id = 0;
Packit 032894
  Dwarf_Off abbrev_offset = 0;
Packit 032894
  uint8_t address_size = 0;
Packit 032894
Packit 032894
  if (version < 2 || version > 5
Packit 032894
      || (version == 5 && ! (unit_type == DW_UT_compile
Packit 032894
			     || unit_type == DW_UT_partial
Packit 032894
			     || unit_type == DW_UT_skeleton
Packit 032894
			     || unit_type == DW_UT_split_compile
Packit 032894
			     || unit_type == DW_UT_type
Packit 032894
			     || unit_type == DW_UT_split_type)))
Packit 032894
    {
Packit 032894
      /* We cannot really know more about the header.  Just report
Packit 032894
	 the length of the unit, version and unit type.  */
Packit 032894
      goto done;
Packit 032894
    }
Packit 032894
Packit 032894
  /* We have to guess the unit_type. But we don't have a real CUDIE.  */
Packit 032894
  if (version < 5)
Packit 032894
    unit_type = v4_debug_types ? DW_UT_type : DW_UT_compile;
Packit 032894
Packit 032894
  /* Now we know how large the header is (should be).  */
Packit 032894
  if (unlikely (__libdw_first_die_from_cu_start (off, offset_size, version,
Packit 032894
						 unit_type)
Packit 032894
		>= dwarf->sectiondata[sec_idx]->d_size))
Packit 032894
    {
Packit 032894
      *next_off = -1;
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* The address size.  Always an 8-bit value.
Packit 032894
     Comes after abbrev_offset for version < 5, otherwise unit type
Packit 032894
     and address size (if a known unit type) comes before abbrev_offset.  */
Packit 032894
  if (version >= 5)
Packit 032894
    address_size = *bytes++;
Packit 032894
Packit 032894
  /* Get offset in .debug_abbrev.  Note that the size of the entry
Packit 032894
     depends on whether this is a 32-bit or 64-bit DWARF definition.  */
Packit 032894
  if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size,
Packit 032894
			       &abbrev_offset, IDX_debug_abbrev, 0))
Packit 032894
    return -1;
Packit 032894
Packit 032894
  if (version < 5)
Packit 032894
    address_size = *bytes++;
Packit 032894
Packit 032894
  /* Extra fields, signature/id and type offset/padding.  */
Packit 032894
  if (v4_debug_types
Packit 032894
      || (version >= 5
Packit 032894
	  && (unit_type == DW_UT_skeleton || unit_type == DW_UT_split_compile
Packit 032894
	      || unit_type == DW_UT_type || unit_type == DW_UT_split_type)))
Packit 032894
    {
Packit 032894
      sig_id = read_8ubyte_unaligned_inc (dwarf, bytes);
Packit 032894
Packit 032894
      if ((v4_debug_types
Packit 032894
	   || unit_type == DW_UT_type || unit_type == DW_UT_split_type))
Packit 032894
	{
Packit 032894
	  if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size,
Packit 032894
				       &subdie_off, sec_idx, 0))
Packit 032894
	    return -1;
Packit 032894
Packit 032894
	  /* Validate that the TYPE_OFFSET points past the header.  */
Packit 032894
	  if (unlikely (subdie_off < (size_t) (bytes - (data + off))))
Packit 032894
	    goto invalid;
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
 done:
Packit 032894
  if (unit_id8p != NULL)
Packit 032894
    *unit_id8p = sig_id;
Packit 032894
Packit 032894
  if (subdie_offsetp != NULL)
Packit 032894
    *subdie_offsetp = subdie_off;
Packit 032894
Packit 032894
  /* Store the header length.  This is really how much we have read
Packit 032894
     from the header.  If we didn't recognize the unit type the
Packit 032894
     header might actually be bigger.  */
Packit 032894
  if (header_sizep != NULL)
Packit 032894
    *header_sizep = bytes - (data + off);
Packit 032894
Packit 032894
  if (versionp != NULL)
Packit 032894
    *versionp = version;
Packit 032894
Packit 032894
  if (unit_typep != NULL)
Packit 032894
    *unit_typep = unit_type;
Packit 032894
Packit 032894
  if (abbrev_offsetp != NULL)
Packit 032894
    *abbrev_offsetp = abbrev_offset;
Packit 032894
Packit 032894
  if (address_sizep != NULL)
Packit 032894
    *address_sizep = address_size;
Packit 032894
Packit 032894
  /* Store the offset size.  */
Packit 032894
  if (offset_sizep != NULL)
Packit 032894
    *offset_sizep = offset_size;
Packit 032894
Packit 032894
  /* The length of the unit doesn't include the length field itself.
Packit 032894
     The length field is either, with offset == 4: 2 * 4 - 4 == 4,
Packit 032894
     or with offset == 8: 2 * 8 - 4 == 12.  */
Packit 032894
  *next_off = off + 2 * offset_size - 4 + length;
Packit 032894
Packit 032894
  /* This means that the length field is bogus, but return the CU anyway.
Packit 032894
     We just won't return anything after this.  */
Packit 032894
  if (*next_off <= off)
Packit 032894
    *next_off = (Dwarf_Off) -1;
Packit 032894
Packit 032894
  return 0;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
dwarf_nextcu (Dwarf *dwarf, Dwarf_Off off, Dwarf_Off *next_off,
Packit 032894
	      size_t *header_sizep, Dwarf_Off *abbrev_offsetp,
Packit 032894
	      uint8_t *address_sizep, uint8_t *offset_sizep)
Packit 032894
{
Packit 032894
  return INTUSE(dwarf_next_unit) (dwarf, off, next_off, header_sizep, NULL,
Packit 032894
				  abbrev_offsetp, address_sizep, offset_sizep,
Packit 032894
				  NULL, NULL);
Packit 032894
}
Packit 032894
INTDEF(dwarf_nextcu)