1 //===-- DynamicLoaderStatic.cpp ---------------------------------*- C++ -*-===// 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/Core/Module.h" 10 #include "lldb/Core/PluginManager.h" 11 #include "lldb/Core/Section.h" 12 #include "lldb/Symbol/ObjectFile.h" 13 #include "lldb/Target/Target.h" 14 15 #include "DynamicLoaderStatic.h" 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 //---------------------------------------------------------------------- 21 // Create an instance of this class. This function is filled into the plugin 22 // info class that gets handed out by the plugin factory and allows the lldb to 23 // instantiate an instance of this class. 24 //---------------------------------------------------------------------- 25 DynamicLoader *DynamicLoaderStatic::CreateInstance(Process *process, 26 bool force) { 27 bool create = force; 28 if (!create) { 29 const llvm::Triple &triple_ref = 30 process->GetTarget().GetArchitecture().GetTriple(); 31 const llvm::Triple::OSType os_type = triple_ref.getOS(); 32 if ((os_type == llvm::Triple::UnknownOS)) 33 create = true; 34 } 35 36 if (!create) { 37 Module *exe_module = process->GetTarget().GetExecutableModulePointer(); 38 if (exe_module) { 39 ObjectFile *object_file = exe_module->GetObjectFile(); 40 if (object_file) { 41 create = (object_file->GetStrata() == ObjectFile::eStrataRawImage); 42 } 43 } 44 } 45 46 if (create) 47 return new DynamicLoaderStatic(process); 48 return NULL; 49 } 50 51 //---------------------------------------------------------------------- 52 // Constructor 53 //---------------------------------------------------------------------- 54 DynamicLoaderStatic::DynamicLoaderStatic(Process *process) 55 : DynamicLoader(process) {} 56 57 //---------------------------------------------------------------------- 58 // Destructor 59 //---------------------------------------------------------------------- 60 DynamicLoaderStatic::~DynamicLoaderStatic() {} 61 62 //------------------------------------------------------------------ 63 /// Called after attaching a process. 64 /// 65 /// Allow DynamicLoader plug-ins to execute some code after 66 /// attaching to a process. 67 //------------------------------------------------------------------ 68 void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); } 69 70 //------------------------------------------------------------------ 71 /// Called after attaching a process. 72 /// 73 /// Allow DynamicLoader plug-ins to execute some code after 74 /// attaching to a process. 75 //------------------------------------------------------------------ 76 void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); } 77 78 void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() { 79 const ModuleList &module_list = m_process->GetTarget().GetImages(); 80 81 ModuleList loaded_module_list; 82 83 // Disable JIT for static dynamic loader targets 84 m_process->SetCanJIT(false); 85 86 std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex()); 87 88 const size_t num_modules = module_list.GetSize(); 89 for (uint32_t idx = 0; idx < num_modules; ++idx) { 90 ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(idx)); 91 if (module_sp) { 92 bool changed = false; 93 ObjectFile *image_object_file = module_sp->GetObjectFile(); 94 if (image_object_file) { 95 SectionList *section_list = image_object_file->GetSectionList(); 96 if (section_list) { 97 // All sections listed in the dyld image info structure will all 98 // either be fixed up already, or they will all be off by a single 99 // slide amount that is determined by finding the first segment that 100 // is at file offset zero which also has bytes (a file size that is 101 // greater than zero) in the object file. 102 103 // Determine the slide amount (if any) 104 const size_t num_sections = section_list->GetSize(); 105 size_t sect_idx = 0; 106 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 107 // Iterate through the object file sections to find the first 108 // section that starts of file offset zero and that has bytes in 109 // the file... 110 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 111 if (section_sp) { 112 if (m_process->GetTarget().SetSectionLoadAddress( 113 section_sp, section_sp->GetFileAddress())) 114 changed = true; 115 } 116 } 117 } 118 } 119 120 if (changed) 121 loaded_module_list.AppendIfNeeded(module_sp); 122 } 123 } 124 125 m_process->GetTarget().ModulesDidLoad(loaded_module_list); 126 } 127 128 ThreadPlanSP 129 DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread, 130 bool stop_others) { 131 return ThreadPlanSP(); 132 } 133 134 Status DynamicLoaderStatic::CanLoadImage() { 135 Status error; 136 error.SetErrorString("can't load images on with a static debug session"); 137 return error; 138 } 139 140 void DynamicLoaderStatic::Initialize() { 141 PluginManager::RegisterPlugin(GetPluginNameStatic(), 142 GetPluginDescriptionStatic(), CreateInstance); 143 } 144 145 void DynamicLoaderStatic::Terminate() { 146 PluginManager::UnregisterPlugin(CreateInstance); 147 } 148 149 lldb_private::ConstString DynamicLoaderStatic::GetPluginNameStatic() { 150 static ConstString g_name("static"); 151 return g_name; 152 } 153 154 const char *DynamicLoaderStatic::GetPluginDescriptionStatic() { 155 return "Dynamic loader plug-in that will load any images at the static " 156 "addresses contained in each image."; 157 } 158 159 //------------------------------------------------------------------ 160 // PluginInterface protocol 161 //------------------------------------------------------------------ 162 lldb_private::ConstString DynamicLoaderStatic::GetPluginName() { 163 return GetPluginNameStatic(); 164 } 165 166 uint32_t DynamicLoaderStatic::GetPluginVersion() { return 1; } 167