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 24 #include "AuxVector.h" 25 #include "DynamicLoaderPOSIXDYLD.h" 26 27 using namespace lldb; 28 using namespace lldb_private; 29 30 void 31 DynamicLoaderPOSIXDYLD::Initialize() 32 { 33 PluginManager::RegisterPlugin(GetPluginNameStatic(), 34 GetPluginDescriptionStatic(), 35 CreateInstance); 36 } 37 38 void 39 DynamicLoaderPOSIXDYLD::Terminate() 40 { 41 } 42 43 lldb_private::ConstString 44 DynamicLoaderPOSIXDYLD::GetPluginName() 45 { 46 return GetPluginNameStatic(); 47 } 48 49 lldb_private::ConstString 50 DynamicLoaderPOSIXDYLD::GetPluginNameStatic() 51 { 52 static ConstString g_name("linux-dyld"); 53 return g_name; 54 } 55 56 const char * 57 DynamicLoaderPOSIXDYLD::GetPluginDescriptionStatic() 58 { 59 return "Dynamic loader plug-in that watches for shared library " 60 "loads/unloads in POSIX processes."; 61 } 62 63 void 64 DynamicLoaderPOSIXDYLD::GetPluginCommandHelp(const char *command, Stream *strm) 65 { 66 } 67 68 uint32_t 69 DynamicLoaderPOSIXDYLD::GetPluginVersion() 70 { 71 return 1; 72 } 73 74 DynamicLoader * 75 DynamicLoaderPOSIXDYLD::CreateInstance(Process *process, bool force) 76 { 77 bool create = force; 78 if (!create) 79 { 80 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple(); 81 if (triple_ref.getOS() == llvm::Triple::Linux || 82 triple_ref.getOS() == llvm::Triple::FreeBSD) 83 create = true; 84 } 85 86 if (create) 87 return new DynamicLoaderPOSIXDYLD (process); 88 return NULL; 89 } 90 91 DynamicLoaderPOSIXDYLD::DynamicLoaderPOSIXDYLD(Process *process) 92 : DynamicLoader(process), 93 m_rendezvous(process), 94 m_load_offset(LLDB_INVALID_ADDRESS), 95 m_entry_point(LLDB_INVALID_ADDRESS), 96 m_auxv() 97 { 98 } 99 100 DynamicLoaderPOSIXDYLD::~DynamicLoaderPOSIXDYLD() 101 { 102 } 103 104 void 105 DynamicLoaderPOSIXDYLD::DidAttach() 106 { 107 ModuleSP executable; 108 addr_t load_offset; 109 110 m_auxv.reset(new AuxVector(m_process)); 111 112 executable = GetTargetExecutable(); 113 load_offset = ComputeLoadOffset(); 114 115 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS) 116 { 117 ModuleList module_list; 118 module_list.Append(executable); 119 UpdateLoadedSections(executable, load_offset); 120 LoadAllCurrentModules(); 121 m_process->GetTarget().ModulesDidLoad(module_list); 122 } 123 } 124 125 void 126 DynamicLoaderPOSIXDYLD::DidLaunch() 127 { 128 ModuleSP executable; 129 addr_t load_offset; 130 131 m_auxv.reset(new AuxVector(m_process)); 132 133 executable = GetTargetExecutable(); 134 load_offset = ComputeLoadOffset(); 135 136 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS) 137 { 138 ModuleList module_list; 139 module_list.Append(executable); 140 UpdateLoadedSections(executable, load_offset); 141 ProbeEntry(); 142 m_process->GetTarget().ModulesDidLoad(module_list); 143 } 144 } 145 146 ModuleSP 147 DynamicLoaderPOSIXDYLD::GetTargetExecutable() 148 { 149 Target &target = m_process->GetTarget(); 150 ModuleSP executable = target.GetExecutableModule(); 151 152 if (executable.get()) 153 { 154 if (executable->GetFileSpec().Exists()) 155 { 156 ModuleSpec module_spec (executable->GetFileSpec(), executable->GetArchitecture()); 157 ModuleSP module_sp (new Module (module_spec)); 158 159 // Check if the executable has changed and set it to the target executable if they differ. 160 if (module_sp.get() && module_sp->GetUUID().IsValid() && executable->GetUUID().IsValid()) 161 { 162 if (module_sp->GetUUID() != executable->GetUUID()) 163 executable.reset(); 164 } 165 else if (executable->FileHasChanged()) 166 { 167 executable.reset(); 168 } 169 170 if (!executable.get()) 171 { 172 executable = target.GetSharedModule(module_spec); 173 if (executable.get() != target.GetExecutableModulePointer()) 174 { 175 // Don't load dependent images since we are in dyld where we will know 176 // and find out about all images that are loaded 177 const bool get_dependent_images = false; 178 target.SetExecutableModule(executable, get_dependent_images); 179 } 180 } 181 } 182 } 183 return executable; 184 } 185 186 Error 187 DynamicLoaderPOSIXDYLD::ExecutePluginCommand(Args &command, Stream *strm) 188 { 189 return Error(); 190 } 191 192 Log * 193 DynamicLoaderPOSIXDYLD::EnablePluginLogging(Stream *strm, Args &command) 194 { 195 return NULL; 196 } 197 198 Error 199 DynamicLoaderPOSIXDYLD::CanLoadImage() 200 { 201 return Error(); 202 } 203 204 void 205 DynamicLoaderPOSIXDYLD::UpdateLoadedSections(ModuleSP module, addr_t base_addr) 206 { 207 ObjectFile *obj_file = module->GetObjectFile(); 208 SectionList *sections = obj_file->GetSectionList(); 209 SectionLoadList &load_list = m_process->GetTarget().GetSectionLoadList(); 210 const size_t num_sections = sections->GetSize(); 211 212 for (unsigned i = 0; i < num_sections; ++i) 213 { 214 SectionSP section_sp (sections->GetSectionAtIndex(i)); 215 lldb::addr_t new_load_addr = section_sp->GetFileAddress() + base_addr; 216 lldb::addr_t old_load_addr = load_list.GetSectionLoadAddress(section_sp); 217 218 // If the file address of the section is zero then this is not an 219 // allocatable/loadable section (property of ELF sh_addr). Skip it. 220 if (new_load_addr == base_addr) 221 continue; 222 223 if (old_load_addr == LLDB_INVALID_ADDRESS || 224 old_load_addr != new_load_addr) 225 load_list.SetSectionLoadAddress(section_sp, new_load_addr); 226 } 227 } 228 229 void 230 DynamicLoaderPOSIXDYLD::ProbeEntry() 231 { 232 Breakpoint *entry_break; 233 addr_t entry; 234 235 if ((entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 236 return; 237 238 entry_break = m_process->GetTarget().CreateBreakpoint(entry, true).get(); 239 entry_break->SetCallback(EntryBreakpointHit, this, true); 240 entry_break->SetBreakpointKind("shared-library-event"); 241 } 242 243 // The runtime linker has run and initialized the rendezvous structure once the 244 // process has hit its entry point. When we hit the corresponding breakpoint we 245 // interrogate the rendezvous structure to get the load addresses of all 246 // dependent modules for the process. Similarly, we can discover the runtime 247 // linker function and setup a breakpoint to notify us of any dynamically loaded 248 // modules (via dlopen). 249 bool 250 DynamicLoaderPOSIXDYLD::EntryBreakpointHit(void *baton, 251 StoppointCallbackContext *context, 252 user_id_t break_id, 253 user_id_t break_loc_id) 254 { 255 DynamicLoaderPOSIXDYLD* dyld_instance; 256 257 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton); 258 dyld_instance->LoadAllCurrentModules(); 259 dyld_instance->SetRendezvousBreakpoint(); 260 return false; // Continue running. 261 } 262 263 void 264 DynamicLoaderPOSIXDYLD::SetRendezvousBreakpoint() 265 { 266 Breakpoint *dyld_break; 267 addr_t break_addr; 268 269 break_addr = m_rendezvous.GetBreakAddress(); 270 dyld_break = m_process->GetTarget().CreateBreakpoint(break_addr, true).get(); 271 dyld_break->SetCallback(RendezvousBreakpointHit, this, true); 272 dyld_break->SetBreakpointKind ("shared-library-event"); 273 } 274 275 bool 276 DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit(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->RefreshModules(); 285 286 // Return true to stop the target, false to just let the target run. 287 return dyld_instance->GetStopWhenImagesChange(); 288 } 289 290 void 291 DynamicLoaderPOSIXDYLD::RefreshModules() 292 { 293 if (!m_rendezvous.Resolve()) 294 return; 295 296 DYLDRendezvous::iterator I; 297 DYLDRendezvous::iterator E; 298 299 ModuleList &loaded_modules = m_process->GetTarget().GetImages(); 300 301 if (m_rendezvous.ModulesDidLoad()) 302 { 303 ModuleList new_modules; 304 305 E = m_rendezvous.loaded_end(); 306 for (I = m_rendezvous.loaded_begin(); I != E; ++I) 307 { 308 FileSpec file(I->path.c_str(), true); 309 ModuleSP module_sp = LoadModuleAtAddress(file, I->base_addr); 310 if (module_sp.get()) 311 loaded_modules.AppendIfNeeded(module_sp); 312 } 313 } 314 315 if (m_rendezvous.ModulesDidUnload()) 316 { 317 ModuleList old_modules; 318 319 E = m_rendezvous.unloaded_end(); 320 for (I = m_rendezvous.unloaded_begin(); I != E; ++I) 321 { 322 FileSpec file(I->path.c_str(), true); 323 ModuleSpec module_spec (file); 324 ModuleSP module_sp = 325 loaded_modules.FindFirstModule (module_spec); 326 if (module_sp.get()) 327 old_modules.Append(module_sp); 328 } 329 loaded_modules.Remove(old_modules); 330 } 331 } 332 333 ThreadPlanSP 334 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, bool stop) 335 { 336 ThreadPlanSP thread_plan_sp; 337 338 StackFrame *frame = thread.GetStackFrameAtIndex(0).get(); 339 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol); 340 Symbol *sym = context.symbol; 341 342 if (sym == NULL || !sym->IsTrampoline()) 343 return thread_plan_sp; 344 345 const ConstString &sym_name = sym->GetMangled().GetName(Mangled::ePreferMangled); 346 if (!sym_name) 347 return thread_plan_sp; 348 349 SymbolContextList target_symbols; 350 Target &target = thread.GetProcess()->GetTarget(); 351 const ModuleList &images = target.GetImages(); 352 353 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols); 354 size_t num_targets = target_symbols.GetSize(); 355 if (!num_targets) 356 return thread_plan_sp; 357 358 typedef std::vector<lldb::addr_t> AddressVector; 359 AddressVector addrs; 360 for (size_t i = 0; i < num_targets; ++i) 361 { 362 SymbolContext context; 363 AddressRange range; 364 if (target_symbols.GetContextAtIndex(i, context)) 365 { 366 context.GetAddressRange(eSymbolContextEverything, 0, false, range); 367 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target); 368 if (addr != LLDB_INVALID_ADDRESS) 369 addrs.push_back(addr); 370 } 371 } 372 373 if (addrs.size() > 0) 374 { 375 AddressVector::iterator start = addrs.begin(); 376 AddressVector::iterator end = addrs.end(); 377 378 std::sort(start, end); 379 addrs.erase(std::unique(start, end), end); 380 thread_plan_sp.reset(new ThreadPlanRunToAddress(thread, addrs, stop)); 381 } 382 383 return thread_plan_sp; 384 } 385 386 void 387 DynamicLoaderPOSIXDYLD::LoadAllCurrentModules() 388 { 389 DYLDRendezvous::iterator I; 390 DYLDRendezvous::iterator E; 391 ModuleList module_list; 392 393 if (!m_rendezvous.Resolve()) 394 return; 395 396 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) 397 { 398 FileSpec file(I->path.c_str(), false); 399 ModuleSP module_sp = LoadModuleAtAddress(file, I->base_addr); 400 if (module_sp.get()) 401 module_list.Append(module_sp); 402 } 403 404 m_process->GetTarget().ModulesDidLoad(module_list); 405 } 406 407 ModuleSP 408 DynamicLoaderPOSIXDYLD::LoadModuleAtAddress(const FileSpec &file, addr_t base_addr) 409 { 410 Target &target = m_process->GetTarget(); 411 ModuleList &modules = target.GetImages(); 412 ModuleSP module_sp; 413 414 ModuleSpec module_spec (file, target.GetArchitecture()); 415 if ((module_sp = modules.FindFirstModule (module_spec))) 416 { 417 UpdateLoadedSections(module_sp, base_addr); 418 } 419 else if ((module_sp = target.GetSharedModule(module_spec))) 420 { 421 UpdateLoadedSections(module_sp, base_addr); 422 } 423 424 return module_sp; 425 } 426 427 addr_t 428 DynamicLoaderPOSIXDYLD::ComputeLoadOffset() 429 { 430 addr_t virt_entry; 431 432 if (m_load_offset != LLDB_INVALID_ADDRESS) 433 return m_load_offset; 434 435 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS) 436 return LLDB_INVALID_ADDRESS; 437 438 ModuleSP module = m_process->GetTarget().GetExecutableModule(); 439 ObjectFile *exe = module->GetObjectFile(); 440 Address file_entry = exe->GetEntryPointAddress(); 441 442 if (!file_entry.IsValid()) 443 return LLDB_INVALID_ADDRESS; 444 445 m_load_offset = virt_entry - file_entry.GetFileAddress(); 446 return m_load_offset; 447 } 448 449 addr_t 450 DynamicLoaderPOSIXDYLD::GetEntryPoint() 451 { 452 if (m_entry_point != LLDB_INVALID_ADDRESS) 453 return m_entry_point; 454 455 if (m_auxv.get() == NULL) 456 return LLDB_INVALID_ADDRESS; 457 458 AuxVector::iterator I = m_auxv->FindEntry(AuxVector::AT_ENTRY); 459 460 if (I == m_auxv->end()) 461 return LLDB_INVALID_ADDRESS; 462 463 m_entry_point = static_cast<addr_t>(I->value); 464 return m_entry_point; 465 } 466