1 //===-- DynamicLoaderPOSIX.h ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 #include "lldb/Core/PluginManager.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Target/Platform.h"
18 #include "lldb/Core/Section.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/Thread.h"
23 #include "lldb/Target/ThreadPlanRunToAddress.h"
24 #include "lldb/Breakpoint/BreakpointLocation.h"
25 
26 #include "AuxVector.h"
27 #include "DynamicLoaderPOSIXDYLD.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 void
33 DynamicLoaderPOSIXDYLD::Initialize()
34 {
35     PluginManager::RegisterPlugin(GetPluginNameStatic(),
36                                   GetPluginDescriptionStatic(),
37                                   CreateInstance);
38 }
39 
40 void
41 DynamicLoaderPOSIXDYLD::Terminate()
42 {
43 }
44 
45 lldb_private::ConstString
46 DynamicLoaderPOSIXDYLD::GetPluginName()
47 {
48     return GetPluginNameStatic();
49 }
50 
51 lldb_private::ConstString
52 DynamicLoaderPOSIXDYLD::GetPluginNameStatic()
53 {
54     static ConstString g_name("linux-dyld");
55     return g_name;
56 }
57 
58 const char *
59 DynamicLoaderPOSIXDYLD::GetPluginDescriptionStatic()
60 {
61     return "Dynamic loader plug-in that watches for shared library "
62            "loads/unloads in POSIX processes.";
63 }
64 
65 void
66 DynamicLoaderPOSIXDYLD::GetPluginCommandHelp(const char *command, Stream *strm)
67 {
68 }
69 
70 uint32_t
71 DynamicLoaderPOSIXDYLD::GetPluginVersion()
72 {
73     return 1;
74 }
75 
76 DynamicLoader *
77 DynamicLoaderPOSIXDYLD::CreateInstance(Process *process, bool force)
78 {
79     bool create = force;
80     if (!create)
81     {
82         const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
83         if (triple_ref.getOS() == llvm::Triple::Linux ||
84             triple_ref.getOS() == llvm::Triple::FreeBSD)
85             create = true;
86     }
87 
88     if (create)
89         return new DynamicLoaderPOSIXDYLD (process);
90     return NULL;
91 }
92 
93 DynamicLoaderPOSIXDYLD::DynamicLoaderPOSIXDYLD(Process *process)
94     : DynamicLoader(process),
95       m_rendezvous(process),
96       m_load_offset(LLDB_INVALID_ADDRESS),
97       m_entry_point(LLDB_INVALID_ADDRESS),
98       m_auxv(),
99       m_dyld_bid(LLDB_INVALID_BREAK_ID)
100 {
101 }
102 
103 DynamicLoaderPOSIXDYLD::~DynamicLoaderPOSIXDYLD()
104 {
105     if (m_dyld_bid != LLDB_INVALID_BREAK_ID)
106     {
107         m_process->GetTarget().RemoveBreakpointByID (m_dyld_bid);
108         m_dyld_bid = LLDB_INVALID_BREAK_ID;
109     }
110 }
111 
112 void
113 DynamicLoaderPOSIXDYLD::DidAttach()
114 {
115     Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
116     if (log)
117         log->Printf ("DynamicLoaderPOSIXDYLD::%s() pid %" PRIu64, __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID);
118 
119     m_auxv.reset(new AuxVector(m_process));
120     if (log)
121         log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " reloaded auxv data", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID);
122 
123     ModuleSP executable_sp = GetTargetExecutable();
124     ResolveExecutableModule(executable_sp);
125 
126     addr_t load_offset = ComputeLoadOffset();
127     if (log)
128         log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " executable '%s', load_offset 0x%" PRIx64, __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID, executable_sp ? executable_sp->GetFileSpec().GetPath().c_str () : "<null executable>", load_offset);
129 
130 
131     if (executable_sp && load_offset != LLDB_INVALID_ADDRESS)
132     {
133         ModuleList module_list;
134 
135         module_list.Append(executable_sp);
136         if (log)
137             log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " added executable '%s' to module load list",
138                          __FUNCTION__,
139                          m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID,
140                          executable_sp->GetFileSpec().GetPath().c_str ());
141 
142         UpdateLoadedSections(executable_sp, LLDB_INVALID_ADDRESS, load_offset);
143 
144         // When attaching to a target, there are two possible states:
145         // (1) We already crossed the entry point and therefore the rendezvous
146         //     structure is ready to be used and we can load the list of modules
147         //     and place the rendezvous breakpoint.
148         // (2) We didn't cross the entry point yet, so these structures are not
149         //     ready; we should behave as if we just launched the target and
150         //     call ProbeEntry(). This will place a breakpoint on the entry
151         //     point which itself will be hit after the rendezvous structure is
152         //     set up and will perform actions described in (1).
153         if (m_rendezvous.Resolve())
154         {
155             if (log)
156                 log->Printf ("DynamicLoaderPOSIXDYLD::%s() pid %" PRIu64 " rendezvous could resolve: attach assuming dynamic loader info is available now", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID);
157             LoadAllCurrentModules();
158             SetRendezvousBreakpoint();
159         }
160         else
161         {
162             if (log)
163                 log->Printf ("DynamicLoaderPOSIXDYLD::%s() pid %" PRIu64 " rendezvous could not yet resolve: adding breakpoint to catch future rendezvous setup", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID);
164             ProbeEntry();
165         }
166 
167         m_process->GetTarget().ModulesDidLoad(module_list);
168         if (log)
169         {
170             log->Printf ("DynamicLoaderPOSIXDYLD::%s told the target about the modules that loaded:", __FUNCTION__);
171             for (auto module_sp : module_list.Modules ())
172             {
173                 log->Printf ("-- [module] %s (pid %" PRIu64 ")",
174                              module_sp ? module_sp->GetFileSpec().GetPath().c_str () : "<null>",
175                              m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID);
176             }
177         }
178     }
179 }
180 
181 void
182 DynamicLoaderPOSIXDYLD::DidLaunch()
183 {
184     Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
185     if (log)
186         log->Printf ("DynamicLoaderPOSIXDYLD::%s()", __FUNCTION__);
187 
188     ModuleSP executable;
189     addr_t load_offset;
190 
191     m_auxv.reset(new AuxVector(m_process));
192 
193     executable = GetTargetExecutable();
194     load_offset = ComputeLoadOffset();
195 
196     if (executable.get() && load_offset != LLDB_INVALID_ADDRESS)
197     {
198         ModuleList module_list;
199         module_list.Append(executable);
200         UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset);
201 
202         if (log)
203             log->Printf ("DynamicLoaderPOSIXDYLD::%s about to call ProbeEntry()", __FUNCTION__);
204         ProbeEntry();
205 
206         m_process->GetTarget().ModulesDidLoad(module_list);
207     }
208 }
209 
210 Error
211 DynamicLoaderPOSIXDYLD::ExecutePluginCommand(Args &command, Stream *strm)
212 {
213     return Error();
214 }
215 
216 Log *
217 DynamicLoaderPOSIXDYLD::EnablePluginLogging(Stream *strm, Args &command)
218 {
219     return NULL;
220 }
221 
222 Error
223 DynamicLoaderPOSIXDYLD::CanLoadImage()
224 {
225     return Error();
226 }
227 
228 void
229 DynamicLoaderPOSIXDYLD::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, addr_t base_addr)
230 {
231     m_loaded_modules[module] = link_map_addr;
232 
233     UpdateLoadedSectionsCommon(module, base_addr);
234 }
235 
236 void
237 DynamicLoaderPOSIXDYLD::UnloadSections(const ModuleSP module)
238 {
239     m_loaded_modules.erase(module);
240 
241     UnloadSectionsCommon(module);
242 }
243 
244 void
245 DynamicLoaderPOSIXDYLD::ProbeEntry()
246 {
247     Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
248 
249     const addr_t entry = GetEntryPoint();
250     if (entry == LLDB_INVALID_ADDRESS)
251     {
252         if (log)
253             log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " GetEntryPoint() returned no address, not setting entry breakpoint", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID);
254         return;
255     }
256 
257     if (log)
258         log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " GetEntryPoint() returned address 0x%" PRIx64 ", setting entry breakpoint", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID, entry);
259 
260     if (m_process)
261     {
262         Breakpoint *const entry_break = m_process->GetTarget().CreateBreakpoint(entry, true, false).get();
263         entry_break->SetCallback(EntryBreakpointHit, this, true);
264         entry_break->SetBreakpointKind("shared-library-event");
265 
266         // Shoudn't hit this more than once.
267         entry_break->SetOneShot (true);
268     }
269 }
270 
271 // The runtime linker has run and initialized the rendezvous structure once the
272 // process has hit its entry point.  When we hit the corresponding breakpoint we
273 // interrogate the rendezvous structure to get the load addresses of all
274 // dependent modules for the process.  Similarly, we can discover the runtime
275 // linker function and setup a breakpoint to notify us of any dynamically loaded
276 // modules (via dlopen).
277 bool
278 DynamicLoaderPOSIXDYLD::EntryBreakpointHit(void *baton,
279                                            StoppointCallbackContext *context,
280                                            user_id_t break_id,
281                                            user_id_t break_loc_id)
282 {
283     assert(baton && "null baton");
284     if (!baton)
285         return false;
286 
287     Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
288     DynamicLoaderPOSIXDYLD *const dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton);
289     if (log)
290         log->Printf ("DynamicLoaderPOSIXDYLD::%s called for pid %" PRIu64, __FUNCTION__, dyld_instance->m_process ? dyld_instance->m_process->GetID () : LLDB_INVALID_PROCESS_ID);
291 
292     // Disable the breakpoint --- if a stop happens right after this, which we've seen on occasion, we don't
293     // want the breakpoint stepping thread-plan logic to show a breakpoint instruction at the disassembled
294     // entry point to the program.  Disabling it prevents it.  (One-shot is not enough - one-shot removal logic
295     // only happens after the breakpoint goes public, which wasn't happening in our scenario).
296     if (dyld_instance->m_process)
297     {
298         BreakpointSP breakpoint_sp = dyld_instance->m_process->GetTarget().GetBreakpointByID (break_id);
299         if (breakpoint_sp)
300         {
301             if (log)
302                 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " disabling breakpoint id %" PRIu64, __FUNCTION__, dyld_instance->m_process->GetID (), break_id);
303             breakpoint_sp->SetEnabled (false);
304         }
305         else
306         {
307             if (log)
308                 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " failed to find breakpoint for breakpoint id %" PRIu64, __FUNCTION__, dyld_instance->m_process->GetID (), break_id);
309         }
310     }
311     else
312     {
313         if (log)
314             log->Printf ("DynamicLoaderPOSIXDYLD::%s breakpoint id %" PRIu64 " no Process instance!  Cannot disable breakpoint", __FUNCTION__, break_id);
315     }
316 
317     dyld_instance->LoadAllCurrentModules();
318     dyld_instance->SetRendezvousBreakpoint();
319     return false; // Continue running.
320 }
321 
322 void
323 DynamicLoaderPOSIXDYLD::SetRendezvousBreakpoint()
324 {
325     Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
326 
327     addr_t break_addr = m_rendezvous.GetBreakAddress();
328     Target &target = m_process->GetTarget();
329 
330     if (m_dyld_bid == LLDB_INVALID_BREAK_ID)
331     {
332         if (log)
333             log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " setting rendezvous break address at 0x%" PRIx64, __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID, break_addr);
334         Breakpoint *dyld_break = target.CreateBreakpoint (break_addr, true, false).get();
335         dyld_break->SetCallback(RendezvousBreakpointHit, this, true);
336         dyld_break->SetBreakpointKind ("shared-library-event");
337         m_dyld_bid = dyld_break->GetID();
338     }
339     else
340     {
341         if (log)
342             log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " reusing break id %" PRIu32 ", address at 0x%" PRIx64, __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID, m_dyld_bid, break_addr);
343     }
344 
345     // Make sure our breakpoint is at the right address.
346     assert (target.GetBreakpointByID(m_dyld_bid)->FindLocationByAddress(break_addr)->GetBreakpoint().GetID() == m_dyld_bid);
347 }
348 
349 bool
350 DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit(void *baton,
351                                                 StoppointCallbackContext *context,
352                                                 user_id_t break_id,
353                                                 user_id_t break_loc_id)
354 {
355     assert (baton && "null baton");
356     if (!baton)
357         return false;
358 
359     Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
360     DynamicLoaderPOSIXDYLD *const dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton);
361     if (log)
362         log->Printf ("DynamicLoaderPOSIXDYLD::%s called for pid %" PRIu64, __FUNCTION__, dyld_instance->m_process ? dyld_instance->m_process->GetID () : LLDB_INVALID_PROCESS_ID);
363 
364     dyld_instance->RefreshModules();
365 
366     // Return true to stop the target, false to just let the target run.
367     const bool stop_when_images_change = dyld_instance->GetStopWhenImagesChange();
368     if (log)
369         log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " stop_when_images_change=%s", __FUNCTION__, dyld_instance->m_process ? dyld_instance->m_process->GetID () : LLDB_INVALID_PROCESS_ID, stop_when_images_change ? "true" : "false");
370     return stop_when_images_change;
371 }
372 
373 void
374 DynamicLoaderPOSIXDYLD::RefreshModules()
375 {
376     if (!m_rendezvous.Resolve())
377         return;
378 
379     DYLDRendezvous::iterator I;
380     DYLDRendezvous::iterator E;
381 
382     ModuleList &loaded_modules = m_process->GetTarget().GetImages();
383 
384     if (m_rendezvous.ModulesDidLoad())
385     {
386         ModuleList new_modules;
387 
388         E = m_rendezvous.loaded_end();
389         for (I = m_rendezvous.loaded_begin(); I != E; ++I)
390         {
391             FileSpec file(I->path.c_str(), true);
392             ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr);
393             if (module_sp.get())
394             {
395                 loaded_modules.AppendIfNeeded(module_sp);
396                 new_modules.Append(module_sp);
397             }
398         }
399         m_process->GetTarget().ModulesDidLoad(new_modules);
400     }
401 
402     if (m_rendezvous.ModulesDidUnload())
403     {
404         ModuleList old_modules;
405 
406         E = m_rendezvous.unloaded_end();
407         for (I = m_rendezvous.unloaded_begin(); I != E; ++I)
408         {
409             FileSpec file(I->path.c_str(), true);
410             ModuleSpec module_spec (file);
411             ModuleSP module_sp =
412                 loaded_modules.FindFirstModule (module_spec);
413 
414             if (module_sp.get())
415             {
416                 old_modules.Append(module_sp);
417                 UnloadSections(module_sp);
418             }
419         }
420         loaded_modules.Remove(old_modules);
421         m_process->GetTarget().ModulesDidUnload(old_modules, false);
422     }
423 }
424 
425 ThreadPlanSP
426 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, bool stop)
427 {
428     ThreadPlanSP thread_plan_sp;
429 
430     StackFrame *frame = thread.GetStackFrameAtIndex(0).get();
431     const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol);
432     Symbol *sym = context.symbol;
433 
434     if (sym == NULL || !sym->IsTrampoline())
435         return thread_plan_sp;
436 
437     const ConstString &sym_name = sym->GetMangled().GetName(Mangled::ePreferMangled);
438     if (!sym_name)
439         return thread_plan_sp;
440 
441     SymbolContextList target_symbols;
442     Target &target = thread.GetProcess()->GetTarget();
443     const ModuleList &images = target.GetImages();
444 
445     images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols);
446     size_t num_targets = target_symbols.GetSize();
447     if (!num_targets)
448         return thread_plan_sp;
449 
450     typedef std::vector<lldb::addr_t> AddressVector;
451     AddressVector addrs;
452     for (size_t i = 0; i < num_targets; ++i)
453     {
454         SymbolContext context;
455         AddressRange range;
456         if (target_symbols.GetContextAtIndex(i, context))
457         {
458             context.GetAddressRange(eSymbolContextEverything, 0, false, range);
459             lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
460             if (addr != LLDB_INVALID_ADDRESS)
461                 addrs.push_back(addr);
462         }
463     }
464 
465     if (addrs.size() > 0)
466     {
467         AddressVector::iterator start = addrs.begin();
468         AddressVector::iterator end = addrs.end();
469 
470         std::sort(start, end);
471         addrs.erase(std::unique(start, end), end);
472         thread_plan_sp.reset(new ThreadPlanRunToAddress(thread, addrs, stop));
473     }
474 
475     return thread_plan_sp;
476 }
477 
478 void
479 DynamicLoaderPOSIXDYLD::LoadAllCurrentModules()
480 {
481     DYLDRendezvous::iterator I;
482     DYLDRendezvous::iterator E;
483     ModuleList module_list;
484 
485     if (!m_rendezvous.Resolve())
486     {
487         Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
488         if (log)
489             log->Printf("DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD rendezvous address",
490                         __FUNCTION__);
491         return;
492     }
493 
494     // The rendezvous class doesn't enumerate the main module, so track
495     // that ourselves here.
496     ModuleSP executable = GetTargetExecutable();
497     m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress();
498 
499 
500     for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I)
501     {
502         const char *module_path = I->path.c_str();
503         FileSpec file(module_path, false);
504         ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr);
505         if (module_sp.get())
506         {
507             module_list.Append(module_sp);
508         }
509         else
510         {
511             Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
512             if (log)
513                 log->Printf("DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64,
514                             __FUNCTION__, module_path, I->base_addr);
515         }
516     }
517 
518     m_process->GetTarget().ModulesDidLoad(module_list);
519 }
520 
521 addr_t
522 DynamicLoaderPOSIXDYLD::ComputeLoadOffset()
523 {
524     addr_t virt_entry;
525 
526     if (m_load_offset != LLDB_INVALID_ADDRESS)
527         return m_load_offset;
528 
529     if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS)
530         return LLDB_INVALID_ADDRESS;
531 
532     ModuleSP module = m_process->GetTarget().GetExecutableModule();
533     if (!module)
534         return LLDB_INVALID_ADDRESS;
535 
536     ObjectFile *exe = module->GetObjectFile();
537     if (!exe)
538         return LLDB_INVALID_ADDRESS;
539 
540     Address file_entry = exe->GetEntryPointAddress();
541 
542     if (!file_entry.IsValid())
543         return LLDB_INVALID_ADDRESS;
544 
545     m_load_offset = virt_entry - file_entry.GetFileAddress();
546     return m_load_offset;
547 }
548 
549 addr_t
550 DynamicLoaderPOSIXDYLD::GetEntryPoint()
551 {
552     if (m_entry_point != LLDB_INVALID_ADDRESS)
553         return m_entry_point;
554 
555     if (m_auxv.get() == NULL)
556         return LLDB_INVALID_ADDRESS;
557 
558     AuxVector::iterator I = m_auxv->FindEntry(AuxVector::AT_ENTRY);
559 
560     if (I == m_auxv->end())
561         return LLDB_INVALID_ADDRESS;
562 
563     m_entry_point = static_cast<addr_t>(I->value);
564 
565     const ArchSpec &arch = m_process->GetTarget().GetArchitecture();
566 
567     // On ppc64, the entry point is actually a descriptor.  Dereference it.
568     if (arch.GetMachine() == llvm::Triple::ppc64)
569         m_entry_point = ReadUnsignedIntWithSizeInBytes(m_entry_point, 8);
570 
571     return m_entry_point;
572 }
573 
574 lldb::addr_t
575 DynamicLoaderPOSIXDYLD::GetThreadLocalData (const lldb::ModuleSP module, const lldb::ThreadSP thread)
576 {
577     auto it = m_loaded_modules.find (module);
578     if (it == m_loaded_modules.end())
579         return LLDB_INVALID_ADDRESS;
580 
581     addr_t link_map = it->second;
582     if (link_map == LLDB_INVALID_ADDRESS)
583         return LLDB_INVALID_ADDRESS;
584 
585     const DYLDRendezvous::ThreadInfo &metadata = m_rendezvous.GetThreadInfo();
586     if (!metadata.valid)
587         return LLDB_INVALID_ADDRESS;
588 
589     // Get the thread pointer.
590     addr_t tp = thread->GetThreadPointer ();
591     if (tp == LLDB_INVALID_ADDRESS)
592         return LLDB_INVALID_ADDRESS;
593 
594     // Find the module's modid.
595     int modid_size = 4;  // FIXME(spucci): This isn't right for big-endian 64-bit
596     int64_t modid = ReadUnsignedIntWithSizeInBytes (link_map + metadata.modid_offset, modid_size);
597     if (modid == -1)
598         return LLDB_INVALID_ADDRESS;
599 
600     // Lookup the DTV structure for this thread.
601     addr_t dtv_ptr = tp + metadata.dtv_offset;
602     addr_t dtv = ReadPointer (dtv_ptr);
603     if (dtv == LLDB_INVALID_ADDRESS)
604         return LLDB_INVALID_ADDRESS;
605 
606     // Find the TLS block for this module.
607     addr_t dtv_slot = dtv + metadata.dtv_slot_size*modid;
608     addr_t tls_block = ReadPointer (dtv_slot + metadata.tls_offset);
609 
610     Module *mod = module.get();
611     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
612     if (log)
613         log->Printf("DynamicLoaderPOSIXDYLD::Performed TLS lookup: "
614                     "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64 ", modid=%" PRId64 ", tls_block=0x%" PRIx64 "\n",
615                     mod->GetObjectName().AsCString(""), link_map, tp, (int64_t)modid, tls_block);
616 
617     return tls_block;
618 }
619 
620 void
621 DynamicLoaderPOSIXDYLD::ResolveExecutableModule (lldb::ModuleSP &module_sp)
622 {
623     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
624 
625     if (m_process == nullptr)
626         return;
627 
628     auto &target = m_process->GetTarget ();
629     const auto platform_sp = target.GetPlatform ();
630 
631     ProcessInstanceInfo process_info;
632     if (!platform_sp->GetProcessInfo (m_process->GetID (), process_info))
633     {
634         if (log)
635             log->Printf ("DynamicLoaderPOSIXDYLD::%s - failed to get process info for pid %" PRIu64,
636                          __FUNCTION__, m_process->GetID ());
637         return;
638     }
639 
640     if (log)
641         log->Printf ("DynamicLoaderPOSIXDYLD::%s - got executable by pid %" PRIu64 ": %s",
642                      __FUNCTION__, m_process->GetID (), process_info.GetExecutableFile ().GetPath ().c_str ());
643 
644     ModuleSpec module_spec (process_info.GetExecutableFile (), process_info.GetArchitecture ());
645     if (module_sp && module_sp->MatchesModuleSpec (module_spec))
646         return;
647 
648     auto error = platform_sp->ResolveExecutable (module_spec, module_sp, nullptr);
649     if (error.Fail ())
650     {
651         StreamString stream;
652         module_spec.Dump (stream);
653 
654         if (log)
655             log->Printf ("DynamicLoaderPOSIXDYLD::%s - failed to resolve executable with module spec \"%s\": %s",
656                          __FUNCTION__, stream.GetString ().c_str (), error.AsCString ());
657         return;
658     }
659 
660     target.SetExecutableModule (module_sp, false);
661 }
662