blob: d79c32eeb5346bfca657b3d3ab9cef7dca3baef5 [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
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"
23
24using namespace lldb;
25using namespace lldb_private;
26
27
Greg Claytond5944cd2013-12-06 01:12:00 +000028SectionLoadList::SectionLoadList (const SectionLoadList& rhs) :
29 m_addr_to_sect(),
30 m_sect_to_addr(),
31 m_mutex (Mutex::eMutexTypeRecursive)
32{
33 Mutex::Locker locker(rhs.m_mutex);
34 m_addr_to_sect = rhs.m_addr_to_sect;
35 m_sect_to_addr = rhs.m_sect_to_addr;
36}
37
38void
39SectionLoadList::operator=(const SectionLoadList &rhs)
40{
41 Mutex::Locker lhs_locker (m_mutex);
42 Mutex::Locker rhs_locker (rhs.m_mutex);
43 m_addr_to_sect = rhs.m_addr_to_sect;
44 m_sect_to_addr = rhs.m_sect_to_addr;
45}
46
Greg Clayton17f69202010-09-14 23:52:43 +000047bool
48SectionLoadList::IsEmpty() const
49{
Greg Clayton10177aa2010-12-08 05:08:21 +000050 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +000051 return m_addr_to_sect.empty();
Greg Clayton17f69202010-09-14 23:52:43 +000052}
53
54void
55SectionLoadList::Clear ()
56{
Greg Clayton10177aa2010-12-08 05:08:21 +000057 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +000058 m_addr_to_sect.clear();
59 m_sect_to_addr.clear();
Greg Clayton17f69202010-09-14 23:52:43 +000060}
61
62addr_t
Greg Clayton7820bd12012-07-07 01:24:12 +000063SectionLoadList::GetSectionLoadAddress (const lldb::SectionSP &section) const
Greg Clayton17f69202010-09-14 23:52:43 +000064{
65 // TODO: add support for the same section having multiple load addresses
66 addr_t section_load_addr = LLDB_INVALID_ADDRESS;
Greg Clayton10177aa2010-12-08 05:08:21 +000067 if (section)
68 {
69 Mutex::Locker locker(m_mutex);
Greg Clayton7820bd12012-07-07 01:24:12 +000070 sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section.get());
Greg Clayton6d093452011-02-05 02:25:06 +000071
72 if (pos != m_sect_to_addr.end())
73 section_load_addr = pos->second;
Greg Clayton10177aa2010-12-08 05:08:21 +000074 }
75 return section_load_addr;
Greg Clayton17f69202010-09-14 23:52:43 +000076}
77
78bool
Greg Clayton7820bd12012-07-07 01:24:12 +000079SectionLoadList::SetSectionLoadAddress (const lldb::SectionSP &section, addr_t load_addr, bool warn_multiple)
Greg Clayton17f69202010-09-14 23:52:43 +000080{
Greg Clayton5160ce52013-03-27 23:08:40 +000081 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +000082
Greg Claytonfd814c52013-08-13 01:42:25 +000083 ModuleSP module_sp (section->GetModule());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000084
Greg Claytonfd814c52013-08-13 01:42:25 +000085 if (module_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000086 {
Greg Claytonfd814c52013-08-13 01:42:25 +000087 if (log)
Greg Claytonc859e2d2012-02-13 23:10:39 +000088 {
Greg Claytonfd814c52013-08-13 01:42:25 +000089 const FileSpec &module_file_spec (module_sp->GetFileSpec());
90 log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ") module = %p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000091 __FUNCTION__, static_cast<void*>(section.get()),
Greg Claytonfd814c52013-08-13 01:42:25 +000092 module_file_spec.GetPath().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000093 section->GetName().AsCString(), load_addr,
94 static_cast<void*>(module_sp.get()));
Greg Claytonfd814c52013-08-13 01:42:25 +000095 }
96
97 if (section->GetByteSize() == 0)
98 return false; // No change
99
100 // Fill in the section -> load_addr map
101 Mutex::Locker locker(m_mutex);
102 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get());
103 if (sta_pos != m_sect_to_addr.end())
104 {
105 if (load_addr == sta_pos->second)
106 return false; // No change...
107 else
108 sta_pos->second = load_addr;
109 }
110 else
111 m_sect_to_addr[section.get()] = load_addr;
112
113 // Fill in the load_addr -> section map
114 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
115 if (ats_pos != m_addr_to_sect.end())
116 {
117 // Some sections are ok to overlap, and for others we should warn. When
118 // we have multiple load addresses that correspond to a section, we will
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +0000119 // always attribute the section to the be last section that claims it
Greg Claytonfd814c52013-08-13 01:42:25 +0000120 // exists at that address. Sometimes it is ok for more that one section
121 // to be loaded at a specific load address, and other times it isn't.
122 // The "warn_multiple" parameter tells us if we should warn in this case
123 // or not. The DynamicLoader plug-in subclasses should know which
124 // sections should warn and which shouldn't (darwin shared cache modules
125 // all shared the same "__LINKEDIT" sections, so the dynamic loader can
126 // pass false for "warn_multiple").
127 if (warn_multiple && section != ats_pos->second)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000128 {
Greg Claytonfd814c52013-08-13 01:42:25 +0000129 ModuleSP module_sp (section->GetModule());
130 if (module_sp)
Greg Clayton9d192e12012-07-31 00:31:32 +0000131 {
Greg Claytonfd814c52013-08-13 01:42:25 +0000132 ModuleSP curr_module_sp (ats_pos->second->GetModule());
133 if (curr_module_sp)
134 {
135 module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s",
136 load_addr,
137 module_sp->GetFileSpec().GetFilename().GetCString(),
138 section->GetName().GetCString(),
139 curr_module_sp->GetFileSpec().GetFilename().GetCString(),
140 ats_pos->second->GetName().GetCString());
141 }
Greg Clayton9d192e12012-07-31 00:31:32 +0000142 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000143 }
Greg Claytonfd814c52013-08-13 01:42:25 +0000144 ats_pos->second = section;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000145 }
Greg Claytonfd814c52013-08-13 01:42:25 +0000146 else
147 m_addr_to_sect[load_addr] = section;
148 return true; // Changed
149
Greg Clayton10177aa2010-12-08 05:08:21 +0000150 }
Greg Clayton6d093452011-02-05 02:25:06 +0000151 else
Greg Claytonfd814c52013-08-13 01:42:25 +0000152 {
153 if (log)
154 {
155 log->Printf ("SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64 ") error: module has been deleted",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000156 __FUNCTION__, static_cast<void*>(section.get()),
Greg Claytonfd814c52013-08-13 01:42:25 +0000157 section->GetName().AsCString(),
158 load_addr);
159 }
160 }
161 return false;
Greg Clayton17f69202010-09-14 23:52:43 +0000162}
163
164size_t
Greg Clayton7820bd12012-07-07 01:24:12 +0000165SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp)
Greg Clayton17f69202010-09-14 23:52:43 +0000166{
Greg Clayton17f69202010-09-14 23:52:43 +0000167 size_t unload_count = 0;
Greg Clayton6d093452011-02-05 02:25:06 +0000168
Greg Clayton7820bd12012-07-07 01:24:12 +0000169 if (section_sp)
170 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000171 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton7820bd12012-07-07 01:24:12 +0000172
173 if (log)
174 {
Jim Inghamcbff63a2016-01-29 20:21:33 +0000175 ModuleSP module_sp = section_sp->GetModule();
176 std::string module_name("<Unknown>");
177 if (module_sp)
178 {
179 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
180 module_name = module_file_spec.GetPath();
181 }
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000182 log->Printf ("SectionLoadList::%s (section = %p (%s.%s))",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000183 __FUNCTION__, static_cast<void*>(section_sp.get()),
Jim Inghamcbff63a2016-01-29 20:21:33 +0000184 module_name.c_str(),
Greg Clayton7820bd12012-07-07 01:24:12 +0000185 section_sp->GetName().AsCString());
186 }
187
188 Mutex::Locker locker(m_mutex);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000189
Greg Clayton7820bd12012-07-07 01:24:12 +0000190 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
191 if (sta_pos != m_sect_to_addr.end())
192 {
Greg Clayton3c947372013-01-29 01:17:09 +0000193 ++unload_count;
Greg Clayton7820bd12012-07-07 01:24:12 +0000194 addr_t load_addr = sta_pos->second;
195 m_sect_to_addr.erase (sta_pos);
196
197 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
198 if (ats_pos != m_addr_to_sect.end())
199 m_addr_to_sect.erase (ats_pos);
200 }
Greg Clayton6d093452011-02-05 02:25:06 +0000201 }
Greg Clayton17f69202010-09-14 23:52:43 +0000202 return unload_count;
203}
204
205bool
Greg Clayton7820bd12012-07-07 01:24:12 +0000206SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
Greg Clayton17f69202010-09-14 23:52:43 +0000207{
Greg Clayton5160ce52013-03-27 23:08:40 +0000208 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000209
210 if (log)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000211 {
Jim Inghamcbff63a2016-01-29 20:21:33 +0000212 ModuleSP module_sp = section_sp->GetModule();
213 std::string module_name("<Unknown>");
214 if (module_sp)
215 {
216 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
217 module_name = module_file_spec.GetPath();
218 }
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000219 log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ")",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000220 __FUNCTION__, static_cast<void*>(section_sp.get()),
Jim Inghamcbff63a2016-01-29 20:21:33 +0000221 module_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000222 section_sp->GetName().AsCString(), load_addr);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000223 }
Greg Clayton6d093452011-02-05 02:25:06 +0000224 bool erased = false;
Greg Clayton10177aa2010-12-08 05:08:21 +0000225 Mutex::Locker locker(m_mutex);
Greg Clayton7820bd12012-07-07 01:24:12 +0000226 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
Greg Clayton6d093452011-02-05 02:25:06 +0000227 if (sta_pos != m_sect_to_addr.end())
228 {
229 erased = true;
230 m_sect_to_addr.erase (sta_pos);
231 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000232
Greg Clayton6d093452011-02-05 02:25:06 +0000233 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
234 if (ats_pos != m_addr_to_sect.end())
235 {
236 erased = true;
237 m_addr_to_sect.erase (ats_pos);
238 }
239
240 return erased;
Greg Clayton17f69202010-09-14 23:52:43 +0000241}
242
243
244bool
245SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
246{
Greg Clayton10177aa2010-12-08 05:08:21 +0000247 // First find the top level section that this load address exists in
248 Mutex::Locker locker(m_mutex);
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000249 if (!m_addr_to_sect.empty())
Greg Clayton17f69202010-09-14 23:52:43 +0000250 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000251 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
252 if (pos != m_addr_to_sect.end())
Greg Clayton17f69202010-09-14 23:52:43 +0000253 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000254 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
255 --pos;
Greg Clayton39f7ee82013-02-01 21:38:35 +0000256 const addr_t pos_load_addr = pos->first;
257 if (load_addr >= pos_load_addr)
Jason Molendaf830e482010-12-22 02:02:45 +0000258 {
Greg Clayton39f7ee82013-02-01 21:38:35 +0000259 addr_t offset = load_addr - pos_load_addr;
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000260 if (offset < pos->second->GetByteSize())
261 {
262 // We have found the top level section, now we need to find the
263 // deepest child section.
264 return pos->second->ResolveContainedAddress (offset, so_addr);
265 }
266 }
267 }
268 else
269 {
Greg Clayton1c870d62011-05-18 18:22:47 +0000270 // There are no entries that have an address that is >= load_addr,
271 // so we need to check the last entry on our collection.
272 addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin();
273 if (load_addr >= rpos->first)
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000274 {
Greg Clayton1c870d62011-05-18 18:22:47 +0000275 addr_t offset = load_addr - rpos->first;
276 if (offset < rpos->second->GetByteSize())
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000277 {
278 // We have found the top level section, now we need to find the
279 // deepest child section.
Greg Clayton1c870d62011-05-18 18:22:47 +0000280 return rpos->second->ResolveContainedAddress (offset, so_addr);
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000281 }
Jason Molendaf830e482010-12-22 02:02:45 +0000282 }
Greg Clayton17f69202010-09-14 23:52:43 +0000283 }
284 }
285 so_addr.Clear();
286 return false;
287}
Greg Clayton10177aa2010-12-08 05:08:21 +0000288
289void
290SectionLoadList::Dump (Stream &s, Target *target)
291{
292 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +0000293 addr_to_sect_collection::const_iterator pos, end;
294 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
Greg Clayton10177aa2010-12-08 05:08:21 +0000295 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000296 s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ",
297 pos->first, static_cast<void*>(pos->second.get()));
Greg Clayton10177aa2010-12-08 05:08:21 +0000298 pos->second->Dump (&s, target, 0);
299 }
300}
301
302