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 
createFromSValue(dwarf::Form F,int64_t V)80 DWARFFormValue DWARFFormValue::createFromSValue(dwarf::Form F, int64_t V) {
81   return DWARFFormValue(F, ValueType(V));
82 }
83 
createFromUValue(dwarf::Form F,uint64_t V)84 DWARFFormValue DWARFFormValue::createFromUValue(dwarf::Form F, uint64_t V) {
85   return DWARFFormValue(F, ValueType(V));
86 }
87 
createFromPValue(dwarf::Form F,const char * V)88 DWARFFormValue DWARFFormValue::createFromPValue(dwarf::Form F, const char *V) {
89   return DWARFFormValue(F, ValueType(V));
90 }
91 
createFromBlockValue(dwarf::Form F,ArrayRef<uint8_t> D)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 
createFromUnit(dwarf::Form F,const DWARFUnit * U,uint64_t * OffsetPtr)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 
skipValue(dwarf::Form Form,DataExtractor DebugInfoData,uint64_t * OffsetPtr,const dwarf::FormParams Params)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       if (Optional<uint8_t> FixedSize =
172               dwarf::getFixedFormByteSize(Form, Params)) {
173         *OffsetPtr += *FixedSize;
174         return true;
175       }
176       return false;
177 
178     // signed or unsigned LEB 128 values.
179     case DW_FORM_sdata:
180       DebugInfoData.getSLEB128(OffsetPtr);
181       return true;
182 
183     case DW_FORM_udata:
184     case DW_FORM_ref_udata:
185     case DW_FORM_strx:
186     case DW_FORM_addrx:
187     case DW_FORM_loclistx:
188     case DW_FORM_rnglistx:
189     case DW_FORM_GNU_addr_index:
190     case DW_FORM_GNU_str_index:
191       DebugInfoData.getULEB128(OffsetPtr);
192       return true;
193 
194     case DW_FORM_indirect:
195       Indirect = true;
196       Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
197       break;
198 
199     default:
200       return false;
201     }
202   } while (Indirect);
203   return true;
204 }
205 
isFormClass(DWARFFormValue::FormClass FC) const206 bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
207   // First, check DWARF5 form classes.
208   if (Form < makeArrayRef(DWARF5FormClasses).size() &&
209       DWARF5FormClasses[Form] == FC)
210     return true;
211   // Check more forms from extensions and proposals.
212   switch (Form) {
213   case DW_FORM_GNU_ref_alt:
214     return (FC == FC_Reference);
215   case DW_FORM_GNU_addr_index:
216     return (FC == FC_Address);
217   case DW_FORM_GNU_str_index:
218   case DW_FORM_GNU_strp_alt:
219     return (FC == FC_String);
220   default:
221     break;
222   }
223 
224   if (FC == FC_SectionOffset) {
225     if (Form == DW_FORM_strp || Form == DW_FORM_line_strp)
226       return true;
227     // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section
228     // offset. If we don't have a DWARFUnit, default to the old behavior.
229     if (Form == DW_FORM_data4 || Form == DW_FORM_data8)
230       return !U || U->getVersion() <= 3;
231   }
232 
233   return false;
234 }
235 
extractValue(const DWARFDataExtractor & Data,uint64_t * OffsetPtr,dwarf::FormParams FP,const DWARFContext * Ctx,const DWARFUnit * CU)236 bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
237                                   uint64_t *OffsetPtr, dwarf::FormParams FP,
238                                   const DWARFContext *Ctx,
239                                   const DWARFUnit *CU) {
240   if (!Ctx && CU)
241     Ctx = &CU->getContext();
242   C = Ctx;
243   U = CU;
244   Format = FP.Format;
245   bool Indirect = false;
246   bool IsBlock = false;
247   Value.data = nullptr;
248   // Read the value for the form into value and follow and DW_FORM_indirect
249   // instances we run into
250   Error Err = Error::success();
251   do {
252     Indirect = false;
253     switch (Form) {
254     case DW_FORM_addr:
255     case DW_FORM_ref_addr: {
256       uint16_t Size =
257           (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
258       Value.uval =
259           Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex, &Err);
260       break;
261     }
262     case DW_FORM_exprloc:
263     case DW_FORM_block:
264       Value.uval = Data.getULEB128(OffsetPtr, &Err);
265       IsBlock = true;
266       break;
267     case DW_FORM_block1:
268       Value.uval = Data.getU8(OffsetPtr, &Err);
269       IsBlock = true;
270       break;
271     case DW_FORM_block2:
272       Value.uval = Data.getU16(OffsetPtr, &Err);
273       IsBlock = true;
274       break;
275     case DW_FORM_block4:
276       Value.uval = Data.getU32(OffsetPtr, &Err);
277       IsBlock = true;
278       break;
279     case DW_FORM_data1:
280     case DW_FORM_ref1:
281     case DW_FORM_flag:
282     case DW_FORM_strx1:
283     case DW_FORM_addrx1:
284       Value.uval = Data.getU8(OffsetPtr, &Err);
285       break;
286     case DW_FORM_data2:
287     case DW_FORM_ref2:
288     case DW_FORM_strx2:
289     case DW_FORM_addrx2:
290       Value.uval = Data.getU16(OffsetPtr, &Err);
291       break;
292     case DW_FORM_strx3:
293       Value.uval = Data.getU24(OffsetPtr, &Err);
294       break;
295     case DW_FORM_data4:
296     case DW_FORM_ref4:
297     case DW_FORM_ref_sup4:
298     case DW_FORM_strx4:
299     case DW_FORM_addrx4:
300       Value.uval = Data.getRelocatedValue(4, OffsetPtr, nullptr, &Err);
301       break;
302     case DW_FORM_data8:
303     case DW_FORM_ref8:
304     case DW_FORM_ref_sup8:
305       Value.uval = Data.getRelocatedValue(8, OffsetPtr, nullptr, &Err);
306       break;
307     case DW_FORM_data16:
308       // Treat this like a 16-byte block.
309       Value.uval = 16;
310       IsBlock = true;
311       break;
312     case DW_FORM_sdata:
313       Value.sval = Data.getSLEB128(OffsetPtr, &Err);
314       break;
315     case DW_FORM_udata:
316     case DW_FORM_ref_udata:
317     case DW_FORM_rnglistx:
318     case DW_FORM_loclistx:
319     case DW_FORM_GNU_addr_index:
320     case DW_FORM_GNU_str_index:
321     case DW_FORM_addrx:
322     case DW_FORM_strx:
323       Value.uval = Data.getULEB128(OffsetPtr, &Err);
324       break;
325     case DW_FORM_string:
326       Value.cstr = Data.getCStr(OffsetPtr, &Err);
327       break;
328     case DW_FORM_indirect:
329       Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr, &Err));
330       Indirect = true;
331       break;
332     case DW_FORM_strp:
333     case DW_FORM_sec_offset:
334     case DW_FORM_GNU_ref_alt:
335     case DW_FORM_GNU_strp_alt:
336     case DW_FORM_line_strp:
337     case DW_FORM_strp_sup: {
338       Value.uval = Data.getRelocatedValue(FP.getDwarfOffsetByteSize(),
339                                           OffsetPtr, nullptr, &Err);
340       break;
341     }
342     case DW_FORM_flag_present:
343       Value.uval = 1;
344       break;
345     case DW_FORM_ref_sig8:
346       Value.uval = Data.getU64(OffsetPtr, &Err);
347       break;
348     default:
349       // DWARFFormValue::skipValue() will have caught this and caused all
350       // DWARF DIEs to fail to be parsed, so this code is not be reachable.
351       llvm_unreachable("unsupported form");
352     }
353   } while (Indirect && !Err);
354 
355   if (IsBlock)
356     Value.data = Data.getBytes(OffsetPtr, Value.uval, &Err).bytes_begin();
357 
358   return !errorToBool(std::move(Err));
359 }
360 
dumpAddress(raw_ostream & OS,uint8_t AddressSize,uint64_t Address)361 void DWARFFormValue::dumpAddress(raw_ostream &OS, uint8_t AddressSize,
362                                  uint64_t Address) {
363   uint8_t HexDigits = AddressSize * 2;
364   OS << format("0x%*.*" PRIx64, HexDigits, HexDigits, Address);
365 }
366 
dumpSectionedAddress(raw_ostream & OS,DIDumpOptions DumpOpts,object::SectionedAddress SA) const367 void DWARFFormValue::dumpSectionedAddress(raw_ostream &OS,
368                                           DIDumpOptions DumpOpts,
369                                           object::SectionedAddress SA) const {
370   dumpAddress(OS, U->getAddressByteSize(), SA.Address);
371   dumpAddressSection(U->getContext().getDWARFObj(), OS, DumpOpts,
372                      SA.SectionIndex);
373 }
374 
dumpAddressSection(const DWARFObject & Obj,raw_ostream & OS,DIDumpOptions DumpOpts,uint64_t SectionIndex)375 void DWARFFormValue::dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
376                                         DIDumpOptions DumpOpts,
377                                         uint64_t SectionIndex) {
378   if (!DumpOpts.Verbose || SectionIndex == -1ULL)
379     return;
380   ArrayRef<SectionName> SectionNames = Obj.getSectionNames();
381   const auto &SecRef = SectionNames[SectionIndex];
382 
383   OS << " \"" << SecRef.Name << '\"';
384 
385   // Print section index if name is not unique.
386   if (!SecRef.IsNameUnique)
387     OS << format(" [%" PRIu64 "]", SectionIndex);
388 }
389 
dump(raw_ostream & OS,DIDumpOptions DumpOpts) const390 void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
391   uint64_t UValue = Value.uval;
392   bool CURelativeOffset = false;
393   raw_ostream &AddrOS = DumpOpts.ShowAddresses
394                             ? WithColor(OS, HighlightColor::Address).get()
395                             : nulls();
396   int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
397   switch (Form) {
398   case DW_FORM_addr:
399     dumpSectionedAddress(AddrOS, DumpOpts, {Value.uval, Value.SectionIndex});
400     break;
401   case DW_FORM_addrx:
402   case DW_FORM_addrx1:
403   case DW_FORM_addrx2:
404   case DW_FORM_addrx3:
405   case DW_FORM_addrx4:
406   case DW_FORM_GNU_addr_index: {
407     if (U == nullptr) {
408       OS << "<invalid dwarf unit>";
409       break;
410     }
411     Optional<object::SectionedAddress> A = U->getAddrOffsetSectionItem(UValue);
412     if (!A || DumpOpts.Verbose)
413       AddrOS << format("indexed (%8.8x) address = ", (uint32_t)UValue);
414     if (A)
415       dumpSectionedAddress(AddrOS, DumpOpts, *A);
416     else
417       OS << "<unresolved>";
418     break;
419   }
420   case DW_FORM_flag_present:
421     OS << "true";
422     break;
423   case DW_FORM_flag:
424   case DW_FORM_data1:
425     OS << format("0x%02x", (uint8_t)UValue);
426     break;
427   case DW_FORM_data2:
428     OS << format("0x%04x", (uint16_t)UValue);
429     break;
430   case DW_FORM_data4:
431     OS << format("0x%08x", (uint32_t)UValue);
432     break;
433   case DW_FORM_ref_sig8:
434     AddrOS << format("0x%016" PRIx64, UValue);
435     break;
436   case DW_FORM_data8:
437     OS << format("0x%016" PRIx64, UValue);
438     break;
439   case DW_FORM_data16:
440     OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
441     break;
442   case DW_FORM_string:
443     OS << '"';
444     OS.write_escaped(Value.cstr);
445     OS << '"';
446     break;
447   case DW_FORM_exprloc:
448   case DW_FORM_block:
449   case DW_FORM_block1:
450   case DW_FORM_block2:
451   case DW_FORM_block4:
452     if (UValue > 0) {
453       switch (Form) {
454       case DW_FORM_exprloc:
455       case DW_FORM_block:
456         AddrOS << format("<0x%" PRIx64 "> ", UValue);
457         break;
458       case DW_FORM_block1:
459         AddrOS << format("<0x%2.2x> ", (uint8_t)UValue);
460         break;
461       case DW_FORM_block2:
462         AddrOS << format("<0x%4.4x> ", (uint16_t)UValue);
463         break;
464       case DW_FORM_block4:
465         AddrOS << format("<0x%8.8x> ", (uint32_t)UValue);
466         break;
467       default:
468         break;
469       }
470 
471       const uint8_t *DataPtr = Value.data;
472       if (DataPtr) {
473         // UValue contains size of block
474         const uint8_t *EndDataPtr = DataPtr + UValue;
475         while (DataPtr < EndDataPtr) {
476           AddrOS << format("%2.2x ", *DataPtr);
477           ++DataPtr;
478         }
479       } else
480         OS << "NULL";
481     }
482     break;
483 
484   case DW_FORM_sdata:
485     OS << Value.sval;
486     break;
487   case DW_FORM_udata:
488     OS << Value.uval;
489     break;
490   case DW_FORM_strp:
491     if (DumpOpts.Verbose)
492       OS << format(" .debug_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth, UValue);
493     dumpString(OS);
494     break;
495   case DW_FORM_line_strp:
496     if (DumpOpts.Verbose)
497       OS << format(" .debug_line_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth,
498                    UValue);
499     dumpString(OS);
500     break;
501   case DW_FORM_strx:
502   case DW_FORM_strx1:
503   case DW_FORM_strx2:
504   case DW_FORM_strx3:
505   case DW_FORM_strx4:
506   case DW_FORM_GNU_str_index:
507     if (DumpOpts.Verbose)
508       OS << format("indexed (%8.8x) string = ", (uint32_t)UValue);
509     dumpString(OS);
510     break;
511   case DW_FORM_GNU_strp_alt:
512     if (DumpOpts.Verbose)
513       OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
514     dumpString(OS);
515     break;
516   case DW_FORM_ref_addr:
517     AddrOS << format("0x%016" PRIx64, UValue);
518     break;
519   case DW_FORM_ref1:
520     CURelativeOffset = true;
521     if (DumpOpts.Verbose)
522       AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
523     break;
524   case DW_FORM_ref2:
525     CURelativeOffset = true;
526     if (DumpOpts.Verbose)
527       AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
528     break;
529   case DW_FORM_ref4:
530     CURelativeOffset = true;
531     if (DumpOpts.Verbose)
532       AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
533     break;
534   case DW_FORM_ref8:
535     CURelativeOffset = true;
536     if (DumpOpts.Verbose)
537       AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
538     break;
539   case DW_FORM_ref_udata:
540     CURelativeOffset = true;
541     if (DumpOpts.Verbose)
542       AddrOS << format("cu + 0x%" PRIx64, UValue);
543     break;
544   case DW_FORM_GNU_ref_alt:
545     AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
546     break;
547 
548   // All DW_FORM_indirect attributes should be resolved prior to calling
549   // this function
550   case DW_FORM_indirect:
551     OS << "DW_FORM_indirect";
552     break;
553 
554   case DW_FORM_rnglistx:
555     OS << format("indexed (0x%x) rangelist = ", (uint32_t)UValue);
556     break;
557 
558   case DW_FORM_loclistx:
559     OS << format("indexed (0x%x) loclist = ", (uint32_t)UValue);
560     break;
561 
562   case DW_FORM_sec_offset:
563     AddrOS << format("0x%0*" PRIx64, OffsetDumpWidth, UValue);
564     break;
565 
566   default:
567     OS << format("DW_FORM(0x%4.4x)", Form);
568     break;
569   }
570 
571   if (CURelativeOffset) {
572     if (DumpOpts.Verbose)
573       OS << " => {";
574     if (DumpOpts.ShowAddresses)
575       WithColor(OS, HighlightColor::Address).get()
576           << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
577     if (DumpOpts.Verbose)
578       OS << "}";
579   }
580 }
581 
dumpString(raw_ostream & OS) const582 void DWARFFormValue::dumpString(raw_ostream &OS) const {
583   Optional<const char *> DbgStr = getAsCString();
584   if (DbgStr.hasValue()) {
585     auto COS = WithColor(OS, HighlightColor::String);
586     COS.get() << '"';
587     COS.get().write_escaped(DbgStr.getValue());
588     COS.get() << '"';
589   }
590 }
591 
getAsCString() const592 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 || C == nullptr)
599     return None;
600   uint64_t Offset = Value.uval;
601   if (Form == DW_FORM_line_strp) {
602     // .debug_line_str is tracked in the Context.
603     if (const char *Str = C->getLineStringExtractor().getCStr(&Offset))
604       return Str;
605     return None;
606   }
607   if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
608       Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
609       Form == DW_FORM_strx4) {
610     if (!U)
611       return None;
612     Optional<uint64_t> StrOffset = U->getStringOffsetSectionItem(Offset);
613     if (!StrOffset)
614       return None;
615     Offset = *StrOffset;
616   }
617   // Prefer the Unit's string extractor, because for .dwo it will point to
618   // .debug_str.dwo, while the Context's extractor always uses .debug_str.
619   if (U) {
620     if (const char *Str = U->getStringExtractor().getCStr(&Offset))
621       return Str;
622     return None;
623   }
624   if (const char *Str = C->getStringExtractor().getCStr(&Offset))
625     return Str;
626   return None;
627 }
628 
getAsAddress() const629 Optional<uint64_t> DWARFFormValue::getAsAddress() const {
630   if (auto SA = getAsSectionedAddress())
631     return SA->Address;
632   return None;
633 }
634 
635 Optional<object::SectionedAddress>
getAsSectionedAddress() const636 DWARFFormValue::getAsSectionedAddress() const {
637   if (!isFormClass(FC_Address))
638     return None;
639   if (Form == DW_FORM_GNU_addr_index || Form == DW_FORM_addrx) {
640     uint32_t Index = Value.uval;
641     if (!U)
642       return None;
643     Optional<object::SectionedAddress> SA = U->getAddrOffsetSectionItem(Index);
644     if (!SA)
645       return None;
646     return SA;
647   }
648   return {{Value.uval, Value.SectionIndex}};
649 }
650 
getAsReference() const651 Optional<uint64_t> DWARFFormValue::getAsReference() const {
652   if (auto R = getAsRelativeReference())
653     return R->Unit ? R->Unit->getOffset() + R->Offset : R->Offset;
654   return None;
655 }
656 
getAsRelativeReference() const657 Optional<DWARFFormValue::UnitOffset> DWARFFormValue::getAsRelativeReference() const {
658   if (!isFormClass(FC_Reference))
659     return None;
660   switch (Form) {
661   case DW_FORM_ref1:
662   case DW_FORM_ref2:
663   case DW_FORM_ref4:
664   case DW_FORM_ref8:
665   case DW_FORM_ref_udata:
666     if (!U)
667       return None;
668     return UnitOffset{const_cast<DWARFUnit*>(U), Value.uval};
669   case DW_FORM_ref_addr:
670   case DW_FORM_ref_sig8:
671   case DW_FORM_GNU_ref_alt:
672     return UnitOffset{nullptr, Value.uval};
673   default:
674     return None;
675   }
676 }
677 
getAsSectionOffset() const678 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
679   if (!isFormClass(FC_SectionOffset))
680     return None;
681   return Value.uval;
682 }
683 
getAsUnsignedConstant() const684 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
685   if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
686       Form == DW_FORM_sdata)
687     return None;
688   return Value.uval;
689 }
690 
getAsSignedConstant() const691 Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
692   if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
693       (Form == DW_FORM_udata &&
694        uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
695     return None;
696   switch (Form) {
697   case DW_FORM_data4:
698     return int32_t(Value.uval);
699   case DW_FORM_data2:
700     return int16_t(Value.uval);
701   case DW_FORM_data1:
702     return int8_t(Value.uval);
703   case DW_FORM_sdata:
704   case DW_FORM_data8:
705   default:
706     return Value.sval;
707   }
708 }
709 
getAsBlock() const710 Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
711   if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
712       Form != DW_FORM_data16)
713     return None;
714   return makeArrayRef(Value.data, Value.uval);
715 }
716 
getAsCStringOffset() const717 Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
718   if (!isFormClass(FC_String) && Form == DW_FORM_string)
719     return None;
720   return Value.uval;
721 }
722 
getAsReferenceUVal() const723 Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
724   if (!isFormClass(FC_Reference))
725     return None;
726   return Value.uval;
727 }
728