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