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/DWARF/DWARFFormValue.h"
11 #include "SyntaxHighlighting.h"
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/None.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/DebugInfo/DWARF/DWARFContext.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. Also accept DW_FORM_strp since this is .debug_str section
313   // offset.
314   return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
315           Form == DW_FORM_strp) &&
316          FC == FC_SectionOffset;
317 }
318 
319 bool DWARFFormValue::extractValue(const DataExtractor &Data,
320                                   uint32_t *OffsetPtr, const DWARFUnit *CU) {
321   U = CU;
322   bool Indirect = false;
323   bool IsBlock = false;
324   Value.data = nullptr;
325   // Read the value for the form into value and follow and DW_FORM_indirect
326   // instances we run into
327   do {
328     Indirect = false;
329     switch (Form) {
330     case DW_FORM_addr:
331     case DW_FORM_ref_addr: {
332       if (!U)
333         return false;
334       uint16_t AddrSize = (Form == DW_FORM_addr) ? U->getAddressByteSize()
335                                                  : U->getRefAddrByteSize();
336       Value.uval =
337           getRelocatedValue(Data, AddrSize, OffsetPtr, U->getRelocMap());
338       break;
339     }
340     case DW_FORM_exprloc:
341     case DW_FORM_block:
342       Value.uval = Data.getULEB128(OffsetPtr);
343       IsBlock = true;
344       break;
345     case DW_FORM_block1:
346       Value.uval = Data.getU8(OffsetPtr);
347       IsBlock = true;
348       break;
349     case DW_FORM_block2:
350       Value.uval = Data.getU16(OffsetPtr);
351       IsBlock = true;
352       break;
353     case DW_FORM_block4:
354       Value.uval = Data.getU32(OffsetPtr);
355       IsBlock = true;
356       break;
357     case DW_FORM_data1:
358     case DW_FORM_ref1:
359     case DW_FORM_flag:
360     case DW_FORM_strx1:
361     case DW_FORM_addrx1:
362       Value.uval = Data.getU8(OffsetPtr);
363       break;
364     case DW_FORM_data2:
365     case DW_FORM_ref2:
366     case DW_FORM_strx2:
367     case DW_FORM_addrx2:
368       Value.uval = Data.getU16(OffsetPtr);
369       break;
370     case DW_FORM_data4:
371     case DW_FORM_ref4:
372     case DW_FORM_ref_sup4:
373     case DW_FORM_strx4:
374     case DW_FORM_addrx4: {
375       const RelocAddrMap *RelocMap = U ? U->getRelocMap() : nullptr;
376       Value.uval = getRelocatedValue(Data, 4, OffsetPtr, RelocMap);
377       break;
378     }
379     case DW_FORM_data8:
380     case DW_FORM_ref8:
381     case DW_FORM_ref_sup8:
382       Value.uval = Data.getU64(OffsetPtr);
383       break;
384     case DW_FORM_sdata:
385       Value.sval = Data.getSLEB128(OffsetPtr);
386       break;
387     case DW_FORM_udata:
388     case DW_FORM_ref_udata:
389       Value.uval = Data.getULEB128(OffsetPtr);
390       break;
391     case DW_FORM_string:
392       Value.cstr = Data.getCStr(OffsetPtr);
393       break;
394     case DW_FORM_indirect:
395       Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
396       Indirect = true;
397       break;
398     case DW_FORM_strp:
399     case DW_FORM_sec_offset:
400     case DW_FORM_GNU_ref_alt:
401     case DW_FORM_GNU_strp_alt:
402     case DW_FORM_line_strp:
403     case DW_FORM_strp_sup: {
404       if (!U)
405         return false;
406       Value.uval = getRelocatedValue(Data, U->getDwarfOffsetByteSize(),
407                                      OffsetPtr, U->getRelocMap());
408       break;
409     }
410     case DW_FORM_flag_present:
411       Value.uval = 1;
412       break;
413     case DW_FORM_ref_sig8:
414       Value.uval = Data.getU64(OffsetPtr);
415       break;
416     case DW_FORM_GNU_addr_index:
417     case DW_FORM_GNU_str_index:
418       Value.uval = Data.getULEB128(OffsetPtr);
419       break;
420     default:
421       // DWARFFormValue::skipValue() will have caught this and caused all
422       // DWARF DIEs to fail to be parsed, so this code is not be reachable.
423       llvm_unreachable("unsupported form");
424     }
425   } while (Indirect);
426 
427   if (IsBlock) {
428     StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
429     Value.data = nullptr;
430     if (!Str.empty()) {
431       Value.data = reinterpret_cast<const uint8_t *>(Str.data());
432       *OffsetPtr += Value.uval;
433     }
434   }
435 
436   return true;
437 }
438 
439 bool DWARFFormValue::skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
440                                const DWARFUnit *U) const {
441   return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U);
442 }
443 
444 bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
445                                uint32_t *OffsetPtr, const DWARFUnit *U) {
446   return skipFormValue(Form, DebugInfoData, OffsetPtr, U);
447 }
448 
449 bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
450                                uint32_t *OffsetPtr, uint16_t Version,
451                                uint8_t AddrSize,
452                                llvm::dwarf::DwarfFormat Format) {
453   FormSizeHelper FSH(Version, AddrSize, Format);
454   return skipFormValue(Form, DebugInfoData, OffsetPtr, &FSH);
455 }
456 
457 void DWARFFormValue::dump(raw_ostream &OS) const {
458   uint64_t UValue = Value.uval;
459   bool CURelativeOffset = false;
460 
461   switch (Form) {
462   case DW_FORM_addr:
463     OS << format("0x%016" PRIx64, UValue);
464     break;
465   case DW_FORM_GNU_addr_index: {
466     OS << format(" indexed (%8.8x) address = ", (uint32_t)UValue);
467     uint64_t Address;
468     if (U == nullptr)
469       OS << "<invalid dwarf unit>";
470     else if (U->getAddrOffsetSectionItem(UValue, Address))
471       OS << format("0x%016" PRIx64, Address);
472     else
473       OS << "<no .debug_addr section>";
474     break;
475   }
476   case DW_FORM_flag_present:
477     OS << "true";
478     break;
479   case DW_FORM_flag:
480   case DW_FORM_data1:
481     OS << format("0x%02x", (uint8_t)UValue);
482     break;
483   case DW_FORM_data2:
484     OS << format("0x%04x", (uint16_t)UValue);
485     break;
486   case DW_FORM_data4:
487     OS << format("0x%08x", (uint32_t)UValue);
488     break;
489   case DW_FORM_ref_sig8:
490   case DW_FORM_data8:
491     OS << format("0x%016" PRIx64, UValue);
492     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:
507         OS << format("<0x%" PRIx64 "> ", UValue);
508         break;
509       case DW_FORM_block1:
510         OS << format("<0x%2.2x> ", (uint8_t)UValue);
511         break;
512       case DW_FORM_block2:
513         OS << format("<0x%4.4x> ", (uint16_t)UValue);
514         break;
515       case DW_FORM_block4:
516         OS << format("<0x%8.8x> ", (uint32_t)UValue);
517         break;
518       default:
519         break;
520       }
521 
522       const uint8_t *DataPtr = Value.data;
523       if (DataPtr) {
524         // UValue contains size of block
525         const uint8_t *EndDataPtr = DataPtr + UValue;
526         while (DataPtr < EndDataPtr) {
527           OS << format("%2.2x ", *DataPtr);
528           ++DataPtr;
529         }
530       } else
531         OS << "NULL";
532     }
533     break;
534 
535   case DW_FORM_sdata:
536     OS << Value.sval;
537     break;
538   case DW_FORM_udata:
539     OS << Value.uval;
540     break;
541   case DW_FORM_strp:
542     OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
543     dumpString(OS);
544     break;
545   case DW_FORM_GNU_str_index:
546     OS << format(" indexed (%8.8x) string = ", (uint32_t)UValue);
547     dumpString(OS);
548     break;
549   case DW_FORM_GNU_strp_alt:
550     OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
551     dumpString(OS);
552     break;
553   case DW_FORM_ref_addr:
554     OS << format("0x%016" PRIx64, UValue);
555     break;
556   case DW_FORM_ref1:
557     CURelativeOffset = true;
558     OS << format("cu + 0x%2.2x", (uint8_t)UValue);
559     break;
560   case DW_FORM_ref2:
561     CURelativeOffset = true;
562     OS << format("cu + 0x%4.4x", (uint16_t)UValue);
563     break;
564   case DW_FORM_ref4:
565     CURelativeOffset = true;
566     OS << format("cu + 0x%4.4x", (uint32_t)UValue);
567     break;
568   case DW_FORM_ref8:
569     CURelativeOffset = true;
570     OS << format("cu + 0x%8.8" PRIx64, UValue);
571     break;
572   case DW_FORM_ref_udata:
573     CURelativeOffset = true;
574     OS << format("cu + 0x%" PRIx64, UValue);
575     break;
576   case DW_FORM_GNU_ref_alt:
577     OS << format("<alt 0x%" PRIx64 ">", UValue);
578     break;
579 
580   // All DW_FORM_indirect attributes should be resolved prior to calling
581   // this function
582   case DW_FORM_indirect:
583     OS << "DW_FORM_indirect";
584     break;
585 
586   // Should be formatted to 64-bit for DWARF64.
587   case DW_FORM_sec_offset:
588     OS << format("0x%08x", (uint32_t)UValue);
589     break;
590 
591   default:
592     OS << format("DW_FORM(0x%4.4x)", Form);
593     break;
594   }
595 
596   if (CURelativeOffset) {
597     OS << " => {";
598     WithColor(OS, syntax::Address).get()
599         << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
600     OS << "}";
601   }
602 }
603 
604 void DWARFFormValue::dumpString(raw_ostream &OS) const {
605   Optional<const char *> DbgStr = getAsCString();
606   if (DbgStr.hasValue()) {
607     raw_ostream &COS = WithColor(OS, syntax::String);
608     COS << '"';
609     COS.write_escaped(DbgStr.getValue());
610     COS << '"';
611   }
612 }
613 
614 Optional<const char *> DWARFFormValue::getAsCString() const {
615   if (!isFormClass(FC_String))
616     return None;
617   if (Form == DW_FORM_string)
618     return Value.cstr;
619   // FIXME: Add support for DW_FORM_GNU_strp_alt
620   if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
621     return None;
622   uint32_t Offset = Value.uval;
623   if (Form == DW_FORM_GNU_str_index) {
624     uint32_t StrOffset;
625     if (!U->getStringOffsetSectionItem(Offset, StrOffset))
626       return None;
627     Offset = StrOffset;
628   }
629   if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
630     return Str;
631   }
632   return None;
633 }
634 
635 Optional<uint64_t> DWARFFormValue::getAsAddress() const {
636   if (!isFormClass(FC_Address))
637     return None;
638   if (Form == DW_FORM_GNU_addr_index) {
639     uint32_t Index = Value.uval;
640     uint64_t Result;
641     if (!U || !U->getAddrOffsetSectionItem(Index, Result))
642       return None;
643     return Result;
644   }
645   return Value.uval;
646 }
647 
648 Optional<uint64_t> DWARFFormValue::getAsReference() const {
649   if (!isFormClass(FC_Reference))
650     return None;
651   switch (Form) {
652   case DW_FORM_ref1:
653   case DW_FORM_ref2:
654   case DW_FORM_ref4:
655   case DW_FORM_ref8:
656   case DW_FORM_ref_udata:
657     if (!U)
658       return None;
659     return Value.uval + U->getOffset();
660   case DW_FORM_ref_addr:
661   case DW_FORM_ref_sig8:
662   case DW_FORM_GNU_ref_alt:
663     return Value.uval;
664   default:
665     return None;
666   }
667 }
668 
669 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
670   if (!isFormClass(FC_SectionOffset))
671     return None;
672   return Value.uval;
673 }
674 
675 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
676   if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
677       Form == DW_FORM_sdata)
678     return None;
679   return Value.uval;
680 }
681 
682 Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
683   if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
684       (Form == DW_FORM_udata &&
685        uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
686     return None;
687   switch (Form) {
688   case DW_FORM_data4:
689     return int32_t(Value.uval);
690   case DW_FORM_data2:
691     return int16_t(Value.uval);
692   case DW_FORM_data1:
693     return int8_t(Value.uval);
694   case DW_FORM_sdata:
695   case DW_FORM_data8:
696   default:
697     return Value.sval;
698   }
699 }
700 
701 Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
702   if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
703     return None;
704   return makeArrayRef(Value.data, Value.uval);
705 }
706 
707 Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
708   if (!isFormClass(FC_String) && Form == DW_FORM_string)
709     return None;
710   return Value.uval;
711 }
712 
713 Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
714   if (!isFormClass(FC_Reference))
715     return None;
716   return Value.uval;
717 }
718