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