blob: 09431abc00f626ca5346a824accc3fcb81d670c2 [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
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 Clayton58e844b2010-12-08 05:08:21 +000048 if (section)
49 {
50 Mutex::Locker locker(m_mutex);
Greg Clayton0bfda0b2011-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 Clayton58e844b2010-12-08 05:08:21 +000055 }
56 return section_load_addr;
Greg Clayton49480b12010-09-14 23:52:43 +000057}
58
59bool
60SectionLoadList::SetSectionLoadAddress (const Section *section, addr_t load_addr)
61{
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());
67 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)",
Greg Clayton49480b12010-09-14 23:52:43 +000068 __FUNCTION__,
69 section,
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 Clayton58e844b2010-12-08 05:08:21 +000077 Mutex::Locker locker(m_mutex);
Greg Clayton0bfda0b2011-02-05 02:25:06 +000078 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
79 if (sta_pos != m_sect_to_addr.end())
Greg Clayton49480b12010-09-14 23:52:43 +000080 {
Greg Clayton0bfda0b2011-02-05 02:25:06 +000081 if (load_addr == sta_pos->second)
Greg Clayton58e844b2010-12-08 05:08:21 +000082 return false; // No change...
83 else
Greg Clayton0bfda0b2011-02-05 02:25:06 +000084 sta_pos->second = load_addr;
Greg Clayton49480b12010-09-14 23:52:43 +000085 }
Greg Clayton58e844b2010-12-08 05:08:21 +000086 else
Greg Clayton0bfda0b2011-02-05 02:25:06 +000087 m_sect_to_addr[section] = load_addr;
88
89 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
90 if (ats_pos != m_addr_to_sect.end())
Greg Clayton58e844b2010-12-08 05:08:21 +000091 {
Greg Clayton0bfda0b2011-02-05 02:25:06 +000092 assert (section != ats_pos->second);
93 ats_pos->second = section;
Greg Clayton58e844b2010-12-08 05:08:21 +000094 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +000095 else
96 m_addr_to_sect[load_addr] = section;
97
Greg Clayton49480b12010-09-14 23:52:43 +000098 return true; // Changed
99}
100
101size_t
102SectionLoadList::SetSectionUnloaded (const Section *section)
103{
Greg Claytone005f2c2010-11-06 01:53:30 +0000104 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton49480b12010-09-14 23:52:43 +0000105
106 if (log)
Greg Clayton427f2902010-12-14 02:59:59 +0000107 {
108 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
109 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))",
Greg Clayton49480b12010-09-14 23:52:43 +0000110 __FUNCTION__,
111 section,
Greg Clayton427f2902010-12-14 02:59:59 +0000112 module_file_spec.GetDirectory().AsCString(),
113 module_file_spec.GetDirectory() ? "/" : "",
114 module_file_spec.GetFilename().AsCString(),
Greg Clayton49480b12010-09-14 23:52:43 +0000115 section->GetName().AsCString());
Greg Clayton427f2902010-12-14 02:59:59 +0000116 }
Greg Clayton49480b12010-09-14 23:52:43 +0000117
Greg Clayton49480b12010-09-14 23:52:43 +0000118 size_t unload_count = 0;
Greg Clayton58e844b2010-12-08 05:08:21 +0000119 Mutex::Locker locker(m_mutex);
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000120
121 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
122 if (sta_pos != m_sect_to_addr.end())
Greg Clayton49480b12010-09-14 23:52:43 +0000123 {
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000124 addr_t load_addr = sta_pos->second;
125 m_sect_to_addr.erase (sta_pos);
126
127 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
128 if (ats_pos != m_addr_to_sect.end())
129 m_addr_to_sect.erase (ats_pos);
130 }
Greg Clayton58e844b2010-12-08 05:08:21 +0000131
Greg Clayton49480b12010-09-14 23:52:43 +0000132 return unload_count;
133}
134
135bool
136SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr)
137{
Greg Claytone005f2c2010-11-06 01:53:30 +0000138 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton49480b12010-09-14 23:52:43 +0000139
140 if (log)
Greg Clayton427f2902010-12-14 02:59:59 +0000141 {
142 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
143 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)",
Greg Clayton49480b12010-09-14 23:52:43 +0000144 __FUNCTION__,
145 section,
Greg Clayton427f2902010-12-14 02:59:59 +0000146 module_file_spec.GetDirectory().AsCString(),
147 module_file_spec.GetDirectory() ? "/" : "",
148 module_file_spec.GetFilename().AsCString(),
Greg Clayton49480b12010-09-14 23:52:43 +0000149 section->GetName().AsCString(),
150 load_addr);
Greg Clayton427f2902010-12-14 02:59:59 +0000151 }
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000152 bool erased = false;
Greg Clayton58e844b2010-12-08 05:08:21 +0000153 Mutex::Locker locker(m_mutex);
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000154 sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
155 if (sta_pos != m_sect_to_addr.end())
156 {
157 erased = true;
158 m_sect_to_addr.erase (sta_pos);
159 }
160
161 addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
162 if (ats_pos != m_addr_to_sect.end())
163 {
164 erased = true;
165 m_addr_to_sect.erase (ats_pos);
166 }
167
168 return erased;
Greg Clayton49480b12010-09-14 23:52:43 +0000169}
170
171
172bool
173SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
174{
Greg Clayton58e844b2010-12-08 05:08:21 +0000175 // First find the top level section that this load address exists in
176 Mutex::Locker locker(m_mutex);
Greg Clayton668a3cd2011-05-17 18:13:59 +0000177 if (!m_addr_to_sect.empty())
Greg Clayton49480b12010-09-14 23:52:43 +0000178 {
Greg Clayton668a3cd2011-05-17 18:13:59 +0000179 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
180 if (pos != m_addr_to_sect.end())
Greg Clayton49480b12010-09-14 23:52:43 +0000181 {
Greg Clayton668a3cd2011-05-17 18:13:59 +0000182 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
183 --pos;
184 if (load_addr >= pos->first)
Jason Molendad4c5cd52010-12-22 02:02:45 +0000185 {
Greg Clayton668a3cd2011-05-17 18:13:59 +0000186 addr_t offset = load_addr - pos->first;
187 if (offset < pos->second->GetByteSize())
188 {
189 // We have found the top level section, now we need to find the
190 // deepest child section.
191 return pos->second->ResolveContainedAddress (offset, so_addr);
192 }
193 }
194 }
195 else
196 {
Greg Clayton289f0582011-05-18 18:22:47 +0000197 // There are no entries that have an address that is >= load_addr,
198 // so we need to check the last entry on our collection.
199 addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin();
200 if (load_addr >= rpos->first)
Greg Clayton668a3cd2011-05-17 18:13:59 +0000201 {
Greg Clayton289f0582011-05-18 18:22:47 +0000202 addr_t offset = load_addr - rpos->first;
203 if (offset < rpos->second->GetByteSize())
Greg Clayton668a3cd2011-05-17 18:13:59 +0000204 {
205 // We have found the top level section, now we need to find the
206 // deepest child section.
Greg Clayton289f0582011-05-18 18:22:47 +0000207 return rpos->second->ResolveContainedAddress (offset, so_addr);
Greg Clayton668a3cd2011-05-17 18:13:59 +0000208 }
Jason Molendad4c5cd52010-12-22 02:02:45 +0000209 }
Greg Clayton49480b12010-09-14 23:52:43 +0000210 }
211 }
212 so_addr.Clear();
213 return false;
214}
Greg Clayton58e844b2010-12-08 05:08:21 +0000215
216void
217SectionLoadList::Dump (Stream &s, Target *target)
218{
219 Mutex::Locker locker(m_mutex);
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000220 addr_to_sect_collection::const_iterator pos, end;
221 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
Greg Clayton58e844b2010-12-08 05:08:21 +0000222 {
223 s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second);
224 pos->second->Dump (&s, target, 0);
225 }
226}
227
228