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