1 //===-- DWARFCallFrameInfo.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_DWARFCallFrameInfo_h_
11 #define liblldb_DWARFCallFrameInfo_h_
16 #include "lldb/Core/AddressRange.h"
17 #include "lldb/Utility/Flags.h"
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"
26 namespace lldb_private {
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
35 class DWARFCallFrameInfo {
37 enum Type { EH, DWARF };
39 DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP §ion, Type type);
41 ~DWARFCallFrameInfo() = default;
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
46 bool GetAddressRange(Address addr, AddressRange &range);
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);
52 typedef RangeVector<lldb::addr_t, uint32_t> FunctionAddressAndSizeVector;
54 //------------------------------------------------------------------
55 // Build a vector of file address and size for all functions in this Module
56 // based on the eh_frame FDE entries.
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.
64 // @param[out] function_info
65 // A vector provided by the caller is filled out. May be empty if no
67 // is present in this Module.
70 GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info);
72 void ForEachFDEEntries(
73 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback);
76 enum { CFI_AUG_MAX_SIZE = 8, CFI_HEADER_SIZE = 8 };
78 CFI_VERSION1 = 1, // DWARF v.2
79 CFI_VERSION3 = 3, // DWARF v.3
80 CFI_VERSION4 = 4 // DWARF v.4, v.5
84 dw_offset_t cie_offset;
86 char augmentation[CFI_AUG_MAX_SIZE]; // This is typically empty or very
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.
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
97 uint8_t lsda_addr_encoding; // The encoding of the LSDA address in the FDE
99 lldb::addr_t personality_loc; // (file) address of the pointer to the
100 // personality routine
101 lldb_private::UnwindPlan::Row initial_row;
103 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() {}
110 typedef std::shared_ptr<CIE> CIESP;
112 typedef std::map<dw_offset_t, CIESP> cie_map_t;
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;
119 bool IsEHFrame() const;
121 bool GetFDEEntryByFileAddress(lldb::addr_t file_offset,
122 FDEEntryMap::Entry &fde_entry);
126 bool FDEToUnwindPlan(uint32_t offset, Address startaddr,
127 UnwindPlan &unwind_plan);
129 const CIE *GetCIE(dw_offset_t cie_offset);
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
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);
142 ObjectFile &m_objfile;
143 lldb::SectionSP m_section_sp;
147 DataExtractor m_cfi_data;
148 bool m_cfi_data_initialized = false; // only copy the section into the DE once
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
157 ParseCIE(const uint32_t cie_offset);
159 lldb::RegisterKind GetRegisterKind() const {
160 return m_type == EH ? lldb::eRegisterKindEHFrame : lldb::eRegisterKindDWARF;
164 } // namespace lldb_private
166 #endif // liblldb_DWARFCallFrameInfo_h_