llvm-project
254 строки · 7.7 Кб
1//===-- UnwindTable.cpp ---------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Symbol/UnwindTable.h"
10
11#include <cstdio>
12#include <optional>
13
14#include "lldb/Core/Module.h"
15#include "lldb/Core/Section.h"
16#include "lldb/Symbol/ArmUnwindInfo.h"
17#include "lldb/Symbol/CallFrameInfo.h"
18#include "lldb/Symbol/CompactUnwindInfo.h"
19#include "lldb/Symbol/DWARFCallFrameInfo.h"
20#include "lldb/Symbol/FuncUnwinders.h"
21#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Symbol/SymbolContext.h"
23#include "lldb/Symbol/SymbolVendor.h"
24
25// There is one UnwindTable object per ObjectFile. It contains a list of Unwind
26// objects -- one per function, populated lazily -- for the ObjectFile. Each
27// Unwind object has multiple UnwindPlans for different scenarios.
28
29using namespace lldb;
30using namespace lldb_private;
31
32UnwindTable::UnwindTable(Module &module)
33: m_module(module), m_unwinds(), m_initialized(false), m_mutex(),
34m_object_file_unwind_up(), m_eh_frame_up(), m_compact_unwind_up(),
35m_arm_unwind_up() {}
36
37// We can't do some of this initialization when the ObjectFile is running its
38// ctor; delay doing it until needed for something.
39
40void UnwindTable::Initialize() {
41if (m_initialized)
42return;
43
44std::lock_guard<std::mutex> guard(m_mutex);
45
46if (m_initialized) // check again once we've acquired the lock
47return;
48m_initialized = true;
49ObjectFile *object_file = m_module.GetObjectFile();
50if (!object_file)
51return;
52
53m_object_file_unwind_up = object_file->CreateCallFrameInfo();
54
55SectionList *sl = m_module.GetSectionList();
56if (!sl)
57return;
58
59SectionSP sect = sl->FindSectionByType(eSectionTypeEHFrame, true);
60if (sect.get()) {
61m_eh_frame_up = std::make_unique<DWARFCallFrameInfo>(
62*object_file, sect, DWARFCallFrameInfo::EH);
63}
64
65sect = sl->FindSectionByType(eSectionTypeDWARFDebugFrame, true);
66if (sect) {
67m_debug_frame_up = std::make_unique<DWARFCallFrameInfo>(
68*object_file, sect, DWARFCallFrameInfo::DWARF);
69}
70
71sect = sl->FindSectionByType(eSectionTypeCompactUnwind, true);
72if (sect) {
73m_compact_unwind_up =
74std::make_unique<CompactUnwindInfo>(*object_file, sect);
75}
76
77sect = sl->FindSectionByType(eSectionTypeARMexidx, true);
78if (sect) {
79SectionSP sect_extab = sl->FindSectionByType(eSectionTypeARMextab, true);
80if (sect_extab.get()) {
81m_arm_unwind_up =
82std::make_unique<ArmUnwindInfo>(*object_file, sect, sect_extab);
83}
84}
85}
86
87void UnwindTable::Update() {
88if (!m_initialized)
89return Initialize();
90
91std::lock_guard<std::mutex> guard(m_mutex);
92
93ObjectFile *object_file = m_module.GetObjectFile();
94if (!object_file)
95return;
96
97if (!m_object_file_unwind_up)
98m_object_file_unwind_up = object_file->CreateCallFrameInfo();
99
100SectionList *sl = m_module.GetSectionList();
101if (!sl)
102return;
103
104SectionSP sect = sl->FindSectionByType(eSectionTypeEHFrame, true);
105if (!m_eh_frame_up && sect) {
106m_eh_frame_up = std::make_unique<DWARFCallFrameInfo>(
107*object_file, sect, DWARFCallFrameInfo::EH);
108}
109
110sect = sl->FindSectionByType(eSectionTypeDWARFDebugFrame, true);
111if (!m_debug_frame_up && sect) {
112m_debug_frame_up = std::make_unique<DWARFCallFrameInfo>(
113*object_file, sect, DWARFCallFrameInfo::DWARF);
114}
115
116sect = sl->FindSectionByType(eSectionTypeCompactUnwind, true);
117if (!m_compact_unwind_up && sect) {
118m_compact_unwind_up =
119std::make_unique<CompactUnwindInfo>(*object_file, sect);
120}
121
122sect = sl->FindSectionByType(eSectionTypeARMexidx, true);
123if (!m_arm_unwind_up && sect) {
124SectionSP sect_extab = sl->FindSectionByType(eSectionTypeARMextab, true);
125if (sect_extab.get()) {
126m_arm_unwind_up =
127std::make_unique<ArmUnwindInfo>(*object_file, sect, sect_extab);
128}
129}
130}
131
132UnwindTable::~UnwindTable() = default;
133
134std::optional<AddressRange>
135UnwindTable::GetAddressRange(const Address &addr, const SymbolContext &sc) {
136AddressRange range;
137
138// First check the unwind info from the object file plugin
139if (m_object_file_unwind_up &&
140m_object_file_unwind_up->GetAddressRange(addr, range))
141return range;
142
143// Check the symbol context
144if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
145false, range) &&
146range.GetBaseAddress().IsValid())
147return range;
148
149// Does the eh_frame unwind info has a function bounds for this addr?
150if (m_eh_frame_up && m_eh_frame_up->GetAddressRange(addr, range))
151return range;
152
153// Try debug_frame as well
154if (m_debug_frame_up && m_debug_frame_up->GetAddressRange(addr, range))
155return range;
156
157return std::nullopt;
158}
159
160FuncUnwindersSP
161UnwindTable::GetFuncUnwindersContainingAddress(const Address &addr,
162SymbolContext &sc) {
163Initialize();
164
165std::lock_guard<std::mutex> guard(m_mutex);
166
167// There is an UnwindTable per object file, so we can safely use file handles
168addr_t file_addr = addr.GetFileAddress();
169iterator end = m_unwinds.end();
170iterator insert_pos = end;
171if (!m_unwinds.empty()) {
172insert_pos = m_unwinds.lower_bound(file_addr);
173iterator pos = insert_pos;
174if ((pos == m_unwinds.end()) ||
175(pos != m_unwinds.begin() &&
176pos->second->GetFunctionStartAddress() != addr))
177--pos;
178
179if (pos->second->ContainsAddress(addr))
180return pos->second;
181}
182
183auto range_or = GetAddressRange(addr, sc);
184if (!range_or)
185return nullptr;
186
187FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, *range_or));
188m_unwinds.insert(insert_pos,
189std::make_pair(range_or->GetBaseAddress().GetFileAddress(),
190func_unwinder_sp));
191return func_unwinder_sp;
192}
193
194// Ignore any existing FuncUnwinders for this function, create a new one and
195// don't add it to the UnwindTable. This is intended for use by target modules
196// show-unwind where we want to create new UnwindPlans, not re-use existing
197// ones.
198FuncUnwindersSP UnwindTable::GetUncachedFuncUnwindersContainingAddress(
199const Address &addr, const SymbolContext &sc) {
200Initialize();
201
202auto range_or = GetAddressRange(addr, sc);
203if (!range_or)
204return nullptr;
205
206return std::make_shared<FuncUnwinders>(*this, *range_or);
207}
208
209void UnwindTable::Dump(Stream &s) {
210std::lock_guard<std::mutex> guard(m_mutex);
211s.Format("UnwindTable for '{0}':\n", m_module.GetFileSpec());
212const_iterator begin = m_unwinds.begin();
213const_iterator end = m_unwinds.end();
214for (const_iterator pos = begin; pos != end; ++pos) {
215s.Printf("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance(begin, pos),
216pos->first);
217}
218s.EOL();
219}
220
221lldb_private::CallFrameInfo *UnwindTable::GetObjectFileUnwindInfo() {
222Initialize();
223return m_object_file_unwind_up.get();
224}
225
226DWARFCallFrameInfo *UnwindTable::GetEHFrameInfo() {
227Initialize();
228return m_eh_frame_up.get();
229}
230
231DWARFCallFrameInfo *UnwindTable::GetDebugFrameInfo() {
232Initialize();
233return m_debug_frame_up.get();
234}
235
236CompactUnwindInfo *UnwindTable::GetCompactUnwindInfo() {
237Initialize();
238return m_compact_unwind_up.get();
239}
240
241ArmUnwindInfo *UnwindTable::GetArmUnwindInfo() {
242Initialize();
243return m_arm_unwind_up.get();
244}
245
246SymbolFile *UnwindTable::GetSymbolFile() { return m_module.GetSymbolFile(); }
247
248ArchSpec UnwindTable::GetArchitecture() { return m_module.GetArchitecture(); }
249
250bool UnwindTable::GetAllowAssemblyEmulationUnwindPlans() {
251if (ObjectFile *object_file = m_module.GetObjectFile())
252return object_file->AllowAssemblyEmulationUnwindPlans();
253return false;
254}
255