blob: 5d9c118473a8a517fef65fd517838966f37d3754 [file] [log] [blame]
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00001//===-- DWARFFormValue.cpp ------------------------------------------------===//
2//
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"
12#include "llvm/ADT/StringRef.h"
Zachary Turner82af9432015-01-30 18:07:45 +000013#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
14#include "llvm/DebugInfo/DWARF/DWARFContext.h"
15#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eric Christopher7c678de2012-11-07 23:22:07 +000016#include "llvm/Support/Debug.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000017#include "llvm/Support/Dwarf.h"
18#include "llvm/Support/Format.h"
19#include "llvm/Support/raw_ostream.h"
20#include <cassert>
Matt Arsenault45cbfa52015-10-21 21:10:12 +000021#include <limits>
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000022using namespace llvm;
23using namespace dwarf;
Adrian Prantl0c36a752015-01-06 16:50:25 +000024using namespace syntax;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000025
Benjamin Kramerc86fdf12013-04-11 11:36:36 +000026namespace {
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000027uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
28 // FIXME: Support DWARF64.
29 return (Version == 2) ? AddrSize : 4;
Benjamin Kramerc86fdf12013-04-11 11:36:36 +000030}
Alexey Samsonovd60859b2013-04-09 14:09:42 +000031
32template <uint8_t AddrSize, uint8_t RefAddrSize>
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000033ArrayRef<uint8_t> makeFixedFormSizesArrayRef() {
34 static const uint8_t sizes[] = {
35 0, // 0x00 unused
36 AddrSize, // 0x01 DW_FORM_addr
37 0, // 0x02 unused
38 0, // 0x03 DW_FORM_block2
39 0, // 0x04 DW_FORM_block4
40 2, // 0x05 DW_FORM_data2
41 4, // 0x06 DW_FORM_data4
42 8, // 0x07 DW_FORM_data8
43 0, // 0x08 DW_FORM_string
44 0, // 0x09 DW_FORM_block
45 0, // 0x0a DW_FORM_block1
46 1, // 0x0b DW_FORM_data1
47 1, // 0x0c DW_FORM_flag
48 0, // 0x0d DW_FORM_sdata
49 4, // 0x0e DW_FORM_strp
50 0, // 0x0f DW_FORM_udata
51 RefAddrSize, // 0x10 DW_FORM_ref_addr
52 1, // 0x11 DW_FORM_ref1
53 2, // 0x12 DW_FORM_ref2
54 4, // 0x13 DW_FORM_ref4
55 8, // 0x14 DW_FORM_ref8
56 0, // 0x15 DW_FORM_ref_udata
57 0, // 0x16 DW_FORM_indirect
58 4, // 0x17 DW_FORM_sec_offset
59 0, // 0x18 DW_FORM_exprloc
60 0, // 0x19 DW_FORM_flag_present
61 };
62 return makeArrayRef(sizes);
63}
Alexander Kornienkof00654e2015-06-23 09:49:53 +000064}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000065
Alexey Samsonova56bbf02013-10-28 23:41:49 +000066ArrayRef<uint8_t> DWARFFormValue::getFixedFormSizes(uint8_t AddrSize,
67 uint16_t Version) {
Alexey Samsonovd60859b2013-04-09 14:09:42 +000068 uint8_t RefAddrSize = getRefAddrSize(AddrSize, Version);
69 if (AddrSize == 4 && RefAddrSize == 4)
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000070 return makeFixedFormSizesArrayRef<4, 4>();
Alexey Samsonovd60859b2013-04-09 14:09:42 +000071 if (AddrSize == 4 && RefAddrSize == 8)
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000072 return makeFixedFormSizesArrayRef<4, 8>();
Alexey Samsonovd60859b2013-04-09 14:09:42 +000073 if (AddrSize == 8 && RefAddrSize == 4)
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000074 return makeFixedFormSizesArrayRef<8, 4>();
Alexey Samsonovd60859b2013-04-09 14:09:42 +000075 if (AddrSize == 8 && RefAddrSize == 8)
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000076 return makeFixedFormSizesArrayRef<8, 8>();
Alexey Samsonova56bbf02013-10-28 23:41:49 +000077 return None;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000078}
79
Alexey Samsonov48cbda52013-10-28 23:01:48 +000080static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
81 DWARFFormValue::FC_Unknown, // 0x0
82 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
83 DWARFFormValue::FC_Unknown, // 0x02 unused
84 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
85 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
86 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
87 // --- These can be FC_SectionOffset in DWARF3 and below:
88 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
89 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
90 // ---
91 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
92 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
93 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
94 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
95 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
96 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
97 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
98 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
99 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
100 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
101 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
102 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
103 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
104 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
105 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
106 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
107 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
108 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000109};
110
111bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
112 // First, check DWARF4 form classes.
Craig Topper0013be12015-09-21 05:32:41 +0000113 if (Form < makeArrayRef(DWARF4FormClasses).size() &&
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000114 DWARF4FormClasses[Form] == FC)
115 return true;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000116 // Check more forms from DWARF4 and DWARF5 proposals.
117 switch (Form) {
118 case DW_FORM_ref_sig8:
119 case DW_FORM_GNU_ref_alt:
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000120 return (FC == FC_Reference);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000121 case DW_FORM_GNU_addr_index:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000122 return (FC == FC_Address);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000123 case DW_FORM_GNU_str_index:
124 case DW_FORM_GNU_strp_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000125 return (FC == FC_String);
Greg Clayton6c273762016-10-27 16:32:04 +0000126 default:
127 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000128 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000129 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
130 // Don't check for DWARF version here, as some producers may still do this
131 // by mistake.
Benjamin Kramer68a29562015-05-25 13:28:03 +0000132 return (Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
133 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000134}
135
Greg Claytoncddab272016-10-31 16:46:02 +0000136bool DWARFFormValue::extractValue(const DataExtractor &data,
137 uint32_t *offset_ptr,
David Blaikie07e22442013-09-23 22:44:40 +0000138 const DWARFUnit *cu) {
Greg Claytoncddab272016-10-31 16:46:02 +0000139 U = cu;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000140 bool indirect = false;
141 bool is_block = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000142 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000143 // Read the value for the form into value and follow and DW_FORM_indirect
144 // instances we run into
145 do {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000146 indirect = false;
147 switch (Form) {
148 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000149 case DW_FORM_ref_addr: {
Greg Claytoncddab272016-10-31 16:46:02 +0000150 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000151 return false;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000152 uint16_t AddrSize =
153 (Form == DW_FORM_addr)
Greg Claytoncddab272016-10-31 16:46:02 +0000154 ? U->getAddressByteSize()
155 : U->getRefAddrByteSize();
156 RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr);
157 if (AI != U->getRelocMap()->end()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000158 const std::pair<uint8_t, int64_t> &R = AI->second;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000159 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
Eric Christopher7370b552012-11-12 21:40:38 +0000160 } else
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000161 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000162 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000163 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000164 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000165 case DW_FORM_block:
166 Value.uval = data.getULEB128(offset_ptr);
167 is_block = true;
168 break;
169 case DW_FORM_block1:
170 Value.uval = data.getU8(offset_ptr);
171 is_block = true;
172 break;
173 case DW_FORM_block2:
174 Value.uval = data.getU16(offset_ptr);
175 is_block = true;
176 break;
177 case DW_FORM_block4:
178 Value.uval = data.getU32(offset_ptr);
179 is_block = true;
180 break;
181 case DW_FORM_data1:
182 case DW_FORM_ref1:
183 case DW_FORM_flag:
184 Value.uval = data.getU8(offset_ptr);
185 break;
186 case DW_FORM_data2:
187 case DW_FORM_ref2:
188 Value.uval = data.getU16(offset_ptr);
189 break;
190 case DW_FORM_data4:
David Blaikie18e73502013-06-19 21:37:13 +0000191 case DW_FORM_ref4: {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000192 Value.uval = data.getU32(offset_ptr);
Greg Claytoncddab272016-10-31 16:46:02 +0000193 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000194 break;
Greg Claytoncddab272016-10-31 16:46:02 +0000195 RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr-4);
196 if (AI != U->getRelocMap()->end())
David Blaikie18e73502013-06-19 21:37:13 +0000197 Value.uval += AI->second.second;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000198 break;
David Blaikie18e73502013-06-19 21:37:13 +0000199 }
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000200 case DW_FORM_data8:
201 case DW_FORM_ref8:
202 Value.uval = data.getU64(offset_ptr);
203 break;
204 case DW_FORM_sdata:
205 Value.sval = data.getSLEB128(offset_ptr);
206 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000207 case DW_FORM_udata:
208 case DW_FORM_ref_udata:
209 Value.uval = data.getULEB128(offset_ptr);
210 break;
211 case DW_FORM_string:
212 Value.cstr = data.getCStr(offset_ptr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000213 break;
214 case DW_FORM_indirect:
Greg Clayton6c273762016-10-27 16:32:04 +0000215 Form = static_cast<dwarf::Form>(data.getULEB128(offset_ptr));
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000216 indirect = true;
217 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000218 case DW_FORM_sec_offset:
219 case DW_FORM_strp:
220 case DW_FORM_GNU_ref_alt:
221 case DW_FORM_GNU_strp_alt: {
Eric Christopher18266172013-01-17 02:59:59 +0000222 // FIXME: This is 64-bit for DWARF64.
Frederic Risse4576d22014-11-12 23:48:04 +0000223 Value.uval = data.getU32(offset_ptr);
Greg Claytoncddab272016-10-31 16:46:02 +0000224 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000225 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000226 RelocAddrMap::const_iterator AI =
Greg Claytoncddab272016-10-31 16:46:02 +0000227 U->getRelocMap()->find(*offset_ptr - 4);
228 if (AI != U->getRelocMap()->end())
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000229 Value.uval += AI->second.second;
Eric Christopherd999bb72012-08-24 01:14:23 +0000230 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000231 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000232 case DW_FORM_flag_present:
233 Value.uval = 1;
234 break;
235 case DW_FORM_ref_sig8:
236 Value.uval = data.getU64(offset_ptr);
237 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000238 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000239 case DW_FORM_GNU_str_index:
240 Value.uval = data.getULEB128(offset_ptr);
241 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000242 default:
243 return false;
244 }
Benjamin Kramereaa74332011-09-13 21:47:32 +0000245 } while (indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000246
Benjamin Kramereaa74332011-09-13 21:47:32 +0000247 if (is_block) {
248 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000249 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000250 if (!str.empty()) {
251 Value.data = reinterpret_cast<const uint8_t *>(str.data());
252 *offset_ptr += Value.uval;
253 }
254 }
255
256 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000257}
258
259bool
260DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
Greg Claytoncddab272016-10-31 16:46:02 +0000261 const DWARFUnit *U) const {
262 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000263}
264
265bool
Greg Clayton6c273762016-10-27 16:32:04 +0000266DWARFFormValue::skipValue(dwarf::Form form, DataExtractor debug_info_data,
David Blaikie07e22442013-09-23 22:44:40 +0000267 uint32_t *offset_ptr, const DWARFUnit *cu) {
David Blaikiead07b5d2015-12-04 17:20:04 +0000268 return skipValue(form, debug_info_data, offset_ptr, cu->getVersion(),
269 cu->getAddressByteSize());
270}
Greg Clayton6c273762016-10-27 16:32:04 +0000271bool
272DWARFFormValue::skipValue(dwarf::Form form, DataExtractor debug_info_data,
273 uint32_t *offset_ptr, uint16_t Version,
274 uint8_t AddrSize) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000275 bool indirect = false;
276 do {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000277 switch (form) {
278 // Blocks if inlined data that have a length field and the data bytes
279 // inlined in the .debug_info
Eric Christopherd999bb72012-08-24 01:14:23 +0000280 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000281 case DW_FORM_block: {
282 uint64_t size = debug_info_data.getULEB128(offset_ptr);
283 *offset_ptr += size;
284 return true;
285 }
286 case DW_FORM_block1: {
287 uint8_t size = debug_info_data.getU8(offset_ptr);
288 *offset_ptr += size;
289 return true;
290 }
291 case DW_FORM_block2: {
292 uint16_t size = debug_info_data.getU16(offset_ptr);
293 *offset_ptr += size;
294 return true;
295 }
296 case DW_FORM_block4: {
297 uint32_t size = debug_info_data.getU32(offset_ptr);
298 *offset_ptr += size;
299 return true;
300 }
301
302 // Inlined NULL terminated C-strings
303 case DW_FORM_string:
304 debug_info_data.getCStr(offset_ptr);
305 return true;
306
307 // Compile unit address sized values
308 case DW_FORM_addr:
David Blaikiead07b5d2015-12-04 17:20:04 +0000309 *offset_ptr += AddrSize;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000310 return true;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000311 case DW_FORM_ref_addr:
David Blaikiead07b5d2015-12-04 17:20:04 +0000312 *offset_ptr += getRefAddrSize(AddrSize, Version);
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000313 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000314
Eric Christopherd999bb72012-08-24 01:14:23 +0000315 // 0 byte values - implied from the form.
316 case DW_FORM_flag_present:
317 return true;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000318
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000319 // 1 byte values
320 case DW_FORM_data1:
321 case DW_FORM_flag:
322 case DW_FORM_ref1:
323 *offset_ptr += 1;
324 return true;
325
326 // 2 byte values
327 case DW_FORM_data2:
328 case DW_FORM_ref2:
329 *offset_ptr += 2;
330 return true;
331
332 // 4 byte values
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000333 case DW_FORM_data4:
334 case DW_FORM_ref4:
335 *offset_ptr += 4;
336 return true;
337
338 // 8 byte values
339 case DW_FORM_data8:
340 case DW_FORM_ref8:
Eric Christopherd999bb72012-08-24 01:14:23 +0000341 case DW_FORM_ref_sig8:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000342 *offset_ptr += 8;
343 return true;
344
345 // signed or unsigned LEB 128 values
346 // case DW_FORM_APPLE_db_str:
347 case DW_FORM_sdata:
348 case DW_FORM_udata:
349 case DW_FORM_ref_udata:
Eric Christopher2cbd5762013-01-07 19:32:41 +0000350 case DW_FORM_GNU_str_index:
351 case DW_FORM_GNU_addr_index:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000352 debug_info_data.getULEB128(offset_ptr);
353 return true;
354
355 case DW_FORM_indirect:
356 indirect = true;
Greg Clayton6c273762016-10-27 16:32:04 +0000357 form = static_cast<dwarf::Form>(debug_info_data.getULEB128(offset_ptr));
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000358 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000359
Eric Christopher4c7765f2013-01-17 03:00:04 +0000360 // FIXME: 4 for DWARF32, 8 for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000361 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000362 case DW_FORM_strp:
363 case DW_FORM_GNU_ref_alt:
364 case DW_FORM_GNU_strp_alt:
Eric Christopher4c7765f2013-01-17 03:00:04 +0000365 *offset_ptr += 4;
Eric Christopherd999bb72012-08-24 01:14:23 +0000366 return true;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000367
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000368 default:
369 return false;
370 }
371 } while (indirect);
372 return true;
373}
374
375void
Greg Claytoncddab272016-10-31 16:46:02 +0000376DWARFFormValue::dump(raw_ostream &OS) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000377 uint64_t uvalue = Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000378 bool cu_relative_offset = false;
379
Benjamin Kramereaa74332011-09-13 21:47:32 +0000380 switch (Form) {
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000381 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher962c9082013-01-15 23:56:56 +0000382 case DW_FORM_GNU_addr_index: {
Eric Christopher962c9082013-01-15 23:56:56 +0000383 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000384 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000385 if (U == nullptr)
386 OS << "<invalid dwarf unit>";
387 else if (U->getAddrOffsetSectionItem(uvalue, Address))
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000388 OS << format("0x%016" PRIx64, Address);
389 else
Eric Christopher962c9082013-01-15 23:56:56 +0000390 OS << "<no .debug_addr section>";
391 break;
392 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000393 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000394 case DW_FORM_flag:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000395 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
396 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
397 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000398 case DW_FORM_ref_sig8:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000399 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000400 case DW_FORM_string:
401 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000402 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000403 OS << '"';
404 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000405 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000406 case DW_FORM_block:
407 case DW_FORM_block1:
408 case DW_FORM_block2:
409 case DW_FORM_block4:
410 if (uvalue > 0) {
411 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000412 case DW_FORM_exprloc:
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000413 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000414 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
415 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
416 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
417 default: break;
418 }
419
420 const uint8_t* data_ptr = Value.data;
421 if (data_ptr) {
422 // uvalue contains size of block
423 const uint8_t* end_data_ptr = data_ptr + uvalue;
424 while (data_ptr < end_data_ptr) {
425 OS << format("%2.2x ", *data_ptr);
426 ++data_ptr;
427 }
428 }
429 else
430 OS << "NULL";
431 }
432 break;
433
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000434 case DW_FORM_sdata: OS << Value.sval; break;
435 case DW_FORM_udata: OS << Value.uval; break;
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000436 case DW_FORM_strp: {
437 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000438 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000439 break;
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000440 }
Eric Christopher2cbd5762013-01-07 19:32:41 +0000441 case DW_FORM_GNU_str_index: {
442 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000443 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000444 break;
445 }
446 case DW_FORM_GNU_strp_alt: {
447 OS << format("alt indirect string, offset: 0x%" PRIx64 "", uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000448 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000449 break;
450 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000451 case DW_FORM_ref_addr:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000452 OS << format("0x%016" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000453 break;
454 case DW_FORM_ref1:
455 cu_relative_offset = true;
456 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
457 break;
458 case DW_FORM_ref2:
459 cu_relative_offset = true;
460 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
461 break;
462 case DW_FORM_ref4:
463 cu_relative_offset = true;
464 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
465 break;
466 case DW_FORM_ref8:
467 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000468 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000469 break;
470 case DW_FORM_ref_udata:
471 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000472 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000473 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000474 case DW_FORM_GNU_ref_alt:
475 OS << format("<alt 0x%" PRIx64 ">", uvalue);
476 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000477
478 // All DW_FORM_indirect attributes should be resolved prior to calling
479 // this function
480 case DW_FORM_indirect:
481 OS << "DW_FORM_indirect";
482 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000483
Eric Christopher4c7765f2013-01-17 03:00:04 +0000484 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000485 case DW_FORM_sec_offset:
Eric Christopher4c7765f2013-01-17 03:00:04 +0000486 OS << format("0x%08x", (uint32_t)uvalue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000487 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000488
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000489 default:
490 OS << format("DW_FORM(0x%4.4x)", Form);
491 break;
492 }
493
Adrian Prantl0c36a752015-01-06 16:50:25 +0000494 if (cu_relative_offset) {
495 OS << " => {";
496 WithColor(OS, syntax::Address).get()
Greg Claytoncddab272016-10-31 16:46:02 +0000497 << format("0x%8.8" PRIx64, uvalue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000498 OS << "}";
499 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000500}
501
Greg Claytoncddab272016-10-31 16:46:02 +0000502void DWARFFormValue::dumpString(raw_ostream &OS) const {
503 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000504 if (DbgStr.hasValue()) {
505 raw_ostream &COS = WithColor(OS, syntax::String);
506 COS << '"';
507 COS.write_escaped(DbgStr.getValue());
508 COS << '"';
509 }
510}
511
Greg Claytoncddab272016-10-31 16:46:02 +0000512Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000513 if (!isFormClass(FC_String))
514 return None;
515 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000516 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000517 // FIXME: Add support for DW_FORM_GNU_strp_alt
518 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000519 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000520 uint32_t Offset = Value.uval;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000521 if (Form == DW_FORM_GNU_str_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000522 uint32_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000523 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
524 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000525 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000526 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000527 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
528 return Str;
529 }
530 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000531}
532
Greg Claytoncddab272016-10-31 16:46:02 +0000533Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000534 if (!isFormClass(FC_Address))
535 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000536 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000537 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000538 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000539 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000540 return None;
541 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000542 }
543 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000544}
545
Greg Claytoncddab272016-10-31 16:46:02 +0000546Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000547 if (!isFormClass(FC_Reference))
548 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000549 switch (Form) {
550 case DW_FORM_ref1:
551 case DW_FORM_ref2:
552 case DW_FORM_ref4:
553 case DW_FORM_ref8:
554 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000555 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000556 return None;
557 return Value.uval + U->getOffset();
558 case DW_FORM_ref_addr:
559 return Value.uval;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000560 // FIXME: Add proper support for DW_FORM_ref_sig8 and DW_FORM_GNU_ref_alt.
Benjamin Kramereaa74332011-09-13 21:47:32 +0000561 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000562 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000563 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000564}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000565
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000566Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
567 if (!isFormClass(FC_SectionOffset))
568 return None;
569 return Value.uval;
570}
571
572Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000573 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
574 || Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000575 return None;
576 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000577}
Frederic Rissf3549a22014-09-04 06:14:35 +0000578
Frederic Riss77f850e2015-03-04 22:07:41 +0000579Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
580 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Matt Arsenault45cbfa52015-10-21 21:10:12 +0000581 (Form == DW_FORM_udata && uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000582 return None;
583 switch (Form) {
584 case DW_FORM_data4:
585 return int32_t(Value.uval);
586 case DW_FORM_data2:
587 return int16_t(Value.uval);
588 case DW_FORM_data1:
589 return int8_t(Value.uval);
590 case DW_FORM_sdata:
591 case DW_FORM_data8:
592 default:
593 return Value.sval;
594 }
595}
596
Frederic Risseab17772014-09-04 06:35:09 +0000597Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000598 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
599 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000600 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000601}
602