blob: 45bd1973a766d42943f420f002df304a38f731f2 [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>
21using namespace llvm;
22using namespace dwarf;
Adrian Prantl0c36a752015-01-06 16:50:25 +000023using namespace syntax;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000024
Benjamin Kramerc86fdf12013-04-11 11:36:36 +000025namespace {
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000026uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
27 // FIXME: Support DWARF64.
28 return (Version == 2) ? AddrSize : 4;
Benjamin Kramerc86fdf12013-04-11 11:36:36 +000029}
Alexey Samsonovd60859b2013-04-09 14:09:42 +000030
31template <uint8_t AddrSize, uint8_t RefAddrSize>
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000032ArrayRef<uint8_t> makeFixedFormSizesArrayRef() {
33 static const uint8_t sizes[] = {
34 0, // 0x00 unused
35 AddrSize, // 0x01 DW_FORM_addr
36 0, // 0x02 unused
37 0, // 0x03 DW_FORM_block2
38 0, // 0x04 DW_FORM_block4
39 2, // 0x05 DW_FORM_data2
40 4, // 0x06 DW_FORM_data4
41 8, // 0x07 DW_FORM_data8
42 0, // 0x08 DW_FORM_string
43 0, // 0x09 DW_FORM_block
44 0, // 0x0a DW_FORM_block1
45 1, // 0x0b DW_FORM_data1
46 1, // 0x0c DW_FORM_flag
47 0, // 0x0d DW_FORM_sdata
48 4, // 0x0e DW_FORM_strp
49 0, // 0x0f DW_FORM_udata
50 RefAddrSize, // 0x10 DW_FORM_ref_addr
51 1, // 0x11 DW_FORM_ref1
52 2, // 0x12 DW_FORM_ref2
53 4, // 0x13 DW_FORM_ref4
54 8, // 0x14 DW_FORM_ref8
55 0, // 0x15 DW_FORM_ref_udata
56 0, // 0x16 DW_FORM_indirect
57 4, // 0x17 DW_FORM_sec_offset
58 0, // 0x18 DW_FORM_exprloc
59 0, // 0x19 DW_FORM_flag_present
60 };
61 return makeArrayRef(sizes);
62}
Alexey Samsonovd60859b2013-04-09 14:09:42 +000063}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000064
Alexey Samsonova56bbf02013-10-28 23:41:49 +000065ArrayRef<uint8_t> DWARFFormValue::getFixedFormSizes(uint8_t AddrSize,
66 uint16_t Version) {
Alexey Samsonovd60859b2013-04-09 14:09:42 +000067 uint8_t RefAddrSize = getRefAddrSize(AddrSize, Version);
68 if (AddrSize == 4 && RefAddrSize == 4)
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000069 return makeFixedFormSizesArrayRef<4, 4>();
Alexey Samsonovd60859b2013-04-09 14:09:42 +000070 if (AddrSize == 4 && RefAddrSize == 8)
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000071 return makeFixedFormSizesArrayRef<4, 8>();
Alexey Samsonovd60859b2013-04-09 14:09:42 +000072 if (AddrSize == 8 && RefAddrSize == 4)
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000073 return makeFixedFormSizesArrayRef<8, 4>();
Alexey Samsonovd60859b2013-04-09 14:09:42 +000074 if (AddrSize == 8 && RefAddrSize == 8)
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000075 return makeFixedFormSizesArrayRef<8, 8>();
Alexey Samsonova56bbf02013-10-28 23:41:49 +000076 return None;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000077}
78
Alexey Samsonov48cbda52013-10-28 23:01:48 +000079static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
80 DWARFFormValue::FC_Unknown, // 0x0
81 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
82 DWARFFormValue::FC_Unknown, // 0x02 unused
83 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
84 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
85 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
86 // --- These can be FC_SectionOffset in DWARF3 and below:
87 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
88 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
89 // ---
90 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
91 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
92 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
93 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
94 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
95 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
96 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
97 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
98 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
99 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
100 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
101 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
102 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
103 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
104 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
105 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
106 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
107 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000108};
109
110bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
111 // First, check DWARF4 form classes.
112 if (Form < ArrayRef<FormClass>(DWARF4FormClasses).size() &&
113 DWARF4FormClasses[Form] == FC)
114 return true;
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000115 // Check DW_FORM_ref_sig8 from DWARF4.
116 if (Form == DW_FORM_ref_sig8)
117 return (FC == FC_Reference);
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000118 // Check for some DWARF5 forms.
119 if (Form == DW_FORM_GNU_addr_index)
120 return (FC == FC_Address);
121 if (Form == DW_FORM_GNU_str_index)
122 return (FC == FC_String);
123 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
124 // Don't check for DWARF version here, as some producers may still do this
125 // by mistake.
126 if ((Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
127 FC == FC_SectionOffset)
128 return true;
129 return false;
130}
131
David Blaikie07e22442013-09-23 22:44:40 +0000132bool DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
133 const DWARFUnit *cu) {
Benjamin Kramereaa74332011-09-13 21:47:32 +0000134 bool indirect = false;
135 bool is_block = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000136 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000137 // Read the value for the form into value and follow and DW_FORM_indirect
138 // instances we run into
139 do {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000140 indirect = false;
141 switch (Form) {
142 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000143 case DW_FORM_ref_addr: {
Frederic Risse4576d22014-11-12 23:48:04 +0000144 if (!cu)
145 return false;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000146 uint16_t AddrSize =
147 (Form == DW_FORM_addr)
148 ? cu->getAddressByteSize()
149 : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
150 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
Eric Christopherda4b2192013-01-02 23:52:13 +0000151 if (AI != cu->getRelocMap()->end()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000152 const std::pair<uint8_t, int64_t> &R = AI->second;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000153 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
Eric Christopher7370b552012-11-12 21:40:38 +0000154 } else
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000155 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000156 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000157 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000158 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000159 case DW_FORM_block:
160 Value.uval = data.getULEB128(offset_ptr);
161 is_block = true;
162 break;
163 case DW_FORM_block1:
164 Value.uval = data.getU8(offset_ptr);
165 is_block = true;
166 break;
167 case DW_FORM_block2:
168 Value.uval = data.getU16(offset_ptr);
169 is_block = true;
170 break;
171 case DW_FORM_block4:
172 Value.uval = data.getU32(offset_ptr);
173 is_block = true;
174 break;
175 case DW_FORM_data1:
176 case DW_FORM_ref1:
177 case DW_FORM_flag:
178 Value.uval = data.getU8(offset_ptr);
179 break;
180 case DW_FORM_data2:
181 case DW_FORM_ref2:
182 Value.uval = data.getU16(offset_ptr);
183 break;
184 case DW_FORM_data4:
David Blaikie18e73502013-06-19 21:37:13 +0000185 case DW_FORM_ref4: {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000186 Value.uval = data.getU32(offset_ptr);
Frederic Risse4576d22014-11-12 23:48:04 +0000187 if (!cu)
188 break;
189 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr-4);
David Blaikie18e73502013-06-19 21:37:13 +0000190 if (AI != cu->getRelocMap()->end())
191 Value.uval += AI->second.second;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000192 break;
David Blaikie18e73502013-06-19 21:37:13 +0000193 }
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000194 case DW_FORM_data8:
195 case DW_FORM_ref8:
196 Value.uval = data.getU64(offset_ptr);
197 break;
198 case DW_FORM_sdata:
199 Value.sval = data.getSLEB128(offset_ptr);
200 break;
Eric Christopher7c678de2012-11-07 23:22:07 +0000201 case DW_FORM_strp: {
Frederic Risse4576d22014-11-12 23:48:04 +0000202 Value.uval = data.getU32(offset_ptr);
203 if (!cu)
204 break;
205 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr-4);
206 if (AI != cu->getRelocMap()->end())
207 Value.uval += AI->second.second;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000208 break;
Eric Christopher7c678de2012-11-07 23:22:07 +0000209 }
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000210 case DW_FORM_udata:
211 case DW_FORM_ref_udata:
212 Value.uval = data.getULEB128(offset_ptr);
213 break;
214 case DW_FORM_string:
215 Value.cstr = data.getCStr(offset_ptr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000216 break;
217 case DW_FORM_indirect:
218 Form = data.getULEB128(offset_ptr);
219 indirect = true;
220 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000221 case DW_FORM_sec_offset: {
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);
224 if (!cu)
225 break;
226 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr-4);
227 if (AI != cu->getRelocMap()->end())
228 Value.uval += AI->second.second;
Eric Christopherd999bb72012-08-24 01:14:23 +0000229 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000230 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000231 case DW_FORM_flag_present:
232 Value.uval = 1;
233 break;
234 case DW_FORM_ref_sig8:
235 Value.uval = data.getU64(offset_ptr);
236 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000237 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000238 case DW_FORM_GNU_str_index:
239 Value.uval = data.getULEB128(offset_ptr);
240 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000241 default:
242 return false;
243 }
Benjamin Kramereaa74332011-09-13 21:47:32 +0000244 } while (indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000245
Benjamin Kramereaa74332011-09-13 21:47:32 +0000246 if (is_block) {
247 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000248 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000249 if (!str.empty()) {
250 Value.data = reinterpret_cast<const uint8_t *>(str.data());
251 *offset_ptr += Value.uval;
252 }
253 }
254
255 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000256}
257
258bool
259DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
David Blaikie07e22442013-09-23 22:44:40 +0000260 const DWARFUnit *cu) const {
Benjamin Kramereaa74332011-09-13 21:47:32 +0000261 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000262}
263
264bool
265DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
David Blaikie07e22442013-09-23 22:44:40 +0000266 uint32_t *offset_ptr, const DWARFUnit *cu) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000267 bool indirect = false;
268 do {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000269 switch (form) {
270 // Blocks if inlined data that have a length field and the data bytes
271 // inlined in the .debug_info
Eric Christopherd999bb72012-08-24 01:14:23 +0000272 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000273 case DW_FORM_block: {
274 uint64_t size = debug_info_data.getULEB128(offset_ptr);
275 *offset_ptr += size;
276 return true;
277 }
278 case DW_FORM_block1: {
279 uint8_t size = debug_info_data.getU8(offset_ptr);
280 *offset_ptr += size;
281 return true;
282 }
283 case DW_FORM_block2: {
284 uint16_t size = debug_info_data.getU16(offset_ptr);
285 *offset_ptr += size;
286 return true;
287 }
288 case DW_FORM_block4: {
289 uint32_t size = debug_info_data.getU32(offset_ptr);
290 *offset_ptr += size;
291 return true;
292 }
293
294 // Inlined NULL terminated C-strings
295 case DW_FORM_string:
296 debug_info_data.getCStr(offset_ptr);
297 return true;
298
299 // Compile unit address sized values
300 case DW_FORM_addr:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000301 *offset_ptr += cu->getAddressByteSize();
302 return true;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000303 case DW_FORM_ref_addr:
304 *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
305 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000306
Eric Christopherd999bb72012-08-24 01:14:23 +0000307 // 0 byte values - implied from the form.
308 case DW_FORM_flag_present:
309 return true;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000310
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000311 // 1 byte values
312 case DW_FORM_data1:
313 case DW_FORM_flag:
314 case DW_FORM_ref1:
315 *offset_ptr += 1;
316 return true;
317
318 // 2 byte values
319 case DW_FORM_data2:
320 case DW_FORM_ref2:
321 *offset_ptr += 2;
322 return true;
323
324 // 4 byte values
325 case DW_FORM_strp:
326 case DW_FORM_data4:
327 case DW_FORM_ref4:
328 *offset_ptr += 4;
329 return true;
330
331 // 8 byte values
332 case DW_FORM_data8:
333 case DW_FORM_ref8:
Eric Christopherd999bb72012-08-24 01:14:23 +0000334 case DW_FORM_ref_sig8:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000335 *offset_ptr += 8;
336 return true;
337
338 // signed or unsigned LEB 128 values
339 // case DW_FORM_APPLE_db_str:
340 case DW_FORM_sdata:
341 case DW_FORM_udata:
342 case DW_FORM_ref_udata:
Eric Christopher2cbd5762013-01-07 19:32:41 +0000343 case DW_FORM_GNU_str_index:
344 case DW_FORM_GNU_addr_index:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000345 debug_info_data.getULEB128(offset_ptr);
346 return true;
347
348 case DW_FORM_indirect:
349 indirect = true;
350 form = debug_info_data.getULEB128(offset_ptr);
351 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000352
Eric Christopher4c7765f2013-01-17 03:00:04 +0000353 // FIXME: 4 for DWARF32, 8 for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000354 case DW_FORM_sec_offset:
Eric Christopher4c7765f2013-01-17 03:00:04 +0000355 *offset_ptr += 4;
Eric Christopherd999bb72012-08-24 01:14:23 +0000356 return true;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000357
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000358 default:
359 return false;
360 }
361 } while (indirect);
362 return true;
363}
364
365void
David Blaikie07e22442013-09-23 22:44:40 +0000366DWARFFormValue::dump(raw_ostream &OS, const DWARFUnit *cu) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000367 uint64_t uvalue = Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000368 bool cu_relative_offset = false;
369
Benjamin Kramereaa74332011-09-13 21:47:32 +0000370 switch (Form) {
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000371 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher962c9082013-01-15 23:56:56 +0000372 case DW_FORM_GNU_addr_index: {
Eric Christopher962c9082013-01-15 23:56:56 +0000373 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000374 uint64_t Address;
375 if (cu->getAddrOffsetSectionItem(uvalue, Address))
376 OS << format("0x%016" PRIx64, Address);
377 else
Eric Christopher962c9082013-01-15 23:56:56 +0000378 OS << "<no .debug_addr section>";
379 break;
380 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000381 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000382 case DW_FORM_flag:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000383 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
384 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
385 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000386 case DW_FORM_ref_sig8:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000387 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000388 case DW_FORM_string:
389 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000390 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000391 OS << '"';
392 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000393 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000394 case DW_FORM_block:
395 case DW_FORM_block1:
396 case DW_FORM_block2:
397 case DW_FORM_block4:
398 if (uvalue > 0) {
399 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000400 case DW_FORM_exprloc:
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000401 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000402 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
403 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
404 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
405 default: break;
406 }
407
408 const uint8_t* data_ptr = Value.data;
409 if (data_ptr) {
410 // uvalue contains size of block
411 const uint8_t* end_data_ptr = data_ptr + uvalue;
412 while (data_ptr < end_data_ptr) {
413 OS << format("%2.2x ", *data_ptr);
414 ++data_ptr;
415 }
416 }
417 else
418 OS << "NULL";
419 }
420 break;
421
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000422 case DW_FORM_sdata: OS << Value.sval; break;
423 case DW_FORM_udata: OS << Value.uval; break;
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000424 case DW_FORM_strp: {
425 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000426 Optional<const char *> DbgStr = getAsCString(cu);
427 if (DbgStr.hasValue()) {
Adrian Prantl0c36a752015-01-06 16:50:25 +0000428 raw_ostream &COS = WithColor(OS, syntax::String);
429 COS << '"';
430 COS.write_escaped(DbgStr.getValue());
431 COS << '"';
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000432 }
433 break;
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000434 }
Eric Christopher2cbd5762013-01-07 19:32:41 +0000435 case DW_FORM_GNU_str_index: {
436 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000437 Optional<const char *> DbgStr = getAsCString(cu);
438 if (DbgStr.hasValue()) {
Adrian Prantl0c36a752015-01-06 16:50:25 +0000439 raw_ostream &COS = WithColor(OS, syntax::String);
440 COS << '"';
441 COS.write_escaped(DbgStr.getValue());
442 COS << '"';
Eric Christopher2cbd5762013-01-07 19:32:41 +0000443 }
444 break;
445 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000446 case DW_FORM_ref_addr:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000447 OS << format("0x%016" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000448 break;
449 case DW_FORM_ref1:
450 cu_relative_offset = true;
451 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
452 break;
453 case DW_FORM_ref2:
454 cu_relative_offset = true;
455 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
456 break;
457 case DW_FORM_ref4:
458 cu_relative_offset = true;
459 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
460 break;
461 case DW_FORM_ref8:
462 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000463 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000464 break;
465 case DW_FORM_ref_udata:
466 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000467 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000468 break;
469
470 // All DW_FORM_indirect attributes should be resolved prior to calling
471 // this function
472 case DW_FORM_indirect:
473 OS << "DW_FORM_indirect";
474 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000475
Eric Christopher4c7765f2013-01-17 03:00:04 +0000476 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000477 case DW_FORM_sec_offset:
Eric Christopher4c7765f2013-01-17 03:00:04 +0000478 OS << format("0x%08x", (uint32_t)uvalue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000479 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000480
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000481 default:
482 OS << format("DW_FORM(0x%4.4x)", Form);
483 break;
484 }
485
Adrian Prantl0c36a752015-01-06 16:50:25 +0000486 if (cu_relative_offset) {
487 OS << " => {";
488 WithColor(OS, syntax::Address).get()
489 << format("0x%8.8" PRIx64, uvalue + (cu ? cu->getOffset() : 0));
490 OS << "}";
491 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000492}
493
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000494Optional<const char *> DWARFFormValue::getAsCString(const DWARFUnit *U) const {
495 if (!isFormClass(FC_String))
496 return None;
497 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000498 return Value.cstr;
Craig Topper2617dcc2014-04-15 06:32:26 +0000499 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000500 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000501 uint32_t Offset = Value.uval;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000502 if (Form == DW_FORM_GNU_str_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000503 uint32_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000504 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
505 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000506 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000507 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000508 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
509 return Str;
510 }
511 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000512}
513
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000514Optional<uint64_t> DWARFFormValue::getAsAddress(const DWARFUnit *U) const {
515 if (!isFormClass(FC_Address))
516 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000517 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000518 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000519 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000520 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000521 return None;
522 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000523 }
524 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000525}
526
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000527Optional<uint64_t> DWARFFormValue::getAsReference(const DWARFUnit *U) const {
528 if (!isFormClass(FC_Reference))
529 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000530 switch (Form) {
531 case DW_FORM_ref1:
532 case DW_FORM_ref2:
533 case DW_FORM_ref4:
534 case DW_FORM_ref8:
535 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000536 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000537 return None;
538 return Value.uval + U->getOffset();
539 case DW_FORM_ref_addr:
540 return Value.uval;
541 // FIXME: Add proper support for DW_FORM_ref_sig8
Benjamin Kramereaa74332011-09-13 21:47:32 +0000542 default:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000543 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000544 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000545}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000546
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000547Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
548 if (!isFormClass(FC_SectionOffset))
549 return None;
550 return Value.uval;
551}
552
553Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000554 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
555 || Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000556 return None;
557 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000558}
Frederic Rissf3549a22014-09-04 06:14:35 +0000559
Frederic Risseab17772014-09-04 06:35:09 +0000560Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000561 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
562 return None;
Frederic Risseab17772014-09-04 06:35:09 +0000563 return ArrayRef<uint8_t>(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000564}
565