blob: dcb491fdd80a0c40c2b9db3be83b79ddb99aaf2f [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
Alexey Samsonov48cbda52013-10-28 23:01:48 +000026static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
27 DWARFFormValue::FC_Unknown, // 0x0
28 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
29 DWARFFormValue::FC_Unknown, // 0x02 unused
30 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
31 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
32 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
33 // --- These can be FC_SectionOffset in DWARF3 and below:
34 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
35 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
36 // ---
37 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
38 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
39 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
40 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
41 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
42 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
43 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
44 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
45 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
46 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
47 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
48 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
49 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
50 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
51 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
52 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
53 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
54 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
Alexey Samsonov48cbda52013-10-28 23:01:48 +000055};
56
Greg Clayton82f12b12016-11-11 16:21:37 +000057namespace {
58
59/// A helper class that can be used in DWARFFormValue.cpp functions that need
60/// to know the byte size of DW_FORM values that vary in size depending on the
61/// DWARF version, address byte size, or DWARF32 or DWARF64.
62class FormSizeHelper {
63 uint16_t Version;
64 uint8_t AddrSize;
65 llvm::dwarf::DwarfFormat Format;
66
67public:
68 FormSizeHelper(uint16_t V, uint8_t A, llvm::dwarf::DwarfFormat F)
69 : Version(V), AddrSize(A), Format(F) {}
70 uint8_t getAddressByteSize() const { return AddrSize; }
71 uint8_t getRefAddrByteSize() const {
72 if (Version == 2)
73 return AddrSize;
74 return getDwarfOffsetByteSize();
75 }
76 uint8_t getDwarfOffsetByteSize() const {
77 switch (Format) {
78 case dwarf::DwarfFormat::DWARF32:
79 return 4;
80 case dwarf::DwarfFormat::DWARF64:
81 return 8;
82 }
83 llvm_unreachable("Invalid Format value");
84 }
85};
86
87} // end anonymous namespace
88
89template <class T>
90static Optional<uint8_t> getFixedByteSize(dwarf::Form Form, const T *U) {
91 switch (Form) {
92 case DW_FORM_addr:
93 if (U)
94 return U->getAddressByteSize();
95 return None;
96
97 case DW_FORM_block: // ULEB128 length L followed by L bytes.
98 case DW_FORM_block1: // 1 byte length L followed by L bytes.
99 case DW_FORM_block2: // 2 byte length L followed by L bytes.
100 case DW_FORM_block4: // 4 byte length L followed by L bytes.
101 case DW_FORM_string: // C-string with null terminator.
102 case DW_FORM_sdata: // SLEB128.
103 case DW_FORM_udata: // ULEB128.
104 case DW_FORM_ref_udata: // ULEB128.
105 case DW_FORM_indirect: // ULEB128.
106 case DW_FORM_exprloc: // ULEB128 length L followed by L bytes.
107 case DW_FORM_strx: // ULEB128.
108 case DW_FORM_addrx: // ULEB128.
109 case DW_FORM_loclistx: // ULEB128.
110 case DW_FORM_rnglistx: // ULEB128.
111 case DW_FORM_GNU_addr_index: // ULEB128.
112 case DW_FORM_GNU_str_index: // ULEB128.
113 return None;
114
115 case DW_FORM_ref_addr:
116 if (U)
117 return U->getRefAddrByteSize();
118 return None;
119
120 case DW_FORM_flag:
121 case DW_FORM_data1:
122 case DW_FORM_ref1:
123 return 1;
124
125 case DW_FORM_data2:
126 case DW_FORM_ref2:
127 return 2;
128
129 case DW_FORM_data4:
130 case DW_FORM_ref4:
131 case DW_FORM_strp:
132 return 4;
133
134 case DW_FORM_GNU_ref_alt:
135 case DW_FORM_GNU_strp_alt:
136 case DW_FORM_line_strp:
137 case DW_FORM_sec_offset:
138 case DW_FORM_strp_sup:
139 case DW_FORM_ref_sup:
140 if (U)
141 return U->getDwarfOffsetByteSize();
142 return None;
143
144 case DW_FORM_data8:
145 case DW_FORM_ref8:
146 case DW_FORM_ref_sig8:
147 return 8;
148
149 case DW_FORM_flag_present:
150 return 0;
151
152 case DW_FORM_data16:
153 return 16;
154
155 case DW_FORM_implicit_const:
156 // The implicit value is stored in the abbreviation as a ULEB128, any
157 // there no data in debug info.
158 return 0;
159
160 default:
161 llvm_unreachable("Handle this form in this switch statement");
162 }
163 return None;
164}
165
166template <class T>
167static bool skipFormValue(dwarf::Form Form, const DataExtractor &DebugInfoData,
168 uint32_t *OffsetPtr, const T *U) {
169 bool Indirect = false;
170 do {
171 switch (Form) {
172 // Blocks if inlined data that have a length field and the data bytes
173 // inlined in the .debug_info.
174 case DW_FORM_exprloc:
175 case DW_FORM_block: {
176 uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
177 *OffsetPtr += size;
178 return true;
179 }
180 case DW_FORM_block1: {
181 uint8_t size = DebugInfoData.getU8(OffsetPtr);
182 *OffsetPtr += size;
183 return true;
184 }
185 case DW_FORM_block2: {
186 uint16_t size = DebugInfoData.getU16(OffsetPtr);
187 *OffsetPtr += size;
188 return true;
189 }
190 case DW_FORM_block4: {
191 uint32_t size = DebugInfoData.getU32(OffsetPtr);
192 *OffsetPtr += size;
193 return true;
194 }
195
196 // Inlined NULL terminated C-strings.
197 case DW_FORM_string:
198 DebugInfoData.getCStr(OffsetPtr);
199 return true;
200
201 case DW_FORM_addr:
202 case DW_FORM_ref_addr:
203 case DW_FORM_flag_present:
204 case DW_FORM_data1:
205 case DW_FORM_data2:
206 case DW_FORM_data4:
207 case DW_FORM_data8:
208 case DW_FORM_flag:
209 case DW_FORM_ref1:
210 case DW_FORM_ref2:
211 case DW_FORM_ref4:
212 case DW_FORM_ref8:
213 case DW_FORM_ref_sig8:
214 case DW_FORM_sec_offset:
215 case DW_FORM_strp:
216 case DW_FORM_GNU_ref_alt:
217 case DW_FORM_GNU_strp_alt:
218 if (Optional<uint8_t> FixedSize = ::getFixedByteSize(Form, U)) {
219 *OffsetPtr += *FixedSize;
220 return true;
221 }
222 return false;
223
224 // signed or unsigned LEB 128 values.
225 case DW_FORM_sdata:
226 DebugInfoData.getSLEB128(OffsetPtr);
227 return true;
228
229 case DW_FORM_udata:
230 case DW_FORM_ref_udata:
231 case DW_FORM_strx:
232 case DW_FORM_addrx:
233 case DW_FORM_loclistx:
234 case DW_FORM_rnglistx:
235 case DW_FORM_GNU_addr_index:
236 case DW_FORM_GNU_str_index:
237 DebugInfoData.getULEB128(OffsetPtr);
238 return true;
239
240 case DW_FORM_indirect:
241 Indirect = true;
242 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
243 break;
244
245 default:
246 return false;
247 }
248 } while (Indirect);
249 return true;
250}
251
252Optional<uint8_t> DWARFFormValue::getFixedByteSize(dwarf::Form Form,
253 const DWARFUnit *U) {
254 return ::getFixedByteSize(Form, U);
255}
256
257Optional<uint8_t>
258DWARFFormValue::getFixedByteSize(dwarf::Form Form, uint16_t Version,
259 uint8_t AddrSize,
260 llvm::dwarf::DwarfFormat Format) {
261 FormSizeHelper FSH(Version, AddrSize, Format);
262 return ::getFixedByteSize(Form, &FSH);
263}
264
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000265bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
266 // First, check DWARF4 form classes.
Craig Topper0013be12015-09-21 05:32:41 +0000267 if (Form < makeArrayRef(DWARF4FormClasses).size() &&
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000268 DWARF4FormClasses[Form] == FC)
269 return true;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000270 // Check more forms from DWARF4 and DWARF5 proposals.
271 switch (Form) {
272 case DW_FORM_ref_sig8:
273 case DW_FORM_GNU_ref_alt:
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000274 return (FC == FC_Reference);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000275 case DW_FORM_GNU_addr_index:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000276 return (FC == FC_Address);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000277 case DW_FORM_GNU_str_index:
278 case DW_FORM_GNU_strp_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000279 return (FC == FC_String);
Greg Clayton6c273762016-10-27 16:32:04 +0000280 default:
281 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000282 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000283 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
284 // Don't check for DWARF version here, as some producers may still do this
285 // by mistake.
Benjamin Kramer68a29562015-05-25 13:28:03 +0000286 return (Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
287 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000288}
289
Greg Claytoncddab272016-10-31 16:46:02 +0000290bool DWARFFormValue::extractValue(const DataExtractor &data,
291 uint32_t *offset_ptr,
David Blaikie07e22442013-09-23 22:44:40 +0000292 const DWARFUnit *cu) {
Greg Claytoncddab272016-10-31 16:46:02 +0000293 U = cu;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000294 bool indirect = false;
295 bool is_block = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000296 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000297 // Read the value for the form into value and follow and DW_FORM_indirect
298 // instances we run into
299 do {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000300 indirect = false;
301 switch (Form) {
302 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000303 case DW_FORM_ref_addr: {
Greg Claytoncddab272016-10-31 16:46:02 +0000304 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000305 return false;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000306 uint16_t AddrSize =
307 (Form == DW_FORM_addr)
Greg Claytoncddab272016-10-31 16:46:02 +0000308 ? U->getAddressByteSize()
309 : U->getRefAddrByteSize();
310 RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr);
311 if (AI != U->getRelocMap()->end()) {
Greg Clayton82f12b12016-11-11 16:21:37 +0000312 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + AI->second.second;
Eric Christopher7370b552012-11-12 21:40:38 +0000313 } else
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000314 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000315 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000316 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000317 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000318 case DW_FORM_block:
319 Value.uval = data.getULEB128(offset_ptr);
320 is_block = true;
321 break;
322 case DW_FORM_block1:
323 Value.uval = data.getU8(offset_ptr);
324 is_block = true;
325 break;
326 case DW_FORM_block2:
327 Value.uval = data.getU16(offset_ptr);
328 is_block = true;
329 break;
330 case DW_FORM_block4:
331 Value.uval = data.getU32(offset_ptr);
332 is_block = true;
333 break;
334 case DW_FORM_data1:
335 case DW_FORM_ref1:
336 case DW_FORM_flag:
337 Value.uval = data.getU8(offset_ptr);
338 break;
339 case DW_FORM_data2:
340 case DW_FORM_ref2:
341 Value.uval = data.getU16(offset_ptr);
342 break;
343 case DW_FORM_data4:
Greg Clayton82f12b12016-11-11 16:21:37 +0000344 case DW_FORM_ref4:
345 case DW_FORM_strp: {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000346 Value.uval = data.getU32(offset_ptr);
Greg Claytoncddab272016-10-31 16:46:02 +0000347 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000348 break;
Greg Claytoncddab272016-10-31 16:46:02 +0000349 RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr-4);
350 if (AI != U->getRelocMap()->end())
David Blaikie18e73502013-06-19 21:37:13 +0000351 Value.uval += AI->second.second;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000352 break;
David Blaikie18e73502013-06-19 21:37:13 +0000353 }
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000354 case DW_FORM_data8:
355 case DW_FORM_ref8:
356 Value.uval = data.getU64(offset_ptr);
357 break;
358 case DW_FORM_sdata:
359 Value.sval = data.getSLEB128(offset_ptr);
360 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000361 case DW_FORM_udata:
362 case DW_FORM_ref_udata:
363 Value.uval = data.getULEB128(offset_ptr);
364 break;
365 case DW_FORM_string:
366 Value.cstr = data.getCStr(offset_ptr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000367 break;
368 case DW_FORM_indirect:
Greg Clayton6c273762016-10-27 16:32:04 +0000369 Form = static_cast<dwarf::Form>(data.getULEB128(offset_ptr));
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000370 indirect = true;
371 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000372 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000373 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000374 case DW_FORM_GNU_strp_alt:
375 case DW_FORM_line_strp:
376 case DW_FORM_strp_sup:
377 case DW_FORM_ref_sup: {
Greg Claytoncddab272016-10-31 16:46:02 +0000378 if (!U)
Greg Clayton82f12b12016-11-11 16:21:37 +0000379 return false;
380 RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr);
381 uint8_t Size = U->getDwarfOffsetByteSize();
382 Value.uval = data.getUnsigned(offset_ptr, Size);
Greg Claytoncddab272016-10-31 16:46:02 +0000383 if (AI != U->getRelocMap()->end())
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000384 Value.uval += AI->second.second;
Eric Christopherd999bb72012-08-24 01:14:23 +0000385 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000386 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000387 case DW_FORM_flag_present:
388 Value.uval = 1;
389 break;
390 case DW_FORM_ref_sig8:
391 Value.uval = data.getU64(offset_ptr);
392 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000393 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000394 case DW_FORM_GNU_str_index:
395 Value.uval = data.getULEB128(offset_ptr);
396 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000397 default:
398 return false;
399 }
Benjamin Kramereaa74332011-09-13 21:47:32 +0000400 } while (indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000401
Benjamin Kramereaa74332011-09-13 21:47:32 +0000402 if (is_block) {
403 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000404 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000405 if (!str.empty()) {
406 Value.data = reinterpret_cast<const uint8_t *>(str.data());
407 *offset_ptr += Value.uval;
408 }
409 }
410
411 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000412}
413
Greg Clayton82f12b12016-11-11 16:21:37 +0000414bool DWARFFormValue::skipValue(DataExtractor DebugInfoData,
415 uint32_t *offset_ptr, const DWARFUnit *U) const {
416 return DWARFFormValue::skipValue(Form, DebugInfoData, offset_ptr, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000417}
418
Greg Clayton82f12b12016-11-11 16:21:37 +0000419bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
420 uint32_t *offset_ptr, const DWARFUnit *U) {
421 return skipFormValue(form, DebugInfoData, offset_ptr, U);
David Blaikiead07b5d2015-12-04 17:20:04 +0000422}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000423
Greg Clayton82f12b12016-11-11 16:21:37 +0000424bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
425 uint32_t *offset_ptr, uint16_t Version,
426 uint8_t AddrSize,
427 llvm::dwarf::DwarfFormat Format) {
428 FormSizeHelper FSH(Version, AddrSize, Format);
429 return skipFormValue(form, DebugInfoData, offset_ptr, &FSH);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000430}
431
432void
Greg Claytoncddab272016-10-31 16:46:02 +0000433DWARFFormValue::dump(raw_ostream &OS) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000434 uint64_t uvalue = Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000435 bool cu_relative_offset = false;
436
Benjamin Kramereaa74332011-09-13 21:47:32 +0000437 switch (Form) {
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000438 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher962c9082013-01-15 23:56:56 +0000439 case DW_FORM_GNU_addr_index: {
Eric Christopher962c9082013-01-15 23:56:56 +0000440 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000441 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000442 if (U == nullptr)
443 OS << "<invalid dwarf unit>";
444 else if (U->getAddrOffsetSectionItem(uvalue, Address))
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000445 OS << format("0x%016" PRIx64, Address);
446 else
Eric Christopher962c9082013-01-15 23:56:56 +0000447 OS << "<no .debug_addr section>";
448 break;
449 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000450 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000451 case DW_FORM_flag:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000452 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
453 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
454 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000455 case DW_FORM_ref_sig8:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000456 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000457 case DW_FORM_string:
458 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000459 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000460 OS << '"';
461 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000462 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000463 case DW_FORM_block:
464 case DW_FORM_block1:
465 case DW_FORM_block2:
466 case DW_FORM_block4:
467 if (uvalue > 0) {
468 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000469 case DW_FORM_exprloc:
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000470 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000471 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
472 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
473 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
474 default: break;
475 }
476
477 const uint8_t* data_ptr = Value.data;
478 if (data_ptr) {
479 // uvalue contains size of block
480 const uint8_t* end_data_ptr = data_ptr + uvalue;
481 while (data_ptr < end_data_ptr) {
482 OS << format("%2.2x ", *data_ptr);
483 ++data_ptr;
484 }
485 }
486 else
487 OS << "NULL";
488 }
489 break;
490
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000491 case DW_FORM_sdata: OS << Value.sval; break;
492 case DW_FORM_udata: OS << Value.uval; break;
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000493 case DW_FORM_strp: {
494 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000495 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000496 break;
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000497 }
Eric Christopher2cbd5762013-01-07 19:32:41 +0000498 case DW_FORM_GNU_str_index: {
499 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000500 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000501 break;
502 }
503 case DW_FORM_GNU_strp_alt: {
504 OS << format("alt indirect string, offset: 0x%" PRIx64 "", uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000505 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000506 break;
507 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000508 case DW_FORM_ref_addr:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000509 OS << format("0x%016" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000510 break;
511 case DW_FORM_ref1:
512 cu_relative_offset = true;
513 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
514 break;
515 case DW_FORM_ref2:
516 cu_relative_offset = true;
517 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
518 break;
519 case DW_FORM_ref4:
520 cu_relative_offset = true;
521 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
522 break;
523 case DW_FORM_ref8:
524 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000525 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000526 break;
527 case DW_FORM_ref_udata:
528 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000529 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000530 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000531 case DW_FORM_GNU_ref_alt:
532 OS << format("<alt 0x%" PRIx64 ">", uvalue);
533 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000534
535 // All DW_FORM_indirect attributes should be resolved prior to calling
536 // this function
537 case DW_FORM_indirect:
538 OS << "DW_FORM_indirect";
539 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000540
Eric Christopher4c7765f2013-01-17 03:00:04 +0000541 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000542 case DW_FORM_sec_offset:
Eric Christopher4c7765f2013-01-17 03:00:04 +0000543 OS << format("0x%08x", (uint32_t)uvalue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000544 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000545
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000546 default:
547 OS << format("DW_FORM(0x%4.4x)", Form);
548 break;
549 }
550
Adrian Prantl0c36a752015-01-06 16:50:25 +0000551 if (cu_relative_offset) {
552 OS << " => {";
553 WithColor(OS, syntax::Address).get()
Greg Claytoncddab272016-10-31 16:46:02 +0000554 << format("0x%8.8" PRIx64, uvalue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000555 OS << "}";
556 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000557}
558
Greg Claytoncddab272016-10-31 16:46:02 +0000559void DWARFFormValue::dumpString(raw_ostream &OS) const {
560 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000561 if (DbgStr.hasValue()) {
562 raw_ostream &COS = WithColor(OS, syntax::String);
563 COS << '"';
564 COS.write_escaped(DbgStr.getValue());
565 COS << '"';
566 }
567}
568
Greg Claytoncddab272016-10-31 16:46:02 +0000569Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000570 if (!isFormClass(FC_String))
571 return None;
572 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000573 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000574 // FIXME: Add support for DW_FORM_GNU_strp_alt
575 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000576 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000577 uint32_t Offset = Value.uval;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000578 if (Form == DW_FORM_GNU_str_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000579 uint32_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000580 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
581 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000582 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000583 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000584 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
585 return Str;
586 }
587 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000588}
589
Greg Claytoncddab272016-10-31 16:46:02 +0000590Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000591 if (!isFormClass(FC_Address))
592 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000593 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000594 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000595 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000596 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000597 return None;
598 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000599 }
600 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000601}
602
Greg Claytoncddab272016-10-31 16:46:02 +0000603Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000604 if (!isFormClass(FC_Reference))
605 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000606 switch (Form) {
607 case DW_FORM_ref1:
608 case DW_FORM_ref2:
609 case DW_FORM_ref4:
610 case DW_FORM_ref8:
611 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000612 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000613 return None;
614 return Value.uval + U->getOffset();
615 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000616 case DW_FORM_ref_sig8:
617 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000618 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000619 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000620 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000621 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000622}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000623
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000624Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
625 if (!isFormClass(FC_SectionOffset))
626 return None;
627 return Value.uval;
628}
629
630Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000631 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
632 || Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000633 return None;
634 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000635}
Frederic Rissf3549a22014-09-04 06:14:35 +0000636
Frederic Riss77f850e2015-03-04 22:07:41 +0000637Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
638 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Matt Arsenault45cbfa52015-10-21 21:10:12 +0000639 (Form == DW_FORM_udata && uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000640 return None;
641 switch (Form) {
642 case DW_FORM_data4:
643 return int32_t(Value.uval);
644 case DW_FORM_data2:
645 return int16_t(Value.uval);
646 case DW_FORM_data1:
647 return int8_t(Value.uval);
648 case DW_FORM_sdata:
649 case DW_FORM_data8:
650 default:
651 return Value.sval;
652 }
653}
654
Frederic Risseab17772014-09-04 06:35:09 +0000655Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000656 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
657 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000658 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000659}
660