LLVM  14.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 
11 
12 using namespace llvm;
13 
14 std::pair<uint64_t, dwarf::DwarfFormat>
16  ErrorAsOutParameter ErrAsOut(Err);
17  if (Err && *Err)
18  return {0, dwarf::DWARF32};
19 
20  Cursor C(*Off);
21  uint64_t Length = getRelocatedValue(C, 4);
23  if (Length == dwarf::DW_LENGTH_DWARF64) {
24  Length = getRelocatedValue(C, 8);
25  Format = dwarf::DWARF64;
26  } else if (Length >= dwarf::DW_LENGTH_lo_reserved) {
27  cantFail(C.takeError());
28  if (Err)
29  *Err = createStringError(
31  "unsupported reserved unit length of value 0x%8.8" PRIx64, Length);
32  return {0, dwarf::DWARF32};
33  }
34 
35  if (C) {
36  *Off = C.tell();
37  return {Length, Format};
38  }
39  if (Err)
40  *Err = C.takeError();
41  else
42  consumeError(C.takeError());
43  return {0, dwarf::DWARF32};
44 }
45 
47  uint64_t *SecNdx,
48  Error *Err) const {
49  if (SecNdx)
51  if (!Section)
52  return getUnsigned(Off, Size, Err);
53 
54  ErrorAsOutParameter ErrAsOut(Err);
55  Optional<RelocAddrEntry> E = Obj->find(*Section, *Off);
56  uint64_t LocData = getUnsigned(Off, Size, Err);
57  if (!E || (Err && *Err))
58  return LocData;
59  if (SecNdx)
60  *SecNdx = E->SectionIndex;
61 
62  uint64_t R =
63  object::resolveRelocation(E->Resolver, E->Reloc, E->SymbolValue, LocData);
64  if (E->Reloc2)
65  R = object::resolveRelocation(E->Resolver, *E->Reloc2, E->SymbolValue2, R);
66  return R;
67 }
68 
71  uint64_t PCRelOffset) const {
72  if (Encoding == dwarf::DW_EH_PE_omit)
73  return None;
74 
75  uint64_t Result = 0;
76  uint64_t OldOffset = *Offset;
77  // First get value
78  switch (Encoding & 0x0F) {
80  switch (getAddressSize()) {
81  case 2:
82  case 4:
83  case 8:
84  Result = getUnsigned(Offset, getAddressSize());
85  break;
86  default:
87  return None;
88  }
89  break;
91  Result = getULEB128(Offset);
92  break;
94  Result = getSLEB128(Offset);
95  break;
97  Result = getUnsigned(Offset, 2);
98  break;
100  Result = getUnsigned(Offset, 4);
101  break;
103  Result = getUnsigned(Offset, 8);
104  break;
106  Result = getSigned(Offset, 2);
107  break;
109  Result = SignExtend64<32>(getRelocatedValue(4, Offset));
110  break;
112  Result = getRelocatedValue(8, Offset);
113  break;
114  default:
115  return None;
116  }
117  // Then add relative offset, if required
118  switch (Encoding & 0x70) {
120  // do nothing
121  break;
123  Result += PCRelOffset;
124  break;
129  default:
130  *Offset = OldOffset;
131  return None;
132  }
133 
134  return Result;
135 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::errc::invalid_argument
@ invalid_argument
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::dwarf::DW_EH_PE_sdata4
@ DW_EH_PE_sdata4
Definition: Dwarf.h:440
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:142
llvm::dwarf::DW_EH_PE_sdata8
@ DW_EH_PE_sdata8
Definition: Dwarf.h:441
DWARFContext.h
llvm::dwarf::DW_LENGTH_DWARF64
@ DW_LENGTH_DWARF64
Indicator of 64-bit DWARF format.
Definition: Dwarf.h:56
llvm::Optional
Definition: APInt.h:33
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1035
llvm::dwarf::DW_EH_PE_textrel
@ DW_EH_PE_textrel
Definition: Dwarf.h:444
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:221
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:46
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::dwarf::DW_EH_PE_udata2
@ DW_EH_PE_udata2
Definition: Dwarf.h:435
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:55
llvm::ErrorAsOutParameter
Helper for Errors used as out-parameters.
Definition: Error.h:1089
llvm::dwarf::DwarfFormat
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:92
llvm::None
const NoneType None
Definition: None.h:23
llvm::dwarf::DW_EH_PE_funcrel
@ DW_EH_PE_funcrel
Definition: Dwarf.h:446
llvm::dwarf::DW_EH_PE_udata4
@ DW_EH_PE_udata4
Definition: Dwarf.h:436
llvm::dwarf::DW_EH_PE_udata8
@ DW_EH_PE_udata8
Definition: Dwarf.h:437
uint64_t
llvm::dwarf::DW_EH_PE_pcrel
@ DW_EH_PE_pcrel
Definition: Dwarf.h:443
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:70
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:126
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:15
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:445
llvm::dwarf::DW_EH_PE_uleb128
@ DW_EH_PE_uleb128
Definition: Dwarf.h:434
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:737
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:438
llvm::dwarf::DW_EH_PE_absptr
@ DW_EH_PE_absptr
Definition: Dwarf.h:432
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1231
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::dwarf::DW_EH_PE_omit
@ DW_EH_PE_omit
Definition: Dwarf.h:433
llvm::dwarf::DW_EH_PE_sdata2
@ DW_EH_PE_sdata2
Definition: Dwarf.h:439
llvm::dwarf::DW_EH_PE_aligned
@ DW_EH_PE_aligned
Definition: Dwarf.h:447
llvm::dwarf::DWARF64
@ DWARF64
Definition: Dwarf.h:92
llvm::dwarf::DWARF32
@ DWARF32
Definition: Dwarf.h:92
llvm::object::resolveRelocation
uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)
Definition: RelocationResolver.cpp:736
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:225