blob: 0963d7bfd7135e46bd4a78e7909c713f10d9a3ca [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 Turner82af9432015-01-30 18:07:45 +000016#include "llvm/DebugInfo/DWARF/DWARFContext.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[] = {
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:
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
Greg Clayton48432cf2017-05-01 22:07:02 +0000312 // by mistake. Also accept DW_FORM_strp since this is .debug_str section
313 // offset.
314 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
315 Form == DW_FORM_strp) &&
Benjamin Kramer68a29562015-05-25 13:28:03 +0000316 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000317}
318
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000319bool DWARFFormValue::extractValue(const DataExtractor &Data,
320 uint32_t *OffsetPtr, const DWARFUnit *CU) {
321 U = CU;
322 bool Indirect = false;
323 bool IsBlock = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000324 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000325 // Read the value for the form into value and follow and DW_FORM_indirect
326 // instances we run into
327 do {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000328 Indirect = false;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000329 switch (Form) {
330 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000331 case DW_FORM_ref_addr: {
Greg Claytoncddab272016-10-31 16:46:02 +0000332 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000333 return false;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000334 uint16_t AddrSize = (Form == DW_FORM_addr) ? U->getAddressByteSize()
335 : U->getRefAddrByteSize();
George Rimara25d3292017-05-27 18:10:23 +0000336 Value.uval = getRelocatedValue(Data, AddrSize, OffsetPtr,
337 U->getRelocMap(), &Value.SectionIndex);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000338 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000339 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000340 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000341 case DW_FORM_block:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000342 Value.uval = Data.getULEB128(OffsetPtr);
343 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000344 break;
345 case DW_FORM_block1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000346 Value.uval = Data.getU8(OffsetPtr);
347 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000348 break;
349 case DW_FORM_block2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000350 Value.uval = Data.getU16(OffsetPtr);
351 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000352 break;
353 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000354 Value.uval = Data.getU32(OffsetPtr);
355 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000356 break;
357 case DW_FORM_data1:
358 case DW_FORM_ref1:
359 case DW_FORM_flag:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000360 case DW_FORM_strx1:
361 case DW_FORM_addrx1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000362 Value.uval = Data.getU8(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000363 break;
364 case DW_FORM_data2:
365 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000366 case DW_FORM_strx2:
367 case DW_FORM_addrx2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000368 Value.uval = Data.getU16(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000369 break;
370 case DW_FORM_data4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000371 case DW_FORM_ref4:
372 case DW_FORM_ref_sup4:
373 case DW_FORM_strx4:
374 case DW_FORM_addrx4: {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000375 const RelocAddrMap *RelocMap = U ? U->getRelocMap() : nullptr;
376 Value.uval = getRelocatedValue(Data, 4, OffsetPtr, RelocMap);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000377 break;
David Blaikie18e73502013-06-19 21:37:13 +0000378 }
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000379 case DW_FORM_data8:
380 case DW_FORM_ref8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000381 case DW_FORM_ref_sup8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000382 Value.uval = Data.getU64(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000383 break;
384 case DW_FORM_sdata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000385 Value.sval = Data.getSLEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000386 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000387 case DW_FORM_udata:
388 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000389 Value.uval = Data.getULEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000390 break;
391 case DW_FORM_string:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000392 Value.cstr = Data.getCStr(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000393 break;
394 case DW_FORM_indirect:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000395 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
396 Indirect = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000397 break;
Greg Clayton04c19282016-11-11 17:38:14 +0000398 case DW_FORM_strp:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000399 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000400 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000401 case DW_FORM_GNU_strp_alt:
402 case DW_FORM_line_strp:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000403 case DW_FORM_strp_sup: {
Greg Claytoncddab272016-10-31 16:46:02 +0000404 if (!U)
Greg Clayton82f12b12016-11-11 16:21:37 +0000405 return false;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000406 Value.uval = getRelocatedValue(Data, U->getDwarfOffsetByteSize(),
407 OffsetPtr, U->getRelocMap());
Eric Christopherd999bb72012-08-24 01:14:23 +0000408 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000409 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000410 case DW_FORM_flag_present:
411 Value.uval = 1;
412 break;
413 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000414 Value.uval = Data.getU64(OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000415 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000416 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000417 case DW_FORM_GNU_str_index:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000418 Value.uval = Data.getULEB128(OffsetPtr);
Eric Christopher59c53c22012-11-16 23:44:11 +0000419 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000420 default:
Greg Clayton0e62ee72017-01-13 00:13:42 +0000421 // DWARFFormValue::skipValue() will have caught this and caused all
422 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
423 llvm_unreachable("unsupported form");
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000424 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000425 } while (Indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000426
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000427 if (IsBlock) {
428 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000429 Value.data = nullptr;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000430 if (!Str.empty()) {
431 Value.data = reinterpret_cast<const uint8_t *>(Str.data());
432 *OffsetPtr += Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000433 }
434 }
435
436 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000437}
438
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000439bool DWARFFormValue::skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
440 const DWARFUnit *U) const {
441 return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000442}
443
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000444bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
445 uint32_t *OffsetPtr, const DWARFUnit *U) {
446 return skipFormValue(Form, DebugInfoData, OffsetPtr, U);
David Blaikiead07b5d2015-12-04 17:20:04 +0000447}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000448
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000449bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
450 uint32_t *OffsetPtr, uint16_t Version,
Greg Clayton82f12b12016-11-11 16:21:37 +0000451 uint8_t AddrSize,
452 llvm::dwarf::DwarfFormat Format) {
453 FormSizeHelper FSH(Version, AddrSize, Format);
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000454 return skipFormValue(Form, DebugInfoData, OffsetPtr, &FSH);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000455}
456
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000457void DWARFFormValue::dump(raw_ostream &OS) const {
458 uint64_t UValue = Value.uval;
459 bool CURelativeOffset = false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000460
Benjamin Kramereaa74332011-09-13 21:47:32 +0000461 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000462 case DW_FORM_addr:
463 OS << format("0x%016" PRIx64, UValue);
464 break;
Eric Christopher962c9082013-01-15 23:56:56 +0000465 case DW_FORM_GNU_addr_index: {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000466 OS << format(" indexed (%8.8x) address = ", (uint32_t)UValue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000467 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000468 if (U == nullptr)
469 OS << "<invalid dwarf unit>";
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000470 else if (U->getAddrOffsetSectionItem(UValue, Address))
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000471 OS << format("0x%016" PRIx64, Address);
472 else
Eric Christopher962c9082013-01-15 23:56:56 +0000473 OS << "<no .debug_addr section>";
474 break;
475 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000476 case DW_FORM_flag_present:
477 OS << "true";
478 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000479 case DW_FORM_flag:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000480 case DW_FORM_data1:
481 OS << format("0x%02x", (uint8_t)UValue);
482 break;
483 case DW_FORM_data2:
484 OS << format("0x%04x", (uint16_t)UValue);
485 break;
486 case DW_FORM_data4:
487 OS << format("0x%08x", (uint32_t)UValue);
488 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000489 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000490 case DW_FORM_data8:
491 OS << format("0x%016" PRIx64, UValue);
492 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:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000503 if (UValue > 0) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000504 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000505 case DW_FORM_exprloc:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000506 case DW_FORM_block:
507 OS << format("<0x%" PRIx64 "> ", UValue);
508 break;
509 case DW_FORM_block1:
510 OS << format("<0x%2.2x> ", (uint8_t)UValue);
511 break;
512 case DW_FORM_block2:
513 OS << format("<0x%4.4x> ", (uint16_t)UValue);
514 break;
515 case DW_FORM_block4:
516 OS << format("<0x%8.8x> ", (uint32_t)UValue);
517 break;
518 default:
519 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000520 }
521
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000522 const uint8_t *DataPtr = Value.data;
523 if (DataPtr) {
524 // UValue contains size of block
525 const uint8_t *EndDataPtr = DataPtr + UValue;
526 while (DataPtr < EndDataPtr) {
527 OS << format("%2.2x ", *DataPtr);
528 ++DataPtr;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000529 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000530 } else
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000531 OS << "NULL";
532 }
533 break;
534
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000535 case DW_FORM_sdata:
536 OS << Value.sval;
537 break;
538 case DW_FORM_udata:
539 OS << Value.uval;
540 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000541 case DW_FORM_strp:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000542 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000543 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000544 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000545 case DW_FORM_GNU_str_index:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000546 OS << format(" indexed (%8.8x) string = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000547 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000548 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000549 case DW_FORM_GNU_strp_alt:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000550 OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000551 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000552 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000553 case DW_FORM_ref_addr:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000554 OS << format("0x%016" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000555 break;
556 case DW_FORM_ref1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000557 CURelativeOffset = true;
558 OS << format("cu + 0x%2.2x", (uint8_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000559 break;
560 case DW_FORM_ref2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000561 CURelativeOffset = true;
562 OS << format("cu + 0x%4.4x", (uint16_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000563 break;
564 case DW_FORM_ref4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000565 CURelativeOffset = true;
566 OS << format("cu + 0x%4.4x", (uint32_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000567 break;
568 case DW_FORM_ref8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000569 CURelativeOffset = true;
570 OS << format("cu + 0x%8.8" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000571 break;
572 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000573 CURelativeOffset = true;
574 OS << format("cu + 0x%" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000575 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000576 case DW_FORM_GNU_ref_alt:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000577 OS << format("<alt 0x%" PRIx64 ">", UValue);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000578 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000579
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000580 // All DW_FORM_indirect attributes should be resolved prior to calling
581 // this function
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000582 case DW_FORM_indirect:
583 OS << "DW_FORM_indirect";
584 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000585
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000586 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000587 case DW_FORM_sec_offset:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000588 OS << format("0x%08x", (uint32_t)UValue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000589 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000590
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000591 default:
592 OS << format("DW_FORM(0x%4.4x)", Form);
593 break;
594 }
595
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000596 if (CURelativeOffset) {
Adrian Prantl0c36a752015-01-06 16:50:25 +0000597 OS << " => {";
598 WithColor(OS, syntax::Address).get()
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000599 << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000600 OS << "}";
601 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000602}
603
Greg Claytoncddab272016-10-31 16:46:02 +0000604void DWARFFormValue::dumpString(raw_ostream &OS) const {
605 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000606 if (DbgStr.hasValue()) {
607 raw_ostream &COS = WithColor(OS, syntax::String);
608 COS << '"';
609 COS.write_escaped(DbgStr.getValue());
610 COS << '"';
611 }
612}
613
Greg Claytoncddab272016-10-31 16:46:02 +0000614Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000615 if (!isFormClass(FC_String))
616 return None;
617 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000618 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000619 // FIXME: Add support for DW_FORM_GNU_strp_alt
620 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000621 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000622 uint32_t Offset = Value.uval;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000623 if (Form == DW_FORM_GNU_str_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000624 uint32_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000625 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
626 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000627 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000628 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000629 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
630 return Str;
631 }
632 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000633}
634
Greg Claytoncddab272016-10-31 16:46:02 +0000635Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000636 if (!isFormClass(FC_Address))
637 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000638 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000639 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000640 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000641 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000642 return None;
643 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000644 }
645 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000646}
647
Greg Claytoncddab272016-10-31 16:46:02 +0000648Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000649 if (!isFormClass(FC_Reference))
650 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000651 switch (Form) {
652 case DW_FORM_ref1:
653 case DW_FORM_ref2:
654 case DW_FORM_ref4:
655 case DW_FORM_ref8:
656 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000657 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000658 return None;
659 return Value.uval + U->getOffset();
660 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000661 case DW_FORM_ref_sig8:
662 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000663 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000664 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000665 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000666 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000667}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000668
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000669Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
670 if (!isFormClass(FC_SectionOffset))
671 return None;
672 return Value.uval;
673}
674
675Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000676 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
677 Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000678 return None;
679 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000680}
Frederic Rissf3549a22014-09-04 06:14:35 +0000681
Frederic Riss77f850e2015-03-04 22:07:41 +0000682Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
683 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000684 (Form == DW_FORM_udata &&
685 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000686 return None;
687 switch (Form) {
688 case DW_FORM_data4:
689 return int32_t(Value.uval);
690 case DW_FORM_data2:
691 return int16_t(Value.uval);
692 case DW_FORM_data1:
693 return int8_t(Value.uval);
694 case DW_FORM_sdata:
695 case DW_FORM_data8:
696 default:
697 return Value.sval;
698 }
699}
700
Frederic Risseab17772014-09-04 06:35:09 +0000701Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000702 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
703 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000704 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000705}
706
Chris Bienemane0e451d2016-12-22 22:44:27 +0000707Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
708 if (!isFormClass(FC_String) && Form == DW_FORM_string)
709 return None;
710 return Value.uval;
711}
712
713Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
714 if (!isFormClass(FC_Reference))
715 return None;
716 return Value.uval;
717}