1 //===-- DynamicLoaderStatic.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
16 #include "DynamicLoaderStatic.h"
19 using namespace lldb_private;
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,
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))
38 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
40 ObjectFile *object_file = exe_module->GetObjectFile();
42 create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
48 return new DynamicLoaderStatic(process);
52 //----------------------------------------------------------------------
54 //----------------------------------------------------------------------
55 DynamicLoaderStatic::DynamicLoaderStatic(Process *process)
56 : DynamicLoader(process) {}
58 //----------------------------------------------------------------------
60 //----------------------------------------------------------------------
61 DynamicLoaderStatic::~DynamicLoaderStatic() {}
63 //------------------------------------------------------------------
64 /// Called after attaching a process.
66 /// Allow DynamicLoader plug-ins to execute some code after
67 /// attaching to a process.
68 //------------------------------------------------------------------
69 void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }
71 //------------------------------------------------------------------
72 /// Called after attaching a process.
74 /// Allow DynamicLoader plug-ins to execute some code after
75 /// attaching to a process.
76 //------------------------------------------------------------------
77 void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
79 void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {
80 const ModuleList &module_list = m_process->GetTarget().GetImages();
82 ModuleList loaded_module_list;
84 // Disable JIT for static dynamic loader targets
85 m_process->SetCanJIT(false);
87 std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
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));
94 ObjectFile *image_object_file = module_sp->GetObjectFile();
95 if (image_object_file) {
96 SectionList *section_list = image_object_file->GetSectionList();
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.
104 // Determine the slide amount (if any)
105 const size_t num_sections = section_list->GetSize();
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));
113 if (m_process->GetTarget().SetSectionLoadAddress(
114 section_sp, section_sp->GetFileAddress()))
122 loaded_module_list.AppendIfNeeded(module_sp);
126 m_process->GetTarget().ModulesDidLoad(loaded_module_list);
130 DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
132 return ThreadPlanSP();
135 Error DynamicLoaderStatic::CanLoadImage() {
137 error.SetErrorString("can't load images on with a static debug session");
141 void DynamicLoaderStatic::Initialize() {
142 PluginManager::RegisterPlugin(GetPluginNameStatic(),
143 GetPluginDescriptionStatic(), CreateInstance);
146 void DynamicLoaderStatic::Terminate() {
147 PluginManager::UnregisterPlugin(CreateInstance);
150 lldb_private::ConstString DynamicLoaderStatic::GetPluginNameStatic() {
151 static ConstString g_name("static");
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.";
160 //------------------------------------------------------------------
161 // PluginInterface protocol
162 //------------------------------------------------------------------
163 lldb_private::ConstString DynamicLoaderStatic::GetPluginName() {
164 return GetPluginNameStatic();
167 uint32_t DynamicLoaderStatic::GetPluginVersion() { return 1; }