blob: 96713c6ea797cdd9e3d8ca349cad4294cb7a5fca [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
28bool
29SectionLoadList::IsEmpty() const
30{
Greg Clayton10177aa2010-12-08 05:08:21 +000031 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +000032 return m_addr_to_sect.empty();
Greg Clayton17f69202010-09-14 23:52:43 +000033}
34
35void
36SectionLoadList::Clear ()
37{
Greg Clayton10177aa2010-12-08 05:08:21 +000038 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +000039 m_addr_to_sect.clear();
40 m_sect_to_addr.clear();
Greg Clayton17f69202010-09-14 23:52:43 +000041}
42
43addr_t
Greg Clayton7820bd12012-07-07 01:24:12 +000044SectionLoadList::GetSectionLoadAddress (const lldb::SectionSP &section) const
Greg Clayton17f69202010-09-14 23:52:43 +000045{
46 // TODO: add support for the same section having multiple load addresses
47 addr_t section_load_addr = LLDB_INVALID_ADDRESS;
Greg Clayton10177aa2010-12-08 05:08:21 +000048 if (section)
49 {
50 Mutex::Locker locker(m_mutex);
Greg Clayton7820bd12012-07-07 01:24:12 +000051 sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section.get());
Greg Clayton6d093452011-02-05 02:25:06 +000052
53 if (pos != m_sect_to_addr.end())
54 section_load_addr = pos->second;
Greg Clayton10177aa2010-12-08 05:08:21 +000055 }
56 return section_load_addr;
Greg Clayton17f69202010-09-14 23:52:43 +000057}
58
59bool
Greg Clayton7820bd12012-07-07 01:24:12 +000060SectionLoadList::SetSectionLoadAddress (const lldb::SectionSP &section, addr_t load_addr, bool warn_multiple)
Greg Clayton17f69202010-09-14 23:52:43 +000061{
Greg Clayton5160ce52013-03-27 23:08:40 +000062 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +000063
Greg Claytonfd814c52013-08-13 01:42:25 +000064 ModuleSP module_sp (section->GetModule());
65
66 if (module_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000067 {
Greg Claytonfd814c52013-08-13 01:42:25 +000068 if (log)
Greg Claytonc859e2d2012-02-13 23:10:39 +000069 {
Greg Claytonfd814c52013-08-13 01:42:25 +000070 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",
72 __FUNCTION__,
73 section.get(),
74 module_file_spec.GetPath().c_str(),
75 section->GetName().AsCString(),
76 load_addr,
77 module_sp.get());
78 }
79
80 if (section->GetByteSize() == 0)
81 return false; // No change
82
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())
87 {
88 if (load_addr == sta_pos->second)
89 return false; // No change...
90 else
91 sta_pos->second = load_addr;
92 }
93 else
94 m_sect_to_addr[section.get()] = load_addr;
95
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())
99 {
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)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000111 {
Greg Claytonfd814c52013-08-13 01:42:25 +0000112 ModuleSP module_sp (section->GetModule());
113 if (module_sp)
Greg Clayton9d192e12012-07-31 00:31:32 +0000114 {
Greg Claytonfd814c52013-08-13 01:42:25 +0000115 ModuleSP curr_module_sp (ats_pos->second->GetModule());
116 if (curr_module_sp)
117 {
118 module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s",
119 load_addr,
120 module_sp->GetFileSpec().GetFilename().GetCString(),
121 section->GetName().GetCString(),
122 curr_module_sp->GetFileSpec().GetFilename().GetCString(),
123 ats_pos->second->GetName().GetCString());
124 }
Greg Clayton9d192e12012-07-31 00:31:32 +0000125 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000126 }
Greg Claytonfd814c52013-08-13 01:42:25 +0000127 ats_pos->second = section;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000128 }
Greg Claytonfd814c52013-08-13 01:42:25 +0000129 else
130 m_addr_to_sect[load_addr] = section;
131 return true; // Changed
132
Greg Clayton10177aa2010-12-08 05:08:21 +0000133 }
Greg Clayton6d093452011-02-05 02:25:06 +0000134 else
Greg Claytonfd814c52013-08-13 01:42:25 +0000135 {
136 if (log)
137 {
138 log->Printf ("SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64 ") error: module has been deleted",
139 __FUNCTION__,
140 section.get(),
141 section->GetName().AsCString(),
142 load_addr);
143 }
144 }
145 return false;
Greg Clayton17f69202010-09-14 23:52:43 +0000146}
147
148size_t
Greg Clayton7820bd12012-07-07 01:24:12 +0000149SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp)
Greg Clayton17f69202010-09-14 23:52:43 +0000150{
Greg Clayton17f69202010-09-14 23:52:43 +0000151 size_t unload_count = 0;
Greg Clayton6d093452011-02-05 02:25:06 +0000152
Greg Clayton7820bd12012-07-07 01:24:12 +0000153 if (section_sp)
154 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000155 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton7820bd12012-07-07 01:24:12 +0000156
157 if (log)
158 {
159 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000160 log->Printf ("SectionLoadList::%s (section = %p (%s.%s))",
Greg Clayton7820bd12012-07-07 01:24:12 +0000161 __FUNCTION__,
162 section_sp.get(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000163 module_file_spec.GetPath().c_str(),
Greg Clayton7820bd12012-07-07 01:24:12 +0000164 section_sp->GetName().AsCString());
165 }
166
167 Mutex::Locker locker(m_mutex);
168
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())
171 {
Greg Clayton3c947372013-01-29 01:17:09 +0000172 ++unload_count;
Greg Clayton7820bd12012-07-07 01:24:12 +0000173 addr_t load_addr = sta_pos->second;
174 m_sect_to_addr.erase (sta_pos);
175
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);
179 }
Greg Clayton6d093452011-02-05 02:25:06 +0000180 }
Greg Clayton17f69202010-09-14 23:52:43 +0000181 return unload_count;
182}
183
184bool
Greg Clayton7820bd12012-07-07 01:24:12 +0000185SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
Greg Clayton17f69202010-09-14 23:52:43 +0000186{
Greg Clayton5160ce52013-03-27 23:08:40 +0000187 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000188
189 if (log)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000190 {
Greg Clayton7820bd12012-07-07 01:24:12 +0000191 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000192 log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ")",
Greg Clayton17f69202010-09-14 23:52:43 +0000193 __FUNCTION__,
Greg Clayton7820bd12012-07-07 01:24:12 +0000194 section_sp.get(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000195 module_file_spec.GetPath().c_str(),
Greg Clayton7820bd12012-07-07 01:24:12 +0000196 section_sp->GetName().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +0000197 load_addr);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000198 }
Greg Clayton6d093452011-02-05 02:25:06 +0000199 bool erased = false;
Greg Clayton10177aa2010-12-08 05:08:21 +0000200 Mutex::Locker locker(m_mutex);
Greg Clayton7820bd12012-07-07 01:24:12 +0000201 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
Greg Clayton6d093452011-02-05 02:25:06 +0000202 if (sta_pos != m_sect_to_addr.end())
203 {
204 erased = true;
205 m_sect_to_addr.erase (sta_pos);
206 }
207
208 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
209 if (ats_pos != m_addr_to_sect.end())
210 {
211 erased = true;
212 m_addr_to_sect.erase (ats_pos);
213 }
214
215 return erased;
Greg Clayton17f69202010-09-14 23:52:43 +0000216}
217
218
219bool
220SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
221{
Greg Clayton10177aa2010-12-08 05:08:21 +0000222 // First find the top level section that this load address exists in
223 Mutex::Locker locker(m_mutex);
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000224 if (!m_addr_to_sect.empty())
Greg Clayton17f69202010-09-14 23:52:43 +0000225 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000226 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
227 if (pos != m_addr_to_sect.end())
Greg Clayton17f69202010-09-14 23:52:43 +0000228 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000229 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
230 --pos;
Greg Clayton39f7ee82013-02-01 21:38:35 +0000231 const addr_t pos_load_addr = pos->first;
232 if (load_addr >= pos_load_addr)
Jason Molendaf830e482010-12-22 02:02:45 +0000233 {
Greg Clayton39f7ee82013-02-01 21:38:35 +0000234 addr_t offset = load_addr - pos_load_addr;
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000235 if (offset < pos->second->GetByteSize())
236 {
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);
240 }
241 }
242 }
243 else
244 {
Greg Clayton1c870d62011-05-18 18:22:47 +0000245 // 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)
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000249 {
Greg Clayton1c870d62011-05-18 18:22:47 +0000250 addr_t offset = load_addr - rpos->first;
251 if (offset < rpos->second->GetByteSize())
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000252 {
253 // We have found the top level section, now we need to find the
254 // deepest child section.
Greg Clayton1c870d62011-05-18 18:22:47 +0000255 return rpos->second->ResolveContainedAddress (offset, so_addr);
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000256 }
Jason Molendaf830e482010-12-22 02:02:45 +0000257 }
Greg Clayton17f69202010-09-14 23:52:43 +0000258 }
259 }
260 so_addr.Clear();
261 return false;
262}
Greg Clayton10177aa2010-12-08 05:08:21 +0000263
264void
265SectionLoadList::Dump (Stream &s, Target *target)
266{
267 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +0000268 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)
Greg Clayton10177aa2010-12-08 05:08:21 +0000270 {
Daniel Malead01b2952012-11-29 21:49:15 +0000271 s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, pos->second.get());
Greg Clayton10177aa2010-12-08 05:08:21 +0000272 pos->second->Dump (&s, target, 0);
273 }
274}
275
276