| Greg Clayton | dc5eb69 | 2011-04-25 18:36:36 +0000 | [diff] [blame] | 1 | //===-- UnwindTable.cpp -----------------------------------------*- C++ -*-===// |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 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 | |
| Greg Clayton | a51ed9b | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 10 | #include "lldb/Symbol/UnwindTable.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 11 | |
| Greg Clayton | a51ed9b | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | |
| Greg Clayton | a51ed9b | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/Section.h" |
| Tamas Berghammer | 648f3c7 | 2015-09-30 13:50:14 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/ArmUnwindInfo.h" |
| Jason Molenda | e589e7e | 2014-12-08 03:09:00 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/CompactUnwindInfo.h" |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
| 19 | #include "lldb/Symbol/FuncUnwinders.h" |
| 20 | #include "lldb/Symbol/ObjectFile.h" |
| 21 | #include "lldb/Symbol/SymbolContext.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 22 | |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 23 | // There is one UnwindTable object per ObjectFile. It contains a list of Unwind |
| 24 | // objects -- one per function, populated lazily -- for the ObjectFile. Each |
| 25 | // Unwind object has multiple UnwindPlans for different scenarios. |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace lldb; |
| 28 | using namespace lldb_private; |
| 29 | |
| Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 30 | UnwindTable::UnwindTable(ObjectFile &objfile) |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | : m_object_file(objfile), m_unwinds(), m_initialized(false), m_mutex(), |
| 32 | m_eh_frame_up(), m_compact_unwind_up(), m_arm_unwind_up() {} |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 33 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | // We can't do some of this initialization when the ObjectFile is running its |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 35 | // ctor; delay doing it until needed for something. |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 36 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | void UnwindTable::Initialize() { |
| 38 | if (m_initialized) |
| 39 | return; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 40 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | std::lock_guard<std::mutex> guard(m_mutex); |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame] | 42 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | if (m_initialized) // check again once we've acquired the lock |
| 44 | return; |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 45 | m_initialized = true; |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame] | 46 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | SectionList *sl = m_object_file.GetSectionList(); |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 48 | if (!sl) |
| 49 | return; |
| 50 | |
| 51 | SectionSP sect = sl->FindSectionByType(eSectionTypeEHFrame, true); |
| 52 | if (sect.get()) { |
| Pavel Labath | 3f2a081 | 2017-06-28 09:09:19 +0000 | [diff] [blame] | 53 | m_eh_frame_up.reset( |
| 54 | new DWARFCallFrameInfo(m_object_file, sect, DWARFCallFrameInfo::EH)); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 57 | sect = sl->FindSectionByType(eSectionTypeDWARFDebugFrame, true); |
| 58 | if (sect) { |
| 59 | m_debug_frame_up.reset( |
| Pavel Labath | 3f2a081 | 2017-06-28 09:09:19 +0000 | [diff] [blame] | 60 | new DWARFCallFrameInfo(m_object_file, sect, DWARFCallFrameInfo::DWARF)); |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | sect = sl->FindSectionByType(eSectionTypeCompactUnwind, true); |
| 64 | if (sect) { |
| 65 | m_compact_unwind_up.reset(new CompactUnwindInfo(m_object_file, sect)); |
| 66 | } |
| 67 | |
| 68 | sect = sl->FindSectionByType(eSectionTypeARMexidx, true); |
| 69 | if (sect) { |
| 70 | SectionSP sect_extab = sl->FindSectionByType(eSectionTypeARMextab, true); |
| 71 | if (sect_extab.get()) { |
| 72 | m_arm_unwind_up.reset(new ArmUnwindInfo(m_object_file, sect, sect_extab)); |
| 73 | } |
| 74 | } |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | UnwindTable::~UnwindTable() {} |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 78 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 79 | llvm::Optional<AddressRange> UnwindTable::GetAddressRange(const Address &addr, |
| 80 | SymbolContext &sc) { |
| 81 | AddressRange range; |
| 82 | |
| 83 | // First check the symbol context |
| 84 | if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, |
| 85 | false, range) && |
| 86 | range.GetBaseAddress().IsValid()) |
| 87 | return range; |
| 88 | |
| 89 | // Does the eh_frame unwind info has a function bounds for this addr? |
| 90 | if (m_eh_frame_up && m_eh_frame_up->GetAddressRange(addr, range)) |
| 91 | return range; |
| 92 | |
| 93 | // Try debug_frame as well |
| 94 | if (m_debug_frame_up && m_debug_frame_up->GetAddressRange(addr, range)) |
| 95 | return range; |
| 96 | |
| 97 | return llvm::None; |
| 98 | } |
| 99 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 100 | FuncUnwindersSP |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | UnwindTable::GetFuncUnwindersContainingAddress(const Address &addr, |
| 102 | SymbolContext &sc) { |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | Initialize(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 104 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | std::lock_guard<std::mutex> guard(m_mutex); |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame] | 106 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | // There is an UnwindTable per object file, so we can safely use file handles |
| 108 | addr_t file_addr = addr.GetFileAddress(); |
| 109 | iterator end = m_unwinds.end(); |
| 110 | iterator insert_pos = end; |
| 111 | if (!m_unwinds.empty()) { |
| 112 | insert_pos = m_unwinds.lower_bound(file_addr); |
| 113 | iterator pos = insert_pos; |
| 114 | if ((pos == m_unwinds.end()) || |
| 115 | (pos != m_unwinds.begin() && |
| 116 | pos->second->GetFunctionStartAddress() != addr)) |
| 117 | --pos; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 118 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | if (pos->second->ContainsAddress(addr)) |
| 120 | return pos->second; |
| 121 | } |
| 122 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 123 | auto range_or = GetAddressRange(addr, sc); |
| 124 | if (!range_or) |
| 125 | return nullptr; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 126 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 127 | FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, *range_or)); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | m_unwinds.insert(insert_pos, |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 129 | std::make_pair(range_or->GetBaseAddress().GetFileAddress(), |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | func_unwinder_sp)); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | return func_unwinder_sp; |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | // Ignore any existing FuncUnwinders for this function, create a new one and |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 135 | // don't add it to the UnwindTable. This is intended for use by target modules |
| 136 | // show-unwind where we want to create new UnwindPlans, not re-use existing |
| 137 | // ones. |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 138 | FuncUnwindersSP |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | UnwindTable::GetUncachedFuncUnwindersContainingAddress(const Address &addr, |
| 140 | SymbolContext &sc) { |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | Initialize(); |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 142 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 143 | auto range_or = GetAddressRange(addr, sc); |
| 144 | if (!range_or) |
| 145 | return nullptr; |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 146 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 147 | return std::make_shared<FuncUnwinders>(*this, *range_or); |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | void UnwindTable::Dump(Stream &s) { |
| 151 | std::lock_guard<std::mutex> guard(m_mutex); |
| 152 | s.Printf("UnwindTable for '%s':\n", |
| 153 | m_object_file.GetFileSpec().GetPath().c_str()); |
| 154 | const_iterator begin = m_unwinds.begin(); |
| 155 | const_iterator end = m_unwinds.end(); |
| 156 | for (const_iterator pos = begin; pos != end; ++pos) { |
| 157 | s.Printf("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance(begin, pos), |
| 158 | pos->first); |
| 159 | } |
| 160 | s.EOL(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | DWARFCallFrameInfo *UnwindTable::GetEHFrameInfo() { |
| 164 | Initialize(); |
| 165 | return m_eh_frame_up.get(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 166 | } |
| Jason Molenda | ab35aa9 | 2014-05-23 01:48:10 +0000 | [diff] [blame] | 167 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 168 | DWARFCallFrameInfo *UnwindTable::GetDebugFrameInfo() { |
| 169 | Initialize(); |
| 170 | return m_debug_frame_up.get(); |
| 171 | } |
| 172 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | CompactUnwindInfo *UnwindTable::GetCompactUnwindInfo() { |
| 174 | Initialize(); |
| 175 | return m_compact_unwind_up.get(); |
| Tamas Berghammer | 648f3c7 | 2015-09-30 13:50:14 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | ArmUnwindInfo *UnwindTable::GetArmUnwindInfo() { |
| 179 | Initialize(); |
| 180 | return m_arm_unwind_up.get(); |
| Jason Molenda | e589e7e | 2014-12-08 03:09:00 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | bool UnwindTable::GetArchitecture(lldb_private::ArchSpec &arch) { |
| 184 | return m_object_file.GetArchitecture(arch); |
| Jason Molenda | ab35aa9 | 2014-05-23 01:48:10 +0000 | [diff] [blame] | 185 | } |
| Jason Molenda | 955dcf2 | 2016-05-04 03:09:40 +0000 | [diff] [blame] | 186 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | bool UnwindTable::GetAllowAssemblyEmulationUnwindPlans() { |
| 188 | return m_object_file.AllowAssemblyEmulationUnwindPlans(); |
| Jason Molenda | 955dcf2 | 2016-05-04 03:09:40 +0000 | [diff] [blame] | 189 | } |