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