blob: 6de57b999adcc3fd249e7588efd413306c562722 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFFormValue.cpp -------------------------------------------------===//
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Adrian Prantl0c36a752015-01-06 16:50:25 +000010#include "SyntaxHighlighting.h"
Alexey Samsonov48cbda52013-10-28 23:01:48 +000011#include "llvm/ADT/ArrayRef.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000012#include "llvm/ADT/None.h"
13#include "llvm/ADT/Optional.h"
Alexey Samsonov48cbda52013-10-28 23:01:48 +000014#include "llvm/ADT/StringRef.h"
Zachary Turner82af9432015-01-30 18:07:45 +000015#include "llvm/DebugInfo/DWARF/DWARFContext.h"
16#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000017#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000019#include "llvm/Support/Dwarf.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000020#include "llvm/Support/ErrorHandling.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000021#include "llvm/Support/Format.h"
22#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000023#include <cinttypes>
24#include <cstdint>
Matt Arsenault45cbfa52015-10-21 21:10:12 +000025#include <limits>
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000026
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000027using namespace llvm;
28using namespace dwarf;
Adrian Prantl0c36a752015-01-06 16:50:25 +000029using namespace syntax;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000030
Alexey Samsonov48cbda52013-10-28 23:01:48 +000031static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
32 DWARFFormValue::FC_Unknown, // 0x0
33 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
34 DWARFFormValue::FC_Unknown, // 0x02 unused
35 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
36 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
37 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
38 // --- These can be FC_SectionOffset in DWARF3 and below:
39 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
40 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
41 // ---
42 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
43 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
44 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
45 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
46 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
47 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
48 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
49 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
50 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
51 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
52 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
53 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
54 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
55 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
56 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
57 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
58 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
59 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
Alexey Samsonov48cbda52013-10-28 23:01:48 +000060};
61
Greg Clayton82f12b12016-11-11 16:21:37 +000062namespace {
63
64/// A helper class that can be used in DWARFFormValue.cpp functions that need
65/// to know the byte size of DW_FORM values that vary in size depending on the
66/// DWARF version, address byte size, or DWARF32 or DWARF64.
67class FormSizeHelper {
68 uint16_t Version;
69 uint8_t AddrSize;
70 llvm::dwarf::DwarfFormat Format;
71
72public:
73 FormSizeHelper(uint16_t V, uint8_t A, llvm::dwarf::DwarfFormat F)
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000074 : Version(V), AddrSize(A), Format(F) {}
75
Greg Clayton82f12b12016-11-11 16:21:37 +000076 uint8_t getAddressByteSize() const { return AddrSize; }
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000077
Greg Clayton82f12b12016-11-11 16:21:37 +000078 uint8_t getRefAddrByteSize() const {
79 if (Version == 2)
80 return AddrSize;
81 return getDwarfOffsetByteSize();
82 }
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000083
Greg Clayton82f12b12016-11-11 16:21:37 +000084 uint8_t getDwarfOffsetByteSize() const {
85 switch (Format) {
86 case dwarf::DwarfFormat::DWARF32:
87 return 4;
88 case dwarf::DwarfFormat::DWARF64:
89 return 8;
90 }
91 llvm_unreachable("Invalid Format value");
92 }
93};
94
95} // end anonymous namespace
96
97template <class T>
98static Optional<uint8_t> getFixedByteSize(dwarf::Form Form, const T *U) {
99 switch (Form) {
100 case DW_FORM_addr:
101 if (U)
102 return U->getAddressByteSize();
103 return None;
104
105 case DW_FORM_block: // ULEB128 length L followed by L bytes.
106 case DW_FORM_block1: // 1 byte length L followed by L bytes.
107 case DW_FORM_block2: // 2 byte length L followed by L bytes.
108 case DW_FORM_block4: // 4 byte length L followed by L bytes.
109 case DW_FORM_string: // C-string with null terminator.
110 case DW_FORM_sdata: // SLEB128.
111 case DW_FORM_udata: // ULEB128.
112 case DW_FORM_ref_udata: // ULEB128.
113 case DW_FORM_indirect: // ULEB128.
114 case DW_FORM_exprloc: // ULEB128 length L followed by L bytes.
115 case DW_FORM_strx: // ULEB128.
116 case DW_FORM_addrx: // ULEB128.
117 case DW_FORM_loclistx: // ULEB128.
118 case DW_FORM_rnglistx: // ULEB128.
119 case DW_FORM_GNU_addr_index: // ULEB128.
120 case DW_FORM_GNU_str_index: // ULEB128.
121 return None;
122
123 case DW_FORM_ref_addr:
124 if (U)
125 return U->getRefAddrByteSize();
126 return None;
127
128 case DW_FORM_flag:
129 case DW_FORM_data1:
130 case DW_FORM_ref1:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000131 case DW_FORM_strx1:
132 case DW_FORM_addrx1:
Greg Clayton82f12b12016-11-11 16:21:37 +0000133 return 1;
134
135 case DW_FORM_data2:
136 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000137 case DW_FORM_strx2:
138 case DW_FORM_addrx2:
Greg Clayton82f12b12016-11-11 16:21:37 +0000139 return 2;
140
141 case DW_FORM_data4:
142 case DW_FORM_ref4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000143 case DW_FORM_ref_sup4:
144 case DW_FORM_strx4:
145 case DW_FORM_addrx4:
Greg Clayton82f12b12016-11-11 16:21:37 +0000146 return 4;
147
Greg Clayton04c19282016-11-11 17:38:14 +0000148 case DW_FORM_strp:
Greg Clayton82f12b12016-11-11 16:21:37 +0000149 case DW_FORM_GNU_ref_alt:
150 case DW_FORM_GNU_strp_alt:
151 case DW_FORM_line_strp:
152 case DW_FORM_sec_offset:
153 case DW_FORM_strp_sup:
Greg Clayton82f12b12016-11-11 16:21:37 +0000154 if (U)
155 return U->getDwarfOffsetByteSize();
156 return None;
157
158 case DW_FORM_data8:
159 case DW_FORM_ref8:
160 case DW_FORM_ref_sig8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000161 case DW_FORM_ref_sup8:
Greg Clayton82f12b12016-11-11 16:21:37 +0000162 return 8;
163
164 case DW_FORM_flag_present:
165 return 0;
166
167 case DW_FORM_data16:
168 return 16;
169
170 case DW_FORM_implicit_const:
Victor Leschukcbddae72017-01-10 21:18:26 +0000171 // The implicit value is stored in the abbreviation as a SLEB128, and
Greg Clayton82f12b12016-11-11 16:21:37 +0000172 // there no data in debug info.
173 return 0;
174
175 default:
176 llvm_unreachable("Handle this form in this switch statement");
177 }
178 return None;
179}
180
181template <class T>
182static bool skipFormValue(dwarf::Form Form, const DataExtractor &DebugInfoData,
183 uint32_t *OffsetPtr, const T *U) {
184 bool Indirect = false;
185 do {
186 switch (Form) {
Greg Clayton04c19282016-11-11 17:38:14 +0000187 // Blocks of inlined data that have a length field and the data bytes
Greg Clayton82f12b12016-11-11 16:21:37 +0000188 // inlined in the .debug_info.
189 case DW_FORM_exprloc:
190 case DW_FORM_block: {
191 uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
192 *OffsetPtr += size;
193 return true;
194 }
195 case DW_FORM_block1: {
196 uint8_t size = DebugInfoData.getU8(OffsetPtr);
197 *OffsetPtr += size;
198 return true;
199 }
200 case DW_FORM_block2: {
201 uint16_t size = DebugInfoData.getU16(OffsetPtr);
202 *OffsetPtr += size;
203 return true;
204 }
205 case DW_FORM_block4: {
206 uint32_t size = DebugInfoData.getU32(OffsetPtr);
207 *OffsetPtr += size;
208 return true;
209 }
210
211 // Inlined NULL terminated C-strings.
212 case DW_FORM_string:
213 DebugInfoData.getCStr(OffsetPtr);
214 return true;
215
216 case DW_FORM_addr:
217 case DW_FORM_ref_addr:
218 case DW_FORM_flag_present:
219 case DW_FORM_data1:
220 case DW_FORM_data2:
221 case DW_FORM_data4:
222 case DW_FORM_data8:
223 case DW_FORM_flag:
224 case DW_FORM_ref1:
225 case DW_FORM_ref2:
226 case DW_FORM_ref4:
227 case DW_FORM_ref8:
228 case DW_FORM_ref_sig8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000229 case DW_FORM_ref_sup4:
230 case DW_FORM_ref_sup8:
231 case DW_FORM_strx1:
232 case DW_FORM_strx2:
233 case DW_FORM_strx4:
234 case DW_FORM_addrx1:
235 case DW_FORM_addrx2:
236 case DW_FORM_addrx4:
Greg Clayton82f12b12016-11-11 16:21:37 +0000237 case DW_FORM_sec_offset:
238 case DW_FORM_strp:
Greg Clayton04c19282016-11-11 17:38:14 +0000239 case DW_FORM_strp_sup:
240 case DW_FORM_line_strp:
Greg Clayton82f12b12016-11-11 16:21:37 +0000241 case DW_FORM_GNU_ref_alt:
242 case DW_FORM_GNU_strp_alt:
243 if (Optional<uint8_t> FixedSize = ::getFixedByteSize(Form, U)) {
244 *OffsetPtr += *FixedSize;
245 return true;
246 }
247 return false;
248
249 // signed or unsigned LEB 128 values.
250 case DW_FORM_sdata:
251 DebugInfoData.getSLEB128(OffsetPtr);
252 return true;
253
254 case DW_FORM_udata:
255 case DW_FORM_ref_udata:
256 case DW_FORM_strx:
257 case DW_FORM_addrx:
258 case DW_FORM_loclistx:
259 case DW_FORM_rnglistx:
260 case DW_FORM_GNU_addr_index:
261 case DW_FORM_GNU_str_index:
262 DebugInfoData.getULEB128(OffsetPtr);
263 return true;
264
265 case DW_FORM_indirect:
266 Indirect = true;
267 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
268 break;
269
270 default:
271 return false;
272 }
273 } while (Indirect);
274 return true;
275}
276
277Optional<uint8_t> DWARFFormValue::getFixedByteSize(dwarf::Form Form,
278 const DWARFUnit *U) {
279 return ::getFixedByteSize(Form, U);
280}
281
282Optional<uint8_t>
283DWARFFormValue::getFixedByteSize(dwarf::Form Form, uint16_t Version,
284 uint8_t AddrSize,
285 llvm::dwarf::DwarfFormat Format) {
286 FormSizeHelper FSH(Version, AddrSize, Format);
287 return ::getFixedByteSize(Form, &FSH);
288}
289
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000290bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
291 // First, check DWARF4 form classes.
Craig Topper0013be12015-09-21 05:32:41 +0000292 if (Form < makeArrayRef(DWARF4FormClasses).size() &&
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000293 DWARF4FormClasses[Form] == FC)
294 return true;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000295 // Check more forms from DWARF4 and DWARF5 proposals.
296 switch (Form) {
297 case DW_FORM_ref_sig8:
298 case DW_FORM_GNU_ref_alt:
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000299 return (FC == FC_Reference);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000300 case DW_FORM_GNU_addr_index:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000301 return (FC == FC_Address);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000302 case DW_FORM_GNU_str_index:
303 case DW_FORM_GNU_strp_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000304 return (FC == FC_String);
Victor Leschukcbddae72017-01-10 21:18:26 +0000305 case DW_FORM_implicit_const:
306 return (FC == FC_Constant);
Greg Clayton6c273762016-10-27 16:32:04 +0000307 default:
308 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000309 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000310 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
311 // Don't check for DWARF version here, as some producers may still do this
312 // by mistake.
Benjamin Kramer68a29562015-05-25 13:28:03 +0000313 return (Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
314 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000315}
316
Greg Claytoncddab272016-10-31 16:46:02 +0000317bool DWARFFormValue::extractValue(const DataExtractor &data,
318 uint32_t *offset_ptr,
David Blaikie07e22442013-09-23 22:44:40 +0000319 const DWARFUnit *cu) {
Greg Claytoncddab272016-10-31 16:46:02 +0000320 U = cu;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000321 bool indirect = false;
322 bool is_block = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000323 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000324 // Read the value for the form into value and follow and DW_FORM_indirect
325 // instances we run into
326 do {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000327 indirect = false;
328 switch (Form) {
329 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000330 case DW_FORM_ref_addr: {
Greg Claytoncddab272016-10-31 16:46:02 +0000331 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000332 return false;
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000333 uint16_t AddrSize =
334 (Form == DW_FORM_addr)
Greg Claytoncddab272016-10-31 16:46:02 +0000335 ? U->getAddressByteSize()
336 : U->getRefAddrByteSize();
337 RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr);
338 if (AI != U->getRelocMap()->end()) {
Greg Clayton82f12b12016-11-11 16:21:37 +0000339 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + AI->second.second;
Eric Christopher7370b552012-11-12 21:40:38 +0000340 } else
Alexey Samsonovd60859b2013-04-09 14:09:42 +0000341 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000342 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000343 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000344 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000345 case DW_FORM_block:
346 Value.uval = data.getULEB128(offset_ptr);
347 is_block = true;
348 break;
349 case DW_FORM_block1:
350 Value.uval = data.getU8(offset_ptr);
351 is_block = true;
352 break;
353 case DW_FORM_block2:
354 Value.uval = data.getU16(offset_ptr);
355 is_block = true;
356 break;
357 case DW_FORM_block4:
358 Value.uval = data.getU32(offset_ptr);
359 is_block = true;
360 break;
361 case DW_FORM_data1:
362 case DW_FORM_ref1:
363 case DW_FORM_flag:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000364 case DW_FORM_strx1:
365 case DW_FORM_addrx1:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000366 Value.uval = data.getU8(offset_ptr);
367 break;
368 case DW_FORM_data2:
369 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000370 case DW_FORM_strx2:
371 case DW_FORM_addrx2:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000372 Value.uval = data.getU16(offset_ptr);
373 break;
374 case DW_FORM_data4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000375 case DW_FORM_ref4:
376 case DW_FORM_ref_sup4:
377 case DW_FORM_strx4:
378 case DW_FORM_addrx4: {
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000379 Value.uval = data.getU32(offset_ptr);
Greg Claytoncddab272016-10-31 16:46:02 +0000380 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000381 break;
Greg Claytoncddab272016-10-31 16:46:02 +0000382 RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr-4);
383 if (AI != U->getRelocMap()->end())
David Blaikie18e73502013-06-19 21:37:13 +0000384 Value.uval += AI->second.second;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000385 break;
David Blaikie18e73502013-06-19 21:37:13 +0000386 }
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000387 case DW_FORM_data8:
388 case DW_FORM_ref8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000389 case DW_FORM_ref_sup8:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000390 Value.uval = data.getU64(offset_ptr);
391 break;
392 case DW_FORM_sdata:
393 Value.sval = data.getSLEB128(offset_ptr);
394 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000395 case DW_FORM_udata:
396 case DW_FORM_ref_udata:
397 Value.uval = data.getULEB128(offset_ptr);
398 break;
399 case DW_FORM_string:
400 Value.cstr = data.getCStr(offset_ptr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000401 break;
402 case DW_FORM_indirect:
Greg Clayton6c273762016-10-27 16:32:04 +0000403 Form = static_cast<dwarf::Form>(data.getULEB128(offset_ptr));
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000404 indirect = true;
405 break;
Greg Clayton04c19282016-11-11 17:38:14 +0000406 case DW_FORM_strp:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000407 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000408 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000409 case DW_FORM_GNU_strp_alt:
410 case DW_FORM_line_strp:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000411 case DW_FORM_strp_sup: {
Greg Claytoncddab272016-10-31 16:46:02 +0000412 if (!U)
Greg Clayton82f12b12016-11-11 16:21:37 +0000413 return false;
414 RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr);
415 uint8_t Size = U->getDwarfOffsetByteSize();
416 Value.uval = data.getUnsigned(offset_ptr, Size);
Greg Claytoncddab272016-10-31 16:46:02 +0000417 if (AI != U->getRelocMap()->end())
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000418 Value.uval += AI->second.second;
Eric Christopherd999bb72012-08-24 01:14:23 +0000419 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000420 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000421 case DW_FORM_flag_present:
422 Value.uval = 1;
423 break;
424 case DW_FORM_ref_sig8:
425 Value.uval = data.getU64(offset_ptr);
426 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000427 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000428 case DW_FORM_GNU_str_index:
429 Value.uval = data.getULEB128(offset_ptr);
430 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000431 default:
Greg Clayton0e62ee72017-01-13 00:13:42 +0000432 // DWARFFormValue::skipValue() will have caught this and caused all
433 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
434 llvm_unreachable("unsupported form");
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000435 }
Benjamin Kramereaa74332011-09-13 21:47:32 +0000436 } while (indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000437
Benjamin Kramereaa74332011-09-13 21:47:32 +0000438 if (is_block) {
439 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000440 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000441 if (!str.empty()) {
442 Value.data = reinterpret_cast<const uint8_t *>(str.data());
443 *offset_ptr += Value.uval;
444 }
445 }
446
447 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000448}
449
Greg Clayton82f12b12016-11-11 16:21:37 +0000450bool DWARFFormValue::skipValue(DataExtractor DebugInfoData,
451 uint32_t *offset_ptr, const DWARFUnit *U) const {
452 return DWARFFormValue::skipValue(Form, DebugInfoData, offset_ptr, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000453}
454
Greg Clayton82f12b12016-11-11 16:21:37 +0000455bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
456 uint32_t *offset_ptr, const DWARFUnit *U) {
457 return skipFormValue(form, DebugInfoData, offset_ptr, U);
David Blaikiead07b5d2015-12-04 17:20:04 +0000458}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000459
Greg Clayton82f12b12016-11-11 16:21:37 +0000460bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
461 uint32_t *offset_ptr, uint16_t Version,
462 uint8_t AddrSize,
463 llvm::dwarf::DwarfFormat Format) {
464 FormSizeHelper FSH(Version, AddrSize, Format);
465 return skipFormValue(form, DebugInfoData, offset_ptr, &FSH);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000466}
467
468void
Greg Claytoncddab272016-10-31 16:46:02 +0000469DWARFFormValue::dump(raw_ostream &OS) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000470 uint64_t uvalue = Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000471 bool cu_relative_offset = false;
472
Benjamin Kramereaa74332011-09-13 21:47:32 +0000473 switch (Form) {
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000474 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher962c9082013-01-15 23:56:56 +0000475 case DW_FORM_GNU_addr_index: {
Eric Christopher962c9082013-01-15 23:56:56 +0000476 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000477 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000478 if (U == nullptr)
479 OS << "<invalid dwarf unit>";
480 else if (U->getAddrOffsetSectionItem(uvalue, Address))
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000481 OS << format("0x%016" PRIx64, Address);
482 else
Eric Christopher962c9082013-01-15 23:56:56 +0000483 OS << "<no .debug_addr section>";
484 break;
485 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000486 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000487 case DW_FORM_flag:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000488 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
489 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
490 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000491 case DW_FORM_ref_sig8:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000492 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000493 case DW_FORM_string:
494 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000495 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000496 OS << '"';
497 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000498 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000499 case DW_FORM_block:
500 case DW_FORM_block1:
501 case DW_FORM_block2:
502 case DW_FORM_block4:
503 if (uvalue > 0) {
504 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000505 case DW_FORM_exprloc:
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000506 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000507 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
508 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
509 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
510 default: break;
511 }
512
513 const uint8_t* data_ptr = Value.data;
514 if (data_ptr) {
515 // uvalue contains size of block
516 const uint8_t* end_data_ptr = data_ptr + uvalue;
517 while (data_ptr < end_data_ptr) {
518 OS << format("%2.2x ", *data_ptr);
519 ++data_ptr;
520 }
521 }
522 else
523 OS << "NULL";
524 }
525 break;
526
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000527 case DW_FORM_sdata: OS << Value.sval; break;
528 case DW_FORM_udata: OS << Value.uval; break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000529 case DW_FORM_strp:
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000530 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000531 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000532 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000533 case DW_FORM_GNU_str_index:
Eric Christopher2cbd5762013-01-07 19:32:41 +0000534 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000535 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000536 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000537 case DW_FORM_GNU_strp_alt:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000538 OS << format("alt indirect string, offset: 0x%" PRIx64 "", uvalue);
Greg Claytoncddab272016-10-31 16:46:02 +0000539 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000540 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000541 case DW_FORM_ref_addr:
Benjamin Kramer79730ad2011-11-05 16:01:13 +0000542 OS << format("0x%016" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000543 break;
544 case DW_FORM_ref1:
545 cu_relative_offset = true;
546 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
547 break;
548 case DW_FORM_ref2:
549 cu_relative_offset = true;
550 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
551 break;
552 case DW_FORM_ref4:
553 cu_relative_offset = true;
554 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
555 break;
556 case DW_FORM_ref8:
557 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000558 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000559 break;
560 case DW_FORM_ref_udata:
561 cu_relative_offset = true;
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000562 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000563 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000564 case DW_FORM_GNU_ref_alt:
565 OS << format("<alt 0x%" PRIx64 ">", uvalue);
566 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000567
568 // All DW_FORM_indirect attributes should be resolved prior to calling
569 // this function
570 case DW_FORM_indirect:
571 OS << "DW_FORM_indirect";
572 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000573
Eric Christopher4c7765f2013-01-17 03:00:04 +0000574 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000575 case DW_FORM_sec_offset:
Eric Christopher4c7765f2013-01-17 03:00:04 +0000576 OS << format("0x%08x", (uint32_t)uvalue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000577 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000578
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000579 default:
580 OS << format("DW_FORM(0x%4.4x)", Form);
581 break;
582 }
583
Adrian Prantl0c36a752015-01-06 16:50:25 +0000584 if (cu_relative_offset) {
585 OS << " => {";
586 WithColor(OS, syntax::Address).get()
Greg Claytoncddab272016-10-31 16:46:02 +0000587 << format("0x%8.8" PRIx64, uvalue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000588 OS << "}";
589 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000590}
591
Greg Claytoncddab272016-10-31 16:46:02 +0000592void DWARFFormValue::dumpString(raw_ostream &OS) const {
593 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000594 if (DbgStr.hasValue()) {
595 raw_ostream &COS = WithColor(OS, syntax::String);
596 COS << '"';
597 COS.write_escaped(DbgStr.getValue());
598 COS << '"';
599 }
600}
601
Greg Claytoncddab272016-10-31 16:46:02 +0000602Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000603 if (!isFormClass(FC_String))
604 return None;
605 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000606 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000607 // FIXME: Add support for DW_FORM_GNU_strp_alt
608 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000609 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000610 uint32_t Offset = Value.uval;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000611 if (Form == DW_FORM_GNU_str_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000612 uint32_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000613 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
614 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000615 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000616 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000617 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
618 return Str;
619 }
620 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000621}
622
Greg Claytoncddab272016-10-31 16:46:02 +0000623Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000624 if (!isFormClass(FC_Address))
625 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000626 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000627 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000628 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000629 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000630 return None;
631 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000632 }
633 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000634}
635
Greg Claytoncddab272016-10-31 16:46:02 +0000636Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000637 if (!isFormClass(FC_Reference))
638 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000639 switch (Form) {
640 case DW_FORM_ref1:
641 case DW_FORM_ref2:
642 case DW_FORM_ref4:
643 case DW_FORM_ref8:
644 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000645 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000646 return None;
647 return Value.uval + U->getOffset();
648 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000649 case DW_FORM_ref_sig8:
650 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000651 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000652 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000653 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000654 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000655}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000656
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000657Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
658 if (!isFormClass(FC_SectionOffset))
659 return None;
660 return Value.uval;
661}
662
663Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000664 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
665 || Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000666 return None;
667 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000668}
Frederic Rissf3549a22014-09-04 06:14:35 +0000669
Frederic Riss77f850e2015-03-04 22:07:41 +0000670Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
671 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Matt Arsenault45cbfa52015-10-21 21:10:12 +0000672 (Form == DW_FORM_udata && uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000673 return None;
674 switch (Form) {
675 case DW_FORM_data4:
676 return int32_t(Value.uval);
677 case DW_FORM_data2:
678 return int16_t(Value.uval);
679 case DW_FORM_data1:
680 return int8_t(Value.uval);
681 case DW_FORM_sdata:
682 case DW_FORM_data8:
683 default:
684 return Value.sval;
685 }
686}
687
Frederic Risseab17772014-09-04 06:35:09 +0000688Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000689 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
690 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000691 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000692}
693
Chris Bienemane0e451d2016-12-22 22:44:27 +0000694Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
695 if (!isFormClass(FC_String) && Form == DW_FORM_string)
696 return None;
697 return Value.uval;
698}
699
700Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
701 if (!isFormClass(FC_Reference))
702 return None;
703 return Value.uval;
704}