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/Module.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Symbol/Block.h"
19 #include "lldb/Symbol/Symbol.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Utility/Log.h"
22 #include "lldb/Utility/Stream.h"
25 using namespace lldb_private;
27 SectionLoadList::SectionLoadList(const SectionLoadList &rhs)
28 : m_addr_to_sect(), m_sect_to_addr(), m_mutex() {
29 std::lock_guard<std::recursive_mutex> guard(rhs.m_mutex);
30 m_addr_to_sect = rhs.m_addr_to_sect;
31 m_sect_to_addr = rhs.m_sect_to_addr;
34 void SectionLoadList::operator=(const SectionLoadList &rhs) {
35 std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex);
36 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex);
37 m_addr_to_sect = rhs.m_addr_to_sect;
38 m_sect_to_addr = rhs.m_sect_to_addr;
41 bool SectionLoadList::IsEmpty() const {
42 std::lock_guard<std::recursive_mutex> guard(m_mutex);
43 return m_addr_to_sect.empty();
46 void SectionLoadList::Clear() {
47 std::lock_guard<std::recursive_mutex> guard(m_mutex);
48 m_addr_to_sect.clear();
49 m_sect_to_addr.clear();
53 SectionLoadList::GetSectionLoadAddress(const lldb::SectionSP §ion) const {
54 // TODO: add support for the same section having multiple load addresses
55 addr_t section_load_addr = LLDB_INVALID_ADDRESS;
57 std::lock_guard<std::recursive_mutex> guard(m_mutex);
58 sect_to_addr_collection::const_iterator pos =
59 m_sect_to_addr.find(section.get());
61 if (pos != m_sect_to_addr.end())
62 section_load_addr = pos->second;
64 return section_load_addr;
67 bool SectionLoadList::SetSectionLoadAddress(const lldb::SectionSP §ion,
70 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
71 ModuleSP module_sp(section->GetModule());
74 LLDB_LOGV(log, "(section = {0} ({1}.{2}), load_addr = {3:x}) module = {4}",
75 section.get(), module_sp->GetFileSpec(), section->GetName(),
76 load_addr, module_sp.get());
78 if (section->GetByteSize() == 0)
79 return false; // No change
81 // Fill in the section -> load_addr map
82 std::lock_guard<std::recursive_mutex> guard(m_mutex);
83 sect_to_addr_collection::iterator sta_pos =
84 m_sect_to_addr.find(section.get());
85 if (sta_pos != m_sect_to_addr.end()) {
86 if (load_addr == sta_pos->second)
87 return false; // No change...
89 sta_pos->second = load_addr;
91 m_sect_to_addr[section.get()] = load_addr;
93 // Fill in the load_addr -> section map
94 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
95 if (ats_pos != m_addr_to_sect.end()) {
96 // Some sections are ok to overlap, and for others we should warn. When
97 // we have multiple load addresses that correspond to a section, we will
98 // always attribute the section to the be last section that claims it
99 // exists at that address. Sometimes it is ok for more that one section
100 // to be loaded at a specific load address, and other times it isn't.
101 // The "warn_multiple" parameter tells us if we should warn in this case
102 // or not. The DynamicLoader plug-in subclasses should know which
103 // sections should warn and which shouldn't (darwin shared cache modules
104 // all shared the same "__LINKEDIT" sections, so the dynamic loader can
105 // pass false for "warn_multiple").
106 if (warn_multiple && section != ats_pos->second) {
107 ModuleSP module_sp(section->GetModule());
109 ModuleSP curr_module_sp(ats_pos->second->GetModule());
110 if (curr_module_sp) {
111 module_sp->ReportWarning(
112 "address 0x%16.16" PRIx64
113 " maps to more than one section: %s.%s and %s.%s",
114 load_addr, module_sp->GetFileSpec().GetFilename().GetCString(),
115 section->GetName().GetCString(),
116 curr_module_sp->GetFileSpec().GetFilename().GetCString(),
117 ats_pos->second->GetName().GetCString());
121 ats_pos->second = section;
123 m_addr_to_sect[load_addr] = section;
124 return true; // Changed
129 "SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64
130 ") error: module has been deleted",
131 __FUNCTION__, static_cast<void *>(section.get()),
132 section->GetName().AsCString(), load_addr);
138 size_t SectionLoadList::SetSectionUnloaded(const lldb::SectionSP §ion_sp) {
139 size_t unload_count = 0;
142 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
144 if (log && log->GetVerbose()) {
145 ModuleSP module_sp = section_sp->GetModule();
146 std::string module_name("<Unknown>");
148 const FileSpec &module_file_spec(
149 section_sp->GetModule()->GetFileSpec());
150 module_name = module_file_spec.GetPath();
152 log->Printf("SectionLoadList::%s (section = %p (%s.%s))", __FUNCTION__,
153 static_cast<void *>(section_sp.get()), module_name.c_str(),
154 section_sp->GetName().AsCString());
157 std::lock_guard<std::recursive_mutex> guard(m_mutex);
159 sect_to_addr_collection::iterator sta_pos =
160 m_sect_to_addr.find(section_sp.get());
161 if (sta_pos != m_sect_to_addr.end()) {
163 addr_t load_addr = sta_pos->second;
164 m_sect_to_addr.erase(sta_pos);
166 addr_to_sect_collection::iterator ats_pos =
167 m_addr_to_sect.find(load_addr);
168 if (ats_pos != m_addr_to_sect.end())
169 m_addr_to_sect.erase(ats_pos);
175 bool SectionLoadList::SetSectionUnloaded(const lldb::SectionSP §ion_sp,
177 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
179 if (log && log->GetVerbose()) {
180 ModuleSP module_sp = section_sp->GetModule();
181 std::string module_name("<Unknown>");
183 const FileSpec &module_file_spec(section_sp->GetModule()->GetFileSpec());
184 module_name = module_file_spec.GetPath();
187 "SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64
189 __FUNCTION__, static_cast<void *>(section_sp.get()),
190 module_name.c_str(), section_sp->GetName().AsCString(), load_addr);
193 std::lock_guard<std::recursive_mutex> guard(m_mutex);
194 sect_to_addr_collection::iterator sta_pos =
195 m_sect_to_addr.find(section_sp.get());
196 if (sta_pos != m_sect_to_addr.end()) {
198 m_sect_to_addr.erase(sta_pos);
201 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
202 if (ats_pos != m_addr_to_sect.end()) {
204 m_addr_to_sect.erase(ats_pos);
210 bool SectionLoadList::ResolveLoadAddress(addr_t load_addr, Address &so_addr,
211 bool allow_section_end) const {
212 // First find the top level section that this load address exists in
213 std::lock_guard<std::recursive_mutex> guard(m_mutex);
214 if (!m_addr_to_sect.empty()) {
215 addr_to_sect_collection::const_iterator pos =
216 m_addr_to_sect.lower_bound(load_addr);
217 if (pos != m_addr_to_sect.end()) {
218 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
220 const addr_t pos_load_addr = pos->first;
221 if (load_addr >= pos_load_addr) {
222 addr_t offset = load_addr - pos_load_addr;
223 if (offset < pos->second->GetByteSize() + (allow_section_end ? 1 : 0)) {
224 // We have found the top level section, now we need to find the
225 // deepest child section.
226 return pos->second->ResolveContainedAddress(offset, so_addr,
231 // There are no entries that have an address that is >= load_addr,
232 // so we need to check the last entry on our collection.
233 addr_to_sect_collection::const_reverse_iterator rpos =
234 m_addr_to_sect.rbegin();
235 if (load_addr >= rpos->first) {
236 addr_t offset = load_addr - rpos->first;
238 rpos->second->GetByteSize() + (allow_section_end ? 1 : 0)) {
239 // We have found the top level section, now we need to find the
240 // deepest child section.
241 return rpos->second->ResolveContainedAddress(offset, so_addr,
251 void SectionLoadList::Dump(Stream &s, Target *target) {
252 std::lock_guard<std::recursive_mutex> guard(m_mutex);
253 addr_to_sect_collection::const_iterator pos, end;
254 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end;
256 s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first,
257 static_cast<void *>(pos->second.get()));
258 pos->second->Dump(&s, target, 0);