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