Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 1 | //===-- SectionLoadList.cpp -------------------------------------*- C++ -*-===// |
| 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 |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Target/SectionLoadList.h" |
| 10 | |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Module.h" |
| 12 | #include "lldb/Core/Section.h" |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 13 | #include "lldb/Symbol/Block.h" |
| 14 | #include "lldb/Symbol/Symbol.h" |
| 15 | #include "lldb/Symbol/SymbolContext.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/Stream.h" |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | SectionLoadList::SectionLoadList(const SectionLoadList &rhs) |
| 23 | : m_addr_to_sect(), m_sect_to_addr(), m_mutex() { |
| 24 | std::lock_guard<std::recursive_mutex> guard(rhs.m_mutex); |
| 25 | m_addr_to_sect = rhs.m_addr_to_sect; |
| 26 | m_sect_to_addr = rhs.m_sect_to_addr; |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | void SectionLoadList::operator=(const SectionLoadList &rhs) { |
Jim Ingham | cdd4892 | 2019-03-29 17:07:30 +0000 | [diff] [blame] | 30 | std::lock(m_mutex, rhs.m_mutex); |
| 31 | std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex, std::adopt_lock); |
| 32 | std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex, std::adopt_lock); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | m_addr_to_sect = rhs.m_addr_to_sect; |
| 34 | m_sect_to_addr = rhs.m_sect_to_addr; |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | bool SectionLoadList::IsEmpty() const { |
| 38 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 39 | return m_addr_to_sect.empty(); |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | void SectionLoadList::Clear() { |
| 43 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 44 | m_addr_to_sect.clear(); |
| 45 | m_sect_to_addr.clear(); |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | addr_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | SectionLoadList::GetSectionLoadAddress(const lldb::SectionSP §ion) const { |
| 50 | // TODO: add support for the same section having multiple load addresses |
| 51 | addr_t section_load_addr = LLDB_INVALID_ADDRESS; |
| 52 | if (section) { |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 53 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | sect_to_addr_collection::const_iterator pos = |
| 55 | m_sect_to_addr.find(section.get()); |
| 56 | |
| 57 | if (pos != m_sect_to_addr.end()) |
| 58 | section_load_addr = pos->second; |
| 59 | } |
| 60 | return section_load_addr; |
| 61 | } |
| 62 | |
| 63 | bool SectionLoadList::SetSectionLoadAddress(const lldb::SectionSP §ion, |
| 64 | addr_t load_addr, |
| 65 | bool warn_multiple) { |
Pavel Labath | 3b7e198 | 2017-02-05 00:44:54 +0000 | [diff] [blame] | 66 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | ModuleSP module_sp(section->GetModule()); |
| 68 | |
| 69 | if (module_sp) { |
Pavel Labath | 3b7e198 | 2017-02-05 00:44:54 +0000 | [diff] [blame] | 70 | LLDB_LOGV(log, "(section = {0} ({1}.{2}), load_addr = {3:x}) module = {4}", |
| 71 | section.get(), module_sp->GetFileSpec(), section->GetName(), |
| 72 | load_addr, module_sp.get()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | if (section->GetByteSize() == 0) |
| 75 | return false; // No change |
| 76 | |
| 77 | // Fill in the section -> load_addr map |
| 78 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 79 | sect_to_addr_collection::iterator sta_pos = |
| 80 | m_sect_to_addr.find(section.get()); |
| 81 | if (sta_pos != m_sect_to_addr.end()) { |
| 82 | if (load_addr == sta_pos->second) |
| 83 | return false; // No change... |
| 84 | else |
| 85 | sta_pos->second = load_addr; |
| 86 | } else |
| 87 | m_sect_to_addr[section.get()] = load_addr; |
| 88 | |
| 89 | // Fill in the load_addr -> section map |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 90 | addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | if (ats_pos != m_addr_to_sect.end()) { |
| 92 | // Some sections are ok to overlap, and for others we should warn. When |
| 93 | // we have multiple load addresses that correspond to a section, we will |
| 94 | // always attribute the section to the be last section that claims it |
| 95 | // exists at that address. Sometimes it is ok for more that one section |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 96 | // to be loaded at a specific load address, and other times it isn't. The |
| 97 | // "warn_multiple" parameter tells us if we should warn in this case or |
| 98 | // not. The DynamicLoader plug-in subclasses should know which sections |
| 99 | // should warn and which shouldn't (darwin shared cache modules all |
| 100 | // shared the same "__LINKEDIT" sections, so the dynamic loader can pass |
| 101 | // false for "warn_multiple"). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | if (warn_multiple && section != ats_pos->second) { |
| 103 | ModuleSP module_sp(section->GetModule()); |
| 104 | if (module_sp) { |
| 105 | ModuleSP curr_module_sp(ats_pos->second->GetModule()); |
| 106 | if (curr_module_sp) { |
| 107 | module_sp->ReportWarning( |
| 108 | "address 0x%16.16" PRIx64 |
| 109 | " maps to more than one section: %s.%s and %s.%s", |
| 110 | load_addr, module_sp->GetFileSpec().GetFilename().GetCString(), |
| 111 | section->GetName().GetCString(), |
| 112 | curr_module_sp->GetFileSpec().GetFilename().GetCString(), |
| 113 | ats_pos->second->GetName().GetCString()); |
| 114 | } |
Greg Clayton | c5e9a7d | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 115 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | } |
| 117 | ats_pos->second = section; |
| 118 | } else |
| 119 | m_addr_to_sect[load_addr] = section; |
| 120 | return true; // Changed |
| 121 | |
| 122 | } else { |
| 123 | if (log) { |
| 124 | log->Printf( |
| 125 | "SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64 |
| 126 | ") error: module has been deleted", |
| 127 | __FUNCTION__, static_cast<void *>(section.get()), |
| 128 | section->GetName().AsCString(), load_addr); |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 129 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | } |
| 131 | return false; |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 132 | } |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | size_t SectionLoadList::SetSectionUnloaded(const lldb::SectionSP §ion_sp) { |
| 135 | size_t unload_count = 0; |
| 136 | |
| 137 | if (section_sp) { |
Pavel Labath | 3b7e198 | 2017-02-05 00:44:54 +0000 | [diff] [blame] | 138 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | |
Pavel Labath | 3b7e198 | 2017-02-05 00:44:54 +0000 | [diff] [blame] | 140 | if (log && log->GetVerbose()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | ModuleSP module_sp = section_sp->GetModule(); |
| 142 | std::string module_name("<Unknown>"); |
| 143 | if (module_sp) { |
| 144 | const FileSpec &module_file_spec( |
| 145 | section_sp->GetModule()->GetFileSpec()); |
| 146 | module_name = module_file_spec.GetPath(); |
| 147 | } |
| 148 | log->Printf("SectionLoadList::%s (section = %p (%s.%s))", __FUNCTION__, |
| 149 | static_cast<void *>(section_sp.get()), module_name.c_str(), |
| 150 | section_sp->GetName().AsCString()); |
| 151 | } |
| 152 | |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 153 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | |
| 155 | sect_to_addr_collection::iterator sta_pos = |
| 156 | m_sect_to_addr.find(section_sp.get()); |
| 157 | if (sta_pos != m_sect_to_addr.end()) { |
| 158 | ++unload_count; |
| 159 | addr_t load_addr = sta_pos->second; |
| 160 | m_sect_to_addr.erase(sta_pos); |
| 161 | |
| 162 | addr_to_sect_collection::iterator ats_pos = |
| 163 | m_addr_to_sect.find(load_addr); |
| 164 | if (ats_pos != m_addr_to_sect.end()) |
| 165 | m_addr_to_sect.erase(ats_pos); |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 166 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | } |
| 168 | return unload_count; |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | bool SectionLoadList::SetSectionUnloaded(const lldb::SectionSP §ion_sp, |
| 172 | addr_t load_addr) { |
Pavel Labath | 3b7e198 | 2017-02-05 00:44:54 +0000 | [diff] [blame] | 173 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 174 | |
Pavel Labath | 3b7e198 | 2017-02-05 00:44:54 +0000 | [diff] [blame] | 175 | if (log && log->GetVerbose()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 176 | ModuleSP module_sp = section_sp->GetModule(); |
| 177 | std::string module_name("<Unknown>"); |
| 178 | if (module_sp) { |
| 179 | const FileSpec &module_file_spec(section_sp->GetModule()->GetFileSpec()); |
| 180 | module_name = module_file_spec.GetPath(); |
| 181 | } |
| 182 | log->Printf( |
| 183 | "SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 |
| 184 | ")", |
| 185 | __FUNCTION__, static_cast<void *>(section_sp.get()), |
| 186 | module_name.c_str(), section_sp->GetName().AsCString(), load_addr); |
| 187 | } |
| 188 | bool erased = false; |
| 189 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 190 | sect_to_addr_collection::iterator sta_pos = |
| 191 | m_sect_to_addr.find(section_sp.get()); |
| 192 | if (sta_pos != m_sect_to_addr.end()) { |
| 193 | erased = true; |
| 194 | m_sect_to_addr.erase(sta_pos); |
| 195 | } |
| 196 | |
| 197 | addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); |
| 198 | if (ats_pos != m_addr_to_sect.end()) { |
| 199 | erased = true; |
| 200 | m_addr_to_sect.erase(ats_pos); |
| 201 | } |
| 202 | |
| 203 | return erased; |
| 204 | } |
| 205 | |
Pavel Labath | c3c7212 | 2017-06-08 13:26:35 +0000 | [diff] [blame] | 206 | bool SectionLoadList::ResolveLoadAddress(addr_t load_addr, Address &so_addr, |
| 207 | bool allow_section_end) const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | // First find the top level section that this load address exists in |
| 209 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 210 | if (!m_addr_to_sect.empty()) { |
| 211 | addr_to_sect_collection::const_iterator pos = |
| 212 | m_addr_to_sect.lower_bound(load_addr); |
| 213 | if (pos != m_addr_to_sect.end()) { |
| 214 | if (load_addr != pos->first && pos != m_addr_to_sect.begin()) |
| 215 | --pos; |
| 216 | const addr_t pos_load_addr = pos->first; |
| 217 | if (load_addr >= pos_load_addr) { |
| 218 | addr_t offset = load_addr - pos_load_addr; |
Pavel Labath | c3c7212 | 2017-06-08 13:26:35 +0000 | [diff] [blame] | 219 | if (offset < pos->second->GetByteSize() + (allow_section_end ? 1 : 0)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | // We have found the top level section, now we need to find the |
| 221 | // deepest child section. |
Pavel Labath | c3c7212 | 2017-06-08 13:26:35 +0000 | [diff] [blame] | 222 | return pos->second->ResolveContainedAddress(offset, so_addr, |
| 223 | allow_section_end); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 227 | // There are no entries that have an address that is >= load_addr, so we |
| 228 | // need to check the last entry on our collection. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | addr_to_sect_collection::const_reverse_iterator rpos = |
| 230 | m_addr_to_sect.rbegin(); |
| 231 | if (load_addr >= rpos->first) { |
| 232 | addr_t offset = load_addr - rpos->first; |
Pavel Labath | c3c7212 | 2017-06-08 13:26:35 +0000 | [diff] [blame] | 233 | if (offset < |
| 234 | rpos->second->GetByteSize() + (allow_section_end ? 1 : 0)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | // We have found the top level section, now we need to find the |
| 236 | // deepest child section. |
Pavel Labath | c3c7212 | 2017-06-08 13:26:35 +0000 | [diff] [blame] | 237 | return rpos->second->ResolveContainedAddress(offset, so_addr, |
| 238 | allow_section_end); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | so_addr.Clear(); |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | void SectionLoadList::Dump(Stream &s, Target *target) { |
| 248 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 249 | addr_to_sect_collection::const_iterator pos, end; |
| 250 | for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; |
| 251 | ++pos) { |
| 252 | s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, |
| 253 | static_cast<void *>(pos->second.get())); |
| 254 | pos->second->Dump(&s, target, 0); |
| 255 | } |
| 256 | } |