1 | //===-- DynamicLoader.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 | |
9 | #include "lldb/Target/DynamicLoader.h" |
10 | |
11 | #include "lldb/Core/Debugger.h" |
12 | #include "lldb/Core/Module.h" |
13 | #include "lldb/Core/ModuleList.h" |
14 | #include "lldb/Core/ModuleSpec.h" |
15 | #include "lldb/Core/PluginManager.h" |
16 | #include "lldb/Core/Progress.h" |
17 | #include "lldb/Core/Section.h" |
18 | #include "lldb/Symbol/ObjectFile.h" |
19 | #include "lldb/Target/MemoryRegionInfo.h" |
20 | #include "lldb/Target/Platform.h" |
21 | #include "lldb/Target/Process.h" |
22 | #include "lldb/Target/Target.h" |
23 | #include "lldb/Utility/ConstString.h" |
24 | #include "lldb/Utility/LLDBLog.h" |
25 | #include "lldb/Utility/Log.h" |
26 | #include "lldb/lldb-private-interfaces.h" |
27 | |
28 | #include "llvm/ADT/StringRef.h" |
29 | |
30 | #include <memory> |
31 | |
32 | #include <cassert> |
33 | |
34 | using namespace lldb; |
35 | using namespace lldb_private; |
36 | |
37 | DynamicLoader *DynamicLoader::FindPlugin(Process *process, |
38 | llvm::StringRef plugin_name) { |
39 | DynamicLoaderCreateInstance create_callback = nullptr; |
40 | if (!plugin_name.empty()) { |
41 | create_callback = |
42 | PluginManager::GetDynamicLoaderCreateCallbackForPluginName(name: plugin_name); |
43 | if (create_callback) { |
44 | std::unique_ptr<DynamicLoader> instance_up( |
45 | create_callback(process, true)); |
46 | if (instance_up) |
47 | return instance_up.release(); |
48 | } |
49 | } else { |
50 | for (uint32_t idx = 0; |
51 | (create_callback = |
52 | PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) != |
53 | nullptr; |
54 | ++idx) { |
55 | std::unique_ptr<DynamicLoader> instance_up( |
56 | create_callback(process, false)); |
57 | if (instance_up) |
58 | return instance_up.release(); |
59 | } |
60 | } |
61 | return nullptr; |
62 | } |
63 | |
64 | DynamicLoader::DynamicLoader(Process *process) : m_process(process) {} |
65 | |
66 | // Accessors to the global setting as to whether to stop at image (shared |
67 | // library) loading/unloading. |
68 | |
69 | bool DynamicLoader::GetStopWhenImagesChange() const { |
70 | return m_process->GetStopOnSharedLibraryEvents(); |
71 | } |
72 | |
73 | void DynamicLoader::SetStopWhenImagesChange(bool stop) { |
74 | m_process->SetStopOnSharedLibraryEvents(stop); |
75 | } |
76 | |
77 | ModuleSP DynamicLoader::GetTargetExecutable() { |
78 | Target &target = m_process->GetTarget(); |
79 | ModuleSP executable = target.GetExecutableModule(); |
80 | |
81 | if (executable) { |
82 | if (FileSystem::Instance().Exists(file_spec: executable->GetFileSpec())) { |
83 | ModuleSpec module_spec(executable->GetFileSpec(), |
84 | executable->GetArchitecture()); |
85 | auto module_sp = std::make_shared<Module>(args&: module_spec); |
86 | // If we're a coredump and we already have a main executable, we don't |
87 | // need to reload the module list that target already has |
88 | if (!m_process->IsLiveDebugSession()) { |
89 | return executable; |
90 | } |
91 | // Check if the executable has changed and set it to the target |
92 | // executable if they differ. |
93 | if (module_sp && module_sp->GetUUID().IsValid() && |
94 | executable->GetUUID().IsValid()) { |
95 | if (module_sp->GetUUID() != executable->GetUUID()) |
96 | executable.reset(); |
97 | } else if (executable->FileHasChanged()) { |
98 | executable.reset(); |
99 | } |
100 | |
101 | if (!executable) { |
102 | executable = target.GetOrCreateModule(module_spec, notify: true /* notify */); |
103 | if (executable.get() != target.GetExecutableModulePointer()) { |
104 | // Don't load dependent images since we are in dyld where we will |
105 | // know and find out about all images that are loaded |
106 | target.SetExecutableModule(module_sp&: executable, load_dependent_files: eLoadDependentsNo); |
107 | } |
108 | } |
109 | } |
110 | } |
111 | return executable; |
112 | } |
113 | |
114 | void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, |
115 | addr_t base_addr, |
116 | bool base_addr_is_offset) { |
117 | UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset); |
118 | } |
119 | |
120 | void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module, |
121 | addr_t base_addr, |
122 | bool base_addr_is_offset) { |
123 | bool changed; |
124 | module->SetLoadAddress(target&: m_process->GetTarget(), value: base_addr, value_is_offset: base_addr_is_offset, |
125 | changed); |
126 | } |
127 | |
128 | void DynamicLoader::UnloadSections(const ModuleSP module) { |
129 | UnloadSectionsCommon(module); |
130 | } |
131 | |
132 | void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) { |
133 | Target &target = m_process->GetTarget(); |
134 | const SectionList *sections = GetSectionListFromModule(module); |
135 | |
136 | assert(sections && "SectionList missing from unloaded module." ); |
137 | |
138 | const size_t num_sections = sections->GetSize(); |
139 | for (size_t i = 0; i < num_sections; ++i) { |
140 | SectionSP section_sp(sections->GetSectionAtIndex(idx: i)); |
141 | target.SetSectionUnloaded(section_sp); |
142 | } |
143 | } |
144 | |
145 | const SectionList * |
146 | DynamicLoader::GetSectionListFromModule(const ModuleSP module) const { |
147 | SectionList *sections = nullptr; |
148 | if (module) { |
149 | ObjectFile *obj_file = module->GetObjectFile(); |
150 | if (obj_file != nullptr) { |
151 | sections = obj_file->GetSectionList(); |
152 | } |
153 | } |
154 | return sections; |
155 | } |
156 | |
157 | ModuleSP DynamicLoader::FindModuleViaTarget(const FileSpec &file) { |
158 | Target &target = m_process->GetTarget(); |
159 | ModuleSpec module_spec(file, target.GetArchitecture()); |
160 | if (UUID uuid = m_process->FindModuleUUID(path: file.GetPath())) { |
161 | // Process may be able to augment the module_spec with UUID, e.g. ELF core. |
162 | module_spec.GetUUID() = uuid; |
163 | } |
164 | |
165 | if (ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec)) |
166 | return module_sp; |
167 | |
168 | if (ModuleSP module_sp = target.GetOrCreateModule(module_spec, notify: false)) |
169 | return module_sp; |
170 | |
171 | return nullptr; |
172 | } |
173 | |
174 | ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file, |
175 | addr_t link_map_addr, |
176 | addr_t base_addr, |
177 | bool base_addr_is_offset) { |
178 | if (ModuleSP module_sp = FindModuleViaTarget(file)) { |
179 | UpdateLoadedSections(module: module_sp, link_map_addr, base_addr, |
180 | base_addr_is_offset); |
181 | return module_sp; |
182 | } |
183 | |
184 | return nullptr; |
185 | } |
186 | |
187 | static ModuleSP ReadUnnamedMemoryModule(Process *process, addr_t addr, |
188 | llvm::StringRef name) { |
189 | char namebuf[80]; |
190 | if (name.empty()) { |
191 | snprintf(s: namebuf, maxlen: sizeof(namebuf), format: "memory-image-0x%" PRIx64, addr); |
192 | name = namebuf; |
193 | } |
194 | return process->ReadModuleFromMemory(file_spec: FileSpec(name), header_addr: addr); |
195 | } |
196 | |
197 | ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress( |
198 | Process *process, llvm::StringRef name, UUID uuid, addr_t value, |
199 | bool value_is_offset, bool force_symbol_search, bool notify, |
200 | bool set_address_in_target, bool allow_memory_image_last_resort) { |
201 | ModuleSP memory_module_sp; |
202 | ModuleSP module_sp; |
203 | PlatformSP platform_sp = process->GetTarget().GetPlatform(); |
204 | Target &target = process->GetTarget(); |
205 | Status error; |
206 | |
207 | StreamString prog_str; |
208 | if (!name.empty()) { |
209 | prog_str << name.str() << " " ; |
210 | } |
211 | if (uuid.IsValid()) |
212 | prog_str << uuid.GetAsString(); |
213 | if (value_is_offset == 0 && value != LLDB_INVALID_ADDRESS) { |
214 | prog_str << "at 0x" ; |
215 | prog_str.PutHex64(uvalue: value); |
216 | } |
217 | |
218 | if (!uuid.IsValid() && !value_is_offset) { |
219 | memory_module_sp = ReadUnnamedMemoryModule(process, addr: value, name); |
220 | |
221 | if (memory_module_sp) { |
222 | uuid = memory_module_sp->GetUUID(); |
223 | if (uuid.IsValid()) { |
224 | prog_str << " " ; |
225 | prog_str << uuid.GetAsString(); |
226 | } |
227 | } |
228 | } |
229 | ModuleSpec module_spec; |
230 | module_spec.GetUUID() = uuid; |
231 | FileSpec name_filespec(name); |
232 | |
233 | if (uuid.IsValid()) { |
234 | Progress progress("Locating binary" , prog_str.GetString().str()); |
235 | |
236 | // Has lldb already seen a module with this UUID? |
237 | // Or have external lookup enabled in DebugSymbols on macOS. |
238 | if (!module_sp) |
239 | error = ModuleList::GetSharedModule(module_spec, module_sp, module_search_paths_ptr: nullptr, |
240 | old_modules: nullptr, did_create_ptr: nullptr); |
241 | |
242 | // Can lldb's symbol/executable location schemes |
243 | // find an executable and symbol file. |
244 | if (!module_sp) { |
245 | FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths(); |
246 | StatisticsMap symbol_locator_map; |
247 | module_spec.GetSymbolFileSpec() = |
248 | PluginManager::LocateExecutableSymbolFile(module_spec, default_search_paths: search_paths, |
249 | map&: symbol_locator_map); |
250 | ModuleSpec objfile_module_spec = |
251 | PluginManager::LocateExecutableObjectFile(module_spec, |
252 | map&: symbol_locator_map); |
253 | module_spec.GetFileSpec() = objfile_module_spec.GetFileSpec(); |
254 | if (FileSystem::Instance().Exists(file_spec: module_spec.GetFileSpec()) && |
255 | FileSystem::Instance().Exists(file_spec: module_spec.GetSymbolFileSpec())) { |
256 | module_sp = std::make_shared<Module>(args&: module_spec); |
257 | } |
258 | |
259 | if (module_sp) { |
260 | module_sp->GetSymbolLocatorStatistics().merge(map_to_merge: symbol_locator_map); |
261 | } |
262 | } |
263 | |
264 | // If we haven't found a binary, or we don't have a SymbolFile, see |
265 | // if there is an external search tool that can find it. |
266 | if (!module_sp || !module_sp->GetSymbolFileFileSpec()) { |
267 | PluginManager::DownloadObjectAndSymbolFile(module_spec, error, |
268 | force_lookup: force_symbol_search); |
269 | if (FileSystem::Instance().Exists(file_spec: module_spec.GetFileSpec())) { |
270 | module_sp = std::make_shared<Module>(args&: module_spec); |
271 | } else if (force_symbol_search && error.AsCString(default_error_str: "" ) && |
272 | error.AsCString(default_error_str: "" )[0] != '\0') { |
273 | *target.GetDebugger().GetAsyncErrorStream() << error.AsCString(); |
274 | } |
275 | } |
276 | |
277 | // If we only found the executable, create a Module based on that. |
278 | if (!module_sp && FileSystem::Instance().Exists(file_spec: module_spec.GetFileSpec())) |
279 | module_sp = std::make_shared<Module>(args&: module_spec); |
280 | } |
281 | |
282 | // If we couldn't find the binary anywhere else, as a last resort, |
283 | // read it out of memory. |
284 | if (allow_memory_image_last_resort && !module_sp.get() && |
285 | value != LLDB_INVALID_ADDRESS && !value_is_offset) { |
286 | if (!memory_module_sp) |
287 | memory_module_sp = ReadUnnamedMemoryModule(process, addr: value, name); |
288 | if (memory_module_sp) |
289 | module_sp = memory_module_sp; |
290 | } |
291 | |
292 | Log *log = GetLog(mask: LLDBLog::DynamicLoader); |
293 | if (module_sp.get()) { |
294 | // Ensure the Target has an architecture set in case |
295 | // we need it while processing this binary/eh_frame/debug info. |
296 | if (!target.GetArchitecture().IsValid()) |
297 | target.SetArchitecture(arch_spec: module_sp->GetArchitecture()); |
298 | target.GetImages().AppendIfNeeded(new_module: module_sp, notify: false); |
299 | |
300 | bool changed = false; |
301 | if (set_address_in_target) { |
302 | if (module_sp->GetObjectFile()) { |
303 | if (value != LLDB_INVALID_ADDRESS) { |
304 | LLDB_LOGF(log, |
305 | "DynamicLoader::LoadBinaryWithUUIDAndAddress Loading " |
306 | "binary %s UUID %s at %s 0x%" PRIx64, |
307 | name.str().c_str(), uuid.GetAsString().c_str(), |
308 | value_is_offset ? "offset" : "address" , value); |
309 | module_sp->SetLoadAddress(target, value, value_is_offset, changed); |
310 | } else { |
311 | // No address/offset/slide, load the binary at file address, |
312 | // offset 0. |
313 | LLDB_LOGF(log, |
314 | "DynamicLoader::LoadBinaryWithUUIDAndAddress Loading " |
315 | "binary %s UUID %s at file address" , |
316 | name.str().c_str(), uuid.GetAsString().c_str()); |
317 | module_sp->SetLoadAddress(target, value: 0, value_is_offset: true /* value_is_slide */, |
318 | changed); |
319 | } |
320 | } else { |
321 | // In-memory image, load at its true address, offset 0. |
322 | LLDB_LOGF(log, |
323 | "DynamicLoader::LoadBinaryWithUUIDAndAddress Loading binary " |
324 | "%s UUID %s from memory at address 0x%" PRIx64, |
325 | name.str().c_str(), uuid.GetAsString().c_str(), value); |
326 | module_sp->SetLoadAddress(target, value: 0, value_is_offset: true /* value_is_slide */, |
327 | changed); |
328 | } |
329 | } |
330 | |
331 | if (notify) { |
332 | ModuleList added_module; |
333 | added_module.Append(module_sp, notify: false); |
334 | target.ModulesDidLoad(module_list&: added_module); |
335 | } |
336 | } else { |
337 | if (force_symbol_search) { |
338 | lldb::StreamUP s = target.GetDebugger().GetAsyncErrorStream(); |
339 | s->Printf(format: "Unable to find file" ); |
340 | if (!name.empty()) |
341 | s->Printf(format: " %s" , name.str().c_str()); |
342 | if (uuid.IsValid()) |
343 | s->Printf(format: " with UUID %s" , uuid.GetAsString().c_str()); |
344 | if (value != LLDB_INVALID_ADDRESS) { |
345 | if (value_is_offset) |
346 | s->Printf(format: " with slide 0x%" PRIx64, value); |
347 | else |
348 | s->Printf(format: " at address 0x%" PRIx64, value); |
349 | } |
350 | s->Printf(format: "\n" ); |
351 | } |
352 | LLDB_LOGF(log, |
353 | "Unable to find binary %s with UUID %s and load it at " |
354 | "%s 0x%" PRIx64, |
355 | name.str().c_str(), uuid.GetAsString().c_str(), |
356 | value_is_offset ? "offset" : "address" , value); |
357 | } |
358 | |
359 | return module_sp; |
360 | } |
361 | |
362 | int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, |
363 | int size_in_bytes) { |
364 | Status error; |
365 | uint64_t value = |
366 | m_process->ReadUnsignedIntegerFromMemory(load_addr: addr, byte_size: size_in_bytes, fail_value: 0, error); |
367 | if (error.Fail()) |
368 | return -1; |
369 | else |
370 | return (int64_t)value; |
371 | } |
372 | |
373 | addr_t DynamicLoader::ReadPointer(addr_t addr) { |
374 | Status error; |
375 | addr_t value = m_process->ReadPointerFromMemory(vm_addr: addr, error); |
376 | if (error.Fail()) |
377 | return LLDB_INVALID_ADDRESS; |
378 | else |
379 | return value; |
380 | } |
381 | |
382 | void DynamicLoader::LoadOperatingSystemPlugin(bool flush) |
383 | { |
384 | if (m_process) |
385 | m_process->LoadOperatingSystemPlugin(flush); |
386 | } |
387 | |