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