1 //===-- DynamicLoader.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Target/DynamicLoader.h" 10 11 #include "lldb/Core/Debugger.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/ModuleList.h" 14 #include "lldb/Core/ModuleSpec.h" 15 #include "lldb/Core/PluginManager.h" 16 #include "lldb/Core/Progress.h" 17 #include "lldb/Core/Section.h" 18 #include "lldb/Symbol/ObjectFile.h" 19 #include "lldb/Target/MemoryRegionInfo.h" 20 #include "lldb/Target/Platform.h" 21 #include "lldb/Target/Process.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Utility/ConstString.h" 24 #include "lldb/Utility/LLDBLog.h" 25 #include "lldb/Utility/Log.h" 26 #include "lldb/lldb-private-interfaces.h" 27 28 #include "llvm/ADT/StringRef.h" 29 30 #include <memory> 31 32 #include <cassert> 33 34 using namespace lldb; 35 using namespace lldb_private; 36 37 DynamicLoader *DynamicLoader::FindPlugin(Process *process, 38 llvm::StringRef plugin_name) { 39 DynamicLoaderCreateInstance create_callback = nullptr; 40 if (!plugin_name.empty()) { 41 create_callback = 42 PluginManager::GetDynamicLoaderCreateCallbackForPluginName(plugin_name); 43 if (create_callback) { 44 std::unique_ptr<DynamicLoader> instance_up( 45 create_callback(process, true)); 46 if (instance_up) 47 return instance_up.release(); 48 } 49 } else { 50 for (uint32_t idx = 0; 51 (create_callback = 52 PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) != 53 nullptr; 54 ++idx) { 55 std::unique_ptr<DynamicLoader> instance_up( 56 create_callback(process, false)); 57 if (instance_up) 58 return instance_up.release(); 59 } 60 } 61 return nullptr; 62 } 63 64 DynamicLoader::DynamicLoader(Process *process) : m_process(process) {} 65 66 // Accessors to the global setting as to whether to stop at image (shared 67 // library) loading/unloading. 68 69 bool DynamicLoader::GetStopWhenImagesChange() const { 70 return m_process->GetStopOnSharedLibraryEvents(); 71 } 72 73 void DynamicLoader::SetStopWhenImagesChange(bool stop) { 74 m_process->SetStopOnSharedLibraryEvents(stop); 75 } 76 77 ModuleSP DynamicLoader::GetTargetExecutable() { 78 Target &target = m_process->GetTarget(); 79 ModuleSP executable = target.GetExecutableModule(); 80 81 if (executable) { 82 if (FileSystem::Instance().Exists(executable->GetFileSpec())) { 83 ModuleSpec module_spec(executable->GetFileSpec(), 84 executable->GetArchitecture()); 85 auto module_sp = std::make_shared<Module>(module_spec); 86 // If we're a coredump and we already have a main executable, we don't 87 // need to reload the module list that target already has 88 if (!m_process->IsLiveDebugSession()) { 89 return executable; 90 } 91 // Check if the executable has changed and set it to the target 92 // executable if they differ. 93 if (module_sp && module_sp->GetUUID().IsValid() && 94 executable->GetUUID().IsValid()) { 95 if (module_sp->GetUUID() != executable->GetUUID()) 96 executable.reset(); 97 } else if (executable->FileHasChanged()) { 98 executable.reset(); 99 } 100 101 if (!executable) { 102 executable = target.GetOrCreateModule(module_spec, true /* notify */); 103 if (executable.get() != target.GetExecutableModulePointer()) { 104 // Don't load dependent images since we are in dyld where we will 105 // know and find out about all images that are loaded 106 target.SetExecutableModule(executable, eLoadDependentsNo); 107 } 108 } 109 } 110 } 111 return executable; 112 } 113 114 void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, 115 addr_t base_addr, 116 bool base_addr_is_offset) { 117 UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset); 118 } 119 120 void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module, 121 addr_t base_addr, 122 bool base_addr_is_offset) { 123 bool changed; 124 module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset, 125 changed); 126 } 127 128 void DynamicLoader::UnloadSections(const ModuleSP module) { 129 UnloadSectionsCommon(module); 130 } 131 132 void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) { 133 Target &target = m_process->GetTarget(); 134 const SectionList *sections = GetSectionListFromModule(module); 135 136 assert(sections && "SectionList missing from unloaded module."); 137 138 const size_t num_sections = sections->GetSize(); 139 for (size_t i = 0; i < num_sections; ++i) { 140 SectionSP section_sp(sections->GetSectionAtIndex(i)); 141 target.SetSectionUnloaded(section_sp); 142 } 143 } 144 145 const SectionList * 146 DynamicLoader::GetSectionListFromModule(const ModuleSP module) const { 147 SectionList *sections = nullptr; 148 if (module) { 149 ObjectFile *obj_file = module->GetObjectFile(); 150 if (obj_file != nullptr) { 151 sections = obj_file->GetSectionList(); 152 } 153 } 154 return sections; 155 } 156 157 ModuleSP DynamicLoader::FindModuleViaTarget(const FileSpec &file) { 158 Target &target = m_process->GetTarget(); 159 ModuleSpec module_spec(file, target.GetArchitecture()); 160 161 if (ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec)) 162 return module_sp; 163 164 if (ModuleSP module_sp = target.GetOrCreateModule(module_spec, false)) 165 return module_sp; 166 167 return nullptr; 168 } 169 170 ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file, 171 addr_t link_map_addr, 172 addr_t base_addr, 173 bool base_addr_is_offset) { 174 if (ModuleSP module_sp = FindModuleViaTarget(file)) { 175 UpdateLoadedSections(module_sp, link_map_addr, base_addr, 176 base_addr_is_offset); 177 return module_sp; 178 } 179 180 return nullptr; 181 } 182 183 static ModuleSP ReadUnnamedMemoryModule(Process *process, addr_t addr, 184 llvm::StringRef name) { 185 char namebuf[80]; 186 if (name.empty()) { 187 snprintf(namebuf, sizeof(namebuf), "memory-image-0x%" PRIx64, addr); 188 name = namebuf; 189 } 190 return process->ReadModuleFromMemory(FileSpec(name), addr); 191 } 192 193 ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress( 194 Process *process, llvm::StringRef name, UUID uuid, addr_t value, 195 bool value_is_offset, bool force_symbol_search, bool notify, 196 bool set_address_in_target, bool allow_memory_image_last_resort) { 197 ModuleSP memory_module_sp; 198 ModuleSP module_sp; 199 PlatformSP platform_sp = process->GetTarget().GetPlatform(); 200 Target &target = process->GetTarget(); 201 Status error; 202 203 StreamString prog_str; 204 if (!name.empty()) { 205 prog_str << name.str() << " "; 206 } 207 if (uuid.IsValid()) 208 prog_str << uuid.GetAsString(); 209 if (value_is_offset == 0 && value != LLDB_INVALID_ADDRESS) { 210 prog_str << "at 0x"; 211 prog_str.PutHex64(value); 212 } 213 214 if (!uuid.IsValid() && !value_is_offset) { 215 memory_module_sp = ReadUnnamedMemoryModule(process, value, name); 216 217 if (memory_module_sp) { 218 uuid = memory_module_sp->GetUUID(); 219 if (uuid.IsValid()) { 220 prog_str << " "; 221 prog_str << uuid.GetAsString(); 222 } 223 } 224 } 225 ModuleSpec module_spec; 226 module_spec.GetUUID() = uuid; 227 FileSpec name_filespec(name); 228 229 if (uuid.IsValid()) { 230 Progress progress("Locating binary", prog_str.GetString().str()); 231 232 // Has lldb already seen a module with this UUID? 233 // Or have external lookup enabled in DebugSymbols on macOS. 234 if (!module_sp) 235 error = ModuleList::GetSharedModule(module_spec, module_sp, nullptr, 236 nullptr, nullptr); 237 238 // Can lldb's symbol/executable location schemes 239 // find an executable and symbol file. 240 if (!module_sp) { 241 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths(); 242 module_spec.GetSymbolFileSpec() = 243 PluginManager::LocateExecutableSymbolFile(module_spec, search_paths); 244 ModuleSpec objfile_module_spec = 245 PluginManager::LocateExecutableObjectFile(module_spec); 246 module_spec.GetFileSpec() = objfile_module_spec.GetFileSpec(); 247 if (FileSystem::Instance().Exists(module_spec.GetFileSpec()) && 248 FileSystem::Instance().Exists(module_spec.GetSymbolFileSpec())) { 249 module_sp = std::make_shared<Module>(module_spec); 250 } 251 } 252 253 // If we haven't found a binary, or we don't have a SymbolFile, see 254 // if there is an external search tool that can find it. 255 if (!module_sp || !module_sp->GetSymbolFileFileSpec()) { 256 PluginManager::DownloadObjectAndSymbolFile(module_spec, error, 257 force_symbol_search); 258 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) { 259 module_sp = std::make_shared<Module>(module_spec); 260 } else if (force_symbol_search && error.AsCString("") && 261 error.AsCString("")[0] != '\0') { 262 target.GetDebugger().GetErrorStream() << error.AsCString(); 263 } 264 } 265 266 // If we only found the executable, create a Module based on that. 267 if (!module_sp && FileSystem::Instance().Exists(module_spec.GetFileSpec())) 268 module_sp = std::make_shared<Module>(module_spec); 269 } 270 271 // If we couldn't find the binary anywhere else, as a last resort, 272 // read it out of memory. 273 if (allow_memory_image_last_resort && !module_sp.get() && 274 value != LLDB_INVALID_ADDRESS && !value_is_offset) { 275 if (!memory_module_sp) 276 memory_module_sp = ReadUnnamedMemoryModule(process, value, name); 277 if (memory_module_sp) 278 module_sp = memory_module_sp; 279 } 280 281 Log *log = GetLog(LLDBLog::DynamicLoader); 282 if (module_sp.get()) { 283 // Ensure the Target has an architecture set in case 284 // we need it while processing this binary/eh_frame/debug info. 285 if (!target.GetArchitecture().IsValid()) 286 target.SetArchitecture(module_sp->GetArchitecture()); 287 target.GetImages().AppendIfNeeded(module_sp, false); 288 289 bool changed = false; 290 if (set_address_in_target) { 291 if (module_sp->GetObjectFile()) { 292 if (value != LLDB_INVALID_ADDRESS) { 293 LLDB_LOGF(log, 294 "DynamicLoader::LoadBinaryWithUUIDAndAddress Loading " 295 "binary %s UUID %s at %s 0x%" PRIx64, 296 name.str().c_str(), uuid.GetAsString().c_str(), 297 value_is_offset ? "offset" : "address", value); 298 module_sp->SetLoadAddress(target, value, value_is_offset, changed); 299 } else { 300 // No address/offset/slide, load the binary at file address, 301 // offset 0. 302 LLDB_LOGF(log, 303 "DynamicLoader::LoadBinaryWithUUIDAndAddress Loading " 304 "binary %s UUID %s at file address", 305 name.str().c_str(), uuid.GetAsString().c_str()); 306 module_sp->SetLoadAddress(target, 0, true /* value_is_slide */, 307 changed); 308 } 309 } else { 310 // In-memory image, load at its true address, offset 0. 311 LLDB_LOGF(log, 312 "DynamicLoader::LoadBinaryWithUUIDAndAddress Loading binary " 313 "%s UUID %s from memory at address 0x%" PRIx64, 314 name.str().c_str(), uuid.GetAsString().c_str(), value); 315 module_sp->SetLoadAddress(target, 0, true /* value_is_slide */, 316 changed); 317 } 318 } 319 320 if (notify) { 321 ModuleList added_module; 322 added_module.Append(module_sp, false); 323 target.ModulesDidLoad(added_module); 324 } 325 } else { 326 if (force_symbol_search) { 327 Stream &s = target.GetDebugger().GetErrorStream(); 328 s.Printf("Unable to find file"); 329 if (!name.empty()) 330 s.Printf(" %s", name.str().c_str()); 331 if (uuid.IsValid()) 332 s.Printf(" with UUID %s", uuid.GetAsString().c_str()); 333 if (value != LLDB_INVALID_ADDRESS) { 334 if (value_is_offset) 335 s.Printf(" with slide 0x%" PRIx64, value); 336 else 337 s.Printf(" at address 0x%" PRIx64, value); 338 } 339 s.Printf("\n"); 340 } 341 LLDB_LOGF(log, 342 "Unable to find binary %s with UUID %s and load it at " 343 "%s 0x%" PRIx64, 344 name.str().c_str(), uuid.GetAsString().c_str(), 345 value_is_offset ? "offset" : "address", value); 346 } 347 348 return module_sp; 349 } 350 351 int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, 352 int size_in_bytes) { 353 Status error; 354 uint64_t value = 355 m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error); 356 if (error.Fail()) 357 return -1; 358 else 359 return (int64_t)value; 360 } 361 362 addr_t DynamicLoader::ReadPointer(addr_t addr) { 363 Status error; 364 addr_t value = m_process->ReadPointerFromMemory(addr, error); 365 if (error.Fail()) 366 return LLDB_INVALID_ADDRESS; 367 else 368 return value; 369 } 370 371 void DynamicLoader::LoadOperatingSystemPlugin(bool flush) 372 { 373 if (m_process) 374 m_process->LoadOperatingSystemPlugin(flush); 375 } 376