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