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