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/Core/Section.h" 18 #include "lldb/Symbol/ObjectFile.h" 19 #include "lldb/Target/Process.h" 20 #include "lldb/Target/Target.h" 21 #include "lldb/Target/Thread.h" 22 #include "lldb/Target/ThreadPlanRunToAddress.h" 23 #include "lldb/Breakpoint/BreakpointLocation.h" 24 25 #include "AuxVector.h" 26 #include "DynamicLoaderPOSIXDYLD.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 void 32 DynamicLoaderPOSIXDYLD::Initialize() 33 { 34 PluginManager::RegisterPlugin(GetPluginNameStatic(), 35 GetPluginDescriptionStatic(), 36 CreateInstance); 37 } 38 39 void 40 DynamicLoaderPOSIXDYLD::Terminate() 41 { 42 } 43 44 lldb_private::ConstString 45 DynamicLoaderPOSIXDYLD::GetPluginName() 46 { 47 return GetPluginNameStatic(); 48 } 49 50 lldb_private::ConstString 51 DynamicLoaderPOSIXDYLD::GetPluginNameStatic() 52 { 53 static ConstString g_name("linux-dyld"); 54 return g_name; 55 } 56 57 const char * 58 DynamicLoaderPOSIXDYLD::GetPluginDescriptionStatic() 59 { 60 return "Dynamic loader plug-in that watches for shared library " 61 "loads/unloads in POSIX processes."; 62 } 63 64 void 65 DynamicLoaderPOSIXDYLD::GetPluginCommandHelp(const char *command, Stream *strm) 66 { 67 } 68 69 uint32_t 70 DynamicLoaderPOSIXDYLD::GetPluginVersion() 71 { 72 return 1; 73 } 74 75 DynamicLoader * 76 DynamicLoaderPOSIXDYLD::CreateInstance(Process *process, bool force) 77 { 78 bool create = force; 79 if (!create) 80 { 81 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple(); 82 if (triple_ref.getOS() == llvm::Triple::Linux || 83 triple_ref.getOS() == llvm::Triple::FreeBSD) 84 create = true; 85 } 86 87 if (create) 88 return new DynamicLoaderPOSIXDYLD (process); 89 return NULL; 90 } 91 92 DynamicLoaderPOSIXDYLD::DynamicLoaderPOSIXDYLD(Process *process) 93 : DynamicLoader(process), 94 m_rendezvous(process), 95 m_load_offset(LLDB_INVALID_ADDRESS), 96 m_entry_point(LLDB_INVALID_ADDRESS), 97 m_auxv(), 98 m_dyld_bid(LLDB_INVALID_BREAK_ID) 99 { 100 } 101 102 DynamicLoaderPOSIXDYLD::~DynamicLoaderPOSIXDYLD() 103 { 104 if (m_dyld_bid != LLDB_INVALID_BREAK_ID) 105 { 106 m_process->GetTarget().RemoveBreakpointByID (m_dyld_bid); 107 m_dyld_bid = LLDB_INVALID_BREAK_ID; 108 } 109 } 110 111 void 112 DynamicLoaderPOSIXDYLD::DidAttach() 113 { 114 ModuleSP executable; 115 addr_t load_offset; 116 117 m_auxv.reset(new AuxVector(m_process)); 118 119 executable = GetTargetExecutable(); 120 load_offset = ComputeLoadOffset(); 121 122 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS) 123 { 124 ModuleList module_list; 125 module_list.Append(executable); 126 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset); 127 LoadAllCurrentModules(); 128 m_process->GetTarget().ModulesDidLoad(module_list); 129 } 130 } 131 132 void 133 DynamicLoaderPOSIXDYLD::DidLaunch() 134 { 135 ModuleSP executable; 136 addr_t load_offset; 137 138 m_auxv.reset(new AuxVector(m_process)); 139 140 executable = GetTargetExecutable(); 141 load_offset = ComputeLoadOffset(); 142 143 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS) 144 { 145 ModuleList module_list; 146 module_list.Append(executable); 147 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset); 148 ProbeEntry(); 149 m_process->GetTarget().ModulesDidLoad(module_list); 150 } 151 } 152 153 ModuleSP 154 DynamicLoaderPOSIXDYLD::GetTargetExecutable() 155 { 156 Target &target = m_process->GetTarget(); 157 ModuleSP executable = target.GetExecutableModule(); 158 159 if (executable.get()) 160 { 161 if (executable->GetFileSpec().Exists()) 162 { 163 ModuleSpec module_spec (executable->GetFileSpec(), executable->GetArchitecture()); 164 ModuleSP module_sp (new Module (module_spec)); 165 166 // Check if the executable has changed and set it to the target executable if they differ. 167 if (module_sp.get() && module_sp->GetUUID().IsValid() && executable->GetUUID().IsValid()) 168 { 169 if (module_sp->GetUUID() != executable->GetUUID()) 170 executable.reset(); 171 } 172 else if (executable->FileHasChanged()) 173 { 174 executable.reset(); 175 } 176 177 if (!executable.get()) 178 { 179 executable = target.GetSharedModule(module_spec); 180 if (executable.get() != target.GetExecutableModulePointer()) 181 { 182 // Don't load dependent images since we are in dyld where we will know 183 // and find out about all images that are loaded 184 const bool get_dependent_images = false; 185 target.SetExecutableModule(executable, get_dependent_images); 186 } 187 } 188 } 189 } 190 return executable; 191 } 192 193 Error 194 DynamicLoaderPOSIXDYLD::ExecutePluginCommand(Args &command, Stream *strm) 195 { 196 return Error(); 197 } 198 199 Log * 200 DynamicLoaderPOSIXDYLD::EnablePluginLogging(Stream *strm, Args &command) 201 { 202 return NULL; 203 } 204 205 Error 206 DynamicLoaderPOSIXDYLD::CanLoadImage() 207 { 208 return Error(); 209 } 210 211 void 212 DynamicLoaderPOSIXDYLD::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, addr_t base_addr) 213 { 214 SectionLoadList &load_list = m_process->GetTarget().GetSectionLoadList(); 215 const SectionList *sections = GetSectionListFromModule(module); 216 217 assert(sections && "SectionList missing from loaded module."); 218 219 m_loaded_modules[module] = link_map_addr; 220 221 const size_t num_sections = sections->GetSize(); 222 223 for (unsigned i = 0; i < num_sections; ++i) 224 { 225 SectionSP section_sp (sections->GetSectionAtIndex(i)); 226 lldb::addr_t new_load_addr = section_sp->GetFileAddress() + base_addr; 227 lldb::addr_t old_load_addr = load_list.GetSectionLoadAddress(section_sp); 228 229 // If the file address of the section is zero then this is not an 230 // allocatable/loadable section (property of ELF sh_addr). Skip it. 231 if (new_load_addr == base_addr) 232 continue; 233 234 if (old_load_addr == LLDB_INVALID_ADDRESS || 235 old_load_addr != new_load_addr) 236 load_list.SetSectionLoadAddress(section_sp, new_load_addr); 237 } 238 } 239 240 void 241 DynamicLoaderPOSIXDYLD::UnloadSections(const ModuleSP module) 242 { 243 SectionLoadList &load_list = m_process->GetTarget().GetSectionLoadList(); 244 const SectionList *sections = GetSectionListFromModule(module); 245 246 assert(sections && "SectionList missing from unloaded module."); 247 248 m_loaded_modules.erase(module); 249 250 const size_t num_sections = sections->GetSize(); 251 for (size_t i = 0; i < num_sections; ++i) 252 { 253 SectionSP section_sp (sections->GetSectionAtIndex(i)); 254 load_list.SetSectionUnloaded(section_sp); 255 } 256 } 257 258 void 259 DynamicLoaderPOSIXDYLD::ProbeEntry() 260 { 261 Breakpoint *entry_break; 262 addr_t entry; 263 264 if ((entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 265 return; 266 267 entry_break = m_process->GetTarget().CreateBreakpoint(entry, true, false).get(); 268 entry_break->SetCallback(EntryBreakpointHit, this, true); 269 entry_break->SetBreakpointKind("shared-library-event"); 270 } 271 272 // The runtime linker has run and initialized the rendezvous structure once the 273 // process has hit its entry point. When we hit the corresponding breakpoint we 274 // interrogate the rendezvous structure to get the load addresses of all 275 // dependent modules for the process. Similarly, we can discover the runtime 276 // linker function and setup a breakpoint to notify us of any dynamically loaded 277 // modules (via dlopen). 278 bool 279 DynamicLoaderPOSIXDYLD::EntryBreakpointHit(void *baton, 280 StoppointCallbackContext *context, 281 user_id_t break_id, 282 user_id_t break_loc_id) 283 { 284 DynamicLoaderPOSIXDYLD* dyld_instance; 285 286 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton); 287 dyld_instance->LoadAllCurrentModules(); 288 dyld_instance->SetRendezvousBreakpoint(); 289 return false; // Continue running. 290 } 291 292 void 293 DynamicLoaderPOSIXDYLD::SetRendezvousBreakpoint() 294 { 295 addr_t break_addr = m_rendezvous.GetBreakAddress(); 296 Target &target = m_process->GetTarget(); 297 298 if (m_dyld_bid == LLDB_INVALID_BREAK_ID) 299 { 300 Breakpoint *dyld_break = target.CreateBreakpoint (break_addr, true, false).get(); 301 dyld_break->SetCallback(RendezvousBreakpointHit, this, true); 302 dyld_break->SetBreakpointKind ("shared-library-event"); 303 m_dyld_bid = dyld_break->GetID(); 304 } 305 306 // Make sure our breakpoint is at the right address. 307 assert (target.GetBreakpointByID(m_dyld_bid)->FindLocationByAddress(break_addr)->GetBreakpoint().GetID() == m_dyld_bid); 308 } 309 310 bool 311 DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit(void *baton, 312 StoppointCallbackContext *context, 313 user_id_t break_id, 314 user_id_t break_loc_id) 315 { 316 DynamicLoaderPOSIXDYLD* dyld_instance; 317 318 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton); 319 dyld_instance->RefreshModules(); 320 321 // Return true to stop the target, false to just let the target run. 322 return dyld_instance->GetStopWhenImagesChange(); 323 } 324 325 void 326 DynamicLoaderPOSIXDYLD::RefreshModules() 327 { 328 if (!m_rendezvous.Resolve()) 329 return; 330 331 DYLDRendezvous::iterator I; 332 DYLDRendezvous::iterator E; 333 334 ModuleList &loaded_modules = m_process->GetTarget().GetImages(); 335 336 if (m_rendezvous.ModulesDidLoad()) 337 { 338 ModuleList new_modules; 339 340 E = m_rendezvous.loaded_end(); 341 for (I = m_rendezvous.loaded_begin(); I != E; ++I) 342 { 343 FileSpec file(I->path.c_str(), true); 344 ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr); 345 if (module_sp.get()) 346 { 347 loaded_modules.AppendIfNeeded(module_sp); 348 new_modules.Append(module_sp); 349 } 350 } 351 m_process->GetTarget().ModulesDidLoad(new_modules); 352 } 353 354 if (m_rendezvous.ModulesDidUnload()) 355 { 356 ModuleList old_modules; 357 358 E = m_rendezvous.unloaded_end(); 359 for (I = m_rendezvous.unloaded_begin(); I != E; ++I) 360 { 361 FileSpec file(I->path.c_str(), true); 362 ModuleSpec module_spec (file); 363 ModuleSP module_sp = 364 loaded_modules.FindFirstModule (module_spec); 365 366 if (module_sp.get()) 367 { 368 old_modules.Append(module_sp); 369 UnloadSections(module_sp); 370 } 371 } 372 loaded_modules.Remove(old_modules); 373 m_process->GetTarget().ModulesDidUnload(old_modules, false); 374 } 375 } 376 377 ThreadPlanSP 378 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, bool stop) 379 { 380 ThreadPlanSP thread_plan_sp; 381 382 StackFrame *frame = thread.GetStackFrameAtIndex(0).get(); 383 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol); 384 Symbol *sym = context.symbol; 385 386 if (sym == NULL || !sym->IsTrampoline()) 387 return thread_plan_sp; 388 389 const ConstString &sym_name = sym->GetMangled().GetName(Mangled::ePreferMangled); 390 if (!sym_name) 391 return thread_plan_sp; 392 393 SymbolContextList target_symbols; 394 Target &target = thread.GetProcess()->GetTarget(); 395 const ModuleList &images = target.GetImages(); 396 397 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols); 398 size_t num_targets = target_symbols.GetSize(); 399 if (!num_targets) 400 return thread_plan_sp; 401 402 typedef std::vector<lldb::addr_t> AddressVector; 403 AddressVector addrs; 404 for (size_t i = 0; i < num_targets; ++i) 405 { 406 SymbolContext context; 407 AddressRange range; 408 if (target_symbols.GetContextAtIndex(i, context)) 409 { 410 context.GetAddressRange(eSymbolContextEverything, 0, false, range); 411 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target); 412 if (addr != LLDB_INVALID_ADDRESS) 413 addrs.push_back(addr); 414 } 415 } 416 417 if (addrs.size() > 0) 418 { 419 AddressVector::iterator start = addrs.begin(); 420 AddressVector::iterator end = addrs.end(); 421 422 std::sort(start, end); 423 addrs.erase(std::unique(start, end), end); 424 thread_plan_sp.reset(new ThreadPlanRunToAddress(thread, addrs, stop)); 425 } 426 427 return thread_plan_sp; 428 } 429 430 void 431 DynamicLoaderPOSIXDYLD::LoadAllCurrentModules() 432 { 433 DYLDRendezvous::iterator I; 434 DYLDRendezvous::iterator E; 435 ModuleList module_list; 436 437 if (!m_rendezvous.Resolve()) 438 { 439 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 440 if (log) 441 log->Printf("DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD rendezvous address", 442 __FUNCTION__); 443 return; 444 } 445 446 // The rendezvous class doesn't enumerate the main module, so track 447 // that ourselves here. 448 ModuleSP executable = GetTargetExecutable(); 449 m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress(); 450 451 452 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) 453 { 454 const char *module_path = I->path.c_str(); 455 FileSpec file(module_path, false); 456 ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr); 457 if (module_sp.get()) 458 { 459 module_list.Append(module_sp); 460 } 461 else 462 { 463 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 464 if (log) 465 log->Printf("DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64, 466 __FUNCTION__, module_path, I->base_addr); 467 } 468 } 469 470 m_process->GetTarget().ModulesDidLoad(module_list); 471 } 472 473 ModuleSP 474 DynamicLoaderPOSIXDYLD::LoadModuleAtAddress(const FileSpec &file, addr_t link_map_addr, addr_t base_addr) 475 { 476 Target &target = m_process->GetTarget(); 477 ModuleList &modules = target.GetImages(); 478 ModuleSP module_sp; 479 480 ModuleSpec module_spec (file, target.GetArchitecture()); 481 if ((module_sp = modules.FindFirstModule (module_spec))) 482 { 483 UpdateLoadedSections(module_sp, link_map_addr, base_addr); 484 } 485 else if ((module_sp = target.GetSharedModule(module_spec))) 486 { 487 UpdateLoadedSections(module_sp, link_map_addr, base_addr); 488 } 489 490 return module_sp; 491 } 492 493 addr_t 494 DynamicLoaderPOSIXDYLD::ComputeLoadOffset() 495 { 496 addr_t virt_entry; 497 498 if (m_load_offset != LLDB_INVALID_ADDRESS) 499 return m_load_offset; 500 501 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 502 return LLDB_INVALID_ADDRESS; 503 504 ModuleSP module = m_process->GetTarget().GetExecutableModule(); 505 if (!module) 506 return LLDB_INVALID_ADDRESS; 507 508 ObjectFile *exe = module->GetObjectFile(); 509 Address file_entry = exe->GetEntryPointAddress(); 510 511 if (!file_entry.IsValid()) 512 return LLDB_INVALID_ADDRESS; 513 514 m_load_offset = virt_entry - file_entry.GetFileAddress(); 515 return m_load_offset; 516 } 517 518 addr_t 519 DynamicLoaderPOSIXDYLD::GetEntryPoint() 520 { 521 if (m_entry_point != LLDB_INVALID_ADDRESS) 522 return m_entry_point; 523 524 if (m_auxv.get() == NULL) 525 return LLDB_INVALID_ADDRESS; 526 527 AuxVector::iterator I = m_auxv->FindEntry(AuxVector::AT_ENTRY); 528 529 if (I == m_auxv->end()) 530 return LLDB_INVALID_ADDRESS; 531 532 m_entry_point = static_cast<addr_t>(I->value); 533 return m_entry_point; 534 } 535 536 const SectionList * 537 DynamicLoaderPOSIXDYLD::GetSectionListFromModule(const ModuleSP module) const 538 { 539 SectionList *sections = nullptr; 540 if (module.get()) 541 { 542 ObjectFile *obj_file = module->GetObjectFile(); 543 if (obj_file) 544 { 545 sections = obj_file->GetSectionList(); 546 } 547 } 548 return sections; 549 } 550 551 static int ReadInt(Process *process, addr_t addr) 552 { 553 Error error; 554 int value = (int)process->ReadUnsignedIntegerFromMemory(addr, sizeof(uint32_t), 0, error); 555 if (error.Fail()) 556 return -1; 557 else 558 return value; 559 } 560 561 static addr_t ReadPointer(Process *process, addr_t addr) 562 { 563 Error error; 564 addr_t value = process->ReadPointerFromMemory(addr, error); 565 if (error.Fail()) 566 return LLDB_INVALID_ADDRESS; 567 else 568 return value; 569 } 570 571 lldb::addr_t 572 DynamicLoaderPOSIXDYLD::GetThreadLocalData (const lldb::ModuleSP module, const lldb::ThreadSP thread) 573 { 574 auto it = m_loaded_modules.find (module); 575 if (it == m_loaded_modules.end()) 576 return LLDB_INVALID_ADDRESS; 577 578 addr_t link_map = it->second; 579 if (link_map == LLDB_INVALID_ADDRESS) 580 return LLDB_INVALID_ADDRESS; 581 582 const DYLDRendezvous::ThreadInfo &metadata = m_rendezvous.GetThreadInfo(); 583 if (!metadata.valid) 584 return LLDB_INVALID_ADDRESS; 585 586 // Get the thread pointer. 587 addr_t tp = thread->GetThreadPointer (); 588 if (tp == LLDB_INVALID_ADDRESS) 589 return LLDB_INVALID_ADDRESS; 590 591 // Find the module's modid. 592 int modid = ReadInt (m_process, link_map + metadata.modid_offset); 593 if (modid == -1) 594 return LLDB_INVALID_ADDRESS; 595 596 // Lookup the DTV stucture for this thread. 597 addr_t dtv_ptr = tp + metadata.dtv_offset; 598 addr_t dtv = ReadPointer (m_process, dtv_ptr); 599 if (dtv == LLDB_INVALID_ADDRESS) 600 return LLDB_INVALID_ADDRESS; 601 602 // Find the TLS block for this module. 603 addr_t dtv_slot = dtv + metadata.dtv_slot_size*modid; 604 addr_t tls_block = ReadPointer (m_process, dtv_slot + metadata.tls_offset); 605 606 Module *mod = module.get(); 607 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 608 if (log) 609 log->Printf("DynamicLoaderPOSIXDYLD::Performed TLS lookup: " 610 "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64 ", modid=%i, tls_block=0x%" PRIx64 "\n", 611 mod->GetObjectName().AsCString(""), link_map, tp, modid, tls_block); 612 613 return tls_block; 614 } 615