xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFFormValue.cpp (revision dbaad75f2834f40bfe74ebe393d2101967052036)
1 //===- DWARFFormValue.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/Dwarf.h"
15 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
17 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/WithColor.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cinttypes>
23 #include <cstdint>
24 #include <limits>
25 
26 using namespace llvm;
27 using namespace dwarf;
28 
29 static const DWARFFormValue::FormClass DWARF5FormClasses[] = {
30     DWARFFormValue::FC_Unknown,  // 0x0
31     DWARFFormValue::FC_Address,  // 0x01 DW_FORM_addr
32     DWARFFormValue::FC_Unknown,  // 0x02 unused
33     DWARFFormValue::FC_Block,    // 0x03 DW_FORM_block2
34     DWARFFormValue::FC_Block,    // 0x04 DW_FORM_block4
35     DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
36     // --- These can be FC_SectionOffset in DWARF3 and below:
37     DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
38     DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
39     // ---
40     DWARFFormValue::FC_String,        // 0x08 DW_FORM_string
41     DWARFFormValue::FC_Block,         // 0x09 DW_FORM_block
42     DWARFFormValue::FC_Block,         // 0x0a DW_FORM_block1
43     DWARFFormValue::FC_Constant,      // 0x0b DW_FORM_data1
44     DWARFFormValue::FC_Flag,          // 0x0c DW_FORM_flag
45     DWARFFormValue::FC_Constant,      // 0x0d DW_FORM_sdata
46     DWARFFormValue::FC_String,        // 0x0e DW_FORM_strp
47     DWARFFormValue::FC_Constant,      // 0x0f DW_FORM_udata
48     DWARFFormValue::FC_Reference,     // 0x10 DW_FORM_ref_addr
49     DWARFFormValue::FC_Reference,     // 0x11 DW_FORM_ref1
50     DWARFFormValue::FC_Reference,     // 0x12 DW_FORM_ref2
51     DWARFFormValue::FC_Reference,     // 0x13 DW_FORM_ref4
52     DWARFFormValue::FC_Reference,     // 0x14 DW_FORM_ref8
53     DWARFFormValue::FC_Reference,     // 0x15 DW_FORM_ref_udata
54     DWARFFormValue::FC_Indirect,      // 0x16 DW_FORM_indirect
55     DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
56     DWARFFormValue::FC_Exprloc,       // 0x18 DW_FORM_exprloc
57     DWARFFormValue::FC_Flag,          // 0x19 DW_FORM_flag_present
58     DWARFFormValue::FC_String,        // 0x1a DW_FORM_strx
59     DWARFFormValue::FC_Address,       // 0x1b DW_FORM_addrx
60     DWARFFormValue::FC_Reference,     // 0x1c DW_FORM_ref_sup4
61     DWARFFormValue::FC_String,        // 0x1d DW_FORM_strp_sup
62     DWARFFormValue::FC_Constant,      // 0x1e DW_FORM_data16
63     DWARFFormValue::FC_String,        // 0x1f DW_FORM_line_strp
64     DWARFFormValue::FC_Reference,     // 0x20 DW_FORM_ref_sig8
65     DWARFFormValue::FC_Constant,      // 0x21 DW_FORM_implicit_const
66     DWARFFormValue::FC_SectionOffset, // 0x22 DW_FORM_loclistx
67     DWARFFormValue::FC_SectionOffset, // 0x23 DW_FORM_rnglistx
68     DWARFFormValue::FC_Reference,     // 0x24 DW_FORM_ref_sup8
69     DWARFFormValue::FC_String,        // 0x25 DW_FORM_strx1
70     DWARFFormValue::FC_String,        // 0x26 DW_FORM_strx2
71     DWARFFormValue::FC_String,        // 0x27 DW_FORM_strx3
72     DWARFFormValue::FC_String,        // 0x28 DW_FORM_strx4
73     DWARFFormValue::FC_Address,       // 0x29 DW_FORM_addrx1
74     DWARFFormValue::FC_Address,       // 0x2a DW_FORM_addrx2
75     DWARFFormValue::FC_Address,       // 0x2b DW_FORM_addrx3
76     DWARFFormValue::FC_Address,       // 0x2c DW_FORM_addrx4
77 
78 };
79 
80 DWARFFormValue DWARFFormValue::createFromSValue(dwarf::Form F, int64_t V) {
81   return DWARFFormValue(F, ValueType(V));
82 }
83 
84 DWARFFormValue DWARFFormValue::createFromUValue(dwarf::Form F, uint64_t V) {
85   return DWARFFormValue(F, ValueType(V));
86 }
87 
88 DWARFFormValue DWARFFormValue::createFromPValue(dwarf::Form F, const char *V) {
89   return DWARFFormValue(F, ValueType(V));
90 }
91 
92 DWARFFormValue DWARFFormValue::createFromBlockValue(dwarf::Form F,
93                                                     ArrayRef<uint8_t> D) {
94   ValueType V;
95   V.uval = D.size();
96   V.data = D.data();
97   return DWARFFormValue(F, V);
98 }
99 
100 DWARFFormValue DWARFFormValue::createFromUnit(dwarf::Form F, const DWARFUnit *U,
101                                               uint64_t *OffsetPtr) {
102   DWARFFormValue FormValue(F);
103   FormValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
104                          U->getFormParams(), U);
105   return FormValue;
106 }
107 
108 bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
109                                uint64_t *OffsetPtr,
110                                const dwarf::FormParams Params) {
111   bool Indirect = false;
112   do {
113     switch (Form) {
114     // Blocks of inlined data that have a length field and the data bytes
115     // inlined in the .debug_info.
116     case DW_FORM_exprloc:
117     case DW_FORM_block: {
118       uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
119       *OffsetPtr += size;
120       return true;
121     }
122     case DW_FORM_block1: {
123       uint8_t size = DebugInfoData.getU8(OffsetPtr);
124       *OffsetPtr += size;
125       return true;
126     }
127     case DW_FORM_block2: {
128       uint16_t size = DebugInfoData.getU16(OffsetPtr);
129       *OffsetPtr += size;
130       return true;
131     }
132     case DW_FORM_block4: {
133       uint32_t size = DebugInfoData.getU32(OffsetPtr);
134       *OffsetPtr += size;
135       return true;
136     }
137 
138     // Inlined NULL terminated C-strings.
139     case DW_FORM_string:
140       DebugInfoData.getCStr(OffsetPtr);
141       return true;
142 
143     case DW_FORM_addr:
144     case DW_FORM_ref_addr:
145     case DW_FORM_flag_present:
146     case DW_FORM_data1:
147     case DW_FORM_data2:
148     case DW_FORM_data4:
149     case DW_FORM_data8:
150     case DW_FORM_data16:
151     case DW_FORM_flag:
152     case DW_FORM_ref1:
153     case DW_FORM_ref2:
154     case DW_FORM_ref4:
155     case DW_FORM_ref8:
156     case DW_FORM_ref_sig8:
157     case DW_FORM_ref_sup4:
158     case DW_FORM_ref_sup8:
159     case DW_FORM_strx1:
160     case DW_FORM_strx2:
161     case DW_FORM_strx4:
162     case DW_FORM_addrx1:
163     case DW_FORM_addrx2:
164     case DW_FORM_addrx4:
165     case DW_FORM_sec_offset:
166     case DW_FORM_strp:
167     case DW_FORM_strp_sup:
168     case DW_FORM_line_strp:
169     case DW_FORM_GNU_ref_alt:
170     case DW_FORM_GNU_strp_alt:
171     case DW_FORM_implicit_const:
172       if (Optional<uint8_t> FixedSize =
173               dwarf::getFixedFormByteSize(Form, Params)) {
174         *OffsetPtr += *FixedSize;
175         return true;
176       }
177       return false;
178 
179     // signed or unsigned LEB 128 values.
180     case DW_FORM_sdata:
181       DebugInfoData.getSLEB128(OffsetPtr);
182       return true;
183 
184     case DW_FORM_udata:
185     case DW_FORM_ref_udata:
186     case DW_FORM_strx:
187     case DW_FORM_addrx:
188     case DW_FORM_loclistx:
189     case DW_FORM_rnglistx:
190     case DW_FORM_GNU_addr_index:
191     case DW_FORM_GNU_str_index:
192       DebugInfoData.getULEB128(OffsetPtr);
193       return true;
194 
195     case DW_FORM_indirect:
196       Indirect = true;
197       Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
198       break;
199 
200     default:
201       return false;
202     }
203   } while (Indirect);
204   return true;
205 }
206 
207 bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
208   // First, check DWARF5 form classes.
209   if (Form < makeArrayRef(DWARF5FormClasses).size() &&
210       DWARF5FormClasses[Form] == FC)
211     return true;
212   // Check more forms from extensions and proposals.
213   switch (Form) {
214   case DW_FORM_GNU_ref_alt:
215     return (FC == FC_Reference);
216   case DW_FORM_GNU_addr_index:
217     return (FC == FC_Address);
218   case DW_FORM_GNU_str_index:
219   case DW_FORM_GNU_strp_alt:
220     return (FC == FC_String);
221   default:
222     break;
223   }
224 
225   if (FC == FC_SectionOffset) {
226     if (Form == DW_FORM_strp || Form == DW_FORM_line_strp)
227       return true;
228     // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section
229     // offset. If we don't have a DWARFUnit, default to the old behavior.
230     if (Form == DW_FORM_data4 || Form == DW_FORM_data8)
231       return !U || U->getVersion() <= 3;
232   }
233 
234   return false;
235 }
236 
237 bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
238                                   uint64_t *OffsetPtr, dwarf::FormParams FP,
239                                   const DWARFContext *Ctx,
240                                   const DWARFUnit *CU) {
241   if (!Ctx && CU)
242     Ctx = &CU->getContext();
243   C = Ctx;
244   U = CU;
245   Format = FP.Format;
246   bool Indirect = false;
247   bool IsBlock = false;
248   Value.data = nullptr;
249   // Read the value for the form into value and follow and DW_FORM_indirect
250   // instances we run into
251   Error Err = Error::success();
252   do {
253     Indirect = false;
254     switch (Form) {
255     case DW_FORM_addr:
256     case DW_FORM_ref_addr: {
257       uint16_t Size =
258           (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
259       Value.uval =
260           Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex, &Err);
261       break;
262     }
263     case DW_FORM_exprloc:
264     case DW_FORM_block:
265       Value.uval = Data.getULEB128(OffsetPtr, &Err);
266       IsBlock = true;
267       break;
268     case DW_FORM_block1:
269       Value.uval = Data.getU8(OffsetPtr, &Err);
270       IsBlock = true;
271       break;
272     case DW_FORM_block2:
273       Value.uval = Data.getU16(OffsetPtr, &Err);
274       IsBlock = true;
275       break;
276     case DW_FORM_block4:
277       Value.uval = Data.getU32(OffsetPtr, &Err);
278       IsBlock = true;
279       break;
280     case DW_FORM_data1:
281     case DW_FORM_ref1:
282     case DW_FORM_flag:
283     case DW_FORM_strx1:
284     case DW_FORM_addrx1:
285       Value.uval = Data.getU8(OffsetPtr, &Err);
286       break;
287     case DW_FORM_data2:
288     case DW_FORM_ref2:
289     case DW_FORM_strx2:
290     case DW_FORM_addrx2:
291       Value.uval = Data.getU16(OffsetPtr, &Err);
292       break;
293     case DW_FORM_strx3:
294       Value.uval = Data.getU24(OffsetPtr, &Err);
295       break;
296     case DW_FORM_data4:
297     case DW_FORM_ref4:
298     case DW_FORM_ref_sup4:
299     case DW_FORM_strx4:
300     case DW_FORM_addrx4:
301       Value.uval = Data.getRelocatedValue(4, OffsetPtr, nullptr, &Err);
302       break;
303     case DW_FORM_data8:
304     case DW_FORM_ref8:
305     case DW_FORM_ref_sup8:
306       Value.uval = Data.getRelocatedValue(8, OffsetPtr, nullptr, &Err);
307       break;
308     case DW_FORM_data16:
309       // Treat this like a 16-byte block.
310       Value.uval = 16;
311       IsBlock = true;
312       break;
313     case DW_FORM_sdata:
314       Value.sval = Data.getSLEB128(OffsetPtr, &Err);
315       break;
316     case DW_FORM_udata:
317     case DW_FORM_ref_udata:
318     case DW_FORM_rnglistx:
319     case DW_FORM_loclistx:
320     case DW_FORM_GNU_addr_index:
321     case DW_FORM_GNU_str_index:
322     case DW_FORM_addrx:
323     case DW_FORM_strx:
324       Value.uval = Data.getULEB128(OffsetPtr, &Err);
325       break;
326     case DW_FORM_string:
327       Value.cstr = Data.getCStr(OffsetPtr, &Err);
328       break;
329     case DW_FORM_indirect:
330       Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr, &Err));
331       Indirect = true;
332       break;
333     case DW_FORM_strp:
334     case DW_FORM_sec_offset:
335     case DW_FORM_GNU_ref_alt:
336     case DW_FORM_GNU_strp_alt:
337     case DW_FORM_line_strp:
338     case DW_FORM_strp_sup: {
339       Value.uval = Data.getRelocatedValue(FP.getDwarfOffsetByteSize(),
340                                           OffsetPtr, nullptr, &Err);
341       break;
342     }
343     case DW_FORM_flag_present:
344       Value.uval = 1;
345       break;
346     case DW_FORM_ref_sig8:
347       Value.uval = Data.getU64(OffsetPtr, &Err);
348       break;
349     case DW_FORM_implicit_const:
350       // Value has been already set by DWARFFormValue::createFromSValue.
351       break;
352     default:
353       // DWARFFormValue::skipValue() will have caught this and caused all
354       // DWARF DIEs to fail to be parsed, so this code is not be reachable.
355       llvm_unreachable("unsupported form");
356     }
357   } while (Indirect && !Err);
358 
359   if (IsBlock)
360     Value.data = Data.getBytes(OffsetPtr, Value.uval, &Err).bytes_begin();
361 
362   return !errorToBool(std::move(Err));
363 }
364 
365 void DWARFFormValue::dumpAddress(raw_ostream &OS, uint8_t AddressSize,
366                                  uint64_t Address) {
367   uint8_t HexDigits = AddressSize * 2;
368   OS << format("0x%*.*" PRIx64, HexDigits, HexDigits, Address);
369 }
370 
371 void DWARFFormValue::dumpSectionedAddress(raw_ostream &OS,
372                                           DIDumpOptions DumpOpts,
373                                           object::SectionedAddress SA) const {
374   dumpAddress(OS, U->getAddressByteSize(), SA.Address);
375   dumpAddressSection(U->getContext().getDWARFObj(), OS, DumpOpts,
376                      SA.SectionIndex);
377 }
378 
379 void DWARFFormValue::dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
380                                         DIDumpOptions DumpOpts,
381                                         uint64_t SectionIndex) {
382   if (!DumpOpts.Verbose || SectionIndex == -1ULL)
383     return;
384   ArrayRef<SectionName> SectionNames = Obj.getSectionNames();
385   const auto &SecRef = SectionNames[SectionIndex];
386 
387   OS << " \"" << SecRef.Name << '\"';
388 
389   // Print section index if name is not unique.
390   if (!SecRef.IsNameUnique)
391     OS << format(" [%" PRIu64 "]", SectionIndex);
392 }
393 
394 void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
395   uint64_t UValue = Value.uval;
396   bool CURelativeOffset = false;
397   raw_ostream &AddrOS = DumpOpts.ShowAddresses
398                             ? WithColor(OS, HighlightColor::Address).get()
399                             : nulls();
400   int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
401   switch (Form) {
402   case DW_FORM_addr:
403     dumpSectionedAddress(AddrOS, DumpOpts, {Value.uval, Value.SectionIndex});
404     break;
405   case DW_FORM_addrx:
406   case DW_FORM_addrx1:
407   case DW_FORM_addrx2:
408   case DW_FORM_addrx3:
409   case DW_FORM_addrx4:
410   case DW_FORM_GNU_addr_index: {
411     if (U == nullptr) {
412       OS << "<invalid dwarf unit>";
413       break;
414     }
415     Optional<object::SectionedAddress> A = U->getAddrOffsetSectionItem(UValue);
416     if (!A || DumpOpts.Verbose)
417       AddrOS << format("indexed (%8.8x) address = ", (uint32_t)UValue);
418     if (A)
419       dumpSectionedAddress(AddrOS, DumpOpts, *A);
420     else
421       OS << "<unresolved>";
422     break;
423   }
424   case DW_FORM_flag_present:
425     OS << "true";
426     break;
427   case DW_FORM_flag:
428   case DW_FORM_data1:
429     OS << format("0x%02x", (uint8_t)UValue);
430     break;
431   case DW_FORM_data2:
432     OS << format("0x%04x", (uint16_t)UValue);
433     break;
434   case DW_FORM_data4:
435     OS << format("0x%08x", (uint32_t)UValue);
436     break;
437   case DW_FORM_ref_sig8:
438     AddrOS << format("0x%016" PRIx64, UValue);
439     break;
440   case DW_FORM_data8:
441     OS << format("0x%016" PRIx64, UValue);
442     break;
443   case DW_FORM_data16:
444     OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
445     break;
446   case DW_FORM_string:
447     OS << '"';
448     OS.write_escaped(Value.cstr);
449     OS << '"';
450     break;
451   case DW_FORM_exprloc:
452   case DW_FORM_block:
453   case DW_FORM_block1:
454   case DW_FORM_block2:
455   case DW_FORM_block4:
456     if (UValue > 0) {
457       switch (Form) {
458       case DW_FORM_exprloc:
459       case DW_FORM_block:
460         AddrOS << format("<0x%" PRIx64 "> ", UValue);
461         break;
462       case DW_FORM_block1:
463         AddrOS << format("<0x%2.2x> ", (uint8_t)UValue);
464         break;
465       case DW_FORM_block2:
466         AddrOS << format("<0x%4.4x> ", (uint16_t)UValue);
467         break;
468       case DW_FORM_block4:
469         AddrOS << format("<0x%8.8x> ", (uint32_t)UValue);
470         break;
471       default:
472         break;
473       }
474 
475       const uint8_t *DataPtr = Value.data;
476       if (DataPtr) {
477         // UValue contains size of block
478         const uint8_t *EndDataPtr = DataPtr + UValue;
479         while (DataPtr < EndDataPtr) {
480           AddrOS << format("%2.2x ", *DataPtr);
481           ++DataPtr;
482         }
483       } else
484         OS << "NULL";
485     }
486     break;
487 
488   case DW_FORM_sdata:
489   case DW_FORM_implicit_const:
490     OS << Value.sval;
491     break;
492   case DW_FORM_udata:
493     OS << Value.uval;
494     break;
495   case DW_FORM_strp:
496     if (DumpOpts.Verbose)
497       OS << format(" .debug_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth, UValue);
498     dumpString(OS);
499     break;
500   case DW_FORM_line_strp:
501     if (DumpOpts.Verbose)
502       OS << format(" .debug_line_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth,
503                    UValue);
504     dumpString(OS);
505     break;
506   case DW_FORM_strx:
507   case DW_FORM_strx1:
508   case DW_FORM_strx2:
509   case DW_FORM_strx3:
510   case DW_FORM_strx4:
511   case DW_FORM_GNU_str_index:
512     if (DumpOpts.Verbose)
513       OS << format("indexed (%8.8x) string = ", (uint32_t)UValue);
514     dumpString(OS);
515     break;
516   case DW_FORM_GNU_strp_alt:
517     if (DumpOpts.Verbose)
518       OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
519     dumpString(OS);
520     break;
521   case DW_FORM_ref_addr:
522     AddrOS << format("0x%016" PRIx64, UValue);
523     break;
524   case DW_FORM_ref1:
525     CURelativeOffset = true;
526     if (DumpOpts.Verbose)
527       AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
528     break;
529   case DW_FORM_ref2:
530     CURelativeOffset = true;
531     if (DumpOpts.Verbose)
532       AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
533     break;
534   case DW_FORM_ref4:
535     CURelativeOffset = true;
536     if (DumpOpts.Verbose)
537       AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
538     break;
539   case DW_FORM_ref8:
540     CURelativeOffset = true;
541     if (DumpOpts.Verbose)
542       AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
543     break;
544   case DW_FORM_ref_udata:
545     CURelativeOffset = true;
546     if (DumpOpts.Verbose)
547       AddrOS << format("cu + 0x%" PRIx64, UValue);
548     break;
549   case DW_FORM_GNU_ref_alt:
550     AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
551     break;
552 
553   // All DW_FORM_indirect attributes should be resolved prior to calling
554   // this function
555   case DW_FORM_indirect:
556     OS << "DW_FORM_indirect";
557     break;
558 
559   case DW_FORM_rnglistx:
560     OS << format("indexed (0x%x) rangelist = ", (uint32_t)UValue);
561     break;
562 
563   case DW_FORM_loclistx:
564     OS << format("indexed (0x%x) loclist = ", (uint32_t)UValue);
565     break;
566 
567   case DW_FORM_sec_offset:
568     AddrOS << format("0x%0*" PRIx64, OffsetDumpWidth, UValue);
569     break;
570 
571   default:
572     OS << format("DW_FORM(0x%4.4x)", Form);
573     break;
574   }
575 
576   if (CURelativeOffset) {
577     if (DumpOpts.Verbose)
578       OS << " => {";
579     if (DumpOpts.ShowAddresses)
580       WithColor(OS, HighlightColor::Address).get()
581           << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
582     if (DumpOpts.Verbose)
583       OS << "}";
584   }
585 }
586 
587 void DWARFFormValue::dumpString(raw_ostream &OS) const {
588   Optional<const char *> DbgStr = getAsCString();
589   if (DbgStr.hasValue()) {
590     auto COS = WithColor(OS, HighlightColor::String);
591     COS.get() << '"';
592     COS.get().write_escaped(DbgStr.getValue());
593     COS.get() << '"';
594   }
595 }
596 
597 Optional<const char *> DWARFFormValue::getAsCString() const {
598   if (!isFormClass(FC_String))
599     return None;
600   if (Form == DW_FORM_string)
601     return Value.cstr;
602   // FIXME: Add support for DW_FORM_GNU_strp_alt
603   if (Form == DW_FORM_GNU_strp_alt || C == nullptr)
604     return None;
605   uint64_t Offset = Value.uval;
606   if (Form == DW_FORM_line_strp) {
607     // .debug_line_str is tracked in the Context.
608     if (const char *Str = C->getLineStringExtractor().getCStr(&Offset))
609       return Str;
610     return None;
611   }
612   if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
613       Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
614       Form == DW_FORM_strx4) {
615     if (!U)
616       return None;
617     Optional<uint64_t> StrOffset = U->getStringOffsetSectionItem(Offset);
618     if (!StrOffset)
619       return None;
620     Offset = *StrOffset;
621   }
622   // Prefer the Unit's string extractor, because for .dwo it will point to
623   // .debug_str.dwo, while the Context's extractor always uses .debug_str.
624   if (U) {
625     if (const char *Str = U->getStringExtractor().getCStr(&Offset))
626       return Str;
627     return None;
628   }
629   if (const char *Str = C->getStringExtractor().getCStr(&Offset))
630     return Str;
631   return None;
632 }
633 
634 Optional<uint64_t> DWARFFormValue::getAsAddress() const {
635   if (auto SA = getAsSectionedAddress())
636     return SA->Address;
637   return None;
638 }
639 
640 Optional<object::SectionedAddress>
641 DWARFFormValue::getAsSectionedAddress() const {
642   if (!isFormClass(FC_Address))
643     return None;
644   if (Form == DW_FORM_GNU_addr_index || Form == DW_FORM_addrx) {
645     uint32_t Index = Value.uval;
646     if (!U)
647       return None;
648     Optional<object::SectionedAddress> SA = U->getAddrOffsetSectionItem(Index);
649     if (!SA)
650       return None;
651     return SA;
652   }
653   return {{Value.uval, Value.SectionIndex}};
654 }
655 
656 Optional<uint64_t> DWARFFormValue::getAsReference() const {
657   if (auto R = getAsRelativeReference())
658     return R->Unit ? R->Unit->getOffset() + R->Offset : R->Offset;
659   return None;
660 }
661 
662 Optional<DWARFFormValue::UnitOffset> DWARFFormValue::getAsRelativeReference() const {
663   if (!isFormClass(FC_Reference))
664     return None;
665   switch (Form) {
666   case DW_FORM_ref1:
667   case DW_FORM_ref2:
668   case DW_FORM_ref4:
669   case DW_FORM_ref8:
670   case DW_FORM_ref_udata:
671     if (!U)
672       return None;
673     return UnitOffset{const_cast<DWARFUnit*>(U), Value.uval};
674   case DW_FORM_ref_addr:
675   case DW_FORM_ref_sig8:
676   case DW_FORM_GNU_ref_alt:
677     return UnitOffset{nullptr, Value.uval};
678   default:
679     return None;
680   }
681 }
682 
683 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
684   if (!isFormClass(FC_SectionOffset))
685     return None;
686   return Value.uval;
687 }
688 
689 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
690   if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
691       Form == DW_FORM_sdata)
692     return None;
693   return Value.uval;
694 }
695 
696 Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
697   if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
698       (Form == DW_FORM_udata &&
699        uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
700     return None;
701   switch (Form) {
702   case DW_FORM_data4:
703     return int32_t(Value.uval);
704   case DW_FORM_data2:
705     return int16_t(Value.uval);
706   case DW_FORM_data1:
707     return int8_t(Value.uval);
708   case DW_FORM_sdata:
709   case DW_FORM_data8:
710   default:
711     return Value.sval;
712   }
713 }
714 
715 Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
716   if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
717       Form != DW_FORM_data16)
718     return None;
719   return makeArrayRef(Value.data, Value.uval);
720 }
721 
722 Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
723   if (!isFormClass(FC_String) && Form == DW_FORM_string)
724     return None;
725   return Value.uval;
726 }
727 
728 Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
729   if (!isFormClass(FC_Reference))
730     return None;
731   return Value.uval;
732 }
733