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