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