Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:51

0001 //===-- DWARFCallFrameInfo.h ------------------------------------*- 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 LLDB_SYMBOL_DWARFCALLFRAMEINFO_H
0010 #define LLDB_SYMBOL_DWARFCALLFRAMEINFO_H
0011 
0012 #include <map>
0013 #include <mutex>
0014 #include <optional>
0015 
0016 #include "lldb/Core/AddressRange.h"
0017 #include "lldb/Core/dwarf.h"
0018 #include "lldb/Symbol/ObjectFile.h"
0019 #include "lldb/Symbol/UnwindPlan.h"
0020 #include "lldb/Utility/Flags.h"
0021 #include "lldb/Utility/RangeMap.h"
0022 #include "lldb/Utility/VMRange.h"
0023 #include "lldb/lldb-private.h"
0024 
0025 namespace lldb_private {
0026 
0027 // DWARFCallFrameInfo is a class which can read eh_frame and DWARF Call Frame
0028 // Information FDEs.  It stores little information internally. Only two APIs
0029 // are exported - one to find the high/low pc values of a function given a text
0030 // address via the information in the eh_frame / debug_frame, and one to
0031 // generate an UnwindPlan based on the FDE in the eh_frame / debug_frame
0032 // section.
0033 
0034 class DWARFCallFrameInfo {
0035 public:
0036   enum Type { EH, DWARF };
0037 
0038   DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP &section, Type type);
0039 
0040   ~DWARFCallFrameInfo() = default;
0041 
0042   // Locate an AddressRange that includes the provided Address in this object's
0043   // eh_frame/debug_info Returns true if a range is found to cover that
0044   // address.
0045   bool GetAddressRange(Address addr, AddressRange &range);
0046 
0047   /// Return an UnwindPlan based on the call frame information encoded in the
0048   /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid
0049   /// (at least) for the given address.
0050   bool GetUnwindPlan(const Address &addr, UnwindPlan &unwind_plan);
0051 
0052   /// Return an UnwindPlan based on the call frame information encoded in the
0053   /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid
0054   /// (at least) for some address in the given range.
0055   bool GetUnwindPlan(const AddressRange &range, UnwindPlan &unwind_plan);
0056 
0057   typedef RangeVector<lldb::addr_t, uint32_t> FunctionAddressAndSizeVector;
0058 
0059   // Build a vector of file address and size for all functions in this Module
0060   // based on the eh_frame FDE entries.
0061   //
0062   // The eh_frame information can be a useful source of file address and size
0063   // of the functions in a Module.  Often a binary's non-exported symbols are
0064   // stripped before shipping so lldb won't know the start addr / size of many
0065   // functions in the Module.  But the eh_frame can help to give the addresses
0066   // of these stripped symbols, at least.
0067   //
0068   // \param[out] function_info
0069   //      A vector provided by the caller is filled out.  May be empty if no
0070   //      FDEs/no eh_frame
0071   //      is present in this Module.
0072 
0073   void
0074   GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info);
0075 
0076   void ForEachFDEEntries(
0077       const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback);
0078 
0079 private:
0080   enum { CFI_AUG_MAX_SIZE = 8, CFI_HEADER_SIZE = 8 };
0081   enum CFIVersion {
0082     CFI_VERSION1 = 1, // DWARF v.2
0083     CFI_VERSION3 = 3, // DWARF v.3
0084     CFI_VERSION4 = 4  // DWARF v.4, v.5
0085   };
0086 
0087   struct CIE {
0088     dw_offset_t cie_offset;
0089     uint8_t version;
0090     char augmentation[CFI_AUG_MAX_SIZE]; // This is typically empty or very
0091                                          // short.
0092     uint8_t address_size = sizeof(uint32_t); // The size of a target address.
0093     uint8_t segment_size = 0;                // The size of a segment selector.
0094 
0095     uint32_t code_align;
0096     int32_t data_align;
0097     uint32_t return_addr_reg_num;
0098     dw_offset_t inst_offset; // offset of CIE instructions in mCFIData
0099     uint32_t inst_length;    // length of CIE instructions in mCFIData
0100     uint8_t ptr_encoding;
0101     uint8_t lsda_addr_encoding;   // The encoding of the LSDA address in the FDE
0102                                   // augmentation data
0103     lldb::addr_t personality_loc; // (file) address of the pointer to the
0104                                   // personality routine
0105     lldb_private::UnwindPlan::Row initial_row;
0106 
0107     CIE(dw_offset_t offset)
0108         : cie_offset(offset), version(-1), code_align(0), data_align(0),
0109           return_addr_reg_num(LLDB_INVALID_REGNUM), inst_offset(0),
0110           inst_length(0), ptr_encoding(0),
0111           lsda_addr_encoding(llvm::dwarf::DW_EH_PE_omit),
0112           personality_loc(LLDB_INVALID_ADDRESS) {}
0113   };
0114 
0115   typedef std::shared_ptr<CIE> CIESP;
0116 
0117   typedef std::map<dw_offset_t, CIESP> cie_map_t;
0118 
0119   // Start address (file address), size, offset of FDE location used for
0120   // finding an FDE for a given File address; the start address field is an
0121   // offset into an individual Module.
0122   typedef RangeDataVector<lldb::addr_t, uint32_t, dw_offset_t> FDEEntryMap;
0123 
0124   bool IsEHFrame() const;
0125 
0126   std::optional<FDEEntryMap::Entry>
0127   GetFirstFDEEntryInRange(const AddressRange &range);
0128 
0129   void GetFDEIndex();
0130 
0131   bool FDEToUnwindPlan(dw_offset_t offset, Address startaddr,
0132                        UnwindPlan &unwind_plan);
0133 
0134   const CIE *GetCIE(dw_offset_t cie_offset);
0135 
0136   void GetCFIData();
0137 
0138   // Applies the specified DWARF opcode to the given row. This function handle
0139   // the commands operates only on a single row (these are the ones what can
0140   // appear both in
0141   // CIE and in FDE).
0142   // Returns true if the opcode is handled and false otherwise.
0143   bool HandleCommonDwarfOpcode(uint8_t primary_opcode, uint8_t extended_opcode,
0144                                int32_t data_align, lldb::offset_t &offset,
0145                                UnwindPlan::Row &row);
0146 
0147   ObjectFile &m_objfile;
0148   lldb::SectionSP m_section_sp;
0149   Flags m_flags = 0;
0150   cie_map_t m_cie_map;
0151 
0152   DataExtractor m_cfi_data;
0153   bool m_cfi_data_initialized = false; // only copy the section into the DE once
0154 
0155   FDEEntryMap m_fde_index;
0156   bool m_fde_index_initialized = false; // only scan the section for FDEs once
0157   std::mutex m_fde_index_mutex; // and isolate the thread that does it
0158 
0159   Type m_type;
0160 
0161   CIESP
0162   ParseCIE(const dw_offset_t cie_offset);
0163 
0164   lldb::RegisterKind GetRegisterKind() const {
0165     return m_type == EH ? lldb::eRegisterKindEHFrame : lldb::eRegisterKindDWARF;
0166   }
0167 };
0168 
0169 } // namespace lldb_private
0170 
0171 #endif // LLDB_SYMBOL_DWARFCALLFRAMEINFO_H