1 //===-- SectionLoadList.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/SectionLoadList.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/Section.h"
19 #include "lldb/Core/Stream.h"
20 #include "lldb/Symbol/Block.h"
21 #include "lldb/Symbol/Symbol.h"
22 #include "lldb/Symbol/SymbolContext.h"
25 using namespace lldb_private;
29 SectionLoadList::IsEmpty() const
31 Mutex::Locker locker(m_mutex);
32 return m_addr_to_sect.empty();
36 SectionLoadList::Clear ()
38 Mutex::Locker locker(m_mutex);
39 m_addr_to_sect.clear();
40 m_sect_to_addr.clear();
44 SectionLoadList::GetSectionLoadAddress (const lldb::SectionSP §ion) const
46 // TODO: add support for the same section having multiple load addresses
47 addr_t section_load_addr = LLDB_INVALID_ADDRESS;
50 Mutex::Locker locker(m_mutex);
51 sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section.get());
53 if (pos != m_sect_to_addr.end())
54 section_load_addr = pos->second;
56 return section_load_addr;
60 SectionLoadList::SetSectionLoadAddress (const lldb::SectionSP §ion, addr_t load_addr, bool warn_multiple)
62 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
64 ModuleSP module_sp (section->GetModule());
70 const FileSpec &module_file_spec (module_sp->GetFileSpec());
71 log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ") module = %p",
74 module_file_spec.GetPath().c_str(),
75 section->GetName().AsCString(),
80 if (section->GetByteSize() == 0)
81 return false; // No change
83 // Fill in the section -> load_addr map
84 Mutex::Locker locker(m_mutex);
85 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get());
86 if (sta_pos != m_sect_to_addr.end())
88 if (load_addr == sta_pos->second)
89 return false; // No change...
91 sta_pos->second = load_addr;
94 m_sect_to_addr[section.get()] = load_addr;
96 // Fill in the load_addr -> section map
97 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
98 if (ats_pos != m_addr_to_sect.end())
100 // Some sections are ok to overlap, and for others we should warn. When
101 // we have multiple load addresses that correspond to a section, we will
102 // allways attribute the section to the be last section that claims it
103 // exists at that address. Sometimes it is ok for more that one section
104 // to be loaded at a specific load address, and other times it isn't.
105 // The "warn_multiple" parameter tells us if we should warn in this case
106 // or not. The DynamicLoader plug-in subclasses should know which
107 // sections should warn and which shouldn't (darwin shared cache modules
108 // all shared the same "__LINKEDIT" sections, so the dynamic loader can
109 // pass false for "warn_multiple").
110 if (warn_multiple && section != ats_pos->second)
112 ModuleSP module_sp (section->GetModule());
115 ModuleSP curr_module_sp (ats_pos->second->GetModule());
118 module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s",
120 module_sp->GetFileSpec().GetFilename().GetCString(),
121 section->GetName().GetCString(),
122 curr_module_sp->GetFileSpec().GetFilename().GetCString(),
123 ats_pos->second->GetName().GetCString());
127 ats_pos->second = section;
130 m_addr_to_sect[load_addr] = section;
131 return true; // Changed
138 log->Printf ("SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64 ") error: module has been deleted",
141 section->GetName().AsCString(),
149 SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp)
151 size_t unload_count = 0;
155 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
159 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
160 log->Printf ("SectionLoadList::%s (section = %p (%s.%s))",
163 module_file_spec.GetPath().c_str(),
164 section_sp->GetName().AsCString());
167 Mutex::Locker locker(m_mutex);
169 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
170 if (sta_pos != m_sect_to_addr.end())
173 addr_t load_addr = sta_pos->second;
174 m_sect_to_addr.erase (sta_pos);
176 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
177 if (ats_pos != m_addr_to_sect.end())
178 m_addr_to_sect.erase (ats_pos);
185 SectionLoadList::SetSectionUnloaded (const lldb::SectionSP §ion_sp, addr_t load_addr)
187 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
191 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
192 log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ")",
195 module_file_spec.GetPath().c_str(),
196 section_sp->GetName().AsCString(),
200 Mutex::Locker locker(m_mutex);
201 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
202 if (sta_pos != m_sect_to_addr.end())
205 m_sect_to_addr.erase (sta_pos);
208 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
209 if (ats_pos != m_addr_to_sect.end())
212 m_addr_to_sect.erase (ats_pos);
220 SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
222 // First find the top level section that this load address exists in
223 Mutex::Locker locker(m_mutex);
224 if (!m_addr_to_sect.empty())
226 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
227 if (pos != m_addr_to_sect.end())
229 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
231 const addr_t pos_load_addr = pos->first;
232 if (load_addr >= pos_load_addr)
234 addr_t offset = load_addr - pos_load_addr;
235 if (offset < pos->second->GetByteSize())
237 // We have found the top level section, now we need to find the
238 // deepest child section.
239 return pos->second->ResolveContainedAddress (offset, so_addr);
245 // There are no entries that have an address that is >= load_addr,
246 // so we need to check the last entry on our collection.
247 addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin();
248 if (load_addr >= rpos->first)
250 addr_t offset = load_addr - rpos->first;
251 if (offset < rpos->second->GetByteSize())
253 // We have found the top level section, now we need to find the
254 // deepest child section.
255 return rpos->second->ResolveContainedAddress (offset, so_addr);
265 SectionLoadList::Dump (Stream &s, Target *target)
267 Mutex::Locker locker(m_mutex);
268 addr_to_sect_collection::const_iterator pos, end;
269 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
271 s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, pos->second.get());
272 pos->second->Dump (&s, target, 0);