1 //===-- DynamicLoaderStatic.cpp ---------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
15 #include "DynamicLoaderStatic.h"
18 using namespace lldb_private;
20 // Create an instance of this class. This function is filled into the plugin
21 // info class that gets handed out by the plugin factory and allows the lldb to
22 // instantiate an instance of this class.
23 DynamicLoader *DynamicLoaderStatic::CreateInstance(Process *process,
27 const llvm::Triple &triple_ref =
28 process->GetTarget().GetArchitecture().GetTriple();
29 const llvm::Triple::OSType os_type = triple_ref.getOS();
30 if ((os_type == llvm::Triple::UnknownOS))
35 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
37 ObjectFile *object_file = exe_module->GetObjectFile();
39 create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
45 return new DynamicLoaderStatic(process);
50 DynamicLoaderStatic::DynamicLoaderStatic(Process *process)
51 : DynamicLoader(process) {}
54 DynamicLoaderStatic::~DynamicLoaderStatic() {}
56 /// Called after attaching a process.
58 /// Allow DynamicLoader plug-ins to execute some code after
59 /// attaching to a process.
60 void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }
62 /// Called after attaching a process.
64 /// Allow DynamicLoader plug-ins to execute some code after
65 /// attaching to a process.
66 void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
68 void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {
69 const ModuleList &module_list = m_process->GetTarget().GetImages();
71 ModuleList loaded_module_list;
73 // Disable JIT for static dynamic loader targets
74 m_process->SetCanJIT(false);
76 std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
78 const size_t num_modules = module_list.GetSize();
79 for (uint32_t idx = 0; idx < num_modules; ++idx) {
80 ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(idx));
83 ObjectFile *image_object_file = module_sp->GetObjectFile();
84 if (image_object_file) {
85 SectionList *section_list = image_object_file->GetSectionList();
87 // All sections listed in the dyld image info structure will all
88 // either be fixed up already, or they will all be off by a single
89 // slide amount that is determined by finding the first segment that
90 // is at file offset zero which also has bytes (a file size that is
91 // greater than zero) in the object file.
93 // Determine the slide amount (if any)
94 const size_t num_sections = section_list->GetSize();
96 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
97 // Iterate through the object file sections to find the first
98 // section that starts of file offset zero and that has bytes in
100 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
102 if (m_process->GetTarget().SetSectionLoadAddress(
103 section_sp, section_sp->GetFileAddress()))
111 loaded_module_list.AppendIfNeeded(module_sp);
115 m_process->GetTarget().ModulesDidLoad(loaded_module_list);
119 DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
121 return ThreadPlanSP();
124 Status DynamicLoaderStatic::CanLoadImage() {
126 error.SetErrorString("can't load images on with a static debug session");
130 void DynamicLoaderStatic::Initialize() {
131 PluginManager::RegisterPlugin(GetPluginNameStatic(),
132 GetPluginDescriptionStatic(), CreateInstance);
135 void DynamicLoaderStatic::Terminate() {
136 PluginManager::UnregisterPlugin(CreateInstance);
139 lldb_private::ConstString DynamicLoaderStatic::GetPluginNameStatic() {
140 static ConstString g_name("static");
144 const char *DynamicLoaderStatic::GetPluginDescriptionStatic() {
145 return "Dynamic loader plug-in that will load any images at the static "
146 "addresses contained in each image.";
149 // PluginInterface protocol
150 lldb_private::ConstString DynamicLoaderStatic::GetPluginName() {
151 return GetPluginNameStatic();
154 uint32_t DynamicLoaderStatic::GetPluginVersion() { return 1; }