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