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 Target &target = m_process->GetTarget(); 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 228 // If the file address of the section is zero then this is not an 229 // allocatable/loadable section (property of ELF sh_addr). Skip it. 230 if (new_load_addr == base_addr) 231 continue; 232 233 target.SetSectionLoadAddress(section_sp, new_load_addr); 234 } 235 } 236 237 void 238 DynamicLoaderPOSIXDYLD::UnloadSections(const ModuleSP module) 239 { 240 Target &target = m_process->GetTarget(); 241 const SectionList *sections = GetSectionListFromModule(module); 242 243 assert(sections && "SectionList missing from unloaded module."); 244 245 m_loaded_modules.erase(module); 246 247 const size_t num_sections = sections->GetSize(); 248 for (size_t i = 0; i < num_sections; ++i) 249 { 250 SectionSP section_sp (sections->GetSectionAtIndex(i)); 251 target.SetSectionUnloaded(section_sp); 252 } 253 } 254 255 void 256 DynamicLoaderPOSIXDYLD::ProbeEntry() 257 { 258 Breakpoint *entry_break; 259 addr_t entry; 260 261 if ((entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 262 return; 263 264 entry_break = m_process->GetTarget().CreateBreakpoint(entry, true, false).get(); 265 entry_break->SetCallback(EntryBreakpointHit, this, true); 266 entry_break->SetBreakpointKind("shared-library-event"); 267 } 268 269 // The runtime linker has run and initialized the rendezvous structure once the 270 // process has hit its entry point. When we hit the corresponding breakpoint we 271 // interrogate the rendezvous structure to get the load addresses of all 272 // dependent modules for the process. Similarly, we can discover the runtime 273 // linker function and setup a breakpoint to notify us of any dynamically loaded 274 // modules (via dlopen). 275 bool 276 DynamicLoaderPOSIXDYLD::EntryBreakpointHit(void *baton, 277 StoppointCallbackContext *context, 278 user_id_t break_id, 279 user_id_t break_loc_id) 280 { 281 DynamicLoaderPOSIXDYLD* dyld_instance; 282 283 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton); 284 dyld_instance->LoadAllCurrentModules(); 285 dyld_instance->SetRendezvousBreakpoint(); 286 return false; // Continue running. 287 } 288 289 void 290 DynamicLoaderPOSIXDYLD::SetRendezvousBreakpoint() 291 { 292 addr_t break_addr = m_rendezvous.GetBreakAddress(); 293 Target &target = m_process->GetTarget(); 294 295 if (m_dyld_bid == LLDB_INVALID_BREAK_ID) 296 { 297 Breakpoint *dyld_break = target.CreateBreakpoint (break_addr, true, false).get(); 298 dyld_break->SetCallback(RendezvousBreakpointHit, this, true); 299 dyld_break->SetBreakpointKind ("shared-library-event"); 300 m_dyld_bid = dyld_break->GetID(); 301 } 302 303 // Make sure our breakpoint is at the right address. 304 assert (target.GetBreakpointByID(m_dyld_bid)->FindLocationByAddress(break_addr)->GetBreakpoint().GetID() == m_dyld_bid); 305 } 306 307 bool 308 DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit(void *baton, 309 StoppointCallbackContext *context, 310 user_id_t break_id, 311 user_id_t break_loc_id) 312 { 313 DynamicLoaderPOSIXDYLD* dyld_instance; 314 315 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton); 316 dyld_instance->RefreshModules(); 317 318 // Return true to stop the target, false to just let the target run. 319 return dyld_instance->GetStopWhenImagesChange(); 320 } 321 322 void 323 DynamicLoaderPOSIXDYLD::RefreshModules() 324 { 325 if (!m_rendezvous.Resolve()) 326 return; 327 328 DYLDRendezvous::iterator I; 329 DYLDRendezvous::iterator E; 330 331 ModuleList &loaded_modules = m_process->GetTarget().GetImages(); 332 333 if (m_rendezvous.ModulesDidLoad()) 334 { 335 ModuleList new_modules; 336 337 E = m_rendezvous.loaded_end(); 338 for (I = m_rendezvous.loaded_begin(); I != E; ++I) 339 { 340 FileSpec file(I->path.c_str(), true); 341 ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr); 342 if (module_sp.get()) 343 { 344 loaded_modules.AppendIfNeeded(module_sp); 345 new_modules.Append(module_sp); 346 } 347 } 348 m_process->GetTarget().ModulesDidLoad(new_modules); 349 } 350 351 if (m_rendezvous.ModulesDidUnload()) 352 { 353 ModuleList old_modules; 354 355 E = m_rendezvous.unloaded_end(); 356 for (I = m_rendezvous.unloaded_begin(); I != E; ++I) 357 { 358 FileSpec file(I->path.c_str(), true); 359 ModuleSpec module_spec (file); 360 ModuleSP module_sp = 361 loaded_modules.FindFirstModule (module_spec); 362 363 if (module_sp.get()) 364 { 365 old_modules.Append(module_sp); 366 UnloadSections(module_sp); 367 } 368 } 369 loaded_modules.Remove(old_modules); 370 m_process->GetTarget().ModulesDidUnload(old_modules, false); 371 } 372 } 373 374 ThreadPlanSP 375 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, bool stop) 376 { 377 ThreadPlanSP thread_plan_sp; 378 379 StackFrame *frame = thread.GetStackFrameAtIndex(0).get(); 380 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol); 381 Symbol *sym = context.symbol; 382 383 if (sym == NULL || !sym->IsTrampoline()) 384 return thread_plan_sp; 385 386 const ConstString &sym_name = sym->GetMangled().GetName(Mangled::ePreferMangled); 387 if (!sym_name) 388 return thread_plan_sp; 389 390 SymbolContextList target_symbols; 391 Target &target = thread.GetProcess()->GetTarget(); 392 const ModuleList &images = target.GetImages(); 393 394 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols); 395 size_t num_targets = target_symbols.GetSize(); 396 if (!num_targets) 397 return thread_plan_sp; 398 399 typedef std::vector<lldb::addr_t> AddressVector; 400 AddressVector addrs; 401 for (size_t i = 0; i < num_targets; ++i) 402 { 403 SymbolContext context; 404 AddressRange range; 405 if (target_symbols.GetContextAtIndex(i, context)) 406 { 407 context.GetAddressRange(eSymbolContextEverything, 0, false, range); 408 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target); 409 if (addr != LLDB_INVALID_ADDRESS) 410 addrs.push_back(addr); 411 } 412 } 413 414 if (addrs.size() > 0) 415 { 416 AddressVector::iterator start = addrs.begin(); 417 AddressVector::iterator end = addrs.end(); 418 419 std::sort(start, end); 420 addrs.erase(std::unique(start, end), end); 421 thread_plan_sp.reset(new ThreadPlanRunToAddress(thread, addrs, stop)); 422 } 423 424 return thread_plan_sp; 425 } 426 427 void 428 DynamicLoaderPOSIXDYLD::LoadAllCurrentModules() 429 { 430 DYLDRendezvous::iterator I; 431 DYLDRendezvous::iterator E; 432 ModuleList module_list; 433 434 if (!m_rendezvous.Resolve()) 435 { 436 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 437 if (log) 438 log->Printf("DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD rendezvous address", 439 __FUNCTION__); 440 return; 441 } 442 443 // The rendezvous class doesn't enumerate the main module, so track 444 // that ourselves here. 445 ModuleSP executable = GetTargetExecutable(); 446 m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress(); 447 448 449 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) 450 { 451 const char *module_path = I->path.c_str(); 452 FileSpec file(module_path, false); 453 ModuleSP module_sp = LoadModuleAtAddress(file, I->link_addr, I->base_addr); 454 if (module_sp.get()) 455 { 456 module_list.Append(module_sp); 457 } 458 else 459 { 460 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 461 if (log) 462 log->Printf("DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64, 463 __FUNCTION__, module_path, I->base_addr); 464 } 465 } 466 467 m_process->GetTarget().ModulesDidLoad(module_list); 468 } 469 470 ModuleSP 471 DynamicLoaderPOSIXDYLD::LoadModuleAtAddress(const FileSpec &file, addr_t link_map_addr, addr_t base_addr) 472 { 473 Target &target = m_process->GetTarget(); 474 ModuleList &modules = target.GetImages(); 475 ModuleSP module_sp; 476 477 ModuleSpec module_spec (file, target.GetArchitecture()); 478 if ((module_sp = modules.FindFirstModule (module_spec))) 479 { 480 UpdateLoadedSections(module_sp, link_map_addr, base_addr); 481 } 482 else if ((module_sp = target.GetSharedModule(module_spec))) 483 { 484 UpdateLoadedSections(module_sp, link_map_addr, base_addr); 485 } 486 487 return module_sp; 488 } 489 490 addr_t 491 DynamicLoaderPOSIXDYLD::ComputeLoadOffset() 492 { 493 addr_t virt_entry; 494 495 if (m_load_offset != LLDB_INVALID_ADDRESS) 496 return m_load_offset; 497 498 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 499 return LLDB_INVALID_ADDRESS; 500 501 ModuleSP module = m_process->GetTarget().GetExecutableModule(); 502 if (!module) 503 return LLDB_INVALID_ADDRESS; 504 505 ObjectFile *exe = module->GetObjectFile(); 506 Address file_entry = exe->GetEntryPointAddress(); 507 508 if (!file_entry.IsValid()) 509 return LLDB_INVALID_ADDRESS; 510 511 m_load_offset = virt_entry - file_entry.GetFileAddress(); 512 return m_load_offset; 513 } 514 515 addr_t 516 DynamicLoaderPOSIXDYLD::GetEntryPoint() 517 { 518 if (m_entry_point != LLDB_INVALID_ADDRESS) 519 return m_entry_point; 520 521 if (m_auxv.get() == NULL) 522 return LLDB_INVALID_ADDRESS; 523 524 AuxVector::iterator I = m_auxv->FindEntry(AuxVector::AT_ENTRY); 525 526 if (I == m_auxv->end()) 527 return LLDB_INVALID_ADDRESS; 528 529 m_entry_point = static_cast<addr_t>(I->value); 530 return m_entry_point; 531 } 532 533 const SectionList * 534 DynamicLoaderPOSIXDYLD::GetSectionListFromModule(const ModuleSP module) const 535 { 536 SectionList *sections = nullptr; 537 if (module.get()) 538 { 539 ObjectFile *obj_file = module->GetObjectFile(); 540 if (obj_file) 541 { 542 sections = obj_file->GetSectionList(); 543 } 544 } 545 return sections; 546 } 547 548 static int ReadInt(Process *process, addr_t addr) 549 { 550 Error error; 551 int value = (int)process->ReadUnsignedIntegerFromMemory(addr, sizeof(uint32_t), 0, error); 552 if (error.Fail()) 553 return -1; 554 else 555 return value; 556 } 557 558 static addr_t ReadPointer(Process *process, addr_t addr) 559 { 560 Error error; 561 addr_t value = process->ReadPointerFromMemory(addr, error); 562 if (error.Fail()) 563 return LLDB_INVALID_ADDRESS; 564 else 565 return value; 566 } 567 568 lldb::addr_t 569 DynamicLoaderPOSIXDYLD::GetThreadLocalData (const lldb::ModuleSP module, const lldb::ThreadSP thread) 570 { 571 auto it = m_loaded_modules.find (module); 572 if (it == m_loaded_modules.end()) 573 return LLDB_INVALID_ADDRESS; 574 575 addr_t link_map = it->second; 576 if (link_map == LLDB_INVALID_ADDRESS) 577 return LLDB_INVALID_ADDRESS; 578 579 const DYLDRendezvous::ThreadInfo &metadata = m_rendezvous.GetThreadInfo(); 580 if (!metadata.valid) 581 return LLDB_INVALID_ADDRESS; 582 583 // Get the thread pointer. 584 addr_t tp = thread->GetThreadPointer (); 585 if (tp == LLDB_INVALID_ADDRESS) 586 return LLDB_INVALID_ADDRESS; 587 588 // Find the module's modid. 589 int modid = ReadInt (m_process, link_map + metadata.modid_offset); 590 if (modid == -1) 591 return LLDB_INVALID_ADDRESS; 592 593 // Lookup the DTV stucture for this thread. 594 addr_t dtv_ptr = tp + metadata.dtv_offset; 595 addr_t dtv = ReadPointer (m_process, dtv_ptr); 596 if (dtv == LLDB_INVALID_ADDRESS) 597 return LLDB_INVALID_ADDRESS; 598 599 // Find the TLS block for this module. 600 addr_t dtv_slot = dtv + metadata.dtv_slot_size*modid; 601 addr_t tls_block = ReadPointer (m_process, dtv_slot + metadata.tls_offset); 602 603 Module *mod = module.get(); 604 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 605 if (log) 606 log->Printf("DynamicLoaderPOSIXDYLD::Performed TLS lookup: " 607 "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64 ", modid=%i, tls_block=0x%" PRIx64 "\n", 608 mod->GetObjectName().AsCString(""), link_map, tp, modid, tls_block); 609 610 return tls_block; 611 } 612