SectionLoadList.cpp revision 0bfda0b1371b2381326b441adc93a6db3dc5f74f
1//===-- 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{
31    Mutex::Locker locker(m_mutex);
32    return m_addr_to_sect.empty();
33}
34
35void
36SectionLoadList::Clear ()
37{
38    Mutex::Locker locker(m_mutex);
39    m_addr_to_sect.clear();
40    m_sect_to_addr.clear();
41}
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;
48    if (section)
49    {
50        Mutex::Locker locker(m_mutex);
51        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;
55    }
56    return section_load_addr;
57}
58
59bool
60SectionLoadList::SetSectionLoadAddress (const Section *section, addr_t load_addr)
61{
62    LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
63
64    if (log)
65    {
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)",
68                     __FUNCTION__,
69                     section,
70                     module_file_spec.GetDirectory().AsCString(),
71                     module_file_spec.GetDirectory() ? "/" : "",
72                     module_file_spec.GetFilename().AsCString(),
73                     section->GetName().AsCString(),
74                     load_addr);
75    }
76
77    Mutex::Locker locker(m_mutex);
78    sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
79    if (sta_pos != m_sect_to_addr.end())
80    {
81        if (load_addr == sta_pos->second)
82            return false; // No change...
83        else
84            sta_pos->second = load_addr;
85    }
86    else
87        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())
91    {
92        assert (section != ats_pos->second);
93        ats_pos->second = section;
94    }
95    else
96        m_addr_to_sect[load_addr] = section;
97
98    return true;    // Changed
99}
100
101size_t
102SectionLoadList::SetSectionUnloaded (const Section *section)
103{
104    LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
105
106    if (log)
107    {
108        const FileSpec &module_file_spec (section->GetModule()->GetFileSpec());
109        log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))",
110                     __FUNCTION__,
111                     section,
112                     module_file_spec.GetDirectory().AsCString(),
113                     module_file_spec.GetDirectory() ? "/" : "",
114                     module_file_spec.GetFilename().AsCString(),
115                     section->GetName().AsCString());
116    }
117
118    size_t unload_count = 0;
119    Mutex::Locker locker(m_mutex);
120
121    sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section);
122    if (sta_pos != m_sect_to_addr.end())
123    {
124        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    }
131
132    return unload_count;
133}
134
135bool
136SectionLoadList::SetSectionUnloaded (const Section *section, addr_t load_addr)
137{
138    LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
139
140    if (log)
141    {
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)",
144                     __FUNCTION__,
145                     section,
146                     module_file_spec.GetDirectory().AsCString(),
147                     module_file_spec.GetDirectory() ? "/" : "",
148                     module_file_spec.GetFilename().AsCString(),
149                     section->GetName().AsCString(),
150                     load_addr);
151    }
152    bool erased = false;
153    Mutex::Locker locker(m_mutex);
154    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;
169}
170
171
172bool
173SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
174{
175    // First find the top level section that this load address exists in
176    Mutex::Locker locker(m_mutex);
177    addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
178    if (pos != m_addr_to_sect.end())
179    {
180        if (load_addr != pos->first && pos != m_addr_to_sect.begin())
181            --pos;
182        if (load_addr >= pos->first)
183        {
184            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            }
191        }
192    }
193    so_addr.Clear();
194    return false;
195}
196
197void
198SectionLoadList::Dump (Stream &s, Target *target)
199{
200    Mutex::Locker locker(m_mutex);
201    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)
203    {
204        s.Printf("addr = 0x%16.16llx, section = %p: ", pos->first, pos->second);
205        pos->second->Dump (&s, target, 0);
206    }
207}
208
209
210