1 //===-- DynamicLoaderPOSIXDYLD.cpp ------------------------------*- C++ -*-===// 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 "AuxVector.h" 13 14 #include "lldb/Breakpoint/BreakpointLocation.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/ModuleSpec.h" 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/Section.h" 19 #include "lldb/Symbol/Function.h" 20 #include "lldb/Symbol/ObjectFile.h" 21 #include "lldb/Target/MemoryRegionInfo.h" 22 #include "lldb/Target/Platform.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Target/Target.h" 25 #include "lldb/Target/Thread.h" 26 #include "lldb/Target/ThreadPlanRunToAddress.h" 27 #include "lldb/Utility/Log.h" 28 29 30 using namespace lldb; 31 using namespace lldb_private; 32 33 void DynamicLoaderPOSIXDYLD::Initialize() { 34 PluginManager::RegisterPlugin(GetPluginNameStatic(), 35 GetPluginDescriptionStatic(), CreateInstance); 36 } 37 38 void DynamicLoaderPOSIXDYLD::Terminate() {} 39 40 lldb_private::ConstString DynamicLoaderPOSIXDYLD::GetPluginName() { 41 return GetPluginNameStatic(); 42 } 43 44 lldb_private::ConstString DynamicLoaderPOSIXDYLD::GetPluginNameStatic() { 45 static ConstString g_name("linux-dyld"); 46 return g_name; 47 } 48 49 const char *DynamicLoaderPOSIXDYLD::GetPluginDescriptionStatic() { 50 return "Dynamic loader plug-in that watches for shared library " 51 "loads/unloads in POSIX processes."; 52 } 53 54 uint32_t DynamicLoaderPOSIXDYLD::GetPluginVersion() { return 1; } 55 56 DynamicLoader *DynamicLoaderPOSIXDYLD::CreateInstance(Process *process, 57 bool force) { 58 bool create = force; 59 if (!create) { 60 const llvm::Triple &triple_ref = 61 process->GetTarget().GetArchitecture().GetTriple(); 62 if (triple_ref.getOS() == llvm::Triple::FreeBSD || 63 triple_ref.getOS() == llvm::Triple::Linux || 64 triple_ref.getOS() == llvm::Triple::NetBSD) 65 create = true; 66 } 67 68 if (create) 69 return new DynamicLoaderPOSIXDYLD(process); 70 return NULL; 71 } 72 73 DynamicLoaderPOSIXDYLD::DynamicLoaderPOSIXDYLD(Process *process) 74 : DynamicLoader(process), m_rendezvous(process), 75 m_load_offset(LLDB_INVALID_ADDRESS), m_entry_point(LLDB_INVALID_ADDRESS), 76 m_auxv(), m_dyld_bid(LLDB_INVALID_BREAK_ID), 77 m_vdso_base(LLDB_INVALID_ADDRESS), 78 m_interpreter_base(LLDB_INVALID_ADDRESS) {} 79 80 DynamicLoaderPOSIXDYLD::~DynamicLoaderPOSIXDYLD() { 81 if (m_dyld_bid != LLDB_INVALID_BREAK_ID) { 82 m_process->GetTarget().RemoveBreakpointByID(m_dyld_bid); 83 m_dyld_bid = LLDB_INVALID_BREAK_ID; 84 } 85 } 86 87 void DynamicLoaderPOSIXDYLD::DidAttach() { 88 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 89 if (log) 90 log->Printf("DynamicLoaderPOSIXDYLD::%s() pid %" PRIu64, __FUNCTION__, 91 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID); 92 93 m_auxv.reset(new AuxVector(m_process)); 94 if (log) 95 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " reloaded auxv data", 96 __FUNCTION__, 97 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID); 98 99 // ask the process if it can load any of its own modules 100 m_process->LoadModules(); 101 102 ModuleSP executable_sp = GetTargetExecutable(); 103 ResolveExecutableModule(executable_sp); 104 105 // find the main process load offset 106 addr_t load_offset = ComputeLoadOffset(); 107 if (log) 108 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 109 " executable '%s', load_offset 0x%" PRIx64, 110 __FUNCTION__, 111 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID, 112 executable_sp ? executable_sp->GetFileSpec().GetPath().c_str() 113 : "<null executable>", 114 load_offset); 115 116 EvalSpecialModulesStatus(); 117 118 // if we dont have a load address we cant re-base 119 bool rebase_exec = load_offset != LLDB_INVALID_ADDRESS; 120 121 // if we have a valid executable 122 if (executable_sp.get()) { 123 lldb_private::ObjectFile *obj = executable_sp->GetObjectFile(); 124 if (obj) { 125 // don't rebase if the module already has a load address 126 Target &target = m_process->GetTarget(); 127 Address addr = obj->GetImageInfoAddress(&target); 128 if (addr.GetLoadAddress(&target) != LLDB_INVALID_ADDRESS) 129 rebase_exec = false; 130 } 131 } else { 132 // no executable, nothing to re-base 133 rebase_exec = false; 134 } 135 136 // if the target executable should be re-based 137 if (rebase_exec) { 138 ModuleList module_list; 139 140 module_list.Append(executable_sp); 141 if (log) 142 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 143 " added executable '%s' to module load list", 144 __FUNCTION__, 145 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID, 146 executable_sp->GetFileSpec().GetPath().c_str()); 147 148 UpdateLoadedSections(executable_sp, LLDB_INVALID_ADDRESS, load_offset, 149 true); 150 151 LoadAllCurrentModules(); 152 if (!SetRendezvousBreakpoint()) { 153 // If we cannot establish rendezvous breakpoint right now we'll try again 154 // at entry point. 155 ProbeEntry(); 156 } 157 158 m_process->GetTarget().ModulesDidLoad(module_list); 159 if (log) { 160 log->Printf("DynamicLoaderPOSIXDYLD::%s told the target about the " 161 "modules that loaded:", 162 __FUNCTION__); 163 for (auto module_sp : module_list.Modules()) { 164 log->Printf("-- [module] %s (pid %" PRIu64 ")", 165 module_sp ? module_sp->GetFileSpec().GetPath().c_str() 166 : "<null>", 167 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID); 168 } 169 } 170 } 171 } 172 173 void DynamicLoaderPOSIXDYLD::DidLaunch() { 174 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 175 if (log) 176 log->Printf("DynamicLoaderPOSIXDYLD::%s()", __FUNCTION__); 177 178 ModuleSP executable; 179 addr_t load_offset; 180 181 m_auxv.reset(new AuxVector(m_process)); 182 183 executable = GetTargetExecutable(); 184 load_offset = ComputeLoadOffset(); 185 EvalSpecialModulesStatus(); 186 187 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS) { 188 ModuleList module_list; 189 module_list.Append(executable); 190 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset, true); 191 192 if (log) 193 log->Printf("DynamicLoaderPOSIXDYLD::%s about to call ProbeEntry()", 194 __FUNCTION__); 195 196 if (!SetRendezvousBreakpoint()) { 197 // If we cannot establish rendezvous breakpoint right now we'll try again 198 // at entry point. 199 ProbeEntry(); 200 } 201 202 LoadVDSO(); 203 m_process->GetTarget().ModulesDidLoad(module_list); 204 } 205 } 206 207 Status DynamicLoaderPOSIXDYLD::CanLoadImage() { return Status(); } 208 209 void DynamicLoaderPOSIXDYLD::UpdateLoadedSections(ModuleSP module, 210 addr_t link_map_addr, 211 addr_t base_addr, 212 bool base_addr_is_offset) { 213 m_loaded_modules[module] = link_map_addr; 214 UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset); 215 } 216 217 void DynamicLoaderPOSIXDYLD::UnloadSections(const ModuleSP module) { 218 m_loaded_modules.erase(module); 219 220 UnloadSectionsCommon(module); 221 } 222 223 void DynamicLoaderPOSIXDYLD::ProbeEntry() { 224 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 225 226 const addr_t entry = GetEntryPoint(); 227 if (entry == LLDB_INVALID_ADDRESS) { 228 if (log) 229 log->Printf( 230 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 231 " GetEntryPoint() returned no address, not setting entry breakpoint", 232 __FUNCTION__, 233 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID); 234 return; 235 } 236 237 if (log) 238 log->Printf("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 239 " GetEntryPoint() returned address 0x%" PRIx64 240 ", setting entry breakpoint", 241 __FUNCTION__, 242 m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID, 243 entry); 244 245 if (m_process) { 246 Breakpoint *const entry_break = 247 m_process->GetTarget().CreateBreakpoint(entry, true, false).get(); 248 entry_break->SetCallback(EntryBreakpointHit, this, true); 249 entry_break->SetBreakpointKind("shared-library-event"); 250 251 // Shoudn't hit this more than once. 252 entry_break->SetOneShot(true); 253 } 254 } 255 256 // The runtime linker has run and initialized the rendezvous structure once the 257 // process has hit its entry point. When we hit the corresponding breakpoint 258 // we interrogate the rendezvous structure to get the load addresses of all 259 // dependent modules for the process. Similarly, we can discover the runtime 260 // linker function and setup a breakpoint to notify us of any dynamically 261 // loaded modules (via dlopen). 262 bool DynamicLoaderPOSIXDYLD::EntryBreakpointHit( 263 void *baton, StoppointCallbackContext *context, user_id_t break_id, 264 user_id_t break_loc_id) { 265 assert(baton && "null baton"); 266 if (!baton) 267 return false; 268 269 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 270 DynamicLoaderPOSIXDYLD *const dyld_instance = 271 static_cast<DynamicLoaderPOSIXDYLD *>(baton); 272 if (log) 273 log->Printf("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 if (log) 289 log->Printf("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 if (log) 295 log->Printf("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 if (log) 301 log->Printf("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 if (log) 395 log->Printf("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 if (log) 405 log->Printf("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()) { 424 ModuleList new_modules; 425 426 E = m_rendezvous.loaded_end(); 427 for (I = m_rendezvous.loaded_begin(); I != E; ++I) { 428 ModuleSP module_sp = 429 LoadModuleAtAddress(I->file_spec, I->link_addr, I->base_addr, true); 430 if (module_sp.get()) { 431 loaded_modules.AppendIfNeeded(module_sp); 432 new_modules.Append(module_sp); 433 } 434 } 435 m_process->GetTarget().ModulesDidLoad(new_modules); 436 } 437 438 if (m_rendezvous.ModulesDidUnload()) { 439 ModuleList old_modules; 440 441 E = m_rendezvous.unloaded_end(); 442 for (I = m_rendezvous.unloaded_begin(); I != E; ++I) { 443 ModuleSpec module_spec{I->file_spec}; 444 ModuleSP module_sp = loaded_modules.FindFirstModule(module_spec); 445 446 if (module_sp.get()) { 447 old_modules.Append(module_sp); 448 UnloadSections(module_sp); 449 } 450 } 451 loaded_modules.Remove(old_modules); 452 m_process->GetTarget().ModulesDidUnload(old_modules, false); 453 } 454 } 455 456 ThreadPlanSP 457 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, 458 bool stop) { 459 ThreadPlanSP thread_plan_sp; 460 461 StackFrame *frame = thread.GetStackFrameAtIndex(0).get(); 462 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol); 463 Symbol *sym = context.symbol; 464 465 if (sym == NULL || !sym->IsTrampoline()) 466 return thread_plan_sp; 467 468 ConstString sym_name = sym->GetName(); 469 if (!sym_name) 470 return thread_plan_sp; 471 472 SymbolContextList target_symbols; 473 Target &target = thread.GetProcess()->GetTarget(); 474 const ModuleList &images = target.GetImages(); 475 476 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols); 477 size_t num_targets = target_symbols.GetSize(); 478 if (!num_targets) 479 return thread_plan_sp; 480 481 typedef std::vector<lldb::addr_t> AddressVector; 482 AddressVector addrs; 483 for (size_t i = 0; i < num_targets; ++i) { 484 SymbolContext context; 485 AddressRange range; 486 if (target_symbols.GetContextAtIndex(i, context)) { 487 context.GetAddressRange(eSymbolContextEverything, 0, false, range); 488 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target); 489 if (addr != LLDB_INVALID_ADDRESS) 490 addrs.push_back(addr); 491 } 492 } 493 494 if (addrs.size() > 0) { 495 AddressVector::iterator start = addrs.begin(); 496 AddressVector::iterator end = addrs.end(); 497 498 llvm::sort(start, end); 499 addrs.erase(std::unique(start, end), end); 500 thread_plan_sp.reset(new 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.GetSharedModule(module_spec)) { 545 UpdateLoadedSections(module_sp, LLDB_INVALID_ADDRESS, m_interpreter_base, 546 false); 547 return module_sp; 548 } 549 return nullptr; 550 } 551 552 void DynamicLoaderPOSIXDYLD::LoadAllCurrentModules() { 553 DYLDRendezvous::iterator I; 554 DYLDRendezvous::iterator E; 555 ModuleList module_list; 556 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 557 558 LoadVDSO(); 559 560 if (!m_rendezvous.Resolve()) { 561 if (log) 562 log->Printf("DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD " 563 "rendezvous address", 564 __FUNCTION__); 565 return; 566 } 567 568 // The rendezvous class doesn't enumerate the main module, so track that 569 // ourselves here. 570 ModuleSP executable = GetTargetExecutable(); 571 m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress(); 572 573 std::vector<FileSpec> module_names; 574 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) 575 module_names.push_back(I->file_spec); 576 m_process->PrefetchModuleSpecs( 577 module_names, m_process->GetTarget().GetArchitecture().GetTriple()); 578 579 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) { 580 ModuleSP module_sp = 581 LoadModuleAtAddress(I->file_spec, I->link_addr, I->base_addr, true); 582 if (module_sp.get()) { 583 LLDB_LOG(log, "LoadAllCurrentModules loading module: {0}", 584 I->file_spec.GetFilename()); 585 module_list.Append(module_sp); 586 } else { 587 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 588 if (log) 589 log->Printf( 590 "DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64, 591 __FUNCTION__, I->file_spec.GetCString(), I->base_addr); 592 } 593 } 594 595 m_process->GetTarget().ModulesDidLoad(module_list); 596 } 597 598 addr_t DynamicLoaderPOSIXDYLD::ComputeLoadOffset() { 599 addr_t virt_entry; 600 601 if (m_load_offset != LLDB_INVALID_ADDRESS) 602 return m_load_offset; 603 604 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 605 return LLDB_INVALID_ADDRESS; 606 607 ModuleSP module = m_process->GetTarget().GetExecutableModule(); 608 if (!module) 609 return LLDB_INVALID_ADDRESS; 610 611 ObjectFile *exe = module->GetObjectFile(); 612 if (!exe) 613 return LLDB_INVALID_ADDRESS; 614 615 Address file_entry = exe->GetEntryPointAddress(); 616 617 if (!file_entry.IsValid()) 618 return LLDB_INVALID_ADDRESS; 619 620 m_load_offset = virt_entry - file_entry.GetFileAddress(); 621 return m_load_offset; 622 } 623 624 void DynamicLoaderPOSIXDYLD::EvalSpecialModulesStatus() { 625 auto I = m_auxv->FindEntry(AuxVector::AUXV_AT_SYSINFO_EHDR); 626 if (I != m_auxv->end() && I->value != 0) 627 m_vdso_base = I->value; 628 629 I = m_auxv->FindEntry(AuxVector::AUXV_AT_BASE); 630 if (I != m_auxv->end() && I->value != 0) 631 m_interpreter_base = I->value; 632 } 633 634 addr_t DynamicLoaderPOSIXDYLD::GetEntryPoint() { 635 if (m_entry_point != LLDB_INVALID_ADDRESS) 636 return m_entry_point; 637 638 if (m_auxv.get() == NULL) 639 return LLDB_INVALID_ADDRESS; 640 641 AuxVector::iterator I = m_auxv->FindEntry(AuxVector::AUXV_AT_ENTRY); 642 643 if (I == m_auxv->end()) 644 return LLDB_INVALID_ADDRESS; 645 646 m_entry_point = static_cast<addr_t>(I->value); 647 648 const ArchSpec &arch = m_process->GetTarget().GetArchitecture(); 649 650 // On ppc64, the entry point is actually a descriptor. Dereference it. 651 if (arch.GetMachine() == llvm::Triple::ppc64) 652 m_entry_point = ReadUnsignedIntWithSizeInBytes(m_entry_point, 8); 653 654 return m_entry_point; 655 } 656 657 lldb::addr_t 658 DynamicLoaderPOSIXDYLD::GetThreadLocalData(const lldb::ModuleSP module_sp, 659 const lldb::ThreadSP thread, 660 lldb::addr_t tls_file_addr) { 661 auto it = m_loaded_modules.find(module_sp); 662 if (it == m_loaded_modules.end()) 663 return LLDB_INVALID_ADDRESS; 664 665 addr_t link_map = it->second; 666 if (link_map == LLDB_INVALID_ADDRESS) 667 return LLDB_INVALID_ADDRESS; 668 669 const DYLDRendezvous::ThreadInfo &metadata = m_rendezvous.GetThreadInfo(); 670 if (!metadata.valid) 671 return LLDB_INVALID_ADDRESS; 672 673 // Get the thread pointer. 674 addr_t tp = thread->GetThreadPointer(); 675 if (tp == LLDB_INVALID_ADDRESS) 676 return LLDB_INVALID_ADDRESS; 677 678 // Find the module's modid. 679 int modid_size = 4; // FIXME(spucci): This isn't right for big-endian 64-bit 680 int64_t modid = ReadUnsignedIntWithSizeInBytes( 681 link_map + metadata.modid_offset, modid_size); 682 if (modid == -1) 683 return LLDB_INVALID_ADDRESS; 684 685 // Lookup the DTV structure for this thread. 686 addr_t dtv_ptr = tp + metadata.dtv_offset; 687 addr_t dtv = ReadPointer(dtv_ptr); 688 if (dtv == LLDB_INVALID_ADDRESS) 689 return LLDB_INVALID_ADDRESS; 690 691 // Find the TLS block for this module. 692 addr_t dtv_slot = dtv + metadata.dtv_slot_size * modid; 693 addr_t tls_block = ReadPointer(dtv_slot + metadata.tls_offset); 694 695 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 696 if (log) 697 log->Printf("DynamicLoaderPOSIXDYLD::Performed TLS lookup: " 698 "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64 699 ", modid=%" PRId64 ", tls_block=0x%" PRIx64 "\n", 700 module_sp->GetObjectName().AsCString(""), link_map, tp, 701 (int64_t)modid, tls_block); 702 703 if (tls_block == LLDB_INVALID_ADDRESS) 704 return LLDB_INVALID_ADDRESS; 705 else 706 return tls_block + tls_file_addr; 707 } 708 709 void DynamicLoaderPOSIXDYLD::ResolveExecutableModule( 710 lldb::ModuleSP &module_sp) { 711 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 712 713 if (m_process == nullptr) 714 return; 715 716 auto &target = m_process->GetTarget(); 717 const auto platform_sp = target.GetPlatform(); 718 719 ProcessInstanceInfo process_info; 720 if (!m_process->GetProcessInfo(process_info)) { 721 if (log) 722 log->Printf("DynamicLoaderPOSIXDYLD::%s - failed to get process info for " 723 "pid %" PRIu64, 724 __FUNCTION__, m_process->GetID()); 725 return; 726 } 727 728 if (log) 729 log->Printf("DynamicLoaderPOSIXDYLD::%s - got executable by pid %" PRIu64 730 ": %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 if (log) 748 log->Printf("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