blob: d0fe05d1838b71d6dd1a52533e7dc4f5fdddc1c4 [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
Paul Robinson75c068c2017-06-26 18:43:01 +000062Optional<uint8_t>
63DWARFFormValue::getFixedByteSize(dwarf::Form Form,
Paul Robinson36e85a82017-06-26 19:52:32 +000064 const DWARFFormParams Params) {
Greg Clayton82f12b12016-11-11 16:21:37 +000065 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +000066 case DW_FORM_addr:
Jonas Devliegherecbf651f2018-01-05 10:03:02 +000067 if (Params)
68 return Params.AddrSize;
69 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +000070
Paul Robinsonae2e6f372017-05-03 21:53:21 +000071 case DW_FORM_block: // ULEB128 length L followed by L bytes.
72 case DW_FORM_block1: // 1 byte length L followed by L bytes.
73 case DW_FORM_block2: // 2 byte length L followed by L bytes.
74 case DW_FORM_block4: // 4 byte length L followed by L bytes.
75 case DW_FORM_string: // C-string with null terminator.
76 case DW_FORM_sdata: // SLEB128.
77 case DW_FORM_udata: // ULEB128.
78 case DW_FORM_ref_udata: // ULEB128.
79 case DW_FORM_indirect: // ULEB128.
80 case DW_FORM_exprloc: // ULEB128 length L followed by L bytes.
81 case DW_FORM_strx: // ULEB128.
82 case DW_FORM_addrx: // ULEB128.
83 case DW_FORM_loclistx: // ULEB128.
84 case DW_FORM_rnglistx: // ULEB128.
85 case DW_FORM_GNU_addr_index: // ULEB128.
86 case DW_FORM_GNU_str_index: // ULEB128.
87 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +000088
Paul Robinsonae2e6f372017-05-03 21:53:21 +000089 case DW_FORM_ref_addr:
Jonas Devliegherecbf651f2018-01-05 10:03:02 +000090 if (Params)
91 return Params.getRefAddrByteSize();
92 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +000093
Paul Robinsonae2e6f372017-05-03 21:53:21 +000094 case DW_FORM_flag:
95 case DW_FORM_data1:
96 case DW_FORM_ref1:
97 case DW_FORM_strx1:
98 case DW_FORM_addrx1:
99 return 1;
Greg Clayton82f12b12016-11-11 16:21:37 +0000100
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000101 case DW_FORM_data2:
102 case DW_FORM_ref2:
103 case DW_FORM_strx2:
104 case DW_FORM_addrx2:
105 return 2;
Greg Clayton82f12b12016-11-11 16:21:37 +0000106
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000107 case DW_FORM_strx3:
108 return 3;
109
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000110 case DW_FORM_data4:
111 case DW_FORM_ref4:
112 case DW_FORM_ref_sup4:
113 case DW_FORM_strx4:
114 case DW_FORM_addrx4:
115 return 4;
Greg Clayton82f12b12016-11-11 16:21:37 +0000116
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000117 case DW_FORM_strp:
118 case DW_FORM_GNU_ref_alt:
119 case DW_FORM_GNU_strp_alt:
120 case DW_FORM_line_strp:
121 case DW_FORM_sec_offset:
122 case DW_FORM_strp_sup:
Jonas Devliegherecbf651f2018-01-05 10:03:02 +0000123 if (Params)
124 return Params.getDwarfOffsetByteSize();
125 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +0000126
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000127 case DW_FORM_data8:
128 case DW_FORM_ref8:
129 case DW_FORM_ref_sig8:
130 case DW_FORM_ref_sup8:
131 return 8;
Greg Clayton82f12b12016-11-11 16:21:37 +0000132
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000133 case DW_FORM_flag_present:
134 return 0;
Greg Clayton82f12b12016-11-11 16:21:37 +0000135
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000136 case DW_FORM_data16:
137 return 16;
Greg Clayton82f12b12016-11-11 16:21:37 +0000138
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000139 case DW_FORM_implicit_const:
140 // The implicit value is stored in the abbreviation as a SLEB128, and
141 // there no data in debug info.
142 return 0;
Greg Clayton82f12b12016-11-11 16:21:37 +0000143
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000144 default:
145 llvm_unreachable("Handle this form in this switch statement");
Greg Clayton82f12b12016-11-11 16:21:37 +0000146 }
147 return None;
148}
149
Paul Robinson75c068c2017-06-26 18:43:01 +0000150bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
151 uint32_t *OffsetPtr,
Paul Robinson36e85a82017-06-26 19:52:32 +0000152 const DWARFFormParams Params) {
Greg Clayton82f12b12016-11-11 16:21:37 +0000153 bool Indirect = false;
154 do {
155 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000156 // Blocks of inlined data that have a length field and the data bytes
157 // inlined in the .debug_info.
158 case DW_FORM_exprloc:
159 case DW_FORM_block: {
160 uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
161 *OffsetPtr += size;
162 return true;
163 }
164 case DW_FORM_block1: {
165 uint8_t size = DebugInfoData.getU8(OffsetPtr);
166 *OffsetPtr += size;
167 return true;
168 }
169 case DW_FORM_block2: {
170 uint16_t size = DebugInfoData.getU16(OffsetPtr);
171 *OffsetPtr += size;
172 return true;
173 }
174 case DW_FORM_block4: {
175 uint32_t size = DebugInfoData.getU32(OffsetPtr);
176 *OffsetPtr += size;
177 return true;
178 }
179
180 // Inlined NULL terminated C-strings.
181 case DW_FORM_string:
182 DebugInfoData.getCStr(OffsetPtr);
183 return true;
184
185 case DW_FORM_addr:
186 case DW_FORM_ref_addr:
187 case DW_FORM_flag_present:
188 case DW_FORM_data1:
189 case DW_FORM_data2:
190 case DW_FORM_data4:
191 case DW_FORM_data8:
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000192 case DW_FORM_data16:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000193 case DW_FORM_flag:
194 case DW_FORM_ref1:
195 case DW_FORM_ref2:
196 case DW_FORM_ref4:
197 case DW_FORM_ref8:
198 case DW_FORM_ref_sig8:
199 case DW_FORM_ref_sup4:
200 case DW_FORM_ref_sup8:
201 case DW_FORM_strx1:
202 case DW_FORM_strx2:
203 case DW_FORM_strx4:
204 case DW_FORM_addrx1:
205 case DW_FORM_addrx2:
206 case DW_FORM_addrx4:
207 case DW_FORM_sec_offset:
208 case DW_FORM_strp:
209 case DW_FORM_strp_sup:
210 case DW_FORM_line_strp:
211 case DW_FORM_GNU_ref_alt:
212 case DW_FORM_GNU_strp_alt:
Paul Robinson75c068c2017-06-26 18:43:01 +0000213 if (Optional<uint8_t> FixedSize =
214 DWARFFormValue::getFixedByteSize(Form, Params)) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000215 *OffsetPtr += *FixedSize;
Greg Clayton82f12b12016-11-11 16:21:37 +0000216 return true;
217 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000218 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000219
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000220 // signed or unsigned LEB 128 values.
221 case DW_FORM_sdata:
222 DebugInfoData.getSLEB128(OffsetPtr);
223 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000224
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000225 case DW_FORM_udata:
226 case DW_FORM_ref_udata:
227 case DW_FORM_strx:
228 case DW_FORM_addrx:
229 case DW_FORM_loclistx:
230 case DW_FORM_rnglistx:
231 case DW_FORM_GNU_addr_index:
232 case DW_FORM_GNU_str_index:
233 DebugInfoData.getULEB128(OffsetPtr);
234 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000235
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000236 case DW_FORM_indirect:
237 Indirect = true;
238 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
239 break;
Greg Clayton82f12b12016-11-11 16:21:37 +0000240
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000241 default:
242 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000243 }
244 } while (Indirect);
245 return true;
246}
247
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000248bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
249 // First, check DWARF4 form classes.
Craig Topper0013be12015-09-21 05:32:41 +0000250 if (Form < makeArrayRef(DWARF4FormClasses).size() &&
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000251 DWARF4FormClasses[Form] == FC)
252 return true;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000253 // Check more forms from DWARF4 and DWARF5 proposals.
254 switch (Form) {
255 case DW_FORM_ref_sig8:
256 case DW_FORM_GNU_ref_alt:
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000257 return (FC == FC_Reference);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000258 case DW_FORM_GNU_addr_index:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000259 return (FC == FC_Address);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000260 case DW_FORM_GNU_str_index:
261 case DW_FORM_GNU_strp_alt:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000262 case DW_FORM_strx:
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000263 case DW_FORM_strx1:
264 case DW_FORM_strx2:
265 case DW_FORM_strx3:
266 case DW_FORM_strx4:
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000267 case DW_FORM_line_strp:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000268 return (FC == FC_String);
Victor Leschukcbddae72017-01-10 21:18:26 +0000269 case DW_FORM_implicit_const:
270 return (FC == FC_Constant);
Greg Clayton6c273762016-10-27 16:32:04 +0000271 default:
272 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000273 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000274 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
275 // Don't check for DWARF version here, as some producers may still do this
Greg Clayton48432cf2017-05-01 22:07:02 +0000276 // by mistake. Also accept DW_FORM_strp since this is .debug_str section
277 // offset.
278 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
279 Form == DW_FORM_strp) &&
Benjamin Kramer68a29562015-05-25 13:28:03 +0000280 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000281}
282
Paul Robinson17536b92017-06-29 16:52:08 +0000283bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
Paul Robinsone5400f82017-11-07 19:57:12 +0000284 uint32_t *OffsetPtr, DWARFFormParams FP,
285 const DWARFUnit *CU) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000286 U = CU;
287 bool Indirect = false;
288 bool IsBlock = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000289 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000290 // Read the value for the form into value and follow and DW_FORM_indirect
291 // instances we run into
292 do {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000293 Indirect = false;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000294 switch (Form) {
295 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000296 case DW_FORM_ref_addr: {
Paul Robinsone5400f82017-11-07 19:57:12 +0000297 uint16_t Size =
298 (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
Paul Robinson17536b92017-06-29 16:52:08 +0000299 Value.uval = Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000300 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000301 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000302 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000303 case DW_FORM_block:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000304 Value.uval = Data.getULEB128(OffsetPtr);
305 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000306 break;
307 case DW_FORM_block1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000308 Value.uval = Data.getU8(OffsetPtr);
309 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000310 break;
311 case DW_FORM_block2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000312 Value.uval = Data.getU16(OffsetPtr);
313 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000314 break;
315 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000316 Value.uval = Data.getU32(OffsetPtr);
317 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000318 break;
319 case DW_FORM_data1:
320 case DW_FORM_ref1:
321 case DW_FORM_flag:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000322 case DW_FORM_strx1:
323 case DW_FORM_addrx1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000324 Value.uval = Data.getU8(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000325 break;
326 case DW_FORM_data2:
327 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000328 case DW_FORM_strx2:
329 case DW_FORM_addrx2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000330 Value.uval = Data.getU16(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000331 break;
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000332 case DW_FORM_strx3:
333 Value.uval = Data.getU24(OffsetPtr);
334 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000335 case DW_FORM_data4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000336 case DW_FORM_ref4:
337 case DW_FORM_ref_sup4:
338 case DW_FORM_strx4:
Paul Robinson17536b92017-06-29 16:52:08 +0000339 case DW_FORM_addrx4:
340 Value.uval = Data.getRelocatedValue(4, OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000341 break;
342 case DW_FORM_data8:
343 case DW_FORM_ref8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000344 case DW_FORM_ref_sup8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000345 Value.uval = Data.getU64(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000346 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000347 case DW_FORM_data16:
348 // Treat this like a 16-byte block.
349 Value.uval = 16;
350 IsBlock = true;
351 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000352 case DW_FORM_sdata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000353 Value.sval = Data.getSLEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000354 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000355 case DW_FORM_udata:
356 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000357 Value.uval = Data.getULEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000358 break;
359 case DW_FORM_string:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000360 Value.cstr = Data.getCStr(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000361 break;
362 case DW_FORM_indirect:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000363 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
364 Indirect = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000365 break;
Greg Clayton04c19282016-11-11 17:38:14 +0000366 case DW_FORM_strp:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000367 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000368 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000369 case DW_FORM_GNU_strp_alt:
370 case DW_FORM_line_strp:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000371 case DW_FORM_strp_sup: {
Paul Robinson17536b92017-06-29 16:52:08 +0000372 Value.uval =
Paul Robinsone5400f82017-11-07 19:57:12 +0000373 Data.getRelocatedValue(FP.getDwarfOffsetByteSize(), OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000374 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000375 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000376 case DW_FORM_flag_present:
377 Value.uval = 1;
378 break;
379 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000380 Value.uval = Data.getU64(OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000381 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000382 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000383 case DW_FORM_GNU_str_index:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000384 case DW_FORM_strx:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000385 Value.uval = Data.getULEB128(OffsetPtr);
Eric Christopher59c53c22012-11-16 23:44:11 +0000386 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000387 default:
Greg Clayton0e62ee72017-01-13 00:13:42 +0000388 // DWARFFormValue::skipValue() will have caught this and caused all
389 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
390 llvm_unreachable("unsupported form");
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000391 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000392 } while (Indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000393
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000394 if (IsBlock) {
395 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000396 Value.data = nullptr;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000397 if (!Str.empty()) {
398 Value.data = reinterpret_cast<const uint8_t *>(Str.data());
399 *OffsetPtr += Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000400 }
401 }
402
403 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000404}
405
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000406void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000407 uint64_t UValue = Value.uval;
408 bool CURelativeOffset = false;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000409 raw_ostream &AddrOS =
410 DumpOpts.ShowAddresses ? WithColor(OS, syntax::Address).get() : nulls();
Benjamin Kramereaa74332011-09-13 21:47:32 +0000411 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000412 case DW_FORM_addr:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000413 AddrOS << format("0x%016" PRIx64, UValue);
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000414 break;
Eric Christopher962c9082013-01-15 23:56:56 +0000415 case DW_FORM_GNU_addr_index: {
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000416 AddrOS << format(" indexed (%8.8x) address = ", (uint32_t)UValue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000417 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000418 if (U == nullptr)
419 OS << "<invalid dwarf unit>";
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000420 else if (U->getAddrOffsetSectionItem(UValue, Address))
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000421 AddrOS << format("0x%016" PRIx64, Address);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000422 else
Eric Christopher962c9082013-01-15 23:56:56 +0000423 OS << "<no .debug_addr section>";
424 break;
425 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000426 case DW_FORM_flag_present:
427 OS << "true";
428 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000429 case DW_FORM_flag:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000430 case DW_FORM_data1:
431 OS << format("0x%02x", (uint8_t)UValue);
432 break;
433 case DW_FORM_data2:
434 OS << format("0x%04x", (uint16_t)UValue);
435 break;
436 case DW_FORM_data4:
437 OS << format("0x%08x", (uint32_t)UValue);
438 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000439 case DW_FORM_ref_sig8:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000440 AddrOS << format("0x%016" PRIx64, UValue);
441 break;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000442 case DW_FORM_data8:
443 OS << format("0x%016" PRIx64, UValue);
444 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000445 case DW_FORM_data16:
446 OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
447 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000448 case DW_FORM_string:
449 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000450 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000451 OS << '"';
452 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000453 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000454 case DW_FORM_block:
455 case DW_FORM_block1:
456 case DW_FORM_block2:
457 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000458 if (UValue > 0) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000459 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000460 case DW_FORM_exprloc:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000461 case DW_FORM_block:
462 OS << format("<0x%" PRIx64 "> ", UValue);
463 break;
464 case DW_FORM_block1:
465 OS << format("<0x%2.2x> ", (uint8_t)UValue);
466 break;
467 case DW_FORM_block2:
468 OS << format("<0x%4.4x> ", (uint16_t)UValue);
469 break;
470 case DW_FORM_block4:
471 OS << format("<0x%8.8x> ", (uint32_t)UValue);
472 break;
473 default:
474 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000475 }
476
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000477 const uint8_t *DataPtr = Value.data;
478 if (DataPtr) {
479 // UValue contains size of block
480 const uint8_t *EndDataPtr = DataPtr + UValue;
481 while (DataPtr < EndDataPtr) {
482 OS << format("%2.2x ", *DataPtr);
483 ++DataPtr;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000484 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000485 } else
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000486 OS << "NULL";
487 }
488 break;
489
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000490 case DW_FORM_sdata:
491 OS << Value.sval;
492 break;
493 case DW_FORM_udata:
494 OS << Value.uval;
495 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000496 case DW_FORM_strp:
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000497 if (DumpOpts.Verbose)
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000498 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000499 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000500 break;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000501 case DW_FORM_strx:
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000502 case DW_FORM_strx1:
503 case DW_FORM_strx2:
504 case DW_FORM_strx3:
505 case DW_FORM_strx4:
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000506 case DW_FORM_GNU_str_index:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000507 if (DumpOpts.Verbose)
508 OS << format(" indexed (%8.8x) string = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000509 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000510 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000511 case DW_FORM_GNU_strp_alt:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000512 if (DumpOpts.Verbose)
513 OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000514 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000515 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000516 case DW_FORM_ref_addr:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000517 AddrOS << format("0x%016" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000518 break;
519 case DW_FORM_ref1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000520 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000521 AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000522 break;
523 case DW_FORM_ref2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000524 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000525 AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000526 break;
527 case DW_FORM_ref4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000528 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000529 AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000530 break;
531 case DW_FORM_ref8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000532 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000533 AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000534 break;
535 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000536 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000537 AddrOS << format("cu + 0x%" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000538 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000539 case DW_FORM_GNU_ref_alt:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000540 AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000541 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000542
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000543 // All DW_FORM_indirect attributes should be resolved prior to calling
544 // this function
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000545 case DW_FORM_indirect:
546 OS << "DW_FORM_indirect";
547 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000548
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000549 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000550 case DW_FORM_sec_offset:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000551 AddrOS << format("0x%08x", (uint32_t)UValue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000552 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000553
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000554 default:
555 OS << format("DW_FORM(0x%4.4x)", Form);
556 break;
557 }
558
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000559 if (CURelativeOffset && DumpOpts.Verbose) {
Adrian Prantl0c36a752015-01-06 16:50:25 +0000560 OS << " => {";
561 WithColor(OS, syntax::Address).get()
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000562 << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000563 OS << "}";
564 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000565}
566
Greg Claytoncddab272016-10-31 16:46:02 +0000567void DWARFFormValue::dumpString(raw_ostream &OS) const {
568 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000569 if (DbgStr.hasValue()) {
570 raw_ostream &COS = WithColor(OS, syntax::String);
571 COS << '"';
572 COS.write_escaped(DbgStr.getValue());
573 COS << '"';
574 }
575}
576
Greg Claytoncddab272016-10-31 16:46:02 +0000577Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000578 if (!isFormClass(FC_String))
579 return None;
580 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000581 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000582 // FIXME: Add support for DW_FORM_GNU_strp_alt
583 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000584 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000585 uint32_t Offset = Value.uval;
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000586 if (Form == DW_FORM_line_strp) {
587 if (const char *Str = U->getLineStringExtractor().getCStr(&Offset))
588 return Str;
589 return None;
590 }
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000591 if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
592 Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
593 Form == DW_FORM_strx4) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000594 uint64_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000595 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
596 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000597 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000598 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000599 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
600 return Str;
601 }
602 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000603}
604
Greg Claytoncddab272016-10-31 16:46:02 +0000605Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000606 if (!isFormClass(FC_Address))
607 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000608 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000609 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000610 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000611 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000612 return None;
613 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000614 }
615 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000616}
617
Greg Claytoncddab272016-10-31 16:46:02 +0000618Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000619 if (!isFormClass(FC_Reference))
620 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000621 switch (Form) {
622 case DW_FORM_ref1:
623 case DW_FORM_ref2:
624 case DW_FORM_ref4:
625 case DW_FORM_ref8:
626 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000627 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000628 return None;
629 return Value.uval + U->getOffset();
630 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000631 case DW_FORM_ref_sig8:
632 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000633 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000634 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000635 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000636 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000637}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000638
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000639Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
640 if (!isFormClass(FC_SectionOffset))
641 return None;
642 return Value.uval;
643}
644
645Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000646 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
647 Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000648 return None;
649 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000650}
Frederic Rissf3549a22014-09-04 06:14:35 +0000651
Frederic Riss77f850e2015-03-04 22:07:41 +0000652Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
653 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000654 (Form == DW_FORM_udata &&
655 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000656 return None;
657 switch (Form) {
658 case DW_FORM_data4:
659 return int32_t(Value.uval);
660 case DW_FORM_data2:
661 return int16_t(Value.uval);
662 case DW_FORM_data1:
663 return int8_t(Value.uval);
664 case DW_FORM_sdata:
665 case DW_FORM_data8:
666 default:
667 return Value.sval;
668 }
669}
670
Frederic Risseab17772014-09-04 06:35:09 +0000671Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000672 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
673 Form != DW_FORM_data16)
Frederic Rissf3549a22014-09-04 06:14:35 +0000674 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000675 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000676}
677
Chris Bienemane0e451d2016-12-22 22:44:27 +0000678Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
679 if (!isFormClass(FC_String) && Form == DW_FORM_string)
680 return None;
681 return Value.uval;
682}
683
684Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
685 if (!isFormClass(FC_Reference))
686 return None;
687 return Value.uval;
688}