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, 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, 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 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 const size_t num_sections = sections->GetSize(); 220 221 for (unsigned i = 0; i < num_sections; ++i) 222 { 223 SectionSP section_sp (sections->GetSectionAtIndex(i)); 224 lldb::addr_t new_load_addr = section_sp->GetFileAddress() + base_addr; 225 lldb::addr_t old_load_addr = load_list.GetSectionLoadAddress(section_sp); 226 227 // If the file address of the section is zero then this is not an 228 // allocatable/loadable section (property of ELF sh_addr). Skip it. 229 if (new_load_addr == base_addr) 230 continue; 231 232 if (old_load_addr == LLDB_INVALID_ADDRESS || 233 old_load_addr != new_load_addr) 234 load_list.SetSectionLoadAddress(section_sp, new_load_addr); 235 } 236 } 237 238 void 239 DynamicLoaderPOSIXDYLD::UnloadSections(const ModuleSP module) 240 { 241 SectionLoadList &load_list = m_process->GetTarget().GetSectionLoadList(); 242 const SectionList *sections = GetSectionListFromModule(module); 243 244 assert(sections && "SectionList missing from unloaded module."); 245 246 const size_t num_sections = sections->GetSize(); 247 for (size_t i = 0; i < num_sections; ++i) 248 { 249 SectionSP section_sp (sections->GetSectionAtIndex(i)); 250 load_list.SetSectionUnloaded(section_sp); 251 } 252 } 253 254 void 255 DynamicLoaderPOSIXDYLD::ProbeEntry() 256 { 257 Breakpoint *entry_break; 258 addr_t entry; 259 260 if ((entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 261 return; 262 263 entry_break = m_process->GetTarget().CreateBreakpoint(entry, true, false).get(); 264 entry_break->SetCallback(EntryBreakpointHit, this, true); 265 entry_break->SetBreakpointKind("shared-library-event"); 266 } 267 268 // The runtime linker has run and initialized the rendezvous structure once the 269 // process has hit its entry point. When we hit the corresponding breakpoint we 270 // interrogate the rendezvous structure to get the load addresses of all 271 // dependent modules for the process. Similarly, we can discover the runtime 272 // linker function and setup a breakpoint to notify us of any dynamically loaded 273 // modules (via dlopen). 274 bool 275 DynamicLoaderPOSIXDYLD::EntryBreakpointHit(void *baton, 276 StoppointCallbackContext *context, 277 user_id_t break_id, 278 user_id_t break_loc_id) 279 { 280 DynamicLoaderPOSIXDYLD* dyld_instance; 281 282 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton); 283 dyld_instance->LoadAllCurrentModules(); 284 dyld_instance->SetRendezvousBreakpoint(); 285 return false; // Continue running. 286 } 287 288 void 289 DynamicLoaderPOSIXDYLD::SetRendezvousBreakpoint() 290 { 291 addr_t break_addr = m_rendezvous.GetBreakAddress(); 292 Target &target = m_process->GetTarget(); 293 294 if (m_dyld_bid == LLDB_INVALID_BREAK_ID) 295 { 296 Breakpoint *dyld_break = target.CreateBreakpoint (break_addr, true, false).get(); 297 dyld_break->SetCallback(RendezvousBreakpointHit, this, true); 298 dyld_break->SetBreakpointKind ("shared-library-event"); 299 m_dyld_bid = dyld_break->GetID(); 300 } 301 302 // Make sure our breakpoint is at the right address. 303 assert (target.GetBreakpointByID(m_dyld_bid)->FindLocationByAddress(break_addr)->GetBreakpoint().GetID() == m_dyld_bid); 304 } 305 306 bool 307 DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit(void *baton, 308 StoppointCallbackContext *context, 309 user_id_t break_id, 310 user_id_t break_loc_id) 311 { 312 DynamicLoaderPOSIXDYLD* dyld_instance; 313 314 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton); 315 dyld_instance->RefreshModules(); 316 317 // Return true to stop the target, false to just let the target run. 318 return dyld_instance->GetStopWhenImagesChange(); 319 } 320 321 void 322 DynamicLoaderPOSIXDYLD::RefreshModules() 323 { 324 if (!m_rendezvous.Resolve()) 325 return; 326 327 DYLDRendezvous::iterator I; 328 DYLDRendezvous::iterator E; 329 330 ModuleList &loaded_modules = m_process->GetTarget().GetImages(); 331 332 if (m_rendezvous.ModulesDidLoad()) 333 { 334 ModuleList new_modules; 335 336 E = m_rendezvous.loaded_end(); 337 for (I = m_rendezvous.loaded_begin(); I != E; ++I) 338 { 339 FileSpec file(I->path.c_str(), true); 340 ModuleSP module_sp = LoadModuleAtAddress(file, I->base_addr); 341 if (module_sp.get()) 342 { 343 loaded_modules.AppendIfNeeded(module_sp); 344 new_modules.Append(module_sp); 345 } 346 } 347 m_process->GetTarget().ModulesDidLoad(new_modules); 348 } 349 350 if (m_rendezvous.ModulesDidUnload()) 351 { 352 ModuleList old_modules; 353 354 E = m_rendezvous.unloaded_end(); 355 for (I = m_rendezvous.unloaded_begin(); I != E; ++I) 356 { 357 FileSpec file(I->path.c_str(), true); 358 ModuleSpec module_spec (file); 359 ModuleSP module_sp = 360 loaded_modules.FindFirstModule (module_spec); 361 362 if (module_sp.get()) 363 { 364 old_modules.Append(module_sp); 365 UnloadSections(module_sp); 366 } 367 } 368 loaded_modules.Remove(old_modules); 369 m_process->GetTarget().ModulesDidUnload(old_modules); 370 } 371 } 372 373 ThreadPlanSP 374 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, bool stop) 375 { 376 ThreadPlanSP thread_plan_sp; 377 378 StackFrame *frame = thread.GetStackFrameAtIndex(0).get(); 379 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol); 380 Symbol *sym = context.symbol; 381 382 if (sym == NULL || !sym->IsTrampoline()) 383 return thread_plan_sp; 384 385 const ConstString &sym_name = sym->GetMangled().GetName(Mangled::ePreferMangled); 386 if (!sym_name) 387 return thread_plan_sp; 388 389 SymbolContextList target_symbols; 390 Target &target = thread.GetProcess()->GetTarget(); 391 const ModuleList &images = target.GetImages(); 392 393 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols); 394 size_t num_targets = target_symbols.GetSize(); 395 if (!num_targets) 396 return thread_plan_sp; 397 398 typedef std::vector<lldb::addr_t> AddressVector; 399 AddressVector addrs; 400 for (size_t i = 0; i < num_targets; ++i) 401 { 402 SymbolContext context; 403 AddressRange range; 404 if (target_symbols.GetContextAtIndex(i, context)) 405 { 406 context.GetAddressRange(eSymbolContextEverything, 0, false, range); 407 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target); 408 if (addr != LLDB_INVALID_ADDRESS) 409 addrs.push_back(addr); 410 } 411 } 412 413 if (addrs.size() > 0) 414 { 415 AddressVector::iterator start = addrs.begin(); 416 AddressVector::iterator end = addrs.end(); 417 418 std::sort(start, end); 419 addrs.erase(std::unique(start, end), end); 420 thread_plan_sp.reset(new ThreadPlanRunToAddress(thread, addrs, stop)); 421 } 422 423 return thread_plan_sp; 424 } 425 426 void 427 DynamicLoaderPOSIXDYLD::LoadAllCurrentModules() 428 { 429 DYLDRendezvous::iterator I; 430 DYLDRendezvous::iterator E; 431 ModuleList module_list; 432 433 if (!m_rendezvous.Resolve()) 434 { 435 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 436 if (log) 437 log->Printf("DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD rendezvous address", 438 __func__); 439 return; 440 } 441 442 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) 443 { 444 const char *module_path = I->path.c_str(); 445 FileSpec file(module_path, false); 446 ModuleSP module_sp = LoadModuleAtAddress(file, I->base_addr); 447 if (module_sp.get()) 448 { 449 module_list.Append(module_sp); 450 } 451 else 452 { 453 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 454 if (log) 455 log->Printf("DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64, 456 __func__, module_path, I->base_addr); 457 } 458 } 459 460 m_process->GetTarget().ModulesDidLoad(module_list); 461 } 462 463 ModuleSP 464 DynamicLoaderPOSIXDYLD::LoadModuleAtAddress(const FileSpec &file, addr_t base_addr) 465 { 466 Target &target = m_process->GetTarget(); 467 ModuleList &modules = target.GetImages(); 468 ModuleSP module_sp; 469 470 ModuleSpec module_spec (file, target.GetArchitecture()); 471 if ((module_sp = modules.FindFirstModule (module_spec))) 472 { 473 UpdateLoadedSections(module_sp, base_addr); 474 } 475 else if ((module_sp = target.GetSharedModule(module_spec))) 476 { 477 UpdateLoadedSections(module_sp, base_addr); 478 } 479 480 return module_sp; 481 } 482 483 addr_t 484 DynamicLoaderPOSIXDYLD::ComputeLoadOffset() 485 { 486 addr_t virt_entry; 487 488 if (m_load_offset != LLDB_INVALID_ADDRESS) 489 return m_load_offset; 490 491 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 492 return LLDB_INVALID_ADDRESS; 493 494 ModuleSP module = m_process->GetTarget().GetExecutableModule(); 495 if (!module) 496 return LLDB_INVALID_ADDRESS; 497 498 ObjectFile *exe = module->GetObjectFile(); 499 Address file_entry = exe->GetEntryPointAddress(); 500 501 if (!file_entry.IsValid()) 502 return LLDB_INVALID_ADDRESS; 503 504 m_load_offset = virt_entry - file_entry.GetFileAddress(); 505 return m_load_offset; 506 } 507 508 addr_t 509 DynamicLoaderPOSIXDYLD::GetEntryPoint() 510 { 511 if (m_entry_point != LLDB_INVALID_ADDRESS) 512 return m_entry_point; 513 514 if (m_auxv.get() == NULL) 515 return LLDB_INVALID_ADDRESS; 516 517 AuxVector::iterator I = m_auxv->FindEntry(AuxVector::AT_ENTRY); 518 519 if (I == m_auxv->end()) 520 return LLDB_INVALID_ADDRESS; 521 522 m_entry_point = static_cast<addr_t>(I->value); 523 return m_entry_point; 524 } 525 526 const SectionList * 527 DynamicLoaderPOSIXDYLD::GetSectionListFromModule(const ModuleSP module) const 528 { 529 SectionList *sections = nullptr; 530 if (module.get()) 531 { 532 ObjectFile *obj_file = module->GetObjectFile(); 533 if (obj_file) 534 { 535 sections = obj_file->GetSectionList(); 536 } 537 } 538 return sections; 539 } 540