1 //===-- DynamicLoaderPOSIX.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
25 #include "AuxVector.h"
26 #include "DynamicLoaderPOSIXDYLD.h"
29 using namespace lldb_private;
32 DynamicLoaderPOSIXDYLD::Initialize()
34 PluginManager::RegisterPlugin(GetPluginNameStatic(),
35 GetPluginDescriptionStatic(),
40 DynamicLoaderPOSIXDYLD::Terminate()
44 lldb_private::ConstString
45 DynamicLoaderPOSIXDYLD::GetPluginName()
47 return GetPluginNameStatic();
50 lldb_private::ConstString
51 DynamicLoaderPOSIXDYLD::GetPluginNameStatic()
53 static ConstString g_name("linux-dyld");
58 DynamicLoaderPOSIXDYLD::GetPluginDescriptionStatic()
60 return "Dynamic loader plug-in that watches for shared library "
61 "loads/unloads in POSIX processes.";
65 DynamicLoaderPOSIXDYLD::GetPluginCommandHelp(const char *command, Stream *strm)
70 DynamicLoaderPOSIXDYLD::GetPluginVersion()
76 DynamicLoaderPOSIXDYLD::CreateInstance(Process *process, bool force)
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)
88 return new DynamicLoaderPOSIXDYLD (process);
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),
98 m_dyld_bid(LLDB_INVALID_BREAK_ID)
102 DynamicLoaderPOSIXDYLD::~DynamicLoaderPOSIXDYLD()
104 if (m_dyld_bid != LLDB_INVALID_BREAK_ID)
106 m_process->GetTarget().RemoveBreakpointByID (m_dyld_bid);
107 m_dyld_bid = LLDB_INVALID_BREAK_ID;
112 DynamicLoaderPOSIXDYLD::DidAttach()
117 m_auxv.reset(new AuxVector(m_process));
119 executable = GetTargetExecutable();
120 load_offset = ComputeLoadOffset();
122 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS)
124 ModuleList module_list;
125 module_list.Append(executable);
126 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset);
127 LoadAllCurrentModules();
128 m_process->GetTarget().ModulesDidLoad(module_list);
133 DynamicLoaderPOSIXDYLD::DidLaunch()
138 m_auxv.reset(new AuxVector(m_process));
140 executable = GetTargetExecutable();
141 load_offset = ComputeLoadOffset();
143 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS)
145 ModuleList module_list;
146 module_list.Append(executable);
147 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset);
149 m_process->GetTarget().ModulesDidLoad(module_list);
154 DynamicLoaderPOSIXDYLD::GetTargetExecutable()
156 Target &target = m_process->GetTarget();
157 ModuleSP executable = target.GetExecutableModule();
159 if (executable.get())
161 if (executable->GetFileSpec().Exists())
163 ModuleSpec module_spec (executable->GetFileSpec(), executable->GetArchitecture());
164 ModuleSP module_sp (new Module (module_spec));
166 // Check if the executable has changed and set it to the target executable if they differ.
167 if (module_sp.get() && module_sp->GetUUID().IsValid() && executable->GetUUID().IsValid())
169 if (module_sp->GetUUID() != executable->GetUUID())
172 else if (executable->FileHasChanged())
177 if (!executable.get())
179 executable = target.GetSharedModule(module_spec);
180 if (executable.get() != target.GetExecutableModulePointer())
182 // Don't load dependent images since we are in dyld where we will know
183 // and find out about all images that are loaded
184 const bool get_dependent_images = false;
185 target.SetExecutableModule(executable, get_dependent_images);
194 DynamicLoaderPOSIXDYLD::ExecutePluginCommand(Args &command, Stream *strm)
200 DynamicLoaderPOSIXDYLD::EnablePluginLogging(Stream *strm, Args &command)
206 DynamicLoaderPOSIXDYLD::CanLoadImage()
212 DynamicLoaderPOSIXDYLD::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, addr_t base_addr)
214 SectionLoadList &load_list = m_process->GetTarget().GetSectionLoadList();
215 const SectionList *sections = GetSectionListFromModule(module);
217 assert(sections && "SectionList missing from loaded module.");
219 m_loaded_modules[module] = link_map_addr;
221 const size_t num_sections = sections->GetSize();
223 for (unsigned i = 0; i < num_sections; ++i)
225 SectionSP section_sp (sections->GetSectionAtIndex(i));
226 lldb::addr_t new_load_addr = section_sp->GetFileAddress() + base_addr;
227 lldb::addr_t old_load_addr = load_list.GetSectionLoadAddress(section_sp);
229 // If the file address of the section is zero then this is not an
230 // allocatable/loadable section (property of ELF sh_addr). Skip it.
231 if (new_load_addr == base_addr)
234 if (old_load_addr == LLDB_INVALID_ADDRESS ||
235 old_load_addr != new_load_addr)
236 load_list.SetSectionLoadAddress(section_sp, new_load_addr);
241 DynamicLoaderPOSIXDYLD::UnloadSections(const ModuleSP module)
243 SectionLoadList &load_list = m_process->GetTarget().GetSectionLoadList();
244 const SectionList *sections = GetSectionListFromModule(module);
246 assert(sections && "SectionList missing from unloaded module.");
248 m_loaded_modules.erase(module);
250 const size_t num_sections = sections->GetSize();
251 for (size_t i = 0; i < num_sections; ++i)
253 SectionSP section_sp (sections->GetSectionAtIndex(i));
254 load_list.SetSectionUnloaded(section_sp);
259 DynamicLoaderPOSIXDYLD::ProbeEntry()
261 Breakpoint *entry_break;
264 if ((entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS)
267 entry_break = m_process->GetTarget().CreateBreakpoint(entry, true, false).get();
268 entry_break->SetCallback(EntryBreakpointHit, this, true);
269 entry_break->SetBreakpointKind("shared-library-event");
272 // The runtime linker has run and initialized the rendezvous structure once the
273 // process has hit its entry point. When we hit the corresponding breakpoint we
274 // interrogate the rendezvous structure to get the load addresses of all
275 // dependent modules for the process. Similarly, we can discover the runtime
276 // linker function and setup a breakpoint to notify us of any dynamically loaded
277 // modules (via dlopen).
279 DynamicLoaderPOSIXDYLD::EntryBreakpointHit(void *baton,
280 StoppointCallbackContext *context,
282 user_id_t break_loc_id)
284 DynamicLoaderPOSIXDYLD* dyld_instance;
286 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton);
287 dyld_instance->LoadAllCurrentModules();
288 dyld_instance->SetRendezvousBreakpoint();
289 return false; // Continue running.
293 DynamicLoaderPOSIXDYLD::SetRendezvousBreakpoint()
295 addr_t break_addr = m_rendezvous.GetBreakAddress();
296 Target &target = m_process->GetTarget();
298 if (m_dyld_bid == LLDB_INVALID_BREAK_ID)
300 Breakpoint *dyld_break = target.CreateBreakpoint (break_addr, true, false).get();
301 dyld_break->SetCallback(RendezvousBreakpointHit, this, true);
302 dyld_break->SetBreakpointKind ("shared-library-event");
303 m_dyld_bid = dyld_break->GetID();
306 // Make sure our breakpoint is at the right address.
307 assert (target.GetBreakpointByID(m_dyld_bid)->FindLocationByAddress(break_addr)->GetBreakpoint().GetID() == m_dyld_bid);
311 DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit(void *baton,
312 StoppointCallbackContext *context,
314 user_id_t break_loc_id)
316 DynamicLoaderPOSIXDYLD* dyld_instance;
318 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton);
319 dyld_instance->RefreshModules();
321 // Return true to stop the target, false to just let the target run.
322 return dyld_instance->GetStopWhenImagesChange();
326 DynamicLoaderPOSIXDYLD::RefreshModules()
328 if (!m_rendezvous.Resolve())
331 DYLDRendezvous::iterator I;
332 DYLDRendezvous::iterator E;
334 ModuleList &loaded_modules = m_process->GetTarget().GetImages();
336 if (m_rendezvous.ModulesDidLoad())
338 ModuleList new_modules;
340 E = m_rendezvous.loaded_end();
341 for (I = m_rendezvous.loaded_begin(); I != E; ++I)
343 FileSpec file(I->path.c_str(), true);
344 ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr);
347 loaded_modules.AppendIfNeeded(module_sp);
348 new_modules.Append(module_sp);
351 m_process->GetTarget().ModulesDidLoad(new_modules);
354 if (m_rendezvous.ModulesDidUnload())
356 ModuleList old_modules;
358 E = m_rendezvous.unloaded_end();
359 for (I = m_rendezvous.unloaded_begin(); I != E; ++I)
361 FileSpec file(I->path.c_str(), true);
362 ModuleSpec module_spec (file);
364 loaded_modules.FindFirstModule (module_spec);
368 old_modules.Append(module_sp);
369 UnloadSections(module_sp);
372 loaded_modules.Remove(old_modules);
373 m_process->GetTarget().ModulesDidUnload(old_modules, false);
378 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, bool stop)
380 ThreadPlanSP thread_plan_sp;
382 StackFrame *frame = thread.GetStackFrameAtIndex(0).get();
383 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol);
384 Symbol *sym = context.symbol;
386 if (sym == NULL || !sym->IsTrampoline())
387 return thread_plan_sp;
389 const ConstString &sym_name = sym->GetMangled().GetName(Mangled::ePreferMangled);
391 return thread_plan_sp;
393 SymbolContextList target_symbols;
394 Target &target = thread.GetProcess()->GetTarget();
395 const ModuleList &images = target.GetImages();
397 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols);
398 size_t num_targets = target_symbols.GetSize();
400 return thread_plan_sp;
402 typedef std::vector<lldb::addr_t> AddressVector;
404 for (size_t i = 0; i < num_targets; ++i)
406 SymbolContext context;
408 if (target_symbols.GetContextAtIndex(i, context))
410 context.GetAddressRange(eSymbolContextEverything, 0, false, range);
411 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
412 if (addr != LLDB_INVALID_ADDRESS)
413 addrs.push_back(addr);
417 if (addrs.size() > 0)
419 AddressVector::iterator start = addrs.begin();
420 AddressVector::iterator end = addrs.end();
422 std::sort(start, end);
423 addrs.erase(std::unique(start, end), end);
424 thread_plan_sp.reset(new ThreadPlanRunToAddress(thread, addrs, stop));
427 return thread_plan_sp;
431 DynamicLoaderPOSIXDYLD::LoadAllCurrentModules()
433 DYLDRendezvous::iterator I;
434 DYLDRendezvous::iterator E;
435 ModuleList module_list;
437 if (!m_rendezvous.Resolve())
439 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
441 log->Printf("DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD rendezvous address",
446 // The rendezvous class doesn't enumerate the main module, so track
447 // that ourselves here.
448 ModuleSP executable = GetTargetExecutable();
449 m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress();
452 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I)
454 const char *module_path = I->path.c_str();
455 FileSpec file(module_path, false);
456 ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr);
459 module_list.Append(module_sp);
463 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
465 log->Printf("DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64,
466 __FUNCTION__, module_path, I->base_addr);
470 m_process->GetTarget().ModulesDidLoad(module_list);
474 DynamicLoaderPOSIXDYLD::LoadModuleAtAddress(const FileSpec &file, addr_t link_map_addr, addr_t base_addr)
476 Target &target = m_process->GetTarget();
477 ModuleList &modules = target.GetImages();
480 ModuleSpec module_spec (file, target.GetArchitecture());
481 if ((module_sp = modules.FindFirstModule (module_spec)))
483 UpdateLoadedSections(module_sp, link_map_addr, base_addr);
485 else if ((module_sp = target.GetSharedModule(module_spec)))
487 UpdateLoadedSections(module_sp, link_map_addr, base_addr);
494 DynamicLoaderPOSIXDYLD::ComputeLoadOffset()
498 if (m_load_offset != LLDB_INVALID_ADDRESS)
499 return m_load_offset;
501 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS)
502 return LLDB_INVALID_ADDRESS;
504 ModuleSP module = m_process->GetTarget().GetExecutableModule();
506 return LLDB_INVALID_ADDRESS;
508 ObjectFile *exe = module->GetObjectFile();
509 Address file_entry = exe->GetEntryPointAddress();
511 if (!file_entry.IsValid())
512 return LLDB_INVALID_ADDRESS;
514 m_load_offset = virt_entry - file_entry.GetFileAddress();
515 return m_load_offset;
519 DynamicLoaderPOSIXDYLD::GetEntryPoint()
521 if (m_entry_point != LLDB_INVALID_ADDRESS)
522 return m_entry_point;
524 if (m_auxv.get() == NULL)
525 return LLDB_INVALID_ADDRESS;
527 AuxVector::iterator I = m_auxv->FindEntry(AuxVector::AT_ENTRY);
529 if (I == m_auxv->end())
530 return LLDB_INVALID_ADDRESS;
532 m_entry_point = static_cast<addr_t>(I->value);
533 return m_entry_point;
537 DynamicLoaderPOSIXDYLD::GetSectionListFromModule(const ModuleSP module) const
539 SectionList *sections = nullptr;
542 ObjectFile *obj_file = module->GetObjectFile();
545 sections = obj_file->GetSectionList();
551 static int ReadInt(Process *process, addr_t addr)
554 int value = (int)process->ReadUnsignedIntegerFromMemory(addr, sizeof(uint32_t), 0, error);
561 static addr_t ReadPointer(Process *process, addr_t addr)
564 addr_t value = process->ReadPointerFromMemory(addr, error);
566 return LLDB_INVALID_ADDRESS;
572 DynamicLoaderPOSIXDYLD::GetThreadLocalData (const lldb::ModuleSP module, const lldb::ThreadSP thread)
574 auto it = m_loaded_modules.find (module);
575 if (it == m_loaded_modules.end())
576 return LLDB_INVALID_ADDRESS;
578 addr_t link_map = it->second;
579 if (link_map == LLDB_INVALID_ADDRESS)
580 return LLDB_INVALID_ADDRESS;
582 const DYLDRendezvous::ThreadInfo &metadata = m_rendezvous.GetThreadInfo();
584 return LLDB_INVALID_ADDRESS;
586 // Get the thread pointer.
587 addr_t tp = thread->GetThreadPointer ();
588 if (tp == LLDB_INVALID_ADDRESS)
589 return LLDB_INVALID_ADDRESS;
591 // Find the module's modid.
592 int modid = ReadInt (m_process, link_map + metadata.modid_offset);
594 return LLDB_INVALID_ADDRESS;
596 // Lookup the DTV stucture for this thread.
597 addr_t dtv_ptr = tp + metadata.dtv_offset;
598 addr_t dtv = ReadPointer (m_process, dtv_ptr);
599 if (dtv == LLDB_INVALID_ADDRESS)
600 return LLDB_INVALID_ADDRESS;
602 // Find the TLS block for this module.
603 addr_t dtv_slot = dtv + metadata.dtv_slot_size*modid;
604 addr_t tls_block = ReadPointer (m_process, dtv_slot + metadata.tls_offset);
606 Module *mod = module.get();
607 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
609 log->Printf("DynamicLoaderPOSIXDYLD::Performed TLS lookup: "
610 "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64 ", modid=%i, tls_block=0x%" PRIx64 "\n",
611 mod->GetObjectName().AsCString(""), link_map, tp, modid, tls_block);