1ac7ddfbfSEd Maste //===-- SectionLoadList.cpp -------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/Target/SectionLoadList.h"
11ac7ddfbfSEd Maste 
12ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
13ac7ddfbfSEd Maste #include "lldb/Core/Section.h"
14ac7ddfbfSEd Maste #include "lldb/Symbol/Block.h"
15ac7ddfbfSEd Maste #include "lldb/Symbol/Symbol.h"
16ac7ddfbfSEd Maste #include "lldb/Symbol/SymbolContext.h"
17f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
18f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
19ac7ddfbfSEd Maste 
20ac7ddfbfSEd Maste using namespace lldb;
21ac7ddfbfSEd Maste using namespace lldb_private;
22ac7ddfbfSEd Maste 
SectionLoadList(const SectionLoadList & rhs)23435933ddSDimitry Andric SectionLoadList::SectionLoadList(const SectionLoadList &rhs)
24435933ddSDimitry Andric     : m_addr_to_sect(), m_sect_to_addr(), m_mutex() {
254bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(rhs.m_mutex);
2612b93ac6SEd Maste   m_addr_to_sect = rhs.m_addr_to_sect;
2712b93ac6SEd Maste   m_sect_to_addr = rhs.m_sect_to_addr;
2812b93ac6SEd Maste }
2912b93ac6SEd Maste 
operator =(const SectionLoadList & rhs)30435933ddSDimitry Andric void SectionLoadList::operator=(const SectionLoadList &rhs) {
314bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex);
324bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex);
3312b93ac6SEd Maste   m_addr_to_sect = rhs.m_addr_to_sect;
3412b93ac6SEd Maste   m_sect_to_addr = rhs.m_sect_to_addr;
3512b93ac6SEd Maste }
3612b93ac6SEd Maste 
IsEmpty() const37435933ddSDimitry Andric bool SectionLoadList::IsEmpty() const {
384bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
39ac7ddfbfSEd Maste   return m_addr_to_sect.empty();
40ac7ddfbfSEd Maste }
41ac7ddfbfSEd Maste 
Clear()42435933ddSDimitry Andric void SectionLoadList::Clear() {
434bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
44ac7ddfbfSEd Maste   m_addr_to_sect.clear();
45ac7ddfbfSEd Maste   m_sect_to_addr.clear();
46ac7ddfbfSEd Maste }
47ac7ddfbfSEd Maste 
48ac7ddfbfSEd Maste addr_t
GetSectionLoadAddress(const lldb::SectionSP & section) const49435933ddSDimitry Andric SectionLoadList::GetSectionLoadAddress(const lldb::SectionSP &section) const {
50ac7ddfbfSEd Maste   // TODO: add support for the same section having multiple load addresses
51ac7ddfbfSEd Maste   addr_t section_load_addr = LLDB_INVALID_ADDRESS;
52435933ddSDimitry Andric   if (section) {
534bb0738eSEd Maste     std::lock_guard<std::recursive_mutex> guard(m_mutex);
54435933ddSDimitry Andric     sect_to_addr_collection::const_iterator pos =
55435933ddSDimitry Andric         m_sect_to_addr.find(section.get());
56ac7ddfbfSEd Maste 
57ac7ddfbfSEd Maste     if (pos != m_sect_to_addr.end())
58ac7ddfbfSEd Maste       section_load_addr = pos->second;
59ac7ddfbfSEd Maste   }
60ac7ddfbfSEd Maste   return section_load_addr;
61ac7ddfbfSEd Maste }
62ac7ddfbfSEd Maste 
SetSectionLoadAddress(const lldb::SectionSP & section,addr_t load_addr,bool warn_multiple)63435933ddSDimitry Andric bool SectionLoadList::SetSectionLoadAddress(const lldb::SectionSP &section,
64435933ddSDimitry Andric                                             addr_t load_addr,
65435933ddSDimitry Andric                                             bool warn_multiple) {
66f678e45dSDimitry Andric   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
67ac7ddfbfSEd Maste   ModuleSP module_sp(section->GetModule());
68ac7ddfbfSEd Maste 
69435933ddSDimitry Andric   if (module_sp) {
70f678e45dSDimitry Andric     LLDB_LOGV(log, "(section = {0} ({1}.{2}), load_addr = {3:x}) module = {4}",
71f678e45dSDimitry Andric               section.get(), module_sp->GetFileSpec(), section->GetName(),
72f678e45dSDimitry Andric               load_addr, module_sp.get());
73ac7ddfbfSEd Maste 
74ac7ddfbfSEd Maste     if (section->GetByteSize() == 0)
75ac7ddfbfSEd Maste       return false; // No change
76ac7ddfbfSEd Maste 
77ac7ddfbfSEd Maste     // Fill in the section -> load_addr map
784bb0738eSEd Maste     std::lock_guard<std::recursive_mutex> guard(m_mutex);
79435933ddSDimitry Andric     sect_to_addr_collection::iterator sta_pos =
80435933ddSDimitry Andric         m_sect_to_addr.find(section.get());
81435933ddSDimitry Andric     if (sta_pos != m_sect_to_addr.end()) {
82ac7ddfbfSEd Maste       if (load_addr == sta_pos->second)
83ac7ddfbfSEd Maste         return false; // No change...
84ac7ddfbfSEd Maste       else
85ac7ddfbfSEd Maste         sta_pos->second = load_addr;
86435933ddSDimitry Andric     } else
87ac7ddfbfSEd Maste       m_sect_to_addr[section.get()] = load_addr;
88ac7ddfbfSEd Maste 
89ac7ddfbfSEd Maste     // Fill in the load_addr -> section map
90ac7ddfbfSEd Maste     addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
91435933ddSDimitry Andric     if (ats_pos != m_addr_to_sect.end()) {
92ac7ddfbfSEd Maste       // Some sections are ok to overlap, and for others we should warn. When
93ac7ddfbfSEd Maste       // we have multiple load addresses that correspond to a section, we will
940127ef0fSEd Maste       // always attribute the section to the be last section that claims it
95ac7ddfbfSEd Maste       // exists at that address. Sometimes it is ok for more that one section
96*4ba319b5SDimitry Andric       // to be loaded at a specific load address, and other times it isn't. The
97*4ba319b5SDimitry Andric       // "warn_multiple" parameter tells us if we should warn in this case or
98*4ba319b5SDimitry Andric       // not. The DynamicLoader plug-in subclasses should know which sections
99*4ba319b5SDimitry Andric       // should warn and which shouldn't (darwin shared cache modules all
100*4ba319b5SDimitry Andric       // shared the same "__LINKEDIT" sections, so the dynamic loader can pass
101*4ba319b5SDimitry Andric       // false for "warn_multiple").
102435933ddSDimitry Andric       if (warn_multiple && section != ats_pos->second) {
103ac7ddfbfSEd Maste         ModuleSP module_sp(section->GetModule());
104435933ddSDimitry Andric         if (module_sp) {
105ac7ddfbfSEd Maste           ModuleSP curr_module_sp(ats_pos->second->GetModule());
106435933ddSDimitry Andric           if (curr_module_sp) {
107435933ddSDimitry Andric             module_sp->ReportWarning(
108435933ddSDimitry Andric                 "address 0x%16.16" PRIx64
109435933ddSDimitry Andric                 " maps to more than one section: %s.%s and %s.%s",
110435933ddSDimitry Andric                 load_addr, module_sp->GetFileSpec().GetFilename().GetCString(),
111ac7ddfbfSEd Maste                 section->GetName().GetCString(),
112ac7ddfbfSEd Maste                 curr_module_sp->GetFileSpec().GetFilename().GetCString(),
113ac7ddfbfSEd Maste                 ats_pos->second->GetName().GetCString());
114ac7ddfbfSEd Maste           }
115ac7ddfbfSEd Maste         }
116ac7ddfbfSEd Maste       }
117ac7ddfbfSEd Maste       ats_pos->second = section;
118435933ddSDimitry Andric     } else
119ac7ddfbfSEd Maste       m_addr_to_sect[load_addr] = section;
120ac7ddfbfSEd Maste     return true; // Changed
121ac7ddfbfSEd Maste 
122435933ddSDimitry Andric   } else {
123435933ddSDimitry Andric     if (log) {
124435933ddSDimitry Andric       log->Printf(
125435933ddSDimitry Andric           "SectionLoadList::%s (section = %p (%s), load_addr = 0x%16.16" PRIx64
126435933ddSDimitry Andric           ") error: module has been deleted",
1270127ef0fSEd Maste           __FUNCTION__, static_cast<void *>(section.get()),
128435933ddSDimitry Andric           section->GetName().AsCString(), load_addr);
129ac7ddfbfSEd Maste     }
130ac7ddfbfSEd Maste   }
131ac7ddfbfSEd Maste   return false;
132ac7ddfbfSEd Maste }
133ac7ddfbfSEd Maste 
SetSectionUnloaded(const lldb::SectionSP & section_sp)134435933ddSDimitry Andric size_t SectionLoadList::SetSectionUnloaded(const lldb::SectionSP &section_sp) {
135ac7ddfbfSEd Maste   size_t unload_count = 0;
136ac7ddfbfSEd Maste 
137435933ddSDimitry Andric   if (section_sp) {
138f678e45dSDimitry Andric     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
139ac7ddfbfSEd Maste 
140f678e45dSDimitry Andric     if (log && log->GetVerbose()) {
1414bb0738eSEd Maste       ModuleSP module_sp = section_sp->GetModule();
1424bb0738eSEd Maste       std::string module_name("<Unknown>");
143435933ddSDimitry Andric       if (module_sp) {
144435933ddSDimitry Andric         const FileSpec &module_file_spec(
145435933ddSDimitry Andric             section_sp->GetModule()->GetFileSpec());
1464bb0738eSEd Maste         module_name = module_file_spec.GetPath();
1474bb0738eSEd Maste       }
148435933ddSDimitry Andric       log->Printf("SectionLoadList::%s (section = %p (%s.%s))", __FUNCTION__,
149435933ddSDimitry Andric                   static_cast<void *>(section_sp.get()), module_name.c_str(),
150ac7ddfbfSEd Maste                   section_sp->GetName().AsCString());
151ac7ddfbfSEd Maste     }
152ac7ddfbfSEd Maste 
1534bb0738eSEd Maste     std::lock_guard<std::recursive_mutex> guard(m_mutex);
154ac7ddfbfSEd Maste 
155435933ddSDimitry Andric     sect_to_addr_collection::iterator sta_pos =
156435933ddSDimitry Andric         m_sect_to_addr.find(section_sp.get());
157435933ddSDimitry Andric     if (sta_pos != m_sect_to_addr.end()) {
158ac7ddfbfSEd Maste       ++unload_count;
159ac7ddfbfSEd Maste       addr_t load_addr = sta_pos->second;
160ac7ddfbfSEd Maste       m_sect_to_addr.erase(sta_pos);
161ac7ddfbfSEd Maste 
162435933ddSDimitry Andric       addr_to_sect_collection::iterator ats_pos =
163435933ddSDimitry Andric           m_addr_to_sect.find(load_addr);
164ac7ddfbfSEd Maste       if (ats_pos != m_addr_to_sect.end())
165ac7ddfbfSEd Maste         m_addr_to_sect.erase(ats_pos);
166ac7ddfbfSEd Maste     }
167ac7ddfbfSEd Maste   }
168ac7ddfbfSEd Maste   return unload_count;
169ac7ddfbfSEd Maste }
170ac7ddfbfSEd Maste 
SetSectionUnloaded(const lldb::SectionSP & section_sp,addr_t load_addr)171435933ddSDimitry Andric bool SectionLoadList::SetSectionUnloaded(const lldb::SectionSP &section_sp,
172435933ddSDimitry Andric                                          addr_t load_addr) {
173f678e45dSDimitry Andric   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
174ac7ddfbfSEd Maste 
175f678e45dSDimitry Andric   if (log && log->GetVerbose()) {
1764bb0738eSEd Maste     ModuleSP module_sp = section_sp->GetModule();
1774bb0738eSEd Maste     std::string module_name("<Unknown>");
178435933ddSDimitry Andric     if (module_sp) {
179ac7ddfbfSEd Maste       const FileSpec &module_file_spec(section_sp->GetModule()->GetFileSpec());
1804bb0738eSEd Maste       module_name = module_file_spec.GetPath();
1814bb0738eSEd Maste     }
182435933ddSDimitry Andric     log->Printf(
183435933ddSDimitry Andric         "SectionLoadList::%s (section = %p (%s.%s), load_addr = 0x%16.16" PRIx64
184435933ddSDimitry Andric         ")",
1850127ef0fSEd Maste         __FUNCTION__, static_cast<void *>(section_sp.get()),
186435933ddSDimitry Andric         module_name.c_str(), section_sp->GetName().AsCString(), load_addr);
187ac7ddfbfSEd Maste   }
188ac7ddfbfSEd Maste   bool erased = false;
1894bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
190435933ddSDimitry Andric   sect_to_addr_collection::iterator sta_pos =
191435933ddSDimitry Andric       m_sect_to_addr.find(section_sp.get());
192435933ddSDimitry Andric   if (sta_pos != m_sect_to_addr.end()) {
193ac7ddfbfSEd Maste     erased = true;
194ac7ddfbfSEd Maste     m_sect_to_addr.erase(sta_pos);
195ac7ddfbfSEd Maste   }
196ac7ddfbfSEd Maste 
197ac7ddfbfSEd Maste   addr_to_sect_collection::iterator ats_pos = m_addr_to_sect.find(load_addr);
198435933ddSDimitry Andric   if (ats_pos != m_addr_to_sect.end()) {
199ac7ddfbfSEd Maste     erased = true;
200ac7ddfbfSEd Maste     m_addr_to_sect.erase(ats_pos);
201ac7ddfbfSEd Maste   }
202ac7ddfbfSEd Maste 
203ac7ddfbfSEd Maste   return erased;
204ac7ddfbfSEd Maste }
205ac7ddfbfSEd Maste 
ResolveLoadAddress(addr_t load_addr,Address & so_addr,bool allow_section_end) const206db17bf38SDimitry Andric bool SectionLoadList::ResolveLoadAddress(addr_t load_addr, Address &so_addr,
207db17bf38SDimitry Andric                                          bool allow_section_end) const {
208ac7ddfbfSEd Maste   // First find the top level section that this load address exists in
2094bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
210435933ddSDimitry Andric   if (!m_addr_to_sect.empty()) {
211435933ddSDimitry Andric     addr_to_sect_collection::const_iterator pos =
212435933ddSDimitry Andric         m_addr_to_sect.lower_bound(load_addr);
213435933ddSDimitry Andric     if (pos != m_addr_to_sect.end()) {
214ac7ddfbfSEd Maste       if (load_addr != pos->first && pos != m_addr_to_sect.begin())
215ac7ddfbfSEd Maste         --pos;
216ac7ddfbfSEd Maste       const addr_t pos_load_addr = pos->first;
217435933ddSDimitry Andric       if (load_addr >= pos_load_addr) {
218ac7ddfbfSEd Maste         addr_t offset = load_addr - pos_load_addr;
219db17bf38SDimitry Andric         if (offset < pos->second->GetByteSize() + (allow_section_end ? 1 : 0)) {
220ac7ddfbfSEd Maste           // We have found the top level section, now we need to find the
221ac7ddfbfSEd Maste           // deepest child section.
222db17bf38SDimitry Andric           return pos->second->ResolveContainedAddress(offset, so_addr,
223db17bf38SDimitry Andric                                                       allow_section_end);
224ac7ddfbfSEd Maste         }
225ac7ddfbfSEd Maste       }
226435933ddSDimitry Andric     } else {
227*4ba319b5SDimitry Andric       // There are no entries that have an address that is >= load_addr, so we
228*4ba319b5SDimitry Andric       // need to check the last entry on our collection.
229435933ddSDimitry Andric       addr_to_sect_collection::const_reverse_iterator rpos =
230435933ddSDimitry Andric           m_addr_to_sect.rbegin();
231435933ddSDimitry Andric       if (load_addr >= rpos->first) {
232ac7ddfbfSEd Maste         addr_t offset = load_addr - rpos->first;
233db17bf38SDimitry Andric         if (offset <
234db17bf38SDimitry Andric             rpos->second->GetByteSize() + (allow_section_end ? 1 : 0)) {
235ac7ddfbfSEd Maste           // We have found the top level section, now we need to find the
236ac7ddfbfSEd Maste           // deepest child section.
237db17bf38SDimitry Andric           return rpos->second->ResolveContainedAddress(offset, so_addr,
238db17bf38SDimitry Andric                                                        allow_section_end);
239ac7ddfbfSEd Maste         }
240ac7ddfbfSEd Maste       }
241ac7ddfbfSEd Maste     }
242ac7ddfbfSEd Maste   }
243ac7ddfbfSEd Maste   so_addr.Clear();
244ac7ddfbfSEd Maste   return false;
245ac7ddfbfSEd Maste }
246ac7ddfbfSEd Maste 
Dump(Stream & s,Target * target)247435933ddSDimitry Andric void SectionLoadList::Dump(Stream &s, Target *target) {
2484bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
249ac7ddfbfSEd Maste   addr_to_sect_collection::const_iterator pos, end;
250435933ddSDimitry Andric   for (pos = m_addr_to_sect.begin(), end = m_addr_to_sect.end(); pos != end;
251435933ddSDimitry Andric        ++pos) {
252435933ddSDimitry Andric     s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first,
253435933ddSDimitry Andric              static_cast<void *>(pos->second.get()));
254ac7ddfbfSEd Maste     pos->second->Dump(&s, target, 0);
255ac7ddfbfSEd Maste   }
256ac7ddfbfSEd Maste }
257