10b57cec5SDimitry Andric //===-- DWARFCallFrameInfo.h ------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 95ffd83dbSDimitry Andric #ifndef LLDB_SYMBOL_DWARFCALLFRAMEINFO_H 105ffd83dbSDimitry Andric #define LLDB_SYMBOL_DWARFCALLFRAMEINFO_H 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include <map> 130b57cec5SDimitry Andric #include <mutex> 14bdd1243dSDimitry Andric #include <optional> 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "lldb/Core/AddressRange.h" 170b57cec5SDimitry Andric #include "lldb/Core/dwarf.h" 180b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h" 190b57cec5SDimitry Andric #include "lldb/Symbol/UnwindPlan.h" 200b57cec5SDimitry Andric #include "lldb/Utility/Flags.h" 210b57cec5SDimitry Andric #include "lldb/Utility/RangeMap.h" 220b57cec5SDimitry Andric #include "lldb/Utility/VMRange.h" 230b57cec5SDimitry Andric #include "lldb/lldb-private.h" 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric namespace lldb_private { 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric // DWARFCallFrameInfo is a class which can read eh_frame and DWARF Call Frame 280b57cec5SDimitry Andric // Information FDEs. It stores little information internally. Only two APIs 290b57cec5SDimitry Andric // are exported - one to find the high/low pc values of a function given a text 300b57cec5SDimitry Andric // address via the information in the eh_frame / debug_frame, and one to 310b57cec5SDimitry Andric // generate an UnwindPlan based on the FDE in the eh_frame / debug_frame 320b57cec5SDimitry Andric // section. 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric class DWARFCallFrameInfo { 350b57cec5SDimitry Andric public: 360b57cec5SDimitry Andric enum Type { EH, DWARF }; 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP §ion, Type type); 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric ~DWARFCallFrameInfo() = default; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric // Locate an AddressRange that includes the provided Address in this object's 430b57cec5SDimitry Andric // eh_frame/debug_info Returns true if a range is found to cover that 440b57cec5SDimitry Andric // address. 450b57cec5SDimitry Andric bool GetAddressRange(Address addr, AddressRange &range); 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric /// Return an UnwindPlan based on the call frame information encoded in the 480b57cec5SDimitry Andric /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid 490b57cec5SDimitry Andric /// (at least) for the given address. 500b57cec5SDimitry Andric bool GetUnwindPlan(const Address &addr, UnwindPlan &unwind_plan); 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric /// Return an UnwindPlan based on the call frame information encoded in the 530b57cec5SDimitry Andric /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid 540b57cec5SDimitry Andric /// (at least) for some address in the given range. 550b57cec5SDimitry Andric bool GetUnwindPlan(const AddressRange &range, UnwindPlan &unwind_plan); 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric typedef RangeVector<lldb::addr_t, uint32_t> FunctionAddressAndSizeVector; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric // Build a vector of file address and size for all functions in this Module 600b57cec5SDimitry Andric // based on the eh_frame FDE entries. 610b57cec5SDimitry Andric // 620b57cec5SDimitry Andric // The eh_frame information can be a useful source of file address and size 630b57cec5SDimitry Andric // of the functions in a Module. Often a binary's non-exported symbols are 640b57cec5SDimitry Andric // stripped before shipping so lldb won't know the start addr / size of many 650b57cec5SDimitry Andric // functions in the Module. But the eh_frame can help to give the addresses 660b57cec5SDimitry Andric // of these stripped symbols, at least. 670b57cec5SDimitry Andric // 680b57cec5SDimitry Andric // \param[out] function_info 690b57cec5SDimitry Andric // A vector provided by the caller is filled out. May be empty if no 700b57cec5SDimitry Andric // FDEs/no eh_frame 710b57cec5SDimitry Andric // is present in this Module. 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric void 740b57cec5SDimitry Andric GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric void ForEachFDEEntries( 770b57cec5SDimitry Andric const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric private: 800b57cec5SDimitry Andric enum { CFI_AUG_MAX_SIZE = 8, CFI_HEADER_SIZE = 8 }; 810b57cec5SDimitry Andric enum CFIVersion { 820b57cec5SDimitry Andric CFI_VERSION1 = 1, // DWARF v.2 830b57cec5SDimitry Andric CFI_VERSION3 = 3, // DWARF v.3 840b57cec5SDimitry Andric CFI_VERSION4 = 4 // DWARF v.4, v.5 850b57cec5SDimitry Andric }; 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric struct CIE { 880b57cec5SDimitry Andric dw_offset_t cie_offset; 890b57cec5SDimitry Andric uint8_t version; 900b57cec5SDimitry Andric char augmentation[CFI_AUG_MAX_SIZE]; // This is typically empty or very 910b57cec5SDimitry Andric // short. 920b57cec5SDimitry Andric uint8_t address_size = sizeof(uint32_t); // The size of a target address. 930b57cec5SDimitry Andric uint8_t segment_size = 0; // The size of a segment selector. 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric uint32_t code_align; 960b57cec5SDimitry Andric int32_t data_align; 970b57cec5SDimitry Andric uint32_t return_addr_reg_num; 980b57cec5SDimitry Andric dw_offset_t inst_offset; // offset of CIE instructions in mCFIData 990b57cec5SDimitry Andric uint32_t inst_length; // length of CIE instructions in mCFIData 1000b57cec5SDimitry Andric uint8_t ptr_encoding; 1010b57cec5SDimitry Andric uint8_t lsda_addr_encoding; // The encoding of the LSDA address in the FDE 1020b57cec5SDimitry Andric // augmentation data 1030b57cec5SDimitry Andric lldb::addr_t personality_loc; // (file) address of the pointer to the 1040b57cec5SDimitry Andric // personality routine 1050b57cec5SDimitry Andric lldb_private::UnwindPlan::Row initial_row; 1060b57cec5SDimitry Andric CIECIE1070b57cec5SDimitry Andric CIE(dw_offset_t offset) 1080b57cec5SDimitry Andric : cie_offset(offset), version(-1), code_align(0), data_align(0), 1090b57cec5SDimitry Andric return_addr_reg_num(LLDB_INVALID_REGNUM), inst_offset(0), 11081ad6265SDimitry Andric inst_length(0), ptr_encoding(0), 11181ad6265SDimitry Andric lsda_addr_encoding(llvm::dwarf::DW_EH_PE_omit), 11204eeddc0SDimitry Andric personality_loc(LLDB_INVALID_ADDRESS) {} 1130b57cec5SDimitry Andric }; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric typedef std::shared_ptr<CIE> CIESP; 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric typedef std::map<dw_offset_t, CIESP> cie_map_t; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric // Start address (file address), size, offset of FDE location used for 1200b57cec5SDimitry Andric // finding an FDE for a given File address; the start address field is an 1210b57cec5SDimitry Andric // offset into an individual Module. 1220b57cec5SDimitry Andric typedef RangeDataVector<lldb::addr_t, uint32_t, dw_offset_t> FDEEntryMap; 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric bool IsEHFrame() const; 1250b57cec5SDimitry Andric 126bdd1243dSDimitry Andric std::optional<FDEEntryMap::Entry> 1270b57cec5SDimitry Andric GetFirstFDEEntryInRange(const AddressRange &range); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric void GetFDEIndex(); 1300b57cec5SDimitry Andric 13106c3fb27SDimitry Andric bool FDEToUnwindPlan(dw_offset_t offset, Address startaddr, 1320b57cec5SDimitry Andric UnwindPlan &unwind_plan); 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric const CIE *GetCIE(dw_offset_t cie_offset); 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric void GetCFIData(); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric // Applies the specified DWARF opcode to the given row. This function handle 1390b57cec5SDimitry Andric // the commands operates only on a single row (these are the ones what can 1400b57cec5SDimitry Andric // appear both in 1410b57cec5SDimitry Andric // CIE and in FDE). 1420b57cec5SDimitry Andric // Returns true if the opcode is handled and false otherwise. 1430b57cec5SDimitry Andric bool HandleCommonDwarfOpcode(uint8_t primary_opcode, uint8_t extended_opcode, 1440b57cec5SDimitry Andric int32_t data_align, lldb::offset_t &offset, 1450b57cec5SDimitry Andric UnwindPlan::Row &row); 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric ObjectFile &m_objfile; 1480b57cec5SDimitry Andric lldb::SectionSP m_section_sp; 1490b57cec5SDimitry Andric Flags m_flags = 0; 1500b57cec5SDimitry Andric cie_map_t m_cie_map; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric DataExtractor m_cfi_data; 1530b57cec5SDimitry Andric bool m_cfi_data_initialized = false; // only copy the section into the DE once 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric FDEEntryMap m_fde_index; 1560b57cec5SDimitry Andric bool m_fde_index_initialized = false; // only scan the section for FDEs once 1570b57cec5SDimitry Andric std::mutex m_fde_index_mutex; // and isolate the thread that does it 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric Type m_type; 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric CIESP 16206c3fb27SDimitry Andric ParseCIE(const dw_offset_t cie_offset); 1630b57cec5SDimitry Andric GetRegisterKind()1640b57cec5SDimitry Andric lldb::RegisterKind GetRegisterKind() const { 1650b57cec5SDimitry Andric return m_type == EH ? lldb::eRegisterKindEHFrame : lldb::eRegisterKindDWARF; 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric }; 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric } // namespace lldb_private 1700b57cec5SDimitry Andric 1715ffd83dbSDimitry Andric #endif // LLDB_SYMBOL_DWARFCALLFRAMEINFO_H 172