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 | |
| 28 | bool |
| 29 | SectionLoadList::IsEmpty() const |
| 30 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 31 | Mutex::Locker locker(m_mutex); |
| 32 | return m_collection.empty(); |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void |
| 36 | SectionLoadList::Clear () |
| 37 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 38 | Mutex::Locker locker(m_mutex); |
| 39 | return m_collection.clear(); |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | addr_t |
| 43 | SectionLoadList::GetSectionLoadAddress (const Section *section) const |
| 44 | { |
| 45 | // TODO: add support for the same section having multiple load addresses |
| 46 | addr_t section_load_addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 47 | if (section) |
| 48 | { |
| 49 | Mutex::Locker locker(m_mutex); |
| 50 | collection::const_iterator pos, end = m_collection.end(); |
| 51 | for (pos = m_collection.begin(); pos != end; ++pos) |
| 52 | { |
| 53 | const addr_t pos_load_addr = pos->first; |
| 54 | const Section *pos_section = pos->second; |
| 55 | if (pos_section == section) |
| 56 | { |
| 57 | section_load_addr = pos_load_addr; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return section_load_addr; |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool |
| 66 | SectionLoadList::SetSectionLoadAddress (const Section *section, addr_t load_addr) |
| 67 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 68 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 69 | |
| 70 | if (log) |
| 71 | log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16llx)", |
| 72 | __FUNCTION__, |
| 73 | section, |
| 74 | section->GetModule()->GetFileSpec().GetFilename().AsCString(), |
| 75 | section->GetName().AsCString(), |
| 76 | load_addr); |
| 77 | |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 78 | Mutex::Locker locker(m_mutex); |
| 79 | collection::iterator pos = m_collection.find(load_addr); |
| 80 | if (pos != m_collection.end()) |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 81 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 82 | if (section == pos->second) |
| 83 | return false; // No change... |
| 84 | else |
| 85 | pos->second = section; |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 86 | } |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 87 | else |
| 88 | { |
| 89 | m_collection[load_addr] = section; |
| 90 | } |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 91 | return true; // Changed |
| 92 | } |
| 93 | |
| 94 | size_t |
| 95 | SectionLoadList::SetSectionUnloaded (const Section *section) |
| 96 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 97 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 98 | |
| 99 | if (log) |
| 100 | log->Printf ("SectionLoadList::%s (section = %p (%s.%s))", |
| 101 | __FUNCTION__, |
| 102 | section, |
| 103 | section->GetModule()->GetFileSpec().GetFilename().AsCString(), |
| 104 | section->GetName().AsCString()); |
| 105 | |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 106 | size_t unload_count = 0; |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 107 | Mutex::Locker locker(m_mutex); |
| 108 | bool erased = false; |
| 109 | do |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 110 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 111 | erased = false; |
| 112 | for (collection::iterator pos = m_collection.begin(); pos != m_collection.end(); ++pos) |
| 113 | { |
| 114 | if (pos->second == section) |
| 115 | { |
| 116 | m_collection.erase(pos); |
| 117 | erased = true; |
| 118 | } |
| 119 | } |
| 120 | } while (erased); |
| 121 | |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 122 | return unload_count; |
| 123 | } |
| 124 | |
| 125 | bool |
| 126 | SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr) |
| 127 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 128 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 129 | |
| 130 | if (log) |
| 131 | log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16llx)", |
| 132 | __FUNCTION__, |
| 133 | section, |
| 134 | section->GetModule()->GetFileSpec().GetFilename().AsCString(), |
| 135 | section->GetName().AsCString(), |
| 136 | load_addr); |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 137 | Mutex::Locker locker(m_mutex); |
| 138 | return m_collection.erase (load_addr) != 0; |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | bool |
| 143 | SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const |
| 144 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 145 | // First find the top level section that this load address exists in |
| 146 | Mutex::Locker locker(m_mutex); |
| 147 | collection::const_iterator pos = m_collection.lower_bound (load_addr); |
| 148 | if (pos != m_collection.end()) |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 149 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 150 | if (load_addr != pos->first && pos != m_collection.begin()) |
| 151 | --pos; |
| 152 | assert (load_addr >= pos->first); |
| 153 | addr_t offset = load_addr - pos->first; |
| 154 | if (offset < pos->second->GetByteSize()) |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 155 | { |
| 156 | // We have found the top level section, now we need to find the |
| 157 | // deepest child section. |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 158 | return pos->second->ResolveContainedAddress (offset, so_addr); |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | so_addr.Clear(); |
| 162 | return false; |
| 163 | } |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 164 | |
| 165 | void |
| 166 | SectionLoadList::Dump (Stream &s, Target *target) |
| 167 | { |
| 168 | Mutex::Locker locker(m_mutex); |
| 169 | collection::const_iterator pos, end; |
| 170 | for (pos = m_collection.begin(), end = m_collection.end(); pos != end; ++pos) |
| 171 | { |
| 172 | s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second); |
| 173 | pos->second->Dump (&s, target, 0); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | |