aboutsummaryrefslogtreecommitdiff
path: root/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp
blob: 5498bed13ebc12269ca772db8c7886ac10a285af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//===-- ObjectFileJIT.cpp ---------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/StringRef.h"

#include "ObjectFileJIT.h"

#include "lldb/lldb-private-log.h"
#include "lldb/Core/ArchSpec.h"
#include "lldb/Core/DataBuffer.h"
#include "lldb/Core/DataBufferHeap.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/FileSpecList.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/RangeMap.h"
#include "lldb/Core/Section.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Core/Timer.h"
#include "lldb/Core/UUID.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/FileSpec.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/Target.h"

#ifndef __APPLE__
#include "Utility/UuidCompatibility.h"
#endif

using namespace lldb;
using namespace lldb_private;


void
ObjectFileJIT::Initialize()
{
    PluginManager::RegisterPlugin (GetPluginNameStatic(),
                                   GetPluginDescriptionStatic(),
                                   CreateInstance,
                                   CreateMemoryInstance,
                                   GetModuleSpecifications);
}

void
ObjectFileJIT::Terminate()
{
    PluginManager::UnregisterPlugin (CreateInstance);
}


lldb_private::ConstString
ObjectFileJIT::GetPluginNameStatic()
{
    static ConstString g_name("jit");
    return g_name;
}

const char *
ObjectFileJIT::GetPluginDescriptionStatic()
{
    return "JIT code object file";
}

ObjectFile *
ObjectFileJIT::CreateInstance (const lldb::ModuleSP &module_sp,
                                 DataBufferSP& data_sp,
                                 lldb::offset_t data_offset,
                                 const FileSpec* file,
                                 lldb::offset_t file_offset,
                                 lldb::offset_t length)
{
    // JIT'ed object file is backed by the ObjectFileJITDelegate, never
    // read from a file
    return NULL;
}

ObjectFile *
ObjectFileJIT::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
                                     DataBufferSP& data_sp,
                                     const ProcessSP &process_sp,
                                     lldb::addr_t header_addr)
{
    // JIT'ed object file is backed by the ObjectFileJITDelegate, never
    // read from memory
    return NULL;
}

size_t
ObjectFileJIT::GetModuleSpecifications (const lldb_private::FileSpec& file,
                                        lldb::DataBufferSP& data_sp,
                                        lldb::offset_t data_offset,
                                        lldb::offset_t file_offset,
                                        lldb::offset_t length,
                                        lldb_private::ModuleSpecList &specs)
{
    // JIT'ed object file can't be read from a file on disk
    return 0;
}

ObjectFileJIT::ObjectFileJIT (const lldb::ModuleSP &module_sp,
                              const ObjectFileJITDelegateSP &delegate_sp) :
    ObjectFile(module_sp, NULL, 0, 0, DataBufferSP(), 0),
    m_delegate_wp ()
{
    if (delegate_sp)
    {
        m_delegate_wp = delegate_sp;
        m_data.SetByteOrder(delegate_sp->GetByteOrder());
        m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
    }
}

ObjectFileJIT::~ObjectFileJIT()
{
}


bool
ObjectFileJIT::ParseHeader ()
{
    // JIT code is never in a file, nor is it required to have any header
    return false;
}

ByteOrder
ObjectFileJIT::GetByteOrder () const
{
    return m_data.GetByteOrder();
}

bool
ObjectFileJIT::IsExecutable() const
{
    return false;
}

uint32_t
ObjectFileJIT::GetAddressByteSize () const
{
    return m_data.GetAddressByteSize();
}


Symtab *
ObjectFileJIT::GetSymtab()
{
    ModuleSP module_sp(GetModule());
    if (module_sp)
    {
        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
        if (m_symtab_ap.get() == NULL)
        {
            m_symtab_ap.reset(new Symtab(this));
            Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
            ObjectFileJITDelegateSP delegate_sp (m_delegate_wp.lock());
            if (delegate_sp)
                delegate_sp->PopulateSymtab(this, *m_symtab_ap);
            // TODO: get symbols from delegate
            m_symtab_ap->Finalize ();
        }
    }
    return m_symtab_ap.get();
}

bool
ObjectFileJIT::IsStripped ()
{
    return false; // JIT code that is in a module is never stripped
}

void
ObjectFileJIT::CreateSections (SectionList &unified_section_list)
{
    if (!m_sections_ap.get())
    {
        m_sections_ap.reset(new SectionList());
        ObjectFileJITDelegateSP delegate_sp (m_delegate_wp.lock());
        if (delegate_sp)
        {
            delegate_sp->PopulateSectionList(this, *m_sections_ap);
            unified_section_list = *m_sections_ap;
        }
    }
}

void
ObjectFileJIT::Dump (Stream *s)
{
    ModuleSP module_sp(GetModule());
    if (module_sp)
    {
        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
        s->Printf("%p: ", static_cast<void*>(this));
        s->Indent();
        s->PutCString("ObjectFileJIT");

        ArchSpec arch;
        if (GetArchitecture(arch))
            *s << ", arch = " << arch.GetArchitectureName();

        s->EOL();

        SectionList *sections = GetSectionList();
        if (sections)
            sections->Dump(s, NULL, true, UINT32_MAX);

        if (m_symtab_ap.get())
            m_symtab_ap->Dump(s, NULL, eSortOrderNone);
    }
}

bool
ObjectFileJIT::GetUUID (lldb_private::UUID* uuid)
{
    // TODO: maybe get from delegate, not needed for first pass
    return false;
}


uint32_t
ObjectFileJIT::GetDependentModules (FileSpecList& files)
{
    // JIT modules don't have dependencies, but they could
    // if external functions are called and we know where they are
    files.Clear();
    return 0;
}

lldb_private::Address
ObjectFileJIT::GetEntryPointAddress ()
{
    return Address();
}

lldb_private::Address
ObjectFileJIT::GetHeaderAddress ()
{
    return Address();
}



ObjectFile::Type
ObjectFileJIT::CalculateType()
{
    return eTypeJIT;
}

ObjectFile::Strata
ObjectFileJIT::CalculateStrata()
{
    return eStrataJIT;
}


bool
ObjectFileJIT::GetArchitecture (ArchSpec &arch)
{
    ObjectFileJITDelegateSP delegate_sp (m_delegate_wp.lock());
    if (delegate_sp)
        return delegate_sp->GetArchitecture(arch);
    return false;
}

//------------------------------------------------------------------
// PluginInterface protocol
//------------------------------------------------------------------
lldb_private::ConstString
ObjectFileJIT::GetPluginName()
{
    return GetPluginNameStatic();
}

uint32_t
ObjectFileJIT::GetPluginVersion()
{
    return 1;
}


bool
ObjectFileJIT::SetLoadAddress (Target &target,
                               lldb::addr_t value,
                               bool value_is_offset)
{
    size_t num_loaded_sections = 0;
    SectionList *section_list = GetSectionList ();
    if (section_list)
    {
        const size_t num_sections = section_list->GetSize();
        // "value" is an offset to apply to each top level segment
        for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
        {
            // Iterate through the object file sections to find all
            // of the sections that size on disk (to avoid __PAGEZERO)
            // and load them
            SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
            if (section_sp &&
                section_sp->GetFileSize() > 0 &&
                section_sp->IsThreadSpecific() == false)
            {
                if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
                    ++num_loaded_sections;
            }
        }
    }
    return num_loaded_sections > 0;
}

    
size_t
ObjectFileJIT::ReadSectionData (const lldb_private::Section *section,
                                lldb::offset_t section_offset,
                                void *dst,
                                size_t dst_len) const
{
    lldb::offset_t file_size = section->GetFileSize();
    if (section_offset < file_size)
    {
        size_t src_len = file_size - section_offset;
        if (src_len > dst_len)
            src_len = dst_len;
        const uint8_t *src = ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
        
        memcpy (dst, src, src_len);
        return src_len;
    }
    return 0;
}

size_t
ObjectFileJIT::ReadSectionData (const lldb_private::Section *section,
                                lldb_private::DataExtractor& section_data) const
{
    if (section->GetFileSize())
    {
        const void *src = (void *)(uintptr_t)section->GetFileOffset();
    
        DataBufferSP data_sp (new lldb_private::DataBufferHeap(src, section->GetFileSize()));
        if (data_sp)
        {
            section_data.SetData (data_sp, 0, data_sp->GetByteSize());
            section_data.SetByteOrder (GetByteOrder());
            section_data.SetAddressByteSize (GetAddressByteSize());
            return section_data.GetByteSize();
        }
    }
    section_data.Clear();
    return 0;
}