1254721Semaste//===-- DynamicLoaderStatic.cpp ---------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/Core/Module.h"
11254721Semaste#include "lldb/Core/PluginManager.h"
12254721Semaste#include "lldb/Core/Section.h"
13254721Semaste#include "lldb/Symbol/ObjectFile.h"
14254721Semaste#include "lldb/Target/Target.h"
15254721Semaste
16254721Semaste#include "DynamicLoaderStatic.h"
17254721Semaste
18254721Semasteusing namespace lldb;
19254721Semasteusing namespace lldb_private;
20254721Semaste
21254721Semaste//----------------------------------------------------------------------
22254721Semaste// Create an instance of this class. This function is filled into
23254721Semaste// the plugin info class that gets handed out by the plugin factory and
24254721Semaste// allows the lldb to instantiate an instance of this class.
25254721Semaste//----------------------------------------------------------------------
26254721SemasteDynamicLoader *
27254721SemasteDynamicLoaderStatic::CreateInstance (Process* process, bool force)
28254721Semaste{
29254721Semaste    bool create = force;
30254721Semaste    if (!create)
31254721Semaste    {
32254721Semaste        const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
33254721Semaste        const llvm::Triple::OSType os_type = triple_ref.getOS();
34254721Semaste        if ((os_type == llvm::Triple::UnknownOS))
35254721Semaste            create = true;
36254721Semaste    }
37254721Semaste
38254721Semaste    if (!create)
39254721Semaste    {
40254721Semaste        Module *exe_module = process->GetTarget().GetExecutableModulePointer();
41254721Semaste        if (exe_module)
42254721Semaste        {
43254721Semaste            ObjectFile *object_file = exe_module->GetObjectFile();
44254721Semaste            if (object_file)
45254721Semaste            {
46254721Semaste                create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
47254721Semaste            }
48254721Semaste        }
49254721Semaste    }
50254721Semaste
51254721Semaste    if (create)
52254721Semaste        return new DynamicLoaderStatic (process);
53254721Semaste    return NULL;
54254721Semaste}
55254721Semaste
56254721Semaste//----------------------------------------------------------------------
57254721Semaste// Constructor
58254721Semaste//----------------------------------------------------------------------
59254721SemasteDynamicLoaderStatic::DynamicLoaderStatic (Process* process) :
60254721Semaste    DynamicLoader(process)
61254721Semaste{
62254721Semaste}
63254721Semaste
64254721Semaste//----------------------------------------------------------------------
65254721Semaste// Destructor
66254721Semaste//----------------------------------------------------------------------
67254721SemasteDynamicLoaderStatic::~DynamicLoaderStatic()
68254721Semaste{
69254721Semaste}
70254721Semaste
71254721Semaste//------------------------------------------------------------------
72254721Semaste/// Called after attaching a process.
73254721Semaste///
74254721Semaste/// Allow DynamicLoader plug-ins to execute some code after
75254721Semaste/// attaching to a process.
76254721Semaste//------------------------------------------------------------------
77254721Semastevoid
78254721SemasteDynamicLoaderStatic::DidAttach ()
79254721Semaste{
80254721Semaste    LoadAllImagesAtFileAddresses();
81254721Semaste}
82254721Semaste
83254721Semaste//------------------------------------------------------------------
84254721Semaste/// Called after attaching a process.
85254721Semaste///
86254721Semaste/// Allow DynamicLoader plug-ins to execute some code after
87254721Semaste/// attaching to a process.
88254721Semaste//------------------------------------------------------------------
89254721Semastevoid
90254721SemasteDynamicLoaderStatic::DidLaunch ()
91254721Semaste{
92254721Semaste    LoadAllImagesAtFileAddresses();
93254721Semaste}
94254721Semaste
95254721Semastevoid
96254721SemasteDynamicLoaderStatic::LoadAllImagesAtFileAddresses ()
97254721Semaste{
98254721Semaste    const ModuleList &module_list = m_process->GetTarget().GetImages();
99254721Semaste
100254721Semaste    ModuleList loaded_module_list;
101254721Semaste
102254721Semaste    // Disable JIT for static dynamic loader targets
103254721Semaste    m_process->SetCanJIT(false);
104254721Semaste
105254721Semaste    Mutex::Locker mutex_locker(module_list.GetMutex());
106254721Semaste
107254721Semaste    const size_t num_modules = module_list.GetSize();
108254721Semaste    for (uint32_t idx = 0; idx < num_modules; ++idx)
109254721Semaste    {
110254721Semaste        ModuleSP module_sp (module_list.GetModuleAtIndexUnlocked (idx));
111254721Semaste        if (module_sp)
112254721Semaste        {
113254721Semaste            bool changed = false;
114254721Semaste            ObjectFile *image_object_file = module_sp->GetObjectFile();
115254721Semaste            if (image_object_file)
116254721Semaste            {
117254721Semaste                SectionList *section_list = image_object_file->GetSectionList ();
118254721Semaste                if (section_list)
119254721Semaste                {
120254721Semaste                    // All sections listed in the dyld image info structure will all
121254721Semaste                    // either be fixed up already, or they will all be off by a single
122254721Semaste                    // slide amount that is determined by finding the first segment
123254721Semaste                    // that is at file offset zero which also has bytes (a file size
124254721Semaste                    // that is greater than zero) in the object file.
125254721Semaste
126254721Semaste                    // Determine the slide amount (if any)
127254721Semaste                    const size_t num_sections = section_list->GetSize();
128254721Semaste                    size_t sect_idx = 0;
129254721Semaste                    for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
130254721Semaste                    {
131254721Semaste                        // Iterate through the object file sections to find the
132254721Semaste                        // first section that starts of file offset zero and that
133254721Semaste                        // has bytes in the file...
134254721Semaste                        SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
135254721Semaste                        if (section_sp)
136254721Semaste                        {
137269024Semaste                            if (m_process->GetTarget().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress()))
138254721Semaste                                changed = true;
139254721Semaste                        }
140254721Semaste                    }
141254721Semaste                }
142254721Semaste            }
143254721Semaste
144254721Semaste            if (changed)
145254721Semaste                loaded_module_list.AppendIfNeeded (module_sp);
146254721Semaste        }
147254721Semaste    }
148254721Semaste
149254721Semaste    m_process->GetTarget().ModulesDidLoad (loaded_module_list);
150254721Semaste}
151254721Semaste
152254721SemasteThreadPlanSP
153254721SemasteDynamicLoaderStatic::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
154254721Semaste{
155254721Semaste    return ThreadPlanSP();
156254721Semaste}
157254721Semaste
158254721SemasteError
159254721SemasteDynamicLoaderStatic::CanLoadImage ()
160254721Semaste{
161254721Semaste    Error error;
162254721Semaste    error.SetErrorString ("can't load images on with a static debug session");
163254721Semaste    return error;
164254721Semaste}
165254721Semaste
166254721Semastevoid
167254721SemasteDynamicLoaderStatic::Initialize()
168254721Semaste{
169254721Semaste    PluginManager::RegisterPlugin (GetPluginNameStatic(),
170254721Semaste                                   GetPluginDescriptionStatic(),
171254721Semaste                                   CreateInstance);
172254721Semaste}
173254721Semaste
174254721Semastevoid
175254721SemasteDynamicLoaderStatic::Terminate()
176254721Semaste{
177254721Semaste    PluginManager::UnregisterPlugin (CreateInstance);
178254721Semaste}
179254721Semaste
180254721Semaste
181254721Semastelldb_private::ConstString
182254721SemasteDynamicLoaderStatic::GetPluginNameStatic()
183254721Semaste{
184254721Semaste    static ConstString g_name("static");
185254721Semaste    return g_name;
186254721Semaste}
187254721Semaste
188254721Semasteconst char *
189254721SemasteDynamicLoaderStatic::GetPluginDescriptionStatic()
190254721Semaste{
191254721Semaste    return "Dynamic loader plug-in that will load any images at the static addresses contained in each image.";
192254721Semaste}
193254721Semaste
194254721Semaste
195254721Semaste//------------------------------------------------------------------
196254721Semaste// PluginInterface protocol
197254721Semaste//------------------------------------------------------------------
198254721Semastelldb_private::ConstString
199254721SemasteDynamicLoaderStatic::GetPluginName()
200254721Semaste{
201254721Semaste    return GetPluginNameStatic();
202254721Semaste}
203254721Semaste
204254721Semasteuint32_t
205254721SemasteDynamicLoaderStatic::GetPluginVersion()
206254721Semaste{
207254721Semaste    return 1;
208254721Semaste}
209254721Semaste
210