blob: 4ae92fc89816589dfa6def04a527ce8ddb73fdbd [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
77bool
78DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
Benjamin Kramer89aedba2011-09-15 03:11:09 +000079 const DWARFCompileUnit *cu) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000080 bool indirect = false;
81 bool is_block = false;
82 Value.data = NULL;
83 // Read the value for the form into value and follow and DW_FORM_indirect
84 // instances we run into
85 do {
Benjamin Kramer89aedba2011-09-15 03:11:09 +000086 indirect = false;
87 switch (Form) {
88 case DW_FORM_addr:
Eric Christopher806e03d2012-11-07 23:22:07 +000089 case DW_FORM_ref_addr: {
Alexey Samsonov32a3e782013-04-09 14:09:42 +000090 uint16_t AddrSize =
91 (Form == DW_FORM_addr)
92 ? cu->getAddressByteSize()
93 : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
94 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
Eric Christopher82de10a2013-01-02 23:52:13 +000095 if (AI != cu->getRelocMap()->end()) {
Eric Christopherd1726a42012-11-12 21:40:38 +000096 const std::pair<uint8_t, int64_t> &R = AI->second;
Alexey Samsonov32a3e782013-04-09 14:09:42 +000097 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
Eric Christopherd1726a42012-11-12 21:40:38 +000098 } else
Alexey Samsonov32a3e782013-04-09 14:09:42 +000099 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000100 break;
Alexey Samsonov4c0ae902012-11-12 14:25:36 +0000101 }
Eric Christopher3887a902012-08-24 01:14:23 +0000102 case DW_FORM_exprloc:
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000103 case DW_FORM_block:
104 Value.uval = data.getULEB128(offset_ptr);
105 is_block = true;
106 break;
107 case DW_FORM_block1:
108 Value.uval = data.getU8(offset_ptr);
109 is_block = true;
110 break;
111 case DW_FORM_block2:
112 Value.uval = data.getU16(offset_ptr);
113 is_block = true;
114 break;
115 case DW_FORM_block4:
116 Value.uval = data.getU32(offset_ptr);
117 is_block = true;
118 break;
119 case DW_FORM_data1:
120 case DW_FORM_ref1:
121 case DW_FORM_flag:
122 Value.uval = data.getU8(offset_ptr);
123 break;
124 case DW_FORM_data2:
125 case DW_FORM_ref2:
126 Value.uval = data.getU16(offset_ptr);
127 break;
128 case DW_FORM_data4:
David Blaikie3df7d2f2013-06-19 21:37:13 +0000129 case DW_FORM_ref4: {
130 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000131 Value.uval = data.getU32(offset_ptr);
David Blaikie3df7d2f2013-06-19 21:37:13 +0000132 if (AI != cu->getRelocMap()->end())
133 Value.uval += AI->second.second;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000134 break;
David Blaikie3df7d2f2013-06-19 21:37:13 +0000135 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000136 case DW_FORM_data8:
137 case DW_FORM_ref8:
138 Value.uval = data.getU64(offset_ptr);
139 break;
140 case DW_FORM_sdata:
141 Value.sval = data.getSLEB128(offset_ptr);
142 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000143 case DW_FORM_strp: {
Eric Christopherd1726a42012-11-12 21:40:38 +0000144 RelocAddrMap::const_iterator AI
Eric Christopher82de10a2013-01-02 23:52:13 +0000145 = cu->getRelocMap()->find(*offset_ptr);
146 if (AI != cu->getRelocMap()->end()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000147 const std::pair<uint8_t, int64_t> &R = AI->second;
Eric Christopher32b37682012-12-27 01:07:07 +0000148 Value.uval = data.getU32(offset_ptr) + R.second;
Eric Christopherd1726a42012-11-12 21:40:38 +0000149 } else
Eric Christopher806e03d2012-11-07 23:22:07 +0000150 Value.uval = data.getU32(offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000151 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000152 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000153 case DW_FORM_udata:
154 case DW_FORM_ref_udata:
155 Value.uval = data.getULEB128(offset_ptr);
156 break;
157 case DW_FORM_string:
158 Value.cstr = data.getCStr(offset_ptr);
159 // Set the string value to also be the data for inlined cstr form
160 // values only so we can tell the differnence between DW_FORM_string
161 // and DW_FORM_strp form values
Roman Divacky59324292012-09-05 22:26:57 +0000162 Value.data = (const uint8_t*)Value.cstr;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000163 break;
164 case DW_FORM_indirect:
165 Form = data.getULEB128(offset_ptr);
166 indirect = true;
167 break;
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000168 case DW_FORM_sec_offset: {
Eric Christopherd96c72a2013-01-17 02:59:59 +0000169 // FIXME: This is 64-bit for DWARF64.
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000170 RelocAddrMap::const_iterator AI
171 = cu->getRelocMap()->find(*offset_ptr);
172 if (AI != cu->getRelocMap()->end()) {
173 const std::pair<uint8_t, int64_t> &R = AI->second;
174 Value.uval = data.getU32(offset_ptr) + R.second;
175 } else
176 Value.uval = data.getU32(offset_ptr);
Eric Christopher3887a902012-08-24 01:14:23 +0000177 break;
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000178 }
Eric Christopher3887a902012-08-24 01:14:23 +0000179 case DW_FORM_flag_present:
180 Value.uval = 1;
181 break;
182 case DW_FORM_ref_sig8:
183 Value.uval = data.getU64(offset_ptr);
184 break;
Eric Christopher205e60b2012-11-16 23:44:11 +0000185 case DW_FORM_GNU_addr_index:
Eric Christopher205e60b2012-11-16 23:44:11 +0000186 case DW_FORM_GNU_str_index:
187 Value.uval = data.getULEB128(offset_ptr);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000188 Value.IsDWOIndex = true;
Eric Christopher205e60b2012-11-16 23:44:11 +0000189 break;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000190 default:
191 return false;
192 }
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000193 } while (indirect);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000194
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000195 if (is_block) {
196 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
197 Value.data = NULL;
198 if (!str.empty()) {
199 Value.data = reinterpret_cast<const uint8_t *>(str.data());
200 *offset_ptr += Value.uval;
201 }
202 }
203
204 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000205}
206
207bool
208DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000209 const DWARFCompileUnit *cu) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000210 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000211}
212
213bool
214DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
215 uint32_t *offset_ptr, const DWARFCompileUnit *cu) {
216 bool indirect = false;
217 do {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000218 switch (form) {
219 // Blocks if inlined data that have a length field and the data bytes
220 // inlined in the .debug_info
Eric Christopher3887a902012-08-24 01:14:23 +0000221 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000222 case DW_FORM_block: {
223 uint64_t size = debug_info_data.getULEB128(offset_ptr);
224 *offset_ptr += size;
225 return true;
226 }
227 case DW_FORM_block1: {
228 uint8_t size = debug_info_data.getU8(offset_ptr);
229 *offset_ptr += size;
230 return true;
231 }
232 case DW_FORM_block2: {
233 uint16_t size = debug_info_data.getU16(offset_ptr);
234 *offset_ptr += size;
235 return true;
236 }
237 case DW_FORM_block4: {
238 uint32_t size = debug_info_data.getU32(offset_ptr);
239 *offset_ptr += size;
240 return true;
241 }
242
243 // Inlined NULL terminated C-strings
244 case DW_FORM_string:
245 debug_info_data.getCStr(offset_ptr);
246 return true;
247
248 // Compile unit address sized values
249 case DW_FORM_addr:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000250 *offset_ptr += cu->getAddressByteSize();
251 return true;
Alexey Samsonov32a3e782013-04-09 14:09:42 +0000252 case DW_FORM_ref_addr:
253 *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
254 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000255
Eric Christopher3887a902012-08-24 01:14:23 +0000256 // 0 byte values - implied from the form.
257 case DW_FORM_flag_present:
258 return true;
Eric Christophere7285c72013-01-07 22:40:48 +0000259
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000260 // 1 byte values
261 case DW_FORM_data1:
262 case DW_FORM_flag:
263 case DW_FORM_ref1:
264 *offset_ptr += 1;
265 return true;
266
267 // 2 byte values
268 case DW_FORM_data2:
269 case DW_FORM_ref2:
270 *offset_ptr += 2;
271 return true;
272
273 // 4 byte values
274 case DW_FORM_strp:
275 case DW_FORM_data4:
276 case DW_FORM_ref4:
277 *offset_ptr += 4;
278 return true;
279
280 // 8 byte values
281 case DW_FORM_data8:
282 case DW_FORM_ref8:
Eric Christopher3887a902012-08-24 01:14:23 +0000283 case DW_FORM_ref_sig8:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000284 *offset_ptr += 8;
285 return true;
286
287 // signed or unsigned LEB 128 values
288 // case DW_FORM_APPLE_db_str:
289 case DW_FORM_sdata:
290 case DW_FORM_udata:
291 case DW_FORM_ref_udata:
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000292 case DW_FORM_GNU_str_index:
293 case DW_FORM_GNU_addr_index:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000294 debug_info_data.getULEB128(offset_ptr);
295 return true;
296
297 case DW_FORM_indirect:
298 indirect = true;
299 form = debug_info_data.getULEB128(offset_ptr);
300 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000301
Eric Christopher446b88f2013-01-17 03:00:04 +0000302 // FIXME: 4 for DWARF32, 8 for DWARF64.
Eric Christopher3887a902012-08-24 01:14:23 +0000303 case DW_FORM_sec_offset:
Eric Christopher446b88f2013-01-17 03:00:04 +0000304 *offset_ptr += 4;
Eric Christopher3887a902012-08-24 01:14:23 +0000305 return true;
Eric Christophere7285c72013-01-07 22:40:48 +0000306
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000307 default:
308 return false;
309 }
310 } while (indirect);
311 return true;
312}
313
314void
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000315DWARFFormValue::dump(raw_ostream &OS, const DWARFCompileUnit *cu) const {
Eric Christopher82de10a2013-01-02 23:52:13 +0000316 DataExtractor debug_str_data(cu->getStringSection(), true, 0);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000317 DataExtractor debug_str_offset_data(cu->getStringOffsetSection(), true, 0);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000318 uint64_t uvalue = getUnsigned();
319 bool cu_relative_offset = false;
320
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000321 switch (Form) {
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000322 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000323 case DW_FORM_GNU_addr_index: {
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000324 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000325 uint64_t Address;
326 if (cu->getAddrOffsetSectionItem(uvalue, Address))
327 OS << format("0x%016" PRIx64, Address);
328 else
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000329 OS << "<no .debug_addr section>";
330 break;
331 }
Eric Christopher3887a902012-08-24 01:14:23 +0000332 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000333 case DW_FORM_flag:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000334 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
335 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
336 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopher3887a902012-08-24 01:14:23 +0000337 case DW_FORM_ref_sig8:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000338 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000339 case DW_FORM_string:
340 OS << '"';
341 OS.write_escaped(getAsCString(NULL));
342 OS << '"';
343 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000344 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000345 case DW_FORM_block:
346 case DW_FORM_block1:
347 case DW_FORM_block2:
348 case DW_FORM_block4:
349 if (uvalue > 0) {
350 switch (Form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000351 case DW_FORM_exprloc:
Benjamin Kramer41a96492011-11-05 08:57:40 +0000352 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000353 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
354 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
355 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
356 default: break;
357 }
358
359 const uint8_t* data_ptr = Value.data;
360 if (data_ptr) {
361 // uvalue contains size of block
362 const uint8_t* end_data_ptr = data_ptr + uvalue;
363 while (data_ptr < end_data_ptr) {
364 OS << format("%2.2x ", *data_ptr);
365 ++data_ptr;
366 }
367 }
368 else
369 OS << "NULL";
370 }
371 break;
372
373 case DW_FORM_sdata: OS << getSigned(); break;
374 case DW_FORM_udata: OS << getUnsigned(); break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000375 case DW_FORM_strp: {
376 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000377 const char* dbg_str = getAsCString(cu);
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000378 if (dbg_str) {
379 OS << '"';
380 OS.write_escaped(dbg_str);
381 OS << '"';
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000382 }
383 break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000384 }
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000385 case DW_FORM_GNU_str_index: {
386 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000387 const char *dbg_str = getAsCString(cu);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000388 if (dbg_str) {
389 OS << '"';
390 OS.write_escaped(dbg_str);
391 OS << '"';
392 }
393 break;
394 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000395 case DW_FORM_ref_addr:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000396 OS << format("0x%016" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000397 break;
398 case DW_FORM_ref1:
399 cu_relative_offset = true;
400 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
401 break;
402 case DW_FORM_ref2:
403 cu_relative_offset = true;
404 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
405 break;
406 case DW_FORM_ref4:
407 cu_relative_offset = true;
408 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
409 break;
410 case DW_FORM_ref8:
411 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000412 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000413 break;
414 case DW_FORM_ref_udata:
415 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000416 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000417 break;
418
419 // All DW_FORM_indirect attributes should be resolved prior to calling
420 // this function
421 case DW_FORM_indirect:
422 OS << "DW_FORM_indirect";
423 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000424
Eric Christopher446b88f2013-01-17 03:00:04 +0000425 // Should be formatted to 64-bit for DWARF64.
Eric Christopher3887a902012-08-24 01:14:23 +0000426 case DW_FORM_sec_offset:
Eric Christopher446b88f2013-01-17 03:00:04 +0000427 OS << format("0x%08x", (uint32_t)uvalue);
Eric Christopher3887a902012-08-24 01:14:23 +0000428 break;
Eric Christophere7285c72013-01-07 22:40:48 +0000429
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000430 default:
431 OS << format("DW_FORM(0x%4.4x)", Form);
432 break;
433 }
434
435 if (cu_relative_offset)
Benjamin Kramere25a2bd2012-04-04 20:33:56 +0000436 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000437}
438
439const char*
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000440DWARFFormValue::getAsCString(const DWARFCompileUnit *CU) const {
441 if (isInlinedCStr())
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000442 return Value.cstr;
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000443 if (!CU)
444 return NULL;
445 uint32_t Offset = Value.uval;
446 if (Value.IsDWOIndex) {
447 uint32_t StrOffset;
448 if (!CU->getStringOffsetSectionItem(Offset, StrOffset))
449 return NULL;
450 Offset = StrOffset;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000451 }
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000452 return CU->getStringExtractor().getCStr(&Offset);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000453}
454
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000455uint64_t
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000456DWARFFormValue::getAsAddress(const DWARFCompileUnit *CU) const {
457 if (!CU)
458 return 0;
459 if (Value.IsDWOIndex) {
460 uint32_t Index = Value.uval;
461 uint64_t Address;
462 if (!CU->getAddrOffsetSectionItem(Index, Address))
463 return 0;
464 return Address;
465 }
466 return Value.uval;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000467}
468
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000469uint64_t DWARFFormValue::getReference(const DWARFCompileUnit *cu) const {
470 uint64_t die_offset = Value.uval;
471 switch (Form) {
472 case DW_FORM_ref1:
473 case DW_FORM_ref2:
474 case DW_FORM_ref4:
475 case DW_FORM_ref8:
476 case DW_FORM_ref_udata:
477 die_offset += (cu ? cu->getOffset() : 0);
478 break;
479 default:
480 break;
481 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000482
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000483 return die_offset;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000484}
485
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000486const uint8_t *DWARFFormValue::BlockData() const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000487 if (!isInlinedCStr())
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000488 return Value.data;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000489 return NULL;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000490}
491
492bool DWARFFormValue::isBlockForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000493 switch (form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000494 case DW_FORM_exprloc:
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000495 case DW_FORM_block:
496 case DW_FORM_block1:
497 case DW_FORM_block2:
498 case DW_FORM_block4:
499 return true;
500 }
501 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000502}
503
504bool DWARFFormValue::isDataForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000505 switch (form) {
506 case DW_FORM_sdata:
507 case DW_FORM_udata:
508 case DW_FORM_data1:
509 case DW_FORM_data2:
510 case DW_FORM_data4:
511 case DW_FORM_data8:
512 return true;
513 }
514 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000515}