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 2817f69208SGreg Clayton bool 2917f69208SGreg Clayton SectionLoadList::IsEmpty() const 3017f69208SGreg Clayton { 3110177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 326d093451SGreg Clayton return m_addr_to_sect.empty(); 3317f69208SGreg Clayton } 3417f69208SGreg Clayton 3517f69208SGreg Clayton void 3617f69208SGreg Clayton SectionLoadList::Clear () 3717f69208SGreg Clayton { 3810177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 396d093451SGreg Clayton m_addr_to_sect.clear(); 406d093451SGreg Clayton m_sect_to_addr.clear(); 4117f69208SGreg Clayton } 4217f69208SGreg Clayton 4317f69208SGreg Clayton addr_t 447820bd1eSGreg Clayton SectionLoadList::GetSectionLoadAddress (const lldb::SectionSP §ion) const 4517f69208SGreg Clayton { 4617f69208SGreg Clayton // TODO: add support for the same section having multiple load addresses 4717f69208SGreg Clayton addr_t section_load_addr = LLDB_INVALID_ADDRESS; 4810177aa0SGreg Clayton if (section) 4910177aa0SGreg Clayton { 5010177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 517820bd1eSGreg Clayton sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section.get()); 526d093451SGreg Clayton 536d093451SGreg Clayton if (pos != m_sect_to_addr.end()) 546d093451SGreg Clayton section_load_addr = pos->second; 5510177aa0SGreg Clayton } 5617f69208SGreg Clayton return section_load_addr; 5717f69208SGreg Clayton } 5817f69208SGreg Clayton 5917f69208SGreg Clayton bool 607820bd1eSGreg Clayton SectionLoadList::SetSectionLoadAddress (const lldb::SectionSP §ion, addr_t load_addr, bool warn_multiple) 6117f69208SGreg Clayton { 622d4edfbcSGreg Clayton LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 6317f69208SGreg Clayton 6417f69208SGreg Clayton if (log) 658b2fe6dcSGreg Clayton { 668b2fe6dcSGreg Clayton const FileSpec &module_file_spec (section->GetModule()->GetFileSpec()); 67*d01b2953SDaniel Malea log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16" PRIx64 ")", 6817f69208SGreg Clayton __FUNCTION__, 697820bd1eSGreg Clayton section.get(), 708b2fe6dcSGreg Clayton module_file_spec.GetDirectory().AsCString(), 718b2fe6dcSGreg Clayton module_file_spec.GetDirectory() ? "/" : "", 728b2fe6dcSGreg Clayton module_file_spec.GetFilename().AsCString(), 7317f69208SGreg Clayton section->GetName().AsCString(), 7417f69208SGreg Clayton load_addr); 758b2fe6dcSGreg Clayton } 7617f69208SGreg Clayton 77c859e2d5SGreg Clayton if (section->GetByteSize() == 0) 78c859e2d5SGreg Clayton return false; // No change 79c859e2d5SGreg Clayton 801e8ac36bSGreg Clayton // Fill in the section -> load_addr map 8110177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 827820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get()); 836d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 8417f69208SGreg Clayton { 856d093451SGreg Clayton if (load_addr == sta_pos->second) 8610177aa0SGreg Clayton return false; // No change... 8710177aa0SGreg Clayton else 886d093451SGreg Clayton sta_pos->second = load_addr; 8917f69208SGreg Clayton } 9010177aa0SGreg Clayton else 917820bd1eSGreg Clayton m_sect_to_addr[section.get()] = load_addr; 926d093451SGreg Clayton 931e8ac36bSGreg Clayton // Fill in the load_addr -> section map 946d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 956d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 9610177aa0SGreg Clayton { 971e8ac36bSGreg Clayton // Some sections are ok to overlap, and for others we should warn. When 981e8ac36bSGreg Clayton // we have multiple load addresses that correspond to a section, we will 991e8ac36bSGreg Clayton // allways attribute the section to the be last section that claims it 1001e8ac36bSGreg Clayton // exists at that address. Sometimes it is ok for more that one section 1011e8ac36bSGreg Clayton // to be loaded at a specific load address, and other times it isn't. 1021e8ac36bSGreg Clayton // The "warn_multiple" parameter tells us if we should warn in this case 1031e8ac36bSGreg Clayton // or not. The DynamicLoader plug-in subclasses should know which 1041e8ac36bSGreg Clayton // sections should warn and which shouldn't (darwin shared cache modules 1051e8ac36bSGreg Clayton // all shared the same "__LINKEDIT" sections, so the dynamic loader can 1061e8ac36bSGreg Clayton // pass false for "warn_multiple"). 1071e8ac36bSGreg Clayton if (warn_multiple && section != ats_pos->second) 108c859e2d5SGreg Clayton { 109e72dfb32SGreg Clayton ModuleSP module_sp (section->GetModule()); 110e72dfb32SGreg Clayton if (module_sp) 111c859e2d5SGreg Clayton { 1129d192e1aSGreg Clayton ModuleSP curr_module_sp (ats_pos->second->GetModule()); 1139d192e1aSGreg Clayton if (curr_module_sp) 1149d192e1aSGreg Clayton { 115*d01b2953SDaniel Malea module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s", 116c859e2d5SGreg Clayton load_addr, 117e72dfb32SGreg Clayton module_sp->GetFileSpec().GetFilename().GetCString(), 118c859e2d5SGreg Clayton section->GetName().GetCString(), 1199d192e1aSGreg Clayton curr_module_sp->GetFileSpec().GetFilename().GetCString(), 120c859e2d5SGreg Clayton ats_pos->second->GetName().GetCString()); 121c859e2d5SGreg Clayton } 122c859e2d5SGreg Clayton } 1239d192e1aSGreg Clayton } 1246d093451SGreg Clayton ats_pos->second = section; 12510177aa0SGreg Clayton } 1266d093451SGreg Clayton else 1276d093451SGreg Clayton m_addr_to_sect[load_addr] = section; 1286d093451SGreg Clayton 12917f69208SGreg Clayton return true; // Changed 13017f69208SGreg Clayton } 13117f69208SGreg Clayton 13217f69208SGreg Clayton size_t 1337820bd1eSGreg Clayton SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp) 1347820bd1eSGreg Clayton { 1357820bd1eSGreg Clayton size_t unload_count = 0; 1367820bd1eSGreg Clayton 1377820bd1eSGreg Clayton if (section_sp) 13817f69208SGreg Clayton { 1392d4edfbcSGreg Clayton LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 14017f69208SGreg Clayton 14117f69208SGreg Clayton if (log) 1428b2fe6dcSGreg Clayton { 1437820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 1448b2fe6dcSGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))", 14517f69208SGreg Clayton __FUNCTION__, 1467820bd1eSGreg Clayton section_sp.get(), 1478b2fe6dcSGreg Clayton module_file_spec.GetDirectory().AsCString(), 1488b2fe6dcSGreg Clayton module_file_spec.GetDirectory() ? "/" : "", 1498b2fe6dcSGreg Clayton module_file_spec.GetFilename().AsCString(), 1507820bd1eSGreg Clayton section_sp->GetName().AsCString()); 1518b2fe6dcSGreg Clayton } 15217f69208SGreg Clayton 15310177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 1546d093451SGreg Clayton 1557820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); 1566d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 15717f69208SGreg Clayton { 1586d093451SGreg Clayton addr_t load_addr = sta_pos->second; 1596d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 1606d093451SGreg Clayton 1616d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 1626d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 1636d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 16417f69208SGreg Clayton } 1657820bd1eSGreg Clayton } 16617f69208SGreg Clayton return unload_count; 16717f69208SGreg Clayton } 16817f69208SGreg Clayton 16917f69208SGreg Clayton bool 1707820bd1eSGreg Clayton SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp, addr_t load_addr) 17117f69208SGreg Clayton { 1722d4edfbcSGreg Clayton LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 17317f69208SGreg Clayton 17417f69208SGreg Clayton if (log) 1758b2fe6dcSGreg Clayton { 1767820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 177*d01b2953SDaniel Malea log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16" PRIx64 ")", 17817f69208SGreg Clayton __FUNCTION__, 1797820bd1eSGreg Clayton section_sp.get(), 1808b2fe6dcSGreg Clayton module_file_spec.GetDirectory().AsCString(), 1818b2fe6dcSGreg Clayton module_file_spec.GetDirectory() ? "/" : "", 1828b2fe6dcSGreg Clayton module_file_spec.GetFilename().AsCString(), 1837820bd1eSGreg Clayton section_sp->GetName().AsCString(), 18417f69208SGreg Clayton load_addr); 1858b2fe6dcSGreg Clayton } 1866d093451SGreg Clayton bool erased = false; 18710177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 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()) 1906d093451SGreg Clayton { 1916d093451SGreg Clayton erased = true; 1926d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 1936d093451SGreg Clayton } 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 { 1986d093451SGreg Clayton erased = true; 1996d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 2006d093451SGreg Clayton } 2016d093451SGreg Clayton 2026d093451SGreg Clayton return erased; 20317f69208SGreg Clayton } 20417f69208SGreg Clayton 20517f69208SGreg Clayton 20617f69208SGreg Clayton bool 20717f69208SGreg Clayton SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const 20817f69208SGreg Clayton { 20917f69208SGreg Clayton // First find the top level section that this load address exists in 21010177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 211c5e9a7dcSGreg Clayton if (!m_addr_to_sect.empty()) 212c5e9a7dcSGreg Clayton { 2136d093451SGreg Clayton addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr); 2146d093451SGreg Clayton if (pos != m_addr_to_sect.end()) 21517f69208SGreg Clayton { 2166d093451SGreg Clayton if (load_addr != pos->first && pos != m_addr_to_sect.begin()) 21710177aa0SGreg Clayton --pos; 218f830e481SJason Molenda if (load_addr >= pos->first) 219f830e481SJason Molenda { 22010177aa0SGreg Clayton addr_t offset = load_addr - pos->first; 22110177aa0SGreg Clayton if (offset < pos->second->GetByteSize()) 22217f69208SGreg Clayton { 22317f69208SGreg Clayton // We have found the top level section, now we need to find the 22417f69208SGreg Clayton // deepest child section. 22510177aa0SGreg Clayton return pos->second->ResolveContainedAddress (offset, so_addr); 22617f69208SGreg Clayton } 22717f69208SGreg Clayton } 228f830e481SJason Molenda } 229c5e9a7dcSGreg Clayton else 230c5e9a7dcSGreg Clayton { 2311c870d6eSGreg Clayton // There are no entries that have an address that is >= load_addr, 2321c870d6eSGreg Clayton // so we need to check the last entry on our collection. 2331c870d6eSGreg Clayton addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin(); 2341c870d6eSGreg Clayton if (load_addr >= rpos->first) 235c5e9a7dcSGreg Clayton { 2361c870d6eSGreg Clayton addr_t offset = load_addr - rpos->first; 2371c870d6eSGreg Clayton if (offset < rpos->second->GetByteSize()) 238c5e9a7dcSGreg Clayton { 239c5e9a7dcSGreg Clayton // We have found the top level section, now we need to find the 240c5e9a7dcSGreg Clayton // deepest child section. 2411c870d6eSGreg Clayton return rpos->second->ResolveContainedAddress (offset, so_addr); 242c5e9a7dcSGreg Clayton } 243c5e9a7dcSGreg Clayton } 244c5e9a7dcSGreg Clayton } 245c5e9a7dcSGreg Clayton } 24617f69208SGreg Clayton so_addr.Clear(); 24717f69208SGreg Clayton return false; 24817f69208SGreg Clayton } 24910177aa0SGreg Clayton 25010177aa0SGreg Clayton void 25110177aa0SGreg Clayton SectionLoadList::Dump (Stream &s, Target *target) 25210177aa0SGreg Clayton { 25310177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 2546d093451SGreg Clayton addr_to_sect_collection::const_iterator pos, end; 2556d093451SGreg Clayton for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos) 25610177aa0SGreg Clayton { 257*d01b2953SDaniel Malea s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, pos->second.get()); 25810177aa0SGreg Clayton pos->second->Dump (&s, target, 0); 25910177aa0SGreg Clayton } 26010177aa0SGreg Clayton } 26110177aa0SGreg Clayton 26210177aa0SGreg Clayton 263