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