LLVM  16.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/None.h"
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/WithColor.h"
25 #include <cinttypes>
26 #include <cstdint>
27 #include <limits>
28 #include <optional>
29 
30 using namespace llvm;
31 using 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 
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_strx4:
166  case DW_FORM_addrx1:
167  case DW_FORM_addrx2:
168  case DW_FORM_addrx4:
169  case DW_FORM_sec_offset:
170  case DW_FORM_strp:
171  case DW_FORM_strp_sup:
172  case DW_FORM_line_strp:
173  case DW_FORM_GNU_ref_alt:
174  case DW_FORM_GNU_strp_alt:
175  case DW_FORM_implicit_const:
176  if (Optional<uint8_t> FixedSize =
178  *OffsetPtr += *FixedSize;
179  return true;
180  }
181  return false;
182 
183  // signed or unsigned LEB 128 values.
184  case DW_FORM_sdata:
185  DebugInfoData.getSLEB128(OffsetPtr);
186  return true;
187 
188  case DW_FORM_udata:
189  case DW_FORM_ref_udata:
190  case DW_FORM_strx:
191  case DW_FORM_addrx:
192  case DW_FORM_loclistx:
193  case DW_FORM_rnglistx:
194  case DW_FORM_GNU_addr_index:
195  case DW_FORM_GNU_str_index:
196  DebugInfoData.getULEB128(OffsetPtr);
197  return true;
198 
199  case DW_FORM_LLVM_addrx_offset:
200  DebugInfoData.getULEB128(OffsetPtr);
201  *OffsetPtr += 4;
202  return true;
203 
204  case DW_FORM_indirect:
205  Indirect = true;
206  Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
207  break;
208 
209  default:
210  return false;
211  }
212  } while (Indirect);
213  return true;
214 }
215 
217  // First, check DWARF5 form classes.
220  return true;
221  // Check more forms from extensions and proposals.
222  switch (Form) {
223  case DW_FORM_GNU_ref_alt:
224  return (FC == FC_Reference);
225  case DW_FORM_GNU_addr_index:
226  return (FC == FC_Address);
227  case DW_FORM_GNU_str_index:
228  case DW_FORM_GNU_strp_alt:
229  return (FC == FC_String);
230  case DW_FORM_LLVM_addrx_offset:
231  return (FC == FC_Address);
232  default:
233  break;
234  }
235 
236  if (FC == FC_SectionOffset) {
237  if (Form == DW_FORM_strp || Form == DW_FORM_line_strp)
238  return true;
239  // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section
240  // offset. If we don't have a DWARFUnit, default to the old behavior.
241  if (Form == DW_FORM_data4 || Form == DW_FORM_data8)
242  return !U || U->getVersion() <= 3;
243  }
244 
245  return false;
246 }
247 
249  uint64_t *OffsetPtr, dwarf::FormParams FP,
250  const DWARFContext *Ctx,
251  const DWARFUnit *CU) {
252  if (!Ctx && CU)
253  Ctx = &CU->getContext();
254  C = Ctx;
255  U = CU;
256  Format = FP.Format;
257  bool Indirect = false;
258  bool IsBlock = false;
259  Value.data = nullptr;
260  // Read the value for the form into value and follow and DW_FORM_indirect
261  // instances we run into
262  Error Err = Error::success();
263  do {
264  Indirect = false;
265  switch (Form) {
266  case DW_FORM_addr:
267  case DW_FORM_ref_addr: {
268  uint16_t Size =
269  (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
270  Value.uval =
271  Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex, &Err);
272  break;
273  }
274  case DW_FORM_exprloc:
275  case DW_FORM_block:
276  Value.uval = Data.getULEB128(OffsetPtr, &Err);
277  IsBlock = true;
278  break;
279  case DW_FORM_block1:
280  Value.uval = Data.getU8(OffsetPtr, &Err);
281  IsBlock = true;
282  break;
283  case DW_FORM_block2:
284  Value.uval = Data.getU16(OffsetPtr, &Err);
285  IsBlock = true;
286  break;
287  case DW_FORM_block4:
288  Value.uval = Data.getU32(OffsetPtr, &Err);
289  IsBlock = true;
290  break;
291  case DW_FORM_data1:
292  case DW_FORM_ref1:
293  case DW_FORM_flag:
294  case DW_FORM_strx1:
295  case DW_FORM_addrx1:
296  Value.uval = Data.getU8(OffsetPtr, &Err);
297  break;
298  case DW_FORM_data2:
299  case DW_FORM_ref2:
300  case DW_FORM_strx2:
301  case DW_FORM_addrx2:
302  Value.uval = Data.getU16(OffsetPtr, &Err);
303  break;
304  case DW_FORM_strx3:
305  Value.uval = Data.getU24(OffsetPtr, &Err);
306  break;
307  case DW_FORM_data4:
308  case DW_FORM_ref4:
309  case DW_FORM_ref_sup4:
310  case DW_FORM_strx4:
311  case DW_FORM_addrx4:
312  Value.uval = Data.getRelocatedValue(4, OffsetPtr, nullptr, &Err);
313  break;
314  case DW_FORM_data8:
315  case DW_FORM_ref8:
316  case DW_FORM_ref_sup8:
317  Value.uval = Data.getRelocatedValue(8, OffsetPtr, nullptr, &Err);
318  break;
319  case DW_FORM_data16:
320  // Treat this like a 16-byte block.
321  Value.uval = 16;
322  IsBlock = true;
323  break;
324  case DW_FORM_sdata:
325  Value.sval = Data.getSLEB128(OffsetPtr, &Err);
326  break;
327  case DW_FORM_udata:
328  case DW_FORM_ref_udata:
329  case DW_FORM_rnglistx:
330  case DW_FORM_loclistx:
331  case DW_FORM_GNU_addr_index:
332  case DW_FORM_GNU_str_index:
333  case DW_FORM_addrx:
334  case DW_FORM_strx:
335  Value.uval = Data.getULEB128(OffsetPtr, &Err);
336  break;
337  case DW_FORM_LLVM_addrx_offset:
338  Value.uval = Data.getULEB128(OffsetPtr, &Err) << 32;
339  Value.uval |= Data.getU32(OffsetPtr, &Err);
340  break;
341  case DW_FORM_string:
342  Value.cstr = Data.getCStr(OffsetPtr, &Err);
343  break;
344  case DW_FORM_indirect:
345  Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr, &Err));
346  Indirect = true;
347  break;
348  case DW_FORM_strp:
349  case DW_FORM_sec_offset:
350  case DW_FORM_GNU_ref_alt:
351  case DW_FORM_GNU_strp_alt:
352  case DW_FORM_line_strp:
353  case DW_FORM_strp_sup: {
354  Value.uval = Data.getRelocatedValue(FP.getDwarfOffsetByteSize(),
355  OffsetPtr, nullptr, &Err);
356  break;
357  }
358  case DW_FORM_flag_present:
359  Value.uval = 1;
360  break;
361  case DW_FORM_ref_sig8:
362  Value.uval = Data.getU64(OffsetPtr, &Err);
363  break;
364  case DW_FORM_implicit_const:
365  // Value has been already set by DWARFFormValue::createFromSValue.
366  break;
367  default:
368  // DWARFFormValue::skipValue() will have caught this and caused all
369  // DWARF DIEs to fail to be parsed, so this code is not be reachable.
370  llvm_unreachable("unsupported form");
371  }
372  } while (Indirect && !Err);
373 
374  if (IsBlock)
375  Value.data = Data.getBytes(OffsetPtr, Value.uval, &Err).bytes_begin();
376 
377  return !errorToBool(std::move(Err));
378 }
379 
380 void DWARFFormValue::dumpAddress(raw_ostream &OS, uint8_t AddressSize,
381  uint64_t Address) {
382  uint8_t HexDigits = AddressSize * 2;
383  OS << format("0x%*.*" PRIx64, HexDigits, HexDigits, Address);
384 }
385 
387  DIDumpOptions DumpOpts,
388  object::SectionedAddress SA) const {
389  dumpAddress(OS, U->getAddressByteSize(), SA.Address);
390  dumpAddressSection(U->getContext().getDWARFObj(), OS, DumpOpts,
391  SA.SectionIndex);
392 }
393 
395  DIDumpOptions DumpOpts,
396  uint64_t SectionIndex) {
397  if (!DumpOpts.Verbose || SectionIndex == -1ULL)
398  return;
399  ArrayRef<SectionName> SectionNames = Obj.getSectionNames();
400  const auto &SecRef = SectionNames[SectionIndex];
401 
402  OS << " \"" << SecRef.Name << '\"';
403 
404  // Print section index if name is not unique.
405  if (!SecRef.IsNameUnique)
406  OS << format(" [%" PRIu64 "]", SectionIndex);
407 }
408 
409 void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
410  uint64_t UValue = Value.uval;
411  bool CURelativeOffset = false;
412  raw_ostream &AddrOS = DumpOpts.ShowAddresses
414  : nulls();
415  int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
416  switch (Form) {
417  case DW_FORM_addr:
418  dumpSectionedAddress(AddrOS, DumpOpts, {Value.uval, Value.SectionIndex});
419  break;
420  case DW_FORM_addrx:
421  case DW_FORM_addrx1:
422  case DW_FORM_addrx2:
423  case DW_FORM_addrx3:
424  case DW_FORM_addrx4:
425  case DW_FORM_GNU_addr_index: {
426  if (U == nullptr) {
427  OS << "<invalid dwarf unit>";
428  break;
429  }
430  Optional<object::SectionedAddress> A = U->getAddrOffsetSectionItem(UValue);
431  if (!A || DumpOpts.Verbose)
432  AddrOS << format("indexed (%8.8x) address = ", (uint32_t)UValue);
433  if (A)
434  dumpSectionedAddress(AddrOS, DumpOpts, *A);
435  else
436  OS << "<unresolved>";
437  break;
438  }
439  case DW_FORM_LLVM_addrx_offset: {
440  if (U == nullptr) {
441  OS << "<invalid dwarf unit>";
442  break;
443  }
444  uint32_t Index = UValue >> 32;
445  uint32_t Offset = UValue & 0xffffffff;
446  Optional<object::SectionedAddress> A = U->getAddrOffsetSectionItem(Index);
447  if (!A || DumpOpts.Verbose)
448  AddrOS << format("indexed (%8.8x) + 0x%x address = ", Index, Offset);
449  if (A) {
450  A->Address += Offset;
451  dumpSectionedAddress(AddrOS, DumpOpts, *A);
452  } else
453  OS << "<unresolved>";
454  break;
455  }
456  case DW_FORM_flag_present:
457  OS << "true";
458  break;
459  case DW_FORM_flag:
460  case DW_FORM_data1:
461  OS << format("0x%02x", (uint8_t)UValue);
462  break;
463  case DW_FORM_data2:
464  OS << format("0x%04x", (uint16_t)UValue);
465  break;
466  case DW_FORM_data4:
467  OS << format("0x%08x", (uint32_t)UValue);
468  break;
469  case DW_FORM_ref_sig8:
470  AddrOS << format("0x%016" PRIx64, UValue);
471  break;
472  case DW_FORM_data8:
473  OS << format("0x%016" PRIx64, UValue);
474  break;
475  case DW_FORM_data16:
476  OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
477  break;
478  case DW_FORM_string:
479  OS << '"';
480  OS.write_escaped(Value.cstr);
481  OS << '"';
482  break;
483  case DW_FORM_exprloc:
484  case DW_FORM_block:
485  case DW_FORM_block1:
486  case DW_FORM_block2:
487  case DW_FORM_block4:
488  if (UValue > 0) {
489  switch (Form) {
490  case DW_FORM_exprloc:
491  case DW_FORM_block:
492  AddrOS << format("<0x%" PRIx64 "> ", UValue);
493  break;
494  case DW_FORM_block1:
495  AddrOS << format("<0x%2.2x> ", (uint8_t)UValue);
496  break;
497  case DW_FORM_block2:
498  AddrOS << format("<0x%4.4x> ", (uint16_t)UValue);
499  break;
500  case DW_FORM_block4:
501  AddrOS << format("<0x%8.8x> ", (uint32_t)UValue);
502  break;
503  default:
504  break;
505  }
506 
507  const uint8_t *DataPtr = Value.data;
508  if (DataPtr) {
509  // UValue contains size of block
510  const uint8_t *EndDataPtr = DataPtr + UValue;
511  while (DataPtr < EndDataPtr) {
512  AddrOS << format("%2.2x ", *DataPtr);
513  ++DataPtr;
514  }
515  } else
516  OS << "NULL";
517  }
518  break;
519 
520  case DW_FORM_sdata:
521  case DW_FORM_implicit_const:
522  OS << Value.sval;
523  break;
524  case DW_FORM_udata:
525  OS << Value.uval;
526  break;
527  case DW_FORM_strp:
528  if (DumpOpts.Verbose)
529  OS << format(" .debug_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth, UValue);
530  dumpString(OS);
531  break;
532  case DW_FORM_line_strp:
533  if (DumpOpts.Verbose)
534  OS << format(" .debug_line_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth,
535  UValue);
536  dumpString(OS);
537  break;
538  case DW_FORM_strx:
539  case DW_FORM_strx1:
540  case DW_FORM_strx2:
541  case DW_FORM_strx3:
542  case DW_FORM_strx4:
543  case DW_FORM_GNU_str_index:
544  if (DumpOpts.Verbose)
545  OS << format("indexed (%8.8x) string = ", (uint32_t)UValue);
546  dumpString(OS);
547  break;
548  case DW_FORM_GNU_strp_alt:
549  if (DumpOpts.Verbose)
550  OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
551  dumpString(OS);
552  break;
553  case DW_FORM_ref_addr:
554  AddrOS << format("0x%016" PRIx64, UValue);
555  break;
556  case DW_FORM_ref1:
557  CURelativeOffset = true;
558  if (DumpOpts.Verbose)
559  AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
560  break;
561  case DW_FORM_ref2:
562  CURelativeOffset = true;
563  if (DumpOpts.Verbose)
564  AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
565  break;
566  case DW_FORM_ref4:
567  CURelativeOffset = true;
568  if (DumpOpts.Verbose)
569  AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
570  break;
571  case DW_FORM_ref8:
572  CURelativeOffset = true;
573  if (DumpOpts.Verbose)
574  AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
575  break;
576  case DW_FORM_ref_udata:
577  CURelativeOffset = true;
578  if (DumpOpts.Verbose)
579  AddrOS << format("cu + 0x%" PRIx64, UValue);
580  break;
581  case DW_FORM_GNU_ref_alt:
582  AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
583  break;
584 
585  // All DW_FORM_indirect attributes should be resolved prior to calling
586  // this function
587  case DW_FORM_indirect:
588  OS << "DW_FORM_indirect";
589  break;
590 
591  case DW_FORM_rnglistx:
592  OS << format("indexed (0x%x) rangelist = ", (uint32_t)UValue);
593  break;
594 
595  case DW_FORM_loclistx:
596  OS << format("indexed (0x%x) loclist = ", (uint32_t)UValue);
597  break;
598 
599  case DW_FORM_sec_offset:
600  AddrOS << format("0x%0*" PRIx64, OffsetDumpWidth, UValue);
601  break;
602 
603  default:
604  OS << format("DW_FORM(0x%4.4x)", Form);
605  break;
606  }
607 
608  if (CURelativeOffset) {
609  if (DumpOpts.Verbose)
610  OS << " => {";
611  if (DumpOpts.ShowAddresses)
613  << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
614  if (DumpOpts.Verbose)
615  OS << "}";
616  }
617 }
618 
619 void DWARFFormValue::dumpString(raw_ostream &OS) const {
620  if (auto DbgStr = dwarf::toString(*this)) {
621  auto COS = WithColor(OS, HighlightColor::String);
622  COS.get() << '"';
623  COS.get().write_escaped(*DbgStr);
624  COS.get() << '"';
625  }
626 }
627 
629  if (!isFormClass(FC_String))
630  return make_error<StringError>("Invalid form for string attribute",
632  if (Form == DW_FORM_string)
633  return Value.cstr;
634  // FIXME: Add support for DW_FORM_GNU_strp_alt
635  if (Form == DW_FORM_GNU_strp_alt || C == nullptr)
636  return make_error<StringError>("Unsupported form for string attribute",
638  uint64_t Offset = Value.uval;
639  std::optional<uint32_t> Index;
640  if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
641  Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
642  Form == DW_FORM_strx4) {
643  if (!U)
644  return make_error<StringError>("API limitation - string extraction not "
645  "available without a DWARFUnit",
647  Expected<uint64_t> StrOffset = U->getStringOffsetSectionItem(Offset);
648  Index = Offset;
649  if (!StrOffset)
650  return StrOffset.takeError();
651  Offset = *StrOffset;
652  }
653  // Prefer the Unit's string extractor, because for .dwo it will point to
654  // .debug_str.dwo, while the Context's extractor always uses .debug_str.
655  DataExtractor StrData = Form == DW_FORM_line_strp
656  ? C->getLineStringExtractor()
657  : U ? U->getStringExtractor()
658  : C->getStringExtractor();
659  if (const char *Str = StrData.getCStr(&Offset))
660  return Str;
661  std::string Msg = FormEncodingString(Form).str();
662  if (Index)
663  Msg += (" uses index " + Twine(*Index) + ", but the referenced string").str();
664  Msg += (" offset " + Twine(Offset) + " is beyond .debug_str bounds").str();
665  return make_error<StringError>(Msg,
667 }
668 
670  if (auto SA = getAsSectionedAddress())
671  return SA->Address;
672  return None;
673 }
674 
677  if (!isFormClass(FC_Address))
678  return None;
679  bool AddrOffset = Form == dwarf::DW_FORM_LLVM_addrx_offset;
680  if (Form == DW_FORM_GNU_addr_index || Form == DW_FORM_addrx || AddrOffset) {
681 
682  uint32_t Index = AddrOffset ? (Value.uval >> 32) : Value.uval;
683  if (!U)
684  return None;
685  Optional<object::SectionedAddress> SA = U->getAddrOffsetSectionItem(Index);
686  if (!SA)
687  return None;
688  if (AddrOffset)
689  SA->Address += (Value.uval & 0xffffffff);
690  return SA;
691  }
692  return {{Value.uval, Value.SectionIndex}};
693 }
694 
696  if (auto R = getAsRelativeReference())
697  return R->Unit ? R->Unit->getOffset() + R->Offset : R->Offset;
698  return None;
699 }
700 
702  if (!isFormClass(FC_Reference))
703  return None;
704  switch (Form) {
705  case DW_FORM_ref1:
706  case DW_FORM_ref2:
707  case DW_FORM_ref4:
708  case DW_FORM_ref8:
709  case DW_FORM_ref_udata:
710  if (!U)
711  return None;
712  return UnitOffset{const_cast<DWARFUnit*>(U), Value.uval};
713  case DW_FORM_ref_addr:
714  case DW_FORM_ref_sig8:
715  case DW_FORM_GNU_ref_alt:
716  return UnitOffset{nullptr, Value.uval};
717  default:
718  return None;
719  }
720 }
721 
723  if (!isFormClass(FC_SectionOffset))
724  return None;
725  return Value.uval;
726 }
727 
729  if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
730  Form == DW_FORM_sdata)
731  return None;
732  return Value.uval;
733 }
734 
736  if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
737  (Form == DW_FORM_udata &&
739  return None;
740  switch (Form) {
741  case DW_FORM_data4:
742  return int32_t(Value.uval);
743  case DW_FORM_data2:
744  return int16_t(Value.uval);
745  case DW_FORM_data1:
746  return int8_t(Value.uval);
747  case DW_FORM_sdata:
748  case DW_FORM_data8:
749  default:
750  return Value.sval;
751  }
752 }
753 
755  if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
756  Form != DW_FORM_data16)
757  return None;
758  return makeArrayRef(Value.data, Value.uval);
759 }
760 
762  if (!isFormClass(FC_String) && Form == DW_FORM_string)
763  return None;
764  return Value.uval;
765 }
766 
768  if (!isFormClass(FC_Reference))
769  return None;
770  return Value.uval;
771 }
772 
775  if (U == nullptr || !isFormClass(FC_Constant))
776  return None;
777  DWARFUnit *DLU = const_cast<DWARFUnit *>(U)->getLinkedUnit();
778  if (auto *LT = DLU->getContext().getLineTableForUnit(DLU)) {
779  std::string FileName;
780  if (LT->getFileNameByIndex(Value.uval, DLU->getCompilationDir(), Kind,
781  FileName))
782  return FileName;
783  }
784  return None;
785 }
DWARFFormValue.h
CmpMode::FP
@ FP
llvm::DWARFContext::getLineTableForUnit
const DWARFDebugLine::LineTable * getLineTableForUnit(DWARFUnit *U)
Get a pointer to a parsed line table corresponding to a compile unit.
Definition: DWARFContext.cpp:967
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Optional.h
llvm::DWARFFormValue::getAsSectionedAddress
Optional< object::SectionedAddress > getAsSectionedAddress() const
Definition: DWARFFormValue.cpp:676
StringRef.h
DWARFObject.h
llvm::DWARFFormValue::dumpAddressSection
static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
Definition: DWARFFormValue.cpp:394
llvm::dwarf::Form
Form
Definition: Dwarf.h:132
llvm::object::SectionedAddress::Address
uint64_t Address
Definition: ObjectFile.h:147
llvm::DWARFFormValue::getAsAddress
Optional< uint64_t > getAsAddress() const
Definition: DWARFFormValue.cpp:669
ErrorHandling.h
llvm::raw_ostream::write_escaped
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
Definition: raw_ostream.cpp:161
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::DWARFFormValue::FC_Constant
@ FC_Constant
Definition: DWARFFormValue.h:34
DWARFDebugLine.h
R600_InstFlag::FC
@ FC
Definition: R600Defines.h:32
llvm::DWARFFormValue::getAsSignedConstant
Optional< int64_t > getAsSignedConstant() const
Definition: DWARFFormValue.cpp:735
DWARFContext.h
llvm::DWARFFormValue::createFromUValue
static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
Definition: DWARFFormValue.cpp:88
llvm::DWARFFormValue::getAsUnsignedConstant
Optional< uint64_t > getAsUnsignedConstant() const
Definition: DWARFFormValue.cpp:728
llvm::WithColor::get
raw_ostream & get()
Definition: WithColor.h:79
llvm::DWARFContext
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:47
llvm::DWARFFormValue::FC_Exprloc
@ FC_Exprloc
Definition: DWARFFormValue.h:40
llvm::WithColor
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:53
llvm::Optional< uint8_t >
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::DWARFFormValue::FC_Reference
@ FC_Reference
Definition: DWARFFormValue.h:37
llvm::DWARFUnit::getFormParams
const dwarf::FormParams & getFormParams() const
Definition: DWARFUnit.h:315
llvm::DWARFDataExtractor
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Definition: DWARFDataExtractor.h:21
Format.h
llvm::nulls
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
Definition: raw_ostream.cpp:898
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::DWARFFormValue::FC_Indirect
@ FC_Indirect
Definition: DWARFFormValue.h:38
llvm::object::SectionedAddress::SectionIndex
uint64_t SectionIndex
Definition: ObjectFile.h:148
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::DWARFFormValue::createFromPValue
static DWARFFormValue createFromPValue(dwarf::Form F, const char *V)
Definition: DWARFFormValue.cpp:92
llvm::AArch64CC::LT
@ LT
Definition: AArch64BaseInfo.h:266
llvm::DataExtractor::getULEB128
uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
Definition: DataExtractor.cpp:220
llvm::DIDumpOptions::ShowAddresses
bool ShowAddresses
Definition: DIContext.h:194
llvm::DWARFFormValue::createFromSValue
static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V)
Definition: DWARFFormValue.cpp:84
llvm::DIDumpOptions::Verbose
bool Verbose
Definition: DIContext.h:199
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::DWARFObject::getSectionNames
virtual ArrayRef< SectionName > getSectionNames() const
Definition: DWARFObject.h:32
llvm::DWARFFormValue::FC_SectionOffset
@ FC_SectionOffset
Definition: DWARFFormValue.h:39
llvm::DWARFFormValue::dump
void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
Definition: DWARFFormValue.cpp:409
DWARFSection.h
llvm::DWARFFormValue::getAsSectionOffset
Optional< uint64_t > getAsSectionOffset() const
Definition: DWARFFormValue.cpp:722
llvm::DWARFFormValue::getAsCStringOffset
Optional< uint64_t > getAsCStringOffset() const
Definition: DWARFFormValue.cpp:761
llvm::DWARFFormValue::getAsCString
Expected< const char * > getAsCString() const
Definition: DWARFFormValue.cpp:628
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::DWARFObject
Definition: DWARFObject.h:25
llvm::DWARFFormValue
Definition: DWARFFormValue.h:28
llvm::dwarf::FormEncodingString
StringRef FormEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:105
llvm::DWARFFormValue::skipValue
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.
Definition: DWARFFormValue.h:147
llvm::DWARFFormValue::getAsRelativeReference
Optional< UnitOffset > getAsRelativeReference() const
Definition: DWARFFormValue.cpp:701
llvm::DWARFUnit::getDebugInfoExtractor
DWARFDataExtractor getDebugInfoExtractor() const
Definition: DWARFUnit.cpp:202
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::DWARFFormValue::FC_String
@ FC_String
Definition: DWARFFormValue.h:35
llvm::DWARFFormValue::createFromBlockValue
static DWARFFormValue createFromBlockValue(dwarf::Form F, ArrayRef< uint8_t > D)
Definition: DWARFFormValue.cpp:96
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DWARFFormValue::FormClass
FormClass
Definition: DWARFFormValue.h:30
DWARFUnit.h
llvm::DILineInfoSpecifier::FileLineInfoKind
FileLineInfoKind
Definition: DIContext.h:140
ArrayRef.h
llvm::dwarf::FormParams
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition: Dwarf.h:653
llvm::HighlightColor::Address
@ Address
llvm::DWARFFormValue::FC_Block
@ FC_Block
Definition: DWARFFormValue.h:33
llvm::DWARFFormValue::FC_Flag
@ FC_Flag
Definition: DWARFFormValue.h:36
llvm::errorToBool
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
Definition: Error.h:1065
DWARF5FormClasses
static const DWARFFormValue::FormClass DWARF5FormClasses[]
Definition: DWARFFormValue.cpp:33
llvm::dwarf::toString
Optional< const char * > toString(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
Definition: DWARFFormValue.h:177
llvm::size
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:1690
llvm::DataExtractor::getU32
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
Definition: DataExtractor.cpp:107
llvm::DataExtractor::getCStr
const char * getCStr(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
Definition: DataExtractor.h:129
llvm::DWARFUnit::getCompilationDir
const char * getCompilationDir()
Definition: DWARFUnit.cpp:389
llvm::ArrayRef< uint8_t >
None.h
llvm::DWARFFormValue::dumpAddress
void dumpAddress(raw_ostream &OS, uint64_t Address) const
llvm::HighlightColor::String
@ String
Dwarf.h
llvm::DWARFUnit::getContext
DWARFContext & getContext() const
Definition: DWARFUnit.h:312
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
if
if(llvm_vc STREQUAL "") set(fake_version_inc "$
Definition: CMakeLists.txt:14
uint32_t
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
llvm::DataExtractor::getU8
uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
Definition: DataExtractor.cpp:79
llvm::ValueType
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
Definition: ScheduleDAGInstrs.h:107
llvm::AMDGPU::SendMsg::Msg
const CustomOperand< const MCSubtargetInfo & > Msg[]
Definition: AMDGPUAsmUtils.cpp:39
llvm::dwarf::getFixedFormByteSize
Optional< uint8_t > getFixedFormByteSize(dwarf::Form Form, FormParams Params)
Get the fixed byte size for a given form.
Definition: Dwarf.cpp:694
WithColor.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
llvm::None
constexpr std::nullopt_t None
Definition: None.h:27
uint16_t
llvm::DWARFUnit
Definition: DWARFUnit.h:207
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::DWARFFormValue::getAsBlock
Optional< ArrayRef< uint8_t > > getAsBlock() const
Definition: DWARFFormValue.cpp:754
llvm::DWARFFormValue::FC_Address
@ FC_Address
Definition: DWARFFormValue.h:32
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:475
llvm::DWARFFormValue::extractValue
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.
Definition: DWARFFormValue.cpp:248
llvm::DWARFFormValue::FC_Unknown
@ FC_Unknown
Definition: DWARFFormValue.h:31
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:596
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DWARFFormValue::isFormClass
bool isFormClass(FormClass FC) const
Definition: DWARFFormValue.cpp:216
llvm::object::SectionedAddress
Definition: ObjectFile.h:144
llvm::DWARFFormValue::getAsReference
Optional< uint64_t > getAsReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
Definition: DWARFFormValue.cpp:695
llvm::DWARFFormValue::getAsReferenceUVal
Optional< uint64_t > getAsReferenceUVal() const
Definition: DWARFFormValue.cpp:767
llvm::StringRef::str
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:221
llvm::DWARFFormValue::createFromUnit
static DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr)
Definition: DWARFFormValue.cpp:104
llvm::DWARFFormValue::getAsFile
Optional< std::string > getAsFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Correctly extract any file paths from a form value.
Definition: DWARFFormValue.cpp:774
llvm::DWARFFormValue::UnitOffset
Definition: DWARFFormValue.h:110
raw_ostream.h
llvm::dwarf::getDwarfOffsetByteSize
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition: Dwarf.h:640
llvm::DWARFFormValue::dumpSectionedAddress
void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts, object::SectionedAddress SA) const
Definition: DWARFFormValue.cpp:386
CU
Definition: AArch64AsmBackend.cpp:504
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
DWARFDataExtractor.h
llvm::DataExtractor::getU16
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
Definition: DataExtractor.cpp:92
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:188
llvm::DataExtractor::getSLEB128
int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a signed LEB128 value from *offset_ptr.
Definition: DataExtractor.cpp:224