blob: 05a97a297e96bb46d4ae73d71441a6e82b4c4b67 [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
60SectionLoadList::SetSectionLoadAddress (const Section *section, addr_t load_addr)
61{
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 Clayton10177aa2010-12-08 05:08:21 +000080 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +000081 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
82 if (sta_pos != m_sect_to_addr.end())
Greg Clayton17f69202010-09-14 23:52:43 +000083 {
Greg Clayton6d093452011-02-05 02:25:06 +000084 if (load_addr == sta_pos->second)
Greg Clayton10177aa2010-12-08 05:08:21 +000085 return false; // No change...
86 else
Greg Clayton6d093452011-02-05 02:25:06 +000087 sta_pos->second = load_addr;
Greg Clayton17f69202010-09-14 23:52:43 +000088 }
Greg Clayton10177aa2010-12-08 05:08:21 +000089 else
Greg Clayton6d093452011-02-05 02:25:06 +000090 m_sect_to_addr[section] = load_addr;
91
92 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
93 if (ats_pos != m_addr_to_sect.end())
Greg Clayton10177aa2010-12-08 05:08:21 +000094 {
Greg Claytonc859e2d2012-02-13 23:10:39 +000095 if (section != ats_pos->second)
96 {
Greg Claytone72dfb32012-02-24 01:59:29 +000097 ModuleSP module_sp (section->GetModule());
98 if (module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +000099 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000100 module_sp->ReportWarning ("address 0x%16.16llx maps to more than one section: %s.%s and %s.%s",
101 load_addr,
102 module_sp->GetFileSpec().GetFilename().GetCString(),
103 section->GetName().GetCString(),
104 ats_pos->second->GetModule()->GetFileSpec().GetFilename().GetCString(),
105 ats_pos->second->GetName().GetCString());
Greg Claytonc859e2d2012-02-13 23:10:39 +0000106 }
107 }
Greg Clayton6d093452011-02-05 02:25:06 +0000108 ats_pos->second = section;
Greg Clayton10177aa2010-12-08 05:08:21 +0000109 }
Greg Clayton6d093452011-02-05 02:25:06 +0000110 else
111 m_addr_to_sect[load_addr] = section;
112
Greg Clayton17f69202010-09-14 23:52:43 +0000113 return true; // Changed
114}
115
116size_t
117SectionLoadList::SetSectionUnloaded (const Section *section)
118{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000119 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000120
121 if (log)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000122 {
123 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
124 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))",
Greg Clayton17f69202010-09-14 23:52:43 +0000125 __FUNCTION__,
126 section,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000127 module_file_spec.GetDirectory().AsCString(),
128 module_file_spec.GetDirectory() ? "/" : "",
129 module_file_spec.GetFilename().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +0000130 section->GetName().AsCString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000131 }
Greg Clayton17f69202010-09-14 23:52:43 +0000132
Greg Clayton17f69202010-09-14 23:52:43 +0000133 size_t unload_count = 0;
Greg Clayton10177aa2010-12-08 05:08:21 +0000134 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +0000135
136 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
137 if (sta_pos != m_sect_to_addr.end())
Greg Clayton17f69202010-09-14 23:52:43 +0000138 {
Greg Clayton6d093452011-02-05 02:25:06 +0000139 addr_t load_addr = sta_pos->second;
140 m_sect_to_addr.erase (sta_pos);
141
142 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
143 if (ats_pos != m_addr_to_sect.end())
144 m_addr_to_sect.erase (ats_pos);
145 }
Greg Clayton10177aa2010-12-08 05:08:21 +0000146
Greg Clayton17f69202010-09-14 23:52:43 +0000147 return unload_count;
148}
149
150bool
151SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr)
152{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000153 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000154
155 if (log)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000156 {
157 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
158 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)",
Greg Clayton17f69202010-09-14 23:52:43 +0000159 __FUNCTION__,
160 section,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000161 module_file_spec.GetDirectory().AsCString(),
162 module_file_spec.GetDirectory() ? "/" : "",
163 module_file_spec.GetFilename().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +0000164 section->GetName().AsCString(),
165 load_addr);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000166 }
Greg Clayton6d093452011-02-05 02:25:06 +0000167 bool erased = false;
Greg Clayton10177aa2010-12-08 05:08:21 +0000168 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +0000169 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
170 if (sta_pos != m_sect_to_addr.end())
171 {
172 erased = true;
173 m_sect_to_addr.erase (sta_pos);
174 }
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 {
179 erased = true;
180 m_addr_to_sect.erase (ats_pos);
181 }
182
183 return erased;
Greg Clayton17f69202010-09-14 23:52:43 +0000184}
185
186
187bool
188SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
189{
Greg Clayton10177aa2010-12-08 05:08:21 +0000190 // First find the top level section that this load address exists in
191 Mutex::Locker locker(m_mutex);
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000192 if (!m_addr_to_sect.empty())
Greg Clayton17f69202010-09-14 23:52:43 +0000193 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000194 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
195 if (pos != m_addr_to_sect.end())
Greg Clayton17f69202010-09-14 23:52:43 +0000196 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000197 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
198 --pos;
199 if (load_addr >= pos->first)
Jason Molendaf830e482010-12-22 02:02:45 +0000200 {
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000201 addr_t offset = load_addr - pos->first;
202 if (offset < pos->second->GetByteSize())
203 {
204 // We have found the top level section, now we need to find the
205 // deepest child section.
206 return pos->second->ResolveContainedAddress (offset, so_addr);
207 }
208 }
209 }
210 else
211 {
Greg Clayton1c870d62011-05-18 18:22:47 +0000212 // There are no entries that have an address that is >= load_addr,
213 // so we need to check the last entry on our collection.
214 addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin();
215 if (load_addr >= rpos->first)
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000216 {
Greg Clayton1c870d62011-05-18 18:22:47 +0000217 addr_t offset = load_addr - rpos->first;
218 if (offset < rpos->second->GetByteSize())
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000219 {
220 // We have found the top level section, now we need to find the
221 // deepest child section.
Greg Clayton1c870d62011-05-18 18:22:47 +0000222 return rpos->second->ResolveContainedAddress (offset, so_addr);
Greg Claytonc5e9a7d2011-05-17 18:13:59 +0000223 }
Jason Molendaf830e482010-12-22 02:02:45 +0000224 }
Greg Clayton17f69202010-09-14 23:52:43 +0000225 }
226 }
227 so_addr.Clear();
228 return false;
229}
Greg Clayton10177aa2010-12-08 05:08:21 +0000230
231void
232SectionLoadList::Dump (Stream &s, Target *target)
233{
234 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +0000235 addr_to_sect_collection::const_iterator pos, end;
236 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
Greg Clayton10177aa2010-12-08 05:08:21 +0000237 {
238 s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second);
239 pos->second->Dump (&s, target, 0);
240 }
241}
242
243