1 //===-- DWARFFormValue.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <cassert>
10 #include <optional>
11 
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/dwarf.h"
14 #include "lldb/Symbol/ObjectFile.h"
15 #include "lldb/Utility/Stream.h"
16 
17 #include "DWARFDebugInfo.h"
18 #include "DWARFFormValue.h"
19 #include "DWARFUnit.h"
20 
21 class DWARFUnit;
22 
23 using namespace lldb_private;
24 using namespace lldb_private::dwarf;
25 
26 void DWARFFormValue::Clear() {
27   m_unit = nullptr;
28   m_form = 0;
29   m_value = ValueTypeTag();
30 }
31 
32 bool DWARFFormValue::ExtractValue(const DWARFDataExtractor &data,
33                                   lldb::offset_t *offset_ptr) {
34   if (m_form == DW_FORM_implicit_const)
35     return true;
36 
37   bool indirect = false;
38   bool is_block = false;
39   m_value.data = nullptr;
40   uint8_t ref_addr_size;
41   // Read the value for the form into value and follow and DW_FORM_indirect
42   // instances we run into
43   do {
44     indirect = false;
45     switch (m_form) {
46     case DW_FORM_addr:
47       assert(m_unit);
48       m_value.value.uval =
49           data.GetMaxU64(offset_ptr, DWARFUnit::GetAddressByteSize(m_unit));
50       break;
51     case DW_FORM_block1:
52       m_value.value.uval = data.GetU8(offset_ptr);
53       is_block = true;
54       break;
55     case DW_FORM_block2:
56       m_value.value.uval = data.GetU16(offset_ptr);
57       is_block = true;
58       break;
59     case DW_FORM_block4:
60       m_value.value.uval = data.GetU32(offset_ptr);
61       is_block = true;
62       break;
63     case DW_FORM_data16:
64       m_value.value.uval = 16;
65       is_block = true;
66       break;
67     case DW_FORM_exprloc:
68     case DW_FORM_block:
69       m_value.value.uval = data.GetULEB128(offset_ptr);
70       is_block = true;
71       break;
72     case DW_FORM_string:
73       m_value.value.cstr = data.GetCStr(offset_ptr);
74       break;
75     case DW_FORM_sdata:
76       m_value.value.sval = data.GetSLEB128(offset_ptr);
77       break;
78     case DW_FORM_strp:
79     case DW_FORM_line_strp:
80     case DW_FORM_sec_offset:
81       m_value.value.uval = data.GetMaxU64(offset_ptr, 4);
82       break;
83     case DW_FORM_addrx1:
84     case DW_FORM_strx1:
85     case DW_FORM_ref1:
86     case DW_FORM_data1:
87     case DW_FORM_flag:
88       m_value.value.uval = data.GetU8(offset_ptr);
89       break;
90     case DW_FORM_addrx2:
91     case DW_FORM_strx2:
92     case DW_FORM_ref2:
93     case DW_FORM_data2:
94       m_value.value.uval = data.GetU16(offset_ptr);
95       break;
96     case DW_FORM_addrx3:
97     case DW_FORM_strx3:
98       m_value.value.uval = data.GetMaxU64(offset_ptr, 3);
99       break;
100     case DW_FORM_addrx4:
101     case DW_FORM_strx4:
102     case DW_FORM_ref4:
103     case DW_FORM_data4:
104       m_value.value.uval = data.GetU32(offset_ptr);
105       break;
106     case DW_FORM_data8:
107     case DW_FORM_ref8:
108     case DW_FORM_ref_sig8:
109       m_value.value.uval = data.GetU64(offset_ptr);
110       break;
111     case DW_FORM_addrx:
112     case DW_FORM_loclistx:
113     case DW_FORM_rnglistx:
114     case DW_FORM_strx:
115     case DW_FORM_udata:
116     case DW_FORM_ref_udata:
117     case DW_FORM_GNU_str_index:
118     case DW_FORM_GNU_addr_index:
119       m_value.value.uval = data.GetULEB128(offset_ptr);
120       break;
121     case DW_FORM_ref_addr:
122       assert(m_unit);
123       if (m_unit->GetVersion() <= 2)
124         ref_addr_size = m_unit->GetAddressByteSize();
125       else
126         ref_addr_size = 4;
127       m_value.value.uval = data.GetMaxU64(offset_ptr, ref_addr_size);
128       break;
129     case DW_FORM_indirect:
130       m_form = data.GetULEB128(offset_ptr);
131       indirect = true;
132       break;
133     case DW_FORM_flag_present:
134       m_value.value.uval = 1;
135       break;
136     default:
137       return false;
138     }
139   } while (indirect);
140 
141   if (is_block) {
142     m_value.data = data.PeekData(*offset_ptr, m_value.value.uval);
143     if (m_value.data != nullptr) {
144       *offset_ptr += m_value.value.uval;
145     }
146   }
147 
148   return true;
149 }
150 
151 struct FormSize {
152   uint8_t valid:1, size:7;
153 };
154 static FormSize g_form_sizes[] = {
155     {0, 0}, // 0x00 unused
156     {0, 0}, // 0x01 DW_FORM_addr
157     {0, 0}, // 0x02 unused
158     {0, 0}, // 0x03 DW_FORM_block2
159     {0, 0}, // 0x04 DW_FORM_block4
160     {1, 2}, // 0x05 DW_FORM_data2
161     {1, 4}, // 0x06 DW_FORM_data4
162     {1, 8}, // 0x07 DW_FORM_data8
163     {0, 0}, // 0x08 DW_FORM_string
164     {0, 0}, // 0x09 DW_FORM_block
165     {0, 0}, // 0x0a DW_FORM_block1
166     {1, 1}, // 0x0b DW_FORM_data1
167     {1, 1}, // 0x0c DW_FORM_flag
168     {0, 0}, // 0x0d DW_FORM_sdata
169     {1, 4}, // 0x0e DW_FORM_strp
170     {0, 0}, // 0x0f DW_FORM_udata
171     {0, 0}, // 0x10 DW_FORM_ref_addr (addr size for DWARF2 and earlier, 4 bytes
172             // for DWARF32, 8 bytes for DWARF32 in DWARF 3 and later
173     {1, 1},  // 0x11 DW_FORM_ref1
174     {1, 2},  // 0x12 DW_FORM_ref2
175     {1, 4},  // 0x13 DW_FORM_ref4
176     {1, 8},  // 0x14 DW_FORM_ref8
177     {0, 0},  // 0x15 DW_FORM_ref_udata
178     {0, 0},  // 0x16 DW_FORM_indirect
179     {1, 4},  // 0x17 DW_FORM_sec_offset
180     {0, 0},  // 0x18 DW_FORM_exprloc
181     {1, 0},  // 0x19 DW_FORM_flag_present
182     {0, 0},  // 0x1a DW_FORM_strx (ULEB128)
183     {0, 0},  // 0x1b DW_FORM_addrx (ULEB128)
184     {1, 4},  // 0x1c DW_FORM_ref_sup4
185     {0, 0},  // 0x1d DW_FORM_strp_sup (4 bytes for DWARF32, 8 bytes for DWARF64)
186     {1, 16}, // 0x1e DW_FORM_data16
187     {1, 4},  // 0x1f DW_FORM_line_strp
188     {1, 8},  // 0x20 DW_FORM_ref_sig8
189 };
190 
191 std::optional<uint8_t> DWARFFormValue::GetFixedSize(dw_form_t form,
192                                                     const DWARFUnit *u) {
193   if (form <= DW_FORM_ref_sig8 && g_form_sizes[form].valid)
194     return static_cast<uint8_t>(g_form_sizes[form].size);
195   if (form == DW_FORM_addr && u)
196     return u->GetAddressByteSize();
197   return std::nullopt;
198 }
199 
200 std::optional<uint8_t> DWARFFormValue::GetFixedSize() const {
201   return GetFixedSize(m_form, m_unit);
202 }
203 
204 bool DWARFFormValue::SkipValue(const DWARFDataExtractor &debug_info_data,
205                                lldb::offset_t *offset_ptr) const {
206   return DWARFFormValue::SkipValue(m_form, debug_info_data, offset_ptr, m_unit);
207 }
208 
209 bool DWARFFormValue::SkipValue(dw_form_t form,
210                                const DWARFDataExtractor &debug_info_data,
211                                lldb::offset_t *offset_ptr,
212                                const DWARFUnit *unit) {
213   uint8_t ref_addr_size;
214   switch (form) {
215   // Blocks if inlined data that have a length field and the data bytes inlined
216   // in the .debug_info
217   case DW_FORM_exprloc:
218   case DW_FORM_block: {
219     dw_uleb128_t size = debug_info_data.GetULEB128(offset_ptr);
220     *offset_ptr += size;
221   }
222     return true;
223   case DW_FORM_block1: {
224     dw_uleb128_t size = debug_info_data.GetU8(offset_ptr);
225     *offset_ptr += size;
226   }
227     return true;
228   case DW_FORM_block2: {
229     dw_uleb128_t size = debug_info_data.GetU16(offset_ptr);
230     *offset_ptr += size;
231   }
232     return true;
233   case DW_FORM_block4: {
234     dw_uleb128_t size = debug_info_data.GetU32(offset_ptr);
235     *offset_ptr += size;
236   }
237     return true;
238 
239   // Inlined NULL terminated C-strings
240   case DW_FORM_string:
241     debug_info_data.GetCStr(offset_ptr);
242     return true;
243 
244   // Compile unit address sized values
245   case DW_FORM_addr:
246     *offset_ptr += DWARFUnit::GetAddressByteSize(unit);
247     return true;
248 
249   case DW_FORM_ref_addr:
250     ref_addr_size = 4;
251     assert(unit); // Unit must be valid for DW_FORM_ref_addr objects or we will
252                   // get this wrong
253     if (unit->GetVersion() <= 2)
254       ref_addr_size = unit->GetAddressByteSize();
255     else
256       ref_addr_size = 4;
257     *offset_ptr += ref_addr_size;
258     return true;
259 
260   // 0 bytes values (implied from DW_FORM)
261   case DW_FORM_flag_present:
262   case DW_FORM_implicit_const:
263     return true;
264 
265     // 1 byte values
266     case DW_FORM_addrx1:
267     case DW_FORM_data1:
268     case DW_FORM_flag:
269     case DW_FORM_ref1:
270     case DW_FORM_strx1:
271       *offset_ptr += 1;
272       return true;
273 
274     // 2 byte values
275     case DW_FORM_addrx2:
276     case DW_FORM_data2:
277     case DW_FORM_ref2:
278     case DW_FORM_strx2:
279       *offset_ptr += 2;
280       return true;
281 
282     // 3 byte values
283     case DW_FORM_addrx3:
284     case DW_FORM_strx3:
285       *offset_ptr += 3;
286       return true;
287 
288     // 32 bit for DWARF 32, 64 for DWARF 64
289     case DW_FORM_sec_offset:
290     case DW_FORM_strp:
291     case DW_FORM_line_strp:
292       *offset_ptr += 4;
293       return true;
294 
295     // 4 byte values
296     case DW_FORM_addrx4:
297     case DW_FORM_data4:
298     case DW_FORM_ref4:
299     case DW_FORM_strx4:
300       *offset_ptr += 4;
301       return true;
302 
303     // 8 byte values
304     case DW_FORM_data8:
305     case DW_FORM_ref8:
306     case DW_FORM_ref_sig8:
307       *offset_ptr += 8;
308       return true;
309 
310     // signed or unsigned LEB 128 values
311     case DW_FORM_addrx:
312     case DW_FORM_loclistx:
313     case DW_FORM_rnglistx:
314     case DW_FORM_sdata:
315     case DW_FORM_udata:
316     case DW_FORM_ref_udata:
317     case DW_FORM_GNU_addr_index:
318     case DW_FORM_GNU_str_index:
319     case DW_FORM_strx:
320       debug_info_data.Skip_LEB128(offset_ptr);
321       return true;
322 
323   case DW_FORM_indirect: {
324     dw_form_t indirect_form = debug_info_data.GetULEB128(offset_ptr);
325     return DWARFFormValue::SkipValue(indirect_form, debug_info_data, offset_ptr,
326                                      unit);
327   }
328 
329   default:
330     break;
331   }
332   return false;
333 }
334 
335 void DWARFFormValue::Dump(Stream &s) const {
336   uint64_t uvalue = Unsigned();
337   bool unit_relative_offset = false;
338 
339   switch (m_form) {
340   case DW_FORM_addr:
341     DumpAddress(s.AsRawOstream(), uvalue, sizeof(uint64_t));
342     break;
343   case DW_FORM_flag:
344   case DW_FORM_data1:
345     s.PutHex8(uvalue);
346     break;
347   case DW_FORM_data2:
348     s.PutHex16(uvalue);
349     break;
350   case DW_FORM_sec_offset:
351   case DW_FORM_data4:
352     s.PutHex32(uvalue);
353     break;
354   case DW_FORM_ref_sig8:
355   case DW_FORM_data8:
356     s.PutHex64(uvalue);
357     break;
358   case DW_FORM_string:
359     s.QuotedCString(AsCString());
360     break;
361   case DW_FORM_exprloc:
362   case DW_FORM_block:
363   case DW_FORM_block1:
364   case DW_FORM_block2:
365   case DW_FORM_block4:
366     if (uvalue > 0) {
367       switch (m_form) {
368       case DW_FORM_exprloc:
369       case DW_FORM_block:
370         s.Printf("<0x%" PRIx64 "> ", uvalue);
371         break;
372       case DW_FORM_block1:
373         s.Printf("<0x%2.2x> ", (uint8_t)uvalue);
374         break;
375       case DW_FORM_block2:
376         s.Printf("<0x%4.4x> ", (uint16_t)uvalue);
377         break;
378       case DW_FORM_block4:
379         s.Printf("<0x%8.8x> ", (uint32_t)uvalue);
380         break;
381       default:
382         break;
383       }
384 
385       const uint8_t *data_ptr = m_value.data;
386       if (data_ptr) {
387         const uint8_t *end_data_ptr =
388             data_ptr + uvalue; // uvalue contains size of block
389         while (data_ptr < end_data_ptr) {
390           s.Printf("%2.2x ", *data_ptr);
391           ++data_ptr;
392         }
393       } else
394         s.PutCString("NULL");
395     }
396     break;
397 
398   case DW_FORM_sdata:
399     s.PutSLEB128(uvalue);
400     break;
401   case DW_FORM_udata:
402     s.PutULEB128(uvalue);
403     break;
404   case DW_FORM_strp:
405   case DW_FORM_line_strp: {
406     const char *dbg_str = AsCString();
407     if (dbg_str) {
408       s.QuotedCString(dbg_str);
409     } else {
410       s.PutHex32(uvalue);
411     }
412   } break;
413 
414   case DW_FORM_ref_addr: {
415     assert(m_unit); // Unit must be valid for DW_FORM_ref_addr objects or we
416                     // will get this wrong
417     if (m_unit->GetVersion() <= 2)
418       DumpAddress(s.AsRawOstream(), uvalue, sizeof(uint64_t) * 2);
419     else
420       DumpAddress(s.AsRawOstream(), uvalue,
421                   4 * 2); // 4 for DWARF32, 8 for DWARF64, but we don't
422                           // support DWARF64 yet
423     break;
424   }
425   case DW_FORM_ref1:
426     unit_relative_offset = true;
427     break;
428   case DW_FORM_ref2:
429     unit_relative_offset = true;
430     break;
431   case DW_FORM_ref4:
432     unit_relative_offset = true;
433     break;
434   case DW_FORM_ref8:
435     unit_relative_offset = true;
436     break;
437   case DW_FORM_ref_udata:
438     unit_relative_offset = true;
439     break;
440 
441   // All DW_FORM_indirect attributes should be resolved prior to calling this
442   // function
443   case DW_FORM_indirect:
444     s.PutCString("DW_FORM_indirect");
445     break;
446   case DW_FORM_flag_present:
447     break;
448   default:
449     s.Printf("DW_FORM(0x%4.4x)", m_form);
450     break;
451   }
452 
453   if (unit_relative_offset) {
454     assert(m_unit); // Unit must be valid for DW_FORM_ref forms that are compile
455                     // unit relative or we will get this wrong
456     s.Printf("{0x%8.8" PRIx64 "}", uvalue + m_unit->GetOffset());
457   }
458 }
459 
460 const char *DWARFFormValue::AsCString() const {
461   DWARFContext &context = m_unit->GetSymbolFileDWARF().GetDWARFContext();
462 
463   if (m_form == DW_FORM_string)
464     return m_value.value.cstr;
465   if (m_form == DW_FORM_strp)
466     return context.getOrLoadStrData().PeekCStr(m_value.value.uval);
467 
468   if (m_form == DW_FORM_GNU_str_index || m_form == DW_FORM_strx ||
469       m_form == DW_FORM_strx1 || m_form == DW_FORM_strx2 ||
470       m_form == DW_FORM_strx3 || m_form == DW_FORM_strx4) {
471 
472     std::optional<uint64_t> offset =
473         m_unit->GetStringOffsetSectionItem(m_value.value.uval);
474     if (!offset)
475       return nullptr;
476     return context.getOrLoadStrData().PeekCStr(*offset);
477   }
478 
479   if (m_form == DW_FORM_line_strp)
480     return context.getOrLoadLineStrData().PeekCStr(m_value.value.uval);
481 
482   return nullptr;
483 }
484 
485 dw_addr_t DWARFFormValue::Address() const {
486   SymbolFileDWARF &symbol_file = m_unit->GetSymbolFileDWARF();
487 
488   if (m_form == DW_FORM_addr)
489     return Unsigned();
490 
491   assert(m_unit);
492   assert(m_form == DW_FORM_GNU_addr_index || m_form == DW_FORM_addrx ||
493          m_form == DW_FORM_addrx1 || m_form == DW_FORM_addrx2 ||
494          m_form == DW_FORM_addrx3 || m_form == DW_FORM_addrx4);
495 
496   uint32_t index_size = m_unit->GetAddressByteSize();
497   dw_offset_t addr_base = m_unit->GetAddrBase();
498   lldb::offset_t offset = addr_base + m_value.value.uval * index_size;
499   return symbol_file.GetDWARFContext().getOrLoadAddrData().GetMaxU64(
500       &offset, index_size);
501 }
502 
503 DWARFDIE DWARFFormValue::Reference() const {
504   uint64_t value = m_value.value.uval;
505   switch (m_form) {
506   case DW_FORM_ref1:
507   case DW_FORM_ref2:
508   case DW_FORM_ref4:
509   case DW_FORM_ref8:
510   case DW_FORM_ref_udata:
511     assert(m_unit); // Unit must be valid for DW_FORM_ref forms that are compile
512                     // unit relative or we will get this wrong
513     value += m_unit->GetOffset();
514     if (!m_unit->ContainsDIEOffset(value)) {
515       m_unit->GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
516           "DW_FORM_ref* DIE reference {0:x16} is outside of its CU", value);
517       return {};
518     }
519     return const_cast<DWARFUnit *>(m_unit)->GetDIE(value);
520 
521   case DW_FORM_ref_addr: {
522     DWARFUnit *ref_cu =
523         m_unit->GetSymbolFileDWARF().DebugInfo().GetUnitContainingDIEOffset(
524             DIERef::Section::DebugInfo, value);
525     if (!ref_cu) {
526       m_unit->GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
527           "DW_FORM_ref_addr DIE reference {0:x16} has no matching CU", value);
528       return {};
529     }
530     return ref_cu->GetDIE(value);
531   }
532 
533   case DW_FORM_ref_sig8: {
534     DWARFTypeUnit *tu =
535         m_unit->GetSymbolFileDWARF().DebugInfo().GetTypeUnitForHash(value);
536     if (!tu)
537       return {};
538     return tu->GetDIE(tu->GetTypeOffset());
539   }
540 
541   default:
542     return {};
543   }
544 }
545 
546 uint64_t DWARFFormValue::Reference(dw_offset_t base_offset) const {
547   uint64_t value = m_value.value.uval;
548   switch (m_form) {
549   case DW_FORM_ref1:
550   case DW_FORM_ref2:
551   case DW_FORM_ref4:
552   case DW_FORM_ref8:
553   case DW_FORM_ref_udata:
554     return value + base_offset;
555 
556   case DW_FORM_ref_addr:
557   case DW_FORM_ref_sig8:
558   case DW_FORM_GNU_ref_alt:
559     return value;
560 
561   default:
562     return DW_INVALID_OFFSET;
563   }
564 }
565 
566 const uint8_t *DWARFFormValue::BlockData() const { return m_value.data; }
567 
568 bool DWARFFormValue::IsBlockForm(const dw_form_t form) {
569   switch (form) {
570   case DW_FORM_exprloc:
571   case DW_FORM_block:
572   case DW_FORM_block1:
573   case DW_FORM_block2:
574   case DW_FORM_block4:
575     return true;
576   }
577   return false;
578 }
579 
580 bool DWARFFormValue::IsDataForm(const dw_form_t form) {
581   switch (form) {
582   case DW_FORM_sdata:
583   case DW_FORM_udata:
584   case DW_FORM_data1:
585   case DW_FORM_data2:
586   case DW_FORM_data4:
587   case DW_FORM_data8:
588     return true;
589   }
590   return false;
591 }
592 
593 bool DWARFFormValue::FormIsSupported(dw_form_t form) {
594   switch (form) {
595     case DW_FORM_addr:
596     case DW_FORM_addrx:
597     case DW_FORM_loclistx:
598     case DW_FORM_rnglistx:
599     case DW_FORM_block2:
600     case DW_FORM_block4:
601     case DW_FORM_data2:
602     case DW_FORM_data4:
603     case DW_FORM_data8:
604     case DW_FORM_string:
605     case DW_FORM_block:
606     case DW_FORM_block1:
607     case DW_FORM_data1:
608     case DW_FORM_flag:
609     case DW_FORM_sdata:
610     case DW_FORM_strp:
611     case DW_FORM_line_strp:
612     case DW_FORM_strx:
613     case DW_FORM_strx1:
614     case DW_FORM_strx2:
615     case DW_FORM_strx3:
616     case DW_FORM_strx4:
617     case DW_FORM_udata:
618     case DW_FORM_ref_addr:
619     case DW_FORM_ref1:
620     case DW_FORM_ref2:
621     case DW_FORM_ref4:
622     case DW_FORM_ref8:
623     case DW_FORM_ref_udata:
624     case DW_FORM_indirect:
625     case DW_FORM_sec_offset:
626     case DW_FORM_exprloc:
627     case DW_FORM_flag_present:
628     case DW_FORM_ref_sig8:
629     case DW_FORM_GNU_str_index:
630     case DW_FORM_GNU_addr_index:
631     case DW_FORM_implicit_const:
632       return true;
633     default:
634       break;
635   }
636   return false;
637 }
638