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