1fc7117aeSGreg Clayton //===-- DynamicLoaderStatic.cpp ---------------------------------*- C++ -*-===//
2fc7117aeSGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fc7117aeSGreg Clayton //
7fc7117aeSGreg Clayton //===----------------------------------------------------------------------===//
8fc7117aeSGreg Clayton 
9fc7117aeSGreg Clayton #include "lldb/Core/Module.h"
10fc7117aeSGreg Clayton #include "lldb/Core/PluginManager.h"
111f746071SGreg Clayton #include "lldb/Core/Section.h"
121f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
13fc7117aeSGreg Clayton #include "lldb/Target/Target.h"
14fc7117aeSGreg Clayton 
15fc7117aeSGreg Clayton #include "DynamicLoaderStatic.h"
16fc7117aeSGreg Clayton 
17fc7117aeSGreg Clayton using namespace lldb;
18fc7117aeSGreg Clayton using namespace lldb_private;
19fc7117aeSGreg Clayton 
2005097246SAdrian Prantl // Create an instance of this class. This function is filled into the plugin
2105097246SAdrian Prantl // info class that gets handed out by the plugin factory and allows the lldb to
2205097246SAdrian Prantl // instantiate an instance of this class.
23b9c1b51eSKate Stone DynamicLoader *DynamicLoaderStatic::CreateInstance(Process *process,
24b9c1b51eSKate Stone                                                    bool force) {
25fc7117aeSGreg Clayton   bool create = force;
26b9c1b51eSKate Stone   if (!create) {
27b9c1b51eSKate Stone     const llvm::Triple &triple_ref =
28b9c1b51eSKate Stone         process->GetTarget().GetArchitecture().GetTriple();
29fc7117aeSGreg Clayton     const llvm::Triple::OSType os_type = triple_ref.getOS();
30fb0b7583SSean Callanan     if ((os_type == llvm::Triple::UnknownOS))
31fc7117aeSGreg Clayton       create = true;
32fc7117aeSGreg Clayton   }
33fc7117aeSGreg Clayton 
34b9c1b51eSKate Stone   if (!create) {
3549bce8ecSSean Callanan     Module *exe_module = process->GetTarget().GetExecutableModulePointer();
36b9c1b51eSKate Stone     if (exe_module) {
3749bce8ecSSean Callanan       ObjectFile *object_file = exe_module->GetObjectFile();
38b9c1b51eSKate Stone       if (object_file) {
3949bce8ecSSean Callanan         create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
4049bce8ecSSean Callanan       }
4149bce8ecSSean Callanan     }
4249bce8ecSSean Callanan   }
4349bce8ecSSean Callanan 
44fc7117aeSGreg Clayton   if (create)
45fc7117aeSGreg Clayton     return new DynamicLoaderStatic(process);
46*248a1305SKonrad Kleine   return nullptr;
47fc7117aeSGreg Clayton }
48fc7117aeSGreg Clayton 
49fc7117aeSGreg Clayton // Constructor
50b9c1b51eSKate Stone DynamicLoaderStatic::DynamicLoaderStatic(Process *process)
51b9c1b51eSKate Stone     : DynamicLoader(process) {}
52fc7117aeSGreg Clayton 
53fc7117aeSGreg Clayton // Destructor
54b9c1b51eSKate Stone DynamicLoaderStatic::~DynamicLoaderStatic() {}
55fc7117aeSGreg Clayton 
56fc7117aeSGreg Clayton /// Called after attaching a process.
57fc7117aeSGreg Clayton ///
58fc7117aeSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
59fc7117aeSGreg Clayton /// attaching to a process.
60b9c1b51eSKate Stone void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }
61fc7117aeSGreg Clayton 
62fc7117aeSGreg Clayton /// Called after attaching a process.
63fc7117aeSGreg Clayton ///
64fc7117aeSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
65fc7117aeSGreg Clayton /// attaching to a process.
66b9c1b51eSKate Stone void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
67fc7117aeSGreg Clayton 
68b9c1b51eSKate Stone void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {
691759848bSEnrico Granata   const ModuleList &module_list = m_process->GetTarget().GetImages();
70fc7117aeSGreg Clayton 
71fc7117aeSGreg Clayton   ModuleList loaded_module_list;
72fc7117aeSGreg Clayton 
7362243f84SGreg Clayton   // Disable JIT for static dynamic loader targets
7462243f84SGreg Clayton   m_process->SetCanJIT(false);
7562243f84SGreg Clayton 
76bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
773ee12ef2SJim Ingham 
78fc7117aeSGreg Clayton   const size_t num_modules = module_list.GetSize();
79b9c1b51eSKate Stone   for (uint32_t idx = 0; idx < num_modules; ++idx) {
803ee12ef2SJim Ingham     ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(idx));
81b9c1b51eSKate Stone     if (module_sp) {
82fc7117aeSGreg Clayton       bool changed = false;
83fc7117aeSGreg Clayton       ObjectFile *image_object_file = module_sp->GetObjectFile();
84b9c1b51eSKate Stone       if (image_object_file) {
85fc7117aeSGreg Clayton         SectionList *section_list = image_object_file->GetSectionList();
86b9c1b51eSKate Stone         if (section_list) {
87fc7117aeSGreg Clayton           // All sections listed in the dyld image info structure will all
88fc7117aeSGreg Clayton           // either be fixed up already, or they will all be off by a single
8905097246SAdrian Prantl           // slide amount that is determined by finding the first segment that
9005097246SAdrian Prantl           // is at file offset zero which also has bytes (a file size that is
9105097246SAdrian Prantl           // greater than zero) in the object file.
92fc7117aeSGreg Clayton 
93fc7117aeSGreg Clayton           // Determine the slide amount (if any)
94fc7117aeSGreg Clayton           const size_t num_sections = section_list->GetSize();
95fc7117aeSGreg Clayton           size_t sect_idx = 0;
96b9c1b51eSKate Stone           for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
9705097246SAdrian Prantl             // Iterate through the object file sections to find the first
9805097246SAdrian Prantl             // section that starts of file offset zero and that has bytes in
9905097246SAdrian Prantl             // the file...
1007820bd1eSGreg Clayton             SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
101b9c1b51eSKate Stone             if (section_sp) {
102b9c1b51eSKate Stone               if (m_process->GetTarget().SetSectionLoadAddress(
103b9c1b51eSKate Stone                       section_sp, section_sp->GetFileAddress()))
104fc7117aeSGreg Clayton                 changed = true;
105fc7117aeSGreg Clayton             }
106fc7117aeSGreg Clayton           }
107fc7117aeSGreg Clayton         }
108fc7117aeSGreg Clayton       }
109fc7117aeSGreg Clayton 
110fc7117aeSGreg Clayton       if (changed)
111fc7117aeSGreg Clayton         loaded_module_list.AppendIfNeeded(module_sp);
112fc7117aeSGreg Clayton     }
113fc7117aeSGreg Clayton   }
114fc7117aeSGreg Clayton 
115fc7117aeSGreg Clayton   m_process->GetTarget().ModulesDidLoad(loaded_module_list);
116fc7117aeSGreg Clayton }
117fc7117aeSGreg Clayton 
118fc7117aeSGreg Clayton ThreadPlanSP
119b9c1b51eSKate Stone DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
120b9c1b51eSKate Stone                                                   bool stop_others) {
121fc7117aeSGreg Clayton   return ThreadPlanSP();
122fc7117aeSGreg Clayton }
123fc7117aeSGreg Clayton 
12497206d57SZachary Turner Status DynamicLoaderStatic::CanLoadImage() {
12597206d57SZachary Turner   Status error;
126fc7117aeSGreg Clayton   error.SetErrorString("can't load images on with a static debug session");
127fc7117aeSGreg Clayton   return error;
128fc7117aeSGreg Clayton }
129fc7117aeSGreg Clayton 
130b9c1b51eSKate Stone void DynamicLoaderStatic::Initialize() {
131fc7117aeSGreg Clayton   PluginManager::RegisterPlugin(GetPluginNameStatic(),
132b9c1b51eSKate Stone                                 GetPluginDescriptionStatic(), CreateInstance);
133fc7117aeSGreg Clayton }
134fc7117aeSGreg Clayton 
135b9c1b51eSKate Stone void DynamicLoaderStatic::Terminate() {
136fc7117aeSGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
137fc7117aeSGreg Clayton }
138fc7117aeSGreg Clayton 
139b9c1b51eSKate Stone lldb_private::ConstString DynamicLoaderStatic::GetPluginNameStatic() {
14057abc5d6SGreg Clayton   static ConstString g_name("static");
14157abc5d6SGreg Clayton   return g_name;
142fc7117aeSGreg Clayton }
143fc7117aeSGreg Clayton 
144b9c1b51eSKate Stone const char *DynamicLoaderStatic::GetPluginDescriptionStatic() {
145b9c1b51eSKate Stone   return "Dynamic loader plug-in that will load any images at the static "
146b9c1b51eSKate Stone          "addresses contained in each image.";
147fc7117aeSGreg Clayton }
148fc7117aeSGreg Clayton 
149fc7117aeSGreg Clayton // PluginInterface protocol
150b9c1b51eSKate Stone lldb_private::ConstString DynamicLoaderStatic::GetPluginName() {
151fc7117aeSGreg Clayton   return GetPluginNameStatic();
152fc7117aeSGreg Clayton }
153fc7117aeSGreg Clayton 
154b9c1b51eSKate Stone uint32_t DynamicLoaderStatic::GetPluginVersion() { return 1; }
155