DWARFDebugFrame.h 29 KB

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