blob: 6add3ec6a2b859b5c726d3f50a87d8e6fd7e1217 [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
44SectionLoadList::GetSectionLoadAddress (const Section *section) const
45{
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 Clayton6d093452011-02-05 02:25:06 +000051 sect_to_addr_collection::const_iterator pos = m_sect_to_addr.find (section);
52
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 Clayton1e8ac362012-04-16 21:01:30 +000060SectionLoadList::SetSectionLoadAddress (const Section *section, addr_t load_addr, bool warn_multiple)
Greg Clayton17f69202010-09-14 23:52:43 +000061{
Greg Clayton2d4edfb2010-11-06 01:53:30 +000062 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +000063
64 if (log)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000065 {
66 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
67 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)",
Greg Clayton17f69202010-09-14 23:52:43 +000068 __FUNCTION__,
69 section,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000070 module_file_spec.GetDirectory().AsCString(),
71 module_file_spec.GetDirectory() ? "/" : "",
72 module_file_spec.GetFilename().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +000073 section->GetName().AsCString(),
74 load_addr);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000075 }
Greg Clayton17f69202010-09-14 23:52:43 +000076
Greg Claytonc859e2d2012-02-13 23:10:39 +000077 if (section->GetByteSize() == 0)
78 return false; // No change
79
Greg Clayton1e8ac362012-04-16 21:01:30 +000080 // Fill in the section -> load_addr map
Greg Clayton10177aa2010-12-08 05:08:21 +000081 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +000082 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
83 if (sta_pos != m_sect_to_addr.end())
Greg Clayton17f69202010-09-14 23:52:43 +000084 {
Greg Clayton6d093452011-02-05 02:25:06 +000085 if (load_addr == sta_pos->second)
Greg Clayton10177aa2010-12-08 05:08:21 +000086 return false; // No change...
87 else
Greg Clayton6d093452011-02-05 02:25:06 +000088 sta_pos->second = load_addr;
Greg Clayton17f69202010-09-14 23:52:43 +000089 }
Greg Clayton10177aa2010-12-08 05:08:21 +000090 else
Greg Clayton6d093452011-02-05 02:25:06 +000091 m_sect_to_addr[section] = load_addr;
92
Greg Clayton1e8ac362012-04-16 21:01:30 +000093 // 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);
95 if (ats_pos != m_addr_to_sect.end())
Greg Clayton10177aa2010-12-08 05:08:21 +000096 {
Greg Clayton1e8ac362012-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 Claytonc859e2d2012-02-13 23:10:39 +0000108 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000109 ModuleSP module_sp (section->GetModule());
110 if (module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000111 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000112 module_sp->ReportWarning ("address 0x%16.16llx maps to more than one section: %s.%s and %s.%s",
113 load_addr,
114 module_sp->GetFileSpec().GetFilename().GetCString(),
115 section->GetName().GetCString(),
116 ats_pos->second->GetModule()->GetFileSpec().GetFilename().GetCString(),
117 ats_pos->second->GetName().GetCString());
Greg Claytonc859e2d2012-02-13 23:10:39 +0000118 }
119 }
Greg Clayton6d093452011-02-05 02:25:06 +0000120 ats_pos->second = section;
Greg Clayton10177aa2010-12-08 05:08:21 +0000121 }
Greg Clayton6d093452011-02-05 02:25:06 +0000122 else
123 m_addr_to_sect[load_addr] = section;
124
Greg Clayton17f69202010-09-14 23:52:43 +0000125 return true; // Changed
126}
127
128size_t
129SectionLoadList::SetSectionUnloaded (const Section *section)
130{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000131 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000132
133 if (log)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000134 {
135 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
136 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))",
Greg Clayton17f69202010-09-14 23:52:43 +0000137 __FUNCTION__,
138 section,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000139 module_file_spec.GetDirectory().AsCString(),
140 module_file_spec.GetDirectory() ? "/" : "",
141 module_file_spec.GetFilename().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +0000142 section->GetName().AsCString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000143 }
Greg Clayton17f69202010-09-14 23:52:43 +0000144
Greg Clayton17f69202010-09-14 23:52:43 +0000145 size_t unload_count = 0;
Greg Clayton10177aa2010-12-08 05:08:21 +0000146 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +0000147
148 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
149 if (sta_pos != m_sect_to_addr.end())
Greg Clayton17f69202010-09-14 23:52:43 +0000150 {
Greg Clayton6d093452011-02-05 02:25:06 +0000151 addr_t load_addr = sta_pos->second;
152 m_sect_to_addr.erase (sta_pos);
153
154 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
155 if (ats_pos != m_addr_to_sect.end())
156 m_addr_to_sect.erase (ats_pos);
157 }
Greg Clayton10177aa2010-12-08 05:08:21 +0000158
Greg Clayton17f69202010-09-14 23:52:43 +0000159 return unload_count;
160}
161
162bool
163SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr)
164{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000165 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000166
167 if (log)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000168 {
169 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
170 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)",
Greg Clayton17f69202010-09-14 23:52:43 +0000171 __FUNCTION__,
172 section,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000173 module_file_spec.GetDirectory().AsCString(),
174 module_file_spec.GetDirectory() ? "/" : "",
175 module_file_spec.GetFilename().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +0000176 section->GetName().AsCString(),
177 load_addr);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000178 }
Greg Clayton6d093452011-02-05 02:25:06 +0000179 bool erased = false;
Greg Clayton10177aa2010-12-08 05:08:21 +0000180 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +0000181 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
182 if (sta_pos != m_sect_to_addr.end())
183 {
184 erased = true;
185 m_sect_to_addr.erase (sta_pos);
186 }
187
188 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
189 if (ats_pos != m_addr_to_sect.end())
190 {
191 erased = true;
192 m_addr_to_sect.erase (ats_pos);
193 }
194
195 return erased;
Greg Clayton17f69202010-09-14 23:52:43 +0000196}
197
198
199bool
200SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
201{
Greg Clayton10177aa2010-12-08 05:08:21 +0000202 // First find the top level section that this load address exists in
203 Mutex::Locker locker(m_mutex);
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000204 if (!m_addr_to_sect.empty())
Greg Clayton17f69202010-09-14 23:52:43 +0000205 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000206 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
207 if (pos != m_addr_to_sect.end())
Greg Clayton17f69202010-09-14 23:52:43 +0000208 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000209 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
210 --pos;
211 if (load_addr >= pos->first)
Jason Molendaf830e482010-12-22 02:02:45 +0000212 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000213 addr_t offset = load_addr - pos->first;
214 if (offset < pos->second->GetByteSize())
215 {
216 // We have found the top level section, now we need to find the
217 // deepest child section.
218 return pos->second->ResolveContainedAddress (offset, so_addr);
219 }
220 }
221 }
222 else
223 {
Greg Clayton1c870d62011-05-18 18:22:47 +0000224 // There are no entries that have an address that is >= load_addr,
225 // so we need to check the last entry on our collection.
226 addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin();
227 if (load_addr >= rpos->first)
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000228 {
Greg Clayton1c870d62011-05-18 18:22:47 +0000229 addr_t offset = load_addr - rpos->first;
230 if (offset < rpos->second->GetByteSize())
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000231 {
232 // We have found the top level section, now we need to find the
233 // deepest child section.
Greg Clayton1c870d62011-05-18 18:22:47 +0000234 return rpos->second->ResolveContainedAddress (offset, so_addr);
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000235 }
Jason Molendaf830e482010-12-22 02:02:45 +0000236 }
Greg Clayton17f69202010-09-14 23:52:43 +0000237 }
238 }
239 so_addr.Clear();
240 return false;
241}
Greg Clayton10177aa2010-12-08 05:08:21 +0000242
243void
244SectionLoadList::Dump (Stream &s, Target *target)
245{
246 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +0000247 addr_to_sect_collection::const_iterator pos, end;
248 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
Greg Clayton10177aa2010-12-08 05:08:21 +0000249 {
250 s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second);
251 pos->second->Dump (&s, target, 0);
252 }
253}
254
255