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