117f69208SGreg Clayton //===-- SectionLoadList.cpp -------------------------------------*- C++ -*-===// 217f69208SGreg Clayton // 317f69208SGreg Clayton // The LLVM Compiler Infrastructure 417f69208SGreg Clayton // 517f69208SGreg Clayton // This file is distributed under the University of Illinois Open Source 617f69208SGreg Clayton // License. See LICENSE.TXT for details. 717f69208SGreg Clayton // 817f69208SGreg Clayton //===----------------------------------------------------------------------===// 917f69208SGreg Clayton 1017f69208SGreg Clayton #include "lldb/Target/SectionLoadList.h" 1117f69208SGreg Clayton 1217f69208SGreg Clayton // C Includes 1317f69208SGreg Clayton // C++ Includes 1417f69208SGreg Clayton // Other libraries and framework includes 1517f69208SGreg Clayton // Project includes 1617f69208SGreg Clayton #include "lldb/Core/Log.h" 1717f69208SGreg Clayton #include "lldb/Core/Module.h" 1817f69208SGreg Clayton #include "lldb/Core/Section.h" 1917f69208SGreg Clayton #include "lldb/Core/Stream.h" 2017f69208SGreg Clayton #include "lldb/Symbol/Block.h" 2117f69208SGreg Clayton #include "lldb/Symbol/Symbol.h" 2217f69208SGreg Clayton #include "lldb/Symbol/SymbolContext.h" 2317f69208SGreg Clayton 2417f69208SGreg Clayton using namespace lldb; 2517f69208SGreg Clayton using namespace lldb_private; 2617f69208SGreg Clayton 2717f69208SGreg Clayton 28d5944cd1SGreg Clayton SectionLoadList::SectionLoadList (const SectionLoadList& rhs) : 29d5944cd1SGreg Clayton m_addr_to_sect(), 30d5944cd1SGreg Clayton m_sect_to_addr(), 31d5944cd1SGreg Clayton m_mutex (Mutex::eMutexTypeRecursive) 32d5944cd1SGreg Clayton { 33d5944cd1SGreg Clayton Mutex::Locker locker(rhs.m_mutex); 34d5944cd1SGreg Clayton m_addr_to_sect = rhs.m_addr_to_sect; 35d5944cd1SGreg Clayton m_sect_to_addr = rhs.m_sect_to_addr; 36d5944cd1SGreg Clayton } 37d5944cd1SGreg Clayton 38d5944cd1SGreg Clayton void 39d5944cd1SGreg Clayton SectionLoadList::operator=(const SectionLoadList &rhs) 40d5944cd1SGreg Clayton { 41d5944cd1SGreg Clayton Mutex::Locker lhs_locker (m_mutex); 42d5944cd1SGreg Clayton Mutex::Locker rhs_locker (rhs.m_mutex); 43d5944cd1SGreg Clayton m_addr_to_sect = rhs.m_addr_to_sect; 44d5944cd1SGreg Clayton m_sect_to_addr = rhs.m_sect_to_addr; 45d5944cd1SGreg Clayton } 46d5944cd1SGreg Clayton 4717f69208SGreg Clayton bool 4817f69208SGreg Clayton SectionLoadList::IsEmpty() const 4917f69208SGreg Clayton { 5010177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 516d093451SGreg Clayton return m_addr_to_sect.empty(); 5217f69208SGreg Clayton } 5317f69208SGreg Clayton 5417f69208SGreg Clayton void 5517f69208SGreg Clayton SectionLoadList::Clear () 5617f69208SGreg Clayton { 5710177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 586d093451SGreg Clayton m_addr_to_sect.clear(); 596d093451SGreg Clayton m_sect_to_addr.clear(); 6017f69208SGreg Clayton } 6117f69208SGreg Clayton 6217f69208SGreg Clayton addr_t 637820bd1eSGreg Clayton SectionLoadList::GetSectionLoadAddress (const lldb::SectionSP §ion) const 6417f69208SGreg Clayton { 6517f69208SGreg Clayton // TODO: add support for the same section having multiple load addresses 6617f69208SGreg Clayton addr_t section_load_addr = LLDB_INVALID_ADDRESS; 6710177aa0SGreg Clayton if (section) 6810177aa0SGreg Clayton { 6910177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 707820bd1eSGreg Clayton sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section.get()); 716d093451SGreg Clayton 726d093451SGreg Clayton if (pos != m_sect_to_addr.end()) 736d093451SGreg Clayton section_load_addr = pos->second; 7410177aa0SGreg Clayton } 7517f69208SGreg Clayton return section_load_addr; 7617f69208SGreg Clayton } 7717f69208SGreg Clayton 7817f69208SGreg Clayton bool 797820bd1eSGreg Clayton SectionLoadList::SetSectionLoadAddress (const lldb::SectionSP §ion, addr_t load_addr, bool warn_multiple) 8017f69208SGreg Clayton { 815160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 8217f69208SGreg Clayton 83fd814c5aSGreg Clayton ModuleSP module_sp (section->GetModule()); 84fd814c5aSGreg Clayton 85fd814c5aSGreg Clayton if (module_sp) 86fd814c5aSGreg Clayton { 8717f69208SGreg Clayton if (log) 888b2fe6dcSGreg Clayton { 89fd814c5aSGreg Clayton const FileSpec &module_file_spec (module_sp->GetFileSpec()); 90fd814c5aSGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ") module = %p", 91324a1036SSaleem Abdulrasool __FUNCTION__, static_cast<void*>(section.get()), 92b5ad4ec7SGreg Clayton module_file_spec.GetPath().c_str(), 93324a1036SSaleem Abdulrasool section->GetName().AsCString(), load_addr, 94324a1036SSaleem Abdulrasool static_cast<void*>(module_sp.get())); 958b2fe6dcSGreg Clayton } 9617f69208SGreg Clayton 97c859e2d5SGreg Clayton if (section->GetByteSize() == 0) 98c859e2d5SGreg Clayton return false; // No change 99c859e2d5SGreg Clayton 1001e8ac36bSGreg Clayton // Fill in the section -> load_addr map 10110177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 1027820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get()); 1036d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 10417f69208SGreg Clayton { 1056d093451SGreg Clayton if (load_addr == sta_pos->second) 10610177aa0SGreg Clayton return false; // No change... 10710177aa0SGreg Clayton else 1086d093451SGreg Clayton sta_pos->second = load_addr; 10917f69208SGreg Clayton } 11010177aa0SGreg Clayton else 1117820bd1eSGreg Clayton m_sect_to_addr[section.get()] = load_addr; 1126d093451SGreg Clayton 1131e8ac36bSGreg Clayton // Fill in the load_addr -> section map 1146d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 1156d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 11610177aa0SGreg Clayton { 1171e8ac36bSGreg Clayton // Some sections are ok to overlap, and for others we should warn. When 1181e8ac36bSGreg Clayton // we have multiple load addresses that correspond to a section, we will 119*aaa0ba31SBruce Mitchener // always attribute the section to the be last section that claims it 1201e8ac36bSGreg Clayton // exists at that address. Sometimes it is ok for more that one section 1211e8ac36bSGreg Clayton // to be loaded at a specific load address, and other times it isn't. 1221e8ac36bSGreg Clayton // The "warn_multiple" parameter tells us if we should warn in this case 1231e8ac36bSGreg Clayton // or not. The DynamicLoader plug-in subclasses should know which 1241e8ac36bSGreg Clayton // sections should warn and which shouldn't (darwin shared cache modules 1251e8ac36bSGreg Clayton // all shared the same "__LINKEDIT" sections, so the dynamic loader can 1261e8ac36bSGreg Clayton // pass false for "warn_multiple"). 1271e8ac36bSGreg Clayton if (warn_multiple && section != ats_pos->second) 128c859e2d5SGreg Clayton { 129e72dfb32SGreg Clayton ModuleSP module_sp (section->GetModule()); 130e72dfb32SGreg Clayton if (module_sp) 131c859e2d5SGreg Clayton { 1329d192e1aSGreg Clayton ModuleSP curr_module_sp (ats_pos->second->GetModule()); 1339d192e1aSGreg Clayton if (curr_module_sp) 1349d192e1aSGreg Clayton { 135d01b2953SDaniel Malea module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s", 136c859e2d5SGreg Clayton load_addr, 137e72dfb32SGreg Clayton module_sp->GetFileSpec().GetFilename().GetCString(), 138c859e2d5SGreg Clayton section->GetName().GetCString(), 1399d192e1aSGreg Clayton curr_module_sp->GetFileSpec().GetFilename().GetCString(), 140c859e2d5SGreg Clayton ats_pos->second->GetName().GetCString()); 141c859e2d5SGreg Clayton } 142c859e2d5SGreg Clayton } 1439d192e1aSGreg Clayton } 1446d093451SGreg Clayton ats_pos->second = section; 14510177aa0SGreg Clayton } 1466d093451SGreg Clayton else 1476d093451SGreg Clayton m_addr_to_sect[load_addr] = section; 14817f69208SGreg Clayton return true; // Changed 149fd814c5aSGreg Clayton 150fd814c5aSGreg Clayton } 151fd814c5aSGreg Clayton else 152fd814c5aSGreg Clayton { 153fd814c5aSGreg Clayton if (log) 154fd814c5aSGreg Clayton { 155fd814c5aSGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64 ") error: module has been deleted", 156324a1036SSaleem Abdulrasool __FUNCTION__, static_cast<void*>(section.get()), 157fd814c5aSGreg Clayton section->GetName().AsCString(), 158fd814c5aSGreg Clayton load_addr); 159fd814c5aSGreg Clayton } 160fd814c5aSGreg Clayton } 161fd814c5aSGreg Clayton return false; 16217f69208SGreg Clayton } 16317f69208SGreg Clayton 16417f69208SGreg Clayton size_t 1657820bd1eSGreg Clayton SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp) 1667820bd1eSGreg Clayton { 1677820bd1eSGreg Clayton size_t unload_count = 0; 1687820bd1eSGreg Clayton 1697820bd1eSGreg Clayton if (section_sp) 17017f69208SGreg Clayton { 1715160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 17217f69208SGreg Clayton 17317f69208SGreg Clayton if (log) 1748b2fe6dcSGreg Clayton { 1757820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 176b5ad4ec7SGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s))", 177324a1036SSaleem Abdulrasool __FUNCTION__, static_cast<void*>(section_sp.get()), 178b5ad4ec7SGreg Clayton module_file_spec.GetPath().c_str(), 1797820bd1eSGreg Clayton section_sp->GetName().AsCString()); 1808b2fe6dcSGreg Clayton } 18117f69208SGreg Clayton 18210177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 1836d093451SGreg Clayton 1847820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); 1856d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 18617f69208SGreg Clayton { 1873c94737fSGreg Clayton ++unload_count; 1886d093451SGreg Clayton addr_t load_addr = sta_pos->second; 1896d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 1906d093451SGreg Clayton 1916d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 1926d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 1936d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 19417f69208SGreg Clayton } 1957820bd1eSGreg Clayton } 19617f69208SGreg Clayton return unload_count; 19717f69208SGreg Clayton } 19817f69208SGreg Clayton 19917f69208SGreg Clayton bool 2007820bd1eSGreg Clayton SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp, addr_t load_addr) 20117f69208SGreg Clayton { 2025160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 20317f69208SGreg Clayton 20417f69208SGreg Clayton if (log) 2058b2fe6dcSGreg Clayton { 2067820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 207b5ad4ec7SGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ")", 208324a1036SSaleem Abdulrasool __FUNCTION__, static_cast<void*>(section_sp.get()), 209b5ad4ec7SGreg Clayton module_file_spec.GetPath().c_str(), 210324a1036SSaleem Abdulrasool section_sp->GetName().AsCString(), load_addr); 2118b2fe6dcSGreg Clayton } 2126d093451SGreg Clayton bool erased = false; 21310177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 2147820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); 2156d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 2166d093451SGreg Clayton { 2176d093451SGreg Clayton erased = true; 2186d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 2196d093451SGreg Clayton } 2206d093451SGreg Clayton 2216d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 2226d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 2236d093451SGreg Clayton { 2246d093451SGreg Clayton erased = true; 2256d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 2266d093451SGreg Clayton } 2276d093451SGreg Clayton 2286d093451SGreg Clayton return erased; 22917f69208SGreg Clayton } 23017f69208SGreg Clayton 23117f69208SGreg Clayton 23217f69208SGreg Clayton bool 23317f69208SGreg Clayton SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const 23417f69208SGreg Clayton { 23517f69208SGreg Clayton // First find the top level section that this load address exists in 23610177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 237c5e9a7dcSGreg Clayton if (!m_addr_to_sect.empty()) 238c5e9a7dcSGreg Clayton { 2396d093451SGreg Clayton addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr); 2406d093451SGreg Clayton if (pos != m_addr_to_sect.end()) 24117f69208SGreg Clayton { 2426d093451SGreg Clayton if (load_addr != pos->first && pos != m_addr_to_sect.begin()) 24310177aa0SGreg Clayton --pos; 24439f7ee86SGreg Clayton const addr_t pos_load_addr = pos->first; 24539f7ee86SGreg Clayton if (load_addr >= pos_load_addr) 246f830e481SJason Molenda { 24739f7ee86SGreg Clayton addr_t offset = load_addr - pos_load_addr; 24810177aa0SGreg Clayton if (offset < pos->second->GetByteSize()) 24917f69208SGreg Clayton { 25017f69208SGreg Clayton // We have found the top level section, now we need to find the 25117f69208SGreg Clayton // deepest child section. 25210177aa0SGreg Clayton return pos->second->ResolveContainedAddress (offset, so_addr); 25317f69208SGreg Clayton } 25417f69208SGreg Clayton } 255f830e481SJason Molenda } 256c5e9a7dcSGreg Clayton else 257c5e9a7dcSGreg Clayton { 2581c870d6eSGreg Clayton // There are no entries that have an address that is >= load_addr, 2591c870d6eSGreg Clayton // so we need to check the last entry on our collection. 2601c870d6eSGreg Clayton addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin(); 2611c870d6eSGreg Clayton if (load_addr >= rpos->first) 262c5e9a7dcSGreg Clayton { 2631c870d6eSGreg Clayton addr_t offset = load_addr - rpos->first; 2641c870d6eSGreg Clayton if (offset < rpos->second->GetByteSize()) 265c5e9a7dcSGreg Clayton { 266c5e9a7dcSGreg Clayton // We have found the top level section, now we need to find the 267c5e9a7dcSGreg Clayton // deepest child section. 2681c870d6eSGreg Clayton return rpos->second->ResolveContainedAddress (offset, so_addr); 269c5e9a7dcSGreg Clayton } 270c5e9a7dcSGreg Clayton } 271c5e9a7dcSGreg Clayton } 272c5e9a7dcSGreg Clayton } 27317f69208SGreg Clayton so_addr.Clear(); 27417f69208SGreg Clayton return false; 27517f69208SGreg Clayton } 27610177aa0SGreg Clayton 27710177aa0SGreg Clayton void 27810177aa0SGreg Clayton SectionLoadList::Dump (Stream &s, Target *target) 27910177aa0SGreg Clayton { 28010177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 2816d093451SGreg Clayton addr_to_sect_collection::const_iterator pos, end; 2826d093451SGreg Clayton for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos) 28310177aa0SGreg Clayton { 284324a1036SSaleem Abdulrasool s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", 285324a1036SSaleem Abdulrasool pos->first, static_cast<void*>(pos->second.get())); 28610177aa0SGreg Clayton pos->second->Dump (&s, target, 0); 28710177aa0SGreg Clayton } 28810177aa0SGreg Clayton } 28910177aa0SGreg Clayton 29010177aa0SGreg Clayton 291