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