1 //===-- UnwindTable.cpp -----------------------------------------*- 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 #include "lldb/Symbol/UnwindTable.h" 11 12 #include <stdio.h> 13 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" 22 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. 27 28 using namespace lldb; 29 using namespace lldb_private; 30 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() {} 34 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. 38 39 void UnwindTable::Initialize() { 40 if (m_initialized) 41 return; 42 43 std::lock_guard<std::mutex> guard(m_mutex); 44 45 if (m_initialized) // check again once we've acquired the lock 46 return; 47 48 SectionList *sl = m_object_file.GetSectionList(); 49 if (sl) { 50 SectionSP sect = sl->FindSectionByType(eSectionTypeEHFrame, true); 51 if (sect.get()) { 52 m_eh_frame_up.reset(new DWARFCallFrameInfo(m_object_file, sect, 53 eRegisterKindEHFrame, true)); 54 } 55 sect = sl->FindSectionByType(eSectionTypeCompactUnwind, true); 56 if (sect.get()) { 57 m_compact_unwind_up.reset(new CompactUnwindInfo(m_object_file, sect)); 58 } 59 sect = sl->FindSectionByType(eSectionTypeARMexidx, true); 60 if (sect.get()) { 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)); 65 } 66 } 67 } 68 69 m_initialized = true; 70 } 71 72 UnwindTable::~UnwindTable() {} 73 74 FuncUnwindersSP 75 UnwindTable::GetFuncUnwindersContainingAddress(const Address &addr, 76 SymbolContext &sc) { 77 FuncUnwindersSP no_unwind_found; 78 79 Initialize(); 80 81 std::lock_guard<std::mutex> guard(m_mutex); 82 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)) 93 --pos; 94 95 if (pos->second->ContainsAddress(addr)) 96 return pos->second; 97 } 98 99 AddressRange range; 100 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, 101 false, range) || 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; 107 } 108 } 109 110 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range)); 111 m_unwinds.insert(insert_pos, 112 std::make_pair(range.GetBaseAddress().GetFileAddress(), 113 func_unwinder_sp)); 114 // StreamFile s(stdout, false); 115 // Dump (s); 116 return func_unwinder_sp; 117 } 118 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 122 // want to create 123 // new UnwindPlans, not re-use existing ones. 124 125 FuncUnwindersSP 126 UnwindTable::GetUncachedFuncUnwindersContainingAddress(const Address &addr, 127 SymbolContext &sc) { 128 FuncUnwindersSP no_unwind_found; 129 Initialize(); 130 131 AddressRange range; 132 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, 133 false, range) || 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; 139 } 140 } 141 142 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range)); 143 return func_unwinder_sp; 144 } 145 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), 154 pos->first); 155 } 156 s.EOL(); 157 } 158 159 DWARFCallFrameInfo *UnwindTable::GetEHFrameInfo() { 160 Initialize(); 161 return m_eh_frame_up.get(); 162 } 163 164 CompactUnwindInfo *UnwindTable::GetCompactUnwindInfo() { 165 Initialize(); 166 return m_compact_unwind_up.get(); 167 } 168 169 ArmUnwindInfo *UnwindTable::GetArmUnwindInfo() { 170 Initialize(); 171 return m_arm_unwind_up.get(); 172 } 173 174 bool UnwindTable::GetArchitecture(lldb_private::ArchSpec &arch) { 175 return m_object_file.GetArchitecture(arch); 176 } 177 178 bool UnwindTable::GetAllowAssemblyEmulationUnwindPlans() { 179 return m_object_file.AllowAssemblyEmulationUnwindPlans(); 180 } 181