1 //===-- DWARFCallFrameInfo.h ------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_DWARFCallFrameInfo_h_
10 #define liblldb_DWARFCallFrameInfo_h_
15 #include "lldb/Core/AddressRange.h"
16 #include "lldb/Core/dwarf.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Symbol/UnwindPlan.h"
19 #include "lldb/Utility/Flags.h"
20 #include "lldb/Utility/RangeMap.h"
21 #include "lldb/Utility/VMRange.h"
22 #include "lldb/lldb-private.h"
24 namespace lldb_private {
26 // DWARFCallFrameInfo is a class which can read eh_frame and DWARF Call Frame
27 // Information FDEs. It stores little information internally. Only two APIs
28 // are exported - one to find the high/low pc values of a function given a text
29 // address via the information in the eh_frame / debug_frame, and one to
30 // generate an UnwindPlan based on the FDE in the eh_frame / debug_frame
33 class DWARFCallFrameInfo {
35 enum Type { EH, DWARF };
37 DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP §ion, Type type);
39 ~DWARFCallFrameInfo() = default;
41 // Locate an AddressRange that includes the provided Address in this object's
42 // eh_frame/debug_info Returns true if a range is found to cover that
44 bool GetAddressRange(Address addr, AddressRange &range);
46 /// Return an UnwindPlan based on the call frame information encoded in the
47 /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid
48 /// (at least) for the given address.
49 bool GetUnwindPlan(const Address &addr, UnwindPlan &unwind_plan);
51 /// Return an UnwindPlan based on the call frame information encoded in the
52 /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid
53 /// (at least) for some address in the given range.
54 bool GetUnwindPlan(const AddressRange &range, UnwindPlan &unwind_plan);
56 typedef RangeVector<lldb::addr_t, uint32_t> FunctionAddressAndSizeVector;
58 // Build a vector of file address and size for all functions in this Module
59 // based on the eh_frame FDE entries.
61 // The eh_frame information can be a useful source of file address and size
62 // of the functions in a Module. Often a binary's non-exported symbols are
63 // stripped before shipping so lldb won't know the start addr / size of many
64 // functions in the Module. But the eh_frame can help to give the addresses
65 // of these stripped symbols, at least.
67 // \param[out] function_info
68 // A vector provided by the caller is filled out. May be empty if no
70 // is present in this Module.
73 GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info);
75 void ForEachFDEEntries(
76 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback);
79 enum { CFI_AUG_MAX_SIZE = 8, CFI_HEADER_SIZE = 8 };
81 CFI_VERSION1 = 1, // DWARF v.2
82 CFI_VERSION3 = 3, // DWARF v.3
83 CFI_VERSION4 = 4 // DWARF v.4, v.5
87 dw_offset_t cie_offset;
89 char augmentation[CFI_AUG_MAX_SIZE]; // This is typically empty or very
91 uint8_t address_size = sizeof(uint32_t); // The size of a target address.
92 uint8_t segment_size = 0; // The size of a segment selector.
96 uint32_t return_addr_reg_num;
97 dw_offset_t inst_offset; // offset of CIE instructions in mCFIData
98 uint32_t inst_length; // length of CIE instructions in mCFIData
100 uint8_t lsda_addr_encoding; // The encoding of the LSDA address in the FDE
102 lldb::addr_t personality_loc; // (file) address of the pointer to the
103 // personality routine
104 lldb_private::UnwindPlan::Row initial_row;
106 CIE(dw_offset_t offset)
107 : cie_offset(offset), version(-1), code_align(0), data_align(0),
108 return_addr_reg_num(LLDB_INVALID_REGNUM), inst_offset(0),
109 inst_length(0), ptr_encoding(0), lsda_addr_encoding(DW_EH_PE_omit),
110 personality_loc(LLDB_INVALID_ADDRESS), initial_row() {}
113 typedef std::shared_ptr<CIE> CIESP;
115 typedef std::map<dw_offset_t, CIESP> cie_map_t;
117 // Start address (file address), size, offset of FDE location used for
118 // finding an FDE for a given File address; the start address field is an
119 // offset into an individual Module.
120 typedef RangeDataVector<lldb::addr_t, uint32_t, dw_offset_t> FDEEntryMap;
122 bool IsEHFrame() const;
124 llvm::Optional<FDEEntryMap::Entry>
125 GetFirstFDEEntryInRange(const AddressRange &range);
129 bool FDEToUnwindPlan(uint32_t offset, Address startaddr,
130 UnwindPlan &unwind_plan);
132 const CIE *GetCIE(dw_offset_t cie_offset);
136 // Applies the specified DWARF opcode to the given row. This function handle
137 // the commands operates only on a single row (these are the ones what can
140 // Returns true if the opcode is handled and false otherwise.
141 bool HandleCommonDwarfOpcode(uint8_t primary_opcode, uint8_t extended_opcode,
142 int32_t data_align, lldb::offset_t &offset,
143 UnwindPlan::Row &row);
145 ObjectFile &m_objfile;
146 lldb::SectionSP m_section_sp;
150 DataExtractor m_cfi_data;
151 bool m_cfi_data_initialized = false; // only copy the section into the DE once
153 FDEEntryMap m_fde_index;
154 bool m_fde_index_initialized = false; // only scan the section for FDEs once
155 std::mutex m_fde_index_mutex; // and isolate the thread that does it
160 ParseCIE(const uint32_t cie_offset);
162 lldb::RegisterKind GetRegisterKind() const {
163 return m_type == EH ? lldb::eRegisterKindEHFrame : lldb::eRegisterKindDWARF;
167 } // namespace lldb_private
169 #endif // liblldb_DWARFCallFrameInfo_h_