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