DWARFDebugFrame.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFDebugFrame.h - Parsing of .debug_frame --------------*- 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_DWARF_DWARFDEBUGFRAME_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGFRAME_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/iterator.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
  21. #include "llvm/Support/Error.h"
  22. #include <memory>
  23. #include <vector>
  24. namespace llvm {
  25. class raw_ostream;
  26. namespace dwarf {
  27. /// Represent a sequence of Call Frame Information instructions that, when read
  28. /// in order, construct a table mapping PC to frame state. This can also be
  29. /// referred to as "CFI rules" in DWARF literature to avoid confusion with
  30. /// computer programs in the broader sense, and in this context each instruction
  31. /// would be a rule to establish the mapping. Refer to pg. 172 in the DWARF5
  32. /// manual, "6.4.1 Structure of Call Frame Information".
  33. class CFIProgram {
  34. public:
  35. typedef SmallVector<uint64_t, 2> Operands;
  36. /// An instruction consists of a DWARF CFI opcode and an optional sequence of
  37. /// operands. If it refers to an expression, then this expression has its own
  38. /// sequence of operations and operands handled separately by DWARFExpression.
  39. struct Instruction {
  40. Instruction(uint8_t Opcode) : Opcode(Opcode) {}
  41. uint8_t Opcode;
  42. Operands Ops;
  43. // Associated DWARF expression in case this instruction refers to one
  44. Optional<DWARFExpression> Expression;
  45. };
  46. using InstrList = std::vector<Instruction>;
  47. using iterator = InstrList::iterator;
  48. using const_iterator = InstrList::const_iterator;
  49. iterator begin() { return Instructions.begin(); }
  50. const_iterator begin() const { return Instructions.begin(); }
  51. iterator end() { return Instructions.end(); }
  52. const_iterator end() const { return Instructions.end(); }
  53. unsigned size() const { return (unsigned)Instructions.size(); }
  54. bool empty() const { return Instructions.empty(); }
  55. CFIProgram(uint64_t CodeAlignmentFactor, int64_t DataAlignmentFactor,
  56. Triple::ArchType Arch)
  57. : CodeAlignmentFactor(CodeAlignmentFactor),
  58. DataAlignmentFactor(DataAlignmentFactor),
  59. Arch(Arch) {}
  60. /// Parse and store a sequence of CFI instructions from Data,
  61. /// starting at *Offset and ending at EndOffset. *Offset is updated
  62. /// to EndOffset upon successful parsing, or indicates the offset
  63. /// where a problem occurred in case an error is returned.
  64. Error parse(DWARFDataExtractor Data, uint64_t *Offset, uint64_t EndOffset);
  65. void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
  66. bool IsEH, unsigned IndentLevel = 1) const;
  67. private:
  68. std::vector<Instruction> Instructions;
  69. const uint64_t CodeAlignmentFactor;
  70. const int64_t DataAlignmentFactor;
  71. Triple::ArchType Arch;
  72. /// Convenience method to add a new instruction with the given opcode.
  73. void addInstruction(uint8_t Opcode) {
  74. Instructions.push_back(Instruction(Opcode));
  75. }
  76. /// Add a new single-operand instruction.
  77. void addInstruction(uint8_t Opcode, uint64_t Operand1) {
  78. Instructions.push_back(Instruction(Opcode));
  79. Instructions.back().Ops.push_back(Operand1);
  80. }
  81. /// Add a new instruction that has two operands.
  82. void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) {
  83. Instructions.push_back(Instruction(Opcode));
  84. Instructions.back().Ops.push_back(Operand1);
  85. Instructions.back().Ops.push_back(Operand2);
  86. }
  87. /// Types of operands to CFI instructions
  88. /// In DWARF, this type is implicitly tied to a CFI instruction opcode and
  89. /// thus this type doesn't need to be explictly written to the file (this is
  90. /// not a DWARF encoding). The relationship of instrs to operand types can
  91. /// be obtained from getOperandTypes() and is only used to simplify
  92. /// instruction printing.
  93. enum OperandType {
  94. OT_Unset,
  95. OT_None,
  96. OT_Address,
  97. OT_Offset,
  98. OT_FactoredCodeOffset,
  99. OT_SignedFactDataOffset,
  100. OT_UnsignedFactDataOffset,
  101. OT_Register,
  102. OT_Expression
  103. };
  104. /// Retrieve the array describing the types of operands according to the enum
  105. /// above. This is indexed by opcode.
  106. static ArrayRef<OperandType[2]> getOperandTypes();
  107. /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
  108. void printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
  109. const MCRegisterInfo *MRI, bool IsEH,
  110. const Instruction &Instr, unsigned OperandIdx,
  111. uint64_t Operand) const;
  112. };
  113. /// An entry in either debug_frame or eh_frame. This entry can be a CIE or an
  114. /// FDE.
  115. class FrameEntry {
  116. public:
  117. enum FrameKind { FK_CIE, FK_FDE };
  118. FrameEntry(FrameKind K, bool IsDWARF64, uint64_t Offset, uint64_t Length,
  119. uint64_t CodeAlign, int64_t DataAlign, Triple::ArchType Arch)
  120. : Kind(K), IsDWARF64(IsDWARF64), Offset(Offset), Length(Length),
  121. CFIs(CodeAlign, DataAlign, Arch) {}
  122. virtual ~FrameEntry() {}
  123. FrameKind getKind() const { return Kind; }
  124. uint64_t getOffset() const { return Offset; }
  125. uint64_t getLength() const { return Length; }
  126. const CFIProgram &cfis() const { return CFIs; }
  127. CFIProgram &cfis() { return CFIs; }
  128. /// Dump the instructions in this CFI fragment
  129. virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
  130. const MCRegisterInfo *MRI, bool IsEH) const = 0;
  131. protected:
  132. const FrameKind Kind;
  133. const bool IsDWARF64;
  134. /// Offset of this entry in the section.
  135. const uint64_t Offset;
  136. /// Entry length as specified in DWARF.
  137. const uint64_t Length;
  138. CFIProgram CFIs;
  139. };
  140. /// DWARF Common Information Entry (CIE)
  141. class CIE : public FrameEntry {
  142. public:
  143. // CIEs (and FDEs) are simply container classes, so the only sensible way to
  144. // create them is by providing the full parsed contents in the constructor.
  145. CIE(bool IsDWARF64, uint64_t Offset, uint64_t Length, uint8_t Version,
  146. SmallString<8> Augmentation, uint8_t AddressSize,
  147. uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor,
  148. int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister,
  149. SmallString<8> AugmentationData, uint32_t FDEPointerEncoding,
  150. uint32_t LSDAPointerEncoding, Optional<uint64_t> Personality,
  151. Optional<uint32_t> PersonalityEnc, Triple::ArchType Arch)
  152. : FrameEntry(FK_CIE, IsDWARF64, Offset, Length, CodeAlignmentFactor,
  153. DataAlignmentFactor, Arch),
  154. Version(Version), Augmentation(std::move(Augmentation)),
  155. AddressSize(AddressSize), SegmentDescriptorSize(SegmentDescriptorSize),
  156. CodeAlignmentFactor(CodeAlignmentFactor),
  157. DataAlignmentFactor(DataAlignmentFactor),
  158. ReturnAddressRegister(ReturnAddressRegister),
  159. AugmentationData(std::move(AugmentationData)),
  160. FDEPointerEncoding(FDEPointerEncoding),
  161. LSDAPointerEncoding(LSDAPointerEncoding), Personality(Personality),
  162. PersonalityEnc(PersonalityEnc) {}
  163. static bool classof(const FrameEntry *FE) { return FE->getKind() == FK_CIE; }
  164. StringRef getAugmentationString() const { return Augmentation; }
  165. uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; }
  166. int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; }
  167. uint8_t getVersion() const { return Version; }
  168. uint64_t getReturnAddressRegister() const { return ReturnAddressRegister; }
  169. Optional<uint64_t> getPersonalityAddress() const { return Personality; }
  170. Optional<uint32_t> getPersonalityEncoding() const { return PersonalityEnc; }
  171. uint32_t getFDEPointerEncoding() const { return FDEPointerEncoding; }
  172. uint32_t getLSDAPointerEncoding() const { return LSDAPointerEncoding; }
  173. void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
  174. bool IsEH) const override;
  175. private:
  176. /// The following fields are defined in section 6.4.1 of the DWARF standard v4
  177. const uint8_t Version;
  178. const SmallString<8> Augmentation;
  179. const uint8_t AddressSize;
  180. const uint8_t SegmentDescriptorSize;
  181. const uint64_t CodeAlignmentFactor;
  182. const int64_t DataAlignmentFactor;
  183. const uint64_t ReturnAddressRegister;
  184. // The following are used when the CIE represents an EH frame entry.
  185. const SmallString<8> AugmentationData;
  186. const uint32_t FDEPointerEncoding;
  187. const uint32_t LSDAPointerEncoding;
  188. const Optional<uint64_t> Personality;
  189. const Optional<uint32_t> PersonalityEnc;
  190. };
  191. /// DWARF Frame Description Entry (FDE)
  192. class FDE : public FrameEntry {
  193. public:
  194. FDE(bool IsDWARF64, uint64_t Offset, uint64_t Length, uint64_t CIEPointer,
  195. uint64_t InitialLocation, uint64_t AddressRange, CIE *Cie,
  196. Optional<uint64_t> LSDAAddress, Triple::ArchType Arch)
  197. : FrameEntry(FK_FDE, IsDWARF64, Offset, Length,
  198. Cie ? Cie->getCodeAlignmentFactor() : 0,
  199. Cie ? Cie->getDataAlignmentFactor() : 0,
  200. Arch),
  201. CIEPointer(CIEPointer), InitialLocation(InitialLocation),
  202. AddressRange(AddressRange), LinkedCIE(Cie), LSDAAddress(LSDAAddress) {}
  203. ~FDE() override = default;
  204. const CIE *getLinkedCIE() const { return LinkedCIE; }
  205. uint64_t getInitialLocation() const { return InitialLocation; }
  206. uint64_t getAddressRange() const { return AddressRange; }
  207. Optional<uint64_t> getLSDAAddress() const { return LSDAAddress; }
  208. void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
  209. bool IsEH) const override;
  210. static bool classof(const FrameEntry *FE) { return FE->getKind() == FK_FDE; }
  211. private:
  212. /// The following fields are defined in section 6.4.1 of the DWARFv3 standard.
  213. /// Note that CIE pointers in EH FDEs, unlike DWARF FDEs, contain relative
  214. /// offsets to the linked CIEs. See the following link for more info:
  215. /// https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
  216. const uint64_t CIEPointer;
  217. const uint64_t InitialLocation;
  218. const uint64_t AddressRange;
  219. const CIE *LinkedCIE;
  220. const Optional<uint64_t> LSDAAddress;
  221. };
  222. } // end namespace dwarf
  223. /// A parsed .debug_frame or .eh_frame section
  224. class DWARFDebugFrame {
  225. const Triple::ArchType Arch;
  226. // True if this is parsing an eh_frame section.
  227. const bool IsEH;
  228. // Not zero for sane pointer values coming out of eh_frame
  229. const uint64_t EHFrameAddress;
  230. std::vector<std::unique_ptr<dwarf::FrameEntry>> Entries;
  231. using iterator = pointee_iterator<decltype(Entries)::const_iterator>;
  232. /// Return the entry at the given offset or nullptr.
  233. dwarf::FrameEntry *getEntryAtOffset(uint64_t Offset) const;
  234. public:
  235. // If IsEH is true, assume it is a .eh_frame section. Otherwise,
  236. // it is a .debug_frame section. EHFrameAddress should be different
  237. // than zero for correct parsing of .eh_frame addresses when they
  238. // use a PC-relative encoding.
  239. DWARFDebugFrame(Triple::ArchType Arch,
  240. bool IsEH = false, uint64_t EHFrameAddress = 0);
  241. ~DWARFDebugFrame();
  242. /// Dump the section data into the given stream.
  243. void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
  244. Optional<uint64_t> Offset) const;
  245. /// Parse the section from raw data. \p Data is assumed to contain the whole
  246. /// frame section contents to be parsed.
  247. Error parse(DWARFDataExtractor Data);
  248. /// Return whether the section has any entries.
  249. bool empty() const { return Entries.empty(); }
  250. /// DWARF Frame entries accessors
  251. iterator begin() const { return Entries.begin(); }
  252. iterator end() const { return Entries.end(); }
  253. iterator_range<iterator> entries() const {
  254. return iterator_range<iterator>(Entries.begin(), Entries.end());
  255. }
  256. uint64_t getEHFrameAddress() const { return EHFrameAddress; }
  257. };
  258. } // end namespace llvm
  259. #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGFRAME_H
  260. #ifdef __GNUC__
  261. #pragma GCC diagnostic pop
  262. #endif