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 // Function names from different dynamic loaders that are known to be 337 // used as rendezvous between the loader and debuggers. 338 static std::vector<std::string> DebugStateCandidates{ 339 "_dl_debug_state", "rtld_db_dlactivity", "__dl_rtld_db_dlactivity", 340 "r_debug_state", "_r_debug_state", "_rtld_debug_state", 341 }; 342 343 ModuleSP interpreter = LoadInterpreterModule(); 344 if (!interpreter) { 345 FileSpecList containingModules; 346 containingModules.Append( 347 m_process->GetTarget().GetExecutableModulePointer()->GetFileSpec()); 348 349 dyld_break = target.CreateBreakpoint( 350 &containingModules, /*containingSourceFiles=*/nullptr, 351 DebugStateCandidates, eFunctionNameTypeFull, eLanguageTypeC, 352 /*offset=*/0, 353 /*skip_prologue=*/eLazyBoolNo, 354 /*internal=*/true, 355 /*request_hardware=*/false); 356 } else { 357 FileSpecList containingModules; 358 containingModules.Append(interpreter->GetFileSpec()); 359 dyld_break = target.CreateBreakpoint( 360 &containingModules, /*containingSourceFiles=*/nullptr, 361 DebugStateCandidates, eFunctionNameTypeFull, eLanguageTypeC, 362 /*offset=*/0, 363 /*skip_prologue=*/eLazyBoolNo, 364 /*internal=*/true, 365 /*request_hardware=*/false); 366 } 367 } 368 369 if (dyld_break->GetNumResolvedLocations() != 1) { 370 LLDB_LOG( 371 log, 372 "Rendezvous breakpoint has abnormal number of" 373 " resolved locations ({0}) in pid {1}. It's supposed to be exactly 1.", 374 dyld_break->GetNumResolvedLocations(), 375 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID); 376 377 target.RemoveBreakpointByID(dyld_break->GetID()); 378 return false; 379 } 380 381 BreakpointLocationSP location = dyld_break->GetLocationAtIndex(0); 382 LLDB_LOG(log, 383 "Successfully set rendezvous breakpoint at address {0:x} " 384 "for pid {1}", 385 location->GetLoadAddress(), 386 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID); 387 388 dyld_break->SetCallback(RendezvousBreakpointHit, this, true); 389 dyld_break->SetBreakpointKind("shared-library-event"); 390 m_dyld_bid = dyld_break->GetID(); 391 return true; 392 } 393 394 bool DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit( 395 void *baton, StoppointCallbackContext *context, user_id_t break_id, 396 user_id_t break_loc_id) { 397 assert(baton && "null baton"); 398 if (!baton) 399 return false; 400 401 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 402 DynamicLoaderPOSIXDYLD *const dyld_instance = 403 static_cast<DynamicLoaderPOSIXDYLD *>(baton); 404 LLDB_LOGF(log, "DynamicLoaderPOSIXDYLD::%s called for pid %" PRIu64, 405 __FUNCTION__, 406 dyld_instance->m_process ? dyld_instance->m_process->GetID() 407 : LLDB_INVALID_PROCESS_ID); 408 409 dyld_instance->RefreshModules(); 410 411 // Return true to stop the target, false to just let the target run. 412 const bool stop_when_images_change = dyld_instance->GetStopWhenImagesChange(); 413 LLDB_LOGF(log, 414 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 415 " stop_when_images_change=%s", 416 __FUNCTION__, 417 dyld_instance->m_process ? dyld_instance->m_process->GetID() 418 : LLDB_INVALID_PROCESS_ID, 419 stop_when_images_change ? "true" : "false"); 420 return stop_when_images_change; 421 } 422 423 void DynamicLoaderPOSIXDYLD::RefreshModules() { 424 if (!m_rendezvous.Resolve()) 425 return; 426 427 DYLDRendezvous::iterator I; 428 DYLDRendezvous::iterator E; 429 430 ModuleList &loaded_modules = m_process->GetTarget().GetImages(); 431 432 if (m_rendezvous.ModulesDidLoad() || !m_initial_modules_added) { 433 ModuleList new_modules; 434 435 // If this is the first time rendezvous breakpoint fires, we need 436 // to take care of adding all the initial modules reported by 437 // the loader. This is necessary to list ld-linux.so on Linux, 438 // and all DT_NEEDED entries on *BSD. 439 if (m_initial_modules_added) { 440 I = m_rendezvous.loaded_begin(); 441 E = m_rendezvous.loaded_end(); 442 } else { 443 I = m_rendezvous.begin(); 444 E = m_rendezvous.end(); 445 m_initial_modules_added = true; 446 } 447 for (; I != E; ++I) { 448 ModuleSP module_sp = 449 LoadModuleAtAddress(I->file_spec, I->link_addr, I->base_addr, true); 450 if (module_sp.get()) { 451 if (module_sp->GetObjectFile()->GetBaseAddress().GetLoadAddress( 452 &m_process->GetTarget()) == m_interpreter_base && 453 module_sp != m_interpreter_module.lock()) { 454 // If this is a duplicate instance of ld.so, unload it. We may end up 455 // with it if we load it via a different path than before (symlink 456 // vs real path). 457 // TODO: remove this once we either fix library matching or avoid 458 // loading the interpreter when setting the rendezvous breakpoint. 459 UnloadSections(module_sp); 460 loaded_modules.Remove(module_sp); 461 continue; 462 } 463 464 loaded_modules.AppendIfNeeded(module_sp); 465 new_modules.Append(module_sp); 466 } 467 } 468 m_process->GetTarget().ModulesDidLoad(new_modules); 469 } 470 471 if (m_rendezvous.ModulesDidUnload()) { 472 ModuleList old_modules; 473 474 E = m_rendezvous.unloaded_end(); 475 for (I = m_rendezvous.unloaded_begin(); I != E; ++I) { 476 ModuleSpec module_spec{I->file_spec}; 477 ModuleSP module_sp = loaded_modules.FindFirstModule(module_spec); 478 479 if (module_sp.get()) { 480 old_modules.Append(module_sp); 481 UnloadSections(module_sp); 482 } 483 } 484 loaded_modules.Remove(old_modules); 485 m_process->GetTarget().ModulesDidUnload(old_modules, false); 486 } 487 } 488 489 ThreadPlanSP 490 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, 491 bool stop) { 492 ThreadPlanSP thread_plan_sp; 493 494 StackFrame *frame = thread.GetStackFrameAtIndex(0).get(); 495 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol); 496 Symbol *sym = context.symbol; 497 498 if (sym == nullptr || !sym->IsTrampoline()) 499 return thread_plan_sp; 500 501 ConstString sym_name = sym->GetName(); 502 if (!sym_name) 503 return thread_plan_sp; 504 505 SymbolContextList target_symbols; 506 Target &target = thread.GetProcess()->GetTarget(); 507 const ModuleList &images = target.GetImages(); 508 509 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols); 510 size_t num_targets = target_symbols.GetSize(); 511 if (!num_targets) 512 return thread_plan_sp; 513 514 typedef std::vector<lldb::addr_t> AddressVector; 515 AddressVector addrs; 516 for (size_t i = 0; i < num_targets; ++i) { 517 SymbolContext context; 518 AddressRange range; 519 if (target_symbols.GetContextAtIndex(i, context)) { 520 context.GetAddressRange(eSymbolContextEverything, 0, false, range); 521 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target); 522 if (addr != LLDB_INVALID_ADDRESS) 523 addrs.push_back(addr); 524 } 525 } 526 527 if (addrs.size() > 0) { 528 AddressVector::iterator start = addrs.begin(); 529 AddressVector::iterator end = addrs.end(); 530 531 llvm::sort(start, end); 532 addrs.erase(std::unique(start, end), end); 533 thread_plan_sp = 534 std::make_shared<ThreadPlanRunToAddress>(thread, addrs, stop); 535 } 536 537 return thread_plan_sp; 538 } 539 540 void DynamicLoaderPOSIXDYLD::LoadVDSO() { 541 if (m_vdso_base == LLDB_INVALID_ADDRESS) 542 return; 543 544 FileSpec file("[vdso]"); 545 546 MemoryRegionInfo info; 547 Status status = m_process->GetMemoryRegionInfo(m_vdso_base, info); 548 if (status.Fail()) { 549 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 550 LLDB_LOG(log, "Failed to get vdso region info: {0}", status); 551 return; 552 } 553 554 if (ModuleSP module_sp = m_process->ReadModuleFromMemory( 555 file, m_vdso_base, info.GetRange().GetByteSize())) { 556 UpdateLoadedSections(module_sp, LLDB_INVALID_ADDRESS, m_vdso_base, false); 557 m_process->GetTarget().GetImages().AppendIfNeeded(module_sp); 558 } 559 } 560 561 ModuleSP DynamicLoaderPOSIXDYLD::LoadInterpreterModule() { 562 if (m_interpreter_base == LLDB_INVALID_ADDRESS) 563 return nullptr; 564 565 MemoryRegionInfo info; 566 Target &target = m_process->GetTarget(); 567 Status status = m_process->GetMemoryRegionInfo(m_interpreter_base, info); 568 if (status.Fail() || info.GetMapped() != MemoryRegionInfo::eYes || 569 info.GetName().IsEmpty()) { 570 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 571 LLDB_LOG(log, "Failed to get interpreter region info: {0}", status); 572 return nullptr; 573 } 574 575 FileSpec file(info.GetName().GetCString()); 576 ModuleSpec module_spec(file, target.GetArchitecture()); 577 578 if (ModuleSP module_sp = target.GetOrCreateModule(module_spec, 579 true /* notify */)) { 580 UpdateLoadedSections(module_sp, LLDB_INVALID_ADDRESS, m_interpreter_base, 581 false); 582 m_interpreter_module = module_sp; 583 return module_sp; 584 } 585 return nullptr; 586 } 587 588 void DynamicLoaderPOSIXDYLD::LoadAllCurrentModules() { 589 DYLDRendezvous::iterator I; 590 DYLDRendezvous::iterator E; 591 ModuleList module_list; 592 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 593 594 LoadVDSO(); 595 596 if (!m_rendezvous.Resolve()) { 597 LLDB_LOGF(log, 598 "DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD " 599 "rendezvous address", 600 __FUNCTION__); 601 return; 602 } 603 604 // The rendezvous class doesn't enumerate the main module, so track that 605 // ourselves here. 606 ModuleSP executable = GetTargetExecutable(); 607 m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress(); 608 609 std::vector<FileSpec> module_names; 610 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) 611 module_names.push_back(I->file_spec); 612 m_process->PrefetchModuleSpecs( 613 module_names, m_process->GetTarget().GetArchitecture().GetTriple()); 614 615 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) { 616 ModuleSP module_sp = 617 LoadModuleAtAddress(I->file_spec, I->link_addr, I->base_addr, true); 618 if (module_sp.get()) { 619 LLDB_LOG(log, "LoadAllCurrentModules loading module: {0}", 620 I->file_spec.GetFilename()); 621 module_list.Append(module_sp); 622 } else { 623 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 624 LLDB_LOGF( 625 log, 626 "DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64, 627 __FUNCTION__, I->file_spec.GetCString(), I->base_addr); 628 } 629 } 630 631 m_process->GetTarget().ModulesDidLoad(module_list); 632 } 633 634 addr_t DynamicLoaderPOSIXDYLD::ComputeLoadOffset() { 635 addr_t virt_entry; 636 637 if (m_load_offset != LLDB_INVALID_ADDRESS) 638 return m_load_offset; 639 640 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 641 return LLDB_INVALID_ADDRESS; 642 643 ModuleSP module = m_process->GetTarget().GetExecutableModule(); 644 if (!module) 645 return LLDB_INVALID_ADDRESS; 646 647 ObjectFile *exe = module->GetObjectFile(); 648 if (!exe) 649 return LLDB_INVALID_ADDRESS; 650 651 Address file_entry = exe->GetEntryPointAddress(); 652 653 if (!file_entry.IsValid()) 654 return LLDB_INVALID_ADDRESS; 655 656 m_load_offset = virt_entry - file_entry.GetFileAddress(); 657 return m_load_offset; 658 } 659 660 void DynamicLoaderPOSIXDYLD::EvalSpecialModulesStatus() { 661 if (llvm::Optional<uint64_t> vdso_base = 662 m_auxv->GetAuxValue(AuxVector::AUXV_AT_SYSINFO_EHDR)) 663 m_vdso_base = *vdso_base; 664 665 if (llvm::Optional<uint64_t> interpreter_base = 666 m_auxv->GetAuxValue(AuxVector::AUXV_AT_BASE)) 667 m_interpreter_base = *interpreter_base; 668 } 669 670 addr_t DynamicLoaderPOSIXDYLD::GetEntryPoint() { 671 if (m_entry_point != LLDB_INVALID_ADDRESS) 672 return m_entry_point; 673 674 if (m_auxv == nullptr) 675 return LLDB_INVALID_ADDRESS; 676 677 llvm::Optional<uint64_t> entry_point = 678 m_auxv->GetAuxValue(AuxVector::AUXV_AT_ENTRY); 679 if (!entry_point) 680 return LLDB_INVALID_ADDRESS; 681 682 m_entry_point = static_cast<addr_t>(*entry_point); 683 684 const ArchSpec &arch = m_process->GetTarget().GetArchitecture(); 685 686 // On ppc64, the entry point is actually a descriptor. Dereference it. 687 if (arch.GetMachine() == llvm::Triple::ppc64) 688 m_entry_point = ReadUnsignedIntWithSizeInBytes(m_entry_point, 8); 689 690 return m_entry_point; 691 } 692 693 lldb::addr_t 694 DynamicLoaderPOSIXDYLD::GetThreadLocalData(const lldb::ModuleSP module_sp, 695 const lldb::ThreadSP thread, 696 lldb::addr_t tls_file_addr) { 697 auto it = m_loaded_modules.find(module_sp); 698 if (it == m_loaded_modules.end()) 699 return LLDB_INVALID_ADDRESS; 700 701 addr_t link_map = it->second; 702 if (link_map == LLDB_INVALID_ADDRESS) 703 return LLDB_INVALID_ADDRESS; 704 705 const DYLDRendezvous::ThreadInfo &metadata = m_rendezvous.GetThreadInfo(); 706 if (!metadata.valid) 707 return LLDB_INVALID_ADDRESS; 708 709 // Get the thread pointer. 710 addr_t tp = thread->GetThreadPointer(); 711 if (tp == LLDB_INVALID_ADDRESS) 712 return LLDB_INVALID_ADDRESS; 713 714 // Find the module's modid. 715 int modid_size = 4; // FIXME(spucci): This isn't right for big-endian 64-bit 716 int64_t modid = ReadUnsignedIntWithSizeInBytes( 717 link_map + metadata.modid_offset, modid_size); 718 if (modid == -1) 719 return LLDB_INVALID_ADDRESS; 720 721 // Lookup the DTV structure for this thread. 722 addr_t dtv_ptr = tp + metadata.dtv_offset; 723 addr_t dtv = ReadPointer(dtv_ptr); 724 if (dtv == LLDB_INVALID_ADDRESS) 725 return LLDB_INVALID_ADDRESS; 726 727 // Find the TLS block for this module. 728 addr_t dtv_slot = dtv + metadata.dtv_slot_size * modid; 729 addr_t tls_block = ReadPointer(dtv_slot + metadata.tls_offset); 730 731 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 732 LLDB_LOGF(log, 733 "DynamicLoaderPOSIXDYLD::Performed TLS lookup: " 734 "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64 735 ", modid=%" PRId64 ", tls_block=0x%" PRIx64 "\n", 736 module_sp->GetObjectName().AsCString(""), link_map, tp, 737 (int64_t)modid, tls_block); 738 739 if (tls_block == LLDB_INVALID_ADDRESS) 740 return LLDB_INVALID_ADDRESS; 741 else 742 return tls_block + tls_file_addr; 743 } 744 745 void DynamicLoaderPOSIXDYLD::ResolveExecutableModule( 746 lldb::ModuleSP &module_sp) { 747 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 748 749 if (m_process == nullptr) 750 return; 751 752 auto &target = m_process->GetTarget(); 753 const auto platform_sp = target.GetPlatform(); 754 755 ProcessInstanceInfo process_info; 756 if (!m_process->GetProcessInfo(process_info)) { 757 LLDB_LOGF(log, 758 "DynamicLoaderPOSIXDYLD::%s - failed to get process info for " 759 "pid %" PRIu64, 760 __FUNCTION__, m_process->GetID()); 761 return; 762 } 763 764 LLDB_LOGF( 765 log, "DynamicLoaderPOSIXDYLD::%s - got executable by pid %" PRIu64 ": %s", 766 __FUNCTION__, m_process->GetID(), 767 process_info.GetExecutableFile().GetPath().c_str()); 768 769 ModuleSpec module_spec(process_info.GetExecutableFile(), 770 process_info.GetArchitecture()); 771 if (module_sp && module_sp->MatchesModuleSpec(module_spec)) 772 return; 773 774 const auto executable_search_paths(Target::GetDefaultExecutableSearchPaths()); 775 auto error = platform_sp->ResolveExecutable( 776 module_spec, module_sp, 777 !executable_search_paths.IsEmpty() ? &executable_search_paths : nullptr); 778 if (error.Fail()) { 779 StreamString stream; 780 module_spec.Dump(stream); 781 782 LLDB_LOGF(log, 783 "DynamicLoaderPOSIXDYLD::%s - failed to resolve executable " 784 "with module spec \"%s\": %s", 785 __FUNCTION__, stream.GetData(), error.AsCString()); 786 return; 787 } 788 789 target.SetExecutableModule(module_sp, eLoadDependentsNo); 790 } 791 792 bool DynamicLoaderPOSIXDYLD::AlwaysRelyOnEHUnwindInfo( 793 lldb_private::SymbolContext &sym_ctx) { 794 ModuleSP module_sp; 795 if (sym_ctx.symbol) 796 module_sp = sym_ctx.symbol->GetAddressRef().GetModule(); 797 if (!module_sp && sym_ctx.function) 798 module_sp = 799 sym_ctx.function->GetAddressRange().GetBaseAddress().GetModule(); 800 if (!module_sp) 801 return false; 802 803 return module_sp->GetFileSpec().GetPath() == "[vdso]"; 804 } 805