blob: 0dcbba42fcb4039fb74b01251f928b13aff65868 [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)
71 log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16llx)",
72 __FUNCTION__,
73 section,
74 section->GetModule()->GetFileSpec().GetFilename().AsCString(),
75 section->GetName().AsCString(),
76 load_addr);
77
Greg Clayton10177aa2010-12-08 05:08:21 +000078 Mutex::Locker locker(m_mutex);
79 collection::iterator pos = m_collection.find(load_addr);
80 if (pos != m_collection.end())
Greg Clayton17f69202010-09-14 23:52:43 +000081 {
Greg Clayton10177aa2010-12-08 05:08:21 +000082 if (section == pos->second)
83 return false; // No change...
84 else
85 pos->second = section;
Greg Clayton17f69202010-09-14 23:52:43 +000086 }
Greg Clayton10177aa2010-12-08 05:08:21 +000087 else
88 {
89 m_collection[load_addr] = section;
90 }
Greg Clayton17f69202010-09-14 23:52:43 +000091 return true; // Changed
92}
93
94size_t
95SectionLoadList::SetSectionUnloaded (const Section *section)
96{
Greg Clayton2d4edfb2010-11-06 01:53:30 +000097 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +000098
99 if (log)
100 log->Printf ("SectionLoadList::%s (section = %p (%s.%s))",
101 __FUNCTION__,
102 section,
103 section->GetModule()->GetFileSpec().GetFilename().AsCString(),
104 section->GetName().AsCString());
105
Greg Clayton17f69202010-09-14 23:52:43 +0000106 size_t unload_count = 0;
Greg Clayton10177aa2010-12-08 05:08:21 +0000107 Mutex::Locker locker(m_mutex);
108 bool erased = false;
109 do
Greg Clayton17f69202010-09-14 23:52:43 +0000110 {
Greg Clayton10177aa2010-12-08 05:08:21 +0000111 erased = false;
112 for (collection::iterator pos = m_collection.begin(); pos != m_collection.end(); ++pos)
113 {
114 if (pos->second == section)
115 {
116 m_collection.erase(pos);
117 erased = true;
118 }
119 }
120 } while (erased);
121
Greg Clayton17f69202010-09-14 23:52:43 +0000122 return unload_count;
123}
124
125bool
126SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr)
127{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000128 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
Greg Clayton17f69202010-09-14 23:52:43 +0000129
130 if (log)
131 log->Printf ("SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16llx)",
132 __FUNCTION__,
133 section,
134 section->GetModule()->GetFileSpec().GetFilename().AsCString(),
135 section->GetName().AsCString(),
136 load_addr);
Greg Clayton10177aa2010-12-08 05:08:21 +0000137 Mutex::Locker locker(m_mutex);
138 return m_collection.erase (load_addr) != 0;
Greg Clayton17f69202010-09-14 23:52:43 +0000139}
140
141
142bool
143SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
144{
Greg Clayton10177aa2010-12-08 05:08:21 +0000145 // First find the top level section that this load address exists in
146 Mutex::Locker locker(m_mutex);
147 collection::const_iterator pos = m_collection.lower_bound (load_addr);
148 if (pos != m_collection.end())
Greg Clayton17f69202010-09-14 23:52:43 +0000149 {
Greg Clayton10177aa2010-12-08 05:08:21 +0000150 if (load_addr != pos->first && pos != m_collection.begin())
151 --pos;
152 assert (load_addr >= pos->first);
153 addr_t offset = load_addr - pos->first;
154 if (offset < pos->second->GetByteSize())
Greg Clayton17f69202010-09-14 23:52:43 +0000155 {
156 // We have found the top level section, now we need to find the
157 // deepest child section.
Greg Clayton10177aa2010-12-08 05:08:21 +0000158 return pos->second->ResolveContainedAddress (offset, so_addr);
Greg Clayton17f69202010-09-14 23:52:43 +0000159 }
160 }
161 so_addr.Clear();
162 return false;
163}
Greg Clayton10177aa2010-12-08 05:08:21 +0000164
165void
166SectionLoadList::Dump (Stream &s, Target *target)
167{
168 Mutex::Locker locker(m_mutex);
169 collection::const_iterator pos, end;
170 for (pos = m_collection.begin(), end = m_collection.end(); pos != end; ++pos)
171 {
172 s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second);
173 pos->second->Dump (&s, target, 0);
174 }
175}
176
177