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