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