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 28*d5944cd1SGreg Clayton SectionLoadList::SectionLoadList (const SectionLoadList& rhs) : 29*d5944cd1SGreg Clayton m_addr_to_sect(), 30*d5944cd1SGreg Clayton m_sect_to_addr(), 31*d5944cd1SGreg Clayton m_mutex (Mutex::eMutexTypeRecursive) 32*d5944cd1SGreg Clayton { 33*d5944cd1SGreg Clayton Mutex::Locker locker(rhs.m_mutex); 34*d5944cd1SGreg Clayton m_addr_to_sect = rhs.m_addr_to_sect; 35*d5944cd1SGreg Clayton m_sect_to_addr = rhs.m_sect_to_addr; 36*d5944cd1SGreg Clayton } 37*d5944cd1SGreg Clayton 38*d5944cd1SGreg Clayton void 39*d5944cd1SGreg Clayton SectionLoadList::operator=(const SectionLoadList &rhs) 40*d5944cd1SGreg Clayton { 41*d5944cd1SGreg Clayton Mutex::Locker lhs_locker (m_mutex); 42*d5944cd1SGreg Clayton Mutex::Locker rhs_locker (rhs.m_mutex); 43*d5944cd1SGreg Clayton m_addr_to_sect = rhs.m_addr_to_sect; 44*d5944cd1SGreg Clayton m_sect_to_addr = rhs.m_sect_to_addr; 45*d5944cd1SGreg Clayton } 46*d5944cd1SGreg 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", 9117f69208SGreg Clayton __FUNCTION__, 927820bd1eSGreg Clayton section.get(), 93b5ad4ec7SGreg Clayton module_file_spec.GetPath().c_str(), 9417f69208SGreg Clayton section->GetName().AsCString(), 95fd814c5aSGreg Clayton load_addr, 96fd814c5aSGreg Clayton module_sp.get()); 978b2fe6dcSGreg Clayton } 9817f69208SGreg Clayton 99c859e2d5SGreg Clayton if (section->GetByteSize() == 0) 100c859e2d5SGreg Clayton return false; // No change 101c859e2d5SGreg Clayton 1021e8ac36bSGreg Clayton // Fill in the section -> load_addr map 10310177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 1047820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get()); 1056d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 10617f69208SGreg Clayton { 1076d093451SGreg Clayton if (load_addr == sta_pos->second) 10810177aa0SGreg Clayton return false; // No change... 10910177aa0SGreg Clayton else 1106d093451SGreg Clayton sta_pos->second = load_addr; 11117f69208SGreg Clayton } 11210177aa0SGreg Clayton else 1137820bd1eSGreg Clayton m_sect_to_addr[section.get()] = load_addr; 1146d093451SGreg Clayton 1151e8ac36bSGreg Clayton // Fill in the load_addr -> section map 1166d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 1176d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 11810177aa0SGreg Clayton { 1191e8ac36bSGreg Clayton // Some sections are ok to overlap, and for others we should warn. When 1201e8ac36bSGreg Clayton // we have multiple load addresses that correspond to a section, we will 1211e8ac36bSGreg Clayton // allways attribute the section to the be last section that claims it 1221e8ac36bSGreg Clayton // exists at that address. Sometimes it is ok for more that one section 1231e8ac36bSGreg Clayton // to be loaded at a specific load address, and other times it isn't. 1241e8ac36bSGreg Clayton // The "warn_multiple" parameter tells us if we should warn in this case 1251e8ac36bSGreg Clayton // or not. The DynamicLoader plug-in subclasses should know which 1261e8ac36bSGreg Clayton // sections should warn and which shouldn't (darwin shared cache modules 1271e8ac36bSGreg Clayton // all shared the same "__LINKEDIT" sections, so the dynamic loader can 1281e8ac36bSGreg Clayton // pass false for "warn_multiple"). 1291e8ac36bSGreg Clayton if (warn_multiple && section != ats_pos->second) 130c859e2d5SGreg Clayton { 131e72dfb32SGreg Clayton ModuleSP module_sp (section->GetModule()); 132e72dfb32SGreg Clayton if (module_sp) 133c859e2d5SGreg Clayton { 1349d192e1aSGreg Clayton ModuleSP curr_module_sp (ats_pos->second->GetModule()); 1359d192e1aSGreg Clayton if (curr_module_sp) 1369d192e1aSGreg Clayton { 137d01b2953SDaniel Malea module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s", 138c859e2d5SGreg Clayton load_addr, 139e72dfb32SGreg Clayton module_sp->GetFileSpec().GetFilename().GetCString(), 140c859e2d5SGreg Clayton section->GetName().GetCString(), 1419d192e1aSGreg Clayton curr_module_sp->GetFileSpec().GetFilename().GetCString(), 142c859e2d5SGreg Clayton ats_pos->second->GetName().GetCString()); 143c859e2d5SGreg Clayton } 144c859e2d5SGreg Clayton } 1459d192e1aSGreg Clayton } 1466d093451SGreg Clayton ats_pos->second = section; 14710177aa0SGreg Clayton } 1486d093451SGreg Clayton else 1496d093451SGreg Clayton m_addr_to_sect[load_addr] = section; 15017f69208SGreg Clayton return true; // Changed 151fd814c5aSGreg Clayton 152fd814c5aSGreg Clayton } 153fd814c5aSGreg Clayton else 154fd814c5aSGreg Clayton { 155fd814c5aSGreg Clayton if (log) 156fd814c5aSGreg Clayton { 157fd814c5aSGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64 ") error: module has been deleted", 158fd814c5aSGreg Clayton __FUNCTION__, 159fd814c5aSGreg Clayton section.get(), 160fd814c5aSGreg Clayton section->GetName().AsCString(), 161fd814c5aSGreg Clayton load_addr); 162fd814c5aSGreg Clayton } 163fd814c5aSGreg Clayton } 164fd814c5aSGreg Clayton return false; 16517f69208SGreg Clayton } 16617f69208SGreg Clayton 16717f69208SGreg Clayton size_t 1687820bd1eSGreg Clayton SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp) 1697820bd1eSGreg Clayton { 1707820bd1eSGreg Clayton size_t unload_count = 0; 1717820bd1eSGreg Clayton 1727820bd1eSGreg Clayton if (section_sp) 17317f69208SGreg Clayton { 1745160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 17517f69208SGreg Clayton 17617f69208SGreg Clayton if (log) 1778b2fe6dcSGreg Clayton { 1787820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 179b5ad4ec7SGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s))", 18017f69208SGreg Clayton __FUNCTION__, 1817820bd1eSGreg Clayton section_sp.get(), 182b5ad4ec7SGreg Clayton module_file_spec.GetPath().c_str(), 1837820bd1eSGreg Clayton section_sp->GetName().AsCString()); 1848b2fe6dcSGreg Clayton } 18517f69208SGreg Clayton 18610177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 1876d093451SGreg Clayton 1887820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); 1896d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 19017f69208SGreg Clayton { 1913c94737fSGreg Clayton ++unload_count; 1926d093451SGreg Clayton addr_t load_addr = sta_pos->second; 1936d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 1946d093451SGreg Clayton 1956d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 1966d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 1976d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 19817f69208SGreg Clayton } 1997820bd1eSGreg Clayton } 20017f69208SGreg Clayton return unload_count; 20117f69208SGreg Clayton } 20217f69208SGreg Clayton 20317f69208SGreg Clayton bool 2047820bd1eSGreg Clayton SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp, addr_t load_addr) 20517f69208SGreg Clayton { 2065160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 20717f69208SGreg Clayton 20817f69208SGreg Clayton if (log) 2098b2fe6dcSGreg Clayton { 2107820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 211b5ad4ec7SGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ")", 21217f69208SGreg Clayton __FUNCTION__, 2137820bd1eSGreg Clayton section_sp.get(), 214b5ad4ec7SGreg Clayton module_file_spec.GetPath().c_str(), 2157820bd1eSGreg Clayton section_sp->GetName().AsCString(), 21617f69208SGreg Clayton load_addr); 2178b2fe6dcSGreg Clayton } 2186d093451SGreg Clayton bool erased = false; 21910177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 2207820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); 2216d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 2226d093451SGreg Clayton { 2236d093451SGreg Clayton erased = true; 2246d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 2256d093451SGreg Clayton } 2266d093451SGreg Clayton 2276d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 2286d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 2296d093451SGreg Clayton { 2306d093451SGreg Clayton erased = true; 2316d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 2326d093451SGreg Clayton } 2336d093451SGreg Clayton 2346d093451SGreg Clayton return erased; 23517f69208SGreg Clayton } 23617f69208SGreg Clayton 23717f69208SGreg Clayton 23817f69208SGreg Clayton bool 23917f69208SGreg Clayton SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const 24017f69208SGreg Clayton { 24117f69208SGreg Clayton // First find the top level section that this load address exists in 24210177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 243c5e9a7dcSGreg Clayton if (!m_addr_to_sect.empty()) 244c5e9a7dcSGreg Clayton { 2456d093451SGreg Clayton addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr); 2466d093451SGreg Clayton if (pos != m_addr_to_sect.end()) 24717f69208SGreg Clayton { 2486d093451SGreg Clayton if (load_addr != pos->first && pos != m_addr_to_sect.begin()) 24910177aa0SGreg Clayton --pos; 25039f7ee86SGreg Clayton const addr_t pos_load_addr = pos->first; 25139f7ee86SGreg Clayton if (load_addr >= pos_load_addr) 252f830e481SJason Molenda { 25339f7ee86SGreg Clayton addr_t offset = load_addr - pos_load_addr; 25410177aa0SGreg Clayton if (offset < pos->second->GetByteSize()) 25517f69208SGreg Clayton { 25617f69208SGreg Clayton // We have found the top level section, now we need to find the 25717f69208SGreg Clayton // deepest child section. 25810177aa0SGreg Clayton return pos->second->ResolveContainedAddress (offset, so_addr); 25917f69208SGreg Clayton } 26017f69208SGreg Clayton } 261f830e481SJason Molenda } 262c5e9a7dcSGreg Clayton else 263c5e9a7dcSGreg Clayton { 2641c870d6eSGreg Clayton // There are no entries that have an address that is >= load_addr, 2651c870d6eSGreg Clayton // so we need to check the last entry on our collection. 2661c870d6eSGreg Clayton addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin(); 2671c870d6eSGreg Clayton if (load_addr >= rpos->first) 268c5e9a7dcSGreg Clayton { 2691c870d6eSGreg Clayton addr_t offset = load_addr - rpos->first; 2701c870d6eSGreg Clayton if (offset < rpos->second->GetByteSize()) 271c5e9a7dcSGreg Clayton { 272c5e9a7dcSGreg Clayton // We have found the top level section, now we need to find the 273c5e9a7dcSGreg Clayton // deepest child section. 2741c870d6eSGreg Clayton return rpos->second->ResolveContainedAddress (offset, so_addr); 275c5e9a7dcSGreg Clayton } 276c5e9a7dcSGreg Clayton } 277c5e9a7dcSGreg Clayton } 278c5e9a7dcSGreg Clayton } 27917f69208SGreg Clayton so_addr.Clear(); 28017f69208SGreg Clayton return false; 28117f69208SGreg Clayton } 28210177aa0SGreg Clayton 28310177aa0SGreg Clayton void 28410177aa0SGreg Clayton SectionLoadList::Dump (Stream &s, Target *target) 28510177aa0SGreg Clayton { 28610177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 2876d093451SGreg Clayton addr_to_sect_collection::const_iterator pos, end; 2886d093451SGreg Clayton for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos) 28910177aa0SGreg Clayton { 290d01b2953SDaniel Malea s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, pos->second.get()); 29110177aa0SGreg Clayton pos->second->Dump (&s, target, 0); 29210177aa0SGreg Clayton } 29310177aa0SGreg Clayton } 29410177aa0SGreg Clayton 29510177aa0SGreg Clayton 296