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