Greg Clayton | 49480b1 | 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 | |
| 28 | bool |
| 29 | SectionLoadList::IsEmpty() const |
| 30 | { |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 31 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 32 | return m_addr_to_sect.empty(); |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void |
| 36 | SectionLoadList::Clear () |
| 37 | { |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 38 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 39 | m_addr_to_sect.clear(); |
| 40 | m_sect_to_addr.clear(); |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | addr_t |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 44 | SectionLoadList::GetSectionLoadAddress (const lldb::SectionSP §ion) const |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 45 | { |
| 46 | // TODO: add support for the same section having multiple load addresses |
| 47 | addr_t section_load_addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 48 | if (section) |
| 49 | { |
| 50 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 51 | sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section.get()); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 52 | |
| 53 | if (pos != m_sect_to_addr.end()) |
| 54 | section_load_addr = pos->second; |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 55 | } |
| 56 | return section_load_addr; |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 60 | SectionLoadList::SetSectionLoadAddress (const lldb::SectionSP §ion, addr_t load_addr, bool warn_multiple) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 61 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 62 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 63 | |
| 64 | if (log) |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 65 | { |
| 66 | const FileSpec &module_file_spec (section->GetModule()->GetFileSpec()); |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 67 | log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16" PRIx64 ")", |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 68 | __FUNCTION__, |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 69 | section.get(), |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 70 | module_file_spec.GetDirectory().AsCString(), |
| 71 | module_file_spec.GetDirectory() ? "/" : "", |
| 72 | module_file_spec.GetFilename().AsCString(), |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 73 | section->GetName().AsCString(), |
| 74 | load_addr); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 75 | } |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 76 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 77 | if (section->GetByteSize() == 0) |
| 78 | return false; // No change |
| 79 | |
Greg Clayton | a414e67 | 2012-04-16 21:01:30 +0000 | [diff] [blame] | 80 | // Fill in the section -> load_addr map |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 81 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 82 | sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get()); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 83 | if (sta_pos != m_sect_to_addr.end()) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 84 | { |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 85 | if (load_addr == sta_pos->second) |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 86 | return false; // No change... |
| 87 | else |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 88 | sta_pos->second = load_addr; |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 89 | } |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 90 | else |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 91 | m_sect_to_addr[section.get()] = load_addr; |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 92 | |
Greg Clayton | a414e67 | 2012-04-16 21:01:30 +0000 | [diff] [blame] | 93 | // Fill in the load_addr -> section map |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 94 | addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); |
| 95 | if (ats_pos != m_addr_to_sect.end()) |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 96 | { |
Greg Clayton | a414e67 | 2012-04-16 21:01:30 +0000 | [diff] [blame] | 97 | // Some sections are ok to overlap, and for others we should warn. When |
| 98 | // we have multiple load addresses that correspond to a section, we will |
| 99 | // allways attribute the section to the be last section that claims it |
| 100 | // exists at that address. Sometimes it is ok for more that one section |
| 101 | // to be loaded at a specific load address, and other times it isn't. |
| 102 | // The "warn_multiple" parameter tells us if we should warn in this case |
| 103 | // or not. The DynamicLoader plug-in subclasses should know which |
| 104 | // sections should warn and which shouldn't (darwin shared cache modules |
| 105 | // all shared the same "__LINKEDIT" sections, so the dynamic loader can |
| 106 | // pass false for "warn_multiple"). |
| 107 | if (warn_multiple && section != ats_pos->second) |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 108 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 109 | ModuleSP module_sp (section->GetModule()); |
| 110 | if (module_sp) |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 111 | { |
Greg Clayton | 40a3976 | 2012-07-31 00:31:32 +0000 | [diff] [blame] | 112 | ModuleSP curr_module_sp (ats_pos->second->GetModule()); |
| 113 | if (curr_module_sp) |
| 114 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 115 | module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s", |
Greg Clayton | 40a3976 | 2012-07-31 00:31:32 +0000 | [diff] [blame] | 116 | load_addr, |
| 117 | module_sp->GetFileSpec().GetFilename().GetCString(), |
| 118 | section->GetName().GetCString(), |
| 119 | curr_module_sp->GetFileSpec().GetFilename().GetCString(), |
| 120 | ats_pos->second->GetName().GetCString()); |
| 121 | } |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 124 | ats_pos->second = section; |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 125 | } |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 126 | else |
| 127 | m_addr_to_sect[load_addr] = section; |
| 128 | |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 129 | return true; // Changed |
| 130 | } |
| 131 | |
| 132 | size_t |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 133 | SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 134 | { |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 135 | size_t unload_count = 0; |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 136 | |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 137 | if (section_sp) |
| 138 | { |
| 139 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); |
| 140 | |
| 141 | if (log) |
| 142 | { |
| 143 | const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); |
| 144 | log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))", |
| 145 | __FUNCTION__, |
| 146 | section_sp.get(), |
| 147 | module_file_spec.GetDirectory().AsCString(), |
| 148 | module_file_spec.GetDirectory() ? "/" : "", |
| 149 | module_file_spec.GetFilename().AsCString(), |
| 150 | section_sp->GetName().AsCString()); |
| 151 | } |
| 152 | |
| 153 | Mutex::Locker locker(m_mutex); |
| 154 | |
| 155 | sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); |
| 156 | if (sta_pos != m_sect_to_addr.end()) |
| 157 | { |
| 158 | addr_t load_addr = sta_pos->second; |
| 159 | m_sect_to_addr.erase (sta_pos); |
| 160 | |
| 161 | addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); |
| 162 | if (ats_pos != m_addr_to_sect.end()) |
| 163 | m_addr_to_sect.erase (ats_pos); |
| 164 | } |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 165 | } |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 166 | return unload_count; |
| 167 | } |
| 168 | |
| 169 | bool |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 170 | SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp, addr_t load_addr) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 171 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 172 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 173 | |
| 174 | if (log) |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 175 | { |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 176 | const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 177 | log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16" PRIx64 ")", |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 178 | __FUNCTION__, |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 179 | section_sp.get(), |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 180 | module_file_spec.GetDirectory().AsCString(), |
| 181 | module_file_spec.GetDirectory() ? "/" : "", |
| 182 | module_file_spec.GetFilename().AsCString(), |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 183 | section_sp->GetName().AsCString(), |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 184 | load_addr); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 185 | } |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 186 | bool erased = false; |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 187 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 545762f | 2012-07-07 01:24:12 +0000 | [diff] [blame] | 188 | sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 189 | if (sta_pos != m_sect_to_addr.end()) |
| 190 | { |
| 191 | erased = true; |
| 192 | m_sect_to_addr.erase (sta_pos); |
| 193 | } |
| 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 | { |
| 198 | erased = true; |
| 199 | m_addr_to_sect.erase (ats_pos); |
| 200 | } |
| 201 | |
| 202 | return erased; |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | bool |
| 207 | SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const |
| 208 | { |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 209 | // First find the top level section that this load address exists in |
| 210 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 211 | if (!m_addr_to_sect.empty()) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 212 | { |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 213 | addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr); |
| 214 | if (pos != m_addr_to_sect.end()) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 215 | { |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 216 | if (load_addr != pos->first && pos != m_addr_to_sect.begin()) |
| 217 | --pos; |
| 218 | if (load_addr >= pos->first) |
Jason Molenda | d4c5cd5 | 2010-12-22 02:02:45 +0000 | [diff] [blame] | 219 | { |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 220 | addr_t offset = load_addr - pos->first; |
| 221 | if (offset < pos->second->GetByteSize()) |
| 222 | { |
| 223 | // We have found the top level section, now we need to find the |
| 224 | // deepest child section. |
| 225 | return pos->second->ResolveContainedAddress (offset, so_addr); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | else |
| 230 | { |
Greg Clayton | 289f058 | 2011-05-18 18:22:47 +0000 | [diff] [blame] | 231 | // There are no entries that have an address that is >= load_addr, |
| 232 | // so we need to check the last entry on our collection. |
| 233 | addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin(); |
| 234 | if (load_addr >= rpos->first) |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 235 | { |
Greg Clayton | 289f058 | 2011-05-18 18:22:47 +0000 | [diff] [blame] | 236 | addr_t offset = load_addr - rpos->first; |
| 237 | if (offset < rpos->second->GetByteSize()) |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 238 | { |
| 239 | // We have found the top level section, now we need to find the |
| 240 | // deepest child section. |
Greg Clayton | 289f058 | 2011-05-18 18:22:47 +0000 | [diff] [blame] | 241 | return rpos->second->ResolveContainedAddress (offset, so_addr); |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 242 | } |
Jason Molenda | d4c5cd5 | 2010-12-22 02:02:45 +0000 | [diff] [blame] | 243 | } |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | so_addr.Clear(); |
| 247 | return false; |
| 248 | } |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 249 | |
| 250 | void |
| 251 | SectionLoadList::Dump (Stream &s, Target *target) |
| 252 | { |
| 253 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 254 | addr_to_sect_collection::const_iterator pos, end; |
| 255 | for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos) |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 256 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 257 | s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, pos->second.get()); |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 258 | pos->second->Dump (&s, target, 0); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | |