]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Symbol/DWARFCallFrameInfo.h
Apply fixes in ena-com
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Symbol / DWARFCallFrameInfo.h
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
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.
34
35 class DWARFCallFrameInfo {
36 public:
37   enum Type { EH, DWARF };
38
39   DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP &section, Type type);
40
41   ~DWARFCallFrameInfo() = default;
42
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);
47
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);
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 of
59   // the functions in a Module.  Often a binary's non-exported symbols are
60   // stripped
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.
64   //
65   // @param[out] function_info
66   //      A vector provided by the caller is filled out.  May be empty if no
67   //      FDEs/no eh_frame
68   //      is present in this Module.
69
70   void
71   GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info);
72
73   void ForEachFDEEntries(
74       const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback);
75
76 private:
77   enum { CFI_AUG_MAX_SIZE = 8, CFI_HEADER_SIZE = 8 };
78   enum CFIVersion {
79     CFI_VERSION1 = 1, // DWARF v.2
80     CFI_VERSION3 = 3, // DWARF v.3
81     CFI_VERSION4 = 4  // DWARF v.4, v.5
82   };
83
84   struct CIE {
85     dw_offset_t cie_offset;
86     uint8_t version;
87     char augmentation[CFI_AUG_MAX_SIZE]; // This is typically empty or very
88                                          // short.
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.
91
92     uint32_t code_align;
93     int32_t data_align;
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
97     uint8_t ptr_encoding;
98     uint8_t lsda_addr_encoding;   // The encoding of the LSDA address in the FDE
99                                   // augmentation data
100     lldb::addr_t personality_loc; // (file) address of the pointer to the
101                                   // personality routine
102     lldb_private::UnwindPlan::Row initial_row;
103
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() {}
109   };
110
111   typedef std::shared_ptr<CIE> CIESP;
112
113   typedef std::map<dw_offset_t, CIESP> cie_map_t;
114
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
117   // is
118   // an offset into an individual Module.
119   typedef RangeDataVector<lldb::addr_t, uint32_t, dw_offset_t> FDEEntryMap;
120
121   bool IsEHFrame() const;
122
123   bool GetFDEEntryByFileAddress(lldb::addr_t file_offset,
124                                 FDEEntryMap::Entry &fde_entry);
125
126   void GetFDEIndex();
127
128   bool FDEToUnwindPlan(uint32_t offset, Address startaddr,
129                        UnwindPlan &unwind_plan);
130
131   const CIE *GetCIE(dw_offset_t cie_offset);
132
133   void GetCFIData();
134
135   // Applies the specified DWARF opcode to the given row. This function handle
136   // the commands
137   // operates only on a single row (these are the ones what can appear both in
138   // CIE and in FDE).
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);
143
144   ObjectFile &m_objfile;
145   lldb::SectionSP m_section_sp;
146   Flags m_flags = 0;
147   cie_map_t m_cie_map;
148
149   DataExtractor m_cfi_data;
150   bool m_cfi_data_initialized = false; // only copy the section into the DE once
151
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
155
156   Type m_type;
157
158   CIESP
159   ParseCIE(const uint32_t cie_offset);
160
161   lldb::RegisterKind GetRegisterKind() const {
162     return m_type == EH ? lldb::eRegisterKindEHFrame : lldb::eRegisterKindDWARF;
163   }
164 };
165
166 } // namespace lldb_private
167
168 #endif // liblldb_DWARFCallFrameInfo_h_