LLVM  15.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 
29 using namespace llvm;
30 using 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 
88  return DWARFFormValue(F, ValueType(V));
89 }
90 
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_strx4:
165  case DW_FORM_addrx1:
166  case DW_FORM_addrx2:
167  case DW_FORM_addrx4:
168  case DW_FORM_sec_offset:
169  case DW_FORM_strp:
170  case DW_FORM_strp_sup:
171  case DW_FORM_line_strp:
172  case DW_FORM_GNU_ref_alt:
173  case DW_FORM_GNU_strp_alt:
174  case DW_FORM_implicit_const:
175  if (Optional<uint8_t> FixedSize =
177  *OffsetPtr += *FixedSize;
178  return true;
179  }
180  return false;
181 
182  // signed or unsigned LEB 128 values.
183  case DW_FORM_sdata:
184  DebugInfoData.getSLEB128(OffsetPtr);
185  return true;
186 
187  case DW_FORM_udata:
188  case DW_FORM_ref_udata:
189  case DW_FORM_strx:
190  case DW_FORM_addrx:
191  case DW_FORM_loclistx:
192  case DW_FORM_rnglistx:
193  case DW_FORM_GNU_addr_index:
194  case DW_FORM_GNU_str_index:
195  DebugInfoData.getULEB128(OffsetPtr);
196  return true;
197 
198  case DW_FORM_LLVM_addrx_offset:
199  DebugInfoData.getULEB128(OffsetPtr);
200  *OffsetPtr += 4;
201  return true;
202 
203  case DW_FORM_indirect:
204  Indirect = true;
205  Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
206  break;
207 
208  default:
209  return false;
210  }
211  } while (Indirect);
212  return true;
213 }
214 
216  // First, check DWARF5 form classes.
219  return true;
220  // Check more forms from extensions and proposals.
221  switch (Form) {
222  case DW_FORM_GNU_ref_alt:
223  return (FC == FC_Reference);
224  case DW_FORM_GNU_addr_index:
225  return (FC == FC_Address);
226  case DW_FORM_GNU_str_index:
227  case DW_FORM_GNU_strp_alt:
228  return (FC == FC_String);
229  case DW_FORM_LLVM_addrx_offset:
230  return (FC == FC_Address);
231  default:
232  break;
233  }
234 
235  if (FC == FC_SectionOffset) {
236  if (Form == DW_FORM_strp || Form == DW_FORM_line_strp)
237  return true;
238  // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section
239  // offset. If we don't have a DWARFUnit, default to the old behavior.
240  if (Form == DW_FORM_data4 || Form == DW_FORM_data8)
241  return !U || U->getVersion() <= 3;
242  }
243 
244  return false;
245 }
246 
248  uint64_t *OffsetPtr, dwarf::FormParams FP,
249  const DWARFContext *Ctx,
250  const DWARFUnit *CU) {
251  if (!Ctx && CU)
252  Ctx = &CU->getContext();
253  C = Ctx;
254  U = CU;
255  Format = FP.Format;
256  bool Indirect = false;
257  bool IsBlock = false;
258  Value.data = nullptr;
259  // Read the value for the form into value and follow and DW_FORM_indirect
260  // instances we run into
261  Error Err = Error::success();
262  do {
263  Indirect = false;
264  switch (Form) {
265  case DW_FORM_addr:
266  case DW_FORM_ref_addr: {
267  uint16_t Size =
268  (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
269  Value.uval =
270  Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex, &Err);
271  break;
272  }
273  case DW_FORM_exprloc:
274  case DW_FORM_block:
275  Value.uval = Data.getULEB128(OffsetPtr, &Err);
276  IsBlock = true;
277  break;
278  case DW_FORM_block1:
279  Value.uval = Data.getU8(OffsetPtr, &Err);
280  IsBlock = true;
281  break;
282  case DW_FORM_block2:
283  Value.uval = Data.getU16(OffsetPtr, &Err);
284  IsBlock = true;
285  break;
286  case DW_FORM_block4:
287  Value.uval = Data.getU32(OffsetPtr, &Err);
288  IsBlock = true;
289  break;
290  case DW_FORM_data1:
291  case DW_FORM_ref1:
292  case DW_FORM_flag:
293  case DW_FORM_strx1:
294  case DW_FORM_addrx1:
295  Value.uval = Data.getU8(OffsetPtr, &Err);
296  break;
297  case DW_FORM_data2:
298  case DW_FORM_ref2:
299  case DW_FORM_strx2:
300  case DW_FORM_addrx2:
301  Value.uval = Data.getU16(OffsetPtr, &Err);
302  break;
303  case DW_FORM_strx3:
304  Value.uval = Data.getU24(OffsetPtr, &Err);
305  break;
306  case DW_FORM_data4:
307  case DW_FORM_ref4:
308  case DW_FORM_ref_sup4:
309  case DW_FORM_strx4:
310  case DW_FORM_addrx4:
311  Value.uval = Data.getRelocatedValue(4, OffsetPtr, nullptr, &Err);
312  break;
313  case DW_FORM_data8:
314  case DW_FORM_ref8:
315  case DW_FORM_ref_sup8:
316  Value.uval = Data.getRelocatedValue(8, OffsetPtr, nullptr, &Err);
317  break;
318  case DW_FORM_data16:
319  // Treat this like a 16-byte block.
320  Value.uval = 16;
321  IsBlock = true;
322  break;
323  case DW_FORM_sdata:
324  Value.sval = Data.getSLEB128(OffsetPtr, &Err);
325  break;
326  case DW_FORM_udata:
327  case DW_FORM_ref_udata:
328  case DW_FORM_rnglistx:
329  case DW_FORM_loclistx:
330  case DW_FORM_GNU_addr_index:
331  case DW_FORM_GNU_str_index:
332  case DW_FORM_addrx:
333  case DW_FORM_strx:
334  Value.uval = Data.getULEB128(OffsetPtr, &Err);
335  break;
336  case DW_FORM_LLVM_addrx_offset:
337  Value.uval = Data.getULEB128(OffsetPtr, &Err) << 32;
338  Value.uval |= Data.getU32(OffsetPtr, &Err);
339  break;
340  case DW_FORM_string:
341  Value.cstr = Data.getCStr(OffsetPtr, &Err);
342  break;
343  case DW_FORM_indirect:
344  Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr, &Err));
345  Indirect = true;
346  break;
347  case DW_FORM_strp:
348  case DW_FORM_sec_offset:
349  case DW_FORM_GNU_ref_alt:
350  case DW_FORM_GNU_strp_alt:
351  case DW_FORM_line_strp:
352  case DW_FORM_strp_sup: {
353  Value.uval = Data.getRelocatedValue(FP.getDwarfOffsetByteSize(),
354  OffsetPtr, nullptr, &Err);
355  break;
356  }
357  case DW_FORM_flag_present:
358  Value.uval = 1;
359  break;
360  case DW_FORM_ref_sig8:
361  Value.uval = Data.getU64(OffsetPtr, &Err);
362  break;
363  case DW_FORM_implicit_const:
364  // Value has been already set by DWARFFormValue::createFromSValue.
365  break;
366  default:
367  // DWARFFormValue::skipValue() will have caught this and caused all
368  // DWARF DIEs to fail to be parsed, so this code is not be reachable.
369  llvm_unreachable("unsupported form");
370  }
371  } while (Indirect && !Err);
372 
373  if (IsBlock)
374  Value.data = Data.getBytes(OffsetPtr, Value.uval, &Err).bytes_begin();
375 
376  return !errorToBool(std::move(Err));
377 }
378 
379 void DWARFFormValue::dumpAddress(raw_ostream &OS, uint8_t AddressSize,
380  uint64_t Address) {
381  uint8_t HexDigits = AddressSize * 2;
382  OS << format("0x%*.*" PRIx64, HexDigits, HexDigits, Address);
383 }
384 
386  DIDumpOptions DumpOpts,
387  object::SectionedAddress SA) const {
388  dumpAddress(OS, U->getAddressByteSize(), SA.Address);
389  dumpAddressSection(U->getContext().getDWARFObj(), OS, DumpOpts,
390  SA.SectionIndex);
391 }
392 
394  DIDumpOptions DumpOpts,
395  uint64_t SectionIndex) {
396  if (!DumpOpts.Verbose || SectionIndex == -1ULL)
397  return;
398  ArrayRef<SectionName> SectionNames = Obj.getSectionNames();
399  const auto &SecRef = SectionNames[SectionIndex];
400 
401  OS << " \"" << SecRef.Name << '\"';
402 
403  // Print section index if name is not unique.
404  if (!SecRef.IsNameUnique)
405  OS << format(" [%" PRIu64 "]", SectionIndex);
406 }
407 
408 void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
409  uint64_t UValue = Value.uval;
410  bool CURelativeOffset = false;
411  raw_ostream &AddrOS = DumpOpts.ShowAddresses
413  : nulls();
414  int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
415  switch (Form) {
416  case DW_FORM_addr:
417  dumpSectionedAddress(AddrOS, DumpOpts, {Value.uval, Value.SectionIndex});
418  break;
419  case DW_FORM_addrx:
420  case DW_FORM_addrx1:
421  case DW_FORM_addrx2:
422  case DW_FORM_addrx3:
423  case DW_FORM_addrx4:
424  case DW_FORM_GNU_addr_index: {
425  if (U == nullptr) {
426  OS << "<invalid dwarf unit>";
427  break;
428  }
429  Optional<object::SectionedAddress> A = U->getAddrOffsetSectionItem(UValue);
430  if (!A || DumpOpts.Verbose)
431  AddrOS << format("indexed (%8.8x) address = ", (uint32_t)UValue);
432  if (A)
433  dumpSectionedAddress(AddrOS, DumpOpts, *A);
434  else
435  OS << "<unresolved>";
436  break;
437  }
438  case DW_FORM_LLVM_addrx_offset: {
439  if (U == nullptr) {
440  OS << "<invalid dwarf unit>";
441  break;
442  }
443  uint32_t Index = UValue >> 32;
444  uint32_t Offset = UValue & 0xffffffff;
445  Optional<object::SectionedAddress> A = U->getAddrOffsetSectionItem(Index);
446  if (!A || DumpOpts.Verbose)
447  AddrOS << format("indexed (%8.8x) + 0x%x address = ", Index, Offset);
448  if (A) {
449  A->Address += Offset;
450  dumpSectionedAddress(AddrOS, DumpOpts, *A);
451  } else
452  OS << "<unresolved>";
453  break;
454  }
455  case DW_FORM_flag_present:
456  OS << "true";
457  break;
458  case DW_FORM_flag:
459  case DW_FORM_data1:
460  OS << format("0x%02x", (uint8_t)UValue);
461  break;
462  case DW_FORM_data2:
463  OS << format("0x%04x", (uint16_t)UValue);
464  break;
465  case DW_FORM_data4:
466  OS << format("0x%08x", (uint32_t)UValue);
467  break;
468  case DW_FORM_ref_sig8:
469  AddrOS << format("0x%016" PRIx64, UValue);
470  break;
471  case DW_FORM_data8:
472  OS << format("0x%016" PRIx64, UValue);
473  break;
474  case DW_FORM_data16:
475  OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
476  break;
477  case DW_FORM_string:
478  OS << '"';
479  OS.write_escaped(Value.cstr);
480  OS << '"';
481  break;
482  case DW_FORM_exprloc:
483  case DW_FORM_block:
484  case DW_FORM_block1:
485  case DW_FORM_block2:
486  case DW_FORM_block4:
487  if (UValue > 0) {
488  switch (Form) {
489  case DW_FORM_exprloc:
490  case DW_FORM_block:
491  AddrOS << format("<0x%" PRIx64 "> ", UValue);
492  break;
493  case DW_FORM_block1:
494  AddrOS << format("<0x%2.2x> ", (uint8_t)UValue);
495  break;
496  case DW_FORM_block2:
497  AddrOS << format("<0x%4.4x> ", (uint16_t)UValue);
498  break;
499  case DW_FORM_block4:
500  AddrOS << format("<0x%8.8x> ", (uint32_t)UValue);
501  break;
502  default:
503  break;
504  }
505 
506  const uint8_t *DataPtr = Value.data;
507  if (DataPtr) {
508  // UValue contains size of block
509  const uint8_t *EndDataPtr = DataPtr + UValue;
510  while (DataPtr < EndDataPtr) {
511  AddrOS << format("%2.2x ", *DataPtr);
512  ++DataPtr;
513  }
514  } else
515  OS << "NULL";
516  }
517  break;
518 
519  case DW_FORM_sdata:
520  case DW_FORM_implicit_const:
521  OS << Value.sval;
522  break;
523  case DW_FORM_udata:
524  OS << Value.uval;
525  break;
526  case DW_FORM_strp:
527  if (DumpOpts.Verbose)
528  OS << format(" .debug_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth, UValue);
529  dumpString(OS);
530  break;
531  case DW_FORM_line_strp:
532  if (DumpOpts.Verbose)
533  OS << format(" .debug_line_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth,
534  UValue);
535  dumpString(OS);
536  break;
537  case DW_FORM_strx:
538  case DW_FORM_strx1:
539  case DW_FORM_strx2:
540  case DW_FORM_strx3:
541  case DW_FORM_strx4:
542  case DW_FORM_GNU_str_index:
543  if (DumpOpts.Verbose)
544  OS << format("indexed (%8.8x) string = ", (uint32_t)UValue);
545  dumpString(OS);
546  break;
547  case DW_FORM_GNU_strp_alt:
548  if (DumpOpts.Verbose)
549  OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
550  dumpString(OS);
551  break;
552  case DW_FORM_ref_addr:
553  AddrOS << format("0x%016" PRIx64, UValue);
554  break;
555  case DW_FORM_ref1:
556  CURelativeOffset = true;
557  if (DumpOpts.Verbose)
558  AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
559  break;
560  case DW_FORM_ref2:
561  CURelativeOffset = true;
562  if (DumpOpts.Verbose)
563  AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
564  break;
565  case DW_FORM_ref4:
566  CURelativeOffset = true;
567  if (DumpOpts.Verbose)
568  AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
569  break;
570  case DW_FORM_ref8:
571  CURelativeOffset = true;
572  if (DumpOpts.Verbose)
573  AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
574  break;
575  case DW_FORM_ref_udata:
576  CURelativeOffset = true;
577  if (DumpOpts.Verbose)
578  AddrOS << format("cu + 0x%" PRIx64, UValue);
579  break;
580  case DW_FORM_GNU_ref_alt:
581  AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
582  break;
583 
584  // All DW_FORM_indirect attributes should be resolved prior to calling
585  // this function
586  case DW_FORM_indirect:
587  OS << "DW_FORM_indirect";
588  break;
589 
590  case DW_FORM_rnglistx:
591  OS << format("indexed (0x%x) rangelist = ", (uint32_t)UValue);
592  break;
593 
594  case DW_FORM_loclistx:
595  OS << format("indexed (0x%x) loclist = ", (uint32_t)UValue);
596  break;
597 
598  case DW_FORM_sec_offset:
599  AddrOS << format("0x%0*" PRIx64, OffsetDumpWidth, UValue);
600  break;
601 
602  default:
603  OS << format("DW_FORM(0x%4.4x)", Form);
604  break;
605  }
606 
607  if (CURelativeOffset) {
608  if (DumpOpts.Verbose)
609  OS << " => {";
610  if (DumpOpts.ShowAddresses)
612  << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
613  if (DumpOpts.Verbose)
614  OS << "}";
615  }
616 }
617 
618 void DWARFFormValue::dumpString(raw_ostream &OS) const {
619  if (auto DbgStr = dwarf::toString(*this)) {
620  auto COS = WithColor(OS, HighlightColor::String);
621  COS.get() << '"';
622  COS.get().write_escaped(*DbgStr);
623  COS.get() << '"';
624  }
625 }
626 
628  if (!isFormClass(FC_String))
629  return make_error<StringError>("Invalid form for string attribute",
631  if (Form == DW_FORM_string)
632  return Value.cstr;
633  // FIXME: Add support for DW_FORM_GNU_strp_alt
634  if (Form == DW_FORM_GNU_strp_alt || C == nullptr)
635  return make_error<StringError>("Unsupported form for string attribute",
637  uint64_t Offset = Value.uval;
639  if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
640  Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
641  Form == DW_FORM_strx4) {
642  if (!U)
643  return make_error<StringError>("API limitation - string extraction not "
644  "available without a DWARFUnit",
646  Expected<uint64_t> StrOffset = U->getStringOffsetSectionItem(Offset);
647  Index = Offset;
648  if (!StrOffset)
649  return StrOffset.takeError();
650  Offset = *StrOffset;
651  }
652  // Prefer the Unit's string extractor, because for .dwo it will point to
653  // .debug_str.dwo, while the Context's extractor always uses .debug_str.
654  DataExtractor StrData = Form == DW_FORM_line_strp
655  ? C->getLineStringExtractor()
656  : U ? U->getStringExtractor()
657  : C->getStringExtractor();
658  if (const char *Str = StrData.getCStr(&Offset))
659  return Str;
660  std::string Msg = FormEncodingString(Form).str();
661  if (Index)
662  Msg += (" uses index " + Twine(*Index) + ", but the referenced string").str();
663  Msg += (" offset " + Twine(Offset) + " is beyond .debug_str bounds").str();
664  return make_error<StringError>(Msg,
666 }
667 
669  if (auto SA = getAsSectionedAddress())
670  return SA->Address;
671  return None;
672 }
673 
676  if (!isFormClass(FC_Address))
677  return None;
678  bool AddrOffset = Form == dwarf::DW_FORM_LLVM_addrx_offset;
679  if (Form == DW_FORM_GNU_addr_index || Form == DW_FORM_addrx || AddrOffset) {
680 
681  uint32_t Index = AddrOffset ? (Value.uval >> 32) : Value.uval;
682  if (!U)
683  return None;
684  Optional<object::SectionedAddress> SA = U->getAddrOffsetSectionItem(Index);
685  if (!SA)
686  return None;
687  if (AddrOffset)
688  SA->Address += (Value.uval & 0xffffffff);
689  return SA;
690  }
691  return {{Value.uval, Value.SectionIndex}};
692 }
693 
695  if (auto R = getAsRelativeReference())
696  return R->Unit ? R->Unit->getOffset() + R->Offset : R->Offset;
697  return None;
698 }
699 
701  if (!isFormClass(FC_Reference))
702  return None;
703  switch (Form) {
704  case DW_FORM_ref1:
705  case DW_FORM_ref2:
706  case DW_FORM_ref4:
707  case DW_FORM_ref8:
708  case DW_FORM_ref_udata:
709  if (!U)
710  return None;
711  return UnitOffset{const_cast<DWARFUnit*>(U), Value.uval};
712  case DW_FORM_ref_addr:
713  case DW_FORM_ref_sig8:
714  case DW_FORM_GNU_ref_alt:
715  return UnitOffset{nullptr, Value.uval};
716  default:
717  return None;
718  }
719 }
720 
722  if (!isFormClass(FC_SectionOffset))
723  return None;
724  return Value.uval;
725 }
726 
728  if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
729  Form == DW_FORM_sdata)
730  return None;
731  return Value.uval;
732 }
733 
735  if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
736  (Form == DW_FORM_udata &&
738  return None;
739  switch (Form) {
740  case DW_FORM_data4:
741  return int32_t(Value.uval);
742  case DW_FORM_data2:
743  return int16_t(Value.uval);
744  case DW_FORM_data1:
745  return int8_t(Value.uval);
746  case DW_FORM_sdata:
747  case DW_FORM_data8:
748  default:
749  return Value.sval;
750  }
751 }
752 
754  if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
755  Form != DW_FORM_data16)
756  return None;
757  return makeArrayRef(Value.data, Value.uval);
758 }
759 
761  if (!isFormClass(FC_String) && Form == DW_FORM_string)
762  return None;
763  return Value.uval;
764 }
765 
767  if (!isFormClass(FC_Reference))
768  return None;
769  return Value.uval;
770 }
771 
774  if (U == nullptr || !isFormClass(FC_Constant))
775  return None;
776  DWARFUnit *DLU = const_cast<DWARFUnit *>(U)->getLinkedUnit();
777  if (auto *LT = DLU->getContext().getLineTableForUnit(DLU)) {
778  std::string FileName;
779  if (LT->getFileNameByIndex(Value.uval, DLU->getCompilationDir(), Kind,
780  FileName))
781  return FileName;
782  }
783  return None;
784 }
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:17
Optional.h
llvm::DWARFFormValue::getAsSectionedAddress
Optional< object::SectionedAddress > getAsSectionedAddress() const
Definition: DWARFFormValue.cpp:675
StringRef.h
DWARFObject.h
llvm::DWARFFormValue::dumpAddressSection
static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
Definition: DWARFFormValue.cpp:393
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:668
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:162
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:734
DWARFContext.h
llvm::DWARFFormValue::createFromUValue
static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
Definition: DWARFFormValue.cpp:87
llvm::DWARFFormValue::getAsUnsignedConstant
Optional< uint64_t > getAsUnsignedConstant() const
Definition: DWARFFormValue.cpp:727
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:291
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:900
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:91
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:83
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:408
DWARFSection.h
llvm::DWARFFormValue::getAsSectionOffset
Optional< uint64_t > getAsSectionOffset() const
Definition: DWARFFormValue.cpp:721
llvm::DWARFFormValue::getAsCStringOffset
Optional< uint64_t > getAsCStringOffset() const
Definition: DWARFFormValue.cpp:760
llvm::DWARFFormValue::getAsCString
Expected< const char * > getAsCString() const
Definition: DWARFFormValue.cpp:627
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:54
llvm::DWARFObject
Definition: DWARFObject.h:25
llvm::DWARFFormValue
Definition: DWARFFormValue.h:28
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:249
llvm::None
const NoneType None
Definition: None.h:24
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
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:700
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:95
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:1066
DWARF5FormClasses
static const DWARFFormValue::FormClass DWARF5FormClasses[]
Definition: DWARFFormValue.cpp:32
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:1598
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:288
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:106
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:83
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:77
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:753
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:247
llvm::DWARFFormValue::FC_Unknown
@ FC_Unknown
Definition: DWARFFormValue.h:31
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DWARFFormValue::isFormClass
bool isFormClass(FormClass FC) const
Definition: DWARFFormValue.cpp:215
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:694
llvm::DWARFFormValue::getAsReferenceUVal
Optional< uint64_t > getAsReferenceUVal() const
Definition: DWARFFormValue.cpp:766
llvm::DWARFFormValue::createFromUnit
static DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr)
Definition: DWARFFormValue.cpp:103
llvm::DWARFFormValue::getAsFile
Optional< std::string > getAsFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Correctly extract any file paths from a form value.
Definition: DWARFFormValue.cpp:773
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:385
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