blob: 7f12c262f47e266706dac3bf666a72f9c00f9d6e [file] [log] [blame]
Greg Clayton17f69202010-09-14 23:52:43 +00001//===-- SectionLoadList.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/SectionLoadList.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Greg Clayton17f69202010-09-14 23:52:43 +000016#include "lldb/Core/Module.h"
17#include "lldb/Core/Section.h"
Greg Clayton17f69202010-09-14 23:52:43 +000018#include "lldb/Symbol/Block.h"
19#include "lldb/Symbol/Symbol.h"
20#include "lldb/Symbol/SymbolContext.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000021#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000022#include "lldb/Utility/Stream.h"
Greg Clayton17f69202010-09-14 23:52:43 +000023
24using namespace lldb;
25using namespace lldb_private;
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027SectionLoadList::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;
Greg Claytond5944cd2013-12-06 01:12:00 +000032}
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034void 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;
Greg Claytond5944cd2013-12-06 01:12:00 +000039}
40
Kate Stoneb9c1b512016-09-06 20:57:50 +000041bool SectionLoadList::IsEmpty() const {
42 std::lock_guard<std::recursive_mutex> guard(m_mutex);
43 return m_addr_to_sect.empty();
Greg Clayton17f69202010-09-14 23:52:43 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046void SectionLoadList::Clear() {
47 std::lock_guard<std::recursive_mutex> guard(m_mutex);
48 m_addr_to_sect.clear();
49 m_sect_to_addr.clear();
Greg Clayton17f69202010-09-14 23:52:43 +000050}
51
52addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +000053SectionLoadList::GetSectionLoadAddress(const lldb::SectionSP &section) const {
54 // TODO: add support for the same section having multiple load addresses
55 addr_t section_load_addr = LLDB_INVALID_ADDRESS;
56 if (section) {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000057 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 sect_to_addr_collection::const_iterator pos =
59 m_sect_to_addr.find(section.get());
60
61 if (pos != m_sect_to_addr.end())
62 section_load_addr = pos->second;
63 }
64 return section_load_addr;
65}
66
67bool SectionLoadList::SetSectionLoadAddress(const lldb::SectionSP &section,
68 addr_t load_addr,
69 bool warn_multiple) {
Pavel Labath3b7e1982017-02-05 00:44:54 +000070 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 ModuleSP module_sp(section->GetModule());
72
73 if (module_sp) {
Pavel Labath3b7e1982017-02-05 00:44:54 +000074 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());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 if (section->GetByteSize() == 0)
79 return false; // No change
80
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...
88 else
89 sta_pos->second = load_addr;
90 } else
91 m_sect_to_addr[section.get()] = load_addr;
92
93 // Fill in the load_addr -> section map
Greg Clayton6d093452011-02-05 02:25:06 +000094 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 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());
108 if (module_sp) {
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());
118 }
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000119 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 }
121 ats_pos->second = section;
122 } else
123 m_addr_to_sect[load_addr] = section;
124 return true; // Changed
125
126 } else {
127 if (log) {
128 log->Printf(
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);
Greg Clayton17f69202010-09-14 23:52:43 +0000133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 }
135 return false;
Greg Clayton17f69202010-09-14 23:52:43 +0000136}
Greg Clayton10177aa2010-12-08 05:08:21 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138size_t SectionLoadList::SetSectionUnloaded(const lldb::SectionSP &section_sp) {
139 size_t unload_count = 0;
140
141 if (section_sp) {
Pavel Labath3b7e1982017-02-05 00:44:54 +0000142 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143
Pavel Labath3b7e1982017-02-05 00:44:54 +0000144 if (log && log->GetVerbose()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 ModuleSP module_sp = section_sp->GetModule();
146 std::string module_name("<Unknown>");
147 if (module_sp) {
148 const FileSpec &module_file_spec(
149 section_sp->GetModule()->GetFileSpec());
150 module_name = module_file_spec.GetPath();
151 }
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());
155 }
156
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000157 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158
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()) {
162 ++unload_count;
163 addr_t load_addr = sta_pos->second;
164 m_sect_to_addr.erase(sta_pos);
165
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);
Greg Clayton10177aa2010-12-08 05:08:21 +0000170 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 }
172 return unload_count;
Greg Clayton10177aa2010-12-08 05:08:21 +0000173}
174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175bool SectionLoadList::SetSectionUnloaded(const lldb::SectionSP &section_sp,
176 addr_t load_addr) {
Pavel Labath3b7e1982017-02-05 00:44:54 +0000177 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton10177aa2010-12-08 05:08:21 +0000178
Pavel Labath3b7e1982017-02-05 00:44:54 +0000179 if (log && log->GetVerbose()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 ModuleSP module_sp = section_sp->GetModule();
181 std::string module_name("<Unknown>");
182 if (module_sp) {
183 const FileSpec &module_file_spec(section_sp->GetModule()->GetFileSpec());
184 module_name = module_file_spec.GetPath();
185 }
186 log->Printf(
187 "SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64
188 ")",
189 __FUNCTION__, static_cast<void *>(section_sp.get()),
190 module_name.c_str(), section_sp->GetName().AsCString(), load_addr);
191 }
192 bool erased = false;
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()) {
197 erased = true;
198 m_sect_to_addr.erase(sta_pos);
199 }
200
201 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
202 if (ats_pos != m_addr_to_sect.end()) {
203 erased = true;
204 m_addr_to_sect.erase(ats_pos);
205 }
206
207 return erased;
208}
209
210bool SectionLoadList::ResolveLoadAddress(addr_t load_addr,
211 Address &so_addr) 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())
219 --pos;
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()) {
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);
227 }
228 }
229 } else {
230 // There are no entries that have an address that is >= load_addr,
231 // so we need to check the last entry on our collection.
232 addr_to_sect_collection::const_reverse_iterator rpos =
233 m_addr_to_sect.rbegin();
234 if (load_addr >= rpos->first) {
235 addr_t offset = load_addr - rpos->first;
236 if (offset < rpos->second->GetByteSize()) {
237 // We have found the top level section, now we need to find the
238 // deepest child section.
239 return rpos->second->ResolveContainedAddress(offset, so_addr);
240 }
241 }
242 }
243 }
244 so_addr.Clear();
245 return false;
246}
247
248void SectionLoadList::Dump(Stream &s, Target *target) {
249 std::lock_guard<std::recursive_mutex> guard(m_mutex);
250 addr_to_sect_collection::const_iterator pos, end;
251 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end;
252 ++pos) {
253 s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first,
254 static_cast<void *>(pos->second.get()));
255 pos->second->Dump(&s, target, 0);
256 }
257}