blob: 17e558144ced52a0d2c1566803fe050e5105269c [file] [log] [blame]
Greg Clayton49480b12010-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 Clayton58e844b2010-12-08 05:08:21 +000031 Mutex::Locker locker(m_mutex);
Greg Clayton0bfda0b2011-02-05 02:25:06 +000032 return m_addr_to_sect.empty();
Greg Clayton49480b12010-09-14 23:52:43 +000033}
34
35void
36SectionLoadList::Clear ()
37{
Greg Clayton58e844b2010-12-08 05:08:21 +000038 Mutex::Locker locker(m_mutex);
Greg Clayton0bfda0b2011-02-05 02:25:06 +000039 m_addr_to_sect.clear();
40 m_sect_to_addr.clear();
Greg Clayton49480b12010-09-14 23:52:43 +000041}
42
43addr_t
Greg Clayton545762f2012-07-07 01:24:12 +000044SectionLoadList::GetSectionLoadAddress (const lldb::SectionSP &section) const
Greg Clayton49480b12010-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 Clayton58e844b2010-12-08 05:08:21 +000048 if (section)
49 {
50 Mutex::Locker locker(m_mutex);
Greg Clayton545762f2012-07-07 01:24:12 +000051 sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section.get());
Greg Clayton0bfda0b2011-02-05 02:25:06 +000052
53 if (pos != m_sect_to_addr.end())
54 section_load_addr = pos->second;
Greg Clayton58e844b2010-12-08 05:08:21 +000055 }
56 return section_load_addr;
Greg Clayton49480b12010-09-14 23:52:43 +000057}
58
59bool
Greg Clayton545762f2012-07-07 01:24:12 +000060SectionLoadList::SetSectionLoadAddress (const lldb::SectionSP &section, addr_t load_addr, bool warn_multiple)
Greg Clayton49480b12010-09-14 23:52:43 +000061{
Greg Claytone005f2c2010-11-06 01:53:30 +000062 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton49480b12010-09-14 23:52:43 +000063
64 if (log)
Greg Clayton427f2902010-12-14 02:59:59 +000065 {
66 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
Daniel Malea5f35a4b2012-11-29 21:49:15 +000067 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16" PRIx64 ")",
Greg Clayton49480b12010-09-14 23:52:43 +000068 __FUNCTION__,
Greg Clayton545762f2012-07-07 01:24:12 +000069 section.get(),
Greg Clayton427f2902010-12-14 02:59:59 +000070 module_file_spec.GetDirectory().AsCString(),
71 module_file_spec.GetDirectory() ? "/" : "",
72 module_file_spec.GetFilename().AsCString(),
Greg Clayton49480b12010-09-14 23:52:43 +000073 section->GetName().AsCString(),
74 load_addr);
Greg Clayton427f2902010-12-14 02:59:59 +000075 }
Greg Clayton49480b12010-09-14 23:52:43 +000076
Greg Clayton9ce95382012-02-13 23:10:39 +000077 if (section->GetByteSize() == 0)
78 return false; // No change
79
Greg Claytona414e672012-04-16 21:01:30 +000080 // Fill in the section -> load_addr map
Greg Clayton58e844b2010-12-08 05:08:21 +000081 Mutex::Locker locker(m_mutex);
Greg Clayton545762f2012-07-07 01:24:12 +000082 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get());
Greg Clayton0bfda0b2011-02-05 02:25:06 +000083 if (sta_pos != m_sect_to_addr.end())
Greg Clayton49480b12010-09-14 23:52:43 +000084 {
Greg Clayton0bfda0b2011-02-05 02:25:06 +000085 if (load_addr == sta_pos->second)
Greg Clayton58e844b2010-12-08 05:08:21 +000086 return false; // No change...
87 else
Greg Clayton0bfda0b2011-02-05 02:25:06 +000088 sta_pos->second = load_addr;
Greg Clayton49480b12010-09-14 23:52:43 +000089 }
Greg Clayton58e844b2010-12-08 05:08:21 +000090 else
Greg Clayton545762f2012-07-07 01:24:12 +000091 m_sect_to_addr[section.get()] = load_addr;
Greg Clayton0bfda0b2011-02-05 02:25:06 +000092
Greg Claytona414e672012-04-16 21:01:30 +000093 // Fill in the load_addr -> section map
Greg Clayton0bfda0b2011-02-05 02:25:06 +000094 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
95 if (ats_pos != m_addr_to_sect.end())
Greg Clayton58e844b2010-12-08 05:08:21 +000096 {
Greg Claytona414e672012-04-16 21:01:30 +000097 // Some sections are ok to overlap, and for others we should warn. When
98 // we have multiple load addresses that correspond to a section, we will
99 // allways attribute the section to the be last section that claims it
100 // exists at that address. Sometimes it is ok for more that one section
101 // to be loaded at a specific load address, and other times it isn't.
102 // The "warn_multiple" parameter tells us if we should warn in this case
103 // or not. The DynamicLoader plug-in subclasses should know which
104 // sections should warn and which shouldn't (darwin shared cache modules
105 // all shared the same "__LINKEDIT" sections, so the dynamic loader can
106 // pass false for "warn_multiple").
107 if (warn_multiple && section != ats_pos->second)
Greg Clayton9ce95382012-02-13 23:10:39 +0000108 {
Greg Clayton3508c382012-02-24 01:59:29 +0000109 ModuleSP module_sp (section->GetModule());
110 if (module_sp)
Greg Clayton9ce95382012-02-13 23:10:39 +0000111 {
Greg Clayton40a39762012-07-31 00:31:32 +0000112 ModuleSP curr_module_sp (ats_pos->second->GetModule());
113 if (curr_module_sp)
114 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000115 module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s",
Greg Clayton40a39762012-07-31 00:31:32 +0000116 load_addr,
117 module_sp->GetFileSpec().GetFilename().GetCString(),
118 section->GetName().GetCString(),
119 curr_module_sp->GetFileSpec().GetFilename().GetCString(),
120 ats_pos->second->GetName().GetCString());
121 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000122 }
123 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000124 ats_pos->second = section;
Greg Clayton58e844b2010-12-08 05:08:21 +0000125 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000126 else
127 m_addr_to_sect[load_addr] = section;
128
Greg Clayton49480b12010-09-14 23:52:43 +0000129 return true; // Changed
130}
131
132size_t
Greg Clayton545762f2012-07-07 01:24:12 +0000133SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp)
Greg Clayton49480b12010-09-14 23:52:43 +0000134{
Greg Clayton49480b12010-09-14 23:52:43 +0000135 size_t unload_count = 0;
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000136
Greg Clayton545762f2012-07-07 01:24:12 +0000137 if (section_sp)
138 {
139 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
140
141 if (log)
142 {
143 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
144 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))",
145 __FUNCTION__,
146 section_sp.get(),
147 module_file_spec.GetDirectory().AsCString(),
148 module_file_spec.GetDirectory() ? "/" : "",
149 module_file_spec.GetFilename().AsCString(),
150 section_sp->GetName().AsCString());
151 }
152
153 Mutex::Locker locker(m_mutex);
154
155 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
156 if (sta_pos != m_sect_to_addr.end())
157 {
Greg Clayton1f71bcf2013-01-29 01:17:09 +0000158 ++unload_count;
Greg Clayton545762f2012-07-07 01:24:12 +0000159 addr_t load_addr = sta_pos->second;
160 m_sect_to_addr.erase (sta_pos);
161
162 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
163 if (ats_pos != m_addr_to_sect.end())
164 m_addr_to_sect.erase (ats_pos);
165 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000166 }
Greg Clayton49480b12010-09-14 23:52:43 +0000167 return unload_count;
168}
169
170bool
Greg Clayton545762f2012-07-07 01:24:12 +0000171SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
Greg Clayton49480b12010-09-14 23:52:43 +0000172{
Greg Claytone005f2c2010-11-06 01:53:30 +0000173 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton49480b12010-09-14 23:52:43 +0000174
175 if (log)
Greg Clayton427f2902010-12-14 02:59:59 +0000176 {
Greg Clayton545762f2012-07-07 01:24:12 +0000177 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000178 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16" PRIx64 ")",
Greg Clayton49480b12010-09-14 23:52:43 +0000179 __FUNCTION__,
Greg Clayton545762f2012-07-07 01:24:12 +0000180 section_sp.get(),
Greg Clayton427f2902010-12-14 02:59:59 +0000181 module_file_spec.GetDirectory().AsCString(),
182 module_file_spec.GetDirectory() ? "/" : "",
183 module_file_spec.GetFilename().AsCString(),
Greg Clayton545762f2012-07-07 01:24:12 +0000184 section_sp->GetName().AsCString(),
Greg Clayton49480b12010-09-14 23:52:43 +0000185 load_addr);
Greg Clayton427f2902010-12-14 02:59:59 +0000186 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000187 bool erased = false;
Greg Clayton58e844b2010-12-08 05:08:21 +0000188 Mutex::Locker locker(m_mutex);
Greg Clayton545762f2012-07-07 01:24:12 +0000189 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000190 if (sta_pos != m_sect_to_addr.end())
191 {
192 erased = true;
193 m_sect_to_addr.erase (sta_pos);
194 }
195
196 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
197 if (ats_pos != m_addr_to_sect.end())
198 {
199 erased = true;
200 m_addr_to_sect.erase (ats_pos);
201 }
202
203 return erased;
Greg Clayton49480b12010-09-14 23:52:43 +0000204}
205
206
207bool
208SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
209{
Greg Clayton58e844b2010-12-08 05:08:21 +0000210 // First find the top level section that this load address exists in
211 Mutex::Locker locker(m_mutex);
Greg Clayton668a3cd2011-05-17 18:13:59 +0000212 if (!m_addr_to_sect.empty())
Greg Clayton49480b12010-09-14 23:52:43 +0000213 {
Greg Clayton668a3cd2011-05-17 18:13:59 +0000214 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
215 if (pos != m_addr_to_sect.end())
Greg Clayton49480b12010-09-14 23:52:43 +0000216 {
Greg Clayton668a3cd2011-05-17 18:13:59 +0000217 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
218 --pos;
219 if (load_addr >= pos->first)
Jason Molendad4c5cd52010-12-22 02:02:45 +0000220 {
Greg Clayton668a3cd2011-05-17 18:13:59 +0000221 addr_t offset = load_addr - pos->first;
222 if (offset < pos->second->GetByteSize())
223 {
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 }
230 else
231 {
Greg Clayton289f0582011-05-18 18:22:47 +0000232 // There are no entries that have an address that is >= load_addr,
233 // so we need to check the last entry on our collection.
234 addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin();
235 if (load_addr >= rpos->first)
Greg Clayton668a3cd2011-05-17 18:13:59 +0000236 {
Greg Clayton289f0582011-05-18 18:22:47 +0000237 addr_t offset = load_addr - rpos->first;
238 if (offset < rpos->second->GetByteSize())
Greg Clayton668a3cd2011-05-17 18:13:59 +0000239 {
240 // We have found the top level section, now we need to find the
241 // deepest child section.
Greg Clayton289f0582011-05-18 18:22:47 +0000242 return rpos->second->ResolveContainedAddress (offset, so_addr);
Greg Clayton668a3cd2011-05-17 18:13:59 +0000243 }
Jason Molendad4c5cd52010-12-22 02:02:45 +0000244 }
Greg Clayton49480b12010-09-14 23:52:43 +0000245 }
246 }
247 so_addr.Clear();
248 return false;
249}
Greg Clayton58e844b2010-12-08 05:08:21 +0000250
251void
252SectionLoadList::Dump (Stream &s, Target *target)
253{
254 Mutex::Locker locker(m_mutex);
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000255 addr_to_sect_collection::const_iterator pos, end;
256 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
Greg Clayton58e844b2010-12-08 05:08:21 +0000257 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000258 s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, pos->second.get());
Greg Clayton58e844b2010-12-08 05:08:21 +0000259 pos->second->Dump (&s, target, 0);
260 }
261}
262
263