LLVM  15.0.0git
DWARFDataExtractor.cpp
Go to the documentation of this file.
1 //===- DWARFDataExtractor.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 
12 #include "llvm/Support/Errc.h"
13 
14 using namespace llvm;
15 
16 std::pair<uint64_t, dwarf::DwarfFormat>
18  ErrorAsOutParameter ErrAsOut(Err);
19  if (Err && *Err)
20  return {0, dwarf::DWARF32};
21 
22  Cursor C(*Off);
23  uint64_t Length = getRelocatedValue(C, 4);
25  if (Length == dwarf::DW_LENGTH_DWARF64) {
26  Length = getRelocatedValue(C, 8);
27  Format = dwarf::DWARF64;
28  } else if (Length >= dwarf::DW_LENGTH_lo_reserved) {
29  cantFail(C.takeError());
30  if (Err)
31  *Err = createStringError(
33  "unsupported reserved unit length of value 0x%8.8" PRIx64, Length);
34  return {0, dwarf::DWARF32};
35  }
36 
37  if (C) {
38  *Off = C.tell();
39  return {Length, Format};
40  }
41  if (Err)
42  *Err = C.takeError();
43  else
44  consumeError(C.takeError());
45  return {0, dwarf::DWARF32};
46 }
47 
49  uint64_t *SecNdx,
50  Error *Err) const {
51  if (SecNdx)
53  if (!Section)
54  return getUnsigned(Off, Size, Err);
55 
56  ErrorAsOutParameter ErrAsOut(Err);
57  Optional<RelocAddrEntry> E = Obj->find(*Section, *Off);
58  uint64_t LocData = getUnsigned(Off, Size, Err);
59  if (!E || (Err && *Err))
60  return LocData;
61  if (SecNdx)
62  *SecNdx = E->SectionIndex;
63 
64  uint64_t R =
65  object::resolveRelocation(E->Resolver, E->Reloc, E->SymbolValue, LocData);
66  if (E->Reloc2)
67  R = object::resolveRelocation(E->Resolver, *E->Reloc2, E->SymbolValue2, R);
68  return R;
69 }
70 
73  uint64_t PCRelOffset) const {
74  if (Encoding == dwarf::DW_EH_PE_omit)
75  return None;
76 
77  uint64_t Result = 0;
78  uint64_t OldOffset = *Offset;
79  // First get value
80  switch (Encoding & 0x0F) {
82  switch (getAddressSize()) {
83  case 2:
84  case 4:
85  case 8:
86  Result = getUnsigned(Offset, getAddressSize());
87  break;
88  default:
89  return None;
90  }
91  break;
93  Result = getULEB128(Offset);
94  break;
96  Result = getSLEB128(Offset);
97  break;
99  Result = getUnsigned(Offset, 2);
100  break;
102  Result = getUnsigned(Offset, 4);
103  break;
105  Result = getUnsigned(Offset, 8);
106  break;
108  Result = getSigned(Offset, 2);
109  break;
111  Result = SignExtend64<32>(getRelocatedValue(4, Offset));
112  break;
114  Result = getRelocatedValue(8, Offset);
115  break;
116  default:
117  return None;
118  }
119  // Then add relative offset, if required
120  switch (Encoding & 0x70) {
122  // do nothing
123  break;
125  Result += PCRelOffset;
126  break;
131  default:
132  *Offset = OldOffset;
133  return None;
134  }
135 
136  return Result;
137 }
llvm::errc::invalid_argument
@ invalid_argument
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::dwarf::DW_EH_PE_sdata4
@ DW_EH_PE_sdata4
Definition: Dwarf.h:445
llvm::DataExtractor::getSigned
int64_t getSigned(uint64_t *offset_ptr, uint32_t size) const
Extract an signed integer of size byte_size from *offset_ptr.
Definition: DataExtractor.cpp:141
DWARFObject.h
llvm::dwarf::DW_EH_PE_sdata8
@ DW_EH_PE_sdata8
Definition: Dwarf.h:446
llvm::dwarf::DW_LENGTH_DWARF64
@ DW_LENGTH_DWARF64
Indicator of 64-bit DWARF format.
Definition: Dwarf.h:57
Errc.h
DWARFRelocMap.h
llvm::Optional
Definition: APInt.h:33
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
llvm::dwarf::DW_EH_PE_textrel
@ DW_EH_PE_textrel
Definition: Dwarf.h:449
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::DWARFDataExtractor::getRelocatedValue
uint64_t getRelocatedValue(uint32_t Size, uint64_t *Off, uint64_t *SectionIndex=nullptr, Error *Err=nullptr) const
Extracts a value and applies a relocation to the result if one exists for the given offset.
Definition: DWARFDataExtractor.cpp:48
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::dwarf::DW_EH_PE_udata2
@ DW_EH_PE_udata2
Definition: Dwarf.h:440
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::DataExtractor::getAddressSize
uint8_t getAddressSize() const
Get the address size for this extractor.
Definition: DataExtractor.h:99
llvm::dwarf::DW_LENGTH_lo_reserved
@ DW_LENGTH_lo_reserved
Special values for an initial length field.
Definition: Dwarf.h:56
llvm::ErrorAsOutParameter
Helper for Errors used as out-parameters.
Definition: Error.h:1097
llvm::dwarf::DwarfFormat
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:93
llvm::None
const NoneType None
Definition: None.h:24
llvm::dwarf::DW_EH_PE_funcrel
@ DW_EH_PE_funcrel
Definition: Dwarf.h:451
llvm::dwarf::DW_EH_PE_udata4
@ DW_EH_PE_udata4
Definition: Dwarf.h:441
llvm::dwarf::DW_EH_PE_udata8
@ DW_EH_PE_udata8
Definition: Dwarf.h:442
uint64_t
llvm::dwarf::DW_EH_PE_pcrel
@ DW_EH_PE_pcrel
Definition: Dwarf.h:448
llvm::object::SectionedAddress::UndefSection
const static uint64_t UndefSection
Definition: ObjectFile.h:145
llvm::DWARFDataExtractor::getEncodedPointer
Optional< uint64_t > getEncodedPointer(uint64_t *Offset, uint8_t Encoding, uint64_t AbsPosOffset=0) const
Extracts a DWARF-encoded pointer in Offset using Encoding.
Definition: DWARFDataExtractor.cpp:72
llvm::DataExtractor::getUnsigned
uint64_t getUnsigned(uint64_t *offset_ptr, uint32_t byte_size, Error *Err=nullptr) const
Extract an unsigned integer of size byte_size from *offset_ptr.
Definition: DataExtractor.cpp:125
llvm::DWARFDataExtractor::getInitialLength
std::pair< uint64_t, dwarf::DwarfFormat > getInitialLength(uint64_t *Off, Error *Err=nullptr) const
Extracts the DWARF "initial length" field, which can either be a 32-bit value smaller than 0xfffffff0...
Definition: DWARFDataExtractor.cpp:17
llvm::DataExtractor::Cursor
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
llvm::dwarf::DW_EH_PE_datarel
@ DW_EH_PE_datarel
Definition: Dwarf.h:450
llvm::dwarf::DW_EH_PE_uleb128
@ DW_EH_PE_uleb128
Definition: Dwarf.h:439
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:745
uint32_t
llvm::DWARFObject::find
virtual Optional< RelocAddrEntry > find(const DWARFSection &Sec, uint64_t Pos) const =0
llvm::dwarf::DW_EH_PE_sleb128
@ DW_EH_PE_sleb128
Definition: Dwarf.h:443
llvm::dwarf::DW_EH_PE_absptr
@ DW_EH_PE_absptr
Definition: Dwarf.h:437
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1239
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::dwarf::DW_EH_PE_omit
@ DW_EH_PE_omit
Definition: Dwarf.h:438
llvm::dwarf::DW_EH_PE_sdata2
@ DW_EH_PE_sdata2
Definition: Dwarf.h:444
llvm::dwarf::DW_EH_PE_aligned
@ DW_EH_PE_aligned
Definition: Dwarf.h:452
llvm::dwarf::DWARF64
@ DWARF64
Definition: Dwarf.h:93
llvm::dwarf::DWARF32
@ DWARF32
Definition: Dwarf.h:93
llvm::object::resolveRelocation
uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)
Definition: RelocationResolver.cpp:781
DWARFDataExtractor.h
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