c28f0dd5997883a499a310a7cd68ea5e46b42cc0
[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/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/Dwarf.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <cassert>
20 using namespace llvm;
21 using namespace dwarf;
22
23 namespace {
24 template <uint8_t AddrSize, uint8_t RefAddrSize> struct FixedFormSizes {
25   // FIXME: do we need a template here?  Will a stack-allocated struct with
26   // an initializer in getFixedFormSizes() work just fine?
27   static const uint8_t sizes[27];
28 };
29 }
30
31 template <uint8_t AddrSize, uint8_t RefAddrSize>
32 const uint8_t FixedFormSizes<AddrSize, RefAddrSize>::sizes[] = {
33   0,           // 0x00 unused
34   AddrSize,    // 0x01 DW_FORM_addr
35   0,           // 0x02 unused
36   0,           // 0x03 DW_FORM_block2
37   0,           // 0x04 DW_FORM_block4
38   2,           // 0x05 DW_FORM_data2
39   4,           // 0x06 DW_FORM_data4
40   8,           // 0x07 DW_FORM_data8
41   0,           // 0x08 DW_FORM_string
42   0,           // 0x09 DW_FORM_block
43   0,           // 0x0a DW_FORM_block1
44   1,           // 0x0b DW_FORM_data1
45   1,           // 0x0c DW_FORM_flag
46   0,           // 0x0d DW_FORM_sdata
47   4,           // 0x0e DW_FORM_strp
48   0,           // 0x0f DW_FORM_udata
49   RefAddrSize, // 0x10 DW_FORM_ref_addr
50   1,           // 0x11 DW_FORM_ref1
51   2,           // 0x12 DW_FORM_ref2
52   4,           // 0x13 DW_FORM_ref4
53   8,           // 0x14 DW_FORM_ref8
54   0,           // 0x15 DW_FORM_ref_udata
55   0,           // 0x16 DW_FORM_indirect
56   4,           // 0x17 DW_FORM_sec_offset
57   0,           // 0x18 DW_FORM_exprloc
58   0,           // 0x19 DW_FORM_flag_present
59   8,           // 0x20 DW_FORM_ref_sig8
60 };
61
62 static uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
63   // FIXME: Support DWARF64.
64   return (Version == 2) ? AddrSize : 4;
65 }
66
67 ArrayRef<uint8_t> DWARFFormValue::getFixedFormSizes(uint8_t AddrSize,
68                                                     uint16_t Version) {
69   uint8_t RefAddrSize = getRefAddrSize(AddrSize, Version);
70   if (AddrSize == 4 && RefAddrSize == 4)
71     return makeArrayRef(FixedFormSizes<4, 4>::sizes);
72   if (AddrSize == 4 && RefAddrSize == 8)
73     return makeArrayRef(FixedFormSizes<4, 8>::sizes);
74   if (AddrSize == 8 && RefAddrSize == 4)
75     return makeArrayRef(FixedFormSizes<8, 4>::sizes);
76   if (AddrSize == 8 && RefAddrSize == 8)
77     return makeArrayRef(FixedFormSizes<8, 8>::sizes);
78   return None;
79 }
80
81 static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
82   DWARFFormValue::FC_Unknown,       // 0x0
83   DWARFFormValue::FC_Address,       // 0x01 DW_FORM_addr
84   DWARFFormValue::FC_Unknown,       // 0x02 unused
85   DWARFFormValue::FC_Block,         // 0x03 DW_FORM_block2
86   DWARFFormValue::FC_Block,         // 0x04 DW_FORM_block4
87   DWARFFormValue::FC_Constant,      // 0x05 DW_FORM_data2
88   // --- These can be FC_SectionOffset in DWARF3 and below:
89   DWARFFormValue::FC_Constant,      // 0x06 DW_FORM_data4
90   DWARFFormValue::FC_Constant,      // 0x07 DW_FORM_data8
91   // ---
92   DWARFFormValue::FC_String,        // 0x08 DW_FORM_string
93   DWARFFormValue::FC_Block,         // 0x09 DW_FORM_block
94   DWARFFormValue::FC_Block,         // 0x0a DW_FORM_block1
95   DWARFFormValue::FC_Constant,      // 0x0b DW_FORM_data1
96   DWARFFormValue::FC_Flag,          // 0x0c DW_FORM_flag
97   DWARFFormValue::FC_Constant,      // 0x0d DW_FORM_sdata
98   DWARFFormValue::FC_String,        // 0x0e DW_FORM_strp
99   DWARFFormValue::FC_Constant,      // 0x0f DW_FORM_udata
100   DWARFFormValue::FC_Reference,     // 0x10 DW_FORM_ref_addr
101   DWARFFormValue::FC_Reference,     // 0x11 DW_FORM_ref1
102   DWARFFormValue::FC_Reference,     // 0x12 DW_FORM_ref2
103   DWARFFormValue::FC_Reference,     // 0x13 DW_FORM_ref4
104   DWARFFormValue::FC_Reference,     // 0x14 DW_FORM_ref8
105   DWARFFormValue::FC_Reference,     // 0x15 DW_FORM_ref_udata
106   DWARFFormValue::FC_Indirect,      // 0x16 DW_FORM_indirect
107   DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
108   DWARFFormValue::FC_Exprloc,       // 0x18 DW_FORM_exprloc
109   DWARFFormValue::FC_Flag,          // 0x19 DW_FORM_flag_present
110   DWARFFormValue::FC_Reference,     // 0x20 DW_FORM_ref_sig8
111 };
112
113 bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
114   // First, check DWARF4 form classes.
115   if (Form < ArrayRef<FormClass>(DWARF4FormClasses).size() &&
116       DWARF4FormClasses[Form] == FC)
117     return true;
118   // Check for some DWARF5 forms.
119   if (Form == DW_FORM_GNU_addr_index)
120     return (FC == FC_Address);
121   if (Form == DW_FORM_GNU_str_index)
122     return (FC == FC_String);
123   // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
124   // Don't check for DWARF version here, as some producers may still do this
125   // by mistake.
126   if ((Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
127       FC == FC_SectionOffset)
128     return true;
129   return false;
130 }
131
132 bool DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
133                                   const DWARFUnit *cu) {
134   bool indirect = false;
135   bool is_block = false;
136   Value.data = NULL;
137   // Read the value for the form into value and follow and DW_FORM_indirect
138   // instances we run into
139   do {
140     indirect = false;
141     switch (Form) {
142     case DW_FORM_addr:
143     case DW_FORM_ref_addr: {
144       uint16_t AddrSize =
145           (Form == DW_FORM_addr)
146               ? cu->getAddressByteSize()
147               : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
148       RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
149       if (AI != cu->getRelocMap()->end()) {
150         const std::pair<uint8_t, int64_t> &R = AI->second;
151         Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
152       } else
153         Value.uval = data.getUnsigned(offset_ptr, AddrSize);
154       break;
155     }
156     case DW_FORM_exprloc:
157     case DW_FORM_block:
158       Value.uval = data.getULEB128(offset_ptr);
159       is_block = true;
160       break;
161     case DW_FORM_block1:
162       Value.uval = data.getU8(offset_ptr);
163       is_block = true;
164       break;
165     case DW_FORM_block2:
166       Value.uval = data.getU16(offset_ptr);
167       is_block = true;
168       break;
169     case DW_FORM_block4:
170       Value.uval = data.getU32(offset_ptr);
171       is_block = true;
172       break;
173     case DW_FORM_data1:
174     case DW_FORM_ref1:
175     case DW_FORM_flag:
176       Value.uval = data.getU8(offset_ptr);
177       break;
178     case DW_FORM_data2:
179     case DW_FORM_ref2:
180       Value.uval = data.getU16(offset_ptr);
181       break;
182     case DW_FORM_data4:
183     case DW_FORM_ref4: {
184       RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
185       Value.uval = data.getU32(offset_ptr);
186       if (AI != cu->getRelocMap()->end())
187         Value.uval += AI->second.second;
188       break;
189     }
190     case DW_FORM_data8:
191     case DW_FORM_ref8:
192       Value.uval = data.getU64(offset_ptr);
193       break;
194     case DW_FORM_sdata:
195       Value.sval = data.getSLEB128(offset_ptr);
196       break;
197     case DW_FORM_strp: {
198       RelocAddrMap::const_iterator AI
199         = cu->getRelocMap()->find(*offset_ptr);
200       if (AI != cu->getRelocMap()->end()) {
201         const std::pair<uint8_t, int64_t> &R = AI->second;
202         Value.uval = data.getU32(offset_ptr) + R.second;
203       } else
204         Value.uval = data.getU32(offset_ptr);
205       break;
206     }
207     case DW_FORM_udata:
208     case DW_FORM_ref_udata:
209       Value.uval = data.getULEB128(offset_ptr);
210       break;
211     case DW_FORM_string:
212       Value.cstr = data.getCStr(offset_ptr);
213       break;
214     case DW_FORM_indirect:
215       Form = data.getULEB128(offset_ptr);
216       indirect = true;
217       break;
218     case DW_FORM_sec_offset: {
219       // FIXME: This is 64-bit for DWARF64.
220       RelocAddrMap::const_iterator AI
221         = cu->getRelocMap()->find(*offset_ptr);
222       if (AI != cu->getRelocMap()->end()) {
223         const std::pair<uint8_t, int64_t> &R = AI->second;
224         Value.uval = data.getU32(offset_ptr) + R.second;
225       } else
226         Value.uval = data.getU32(offset_ptr);
227       break;
228     }
229     case DW_FORM_flag_present:
230       Value.uval = 1;
231       break;
232     case DW_FORM_ref_sig8:
233       Value.uval = data.getU64(offset_ptr);
234       break;
235     case DW_FORM_GNU_addr_index:
236     case DW_FORM_GNU_str_index:
237       Value.uval = data.getULEB128(offset_ptr);
238       break;
239     default:
240       return false;
241     }
242   } while (indirect);
243
244   if (is_block) {
245     StringRef str = data.getData().substr(*offset_ptr, Value.uval);
246     Value.data = NULL;
247     if (!str.empty()) {
248       Value.data = reinterpret_cast<const uint8_t *>(str.data());
249       *offset_ptr += Value.uval;
250     }
251   }
252
253   return true;
254 }
255
256 bool
257 DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
258                           const DWARFUnit *cu) const {
259   return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
260 }
261
262 bool
263 DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
264                           uint32_t *offset_ptr, const DWARFUnit *cu) {
265   bool indirect = false;
266   do {
267     switch (form) {
268     // Blocks if inlined data that have a length field and the data bytes
269     // inlined in the .debug_info
270     case DW_FORM_exprloc:
271     case DW_FORM_block: {
272       uint64_t size = debug_info_data.getULEB128(offset_ptr);
273       *offset_ptr += size;
274       return true;
275     }
276     case DW_FORM_block1: {
277       uint8_t size = debug_info_data.getU8(offset_ptr);
278       *offset_ptr += size;
279       return true;
280     }
281     case DW_FORM_block2: {
282       uint16_t size = debug_info_data.getU16(offset_ptr);
283       *offset_ptr += size;
284       return true;
285     }
286     case DW_FORM_block4: {
287       uint32_t size = debug_info_data.getU32(offset_ptr);
288       *offset_ptr += size;
289       return true;
290     }
291
292     // Inlined NULL terminated C-strings
293     case DW_FORM_string:
294       debug_info_data.getCStr(offset_ptr);
295       return true;
296
297     // Compile unit address sized values
298     case DW_FORM_addr:
299       *offset_ptr += cu->getAddressByteSize();
300       return true;
301     case DW_FORM_ref_addr:
302       *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
303       return true;
304
305     // 0 byte values - implied from the form.
306     case DW_FORM_flag_present:
307       return true;
308
309     // 1 byte values
310     case DW_FORM_data1:
311     case DW_FORM_flag:
312     case DW_FORM_ref1:
313       *offset_ptr += 1;
314       return true;
315
316     // 2 byte values
317     case DW_FORM_data2:
318     case DW_FORM_ref2:
319       *offset_ptr += 2;
320       return true;
321
322     // 4 byte values
323     case DW_FORM_strp:
324     case DW_FORM_data4:
325     case DW_FORM_ref4:
326       *offset_ptr += 4;
327       return true;
328
329     // 8 byte values
330     case DW_FORM_data8:
331     case DW_FORM_ref8:
332     case DW_FORM_ref_sig8:
333       *offset_ptr += 8;
334       return true;
335
336     // signed or unsigned LEB 128 values
337     //  case DW_FORM_APPLE_db_str:
338     case DW_FORM_sdata:
339     case DW_FORM_udata:
340     case DW_FORM_ref_udata:
341     case DW_FORM_GNU_str_index:
342     case DW_FORM_GNU_addr_index:
343       debug_info_data.getULEB128(offset_ptr);
344       return true;
345
346     case DW_FORM_indirect:
347       indirect = true;
348       form = debug_info_data.getULEB128(offset_ptr);
349       break;
350
351     // FIXME: 4 for DWARF32, 8 for DWARF64.
352     case DW_FORM_sec_offset:
353       *offset_ptr += 4;
354       return true;
355
356     default:
357       return false;
358     }
359   } while (indirect);
360   return true;
361 }
362
363 void
364 DWARFFormValue::dump(raw_ostream &OS, const DWARFUnit *cu) const {
365   DataExtractor debug_str_data(cu->getStringSection(), true, 0);
366   DataExtractor debug_str_offset_data(cu->getStringOffsetSection(), true, 0);
367   uint64_t uvalue = Value.uval;
368   bool cu_relative_offset = false;
369
370   switch (Form) {
371   case DW_FORM_addr:      OS << format("0x%016" PRIx64, uvalue); break;
372   case DW_FORM_GNU_addr_index: {
373     OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
374     uint64_t Address;
375     if (cu->getAddrOffsetSectionItem(uvalue, Address))
376       OS << format("0x%016" PRIx64, Address);
377     else
378       OS << "<no .debug_addr section>";
379     break;
380   }
381   case DW_FORM_flag_present: OS << "true"; break;
382   case DW_FORM_flag:
383   case DW_FORM_data1:     OS << format("0x%02x", (uint8_t)uvalue); break;
384   case DW_FORM_data2:     OS << format("0x%04x", (uint16_t)uvalue); break;
385   case DW_FORM_data4:     OS << format("0x%08x", (uint32_t)uvalue); break;
386   case DW_FORM_ref_sig8:
387   case DW_FORM_data8:     OS << format("0x%016" PRIx64, uvalue); break;
388   case DW_FORM_string:
389     OS << '"';
390     OS.write_escaped(Value.cstr);
391     OS << '"';
392     break;
393   case DW_FORM_exprloc:
394   case DW_FORM_block:
395   case DW_FORM_block1:
396   case DW_FORM_block2:
397   case DW_FORM_block4:
398     if (uvalue > 0) {
399       switch (Form) {
400       case DW_FORM_exprloc:
401       case DW_FORM_block:  OS << format("<0x%" PRIx64 "> ", uvalue);     break;
402       case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue);  break;
403       case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
404       case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
405       default: break;
406       }
407
408       const uint8_t* data_ptr = Value.data;
409       if (data_ptr) {
410         // uvalue contains size of block
411         const uint8_t* end_data_ptr = data_ptr + uvalue;
412         while (data_ptr < end_data_ptr) {
413           OS << format("%2.2x ", *data_ptr);
414           ++data_ptr;
415         }
416       }
417       else
418         OS << "NULL";
419     }
420     break;
421
422   case DW_FORM_sdata:     OS << Value.sval; break;
423   case DW_FORM_udata:     OS << Value.uval; break;
424   case DW_FORM_strp: {
425     OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
426     Optional<const char *> DbgStr = getAsCString(cu);
427     if (DbgStr.hasValue()) {
428       OS << '"';
429       OS.write_escaped(DbgStr.getValue());
430       OS << '"';
431     }
432     break;
433   }
434   case DW_FORM_GNU_str_index: {
435     OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
436     Optional<const char *> DbgStr = getAsCString(cu);
437     if (DbgStr.hasValue()) {
438       OS << '"';
439       OS.write_escaped(DbgStr.getValue());
440       OS << '"';
441     }
442     break;
443   }
444   case DW_FORM_ref_addr:
445     OS << format("0x%016" PRIx64, uvalue);
446     break;
447   case DW_FORM_ref1:
448     cu_relative_offset = true;
449     OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
450     break;
451   case DW_FORM_ref2:
452     cu_relative_offset = true;
453     OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
454     break;
455   case DW_FORM_ref4:
456     cu_relative_offset = true;
457     OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
458     break;
459   case DW_FORM_ref8:
460     cu_relative_offset = true;
461     OS << format("cu + 0x%8.8" PRIx64, uvalue);
462     break;
463   case DW_FORM_ref_udata:
464     cu_relative_offset = true;
465     OS << format("cu + 0x%" PRIx64, uvalue);
466     break;
467
468     // All DW_FORM_indirect attributes should be resolved prior to calling
469     // this function
470   case DW_FORM_indirect:
471     OS << "DW_FORM_indirect";
472     break;
473
474     // Should be formatted to 64-bit for DWARF64.
475   case DW_FORM_sec_offset:
476     OS << format("0x%08x", (uint32_t)uvalue);
477     break;
478
479   default:
480     OS << format("DW_FORM(0x%4.4x)", Form);
481     break;
482   }
483
484   if (cu_relative_offset)
485     OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
486 }
487
488 Optional<const char *> DWARFFormValue::getAsCString(const DWARFUnit *U) const {
489   if (!isFormClass(FC_String))
490     return None;
491   if (Form == DW_FORM_string)
492     return Value.cstr;
493   if (U == 0)
494     return None;
495   uint32_t Offset = Value.uval;
496   if (Form == DW_FORM_GNU_str_index) {
497     uint32_t StrOffset;
498     if (!U->getStringOffsetSectionItem(Offset, StrOffset))
499       return None;
500     Offset = StrOffset;
501   }
502   if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
503     return Str;
504   }
505   return None;
506 }
507
508 Optional<uint64_t> DWARFFormValue::getAsAddress(const DWARFUnit *U) const {
509   if (!isFormClass(FC_Address))
510     return None;
511   if (Form == DW_FORM_GNU_addr_index) {
512     uint32_t Index = Value.uval;
513     uint64_t Result;
514     if (U == 0 || !U->getAddrOffsetSectionItem(Index, Result))
515       return None;
516     return Result;
517   }
518   return Value.uval;
519 }
520
521 Optional<uint64_t> DWARFFormValue::getAsReference(const DWARFUnit *U) const {
522   if (!isFormClass(FC_Reference))
523     return None;
524   switch (Form) {
525   case DW_FORM_ref1:
526   case DW_FORM_ref2:
527   case DW_FORM_ref4:
528   case DW_FORM_ref8:
529   case DW_FORM_ref_udata:
530     if (U == 0)
531       return None;
532     return Value.uval + U->getOffset();
533   case DW_FORM_ref_addr:
534     return Value.uval;
535   // FIXME: Add proper support for DW_FORM_ref_sig8
536   default:
537     return Value.uval;
538   }
539 }
540
541 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
542   if (!isFormClass(FC_SectionOffset))
543     return None;
544   return Value.uval;
545 }
546
547 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
548   if (!isFormClass(FC_Constant) || Form == DW_FORM_sdata)
549     return None;
550   return Value.uval;
551 }