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 { 31 Mutex::Locker locker(m_mutex); 32 return m_addr_to_sect.empty(); 33 } 34 35 void 36 SectionLoadList::Clear () 37 { 38 Mutex::Locker locker(m_mutex); 39 m_addr_to_sect.clear(); 40 m_sect_to_addr.clear(); 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; 48 if (section) 49 { 50 Mutex::Locker locker(m_mutex); 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; 55 } 56 return section_load_addr; 57 } 58 59 bool 60 SectionLoadList::SetSectionLoadAddress (const Section *section, addr_t load_addr) 61 { 62 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 63 64 if (log) 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)", 68 __FUNCTION__, 69 section, 70 module_file_spec.GetDirectory().AsCString(), 71 module_file_spec.GetDirectory() ? "/" : "", 72 module_file_spec.GetFilename().AsCString(), 73 section->GetName().AsCString(), 74 load_addr); 75 } 76 77 Mutex::Locker locker(m_mutex); 78 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section); 79 if (sta_pos != m_sect_to_addr.end()) 80 { 81 if (load_addr == sta_pos->second) 82 return false; // No change... 83 else 84 sta_pos->second = load_addr; 85 } 86 else 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()) 91 { 92 assert (section != ats_pos->second); 93 ats_pos->second = section; 94 } 95 else 96 m_addr_to_sect[load_addr] = section; 97 98 return true; // Changed 99 } 100 101 size_t 102 SectionLoadList::SetSectionUnloaded (const Section *section) 103 { 104 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 105 106 if (log) 107 { 108 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec()); 109 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))", 110 __FUNCTION__, 111 section, 112 module_file_spec.GetDirectory().AsCString(), 113 module_file_spec.GetDirectory() ? "/" : "", 114 module_file_spec.GetFilename().AsCString(), 115 section->GetName().AsCString()); 116 } 117 118 size_t unload_count = 0; 119 Mutex::Locker locker(m_mutex); 120 121 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section); 122 if (sta_pos != m_sect_to_addr.end()) 123 { 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 } 131 132 return unload_count; 133 } 134 135 bool 136 SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr) 137 { 138 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 139 140 if (log) 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)", 144 __FUNCTION__, 145 section, 146 module_file_spec.GetDirectory().AsCString(), 147 module_file_spec.GetDirectory() ? "/" : "", 148 module_file_spec.GetFilename().AsCString(), 149 section->GetName().AsCString(), 150 load_addr); 151 } 152 bool erased = false; 153 Mutex::Locker locker(m_mutex); 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; 169 } 170 171 172 bool 173 SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const 174 { 175 // First find the top level section that this load address exists in 176 Mutex::Locker locker(m_mutex); 177 if (!m_addr_to_sect.empty()) 178 { 179 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr); 180 if (pos != m_addr_to_sect.end()) 181 { 182 if (load_addr != pos->first && pos != m_addr_to_sect.begin()) 183 --pos; 184 if (load_addr >= pos->first) 185 { 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 { 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) 201 { 202 addr_t offset = load_addr - rpos->first; 203 if (offset < rpos->second->GetByteSize()) 204 { 205 // We have found the top level section, now we need to find the 206 // deepest child section. 207 return rpos->second->ResolveContainedAddress (offset, so_addr); 208 } 209 } 210 } 211 } 212 so_addr.Clear(); 213 return false; 214 } 215 216 void 217 SectionLoadList::Dump (Stream &s, Target *target) 218 { 219 Mutex::Locker locker(m_mutex); 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) 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 229