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