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 m_initialized = true; 48 49 SectionList *sl = m_object_file.GetSectionList(); 50 if (!sl) 51 return; 52 53 SectionSP sect = sl->FindSectionByType(eSectionTypeEHFrame, true); 54 if (sect.get()) { 55 m_eh_frame_up.reset( 56 new DWARFCallFrameInfo(m_object_file, sect, DWARFCallFrameInfo::EH)); 57 } 58 59 sect = sl->FindSectionByType(eSectionTypeDWARFDebugFrame, true); 60 if (sect) { 61 m_debug_frame_up.reset( 62 new DWARFCallFrameInfo(m_object_file, sect, DWARFCallFrameInfo::DWARF)); 63 } 64 65 sect = sl->FindSectionByType(eSectionTypeCompactUnwind, true); 66 if (sect) { 67 m_compact_unwind_up.reset(new CompactUnwindInfo(m_object_file, sect)); 68 } 69 70 sect = sl->FindSectionByType(eSectionTypeARMexidx, true); 71 if (sect) { 72 SectionSP sect_extab = sl->FindSectionByType(eSectionTypeARMextab, true); 73 if (sect_extab.get()) { 74 m_arm_unwind_up.reset(new ArmUnwindInfo(m_object_file, sect, sect_extab)); 75 } 76 } 77 } 78 79 UnwindTable::~UnwindTable() {} 80 81 llvm::Optional<AddressRange> UnwindTable::GetAddressRange(const Address &addr, 82 SymbolContext &sc) { 83 AddressRange range; 84 85 // First check the symbol context 86 if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, 87 false, range) && 88 range.GetBaseAddress().IsValid()) 89 return range; 90 91 // Does the eh_frame unwind info has a function bounds for this addr? 92 if (m_eh_frame_up && m_eh_frame_up->GetAddressRange(addr, range)) 93 return range; 94 95 // Try debug_frame as well 96 if (m_debug_frame_up && m_debug_frame_up->GetAddressRange(addr, range)) 97 return range; 98 99 return llvm::None; 100 } 101 102 FuncUnwindersSP 103 UnwindTable::GetFuncUnwindersContainingAddress(const Address &addr, 104 SymbolContext &sc) { 105 Initialize(); 106 107 std::lock_guard<std::mutex> guard(m_mutex); 108 109 // There is an UnwindTable per object file, so we can safely use file handles 110 addr_t file_addr = addr.GetFileAddress(); 111 iterator end = m_unwinds.end(); 112 iterator insert_pos = end; 113 if (!m_unwinds.empty()) { 114 insert_pos = m_unwinds.lower_bound(file_addr); 115 iterator pos = insert_pos; 116 if ((pos == m_unwinds.end()) || 117 (pos != m_unwinds.begin() && 118 pos->second->GetFunctionStartAddress() != addr)) 119 --pos; 120 121 if (pos->second->ContainsAddress(addr)) 122 return pos->second; 123 } 124 125 auto range_or = GetAddressRange(addr, sc); 126 if (!range_or) 127 return nullptr; 128 129 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, *range_or)); 130 m_unwinds.insert(insert_pos, 131 std::make_pair(range_or->GetBaseAddress().GetFileAddress(), 132 func_unwinder_sp)); 133 return func_unwinder_sp; 134 } 135 136 // Ignore any existing FuncUnwinders for this function, create a new one and 137 // don't add it to the 138 // UnwindTable. This is intended for use by target modules show-unwind where we 139 // want to create 140 // new UnwindPlans, not re-use existing ones. 141 FuncUnwindersSP 142 UnwindTable::GetUncachedFuncUnwindersContainingAddress(const Address &addr, 143 SymbolContext &sc) { 144 Initialize(); 145 146 auto range_or = GetAddressRange(addr, sc); 147 if (!range_or) 148 return nullptr; 149 150 return std::make_shared<FuncUnwinders>(*this, *range_or); 151 } 152 153 void UnwindTable::Dump(Stream &s) { 154 std::lock_guard<std::mutex> guard(m_mutex); 155 s.Printf("UnwindTable for '%s':\n", 156 m_object_file.GetFileSpec().GetPath().c_str()); 157 const_iterator begin = m_unwinds.begin(); 158 const_iterator end = m_unwinds.end(); 159 for (const_iterator pos = begin; pos != end; ++pos) { 160 s.Printf("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance(begin, pos), 161 pos->first); 162 } 163 s.EOL(); 164 } 165 166 DWARFCallFrameInfo *UnwindTable::GetEHFrameInfo() { 167 Initialize(); 168 return m_eh_frame_up.get(); 169 } 170 171 DWARFCallFrameInfo *UnwindTable::GetDebugFrameInfo() { 172 Initialize(); 173 return m_debug_frame_up.get(); 174 } 175 176 CompactUnwindInfo *UnwindTable::GetCompactUnwindInfo() { 177 Initialize(); 178 return m_compact_unwind_up.get(); 179 } 180 181 ArmUnwindInfo *UnwindTable::GetArmUnwindInfo() { 182 Initialize(); 183 return m_arm_unwind_up.get(); 184 } 185 186 bool UnwindTable::GetArchitecture(lldb_private::ArchSpec &arch) { 187 return m_object_file.GetArchitecture(arch); 188 } 189 190 bool UnwindTable::GetAllowAssemblyEmulationUnwindPlans() { 191 return m_object_file.AllowAssemblyEmulationUnwindPlans(); 192 } 193