blob: 75ca7622139db256437adf2d2d8f505d8856e376 [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>
Frederic Riss77f850e2015-03-04 22:07:41 +000021#include <climits>
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}
Alexey Samsonovd60859b2013-04-09 14:09:42 +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.
113 if (Form < ArrayRef<FormClass>(DWARF4FormClasses).size() &&
114 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);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000126 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000127 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
128 // Don't check for DWARF version here, as some producers may still do this
129 // by mistake.
130 if ((Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
131 FC == FC_SectionOffset)
132 return true;
133 return false;
134}
135
David Blaikie07e22442013-09-23 22:44:40 +0000136bool DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
137 const DWARFUnit *cu) {
Benjamin Kramereaa74332011-09-13 21:47:32 +0000138 bool indirect = false;
139 bool is_block = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000140 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000141 // Read the value for the form into value and follow and DW_FORM_indirect
142 // instances we run into
143 do {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000144 indirect = false;
145 switch (Form) {
146 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000147 case DW_FORM_ref_addr: {
Frederic Risse4576d22014-11-12 23:48:04 +0000148 if (!cu)
149 return false;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000150 uint16_t AddrSize =
151 (Form == DW_FORM_addr)
152 ? cu->getAddressByteSize()
153 : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
154 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
Eric Christopherda4b2192013-01-02 23:52:13 +0000155 if (AI != cu->getRelocMap()->end()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000156 const std::pair<uint8_t, int64_t> &R = AI->second;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000157 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
Eric Christopher7370b552012-11-12 21:40:38 +0000158 } else
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000159 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000160 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000161 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000162 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000163 case DW_FORM_block:
164 Value.uval = data.getULEB128(offset_ptr);
165 is_block = true;
166 break;
167 case DW_FORM_block1:
168 Value.uval = data.getU8(offset_ptr);
169 is_block = true;
170 break;
171 case DW_FORM_block2:
172 Value.uval = data.getU16(offset_ptr);
173 is_block = true;
174 break;
175 case DW_FORM_block4:
176 Value.uval = data.getU32(offset_ptr);
177 is_block = true;
178 break;
179 case DW_FORM_data1:
180 case DW_FORM_ref1:
181 case DW_FORM_flag:
182 Value.uval = data.getU8(offset_ptr);
183 break;
184 case DW_FORM_data2:
185 case DW_FORM_ref2:
186 Value.uval = data.getU16(offset_ptr);
187 break;
188 case DW_FORM_data4:
David Blaikie18e73502013-06-19 21:37:13 +0000189 case DW_FORM_ref4: {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000190 Value.uval = data.getU32(offset_ptr);
Frederic Risse4576d22014-11-12 23:48:04 +0000191 if (!cu)
192 break;
193 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr-4);
David Blaikie18e73502013-06-19 21:37:13 +0000194 if (AI != cu->getRelocMap()->end())
195 Value.uval += AI->second.second;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000196 break;
David Blaikie18e73502013-06-19 21:37:13 +0000197 }
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000198 case DW_FORM_data8:
199 case DW_FORM_ref8:
200 Value.uval = data.getU64(offset_ptr);
201 break;
202 case DW_FORM_sdata:
203 Value.sval = data.getSLEB128(offset_ptr);
204 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000205 case DW_FORM_udata:
206 case DW_FORM_ref_udata:
207 Value.uval = data.getULEB128(offset_ptr);
208 break;
209 case DW_FORM_string:
210 Value.cstr = data.getCStr(offset_ptr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000211 break;
212 case DW_FORM_indirect:
213 Form = data.getULEB128(offset_ptr);
214 indirect = true;
215 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000216 case DW_FORM_sec_offset:
217 case DW_FORM_strp:
218 case DW_FORM_GNU_ref_alt:
219 case DW_FORM_GNU_strp_alt: {
Eric Christopher18266172013-01-17 02:59:59 +0000220 // FIXME: This is 64-bit for DWARF64.
Frederic Risse4576d22014-11-12 23:48:04 +0000221 Value.uval = data.getU32(offset_ptr);
222 if (!cu)
223 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000224 RelocAddrMap::const_iterator AI =
225 cu->getRelocMap()->find(*offset_ptr - 4);
Frederic Risse4576d22014-11-12 23:48:04 +0000226 if (AI != cu->getRelocMap()->end())
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000227 Value.uval += AI->second.second;
Eric Christopherd999bb72012-08-24 01:14:23 +0000228 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000229 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000230 case DW_FORM_flag_present:
231 Value.uval = 1;
232 break;
233 case DW_FORM_ref_sig8:
234 Value.uval = data.getU64(offset_ptr);
235 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000236 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000237 case DW_FORM_GNU_str_index:
238 Value.uval = data.getULEB128(offset_ptr);
239 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000240 default:
241 return false;
242 }
Benjamin Kramereaa74332011-09-13 21:47:32 +0000243 } while (indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000244
Benjamin Kramereaa74332011-09-13 21:47:32 +0000245 if (is_block) {
246 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000247 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000248 if (!str.empty()) {
249 Value.data = reinterpret_cast<const uint8_t *>(str.data());
250 *offset_ptr += Value.uval;
251 }
252 }
253
254 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000255}
256
257bool
258DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
David Blaikie07e22442013-09-23 22:44:40 +0000259 const DWARFUnit *cu) const {
Benjamin Kramereaa74332011-09-13 21:47:32 +0000260 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000261}
262
263bool
264DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
David Blaikie07e22442013-09-23 22:44:40 +0000265 uint32_t *offset_ptr, const DWARFUnit *cu) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000266 bool indirect = false;
267 do {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000268 switch (form) {
269 // Blocks if inlined data that have a length field and the data bytes
270 // inlined in the .debug_info
Eric Christopherd999bb72012-08-24 01:14:23 +0000271 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000272 case DW_FORM_block: {
273 uint64_t size = debug_info_data.getULEB128(offset_ptr);
274 *offset_ptr += size;
275 return true;
276 }
277 case DW_FORM_block1: {
278 uint8_t size = debug_info_data.getU8(offset_ptr);
279 *offset_ptr += size;
280 return true;
281 }
282 case DW_FORM_block2: {
283 uint16_t size = debug_info_data.getU16(offset_ptr);
284 *offset_ptr += size;
285 return true;
286 }
287 case DW_FORM_block4: {
288 uint32_t size = debug_info_data.getU32(offset_ptr);
289 *offset_ptr += size;
290 return true;
291 }
292
293 // Inlined NULL terminated C-strings
294 case DW_FORM_string:
295 debug_info_data.getCStr(offset_ptr);
296 return true;
297
298 // Compile unit address sized values
299 case DW_FORM_addr:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000300 *offset_ptr += cu->getAddressByteSize();
301 return true;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000302 case DW_FORM_ref_addr:
303 *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
304 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000305
Eric Christopherd999bb72012-08-24 01:14:23 +0000306 // 0 byte values - implied from the form.
307 case DW_FORM_flag_present:
308 return true;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000309
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000310 // 1 byte values
311 case DW_FORM_data1:
312 case DW_FORM_flag:
313 case DW_FORM_ref1:
314 *offset_ptr += 1;
315 return true;
316
317 // 2 byte values
318 case DW_FORM_data2:
319 case DW_FORM_ref2:
320 *offset_ptr += 2;
321 return true;
322
323 // 4 byte values
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000324 case DW_FORM_data4:
325 case DW_FORM_ref4:
326 *offset_ptr += 4;
327 return true;
328
329 // 8 byte values
330 case DW_FORM_data8:
331 case DW_FORM_ref8:
Eric Christopherd999bb72012-08-24 01:14:23 +0000332 case DW_FORM_ref_sig8:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000333 *offset_ptr += 8;
334 return true;
335
336 // signed or unsigned LEB 128 values
337 // case DW_FORM_APPLE_db_str:
338 case DW_FORM_sdata:
339 case DW_FORM_udata:
340 case DW_FORM_ref_udata:
Eric Christopher2cbd5762013-01-07 19:32:41 +0000341 case DW_FORM_GNU_str_index:
342 case DW_FORM_GNU_addr_index:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000343 debug_info_data.getULEB128(offset_ptr);
344 return true;
345
346 case DW_FORM_indirect:
347 indirect = true;
348 form = debug_info_data.getULEB128(offset_ptr);
349 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000350
Eric Christopher4c7765f2013-01-17 03:00:04 +0000351 // FIXME: 4 for DWARF32, 8 for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000352 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000353 case DW_FORM_strp:
354 case DW_FORM_GNU_ref_alt:
355 case DW_FORM_GNU_strp_alt:
Eric Christopher4c7765f2013-01-17 03:00:04 +0000356 *offset_ptr += 4;
Eric Christopherd999bb72012-08-24 01:14:23 +0000357 return true;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000358
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000359 default:
360 return false;
361 }
362 } while (indirect);
363 return true;
364}
365
366void
David Blaikie07e22442013-09-23 22:44:40 +0000367DWARFFormValue::dump(raw_ostream &OS, const DWARFUnit *cu) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000368 uint64_t uvalue = Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000369 bool cu_relative_offset = false;
370
Benjamin Kramereaa74332011-09-13 21:47:32 +0000371 switch (Form) {
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000372 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher962c9082013-01-15 23:56:56 +0000373 case DW_FORM_GNU_addr_index: {
Eric Christopher962c9082013-01-15 23:56:56 +0000374 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000375 uint64_t Address;
376 if (cu->getAddrOffsetSectionItem(uvalue, Address))
377 OS << format("0x%016" PRIx64, Address);
378 else
Eric Christopher962c9082013-01-15 23:56:56 +0000379 OS << "<no .debug_addr section>";
380 break;
381 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000382 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000383 case DW_FORM_flag:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000384 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
385 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
386 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000387 case DW_FORM_ref_sig8:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000388 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000389 case DW_FORM_string:
390 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000391 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000392 OS << '"';
393 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000394 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000395 case DW_FORM_block:
396 case DW_FORM_block1:
397 case DW_FORM_block2:
398 case DW_FORM_block4:
399 if (uvalue > 0) {
400 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000401 case DW_FORM_exprloc:
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000402 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000403 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
404 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
405 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
406 default: break;
407 }
408
409 const uint8_t* data_ptr = Value.data;
410 if (data_ptr) {
411 // uvalue contains size of block
412 const uint8_t* end_data_ptr = data_ptr + uvalue;
413 while (data_ptr < end_data_ptr) {
414 OS << format("%2.2x ", *data_ptr);
415 ++data_ptr;
416 }
417 }
418 else
419 OS << "NULL";
420 }
421 break;
422
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000423 case DW_FORM_sdata: OS << Value.sval; break;
424 case DW_FORM_udata: OS << Value.uval; break;
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000425 case DW_FORM_strp: {
426 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000427 dumpString(OS, cu);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000428 break;
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000429 }
Eric Christopher2cbd5762013-01-07 19:32:41 +0000430 case DW_FORM_GNU_str_index: {
431 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000432 dumpString(OS, cu);
433 break;
434 }
435 case DW_FORM_GNU_strp_alt: {
436 OS << format("alt indirect string, offset: 0x%" PRIx64 "", uvalue);
437 dumpString(OS, cu);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000438 break;
439 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000440 case DW_FORM_ref_addr:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000441 OS << format("0x%016" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000442 break;
443 case DW_FORM_ref1:
444 cu_relative_offset = true;
445 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
446 break;
447 case DW_FORM_ref2:
448 cu_relative_offset = true;
449 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
450 break;
451 case DW_FORM_ref4:
452 cu_relative_offset = true;
453 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
454 break;
455 case DW_FORM_ref8:
456 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000457 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000458 break;
459 case DW_FORM_ref_udata:
460 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000461 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000462 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000463 case DW_FORM_GNU_ref_alt:
464 OS << format("<alt 0x%" PRIx64 ">", uvalue);
465 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000466
467 // All DW_FORM_indirect attributes should be resolved prior to calling
468 // this function
469 case DW_FORM_indirect:
470 OS << "DW_FORM_indirect";
471 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000472
Eric Christopher4c7765f2013-01-17 03:00:04 +0000473 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000474 case DW_FORM_sec_offset:
Eric Christopher4c7765f2013-01-17 03:00:04 +0000475 OS << format("0x%08x", (uint32_t)uvalue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000476 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000477
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000478 default:
479 OS << format("DW_FORM(0x%4.4x)", Form);
480 break;
481 }
482
Adrian Prantl0c36a752015-01-06 16:50:25 +0000483 if (cu_relative_offset) {
484 OS << " => {";
485 WithColor(OS, syntax::Address).get()
486 << format("0x%8.8" PRIx64, uvalue + (cu ? cu->getOffset() : 0));
487 OS << "}";
488 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000489}
490
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000491void DWARFFormValue::dumpString(raw_ostream &OS, const DWARFUnit *U) const {
492 Optional<const char *> DbgStr = getAsCString(U);
493 if (DbgStr.hasValue()) {
494 raw_ostream &COS = WithColor(OS, syntax::String);
495 COS << '"';
496 COS.write_escaped(DbgStr.getValue());
497 COS << '"';
498 }
499}
500
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000501Optional<const char *> DWARFFormValue::getAsCString(const DWARFUnit *U) const {
502 if (!isFormClass(FC_String))
503 return None;
504 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000505 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000506 // FIXME: Add support for DW_FORM_GNU_strp_alt
507 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000508 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000509 uint32_t Offset = Value.uval;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000510 if (Form == DW_FORM_GNU_str_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000511 uint32_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000512 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
513 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000514 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000515 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000516 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
517 return Str;
518 }
519 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000520}
521
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000522Optional<uint64_t> DWARFFormValue::getAsAddress(const DWARFUnit *U) const {
523 if (!isFormClass(FC_Address))
524 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000525 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000526 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000527 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000528 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000529 return None;
530 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000531 }
532 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000533}
534
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000535Optional<uint64_t> DWARFFormValue::getAsReference(const DWARFUnit *U) const {
536 if (!isFormClass(FC_Reference))
537 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000538 switch (Form) {
539 case DW_FORM_ref1:
540 case DW_FORM_ref2:
541 case DW_FORM_ref4:
542 case DW_FORM_ref8:
543 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000544 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000545 return None;
546 return Value.uval + U->getOffset();
547 case DW_FORM_ref_addr:
548 return Value.uval;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000549 // FIXME: Add proper support for DW_FORM_ref_sig8 and DW_FORM_GNU_ref_alt.
Benjamin Kramereaa74332011-09-13 21:47:32 +0000550 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000551 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000552 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000553}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000554
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000555Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
556 if (!isFormClass(FC_SectionOffset))
557 return None;
558 return Value.uval;
559}
560
561Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000562 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
563 || Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000564 return None;
565 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000566}
Frederic Rissf3549a22014-09-04 06:14:35 +0000567
Frederic Riss77f850e2015-03-04 22:07:41 +0000568Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
569 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
570 (Form == DW_FORM_udata && uint64_t(LLONG_MAX) < Value.uval))
571 return None;
572 switch (Form) {
573 case DW_FORM_data4:
574 return int32_t(Value.uval);
575 case DW_FORM_data2:
576 return int16_t(Value.uval);
577 case DW_FORM_data1:
578 return int8_t(Value.uval);
579 case DW_FORM_sdata:
580 case DW_FORM_data8:
581 default:
582 return Value.sval;
583 }
584}
585
Frederic Risseab17772014-09-04 06:35:09 +0000586Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000587 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
588 return None;
Frederic Risseab17772014-09-04 06:35:09 +0000589 return ArrayRef<uint8_t>(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000590}
591