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 { 625160ce5cSGreg Clayton Log *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*b5ad4ec7SGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ")", 6817f69208SGreg Clayton __FUNCTION__, 697820bd1eSGreg Clayton section.get(), 70*b5ad4ec7SGreg Clayton module_file_spec.GetPath().c_str(), 7117f69208SGreg Clayton section->GetName().AsCString(), 7217f69208SGreg Clayton load_addr); 738b2fe6dcSGreg Clayton } 7417f69208SGreg Clayton 75c859e2d5SGreg Clayton if (section->GetByteSize() == 0) 76c859e2d5SGreg Clayton return false; // No change 77c859e2d5SGreg Clayton 781e8ac36bSGreg Clayton // Fill in the section -> load_addr map 7910177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 807820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get()); 816d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 8217f69208SGreg Clayton { 836d093451SGreg Clayton if (load_addr == sta_pos->second) 8410177aa0SGreg Clayton return false; // No change... 8510177aa0SGreg Clayton else 866d093451SGreg Clayton sta_pos->second = load_addr; 8717f69208SGreg Clayton } 8810177aa0SGreg Clayton else 897820bd1eSGreg Clayton m_sect_to_addr[section.get()] = load_addr; 906d093451SGreg Clayton 911e8ac36bSGreg Clayton // Fill in the load_addr -> section map 926d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 936d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 9410177aa0SGreg Clayton { 951e8ac36bSGreg Clayton // Some sections are ok to overlap, and for others we should warn. When 961e8ac36bSGreg Clayton // we have multiple load addresses that correspond to a section, we will 971e8ac36bSGreg Clayton // allways attribute the section to the be last section that claims it 981e8ac36bSGreg Clayton // exists at that address. Sometimes it is ok for more that one section 991e8ac36bSGreg Clayton // to be loaded at a specific load address, and other times it isn't. 1001e8ac36bSGreg Clayton // The "warn_multiple" parameter tells us if we should warn in this case 1011e8ac36bSGreg Clayton // or not. The DynamicLoader plug-in subclasses should know which 1021e8ac36bSGreg Clayton // sections should warn and which shouldn't (darwin shared cache modules 1031e8ac36bSGreg Clayton // all shared the same "__LINKEDIT" sections, so the dynamic loader can 1041e8ac36bSGreg Clayton // pass false for "warn_multiple"). 1051e8ac36bSGreg Clayton if (warn_multiple && section != ats_pos->second) 106c859e2d5SGreg Clayton { 107e72dfb32SGreg Clayton ModuleSP module_sp (section->GetModule()); 108e72dfb32SGreg Clayton if (module_sp) 109c859e2d5SGreg Clayton { 1109d192e1aSGreg Clayton ModuleSP curr_module_sp (ats_pos->second->GetModule()); 1119d192e1aSGreg Clayton if (curr_module_sp) 1129d192e1aSGreg Clayton { 113d01b2953SDaniel Malea module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s", 114c859e2d5SGreg Clayton load_addr, 115e72dfb32SGreg Clayton module_sp->GetFileSpec().GetFilename().GetCString(), 116c859e2d5SGreg Clayton section->GetName().GetCString(), 1179d192e1aSGreg Clayton curr_module_sp->GetFileSpec().GetFilename().GetCString(), 118c859e2d5SGreg Clayton ats_pos->second->GetName().GetCString()); 119c859e2d5SGreg Clayton } 120c859e2d5SGreg Clayton } 1219d192e1aSGreg Clayton } 1226d093451SGreg Clayton ats_pos->second = section; 12310177aa0SGreg Clayton } 1246d093451SGreg Clayton else 1256d093451SGreg Clayton m_addr_to_sect[load_addr] = section; 1266d093451SGreg Clayton 12717f69208SGreg Clayton return true; // Changed 12817f69208SGreg Clayton } 12917f69208SGreg Clayton 13017f69208SGreg Clayton size_t 1317820bd1eSGreg Clayton SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp) 1327820bd1eSGreg Clayton { 1337820bd1eSGreg Clayton size_t unload_count = 0; 1347820bd1eSGreg Clayton 1357820bd1eSGreg Clayton if (section_sp) 13617f69208SGreg Clayton { 1375160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 13817f69208SGreg Clayton 13917f69208SGreg Clayton if (log) 1408b2fe6dcSGreg Clayton { 1417820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 142*b5ad4ec7SGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s))", 14317f69208SGreg Clayton __FUNCTION__, 1447820bd1eSGreg Clayton section_sp.get(), 145*b5ad4ec7SGreg Clayton module_file_spec.GetPath().c_str(), 1467820bd1eSGreg Clayton section_sp->GetName().AsCString()); 1478b2fe6dcSGreg Clayton } 14817f69208SGreg Clayton 14910177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 1506d093451SGreg Clayton 1517820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); 1526d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 15317f69208SGreg Clayton { 1543c94737fSGreg Clayton ++unload_count; 1556d093451SGreg Clayton addr_t load_addr = sta_pos->second; 1566d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 1576d093451SGreg Clayton 1586d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 1596d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 1606d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 16117f69208SGreg Clayton } 1627820bd1eSGreg Clayton } 16317f69208SGreg Clayton return unload_count; 16417f69208SGreg Clayton } 16517f69208SGreg Clayton 16617f69208SGreg Clayton bool 1677820bd1eSGreg Clayton SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp, addr_t load_addr) 16817f69208SGreg Clayton { 1695160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE)); 17017f69208SGreg Clayton 17117f69208SGreg Clayton if (log) 1728b2fe6dcSGreg Clayton { 1737820bd1eSGreg Clayton const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec()); 174*b5ad4ec7SGreg Clayton log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ")", 17517f69208SGreg Clayton __FUNCTION__, 1767820bd1eSGreg Clayton section_sp.get(), 177*b5ad4ec7SGreg Clayton module_file_spec.GetPath().c_str(), 1787820bd1eSGreg Clayton section_sp->GetName().AsCString(), 17917f69208SGreg Clayton load_addr); 1808b2fe6dcSGreg Clayton } 1816d093451SGreg Clayton bool erased = false; 18210177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 1837820bd1eSGreg Clayton sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get()); 1846d093451SGreg Clayton if (sta_pos != m_sect_to_addr.end()) 1856d093451SGreg Clayton { 1866d093451SGreg Clayton erased = true; 1876d093451SGreg Clayton m_sect_to_addr.erase (sta_pos); 1886d093451SGreg Clayton } 1896d093451SGreg Clayton 1906d093451SGreg Clayton addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr); 1916d093451SGreg Clayton if (ats_pos != m_addr_to_sect.end()) 1926d093451SGreg Clayton { 1936d093451SGreg Clayton erased = true; 1946d093451SGreg Clayton m_addr_to_sect.erase (ats_pos); 1956d093451SGreg Clayton } 1966d093451SGreg Clayton 1976d093451SGreg Clayton return erased; 19817f69208SGreg Clayton } 19917f69208SGreg Clayton 20017f69208SGreg Clayton 20117f69208SGreg Clayton bool 20217f69208SGreg Clayton SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const 20317f69208SGreg Clayton { 20417f69208SGreg Clayton // First find the top level section that this load address exists in 20510177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 206c5e9a7dcSGreg Clayton if (!m_addr_to_sect.empty()) 207c5e9a7dcSGreg Clayton { 2086d093451SGreg Clayton addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr); 2096d093451SGreg Clayton if (pos != m_addr_to_sect.end()) 21017f69208SGreg Clayton { 2116d093451SGreg Clayton if (load_addr != pos->first && pos != m_addr_to_sect.begin()) 21210177aa0SGreg Clayton --pos; 21339f7ee86SGreg Clayton const addr_t pos_load_addr = pos->first; 21439f7ee86SGreg Clayton if (load_addr >= pos_load_addr) 215f830e481SJason Molenda { 21639f7ee86SGreg Clayton addr_t offset = load_addr - pos_load_addr; 21710177aa0SGreg Clayton if (offset < pos->second->GetByteSize()) 21817f69208SGreg Clayton { 21917f69208SGreg Clayton // We have found the top level section, now we need to find the 22017f69208SGreg Clayton // deepest child section. 22110177aa0SGreg Clayton return pos->second->ResolveContainedAddress (offset, so_addr); 22217f69208SGreg Clayton } 22317f69208SGreg Clayton } 224f830e481SJason Molenda } 225c5e9a7dcSGreg Clayton else 226c5e9a7dcSGreg Clayton { 2271c870d6eSGreg Clayton // There are no entries that have an address that is >= load_addr, 2281c870d6eSGreg Clayton // so we need to check the last entry on our collection. 2291c870d6eSGreg Clayton addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin(); 2301c870d6eSGreg Clayton if (load_addr >= rpos->first) 231c5e9a7dcSGreg Clayton { 2321c870d6eSGreg Clayton addr_t offset = load_addr - rpos->first; 2331c870d6eSGreg Clayton if (offset < rpos->second->GetByteSize()) 234c5e9a7dcSGreg Clayton { 235c5e9a7dcSGreg Clayton // We have found the top level section, now we need to find the 236c5e9a7dcSGreg Clayton // deepest child section. 2371c870d6eSGreg Clayton return rpos->second->ResolveContainedAddress (offset, so_addr); 238c5e9a7dcSGreg Clayton } 239c5e9a7dcSGreg Clayton } 240c5e9a7dcSGreg Clayton } 241c5e9a7dcSGreg Clayton } 24217f69208SGreg Clayton so_addr.Clear(); 24317f69208SGreg Clayton return false; 24417f69208SGreg Clayton } 24510177aa0SGreg Clayton 24610177aa0SGreg Clayton void 24710177aa0SGreg Clayton SectionLoadList::Dump (Stream &s, Target *target) 24810177aa0SGreg Clayton { 24910177aa0SGreg Clayton Mutex::Locker locker(m_mutex); 2506d093451SGreg Clayton addr_to_sect_collection::const_iterator pos, end; 2516d093451SGreg Clayton for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos) 25210177aa0SGreg Clayton { 253d01b2953SDaniel Malea s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, pos->second.get()); 25410177aa0SGreg Clayton pos->second->Dump (&s, target, 0); 25510177aa0SGreg Clayton } 25610177aa0SGreg Clayton } 25710177aa0SGreg Clayton 25810177aa0SGreg Clayton 259