LLVM 23.0.0git
DWARFFormValue.cpp
Go to the documentation of this file.
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
10#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Format.h"
25#include <cinttypes>
26#include <cstdint>
27#include <limits>
28#include <optional>
29
30using namespace llvm;
31using namespace dwarf;
32
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
85 return DWARFFormValue(F, ValueType(V));
86}
87
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
98 ValueType V;
99 V.uval = D.size();
100 V.data = D.data();
101 return DWARFFormValue(F, V);
102}
103
105 uint64_t *OffsetPtr) {
106 DWARFFormValue FormValue(F);
107 FormValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
108 U->getFormParams(), U);
109 return FormValue;
110}
111
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(OffsetPtr);
123 *OffsetPtr += size;
124 return true;
125 }
126 case DW_FORM_block1: {
127 uint8_t size = DebugInfoData.getU8(OffsetPtr);
128 *OffsetPtr += size;
129 return true;
130 }
131 case DW_FORM_block2: {
132 uint16_t size = DebugInfoData.getU16(OffsetPtr);
133 *OffsetPtr += size;
134 return true;
135 }
136 case DW_FORM_block4: {
137 uint32_t size = DebugInfoData.getU32(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(OffsetPtr);
199 return true;
200
201 case DW_FORM_LLVM_addrx_offset:
202 DebugInfoData.getULEB128(OffsetPtr);
203 *OffsetPtr += 4;
204 return true;
205
206 case DW_FORM_indirect:
207 Indirect = true;
208 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
209 break;
210
211 default:
212 return false;
213 }
214 } while (Indirect);
215 return true;
216}
217
219 return doesFormBelongToClass(Form, FC, U ? U->getVersion() : 3);
220}
221
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, OffsetPtr, &Value.SectionIndex, &Err);
246 break;
247 }
248 case DW_FORM_exprloc:
249 case DW_FORM_block:
250 Value.uval = Data.getULEB128(OffsetPtr, &Err);
251 IsBlock = true;
252 break;
253 case DW_FORM_block1:
254 Value.uval = Data.getU8(OffsetPtr, &Err);
255 IsBlock = true;
256 break;
257 case DW_FORM_block2:
258 Value.uval = Data.getU16(OffsetPtr, &Err);
259 IsBlock = true;
260 break;
261 case DW_FORM_block4:
262 Value.uval = Data.getU32(OffsetPtr, &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(OffsetPtr, &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(OffsetPtr, &Err);
277 break;
278 case DW_FORM_strx3:
279 case DW_FORM_addrx3:
280 Value.uval = Data.getU24(OffsetPtr, &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(4, OffsetPtr, nullptr, &Err);
288 break;
289 case DW_FORM_data8:
290 case DW_FORM_ref8:
291 case DW_FORM_ref_sup8:
292 Value.uval = Data.getRelocatedValue(8, OffsetPtr, nullptr, &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);
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(OffsetPtr, &Err);
311 break;
312 case DW_FORM_LLVM_addrx_offset:
313 Value.uval = Data.getULEB128(OffsetPtr, &Err) << 32;
314 Value.uval |= Data.getU32(OffsetPtr, &Err);
315 break;
316 case DW_FORM_string:
317 Value.cstr = Data.getCStr(OffsetPtr, &Err);
318 break;
319 case DW_FORM_indirect:
320 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr, &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(FP.getDwarfOffsetByteSize(),
330 OffsetPtr, nullptr, &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(OffsetPtr, &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, Value.uval, &Err).bytes_begin();
351
352 return !errorToBool(std::move(Err));
353}
354
357 uint8_t HexDigits = AddressSize * 2;
358 OS << formatv("0x{0:x-}",
359 fmt_align(Address, AlignStyle::Right, HexDigits, '0'));
360}
361
363 DIDumpOptions DumpOpts,
364 object::SectionedAddress SA) const {
365 dumpAddress(OS, U->getAddressByteSize(), SA.Address);
366 dumpAddressSection(U->getContext().getDWARFObj(), OS, DumpOpts,
367 SA.SectionIndex);
368}
369
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(" [{0}]", SectionIndex);
383}
384
386 uint64_t UValue = Value.uval;
387 bool CURelativeOffset = false;
388 raw_ostream &AddrOS = DumpOpts.ShowAddresses
390 : nulls();
391 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
392 switch (Form) {
393 case DW_FORM_addr:
394 dumpSectionedAddress(AddrOS, DumpOpts, {Value.uval, 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("indexed ({0:x-8}) + {1:x+} address = ", Index,
413 Offset);
414 } else
415 AddrOS << formatv("indexed ({0:x-8}) address = ", (uint32_t)UValue);
416 }
417 if (A)
418 dumpSectionedAddress(AddrOS, DumpOpts, *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("{0:x+2}", (uint8_t)UValue);
429 break;
430 case DW_FORM_data2:
431 OS << formatv("{0:x+4}", (uint16_t)UValue);
432 break;
433 case DW_FORM_data4:
434 OS << formatv("{0:x+8}", (uint32_t)UValue);
435 break;
436 case DW_FORM_ref_sig8:
437 AddrOS << formatv("{0:x+16}", UValue);
438 break;
439 case DW_FORM_data8:
440 OS << formatv("{0:x+16}", UValue);
441 break;
442 case DW_FORM_data16:
443 OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), std::nullopt, 16, 16);
444 break;
445 case DW_FORM_string:
446 OS << '"';
447 OS.write_escaped(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("<{0:x+}> ", UValue);
460 break;
461 case DW_FORM_block1:
462 AddrOS << formatv("<{0:x+2}> ", (uint8_t)UValue);
463 break;
464 case DW_FORM_block2:
465 AddrOS << formatv("<{0:x+4}> ", (uint16_t)UValue);
466 break;
467 case DW_FORM_block4:
468 AddrOS << formatv("<{0:x+8}> ", (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("{0:x-2} ", *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(" .debug_str[0x{0:x-}] = ",
497 fmt_align(UValue, AlignStyle::Right, OffsetDumpWidth, '0'));
498 dumpString(OS);
499 break;
500 case DW_FORM_line_strp:
501 if (DumpOpts.Verbose)
502 OS << formatv(" .debug_line_str[0x{0:x-}] = ",
503 fmt_align(UValue, AlignStyle::Right, OffsetDumpWidth, '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("indexed ({0:x-8}) string = ", (uint32_t)UValue);
514 dumpString(OS);
515 break;
516 case DW_FORM_GNU_strp_alt:
517 if (DumpOpts.Verbose)
518 OS << formatv("alt indirect string, offset: {0:x+}", UValue);
519 dumpString(OS);
520 break;
521 case DW_FORM_ref_addr:
522 AddrOS << formatv("{0:x+16}", UValue);
523 break;
524 case DW_FORM_ref1:
525 CURelativeOffset = true;
526 if (DumpOpts.Verbose)
527 AddrOS << formatv("cu + {0:x+2}", (uint8_t)UValue);
528 break;
529 case DW_FORM_ref2:
530 CURelativeOffset = true;
531 if (DumpOpts.Verbose)
532 AddrOS << formatv("cu + {0:x+4}", (uint16_t)UValue);
533 break;
534 case DW_FORM_ref4:
535 CURelativeOffset = true;
536 if (DumpOpts.Verbose)
537 AddrOS << formatv("cu + {0:x+4}", (uint32_t)UValue);
538 break;
539 case DW_FORM_ref8:
540 CURelativeOffset = true;
541 if (DumpOpts.Verbose)
542 AddrOS << formatv("cu + {0:x+8}", UValue);
543 break;
544 case DW_FORM_ref_udata:
545 CURelativeOffset = true;
546 if (DumpOpts.Verbose)
547 AddrOS << formatv("cu + {0:x+}", UValue);
548 break;
549 case DW_FORM_GNU_ref_alt:
550 AddrOS << formatv("<alt {0:x+}>", 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("indexed ({0:x+}) rangelist = ", (uint32_t)UValue);
561 break;
562
563 case DW_FORM_loclistx:
564 OS << formatv("indexed ({0:x+}) loclist = ", (uint32_t)UValue);
565 break;
566
567 case DW_FORM_sec_offset:
568 AddrOS << formatv(
569 "0x{0:x-}", fmt_align(UValue, AlignStyle::Right, OffsetDumpWidth, '0'));
570 break;
571
572 default:
573 OS << formatv("DW_FORM(0x{0:x-4})", Form);
574 break;
575 }
576
577 if (CURelativeOffset) {
578 if (DumpOpts.Verbose)
579 OS << " => {";
580 if (DumpOpts.ShowAddresses)
582 << formatv("{0:x+8}", 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(*this)) {
590 auto COS = WithColor(OS, HighlightColor::String);
591 COS.get() << '"';
592 COS.get().write_escaped(*DbgStr);
593 COS.get() << '"';
594 }
595}
596
599 return make_error<StringError>("Invalid form for string attribute",
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>("Unsupported form for string attribute",
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>("API limitation - string extraction not "
614 "available without a DWARFUnit",
616 Expected<uint64_t> StrOffset = U->getStringOffsetSectionItem(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(&Offset))
629 return Str;
630 std::string Msg = FormEncodingString(Form).str();
631 if (Index)
632 Msg += formatv(" uses index {0}, but the referenced string", *Index).str();
633
634 Msg += formatv(" offset {0} is beyond {1} bounds", Offset,
635 (IsDebugLineString ? ".debug_line_str" : ".debug_str"))
636 .str();
637
638 return make_error<StringError>(Msg,
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_Address, 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 {{Value.uval, Value.SectionIndex}};
669}
670
671std::optional<object::SectionedAddress>
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 {
716 return std::nullopt;
717 return Value.uval;
718}
719
720std::optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
722 Form == DW_FORM_sdata)
723 return std::nullopt;
724 return Value.uval;
725}
726
727std::optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
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 {
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_String) && Form == DW_FORM_string)
755 return std::nullopt;
756 return Value.uval;
757}
758
759std::optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
761 return std::nullopt;
762 return Value.uval;
763}
764
765std::optional<std::string>
767 if (U == nullptr || !isFormClass(FC_Constant))
768 return std::nullopt;
769 DWARFUnit *DLU = const_cast<DWARFUnit *>(U)->getLinkedUnit();
770 if (auto *LT = DLU->getContext().getLineTableForUnit(DLU)) {
771 std::string FileName;
772 if (LT->getFileNameByIndex(Value.uval, DLU->getCompilationDir(), Kind,
773 FileName))
774 return FileName;
775 }
776 return std::nullopt;
777}
778
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:
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}
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static const DWARFFormValue::FormClass DWARF5FormClasses[]
This file contains constants used for implementing Dwarf debug support.
#define F(x, y, z)
Definition MD5.cpp:54
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
const DWARFDebugLine::LineTable * getLineTableForUnit(DWARFUnit *U)
Get a pointer to a parsed line table corresponding to a compile unit.
A DWARFDataExtractor (typically for an in-memory copy of an object-file section) plus a relocation ma...
static LLVM_ABI void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
static LLVM_ABI DWARFFormValue createFromPValue(dwarf::Form F, const char *V)
LLVM_ABI std::optional< uint64_t > getAsCStringOffset() const
static LLVM_ABI DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
LLVM_ABI std::optional< uint64_t > getAsSupplementaryReference() const
LLVM_ABI std::optional< std::string > getAsFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Correctly extract any file paths from a form value.
LLVM_ABI std::optional< ArrayRef< uint8_t > > getAsBlock() const
LLVM_ABI std::optional< uint64_t > getAsSectionOffset() const
LLVM_ABI void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts, object::SectionedAddress SA) const
LLVM_ABI bool isFormClass(FormClass FC) const
LLVM_ABI std::optional< object::SectionedAddress > getAsSectionedAddress() const
LLVM_ABI std::optional< uint64_t > getAsReferenceUVal() const
LLVM_ABI void dumpAddress(raw_ostream &OS, uint64_t Address) const
LLVM_ABI std::optional< int64_t > getAsSignedConstant() const
LLVM_ABI std::optional< uint64_t > getAsAddress() const
LLVM_ABI bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr, dwarf::FormParams FormParams, const DWARFContext *Context=nullptr, const DWARFUnit *Unit=nullptr)
Extracts a value in Data at offset *OffsetPtr.
LLVM_ABI std::optional< uint64_t > getAsRelativeReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
LLVM_ABI std::optional< uint64_t > getAsSignatureReference() const
LLVM_ABI std::optional< uint64_t > getAsDebugInfoReference() const
static LLVM_ABI DWARFFormValue createFromBlockValue(dwarf::Form F, ArrayRef< uint8_t > D)
LLVM_ABI void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
static LLVM_ABI DWARFFormValue createFromSValue(dwarf::Form F, int64_t V)
LLVM_ABI std::optional< uint64_t > getAsUnsignedConstant() const
bool skipValue(DataExtractor DebugInfoData, uint64_t *OffsetPtr, const dwarf::FormParams Params) const
Skip a form's value in DebugInfoData at the offset specified by OffsetPtr.
static LLVM_ABI DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr)
LLVM_ABI Expected< const char * > getAsCString() const
DWARFContext & getContext() const
Definition DWARFUnit.h:326
const char * getCompilationDir()
LLVM_ABI uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
const char * getCStr(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
LLVM_ABI uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
LLVM_ABI uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
LLVM_ABI int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a signed LEB128 value from *offset_ptr.
LLVM_ABI uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:222
An RAII object that temporarily switches an output stream to a specific color.
Definition WithColor.h:54
raw_ostream & get()
Definition WithColor.h:79
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
LLVM_ABI StringRef FormEncodingString(unsigned Encoding)
Definition Dwarf.cpp:105
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static constexpr StringLiteral SectionNames[SectionKindsNum]
Calculates the starting offsets for various sections within the .debug_names section.
Definition Dwarf.h:35
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
LLVM_ABI bool doesFormBelongToClass(dwarf::Form Form, DWARFFormValue::FormClass FC, uint16_t DwarfVersion)
Check whether specified Form belongs to the FC class.
LLVM_ABI std::optional< uint8_t > getFixedFormByteSize(dwarf::Form Form, FormParams Params)
Get the fixed byte size for a given form.
Definition Dwarf.cpp:926
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition Dwarf.h:1097
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
Definition Error.h:1113
@ Offset
Definition DWP.cpp:532
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
ArrayRef(const T &OneElt) -> ArrayRef< T >
support::detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
Container for dump options that control which debug information will be dumped.
Definition DIContext.h:196
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition Dwarf.h:1110