blob: 02498733d55fb8249317e2a23df973c49e5359f3 [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[] = {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000029 0, // 0x00 unused
Alexey Samsonov32a3e782013-04-09 14:09:42 +000030 AddrSize, // 0x01 DW_FORM_addr
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000031 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
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +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
Eric Christopher3887a902012-08-24 01:14:23 +000052 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:
129 case DW_FORM_ref4:
130 Value.uval = data.getU32(offset_ptr);
131 break;
132 case DW_FORM_data8:
133 case DW_FORM_ref8:
134 Value.uval = data.getU64(offset_ptr);
135 break;
136 case DW_FORM_sdata:
137 Value.sval = data.getSLEB128(offset_ptr);
138 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000139 case DW_FORM_strp: {
Eric Christopherd1726a42012-11-12 21:40:38 +0000140 RelocAddrMap::const_iterator AI
Eric Christopher82de10a2013-01-02 23:52:13 +0000141 = cu->getRelocMap()->find(*offset_ptr);
142 if (AI != cu->getRelocMap()->end()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000143 const std::pair<uint8_t, int64_t> &R = AI->second;
Eric Christopher32b37682012-12-27 01:07:07 +0000144 Value.uval = data.getU32(offset_ptr) + R.second;
Eric Christopherd1726a42012-11-12 21:40:38 +0000145 } else
Eric Christopher806e03d2012-11-07 23:22:07 +0000146 Value.uval = data.getU32(offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000147 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000148 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000149 case DW_FORM_udata:
150 case DW_FORM_ref_udata:
151 Value.uval = data.getULEB128(offset_ptr);
152 break;
153 case DW_FORM_string:
154 Value.cstr = data.getCStr(offset_ptr);
155 // Set the string value to also be the data for inlined cstr form
156 // values only so we can tell the differnence between DW_FORM_string
157 // and DW_FORM_strp form values
Roman Divacky59324292012-09-05 22:26:57 +0000158 Value.data = (const uint8_t*)Value.cstr;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000159 break;
160 case DW_FORM_indirect:
161 Form = data.getULEB128(offset_ptr);
162 indirect = true;
163 break;
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000164 case DW_FORM_sec_offset: {
Eric Christopherd96c72a2013-01-17 02:59:59 +0000165 // FIXME: This is 64-bit for DWARF64.
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000166 RelocAddrMap::const_iterator AI
167 = cu->getRelocMap()->find(*offset_ptr);
168 if (AI != cu->getRelocMap()->end()) {
169 const std::pair<uint8_t, int64_t> &R = AI->second;
170 Value.uval = data.getU32(offset_ptr) + R.second;
171 } else
172 Value.uval = data.getU32(offset_ptr);
Eric Christopher3887a902012-08-24 01:14:23 +0000173 break;
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000174 }
Eric Christopher3887a902012-08-24 01:14:23 +0000175 case DW_FORM_flag_present:
176 Value.uval = 1;
177 break;
178 case DW_FORM_ref_sig8:
179 Value.uval = data.getU64(offset_ptr);
180 break;
Eric Christopher205e60b2012-11-16 23:44:11 +0000181 case DW_FORM_GNU_addr_index:
182 Value.uval = data.getULEB128(offset_ptr);
183 break;
184 case DW_FORM_GNU_str_index:
185 Value.uval = data.getULEB128(offset_ptr);
186 break;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000187 default:
188 return false;
189 }
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000190 } while (indirect);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000191
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000192 if (is_block) {
193 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
194 Value.data = NULL;
195 if (!str.empty()) {
196 Value.data = reinterpret_cast<const uint8_t *>(str.data());
197 *offset_ptr += Value.uval;
198 }
199 }
200
201 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000202}
203
204bool
205DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000206 const DWARFCompileUnit *cu) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000207 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000208}
209
210bool
211DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
212 uint32_t *offset_ptr, const DWARFCompileUnit *cu) {
213 bool indirect = false;
214 do {
215 indirect = false;
216 switch (form) {
217 // Blocks if inlined data that have a length field and the data bytes
218 // inlined in the .debug_info
Eric Christopher3887a902012-08-24 01:14:23 +0000219 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000220 case DW_FORM_block: {
221 uint64_t size = debug_info_data.getULEB128(offset_ptr);
222 *offset_ptr += size;
223 return true;
224 }
225 case DW_FORM_block1: {
226 uint8_t size = debug_info_data.getU8(offset_ptr);
227 *offset_ptr += size;
228 return true;
229 }
230 case DW_FORM_block2: {
231 uint16_t size = debug_info_data.getU16(offset_ptr);
232 *offset_ptr += size;
233 return true;
234 }
235 case DW_FORM_block4: {
236 uint32_t size = debug_info_data.getU32(offset_ptr);
237 *offset_ptr += size;
238 return true;
239 }
240
241 // Inlined NULL terminated C-strings
242 case DW_FORM_string:
243 debug_info_data.getCStr(offset_ptr);
244 return true;
245
246 // Compile unit address sized values
247 case DW_FORM_addr:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000248 *offset_ptr += cu->getAddressByteSize();
249 return true;
Alexey Samsonov32a3e782013-04-09 14:09:42 +0000250 case DW_FORM_ref_addr:
251 *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
252 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000253
Eric Christopher3887a902012-08-24 01:14:23 +0000254 // 0 byte values - implied from the form.
255 case DW_FORM_flag_present:
256 return true;
Eric Christophere7285c72013-01-07 22:40:48 +0000257
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000258 // 1 byte values
259 case DW_FORM_data1:
260 case DW_FORM_flag:
261 case DW_FORM_ref1:
262 *offset_ptr += 1;
263 return true;
264
265 // 2 byte values
266 case DW_FORM_data2:
267 case DW_FORM_ref2:
268 *offset_ptr += 2;
269 return true;
270
271 // 4 byte values
272 case DW_FORM_strp:
273 case DW_FORM_data4:
274 case DW_FORM_ref4:
275 *offset_ptr += 4;
276 return true;
277
278 // 8 byte values
279 case DW_FORM_data8:
280 case DW_FORM_ref8:
Eric Christopher3887a902012-08-24 01:14:23 +0000281 case DW_FORM_ref_sig8:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000282 *offset_ptr += 8;
283 return true;
284
285 // signed or unsigned LEB 128 values
286 // case DW_FORM_APPLE_db_str:
287 case DW_FORM_sdata:
288 case DW_FORM_udata:
289 case DW_FORM_ref_udata:
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000290 case DW_FORM_GNU_str_index:
291 case DW_FORM_GNU_addr_index:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000292 debug_info_data.getULEB128(offset_ptr);
293 return true;
294
295 case DW_FORM_indirect:
296 indirect = true;
297 form = debug_info_data.getULEB128(offset_ptr);
298 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000299
Eric Christopher446b88f2013-01-17 03:00:04 +0000300 // FIXME: 4 for DWARF32, 8 for DWARF64.
Eric Christopher3887a902012-08-24 01:14:23 +0000301 case DW_FORM_sec_offset:
Eric Christopher446b88f2013-01-17 03:00:04 +0000302 *offset_ptr += 4;
Eric Christopher3887a902012-08-24 01:14:23 +0000303 return true;
Eric Christophere7285c72013-01-07 22:40:48 +0000304
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000305 default:
306 return false;
307 }
308 } while (indirect);
309 return true;
310}
311
312void
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000313DWARFFormValue::dump(raw_ostream &OS, const DWARFCompileUnit *cu) const {
Eric Christopher82de10a2013-01-02 23:52:13 +0000314 DataExtractor debug_str_data(cu->getStringSection(), true, 0);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000315 DataExtractor debug_str_offset_data(cu->getStringOffsetSection(), true, 0);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000316 uint64_t uvalue = getUnsigned();
317 bool cu_relative_offset = false;
318
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000319 switch (Form) {
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000320 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000321 case DW_FORM_GNU_addr_index: {
322 StringRef AddrOffsetSec = cu->getAddrOffsetSection();
323 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
324 if (AddrOffsetSec.size() != 0) {
325 DataExtractor DA(AddrOffsetSec, true, cu->getAddressByteSize());
326 OS << format("0x%016" PRIx64, getIndirectAddress(&DA, cu));
327 } else
328 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);
376 const char* dbg_str = getAsCString(&debug_str_data);
377 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);
386 const char *dbg_str = getIndirectCString(&debug_str_data,
387 &debug_str_offset_data);
388 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*
440DWARFFormValue::getAsCString(const DataExtractor *debug_str_data_ptr) const {
441 if (isInlinedCStr()) {
442 return Value.cstr;
443 } else if (debug_str_data_ptr) {
444 uint32_t offset = Value.uval;
445 return debug_str_data_ptr->getCStr(&offset);
446 }
447 return NULL;
448}
449
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000450const char*
451DWARFFormValue::getIndirectCString(const DataExtractor *DS,
452 const DataExtractor *DSO) const {
453 if (!DS || !DSO) return NULL;
454
455 uint32_t offset = Value.uval * 4;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000456 uint32_t soffset = DSO->getU32(&offset);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000457 return DS->getCStr(&soffset);
458}
459
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000460uint64_t
461DWARFFormValue::getIndirectAddress(const DataExtractor *DA,
462 const DWARFCompileUnit *cu) const {
463 if (!DA) return 0;
464
465 uint32_t offset = Value.uval * cu->getAddressByteSize();
466 return DA->getAddress(&offset);
467}
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
486bool
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000487DWARFFormValue::resolveCompileUnitReferences(const DWARFCompileUnit *cu) {
488 switch (Form) {
489 case DW_FORM_ref1:
490 case DW_FORM_ref2:
491 case DW_FORM_ref4:
492 case DW_FORM_ref8:
493 case DW_FORM_ref_udata:
494 Value.uval += cu->getOffset();
495 Form = DW_FORM_ref_addr;
496 return true;
497 default:
498 break;
499 }
500 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000501}
502
503const uint8_t *DWARFFormValue::BlockData() const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000504 if (!isInlinedCStr())
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000505 return Value.data;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000506 return NULL;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000507}
508
509bool DWARFFormValue::isBlockForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000510 switch (form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000511 case DW_FORM_exprloc:
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000512 case DW_FORM_block:
513 case DW_FORM_block1:
514 case DW_FORM_block2:
515 case DW_FORM_block4:
516 return true;
517 }
518 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000519}
520
521bool DWARFFormValue::isDataForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000522 switch (form) {
523 case DW_FORM_sdata:
524 case DW_FORM_udata:
525 case DW_FORM_data1:
526 case DW_FORM_data2:
527 case DW_FORM_data4:
528 case DW_FORM_data8:
529 return true;
530 }
531 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000532}