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