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@redhat.com>, 2002.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
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
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
19 or both in parallel, as here.
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.
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/>. */
39 dwarf_next_unit (dwarf, off, next_off, header_sizep, versionp, abbrev_offsetp,
40 address_sizep, offset_sizep, type_signaturep, type_offsetp)
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;
52 const bool debug_types = type_signaturep != NULL;
53 const size_t sec_idx = debug_types ? IDX_debug_types : IDX_debug_info;
55 /* Maybe there has been an error before. */
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))
67 *next_off = (Dwarf_Off) -1l;
71 /* This points into the .debug_info section to the beginning of the
73 const unsigned char *data = dwarf->sectiondata[sec_idx]->d_buf;
74 const unsigned char *bytes = data + off;
76 /* The format of the CU header is described in dwarf2p1 7.5.1:
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).
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.
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
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)
108 else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
109 && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
112 __libdw_seterrno (DWARF_E_INVALID_DWARF);
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))
124 if (length == DWARF3_LENGTH_64_BIT)
125 /* This is a 64-bit DWARF format. */
126 length = read_8ubyte_unaligned_inc (dwarf, bytes);
128 /* Read the version stamp. Always a 16-bit value. */
129 uint_fast16_t version = read_2ubyte_unaligned_inc (dwarf, bytes);
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))
138 /* The address size. Always an 8-bit value. */
139 uint8_t address_size = *bytes++;
143 uint64_t type_sig8 = read_8ubyte_unaligned_inc (dwarf, bytes);
145 Dwarf_Off type_offset;
146 if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size,
147 &type_offset, sec_idx, 0))
150 /* Validate that the TYPE_OFFSET points past the header. */
151 if (unlikely (type_offset < (size_t) (bytes - (data + off))))
154 *type_signaturep = type_sig8;
155 if (type_offsetp != NULL)
156 *type_offsetp = type_offset;
159 /* Store the header length. */
160 if (header_sizep != NULL)
161 *header_sizep = bytes - (data + off);
163 if (versionp != NULL)
166 if (abbrev_offsetp != NULL)
167 *abbrev_offsetp = abbrev_offset;
169 if (address_sizep != NULL)
170 *address_sizep = address_size;
172 /* Store the offset size. */
173 if (offset_sizep != NULL)
174 *offset_sizep = offset_size;
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;
182 INTDEF(dwarf_next_unit)
185 dwarf_nextcu (dwarf, off, next_off, header_sizep, abbrev_offsetp,
186 address_sizep, offset_sizep)
190 size_t *header_sizep;
191 Dwarf_Off *abbrev_offsetp;
192 uint8_t *address_sizep;
193 uint8_t *offset_sizep;
195 return INTUSE(dwarf_next_unit) (dwarf, off, next_off, header_sizep, NULL,
196 abbrev_offsetp, address_sizep, offset_sizep,