DWARFDebugFrame.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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/SmallString.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/ADT/iterator.h"
  19. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
  21. #include "llvm/Support/Error.h"
  22. #include <map>
  23. #include <memory>
  24. #include <vector>
  25. namespace llvm {
  26. class raw_ostream;
  27. namespace dwarf {
  28. constexpr uint32_t InvalidRegisterNumber = UINT32_MAX;
  29. /// A class that represents a location for the Call Frame Address (CFA) or a
  30. /// register. This is decoded from the DWARF Call Frame Information
  31. /// instructions and put into an UnwindRow.
  32. class UnwindLocation {
  33. public:
  34. enum Location {
  35. /// Not specified.
  36. Unspecified,
  37. /// Register is not available and can't be recovered.
  38. Undefined,
  39. /// Register value is in the register, nothing needs to be done to unwind
  40. /// it:
  41. /// reg = reg
  42. Same,
  43. /// Register is in or at the CFA plus an offset:
  44. /// reg = CFA + offset
  45. /// reg = defef(CFA + offset)
  46. CFAPlusOffset,
  47. /// Register or CFA is in or at a register plus offset, optionally in
  48. /// an address space:
  49. /// reg = reg + offset [in addrspace]
  50. /// reg = deref(reg + offset [in addrspace])
  51. RegPlusOffset,
  52. /// Register or CFA value is in or at a value found by evaluating a DWARF
  53. /// expression:
  54. /// reg = eval(dwarf_expr)
  55. /// reg = deref(eval(dwarf_expr))
  56. DWARFExpr,
  57. /// Value is a constant value contained in "Offset":
  58. /// reg = Offset
  59. Constant,
  60. };
  61. private:
  62. Location Kind; /// The type of the location that describes how to unwind it.
  63. uint32_t RegNum; /// The register number for Kind == RegPlusOffset.
  64. int32_t Offset; /// The offset for Kind == CFAPlusOffset or RegPlusOffset.
  65. Optional<uint32_t> AddrSpace; /// The address space for Kind == RegPlusOffset
  66. /// for CFA.
  67. Optional<DWARFExpression> Expr; /// The DWARF expression for Kind ==
  68. /// DWARFExpression.
  69. bool Dereference; /// If true, the resulting location must be dereferenced
  70. /// after the location value is computed.
  71. // Constructors are private to force people to use the create static
  72. // functions.
  73. UnwindLocation(Location K)
  74. : Kind(K), RegNum(InvalidRegisterNumber), Offset(0), AddrSpace(None),
  75. Dereference(false) {}
  76. UnwindLocation(Location K, uint32_t Reg, int32_t Off, Optional<uint32_t> AS,
  77. bool Deref)
  78. : Kind(K), RegNum(Reg), Offset(Off), AddrSpace(AS), Dereference(Deref) {}
  79. UnwindLocation(DWARFExpression E, bool Deref)
  80. : Kind(DWARFExpr), RegNum(InvalidRegisterNumber), Offset(0), Expr(E),
  81. Dereference(Deref) {}
  82. public:
  83. /// Create a location whose rule is set to Unspecified. This means the
  84. /// register value might be in the same register but it wasn't specified in
  85. /// the unwind opcodes.
  86. static UnwindLocation createUnspecified();
  87. /// Create a location where the value is undefined and not available. This can
  88. /// happen when a register is volatile and can't be recovered.
  89. static UnwindLocation createUndefined();
  90. /// Create a location where the value is known to be in the register itself.
  91. static UnwindLocation createSame();
  92. /// Create a location that is in (Deref == false) or at (Deref == true) the
  93. /// CFA plus an offset. Most registers that are spilled onto the stack use
  94. /// this rule. The rule for the register will use this rule and specify a
  95. /// unique offset from the CFA with \a Deref set to true. This value will be
  96. /// relative to a CFA value which is typically defined using the register
  97. /// plus offset location. \see createRegisterPlusOffset(...) for more
  98. /// information.
  99. static UnwindLocation createIsCFAPlusOffset(int32_t Off);
  100. static UnwindLocation createAtCFAPlusOffset(int32_t Off);
  101. /// Create a location where the saved value is in (Deref == false) or at
  102. /// (Deref == true) a regiser plus an offset and, optionally, in the specified
  103. /// address space (used mostly for the CFA).
  104. ///
  105. /// The CFA is usually defined using this rule by using the stack pointer or
  106. /// frame pointer as the register, with an offset that accounts for all
  107. /// spilled registers and all local variables in a function, and Deref ==
  108. /// false.
  109. static UnwindLocation
  110. createIsRegisterPlusOffset(uint32_t Reg, int32_t Off,
  111. Optional<uint32_t> AddrSpace = None);
  112. static UnwindLocation
  113. createAtRegisterPlusOffset(uint32_t Reg, int32_t Off,
  114. Optional<uint32_t> AddrSpace = None);
  115. /// Create a location whose value is the result of evaluating a DWARF
  116. /// expression. This allows complex expressions to be evaluated in order to
  117. /// unwind a register or CFA value.
  118. static UnwindLocation createIsDWARFExpression(DWARFExpression Expr);
  119. static UnwindLocation createAtDWARFExpression(DWARFExpression Expr);
  120. static UnwindLocation createIsConstant(int32_t Value);
  121. Location getLocation() const { return Kind; }
  122. uint32_t getRegister() const { return RegNum; }
  123. int32_t getOffset() const { return Offset; }
  124. uint32_t getAddressSpace() const {
  125. assert(Kind == RegPlusOffset && AddrSpace.hasValue());
  126. return *AddrSpace;
  127. }
  128. int32_t getConstant() const { return Offset; }
  129. /// Some opcodes will modify the CFA location's register only, so we need
  130. /// to be able to modify the CFA register when evaluating DWARF Call Frame
  131. /// Information opcodes.
  132. void setRegister(uint32_t NewRegNum) { RegNum = NewRegNum; }
  133. /// Some opcodes will modify the CFA location's offset only, so we need
  134. /// to be able to modify the CFA offset when evaluating DWARF Call Frame
  135. /// Information opcodes.
  136. void setOffset(int32_t NewOffset) { Offset = NewOffset; }
  137. /// Some opcodes modify a constant value and we need to be able to update
  138. /// the constant value (DW_CFA_GNU_window_save which is also known as
  139. // DW_CFA_AARCH64_negate_ra_state).
  140. void setConstant(int32_t Value) { Offset = Value; }
  141. Optional<DWARFExpression> getDWARFExpressionBytes() const { return Expr; }
  142. /// Dump a location expression as text and use the register information if
  143. /// some is provided.
  144. ///
  145. /// \param OS the stream to use for output.
  146. ///
  147. /// \param MRI register information that helps emit register names insteead
  148. /// of raw register numbers.
  149. ///
  150. /// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
  151. /// instead of from .debug_frame. This is needed for register number
  152. /// conversion because some register numbers differ between the two sections
  153. /// for certain architectures like x86.
  154. void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const;
  155. bool operator==(const UnwindLocation &RHS) const;
  156. };
  157. raw_ostream &operator<<(raw_ostream &OS, const UnwindLocation &R);
  158. /// A class that can track all registers with locations in a UnwindRow object.
  159. ///
  160. /// Register locations use a map where the key is the register number and the
  161. /// the value is a UnwindLocation.
  162. ///
  163. /// The register maps are put into a class so that all register locations can
  164. /// be copied when parsing the unwind opcodes DW_CFA_remember_state and
  165. /// DW_CFA_restore_state.
  166. class RegisterLocations {
  167. std::map<uint32_t, UnwindLocation> Locations;
  168. public:
  169. /// Return the location for the register in \a RegNum if there is a location.
  170. ///
  171. /// \param RegNum the register number to find a location for.
  172. ///
  173. /// \returns A location if one is available for \a RegNum, or llvm::None
  174. /// otherwise.
  175. Optional<UnwindLocation> getRegisterLocation(uint32_t RegNum) const {
  176. auto Pos = Locations.find(RegNum);
  177. if (Pos == Locations.end())
  178. return llvm::None;
  179. return Pos->second;
  180. }
  181. /// Set the location for the register in \a RegNum to \a Location.
  182. ///
  183. /// \param RegNum the register number to set the location for.
  184. ///
  185. /// \param Location the UnwindLocation that describes how to unwind the value.
  186. void setRegisterLocation(uint32_t RegNum, const UnwindLocation &Location) {
  187. Locations.erase(RegNum);
  188. Locations.insert(std::make_pair(RegNum, Location));
  189. }
  190. /// Removes any rule for the register in \a RegNum.
  191. ///
  192. /// \param RegNum the register number to remove the location for.
  193. void removeRegisterLocation(uint32_t RegNum) { Locations.erase(RegNum); }
  194. /// Dump all registers + locations that are currently defined in this object.
  195. ///
  196. /// \param OS the stream to use for output.
  197. ///
  198. /// \param MRI register information that helps emit register names insteead
  199. /// of raw register numbers.
  200. ///
  201. /// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
  202. /// instead of from .debug_frame. This is needed for register number
  203. /// conversion because some register numbers differ between the two sections
  204. /// for certain architectures like x86.
  205. void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const;
  206. /// Returns true if we have any register locations in this object.
  207. bool hasLocations() const { return !Locations.empty(); }
  208. size_t size() const { return Locations.size(); }
  209. bool operator==(const RegisterLocations &RHS) const {
  210. return Locations == RHS.Locations;
  211. }
  212. };
  213. raw_ostream &operator<<(raw_ostream &OS, const RegisterLocations &RL);
  214. /// A class that represents a single row in the unwind table that is decoded by
  215. /// parsing the DWARF Call Frame Information opcodes.
  216. ///
  217. /// The row consists of an optional address, the rule to unwind the CFA and all
  218. /// rules to unwind any registers. If the address doesn't have a value, this
  219. /// row represents the initial instructions for a CIE. If the address has a
  220. /// value the UnwindRow represents a row in the UnwindTable for a FDE. The
  221. /// address is the first address for which the CFA location and register rules
  222. /// are valid within a function.
  223. ///
  224. /// UnwindRow objects are created by parsing opcodes in the DWARF Call Frame
  225. /// Information and UnwindRow objects are lazily populated and pushed onto a
  226. /// stack in the UnwindTable when evaluating this state machine. Accessors are
  227. /// needed for the address, CFA value, and register locations as the opcodes
  228. /// encode a state machine that produces a sorted array of UnwindRow objects
  229. /// \see UnwindTable.
  230. class UnwindRow {
  231. /// The address will be valid when parsing the instructions in a FDE. If
  232. /// invalid, this object represents the initial instructions of a CIE.
  233. Optional<uint64_t> Address; ///< Address for row in FDE, invalid for CIE.
  234. UnwindLocation CFAValue; ///< How to unwind the Call Frame Address (CFA).
  235. RegisterLocations RegLocs; ///< How to unwind all registers in this list.
  236. public:
  237. UnwindRow() : CFAValue(UnwindLocation::createUnspecified()) {}
  238. /// Returns true if the address is valid in this object.
  239. bool hasAddress() const { return Address.hasValue(); }
  240. /// Get the address for this row.
  241. ///
  242. /// Clients should only call this function after verifying it has a valid
  243. /// address with a call to \see hasAddress().
  244. uint64_t getAddress() const { return *Address; }
  245. /// Set the address for this UnwindRow.
  246. ///
  247. /// The address represents the first address for which the CFAValue and
  248. /// RegLocs are valid within a function.
  249. void setAddress(uint64_t Addr) { Address = Addr; }
  250. /// Offset the address for this UnwindRow.
  251. ///
  252. /// The address represents the first address for which the CFAValue and
  253. /// RegLocs are valid within a function. Clients must ensure that this object
  254. /// already has an address (\see hasAddress()) prior to calling this
  255. /// function.
  256. void slideAddress(uint64_t Offset) { *Address += Offset; }
  257. UnwindLocation &getCFAValue() { return CFAValue; }
  258. const UnwindLocation &getCFAValue() const { return CFAValue; }
  259. RegisterLocations &getRegisterLocations() { return RegLocs; }
  260. const RegisterLocations &getRegisterLocations() const { return RegLocs; }
  261. /// Dump the UnwindRow to the stream.
  262. ///
  263. /// \param OS the stream to use for output.
  264. ///
  265. /// \param MRI register information that helps emit register names insteead
  266. /// of raw register numbers.
  267. ///
  268. /// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
  269. /// instead of from .debug_frame. This is needed for register number
  270. /// conversion because some register numbers differ between the two sections
  271. /// for certain architectures like x86.
  272. ///
  273. /// \param IndentLevel specify the indent level as an integer. The UnwindRow
  274. /// will be output to the stream preceded by 2 * IndentLevel number of spaces.
  275. void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
  276. unsigned IndentLevel = 0) const;
  277. };
  278. raw_ostream &operator<<(raw_ostream &OS, const UnwindRow &Row);
  279. class CFIProgram;
  280. class CIE;
  281. class FDE;
  282. /// A class that contains all UnwindRow objects for an FDE or a single unwind
  283. /// row for a CIE. To unwind an address the rows, which are sorted by start
  284. /// address, can be searched to find the UnwindRow with the lowest starting
  285. /// address that is greater than or equal to the address that is being looked
  286. /// up.
  287. class UnwindTable {
  288. public:
  289. using RowContainer = std::vector<UnwindRow>;
  290. using iterator = RowContainer::iterator;
  291. using const_iterator = RowContainer::const_iterator;
  292. size_t size() const { return Rows.size(); }
  293. iterator begin() { return Rows.begin(); }
  294. const_iterator begin() const { return Rows.begin(); }
  295. iterator end() { return Rows.end(); }
  296. const_iterator end() const { return Rows.end(); }
  297. const UnwindRow &operator[](size_t Index) const {
  298. assert(Index < size());
  299. return Rows[Index];
  300. }
  301. /// Dump the UnwindTable to the stream.
  302. ///
  303. /// \param OS the stream to use for output.
  304. ///
  305. /// \param MRI register information that helps emit register names insteead
  306. /// of raw register numbers.
  307. ///
  308. /// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
  309. /// instead of from .debug_frame. This is needed for register number
  310. /// conversion because some register numbers differ between the two sections
  311. /// for certain architectures like x86.
  312. ///
  313. /// \param IndentLevel specify the indent level as an integer. The UnwindRow
  314. /// will be output to the stream preceded by 2 * IndentLevel number of spaces.
  315. void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
  316. unsigned IndentLevel = 0) const;
  317. /// Create an UnwindTable from a Common Information Entry (CIE).
  318. ///
  319. /// \param Cie The Common Information Entry to extract the table from. The
  320. /// CFIProgram is retrieved from the \a Cie object and used to create the
  321. /// UnwindTable.
  322. ///
  323. /// \returns An error if the DWARF Call Frame Information opcodes have state
  324. /// machine errors, or a valid UnwindTable otherwise.
  325. static Expected<UnwindTable> create(const CIE *Cie);
  326. /// Create an UnwindTable from a Frame Descriptor Entry (FDE).
  327. ///
  328. /// \param Fde The Frame Descriptor Entry to extract the table from. The
  329. /// CFIProgram is retrieved from the \a Fde object and used to create the
  330. /// UnwindTable.
  331. ///
  332. /// \returns An error if the DWARF Call Frame Information opcodes have state
  333. /// machine errors, or a valid UnwindTable otherwise.
  334. static Expected<UnwindTable> create(const FDE *Fde);
  335. private:
  336. RowContainer Rows;
  337. /// The end address when data is extracted from a FDE. This value will be
  338. /// invalid when a UnwindTable is extracted from a CIE.
  339. Optional<uint64_t> EndAddress;
  340. /// Parse the information in the CFIProgram and update the CurrRow object
  341. /// that the state machine describes.
  342. ///
  343. /// This is an internal implementation that emulates the state machine
  344. /// described in the DWARF Call Frame Information opcodes and will push
  345. /// CurrRow onto the Rows container when needed.
  346. ///
  347. /// \param CFIP the CFI program that contains the opcodes from a CIE or FDE.
  348. ///
  349. /// \param CurrRow the current row to modify while parsing the state machine.
  350. ///
  351. /// \param InitialLocs If non-NULL, we are parsing a FDE and this contains
  352. /// the initial register locations from the CIE. If NULL, then a CIE's
  353. /// opcodes are being parsed and this is not needed. This is used for the
  354. /// DW_CFA_restore and DW_CFA_restore_extended opcodes.
  355. Error parseRows(const CFIProgram &CFIP, UnwindRow &CurrRow,
  356. const RegisterLocations *InitialLocs);
  357. };
  358. raw_ostream &operator<<(raw_ostream &OS, const UnwindTable &Rows);
  359. /// Represent a sequence of Call Frame Information instructions that, when read
  360. /// in order, construct a table mapping PC to frame state. This can also be
  361. /// referred to as "CFI rules" in DWARF literature to avoid confusion with
  362. /// computer programs in the broader sense, and in this context each instruction
  363. /// would be a rule to establish the mapping. Refer to pg. 172 in the DWARF5
  364. /// manual, "6.4.1 Structure of Call Frame Information".
  365. class CFIProgram {
  366. public:
  367. static constexpr size_t MaxOperands = 3;
  368. typedef SmallVector<uint64_t, MaxOperands> Operands;
  369. /// An instruction consists of a DWARF CFI opcode and an optional sequence of
  370. /// operands. If it refers to an expression, then this expression has its own
  371. /// sequence of operations and operands handled separately by DWARFExpression.
  372. struct Instruction {
  373. Instruction(uint8_t Opcode) : Opcode(Opcode) {}
  374. uint8_t Opcode;
  375. Operands Ops;
  376. // Associated DWARF expression in case this instruction refers to one
  377. Optional<DWARFExpression> Expression;
  378. Expected<uint64_t> getOperandAsUnsigned(const CFIProgram &CFIP,
  379. uint32_t OperandIdx) const;
  380. Expected<int64_t> getOperandAsSigned(const CFIProgram &CFIP,
  381. uint32_t OperandIdx) const;
  382. };
  383. using InstrList = std::vector<Instruction>;
  384. using iterator = InstrList::iterator;
  385. using const_iterator = InstrList::const_iterator;
  386. iterator begin() { return Instructions.begin(); }
  387. const_iterator begin() const { return Instructions.begin(); }
  388. iterator end() { return Instructions.end(); }
  389. const_iterator end() const { return Instructions.end(); }
  390. unsigned size() const { return (unsigned)Instructions.size(); }
  391. bool empty() const { return Instructions.empty(); }
  392. uint64_t codeAlign() const { return CodeAlignmentFactor; }
  393. int64_t dataAlign() const { return DataAlignmentFactor; }
  394. Triple::ArchType triple() const { return Arch; }
  395. CFIProgram(uint64_t CodeAlignmentFactor, int64_t DataAlignmentFactor,
  396. Triple::ArchType Arch)
  397. : CodeAlignmentFactor(CodeAlignmentFactor),
  398. DataAlignmentFactor(DataAlignmentFactor),
  399. Arch(Arch) {}
  400. /// Parse and store a sequence of CFI instructions from Data,
  401. /// starting at *Offset and ending at EndOffset. *Offset is updated
  402. /// to EndOffset upon successful parsing, or indicates the offset
  403. /// where a problem occurred in case an error is returned.
  404. Error parse(DWARFDataExtractor Data, uint64_t *Offset, uint64_t EndOffset);
  405. void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
  406. bool IsEH, unsigned IndentLevel = 1) const;
  407. void addInstruction(const Instruction &I) { Instructions.push_back(I); }
  408. /// Get a DWARF CFI call frame string for the given DW_CFA opcode.
  409. StringRef callFrameString(unsigned Opcode) const;
  410. private:
  411. std::vector<Instruction> Instructions;
  412. const uint64_t CodeAlignmentFactor;
  413. const int64_t DataAlignmentFactor;
  414. Triple::ArchType Arch;
  415. /// Convenience method to add a new instruction with the given opcode.
  416. void addInstruction(uint8_t Opcode) {
  417. Instructions.push_back(Instruction(Opcode));
  418. }
  419. /// Add a new single-operand instruction.
  420. void addInstruction(uint8_t Opcode, uint64_t Operand1) {
  421. Instructions.push_back(Instruction(Opcode));
  422. Instructions.back().Ops.push_back(Operand1);
  423. }
  424. /// Add a new instruction that has two operands.
  425. void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) {
  426. Instructions.push_back(Instruction(Opcode));
  427. Instructions.back().Ops.push_back(Operand1);
  428. Instructions.back().Ops.push_back(Operand2);
  429. }
  430. /// Add a new instruction that has three operands.
  431. void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2,
  432. uint64_t Operand3) {
  433. Instructions.push_back(Instruction(Opcode));
  434. Instructions.back().Ops.push_back(Operand1);
  435. Instructions.back().Ops.push_back(Operand2);
  436. Instructions.back().Ops.push_back(Operand3);
  437. }
  438. /// Types of operands to CFI instructions
  439. /// In DWARF, this type is implicitly tied to a CFI instruction opcode and
  440. /// thus this type doesn't need to be explictly written to the file (this is
  441. /// not a DWARF encoding). The relationship of instrs to operand types can
  442. /// be obtained from getOperandTypes() and is only used to simplify
  443. /// instruction printing.
  444. enum OperandType {
  445. OT_Unset,
  446. OT_None,
  447. OT_Address,
  448. OT_Offset,
  449. OT_FactoredCodeOffset,
  450. OT_SignedFactDataOffset,
  451. OT_UnsignedFactDataOffset,
  452. OT_Register,
  453. OT_AddressSpace,
  454. OT_Expression
  455. };
  456. /// Get the OperandType as a "const char *".
  457. static const char *operandTypeString(OperandType OT);
  458. /// Retrieve the array describing the types of operands according to the enum
  459. /// above. This is indexed by opcode.
  460. static ArrayRef<OperandType[MaxOperands]> getOperandTypes();
  461. /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
  462. void printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
  463. const MCRegisterInfo *MRI, bool IsEH,
  464. const Instruction &Instr, unsigned OperandIdx,
  465. uint64_t Operand) const;
  466. };
  467. /// An entry in either debug_frame or eh_frame. This entry can be a CIE or an
  468. /// FDE.
  469. class FrameEntry {
  470. public:
  471. enum FrameKind { FK_CIE, FK_FDE };
  472. FrameEntry(FrameKind K, bool IsDWARF64, uint64_t Offset, uint64_t Length,
  473. uint64_t CodeAlign, int64_t DataAlign, Triple::ArchType Arch)
  474. : Kind(K), IsDWARF64(IsDWARF64), Offset(Offset), Length(Length),
  475. CFIs(CodeAlign, DataAlign, Arch) {}
  476. virtual ~FrameEntry() = default;
  477. FrameKind getKind() const { return Kind; }
  478. uint64_t getOffset() const { return Offset; }
  479. uint64_t getLength() const { return Length; }
  480. const CFIProgram &cfis() const { return CFIs; }
  481. CFIProgram &cfis() { return CFIs; }
  482. /// Dump the instructions in this CFI fragment
  483. virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
  484. const MCRegisterInfo *MRI, bool IsEH) const = 0;
  485. protected:
  486. const FrameKind Kind;
  487. const bool IsDWARF64;
  488. /// Offset of this entry in the section.
  489. const uint64_t Offset;
  490. /// Entry length as specified in DWARF.
  491. const uint64_t Length;
  492. CFIProgram CFIs;
  493. };
  494. /// DWARF Common Information Entry (CIE)
  495. class CIE : public FrameEntry {
  496. public:
  497. // CIEs (and FDEs) are simply container classes, so the only sensible way to
  498. // create them is by providing the full parsed contents in the constructor.
  499. CIE(bool IsDWARF64, uint64_t Offset, uint64_t Length, uint8_t Version,
  500. SmallString<8> Augmentation, uint8_t AddressSize,
  501. uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor,
  502. int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister,
  503. SmallString<8> AugmentationData, uint32_t FDEPointerEncoding,
  504. uint32_t LSDAPointerEncoding, Optional<uint64_t> Personality,
  505. Optional<uint32_t> PersonalityEnc, Triple::ArchType Arch)
  506. : FrameEntry(FK_CIE, IsDWARF64, Offset, Length, CodeAlignmentFactor,
  507. DataAlignmentFactor, Arch),
  508. Version(Version), Augmentation(std::move(Augmentation)),
  509. AddressSize(AddressSize), SegmentDescriptorSize(SegmentDescriptorSize),
  510. CodeAlignmentFactor(CodeAlignmentFactor),
  511. DataAlignmentFactor(DataAlignmentFactor),
  512. ReturnAddressRegister(ReturnAddressRegister),
  513. AugmentationData(std::move(AugmentationData)),
  514. FDEPointerEncoding(FDEPointerEncoding),
  515. LSDAPointerEncoding(LSDAPointerEncoding), Personality(Personality),
  516. PersonalityEnc(PersonalityEnc) {}
  517. static bool classof(const FrameEntry *FE) { return FE->getKind() == FK_CIE; }
  518. StringRef getAugmentationString() const { return Augmentation; }
  519. uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; }
  520. int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; }
  521. uint8_t getVersion() const { return Version; }
  522. uint64_t getReturnAddressRegister() const { return ReturnAddressRegister; }
  523. Optional<uint64_t> getPersonalityAddress() const { return Personality; }
  524. Optional<uint32_t> getPersonalityEncoding() const { return PersonalityEnc; }
  525. uint32_t getFDEPointerEncoding() const { return FDEPointerEncoding; }
  526. uint32_t getLSDAPointerEncoding() const { return LSDAPointerEncoding; }
  527. void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
  528. bool IsEH) const override;
  529. private:
  530. /// The following fields are defined in section 6.4.1 of the DWARF standard v4
  531. const uint8_t Version;
  532. const SmallString<8> Augmentation;
  533. const uint8_t AddressSize;
  534. const uint8_t SegmentDescriptorSize;
  535. const uint64_t CodeAlignmentFactor;
  536. const int64_t DataAlignmentFactor;
  537. const uint64_t ReturnAddressRegister;
  538. // The following are used when the CIE represents an EH frame entry.
  539. const SmallString<8> AugmentationData;
  540. const uint32_t FDEPointerEncoding;
  541. const uint32_t LSDAPointerEncoding;
  542. const Optional<uint64_t> Personality;
  543. const Optional<uint32_t> PersonalityEnc;
  544. };
  545. /// DWARF Frame Description Entry (FDE)
  546. class FDE : public FrameEntry {
  547. public:
  548. FDE(bool IsDWARF64, uint64_t Offset, uint64_t Length, uint64_t CIEPointer,
  549. uint64_t InitialLocation, uint64_t AddressRange, CIE *Cie,
  550. Optional<uint64_t> LSDAAddress, Triple::ArchType Arch)
  551. : FrameEntry(FK_FDE, IsDWARF64, Offset, Length,
  552. Cie ? Cie->getCodeAlignmentFactor() : 0,
  553. Cie ? Cie->getDataAlignmentFactor() : 0,
  554. Arch),
  555. CIEPointer(CIEPointer), InitialLocation(InitialLocation),
  556. AddressRange(AddressRange), LinkedCIE(Cie), LSDAAddress(LSDAAddress) {}
  557. ~FDE() override = default;
  558. const CIE *getLinkedCIE() const { return LinkedCIE; }
  559. uint64_t getInitialLocation() const { return InitialLocation; }
  560. uint64_t getAddressRange() const { return AddressRange; }
  561. Optional<uint64_t> getLSDAAddress() const { return LSDAAddress; }
  562. void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
  563. bool IsEH) const override;
  564. static bool classof(const FrameEntry *FE) { return FE->getKind() == FK_FDE; }
  565. private:
  566. /// The following fields are defined in section 6.4.1 of the DWARFv3 standard.
  567. /// Note that CIE pointers in EH FDEs, unlike DWARF FDEs, contain relative
  568. /// offsets to the linked CIEs. See the following link for more info:
  569. /// https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
  570. const uint64_t CIEPointer;
  571. const uint64_t InitialLocation;
  572. const uint64_t AddressRange;
  573. const CIE *LinkedCIE;
  574. const Optional<uint64_t> LSDAAddress;
  575. };
  576. } // end namespace dwarf
  577. /// A parsed .debug_frame or .eh_frame section
  578. class DWARFDebugFrame {
  579. const Triple::ArchType Arch;
  580. // True if this is parsing an eh_frame section.
  581. const bool IsEH;
  582. // Not zero for sane pointer values coming out of eh_frame
  583. const uint64_t EHFrameAddress;
  584. std::vector<std::unique_ptr<dwarf::FrameEntry>> Entries;
  585. using iterator = pointee_iterator<decltype(Entries)::const_iterator>;
  586. /// Return the entry at the given offset or nullptr.
  587. dwarf::FrameEntry *getEntryAtOffset(uint64_t Offset) const;
  588. public:
  589. // If IsEH is true, assume it is a .eh_frame section. Otherwise,
  590. // it is a .debug_frame section. EHFrameAddress should be different
  591. // than zero for correct parsing of .eh_frame addresses when they
  592. // use a PC-relative encoding.
  593. DWARFDebugFrame(Triple::ArchType Arch,
  594. bool IsEH = false, uint64_t EHFrameAddress = 0);
  595. ~DWARFDebugFrame();
  596. /// Dump the section data into the given stream.
  597. void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
  598. Optional<uint64_t> Offset) const;
  599. /// Parse the section from raw data. \p Data is assumed to contain the whole
  600. /// frame section contents to be parsed.
  601. Error parse(DWARFDataExtractor Data);
  602. /// Return whether the section has any entries.
  603. bool empty() const { return Entries.empty(); }
  604. /// DWARF Frame entries accessors
  605. iterator begin() const { return Entries.begin(); }
  606. iterator end() const { return Entries.end(); }
  607. iterator_range<iterator> entries() const {
  608. return iterator_range<iterator>(Entries.begin(), Entries.end());
  609. }
  610. uint64_t getEHFrameAddress() const { return EHFrameAddress; }
  611. };
  612. } // end namespace llvm
  613. #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGFRAME_H
  614. #ifdef __GNUC__
  615. #pragma GCC diagnostic pop
  616. #endif