blob: 83a7792e12447f1414a693be9ae155c5ea29c043 [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:
189 case DW_FORM_flag:
190 case DW_FORM_ref1:
191 case DW_FORM_ref2:
192 case DW_FORM_ref4:
193 case DW_FORM_ref8:
194 case DW_FORM_ref_sig8:
195 case DW_FORM_ref_sup4:
196 case DW_FORM_ref_sup8:
197 case DW_FORM_strx1:
198 case DW_FORM_strx2:
199 case DW_FORM_strx4:
200 case DW_FORM_addrx1:
201 case DW_FORM_addrx2:
202 case DW_FORM_addrx4:
203 case DW_FORM_sec_offset:
204 case DW_FORM_strp:
205 case DW_FORM_strp_sup:
206 case DW_FORM_line_strp:
207 case DW_FORM_GNU_ref_alt:
208 case DW_FORM_GNU_strp_alt:
Paul Robinson75c068c2017-06-26 18:43:01 +0000209 if (Optional<uint8_t> FixedSize =
210 DWARFFormValue::getFixedByteSize(Form, Params)) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000211 *OffsetPtr += *FixedSize;
Greg Clayton82f12b12016-11-11 16:21:37 +0000212 return true;
213 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000214 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000215
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000216 // signed or unsigned LEB 128 values.
217 case DW_FORM_sdata:
218 DebugInfoData.getSLEB128(OffsetPtr);
219 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000220
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000221 case DW_FORM_udata:
222 case DW_FORM_ref_udata:
223 case DW_FORM_strx:
224 case DW_FORM_addrx:
225 case DW_FORM_loclistx:
226 case DW_FORM_rnglistx:
227 case DW_FORM_GNU_addr_index:
228 case DW_FORM_GNU_str_index:
229 DebugInfoData.getULEB128(OffsetPtr);
230 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000231
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000232 case DW_FORM_indirect:
233 Indirect = true;
234 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
235 break;
Greg Clayton82f12b12016-11-11 16:21:37 +0000236
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000237 default:
238 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000239 }
240 } while (Indirect);
241 return true;
242}
243
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000244bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
245 // First, check DWARF4 form classes.
Craig Topper0013be12015-09-21 05:32:41 +0000246 if (Form < makeArrayRef(DWARF4FormClasses).size() &&
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000247 DWARF4FormClasses[Form] == FC)
248 return true;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000249 // Check more forms from DWARF4 and DWARF5 proposals.
250 switch (Form) {
251 case DW_FORM_ref_sig8:
252 case DW_FORM_GNU_ref_alt:
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000253 return (FC == FC_Reference);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000254 case DW_FORM_GNU_addr_index:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000255 return (FC == FC_Address);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000256 case DW_FORM_GNU_str_index:
257 case DW_FORM_GNU_strp_alt:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000258 case DW_FORM_strx:
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000259 case DW_FORM_strx1:
260 case DW_FORM_strx2:
261 case DW_FORM_strx3:
262 case DW_FORM_strx4:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000263 return (FC == FC_String);
Victor Leschukcbddae72017-01-10 21:18:26 +0000264 case DW_FORM_implicit_const:
265 return (FC == FC_Constant);
Greg Clayton6c273762016-10-27 16:32:04 +0000266 default:
267 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000268 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000269 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
270 // Don't check for DWARF version here, as some producers may still do this
Greg Clayton48432cf2017-05-01 22:07:02 +0000271 // by mistake. Also accept DW_FORM_strp since this is .debug_str section
272 // offset.
273 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
274 Form == DW_FORM_strp) &&
Benjamin Kramer68a29562015-05-25 13:28:03 +0000275 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000276}
277
Paul Robinson17536b92017-06-29 16:52:08 +0000278bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000279 uint32_t *OffsetPtr, const DWARFUnit *CU) {
280 U = CU;
281 bool Indirect = false;
282 bool IsBlock = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000283 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000284 // Read the value for the form into value and follow and DW_FORM_indirect
285 // instances we run into
286 do {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000287 Indirect = false;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000288 switch (Form) {
289 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000290 case DW_FORM_ref_addr: {
Greg Claytoncddab272016-10-31 16:46:02 +0000291 if (!U)
Frederic Risse4576d22014-11-12 23:48:04 +0000292 return false;
Paul Robinson17536b92017-06-29 16:52:08 +0000293 uint16_t Size = (Form == DW_FORM_addr) ? U->getAddressByteSize()
294 : U->getRefAddrByteSize();
295 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;
343 case DW_FORM_sdata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000344 Value.sval = Data.getSLEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000345 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000346 case DW_FORM_udata:
347 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000348 Value.uval = Data.getULEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000349 break;
350 case DW_FORM_string:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000351 Value.cstr = Data.getCStr(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000352 break;
353 case DW_FORM_indirect:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000354 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
355 Indirect = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000356 break;
Greg Clayton04c19282016-11-11 17:38:14 +0000357 case DW_FORM_strp:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000358 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000359 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000360 case DW_FORM_GNU_strp_alt:
361 case DW_FORM_line_strp:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000362 case DW_FORM_strp_sup: {
Greg Claytoncddab272016-10-31 16:46:02 +0000363 if (!U)
Greg Clayton82f12b12016-11-11 16:21:37 +0000364 return false;
Paul Robinson17536b92017-06-29 16:52:08 +0000365 Value.uval =
366 Data.getRelocatedValue(U->getDwarfOffsetByteSize(), OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000367 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000368 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000369 case DW_FORM_flag_present:
370 Value.uval = 1;
371 break;
372 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000373 Value.uval = Data.getU64(OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000374 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000375 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000376 case DW_FORM_GNU_str_index:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000377 case DW_FORM_strx:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000378 Value.uval = Data.getULEB128(OffsetPtr);
Eric Christopher59c53c22012-11-16 23:44:11 +0000379 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000380 default:
Greg Clayton0e62ee72017-01-13 00:13:42 +0000381 // DWARFFormValue::skipValue() will have caught this and caused all
382 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
383 llvm_unreachable("unsupported form");
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000384 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000385 } while (Indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000386
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000387 if (IsBlock) {
388 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000389 Value.data = nullptr;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000390 if (!Str.empty()) {
391 Value.data = reinterpret_cast<const uint8_t *>(Str.data());
392 *OffsetPtr += Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000393 }
394 }
395
396 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000397}
398
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000399void DWARFFormValue::dump(raw_ostream &OS) const {
400 uint64_t UValue = Value.uval;
401 bool CURelativeOffset = false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000402
Benjamin Kramereaa74332011-09-13 21:47:32 +0000403 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000404 case DW_FORM_addr:
405 OS << format("0x%016" PRIx64, UValue);
406 break;
Eric Christopher962c9082013-01-15 23:56:56 +0000407 case DW_FORM_GNU_addr_index: {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000408 OS << format(" indexed (%8.8x) address = ", (uint32_t)UValue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000409 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000410 if (U == nullptr)
411 OS << "<invalid dwarf unit>";
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000412 else if (U->getAddrOffsetSectionItem(UValue, Address))
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000413 OS << format("0x%016" PRIx64, Address);
414 else
Eric Christopher962c9082013-01-15 23:56:56 +0000415 OS << "<no .debug_addr section>";
416 break;
417 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000418 case DW_FORM_flag_present:
419 OS << "true";
420 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000421 case DW_FORM_flag:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000422 case DW_FORM_data1:
423 OS << format("0x%02x", (uint8_t)UValue);
424 break;
425 case DW_FORM_data2:
426 OS << format("0x%04x", (uint16_t)UValue);
427 break;
428 case DW_FORM_data4:
429 OS << format("0x%08x", (uint32_t)UValue);
430 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000431 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000432 case DW_FORM_data8:
433 OS << format("0x%016" PRIx64, UValue);
434 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000435 case DW_FORM_string:
436 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000437 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000438 OS << '"';
439 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000440 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000441 case DW_FORM_block:
442 case DW_FORM_block1:
443 case DW_FORM_block2:
444 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000445 if (UValue > 0) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000446 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000447 case DW_FORM_exprloc:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000448 case DW_FORM_block:
449 OS << format("<0x%" PRIx64 "> ", UValue);
450 break;
451 case DW_FORM_block1:
452 OS << format("<0x%2.2x> ", (uint8_t)UValue);
453 break;
454 case DW_FORM_block2:
455 OS << format("<0x%4.4x> ", (uint16_t)UValue);
456 break;
457 case DW_FORM_block4:
458 OS << format("<0x%8.8x> ", (uint32_t)UValue);
459 break;
460 default:
461 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000462 }
463
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000464 const uint8_t *DataPtr = Value.data;
465 if (DataPtr) {
466 // UValue contains size of block
467 const uint8_t *EndDataPtr = DataPtr + UValue;
468 while (DataPtr < EndDataPtr) {
469 OS << format("%2.2x ", *DataPtr);
470 ++DataPtr;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000471 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000472 } else
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000473 OS << "NULL";
474 }
475 break;
476
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000477 case DW_FORM_sdata:
478 OS << Value.sval;
479 break;
480 case DW_FORM_udata:
481 OS << Value.uval;
482 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000483 case DW_FORM_strp:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000484 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000485 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000486 break;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000487 case DW_FORM_strx:
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000488 case DW_FORM_strx1:
489 case DW_FORM_strx2:
490 case DW_FORM_strx3:
491 case DW_FORM_strx4:
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000492 case DW_FORM_GNU_str_index:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000493 OS << format(" indexed (%8.8x) string = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000494 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000495 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000496 case DW_FORM_GNU_strp_alt:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000497 OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000498 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000499 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000500 case DW_FORM_ref_addr:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000501 OS << format("0x%016" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000502 break;
503 case DW_FORM_ref1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000504 CURelativeOffset = true;
505 OS << format("cu + 0x%2.2x", (uint8_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000506 break;
507 case DW_FORM_ref2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000508 CURelativeOffset = true;
509 OS << format("cu + 0x%4.4x", (uint16_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000510 break;
511 case DW_FORM_ref4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000512 CURelativeOffset = true;
513 OS << format("cu + 0x%4.4x", (uint32_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000514 break;
515 case DW_FORM_ref8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000516 CURelativeOffset = true;
517 OS << format("cu + 0x%8.8" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000518 break;
519 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000520 CURelativeOffset = true;
521 OS << format("cu + 0x%" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000522 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000523 case DW_FORM_GNU_ref_alt:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000524 OS << format("<alt 0x%" PRIx64 ">", UValue);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000525 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000526
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000527 // All DW_FORM_indirect attributes should be resolved prior to calling
528 // this function
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000529 case DW_FORM_indirect:
530 OS << "DW_FORM_indirect";
531 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000532
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000533 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000534 case DW_FORM_sec_offset:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000535 OS << format("0x%08x", (uint32_t)UValue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000536 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000537
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000538 default:
539 OS << format("DW_FORM(0x%4.4x)", Form);
540 break;
541 }
542
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000543 if (CURelativeOffset) {
Adrian Prantl0c36a752015-01-06 16:50:25 +0000544 OS << " => {";
545 WithColor(OS, syntax::Address).get()
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000546 << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000547 OS << "}";
548 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000549}
550
Greg Claytoncddab272016-10-31 16:46:02 +0000551void DWARFFormValue::dumpString(raw_ostream &OS) const {
552 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000553 if (DbgStr.hasValue()) {
554 raw_ostream &COS = WithColor(OS, syntax::String);
555 COS << '"';
556 COS.write_escaped(DbgStr.getValue());
557 COS << '"';
558 }
559}
560
Greg Claytoncddab272016-10-31 16:46:02 +0000561Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000562 if (!isFormClass(FC_String))
563 return None;
564 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000565 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000566 // FIXME: Add support for DW_FORM_GNU_strp_alt
567 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000568 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000569 uint32_t Offset = Value.uval;
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000570 if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
571 Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
572 Form == DW_FORM_strx4) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000573 uint64_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000574 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
575 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000576 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000577 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000578 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
579 return Str;
580 }
581 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000582}
583
Greg Claytoncddab272016-10-31 16:46:02 +0000584Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000585 if (!isFormClass(FC_Address))
586 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000587 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000588 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000589 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000590 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000591 return None;
592 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000593 }
594 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000595}
596
Greg Claytoncddab272016-10-31 16:46:02 +0000597Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000598 if (!isFormClass(FC_Reference))
599 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000600 switch (Form) {
601 case DW_FORM_ref1:
602 case DW_FORM_ref2:
603 case DW_FORM_ref4:
604 case DW_FORM_ref8:
605 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000606 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000607 return None;
608 return Value.uval + U->getOffset();
609 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000610 case DW_FORM_ref_sig8:
611 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000612 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000613 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000614 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000615 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000616}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000617
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000618Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
619 if (!isFormClass(FC_SectionOffset))
620 return None;
621 return Value.uval;
622}
623
624Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000625 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
626 Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000627 return None;
628 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000629}
Frederic Rissf3549a22014-09-04 06:14:35 +0000630
Frederic Riss77f850e2015-03-04 22:07:41 +0000631Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
632 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000633 (Form == DW_FORM_udata &&
634 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000635 return None;
636 switch (Form) {
637 case DW_FORM_data4:
638 return int32_t(Value.uval);
639 case DW_FORM_data2:
640 return int16_t(Value.uval);
641 case DW_FORM_data1:
642 return int8_t(Value.uval);
643 case DW_FORM_sdata:
644 case DW_FORM_data8:
645 default:
646 return Value.sval;
647 }
648}
649
Frederic Risseab17772014-09-04 06:35:09 +0000650Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Frederic Rissf3549a22014-09-04 06:14:35 +0000651 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
652 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000653 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000654}
655
Chris Bienemane0e451d2016-12-22 22:44:27 +0000656Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
657 if (!isFormClass(FC_String) && Form == DW_FORM_string)
658 return None;
659 return Value.uval;
660}
661
662Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
663 if (!isFormClass(FC_Reference))
664 return None;
665 return Value.uval;
666}