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 119aaa0ba31SBruce 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 { 175*cbff63adSJim Ingham ModuleSP module_sp = section_sp->GetModule(); 176*cbff63adSJim Ingham std::string module_name("<Unknown>"); 177*cbff63adSJim Ingham if (module_sp) 178*cbff63adSJim Ingham { 1797820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 180*cbff63adSJim Ingham module_name = module_file_spec.GetPath(); 181*cbff63adSJim Ingham } 182b5ad4ec7SGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s))", 183324a1036SSaleem Abdulrasool __FUNCTION__, static_cast<void*>(section_sp.get()), 184*cbff63adSJim Ingham module_name.c_str(), 1857820bd1eSGreg Clayton section_sp->GetName().AsCString()); 1868b2fe6dcSGreg Clayton } 18717f69208SGreg Clayton 18810177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 1896d093451SGreg Clayton 1907820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); 1916d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 19217f69208SGreg Clayton { 1933c94737fSGreg Clayton ++unload_count; 1946d093451SGreg Clayton addr_t load_addr = sta_pos->second; 1956d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 1966d093451SGreg Clayton 1976d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 1986d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 1996d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 20017f69208SGreg Clayton } 2017820bd1eSGreg Clayton } 20217f69208SGreg Clayton return unload_count; 20317f69208SGreg Clayton } 20417f69208SGreg Clayton 20517f69208SGreg Clayton bool 2067820bd1eSGreg Clayton SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp, addr_t load_addr) 20717f69208SGreg Clayton { 2085160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 20917f69208SGreg Clayton 21017f69208SGreg Clayton if (log) 2118b2fe6dcSGreg Clayton { 212*cbff63adSJim Ingham ModuleSP module_sp = section_sp->GetModule(); 213*cbff63adSJim Ingham std::string module_name("<Unknown>"); 214*cbff63adSJim Ingham if (module_sp) 215*cbff63adSJim Ingham { 2167820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 217*cbff63adSJim Ingham module_name = module_file_spec.GetPath(); 218*cbff63adSJim Ingham } 219b5ad4ec7SGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ")", 220324a1036SSaleem Abdulrasool __FUNCTION__, static_cast<void*>(section_sp.get()), 221*cbff63adSJim Ingham module_name.c_str(), 222324a1036SSaleem Abdulrasool section_sp->GetName().AsCString(), load_addr); 2238b2fe6dcSGreg Clayton } 2246d093451SGreg Clayton bool erased = false; 22510177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 2267820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); 2276d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 2286d093451SGreg Clayton { 2296d093451SGreg Clayton erased = true; 2306d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 2316d093451SGreg Clayton } 2326d093451SGreg Clayton 2336d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 2346d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 2356d093451SGreg Clayton { 2366d093451SGreg Clayton erased = true; 2376d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 2386d093451SGreg Clayton } 2396d093451SGreg Clayton 2406d093451SGreg Clayton return erased; 24117f69208SGreg Clayton } 24217f69208SGreg Clayton 24317f69208SGreg Clayton 24417f69208SGreg Clayton bool 24517f69208SGreg Clayton SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const 24617f69208SGreg Clayton { 24717f69208SGreg Clayton // First find the top level section that this load address exists in 24810177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 249c5e9a7dcSGreg Clayton if (!m_addr_to_sect.empty()) 250c5e9a7dcSGreg Clayton { 2516d093451SGreg Clayton addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr); 2526d093451SGreg Clayton if (pos != m_addr_to_sect.end()) 25317f69208SGreg Clayton { 2546d093451SGreg Clayton if (load_addr != pos->first && pos != m_addr_to_sect.begin()) 25510177aa0SGreg Clayton --pos; 25639f7ee86SGreg Clayton const addr_t pos_load_addr = pos->first; 25739f7ee86SGreg Clayton if (load_addr >= pos_load_addr) 258f830e481SJason Molenda { 25939f7ee86SGreg Clayton addr_t offset = load_addr - pos_load_addr; 26010177aa0SGreg Clayton if (offset < pos->second->GetByteSize()) 26117f69208SGreg Clayton { 26217f69208SGreg Clayton // We have found the top level section, now we need to find the 26317f69208SGreg Clayton // deepest child section. 26410177aa0SGreg Clayton return pos->second->ResolveContainedAddress (offset, so_addr); 26517f69208SGreg Clayton } 26617f69208SGreg Clayton } 267f830e481SJason Molenda } 268c5e9a7dcSGreg Clayton else 269c5e9a7dcSGreg Clayton { 2701c870d6eSGreg Clayton // There are no entries that have an address that is >= load_addr, 2711c870d6eSGreg Clayton // so we need to check the last entry on our collection. 2721c870d6eSGreg Clayton addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin(); 2731c870d6eSGreg Clayton if (load_addr >= rpos->first) 274c5e9a7dcSGreg Clayton { 2751c870d6eSGreg Clayton addr_t offset = load_addr - rpos->first; 2761c870d6eSGreg Clayton if (offset < rpos->second->GetByteSize()) 277c5e9a7dcSGreg Clayton { 278c5e9a7dcSGreg Clayton // We have found the top level section, now we need to find the 279c5e9a7dcSGreg Clayton // deepest child section. 2801c870d6eSGreg Clayton return rpos->second->ResolveContainedAddress (offset, so_addr); 281c5e9a7dcSGreg Clayton } 282c5e9a7dcSGreg Clayton } 283c5e9a7dcSGreg Clayton } 284c5e9a7dcSGreg Clayton } 28517f69208SGreg Clayton so_addr.Clear(); 28617f69208SGreg Clayton return false; 28717f69208SGreg Clayton } 28810177aa0SGreg Clayton 28910177aa0SGreg Clayton void 29010177aa0SGreg Clayton SectionLoadList::Dump (Stream &s, Target *target) 29110177aa0SGreg Clayton { 29210177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 2936d093451SGreg Clayton addr_to_sect_collection::const_iterator pos, end; 2946d093451SGreg Clayton for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos) 29510177aa0SGreg Clayton { 296324a1036SSaleem Abdulrasool s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", 297324a1036SSaleem Abdulrasool pos->first, static_cast<void*>(pos->second.get())); 29810177aa0SGreg Clayton pos->second->Dump (&s, target, 0); 29910177aa0SGreg Clayton } 30010177aa0SGreg Clayton } 30110177aa0SGreg Clayton 30210177aa0SGreg Clayton 303