blob: 8ea30c612d97ff4ec1adcb38ef8c4d530175713e [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- DWARFFormValue.cpp ------------------------------------------------===//
2//
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
Alexey Samsonovcd614552013-04-17 08:29:02 +000010#include "llvm/DebugInfo/DWARFFormValue.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000011#include "DWARFCompileUnit.h"
Benjamin Kramer34f864f2011-09-15 16:57:13 +000012#include "DWARFContext.h"
Eric Christopher806e03d2012-11-07 23:22:07 +000013#include "llvm/Support/Debug.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000014#include "llvm/Support/Dwarf.h"
15#include "llvm/Support/Format.h"
16#include "llvm/Support/raw_ostream.h"
17#include <cassert>
18using namespace llvm;
19using namespace dwarf;
20
Benjamin Krameracc897a2013-04-11 11:36:36 +000021namespace {
22template <uint8_t AddrSize, uint8_t RefAddrSize> struct FixedFormSizes {
Alexey Samsonov32a3e782013-04-09 14:09:42 +000023 static const uint8_t sizes[];
24};
Benjamin Krameracc897a2013-04-11 11:36:36 +000025}
Alexey Samsonov32a3e782013-04-09 14:09:42 +000026
27template <uint8_t AddrSize, uint8_t RefAddrSize>
28const uint8_t FixedFormSizes<AddrSize, RefAddrSize>::sizes[] = {
Eric Christopher86a0f192013-05-30 00:43:30 +000029 0, // 0x00 unused
30 AddrSize, // 0x01 DW_FORM_addr
31 0, // 0x02 unused
32 0, // 0x03 DW_FORM_block2
33 0, // 0x04 DW_FORM_block4
34 2, // 0x05 DW_FORM_data2
35 4, // 0x06 DW_FORM_data4
36 8, // 0x07 DW_FORM_data8
37 0, // 0x08 DW_FORM_string
38 0, // 0x09 DW_FORM_block
39 0, // 0x0a DW_FORM_block1
40 1, // 0x0b DW_FORM_data1
41 1, // 0x0c DW_FORM_flag
42 0, // 0x0d DW_FORM_sdata
43 4, // 0x0e DW_FORM_strp
44 0, // 0x0f DW_FORM_udata
Alexey Samsonov32a3e782013-04-09 14:09:42 +000045 RefAddrSize, // 0x10 DW_FORM_ref_addr
Eric Christopher86a0f192013-05-30 00:43:30 +000046 1, // 0x11 DW_FORM_ref1
47 2, // 0x12 DW_FORM_ref2
48 4, // 0x13 DW_FORM_ref4
49 8, // 0x14 DW_FORM_ref8
50 0, // 0x15 DW_FORM_ref_udata
51 0, // 0x16 DW_FORM_indirect
52 4, // 0x17 DW_FORM_sec_offset
53 0, // 0x18 DW_FORM_exprloc
54 0, // 0x19 DW_FORM_flag_present
55 8, // 0x20 DW_FORM_ref_sig8
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000056};
57
Alexey Samsonov32a3e782013-04-09 14:09:42 +000058static uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
59 // FIXME: Support DWARF64.
60 return (Version == 2) ? AddrSize : 4;
61}
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000062
63const uint8_t *
Alexey Samsonov32a3e782013-04-09 14:09:42 +000064DWARFFormValue::getFixedFormSizes(uint8_t AddrSize, uint16_t Version) {
65 uint8_t RefAddrSize = getRefAddrSize(AddrSize, Version);
66 if (AddrSize == 4 && RefAddrSize == 4)
67 return FixedFormSizes<4, 4>::sizes;
68 if (AddrSize == 4 && RefAddrSize == 8)
69 return FixedFormSizes<4, 8>::sizes;
70 if (AddrSize == 8 && RefAddrSize == 4)
71 return FixedFormSizes<8, 4>::sizes;
72 if (AddrSize == 8 && RefAddrSize == 8)
73 return FixedFormSizes<8, 8>::sizes;
74 return 0;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000075}
76
David Blaikiecd7c4982013-09-23 22:44:40 +000077bool DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
78 const DWARFUnit *cu) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000079 bool indirect = false;
80 bool is_block = false;
81 Value.data = NULL;
82 // Read the value for the form into value and follow and DW_FORM_indirect
83 // instances we run into
84 do {
Benjamin Kramer89aedba2011-09-15 03:11:09 +000085 indirect = false;
86 switch (Form) {
87 case DW_FORM_addr:
Eric Christopher806e03d2012-11-07 23:22:07 +000088 case DW_FORM_ref_addr: {
Alexey Samsonov32a3e782013-04-09 14:09:42 +000089 uint16_t AddrSize =
90 (Form == DW_FORM_addr)
91 ? cu->getAddressByteSize()
92 : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
93 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
Eric Christopher82de10a2013-01-02 23:52:13 +000094 if (AI != cu->getRelocMap()->end()) {
Eric Christopherd1726a42012-11-12 21:40:38 +000095 const std::pair<uint8_t, int64_t> &R = AI->second;
Alexey Samsonov32a3e782013-04-09 14:09:42 +000096 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
Eric Christopherd1726a42012-11-12 21:40:38 +000097 } else
Alexey Samsonov32a3e782013-04-09 14:09:42 +000098 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
Benjamin Kramer89aedba2011-09-15 03:11:09 +000099 break;
Alexey Samsonov4c0ae902012-11-12 14:25:36 +0000100 }
Eric Christopher3887a902012-08-24 01:14:23 +0000101 case DW_FORM_exprloc:
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000102 case DW_FORM_block:
103 Value.uval = data.getULEB128(offset_ptr);
104 is_block = true;
105 break;
106 case DW_FORM_block1:
107 Value.uval = data.getU8(offset_ptr);
108 is_block = true;
109 break;
110 case DW_FORM_block2:
111 Value.uval = data.getU16(offset_ptr);
112 is_block = true;
113 break;
114 case DW_FORM_block4:
115 Value.uval = data.getU32(offset_ptr);
116 is_block = true;
117 break;
118 case DW_FORM_data1:
119 case DW_FORM_ref1:
120 case DW_FORM_flag:
121 Value.uval = data.getU8(offset_ptr);
122 break;
123 case DW_FORM_data2:
124 case DW_FORM_ref2:
125 Value.uval = data.getU16(offset_ptr);
126 break;
127 case DW_FORM_data4:
David Blaikie3df7d2f2013-06-19 21:37:13 +0000128 case DW_FORM_ref4: {
129 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000130 Value.uval = data.getU32(offset_ptr);
David Blaikie3df7d2f2013-06-19 21:37:13 +0000131 if (AI != cu->getRelocMap()->end())
132 Value.uval += AI->second.second;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000133 break;
David Blaikie3df7d2f2013-06-19 21:37:13 +0000134 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000135 case DW_FORM_data8:
136 case DW_FORM_ref8:
137 Value.uval = data.getU64(offset_ptr);
138 break;
139 case DW_FORM_sdata:
140 Value.sval = data.getSLEB128(offset_ptr);
141 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000142 case DW_FORM_strp: {
Eric Christopherd1726a42012-11-12 21:40:38 +0000143 RelocAddrMap::const_iterator AI
Eric Christopher82de10a2013-01-02 23:52:13 +0000144 = cu->getRelocMap()->find(*offset_ptr);
145 if (AI != cu->getRelocMap()->end()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000146 const std::pair<uint8_t, int64_t> &R = AI->second;
Eric Christopher32b37682012-12-27 01:07:07 +0000147 Value.uval = data.getU32(offset_ptr) + R.second;
Eric Christopherd1726a42012-11-12 21:40:38 +0000148 } else
Eric Christopher806e03d2012-11-07 23:22:07 +0000149 Value.uval = data.getU32(offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000150 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000151 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000152 case DW_FORM_udata:
153 case DW_FORM_ref_udata:
154 Value.uval = data.getULEB128(offset_ptr);
155 break;
156 case DW_FORM_string:
157 Value.cstr = data.getCStr(offset_ptr);
158 // Set the string value to also be the data for inlined cstr form
159 // values only so we can tell the differnence between DW_FORM_string
160 // and DW_FORM_strp form values
Roman Divacky59324292012-09-05 22:26:57 +0000161 Value.data = (const uint8_t*)Value.cstr;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000162 break;
163 case DW_FORM_indirect:
164 Form = data.getULEB128(offset_ptr);
165 indirect = true;
166 break;
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000167 case DW_FORM_sec_offset: {
Eric Christopherd96c72a2013-01-17 02:59:59 +0000168 // FIXME: This is 64-bit for DWARF64.
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000169 RelocAddrMap::const_iterator AI
170 = cu->getRelocMap()->find(*offset_ptr);
171 if (AI != cu->getRelocMap()->end()) {
172 const std::pair<uint8_t, int64_t> &R = AI->second;
173 Value.uval = data.getU32(offset_ptr) + R.second;
174 } else
175 Value.uval = data.getU32(offset_ptr);
Eric Christopher3887a902012-08-24 01:14:23 +0000176 break;
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000177 }
Eric Christopher3887a902012-08-24 01:14:23 +0000178 case DW_FORM_flag_present:
179 Value.uval = 1;
180 break;
181 case DW_FORM_ref_sig8:
182 Value.uval = data.getU64(offset_ptr);
183 break;
Eric Christopher205e60b2012-11-16 23:44:11 +0000184 case DW_FORM_GNU_addr_index:
Eric Christopher205e60b2012-11-16 23:44:11 +0000185 case DW_FORM_GNU_str_index:
186 Value.uval = data.getULEB128(offset_ptr);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000187 Value.IsDWOIndex = true;
Eric Christopher205e60b2012-11-16 23:44:11 +0000188 break;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000189 default:
190 return false;
191 }
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000192 } while (indirect);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000193
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000194 if (is_block) {
195 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
196 Value.data = NULL;
197 if (!str.empty()) {
198 Value.data = reinterpret_cast<const uint8_t *>(str.data());
199 *offset_ptr += Value.uval;
200 }
201 }
202
203 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000204}
205
206bool
207DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
David Blaikiecd7c4982013-09-23 22:44:40 +0000208 const DWARFUnit *cu) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000209 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000210}
211
212bool
213DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
David Blaikiecd7c4982013-09-23 22:44:40 +0000214 uint32_t *offset_ptr, const DWARFUnit *cu) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000215 bool indirect = false;
216 do {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000217 switch (form) {
218 // Blocks if inlined data that have a length field and the data bytes
219 // inlined in the .debug_info
Eric Christopher3887a902012-08-24 01:14:23 +0000220 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000221 case DW_FORM_block: {
222 uint64_t size = debug_info_data.getULEB128(offset_ptr);
223 *offset_ptr += size;
224 return true;
225 }
226 case DW_FORM_block1: {
227 uint8_t size = debug_info_data.getU8(offset_ptr);
228 *offset_ptr += size;
229 return true;
230 }
231 case DW_FORM_block2: {
232 uint16_t size = debug_info_data.getU16(offset_ptr);
233 *offset_ptr += size;
234 return true;
235 }
236 case DW_FORM_block4: {
237 uint32_t size = debug_info_data.getU32(offset_ptr);
238 *offset_ptr += size;
239 return true;
240 }
241
242 // Inlined NULL terminated C-strings
243 case DW_FORM_string:
244 debug_info_data.getCStr(offset_ptr);
245 return true;
246
247 // Compile unit address sized values
248 case DW_FORM_addr:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000249 *offset_ptr += cu->getAddressByteSize();
250 return true;
Alexey Samsonov32a3e782013-04-09 14:09:42 +0000251 case DW_FORM_ref_addr:
252 *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
253 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000254
Eric Christopher3887a902012-08-24 01:14:23 +0000255 // 0 byte values - implied from the form.
256 case DW_FORM_flag_present:
257 return true;
Eric Christophere7285c72013-01-07 22:40:48 +0000258
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000259 // 1 byte values
260 case DW_FORM_data1:
261 case DW_FORM_flag:
262 case DW_FORM_ref1:
263 *offset_ptr += 1;
264 return true;
265
266 // 2 byte values
267 case DW_FORM_data2:
268 case DW_FORM_ref2:
269 *offset_ptr += 2;
270 return true;
271
272 // 4 byte values
273 case DW_FORM_strp:
274 case DW_FORM_data4:
275 case DW_FORM_ref4:
276 *offset_ptr += 4;
277 return true;
278
279 // 8 byte values
280 case DW_FORM_data8:
281 case DW_FORM_ref8:
Eric Christopher3887a902012-08-24 01:14:23 +0000282 case DW_FORM_ref_sig8:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000283 *offset_ptr += 8;
284 return true;
285
286 // signed or unsigned LEB 128 values
287 // case DW_FORM_APPLE_db_str:
288 case DW_FORM_sdata:
289 case DW_FORM_udata:
290 case DW_FORM_ref_udata:
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000291 case DW_FORM_GNU_str_index:
292 case DW_FORM_GNU_addr_index:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000293 debug_info_data.getULEB128(offset_ptr);
294 return true;
295
296 case DW_FORM_indirect:
297 indirect = true;
298 form = debug_info_data.getULEB128(offset_ptr);
299 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000300
Eric Christopher446b88f2013-01-17 03:00:04 +0000301 // FIXME: 4 for DWARF32, 8 for DWARF64.
Eric Christopher3887a902012-08-24 01:14:23 +0000302 case DW_FORM_sec_offset:
Eric Christopher446b88f2013-01-17 03:00:04 +0000303 *offset_ptr += 4;
Eric Christopher3887a902012-08-24 01:14:23 +0000304 return true;
Eric Christophere7285c72013-01-07 22:40:48 +0000305
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000306 default:
307 return false;
308 }
309 } while (indirect);
310 return true;
311}
312
313void
David Blaikiecd7c4982013-09-23 22:44:40 +0000314DWARFFormValue::dump(raw_ostream &OS, const DWARFUnit *cu) const {
Eric Christopher82de10a2013-01-02 23:52:13 +0000315 DataExtractor debug_str_data(cu->getStringSection(), true, 0);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000316 DataExtractor debug_str_offset_data(cu->getStringOffsetSection(), true, 0);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000317 uint64_t uvalue = getUnsigned();
318 bool cu_relative_offset = false;
319
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000320 switch (Form) {
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000321 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000322 case DW_FORM_GNU_addr_index: {
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000323 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000324 uint64_t Address;
325 if (cu->getAddrOffsetSectionItem(uvalue, Address))
326 OS << format("0x%016" PRIx64, Address);
327 else
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000328 OS << "<no .debug_addr section>";
329 break;
330 }
Eric Christopher3887a902012-08-24 01:14:23 +0000331 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000332 case DW_FORM_flag:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000333 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
334 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
335 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopher3887a902012-08-24 01:14:23 +0000336 case DW_FORM_ref_sig8:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000337 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000338 case DW_FORM_string:
339 OS << '"';
340 OS.write_escaped(getAsCString(NULL));
341 OS << '"';
342 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000343 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000344 case DW_FORM_block:
345 case DW_FORM_block1:
346 case DW_FORM_block2:
347 case DW_FORM_block4:
348 if (uvalue > 0) {
349 switch (Form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000350 case DW_FORM_exprloc:
Benjamin Kramer41a96492011-11-05 08:57:40 +0000351 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000352 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
353 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
354 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
355 default: break;
356 }
357
358 const uint8_t* data_ptr = Value.data;
359 if (data_ptr) {
360 // uvalue contains size of block
361 const uint8_t* end_data_ptr = data_ptr + uvalue;
362 while (data_ptr < end_data_ptr) {
363 OS << format("%2.2x ", *data_ptr);
364 ++data_ptr;
365 }
366 }
367 else
368 OS << "NULL";
369 }
370 break;
371
372 case DW_FORM_sdata: OS << getSigned(); break;
373 case DW_FORM_udata: OS << getUnsigned(); break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000374 case DW_FORM_strp: {
375 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000376 const char* dbg_str = getAsCString(cu);
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000377 if (dbg_str) {
378 OS << '"';
379 OS.write_escaped(dbg_str);
380 OS << '"';
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000381 }
382 break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000383 }
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000384 case DW_FORM_GNU_str_index: {
385 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000386 const char *dbg_str = getAsCString(cu);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000387 if (dbg_str) {
388 OS << '"';
389 OS.write_escaped(dbg_str);
390 OS << '"';
391 }
392 break;
393 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000394 case DW_FORM_ref_addr:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000395 OS << format("0x%016" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000396 break;
397 case DW_FORM_ref1:
398 cu_relative_offset = true;
399 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
400 break;
401 case DW_FORM_ref2:
402 cu_relative_offset = true;
403 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
404 break;
405 case DW_FORM_ref4:
406 cu_relative_offset = true;
407 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
408 break;
409 case DW_FORM_ref8:
410 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000411 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000412 break;
413 case DW_FORM_ref_udata:
414 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000415 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000416 break;
417
418 // All DW_FORM_indirect attributes should be resolved prior to calling
419 // this function
420 case DW_FORM_indirect:
421 OS << "DW_FORM_indirect";
422 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000423
Eric Christopher446b88f2013-01-17 03:00:04 +0000424 // Should be formatted to 64-bit for DWARF64.
Eric Christopher3887a902012-08-24 01:14:23 +0000425 case DW_FORM_sec_offset:
Eric Christopher446b88f2013-01-17 03:00:04 +0000426 OS << format("0x%08x", (uint32_t)uvalue);
Eric Christopher3887a902012-08-24 01:14:23 +0000427 break;
Eric Christophere7285c72013-01-07 22:40:48 +0000428
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000429 default:
430 OS << format("DW_FORM(0x%4.4x)", Form);
431 break;
432 }
433
434 if (cu_relative_offset)
Benjamin Kramere25a2bd2012-04-04 20:33:56 +0000435 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000436}
437
David Blaikiecd7c4982013-09-23 22:44:40 +0000438const char *DWARFFormValue::getAsCString(const DWARFUnit *CU) const {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000439 if (isInlinedCStr())
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000440 return Value.cstr;
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000441 if (!CU)
442 return NULL;
443 uint32_t Offset = Value.uval;
444 if (Value.IsDWOIndex) {
445 uint32_t StrOffset;
446 if (!CU->getStringOffsetSectionItem(Offset, StrOffset))
447 return NULL;
448 Offset = StrOffset;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000449 }
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000450 return CU->getStringExtractor().getCStr(&Offset);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000451}
452
David Blaikiecd7c4982013-09-23 22:44:40 +0000453uint64_t DWARFFormValue::getAsAddress(const DWARFUnit *CU) const {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000454 if (!CU)
455 return 0;
456 if (Value.IsDWOIndex) {
457 uint32_t Index = Value.uval;
458 uint64_t Address;
459 if (!CU->getAddrOffsetSectionItem(Index, Address))
460 return 0;
461 return Address;
462 }
463 return Value.uval;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000464}
465
David Blaikiecd7c4982013-09-23 22:44:40 +0000466uint64_t DWARFFormValue::getReference(const DWARFUnit *cu) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000467 uint64_t die_offset = Value.uval;
468 switch (Form) {
469 case DW_FORM_ref1:
470 case DW_FORM_ref2:
471 case DW_FORM_ref4:
472 case DW_FORM_ref8:
473 case DW_FORM_ref_udata:
474 die_offset += (cu ? cu->getOffset() : 0);
475 break;
476 default:
477 break;
478 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000479
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000480 return die_offset;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000481}
482
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000483const uint8_t *DWARFFormValue::BlockData() const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000484 if (!isInlinedCStr())
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000485 return Value.data;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000486 return NULL;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000487}
488
489bool DWARFFormValue::isBlockForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000490 switch (form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000491 case DW_FORM_exprloc:
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000492 case DW_FORM_block:
493 case DW_FORM_block1:
494 case DW_FORM_block2:
495 case DW_FORM_block4:
496 return true;
497 }
498 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000499}
500
501bool DWARFFormValue::isDataForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000502 switch (form) {
503 case DW_FORM_sdata:
504 case DW_FORM_udata:
505 case DW_FORM_data1:
506 case DW_FORM_data2:
507 case DW_FORM_data4:
508 case DW_FORM_data8:
509 return true;
510 }
511 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000512}