blob: 875d86946d9ca3a25902ab3cb0ef0e8ce9f4a258 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Advance to next CU header.
Roland McGrath3e0f7d12010-06-15 23:10:35 -07002 Copyright (C) 2002-2010 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * 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
Ulrich Drepper361df7d2006-04-04 21:38:57 +000022 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
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <libdwP.h>
Ulrich Drepper35f08c42008-01-18 19:59:08 +000035#include <dwarf.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000036
37
38int
Roland McGrath3e0f7d12010-06-15 23:10:35 -070039dwarf_next_unit (dwarf, off, next_off, header_sizep, versionp, abbrev_offsetp,
40 address_sizep, offset_sizep, type_signaturep, type_offsetp)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000041 Dwarf *dwarf;
42 Dwarf_Off off;
43 Dwarf_Off *next_off;
44 size_t *header_sizep;
Roland McGrath2b1f0952010-06-20 17:55:50 -070045 Dwarf_Half *versionp;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000046 Dwarf_Off *abbrev_offsetp;
47 uint8_t *address_sizep;
48 uint8_t *offset_sizep;
Roland McGrath3e0f7d12010-06-15 23:10:35 -070049 uint64_t *type_signaturep;
50 Dwarf_Off *type_offsetp;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000051{
Roland McGrath3e0f7d12010-06-15 23:10:35 -070052 const bool debug_types = type_signaturep != NULL;
53 const size_t sec_idx = debug_types ? IDX_debug_types : IDX_debug_info;
54
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000055 /* 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
Roland McGrath3e0f7d12010-06-15 23:10:35 -070061 || unlikely (dwarf->sectiondata[sec_idx] == NULL)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000062 /* 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. */
Roland McGrath3e0f7d12010-06-15 23:10:35 -070065 || unlikely (off + 4 >= dwarf->sectiondata[sec_idx]->d_size))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000066 {
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. */
Roland McGrath3e0f7d12010-06-15 23:10:35 -070073 const unsigned char *data = dwarf->sectiondata[sec_idx]->d_buf;
Roland McGrath05c4e042009-06-18 13:56:02 -070074 const unsigned char *bytes = data + off;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000075
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
Ulrich Drepper35f08c42008-01-18 19:59:08 +000082 0xfffffff0); in the 64-bit DWARF format, this consists of the
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000083 4-byte value 0xffffffff followed by an 8-byte unsigned integer
Ulrich Drepper35f08c42008-01-18 19:59:08 +000084 that gives the actual length (see Section 7.2.2).
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000085
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;
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000103 /* 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)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000107 offset_size = 8;
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000108 else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
109 && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
110 {
Roland McGrath3e0f7d12010-06-15 23:10:35 -0700111 invalid:
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000112 __libdw_seterrno (DWARF_E_INVALID_DWARF);
113 return -1;
114 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000115
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000116 /* Now we know how large the header is. */
Roland McGrath3e0f7d12010-06-15 23:10:35 -0700117 if (unlikely (DIE_OFFSET_FROM_CU_OFFSET (off, offset_size, debug_types)
118 >= dwarf->sectiondata[sec_idx]->d_size))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000119 {
120 *next_off = -1;
121 return 1;
122 }
123
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000124 if (length == DWARF3_LENGTH_64_BIT)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000125 /* This is a 64-bit DWARF format. */
126 length = read_8ubyte_unaligned_inc (dwarf, bytes);
127
Roland McGrath3e0f7d12010-06-15 23:10:35 -0700128 /* Read the version stamp. Always a 16-bit value. */
129 uint_fast16_t version = read_2ubyte_unaligned_inc (dwarf, bytes);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000130
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;
Roland McGrath3e0f7d12010-06-15 23:10:35 -0700134 if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size,
Ulrich Drepper99d23722009-06-14 20:19:45 -0700135 &abbrev_offset, IDX_debug_abbrev, 0))
136 return -1;
137
Roland McGrath3e0f7d12010-06-15 23:10:35 -0700138 /* 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
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000166 if (abbrev_offsetp != NULL)
167 *abbrev_offsetp = abbrev_offset;
168
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000169 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
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000176 /* See definition of DIE_OFFSET_FROM_CU_OFFSET macro
177 for an explanation of the trick in this expression. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000178 *next_off = off + 2 * offset_size - 4 + length;
179
180 return 0;
181}
Roland McGrath3e0f7d12010-06-15 23:10:35 -0700182INTDEF(dwarf_next_unit)
183
184int
185dwarf_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}
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000199INTDEF(dwarf_nextcu)