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 "SyntaxHighlighting.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/None.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
17 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
19 #include "llvm/Support/Dwarf.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <cinttypes>
24 #include <cstdint>
25 #include <limits>
26 
27 using namespace llvm;
28 using namespace dwarf;
29 using namespace syntax;
30 
31 static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
32   DWARFFormValue::FC_Unknown,       // 0x0
33   DWARFFormValue::FC_Address,       // 0x01 DW_FORM_addr
34   DWARFFormValue::FC_Unknown,       // 0x02 unused
35   DWARFFormValue::FC_Block,         // 0x03 DW_FORM_block2
36   DWARFFormValue::FC_Block,         // 0x04 DW_FORM_block4
37   DWARFFormValue::FC_Constant,      // 0x05 DW_FORM_data2
38   // --- These can be FC_SectionOffset in DWARF3 and below:
39   DWARFFormValue::FC_Constant,      // 0x06 DW_FORM_data4
40   DWARFFormValue::FC_Constant,      // 0x07 DW_FORM_data8
41   // ---
42   DWARFFormValue::FC_String,        // 0x08 DW_FORM_string
43   DWARFFormValue::FC_Block,         // 0x09 DW_FORM_block
44   DWARFFormValue::FC_Block,         // 0x0a DW_FORM_block1
45   DWARFFormValue::FC_Constant,      // 0x0b DW_FORM_data1
46   DWARFFormValue::FC_Flag,          // 0x0c DW_FORM_flag
47   DWARFFormValue::FC_Constant,      // 0x0d DW_FORM_sdata
48   DWARFFormValue::FC_String,        // 0x0e DW_FORM_strp
49   DWARFFormValue::FC_Constant,      // 0x0f DW_FORM_udata
50   DWARFFormValue::FC_Reference,     // 0x10 DW_FORM_ref_addr
51   DWARFFormValue::FC_Reference,     // 0x11 DW_FORM_ref1
52   DWARFFormValue::FC_Reference,     // 0x12 DW_FORM_ref2
53   DWARFFormValue::FC_Reference,     // 0x13 DW_FORM_ref4
54   DWARFFormValue::FC_Reference,     // 0x14 DW_FORM_ref8
55   DWARFFormValue::FC_Reference,     // 0x15 DW_FORM_ref_udata
56   DWARFFormValue::FC_Indirect,      // 0x16 DW_FORM_indirect
57   DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
58   DWARFFormValue::FC_Exprloc,       // 0x18 DW_FORM_exprloc
59   DWARFFormValue::FC_Flag,          // 0x19 DW_FORM_flag_present
60 };
61 
62 namespace {
63 
64 /// A helper class that can be used in DWARFFormValue.cpp functions that need
65 /// to know the byte size of DW_FORM values that vary in size depending on the
66 /// DWARF version, address byte size, or DWARF32 or DWARF64.
67 class FormSizeHelper {
68   uint16_t Version;
69   uint8_t AddrSize;
70   llvm::dwarf::DwarfFormat Format;
71 
72 public:
73   FormSizeHelper(uint16_t V, uint8_t A, llvm::dwarf::DwarfFormat F)
74       : Version(V), AddrSize(A), Format(F) {}
75 
76   uint8_t getAddressByteSize() const { return AddrSize; }
77 
78   uint8_t getRefAddrByteSize() const {
79     if (Version == 2)
80       return AddrSize;
81     return getDwarfOffsetByteSize();
82   }
83 
84   uint8_t getDwarfOffsetByteSize() const {
85     switch (Format) {
86       case dwarf::DwarfFormat::DWARF32:
87         return 4;
88       case dwarf::DwarfFormat::DWARF64:
89         return 8;
90     }
91     llvm_unreachable("Invalid Format value");
92   }
93 };
94 
95 } // end anonymous namespace
96 
97 template <class T>
98 static Optional<uint8_t> getFixedByteSize(dwarf::Form Form, const T *U) {
99   switch (Form) {
100     case DW_FORM_addr:
101       if (U)
102         return U->getAddressByteSize();
103       return None;
104 
105     case DW_FORM_block:          // ULEB128 length L followed by L bytes.
106     case DW_FORM_block1:         // 1 byte length L followed by L bytes.
107     case DW_FORM_block2:         // 2 byte length L followed by L bytes.
108     case DW_FORM_block4:         // 4 byte length L followed by L bytes.
109     case DW_FORM_string:         // C-string with null terminator.
110     case DW_FORM_sdata:          // SLEB128.
111     case DW_FORM_udata:          // ULEB128.
112     case DW_FORM_ref_udata:      // ULEB128.
113     case DW_FORM_indirect:       // ULEB128.
114     case DW_FORM_exprloc:        // ULEB128 length L followed by L bytes.
115     case DW_FORM_strx:           // ULEB128.
116     case DW_FORM_addrx:          // ULEB128.
117     case DW_FORM_loclistx:       // ULEB128.
118     case DW_FORM_rnglistx:       // ULEB128.
119     case DW_FORM_GNU_addr_index: // ULEB128.
120     case DW_FORM_GNU_str_index:  // ULEB128.
121       return None;
122 
123     case DW_FORM_ref_addr:
124       if (U)
125         return U->getRefAddrByteSize();
126       return None;
127 
128     case DW_FORM_flag:
129     case DW_FORM_data1:
130     case DW_FORM_ref1:
131     case DW_FORM_strx1:
132     case DW_FORM_addrx1:
133       return 1;
134 
135     case DW_FORM_data2:
136     case DW_FORM_ref2:
137     case DW_FORM_strx2:
138     case DW_FORM_addrx2:
139       return 2;
140 
141     case DW_FORM_data4:
142     case DW_FORM_ref4:
143     case DW_FORM_ref_sup4:
144     case DW_FORM_strx4:
145     case DW_FORM_addrx4:
146       return 4;
147 
148     case DW_FORM_strp:
149     case DW_FORM_GNU_ref_alt:
150     case DW_FORM_GNU_strp_alt:
151     case DW_FORM_line_strp:
152     case DW_FORM_sec_offset:
153     case DW_FORM_strp_sup:
154       if (U)
155         return U->getDwarfOffsetByteSize();
156       return None;
157 
158     case DW_FORM_data8:
159     case DW_FORM_ref8:
160     case DW_FORM_ref_sig8:
161     case DW_FORM_ref_sup8:
162       return 8;
163 
164     case DW_FORM_flag_present:
165       return 0;
166 
167     case DW_FORM_data16:
168       return 16;
169 
170     case DW_FORM_implicit_const:
171       // The implicit value is stored in the abbreviation as a SLEB128, and
172       // there no data in debug info.
173       return 0;
174 
175     default:
176       llvm_unreachable("Handle this form in this switch statement");
177   }
178   return None;
179 }
180 
181 template <class T>
182 static bool skipFormValue(dwarf::Form Form, const DataExtractor &DebugInfoData,
183                           uint32_t *OffsetPtr, const T *U) {
184   bool Indirect = false;
185   do {
186     switch (Form) {
187         // Blocks of inlined data that have a length field and the data bytes
188         // inlined in the .debug_info.
189       case DW_FORM_exprloc:
190       case DW_FORM_block: {
191         uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
192         *OffsetPtr += size;
193         return true;
194       }
195       case DW_FORM_block1: {
196         uint8_t size = DebugInfoData.getU8(OffsetPtr);
197         *OffsetPtr += size;
198         return true;
199       }
200       case DW_FORM_block2: {
201         uint16_t size = DebugInfoData.getU16(OffsetPtr);
202         *OffsetPtr += size;
203         return true;
204       }
205       case DW_FORM_block4: {
206         uint32_t size = DebugInfoData.getU32(OffsetPtr);
207         *OffsetPtr += size;
208         return true;
209       }
210 
211         // Inlined NULL terminated C-strings.
212       case DW_FORM_string:
213         DebugInfoData.getCStr(OffsetPtr);
214         return true;
215 
216       case DW_FORM_addr:
217       case DW_FORM_ref_addr:
218       case DW_FORM_flag_present:
219       case DW_FORM_data1:
220       case DW_FORM_data2:
221       case DW_FORM_data4:
222       case DW_FORM_data8:
223       case DW_FORM_flag:
224       case DW_FORM_ref1:
225       case DW_FORM_ref2:
226       case DW_FORM_ref4:
227       case DW_FORM_ref8:
228       case DW_FORM_ref_sig8:
229       case DW_FORM_ref_sup4:
230       case DW_FORM_ref_sup8:
231       case DW_FORM_strx1:
232       case DW_FORM_strx2:
233       case DW_FORM_strx4:
234       case DW_FORM_addrx1:
235       case DW_FORM_addrx2:
236       case DW_FORM_addrx4:
237       case DW_FORM_sec_offset:
238       case DW_FORM_strp:
239       case DW_FORM_strp_sup:
240       case DW_FORM_line_strp:
241       case DW_FORM_GNU_ref_alt:
242       case DW_FORM_GNU_strp_alt:
243         if (Optional<uint8_t> FixedSize = ::getFixedByteSize(Form, U)) {
244           *OffsetPtr += *FixedSize;
245           return true;
246         }
247         return false;
248 
249         // signed or unsigned LEB 128 values.
250       case DW_FORM_sdata:
251         DebugInfoData.getSLEB128(OffsetPtr);
252         return true;
253 
254       case DW_FORM_udata:
255       case DW_FORM_ref_udata:
256       case DW_FORM_strx:
257       case DW_FORM_addrx:
258       case DW_FORM_loclistx:
259       case DW_FORM_rnglistx:
260       case DW_FORM_GNU_addr_index:
261       case DW_FORM_GNU_str_index:
262         DebugInfoData.getULEB128(OffsetPtr);
263         return true;
264 
265       case DW_FORM_indirect:
266         Indirect = true;
267         Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
268         break;
269 
270       default:
271         return false;
272     }
273   } while (Indirect);
274   return true;
275 }
276 
277 Optional<uint8_t> DWARFFormValue::getFixedByteSize(dwarf::Form Form,
278                                                    const DWARFUnit *U) {
279   return ::getFixedByteSize(Form, U);
280 }
281 
282 Optional<uint8_t>
283 DWARFFormValue::getFixedByteSize(dwarf::Form Form, uint16_t Version,
284                                  uint8_t AddrSize,
285                                  llvm::dwarf::DwarfFormat Format) {
286   FormSizeHelper FSH(Version, AddrSize, Format);
287   return ::getFixedByteSize(Form, &FSH);
288 }
289 
290 bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
291   // First, check DWARF4 form classes.
292   if (Form < makeArrayRef(DWARF4FormClasses).size() &&
293       DWARF4FormClasses[Form] == FC)
294     return true;
295   // Check more forms from DWARF4 and DWARF5 proposals.
296   switch (Form) {
297   case DW_FORM_ref_sig8:
298   case DW_FORM_GNU_ref_alt:
299     return (FC == FC_Reference);
300   case DW_FORM_GNU_addr_index:
301     return (FC == FC_Address);
302   case DW_FORM_GNU_str_index:
303   case DW_FORM_GNU_strp_alt:
304     return (FC == FC_String);
305   case DW_FORM_implicit_const:
306     return (FC == FC_Constant);
307   default:
308     break;
309   }
310   // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
311   // Don't check for DWARF version here, as some producers may still do this
312   // by mistake.
313   return (Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
314          FC == FC_SectionOffset;
315 }
316 
317 bool DWARFFormValue::extractValue(const DataExtractor &data,
318                                   uint32_t *offset_ptr,
319                                   const DWARFUnit *cu) {
320   U = cu;
321   bool indirect = false;
322   bool is_block = false;
323   Value.data = nullptr;
324   // Read the value for the form into value and follow and DW_FORM_indirect
325   // instances we run into
326   do {
327     indirect = false;
328     switch (Form) {
329     case DW_FORM_addr:
330     case DW_FORM_ref_addr: {
331       if (!U)
332         return false;
333       uint16_t AddrSize =
334           (Form == DW_FORM_addr)
335               ? U->getAddressByteSize()
336               : U->getRefAddrByteSize();
337       RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr);
338       if (AI != U->getRelocMap()->end()) {
339         Value.uval = data.getUnsigned(offset_ptr, AddrSize) + AI->second.second;
340       } else
341         Value.uval = data.getUnsigned(offset_ptr, AddrSize);
342       break;
343     }
344     case DW_FORM_exprloc:
345     case DW_FORM_block:
346       Value.uval = data.getULEB128(offset_ptr);
347       is_block = true;
348       break;
349     case DW_FORM_block1:
350       Value.uval = data.getU8(offset_ptr);
351       is_block = true;
352       break;
353     case DW_FORM_block2:
354       Value.uval = data.getU16(offset_ptr);
355       is_block = true;
356       break;
357     case DW_FORM_block4:
358       Value.uval = data.getU32(offset_ptr);
359       is_block = true;
360       break;
361     case DW_FORM_data1:
362     case DW_FORM_ref1:
363     case DW_FORM_flag:
364     case DW_FORM_strx1:
365     case DW_FORM_addrx1:
366       Value.uval = data.getU8(offset_ptr);
367       break;
368     case DW_FORM_data2:
369     case DW_FORM_ref2:
370     case DW_FORM_strx2:
371     case DW_FORM_addrx2:
372       Value.uval = data.getU16(offset_ptr);
373       break;
374     case DW_FORM_data4:
375     case DW_FORM_ref4:
376     case DW_FORM_ref_sup4:
377     case DW_FORM_strx4:
378     case DW_FORM_addrx4: {
379       Value.uval = data.getU32(offset_ptr);
380       if (!U)
381         break;
382       RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr-4);
383       if (AI != U->getRelocMap()->end())
384         Value.uval += AI->second.second;
385       break;
386     }
387     case DW_FORM_data8:
388     case DW_FORM_ref8:
389     case DW_FORM_ref_sup8:
390       Value.uval = data.getU64(offset_ptr);
391       break;
392     case DW_FORM_sdata:
393       Value.sval = data.getSLEB128(offset_ptr);
394       break;
395     case DW_FORM_udata:
396     case DW_FORM_ref_udata:
397       Value.uval = data.getULEB128(offset_ptr);
398       break;
399     case DW_FORM_string:
400       Value.cstr = data.getCStr(offset_ptr);
401       break;
402     case DW_FORM_indirect:
403       Form = static_cast<dwarf::Form>(data.getULEB128(offset_ptr));
404       indirect = true;
405       break;
406     case DW_FORM_strp:
407     case DW_FORM_sec_offset:
408     case DW_FORM_GNU_ref_alt:
409     case DW_FORM_GNU_strp_alt:
410     case DW_FORM_line_strp:
411     case DW_FORM_strp_sup: {
412       if (!U)
413         return false;
414       RelocAddrMap::const_iterator AI = U->getRelocMap()->find(*offset_ptr);
415       uint8_t Size = U->getDwarfOffsetByteSize();
416       Value.uval = data.getUnsigned(offset_ptr, Size);
417       if (AI != U->getRelocMap()->end())
418         Value.uval += AI->second.second;
419       break;
420     }
421     case DW_FORM_flag_present:
422       Value.uval = 1;
423       break;
424     case DW_FORM_ref_sig8:
425       Value.uval = data.getU64(offset_ptr);
426       break;
427     case DW_FORM_GNU_addr_index:
428     case DW_FORM_GNU_str_index:
429       Value.uval = data.getULEB128(offset_ptr);
430       break;
431     default:
432       // DWARFFormValue::skipValue() will have caught this and caused all
433       // DWARF DIEs to fail to be parsed, so this code is not be reachable.
434       llvm_unreachable("unsupported form");
435     }
436   } while (indirect);
437 
438   if (is_block) {
439     StringRef str = data.getData().substr(*offset_ptr, Value.uval);
440     Value.data = nullptr;
441     if (!str.empty()) {
442       Value.data = reinterpret_cast<const uint8_t *>(str.data());
443       *offset_ptr += Value.uval;
444     }
445   }
446 
447   return true;
448 }
449 
450 bool DWARFFormValue::skipValue(DataExtractor DebugInfoData,
451                                uint32_t *offset_ptr, const DWARFUnit *U) const {
452   return DWARFFormValue::skipValue(Form, DebugInfoData, offset_ptr, U);
453 }
454 
455 bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
456                                uint32_t *offset_ptr, const DWARFUnit *U) {
457   return skipFormValue(form, DebugInfoData, offset_ptr, U);
458 }
459 
460 bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
461                                uint32_t *offset_ptr, uint16_t Version,
462                                uint8_t AddrSize,
463                                llvm::dwarf::DwarfFormat Format) {
464   FormSizeHelper FSH(Version, AddrSize, Format);
465   return skipFormValue(form, DebugInfoData, offset_ptr, &FSH);
466 }
467 
468 void
469 DWARFFormValue::dump(raw_ostream &OS) const {
470   uint64_t uvalue = Value.uval;
471   bool cu_relative_offset = false;
472 
473   switch (Form) {
474   case DW_FORM_addr:      OS << format("0x%016" PRIx64, uvalue); break;
475   case DW_FORM_GNU_addr_index: {
476     OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
477     uint64_t Address;
478     if (U == nullptr)
479       OS << "<invalid dwarf unit>";
480     else if (U->getAddrOffsetSectionItem(uvalue, Address))
481       OS << format("0x%016" PRIx64, Address);
482     else
483       OS << "<no .debug_addr section>";
484     break;
485   }
486   case DW_FORM_flag_present: OS << "true"; break;
487   case DW_FORM_flag:
488   case DW_FORM_data1:     OS << format("0x%02x", (uint8_t)uvalue); break;
489   case DW_FORM_data2:     OS << format("0x%04x", (uint16_t)uvalue); break;
490   case DW_FORM_data4:     OS << format("0x%08x", (uint32_t)uvalue); break;
491   case DW_FORM_ref_sig8:
492   case DW_FORM_data8:     OS << format("0x%016" PRIx64, uvalue); break;
493   case DW_FORM_string:
494     OS << '"';
495     OS.write_escaped(Value.cstr);
496     OS << '"';
497     break;
498   case DW_FORM_exprloc:
499   case DW_FORM_block:
500   case DW_FORM_block1:
501   case DW_FORM_block2:
502   case DW_FORM_block4:
503     if (uvalue > 0) {
504       switch (Form) {
505       case DW_FORM_exprloc:
506       case DW_FORM_block:  OS << format("<0x%" PRIx64 "> ", uvalue);     break;
507       case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue);  break;
508       case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
509       case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
510       default: break;
511       }
512 
513       const uint8_t* data_ptr = Value.data;
514       if (data_ptr) {
515         // uvalue contains size of block
516         const uint8_t* end_data_ptr = data_ptr + uvalue;
517         while (data_ptr < end_data_ptr) {
518           OS << format("%2.2x ", *data_ptr);
519           ++data_ptr;
520         }
521       }
522       else
523         OS << "NULL";
524     }
525     break;
526 
527   case DW_FORM_sdata:     OS << Value.sval; break;
528   case DW_FORM_udata:     OS << Value.uval; break;
529   case DW_FORM_strp:
530     OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
531     dumpString(OS);
532     break;
533   case DW_FORM_GNU_str_index:
534     OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
535     dumpString(OS);
536     break;
537   case DW_FORM_GNU_strp_alt:
538     OS << format("alt indirect string, offset: 0x%" PRIx64 "", uvalue);
539     dumpString(OS);
540     break;
541   case DW_FORM_ref_addr:
542     OS << format("0x%016" PRIx64, uvalue);
543     break;
544   case DW_FORM_ref1:
545     cu_relative_offset = true;
546     OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
547     break;
548   case DW_FORM_ref2:
549     cu_relative_offset = true;
550     OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
551     break;
552   case DW_FORM_ref4:
553     cu_relative_offset = true;
554     OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
555     break;
556   case DW_FORM_ref8:
557     cu_relative_offset = true;
558     OS << format("cu + 0x%8.8" PRIx64, uvalue);
559     break;
560   case DW_FORM_ref_udata:
561     cu_relative_offset = true;
562     OS << format("cu + 0x%" PRIx64, uvalue);
563     break;
564   case DW_FORM_GNU_ref_alt:
565     OS << format("<alt 0x%" PRIx64 ">", uvalue);
566     break;
567 
568     // All DW_FORM_indirect attributes should be resolved prior to calling
569     // this function
570   case DW_FORM_indirect:
571     OS << "DW_FORM_indirect";
572     break;
573 
574     // Should be formatted to 64-bit for DWARF64.
575   case DW_FORM_sec_offset:
576     OS << format("0x%08x", (uint32_t)uvalue);
577     break;
578 
579   default:
580     OS << format("DW_FORM(0x%4.4x)", Form);
581     break;
582   }
583 
584   if (cu_relative_offset) {
585     OS << " => {";
586     WithColor(OS, syntax::Address).get()
587       << format("0x%8.8" PRIx64, uvalue + (U ? U->getOffset() : 0));
588     OS << "}";
589   }
590 }
591 
592 void DWARFFormValue::dumpString(raw_ostream &OS) const {
593   Optional<const char *> DbgStr = getAsCString();
594   if (DbgStr.hasValue()) {
595     raw_ostream &COS = WithColor(OS, syntax::String);
596     COS << '"';
597     COS.write_escaped(DbgStr.getValue());
598     COS << '"';
599   }
600 }
601 
602 Optional<const char *> DWARFFormValue::getAsCString() const {
603   if (!isFormClass(FC_String))
604     return None;
605   if (Form == DW_FORM_string)
606     return Value.cstr;
607   // FIXME: Add support for DW_FORM_GNU_strp_alt
608   if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
609     return None;
610   uint32_t Offset = Value.uval;
611   if (Form == DW_FORM_GNU_str_index) {
612     uint32_t StrOffset;
613     if (!U->getStringOffsetSectionItem(Offset, StrOffset))
614       return None;
615     Offset = StrOffset;
616   }
617   if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
618     return Str;
619   }
620   return None;
621 }
622 
623 Optional<uint64_t> DWARFFormValue::getAsAddress() const {
624   if (!isFormClass(FC_Address))
625     return None;
626   if (Form == DW_FORM_GNU_addr_index) {
627     uint32_t Index = Value.uval;
628     uint64_t Result;
629     if (!U || !U->getAddrOffsetSectionItem(Index, Result))
630       return None;
631     return Result;
632   }
633   return Value.uval;
634 }
635 
636 Optional<uint64_t> DWARFFormValue::getAsReference() const {
637   if (!isFormClass(FC_Reference))
638     return None;
639   switch (Form) {
640   case DW_FORM_ref1:
641   case DW_FORM_ref2:
642   case DW_FORM_ref4:
643   case DW_FORM_ref8:
644   case DW_FORM_ref_udata:
645     if (!U)
646       return None;
647     return Value.uval + U->getOffset();
648   case DW_FORM_ref_addr:
649   case DW_FORM_ref_sig8:
650   case DW_FORM_GNU_ref_alt:
651     return Value.uval;
652   default:
653     return None;
654   }
655 }
656 
657 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
658   if (!isFormClass(FC_SectionOffset))
659     return None;
660   return Value.uval;
661 }
662 
663 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
664   if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
665       || Form == DW_FORM_sdata)
666     return None;
667   return Value.uval;
668 }
669 
670 Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
671   if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
672       (Form == DW_FORM_udata && uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
673     return None;
674   switch (Form) {
675   case DW_FORM_data4:
676     return int32_t(Value.uval);
677   case DW_FORM_data2:
678     return int16_t(Value.uval);
679   case DW_FORM_data1:
680     return int8_t(Value.uval);
681   case DW_FORM_sdata:
682   case DW_FORM_data8:
683   default:
684     return Value.sval;
685   }
686 }
687 
688 Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
689   if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
690     return None;
691   return makeArrayRef(Value.data, Value.uval);
692 }
693 
694 Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
695   if (!isFormClass(FC_String) && Form == DW_FORM_string)
696     return None;
697   return Value.uval;
698 }
699 
700 Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
701   if (!isFormClass(FC_Reference))
702     return None;
703   return Value.uval;
704 }
705