1 //===-- DynamicLoaderPOSIXDYLD.cpp ------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 #include "DynamicLoaderPOSIXDYLD.h"
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Symbol/Function.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Target/MemoryRegionInfo.h"
20 #include "lldb/Target/Platform.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/Thread.h"
23 #include "lldb/Target/ThreadPlanRunToAddress.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/ProcessInfo.h"
30 using namespace lldb_private;
32 void DynamicLoaderPOSIXDYLD::Initialize() {
33 PluginManager::RegisterPlugin(GetPluginNameStatic(),
34 GetPluginDescriptionStatic(), CreateInstance);
37 void DynamicLoaderPOSIXDYLD::Terminate() {}
39 lldb_private::ConstString DynamicLoaderPOSIXDYLD::GetPluginName() {
40 return GetPluginNameStatic();
43 lldb_private::ConstString DynamicLoaderPOSIXDYLD::GetPluginNameStatic() {
44 static ConstString g_name("linux-dyld");
48 const char *DynamicLoaderPOSIXDYLD::GetPluginDescriptionStatic() {
49 return "Dynamic loader plug-in that watches for shared library "
50 "loads/unloads in POSIX processes.";
53 uint32_t DynamicLoaderPOSIXDYLD::GetPluginVersion() { return 1; }
55 DynamicLoader *DynamicLoaderPOSIXDYLD::CreateInstance(Process *process,
59 const llvm::Triple &triple_ref =
60 process->GetTarget().GetArchitecture().GetTriple();
61 if (triple_ref.getOS() == llvm::Triple::FreeBSD ||
62 triple_ref.getOS() == llvm::Triple::Linux ||
63 triple_ref.getOS() == llvm::Triple::NetBSD)
68 return new DynamicLoaderPOSIXDYLD(process);
72 DynamicLoaderPOSIXDYLD::DynamicLoaderPOSIXDYLD(Process *process)
73 : DynamicLoader(process), m_rendezvous(process),
74 m_load_offset(LLDB_INVALID_ADDRESS), m_entry_point(LLDB_INVALID_ADDRESS),
75 m_auxv(), m_dyld_bid(LLDB_INVALID_BREAK_ID),
76 m_vdso_base(LLDB_INVALID_ADDRESS),
77 m_interpreter_base(LLDB_INVALID_ADDRESS) {}
79 DynamicLoaderPOSIXDYLD::~DynamicLoaderPOSIXDYLD() {
80 if (m_dyld_bid != LLDB_INVALID_BREAK_ID) {
81 m_process->GetTarget().RemoveBreakpointByID(m_dyld_bid);
82 m_dyld_bid = LLDB_INVALID_BREAK_ID;
86 void DynamicLoaderPOSIXDYLD::DidAttach() {
87 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
89 log->Printf("DynamicLoaderPOSIXDYLD::%s() pid %" PRIu64, __FUNCTION__,
90 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID);
91 m_auxv = llvm::make_unique<AuxVector>(m_process->GetAuxvData());
94 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " reloaded auxv data",
96 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID);
98 // ask the process if it can load any of its own modules
99 m_process->LoadModules();
101 ModuleSP executable_sp = GetTargetExecutable();
102 ResolveExecutableModule(executable_sp);
104 // find the main process load offset
105 addr_t load_offset = ComputeLoadOffset();
107 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
108 " executable '%s', load_offset 0x%" PRIx64,
110 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID,
111 executable_sp ? executable_sp->GetFileSpec().GetPath().c_str()
112 : "<null executable>",
115 EvalSpecialModulesStatus();
117 // if we dont have a load address we cant re-base
118 bool rebase_exec = load_offset != LLDB_INVALID_ADDRESS;
120 // if we have a valid executable
121 if (executable_sp.get()) {
122 lldb_private::ObjectFile *obj = executable_sp->GetObjectFile();
124 // don't rebase if the module already has a load address
125 Target &target = m_process->GetTarget();
126 Address addr = obj->GetImageInfoAddress(&target);
127 if (addr.GetLoadAddress(&target) != LLDB_INVALID_ADDRESS)
131 // no executable, nothing to re-base
135 // if the target executable should be re-based
137 ModuleList module_list;
139 module_list.Append(executable_sp);
141 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
142 " added executable '%s' to module load list",
144 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID,
145 executable_sp->GetFileSpec().GetPath().c_str());
147 UpdateLoadedSections(executable_sp, LLDB_INVALID_ADDRESS, load_offset,
150 LoadAllCurrentModules();
152 m_process->GetTarget().ModulesDidLoad(module_list);
154 log->Printf("DynamicLoaderPOSIXDYLD::%s told the target about the "
155 "modules that loaded:",
157 for (auto module_sp : module_list.Modules()) {
158 log->Printf("-- [module] %s (pid %" PRIu64 ")",
159 module_sp ? module_sp->GetFileSpec().GetPath().c_str()
161 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID);
166 if (executable_sp.get()) {
167 if (!SetRendezvousBreakpoint()) {
168 // If we cannot establish rendezvous breakpoint right now we'll try again
175 void DynamicLoaderPOSIXDYLD::DidLaunch() {
176 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
178 log->Printf("DynamicLoaderPOSIXDYLD::%s()", __FUNCTION__);
183 m_auxv = llvm::make_unique<AuxVector>(m_process->GetAuxvData());
185 executable = GetTargetExecutable();
186 load_offset = ComputeLoadOffset();
187 EvalSpecialModulesStatus();
189 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS) {
190 ModuleList module_list;
191 module_list.Append(executable);
192 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset, true);
195 log->Printf("DynamicLoaderPOSIXDYLD::%s about to call ProbeEntry()",
198 if (!SetRendezvousBreakpoint()) {
199 // If we cannot establish rendezvous breakpoint right now we'll try again
205 m_process->GetTarget().ModulesDidLoad(module_list);
209 Status DynamicLoaderPOSIXDYLD::CanLoadImage() { return Status(); }
211 void DynamicLoaderPOSIXDYLD::UpdateLoadedSections(ModuleSP module,
212 addr_t link_map_addr,
214 bool base_addr_is_offset) {
215 m_loaded_modules[module] = link_map_addr;
216 UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);
219 void DynamicLoaderPOSIXDYLD::UnloadSections(const ModuleSP module) {
220 m_loaded_modules.erase(module);
222 UnloadSectionsCommon(module);
225 void DynamicLoaderPOSIXDYLD::ProbeEntry() {
226 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
228 const addr_t entry = GetEntryPoint();
229 if (entry == LLDB_INVALID_ADDRESS) {
232 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
233 " GetEntryPoint() returned no address, not setting entry breakpoint",
235 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID);
240 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
241 " GetEntryPoint() returned address 0x%" PRIx64
242 ", setting entry breakpoint",
244 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID,
248 Breakpoint *const entry_break =
249 m_process->GetTarget().CreateBreakpoint(entry, true, false).get();
250 entry_break->SetCallback(EntryBreakpointHit, this, true);
251 entry_break->SetBreakpointKind("shared-library-event");
253 // Shoudn't hit this more than once.
254 entry_break->SetOneShot(true);
258 // The runtime linker has run and initialized the rendezvous structure once the
259 // process has hit its entry point. When we hit the corresponding breakpoint
260 // we interrogate the rendezvous structure to get the load addresses of all
261 // dependent modules for the process. Similarly, we can discover the runtime
262 // linker function and setup a breakpoint to notify us of any dynamically
263 // loaded modules (via dlopen).
264 bool DynamicLoaderPOSIXDYLD::EntryBreakpointHit(
265 void *baton, StoppointCallbackContext *context, user_id_t break_id,
266 user_id_t break_loc_id) {
267 assert(baton && "null baton");
271 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
272 DynamicLoaderPOSIXDYLD *const dyld_instance =
273 static_cast<DynamicLoaderPOSIXDYLD *>(baton);
275 log->Printf("DynamicLoaderPOSIXDYLD::%s called for pid %" PRIu64,
277 dyld_instance->m_process ? dyld_instance->m_process->GetID()
278 : LLDB_INVALID_PROCESS_ID);
280 // Disable the breakpoint --- if a stop happens right after this, which we've
281 // seen on occasion, we don't want the breakpoint stepping thread-plan logic
282 // to show a breakpoint instruction at the disassembled entry point to the
283 // program. Disabling it prevents it. (One-shot is not enough - one-shot
284 // removal logic only happens after the breakpoint goes public, which wasn't
285 // happening in our scenario).
286 if (dyld_instance->m_process) {
287 BreakpointSP breakpoint_sp =
288 dyld_instance->m_process->GetTarget().GetBreakpointByID(break_id);
291 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
292 " disabling breakpoint id %" PRIu64,
293 __FUNCTION__, dyld_instance->m_process->GetID(), break_id);
294 breakpoint_sp->SetEnabled(false);
297 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
298 " failed to find breakpoint for breakpoint id %" PRIu64,
299 __FUNCTION__, dyld_instance->m_process->GetID(), break_id);
303 log->Printf("DynamicLoaderPOSIXDYLD::%s breakpoint id %" PRIu64
304 " no Process instance! Cannot disable breakpoint",
305 __FUNCTION__, break_id);
308 dyld_instance->LoadAllCurrentModules();
309 dyld_instance->SetRendezvousBreakpoint();
310 return false; // Continue running.
313 bool DynamicLoaderPOSIXDYLD::SetRendezvousBreakpoint() {
314 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
315 if (m_dyld_bid != LLDB_INVALID_BREAK_ID) {
317 "Rendezvous breakpoint breakpoint id {0} for pid {1}"
320 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID);
325 Target &target = m_process->GetTarget();
326 BreakpointSP dyld_break;
327 if (m_rendezvous.IsValid()) {
328 break_addr = m_rendezvous.GetBreakAddress();
329 LLDB_LOG(log, "Setting rendezvous break address for pid {0} at {1:x}",
330 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID,
332 dyld_break = target.CreateBreakpoint(break_addr, true, false);
334 LLDB_LOG(log, "Rendezvous structure is not set up yet. "
335 "Trying to locate rendezvous breakpoint in the interpreter "
337 ModuleSP interpreter = LoadInterpreterModule();
339 LLDB_LOG(log, "Can't find interpreter, rendezvous breakpoint isn't set.");
343 // Function names from different dynamic loaders that are known to be used
344 // as rendezvous between the loader and debuggers.
345 static std::vector<std::string> DebugStateCandidates{
346 "_dl_debug_state", "rtld_db_dlactivity", "__dl_rtld_db_dlactivity",
347 "r_debug_state", "_r_debug_state", "_rtld_debug_state",
350 FileSpecList containingModules;
351 containingModules.Append(interpreter->GetFileSpec());
352 dyld_break = target.CreateBreakpoint(
353 &containingModules, nullptr /* containingSourceFiles */,
354 DebugStateCandidates, eFunctionNameTypeFull, eLanguageTypeC,
356 eLazyBoolNo, /* skip_prologue */
358 false /* request_hardware */);
361 if (dyld_break->GetNumResolvedLocations() != 1) {
364 "Rendezvous breakpoint has abnormal number of"
365 " resolved locations ({0}) in pid {1}. It's supposed to be exactly 1.",
366 dyld_break->GetNumResolvedLocations(),
367 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID);
369 target.RemoveBreakpointByID(dyld_break->GetID());
373 BreakpointLocationSP location = dyld_break->GetLocationAtIndex(0);
375 "Successfully set rendezvous breakpoint at address {0:x} "
377 location->GetLoadAddress(),
378 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID);
380 dyld_break->SetCallback(RendezvousBreakpointHit, this, true);
381 dyld_break->SetBreakpointKind("shared-library-event");
382 m_dyld_bid = dyld_break->GetID();
386 bool DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit(
387 void *baton, StoppointCallbackContext *context, user_id_t break_id,
388 user_id_t break_loc_id) {
389 assert(baton && "null baton");
393 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
394 DynamicLoaderPOSIXDYLD *const dyld_instance =
395 static_cast<DynamicLoaderPOSIXDYLD *>(baton);
397 log->Printf("DynamicLoaderPOSIXDYLD::%s called for pid %" PRIu64,
399 dyld_instance->m_process ? dyld_instance->m_process->GetID()
400 : LLDB_INVALID_PROCESS_ID);
402 dyld_instance->RefreshModules();
404 // Return true to stop the target, false to just let the target run.
405 const bool stop_when_images_change = dyld_instance->GetStopWhenImagesChange();
407 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
408 " stop_when_images_change=%s",
410 dyld_instance->m_process ? dyld_instance->m_process->GetID()
411 : LLDB_INVALID_PROCESS_ID,
412 stop_when_images_change ? "true" : "false");
413 return stop_when_images_change;
416 void DynamicLoaderPOSIXDYLD::RefreshModules() {
417 if (!m_rendezvous.Resolve())
420 DYLDRendezvous::iterator I;
421 DYLDRendezvous::iterator E;
423 ModuleList &loaded_modules = m_process->GetTarget().GetImages();
425 if (m_rendezvous.ModulesDidLoad()) {
426 ModuleList new_modules;
428 E = m_rendezvous.loaded_end();
429 for (I = m_rendezvous.loaded_begin(); I != E; ++I) {
431 LoadModuleAtAddress(I->file_spec, I->link_addr, I->base_addr, true);
432 if (module_sp.get()) {
433 loaded_modules.AppendIfNeeded(module_sp);
434 new_modules.Append(module_sp);
437 m_process->GetTarget().ModulesDidLoad(new_modules);
440 if (m_rendezvous.ModulesDidUnload()) {
441 ModuleList old_modules;
443 E = m_rendezvous.unloaded_end();
444 for (I = m_rendezvous.unloaded_begin(); I != E; ++I) {
445 ModuleSpec module_spec{I->file_spec};
446 ModuleSP module_sp = loaded_modules.FindFirstModule(module_spec);
448 if (module_sp.get()) {
449 old_modules.Append(module_sp);
450 UnloadSections(module_sp);
453 loaded_modules.Remove(old_modules);
454 m_process->GetTarget().ModulesDidUnload(old_modules, false);
459 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread,
461 ThreadPlanSP thread_plan_sp;
463 StackFrame *frame = thread.GetStackFrameAtIndex(0).get();
464 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol);
465 Symbol *sym = context.symbol;
467 if (sym == nullptr || !sym->IsTrampoline())
468 return thread_plan_sp;
470 ConstString sym_name = sym->GetName();
472 return thread_plan_sp;
474 SymbolContextList target_symbols;
475 Target &target = thread.GetProcess()->GetTarget();
476 const ModuleList &images = target.GetImages();
478 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols);
479 size_t num_targets = target_symbols.GetSize();
481 return thread_plan_sp;
483 typedef std::vector<lldb::addr_t> AddressVector;
485 for (size_t i = 0; i < num_targets; ++i) {
486 SymbolContext context;
488 if (target_symbols.GetContextAtIndex(i, context)) {
489 context.GetAddressRange(eSymbolContextEverything, 0, false, range);
490 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
491 if (addr != LLDB_INVALID_ADDRESS)
492 addrs.push_back(addr);
496 if (addrs.size() > 0) {
497 AddressVector::iterator start = addrs.begin();
498 AddressVector::iterator end = addrs.end();
500 llvm::sort(start, end);
501 addrs.erase(std::unique(start, end), end);
503 std::make_shared<ThreadPlanRunToAddress>(thread, addrs, stop);
506 return thread_plan_sp;
509 void DynamicLoaderPOSIXDYLD::LoadVDSO() {
510 if (m_vdso_base == LLDB_INVALID_ADDRESS)
513 FileSpec file("[vdso]");
515 MemoryRegionInfo info;
516 Status status = m_process->GetMemoryRegionInfo(m_vdso_base, info);
518 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
519 LLDB_LOG(log, "Failed to get vdso region info: {0}", status);
523 if (ModuleSP module_sp = m_process->ReadModuleFromMemory(
524 file, m_vdso_base, info.GetRange().GetByteSize())) {
525 UpdateLoadedSections(module_sp, LLDB_INVALID_ADDRESS, m_vdso_base, false);
526 m_process->GetTarget().GetImages().AppendIfNeeded(module_sp);
530 ModuleSP DynamicLoaderPOSIXDYLD::LoadInterpreterModule() {
531 if (m_interpreter_base == LLDB_INVALID_ADDRESS)
534 MemoryRegionInfo info;
535 Target &target = m_process->GetTarget();
536 Status status = m_process->GetMemoryRegionInfo(m_interpreter_base, info);
537 if (status.Fail() || info.GetMapped() != MemoryRegionInfo::eYes ||
538 info.GetName().IsEmpty()) {
539 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
540 LLDB_LOG(log, "Failed to get interpreter region info: {0}", status);
544 FileSpec file(info.GetName().GetCString());
545 ModuleSpec module_spec(file, target.GetArchitecture());
547 if (ModuleSP module_sp = target.GetOrCreateModule(module_spec,
548 true /* notify */)) {
549 UpdateLoadedSections(module_sp, LLDB_INVALID_ADDRESS, m_interpreter_base,
556 void DynamicLoaderPOSIXDYLD::LoadAllCurrentModules() {
557 DYLDRendezvous::iterator I;
558 DYLDRendezvous::iterator E;
559 ModuleList module_list;
560 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
564 if (!m_rendezvous.Resolve()) {
566 log->Printf("DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD "
567 "rendezvous address",
572 // The rendezvous class doesn't enumerate the main module, so track that
574 ModuleSP executable = GetTargetExecutable();
575 m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress();
577 std::vector<FileSpec> module_names;
578 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I)
579 module_names.push_back(I->file_spec);
580 m_process->PrefetchModuleSpecs(
581 module_names, m_process->GetTarget().GetArchitecture().GetTriple());
583 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) {
585 LoadModuleAtAddress(I->file_spec, I->link_addr, I->base_addr, true);
586 if (module_sp.get()) {
587 LLDB_LOG(log, "LoadAllCurrentModules loading module: {0}",
588 I->file_spec.GetFilename());
589 module_list.Append(module_sp);
591 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
594 "DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64,
595 __FUNCTION__, I->file_spec.GetCString(), I->base_addr);
599 m_process->GetTarget().ModulesDidLoad(module_list);
602 addr_t DynamicLoaderPOSIXDYLD::ComputeLoadOffset() {
605 if (m_load_offset != LLDB_INVALID_ADDRESS)
606 return m_load_offset;
608 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS)
609 return LLDB_INVALID_ADDRESS;
611 ModuleSP module = m_process->GetTarget().GetExecutableModule();
613 return LLDB_INVALID_ADDRESS;
615 ObjectFile *exe = module->GetObjectFile();
617 return LLDB_INVALID_ADDRESS;
619 Address file_entry = exe->GetEntryPointAddress();
621 if (!file_entry.IsValid())
622 return LLDB_INVALID_ADDRESS;
624 m_load_offset = virt_entry - file_entry.GetFileAddress();
625 return m_load_offset;
628 void DynamicLoaderPOSIXDYLD::EvalSpecialModulesStatus() {
629 if (llvm::Optional<uint64_t> vdso_base =
630 m_auxv->GetAuxValue(AuxVector::AUXV_AT_SYSINFO_EHDR))
631 m_vdso_base = *vdso_base;
633 if (llvm::Optional<uint64_t> interpreter_base =
634 m_auxv->GetAuxValue(AuxVector::AUXV_AT_BASE))
635 m_interpreter_base = *interpreter_base;
638 addr_t DynamicLoaderPOSIXDYLD::GetEntryPoint() {
639 if (m_entry_point != LLDB_INVALID_ADDRESS)
640 return m_entry_point;
642 if (m_auxv == nullptr)
643 return LLDB_INVALID_ADDRESS;
645 llvm::Optional<uint64_t> entry_point =
646 m_auxv->GetAuxValue(AuxVector::AUXV_AT_ENTRY);
648 return LLDB_INVALID_ADDRESS;
650 m_entry_point = static_cast<addr_t>(*entry_point);
652 const ArchSpec &arch = m_process->GetTarget().GetArchitecture();
654 // On ppc64, the entry point is actually a descriptor. Dereference it.
655 if (arch.GetMachine() == llvm::Triple::ppc64)
656 m_entry_point = ReadUnsignedIntWithSizeInBytes(m_entry_point, 8);
658 return m_entry_point;
662 DynamicLoaderPOSIXDYLD::GetThreadLocalData(const lldb::ModuleSP module_sp,
663 const lldb::ThreadSP thread,
664 lldb::addr_t tls_file_addr) {
665 auto it = m_loaded_modules.find(module_sp);
666 if (it == m_loaded_modules.end())
667 return LLDB_INVALID_ADDRESS;
669 addr_t link_map = it->second;
670 if (link_map == LLDB_INVALID_ADDRESS)
671 return LLDB_INVALID_ADDRESS;
673 const DYLDRendezvous::ThreadInfo &metadata = m_rendezvous.GetThreadInfo();
675 return LLDB_INVALID_ADDRESS;
677 // Get the thread pointer.
678 addr_t tp = thread->GetThreadPointer();
679 if (tp == LLDB_INVALID_ADDRESS)
680 return LLDB_INVALID_ADDRESS;
682 // Find the module's modid.
683 int modid_size = 4; // FIXME(spucci): This isn't right for big-endian 64-bit
684 int64_t modid = ReadUnsignedIntWithSizeInBytes(
685 link_map + metadata.modid_offset, modid_size);
687 return LLDB_INVALID_ADDRESS;
689 // Lookup the DTV structure for this thread.
690 addr_t dtv_ptr = tp + metadata.dtv_offset;
691 addr_t dtv = ReadPointer(dtv_ptr);
692 if (dtv == LLDB_INVALID_ADDRESS)
693 return LLDB_INVALID_ADDRESS;
695 // Find the TLS block for this module.
696 addr_t dtv_slot = dtv + metadata.dtv_slot_size * modid;
697 addr_t tls_block = ReadPointer(dtv_slot + metadata.tls_offset);
699 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
701 log->Printf("DynamicLoaderPOSIXDYLD::Performed TLS lookup: "
702 "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64
703 ", modid=%" PRId64 ", tls_block=0x%" PRIx64 "\n",
704 module_sp->GetObjectName().AsCString(""), link_map, tp,
705 (int64_t)modid, tls_block);
707 if (tls_block == LLDB_INVALID_ADDRESS)
708 return LLDB_INVALID_ADDRESS;
710 return tls_block + tls_file_addr;
713 void DynamicLoaderPOSIXDYLD::ResolveExecutableModule(
714 lldb::ModuleSP &module_sp) {
715 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
717 if (m_process == nullptr)
720 auto &target = m_process->GetTarget();
721 const auto platform_sp = target.GetPlatform();
723 ProcessInstanceInfo process_info;
724 if (!m_process->GetProcessInfo(process_info)) {
726 log->Printf("DynamicLoaderPOSIXDYLD::%s - failed to get process info for "
728 __FUNCTION__, m_process->GetID());
733 log->Printf("DynamicLoaderPOSIXDYLD::%s - got executable by pid %" PRIu64
735 __FUNCTION__, m_process->GetID(),
736 process_info.GetExecutableFile().GetPath().c_str());
738 ModuleSpec module_spec(process_info.GetExecutableFile(),
739 process_info.GetArchitecture());
740 if (module_sp && module_sp->MatchesModuleSpec(module_spec))
743 const auto executable_search_paths(Target::GetDefaultExecutableSearchPaths());
744 auto error = platform_sp->ResolveExecutable(
745 module_spec, module_sp,
746 !executable_search_paths.IsEmpty() ? &executable_search_paths : nullptr);
749 module_spec.Dump(stream);
752 log->Printf("DynamicLoaderPOSIXDYLD::%s - failed to resolve executable "
753 "with module spec \"%s\": %s",
754 __FUNCTION__, stream.GetData(), error.AsCString());
758 target.SetExecutableModule(module_sp, eLoadDependentsNo);
761 bool DynamicLoaderPOSIXDYLD::AlwaysRelyOnEHUnwindInfo(
762 lldb_private::SymbolContext &sym_ctx) {
765 module_sp = sym_ctx.symbol->GetAddressRef().GetModule();
766 if (!module_sp && sym_ctx.function)
768 sym_ctx.function->GetAddressRange().GetBaseAddress().GetModule();
772 return module_sp->GetFileSpec().GetPath() == "[vdso]";