blob: 7f827de89240c46d6fa17b96029c26d585a15af0 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFFormValue.cpp -------------------------------------------------===//
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Adrian Prantl0c36a752015-01-06 16:50:25 +000010#include "SyntaxHighlighting.h"
Alexey Samsonov48cbda52013-10-28 23:01:48 +000011#include "llvm/ADT/ArrayRef.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000012#include "llvm/ADT/None.h"
13#include "llvm/ADT/Optional.h"
Alexey Samsonov48cbda52013-10-28 23:01:48 +000014#include "llvm/ADT/StringRef.h"
Zachary Turner82af9432015-01-30 18:07:45 +000015#include "llvm/DebugInfo/DWARF/DWARFContext.h"
16#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000017#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000019#include "llvm/Support/Dwarf.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000020#include "llvm/Support/ErrorHandling.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000021#include "llvm/Support/Format.h"
22#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000023#include <cinttypes>
24#include <cstdint>
Matt Arsenault45cbfa52015-10-21 21:10:12 +000025#include <limits>
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000026
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000027using namespace llvm;
28using namespace dwarf;
Adrian Prantl0c36a752015-01-06 16:50:25 +000029using namespace syntax;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000030
Alexey Samsonov48cbda52013-10-28 23:01:48 +000031static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
32 DWARFFormValue::FC_Unknown, // 0x0
33 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
34 DWARFFormValue::FC_Unknown, // 0x02 unused
35 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
36 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
37 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
38 // --- These can be FC_SectionOffset in DWARF3 and below:
39 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
40 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
41 // ---
42 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
43 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
44 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
45 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
46 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
47 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
48 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
49 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
50 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
51 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
52 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
53 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
54 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
55 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
56 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
57 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
58 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
59 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
Alexey Samsonov48cbda52013-10-28 23:01:48 +000060};
61
Greg Clayton82f12b12016-11-11 16:21:37 +000062namespace {
63
64/// A helper class that can be used in DWARFFormValue.cpp functions that need
65/// to know the byte size of DW_FORM values that vary in size depending on the
66/// DWARF version, address byte size, or DWARF32 or DWARF64.
67class FormSizeHelper {
68 uint16_t Version;
69 uint8_t AddrSize;
70 llvm::dwarf::DwarfFormat Format;
71
72public:
73 FormSizeHelper(uint16_t V, uint8_t A, llvm::dwarf::DwarfFormat F)
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000074 : Version(V), AddrSize(A), Format(F) {}
75
Greg Clayton82f12b12016-11-11 16:21:37 +000076 uint8_t getAddressByteSize() const { return AddrSize; }
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000077
Greg Clayton82f12b12016-11-11 16:21:37 +000078 uint8_t getRefAddrByteSize() const {
79 if (Version == 2)
80 return AddrSize;
81 return getDwarfOffsetByteSize();
82 }
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000083
Greg Clayton82f12b12016-11-11 16:21:37 +000084 uint8_t getDwarfOffsetByteSize() const {
85 switch (Format) {
86 case dwarf::DwarfFormat::DWARF32:
87 return 4;
88 case dwarf::DwarfFormat::DWARF64:
89 return 8;
90 }
91 llvm_unreachable("Invalid Format value");
92 }
93};
94
95} // end anonymous namespace
96
97template <class T>
98static Optional<uint8_t> getFixedByteSize(dwarf::Form Form, const T *U) {
99 switch (Form) {
100 case DW_FORM_addr:
101 if (U)
102 return U->getAddressByteSize();
103 return None;
104
105 case DW_FORM_block: // ULEB128 length L followed by L bytes.
106 case DW_FORM_block1: // 1 byte length L followed by L bytes.
107 case DW_FORM_block2: // 2 byte length L followed by L bytes.
108 case DW_FORM_block4: // 4 byte length L followed by L bytes.
109 case DW_FORM_string: // C-string with null terminator.
110 case DW_FORM_sdata: // SLEB128.
111 case DW_FORM_udata: // ULEB128.
112 case DW_FORM_ref_udata: // ULEB128.
113 case DW_FORM_indirect: // ULEB128.
114 case DW_FORM_exprloc: // ULEB128 length L followed by L bytes.
115 case DW_FORM_strx: // ULEB128.
116 case DW_FORM_addrx: // ULEB128.
117 case DW_FORM_loclistx: // ULEB128.
118 case DW_FORM_rnglistx: // ULEB128.
119 case DW_FORM_GNU_addr_index: // ULEB128.
120 case DW_FORM_GNU_str_index: // ULEB128.
121 return None;
122
123 case DW_FORM_ref_addr:
124 if (U)
125 return U->getRefAddrByteSize();
126 return None;
127
128 case DW_FORM_flag:
129 case DW_FORM_data1:
130 case DW_FORM_ref1:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000131 case DW_FORM_strx1:
132 case DW_FORM_addrx1:
Greg Clayton82f12b12016-11-11 16:21:37 +0000133 return 1;
134
135 case DW_FORM_data2:
136 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000137 case DW_FORM_strx2:
138 case DW_FORM_addrx2:
Greg Clayton82f12b12016-11-11 16:21:37 +0000139 return 2;
140
141 case DW_FORM_data4:
142 case DW_FORM_ref4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000143 case DW_FORM_ref_sup4:
144 case DW_FORM_strx4:
145 case DW_FORM_addrx4:
Greg Clayton82f12b12016-11-11 16:21:37 +0000146 return 4;
147
Greg Clayton04c19282016-11-11 17:38:14 +0000148 case DW_FORM_strp:
Greg Clayton82f12b12016-11-11 16:21:37 +0000149 case DW_FORM_GNU_ref_alt:
150 case DW_FORM_GNU_strp_alt:
151 case DW_FORM_line_strp:
152 case DW_FORM_sec_offset:
153 case DW_FORM_strp_sup:
Greg Clayton82f12b12016-11-11 16:21:37 +0000154 if (U)
155 return U->getDwarfOffsetByteSize();
156 return None;
157
158 case DW_FORM_data8:
159 case DW_FORM_ref8:
160 case DW_FORM_ref_sig8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000161 case DW_FORM_ref_sup8:
Greg Clayton82f12b12016-11-11 16:21:37 +0000162 return 8;
163
164 case DW_FORM_flag_present:
165 return 0;
166
167 case DW_FORM_data16:
168 return 16;
169
170 case DW_FORM_implicit_const:
Victor Leschukcbddae72017-01-10 21:18:26 +0000171 // The implicit value is stored in the abbreviation as a SLEB128, and
Greg Clayton82f12b12016-11-11 16:21:37 +0000172 // there no data in debug info.
173 return 0;
174
175 default:
176 llvm_unreachable("Handle this form in this switch statement");
177 }
178 return None;
179}
180
181template <class T>
182static bool skipFormValue(dwarf::Form Form, const DataExtractor &DebugInfoData,
183 uint32_t *OffsetPtr, const T *U) {
184 bool Indirect = false;
185 do {
186 switch (Form) {
Greg Clayton04c19282016-11-11 17:38:14 +0000187 // Blocks of inlined data that have a length field and the data bytes
Greg Clayton82f12b12016-11-11 16:21:37 +0000188 // inlined in the .debug_info.
189 case DW_FORM_exprloc:
190 case DW_FORM_block: {
191 uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
192 *OffsetPtr += size;
193 return true;
194 }
195 case DW_FORM_block1: {
196 uint8_t size = DebugInfoData.getU8(OffsetPtr);
197 *OffsetPtr += size;
198 return true;
199 }
200 case DW_FORM_block2: {
201 uint16_t size = DebugInfoData.getU16(OffsetPtr);
202 *OffsetPtr += size;
203 return true;
204 }
205 case DW_FORM_block4: {
206 uint32_t size = DebugInfoData.getU32(OffsetPtr);
207 *OffsetPtr += size;
208 return true;
209 }
210
211 // Inlined NULL terminated C-strings.
212 case DW_FORM_string:
213 DebugInfoData.getCStr(OffsetPtr);
214 return true;
215
216 case DW_FORM_addr:
217 case DW_FORM_ref_addr:
218 case DW_FORM_flag_present:
219 case DW_FORM_data1:
220 case DW_FORM_data2:
221 case DW_FORM_data4:
222 case DW_FORM_data8:
223 case DW_FORM_flag:
224 case DW_FORM_ref1:
225 case DW_FORM_ref2:
226 case DW_FORM_ref4:
227 case DW_FORM_ref8:
228 case DW_FORM_ref_sig8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000229 case DW_FORM_ref_sup4:
230 case DW_FORM_ref_sup8:
231 case DW_FORM_strx1:
232 case DW_FORM_strx2:
233 case DW_FORM_strx4:
234 case DW_FORM_addrx1:
235 case DW_FORM_addrx2:
236 case DW_FORM_addrx4:
Greg Clayton82f12b12016-11-11 16:21:37 +0000237 case DW_FORM_sec_offset:
238 case DW_FORM_strp:
Greg Clayton04c19282016-11-11 17:38:14 +0000239 case DW_FORM_strp_sup:
240 case DW_FORM_line_strp:
Greg Clayton82f12b12016-11-11 16:21:37 +0000241 case DW_FORM_GNU_ref_alt:
242 case DW_FORM_GNU_strp_alt:
243 if (Optional<uint8_t> FixedSize = ::getFixedByteSize(Form, U)) {
244 *OffsetPtr += *FixedSize;
245 return true;
246 }
247 return false;
248
249 // signed or unsigned LEB 128 values.
250 case DW_FORM_sdata:
251 DebugInfoData.getSLEB128(OffsetPtr);
252 return true;
253
254 case DW_FORM_udata:
255 case DW_FORM_ref_udata:
256 case DW_FORM_strx:
257 case DW_FORM_addrx:
258 case DW_FORM_loclistx:
259 case DW_FORM_rnglistx:
260 case DW_FORM_GNU_addr_index:
261 case DW_FORM_GNU_str_index:
262 DebugInfoData.getULEB128(OffsetPtr);
263 return true;
264
265 case DW_FORM_indirect:
266 Indirect = true;
267 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
268 break;
269
270 default:
271 return false;
272 }
273 } while (Indirect);
274 return true;
275}
276
277Optional<uint8_t> DWARFFormValue::getFixedByteSize(dwarf::Form Form,
278 const DWARFUnit *U) {
279 return ::getFixedByteSize(Form, U);
280}
281
282Optional<uint8_t>
283DWARFFormValue::getFixedByteSize(dwarf::Form Form, uint16_t Version,
284 uint8_t AddrSize,
285 llvm::dwarf::DwarfFormat Format) {
286 FormSizeHelper FSH(Version, AddrSize, Format);
287 return ::getFixedByteSize(Form, &FSH);
288}
289
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000290bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
291 // First, check DWARF4 form classes.
Craig Topper0013be12015-09-21 05:32:41 +0000292 if (Form < makeArrayRef(DWARF4FormClasses).size() &&
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000293 DWARF4FormClasses[Form] == FC)
294 return true;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000295 // Check more forms from DWARF4 and DWARF5 proposals.
296 switch (Form) {
297 case DW_FORM_ref_sig8:
298 case DW_FORM_GNU_ref_alt:
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000299 return (FC == FC_Reference);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000300 case DW_FORM_GNU_addr_index:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000301 return (FC == FC_Address);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000302 case DW_FORM_GNU_str_index:
303 case DW_FORM_GNU_strp_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000304 return (FC == FC_String);
Victor Leschukcbddae72017-01-10 21:18:26 +0000305 case DW_FORM_implicit_const:
306 return (FC == FC_Constant);
Greg Clayton6c273762016-10-27 16:32:04 +0000307 default:
308 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000309 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000310 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
311 // Don't check for DWARF version here, as some producers may still do this
Greg Clayton48432cf2017-05-01 22:07:02 +0000312 // by mistake. Also accept DW_FORM_strp since this is .debug_str section
313 // offset.
314 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
315 Form == DW_FORM_strp) &&
Benjamin Kramer68a29562015-05-25 13:28:03 +0000316 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000317}
318
Greg Claytoncddab272016-10-31 16:46:02 +0000319bool DWARFFormValue::extractValue(const DataExtractor &data,
320 uint32_t *offset_ptr,
David Blaikie07e22442013-09-23 22:44:40 +0000321 const DWARFUnit *cu) {
Greg Claytoncddab272016-10-31 16:46:02 +0000322 U = cu;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000323 bool indirect = false;
324 bool is_block = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000325 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000326 // Read the value for the form into value and follow and DW_FORM_indirect
327 // instances we run into
328 do {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000329 indirect = false;
330 switch (Form) {
331 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000332 case DW_FORM_ref_addr: {
Greg Claytoncddab272016-10-31 16:46:02 +0000333 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000334 return false;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000335 uint16_t AddrSize =
336 (Form == DW_FORM_addr)
Greg Claytoncddab272016-10-31 16:46:02 +0000337 ? U->getAddressByteSize()
338 : U->getRefAddrByteSize();
George Rimarf8a96422017-04-21 09:12:18 +0000339 Value.uval =
340 getRelocatedValue(data, AddrSize, offset_ptr, U->getRelocMap());
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000341 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000342 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000343 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000344 case DW_FORM_block:
345 Value.uval = data.getULEB128(offset_ptr);
346 is_block = true;
347 break;
348 case DW_FORM_block1:
349 Value.uval = data.getU8(offset_ptr);
350 is_block = true;
351 break;
352 case DW_FORM_block2:
353 Value.uval = data.getU16(offset_ptr);
354 is_block = true;
355 break;
356 case DW_FORM_block4:
357 Value.uval = data.getU32(offset_ptr);
358 is_block = true;
359 break;
360 case DW_FORM_data1:
361 case DW_FORM_ref1:
362 case DW_FORM_flag:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000363 case DW_FORM_strx1:
364 case DW_FORM_addrx1:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000365 Value.uval = data.getU8(offset_ptr);
366 break;
367 case DW_FORM_data2:
368 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000369 case DW_FORM_strx2:
370 case DW_FORM_addrx2:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000371 Value.uval = data.getU16(offset_ptr);
372 break;
373 case DW_FORM_data4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000374 case DW_FORM_ref4:
375 case DW_FORM_ref_sup4:
376 case DW_FORM_strx4:
377 case DW_FORM_addrx4: {
George Rimarf8a96422017-04-21 09:12:18 +0000378 const RelocAddrMap* RelocMap = U ? U->getRelocMap() : nullptr;
379 Value.uval = getRelocatedValue(data, 4, offset_ptr, RelocMap);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000380 break;
David Blaikie18e73502013-06-19 21:37:13 +0000381 }
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000382 case DW_FORM_data8:
383 case DW_FORM_ref8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000384 case DW_FORM_ref_sup8:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000385 Value.uval = data.getU64(offset_ptr);
386 break;
387 case DW_FORM_sdata:
388 Value.sval = data.getSLEB128(offset_ptr);
389 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000390 case DW_FORM_udata:
391 case DW_FORM_ref_udata:
392 Value.uval = data.getULEB128(offset_ptr);
393 break;
394 case DW_FORM_string:
395 Value.cstr = data.getCStr(offset_ptr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000396 break;
397 case DW_FORM_indirect:
Greg Clayton6c273762016-10-27 16:32:04 +0000398 Form = static_cast<dwarf::Form>(data.getULEB128(offset_ptr));
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000399 indirect = true;
400 break;
Greg Clayton04c19282016-11-11 17:38:14 +0000401 case DW_FORM_strp:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000402 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000403 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000404 case DW_FORM_GNU_strp_alt:
405 case DW_FORM_line_strp:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000406 case DW_FORM_strp_sup: {
Greg Claytoncddab272016-10-31 16:46:02 +0000407 if (!U)
Greg Clayton82f12b12016-11-11 16:21:37 +0000408 return false;
George Rimarf8a96422017-04-21 09:12:18 +0000409 Value.uval = getRelocatedValue(data, U->getDwarfOffsetByteSize(),
410 offset_ptr, U->getRelocMap());
Eric Christopherd999bb72012-08-24 01:14:23 +0000411 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000412 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000413 case DW_FORM_flag_present:
414 Value.uval = 1;
415 break;
416 case DW_FORM_ref_sig8:
417 Value.uval = data.getU64(offset_ptr);
418 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000419 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000420 case DW_FORM_GNU_str_index:
421 Value.uval = data.getULEB128(offset_ptr);
422 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000423 default:
Greg Clayton0e62ee72017-01-13 00:13:42 +0000424 // DWARFFormValue::skipValue() will have caught this and caused all
425 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
426 llvm_unreachable("unsupported form");
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000427 }
Benjamin Kramereaa74332011-09-13 21:47:32 +0000428 } while (indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000429
Benjamin Kramereaa74332011-09-13 21:47:32 +0000430 if (is_block) {
431 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000432 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000433 if (!str.empty()) {
434 Value.data = reinterpret_cast<const uint8_t *>(str.data());
435 *offset_ptr += Value.uval;
436 }
437 }
438
439 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000440}
441
Greg Clayton82f12b12016-11-11 16:21:37 +0000442bool DWARFFormValue::skipValue(DataExtractor DebugInfoData,
443 uint32_t *offset_ptr, const DWARFUnit *U) const {
444 return DWARFFormValue::skipValue(Form, DebugInfoData, offset_ptr, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000445}
446
Greg Clayton82f12b12016-11-11 16:21:37 +0000447bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
448 uint32_t *offset_ptr, const DWARFUnit *U) {
449 return skipFormValue(form, DebugInfoData, offset_ptr, U);
David Blaikiead07b5d2015-12-04 17:20:04 +0000450}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000451
Greg Clayton82f12b12016-11-11 16:21:37 +0000452bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
453 uint32_t *offset_ptr, uint16_t Version,
454 uint8_t AddrSize,
455 llvm::dwarf::DwarfFormat Format) {
456 FormSizeHelper FSH(Version, AddrSize, Format);
457 return skipFormValue(form, DebugInfoData, offset_ptr, &FSH);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000458}
459
460void
Greg Claytoncddab272016-10-31 16:46:02 +0000461DWARFFormValue::dump(raw_ostream &OS) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000462 uint64_t uvalue = Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000463 bool cu_relative_offset = false;
464
Benjamin Kramereaa74332011-09-13 21:47:32 +0000465 switch (Form) {
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000466 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher962c9082013-01-15 23:56:56 +0000467 case DW_FORM_GNU_addr_index: {
Eric Christopher962c9082013-01-15 23:56:56 +0000468 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000469 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000470 if (U == nullptr)
471 OS << "<invalid dwarf unit>";
472 else if (U->getAddrOffsetSectionItem(uvalue, Address))
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000473 OS << format("0x%016" PRIx64, Address);
474 else
Eric Christopher962c9082013-01-15 23:56:56 +0000475 OS << "<no .debug_addr section>";
476 break;
477 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000478 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000479 case DW_FORM_flag:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000480 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
481 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
482 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000483 case DW_FORM_ref_sig8:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000484 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000485 case DW_FORM_string:
486 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000487 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000488 OS << '"';
489 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000490 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000491 case DW_FORM_block:
492 case DW_FORM_block1:
493 case DW_FORM_block2:
494 case DW_FORM_block4:
495 if (uvalue > 0) {
496 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000497 case DW_FORM_exprloc:
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000498 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000499 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
500 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
501 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
502 default: break;
503 }
504
505 const uint8_t* data_ptr = Value.data;
506 if (data_ptr) {
507 // uvalue contains size of block
508 const uint8_t* end_data_ptr = data_ptr + uvalue;
509 while (data_ptr < end_data_ptr) {
510 OS << format("%2.2x ", *data_ptr);
511 ++data_ptr;
512 }
513 }
514 else
515 OS << "NULL";
516 }
517 break;
518
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000519 case DW_FORM_sdata: OS << Value.sval; break;
520 case DW_FORM_udata: OS << Value.uval; break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000521 case DW_FORM_strp:
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000522 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000523 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000524 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000525 case DW_FORM_GNU_str_index:
Eric Christopher2cbd5762013-01-07 19:32:41 +0000526 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000527 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000528 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000529 case DW_FORM_GNU_strp_alt:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000530 OS << format("alt indirect string, offset: 0x%" PRIx64 "", uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000531 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000532 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000533 case DW_FORM_ref_addr:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000534 OS << format("0x%016" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000535 break;
536 case DW_FORM_ref1:
537 cu_relative_offset = true;
538 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
539 break;
540 case DW_FORM_ref2:
541 cu_relative_offset = true;
542 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
543 break;
544 case DW_FORM_ref4:
545 cu_relative_offset = true;
546 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
547 break;
548 case DW_FORM_ref8:
549 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000550 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000551 break;
552 case DW_FORM_ref_udata:
553 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000554 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000555 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000556 case DW_FORM_GNU_ref_alt:
557 OS << format("<alt 0x%" PRIx64 ">", uvalue);
558 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000559
560 // All DW_FORM_indirect attributes should be resolved prior to calling
561 // this function
562 case DW_FORM_indirect:
563 OS << "DW_FORM_indirect";
564 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000565
Eric Christopher4c7765f2013-01-17 03:00:04 +0000566 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000567 case DW_FORM_sec_offset:
Eric Christopher4c7765f2013-01-17 03:00:04 +0000568 OS << format("0x%08x", (uint32_t)uvalue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000569 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000570
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000571 default:
572 OS << format("DW_FORM(0x%4.4x)", Form);
573 break;
574 }
575
Adrian Prantl0c36a752015-01-06 16:50:25 +0000576 if (cu_relative_offset) {
577 OS << " => {";
578 WithColor(OS, syntax::Address).get()
Greg Claytoncddab272016-10-31 16:46:02 +0000579 << format("0x%8.8" PRIx64, uvalue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000580 OS << "}";
581 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000582}
583
Greg Claytoncddab272016-10-31 16:46:02 +0000584void DWARFFormValue::dumpString(raw_ostream &OS) const {
585 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000586 if (DbgStr.hasValue()) {
587 raw_ostream &COS = WithColor(OS, syntax::String);
588 COS << '"';
589 COS.write_escaped(DbgStr.getValue());
590 COS << '"';
591 }
592}
593
Greg Claytoncddab272016-10-31 16:46:02 +0000594Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000595 if (!isFormClass(FC_String))
596 return None;
597 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000598 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000599 // FIXME: Add support for DW_FORM_GNU_strp_alt
600 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000601 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000602 uint32_t Offset = Value.uval;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000603 if (Form == DW_FORM_GNU_str_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000604 uint32_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000605 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
606 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000607 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000608 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000609 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
610 return Str;
611 }
612 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000613}
614
Greg Claytoncddab272016-10-31 16:46:02 +0000615Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000616 if (!isFormClass(FC_Address))
617 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000618 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000619 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000620 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000621 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000622 return None;
623 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000624 }
625 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000626}
627
Greg Claytoncddab272016-10-31 16:46:02 +0000628Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000629 if (!isFormClass(FC_Reference))
630 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000631 switch (Form) {
632 case DW_FORM_ref1:
633 case DW_FORM_ref2:
634 case DW_FORM_ref4:
635 case DW_FORM_ref8:
636 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000637 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000638 return None;
639 return Value.uval + U->getOffset();
640 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000641 case DW_FORM_ref_sig8:
642 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000643 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000644 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000645 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000646 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000647}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000648
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000649Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
650 if (!isFormClass(FC_SectionOffset))
651 return None;
652 return Value.uval;
653}
654
655Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000656 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
657 || Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000658 return None;
659 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000660}
Frederic Rissf3549a22014-09-04 06:14:35 +0000661
Frederic Riss77f850e2015-03-04 22:07:41 +0000662Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
663 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Matt Arsenault45cbfa52015-10-21 21:10:12 +0000664 (Form == DW_FORM_udata && uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000665 return None;
666 switch (Form) {
667 case DW_FORM_data4:
668 return int32_t(Value.uval);
669 case DW_FORM_data2:
670 return int16_t(Value.uval);
671 case DW_FORM_data1:
672 return int8_t(Value.uval);
673 case DW_FORM_sdata:
674 case DW_FORM_data8:
675 default:
676 return Value.sval;
677 }
678}
679
Frederic Risseab17772014-09-04 06:35:09 +0000680Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000681 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
682 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000683 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000684}
685
Chris Bienemane0e451d2016-12-22 22:44:27 +0000686Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
687 if (!isFormClass(FC_String) && Form == DW_FORM_string)
688 return None;
689 return Value.uval;
690}
691
692Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
693 if (!isFormClass(FC_Reference))
694 return None;
695 return Value.uval;
696}