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 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 
28 bool
29 SectionLoadList::IsEmpty() const
30 {
31     Mutex::Locker locker(m_mutex);
32     return m_addr_to_sect.empty();
33 }
34 
35 void
36 SectionLoadList::Clear ()
37 {
38     Mutex::Locker locker(m_mutex);
39     m_addr_to_sect.clear();
40     m_sect_to_addr.clear();
41 }
42 
43 addr_t
44 SectionLoadList::GetSectionLoadAddress (const lldb::SectionSP &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.get());
52 
53         if (pos != m_sect_to_addr.end())
54             section_load_addr = pos->second;
55     }
56     return section_load_addr;
57 }
58 
59 bool
60 SectionLoadList::SetSectionLoadAddress (const lldb::SectionSP &section, addr_t load_addr, bool warn_multiple)
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.16" PRIx64 ")",
68                      __FUNCTION__,
69                      section.get(),
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     if (section->GetByteSize() == 0)
78         return false; // No change
79 
80     // Fill in the section -> load_addr map
81     Mutex::Locker locker(m_mutex);
82     sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section.get());
83     if (sta_pos != m_sect_to_addr.end())
84     {
85         if (load_addr == sta_pos->second)
86             return false; // No change...
87         else
88             sta_pos->second = load_addr;
89     }
90     else
91         m_sect_to_addr[section.get()] = load_addr;
92 
93     // Fill in the load_addr -> section map
94     addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
95     if (ats_pos != m_addr_to_sect.end())
96     {
97         // Some sections are ok to overlap, and for others we should warn. When
98         // we have multiple load addresses that correspond to a section, we will
99         // allways attribute the section to the be last section that claims it
100         // exists at that address. Sometimes it is ok for more that one section
101         // to be loaded at a specific load address, and other times it isn't.
102         // The "warn_multiple" parameter tells us if we should warn in this case
103         // or not. The DynamicLoader plug-in subclasses should know which
104         // sections should warn and which shouldn't (darwin shared cache modules
105         // all shared the same "__LINKEDIT" sections, so the dynamic loader can
106         // pass false for "warn_multiple").
107         if (warn_multiple && section != ats_pos->second)
108         {
109             ModuleSP module_sp (section->GetModule());
110             if (module_sp)
111             {
112                 ModuleSP curr_module_sp (ats_pos->second->GetModule());
113                 if (curr_module_sp)
114                 {
115                     module_sp->ReportWarning ("address 0x%16.16" PRIx64 " maps to more than one section: %s.%s and %s.%s",
116                                               load_addr,
117                                               module_sp->GetFileSpec().GetFilename().GetCString(),
118                                               section->GetName().GetCString(),
119                                               curr_module_sp->GetFileSpec().GetFilename().GetCString(),
120                                               ats_pos->second->GetName().GetCString());
121                 }
122             }
123         }
124         ats_pos->second = section;
125     }
126     else
127         m_addr_to_sect[load_addr] = section;
128 
129     return true;    // Changed
130 }
131 
132 size_t
133 SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp)
134 {
135     size_t unload_count = 0;
136 
137     if (section_sp)
138     {
139         LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
140 
141         if (log)
142         {
143             const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
144             log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s))",
145                          __FUNCTION__,
146                          section_sp.get(),
147                          module_file_spec.GetDirectory().AsCString(),
148                          module_file_spec.GetDirectory() ? "/" : "",
149                          module_file_spec.GetFilename().AsCString(),
150                          section_sp->GetName().AsCString());
151         }
152 
153         Mutex::Locker locker(m_mutex);
154 
155         sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
156         if (sta_pos != m_sect_to_addr.end())
157         {
158             ++unload_count;
159             addr_t load_addr = sta_pos->second;
160             m_sect_to_addr.erase (sta_pos);
161 
162             addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
163             if (ats_pos != m_addr_to_sect.end())
164                 m_addr_to_sect.erase (ats_pos);
165         }
166     }
167     return unload_count;
168 }
169 
170 bool
171 SectionLoadList::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
172 {
173     LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_VERBOSE));
174 
175     if (log)
176     {
177         const FileSpec &module_file_spec (section_sp->GetModule()->GetFileSpec());
178         log->Printf ("SectionLoadList::%s (section = %p (%s%s%s.%s), load_addr = 0x%16.16" PRIx64 ")",
179                      __FUNCTION__,
180                      section_sp.get(),
181                      module_file_spec.GetDirectory().AsCString(),
182                      module_file_spec.GetDirectory() ? "/" : "",
183                      module_file_spec.GetFilename().AsCString(),
184                      section_sp->GetName().AsCString(),
185                      load_addr);
186     }
187     bool erased = false;
188     Mutex::Locker locker(m_mutex);
189     sect_to_addr_collection::iterator sta_pos = m_sect_to_addr.find(section_sp.get());
190     if (sta_pos != m_sect_to_addr.end())
191     {
192         erased = true;
193         m_sect_to_addr.erase (sta_pos);
194     }
195 
196     addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
197     if (ats_pos != m_addr_to_sect.end())
198     {
199         erased = true;
200         m_addr_to_sect.erase (ats_pos);
201     }
202 
203     return erased;
204 }
205 
206 
207 bool
208 SectionLoadList::ResolveLoadAddress (addr_t load_addr, Address &so_addr) const
209 {
210     // First find the top level section that this load address exists in
211     Mutex::Locker locker(m_mutex);
212     if (!m_addr_to_sect.empty())
213     {
214         addr_to_sect_collection::const_iterator pos = m_addr_to_sect.lower_bound (load_addr);
215         if (pos != m_addr_to_sect.end())
216         {
217             if (load_addr != pos->first && pos != m_addr_to_sect.begin())
218                 --pos;
219             const addr_t pos_load_addr = pos->first;
220             if (load_addr >= pos_load_addr)
221             {
222                 addr_t offset = load_addr - pos_load_addr;
223                 if (offset < pos->second->GetByteSize())
224                 {
225                     // We have found the top level section, now we need to find the
226                     // deepest child section.
227                     return pos->second->ResolveContainedAddress (offset, so_addr);
228                 }
229             }
230         }
231         else
232         {
233             // There are no entries that have an address that is >= load_addr,
234             // so we need to check the last entry on our collection.
235             addr_to_sect_collection::const_reverse_iterator rpos = m_addr_to_sect.rbegin();
236             if (load_addr >= rpos->first)
237             {
238                 addr_t offset = load_addr - rpos->first;
239                 if (offset < rpos->second->GetByteSize())
240                 {
241                     // We have found the top level section, now we need to find the
242                     // deepest child section.
243                     return rpos->second->ResolveContainedAddress (offset, so_addr);
244                 }
245             }
246         }
247     }
248     so_addr.Clear();
249     return false;
250 }
251 
252 void
253 SectionLoadList::Dump (Stream &s, Target *target)
254 {
255     Mutex::Locker locker(m_mutex);
256     addr_to_sect_collection::const_iterator pos, end;
257     for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end; ++pos)
258     {
259         s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first, pos->second.get());
260         pos->second->Dump (&s, target, 0);
261     }
262 }
263 
264 
265