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
4417f69208SGreg Clayton SectionLoadList::GetSectionLoadAddress (const Section *section) 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);
516d093451SGreg Clayton         sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section);
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
6017f69208SGreg Clayton SectionLoadList::SetSectionLoadAddress (const Section *section, addr_t load_addr)
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());
678b2fe6dcSGreg Clayton         log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)",
6817f69208SGreg Clayton                      __FUNCTION__,
6917f69208SGreg Clayton                      section,
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 
8010177aa0SGreg Clayton     Mutex::Locker locker(m_mutex);
816d093451SGreg Clayton     sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
826d093451SGreg Clayton     if (sta_pos != m_sect_to_addr.end())
8317f69208SGreg Clayton     {
846d093451SGreg Clayton         if (load_addr == sta_pos->second)
8510177aa0SGreg Clayton             return false; // No change...
8610177aa0SGreg Clayton         else
876d093451SGreg Clayton             sta_pos->second = load_addr;
8817f69208SGreg Clayton     }
8910177aa0SGreg Clayton     else
906d093451SGreg Clayton         m_sect_to_addr[section] = load_addr;
916d093451SGreg Clayton 
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     {
95c859e2d5SGreg Clayton         if (section != ats_pos->second)
96c859e2d5SGreg Clayton         {
97*e72dfb32SGreg Clayton             ModuleSP module_sp (section->GetModule());
98*e72dfb32SGreg Clayton             if (module_sp)
99c859e2d5SGreg Clayton             {
100*e72dfb32SGreg Clayton                 module_sp->ReportWarning ("address 0x%16.16llx maps to more than one section: %s.%s and %s.%s",
101c859e2d5SGreg Clayton                                           load_addr,
102*e72dfb32SGreg Clayton                                           module_sp->GetFileSpec().GetFilename().GetCString(),
103c859e2d5SGreg Clayton                                           section->GetName().GetCString(),
104c859e2d5SGreg Clayton                                           ats_pos->second->GetModule()->GetFileSpec().GetFilename().GetCString(),
105c859e2d5SGreg Clayton                                           ats_pos->second->GetName().GetCString());
106c859e2d5SGreg Clayton             }
107c859e2d5SGreg Clayton         }
1086d093451SGreg Clayton         ats_pos->second = section;
10910177aa0SGreg Clayton     }
1106d093451SGreg Clayton     else
1116d093451SGreg Clayton         m_addr_to_sect[load_addr] = section;
1126d093451SGreg Clayton 
11317f69208SGreg Clayton     return true;    // Changed
11417f69208SGreg Clayton }
11517f69208SGreg Clayton 
11617f69208SGreg Clayton size_t
11717f69208SGreg Clayton SectionLoadList::SetSectionUnloaded (const Section *section)
11817f69208SGreg Clayton {
1192d4edfbcSGreg Clayton     LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
12017f69208SGreg Clayton 
12117f69208SGreg Clayton     if (log)
1228b2fe6dcSGreg Clayton     {
1238b2fe6dcSGreg Clayton         const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
1248b2fe6dcSGreg Clayton         log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))",
12517f69208SGreg Clayton                      __FUNCTION__,
12617f69208SGreg Clayton                      section,
1278b2fe6dcSGreg Clayton                      module_file_spec.GetDirectory().AsCString(),
1288b2fe6dcSGreg Clayton                      module_file_spec.GetDirectory() ? "/" : "",
1298b2fe6dcSGreg Clayton                      module_file_spec.GetFilename().AsCString(),
13017f69208SGreg Clayton                      section->GetName().AsCString());
1318b2fe6dcSGreg Clayton     }
13217f69208SGreg Clayton 
13317f69208SGreg Clayton     size_t unload_count = 0;
13410177aa0SGreg Clayton     Mutex::Locker locker(m_mutex);
1356d093451SGreg Clayton 
1366d093451SGreg Clayton     sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
1376d093451SGreg Clayton     if (sta_pos != m_sect_to_addr.end())
13817f69208SGreg Clayton     {
1396d093451SGreg Clayton         addr_t load_addr = sta_pos->second;
1406d093451SGreg Clayton         m_sect_to_addr.erase (sta_pos);
1416d093451SGreg Clayton 
1426d093451SGreg Clayton         addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
1436d093451SGreg Clayton         if (ats_pos != m_addr_to_sect.end())
1446d093451SGreg Clayton             m_addr_to_sect.erase (ats_pos);
14517f69208SGreg Clayton     }
14610177aa0SGreg Clayton 
14717f69208SGreg Clayton     return unload_count;
14817f69208SGreg Clayton }
14917f69208SGreg Clayton 
15017f69208SGreg Clayton bool
15117f69208SGreg Clayton SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr)
15217f69208SGreg Clayton {
1532d4edfbcSGreg Clayton     LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
15417f69208SGreg Clayton 
15517f69208SGreg Clayton     if (log)
1568b2fe6dcSGreg Clayton     {
1578b2fe6dcSGreg Clayton         const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
1588b2fe6dcSGreg Clayton         log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)",
15917f69208SGreg Clayton                      __FUNCTION__,
16017f69208SGreg Clayton                      section,
1618b2fe6dcSGreg Clayton                      module_file_spec.GetDirectory().AsCString(),
1628b2fe6dcSGreg Clayton                      module_file_spec.GetDirectory() ? "/" : "",
1638b2fe6dcSGreg Clayton                      module_file_spec.GetFilename().AsCString(),
16417f69208SGreg Clayton                      section->GetName().AsCString(),
16517f69208SGreg Clayton                      load_addr);
1668b2fe6dcSGreg Clayton     }
1676d093451SGreg Clayton     bool erased = false;
16810177aa0SGreg Clayton     Mutex::Locker locker(m_mutex);
1696d093451SGreg Clayton     sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
1706d093451SGreg Clayton     if (sta_pos != m_sect_to_addr.end())
1716d093451SGreg Clayton     {
1726d093451SGreg Clayton         erased = true;
1736d093451SGreg Clayton         m_sect_to_addr.erase (sta_pos);
1746d093451SGreg Clayton     }
1756d093451SGreg Clayton 
1766d093451SGreg Clayton     addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
1776d093451SGreg Clayton     if (ats_pos != m_addr_to_sect.end())
1786d093451SGreg Clayton     {
1796d093451SGreg Clayton         erased = true;
1806d093451SGreg Clayton         m_addr_to_sect.erase (ats_pos);
1816d093451SGreg Clayton     }
1826d093451SGreg Clayton 
1836d093451SGreg Clayton     return erased;
18417f69208SGreg Clayton }
18517f69208SGreg Clayton 
18617f69208SGreg Clayton 
18717f69208SGreg Clayton bool
18817f69208SGreg Clayton SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
18917f69208SGreg Clayton {
19017f69208SGreg Clayton     // First find the top level section that this load address exists in
19110177aa0SGreg Clayton     Mutex::Locker locker(m_mutex);
192c5e9a7dcSGreg Clayton     if (!m_addr_to_sect.empty())
193c5e9a7dcSGreg Clayton     {
1946d093451SGreg Clayton         addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
1956d093451SGreg Clayton         if (pos != m_addr_to_sect.end())
19617f69208SGreg Clayton         {
1976d093451SGreg Clayton             if (load_addr != pos->first && pos != m_addr_to_sect.begin())
19810177aa0SGreg Clayton                 --pos;
199f830e481SJason Molenda             if (load_addr >= pos->first)
200f830e481SJason Molenda             {
20110177aa0SGreg Clayton                 addr_t offset = load_addr - pos->first;
20210177aa0SGreg Clayton                 if (offset < pos->second->GetByteSize())
20317f69208SGreg Clayton                 {
20417f69208SGreg Clayton                     // We have found the top level section, now we need to find the
20517f69208SGreg Clayton                     // deepest child section.
20610177aa0SGreg Clayton                     return pos->second->ResolveContainedAddress (offset, so_addr);
20717f69208SGreg Clayton                 }
20817f69208SGreg Clayton             }
209f830e481SJason Molenda         }
210c5e9a7dcSGreg Clayton         else
211c5e9a7dcSGreg Clayton         {
2121c870d6eSGreg Clayton             // There are no entries that have an address that is >= load_addr,
2131c870d6eSGreg Clayton             // so we need to check the last entry on our collection.
2141c870d6eSGreg Clayton             addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin();
2151c870d6eSGreg Clayton             if (load_addr >= rpos->first)
216c5e9a7dcSGreg Clayton             {
2171c870d6eSGreg Clayton                 addr_t offset = load_addr - rpos->first;
2181c870d6eSGreg Clayton                 if (offset < rpos->second->GetByteSize())
219c5e9a7dcSGreg Clayton                 {
220c5e9a7dcSGreg Clayton                     // We have found the top level section, now we need to find the
221c5e9a7dcSGreg Clayton                     // deepest child section.
2221c870d6eSGreg Clayton                     return rpos->second->ResolveContainedAddress (offset, so_addr);
223c5e9a7dcSGreg Clayton                 }
224c5e9a7dcSGreg Clayton             }
225c5e9a7dcSGreg Clayton         }
226c5e9a7dcSGreg Clayton     }
22717f69208SGreg Clayton     so_addr.Clear();
22817f69208SGreg Clayton     return false;
22917f69208SGreg Clayton }
23010177aa0SGreg Clayton 
23110177aa0SGreg Clayton void
23210177aa0SGreg Clayton SectionLoadList::Dump (Stream &s, Target *target)
23310177aa0SGreg Clayton {
23410177aa0SGreg Clayton     Mutex::Locker locker(m_mutex);
2356d093451SGreg Clayton     addr_to_sect_collection::const_iterator pos, end;
2366d093451SGreg Clayton     for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
23710177aa0SGreg Clayton     {
23810177aa0SGreg Clayton         s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second);
23910177aa0SGreg Clayton         pos->second->Dump (&s, target, 0);
24010177aa0SGreg Clayton     }
24110177aa0SGreg Clayton }
24210177aa0SGreg Clayton 
24310177aa0SGreg Clayton 
244