blob: ed1f5f46dcfb8297bd1a048bc49efad477300709 [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
Paul Robinsonae2e6f372017-05-03 21:53:21 +000010#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Adrian Prantl0c36a752015-01-06 16:50:25 +000011#include "SyntaxHighlighting.h"
Alexey Samsonov48cbda52013-10-28 23:01:48 +000012#include "llvm/ADT/ArrayRef.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000013#include "llvm/ADT/None.h"
14#include "llvm/ADT/Optional.h"
Alexey Samsonov48cbda52013-10-28 23:01:48 +000015#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/Dwarf.h"
Zachary Turner82af9432015-01-30 18:07:45 +000017#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000018#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
19#include "llvm/DebugInfo/DWARF/DWARFUnit.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[] = {
Paul Robinsonae2e6f372017-05-03 21:53:21 +000032 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) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +000086 case dwarf::DwarfFormat::DWARF32:
87 return 4;
88 case dwarf::DwarfFormat::DWARF64:
89 return 8;
Greg Clayton82f12b12016-11-11 16:21:37 +000090 }
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) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000100 case DW_FORM_addr:
101 if (U)
102 return U->getAddressByteSize();
103 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +0000104
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000105 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;
Greg Clayton82f12b12016-11-11 16:21:37 +0000122
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000123 case DW_FORM_ref_addr:
124 if (U)
125 return U->getRefAddrByteSize();
126 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +0000127
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000128 case DW_FORM_flag:
129 case DW_FORM_data1:
130 case DW_FORM_ref1:
131 case DW_FORM_strx1:
132 case DW_FORM_addrx1:
133 return 1;
Greg Clayton82f12b12016-11-11 16:21:37 +0000134
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000135 case DW_FORM_data2:
136 case DW_FORM_ref2:
137 case DW_FORM_strx2:
138 case DW_FORM_addrx2:
139 return 2;
Greg Clayton82f12b12016-11-11 16:21:37 +0000140
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000141 case DW_FORM_data4:
142 case DW_FORM_ref4:
143 case DW_FORM_ref_sup4:
144 case DW_FORM_strx4:
145 case DW_FORM_addrx4:
146 return 4;
Greg Clayton82f12b12016-11-11 16:21:37 +0000147
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000148 case DW_FORM_strp:
149 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:
154 if (U)
155 return U->getDwarfOffsetByteSize();
156 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +0000157
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000158 case DW_FORM_data8:
159 case DW_FORM_ref8:
160 case DW_FORM_ref_sig8:
161 case DW_FORM_ref_sup8:
162 return 8;
Greg Clayton82f12b12016-11-11 16:21:37 +0000163
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000164 case DW_FORM_flag_present:
165 return 0;
Greg Clayton82f12b12016-11-11 16:21:37 +0000166
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000167 case DW_FORM_data16:
168 return 16;
Greg Clayton82f12b12016-11-11 16:21:37 +0000169
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000170 case DW_FORM_implicit_const:
171 // The implicit value is stored in the abbreviation as a SLEB128, and
172 // there no data in debug info.
173 return 0;
Greg Clayton82f12b12016-11-11 16:21:37 +0000174
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000175 default:
176 llvm_unreachable("Handle this form in this switch statement");
Greg Clayton82f12b12016-11-11 16:21:37 +0000177 }
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) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000187 // Blocks of inlined data that have a length field and the data bytes
188 // 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:
229 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:
237 case DW_FORM_sec_offset:
238 case DW_FORM_strp:
239 case DW_FORM_strp_sup:
240 case DW_FORM_line_strp:
241 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;
Greg Clayton82f12b12016-11-11 16:21:37 +0000245 return true;
246 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000247 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000248
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000249 // signed or unsigned LEB 128 values.
250 case DW_FORM_sdata:
251 DebugInfoData.getSLEB128(OffsetPtr);
252 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000253
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000254 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;
Greg Clayton82f12b12016-11-11 16:21:37 +0000264
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000265 case DW_FORM_indirect:
266 Indirect = true;
267 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
268 break;
Greg Clayton82f12b12016-11-11 16:21:37 +0000269
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000270 default:
271 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000272 }
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:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000304 case DW_FORM_strx:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000305 return (FC == FC_String);
Victor Leschukcbddae72017-01-10 21:18:26 +0000306 case DW_FORM_implicit_const:
307 return (FC == FC_Constant);
Greg Clayton6c273762016-10-27 16:32:04 +0000308 default:
309 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000310 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000311 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
312 // Don't check for DWARF version here, as some producers may still do this
Greg Clayton48432cf2017-05-01 22:07:02 +0000313 // by mistake. Also accept DW_FORM_strp since this is .debug_str section
314 // offset.
315 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
316 Form == DW_FORM_strp) &&
Benjamin Kramer68a29562015-05-25 13:28:03 +0000317 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000318}
319
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000320bool DWARFFormValue::extractValue(const DataExtractor &Data,
321 uint32_t *OffsetPtr, const DWARFUnit *CU) {
322 U = CU;
323 bool Indirect = false;
324 bool IsBlock = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000325 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000326 // Read the value for the form into value and follow and DW_FORM_indirect
327 // instances we run into
328 do {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000329 Indirect = false;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000330 switch (Form) {
331 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000332 case DW_FORM_ref_addr: {
Greg Claytoncddab272016-10-31 16:46:02 +0000333 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000334 return false;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000335 uint16_t AddrSize = (Form == DW_FORM_addr) ? U->getAddressByteSize()
336 : U->getRefAddrByteSize();
George Rimara25d3292017-05-27 18:10:23 +0000337 Value.uval = getRelocatedValue(Data, AddrSize, OffsetPtr,
338 U->getRelocMap(), &Value.SectionIndex);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000339 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000340 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000341 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000342 case DW_FORM_block:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000343 Value.uval = Data.getULEB128(OffsetPtr);
344 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000345 break;
346 case DW_FORM_block1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000347 Value.uval = Data.getU8(OffsetPtr);
348 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000349 break;
350 case DW_FORM_block2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000351 Value.uval = Data.getU16(OffsetPtr);
352 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000353 break;
354 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000355 Value.uval = Data.getU32(OffsetPtr);
356 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000357 break;
358 case DW_FORM_data1:
359 case DW_FORM_ref1:
360 case DW_FORM_flag:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000361 case DW_FORM_strx1:
362 case DW_FORM_addrx1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000363 Value.uval = Data.getU8(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000364 break;
365 case DW_FORM_data2:
366 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000367 case DW_FORM_strx2:
368 case DW_FORM_addrx2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000369 Value.uval = Data.getU16(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000370 break;
371 case DW_FORM_data4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000372 case DW_FORM_ref4:
373 case DW_FORM_ref_sup4:
374 case DW_FORM_strx4:
375 case DW_FORM_addrx4: {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000376 const RelocAddrMap *RelocMap = U ? U->getRelocMap() : nullptr;
377 Value.uval = getRelocatedValue(Data, 4, OffsetPtr, RelocMap);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000378 break;
David Blaikie18e73502013-06-19 21:37:13 +0000379 }
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000380 case DW_FORM_data8:
381 case DW_FORM_ref8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000382 case DW_FORM_ref_sup8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000383 Value.uval = Data.getU64(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000384 break;
385 case DW_FORM_sdata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000386 Value.sval = Data.getSLEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000387 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000388 case DW_FORM_udata:
389 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000390 Value.uval = Data.getULEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000391 break;
392 case DW_FORM_string:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000393 Value.cstr = Data.getCStr(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000394 break;
395 case DW_FORM_indirect:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000396 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
397 Indirect = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000398 break;
Greg Clayton04c19282016-11-11 17:38:14 +0000399 case DW_FORM_strp:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000400 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000401 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000402 case DW_FORM_GNU_strp_alt:
403 case DW_FORM_line_strp:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000404 case DW_FORM_strp_sup: {
Greg Claytoncddab272016-10-31 16:46:02 +0000405 if (!U)
Greg Clayton82f12b12016-11-11 16:21:37 +0000406 return false;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000407 Value.uval = getRelocatedValue(Data, U->getDwarfOffsetByteSize(),
408 OffsetPtr, U->getRelocMap());
Eric Christopherd999bb72012-08-24 01:14:23 +0000409 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000410 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000411 case DW_FORM_flag_present:
412 Value.uval = 1;
413 break;
414 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000415 Value.uval = Data.getU64(OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000416 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000417 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000418 case DW_FORM_GNU_str_index:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000419 case DW_FORM_strx:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000420 Value.uval = Data.getULEB128(OffsetPtr);
Eric Christopher59c53c22012-11-16 23:44:11 +0000421 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000422 default:
Greg Clayton0e62ee72017-01-13 00:13:42 +0000423 // DWARFFormValue::skipValue() will have caught this and caused all
424 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
425 llvm_unreachable("unsupported form");
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000426 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000427 } while (Indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000428
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000429 if (IsBlock) {
430 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000431 Value.data = nullptr;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000432 if (!Str.empty()) {
433 Value.data = reinterpret_cast<const uint8_t *>(Str.data());
434 *OffsetPtr += Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000435 }
436 }
437
438 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000439}
440
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000441bool DWARFFormValue::skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
442 const DWARFUnit *U) const {
443 return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000444}
445
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000446bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
447 uint32_t *OffsetPtr, const DWARFUnit *U) {
448 return skipFormValue(Form, DebugInfoData, OffsetPtr, U);
David Blaikiead07b5d2015-12-04 17:20:04 +0000449}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000450
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000451bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
452 uint32_t *OffsetPtr, uint16_t Version,
Greg Clayton82f12b12016-11-11 16:21:37 +0000453 uint8_t AddrSize,
454 llvm::dwarf::DwarfFormat Format) {
455 FormSizeHelper FSH(Version, AddrSize, Format);
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000456 return skipFormValue(Form, DebugInfoData, OffsetPtr, &FSH);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000457}
458
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000459void DWARFFormValue::dump(raw_ostream &OS) const {
460 uint64_t UValue = Value.uval;
461 bool CURelativeOffset = false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000462
Benjamin Kramereaa74332011-09-13 21:47:32 +0000463 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000464 case DW_FORM_addr:
465 OS << format("0x%016" PRIx64, UValue);
466 break;
Eric Christopher962c9082013-01-15 23:56:56 +0000467 case DW_FORM_GNU_addr_index: {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000468 OS << format(" indexed (%8.8x) address = ", (uint32_t)UValue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000469 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000470 if (U == nullptr)
471 OS << "<invalid dwarf unit>";
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000472 else if (U->getAddrOffsetSectionItem(UValue, Address))
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000473 OS << format("0x%016" PRIx64, Address);
474 else
Eric Christopher962c9082013-01-15 23:56:56 +0000475 OS << "<no .debug_addr section>";
476 break;
477 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000478 case DW_FORM_flag_present:
479 OS << "true";
480 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000481 case DW_FORM_flag:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000482 case DW_FORM_data1:
483 OS << format("0x%02x", (uint8_t)UValue);
484 break;
485 case DW_FORM_data2:
486 OS << format("0x%04x", (uint16_t)UValue);
487 break;
488 case DW_FORM_data4:
489 OS << format("0x%08x", (uint32_t)UValue);
490 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000491 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000492 case DW_FORM_data8:
493 OS << format("0x%016" PRIx64, UValue);
494 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000495 case DW_FORM_string:
496 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000497 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000498 OS << '"';
499 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000500 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000501 case DW_FORM_block:
502 case DW_FORM_block1:
503 case DW_FORM_block2:
504 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000505 if (UValue > 0) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000506 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000507 case DW_FORM_exprloc:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000508 case DW_FORM_block:
509 OS << format("<0x%" PRIx64 "> ", UValue);
510 break;
511 case DW_FORM_block1:
512 OS << format("<0x%2.2x> ", (uint8_t)UValue);
513 break;
514 case DW_FORM_block2:
515 OS << format("<0x%4.4x> ", (uint16_t)UValue);
516 break;
517 case DW_FORM_block4:
518 OS << format("<0x%8.8x> ", (uint32_t)UValue);
519 break;
520 default:
521 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000522 }
523
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000524 const uint8_t *DataPtr = Value.data;
525 if (DataPtr) {
526 // UValue contains size of block
527 const uint8_t *EndDataPtr = DataPtr + UValue;
528 while (DataPtr < EndDataPtr) {
529 OS << format("%2.2x ", *DataPtr);
530 ++DataPtr;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000531 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000532 } else
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000533 OS << "NULL";
534 }
535 break;
536
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000537 case DW_FORM_sdata:
538 OS << Value.sval;
539 break;
540 case DW_FORM_udata:
541 OS << Value.uval;
542 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000543 case DW_FORM_strp:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000544 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000545 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000546 break;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000547 case DW_FORM_strx:
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000548 case DW_FORM_GNU_str_index:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000549 OS << format(" indexed (%8.8x) string = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000550 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000551 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000552 case DW_FORM_GNU_strp_alt:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000553 OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000554 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000555 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000556 case DW_FORM_ref_addr:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000557 OS << format("0x%016" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000558 break;
559 case DW_FORM_ref1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000560 CURelativeOffset = true;
561 OS << format("cu + 0x%2.2x", (uint8_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000562 break;
563 case DW_FORM_ref2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000564 CURelativeOffset = true;
565 OS << format("cu + 0x%4.4x", (uint16_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000566 break;
567 case DW_FORM_ref4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000568 CURelativeOffset = true;
569 OS << format("cu + 0x%4.4x", (uint32_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000570 break;
571 case DW_FORM_ref8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000572 CURelativeOffset = true;
573 OS << format("cu + 0x%8.8" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000574 break;
575 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000576 CURelativeOffset = true;
577 OS << format("cu + 0x%" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000578 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000579 case DW_FORM_GNU_ref_alt:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000580 OS << format("<alt 0x%" PRIx64 ">", UValue);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000581 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000582
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000583 // All DW_FORM_indirect attributes should be resolved prior to calling
584 // this function
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000585 case DW_FORM_indirect:
586 OS << "DW_FORM_indirect";
587 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000588
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000589 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000590 case DW_FORM_sec_offset:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000591 OS << format("0x%08x", (uint32_t)UValue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000592 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000593
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000594 default:
595 OS << format("DW_FORM(0x%4.4x)", Form);
596 break;
597 }
598
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000599 if (CURelativeOffset) {
Adrian Prantl0c36a752015-01-06 16:50:25 +0000600 OS << " => {";
601 WithColor(OS, syntax::Address).get()
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000602 << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000603 OS << "}";
604 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000605}
606
Greg Claytoncddab272016-10-31 16:46:02 +0000607void DWARFFormValue::dumpString(raw_ostream &OS) const {
608 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000609 if (DbgStr.hasValue()) {
610 raw_ostream &COS = WithColor(OS, syntax::String);
611 COS << '"';
612 COS.write_escaped(DbgStr.getValue());
613 COS << '"';
614 }
615}
616
Greg Claytoncddab272016-10-31 16:46:02 +0000617Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000618 if (!isFormClass(FC_String))
619 return None;
620 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000621 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000622 // FIXME: Add support for DW_FORM_GNU_strp_alt
623 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000624 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000625 uint32_t Offset = Value.uval;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000626 if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx) {
627 uint64_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000628 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
629 return None;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000630 StrOffset += U->getStringOffsetSectionRelocation(Offset);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000631 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000632 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000633 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
634 return Str;
635 }
636 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000637}
638
Greg Claytoncddab272016-10-31 16:46:02 +0000639Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000640 if (!isFormClass(FC_Address))
641 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000642 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000643 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000644 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000645 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000646 return None;
647 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000648 }
649 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000650}
651
Greg Claytoncddab272016-10-31 16:46:02 +0000652Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000653 if (!isFormClass(FC_Reference))
654 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000655 switch (Form) {
656 case DW_FORM_ref1:
657 case DW_FORM_ref2:
658 case DW_FORM_ref4:
659 case DW_FORM_ref8:
660 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000661 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000662 return None;
663 return Value.uval + U->getOffset();
664 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000665 case DW_FORM_ref_sig8:
666 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000667 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000668 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000669 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000670 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000671}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000672
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000673Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
674 if (!isFormClass(FC_SectionOffset))
675 return None;
676 return Value.uval;
677}
678
679Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000680 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
681 Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000682 return None;
683 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000684}
Frederic Rissf3549a22014-09-04 06:14:35 +0000685
Frederic Riss77f850e2015-03-04 22:07:41 +0000686Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
687 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000688 (Form == DW_FORM_udata &&
689 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000690 return None;
691 switch (Form) {
692 case DW_FORM_data4:
693 return int32_t(Value.uval);
694 case DW_FORM_data2:
695 return int16_t(Value.uval);
696 case DW_FORM_data1:
697 return int8_t(Value.uval);
698 case DW_FORM_sdata:
699 case DW_FORM_data8:
700 default:
701 return Value.sval;
702 }
703}
704
Frederic Risseab17772014-09-04 06:35:09 +0000705Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000706 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
707 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000708 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000709}
710
Chris Bienemane0e451d2016-12-22 22:44:27 +0000711Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
712 if (!isFormClass(FC_String) && Form == DW_FORM_string)
713 return None;
714 return Value.uval;
715}
716
717Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
718 if (!isFormClass(FC_Reference))
719 return None;
720 return Value.uval;
721}