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_DWARFDATAEXTRACTOR_H
10 #define LLVM_DEBUGINFO_DWARFDATAEXTRACTOR_H
11 
12 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
13 #include "llvm/Support/DataExtractor.h"
14 
15 namespace llvm {
16 class DWARFObject;
17 
18 /// A DataExtractor (typically for an in-memory copy of an object-file section)
19 /// plus a relocation map for that section, if there is one.
20 class DWARFDataExtractor : public DataExtractor {
21   const DWARFObject *Obj = nullptr;
22   const DWARFSection *Section = nullptr;
23 
24 public:
25   /// Constructor for the normal case of extracting data from a DWARF section.
26   /// The DWARFSection's lifetime must be at least as long as the extractor's.
27   DWARFDataExtractor(const DWARFObject &Obj, const DWARFSection &Section,
28                      bool IsLittleEndian, uint8_t AddressSize)
29       : DataExtractor(Section.Data, IsLittleEndian, AddressSize), Obj(&Obj),
30         Section(&Section) {}
31 
32   /// Constructor for cases when there are no relocations.
33   DWARFDataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
34     : DataExtractor(Data, IsLittleEndian, AddressSize) {}
35 
36   /// Extracts a value and applies a relocation to the result if
37   /// one exists for the given offset.
38   uint64_t getRelocatedValue(uint32_t Size, uint32_t *Off,
39                              uint64_t *SectionIndex = nullptr) const;
40 
41   /// Extracts an address-sized value and applies a relocation to the result if
42   /// one exists for the given offset.
43   uint64_t getRelocatedAddress(uint32_t *Off, uint64_t *SecIx = nullptr) const {
44     return getRelocatedValue(getAddressSize(), Off, SecIx);
45   }
46 
47   /// Extracts a DWARF-encoded pointer in \p Offset using \p Encoding.
48   /// There is a DWARF encoding that uses a PC-relative adjustment.
49   /// For these values, \p AbsPosOffset is used to fix them, which should
50   /// reflect the absolute address of this pointer.
51   Optional<uint64_t> getEncodedPointer(uint32_t *Offset, uint8_t Encoding,
52                                        uint64_t AbsPosOffset = 0) const;
53 
54   size_t size() const { return Section == nullptr ? 0 : Section->Data.size(); }
55 };
56 
57 } // end namespace llvm
58 
59 #endif // LLVM_DEBUGINFO_DWARFDATAEXTRACTOR_H
60