1 /* Advance to next CU header.
2 Copyright (C) 2002-2010, 2016, 2017 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 *dwarf, Dwarf_Off off, Dwarf_Off *next_off,
40 size_t *header_sizep, Dwarf_Half *versionp,
41 Dwarf_Off *abbrev_offsetp, uint8_t *address_sizep,
42 uint8_t *offset_sizep, uint64_t *v4_type_signaturep,
43 Dwarf_Off *v4_type_offsetp)
45 const bool v4_debug_types = v4_type_signaturep != NULL;
46 return __libdw_next_unit (dwarf, v4_debug_types, off, next_off,
47 header_sizep, versionp, NULL,
48 abbrev_offsetp, address_sizep, offset_sizep,
49 v4_type_signaturep, v4_type_offsetp);
51 INTDEF(dwarf_next_unit)
55 __libdw_next_unit (Dwarf *dwarf, bool v4_debug_types, Dwarf_Off off,
56 Dwarf_Off *next_off, size_t *header_sizep,
57 Dwarf_Half *versionp, uint8_t *unit_typep,
58 Dwarf_Off *abbrev_offsetp, uint8_t *address_sizep,
59 uint8_t *offset_sizep, uint64_t *unit_id8p,
60 Dwarf_Off *subdie_offsetp)
62 /* Note that debug_type units come from .debug_types in DWARF < 5 and
63 from .debug_info in DWARF >= 5. If the user requested the
64 v4_type_signature we return from .debug_types always. If no signature
65 is requested we return units (any type) from .debug_info. */
66 const size_t sec_idx = v4_debug_types ? IDX_debug_types : IDX_debug_info;
68 /* Maybe there has been an error before. */
72 /* If we reached the end before don't do anything. */
73 if (off == (Dwarf_Off) -1l
74 || unlikely (dwarf->sectiondata[sec_idx] == NULL)
75 /* Make sure there is enough space in the .debug_info section
76 for at least the initial word. We cannot test the rest since
77 we don't know yet whether this is a 64-bit object or not. */
78 || unlikely (off + 4 >= dwarf->sectiondata[sec_idx]->d_size))
80 *next_off = (Dwarf_Off) -1l;
84 /* This points into the .debug_info or .debug_types section to the
85 beginning of the CU entry. */
86 const unsigned char *data = dwarf->sectiondata[sec_idx]->d_buf;
87 const unsigned char *bytes = data + off;
89 /* The format of the CU header is described in dwarf2p1 7.5.1 and
90 changed in DWARFv5 (to include unit type, switch location of some
91 fields and add some optional fields).
93 1. A 4-byte or 12-byte unsigned integer representing the length
94 of the .debug_info contribution for that compilation unit, not
95 including the length field itself. In the 32-bit DWARF format,
96 this is a 4-byte unsigned integer (which must be less than
97 0xfffffff0); in the 64-bit DWARF format, this consists of the
98 4-byte value 0xffffffff followed by an 8-byte unsigned integer
99 that gives the actual length (see Section 7.2.2). This field
100 indicates whether this unit is 32-bit of 64-bit DWARF, which
101 affects all other offset fields in this header.
103 2. A 2-byte unsigned integer representing the version of the
104 DWARF information for that compilation unit. For DWARF Version
105 2.1, the value in this field is 2 (3 for v3, 4 for v4, 5 for v5).
106 This fields determines the order of the next fields and whether
107 there are any optional fields in this header.
109 3. For DWARF 2, 3 and 4 (including v4 type units):
110 A 4-byte or 8-byte unsigned offset into the .debug_abbrev
111 section. This offset associates the compilation unit with a
112 particular set of debugging information entry abbreviations. In
113 the 32-bit DWARF format, this is a 4-byte unsigned length; in
114 the 64-bit DWARF format, this is an 8-byte unsigned length (see
118 A 1-byte unsigned integer representing the unit (header) type.
119 This field determines what the optional fields in the header
120 represent. If this is an unknown unit type then we cannot
121 assume anything about the rest of the unit (header).
123 4. For all DWARF versions (including v4 type units):
124 A 1-byte unsigned integer representing the size in bytes of
125 an address on the target architecture. If the system uses
126 segmented addressing, this value represents the size of the
127 offset portion of an address. This is the last field in the header
128 for DWARF versions 2, 3 and 4 (except for v4 type units).
130 5. For DWARF 5 only (this is field 3 for DWARF 2, 3, 4 and v4 types):
131 A 4-byte or 8-byte unsigned offset into the .debug_abbrev
132 section. This offset associates the compilation unit with a
133 particular set of debugging information entry abbreviations. In
134 the 32-bit DWARF format, this is a 4-byte unsigned length; in
135 the 64-bit DWARF format, this is an 8-byte unsigned length.
137 6. For v4 type units (this is really field 5 for v4 types) and
138 DWARF 5 optional (skeleton, split_compile, type and
139 split_type): An 8 byte (opaque) integer constant value. For
140 v4 and v5 type units this is the type signature. For skeleton
141 and split compile units this is the compilation ID.
143 7. For v4 type units (this is really field 6 for v4 types) and
144 DWARF 5 optional (type and split_type) and v4 type units:
145 A 4-byte or 8-byte unsigned offset. In the 32-bit DWARF format,
146 this is a 4-byte unsigned length; in the 64-bit DWARF format,
147 this is an 8-byte unsigned length. This is the type DIE offset
148 (which is not necessarily the first DIE in the unit).
151 uint64_t length = read_4ubyte_unaligned_inc (dwarf, bytes);
152 size_t offset_size = 4;
153 /* Lengths of 0xfffffff0 - 0xffffffff are escape codes. Oxffffffff is
154 used to indicate that 64-bit dwarf information is being used, the
155 other values are currently reserved. */
156 if (length == DWARF3_LENGTH_64_BIT)
158 else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
159 && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
162 __libdw_seterrno (DWARF_E_INVALID_DWARF);
166 if (length == DWARF3_LENGTH_64_BIT)
167 /* This is a 64-bit DWARF format. */
168 length = read_8ubyte_unaligned_inc (dwarf, bytes);
170 /* Read the version stamp. Always a 16-bit value. */
171 uint_fast16_t version = read_2ubyte_unaligned_inc (dwarf, bytes);
173 /* We keep unit_type at zero for older DWARF since we cannot
174 easily guess whether it is a compile or partial unit. */
175 uint8_t unit_type = 0;
177 unit_type = *bytes++;
179 /* All these are optional. */
180 Dwarf_Off subdie_off = 0;
182 Dwarf_Off abbrev_offset = 0;
183 uint8_t address_size = 0;
185 if (version < 2 || version > 5
186 || (version == 5 && ! (unit_type == DW_UT_compile
187 || unit_type == DW_UT_partial
188 || unit_type == DW_UT_skeleton
189 || unit_type == DW_UT_split_compile
190 || unit_type == DW_UT_type
191 || unit_type == DW_UT_split_type)))
193 /* We cannot really know more about the header. Just report
194 the length of the unit, version and unit type. */
198 /* We have to guess the unit_type. But we don't have a real CUDIE. */
200 unit_type = v4_debug_types ? DW_UT_type : DW_UT_compile;
202 /* Now we know how large the header is (should be). */
203 if (unlikely (__libdw_first_die_from_cu_start (off, offset_size, version,
205 >= dwarf->sectiondata[sec_idx]->d_size))
211 /* The address size. Always an 8-bit value.
212 Comes after abbrev_offset for version < 5, otherwise unit type
213 and address size (if a known unit type) comes before abbrev_offset. */
215 address_size = *bytes++;
217 /* Get offset in .debug_abbrev. Note that the size of the entry
218 depends on whether this is a 32-bit or 64-bit DWARF definition. */
219 if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size,
220 &abbrev_offset, IDX_debug_abbrev, 0))
224 address_size = *bytes++;
226 /* Extra fields, signature/id and type offset/padding. */
229 && (unit_type == DW_UT_skeleton || unit_type == DW_UT_split_compile
230 || unit_type == DW_UT_type || unit_type == DW_UT_split_type)))
232 sig_id = read_8ubyte_unaligned_inc (dwarf, bytes);
235 || unit_type == DW_UT_type || unit_type == DW_UT_split_type))
237 if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size,
238 &subdie_off, sec_idx, 0))
241 /* Validate that the TYPE_OFFSET points past the header. */
242 if (unlikely (subdie_off < (size_t) (bytes - (data + off))))
248 if (unit_id8p != NULL)
251 if (subdie_offsetp != NULL)
252 *subdie_offsetp = subdie_off;
254 /* Store the header length. This is really how much we have read
255 from the header. If we didn't recognize the unit type the
256 header might actually be bigger. */
257 if (header_sizep != NULL)
258 *header_sizep = bytes - (data + off);
260 if (versionp != NULL)
263 if (unit_typep != NULL)
264 *unit_typep = unit_type;
266 if (abbrev_offsetp != NULL)
267 *abbrev_offsetp = abbrev_offset;
269 if (address_sizep != NULL)
270 *address_sizep = address_size;
272 /* Store the offset size. */
273 if (offset_sizep != NULL)
274 *offset_sizep = offset_size;
276 /* The length of the unit doesn't include the length field itself.
277 The length field is either, with offset == 4: 2 * 4 - 4 == 4,
278 or with offset == 8: 2 * 8 - 4 == 12. */
279 *next_off = off + 2 * offset_size - 4 + length;
281 /* This means that the length field is bogus, but return the CU anyway.
282 We just won't return anything after this. */
283 if (*next_off <= off)
284 *next_off = (Dwarf_Off) -1;
290 dwarf_nextcu (Dwarf *dwarf, Dwarf_Off off, Dwarf_Off *next_off,
291 size_t *header_sizep, Dwarf_Off *abbrev_offsetp,
292 uint8_t *address_sizep, uint8_t *offset_sizep)
294 return INTUSE(dwarf_next_unit) (dwarf, off, next_off, header_sizep, NULL,
295 abbrev_offsetp, address_sizep, offset_sizep,