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
29 // Call Frame Information FDEs. It stores little information internally.
30 // Only two APIs are exported - one to find the high/low pc values
31 // of a function given a text address via the information in the
32 // eh_frame / debug_frame, and one to generate an UnwindPlan based
33 // on the FDE in the eh_frame / debug_frame section.
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
44 // object's eh_frame/debug_info
45 // Returns true if a range is found to cover that address.
46 bool GetAddressRange(Address addr, AddressRange &range);
48 // Return an UnwindPlan based on the call frame information encoded
49 // in the 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 of
59 // the functions in a Module. Often a binary's non-exported symbols are
61 // before shipping so lldb won't know the start addr / size of many functions
62 // in the Module. But the eh_frame can help to give the addresses of these
63 // stripped symbols, at least.
65 // @param[out] function_info
66 // A vector provided by the caller is filled out. May be empty if no
68 // is present in this Module.
71 GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info);
73 void ForEachFDEEntries(
74 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback);
77 enum { CFI_AUG_MAX_SIZE = 8, CFI_HEADER_SIZE = 8 };
79 CFI_VERSION1 = 1, // DWARF v.2
80 CFI_VERSION3 = 3, // DWARF v.3
81 CFI_VERSION4 = 4 // DWARF v.4, v.5
85 dw_offset_t cie_offset;
87 char augmentation[CFI_AUG_MAX_SIZE]; // This is typically empty or very
89 uint8_t address_size = sizeof(uint32_t); // The size of a target address.
90 uint8_t segment_size = 0; // The size of a segment selector.
94 uint32_t return_addr_reg_num;
95 dw_offset_t inst_offset; // offset of CIE instructions in mCFIData
96 uint32_t inst_length; // length of CIE instructions in mCFIData
98 uint8_t lsda_addr_encoding; // The encoding of the LSDA address in the FDE
100 lldb::addr_t personality_loc; // (file) address of the pointer to the
101 // personality routine
102 lldb_private::UnwindPlan::Row initial_row;
104 CIE(dw_offset_t offset)
105 : cie_offset(offset), version(-1), code_align(0), data_align(0),
106 return_addr_reg_num(LLDB_INVALID_REGNUM), inst_offset(0),
107 inst_length(0), ptr_encoding(0), lsda_addr_encoding(DW_EH_PE_omit),
108 personality_loc(LLDB_INVALID_ADDRESS), initial_row() {}
111 typedef std::shared_ptr<CIE> CIESP;
113 typedef std::map<dw_offset_t, CIESP> cie_map_t;
115 // Start address (file address), size, offset of FDE location
116 // used for finding an FDE for a given File address; the start address field
118 // an offset into an individual Module.
119 typedef RangeDataVector<lldb::addr_t, uint32_t, dw_offset_t> FDEEntryMap;
121 bool IsEHFrame() const;
123 bool GetFDEEntryByFileAddress(lldb::addr_t file_offset,
124 FDEEntryMap::Entry &fde_entry);
128 bool FDEToUnwindPlan(uint32_t offset, Address startaddr,
129 UnwindPlan &unwind_plan);
131 const CIE *GetCIE(dw_offset_t cie_offset);
135 // Applies the specified DWARF opcode to the given row. This function handle
137 // operates only on a single row (these are the ones what can appear both in
139 // Returns true if the opcode is handled and false otherwise.
140 bool HandleCommonDwarfOpcode(uint8_t primary_opcode, uint8_t extended_opcode,
141 int32_t data_align, lldb::offset_t &offset,
142 UnwindPlan::Row &row);
144 ObjectFile &m_objfile;
145 lldb::SectionSP m_section_sp;
149 DataExtractor m_cfi_data;
150 bool m_cfi_data_initialized = false; // only copy the section into the DE once
152 FDEEntryMap m_fde_index;
153 bool m_fde_index_initialized = false; // only scan the section for FDEs once
154 std::mutex m_fde_index_mutex; // and isolate the thread that does it
159 ParseCIE(const uint32_t cie_offset);
161 lldb::RegisterKind GetRegisterKind() const {
162 return m_type == EH ? lldb::eRegisterKindEHFrame : lldb::eRegisterKindDWARF;
166 } // namespace lldb_private
168 #endif // liblldb_DWARFCallFrameInfo_h_