1 //===-- DynamicLoaderPOSIX.h ------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 #include "lldb/Core/PluginManager.h" 14 #include "lldb/Core/Log.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/ModuleSpec.h" 17 #include "lldb/Target/Platform.h" 18 #include "lldb/Core/Section.h" 19 #include "lldb/Symbol/ObjectFile.h" 20 #include "lldb/Target/Process.h" 21 #include "lldb/Target/Target.h" 22 #include "lldb/Target/Thread.h" 23 #include "lldb/Target/ThreadPlanRunToAddress.h" 24 #include "lldb/Breakpoint/BreakpointLocation.h" 25 26 #include "AuxVector.h" 27 #include "DynamicLoaderPOSIXDYLD.h" 28 29 using namespace lldb; 30 using namespace lldb_private; 31 32 void 33 DynamicLoaderPOSIXDYLD::Initialize() 34 { 35 PluginManager::RegisterPlugin(GetPluginNameStatic(), 36 GetPluginDescriptionStatic(), 37 CreateInstance); 38 } 39 40 void 41 DynamicLoaderPOSIXDYLD::Terminate() 42 { 43 } 44 45 lldb_private::ConstString 46 DynamicLoaderPOSIXDYLD::GetPluginName() 47 { 48 return GetPluginNameStatic(); 49 } 50 51 lldb_private::ConstString 52 DynamicLoaderPOSIXDYLD::GetPluginNameStatic() 53 { 54 static ConstString g_name("linux-dyld"); 55 return g_name; 56 } 57 58 const char * 59 DynamicLoaderPOSIXDYLD::GetPluginDescriptionStatic() 60 { 61 return "Dynamic loader plug-in that watches for shared library " 62 "loads/unloads in POSIX processes."; 63 } 64 65 void 66 DynamicLoaderPOSIXDYLD::GetPluginCommandHelp(const char *command, Stream *strm) 67 { 68 } 69 70 uint32_t 71 DynamicLoaderPOSIXDYLD::GetPluginVersion() 72 { 73 return 1; 74 } 75 76 DynamicLoader * 77 DynamicLoaderPOSIXDYLD::CreateInstance(Process *process, bool force) 78 { 79 bool create = force; 80 if (!create) 81 { 82 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple(); 83 if (triple_ref.getOS() == llvm::Triple::Linux || 84 triple_ref.getOS() == llvm::Triple::FreeBSD) 85 create = true; 86 } 87 88 if (create) 89 return new DynamicLoaderPOSIXDYLD (process); 90 return NULL; 91 } 92 93 DynamicLoaderPOSIXDYLD::DynamicLoaderPOSIXDYLD(Process *process) 94 : DynamicLoader(process), 95 m_rendezvous(process), 96 m_load_offset(LLDB_INVALID_ADDRESS), 97 m_entry_point(LLDB_INVALID_ADDRESS), 98 m_auxv(), 99 m_dyld_bid(LLDB_INVALID_BREAK_ID) 100 { 101 } 102 103 DynamicLoaderPOSIXDYLD::~DynamicLoaderPOSIXDYLD() 104 { 105 if (m_dyld_bid != LLDB_INVALID_BREAK_ID) 106 { 107 m_process->GetTarget().RemoveBreakpointByID (m_dyld_bid); 108 m_dyld_bid = LLDB_INVALID_BREAK_ID; 109 } 110 } 111 112 void 113 DynamicLoaderPOSIXDYLD::DidAttach() 114 { 115 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 116 if (log) 117 log->Printf ("DynamicLoaderPOSIXDYLD::%s() pid %" PRIu64, __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID); 118 119 m_auxv.reset(new AuxVector(m_process)); 120 if (log) 121 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " reloaded auxv data", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID); 122 123 // ask the process if it can load any of its own modules 124 m_process->LoadModules (); 125 126 ModuleSP executable_sp = GetTargetExecutable (); 127 ResolveExecutableModule (executable_sp); 128 129 // find the main process load offset 130 addr_t load_offset = ComputeLoadOffset (); 131 if (log) 132 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " executable '%s', load_offset 0x%" PRIx64, __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID, executable_sp ? executable_sp->GetFileSpec().GetPath().c_str () : "<null executable>", load_offset); 133 134 // if we dont have a load address we cant re-base 135 bool rebase_exec = (load_offset == LLDB_INVALID_ADDRESS) ? false : true; 136 137 // if we have a valid executable 138 if (executable_sp.get()) 139 { 140 lldb_private::ObjectFile * obj = executable_sp->GetObjectFile(); 141 if (obj) 142 { 143 // don't rebase if the module already has a load address 144 Target & target = m_process->GetTarget (); 145 Address addr = obj->GetImageInfoAddress (&target); 146 if (addr.GetLoadAddress (&target) != LLDB_INVALID_ADDRESS) 147 rebase_exec = false; 148 } 149 } 150 else 151 { 152 // no executable, nothing to re-base 153 rebase_exec = false; 154 } 155 156 // if the target executable should be re-based 157 if (rebase_exec) 158 { 159 ModuleList module_list; 160 161 module_list.Append(executable_sp); 162 if (log) 163 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " added executable '%s' to module load list", 164 __FUNCTION__, 165 m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID, 166 executable_sp->GetFileSpec().GetPath().c_str ()); 167 168 UpdateLoadedSections(executable_sp, LLDB_INVALID_ADDRESS, load_offset); 169 170 // When attaching to a target, there are two possible states: 171 // (1) We already crossed the entry point and therefore the rendezvous 172 // structure is ready to be used and we can load the list of modules 173 // and place the rendezvous breakpoint. 174 // (2) We didn't cross the entry point yet, so these structures are not 175 // ready; we should behave as if we just launched the target and 176 // call ProbeEntry(). This will place a breakpoint on the entry 177 // point which itself will be hit after the rendezvous structure is 178 // set up and will perform actions described in (1). 179 if (m_rendezvous.Resolve()) 180 { 181 if (log) 182 log->Printf ("DynamicLoaderPOSIXDYLD::%s() pid %" PRIu64 " rendezvous could resolve: attach assuming dynamic loader info is available now", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID); 183 LoadAllCurrentModules(); 184 SetRendezvousBreakpoint(); 185 } 186 else 187 { 188 if (log) 189 log->Printf ("DynamicLoaderPOSIXDYLD::%s() pid %" PRIu64 " rendezvous could not yet resolve: adding breakpoint to catch future rendezvous setup", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID); 190 ProbeEntry(); 191 } 192 193 m_process->GetTarget().ModulesDidLoad(module_list); 194 if (log) 195 { 196 log->Printf ("DynamicLoaderPOSIXDYLD::%s told the target about the modules that loaded:", __FUNCTION__); 197 for (auto module_sp : module_list.Modules ()) 198 { 199 log->Printf ("-- [module] %s (pid %" PRIu64 ")", 200 module_sp ? module_sp->GetFileSpec().GetPath().c_str () : "<null>", 201 m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID); 202 } 203 } 204 } 205 } 206 207 void 208 DynamicLoaderPOSIXDYLD::DidLaunch() 209 { 210 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 211 if (log) 212 log->Printf ("DynamicLoaderPOSIXDYLD::%s()", __FUNCTION__); 213 214 ModuleSP executable; 215 addr_t load_offset; 216 217 m_auxv.reset(new AuxVector(m_process)); 218 219 executable = GetTargetExecutable(); 220 load_offset = ComputeLoadOffset(); 221 222 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS) 223 { 224 ModuleList module_list; 225 module_list.Append(executable); 226 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset); 227 228 if (log) 229 log->Printf ("DynamicLoaderPOSIXDYLD::%s about to call ProbeEntry()", __FUNCTION__); 230 ProbeEntry(); 231 232 m_process->GetTarget().ModulesDidLoad(module_list); 233 } 234 } 235 236 Error 237 DynamicLoaderPOSIXDYLD::ExecutePluginCommand(Args &command, Stream *strm) 238 { 239 return Error(); 240 } 241 242 Log * 243 DynamicLoaderPOSIXDYLD::EnablePluginLogging(Stream *strm, Args &command) 244 { 245 return NULL; 246 } 247 248 Error 249 DynamicLoaderPOSIXDYLD::CanLoadImage() 250 { 251 return Error(); 252 } 253 254 void 255 DynamicLoaderPOSIXDYLD::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, addr_t base_addr) 256 { 257 m_loaded_modules[module] = link_map_addr; 258 259 UpdateLoadedSectionsCommon(module, base_addr); 260 } 261 262 void 263 DynamicLoaderPOSIXDYLD::UnloadSections(const ModuleSP module) 264 { 265 m_loaded_modules.erase(module); 266 267 UnloadSectionsCommon(module); 268 } 269 270 void 271 DynamicLoaderPOSIXDYLD::ProbeEntry() 272 { 273 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 274 275 const addr_t entry = GetEntryPoint(); 276 if (entry == LLDB_INVALID_ADDRESS) 277 { 278 if (log) 279 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " GetEntryPoint() returned no address, not setting entry breakpoint", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID); 280 return; 281 } 282 283 if (log) 284 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " GetEntryPoint() returned address 0x%" PRIx64 ", setting entry breakpoint", __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID, entry); 285 286 if (m_process) 287 { 288 Breakpoint *const entry_break = m_process->GetTarget().CreateBreakpoint(entry, true, false).get(); 289 entry_break->SetCallback(EntryBreakpointHit, this, true); 290 entry_break->SetBreakpointKind("shared-library-event"); 291 292 // Shoudn't hit this more than once. 293 entry_break->SetOneShot (true); 294 } 295 } 296 297 // The runtime linker has run and initialized the rendezvous structure once the 298 // process has hit its entry point. When we hit the corresponding breakpoint we 299 // interrogate the rendezvous structure to get the load addresses of all 300 // dependent modules for the process. Similarly, we can discover the runtime 301 // linker function and setup a breakpoint to notify us of any dynamically loaded 302 // modules (via dlopen). 303 bool 304 DynamicLoaderPOSIXDYLD::EntryBreakpointHit(void *baton, 305 StoppointCallbackContext *context, 306 user_id_t break_id, 307 user_id_t break_loc_id) 308 { 309 assert(baton && "null baton"); 310 if (!baton) 311 return false; 312 313 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 314 DynamicLoaderPOSIXDYLD *const dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton); 315 if (log) 316 log->Printf ("DynamicLoaderPOSIXDYLD::%s called for pid %" PRIu64, __FUNCTION__, dyld_instance->m_process ? dyld_instance->m_process->GetID () : LLDB_INVALID_PROCESS_ID); 317 318 // Disable the breakpoint --- if a stop happens right after this, which we've seen on occasion, we don't 319 // want the breakpoint stepping thread-plan logic to show a breakpoint instruction at the disassembled 320 // entry point to the program. Disabling it prevents it. (One-shot is not enough - one-shot removal logic 321 // only happens after the breakpoint goes public, which wasn't happening in our scenario). 322 if (dyld_instance->m_process) 323 { 324 BreakpointSP breakpoint_sp = dyld_instance->m_process->GetTarget().GetBreakpointByID (break_id); 325 if (breakpoint_sp) 326 { 327 if (log) 328 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " disabling breakpoint id %" PRIu64, __FUNCTION__, dyld_instance->m_process->GetID (), break_id); 329 breakpoint_sp->SetEnabled (false); 330 } 331 else 332 { 333 if (log) 334 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " failed to find breakpoint for breakpoint id %" PRIu64, __FUNCTION__, dyld_instance->m_process->GetID (), break_id); 335 } 336 } 337 else 338 { 339 if (log) 340 log->Printf ("DynamicLoaderPOSIXDYLD::%s breakpoint id %" PRIu64 " no Process instance! Cannot disable breakpoint", __FUNCTION__, break_id); 341 } 342 343 dyld_instance->LoadAllCurrentModules(); 344 dyld_instance->SetRendezvousBreakpoint(); 345 return false; // Continue running. 346 } 347 348 void 349 DynamicLoaderPOSIXDYLD::SetRendezvousBreakpoint() 350 { 351 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 352 353 addr_t break_addr = m_rendezvous.GetBreakAddress(); 354 Target &target = m_process->GetTarget(); 355 356 if (m_dyld_bid == LLDB_INVALID_BREAK_ID) 357 { 358 if (log) 359 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " setting rendezvous break address at 0x%" PRIx64, __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID, break_addr); 360 Breakpoint *dyld_break = target.CreateBreakpoint (break_addr, true, false).get(); 361 dyld_break->SetCallback(RendezvousBreakpointHit, this, true); 362 dyld_break->SetBreakpointKind ("shared-library-event"); 363 m_dyld_bid = dyld_break->GetID(); 364 } 365 else 366 { 367 if (log) 368 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " reusing break id %" PRIu32 ", address at 0x%" PRIx64, __FUNCTION__, m_process ? m_process->GetID () : LLDB_INVALID_PROCESS_ID, m_dyld_bid, break_addr); 369 } 370 371 // Make sure our breakpoint is at the right address. 372 assert (target.GetBreakpointByID(m_dyld_bid)->FindLocationByAddress(break_addr)->GetBreakpoint().GetID() == m_dyld_bid); 373 } 374 375 bool 376 DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit(void *baton, 377 StoppointCallbackContext *context, 378 user_id_t break_id, 379 user_id_t break_loc_id) 380 { 381 assert (baton && "null baton"); 382 if (!baton) 383 return false; 384 385 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 386 DynamicLoaderPOSIXDYLD *const dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton); 387 if (log) 388 log->Printf ("DynamicLoaderPOSIXDYLD::%s called for pid %" PRIu64, __FUNCTION__, dyld_instance->m_process ? dyld_instance->m_process->GetID () : LLDB_INVALID_PROCESS_ID); 389 390 dyld_instance->RefreshModules(); 391 392 // Return true to stop the target, false to just let the target run. 393 const bool stop_when_images_change = dyld_instance->GetStopWhenImagesChange(); 394 if (log) 395 log->Printf ("DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " stop_when_images_change=%s", __FUNCTION__, dyld_instance->m_process ? dyld_instance->m_process->GetID () : LLDB_INVALID_PROCESS_ID, stop_when_images_change ? "true" : "false"); 396 return stop_when_images_change; 397 } 398 399 void 400 DynamicLoaderPOSIXDYLD::RefreshModules() 401 { 402 if (!m_rendezvous.Resolve()) 403 return; 404 405 DYLDRendezvous::iterator I; 406 DYLDRendezvous::iterator E; 407 408 ModuleList &loaded_modules = m_process->GetTarget().GetImages(); 409 410 if (m_rendezvous.ModulesDidLoad()) 411 { 412 ModuleList new_modules; 413 414 E = m_rendezvous.loaded_end(); 415 for (I = m_rendezvous.loaded_begin(); I != E; ++I) 416 { 417 FileSpec file(I->path.c_str(), true); 418 ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr); 419 if (module_sp.get()) 420 { 421 loaded_modules.AppendIfNeeded(module_sp); 422 new_modules.Append(module_sp); 423 } 424 } 425 m_process->GetTarget().ModulesDidLoad(new_modules); 426 } 427 428 if (m_rendezvous.ModulesDidUnload()) 429 { 430 ModuleList old_modules; 431 432 E = m_rendezvous.unloaded_end(); 433 for (I = m_rendezvous.unloaded_begin(); I != E; ++I) 434 { 435 FileSpec file(I->path.c_str(), true); 436 ModuleSpec module_spec (file); 437 ModuleSP module_sp = 438 loaded_modules.FindFirstModule (module_spec); 439 440 if (module_sp.get()) 441 { 442 old_modules.Append(module_sp); 443 UnloadSections(module_sp); 444 } 445 } 446 loaded_modules.Remove(old_modules); 447 m_process->GetTarget().ModulesDidUnload(old_modules, false); 448 } 449 } 450 451 ThreadPlanSP 452 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, bool stop) 453 { 454 ThreadPlanSP thread_plan_sp; 455 456 StackFrame *frame = thread.GetStackFrameAtIndex(0).get(); 457 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol); 458 Symbol *sym = context.symbol; 459 460 if (sym == NULL || !sym->IsTrampoline()) 461 return thread_plan_sp; 462 463 const ConstString &sym_name = sym->GetMangled().GetName(Mangled::ePreferMangled); 464 if (!sym_name) 465 return thread_plan_sp; 466 467 SymbolContextList target_symbols; 468 Target &target = thread.GetProcess()->GetTarget(); 469 const ModuleList &images = target.GetImages(); 470 471 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols); 472 size_t num_targets = target_symbols.GetSize(); 473 if (!num_targets) 474 return thread_plan_sp; 475 476 typedef std::vector<lldb::addr_t> AddressVector; 477 AddressVector addrs; 478 for (size_t i = 0; i < num_targets; ++i) 479 { 480 SymbolContext context; 481 AddressRange range; 482 if (target_symbols.GetContextAtIndex(i, context)) 483 { 484 context.GetAddressRange(eSymbolContextEverything, 0, false, range); 485 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target); 486 if (addr != LLDB_INVALID_ADDRESS) 487 addrs.push_back(addr); 488 } 489 } 490 491 if (addrs.size() > 0) 492 { 493 AddressVector::iterator start = addrs.begin(); 494 AddressVector::iterator end = addrs.end(); 495 496 std::sort(start, end); 497 addrs.erase(std::unique(start, end), end); 498 thread_plan_sp.reset(new ThreadPlanRunToAddress(thread, addrs, stop)); 499 } 500 501 return thread_plan_sp; 502 } 503 504 void 505 DynamicLoaderPOSIXDYLD::LoadAllCurrentModules() 506 { 507 DYLDRendezvous::iterator I; 508 DYLDRendezvous::iterator E; 509 ModuleList module_list; 510 511 if (!m_rendezvous.Resolve()) 512 { 513 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 514 if (log) 515 log->Printf("DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD rendezvous address", 516 __FUNCTION__); 517 return; 518 } 519 520 // The rendezvous class doesn't enumerate the main module, so track 521 // that ourselves here. 522 ModuleSP executable = GetTargetExecutable(); 523 m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress(); 524 525 526 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) 527 { 528 const char *module_path = I->path.c_str(); 529 FileSpec file(module_path, false); 530 ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr); 531 if (module_sp.get()) 532 { 533 module_list.Append(module_sp); 534 } 535 else 536 { 537 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 538 if (log) 539 log->Printf("DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64, 540 __FUNCTION__, module_path, I->base_addr); 541 } 542 } 543 544 m_process->GetTarget().ModulesDidLoad(module_list); 545 } 546 547 addr_t 548 DynamicLoaderPOSIXDYLD::ComputeLoadOffset() 549 { 550 addr_t virt_entry; 551 552 if (m_load_offset != LLDB_INVALID_ADDRESS) 553 return m_load_offset; 554 555 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 556 return LLDB_INVALID_ADDRESS; 557 558 ModuleSP module = m_process->GetTarget().GetExecutableModule(); 559 if (!module) 560 return LLDB_INVALID_ADDRESS; 561 562 ObjectFile *exe = module->GetObjectFile(); 563 if (!exe) 564 return LLDB_INVALID_ADDRESS; 565 566 Address file_entry = exe->GetEntryPointAddress(); 567 568 if (!file_entry.IsValid()) 569 return LLDB_INVALID_ADDRESS; 570 571 m_load_offset = virt_entry - file_entry.GetFileAddress(); 572 return m_load_offset; 573 } 574 575 addr_t 576 DynamicLoaderPOSIXDYLD::GetEntryPoint() 577 { 578 if (m_entry_point != LLDB_INVALID_ADDRESS) 579 return m_entry_point; 580 581 if (m_auxv.get() == NULL) 582 return LLDB_INVALID_ADDRESS; 583 584 AuxVector::iterator I = m_auxv->FindEntry(AuxVector::AT_ENTRY); 585 586 if (I == m_auxv->end()) 587 return LLDB_INVALID_ADDRESS; 588 589 m_entry_point = static_cast<addr_t>(I->value); 590 591 const ArchSpec &arch = m_process->GetTarget().GetArchitecture(); 592 593 // On ppc64, the entry point is actually a descriptor. Dereference it. 594 if (arch.GetMachine() == llvm::Triple::ppc64) 595 m_entry_point = ReadUnsignedIntWithSizeInBytes(m_entry_point, 8); 596 597 return m_entry_point; 598 } 599 600 lldb::addr_t 601 DynamicLoaderPOSIXDYLD::GetThreadLocalData (const lldb::ModuleSP module, const lldb::ThreadSP thread) 602 { 603 auto it = m_loaded_modules.find (module); 604 if (it == m_loaded_modules.end()) 605 return LLDB_INVALID_ADDRESS; 606 607 addr_t link_map = it->second; 608 if (link_map == LLDB_INVALID_ADDRESS) 609 return LLDB_INVALID_ADDRESS; 610 611 const DYLDRendezvous::ThreadInfo &metadata = m_rendezvous.GetThreadInfo(); 612 if (!metadata.valid) 613 return LLDB_INVALID_ADDRESS; 614 615 // Get the thread pointer. 616 addr_t tp = thread->GetThreadPointer (); 617 if (tp == LLDB_INVALID_ADDRESS) 618 return LLDB_INVALID_ADDRESS; 619 620 // Find the module's modid. 621 int modid_size = 4; // FIXME(spucci): This isn't right for big-endian 64-bit 622 int64_t modid = ReadUnsignedIntWithSizeInBytes (link_map + metadata.modid_offset, modid_size); 623 if (modid == -1) 624 return LLDB_INVALID_ADDRESS; 625 626 // Lookup the DTV structure for this thread. 627 addr_t dtv_ptr = tp + metadata.dtv_offset; 628 addr_t dtv = ReadPointer (dtv_ptr); 629 if (dtv == LLDB_INVALID_ADDRESS) 630 return LLDB_INVALID_ADDRESS; 631 632 // Find the TLS block for this module. 633 addr_t dtv_slot = dtv + metadata.dtv_slot_size*modid; 634 addr_t tls_block = ReadPointer (dtv_slot + metadata.tls_offset); 635 636 Module *mod = module.get(); 637 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 638 if (log) 639 log->Printf("DynamicLoaderPOSIXDYLD::Performed TLS lookup: " 640 "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64 ", modid=%" PRId64 ", tls_block=0x%" PRIx64 "\n", 641 mod->GetObjectName().AsCString(""), link_map, tp, (int64_t)modid, tls_block); 642 643 return tls_block; 644 } 645 646 void 647 DynamicLoaderPOSIXDYLD::ResolveExecutableModule (lldb::ModuleSP &module_sp) 648 { 649 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 650 651 if (m_process == nullptr) 652 return; 653 654 auto &target = m_process->GetTarget (); 655 const auto platform_sp = target.GetPlatform (); 656 657 ProcessInstanceInfo process_info; 658 if (!platform_sp->GetProcessInfo (m_process->GetID (), process_info)) 659 { 660 if (log) 661 log->Printf ("DynamicLoaderPOSIXDYLD::%s - failed to get process info for pid %" PRIu64, 662 __FUNCTION__, m_process->GetID ()); 663 return; 664 } 665 666 if (log) 667 log->Printf ("DynamicLoaderPOSIXDYLD::%s - got executable by pid %" PRIu64 ": %s", 668 __FUNCTION__, m_process->GetID (), process_info.GetExecutableFile ().GetPath ().c_str ()); 669 670 ModuleSpec module_spec (process_info.GetExecutableFile (), process_info.GetArchitecture ()); 671 if (module_sp && module_sp->MatchesModuleSpec (module_spec)) 672 return; 673 674 auto error = platform_sp->ResolveExecutable (module_spec, module_sp, nullptr); 675 if (error.Fail ()) 676 { 677 StreamString stream; 678 module_spec.Dump (stream); 679 680 if (log) 681 log->Printf ("DynamicLoaderPOSIXDYLD::%s - failed to resolve executable with module spec \"%s\": %s", 682 __FUNCTION__, stream.GetString ().c_str (), error.AsCString ()); 683 return; 684 } 685 686 target.SetExecutableModule (module_sp, false); 687 } 688