Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- DWARFDebugFrame.h - Parsing of .debug_frame --------------*- 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_DWARFDEBUGFRAME_H
0010 #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGFRAME_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/SmallString.h"
0014 #include "llvm/ADT/iterator.h"
0015 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
0016 #include "llvm/Support/Error.h"
0017 #include "llvm/TargetParser/Triple.h"
0018 #include <map>
0019 #include <memory>
0020 #include <vector>
0021 
0022 namespace llvm {
0023 
0024 class raw_ostream;
0025 class DWARFDataExtractor;
0026 class MCRegisterInfo;
0027 struct DIDumpOptions;
0028 
0029 namespace dwarf {
0030 
0031 constexpr uint32_t InvalidRegisterNumber = UINT32_MAX;
0032 
0033 /// A class that represents a location for the Call Frame Address (CFA) or a
0034 /// register. This is decoded from the DWARF Call Frame Information
0035 /// instructions and put into an UnwindRow.
0036 class UnwindLocation {
0037 public:
0038   enum Location {
0039     /// Not specified.
0040     Unspecified,
0041     /// Register is not available and can't be recovered.
0042     Undefined,
0043     /// Register value is in the register, nothing needs to be done to unwind
0044     /// it:
0045     ///   reg = reg
0046     Same,
0047     /// Register is in or at the CFA plus an offset:
0048     ///   reg = CFA + offset
0049     ///   reg = defef(CFA + offset)
0050     CFAPlusOffset,
0051     /// Register or CFA is in or at a register plus offset, optionally in
0052     /// an address space:
0053     ///   reg = reg + offset [in addrspace]
0054     ///   reg = deref(reg + offset [in addrspace])
0055     RegPlusOffset,
0056     /// Register or CFA value is in or at a value found by evaluating a DWARF
0057     /// expression:
0058     ///   reg = eval(dwarf_expr)
0059     ///   reg = deref(eval(dwarf_expr))
0060     DWARFExpr,
0061     /// Value is a constant value contained in "Offset":
0062     ///   reg = Offset
0063     Constant,
0064   };
0065 
0066 private:
0067   Location Kind;   /// The type of the location that describes how to unwind it.
0068   uint32_t RegNum; /// The register number for Kind == RegPlusOffset.
0069   int32_t Offset;  /// The offset for Kind == CFAPlusOffset or RegPlusOffset.
0070   std::optional<uint32_t> AddrSpace;   /// The address space for Kind ==
0071                                        /// RegPlusOffset for CFA.
0072   std::optional<DWARFExpression> Expr; /// The DWARF expression for Kind ==
0073                                        /// DWARFExpression.
0074   bool Dereference; /// If true, the resulting location must be dereferenced
0075                     /// after the location value is computed.
0076 
0077   // Constructors are private to force people to use the create static
0078   // functions.
0079   UnwindLocation(Location K)
0080       : Kind(K), RegNum(InvalidRegisterNumber), Offset(0),
0081         AddrSpace(std::nullopt), Dereference(false) {}
0082 
0083   UnwindLocation(Location K, uint32_t Reg, int32_t Off,
0084                  std::optional<uint32_t> AS, bool Deref)
0085       : Kind(K), RegNum(Reg), Offset(Off), AddrSpace(AS), Dereference(Deref) {}
0086 
0087   UnwindLocation(DWARFExpression E, bool Deref)
0088       : Kind(DWARFExpr), RegNum(InvalidRegisterNumber), Offset(0), Expr(E),
0089         Dereference(Deref) {}
0090 
0091 public:
0092   /// Create a location whose rule is set to Unspecified. This means the
0093   /// register value might be in the same register but it wasn't specified in
0094   /// the unwind opcodes.
0095   static UnwindLocation createUnspecified();
0096   /// Create a location where the value is undefined and not available. This can
0097   /// happen when a register is volatile and can't be recovered.
0098   static UnwindLocation createUndefined();
0099   /// Create a location where the value is known to be in the register itself.
0100   static UnwindLocation createSame();
0101   /// Create a location that is in (Deref == false) or at (Deref == true) the
0102   /// CFA plus an offset. Most registers that are spilled onto the stack use
0103   /// this rule. The rule for the register will use this rule and specify a
0104   /// unique offset from the CFA with \a Deref set to true. This value will be
0105   /// relative to a CFA value which is typically defined using the register
0106   /// plus offset location. \see createRegisterPlusOffset(...) for more
0107   /// information.
0108   static UnwindLocation createIsCFAPlusOffset(int32_t Off);
0109   static UnwindLocation createAtCFAPlusOffset(int32_t Off);
0110   /// Create a location where the saved value is in (Deref == false) or at
0111   /// (Deref == true) a regiser plus an offset and, optionally, in the specified
0112   /// address space (used mostly for the CFA).
0113   ///
0114   /// The CFA is usually defined using this rule by using the stack pointer or
0115   /// frame pointer as the register, with an offset that accounts for all
0116   /// spilled registers and all local variables in a function, and Deref ==
0117   /// false.
0118   static UnwindLocation
0119   createIsRegisterPlusOffset(uint32_t Reg, int32_t Off,
0120                              std::optional<uint32_t> AddrSpace = std::nullopt);
0121   static UnwindLocation
0122   createAtRegisterPlusOffset(uint32_t Reg, int32_t Off,
0123                              std::optional<uint32_t> AddrSpace = std::nullopt);
0124   /// Create a location whose value is the result of evaluating a DWARF
0125   /// expression. This allows complex expressions to be evaluated in order to
0126   /// unwind a register or CFA value.
0127   static UnwindLocation createIsDWARFExpression(DWARFExpression Expr);
0128   static UnwindLocation createAtDWARFExpression(DWARFExpression Expr);
0129   static UnwindLocation createIsConstant(int32_t Value);
0130 
0131   Location getLocation() const { return Kind; }
0132   uint32_t getRegister() const { return RegNum; }
0133   int32_t getOffset() const { return Offset; }
0134   uint32_t getAddressSpace() const {
0135     assert(Kind == RegPlusOffset && AddrSpace);
0136     return *AddrSpace;
0137   }
0138   int32_t getConstant() const { return Offset; }
0139   /// Some opcodes will modify the CFA location's register only, so we need
0140   /// to be able to modify the CFA register when evaluating DWARF Call Frame
0141   /// Information opcodes.
0142   void setRegister(uint32_t NewRegNum) { RegNum = NewRegNum; }
0143   /// Some opcodes will modify the CFA location's offset only, so we need
0144   /// to be able to modify the CFA offset when evaluating DWARF Call Frame
0145   /// Information opcodes.
0146   void setOffset(int32_t NewOffset) { Offset = NewOffset; }
0147   /// Some opcodes modify a constant value and we need to be able to update
0148   /// the constant value (DW_CFA_GNU_window_save which is also known as
0149   // DW_CFA_AARCH64_negate_ra_state).
0150   void setConstant(int32_t Value) { Offset = Value; }
0151 
0152   std::optional<DWARFExpression> getDWARFExpressionBytes() const {
0153     return Expr;
0154   }
0155   /// Dump a location expression as text and use the register information if
0156   /// some is provided.
0157   ///
0158   /// \param OS the stream to use for output.
0159   ///
0160   /// \param MRI register information that helps emit register names insteead
0161   /// of raw register numbers.
0162   ///
0163   /// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
0164   /// instead of from .debug_frame. This is needed for register number
0165   /// conversion because some register numbers differ between the two sections
0166   /// for certain architectures like x86.
0167   void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const;
0168 
0169   bool operator==(const UnwindLocation &RHS) const;
0170 };
0171 
0172 raw_ostream &operator<<(raw_ostream &OS, const UnwindLocation &R);
0173 
0174 /// A class that can track all registers with locations in a UnwindRow object.
0175 ///
0176 /// Register locations use a map where the key is the register number and the
0177 /// the value is a UnwindLocation.
0178 ///
0179 /// The register maps are put into a class so that all register locations can
0180 /// be copied when parsing the unwind opcodes DW_CFA_remember_state and
0181 /// DW_CFA_restore_state.
0182 class RegisterLocations {
0183   std::map<uint32_t, UnwindLocation> Locations;
0184 
0185 public:
0186   /// Return the location for the register in \a RegNum if there is a location.
0187   ///
0188   /// \param RegNum the register number to find a location for.
0189   ///
0190   /// \returns A location if one is available for \a RegNum, or std::nullopt
0191   /// otherwise.
0192   std::optional<UnwindLocation> getRegisterLocation(uint32_t RegNum) const {
0193     auto Pos = Locations.find(RegNum);
0194     if (Pos == Locations.end())
0195       return std::nullopt;
0196     return Pos->second;
0197   }
0198 
0199   /// Set the location for the register in \a RegNum to \a Location.
0200   ///
0201   /// \param RegNum the register number to set the location for.
0202   ///
0203   /// \param Location the UnwindLocation that describes how to unwind the value.
0204   void setRegisterLocation(uint32_t RegNum, const UnwindLocation &Location) {
0205     Locations.erase(RegNum);
0206     Locations.insert(std::make_pair(RegNum, Location));
0207   }
0208 
0209   /// Removes any rule for the register in \a RegNum.
0210   ///
0211   /// \param RegNum the register number to remove the location for.
0212   void removeRegisterLocation(uint32_t RegNum) { Locations.erase(RegNum); }
0213 
0214   /// Dump all registers + locations that are currently defined in this object.
0215   ///
0216   /// \param OS the stream to use for output.
0217   ///
0218   /// \param MRI register information that helps emit register names insteead
0219   /// of raw register numbers.
0220   ///
0221   /// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
0222   /// instead of from .debug_frame. This is needed for register number
0223   /// conversion because some register numbers differ between the two sections
0224   /// for certain architectures like x86.
0225   void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const;
0226 
0227   /// Returns true if we have any register locations in this object.
0228   bool hasLocations() const { return !Locations.empty(); }
0229 
0230   size_t size() const { return Locations.size(); }
0231 
0232   bool operator==(const RegisterLocations &RHS) const {
0233     return Locations == RHS.Locations;
0234   }
0235 };
0236 
0237 raw_ostream &operator<<(raw_ostream &OS, const RegisterLocations &RL);
0238 
0239 /// A class that represents a single row in the unwind table that is decoded by
0240 /// parsing the DWARF Call Frame Information opcodes.
0241 ///
0242 /// The row consists of an optional address, the rule to unwind the CFA and all
0243 /// rules to unwind any registers. If the address doesn't have a value, this
0244 /// row represents the initial instructions for a CIE. If the address has a
0245 /// value the UnwindRow represents a row in the UnwindTable for a FDE. The
0246 /// address is the first address for which the CFA location and register rules
0247 /// are valid within a function.
0248 ///
0249 /// UnwindRow objects are created by parsing opcodes in the DWARF Call Frame
0250 /// Information and UnwindRow objects are lazily populated and pushed onto a
0251 /// stack in the UnwindTable when evaluating this state machine. Accessors are
0252 /// needed for the address, CFA value, and register locations as the opcodes
0253 /// encode a state machine that produces a sorted array of UnwindRow objects
0254 /// \see UnwindTable.
0255 class UnwindRow {
0256   /// The address will be valid when parsing the instructions in a FDE. If
0257   /// invalid, this object represents the initial instructions of a CIE.
0258   std::optional<uint64_t> Address; ///< Address for row in FDE, invalid for CIE.
0259   UnwindLocation CFAValue;    ///< How to unwind the Call Frame Address (CFA).
0260   RegisterLocations RegLocs;  ///< How to unwind all registers in this list.
0261 
0262 public:
0263   UnwindRow() : CFAValue(UnwindLocation::createUnspecified()) {}
0264 
0265   /// Returns true if the address is valid in this object.
0266   bool hasAddress() const { return Address.has_value(); }
0267 
0268   /// Get the address for this row.
0269   ///
0270   /// Clients should only call this function after verifying it has a valid
0271   /// address with a call to \see hasAddress().
0272   uint64_t getAddress() const { return *Address; }
0273 
0274   /// Set the address for this UnwindRow.
0275   ///
0276   /// The address represents the first address for which the CFAValue and
0277   /// RegLocs are valid within a function.
0278   void setAddress(uint64_t Addr) { Address = Addr; }
0279 
0280   /// Offset the address for this UnwindRow.
0281   ///
0282   /// The address represents the first address for which the CFAValue and
0283   /// RegLocs are valid within a function. Clients must ensure that this object
0284   /// already has an address (\see hasAddress()) prior to calling this
0285   /// function.
0286   void slideAddress(uint64_t Offset) { *Address += Offset; }
0287   UnwindLocation &getCFAValue() { return CFAValue; }
0288   const UnwindLocation &getCFAValue() const { return CFAValue; }
0289   RegisterLocations &getRegisterLocations() { return RegLocs; }
0290   const RegisterLocations &getRegisterLocations() const { return RegLocs; }
0291 
0292   /// Dump the UnwindRow to the stream.
0293   ///
0294   /// \param OS the stream to use for output.
0295   ///
0296   /// \param MRI register information that helps emit register names insteead
0297   /// of raw register numbers.
0298   ///
0299   /// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
0300   /// instead of from .debug_frame. This is needed for register number
0301   /// conversion because some register numbers differ between the two sections
0302   /// for certain architectures like x86.
0303   ///
0304   /// \param IndentLevel specify the indent level as an integer. The UnwindRow
0305   /// will be output to the stream preceded by 2 * IndentLevel number of spaces.
0306   void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
0307             unsigned IndentLevel = 0) const;
0308 };
0309 
0310 raw_ostream &operator<<(raw_ostream &OS, const UnwindRow &Row);
0311 
0312 class CFIProgram;
0313 class CIE;
0314 class FDE;
0315 
0316 /// A class that contains all UnwindRow objects for an FDE or a single unwind
0317 /// row for a CIE. To unwind an address the rows, which are sorted by start
0318 /// address, can be searched to find the UnwindRow with the lowest starting
0319 /// address that is greater than or equal to the address that is being looked
0320 /// up.
0321 class UnwindTable {
0322 public:
0323   using RowContainer = std::vector<UnwindRow>;
0324   using iterator = RowContainer::iterator;
0325   using const_iterator = RowContainer::const_iterator;
0326 
0327   size_t size() const { return Rows.size(); }
0328   iterator begin() { return Rows.begin(); }
0329   const_iterator begin() const { return Rows.begin(); }
0330   iterator end() { return Rows.end(); }
0331   const_iterator end() const { return Rows.end(); }
0332   const UnwindRow &operator[](size_t Index) const {
0333     assert(Index < size());
0334     return Rows[Index];
0335   }
0336 
0337   /// Dump the UnwindTable to the stream.
0338   ///
0339   /// \param OS the stream to use for output.
0340   ///
0341   /// \param MRI register information that helps emit register names insteead
0342   /// of raw register numbers.
0343   ///
0344   /// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
0345   /// instead of from .debug_frame. This is needed for register number
0346   /// conversion because some register numbers differ between the two sections
0347   /// for certain architectures like x86.
0348   ///
0349   /// \param IndentLevel specify the indent level as an integer. The UnwindRow
0350   /// will be output to the stream preceded by 2 * IndentLevel number of spaces.
0351   void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
0352             unsigned IndentLevel = 0) const;
0353 
0354   /// Create an UnwindTable from a Common Information Entry (CIE).
0355   ///
0356   /// \param Cie The Common Information Entry to extract the table from. The
0357   /// CFIProgram is retrieved from the \a Cie object and used to create the
0358   /// UnwindTable.
0359   ///
0360   /// \returns An error if the DWARF Call Frame Information opcodes have state
0361   /// machine errors, or a valid UnwindTable otherwise.
0362   static Expected<UnwindTable> create(const CIE *Cie);
0363 
0364   /// Create an UnwindTable from a Frame Descriptor Entry (FDE).
0365   ///
0366   /// \param Fde The Frame Descriptor Entry to extract the table from. The
0367   /// CFIProgram is retrieved from the \a Fde object and used to create the
0368   /// UnwindTable.
0369   ///
0370   /// \returns An error if the DWARF Call Frame Information opcodes have state
0371   /// machine errors, or a valid UnwindTable otherwise.
0372   static Expected<UnwindTable> create(const FDE *Fde);
0373 
0374 private:
0375   RowContainer Rows;
0376   /// The end address when data is extracted from a FDE. This value will be
0377   /// invalid when a UnwindTable is extracted from a CIE.
0378   std::optional<uint64_t> EndAddress;
0379 
0380   /// Parse the information in the CFIProgram and update the CurrRow object
0381   /// that the state machine describes.
0382   ///
0383   /// This is an internal implementation that emulates the state machine
0384   /// described in the DWARF Call Frame Information opcodes and will push
0385   /// CurrRow onto the Rows container when needed.
0386   ///
0387   /// \param CFIP the CFI program that contains the opcodes from a CIE or FDE.
0388   ///
0389   /// \param CurrRow the current row to modify while parsing the state machine.
0390   ///
0391   /// \param InitialLocs If non-NULL, we are parsing a FDE and this contains
0392   /// the initial register locations from the CIE. If NULL, then a CIE's
0393   /// opcodes are being parsed and this is not needed. This is used for the
0394   /// DW_CFA_restore and DW_CFA_restore_extended opcodes.
0395   Error parseRows(const CFIProgram &CFIP, UnwindRow &CurrRow,
0396                   const RegisterLocations *InitialLocs);
0397 };
0398 
0399 raw_ostream &operator<<(raw_ostream &OS, const UnwindTable &Rows);
0400 
0401 /// Represent a sequence of Call Frame Information instructions that, when read
0402 /// in order, construct a table mapping PC to frame state. This can also be
0403 /// referred to as "CFI rules" in DWARF literature to avoid confusion with
0404 /// computer programs in the broader sense, and in this context each instruction
0405 /// would be a rule to establish the mapping. Refer to pg. 172 in the DWARF5
0406 /// manual, "6.4.1 Structure of Call Frame Information".
0407 class CFIProgram {
0408 public:
0409   static constexpr size_t MaxOperands = 3;
0410   typedef SmallVector<uint64_t, MaxOperands> Operands;
0411 
0412   /// An instruction consists of a DWARF CFI opcode and an optional sequence of
0413   /// operands. If it refers to an expression, then this expression has its own
0414   /// sequence of operations and operands handled separately by DWARFExpression.
0415   struct Instruction {
0416     Instruction(uint8_t Opcode) : Opcode(Opcode) {}
0417 
0418     uint8_t Opcode;
0419     Operands Ops;
0420     // Associated DWARF expression in case this instruction refers to one
0421     std::optional<DWARFExpression> Expression;
0422 
0423     Expected<uint64_t> getOperandAsUnsigned(const CFIProgram &CFIP,
0424                                             uint32_t OperandIdx) const;
0425 
0426     Expected<int64_t> getOperandAsSigned(const CFIProgram &CFIP,
0427                                          uint32_t OperandIdx) const;
0428   };
0429 
0430   using InstrList = std::vector<Instruction>;
0431   using iterator = InstrList::iterator;
0432   using const_iterator = InstrList::const_iterator;
0433 
0434   iterator begin() { return Instructions.begin(); }
0435   const_iterator begin() const { return Instructions.begin(); }
0436   iterator end() { return Instructions.end(); }
0437   const_iterator end() const { return Instructions.end(); }
0438 
0439   unsigned size() const { return (unsigned)Instructions.size(); }
0440   bool empty() const { return Instructions.empty(); }
0441   uint64_t codeAlign() const { return CodeAlignmentFactor; }
0442   int64_t dataAlign() const { return DataAlignmentFactor; }
0443   Triple::ArchType triple() const { return Arch; }
0444 
0445   CFIProgram(uint64_t CodeAlignmentFactor, int64_t DataAlignmentFactor,
0446              Triple::ArchType Arch)
0447       : CodeAlignmentFactor(CodeAlignmentFactor),
0448         DataAlignmentFactor(DataAlignmentFactor),
0449         Arch(Arch) {}
0450 
0451   /// Parse and store a sequence of CFI instructions from Data,
0452   /// starting at *Offset and ending at EndOffset. *Offset is updated
0453   /// to EndOffset upon successful parsing, or indicates the offset
0454   /// where a problem occurred in case an error is returned.
0455   Error parse(DWARFDataExtractor Data, uint64_t *Offset, uint64_t EndOffset);
0456 
0457   void dump(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel,
0458             std::optional<uint64_t> InitialLocation) const;
0459 
0460   void addInstruction(const Instruction &I) { Instructions.push_back(I); }
0461 
0462   /// Get a DWARF CFI call frame string for the given DW_CFA opcode.
0463   StringRef callFrameString(unsigned Opcode) const;
0464 
0465 private:
0466   std::vector<Instruction> Instructions;
0467   const uint64_t CodeAlignmentFactor;
0468   const int64_t DataAlignmentFactor;
0469   Triple::ArchType Arch;
0470 
0471   /// Convenience method to add a new instruction with the given opcode.
0472   void addInstruction(uint8_t Opcode) {
0473     Instructions.push_back(Instruction(Opcode));
0474   }
0475 
0476   /// Add a new single-operand instruction.
0477   void addInstruction(uint8_t Opcode, uint64_t Operand1) {
0478     Instructions.push_back(Instruction(Opcode));
0479     Instructions.back().Ops.push_back(Operand1);
0480   }
0481 
0482   /// Add a new instruction that has two operands.
0483   void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) {
0484     Instructions.push_back(Instruction(Opcode));
0485     Instructions.back().Ops.push_back(Operand1);
0486     Instructions.back().Ops.push_back(Operand2);
0487   }
0488 
0489   /// Add a new instruction that has three operands.
0490   void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2,
0491                       uint64_t Operand3) {
0492     Instructions.push_back(Instruction(Opcode));
0493     Instructions.back().Ops.push_back(Operand1);
0494     Instructions.back().Ops.push_back(Operand2);
0495     Instructions.back().Ops.push_back(Operand3);
0496   }
0497 
0498   /// Types of operands to CFI instructions
0499   /// In DWARF, this type is implicitly tied to a CFI instruction opcode and
0500   /// thus this type doesn't need to be explicitly written to the file (this is
0501   /// not a DWARF encoding). The relationship of instrs to operand types can
0502   /// be obtained from getOperandTypes() and is only used to simplify
0503   /// instruction printing.
0504   enum OperandType {
0505     OT_Unset,
0506     OT_None,
0507     OT_Address,
0508     OT_Offset,
0509     OT_FactoredCodeOffset,
0510     OT_SignedFactDataOffset,
0511     OT_UnsignedFactDataOffset,
0512     OT_Register,
0513     OT_AddressSpace,
0514     OT_Expression
0515   };
0516 
0517   /// Get the OperandType as a "const char *".
0518   static const char *operandTypeString(OperandType OT);
0519 
0520   /// Retrieve the array describing the types of operands according to the enum
0521   /// above. This is indexed by opcode.
0522   static ArrayRef<OperandType[MaxOperands]> getOperandTypes();
0523 
0524   /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
0525   void printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
0526                     const Instruction &Instr, unsigned OperandIdx,
0527                     uint64_t Operand, std::optional<uint64_t> &Address) const;
0528 };
0529 
0530 /// An entry in either debug_frame or eh_frame. This entry can be a CIE or an
0531 /// FDE.
0532 class FrameEntry {
0533 public:
0534   enum FrameKind { FK_CIE, FK_FDE };
0535 
0536   FrameEntry(FrameKind K, bool IsDWARF64, uint64_t Offset, uint64_t Length,
0537              uint64_t CodeAlign, int64_t DataAlign, Triple::ArchType Arch)
0538       : Kind(K), IsDWARF64(IsDWARF64), Offset(Offset), Length(Length),
0539         CFIs(CodeAlign, DataAlign, Arch) {}
0540 
0541   virtual ~FrameEntry() = default;
0542 
0543   FrameKind getKind() const { return Kind; }
0544   uint64_t getOffset() const { return Offset; }
0545   uint64_t getLength() const { return Length; }
0546   const CFIProgram &cfis() const { return CFIs; }
0547   CFIProgram &cfis() { return CFIs; }
0548 
0549   /// Dump the instructions in this CFI fragment
0550   virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const = 0;
0551 
0552 protected:
0553   const FrameKind Kind;
0554 
0555   const bool IsDWARF64;
0556 
0557   /// Offset of this entry in the section.
0558   const uint64_t Offset;
0559 
0560   /// Entry length as specified in DWARF.
0561   const uint64_t Length;
0562 
0563   CFIProgram CFIs;
0564 };
0565 
0566 /// DWARF Common Information Entry (CIE)
0567 class CIE : public FrameEntry {
0568 public:
0569   // CIEs (and FDEs) are simply container classes, so the only sensible way to
0570   // create them is by providing the full parsed contents in the constructor.
0571   CIE(bool IsDWARF64, uint64_t Offset, uint64_t Length, uint8_t Version,
0572       SmallString<8> Augmentation, uint8_t AddressSize,
0573       uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor,
0574       int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister,
0575       SmallString<8> AugmentationData, uint32_t FDEPointerEncoding,
0576       uint32_t LSDAPointerEncoding, std::optional<uint64_t> Personality,
0577       std::optional<uint32_t> PersonalityEnc, Triple::ArchType Arch)
0578       : FrameEntry(FK_CIE, IsDWARF64, Offset, Length, CodeAlignmentFactor,
0579                    DataAlignmentFactor, Arch),
0580         Version(Version), Augmentation(std::move(Augmentation)),
0581         AddressSize(AddressSize), SegmentDescriptorSize(SegmentDescriptorSize),
0582         CodeAlignmentFactor(CodeAlignmentFactor),
0583         DataAlignmentFactor(DataAlignmentFactor),
0584         ReturnAddressRegister(ReturnAddressRegister),
0585         AugmentationData(std::move(AugmentationData)),
0586         FDEPointerEncoding(FDEPointerEncoding),
0587         LSDAPointerEncoding(LSDAPointerEncoding), Personality(Personality),
0588         PersonalityEnc(PersonalityEnc) {}
0589 
0590   static bool classof(const FrameEntry *FE) { return FE->getKind() == FK_CIE; }
0591 
0592   StringRef getAugmentationString() const { return Augmentation; }
0593   uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; }
0594   int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; }
0595   uint8_t getVersion() const { return Version; }
0596   uint64_t getReturnAddressRegister() const { return ReturnAddressRegister; }
0597   std::optional<uint64_t> getPersonalityAddress() const { return Personality; }
0598   std::optional<uint32_t> getPersonalityEncoding() const {
0599     return PersonalityEnc;
0600   }
0601 
0602   StringRef getAugmentationData() const { return AugmentationData; }
0603 
0604   uint32_t getFDEPointerEncoding() const { return FDEPointerEncoding; }
0605 
0606   uint32_t getLSDAPointerEncoding() const { return LSDAPointerEncoding; }
0607 
0608   void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const override;
0609 
0610 private:
0611   /// The following fields are defined in section 6.4.1 of the DWARF standard v4
0612   const uint8_t Version;
0613   const SmallString<8> Augmentation;
0614   const uint8_t AddressSize;
0615   const uint8_t SegmentDescriptorSize;
0616   const uint64_t CodeAlignmentFactor;
0617   const int64_t DataAlignmentFactor;
0618   const uint64_t ReturnAddressRegister;
0619 
0620   // The following are used when the CIE represents an EH frame entry.
0621   const SmallString<8> AugmentationData;
0622   const uint32_t FDEPointerEncoding;
0623   const uint32_t LSDAPointerEncoding;
0624   const std::optional<uint64_t> Personality;
0625   const std::optional<uint32_t> PersonalityEnc;
0626 };
0627 
0628 /// DWARF Frame Description Entry (FDE)
0629 class FDE : public FrameEntry {
0630 public:
0631   FDE(bool IsDWARF64, uint64_t Offset, uint64_t Length, uint64_t CIEPointer,
0632       uint64_t InitialLocation, uint64_t AddressRange, CIE *Cie,
0633       std::optional<uint64_t> LSDAAddress, Triple::ArchType Arch)
0634       : FrameEntry(FK_FDE, IsDWARF64, Offset, Length,
0635                    Cie ? Cie->getCodeAlignmentFactor() : 0,
0636                    Cie ? Cie->getDataAlignmentFactor() : 0, Arch),
0637         CIEPointer(CIEPointer), InitialLocation(InitialLocation),
0638         AddressRange(AddressRange), LinkedCIE(Cie), LSDAAddress(LSDAAddress) {}
0639 
0640   ~FDE() override = default;
0641 
0642   const CIE *getLinkedCIE() const { return LinkedCIE; }
0643   uint64_t getCIEPointer() const { return CIEPointer; }
0644   uint64_t getInitialLocation() const { return InitialLocation; }
0645   uint64_t getAddressRange() const { return AddressRange; }
0646   std::optional<uint64_t> getLSDAAddress() const { return LSDAAddress; }
0647 
0648   void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const override;
0649 
0650   static bool classof(const FrameEntry *FE) { return FE->getKind() == FK_FDE; }
0651 
0652 private:
0653   /// The following fields are defined in section 6.4.1 of the DWARFv3 standard.
0654   /// Note that CIE pointers in EH FDEs, unlike DWARF FDEs, contain relative
0655   /// offsets to the linked CIEs. See the following link for more info:
0656   /// https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
0657   const uint64_t CIEPointer;
0658   const uint64_t InitialLocation;
0659   const uint64_t AddressRange;
0660   const CIE *LinkedCIE;
0661   const std::optional<uint64_t> LSDAAddress;
0662 };
0663 
0664 } // end namespace dwarf
0665 
0666 /// A parsed .debug_frame or .eh_frame section
0667 class DWARFDebugFrame {
0668   const Triple::ArchType Arch;
0669   // True if this is parsing an eh_frame section.
0670   const bool IsEH;
0671   // Not zero for sane pointer values coming out of eh_frame
0672   const uint64_t EHFrameAddress;
0673 
0674   std::vector<std::unique_ptr<dwarf::FrameEntry>> Entries;
0675   using iterator = pointee_iterator<decltype(Entries)::const_iterator>;
0676 
0677   /// Return the entry at the given offset or nullptr.
0678   dwarf::FrameEntry *getEntryAtOffset(uint64_t Offset) const;
0679 
0680 public:
0681   // If IsEH is true, assume it is a .eh_frame section. Otherwise,
0682   // it is a .debug_frame section. EHFrameAddress should be different
0683   // than zero for correct parsing of .eh_frame addresses when they
0684   // use a PC-relative encoding.
0685   DWARFDebugFrame(Triple::ArchType Arch,
0686                   bool IsEH = false, uint64_t EHFrameAddress = 0);
0687   ~DWARFDebugFrame();
0688 
0689   /// Dump the section data into the given stream.
0690   void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
0691             std::optional<uint64_t> Offset) const;
0692 
0693   /// Parse the section from raw data. \p Data is assumed to contain the whole
0694   /// frame section contents to be parsed.
0695   Error parse(DWARFDataExtractor Data);
0696 
0697   /// Return whether the section has any entries.
0698   bool empty() const { return Entries.empty(); }
0699 
0700   /// DWARF Frame entries accessors
0701   iterator begin() const { return Entries.begin(); }
0702   iterator end() const { return Entries.end(); }
0703   iterator_range<iterator> entries() const {
0704     return iterator_range<iterator>(Entries.begin(), Entries.end());
0705   }
0706 
0707   uint64_t getEHFrameAddress() const { return EHFrameAddress; }
0708 };
0709 
0710 } // end namespace llvm
0711 
0712 #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGFRAME_H