1 //===-- DynamicLoader.cpp ---------------------------------------*- 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 #include "lldb/Target/DynamicLoader.h" 11 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/Section.h" 17 #include "lldb/Symbol/ObjectFile.h" 18 #include "lldb/Target/MemoryRegionInfo.h" 19 #include "lldb/Target/Process.h" 20 #include "lldb/Target/Target.h" 21 #include "lldb/Utility/ConstString.h" 22 #include "lldb/lldb-private-interfaces.h" 23 24 #include "llvm/ADT/StringRef.h" 25 26 #include <memory> 27 28 #include <assert.h> 29 30 using namespace lldb; 31 using namespace lldb_private; 32 33 DynamicLoader *DynamicLoader::FindPlugin(Process *process, 34 const char *plugin_name) { 35 DynamicLoaderCreateInstance create_callback = nullptr; 36 if (plugin_name) { 37 ConstString const_plugin_name(plugin_name); 38 create_callback = 39 PluginManager::GetDynamicLoaderCreateCallbackForPluginName( 40 const_plugin_name); 41 if (create_callback) { 42 std::unique_ptr<DynamicLoader> instance_ap( 43 create_callback(process, true)); 44 if (instance_ap) 45 return instance_ap.release(); 46 } 47 } else { 48 for (uint32_t idx = 0; 49 (create_callback = 50 PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) != 51 nullptr; 52 ++idx) { 53 std::unique_ptr<DynamicLoader> instance_ap( 54 create_callback(process, false)); 55 if (instance_ap) 56 return instance_ap.release(); 57 } 58 } 59 return nullptr; 60 } 61 62 DynamicLoader::DynamicLoader(Process *process) : m_process(process) {} 63 64 DynamicLoader::~DynamicLoader() = default; 65 66 //---------------------------------------------------------------------- 67 // Accessosors to the global setting as to whether to stop at image (shared 68 // library) loading/unloading. 69 //---------------------------------------------------------------------- 70 71 bool DynamicLoader::GetStopWhenImagesChange() const { 72 return m_process->GetStopOnSharedLibraryEvents(); 73 } 74 75 void DynamicLoader::SetStopWhenImagesChange(bool stop) { 76 m_process->SetStopOnSharedLibraryEvents(stop); 77 } 78 79 ModuleSP DynamicLoader::GetTargetExecutable() { 80 Target &target = m_process->GetTarget(); 81 ModuleSP executable = target.GetExecutableModule(); 82 83 if (executable) { 84 if (FileSystem::Instance().Exists(executable->GetFileSpec())) { 85 ModuleSpec module_spec(executable->GetFileSpec(), 86 executable->GetArchitecture()); 87 auto module_sp = std::make_shared<Module>(module_spec); 88 89 // Check if the executable has changed and set it to the target 90 // executable if they differ. 91 if (module_sp && module_sp->GetUUID().IsValid() && 92 executable->GetUUID().IsValid()) { 93 if (module_sp->GetUUID() != executable->GetUUID()) 94 executable.reset(); 95 } else if (executable->FileHasChanged()) { 96 executable.reset(); 97 } 98 99 if (!executable) { 100 executable = target.GetSharedModule(module_spec); 101 if (executable.get() != target.GetExecutableModulePointer()) { 102 // Don't load dependent images since we are in dyld where we will 103 // know and find out about all images that are loaded 104 target.SetExecutableModule(executable, eLoadDependentsNo); 105 } 106 } 107 } 108 } 109 return executable; 110 } 111 112 void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, 113 addr_t base_addr, 114 bool base_addr_is_offset) { 115 UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset); 116 } 117 118 void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module, 119 addr_t base_addr, 120 bool base_addr_is_offset) { 121 bool changed; 122 module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset, 123 changed); 124 } 125 126 void DynamicLoader::UnloadSections(const ModuleSP module) { 127 UnloadSectionsCommon(module); 128 } 129 130 void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) { 131 Target &target = m_process->GetTarget(); 132 const SectionList *sections = GetSectionListFromModule(module); 133 134 assert(sections && "SectionList missing from unloaded module."); 135 136 const size_t num_sections = sections->GetSize(); 137 for (size_t i = 0; i < num_sections; ++i) { 138 SectionSP section_sp(sections->GetSectionAtIndex(i)); 139 target.SetSectionUnloaded(section_sp); 140 } 141 } 142 143 const SectionList * 144 DynamicLoader::GetSectionListFromModule(const ModuleSP module) const { 145 SectionList *sections = nullptr; 146 if (module) { 147 ObjectFile *obj_file = module->GetObjectFile(); 148 if (obj_file != nullptr) { 149 sections = obj_file->GetSectionList(); 150 } 151 } 152 return sections; 153 } 154 155 ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file, 156 addr_t link_map_addr, 157 addr_t base_addr, 158 bool base_addr_is_offset) { 159 Target &target = m_process->GetTarget(); 160 ModuleList &modules = target.GetImages(); 161 ModuleSpec module_spec(file, target.GetArchitecture()); 162 ModuleSP module_sp; 163 164 if ((module_sp = modules.FindFirstModule(module_spec))) { 165 UpdateLoadedSections(module_sp, link_map_addr, base_addr, 166 base_addr_is_offset); 167 return module_sp; 168 } 169 170 if ((module_sp = target.GetSharedModule(module_spec))) { 171 UpdateLoadedSections(module_sp, link_map_addr, base_addr, 172 base_addr_is_offset); 173 return module_sp; 174 } 175 176 bool check_alternative_file_name = true; 177 if (base_addr_is_offset) { 178 // Try to fetch the load address of the file from the process as we need 179 // absolute load address to read the file out of the memory instead of a 180 // load bias. 181 bool is_loaded = false; 182 lldb::addr_t load_addr; 183 Status error = m_process->GetFileLoadAddress(file, is_loaded, load_addr); 184 if (error.Success() && is_loaded) { 185 check_alternative_file_name = false; 186 base_addr = load_addr; 187 } 188 } 189 190 // We failed to find the module based on its name. Lets try to check if we 191 // can find a different name based on the memory region info. 192 if (check_alternative_file_name) { 193 MemoryRegionInfo memory_info; 194 Status error = m_process->GetMemoryRegionInfo(base_addr, memory_info); 195 if (error.Success() && memory_info.GetMapped() && 196 memory_info.GetRange().GetRangeBase() == base_addr && 197 !(memory_info.GetName().IsEmpty())) { 198 ModuleSpec new_module_spec(FileSpec(memory_info.GetName().AsCString()), 199 target.GetArchitecture()); 200 201 if ((module_sp = modules.FindFirstModule(new_module_spec))) { 202 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false); 203 return module_sp; 204 } 205 206 if ((module_sp = target.GetSharedModule(new_module_spec))) { 207 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false); 208 return module_sp; 209 } 210 } 211 } 212 213 if ((module_sp = m_process->ReadModuleFromMemory(file, base_addr))) { 214 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false); 215 target.GetImages().AppendIfNeeded(module_sp); 216 } 217 218 return module_sp; 219 } 220 221 int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, 222 int size_in_bytes) { 223 Status error; 224 uint64_t value = 225 m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error); 226 if (error.Fail()) 227 return -1; 228 else 229 return (int64_t)value; 230 } 231 232 addr_t DynamicLoader::ReadPointer(addr_t addr) { 233 Status error; 234 addr_t value = m_process->ReadPointerFromMemory(addr, error); 235 if (error.Fail()) 236 return LLDB_INVALID_ADDRESS; 237 else 238 return value; 239 } 240 241 void DynamicLoader::LoadOperatingSystemPlugin(bool flush) 242 { 243 if (m_process) 244 m_process->LoadOperatingSystemPlugin(flush); 245 } 246 247