Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:41

0001 //===- DWARFDataExtractor.h -------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_DEBUGINFO_DWARF_DWARFDATAEXTRACTOR_H
0010 #define LLVM_DEBUGINFO_DWARF_DWARFDATAEXTRACTOR_H
0011 
0012 #include "llvm/BinaryFormat/Dwarf.h"
0013 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
0014 #include "llvm/Support/DataExtractor.h"
0015 
0016 namespace llvm {
0017 class DWARFObject;
0018 
0019 /// A DataExtractor (typically for an in-memory copy of an object-file section)
0020 /// plus a relocation map for that section, if there is one.
0021 class DWARFDataExtractor : public DataExtractor {
0022   const DWARFObject *Obj = nullptr;
0023   const DWARFSection *Section = nullptr;
0024 
0025 public:
0026   /// Constructor for the normal case of extracting data from a DWARF section.
0027   /// The DWARFSection's lifetime must be at least as long as the extractor's.
0028   DWARFDataExtractor(const DWARFObject &Obj, const DWARFSection &Section,
0029                      bool IsLittleEndian, uint8_t AddressSize)
0030       : DataExtractor(Section.Data, IsLittleEndian, AddressSize), Obj(&Obj),
0031         Section(&Section) {}
0032 
0033   /// Constructor for cases when there are no relocations.
0034   DWARFDataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
0035     : DataExtractor(Data, IsLittleEndian, AddressSize) {}
0036   DWARFDataExtractor(ArrayRef<uint8_t> Data, bool IsLittleEndian,
0037                      uint8_t AddressSize)
0038       : DataExtractor(
0039             StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()),
0040             IsLittleEndian, AddressSize) {}
0041 
0042   /// Truncating constructor
0043   DWARFDataExtractor(const DWARFDataExtractor &Other, size_t Length)
0044       : DataExtractor(Other.getData().substr(0, Length), Other.isLittleEndian(),
0045                       Other.getAddressSize()),
0046         Obj(Other.Obj), Section(Other.Section) {}
0047 
0048   /// Extracts the DWARF "initial length" field, which can either be a 32-bit
0049   /// value smaller than 0xfffffff0, or the value 0xffffffff followed by a
0050   /// 64-bit length. Returns the actual length, and the DWARF format which is
0051   /// encoded in the field. In case of errors, it returns {0, DWARF32} and
0052   /// leaves the offset unchanged.
0053   std::pair<uint64_t, dwarf::DwarfFormat>
0054   getInitialLength(uint64_t *Off, Error *Err = nullptr) const;
0055 
0056   std::pair<uint64_t, dwarf::DwarfFormat> getInitialLength(Cursor &C) const {
0057     return getInitialLength(&getOffset(C), &getError(C));
0058   }
0059 
0060   /// Extracts a value and applies a relocation to the result if
0061   /// one exists for the given offset.
0062   uint64_t getRelocatedValue(uint32_t Size, uint64_t *Off,
0063                              uint64_t *SectionIndex = nullptr,
0064                              Error *Err = nullptr) const;
0065   uint64_t getRelocatedValue(Cursor &C, uint32_t Size,
0066                              uint64_t *SectionIndex = nullptr) const {
0067     return getRelocatedValue(Size, &getOffset(C), SectionIndex, &getError(C));
0068   }
0069 
0070   /// Extracts an address-sized value and applies a relocation to the result if
0071   /// one exists for the given offset.
0072   uint64_t getRelocatedAddress(uint64_t *Off, uint64_t *SecIx = nullptr) const {
0073     return getRelocatedValue(getAddressSize(), Off, SecIx);
0074   }
0075   uint64_t getRelocatedAddress(Cursor &C, uint64_t *SecIx = nullptr) const {
0076     return getRelocatedValue(getAddressSize(), &getOffset(C), SecIx,
0077                              &getError(C));
0078   }
0079 
0080   /// Extracts a DWARF-encoded pointer in \p Offset using \p Encoding.
0081   /// There is a DWARF encoding that uses a PC-relative adjustment.
0082   /// For these values, \p AbsPosOffset is used to fix them, which should
0083   /// reflect the absolute address of this pointer.
0084   std::optional<uint64_t> getEncodedPointer(uint64_t *Offset, uint8_t Encoding,
0085                                             uint64_t AbsPosOffset = 0) const;
0086 };
0087 
0088 } // end namespace llvm
0089 
0090 #endif // LLVM_DEBUGINFO_DWARF_DWARFDATAEXTRACTOR_H