1 //===-- UnwindTable.cpp -----------------------------------------*- 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 #include "lldb/Symbol/UnwindTable.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Symbol/ArmUnwindInfo.h"
17 #include "lldb/Symbol/CompactUnwindInfo.h"
18 #include "lldb/Symbol/DWARFCallFrameInfo.h"
19 #include "lldb/Symbol/FuncUnwinders.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/SymbolContext.h"
23 // There is one UnwindTable object per ObjectFile.
24 // It contains a list of Unwind objects -- one per function, populated lazily --
25 // for the ObjectFile.
26 // Each Unwind object has multiple UnwindPlans for different scenarios.
29 using namespace lldb_private;
31 UnwindTable::UnwindTable(ObjectFile &objfile)
32 : m_object_file(objfile), m_unwinds(), m_initialized(false), m_mutex(),
33 m_eh_frame_up(), m_compact_unwind_up(), m_arm_unwind_up() {}
35 // We can't do some of this initialization when the ObjectFile is running its
36 // ctor; delay doing it
37 // until needed for something.
39 void UnwindTable::Initialize() {
43 std::lock_guard<std::mutex> guard(m_mutex);
45 if (m_initialized) // check again once we've acquired the lock
48 SectionList *sl = m_object_file.GetSectionList();
50 SectionSP sect = sl->FindSectionByType(eSectionTypeEHFrame, true);
52 m_eh_frame_up.reset(new DWARFCallFrameInfo(m_object_file, sect,
53 eRegisterKindEHFrame, true));
55 sect = sl->FindSectionByType(eSectionTypeCompactUnwind, true);
57 m_compact_unwind_up.reset(new CompactUnwindInfo(m_object_file, sect));
59 sect = sl->FindSectionByType(eSectionTypeARMexidx, true);
61 SectionSP sect_extab = sl->FindSectionByType(eSectionTypeARMextab, true);
62 if (sect_extab.get()) {
63 m_arm_unwind_up.reset(
64 new ArmUnwindInfo(m_object_file, sect, sect_extab));
72 UnwindTable::~UnwindTable() {}
75 UnwindTable::GetFuncUnwindersContainingAddress(const Address &addr,
77 FuncUnwindersSP no_unwind_found;
81 std::lock_guard<std::mutex> guard(m_mutex);
83 // There is an UnwindTable per object file, so we can safely use file handles
84 addr_t file_addr = addr.GetFileAddress();
85 iterator end = m_unwinds.end();
86 iterator insert_pos = end;
87 if (!m_unwinds.empty()) {
88 insert_pos = m_unwinds.lower_bound(file_addr);
89 iterator pos = insert_pos;
90 if ((pos == m_unwinds.end()) ||
91 (pos != m_unwinds.begin() &&
92 pos->second->GetFunctionStartAddress() != addr))
95 if (pos->second->ContainsAddress(addr))
100 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
102 !range.GetBaseAddress().IsValid()) {
103 // Does the eh_frame unwind info has a function bounds for this addr?
104 if (m_eh_frame_up == nullptr ||
105 !m_eh_frame_up->GetAddressRange(addr, range)) {
106 return no_unwind_found;
110 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range));
111 m_unwinds.insert(insert_pos,
112 std::make_pair(range.GetBaseAddress().GetFileAddress(),
114 // StreamFile s(stdout, false);
116 return func_unwinder_sp;
119 // Ignore any existing FuncUnwinders for this function, create a new one and
120 // don't add it to the
121 // UnwindTable. This is intended for use by target modules show-unwind where we
123 // new UnwindPlans, not re-use existing ones.
126 UnwindTable::GetUncachedFuncUnwindersContainingAddress(const Address &addr,
128 FuncUnwindersSP no_unwind_found;
132 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
134 !range.GetBaseAddress().IsValid()) {
135 // Does the eh_frame unwind info has a function bounds for this addr?
136 if (m_eh_frame_up == nullptr ||
137 !m_eh_frame_up->GetAddressRange(addr, range)) {
138 return no_unwind_found;
142 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range));
143 return func_unwinder_sp;
146 void UnwindTable::Dump(Stream &s) {
147 std::lock_guard<std::mutex> guard(m_mutex);
148 s.Printf("UnwindTable for '%s':\n",
149 m_object_file.GetFileSpec().GetPath().c_str());
150 const_iterator begin = m_unwinds.begin();
151 const_iterator end = m_unwinds.end();
152 for (const_iterator pos = begin; pos != end; ++pos) {
153 s.Printf("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance(begin, pos),
159 DWARFCallFrameInfo *UnwindTable::GetEHFrameInfo() {
161 return m_eh_frame_up.get();
164 CompactUnwindInfo *UnwindTable::GetCompactUnwindInfo() {
166 return m_compact_unwind_up.get();
169 ArmUnwindInfo *UnwindTable::GetArmUnwindInfo() {
171 return m_arm_unwind_up.get();
174 bool UnwindTable::GetArchitecture(lldb_private::ArchSpec &arch) {
175 return m_object_file.GetArchitecture(arch);
178 bool UnwindTable::GetAllowAssemblyEmulationUnwindPlans() {
179 return m_object_file.AllowAssemblyEmulationUnwindPlans();