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