blob: e3ffed1310fb5a5b184ed86333baecb03289931b [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 Clayton10177aa2010-12-08 05:08:21 +000077 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-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 Clayton17f69202010-09-14 23:52:43 +000080 {
Greg Clayton6d093452011-02-05 02:25:06 +000081 if (load_addr == sta_pos->second)
Greg Clayton10177aa2010-12-08 05:08:21 +000082 return false; // No change...
83 else
Greg Clayton6d093452011-02-05 02:25:06 +000084 sta_pos->second = load_addr;
Greg Clayton17f69202010-09-14 23:52:43 +000085 }
Greg Clayton10177aa2010-12-08 05:08:21 +000086 else
Greg Clayton6d093452011-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 Clayton10177aa2010-12-08 05:08:21 +000091 {
Greg Clayton6d093452011-02-05 02:25:06 +000092 assert (section != ats_pos->second);
93 ats_pos->second = section;
Greg Clayton10177aa2010-12-08 05:08:21 +000094 }
Greg Clayton6d093452011-02-05 02:25:06 +000095 else
96 m_addr_to_sect[load_addr] = section;
97
Greg Clayton17f69202010-09-14 23:52:43 +000098 return true; // Changed
99}
100
101size_t
102SectionLoadList::SetSectionUnloaded (const Section *section)
103{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000104 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000105
106 if (log)
Greg Clayton8b2fe6d2010-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 Clayton17f69202010-09-14 23:52:43 +0000110 __FUNCTION__,
111 section,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000112 module_file_spec.GetDirectory().AsCString(),
113 module_file_spec.GetDirectory() ? "/" : "",
114 module_file_spec.GetFilename().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +0000115 section->GetName().AsCString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000116 }
Greg Clayton17f69202010-09-14 23:52:43 +0000117
Greg Clayton17f69202010-09-14 23:52:43 +0000118 size_t unload_count = 0;
Greg Clayton10177aa2010-12-08 05:08:21 +0000119 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-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 Clayton17f69202010-09-14 23:52:43 +0000123 {
Greg Clayton6d093452011-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 Clayton10177aa2010-12-08 05:08:21 +0000131
Greg Clayton17f69202010-09-14 23:52:43 +0000132 return unload_count;
133}
134
135bool
136SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr)
137{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000138 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000139
140 if (log)
Greg Clayton8b2fe6d2010-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 Clayton17f69202010-09-14 23:52:43 +0000144 __FUNCTION__,
145 section,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000146 module_file_spec.GetDirectory().AsCString(),
147 module_file_spec.GetDirectory() ? "/" : "",
148 module_file_spec.GetFilename().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +0000149 section->GetName().AsCString(),
150 load_addr);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000151 }
Greg Clayton6d093452011-02-05 02:25:06 +0000152 bool erased = false;
Greg Clayton10177aa2010-12-08 05:08:21 +0000153 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-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 Clayton17f69202010-09-14 23:52:43 +0000169}
170
171
172bool
173SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
174{
Greg Clayton10177aa2010-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 Clayton6d093452011-02-05 02:25:06 +0000177 addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
178 if (pos != m_addr_to_sect.end())
Greg Clayton17f69202010-09-14 23:52:43 +0000179 {
Greg Clayton6d093452011-02-05 02:25:06 +0000180 if (load_addr != pos->first && pos != m_addr_to_sect.begin())
Greg Clayton10177aa2010-12-08 05:08:21 +0000181 --pos;
Jason Molendaf830e482010-12-22 02:02:45 +0000182 if (load_addr >= pos->first)
Greg Clayton17f69202010-09-14 23:52:43 +0000183 {
Jason Molendaf830e482010-12-22 02:02:45 +0000184 addr_t offset = load_addr - pos->first;
185 if (offset < pos->second->GetByteSize())
186 {
187 // We have found the top level section, now we need to find the
188 // deepest child section.
189 return pos->second->ResolveContainedAddress (offset, so_addr);
190 }
Greg Clayton17f69202010-09-14 23:52:43 +0000191 }
192 }
193 so_addr.Clear();
194 return false;
195}
Greg Clayton10177aa2010-12-08 05:08:21 +0000196
197void
198SectionLoadList::Dump (Stream &s, Target *target)
199{
200 Mutex::Locker locker(m_mutex);
Greg Clayton6d093452011-02-05 02:25:06 +0000201 addr_to_sect_collection::const_iterator pos, end;
202 for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
Greg Clayton10177aa2010-12-08 05:08:21 +0000203 {
204 s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second);
205 pos->second->Dump (&s, target, 0);
206 }
207}
208
209