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/ObjectFile.h" 17 #include "lldb/Symbol/FuncUnwinders.h" 18 #include "lldb/Symbol/SymbolContext.h" 19 #include "lldb/Symbol/DWARFCallFrameInfo.h" 20 #include "lldb/Symbol/ArmUnwindInfo.h" 21 #include "lldb/Symbol/CompactUnwindInfo.h" 22 23 // There is one UnwindTable object per ObjectFile. 24 // It contains a list of Unwind objects -- one per function, populated lazily -- for the ObjectFile. 25 // Each Unwind object has multiple UnwindPlans for different scenarios. 26 27 using namespace lldb; 28 using namespace lldb_private; 29 30 UnwindTable::UnwindTable (ObjectFile& objfile) : 31 m_object_file (objfile), 32 m_unwinds (), 33 m_initialized (false), 34 m_mutex (), 35 m_eh_frame_up (), 36 m_compact_unwind_up (), 37 m_arm_unwind_up () 38 { 39 } 40 41 // We can't do some of this initialization when the ObjectFile is running its ctor; delay doing it 42 // until needed for something. 43 44 void 45 UnwindTable::Initialize () 46 { 47 if (m_initialized) 48 return; 49 50 Mutex::Locker locker(m_mutex); 51 52 if (m_initialized) // check again once we've acquired the lock 53 return; 54 55 SectionList* sl = m_object_file.GetSectionList (); 56 if (sl) 57 { 58 SectionSP sect = sl->FindSectionByType (eSectionTypeEHFrame, true); 59 if (sect.get()) 60 { 61 m_eh_frame_up.reset(new DWARFCallFrameInfo(m_object_file, sect, eRegisterKindEHFrame, true)); 62 } 63 sect = sl->FindSectionByType (eSectionTypeCompactUnwind, true); 64 if (sect.get()) 65 { 66 m_compact_unwind_up.reset(new CompactUnwindInfo(m_object_file, sect)); 67 } 68 sect = sl->FindSectionByType (eSectionTypeARMexidx, true); 69 if (sect.get()) 70 { 71 SectionSP sect_extab = sl->FindSectionByType (eSectionTypeARMextab, true); 72 if (sect_extab.get()) 73 { 74 m_arm_unwind_up.reset(new ArmUnwindInfo(m_object_file, sect, sect_extab)); 75 } 76 } 77 } 78 79 m_initialized = true; 80 } 81 82 UnwindTable::~UnwindTable () 83 { 84 } 85 86 FuncUnwindersSP 87 UnwindTable::GetFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc) 88 { 89 FuncUnwindersSP no_unwind_found; 90 91 Initialize(); 92 93 Mutex::Locker locker(m_mutex); 94 95 // There is an UnwindTable per object file, so we can safely use file handles 96 addr_t file_addr = addr.GetFileAddress(); 97 iterator end = m_unwinds.end (); 98 iterator insert_pos = end; 99 if (!m_unwinds.empty()) 100 { 101 insert_pos = m_unwinds.lower_bound (file_addr); 102 iterator pos = insert_pos; 103 if ((pos == m_unwinds.end ()) || (pos != m_unwinds.begin() && pos->second->GetFunctionStartAddress() != addr)) 104 --pos; 105 106 if (pos->second->ContainsAddress (addr)) 107 return pos->second; 108 } 109 110 AddressRange range; 111 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid()) 112 { 113 // Does the eh_frame unwind info has a function bounds for this addr? 114 if (m_eh_frame_up == nullptr || !m_eh_frame_up->GetAddressRange (addr, range)) 115 { 116 return no_unwind_found; 117 } 118 } 119 120 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range)); 121 m_unwinds.insert (insert_pos, std::make_pair(range.GetBaseAddress().GetFileAddress(), func_unwinder_sp)); 122 // StreamFile s(stdout, false); 123 // Dump (s); 124 return func_unwinder_sp; 125 } 126 127 // Ignore any existing FuncUnwinders for this function, create a new one and don't add it to the 128 // UnwindTable. This is intended for use by target modules show-unwind where we want to create 129 // new UnwindPlans, not re-use existing ones. 130 131 FuncUnwindersSP 132 UnwindTable::GetUncachedFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc) 133 { 134 FuncUnwindersSP no_unwind_found; 135 Initialize(); 136 137 AddressRange range; 138 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid()) 139 { 140 // Does the eh_frame unwind info has a function bounds for this addr? 141 if (m_eh_frame_up == nullptr || !m_eh_frame_up->GetAddressRange (addr, range)) 142 { 143 return no_unwind_found; 144 } 145 } 146 147 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range)); 148 return func_unwinder_sp; 149 } 150 151 152 void 153 UnwindTable::Dump (Stream &s) 154 { 155 Mutex::Locker locker(m_mutex); 156 s.Printf("UnwindTable for '%s':\n", 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 { 161 s.Printf ("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance (begin, pos), pos->first); 162 } 163 s.EOL(); 164 } 165 166 DWARFCallFrameInfo * 167 UnwindTable::GetEHFrameInfo () 168 { 169 Initialize(); 170 return m_eh_frame_up.get(); 171 } 172 173 CompactUnwindInfo * 174 UnwindTable::GetCompactUnwindInfo () 175 { 176 Initialize(); 177 return m_compact_unwind_up.get(); 178 } 179 180 ArmUnwindInfo * 181 UnwindTable::GetArmUnwindInfo () 182 { 183 Initialize(); 184 return m_arm_unwind_up.get(); 185 } 186 187 bool 188 UnwindTable::GetArchitecture (lldb_private::ArchSpec &arch) 189 { 190 return m_object_file.GetArchitecture (arch); 191 } 192