xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 = dw_form_t(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 = static_cast<dw_form_t>(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     uint64_t size = debug_info_data.GetULEB128(offset_ptr);
220     *offset_ptr += size;
221   }
222     return true;
223   case DW_FORM_block1: {
224     uint8_t size = debug_info_data.GetU8(offset_ptr);
225     *offset_ptr += size;
226   }
227     return true;
228   case DW_FORM_block2: {
229     uint16_t size = debug_info_data.GetU16(offset_ptr);
230     *offset_ptr += size;
231   }
232     return true;
233   case DW_FORM_block4: {
234     uint32_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       auto indirect_form =
325           static_cast<dw_form_t>(debug_info_data.GetULEB128(offset_ptr));
326       return DWARFFormValue::SkipValue(indirect_form, debug_info_data,
327                                        offset_ptr, unit);
328   }
329 
330   default:
331     break;
332   }
333   return false;
334 }
335 
336 void DWARFFormValue::Dump(Stream &s) const {
337   uint64_t uvalue = Unsigned();
338   bool unit_relative_offset = false;
339 
340   switch (m_form) {
341   case DW_FORM_addr:
342     DumpAddress(s.AsRawOstream(), uvalue, sizeof(uint64_t));
343     break;
344   case DW_FORM_flag:
345   case DW_FORM_data1:
346     s.PutHex8(uvalue);
347     break;
348   case DW_FORM_data2:
349     s.PutHex16(uvalue);
350     break;
351   case DW_FORM_sec_offset:
352   case DW_FORM_data4:
353     s.PutHex32(uvalue);
354     break;
355   case DW_FORM_ref_sig8:
356   case DW_FORM_data8:
357     s.PutHex64(uvalue);
358     break;
359   case DW_FORM_string:
360     s.QuotedCString(AsCString());
361     break;
362   case DW_FORM_exprloc:
363   case DW_FORM_block:
364   case DW_FORM_block1:
365   case DW_FORM_block2:
366   case DW_FORM_block4:
367     if (uvalue > 0) {
368       switch (m_form) {
369       case DW_FORM_exprloc:
370       case DW_FORM_block:
371         s.Printf("<0x%" PRIx64 "> ", uvalue);
372         break;
373       case DW_FORM_block1:
374         s.Printf("<0x%2.2x> ", (uint8_t)uvalue);
375         break;
376       case DW_FORM_block2:
377         s.Printf("<0x%4.4x> ", (uint16_t)uvalue);
378         break;
379       case DW_FORM_block4:
380         s.Printf("<0x%8.8x> ", (uint32_t)uvalue);
381         break;
382       default:
383         break;
384       }
385 
386       const uint8_t *data_ptr = m_value.data;
387       if (data_ptr) {
388         const uint8_t *end_data_ptr =
389             data_ptr + uvalue; // uvalue contains size of block
390         while (data_ptr < end_data_ptr) {
391           s.Printf("%2.2x ", *data_ptr);
392           ++data_ptr;
393         }
394       } else
395         s.PutCString("NULL");
396     }
397     break;
398 
399   case DW_FORM_sdata:
400     s.PutSLEB128(uvalue);
401     break;
402   case DW_FORM_udata:
403     s.PutULEB128(uvalue);
404     break;
405   case DW_FORM_strp:
406   case DW_FORM_line_strp: {
407     const char *dbg_str = AsCString();
408     if (dbg_str) {
409       s.QuotedCString(dbg_str);
410     } else {
411       s.PutHex32(uvalue);
412     }
413   } break;
414 
415   case DW_FORM_ref_addr: {
416     assert(m_unit); // Unit must be valid for DW_FORM_ref_addr objects or we
417                     // will get this wrong
418     if (m_unit->GetVersion() <= 2)
419       DumpAddress(s.AsRawOstream(), uvalue, sizeof(uint64_t) * 2);
420     else
421       DumpAddress(s.AsRawOstream(), uvalue,
422                   4 * 2); // 4 for DWARF32, 8 for DWARF64, but we don't
423                           // support DWARF64 yet
424     break;
425   }
426   case DW_FORM_ref1:
427     unit_relative_offset = true;
428     break;
429   case DW_FORM_ref2:
430     unit_relative_offset = true;
431     break;
432   case DW_FORM_ref4:
433     unit_relative_offset = true;
434     break;
435   case DW_FORM_ref8:
436     unit_relative_offset = true;
437     break;
438   case DW_FORM_ref_udata:
439     unit_relative_offset = true;
440     break;
441 
442   // All DW_FORM_indirect attributes should be resolved prior to calling this
443   // function
444   case DW_FORM_indirect:
445     s.PutCString("DW_FORM_indirect");
446     break;
447   case DW_FORM_flag_present:
448     break;
449   default:
450     s.Printf("DW_FORM(0x%4.4x)", m_form);
451     break;
452   }
453 
454   if (unit_relative_offset) {
455     assert(m_unit); // Unit must be valid for DW_FORM_ref forms that are compile
456                     // unit relative or we will get this wrong
457     s.Printf("{0x%8.8" PRIx64 "}", uvalue + m_unit->GetOffset());
458   }
459 }
460 
461 const char *DWARFFormValue::AsCString() const {
462   DWARFContext &context = m_unit->GetSymbolFileDWARF().GetDWARFContext();
463 
464   if (m_form == DW_FORM_string)
465     return m_value.value.cstr;
466   if (m_form == DW_FORM_strp)
467     return context.getOrLoadStrData().PeekCStr(m_value.value.uval);
468 
469   if (m_form == DW_FORM_GNU_str_index || m_form == DW_FORM_strx ||
470       m_form == DW_FORM_strx1 || m_form == DW_FORM_strx2 ||
471       m_form == DW_FORM_strx3 || m_form == DW_FORM_strx4) {
472 
473     std::optional<uint64_t> offset =
474         m_unit->GetStringOffsetSectionItem(m_value.value.uval);
475     if (!offset)
476       return nullptr;
477     return context.getOrLoadStrData().PeekCStr(*offset);
478   }
479 
480   if (m_form == DW_FORM_line_strp)
481     return context.getOrLoadLineStrData().PeekCStr(m_value.value.uval);
482 
483   return nullptr;
484 }
485 
486 dw_addr_t DWARFFormValue::Address() const {
487   SymbolFileDWARF &symbol_file = m_unit->GetSymbolFileDWARF();
488 
489   if (m_form == DW_FORM_addr)
490     return Unsigned();
491 
492   assert(m_unit);
493   assert(m_form == DW_FORM_GNU_addr_index || m_form == DW_FORM_addrx ||
494          m_form == DW_FORM_addrx1 || m_form == DW_FORM_addrx2 ||
495          m_form == DW_FORM_addrx3 || m_form == DW_FORM_addrx4);
496 
497   uint32_t index_size = m_unit->GetAddressByteSize();
498   dw_offset_t addr_base = m_unit->GetAddrBase();
499   lldb::offset_t offset = addr_base + m_value.value.uval * index_size;
500   return symbol_file.GetDWARFContext().getOrLoadAddrData().GetMaxU64(
501       &offset, index_size);
502 }
503 
504 DWARFDIE DWARFFormValue::Reference() const {
505   uint64_t value = m_value.value.uval;
506   switch (m_form) {
507   case DW_FORM_ref1:
508   case DW_FORM_ref2:
509   case DW_FORM_ref4:
510   case DW_FORM_ref8:
511   case DW_FORM_ref_udata:
512     assert(m_unit); // Unit must be valid for DW_FORM_ref forms that are compile
513                     // unit relative or we will get this wrong
514     value += m_unit->GetOffset();
515     if (!m_unit->ContainsDIEOffset(value)) {
516       m_unit->GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
517           "DW_FORM_ref* DIE reference {0:x16} is outside of its CU", value);
518       return {};
519     }
520     return const_cast<DWARFUnit *>(m_unit)->GetDIE(value);
521 
522   case DW_FORM_ref_addr: {
523     DWARFUnit *ref_cu =
524         m_unit->GetSymbolFileDWARF().DebugInfo().GetUnitContainingDIEOffset(
525             DIERef::Section::DebugInfo, value);
526     if (!ref_cu) {
527       m_unit->GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
528           "DW_FORM_ref_addr DIE reference {0:x16} has no matching CU", value);
529       return {};
530     }
531     return ref_cu->GetDIE(value);
532   }
533 
534   case DW_FORM_ref_sig8: {
535     DWARFTypeUnit *tu =
536         m_unit->GetSymbolFileDWARF().DebugInfo().GetTypeUnitForHash(value);
537     if (!tu)
538       return {};
539     return tu->GetDIE(tu->GetTypeOffset());
540   }
541 
542   default:
543     return {};
544   }
545 }
546 
547 uint64_t DWARFFormValue::Reference(dw_offset_t base_offset) const {
548   uint64_t value = m_value.value.uval;
549   switch (m_form) {
550   case DW_FORM_ref1:
551   case DW_FORM_ref2:
552   case DW_FORM_ref4:
553   case DW_FORM_ref8:
554   case DW_FORM_ref_udata:
555     return value + base_offset;
556 
557   case DW_FORM_ref_addr:
558   case DW_FORM_ref_sig8:
559   case DW_FORM_GNU_ref_alt:
560     return value;
561 
562   default:
563     return DW_INVALID_OFFSET;
564   }
565 }
566 
567 const uint8_t *DWARFFormValue::BlockData() const { return m_value.data; }
568 
569 bool DWARFFormValue::IsBlockForm(const dw_form_t form) {
570   switch (form) {
571   case DW_FORM_exprloc:
572   case DW_FORM_block:
573   case DW_FORM_block1:
574   case DW_FORM_block2:
575   case DW_FORM_block4:
576     return true;
577   default:
578     return false;
579   }
580   llvm_unreachable("All cases handled above!");
581 }
582 
583 bool DWARFFormValue::IsDataForm(const dw_form_t form) {
584   switch (form) {
585   case DW_FORM_sdata:
586   case DW_FORM_udata:
587   case DW_FORM_data1:
588   case DW_FORM_data2:
589   case DW_FORM_data4:
590   case DW_FORM_data8:
591     return true;
592   default:
593     return false;
594   }
595   llvm_unreachable("All cases handled above!");
596 }
597 
598 bool DWARFFormValue::FormIsSupported(dw_form_t form) {
599   switch (form) {
600     case DW_FORM_addr:
601     case DW_FORM_addrx:
602     case DW_FORM_loclistx:
603     case DW_FORM_rnglistx:
604     case DW_FORM_block2:
605     case DW_FORM_block4:
606     case DW_FORM_data2:
607     case DW_FORM_data4:
608     case DW_FORM_data8:
609     case DW_FORM_string:
610     case DW_FORM_block:
611     case DW_FORM_block1:
612     case DW_FORM_data1:
613     case DW_FORM_flag:
614     case DW_FORM_sdata:
615     case DW_FORM_strp:
616     case DW_FORM_line_strp:
617     case DW_FORM_strx:
618     case DW_FORM_strx1:
619     case DW_FORM_strx2:
620     case DW_FORM_strx3:
621     case DW_FORM_strx4:
622     case DW_FORM_udata:
623     case DW_FORM_ref_addr:
624     case DW_FORM_ref1:
625     case DW_FORM_ref2:
626     case DW_FORM_ref4:
627     case DW_FORM_ref8:
628     case DW_FORM_ref_udata:
629     case DW_FORM_indirect:
630     case DW_FORM_sec_offset:
631     case DW_FORM_exprloc:
632     case DW_FORM_flag_present:
633     case DW_FORM_ref_sig8:
634     case DW_FORM_GNU_str_index:
635     case DW_FORM_GNU_addr_index:
636     case DW_FORM_implicit_const:
637       return true;
638     default:
639       break;
640   }
641   return false;
642 }
643