[DebugInfo] Remove unneeded struct member and hide struct definition. No functionalit...
[oota-llvm.git] / lib / DebugInfo / DWARFFormValue.cpp
1 //===-- 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
10 #include "llvm/DebugInfo/DWARFFormValue.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Support/Dwarf.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <cassert>
18 using namespace llvm;
19 using namespace dwarf;
20
21 namespace {
22 template <uint8_t AddrSize, uint8_t RefAddrSize> struct FixedFormSizes {
23   static const uint8_t sizes[];
24 };
25 }
26
27 template <uint8_t AddrSize, uint8_t RefAddrSize>
28 const uint8_t FixedFormSizes<AddrSize, RefAddrSize>::sizes[] = {
29   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
45   RefAddrSize, // 0x10 DW_FORM_ref_addr
46   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
56 };
57
58 static uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
59   // FIXME: Support DWARF64.
60   return (Version == 2) ? AddrSize : 4;
61 }
62
63 const uint8_t *
64 DWARFFormValue::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;
75 }
76
77 bool DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
78                                   const DWARFUnit *cu) {
79   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 {
85     indirect = false;
86     switch (Form) {
87     case DW_FORM_addr:
88     case DW_FORM_ref_addr: {
89       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);
94       if (AI != cu->getRelocMap()->end()) {
95         const std::pair<uint8_t, int64_t> &R = AI->second;
96         Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
97       } else
98         Value.uval = data.getUnsigned(offset_ptr, AddrSize);
99       break;
100     }
101     case DW_FORM_exprloc:
102     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:
128     case DW_FORM_ref4: {
129       RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
130       Value.uval = data.getU32(offset_ptr);
131       if (AI != cu->getRelocMap()->end())
132         Value.uval += AI->second.second;
133       break;
134     }
135     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;
142     case DW_FORM_strp: {
143       RelocAddrMap::const_iterator AI
144         = cu->getRelocMap()->find(*offset_ptr);
145       if (AI != cu->getRelocMap()->end()) {
146         const std::pair<uint8_t, int64_t> &R = AI->second;
147         Value.uval = data.getU32(offset_ptr) + R.second;
148       } else
149         Value.uval = data.getU32(offset_ptr);
150       break;
151     }
152     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
161       Value.data = (const uint8_t*)Value.cstr;
162       break;
163     case DW_FORM_indirect:
164       Form = data.getULEB128(offset_ptr);
165       indirect = true;
166       break;
167     case DW_FORM_sec_offset: {
168       // FIXME: This is 64-bit for DWARF64.
169       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);
176       break;
177     }
178     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;
184     case DW_FORM_GNU_addr_index:
185     case DW_FORM_GNU_str_index:
186       Value.uval = data.getULEB128(offset_ptr);
187       break;
188     default:
189       return false;
190     }
191   } while (indirect);
192
193   if (is_block) {
194     StringRef str = data.getData().substr(*offset_ptr, Value.uval);
195     Value.data = NULL;
196     if (!str.empty()) {
197       Value.data = reinterpret_cast<const uint8_t *>(str.data());
198       *offset_ptr += Value.uval;
199     }
200   }
201
202   return true;
203 }
204
205 bool
206 DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
207                           const DWARFUnit *cu) const {
208   return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
209 }
210
211 bool
212 DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
213                           uint32_t *offset_ptr, const DWARFUnit *cu) {
214   bool indirect = false;
215   do {
216     switch (form) {
217     // Blocks if inlined data that have a length field and the data bytes
218     // inlined in the .debug_info
219     case DW_FORM_exprloc:
220     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:
248       *offset_ptr += cu->getAddressByteSize();
249       return true;
250     case DW_FORM_ref_addr:
251       *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
252       return true;
253
254     // 0 byte values - implied from the form.
255     case DW_FORM_flag_present:
256       return true;
257
258     // 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:
281     case DW_FORM_ref_sig8:
282       *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:
290     case DW_FORM_GNU_str_index:
291     case DW_FORM_GNU_addr_index:
292       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;
299
300     // FIXME: 4 for DWARF32, 8 for DWARF64.
301     case DW_FORM_sec_offset:
302       *offset_ptr += 4;
303       return true;
304
305     default:
306       return false;
307     }
308   } while (indirect);
309   return true;
310 }
311
312 void
313 DWARFFormValue::dump(raw_ostream &OS, const DWARFUnit *cu) const {
314   DataExtractor debug_str_data(cu->getStringSection(), true, 0);
315   DataExtractor debug_str_offset_data(cu->getStringOffsetSection(), true, 0);
316   uint64_t uvalue = getUnsigned();
317   bool cu_relative_offset = false;
318
319   switch (Form) {
320   case DW_FORM_addr:      OS << format("0x%016" PRIx64, uvalue); break;
321   case DW_FORM_GNU_addr_index: {
322     OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
323     uint64_t Address;
324     if (cu->getAddrOffsetSectionItem(uvalue, Address))
325       OS << format("0x%016" PRIx64, Address);
326     else
327       OS << "<no .debug_addr section>";
328     break;
329   }
330   case DW_FORM_flag_present: OS << "true"; break;
331   case DW_FORM_flag:
332   case DW_FORM_data1:     OS << format("0x%02x", (uint8_t)uvalue); break;
333   case DW_FORM_data2:     OS << format("0x%04x", (uint16_t)uvalue); break;
334   case DW_FORM_data4:     OS << format("0x%08x", (uint32_t)uvalue); break;
335   case DW_FORM_ref_sig8:
336   case DW_FORM_data8:     OS << format("0x%016" PRIx64, uvalue); break;
337   case DW_FORM_string:
338     OS << '"';
339     OS.write_escaped(getAsCString(NULL));
340     OS << '"';
341     break;
342   case DW_FORM_exprloc:
343   case DW_FORM_block:
344   case DW_FORM_block1:
345   case DW_FORM_block2:
346   case DW_FORM_block4:
347     if (uvalue > 0) {
348       switch (Form) {
349       case DW_FORM_exprloc:
350       case DW_FORM_block:  OS << format("<0x%" PRIx64 "> ", uvalue);     break;
351       case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue);  break;
352       case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
353       case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
354       default: break;
355       }
356
357       const uint8_t* data_ptr = Value.data;
358       if (data_ptr) {
359         // uvalue contains size of block
360         const uint8_t* end_data_ptr = data_ptr + uvalue;
361         while (data_ptr < end_data_ptr) {
362           OS << format("%2.2x ", *data_ptr);
363           ++data_ptr;
364         }
365       }
366       else
367         OS << "NULL";
368     }
369     break;
370
371   case DW_FORM_sdata:     OS << getSigned();   break;
372   case DW_FORM_udata:     OS << getUnsigned(); break;
373   case DW_FORM_strp: {
374     OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
375     const char* dbg_str = getAsCString(cu);
376     if (dbg_str) {
377       OS << '"';
378       OS.write_escaped(dbg_str);
379       OS << '"';
380     }
381     break;
382   }
383   case DW_FORM_GNU_str_index: {
384     OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
385     const char *dbg_str = getAsCString(cu);
386     if (dbg_str) {
387       OS << '"';
388       OS.write_escaped(dbg_str);
389       OS << '"';
390     }
391     break;
392   }
393   case DW_FORM_ref_addr:
394     OS << format("0x%016" PRIx64, uvalue);
395     break;
396   case DW_FORM_ref1:
397     cu_relative_offset = true;
398     OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
399     break;
400   case DW_FORM_ref2:
401     cu_relative_offset = true;
402     OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
403     break;
404   case DW_FORM_ref4:
405     cu_relative_offset = true;
406     OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
407     break;
408   case DW_FORM_ref8:
409     cu_relative_offset = true;
410     OS << format("cu + 0x%8.8" PRIx64, uvalue);
411     break;
412   case DW_FORM_ref_udata:
413     cu_relative_offset = true;
414     OS << format("cu + 0x%" PRIx64, uvalue);
415     break;
416
417     // All DW_FORM_indirect attributes should be resolved prior to calling
418     // this function
419   case DW_FORM_indirect:
420     OS << "DW_FORM_indirect";
421     break;
422
423     // Should be formatted to 64-bit for DWARF64.
424   case DW_FORM_sec_offset:
425     OS << format("0x%08x", (uint32_t)uvalue);
426     break;
427
428   default:
429     OS << format("DW_FORM(0x%4.4x)", Form);
430     break;
431   }
432
433   if (cu_relative_offset)
434     OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
435 }
436
437 const char *DWARFFormValue::getAsCString(const DWARFUnit *CU) const {
438   if (isInlinedCStr())
439     return Value.cstr;
440   if (!CU)
441     return NULL;
442   uint32_t Offset = Value.uval;
443   if (Form == DW_FORM_GNU_str_index) {
444     uint32_t StrOffset;
445     if (!CU->getStringOffsetSectionItem(Offset, StrOffset))
446       return NULL;
447     Offset = StrOffset;
448   }
449   return CU->getStringExtractor().getCStr(&Offset);
450 }
451
452 uint64_t DWARFFormValue::getAsAddress(const DWARFUnit *CU) const {
453   if (!CU)
454     return 0;
455   if (Form == DW_FORM_GNU_addr_index) {
456     uint32_t Index = Value.uval;
457     uint64_t Address;
458     if (!CU->getAddrOffsetSectionItem(Index, Address))
459       return 0;
460     return Address;
461   }
462   return Value.uval;
463 }
464
465 uint64_t DWARFFormValue::getReference(const DWARFUnit *cu) const {
466   uint64_t die_offset = Value.uval;
467   switch (Form) {
468   case DW_FORM_ref1:
469   case DW_FORM_ref2:
470   case DW_FORM_ref4:
471   case DW_FORM_ref8:
472   case DW_FORM_ref_udata:
473       die_offset += (cu ? cu->getOffset() : 0);
474       break;
475   default:
476       break;
477   }
478
479   return die_offset;
480 }