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