| 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 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| Greg Clayton | a51ed9b | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 9 | #include "lldb/Symbol/UnwindTable.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 10 | |
| Greg Clayton | a51ed9b | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | |
| Greg Clayton | a51ed9b | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Module.h" |
| 14 | #include "lldb/Core/Section.h" |
| Tamas Berghammer | 648f3c7 | 2015-09-30 13:50:14 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/ArmUnwindInfo.h" |
| Jason Molenda | e589e7e | 2014-12-08 03:09:00 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/CompactUnwindInfo.h" |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
| 18 | #include "lldb/Symbol/FuncUnwinders.h" |
| 19 | #include "lldb/Symbol/ObjectFile.h" |
| 20 | #include "lldb/Symbol/SymbolContext.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 21 | |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 22 | // There is one UnwindTable object per ObjectFile. It contains a list of Unwind |
| 23 | // objects -- one per function, populated lazily -- for the ObjectFile. Each |
| 24 | // Unwind object has multiple UnwindPlans for different scenarios. |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
| Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 29 | UnwindTable::UnwindTable(Module &module) |
| 30 | : m_module(module), m_unwinds(), m_initialized(false), m_mutex(), |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | m_eh_frame_up(), m_compact_unwind_up(), m_arm_unwind_up() {} |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 32 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | // 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] | 34 | // ctor; delay doing it until needed for something. |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 35 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | void UnwindTable::Initialize() { |
| 37 | if (m_initialized) |
| 38 | return; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 39 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | std::lock_guard<std::mutex> guard(m_mutex); |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame] | 41 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | if (m_initialized) // check again once we've acquired the lock |
| 43 | return; |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 44 | m_initialized = true; |
| Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 45 | ObjectFile *object_file = m_module.GetObjectFile(); |
| 46 | if (!object_file) |
| 47 | return; |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame] | 48 | |
| Pavel Labath | dec9639 | 2019-03-18 10:45:02 +0000 | [diff] [blame] | 49 | SectionList *sl = m_module.GetSectionList(); |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 50 | if (!sl) |
| 51 | return; |
| 52 | |
| 53 | SectionSP sect = sl->FindSectionByType(eSectionTypeEHFrame, true); |
| 54 | if (sect.get()) { |
| Pavel Labath | 3f2a081 | 2017-06-28 09:09:19 +0000 | [diff] [blame] | 55 | m_eh_frame_up.reset( |
| Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 56 | new DWARFCallFrameInfo(*object_file, sect, DWARFCallFrameInfo::EH)); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 59 | sect = sl->FindSectionByType(eSectionTypeDWARFDebugFrame, true); |
| 60 | if (sect) { |
| 61 | m_debug_frame_up.reset( |
| Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 62 | new DWARFCallFrameInfo(*object_file, sect, DWARFCallFrameInfo::DWARF)); |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | sect = sl->FindSectionByType(eSectionTypeCompactUnwind, true); |
| 66 | if (sect) { |
| Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 67 | m_compact_unwind_up.reset(new CompactUnwindInfo(*object_file, sect)); |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | sect = sl->FindSectionByType(eSectionTypeARMexidx, true); |
| 71 | if (sect) { |
| 72 | SectionSP sect_extab = sl->FindSectionByType(eSectionTypeARMextab, true); |
| 73 | if (sect_extab.get()) { |
| Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 74 | m_arm_unwind_up.reset(new ArmUnwindInfo(*object_file, sect, sect_extab)); |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | UnwindTable::~UnwindTable() {} |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 80 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 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 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 102 | FuncUnwindersSP |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | UnwindTable::GetFuncUnwindersContainingAddress(const Address &addr, |
| 104 | SymbolContext &sc) { |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | Initialize(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 106 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | std::lock_guard<std::mutex> guard(m_mutex); |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame] | 108 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 120 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | if (pos->second->ContainsAddress(addr)) |
| 122 | return pos->second; |
| 123 | } |
| 124 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 125 | auto range_or = GetAddressRange(addr, sc); |
| 126 | if (!range_or) |
| 127 | return nullptr; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 128 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 129 | FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, *range_or)); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | m_unwinds.insert(insert_pos, |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 131 | std::make_pair(range_or->GetBaseAddress().GetFileAddress(), |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | func_unwinder_sp)); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | return func_unwinder_sp; |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | // Ignore any existing FuncUnwinders for this function, create a new one and |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 137 | // don't add it to the UnwindTable. This is intended for use by target modules |
| 138 | // show-unwind where we want to create new UnwindPlans, not re-use existing |
| 139 | // ones. |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 140 | FuncUnwindersSP |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | UnwindTable::GetUncachedFuncUnwindersContainingAddress(const Address &addr, |
| 142 | SymbolContext &sc) { |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | Initialize(); |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 144 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 145 | auto range_or = GetAddressRange(addr, sc); |
| 146 | if (!range_or) |
| 147 | return nullptr; |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 148 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 149 | return std::make_shared<FuncUnwinders>(*this, *range_or); |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | void UnwindTable::Dump(Stream &s) { |
| 153 | std::lock_guard<std::mutex> guard(m_mutex); |
| Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 154 | s.Format("UnwindTable for '{0}':\n", m_module.GetFileSpec()); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | const_iterator begin = m_unwinds.begin(); |
| 156 | const_iterator end = m_unwinds.end(); |
| 157 | for (const_iterator pos = begin; pos != end; ++pos) { |
| 158 | s.Printf("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance(begin, pos), |
| 159 | pos->first); |
| 160 | } |
| 161 | s.EOL(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | DWARFCallFrameInfo *UnwindTable::GetEHFrameInfo() { |
| 165 | Initialize(); |
| 166 | return m_eh_frame_up.get(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 167 | } |
| Jason Molenda | ab35aa9 | 2014-05-23 01:48:10 +0000 | [diff] [blame] | 168 | |
| Pavel Labath | cdda23e | 2017-06-27 11:16:26 +0000 | [diff] [blame] | 169 | DWARFCallFrameInfo *UnwindTable::GetDebugFrameInfo() { |
| 170 | Initialize(); |
| 171 | return m_debug_frame_up.get(); |
| 172 | } |
| 173 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | CompactUnwindInfo *UnwindTable::GetCompactUnwindInfo() { |
| 175 | Initialize(); |
| 176 | return m_compact_unwind_up.get(); |
| Tamas Berghammer | 648f3c7 | 2015-09-30 13:50:14 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | ArmUnwindInfo *UnwindTable::GetArmUnwindInfo() { |
| 180 | Initialize(); |
| 181 | return m_arm_unwind_up.get(); |
| Jason Molenda | e589e7e | 2014-12-08 03:09:00 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 184 | ArchSpec UnwindTable::GetArchitecture() { return m_module.GetArchitecture(); } |
| Jason Molenda | 955dcf2 | 2016-05-04 03:09:40 +0000 | [diff] [blame] | 185 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | bool UnwindTable::GetAllowAssemblyEmulationUnwindPlans() { |
| Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 187 | if (ObjectFile *object_file = m_module.GetObjectFile()) |
| 188 | return object_file->AllowAssemblyEmulationUnwindPlans(); |
| 189 | return false; |
| Jason Molenda | 955dcf2 | 2016-05-04 03:09:40 +0000 | [diff] [blame] | 190 | } |