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/Module.h" 17 #include "lldb/Core/Section.h" 18 #include "lldb/Symbol/Block.h" 19 #include "lldb/Symbol/Symbol.h" 20 #include "lldb/Symbol/SymbolContext.h" 21 #include "lldb/Utility/Log.h" 22 #include "lldb/Utility/Stream.h" 23 24 using namespace lldb; 25 using namespace lldb_private; 26 27 SectionLoadList::SectionLoadList(const SectionLoadList &rhs) 28 : m_addr_to_sect(), m_sect_to_addr(), m_mutex() { 29 std::lock_guard<std::recursive_mutex> guard(rhs.m_mutex); 30 m_addr_to_sect = rhs.m_addr_to_sect; 31 m_sect_to_addr = rhs.m_sect_to_addr; 32 } 33 34 void SectionLoadList::operator=(const SectionLoadList &rhs) { 35 std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex); 36 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex); 37 m_addr_to_sect = rhs.m_addr_to_sect; 38 m_sect_to_addr = rhs.m_sect_to_addr; 39 } 40 41 bool SectionLoadList::IsEmpty() const { 42 std::lock_guard<std::recursive_mutex> guard(m_mutex); 43 return m_addr_to_sect.empty(); 44 } 45 46 void SectionLoadList::Clear() { 47 std::lock_guard<std::recursive_mutex> guard(m_mutex); 48 m_addr_to_sect.clear(); 49 m_sect_to_addr.clear(); 50 } 51 52 addr_t 53 SectionLoadList::GetSectionLoadAddress(const lldb::SectionSP §ion) const { 54 // TODO: add support for the same section having multiple load addresses 55 addr_t section_load_addr = LLDB_INVALID_ADDRESS; 56 if (section) { 57 std::lock_guard<std::recursive_mutex> guard(m_mutex); 58 sect_to_addr_collection::const_iterator pos = 59 m_sect_to_addr.find(section.get()); 60 61 if (pos != m_sect_to_addr.end()) 62 section_load_addr = pos->second; 63 } 64 return section_load_addr; 65 } 66 67 bool SectionLoadList::SetSectionLoadAddress(const lldb::SectionSP §ion, 68 addr_t load_addr, 69 bool warn_multiple) { 70 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 71 ModuleSP module_sp(section->GetModule()); 72 73 if (module_sp) { 74 LLDB_LOGV(log, "(section = {0} ({1}.{2}), load_addr = {3:x}) module = {4}", 75 section.get(), module_sp->GetFileSpec(), section->GetName(), 76 load_addr, module_sp.get()); 77 78 if (section->GetByteSize() == 0) 79 return false; // No change 80 81 // Fill in the section -> load_addr map 82 std::lock_guard<std::recursive_mutex> guard(m_mutex); 83 sect_to_addr_collection::iterator sta_pos = 84 m_sect_to_addr.find(section.get()); 85 if (sta_pos != m_sect_to_addr.end()) { 86 if (load_addr == sta_pos->second) 87 return false; // No change... 88 else 89 sta_pos->second = load_addr; 90 } else 91 m_sect_to_addr[section.get()] = load_addr; 92 93 // Fill in the load_addr -> section map 94 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 95 if (ats_pos != m_addr_to_sect.end()) { 96 // Some sections are ok to overlap, and for others we should warn. When 97 // we have multiple load addresses that correspond to a section, we will 98 // always attribute the section to the be last section that claims it 99 // exists at that address. Sometimes it is ok for more that one section 100 // to be loaded at a specific load address, and other times it isn't. 101 // The "warn_multiple" parameter tells us if we should warn in this case 102 // or not. The DynamicLoader plug-in subclasses should know which 103 // sections should warn and which shouldn't (darwin shared cache modules 104 // all shared the same "__LINKEDIT" sections, so the dynamic loader can 105 // pass false for "warn_multiple"). 106 if (warn_multiple && section != ats_pos->second) { 107 ModuleSP module_sp(section->GetModule()); 108 if (module_sp) { 109 ModuleSP curr_module_sp(ats_pos->second->GetModule()); 110 if (curr_module_sp) { 111 module_sp->ReportWarning( 112 "address 0x%16.16" PRIx64 113 " maps to more than one section: %s.%s and %s.%s", 114 load_addr, module_sp->GetFileSpec().GetFilename().GetCString(), 115 section->GetName().GetCString(), 116 curr_module_sp->GetFileSpec().GetFilename().GetCString(), 117 ats_pos->second->GetName().GetCString()); 118 } 119 } 120 } 121 ats_pos->second = section; 122 } else 123 m_addr_to_sect[load_addr] = section; 124 return true; // Changed 125 126 } else { 127 if (log) { 128 log->Printf( 129 "SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64 130 ") error: module has been deleted", 131 __FUNCTION__, static_cast<void *>(section.get()), 132 section->GetName().AsCString(), load_addr); 133 } 134 } 135 return false; 136 } 137 138 size_t SectionLoadList::SetSectionUnloaded(const lldb::SectionSP §ion_sp) { 139 size_t unload_count = 0; 140 141 if (section_sp) { 142 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 143 144 if (log && log->GetVerbose()) { 145 ModuleSP module_sp = section_sp->GetModule(); 146 std::string module_name("<Unknown>"); 147 if (module_sp) { 148 const FileSpec &module_file_spec( 149 section_sp->GetModule()->GetFileSpec()); 150 module_name = module_file_spec.GetPath(); 151 } 152 log->Printf("SectionLoadList::%s (section = %p (%s.%s))", __FUNCTION__, 153 static_cast<void *>(section_sp.get()), module_name.c_str(), 154 section_sp->GetName().AsCString()); 155 } 156 157 std::lock_guard<std::recursive_mutex> guard(m_mutex); 158 159 sect_to_addr_collection::iterator sta_pos = 160 m_sect_to_addr.find(section_sp.get()); 161 if (sta_pos != m_sect_to_addr.end()) { 162 ++unload_count; 163 addr_t load_addr = sta_pos->second; 164 m_sect_to_addr.erase(sta_pos); 165 166 addr_to_sect_collection::iterator ats_pos = 167 m_addr_to_sect.find(load_addr); 168 if (ats_pos != m_addr_to_sect.end()) 169 m_addr_to_sect.erase(ats_pos); 170 } 171 } 172 return unload_count; 173 } 174 175 bool SectionLoadList::SetSectionUnloaded(const lldb::SectionSP §ion_sp, 176 addr_t load_addr) { 177 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 178 179 if (log && log->GetVerbose()) { 180 ModuleSP module_sp = section_sp->GetModule(); 181 std::string module_name("<Unknown>"); 182 if (module_sp) { 183 const FileSpec &module_file_spec(section_sp->GetModule()->GetFileSpec()); 184 module_name = module_file_spec.GetPath(); 185 } 186 log->Printf( 187 "SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 188 ")", 189 __FUNCTION__, static_cast<void *>(section_sp.get()), 190 module_name.c_str(), section_sp->GetName().AsCString(), load_addr); 191 } 192 bool erased = false; 193 std::lock_guard<std::recursive_mutex> guard(m_mutex); 194 sect_to_addr_collection::iterator sta_pos = 195 m_sect_to_addr.find(section_sp.get()); 196 if (sta_pos != m_sect_to_addr.end()) { 197 erased = true; 198 m_sect_to_addr.erase(sta_pos); 199 } 200 201 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 202 if (ats_pos != m_addr_to_sect.end()) { 203 erased = true; 204 m_addr_to_sect.erase(ats_pos); 205 } 206 207 return erased; 208 } 209 210 bool SectionLoadList::ResolveLoadAddress(addr_t load_addr, 211 Address &so_addr) const { 212 // First find the top level section that this load address exists in 213 std::lock_guard<std::recursive_mutex> guard(m_mutex); 214 if (!m_addr_to_sect.empty()) { 215 addr_to_sect_collection::const_iterator pos = 216 m_addr_to_sect.lower_bound(load_addr); 217 if (pos != m_addr_to_sect.end()) { 218 if (load_addr != pos->first && pos != m_addr_to_sect.begin()) 219 --pos; 220 const addr_t pos_load_addr = pos->first; 221 if (load_addr >= pos_load_addr) { 222 addr_t offset = load_addr - pos_load_addr; 223 if (offset < pos->second->GetByteSize()) { 224 // We have found the top level section, now we need to find the 225 // deepest child section. 226 return pos->second->ResolveContainedAddress(offset, so_addr); 227 } 228 } 229 } else { 230 // There are no entries that have an address that is >= load_addr, 231 // so we need to check the last entry on our collection. 232 addr_to_sect_collection::const_reverse_iterator rpos = 233 m_addr_to_sect.rbegin(); 234 if (load_addr >= rpos->first) { 235 addr_t offset = load_addr - rpos->first; 236 if (offset < rpos->second->GetByteSize()) { 237 // We have found the top level section, now we need to find the 238 // deepest child section. 239 return rpos->second->ResolveContainedAddress(offset, so_addr); 240 } 241 } 242 } 243 } 244 so_addr.Clear(); 245 return false; 246 } 247 248 void SectionLoadList::Dump(Stream &s, Target *target) { 249 std::lock_guard<std::recursive_mutex> guard(m_mutex); 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; 252 ++pos) { 253 s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, 254 static_cast<void *>(pos->second.get())); 255 pos->second->Dump(&s, target, 0); 256 } 257 } 258