LLVM  13.0.0git
DWARFDataExtractor.h
Go to the documentation of this file.
1 //===- DWARFDataExtractor.h -------------------------------------*- C++ -*-===//
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 
9 #ifndef LLVM_DEBUGINFO_DWARF_DWARFDATAEXTRACTOR_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFDATAEXTRACTOR_H
11 
15 
16 namespace llvm {
17 class DWARFObject;
18 
19 /// A DataExtractor (typically for an in-memory copy of an object-file section)
20 /// plus a relocation map for that section, if there is one.
22  const DWARFObject *Obj = nullptr;
23  const DWARFSection *Section = nullptr;
24 
25 public:
26  /// Constructor for the normal case of extracting data from a DWARF section.
27  /// The DWARFSection's lifetime must be at least as long as the extractor's.
28  DWARFDataExtractor(const DWARFObject &Obj, const DWARFSection &Section,
29  bool IsLittleEndian, uint8_t AddressSize)
30  : DataExtractor(Section.Data, IsLittleEndian, AddressSize), Obj(&Obj),
31  Section(&Section) {}
32 
33  /// Constructor for cases when there are no relocations.
34  DWARFDataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
35  : DataExtractor(Data, IsLittleEndian, AddressSize) {}
36  DWARFDataExtractor(ArrayRef<uint8_t> Data, bool IsLittleEndian,
37  uint8_t AddressSize)
38  : DataExtractor(
39  StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()),
40  IsLittleEndian, AddressSize) {}
41 
42  /// Truncating constructor
43  DWARFDataExtractor(const DWARFDataExtractor &Other, size_t Length)
44  : DataExtractor(Other.getData().substr(0, Length), Other.isLittleEndian(),
46  Obj(Other.Obj), Section(Other.Section) {}
47 
48  /// Extracts the DWARF "initial length" field, which can either be a 32-bit
49  /// value smaller than 0xfffffff0, or the value 0xffffffff followed by a
50  /// 64-bit length. Returns the actual length, and the DWARF format which is
51  /// encoded in the field. In case of errors, it returns {0, DWARF32} and
52  /// leaves the offset unchanged.
53  std::pair<uint64_t, dwarf::DwarfFormat>
54  getInitialLength(uint64_t *Off, Error *Err = nullptr) const;
55 
56  std::pair<uint64_t, dwarf::DwarfFormat> getInitialLength(Cursor &C) const {
57  return getInitialLength(&getOffset(C), &getError(C));
58  }
59 
60  /// Extracts a value and applies a relocation to the result if
61  /// one exists for the given offset.
62  uint64_t getRelocatedValue(uint32_t Size, uint64_t *Off,
63  uint64_t *SectionIndex = nullptr,
64  Error *Err = nullptr) const;
66  uint64_t *SectionIndex = nullptr) const {
67  return getRelocatedValue(Size, &getOffset(C), SectionIndex, &getError(C));
68  }
69 
70  /// Extracts an address-sized value and applies a relocation to the result if
71  /// one exists for the given offset.
72  uint64_t getRelocatedAddress(uint64_t *Off, uint64_t *SecIx = nullptr) const {
73  return getRelocatedValue(getAddressSize(), Off, SecIx);
74  }
75  uint64_t getRelocatedAddress(Cursor &C, uint64_t *SecIx = nullptr) const {
76  return getRelocatedValue(getAddressSize(), &getOffset(C), SecIx,
77  &getError(C));
78  }
79 
80  /// Extracts a DWARF-encoded pointer in \p Offset using \p Encoding.
81  /// There is a DWARF encoding that uses a PC-relative adjustment.
82  /// For these values, \p AbsPosOffset is used to fix them, which should
83  /// reflect the absolute address of this pointer.
84  Optional<uint64_t> getEncodedPointer(uint64_t *Offset, uint8_t Encoding,
85  uint64_t AbsPosOffset = 0) const;
86 };
87 
88 } // end namespace llvm
89 
90 #endif // LLVM_DEBUGINFO_DWARF_DWARFDATAEXTRACTOR_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::DWARFSection
Definition: DWARFSection.h:16
llvm
Definition: AllocatorList.h:23
llvm::DWARFDataExtractor::DWARFDataExtractor
DWARFDataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
Constructor for cases when there are no relocations.
Definition: DWARFDataExtractor.h:34
llvm::Optional< uint64_t >
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::DWARFDataExtractor
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Definition: DWARFDataExtractor.h:21
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::DWARFDataExtractor::DWARFDataExtractor
DWARFDataExtractor(const DWARFObject &Obj, const DWARFSection &Section, bool IsLittleEndian, uint8_t AddressSize)
Constructor for the normal case of extracting data from a DWARF section.
Definition: DWARFDataExtractor.h:28
llvm::DWARFDataExtractor::getRelocatedAddress
uint64_t getRelocatedAddress(uint64_t *Off, uint64_t *SecIx=nullptr) const
Extracts an address-sized value and applies a relocation to the result if one exists for the given of...
Definition: DWARFDataExtractor.h:72
llvm::DataExtractor::isLittleEndian
bool isLittleEndian() const
Get the endianness for this extractor.
Definition: DataExtractor.h:94
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
llvm::DataExtractor::getError
static Error & getError(Cursor &C)
Definition: DataExtractor.h:691
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:96
DWARFSection.h
llvm::DWARFObject
Definition: DWARFObject.h:25
llvm::DataExtractor::getOffset
static uint64_t & getOffset(Cursor &C)
Definition: DataExtractor.h:690
llvm::DataExtractor::size
size_t size() const
Return the number of bytes in the underlying buffer.
Definition: DataExtractor.h:685
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
llvm::DWARFDataExtractor::DWARFDataExtractor
DWARFDataExtractor(const DWARFDataExtractor &Other, size_t Length)
Truncating constructor.
Definition: DWARFDataExtractor.h:43
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::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::DWARFDataExtractor::getInitialLength
std::pair< uint64_t, dwarf::DwarfFormat > getInitialLength(Cursor &C) const
Definition: DWARFDataExtractor.h:56
llvm::DWARFDataExtractor::getRelocatedAddress
uint64_t getRelocatedAddress(Cursor &C, uint64_t *SecIx=nullptr) const
Definition: DWARFDataExtractor.h:75
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
Dwarf.h
llvm::DWARFDataExtractor::DWARFDataExtractor
DWARFDataExtractor(ArrayRef< uint8_t > Data, bool IsLittleEndian, uint8_t AddressSize)
Definition: DWARFDataExtractor.h:36
uint32_t
llvm::DWARFDataExtractor::getRelocatedValue
uint64_t getRelocatedValue(Cursor &C, uint32_t Size, uint64_t *SectionIndex=nullptr) const
Definition: DWARFDataExtractor.h:65
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
DataExtractor.h
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DataExtractor::getData
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:92
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1163