xref: /src/contrib/llvm-project/lldb/source/Target/SectionLoadList.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1cfca06d7SDimitry Andric //===-- SectionLoadList.cpp -----------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/Target/SectionLoadList.h"
10f034231aSEd Maste 
11f034231aSEd Maste #include "lldb/Core/Module.h"
12f034231aSEd Maste #include "lldb/Core/Section.h"
13f034231aSEd Maste #include "lldb/Symbol/Block.h"
14f034231aSEd Maste #include "lldb/Symbol/Symbol.h"
15f034231aSEd Maste #include "lldb/Symbol/SymbolContext.h"
16145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
1774a628f7SDimitry Andric #include "lldb/Utility/Log.h"
1874a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
19f034231aSEd Maste 
20f034231aSEd Maste using namespace lldb;
21f034231aSEd Maste using namespace lldb_private;
22f034231aSEd Maste 
SectionLoadList(const SectionLoadList & rhs)2314f1b3e8SDimitry Andric SectionLoadList::SectionLoadList(const SectionLoadList &rhs)
2414f1b3e8SDimitry Andric     : m_addr_to_sect(), m_sect_to_addr(), m_mutex() {
25f3fbd1c0SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(rhs.m_mutex);
26866dcdacSEd Maste   m_addr_to_sect = rhs.m_addr_to_sect;
27866dcdacSEd Maste   m_sect_to_addr = rhs.m_sect_to_addr;
28866dcdacSEd Maste }
29866dcdacSEd Maste 
operator =(const SectionLoadList & rhs)3014f1b3e8SDimitry Andric void SectionLoadList::operator=(const SectionLoadList &rhs) {
315f29bb8aSDimitry Andric   std::lock(m_mutex, rhs.m_mutex);
325f29bb8aSDimitry Andric   std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex, std::adopt_lock);
335f29bb8aSDimitry Andric   std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex, std::adopt_lock);
34866dcdacSEd Maste   m_addr_to_sect = rhs.m_addr_to_sect;
35866dcdacSEd Maste   m_sect_to_addr = rhs.m_sect_to_addr;
36866dcdacSEd Maste }
37866dcdacSEd Maste 
IsEmpty() const3814f1b3e8SDimitry Andric bool SectionLoadList::IsEmpty() const {
39f3fbd1c0SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
40f034231aSEd Maste   return m_addr_to_sect.empty();
41f034231aSEd Maste }
42f034231aSEd Maste 
Clear()4314f1b3e8SDimitry Andric void SectionLoadList::Clear() {
44f3fbd1c0SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
45f034231aSEd Maste   m_addr_to_sect.clear();
46f034231aSEd Maste   m_sect_to_addr.clear();
47f034231aSEd Maste }
48f034231aSEd Maste 
49f034231aSEd Maste addr_t
GetSectionLoadAddress(const lldb::SectionSP & section) const5014f1b3e8SDimitry Andric SectionLoadList::GetSectionLoadAddress(const lldb::SectionSP &section) const {
51f034231aSEd Maste   // TODO: add support for the same section having multiple load addresses
52f034231aSEd Maste   addr_t section_load_addr = LLDB_INVALID_ADDRESS;
5314f1b3e8SDimitry Andric   if (section) {
54f3fbd1c0SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(m_mutex);
5514f1b3e8SDimitry Andric     sect_to_addr_collection::const_iterator pos =
5614f1b3e8SDimitry Andric         m_sect_to_addr.find(section.get());
57f034231aSEd Maste 
58f034231aSEd Maste     if (pos != m_sect_to_addr.end())
59f034231aSEd Maste       section_load_addr = pos->second;
60f034231aSEd Maste   }
61f034231aSEd Maste   return section_load_addr;
62f034231aSEd Maste }
63f034231aSEd Maste 
SetSectionLoadAddress(const lldb::SectionSP & section,addr_t load_addr,bool warn_multiple)6414f1b3e8SDimitry Andric bool SectionLoadList::SetSectionLoadAddress(const lldb::SectionSP &section,
6514f1b3e8SDimitry Andric                                             addr_t load_addr,
6614f1b3e8SDimitry Andric                                             bool warn_multiple) {
67145449b1SDimitry Andric   Log *log = GetLog(LLDBLog::DynamicLoader);
68f034231aSEd Maste   ModuleSP module_sp(section->GetModule());
69f034231aSEd Maste 
7014f1b3e8SDimitry Andric   if (module_sp) {
7174a628f7SDimitry Andric     LLDB_LOGV(log, "(section = {0} ({1}.{2}), load_addr = {3:x}) module = {4}",
7274a628f7SDimitry Andric               section.get(), module_sp->GetFileSpec(), section->GetName(),
7374a628f7SDimitry Andric               load_addr, module_sp.get());
74f034231aSEd Maste 
75f034231aSEd Maste     if (section->GetByteSize() == 0)
76f034231aSEd Maste       return false; // No change
77f034231aSEd Maste 
78f034231aSEd Maste     // Fill in the section -> load_addr map
79f3fbd1c0SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(m_mutex);
8014f1b3e8SDimitry Andric     sect_to_addr_collection::iterator sta_pos =
8114f1b3e8SDimitry Andric         m_sect_to_addr.find(section.get());
8214f1b3e8SDimitry Andric     if (sta_pos != m_sect_to_addr.end()) {
83f034231aSEd Maste       if (load_addr == sta_pos->second)
84f034231aSEd Maste         return false; // No change...
85f034231aSEd Maste       else
86f034231aSEd Maste         sta_pos->second = load_addr;
8714f1b3e8SDimitry Andric     } else
88f034231aSEd Maste       m_sect_to_addr[section.get()] = load_addr;
89f034231aSEd Maste 
90f034231aSEd Maste     // Fill in the load_addr -> section map
91f034231aSEd Maste     addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
9214f1b3e8SDimitry Andric     if (ats_pos != m_addr_to_sect.end()) {
93f034231aSEd Maste       // Some sections are ok to overlap, and for others we should warn. When
94f034231aSEd Maste       // we have multiple load addresses that correspond to a section, we will
950cac4ca3SEd Maste       // always attribute the section to the be last section that claims it
96f034231aSEd Maste       // exists at that address. Sometimes it is ok for more that one section
97f73363f1SDimitry Andric       // to be loaded at a specific load address, and other times it isn't. The
98f73363f1SDimitry Andric       // "warn_multiple" parameter tells us if we should warn in this case or
99f73363f1SDimitry Andric       // not. The DynamicLoader plug-in subclasses should know which sections
100f73363f1SDimitry Andric       // should warn and which shouldn't (darwin shared cache modules all
101f73363f1SDimitry Andric       // shared the same "__LINKEDIT" sections, so the dynamic loader can pass
102f73363f1SDimitry Andric       // false for "warn_multiple").
10314f1b3e8SDimitry Andric       if (warn_multiple && section != ats_pos->second) {
104f034231aSEd Maste         ModuleSP module_sp(section->GetModule());
10514f1b3e8SDimitry Andric         if (module_sp) {
106f034231aSEd Maste           ModuleSP curr_module_sp(ats_pos->second->GetModule());
10714f1b3e8SDimitry Andric           if (curr_module_sp) {
10814f1b3e8SDimitry Andric             module_sp->ReportWarning(
109e3b55780SDimitry Andric                 "address {0:x16} maps to more than one section: {1}.{2} and "
110e3b55780SDimitry Andric                 "{3}.{4}",
11114f1b3e8SDimitry Andric                 load_addr, module_sp->GetFileSpec().GetFilename().GetCString(),
112f034231aSEd Maste                 section->GetName().GetCString(),
113f034231aSEd Maste                 curr_module_sp->GetFileSpec().GetFilename().GetCString(),
114f034231aSEd Maste                 ats_pos->second->GetName().GetCString());
115f034231aSEd Maste           }
116f034231aSEd Maste         }
117f034231aSEd Maste       }
118f034231aSEd Maste       ats_pos->second = section;
119e3b55780SDimitry Andric     } else {
120e3b55780SDimitry Andric       // Remove the old address->section entry, if
121e3b55780SDimitry Andric       // there is one.
122e3b55780SDimitry Andric       for (const auto &entry : m_addr_to_sect) {
123e3b55780SDimitry Andric         if (entry.second == section) {
124e3b55780SDimitry Andric           const auto &it_pos = m_addr_to_sect.find(entry.first);
125e3b55780SDimitry Andric           m_addr_to_sect.erase(it_pos);
126e3b55780SDimitry Andric           break;
127e3b55780SDimitry Andric         }
128e3b55780SDimitry Andric       }
129f034231aSEd Maste       m_addr_to_sect[load_addr] = section;
130e3b55780SDimitry Andric     }
131f034231aSEd Maste     return true; // Changed
132f034231aSEd Maste 
13314f1b3e8SDimitry Andric   } else {
13414f1b3e8SDimitry Andric     if (log) {
135ead24645SDimitry Andric       LLDB_LOGF(
136ead24645SDimitry Andric           log,
13714f1b3e8SDimitry Andric           "SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64
13814f1b3e8SDimitry Andric           ") error: module has been deleted",
1390cac4ca3SEd Maste           __FUNCTION__, static_cast<void *>(section.get()),
14014f1b3e8SDimitry Andric           section->GetName().AsCString(), load_addr);
141f034231aSEd Maste     }
142f034231aSEd Maste   }
143f034231aSEd Maste   return false;
144f034231aSEd Maste }
145f034231aSEd Maste 
SetSectionUnloaded(const lldb::SectionSP & section_sp)14614f1b3e8SDimitry Andric size_t SectionLoadList::SetSectionUnloaded(const lldb::SectionSP &section_sp) {
147f034231aSEd Maste   size_t unload_count = 0;
148f034231aSEd Maste 
14914f1b3e8SDimitry Andric   if (section_sp) {
150145449b1SDimitry Andric     Log *log = GetLog(LLDBLog::DynamicLoader);
151f034231aSEd Maste 
15274a628f7SDimitry Andric     if (log && log->GetVerbose()) {
153f3fbd1c0SDimitry Andric       ModuleSP module_sp = section_sp->GetModule();
154f3fbd1c0SDimitry Andric       std::string module_name("<Unknown>");
15514f1b3e8SDimitry Andric       if (module_sp) {
15614f1b3e8SDimitry Andric         const FileSpec &module_file_spec(
15714f1b3e8SDimitry Andric             section_sp->GetModule()->GetFileSpec());
158f3fbd1c0SDimitry Andric         module_name = module_file_spec.GetPath();
159f3fbd1c0SDimitry Andric       }
160ead24645SDimitry Andric       LLDB_LOGF(log, "SectionLoadList::%s (section = %p (%s.%s))", __FUNCTION__,
16114f1b3e8SDimitry Andric                 static_cast<void *>(section_sp.get()), module_name.c_str(),
162f034231aSEd Maste                 section_sp->GetName().AsCString());
163f034231aSEd Maste     }
164f034231aSEd Maste 
165f3fbd1c0SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(m_mutex);
166f034231aSEd Maste 
16714f1b3e8SDimitry Andric     sect_to_addr_collection::iterator sta_pos =
16814f1b3e8SDimitry Andric         m_sect_to_addr.find(section_sp.get());
16914f1b3e8SDimitry Andric     if (sta_pos != m_sect_to_addr.end()) {
170f034231aSEd Maste       ++unload_count;
171f034231aSEd Maste       addr_t load_addr = sta_pos->second;
172f034231aSEd Maste       m_sect_to_addr.erase(sta_pos);
173f034231aSEd Maste 
17414f1b3e8SDimitry Andric       addr_to_sect_collection::iterator ats_pos =
17514f1b3e8SDimitry Andric           m_addr_to_sect.find(load_addr);
176f034231aSEd Maste       if (ats_pos != m_addr_to_sect.end())
177f034231aSEd Maste         m_addr_to_sect.erase(ats_pos);
178f034231aSEd Maste     }
179f034231aSEd Maste   }
180f034231aSEd Maste   return unload_count;
181f034231aSEd Maste }
182f034231aSEd Maste 
SetSectionUnloaded(const lldb::SectionSP & section_sp,addr_t load_addr)18314f1b3e8SDimitry Andric bool SectionLoadList::SetSectionUnloaded(const lldb::SectionSP &section_sp,
18414f1b3e8SDimitry Andric                                          addr_t load_addr) {
185145449b1SDimitry Andric   Log *log = GetLog(LLDBLog::DynamicLoader);
186f034231aSEd Maste 
18774a628f7SDimitry Andric   if (log && log->GetVerbose()) {
188f3fbd1c0SDimitry Andric     ModuleSP module_sp = section_sp->GetModule();
189f3fbd1c0SDimitry Andric     std::string module_name("<Unknown>");
19014f1b3e8SDimitry Andric     if (module_sp) {
191f034231aSEd Maste       const FileSpec &module_file_spec(section_sp->GetModule()->GetFileSpec());
192f3fbd1c0SDimitry Andric       module_name = module_file_spec.GetPath();
193f3fbd1c0SDimitry Andric     }
194ead24645SDimitry Andric     LLDB_LOGF(
195ead24645SDimitry Andric         log,
19614f1b3e8SDimitry Andric         "SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64
19714f1b3e8SDimitry Andric         ")",
1980cac4ca3SEd Maste         __FUNCTION__, static_cast<void *>(section_sp.get()),
19914f1b3e8SDimitry Andric         module_name.c_str(), section_sp->GetName().AsCString(), load_addr);
200f034231aSEd Maste   }
201f034231aSEd Maste   bool erased = false;
202f3fbd1c0SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
20314f1b3e8SDimitry Andric   sect_to_addr_collection::iterator sta_pos =
20414f1b3e8SDimitry Andric       m_sect_to_addr.find(section_sp.get());
20514f1b3e8SDimitry Andric   if (sta_pos != m_sect_to_addr.end()) {
206f034231aSEd Maste     erased = true;
207f034231aSEd Maste     m_sect_to_addr.erase(sta_pos);
208f034231aSEd Maste   }
209f034231aSEd Maste 
210f034231aSEd Maste   addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
21114f1b3e8SDimitry Andric   if (ats_pos != m_addr_to_sect.end()) {
212f034231aSEd Maste     erased = true;
213f034231aSEd Maste     m_addr_to_sect.erase(ats_pos);
214f034231aSEd Maste   }
215f034231aSEd Maste 
216f034231aSEd Maste   return erased;
217f034231aSEd Maste }
218f034231aSEd Maste 
ResolveLoadAddress(addr_t load_addr,Address & so_addr,bool allow_section_end) const219f1d04915SDimitry Andric bool SectionLoadList::ResolveLoadAddress(addr_t load_addr, Address &so_addr,
220f1d04915SDimitry Andric                                          bool allow_section_end) const {
221f034231aSEd Maste   // First find the top level section that this load address exists in
222f3fbd1c0SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
22314f1b3e8SDimitry Andric   if (!m_addr_to_sect.empty()) {
22414f1b3e8SDimitry Andric     addr_to_sect_collection::const_iterator pos =
22514f1b3e8SDimitry Andric         m_addr_to_sect.lower_bound(load_addr);
22614f1b3e8SDimitry Andric     if (pos != m_addr_to_sect.end()) {
227f034231aSEd Maste       if (load_addr != pos->first && pos != m_addr_to_sect.begin())
228f034231aSEd Maste         --pos;
229f034231aSEd Maste       const addr_t pos_load_addr = pos->first;
23014f1b3e8SDimitry Andric       if (load_addr >= pos_load_addr) {
231f034231aSEd Maste         addr_t offset = load_addr - pos_load_addr;
232f1d04915SDimitry Andric         if (offset < pos->second->GetByteSize() + (allow_section_end ? 1 : 0)) {
233f034231aSEd Maste           // We have found the top level section, now we need to find the
234f034231aSEd Maste           // deepest child section.
235f1d04915SDimitry Andric           return pos->second->ResolveContainedAddress(offset, so_addr,
236f1d04915SDimitry Andric                                                       allow_section_end);
237f034231aSEd Maste         }
238f034231aSEd Maste       }
23914f1b3e8SDimitry Andric     } else {
240f73363f1SDimitry Andric       // There are no entries that have an address that is >= load_addr, so we
241f73363f1SDimitry Andric       // need to check the last entry on our collection.
24214f1b3e8SDimitry Andric       addr_to_sect_collection::const_reverse_iterator rpos =
24314f1b3e8SDimitry Andric           m_addr_to_sect.rbegin();
24414f1b3e8SDimitry Andric       if (load_addr >= rpos->first) {
245f034231aSEd Maste         addr_t offset = load_addr - rpos->first;
246f1d04915SDimitry Andric         if (offset <
247f1d04915SDimitry Andric             rpos->second->GetByteSize() + (allow_section_end ? 1 : 0)) {
248f034231aSEd Maste           // We have found the top level section, now we need to find the
249f034231aSEd Maste           // deepest child section.
250f1d04915SDimitry Andric           return rpos->second->ResolveContainedAddress(offset, so_addr,
251f1d04915SDimitry Andric                                                        allow_section_end);
252f034231aSEd Maste         }
253f034231aSEd Maste       }
254f034231aSEd Maste     }
255f034231aSEd Maste   }
256f034231aSEd Maste   so_addr.Clear();
257f034231aSEd Maste   return false;
258f034231aSEd Maste }
259f034231aSEd Maste 
Dump(Stream & s,Target * target)26014f1b3e8SDimitry Andric void SectionLoadList::Dump(Stream &s, Target *target) {
261f3fbd1c0SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
262f034231aSEd Maste   addr_to_sect_collection::const_iterator pos, end;
26314f1b3e8SDimitry Andric   for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end;
26414f1b3e8SDimitry Andric        ++pos) {
26514f1b3e8SDimitry Andric     s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first,
26614f1b3e8SDimitry Andric              static_cast<void *>(pos->second.get()));
267cfca06d7SDimitry Andric     pos->second->Dump(s.AsRawOstream(), s.GetIndentLevel(), target, 0);
268f034231aSEd Maste   }
269f034231aSEd Maste }
270