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