DWARFDataExtractor.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFDataExtractor.h -------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_DWARFDATAEXTRACTOR_H
  14. #define LLVM_DEBUGINFO_DWARFDATAEXTRACTOR_H
  15. #include "llvm/BinaryFormat/Dwarf.h"
  16. #include "llvm/DebugInfo/DWARF/DWARFSection.h"
  17. #include "llvm/Support/DataExtractor.h"
  18. namespace llvm {
  19. class DWARFObject;
  20. /// A DataExtractor (typically for an in-memory copy of an object-file section)
  21. /// plus a relocation map for that section, if there is one.
  22. class DWARFDataExtractor : public DataExtractor {
  23. const DWARFObject *Obj = nullptr;
  24. const DWARFSection *Section = nullptr;
  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. /// Constructor for cases when there are no relocations.
  33. DWARFDataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
  34. : DataExtractor(Data, IsLittleEndian, AddressSize) {}
  35. DWARFDataExtractor(ArrayRef<uint8_t> Data, bool IsLittleEndian,
  36. uint8_t AddressSize)
  37. : DataExtractor(
  38. StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()),
  39. IsLittleEndian, AddressSize) {}
  40. /// Truncating constructor
  41. DWARFDataExtractor(const DWARFDataExtractor &Other, size_t Length)
  42. : DataExtractor(Other.getData().substr(0, Length), Other.isLittleEndian(),
  43. Other.getAddressSize()),
  44. Obj(Other.Obj), Section(Other.Section) {}
  45. /// Extracts the DWARF "initial length" field, which can either be a 32-bit
  46. /// value smaller than 0xfffffff0, or the value 0xffffffff followed by a
  47. /// 64-bit length. Returns the actual length, and the DWARF format which is
  48. /// encoded in the field. In case of errors, it returns {0, DWARF32} and
  49. /// leaves the offset unchanged.
  50. std::pair<uint64_t, dwarf::DwarfFormat>
  51. getInitialLength(uint64_t *Off, Error *Err = nullptr) const;
  52. std::pair<uint64_t, dwarf::DwarfFormat> getInitialLength(Cursor &C) const {
  53. return getInitialLength(&getOffset(C), &getError(C));
  54. }
  55. /// Extracts a value and applies a relocation to the result if
  56. /// one exists for the given offset.
  57. uint64_t getRelocatedValue(uint32_t Size, uint64_t *Off,
  58. uint64_t *SectionIndex = nullptr,
  59. Error *Err = nullptr) const;
  60. uint64_t getRelocatedValue(Cursor &C, uint32_t Size,
  61. uint64_t *SectionIndex = nullptr) const {
  62. return getRelocatedValue(Size, &getOffset(C), SectionIndex, &getError(C));
  63. }
  64. /// Extracts an address-sized value and applies a relocation to the result if
  65. /// one exists for the given offset.
  66. uint64_t getRelocatedAddress(uint64_t *Off, uint64_t *SecIx = nullptr) const {
  67. return getRelocatedValue(getAddressSize(), Off, SecIx);
  68. }
  69. uint64_t getRelocatedAddress(Cursor &C, uint64_t *SecIx = nullptr) const {
  70. return getRelocatedValue(getAddressSize(), &getOffset(C), SecIx,
  71. &getError(C));
  72. }
  73. /// Extracts a DWARF-encoded pointer in \p Offset using \p Encoding.
  74. /// There is a DWARF encoding that uses a PC-relative adjustment.
  75. /// For these values, \p AbsPosOffset is used to fix them, which should
  76. /// reflect the absolute address of this pointer.
  77. Optional<uint64_t> getEncodedPointer(uint64_t *Offset, uint8_t Encoding,
  78. uint64_t AbsPosOffset = 0) const;
  79. };
  80. } // end namespace llvm
  81. #endif // LLVM_DEBUGINFO_DWARFDATAEXTRACTOR_H
  82. #ifdef __GNUC__
  83. #pragma GCC diagnostic pop
  84. #endif