blob: 44886de2e3d5804246bbf4f61d245d32eeae4db6 [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:
Paul Robinson75c068c2017-06-26 18:43:01 +000067 assert(Params.Version && Params.AddrSize && "Invalid Params for form");
68 return Params.AddrSize;
Greg Clayton82f12b12016-11-11 16:21:37 +000069
Paul Robinsonae2e6f372017-05-03 21:53:21 +000070 case DW_FORM_block: // ULEB128 length L followed by L bytes.
71 case DW_FORM_block1: // 1 byte length L followed by L bytes.
72 case DW_FORM_block2: // 2 byte length L followed by L bytes.
73 case DW_FORM_block4: // 4 byte length L followed by L bytes.
74 case DW_FORM_string: // C-string with null terminator.
75 case DW_FORM_sdata: // SLEB128.
76 case DW_FORM_udata: // ULEB128.
77 case DW_FORM_ref_udata: // ULEB128.
78 case DW_FORM_indirect: // ULEB128.
79 case DW_FORM_exprloc: // ULEB128 length L followed by L bytes.
80 case DW_FORM_strx: // ULEB128.
81 case DW_FORM_addrx: // ULEB128.
82 case DW_FORM_loclistx: // ULEB128.
83 case DW_FORM_rnglistx: // ULEB128.
84 case DW_FORM_GNU_addr_index: // ULEB128.
85 case DW_FORM_GNU_str_index: // ULEB128.
86 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +000087
Paul Robinsonae2e6f372017-05-03 21:53:21 +000088 case DW_FORM_ref_addr:
Paul Robinson75c068c2017-06-26 18:43:01 +000089 assert(Params.Version && Params.AddrSize && "Invalid Params for form");
90 return Params.getRefAddrByteSize();
Greg Clayton82f12b12016-11-11 16:21:37 +000091
Paul Robinsonae2e6f372017-05-03 21:53:21 +000092 case DW_FORM_flag:
93 case DW_FORM_data1:
94 case DW_FORM_ref1:
95 case DW_FORM_strx1:
96 case DW_FORM_addrx1:
97 return 1;
Greg Clayton82f12b12016-11-11 16:21:37 +000098
Paul Robinsonae2e6f372017-05-03 21:53:21 +000099 case DW_FORM_data2:
100 case DW_FORM_ref2:
101 case DW_FORM_strx2:
102 case DW_FORM_addrx2:
103 return 2;
Greg Clayton82f12b12016-11-11 16:21:37 +0000104
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000105 case DW_FORM_strx3:
106 return 3;
107
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000108 case DW_FORM_data4:
109 case DW_FORM_ref4:
110 case DW_FORM_ref_sup4:
111 case DW_FORM_strx4:
112 case DW_FORM_addrx4:
113 return 4;
Greg Clayton82f12b12016-11-11 16:21:37 +0000114
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000115 case DW_FORM_strp:
116 case DW_FORM_GNU_ref_alt:
117 case DW_FORM_GNU_strp_alt:
118 case DW_FORM_line_strp:
119 case DW_FORM_sec_offset:
120 case DW_FORM_strp_sup:
Paul Robinson75c068c2017-06-26 18:43:01 +0000121 assert(Params.Version && Params.AddrSize && "Invalid Params for form");
122 return Params.getDwarfOffsetByteSize();
Greg Clayton82f12b12016-11-11 16:21:37 +0000123
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000124 case DW_FORM_data8:
125 case DW_FORM_ref8:
126 case DW_FORM_ref_sig8:
127 case DW_FORM_ref_sup8:
128 return 8;
Greg Clayton82f12b12016-11-11 16:21:37 +0000129
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000130 case DW_FORM_flag_present:
131 return 0;
Greg Clayton82f12b12016-11-11 16:21:37 +0000132
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000133 case DW_FORM_data16:
134 return 16;
Greg Clayton82f12b12016-11-11 16:21:37 +0000135
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000136 case DW_FORM_implicit_const:
137 // The implicit value is stored in the abbreviation as a SLEB128, and
138 // there no data in debug info.
139 return 0;
Greg Clayton82f12b12016-11-11 16:21:37 +0000140
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000141 default:
142 llvm_unreachable("Handle this form in this switch statement");
Greg Clayton82f12b12016-11-11 16:21:37 +0000143 }
144 return None;
145}
146
Paul Robinson75c068c2017-06-26 18:43:01 +0000147bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
148 uint32_t *OffsetPtr,
Paul Robinson36e85a82017-06-26 19:52:32 +0000149 const DWARFFormParams Params) {
Greg Clayton82f12b12016-11-11 16:21:37 +0000150 bool Indirect = false;
151 do {
152 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000153 // Blocks of inlined data that have a length field and the data bytes
154 // inlined in the .debug_info.
155 case DW_FORM_exprloc:
156 case DW_FORM_block: {
157 uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
158 *OffsetPtr += size;
159 return true;
160 }
161 case DW_FORM_block1: {
162 uint8_t size = DebugInfoData.getU8(OffsetPtr);
163 *OffsetPtr += size;
164 return true;
165 }
166 case DW_FORM_block2: {
167 uint16_t size = DebugInfoData.getU16(OffsetPtr);
168 *OffsetPtr += size;
169 return true;
170 }
171 case DW_FORM_block4: {
172 uint32_t size = DebugInfoData.getU32(OffsetPtr);
173 *OffsetPtr += size;
174 return true;
175 }
176
177 // Inlined NULL terminated C-strings.
178 case DW_FORM_string:
179 DebugInfoData.getCStr(OffsetPtr);
180 return true;
181
182 case DW_FORM_addr:
183 case DW_FORM_ref_addr:
184 case DW_FORM_flag_present:
185 case DW_FORM_data1:
186 case DW_FORM_data2:
187 case DW_FORM_data4:
188 case DW_FORM_data8:
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000189 case DW_FORM_data16:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000190 case DW_FORM_flag:
191 case DW_FORM_ref1:
192 case DW_FORM_ref2:
193 case DW_FORM_ref4:
194 case DW_FORM_ref8:
195 case DW_FORM_ref_sig8:
196 case DW_FORM_ref_sup4:
197 case DW_FORM_ref_sup8:
198 case DW_FORM_strx1:
199 case DW_FORM_strx2:
200 case DW_FORM_strx4:
201 case DW_FORM_addrx1:
202 case DW_FORM_addrx2:
203 case DW_FORM_addrx4:
204 case DW_FORM_sec_offset:
205 case DW_FORM_strp:
206 case DW_FORM_strp_sup:
207 case DW_FORM_line_strp:
208 case DW_FORM_GNU_ref_alt:
209 case DW_FORM_GNU_strp_alt:
Paul Robinson75c068c2017-06-26 18:43:01 +0000210 if (Optional<uint8_t> FixedSize =
211 DWARFFormValue::getFixedByteSize(Form, Params)) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000212 *OffsetPtr += *FixedSize;
Greg Clayton82f12b12016-11-11 16:21:37 +0000213 return true;
214 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000215 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000216
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000217 // signed or unsigned LEB 128 values.
218 case DW_FORM_sdata:
219 DebugInfoData.getSLEB128(OffsetPtr);
220 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000221
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000222 case DW_FORM_udata:
223 case DW_FORM_ref_udata:
224 case DW_FORM_strx:
225 case DW_FORM_addrx:
226 case DW_FORM_loclistx:
227 case DW_FORM_rnglistx:
228 case DW_FORM_GNU_addr_index:
229 case DW_FORM_GNU_str_index:
230 DebugInfoData.getULEB128(OffsetPtr);
231 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000232
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000233 case DW_FORM_indirect:
234 Indirect = true;
235 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
236 break;
Greg Clayton82f12b12016-11-11 16:21:37 +0000237
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000238 default:
239 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000240 }
241 } while (Indirect);
242 return true;
243}
244
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000245bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
246 // First, check DWARF4 form classes.
Craig Topper0013be12015-09-21 05:32:41 +0000247 if (Form < makeArrayRef(DWARF4FormClasses).size() &&
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000248 DWARF4FormClasses[Form] == FC)
249 return true;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000250 // Check more forms from DWARF4 and DWARF5 proposals.
251 switch (Form) {
252 case DW_FORM_ref_sig8:
253 case DW_FORM_GNU_ref_alt:
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000254 return (FC == FC_Reference);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000255 case DW_FORM_GNU_addr_index:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000256 return (FC == FC_Address);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000257 case DW_FORM_GNU_str_index:
258 case DW_FORM_GNU_strp_alt:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000259 case DW_FORM_strx:
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000260 case DW_FORM_strx1:
261 case DW_FORM_strx2:
262 case DW_FORM_strx3:
263 case DW_FORM_strx4:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000264 return (FC == FC_String);
Victor Leschukcbddae72017-01-10 21:18:26 +0000265 case DW_FORM_implicit_const:
266 return (FC == FC_Constant);
Greg Clayton6c273762016-10-27 16:32:04 +0000267 default:
268 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000269 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000270 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
271 // Don't check for DWARF version here, as some producers may still do this
Greg Clayton48432cf2017-05-01 22:07:02 +0000272 // by mistake. Also accept DW_FORM_strp since this is .debug_str section
273 // offset.
274 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
275 Form == DW_FORM_strp) &&
Benjamin Kramer68a29562015-05-25 13:28:03 +0000276 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000277}
278
Paul Robinson17536b92017-06-29 16:52:08 +0000279bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
Paul Robinsone5400f82017-11-07 19:57:12 +0000280 uint32_t *OffsetPtr, DWARFFormParams FP,
281 const DWARFUnit *CU) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000282 U = CU;
283 bool Indirect = false;
284 bool IsBlock = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000285 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000286 // Read the value for the form into value and follow and DW_FORM_indirect
287 // instances we run into
288 do {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000289 Indirect = false;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000290 switch (Form) {
291 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000292 case DW_FORM_ref_addr: {
Paul Robinsone5400f82017-11-07 19:57:12 +0000293 uint16_t Size =
294 (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
Paul Robinson17536b92017-06-29 16:52:08 +0000295 Value.uval = Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000296 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000297 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000298 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000299 case DW_FORM_block:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000300 Value.uval = Data.getULEB128(OffsetPtr);
301 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000302 break;
303 case DW_FORM_block1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000304 Value.uval = Data.getU8(OffsetPtr);
305 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000306 break;
307 case DW_FORM_block2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000308 Value.uval = Data.getU16(OffsetPtr);
309 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000310 break;
311 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000312 Value.uval = Data.getU32(OffsetPtr);
313 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000314 break;
315 case DW_FORM_data1:
316 case DW_FORM_ref1:
317 case DW_FORM_flag:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000318 case DW_FORM_strx1:
319 case DW_FORM_addrx1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000320 Value.uval = Data.getU8(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000321 break;
322 case DW_FORM_data2:
323 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000324 case DW_FORM_strx2:
325 case DW_FORM_addrx2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000326 Value.uval = Data.getU16(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000327 break;
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000328 case DW_FORM_strx3:
329 Value.uval = Data.getU24(OffsetPtr);
330 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000331 case DW_FORM_data4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000332 case DW_FORM_ref4:
333 case DW_FORM_ref_sup4:
334 case DW_FORM_strx4:
Paul Robinson17536b92017-06-29 16:52:08 +0000335 case DW_FORM_addrx4:
336 Value.uval = Data.getRelocatedValue(4, OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000337 break;
338 case DW_FORM_data8:
339 case DW_FORM_ref8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000340 case DW_FORM_ref_sup8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000341 Value.uval = Data.getU64(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000342 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000343 case DW_FORM_data16:
344 // Treat this like a 16-byte block.
345 Value.uval = 16;
346 IsBlock = true;
347 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000348 case DW_FORM_sdata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000349 Value.sval = Data.getSLEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000350 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000351 case DW_FORM_udata:
352 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000353 Value.uval = Data.getULEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000354 break;
355 case DW_FORM_string:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000356 Value.cstr = Data.getCStr(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000357 break;
358 case DW_FORM_indirect:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000359 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
360 Indirect = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000361 break;
Greg Clayton04c19282016-11-11 17:38:14 +0000362 case DW_FORM_strp:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000363 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000364 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000365 case DW_FORM_GNU_strp_alt:
366 case DW_FORM_line_strp:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000367 case DW_FORM_strp_sup: {
Paul Robinson17536b92017-06-29 16:52:08 +0000368 Value.uval =
Paul Robinsone5400f82017-11-07 19:57:12 +0000369 Data.getRelocatedValue(FP.getDwarfOffsetByteSize(), OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000370 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000371 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000372 case DW_FORM_flag_present:
373 Value.uval = 1;
374 break;
375 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000376 Value.uval = Data.getU64(OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000377 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000378 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000379 case DW_FORM_GNU_str_index:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000380 case DW_FORM_strx:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000381 Value.uval = Data.getULEB128(OffsetPtr);
Eric Christopher59c53c22012-11-16 23:44:11 +0000382 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000383 default:
Greg Clayton0e62ee72017-01-13 00:13:42 +0000384 // DWARFFormValue::skipValue() will have caught this and caused all
385 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
386 llvm_unreachable("unsupported form");
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000387 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000388 } while (Indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000389
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000390 if (IsBlock) {
391 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000392 Value.data = nullptr;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000393 if (!Str.empty()) {
394 Value.data = reinterpret_cast<const uint8_t *>(Str.data());
395 *OffsetPtr += Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000396 }
397 }
398
399 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000400}
401
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000402void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000403 uint64_t UValue = Value.uval;
404 bool CURelativeOffset = false;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000405 raw_ostream &AddrOS =
406 DumpOpts.ShowAddresses ? WithColor(OS, syntax::Address).get() : nulls();
Benjamin Kramereaa74332011-09-13 21:47:32 +0000407 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000408 case DW_FORM_addr:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000409 AddrOS << format("0x%016" PRIx64, UValue);
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000410 break;
Eric Christopher962c9082013-01-15 23:56:56 +0000411 case DW_FORM_GNU_addr_index: {
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000412 AddrOS << format(" indexed (%8.8x) address = ", (uint32_t)UValue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000413 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000414 if (U == nullptr)
415 OS << "<invalid dwarf unit>";
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000416 else if (U->getAddrOffsetSectionItem(UValue, Address))
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000417 AddrOS << format("0x%016" PRIx64, Address);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000418 else
Eric Christopher962c9082013-01-15 23:56:56 +0000419 OS << "<no .debug_addr section>";
420 break;
421 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000422 case DW_FORM_flag_present:
423 OS << "true";
424 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000425 case DW_FORM_flag:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000426 case DW_FORM_data1:
427 OS << format("0x%02x", (uint8_t)UValue);
428 break;
429 case DW_FORM_data2:
430 OS << format("0x%04x", (uint16_t)UValue);
431 break;
432 case DW_FORM_data4:
433 OS << format("0x%08x", (uint32_t)UValue);
434 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000435 case DW_FORM_ref_sig8:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000436 AddrOS << format("0x%016" PRIx64, UValue);
437 break;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000438 case DW_FORM_data8:
439 OS << format("0x%016" PRIx64, UValue);
440 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000441 case DW_FORM_data16:
442 OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
443 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000444 case DW_FORM_string:
445 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000446 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000447 OS << '"';
448 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000449 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000450 case DW_FORM_block:
451 case DW_FORM_block1:
452 case DW_FORM_block2:
453 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000454 if (UValue > 0) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000455 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000456 case DW_FORM_exprloc:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000457 case DW_FORM_block:
458 OS << format("<0x%" PRIx64 "> ", UValue);
459 break;
460 case DW_FORM_block1:
461 OS << format("<0x%2.2x> ", (uint8_t)UValue);
462 break;
463 case DW_FORM_block2:
464 OS << format("<0x%4.4x> ", (uint16_t)UValue);
465 break;
466 case DW_FORM_block4:
467 OS << format("<0x%8.8x> ", (uint32_t)UValue);
468 break;
469 default:
470 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000471 }
472
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000473 const uint8_t *DataPtr = Value.data;
474 if (DataPtr) {
475 // UValue contains size of block
476 const uint8_t *EndDataPtr = DataPtr + UValue;
477 while (DataPtr < EndDataPtr) {
478 OS << format("%2.2x ", *DataPtr);
479 ++DataPtr;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000480 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000481 } else
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000482 OS << "NULL";
483 }
484 break;
485
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000486 case DW_FORM_sdata:
487 OS << Value.sval;
488 break;
489 case DW_FORM_udata:
490 OS << Value.uval;
491 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000492 case DW_FORM_strp:
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000493 if (DumpOpts.Verbose)
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000494 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000495 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000496 break;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000497 case DW_FORM_strx:
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000498 case DW_FORM_strx1:
499 case DW_FORM_strx2:
500 case DW_FORM_strx3:
501 case DW_FORM_strx4:
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000502 case DW_FORM_GNU_str_index:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000503 if (DumpOpts.Verbose)
504 OS << format(" indexed (%8.8x) string = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000505 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000506 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000507 case DW_FORM_GNU_strp_alt:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000508 if (DumpOpts.Verbose)
509 OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000510 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000511 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000512 case DW_FORM_ref_addr:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000513 AddrOS << format("0x%016" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000514 break;
515 case DW_FORM_ref1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000516 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000517 AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000518 break;
519 case DW_FORM_ref2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000520 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000521 AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000522 break;
523 case DW_FORM_ref4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000524 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000525 AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000526 break;
527 case DW_FORM_ref8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000528 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000529 AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000530 break;
531 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000532 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000533 AddrOS << format("cu + 0x%" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000534 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000535 case DW_FORM_GNU_ref_alt:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000536 AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000537 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000538
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000539 // All DW_FORM_indirect attributes should be resolved prior to calling
540 // this function
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000541 case DW_FORM_indirect:
542 OS << "DW_FORM_indirect";
543 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000544
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000545 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000546 case DW_FORM_sec_offset:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000547 AddrOS << format("0x%08x", (uint32_t)UValue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000548 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000549
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000550 default:
551 OS << format("DW_FORM(0x%4.4x)", Form);
552 break;
553 }
554
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000555 if (CURelativeOffset && DumpOpts.Verbose) {
Adrian Prantl0c36a752015-01-06 16:50:25 +0000556 OS << " => {";
557 WithColor(OS, syntax::Address).get()
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000558 << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000559 OS << "}";
560 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000561}
562
Greg Claytoncddab272016-10-31 16:46:02 +0000563void DWARFFormValue::dumpString(raw_ostream &OS) const {
564 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000565 if (DbgStr.hasValue()) {
566 raw_ostream &COS = WithColor(OS, syntax::String);
567 COS << '"';
568 COS.write_escaped(DbgStr.getValue());
569 COS << '"';
570 }
571}
572
Greg Claytoncddab272016-10-31 16:46:02 +0000573Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000574 if (!isFormClass(FC_String))
575 return None;
576 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000577 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000578 // FIXME: Add support for DW_FORM_GNU_strp_alt
579 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000580 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000581 uint32_t Offset = Value.uval;
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000582 if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
583 Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
584 Form == DW_FORM_strx4) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000585 uint64_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000586 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
587 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000588 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000589 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000590 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
591 return Str;
592 }
593 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000594}
595
Greg Claytoncddab272016-10-31 16:46:02 +0000596Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000597 if (!isFormClass(FC_Address))
598 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000599 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000600 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000601 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000602 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000603 return None;
604 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000605 }
606 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000607}
608
Greg Claytoncddab272016-10-31 16:46:02 +0000609Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000610 if (!isFormClass(FC_Reference))
611 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000612 switch (Form) {
613 case DW_FORM_ref1:
614 case DW_FORM_ref2:
615 case DW_FORM_ref4:
616 case DW_FORM_ref8:
617 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000618 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000619 return None;
620 return Value.uval + U->getOffset();
621 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000622 case DW_FORM_ref_sig8:
623 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000624 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000625 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000626 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000627 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000628}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000629
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000630Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
631 if (!isFormClass(FC_SectionOffset))
632 return None;
633 return Value.uval;
634}
635
636Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000637 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
638 Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000639 return None;
640 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000641}
Frederic Rissf3549a22014-09-04 06:14:35 +0000642
Frederic Riss77f850e2015-03-04 22:07:41 +0000643Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
644 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000645 (Form == DW_FORM_udata &&
646 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000647 return None;
648 switch (Form) {
649 case DW_FORM_data4:
650 return int32_t(Value.uval);
651 case DW_FORM_data2:
652 return int16_t(Value.uval);
653 case DW_FORM_data1:
654 return int8_t(Value.uval);
655 case DW_FORM_sdata:
656 case DW_FORM_data8:
657 default:
658 return Value.sval;
659 }
660}
661
Frederic Risseab17772014-09-04 06:35:09 +0000662Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000663 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
664 Form != DW_FORM_data16)
Frederic Rissf3549a22014-09-04 06:14:35 +0000665 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000666 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000667}
668
Chris Bienemane0e451d2016-12-22 22:44:27 +0000669Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
670 if (!isFormClass(FC_String) && Form == DW_FORM_string)
671 return None;
672 return Value.uval;
673}
674
675Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
676 if (!isFormClass(FC_Reference))
677 return None;
678 return Value.uval;
679}