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 |
| 44 | SectionLoadList::GetSectionLoadAddress (const Section *section) const |
| 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 | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 51 | sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section); |
| 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 |
| 60 | SectionLoadList::SetSectionLoadAddress (const Section *section, addr_t load_addr) |
| 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()); |
| 67 | log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)", |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 68 | __FUNCTION__, |
| 69 | section, |
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 | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 77 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 78 | sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section); |
| 79 | if (sta_pos != m_sect_to_addr.end()) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 80 | { |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 81 | if (load_addr == sta_pos->second) |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 82 | return false; // No change... |
| 83 | else |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 84 | sta_pos->second = load_addr; |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 85 | } |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 86 | else |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 87 | m_sect_to_addr[section] = load_addr; |
| 88 | |
| 89 | addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); |
| 90 | if (ats_pos != m_addr_to_sect.end()) |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 91 | { |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 92 | assert (section != ats_pos->second); |
| 93 | ats_pos->second = section; |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 94 | } |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 95 | else |
| 96 | m_addr_to_sect[load_addr] = section; |
| 97 | |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 98 | return true; // Changed |
| 99 | } |
| 100 | |
| 101 | size_t |
| 102 | SectionLoadList::SetSectionUnloaded (const Section *section) |
| 103 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 104 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 105 | |
| 106 | if (log) |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 107 | { |
| 108 | const FileSpec &module_file_spec (section->GetModule()->GetFileSpec()); |
| 109 | log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))", |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 110 | __FUNCTION__, |
| 111 | section, |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 112 | module_file_spec.GetDirectory().AsCString(), |
| 113 | module_file_spec.GetDirectory() ? "/" : "", |
| 114 | module_file_spec.GetFilename().AsCString(), |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 115 | section->GetName().AsCString()); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 116 | } |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 117 | |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 118 | size_t unload_count = 0; |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 119 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 120 | |
| 121 | sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section); |
| 122 | if (sta_pos != m_sect_to_addr.end()) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 123 | { |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 124 | addr_t load_addr = sta_pos->second; |
| 125 | m_sect_to_addr.erase (sta_pos); |
| 126 | |
| 127 | addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); |
| 128 | if (ats_pos != m_addr_to_sect.end()) |
| 129 | m_addr_to_sect.erase (ats_pos); |
| 130 | } |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 131 | |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 132 | return unload_count; |
| 133 | } |
| 134 | |
| 135 | bool |
| 136 | SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr) |
| 137 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 138 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 139 | |
| 140 | if (log) |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 141 | { |
| 142 | const FileSpec &module_file_spec (section->GetModule()->GetFileSpec()); |
| 143 | log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)", |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 144 | __FUNCTION__, |
| 145 | section, |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 146 | module_file_spec.GetDirectory().AsCString(), |
| 147 | module_file_spec.GetDirectory() ? "/" : "", |
| 148 | module_file_spec.GetFilename().AsCString(), |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 149 | section->GetName().AsCString(), |
| 150 | load_addr); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 151 | } |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 152 | bool erased = false; |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 153 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 154 | sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section); |
| 155 | if (sta_pos != m_sect_to_addr.end()) |
| 156 | { |
| 157 | erased = true; |
| 158 | m_sect_to_addr.erase (sta_pos); |
| 159 | } |
| 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 | { |
| 164 | erased = true; |
| 165 | m_addr_to_sect.erase (ats_pos); |
| 166 | } |
| 167 | |
| 168 | return erased; |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| 172 | bool |
| 173 | SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const |
| 174 | { |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 175 | // First find the top level section that this load address exists in |
| 176 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 177 | if (!m_addr_to_sect.empty()) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 178 | { |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 179 | addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr); |
| 180 | if (pos != m_addr_to_sect.end()) |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 181 | { |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 182 | if (load_addr != pos->first && pos != m_addr_to_sect.begin()) |
| 183 | --pos; |
| 184 | if (load_addr >= pos->first) |
Jason Molenda | d4c5cd5 | 2010-12-22 02:02:45 +0000 | [diff] [blame] | 185 | { |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 186 | addr_t offset = load_addr - pos->first; |
| 187 | if (offset < pos->second->GetByteSize()) |
| 188 | { |
| 189 | // We have found the top level section, now we need to find the |
| 190 | // deepest child section. |
| 191 | return pos->second->ResolveContainedAddress (offset, so_addr); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | else |
| 196 | { |
Greg Clayton | 289f058 | 2011-05-18 18:22:47 +0000 | [diff] [blame] | 197 | // There are no entries that have an address that is >= load_addr, |
| 198 | // so we need to check the last entry on our collection. |
| 199 | addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin(); |
| 200 | if (load_addr >= rpos->first) |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 201 | { |
Greg Clayton | 289f058 | 2011-05-18 18:22:47 +0000 | [diff] [blame] | 202 | addr_t offset = load_addr - rpos->first; |
| 203 | if (offset < rpos->second->GetByteSize()) |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 204 | { |
| 205 | // We have found the top level section, now we need to find the |
| 206 | // deepest child section. |
Greg Clayton | 289f058 | 2011-05-18 18:22:47 +0000 | [diff] [blame] | 207 | return rpos->second->ResolveContainedAddress (offset, so_addr); |
Greg Clayton | 668a3cd | 2011-05-17 18:13:59 +0000 | [diff] [blame] | 208 | } |
Jason Molenda | d4c5cd5 | 2010-12-22 02:02:45 +0000 | [diff] [blame] | 209 | } |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | so_addr.Clear(); |
| 213 | return false; |
| 214 | } |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 215 | |
| 216 | void |
| 217 | SectionLoadList::Dump (Stream &s, Target *target) |
| 218 | { |
| 219 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 220 | addr_to_sect_collection::const_iterator pos, end; |
| 221 | 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] | 222 | { |
| 223 | s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second); |
| 224 | pos->second->Dump (&s, target, 0); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |