blob: af5636555ae75c007ec1c9fe861eeda8c03d965c [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 Clayton952e9dc2013-03-27 23:08:40 +000062 Log *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());
Greg Clayton97a19b22013-04-29 17:25:54 +000067 log->Printf ("SectionLoadList::%s (section = %p (%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 Clayton97a19b22013-04-29 17:25:54 +000070 module_file_spec.GetPath().c_str(),
Greg Clayton49480b12010-09-14 23:52:43 +000071 section->GetName().AsCString(),
72 load_addr);
Greg Clayton427f2902010-12-14 02:59:59 +000073 }
Greg Clayton49480b12010-09-14 23:52:43 +000074
Greg Clayton9ce95382012-02-13 23:10:39 +000075 if (section->GetByteSize() == 0)
76 return false; // No change
77
Greg Claytona414e672012-04-16 21:01:30 +000078 // Fill in the section -> load_addr map
Greg Clayton58e844b2010-12-08 05:08:21 +000079 Mutex::Locker locker(m_mutex);
Greg Clayton545762f2012-07-07 01:24:12 +000080 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get());
Greg Clayton0bfda0b2011-02-05 02:25:06 +000081 if (sta_pos != m_sect_to_addr.end())
Greg Clayton49480b12010-09-14 23:52:43 +000082 {
Greg Clayton0bfda0b2011-02-05 02:25:06 +000083 if (load_addr == sta_pos->second)
Greg Clayton58e844b2010-12-08 05:08:21 +000084 return false; // No change...
85 else
Greg Clayton0bfda0b2011-02-05 02:25:06 +000086 sta_pos->second = load_addr;
Greg Clayton49480b12010-09-14 23:52:43 +000087 }
Greg Clayton58e844b2010-12-08 05:08:21 +000088 else
Greg Clayton545762f2012-07-07 01:24:12 +000089 m_sect_to_addr[section.get()] = load_addr;
Greg Clayton0bfda0b2011-02-05 02:25:06 +000090
Greg Claytona414e672012-04-16 21:01:30 +000091 // Fill in the load_addr -> section map
Greg Clayton0bfda0b2011-02-05 02:25:06 +000092 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
93 if (ats_pos != m_addr_to_sect.end())
Greg Clayton58e844b2010-12-08 05:08:21 +000094 {
Greg Claytona414e672012-04-16 21:01:30 +000095 // Some sections are ok to overlap, and for others we should warn. When
96 // we have multiple load addresses that correspond to a section, we will
97 // allways attribute the section to the be last section that claims it
98 // exists at that address. Sometimes it is ok for more that one section
99 // to be loaded at a specific load address, and other times it isn't.
100 // The "warn_multiple" parameter tells us if we should warn in this case
101 // or not. The DynamicLoader plug-in subclasses should know which
102 // sections should warn and which shouldn't (darwin shared cache modules
103 // all shared the same "__LINKEDIT" sections, so the dynamic loader can
104 // pass false for "warn_multiple").
105 if (warn_multiple && section != ats_pos->second)
Greg Clayton9ce95382012-02-13 23:10:39 +0000106 {
Greg Clayton3508c382012-02-24 01:59:29 +0000107 ModuleSP module_sp (section->GetModule());
108 if (module_sp)
Greg Clayton9ce95382012-02-13 23:10:39 +0000109 {
Greg Clayton40a39762012-07-31 00:31:32 +0000110 ModuleSP curr_module_sp (ats_pos->second->GetModule());
111 if (curr_module_sp)
112 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000113 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 +0000114 load_addr,
115 module_sp->GetFileSpec().GetFilename().GetCString(),
116 section->GetName().GetCString(),
117 curr_module_sp->GetFileSpec().GetFilename().GetCString(),
118 ats_pos->second->GetName().GetCString());
119 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000120 }
121 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000122 ats_pos->second = section;
Greg Clayton58e844b2010-12-08 05:08:21 +0000123 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000124 else
125 m_addr_to_sect[load_addr] = section;
126
Greg Clayton49480b12010-09-14 23:52:43 +0000127 return true; // Changed
128}
129
130size_t
Greg Clayton545762f2012-07-07 01:24:12 +0000131SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp)
Greg Clayton49480b12010-09-14 23:52:43 +0000132{
Greg Clayton49480b12010-09-14 23:52:43 +0000133 size_t unload_count = 0;
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000134
Greg Clayton545762f2012-07-07 01:24:12 +0000135 if (section_sp)
136 {
Greg Clayton952e9dc2013-03-27 23:08:40 +0000137 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton545762f2012-07-07 01:24:12 +0000138
139 if (log)
140 {
141 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
Greg Clayton97a19b22013-04-29 17:25:54 +0000142 log->Printf ("SectionLoadList::%s (section = %p (%s.%s))",
Greg Clayton545762f2012-07-07 01:24:12 +0000143 __FUNCTION__,
144 section_sp.get(),
Greg Clayton97a19b22013-04-29 17:25:54 +0000145 module_file_spec.GetPath().c_str(),
Greg Clayton545762f2012-07-07 01:24:12 +0000146 section_sp->GetName().AsCString());
147 }
148
149 Mutex::Locker locker(m_mutex);
150
151 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
152 if (sta_pos != m_sect_to_addr.end())
153 {
Greg Clayton1f71bcf2013-01-29 01:17:09 +0000154 ++unload_count;
Greg Clayton545762f2012-07-07 01:24:12 +0000155 addr_t load_addr = sta_pos->second;
156 m_sect_to_addr.erase (sta_pos);
157
158 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
159 if (ats_pos != m_addr_to_sect.end())
160 m_addr_to_sect.erase (ats_pos);
161 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000162 }
Greg Clayton49480b12010-09-14 23:52:43 +0000163 return unload_count;
164}
165
166bool
Greg Clayton545762f2012-07-07 01:24:12 +0000167SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
Greg Clayton49480b12010-09-14 23:52:43 +0000168{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000169 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton49480b12010-09-14 23:52:43 +0000170
171 if (log)
Greg Clayton427f2902010-12-14 02:59:59 +0000172 {
Greg Clayton545762f2012-07-07 01:24:12 +0000173 const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
Greg Clayton97a19b22013-04-29 17:25:54 +0000174 log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64 ")",
Greg Clayton49480b12010-09-14 23:52:43 +0000175 __FUNCTION__,
Greg Clayton545762f2012-07-07 01:24:12 +0000176 section_sp.get(),
Greg Clayton97a19b22013-04-29 17:25:54 +0000177 module_file_spec.GetPath().c_str(),
Greg Clayton545762f2012-07-07 01:24:12 +0000178 section_sp->GetName().AsCString(),
Greg Clayton49480b12010-09-14 23:52:43 +0000179 load_addr);
Greg Clayton427f2902010-12-14 02:59:59 +0000180 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000181 bool erased = false;
Greg Clayton58e844b2010-12-08 05:08:21 +0000182 Mutex::Locker locker(m_mutex);
Greg Clayton545762f2012-07-07 01:24:12 +0000183 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000184 if (sta_pos != m_sect_to_addr.end())
185 {
186 erased = true;
187 m_sect_to_addr.erase (sta_pos);
188 }
189
190 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
191 if (ats_pos != m_addr_to_sect.end())
192 {
193 erased = true;
194 m_addr_to_sect.erase (ats_pos);
195 }
196
197 return erased;
Greg Clayton49480b12010-09-14 23:52:43 +0000198}
199
200
201bool
202SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
203{
Greg Clayton58e844b2010-12-08 05:08:21 +0000204 // First find the top level section that this load address exists in
205 Mutex::Locker locker(m_mutex);
Greg Clayton668a3cd2011-05-17 18:13:59 +0000206 if (!m_addr_to_sect.empty())
Greg Clayton49480b12010-09-14 23:52:43 +0000207 {
Greg Clayton668a3cd2011-05-17 18:13:59 +0000208 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
209 if (pos != m_addr_to_sect.end())
Greg Clayton49480b12010-09-14 23:52:43 +0000210 {
Greg Clayton668a3cd2011-05-17 18:13:59 +0000211 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
212 --pos;
Greg Clayton2ddb2b82013-02-01 21:38:35 +0000213 const addr_t pos_load_addr = pos->first;
214 if (load_addr >= pos_load_addr)
Jason Molendad4c5cd52010-12-22 02:02:45 +0000215 {
Greg Clayton2ddb2b82013-02-01 21:38:35 +0000216 addr_t offset = load_addr - pos_load_addr;
Greg Clayton668a3cd2011-05-17 18:13:59 +0000217 if (offset < pos->second->GetByteSize())
218 {
219 // We have found the top level section, now we need to find the
220 // deepest child section.
221 return pos->second->ResolveContainedAddress (offset, so_addr);
222 }
223 }
224 }
225 else
226 {
Greg Clayton289f0582011-05-18 18:22:47 +0000227 // There are no entries that have an address that is >= load_addr,
228 // so we need to check the last entry on our collection.
229 addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin();
230 if (load_addr >= rpos->first)
Greg Clayton668a3cd2011-05-17 18:13:59 +0000231 {
Greg Clayton289f0582011-05-18 18:22:47 +0000232 addr_t offset = load_addr - rpos->first;
233 if (offset < rpos->second->GetByteSize())
Greg Clayton668a3cd2011-05-17 18:13:59 +0000234 {
235 // We have found the top level section, now we need to find the
236 // deepest child section.
Greg Clayton289f0582011-05-18 18:22:47 +0000237 return rpos->second->ResolveContainedAddress (offset, so_addr);
Greg Clayton668a3cd2011-05-17 18:13:59 +0000238 }
Jason Molendad4c5cd52010-12-22 02:02:45 +0000239 }
Greg Clayton49480b12010-09-14 23:52:43 +0000240 }
241 }
242 so_addr.Clear();
243 return false;
244}
Greg Clayton58e844b2010-12-08 05:08:21 +0000245
246void
247SectionLoadList::Dump (Stream &s, Target *target)
248{
249 Mutex::Locker locker(m_mutex);
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000250 addr_to_sect_collection::const_iterator pos, end;
251 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
Greg Clayton58e844b2010-12-08 05:08:21 +0000252 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000253 s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, pos->second.get());
Greg Clayton58e844b2010-12-08 05:08:21 +0000254 pos->second->Dump (&s, target, 0);
255 }
256}
257
258