blob: 81b60c57daac35b25b125cf5c3433d30f9ec11c8 [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
Paul Robinson01158442018-01-25 23:06:36 +000031static const DWARFFormValue::FormClass DWARF5FormClasses[] = {
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
Paul Robinson01158442018-01-25 23:06:36 +000060 DWARFFormValue::FC_String, // 0x1a DW_FORM_strx
61 DWARFFormValue::FC_Address, // 0x1b DW_FORM_addrx
62 DWARFFormValue::FC_Reference, // 0x1c DW_FORM_ref_sup4
63 DWARFFormValue::FC_String, // 0x1d DW_FORM_strp_sup
64 DWARFFormValue::FC_Constant, // 0x1e DW_FORM_data16
65 DWARFFormValue::FC_String, // 0x1f DW_FORM_line_strp
66 DWARFFormValue::FC_Reference, // 0x20 DW_FORM_ref_sig8
67 DWARFFormValue::FC_Constant, // 0x21 DW_FORM_implicit_const
68 DWARFFormValue::FC_SectionOffset, // 0x22 DW_FORM_loclistx
69 DWARFFormValue::FC_SectionOffset, // 0x23 DW_FORM_rnglistx
70 DWARFFormValue::FC_Reference, // 0x24 DW_FORM_ref_sup8
71 DWARFFormValue::FC_String, // 0x25 DW_FORM_strx1
72 DWARFFormValue::FC_String, // 0x26 DW_FORM_strx2
73 DWARFFormValue::FC_String, // 0x27 DW_FORM_strx3
74 DWARFFormValue::FC_String, // 0x28 DW_FORM_strx4
75 DWARFFormValue::FC_Address, // 0x29 DW_FORM_addrx1
76 DWARFFormValue::FC_Address, // 0x2a DW_FORM_addrx2
77 DWARFFormValue::FC_Address, // 0x2b DW_FORM_addrx3
78 DWARFFormValue::FC_Address, // 0x2c DW_FORM_addrx4
79
Alexey Samsonov48cbda52013-10-28 23:01:48 +000080};
81
Paul Robinson75c068c2017-06-26 18:43:01 +000082Optional<uint8_t>
83DWARFFormValue::getFixedByteSize(dwarf::Form Form,
Paul Robinson36e85a82017-06-26 19:52:32 +000084 const DWARFFormParams Params) {
Greg Clayton82f12b12016-11-11 16:21:37 +000085 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +000086 case DW_FORM_addr:
Jonas Devliegherecbf651f2018-01-05 10:03:02 +000087 if (Params)
88 return Params.AddrSize;
89 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +000090
Paul Robinsonae2e6f372017-05-03 21:53:21 +000091 case DW_FORM_block: // ULEB128 length L followed by L bytes.
92 case DW_FORM_block1: // 1 byte length L followed by L bytes.
93 case DW_FORM_block2: // 2 byte length L followed by L bytes.
94 case DW_FORM_block4: // 4 byte length L followed by L bytes.
95 case DW_FORM_string: // C-string with null terminator.
96 case DW_FORM_sdata: // SLEB128.
97 case DW_FORM_udata: // ULEB128.
98 case DW_FORM_ref_udata: // ULEB128.
99 case DW_FORM_indirect: // ULEB128.
100 case DW_FORM_exprloc: // ULEB128 length L followed by L bytes.
101 case DW_FORM_strx: // ULEB128.
102 case DW_FORM_addrx: // ULEB128.
103 case DW_FORM_loclistx: // ULEB128.
104 case DW_FORM_rnglistx: // ULEB128.
105 case DW_FORM_GNU_addr_index: // ULEB128.
106 case DW_FORM_GNU_str_index: // ULEB128.
107 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +0000108
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000109 case DW_FORM_ref_addr:
Jonas Devliegherecbf651f2018-01-05 10:03:02 +0000110 if (Params)
111 return Params.getRefAddrByteSize();
112 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +0000113
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000114 case DW_FORM_flag:
115 case DW_FORM_data1:
116 case DW_FORM_ref1:
117 case DW_FORM_strx1:
118 case DW_FORM_addrx1:
119 return 1;
Greg Clayton82f12b12016-11-11 16:21:37 +0000120
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000121 case DW_FORM_data2:
122 case DW_FORM_ref2:
123 case DW_FORM_strx2:
124 case DW_FORM_addrx2:
125 return 2;
Greg Clayton82f12b12016-11-11 16:21:37 +0000126
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000127 case DW_FORM_strx3:
128 return 3;
129
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000130 case DW_FORM_data4:
131 case DW_FORM_ref4:
132 case DW_FORM_ref_sup4:
133 case DW_FORM_strx4:
134 case DW_FORM_addrx4:
135 return 4;
Greg Clayton82f12b12016-11-11 16:21:37 +0000136
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000137 case DW_FORM_strp:
138 case DW_FORM_GNU_ref_alt:
139 case DW_FORM_GNU_strp_alt:
140 case DW_FORM_line_strp:
141 case DW_FORM_sec_offset:
142 case DW_FORM_strp_sup:
Jonas Devliegherecbf651f2018-01-05 10:03:02 +0000143 if (Params)
144 return Params.getDwarfOffsetByteSize();
145 return None;
Greg Clayton82f12b12016-11-11 16:21:37 +0000146
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000147 case DW_FORM_data8:
148 case DW_FORM_ref8:
149 case DW_FORM_ref_sig8:
150 case DW_FORM_ref_sup8:
151 return 8;
Greg Clayton82f12b12016-11-11 16:21:37 +0000152
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000153 case DW_FORM_flag_present:
154 return 0;
Greg Clayton82f12b12016-11-11 16:21:37 +0000155
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000156 case DW_FORM_data16:
157 return 16;
Greg Clayton82f12b12016-11-11 16:21:37 +0000158
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000159 case DW_FORM_implicit_const:
160 // The implicit value is stored in the abbreviation as a SLEB128, and
161 // there no data in debug info.
162 return 0;
Greg Clayton82f12b12016-11-11 16:21:37 +0000163
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000164 default:
165 llvm_unreachable("Handle this form in this switch statement");
Greg Clayton82f12b12016-11-11 16:21:37 +0000166 }
167 return None;
168}
169
Paul Robinson75c068c2017-06-26 18:43:01 +0000170bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
171 uint32_t *OffsetPtr,
Paul Robinson36e85a82017-06-26 19:52:32 +0000172 const DWARFFormParams Params) {
Greg Clayton82f12b12016-11-11 16:21:37 +0000173 bool Indirect = false;
174 do {
175 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000176 // Blocks of inlined data that have a length field and the data bytes
177 // inlined in the .debug_info.
178 case DW_FORM_exprloc:
179 case DW_FORM_block: {
180 uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
181 *OffsetPtr += size;
182 return true;
183 }
184 case DW_FORM_block1: {
185 uint8_t size = DebugInfoData.getU8(OffsetPtr);
186 *OffsetPtr += size;
187 return true;
188 }
189 case DW_FORM_block2: {
190 uint16_t size = DebugInfoData.getU16(OffsetPtr);
191 *OffsetPtr += size;
192 return true;
193 }
194 case DW_FORM_block4: {
195 uint32_t size = DebugInfoData.getU32(OffsetPtr);
196 *OffsetPtr += size;
197 return true;
198 }
199
200 // Inlined NULL terminated C-strings.
201 case DW_FORM_string:
202 DebugInfoData.getCStr(OffsetPtr);
203 return true;
204
205 case DW_FORM_addr:
206 case DW_FORM_ref_addr:
207 case DW_FORM_flag_present:
208 case DW_FORM_data1:
209 case DW_FORM_data2:
210 case DW_FORM_data4:
211 case DW_FORM_data8:
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000212 case DW_FORM_data16:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000213 case DW_FORM_flag:
214 case DW_FORM_ref1:
215 case DW_FORM_ref2:
216 case DW_FORM_ref4:
217 case DW_FORM_ref8:
218 case DW_FORM_ref_sig8:
219 case DW_FORM_ref_sup4:
220 case DW_FORM_ref_sup8:
221 case DW_FORM_strx1:
222 case DW_FORM_strx2:
223 case DW_FORM_strx4:
224 case DW_FORM_addrx1:
225 case DW_FORM_addrx2:
226 case DW_FORM_addrx4:
227 case DW_FORM_sec_offset:
228 case DW_FORM_strp:
229 case DW_FORM_strp_sup:
230 case DW_FORM_line_strp:
231 case DW_FORM_GNU_ref_alt:
232 case DW_FORM_GNU_strp_alt:
Paul Robinson75c068c2017-06-26 18:43:01 +0000233 if (Optional<uint8_t> FixedSize =
234 DWARFFormValue::getFixedByteSize(Form, Params)) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000235 *OffsetPtr += *FixedSize;
Greg Clayton82f12b12016-11-11 16:21:37 +0000236 return true;
237 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000238 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000239
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000240 // signed or unsigned LEB 128 values.
241 case DW_FORM_sdata:
242 DebugInfoData.getSLEB128(OffsetPtr);
243 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000244
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000245 case DW_FORM_udata:
246 case DW_FORM_ref_udata:
247 case DW_FORM_strx:
248 case DW_FORM_addrx:
249 case DW_FORM_loclistx:
250 case DW_FORM_rnglistx:
251 case DW_FORM_GNU_addr_index:
252 case DW_FORM_GNU_str_index:
253 DebugInfoData.getULEB128(OffsetPtr);
254 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000255
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000256 case DW_FORM_indirect:
257 Indirect = true;
258 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
259 break;
Greg Clayton82f12b12016-11-11 16:21:37 +0000260
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000261 default:
262 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000263 }
264 } while (Indirect);
265 return true;
266}
267
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000268bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
Paul Robinson01158442018-01-25 23:06:36 +0000269 // First, check DWARF5 form classes.
270 if (Form < makeArrayRef(DWARF5FormClasses).size() &&
271 DWARF5FormClasses[Form] == FC)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000272 return true;
Paul Robinson01158442018-01-25 23:06:36 +0000273 // Check more forms from extensions and proposals.
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000274 switch (Form) {
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000275 case DW_FORM_GNU_ref_alt:
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000276 return (FC == FC_Reference);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000277 case DW_FORM_GNU_addr_index:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000278 return (FC == FC_Address);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000279 case DW_FORM_GNU_str_index:
280 case DW_FORM_GNU_strp_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000281 return (FC == FC_String);
Greg Clayton6c273762016-10-27 16:32:04 +0000282 default:
283 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000284 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000285 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
286 // Don't check for DWARF version here, as some producers may still do this
Greg Clayton48432cf2017-05-01 22:07:02 +0000287 // by mistake. Also accept DW_FORM_strp since this is .debug_str section
288 // offset.
289 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
290 Form == DW_FORM_strp) &&
Benjamin Kramer68a29562015-05-25 13:28:03 +0000291 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000292}
293
Paul Robinson17536b92017-06-29 16:52:08 +0000294bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
Paul Robinsone5400f82017-11-07 19:57:12 +0000295 uint32_t *OffsetPtr, DWARFFormParams FP,
296 const DWARFUnit *CU) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000297 U = CU;
298 bool Indirect = false;
299 bool IsBlock = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000300 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000301 // Read the value for the form into value and follow and DW_FORM_indirect
302 // instances we run into
303 do {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000304 Indirect = false;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000305 switch (Form) {
306 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000307 case DW_FORM_ref_addr: {
Paul Robinsone5400f82017-11-07 19:57:12 +0000308 uint16_t Size =
309 (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
Paul Robinson17536b92017-06-29 16:52:08 +0000310 Value.uval = Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000311 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000312 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000313 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000314 case DW_FORM_block:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000315 Value.uval = Data.getULEB128(OffsetPtr);
316 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000317 break;
318 case DW_FORM_block1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000319 Value.uval = Data.getU8(OffsetPtr);
320 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000321 break;
322 case DW_FORM_block2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000323 Value.uval = Data.getU16(OffsetPtr);
324 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000325 break;
326 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000327 Value.uval = Data.getU32(OffsetPtr);
328 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000329 break;
330 case DW_FORM_data1:
331 case DW_FORM_ref1:
332 case DW_FORM_flag:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000333 case DW_FORM_strx1:
334 case DW_FORM_addrx1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000335 Value.uval = Data.getU8(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000336 break;
337 case DW_FORM_data2:
338 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000339 case DW_FORM_strx2:
340 case DW_FORM_addrx2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000341 Value.uval = Data.getU16(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000342 break;
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000343 case DW_FORM_strx3:
344 Value.uval = Data.getU24(OffsetPtr);
345 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000346 case DW_FORM_data4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000347 case DW_FORM_ref4:
348 case DW_FORM_ref_sup4:
349 case DW_FORM_strx4:
Paul Robinson17536b92017-06-29 16:52:08 +0000350 case DW_FORM_addrx4:
351 Value.uval = Data.getRelocatedValue(4, OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000352 break;
353 case DW_FORM_data8:
354 case DW_FORM_ref8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000355 case DW_FORM_ref_sup8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000356 Value.uval = Data.getU64(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000357 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000358 case DW_FORM_data16:
359 // Treat this like a 16-byte block.
360 Value.uval = 16;
361 IsBlock = true;
362 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000363 case DW_FORM_sdata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000364 Value.sval = Data.getSLEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000365 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000366 case DW_FORM_udata:
367 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000368 Value.uval = Data.getULEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000369 break;
370 case DW_FORM_string:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000371 Value.cstr = Data.getCStr(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000372 break;
373 case DW_FORM_indirect:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000374 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
375 Indirect = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000376 break;
Greg Clayton04c19282016-11-11 17:38:14 +0000377 case DW_FORM_strp:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000378 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000379 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000380 case DW_FORM_GNU_strp_alt:
381 case DW_FORM_line_strp:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000382 case DW_FORM_strp_sup: {
Paul Robinson17536b92017-06-29 16:52:08 +0000383 Value.uval =
Paul Robinsone5400f82017-11-07 19:57:12 +0000384 Data.getRelocatedValue(FP.getDwarfOffsetByteSize(), OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000385 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000386 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000387 case DW_FORM_flag_present:
388 Value.uval = 1;
389 break;
390 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000391 Value.uval = Data.getU64(OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000392 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000393 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000394 case DW_FORM_GNU_str_index:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000395 case DW_FORM_strx:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000396 Value.uval = Data.getULEB128(OffsetPtr);
Eric Christopher59c53c22012-11-16 23:44:11 +0000397 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000398 default:
Greg Clayton0e62ee72017-01-13 00:13:42 +0000399 // DWARFFormValue::skipValue() will have caught this and caused all
400 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
401 llvm_unreachable("unsupported form");
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000402 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000403 } while (Indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000404
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000405 if (IsBlock) {
406 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000407 Value.data = nullptr;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000408 if (!Str.empty()) {
409 Value.data = reinterpret_cast<const uint8_t *>(Str.data());
410 *OffsetPtr += Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000411 }
412 }
413
414 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000415}
416
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000417void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000418 uint64_t UValue = Value.uval;
419 bool CURelativeOffset = false;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000420 raw_ostream &AddrOS =
421 DumpOpts.ShowAddresses ? WithColor(OS, syntax::Address).get() : nulls();
Benjamin Kramereaa74332011-09-13 21:47:32 +0000422 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000423 case DW_FORM_addr:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000424 AddrOS << format("0x%016" PRIx64, UValue);
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000425 break;
Eric Christopher962c9082013-01-15 23:56:56 +0000426 case DW_FORM_GNU_addr_index: {
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000427 AddrOS << format(" indexed (%8.8x) address = ", (uint32_t)UValue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000428 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000429 if (U == nullptr)
430 OS << "<invalid dwarf unit>";
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000431 else if (U->getAddrOffsetSectionItem(UValue, Address))
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000432 AddrOS << format("0x%016" PRIx64, Address);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000433 else
Eric Christopher962c9082013-01-15 23:56:56 +0000434 OS << "<no .debug_addr section>";
435 break;
436 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000437 case DW_FORM_flag_present:
438 OS << "true";
439 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000440 case DW_FORM_flag:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000441 case DW_FORM_data1:
442 OS << format("0x%02x", (uint8_t)UValue);
443 break;
444 case DW_FORM_data2:
445 OS << format("0x%04x", (uint16_t)UValue);
446 break;
447 case DW_FORM_data4:
448 OS << format("0x%08x", (uint32_t)UValue);
449 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000450 case DW_FORM_ref_sig8:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000451 AddrOS << format("0x%016" PRIx64, UValue);
452 break;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000453 case DW_FORM_data8:
454 OS << format("0x%016" PRIx64, UValue);
455 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000456 case DW_FORM_data16:
457 OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
458 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000459 case DW_FORM_string:
460 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000461 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000462 OS << '"';
463 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000464 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000465 case DW_FORM_block:
466 case DW_FORM_block1:
467 case DW_FORM_block2:
468 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000469 if (UValue > 0) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000470 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000471 case DW_FORM_exprloc:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000472 case DW_FORM_block:
473 OS << format("<0x%" PRIx64 "> ", UValue);
474 break;
475 case DW_FORM_block1:
476 OS << format("<0x%2.2x> ", (uint8_t)UValue);
477 break;
478 case DW_FORM_block2:
479 OS << format("<0x%4.4x> ", (uint16_t)UValue);
480 break;
481 case DW_FORM_block4:
482 OS << format("<0x%8.8x> ", (uint32_t)UValue);
483 break;
484 default:
485 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000486 }
487
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000488 const uint8_t *DataPtr = Value.data;
489 if (DataPtr) {
490 // UValue contains size of block
491 const uint8_t *EndDataPtr = DataPtr + UValue;
492 while (DataPtr < EndDataPtr) {
493 OS << format("%2.2x ", *DataPtr);
494 ++DataPtr;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000495 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000496 } else
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000497 OS << "NULL";
498 }
499 break;
500
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000501 case DW_FORM_sdata:
502 OS << Value.sval;
503 break;
504 case DW_FORM_udata:
505 OS << Value.uval;
506 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000507 case DW_FORM_strp:
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000508 if (DumpOpts.Verbose)
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000509 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000510 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000511 break;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000512 case DW_FORM_strx:
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000513 case DW_FORM_strx1:
514 case DW_FORM_strx2:
515 case DW_FORM_strx3:
516 case DW_FORM_strx4:
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000517 case DW_FORM_GNU_str_index:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000518 if (DumpOpts.Verbose)
519 OS << format(" indexed (%8.8x) string = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000520 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000521 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000522 case DW_FORM_GNU_strp_alt:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000523 if (DumpOpts.Verbose)
524 OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000525 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000526 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000527 case DW_FORM_ref_addr:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000528 AddrOS << format("0x%016" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000529 break;
530 case DW_FORM_ref1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000531 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000532 AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000533 break;
534 case DW_FORM_ref2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000535 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000536 AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000537 break;
538 case DW_FORM_ref4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000539 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000540 AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000541 break;
542 case DW_FORM_ref8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000543 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000544 AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000545 break;
546 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000547 CURelativeOffset = true;
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000548 AddrOS << format("cu + 0x%" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000549 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000550 case DW_FORM_GNU_ref_alt:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000551 AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000552 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000553
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000554 // All DW_FORM_indirect attributes should be resolved prior to calling
555 // this function
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000556 case DW_FORM_indirect:
557 OS << "DW_FORM_indirect";
558 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000559
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000560 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000561 case DW_FORM_sec_offset:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000562 AddrOS << format("0x%08x", (uint32_t)UValue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000563 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000564
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000565 default:
566 OS << format("DW_FORM(0x%4.4x)", Form);
567 break;
568 }
569
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000570 if (CURelativeOffset && DumpOpts.Verbose) {
Adrian Prantl0c36a752015-01-06 16:50:25 +0000571 OS << " => {";
572 WithColor(OS, syntax::Address).get()
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000573 << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
Adrian Prantl0c36a752015-01-06 16:50:25 +0000574 OS << "}";
575 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000576}
577
Greg Claytoncddab272016-10-31 16:46:02 +0000578void DWARFFormValue::dumpString(raw_ostream &OS) const {
579 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000580 if (DbgStr.hasValue()) {
581 raw_ostream &COS = WithColor(OS, syntax::String);
582 COS << '"';
583 COS.write_escaped(DbgStr.getValue());
584 COS << '"';
585 }
586}
587
Greg Claytoncddab272016-10-31 16:46:02 +0000588Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000589 if (!isFormClass(FC_String))
590 return None;
591 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000592 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000593 // FIXME: Add support for DW_FORM_GNU_strp_alt
594 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000595 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000596 uint32_t Offset = Value.uval;
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000597 if (Form == DW_FORM_line_strp) {
598 if (const char *Str = U->getLineStringExtractor().getCStr(&Offset))
599 return Str;
600 return None;
601 }
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000602 if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
603 Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
604 Form == DW_FORM_strx4) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000605 uint64_t StrOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000606 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
607 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000608 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000609 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000610 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
611 return Str;
612 }
613 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000614}
615
Greg Claytoncddab272016-10-31 16:46:02 +0000616Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000617 if (!isFormClass(FC_Address))
618 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000619 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000620 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000621 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000622 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000623 return None;
624 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000625 }
626 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000627}
628
Greg Claytoncddab272016-10-31 16:46:02 +0000629Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000630 if (!isFormClass(FC_Reference))
631 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000632 switch (Form) {
633 case DW_FORM_ref1:
634 case DW_FORM_ref2:
635 case DW_FORM_ref4:
636 case DW_FORM_ref8:
637 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000638 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000639 return None;
640 return Value.uval + U->getOffset();
641 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000642 case DW_FORM_ref_sig8:
643 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000644 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000645 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000646 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000647 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000648}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000649
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000650Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
651 if (!isFormClass(FC_SectionOffset))
652 return None;
653 return Value.uval;
654}
655
656Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000657 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
658 Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000659 return None;
660 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000661}
Frederic Rissf3549a22014-09-04 06:14:35 +0000662
Frederic Riss77f850e2015-03-04 22:07:41 +0000663Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
664 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000665 (Form == DW_FORM_udata &&
666 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000667 return None;
668 switch (Form) {
669 case DW_FORM_data4:
670 return int32_t(Value.uval);
671 case DW_FORM_data2:
672 return int16_t(Value.uval);
673 case DW_FORM_data1:
674 return int8_t(Value.uval);
675 case DW_FORM_sdata:
676 case DW_FORM_data8:
677 default:
678 return Value.sval;
679 }
680}
681
Frederic Risseab17772014-09-04 06:35:09 +0000682Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000683 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
684 Form != DW_FORM_data16)
Frederic Rissf3549a22014-09-04 06:14:35 +0000685 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000686 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000687}
688
Chris Bienemane0e451d2016-12-22 22:44:27 +0000689Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
690 if (!isFormClass(FC_String) && Form == DW_FORM_string)
691 return None;
692 return Value.uval;
693}
694
695Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
696 if (!isFormClass(FC_Reference))
697 return None;
698 return Value.uval;
699}