1 //===-- DWARFCallFrameInfo.h ------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_DWARFCallFrameInfo_h_ 11 #define liblldb_DWARFCallFrameInfo_h_ 12 13 #include <map> 14 #include <mutex> 15 16 #include "lldb/Core/AddressRange.h" 17 #include "lldb/Utility/Flags.h" 18 19 #include "lldb/Core/RangeMap.h" 20 #include "lldb/Core/dwarf.h" 21 #include "lldb/Symbol/ObjectFile.h" 22 #include "lldb/Symbol/UnwindPlan.h" 23 #include "lldb/Utility/VMRange.h" 24 #include "lldb/lldb-private.h" 25 26 namespace lldb_private { 27 28 // DWARFCallFrameInfo is a class which can read eh_frame and DWARF Call Frame 29 // Information FDEs. It stores little information internally. Only two APIs 30 // are exported - one to find the high/low pc values of a function given a text 31 // address via the information in the eh_frame / debug_frame, and one to 32 // generate an UnwindPlan based on the FDE in the eh_frame / debug_frame 33 // section. 34 35 class DWARFCallFrameInfo { 36 public: 37 enum Type { EH, DWARF }; 38 39 DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP §ion, Type type); 40 41 ~DWARFCallFrameInfo() = default; 42 43 // Locate an AddressRange that includes the provided Address in this object's 44 // eh_frame/debug_info Returns true if a range is found to cover that 45 // address. 46 bool GetAddressRange(Address addr, AddressRange &range); 47 48 // Return an UnwindPlan based on the call frame information encoded in the 49 // FDE of this DWARFCallFrameInfo section. 50 bool GetUnwindPlan(Address addr, UnwindPlan &unwind_plan); 51 52 typedef RangeVector<lldb::addr_t, uint32_t> FunctionAddressAndSizeVector; 53 54 //------------------------------------------------------------------ 55 // Build a vector of file address and size for all functions in this Module 56 // based on the eh_frame FDE entries. 57 // 58 // The eh_frame information can be a useful source of file address and size 59 // of the functions in a Module. Often a binary's non-exported symbols are 60 // stripped before shipping so lldb won't know the start addr / size of many 61 // functions in the Module. But the eh_frame can help to give the addresses 62 // of these stripped symbols, at least. 63 // 64 // @param[out] function_info 65 // A vector provided by the caller is filled out. May be empty if no 66 // FDEs/no eh_frame 67 // is present in this Module. 68 69 void 70 GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info); 71 72 void ForEachFDEEntries( 73 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback); 74 75 private: 76 enum { CFI_AUG_MAX_SIZE = 8, CFI_HEADER_SIZE = 8 }; 77 enum CFIVersion { 78 CFI_VERSION1 = 1, // DWARF v.2 79 CFI_VERSION3 = 3, // DWARF v.3 80 CFI_VERSION4 = 4 // DWARF v.4, v.5 81 }; 82 83 struct CIE { 84 dw_offset_t cie_offset; 85 uint8_t version; 86 char augmentation[CFI_AUG_MAX_SIZE]; // This is typically empty or very 87 // short. 88 uint8_t address_size = sizeof(uint32_t); // The size of a target address. 89 uint8_t segment_size = 0; // The size of a segment selector. 90 91 uint32_t code_align; 92 int32_t data_align; 93 uint32_t return_addr_reg_num; 94 dw_offset_t inst_offset; // offset of CIE instructions in mCFIData 95 uint32_t inst_length; // length of CIE instructions in mCFIData 96 uint8_t ptr_encoding; 97 uint8_t lsda_addr_encoding; // The encoding of the LSDA address in the FDE 98 // augmentation data 99 lldb::addr_t personality_loc; // (file) address of the pointer to the 100 // personality routine 101 lldb_private::UnwindPlan::Row initial_row; 102 CIECIE103 CIE(dw_offset_t offset) 104 : cie_offset(offset), version(-1), code_align(0), data_align(0), 105 return_addr_reg_num(LLDB_INVALID_REGNUM), inst_offset(0), 106 inst_length(0), ptr_encoding(0), lsda_addr_encoding(DW_EH_PE_omit), 107 personality_loc(LLDB_INVALID_ADDRESS), initial_row() {} 108 }; 109 110 typedef std::shared_ptr<CIE> CIESP; 111 112 typedef std::map<dw_offset_t, CIESP> cie_map_t; 113 114 // Start address (file address), size, offset of FDE location used for 115 // finding an FDE for a given File address; the start address field is an 116 // offset into an individual Module. 117 typedef RangeDataVector<lldb::addr_t, uint32_t, dw_offset_t> FDEEntryMap; 118 119 bool IsEHFrame() const; 120 121 bool GetFDEEntryByFileAddress(lldb::addr_t file_offset, 122 FDEEntryMap::Entry &fde_entry); 123 124 void GetFDEIndex(); 125 126 bool FDEToUnwindPlan(uint32_t offset, Address startaddr, 127 UnwindPlan &unwind_plan); 128 129 const CIE *GetCIE(dw_offset_t cie_offset); 130 131 void GetCFIData(); 132 133 // Applies the specified DWARF opcode to the given row. This function handle 134 // the commands operates only on a single row (these are the ones what can 135 // appear both in 136 // CIE and in FDE). 137 // Returns true if the opcode is handled and false otherwise. 138 bool HandleCommonDwarfOpcode(uint8_t primary_opcode, uint8_t extended_opcode, 139 int32_t data_align, lldb::offset_t &offset, 140 UnwindPlan::Row &row); 141 142 ObjectFile &m_objfile; 143 lldb::SectionSP m_section_sp; 144 Flags m_flags = 0; 145 cie_map_t m_cie_map; 146 147 DataExtractor m_cfi_data; 148 bool m_cfi_data_initialized = false; // only copy the section into the DE once 149 150 FDEEntryMap m_fde_index; 151 bool m_fde_index_initialized = false; // only scan the section for FDEs once 152 std::mutex m_fde_index_mutex; // and isolate the thread that does it 153 154 Type m_type; 155 156 CIESP 157 ParseCIE(const uint32_t cie_offset); 158 GetRegisterKind()159 lldb::RegisterKind GetRegisterKind() const { 160 return m_type == EH ? lldb::eRegisterKindEHFrame : lldb::eRegisterKindDWARF; 161 } 162 }; 163 164 } // namespace lldb_private 165 166 #endif // liblldb_DWARFCallFrameInfo_h_ 167