Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/lldb/source/Core/DynamicLoader.cpp
39587 views
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(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(executable->GetFileSpec())) {
83
ModuleSpec module_spec(executable->GetFileSpec(),
84
executable->GetArchitecture());
85
auto module_sp = std::make_shared<Module>(module_spec);
86
87
// Check if the executable has changed and set it to the target
88
// executable if they differ.
89
if (module_sp && module_sp->GetUUID().IsValid() &&
90
executable->GetUUID().IsValid()) {
91
if (module_sp->GetUUID() != executable->GetUUID())
92
executable.reset();
93
} else if (executable->FileHasChanged()) {
94
executable.reset();
95
}
96
97
if (!executable) {
98
executable = target.GetOrCreateModule(module_spec, true /* notify */);
99
if (executable.get() != target.GetExecutableModulePointer()) {
100
// Don't load dependent images since we are in dyld where we will
101
// know and find out about all images that are loaded
102
target.SetExecutableModule(executable, eLoadDependentsNo);
103
}
104
}
105
}
106
}
107
return executable;
108
}
109
110
void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr,
111
addr_t base_addr,
112
bool base_addr_is_offset) {
113
UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);
114
}
115
116
void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module,
117
addr_t base_addr,
118
bool base_addr_is_offset) {
119
bool changed;
120
module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset,
121
changed);
122
}
123
124
void DynamicLoader::UnloadSections(const ModuleSP module) {
125
UnloadSectionsCommon(module);
126
}
127
128
void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) {
129
Target &target = m_process->GetTarget();
130
const SectionList *sections = GetSectionListFromModule(module);
131
132
assert(sections && "SectionList missing from unloaded module.");
133
134
const size_t num_sections = sections->GetSize();
135
for (size_t i = 0; i < num_sections; ++i) {
136
SectionSP section_sp(sections->GetSectionAtIndex(i));
137
target.SetSectionUnloaded(section_sp);
138
}
139
}
140
141
const SectionList *
142
DynamicLoader::GetSectionListFromModule(const ModuleSP module) const {
143
SectionList *sections = nullptr;
144
if (module) {
145
ObjectFile *obj_file = module->GetObjectFile();
146
if (obj_file != nullptr) {
147
sections = obj_file->GetSectionList();
148
}
149
}
150
return sections;
151
}
152
153
ModuleSP DynamicLoader::FindModuleViaTarget(const FileSpec &file) {
154
Target &target = m_process->GetTarget();
155
ModuleSpec module_spec(file, target.GetArchitecture());
156
157
if (ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec))
158
return module_sp;
159
160
if (ModuleSP module_sp = target.GetOrCreateModule(module_spec, false))
161
return module_sp;
162
163
return nullptr;
164
}
165
166
ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file,
167
addr_t link_map_addr,
168
addr_t base_addr,
169
bool base_addr_is_offset) {
170
if (ModuleSP module_sp = FindModuleViaTarget(file)) {
171
UpdateLoadedSections(module_sp, link_map_addr, base_addr,
172
base_addr_is_offset);
173
return module_sp;
174
}
175
176
return nullptr;
177
}
178
179
static ModuleSP ReadUnnamedMemoryModule(Process *process, addr_t addr,
180
llvm::StringRef name) {
181
char namebuf[80];
182
if (name.empty()) {
183
snprintf(namebuf, sizeof(namebuf), "memory-image-0x%" PRIx64, addr);
184
name = namebuf;
185
}
186
return process->ReadModuleFromMemory(FileSpec(name), addr);
187
}
188
189
ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
190
Process *process, llvm::StringRef name, UUID uuid, addr_t value,
191
bool value_is_offset, bool force_symbol_search, bool notify,
192
bool set_address_in_target, bool allow_memory_image_last_resort) {
193
ModuleSP memory_module_sp;
194
ModuleSP module_sp;
195
PlatformSP platform_sp = process->GetTarget().GetPlatform();
196
Target &target = process->GetTarget();
197
Status error;
198
199
StreamString prog_str;
200
if (!name.empty()) {
201
prog_str << name.str() << " ";
202
}
203
if (uuid.IsValid())
204
prog_str << uuid.GetAsString();
205
if (value_is_offset == 0 && value != LLDB_INVALID_ADDRESS) {
206
prog_str << "at 0x";
207
prog_str.PutHex64(value);
208
}
209
210
if (!uuid.IsValid() && !value_is_offset) {
211
memory_module_sp = ReadUnnamedMemoryModule(process, value, name);
212
213
if (memory_module_sp) {
214
uuid = memory_module_sp->GetUUID();
215
if (uuid.IsValid()) {
216
prog_str << " ";
217
prog_str << uuid.GetAsString();
218
}
219
}
220
}
221
ModuleSpec module_spec;
222
module_spec.GetUUID() = uuid;
223
FileSpec name_filespec(name);
224
225
if (uuid.IsValid()) {
226
Progress progress("Locating binary", prog_str.GetString().str());
227
228
// Has lldb already seen a module with this UUID?
229
// Or have external lookup enabled in DebugSymbols on macOS.
230
if (!module_sp)
231
error = ModuleList::GetSharedModule(module_spec, module_sp, nullptr,
232
nullptr, nullptr);
233
234
// Can lldb's symbol/executable location schemes
235
// find an executable and symbol file.
236
if (!module_sp) {
237
FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
238
module_spec.GetSymbolFileSpec() =
239
PluginManager::LocateExecutableSymbolFile(module_spec, search_paths);
240
ModuleSpec objfile_module_spec =
241
PluginManager::LocateExecutableObjectFile(module_spec);
242
module_spec.GetFileSpec() = objfile_module_spec.GetFileSpec();
243
if (FileSystem::Instance().Exists(module_spec.GetFileSpec()) &&
244
FileSystem::Instance().Exists(module_spec.GetSymbolFileSpec())) {
245
module_sp = std::make_shared<Module>(module_spec);
246
}
247
}
248
249
// If we haven't found a binary, or we don't have a SymbolFile, see
250
// if there is an external search tool that can find it.
251
if (!module_sp || !module_sp->GetSymbolFileFileSpec()) {
252
PluginManager::DownloadObjectAndSymbolFile(module_spec, error,
253
force_symbol_search);
254
if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
255
module_sp = std::make_shared<Module>(module_spec);
256
} else if (force_symbol_search && error.AsCString("") &&
257
error.AsCString("")[0] != '\0') {
258
target.GetDebugger().GetErrorStream() << error.AsCString();
259
}
260
}
261
262
// If we only found the executable, create a Module based on that.
263
if (!module_sp && FileSystem::Instance().Exists(module_spec.GetFileSpec()))
264
module_sp = std::make_shared<Module>(module_spec);
265
}
266
267
// If we couldn't find the binary anywhere else, as a last resort,
268
// read it out of memory.
269
if (allow_memory_image_last_resort && !module_sp.get() &&
270
value != LLDB_INVALID_ADDRESS && !value_is_offset) {
271
if (!memory_module_sp)
272
memory_module_sp = ReadUnnamedMemoryModule(process, value, name);
273
if (memory_module_sp)
274
module_sp = memory_module_sp;
275
}
276
277
Log *log = GetLog(LLDBLog::DynamicLoader);
278
if (module_sp.get()) {
279
// Ensure the Target has an architecture set in case
280
// we need it while processing this binary/eh_frame/debug info.
281
if (!target.GetArchitecture().IsValid())
282
target.SetArchitecture(module_sp->GetArchitecture());
283
target.GetImages().AppendIfNeeded(module_sp, false);
284
285
bool changed = false;
286
if (set_address_in_target) {
287
if (module_sp->GetObjectFile()) {
288
if (value != LLDB_INVALID_ADDRESS) {
289
LLDB_LOGF(log,
290
"DynamicLoader::LoadBinaryWithUUIDAndAddress Loading "
291
"binary %s UUID %s at %s 0x%" PRIx64,
292
name.str().c_str(), uuid.GetAsString().c_str(),
293
value_is_offset ? "offset" : "address", value);
294
module_sp->SetLoadAddress(target, value, value_is_offset, changed);
295
} else {
296
// No address/offset/slide, load the binary at file address,
297
// offset 0.
298
LLDB_LOGF(log,
299
"DynamicLoader::LoadBinaryWithUUIDAndAddress Loading "
300
"binary %s UUID %s at file address",
301
name.str().c_str(), uuid.GetAsString().c_str());
302
module_sp->SetLoadAddress(target, 0, true /* value_is_slide */,
303
changed);
304
}
305
} else {
306
// In-memory image, load at its true address, offset 0.
307
LLDB_LOGF(log,
308
"DynamicLoader::LoadBinaryWithUUIDAndAddress Loading binary "
309
"%s UUID %s from memory at address 0x%" PRIx64,
310
name.str().c_str(), uuid.GetAsString().c_str(), value);
311
module_sp->SetLoadAddress(target, 0, true /* value_is_slide */,
312
changed);
313
}
314
}
315
316
if (notify) {
317
ModuleList added_module;
318
added_module.Append(module_sp, false);
319
target.ModulesDidLoad(added_module);
320
}
321
} else {
322
if (force_symbol_search) {
323
Stream &s = target.GetDebugger().GetErrorStream();
324
s.Printf("Unable to find file");
325
if (!name.empty())
326
s.Printf(" %s", name.str().c_str());
327
if (uuid.IsValid())
328
s.Printf(" with UUID %s", uuid.GetAsString().c_str());
329
if (value != LLDB_INVALID_ADDRESS) {
330
if (value_is_offset)
331
s.Printf(" with slide 0x%" PRIx64, value);
332
else
333
s.Printf(" at address 0x%" PRIx64, value);
334
}
335
s.Printf("\n");
336
}
337
LLDB_LOGF(log,
338
"Unable to find binary %s with UUID %s and load it at "
339
"%s 0x%" PRIx64,
340
name.str().c_str(), uuid.GetAsString().c_str(),
341
value_is_offset ? "offset" : "address", value);
342
}
343
344
return module_sp;
345
}
346
347
int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr,
348
int size_in_bytes) {
349
Status error;
350
uint64_t value =
351
m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
352
if (error.Fail())
353
return -1;
354
else
355
return (int64_t)value;
356
}
357
358
addr_t DynamicLoader::ReadPointer(addr_t addr) {
359
Status error;
360
addr_t value = m_process->ReadPointerFromMemory(addr, error);
361
if (error.Fail())
362
return LLDB_INVALID_ADDRESS;
363
else
364
return value;
365
}
366
367
void DynamicLoader::LoadOperatingSystemPlugin(bool flush)
368
{
369
if (m_process)
370
m_process->LoadOperatingSystemPlugin(flush);
371
}
372
373
374