blob: ac9f8180c48ef9d9f8e72b7f44e18612533459b6 [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);
32 return m_collection.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);
39 return m_collection.clear();
Greg Clayton17f69202010-09-14 23:52:43 +000040}
41
42addr_t
43SectionLoadList::GetSectionLoadAddress (const Section *section) const
44{
45 // TODO: add support for the same section having multiple load addresses
46 addr_t section_load_addr = LLDB_INVALID_ADDRESS;
Greg Clayton10177aa2010-12-08 05:08:21 +000047 if (section)
48 {
49 Mutex::Locker locker(m_mutex);
50 collection::const_iterator pos, end = m_collection.end();
51 for (pos = m_collection.begin(); pos != end; ++pos)
52 {
53 const addr_t pos_load_addr = pos->first;
54 const Section *pos_section = pos->second;
55 if (pos_section == section)
56 {
57 section_load_addr = pos_load_addr;
58 break;
59 }
60 }
61 }
62 return section_load_addr;
Greg Clayton17f69202010-09-14 23:52:43 +000063}
64
65bool
66SectionLoadList::SetSectionLoadAddress (const Section *section, addr_t load_addr)
67{
Greg Clayton2d4edfb2010-11-06 01:53:30 +000068 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +000069
70 if (log)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000071 {
72 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
73 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16llx)",
Greg Clayton17f69202010-09-14 23:52:43 +000074 __FUNCTION__,
75 section,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000076 module_file_spec.GetDirectory().AsCString(),
77 module_file_spec.GetDirectory() ? "/" : "",
78 module_file_spec.GetFilename().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +000079 section->GetName().AsCString(),
80 load_addr);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000081 }
Greg Clayton17f69202010-09-14 23:52:43 +000082
Greg Clayton10177aa2010-12-08 05:08:21 +000083 Mutex::Locker locker(m_mutex);
84 collection::iterator pos = m_collection.find(load_addr);
85 if (pos != m_collection.end())
Greg Clayton17f69202010-09-14 23:52:43 +000086 {
Greg Clayton10177aa2010-12-08 05:08:21 +000087 if (section == pos->second)
88 return false; // No change...
89 else
90 pos->second = section;
Greg Clayton17f69202010-09-14 23:52:43 +000091 }
Greg Clayton10177aa2010-12-08 05:08:21 +000092 else
93 {
94 m_collection[load_addr] = section;
95 }
Greg Clayton17f69202010-09-14 23:52:43 +000096 return true; // Changed
97}
98
99size_t
100SectionLoadList::SetSectionUnloaded (const Section *section)
101{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000102 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000103
104 if (log)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000105 {
106 const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
107 log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))",
Greg Clayton17f69202010-09-14 23:52:43 +0000108 __FUNCTION__,
109 section,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000110 module_file_spec.GetDirectory().AsCString(),
111 module_file_spec.GetDirectory() ? "/" : "",
112 module_file_spec.GetFilename().AsCString(),
Greg Clayton17f69202010-09-14 23:52:43 +0000113 section->GetName().AsCString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000114 }
Greg Clayton17f69202010-09-14 23:52:43 +0000115
Greg Clayton17f69202010-09-14 23:52:43 +0000116 size_t unload_count = 0;
Greg Clayton10177aa2010-12-08 05:08:21 +0000117 Mutex::Locker locker(m_mutex);
118 bool erased = false;
119 do
Greg Clayton17f69202010-09-14 23:52:43 +0000120 {
Greg Clayton10177aa2010-12-08 05:08:21 +0000121 erased = false;
122 for (collection::iterator pos = m_collection.begin(); pos != m_collection.end(); ++pos)
123 {
124 if (pos->second == section)
125 {
126 m_collection.erase(pos);
127 erased = true;
128 }
129 }
130 } while (erased);
131
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 Clayton10177aa2010-12-08 05:08:21 +0000152 Mutex::Locker locker(m_mutex);
153 return m_collection.erase (load_addr) != 0;
Greg Clayton17f69202010-09-14 23:52:43 +0000154}
155
156
157bool
158SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
159{
Greg Clayton10177aa2010-12-08 05:08:21 +0000160 // First find the top level section that this load address exists in
161 Mutex::Locker locker(m_mutex);
162 collection::const_iterator pos = m_collection.lower_bound (load_addr);
163 if (pos != m_collection.end())
Greg Clayton17f69202010-09-14 23:52:43 +0000164 {
Greg Clayton10177aa2010-12-08 05:08:21 +0000165 if (load_addr != pos->first && pos != m_collection.begin())
166 --pos;
Jason Molendaf830e482010-12-22 02:02:45 +0000167 if (load_addr >= pos->first)
Greg Clayton17f69202010-09-14 23:52:43 +0000168 {
Jason Molendaf830e482010-12-22 02:02:45 +0000169 addr_t offset = load_addr - pos->first;
170 if (offset < pos->second->GetByteSize())
171 {
172 // We have found the top level section, now we need to find the
173 // deepest child section.
174 return pos->second->ResolveContainedAddress (offset, so_addr);
175 }
Greg Clayton17f69202010-09-14 23:52:43 +0000176 }
177 }
178 so_addr.Clear();
179 return false;
180}
Greg Clayton10177aa2010-12-08 05:08:21 +0000181
182void
183SectionLoadList::Dump (Stream &s, Target *target)
184{
185 Mutex::Locker locker(m_mutex);
186 collection::const_iterator pos, end;
187 for (pos = m_collection.begin(), end = m_collection.end(); pos != end; ++pos)
188 {
189 s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second);
190 pos->second->Dump (&s, target, 0);
191 }
192}
193
194