Blame libdw/dwarf_nextcu.c

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