123f8c95aSGreg Clayton //===-- ObjectFileJIT.cpp ---------------------------------------*- C++ -*-===//
223f8c95aSGreg Clayton //
323f8c95aSGreg Clayton //                     The LLVM Compiler Infrastructure
423f8c95aSGreg Clayton //
523f8c95aSGreg Clayton // This file is distributed under the University of Illinois Open Source
623f8c95aSGreg Clayton // License. See LICENSE.TXT for details.
723f8c95aSGreg Clayton //
823f8c95aSGreg Clayton //===----------------------------------------------------------------------===//
923f8c95aSGreg Clayton 
1023f8c95aSGreg Clayton #include "llvm/ADT/StringRef.h"
1123f8c95aSGreg Clayton 
1223f8c95aSGreg Clayton #include "ObjectFileJIT.h"
1323f8c95aSGreg Clayton 
1423f8c95aSGreg Clayton #include "lldb/Core/ArchSpec.h"
1523f8c95aSGreg Clayton #include "lldb/Core/DataBuffer.h"
1623f8c95aSGreg Clayton #include "lldb/Core/DataBufferHeap.h"
1723f8c95aSGreg Clayton #include "lldb/Core/Debugger.h"
1823f8c95aSGreg Clayton #include "lldb/Core/FileSpecList.h"
1923f8c95aSGreg Clayton #include "lldb/Core/Log.h"
2023f8c95aSGreg Clayton #include "lldb/Core/Module.h"
2123f8c95aSGreg Clayton #include "lldb/Core/ModuleSpec.h"
2223f8c95aSGreg Clayton #include "lldb/Core/PluginManager.h"
2323f8c95aSGreg Clayton #include "lldb/Core/RangeMap.h"
2423f8c95aSGreg Clayton #include "lldb/Core/Section.h"
2523f8c95aSGreg Clayton #include "lldb/Core/StreamFile.h"
2623f8c95aSGreg Clayton #include "lldb/Core/StreamString.h"
2723f8c95aSGreg Clayton #include "lldb/Core/Timer.h"
2823f8c95aSGreg Clayton #include "lldb/Core/UUID.h"
2923f8c95aSGreg Clayton #include "lldb/Host/Host.h"
3023f8c95aSGreg Clayton #include "lldb/Host/FileSpec.h"
3123f8c95aSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
3223f8c95aSGreg Clayton #include "lldb/Target/Platform.h"
3323f8c95aSGreg Clayton #include "lldb/Target/Process.h"
3423f8c95aSGreg Clayton #include "lldb/Target/SectionLoadList.h"
3523f8c95aSGreg Clayton #include "lldb/Target/Target.h"
3623f8c95aSGreg Clayton 
3723f8c95aSGreg Clayton #ifndef __APPLE__
3823f8c95aSGreg Clayton #include "Utility/UuidCompatibility.h"
3923f8c95aSGreg Clayton #endif
4023f8c95aSGreg Clayton 
4123f8c95aSGreg Clayton using namespace lldb;
4223f8c95aSGreg Clayton using namespace lldb_private;
4323f8c95aSGreg Clayton 
4423f8c95aSGreg Clayton 
4523f8c95aSGreg Clayton void
4623f8c95aSGreg Clayton ObjectFileJIT::Initialize()
4723f8c95aSGreg Clayton {
4823f8c95aSGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
4923f8c95aSGreg Clayton                                    GetPluginDescriptionStatic(),
5023f8c95aSGreg Clayton                                    CreateInstance,
5123f8c95aSGreg Clayton                                    CreateMemoryInstance,
5223f8c95aSGreg Clayton                                    GetModuleSpecifications);
5323f8c95aSGreg Clayton }
5423f8c95aSGreg Clayton 
5523f8c95aSGreg Clayton void
5623f8c95aSGreg Clayton ObjectFileJIT::Terminate()
5723f8c95aSGreg Clayton {
5823f8c95aSGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
5923f8c95aSGreg Clayton }
6023f8c95aSGreg Clayton 
6123f8c95aSGreg Clayton 
6223f8c95aSGreg Clayton lldb_private::ConstString
6323f8c95aSGreg Clayton ObjectFileJIT::GetPluginNameStatic()
6423f8c95aSGreg Clayton {
6523f8c95aSGreg Clayton     static ConstString g_name("jit");
6623f8c95aSGreg Clayton     return g_name;
6723f8c95aSGreg Clayton }
6823f8c95aSGreg Clayton 
6923f8c95aSGreg Clayton const char *
7023f8c95aSGreg Clayton ObjectFileJIT::GetPluginDescriptionStatic()
7123f8c95aSGreg Clayton {
7223f8c95aSGreg Clayton     return "JIT code object file";
7323f8c95aSGreg Clayton }
7423f8c95aSGreg Clayton 
7523f8c95aSGreg Clayton ObjectFile *
7623f8c95aSGreg Clayton ObjectFileJIT::CreateInstance (const lldb::ModuleSP &module_sp,
7723f8c95aSGreg Clayton                                  DataBufferSP& data_sp,
7823f8c95aSGreg Clayton                                  lldb::offset_t data_offset,
7923f8c95aSGreg Clayton                                  const FileSpec* file,
8023f8c95aSGreg Clayton                                  lldb::offset_t file_offset,
8123f8c95aSGreg Clayton                                  lldb::offset_t length)
8223f8c95aSGreg Clayton {
8323f8c95aSGreg Clayton     // JIT'ed object file is backed by the ObjectFileJITDelegate, never
8423f8c95aSGreg Clayton     // read from a file
8523f8c95aSGreg Clayton     return NULL;
8623f8c95aSGreg Clayton }
8723f8c95aSGreg Clayton 
8823f8c95aSGreg Clayton ObjectFile *
8923f8c95aSGreg Clayton ObjectFileJIT::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
9023f8c95aSGreg Clayton                                      DataBufferSP& data_sp,
9123f8c95aSGreg Clayton                                      const ProcessSP &process_sp,
9223f8c95aSGreg Clayton                                      lldb::addr_t header_addr)
9323f8c95aSGreg Clayton {
9423f8c95aSGreg Clayton     // JIT'ed object file is backed by the ObjectFileJITDelegate, never
9523f8c95aSGreg Clayton     // read from memory
9623f8c95aSGreg Clayton     return NULL;
9723f8c95aSGreg Clayton }
9823f8c95aSGreg Clayton 
9923f8c95aSGreg Clayton size_t
10023f8c95aSGreg Clayton ObjectFileJIT::GetModuleSpecifications (const lldb_private::FileSpec& file,
10123f8c95aSGreg Clayton                                         lldb::DataBufferSP& data_sp,
10223f8c95aSGreg Clayton                                         lldb::offset_t data_offset,
10323f8c95aSGreg Clayton                                         lldb::offset_t file_offset,
10423f8c95aSGreg Clayton                                         lldb::offset_t length,
10523f8c95aSGreg Clayton                                         lldb_private::ModuleSpecList &specs)
10623f8c95aSGreg Clayton {
10723f8c95aSGreg Clayton     // JIT'ed object file can't be read from a file on disk
10823f8c95aSGreg Clayton     return 0;
10923f8c95aSGreg Clayton }
11023f8c95aSGreg Clayton 
11123f8c95aSGreg Clayton ObjectFileJIT::ObjectFileJIT (const lldb::ModuleSP &module_sp,
11223f8c95aSGreg Clayton                               const ObjectFileJITDelegateSP &delegate_sp) :
11323f8c95aSGreg Clayton     ObjectFile(module_sp, NULL, 0, 0, DataBufferSP(), 0),
11423f8c95aSGreg Clayton     m_delegate_wp ()
11523f8c95aSGreg Clayton {
11623f8c95aSGreg Clayton     if (delegate_sp)
11723f8c95aSGreg Clayton     {
11823f8c95aSGreg Clayton         m_delegate_wp = delegate_sp;
11923f8c95aSGreg Clayton         m_data.SetByteOrder(delegate_sp->GetByteOrder());
12023f8c95aSGreg Clayton         m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
12123f8c95aSGreg Clayton     }
12223f8c95aSGreg Clayton }
12323f8c95aSGreg Clayton 
12423f8c95aSGreg Clayton ObjectFileJIT::~ObjectFileJIT()
12523f8c95aSGreg Clayton {
12623f8c95aSGreg Clayton }
12723f8c95aSGreg Clayton 
12823f8c95aSGreg Clayton 
12923f8c95aSGreg Clayton bool
13023f8c95aSGreg Clayton ObjectFileJIT::ParseHeader ()
13123f8c95aSGreg Clayton {
13223f8c95aSGreg Clayton     // JIT code is never in a file, nor is it required to have any header
13323f8c95aSGreg Clayton     return false;
13423f8c95aSGreg Clayton }
13523f8c95aSGreg Clayton 
13623f8c95aSGreg Clayton ByteOrder
13723f8c95aSGreg Clayton ObjectFileJIT::GetByteOrder () const
13823f8c95aSGreg Clayton {
13923f8c95aSGreg Clayton     return m_data.GetByteOrder();
14023f8c95aSGreg Clayton }
14123f8c95aSGreg Clayton 
14223f8c95aSGreg Clayton bool
14323f8c95aSGreg Clayton ObjectFileJIT::IsExecutable() const
14423f8c95aSGreg Clayton {
14523f8c95aSGreg Clayton     return false;
14623f8c95aSGreg Clayton }
14723f8c95aSGreg Clayton 
14823f8c95aSGreg Clayton uint32_t
14923f8c95aSGreg Clayton ObjectFileJIT::GetAddressByteSize () const
15023f8c95aSGreg Clayton {
15123f8c95aSGreg Clayton     return m_data.GetAddressByteSize();
15223f8c95aSGreg Clayton }
15323f8c95aSGreg Clayton 
15423f8c95aSGreg Clayton 
15523f8c95aSGreg Clayton Symtab *
15623f8c95aSGreg Clayton ObjectFileJIT::GetSymtab()
15723f8c95aSGreg Clayton {
15823f8c95aSGreg Clayton     ModuleSP module_sp(GetModule());
15923f8c95aSGreg Clayton     if (module_sp)
16023f8c95aSGreg Clayton     {
161*16ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
16223f8c95aSGreg Clayton         if (m_symtab_ap.get() == NULL)
16323f8c95aSGreg Clayton         {
16423f8c95aSGreg Clayton             m_symtab_ap.reset(new Symtab(this));
16523f8c95aSGreg Clayton             Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
16623f8c95aSGreg Clayton             ObjectFileJITDelegateSP delegate_sp (m_delegate_wp.lock());
16723f8c95aSGreg Clayton             if (delegate_sp)
16823f8c95aSGreg Clayton                 delegate_sp->PopulateSymtab(this, *m_symtab_ap);
16923f8c95aSGreg Clayton             // TODO: get symbols from delegate
17023f8c95aSGreg Clayton             m_symtab_ap->Finalize ();
17123f8c95aSGreg Clayton         }
17223f8c95aSGreg Clayton     }
17323f8c95aSGreg Clayton     return m_symtab_ap.get();
17423f8c95aSGreg Clayton }
17523f8c95aSGreg Clayton 
17623f8c95aSGreg Clayton bool
17723f8c95aSGreg Clayton ObjectFileJIT::IsStripped ()
17823f8c95aSGreg Clayton {
17923f8c95aSGreg Clayton     return false; // JIT code that is in a module is never stripped
18023f8c95aSGreg Clayton }
18123f8c95aSGreg Clayton 
18223f8c95aSGreg Clayton void
18323f8c95aSGreg Clayton ObjectFileJIT::CreateSections (SectionList &unified_section_list)
18423f8c95aSGreg Clayton {
18523f8c95aSGreg Clayton     if (!m_sections_ap.get())
18623f8c95aSGreg Clayton     {
18723f8c95aSGreg Clayton         m_sections_ap.reset(new SectionList());
18823f8c95aSGreg Clayton         ObjectFileJITDelegateSP delegate_sp (m_delegate_wp.lock());
18923f8c95aSGreg Clayton         if (delegate_sp)
19023f8c95aSGreg Clayton         {
19123f8c95aSGreg Clayton             delegate_sp->PopulateSectionList(this, *m_sections_ap);
19223f8c95aSGreg Clayton             unified_section_list = *m_sections_ap;
19323f8c95aSGreg Clayton         }
19423f8c95aSGreg Clayton     }
19523f8c95aSGreg Clayton }
19623f8c95aSGreg Clayton 
19723f8c95aSGreg Clayton void
19823f8c95aSGreg Clayton ObjectFileJIT::Dump (Stream *s)
19923f8c95aSGreg Clayton {
20023f8c95aSGreg Clayton     ModuleSP module_sp(GetModule());
20123f8c95aSGreg Clayton     if (module_sp)
20223f8c95aSGreg Clayton     {
203*16ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
204324a1036SSaleem Abdulrasool         s->Printf("%p: ", static_cast<void*>(this));
20523f8c95aSGreg Clayton         s->Indent();
20623f8c95aSGreg Clayton         s->PutCString("ObjectFileJIT");
20723f8c95aSGreg Clayton 
20823f8c95aSGreg Clayton         ArchSpec arch;
20923f8c95aSGreg Clayton         if (GetArchitecture(arch))
21023f8c95aSGreg Clayton             *s << ", arch = " << arch.GetArchitectureName();
21123f8c95aSGreg Clayton 
21223f8c95aSGreg Clayton         s->EOL();
21323f8c95aSGreg Clayton 
21423f8c95aSGreg Clayton         SectionList *sections = GetSectionList();
21523f8c95aSGreg Clayton         if (sections)
21623f8c95aSGreg Clayton             sections->Dump(s, NULL, true, UINT32_MAX);
21723f8c95aSGreg Clayton 
21823f8c95aSGreg Clayton         if (m_symtab_ap.get())
21923f8c95aSGreg Clayton             m_symtab_ap->Dump(s, NULL, eSortOrderNone);
22023f8c95aSGreg Clayton     }
22123f8c95aSGreg Clayton }
22223f8c95aSGreg Clayton 
22323f8c95aSGreg Clayton bool
22423f8c95aSGreg Clayton ObjectFileJIT::GetUUID (lldb_private::UUID* uuid)
22523f8c95aSGreg Clayton {
22623f8c95aSGreg Clayton     // TODO: maybe get from delegate, not needed for first pass
22723f8c95aSGreg Clayton     return false;
22823f8c95aSGreg Clayton }
22923f8c95aSGreg Clayton 
23023f8c95aSGreg Clayton 
23123f8c95aSGreg Clayton uint32_t
23223f8c95aSGreg Clayton ObjectFileJIT::GetDependentModules (FileSpecList& files)
23323f8c95aSGreg Clayton {
23423f8c95aSGreg Clayton     // JIT modules don't have dependencies, but they could
23523f8c95aSGreg Clayton     // if external functions are called and we know where they are
23623f8c95aSGreg Clayton     files.Clear();
23723f8c95aSGreg Clayton     return 0;
23823f8c95aSGreg Clayton }
23923f8c95aSGreg Clayton 
24023f8c95aSGreg Clayton lldb_private::Address
24123f8c95aSGreg Clayton ObjectFileJIT::GetEntryPointAddress ()
24223f8c95aSGreg Clayton {
24323f8c95aSGreg Clayton     return Address();
24423f8c95aSGreg Clayton }
24523f8c95aSGreg Clayton 
24623f8c95aSGreg Clayton lldb_private::Address
24723f8c95aSGreg Clayton ObjectFileJIT::GetHeaderAddress ()
24823f8c95aSGreg Clayton {
24923f8c95aSGreg Clayton     return Address();
25023f8c95aSGreg Clayton }
25123f8c95aSGreg Clayton 
25223f8c95aSGreg Clayton 
25323f8c95aSGreg Clayton 
25423f8c95aSGreg Clayton ObjectFile::Type
25523f8c95aSGreg Clayton ObjectFileJIT::CalculateType()
25623f8c95aSGreg Clayton {
25723f8c95aSGreg Clayton     return eTypeJIT;
25823f8c95aSGreg Clayton }
25923f8c95aSGreg Clayton 
26023f8c95aSGreg Clayton ObjectFile::Strata
26123f8c95aSGreg Clayton ObjectFileJIT::CalculateStrata()
26223f8c95aSGreg Clayton {
26323f8c95aSGreg Clayton     return eStrataJIT;
26423f8c95aSGreg Clayton }
26523f8c95aSGreg Clayton 
26623f8c95aSGreg Clayton 
26723f8c95aSGreg Clayton bool
26823f8c95aSGreg Clayton ObjectFileJIT::GetArchitecture (ArchSpec &arch)
26923f8c95aSGreg Clayton {
27023f8c95aSGreg Clayton     ObjectFileJITDelegateSP delegate_sp (m_delegate_wp.lock());
27123f8c95aSGreg Clayton     if (delegate_sp)
27223f8c95aSGreg Clayton         return delegate_sp->GetArchitecture(arch);
27323f8c95aSGreg Clayton     return false;
27423f8c95aSGreg Clayton }
27523f8c95aSGreg Clayton 
27623f8c95aSGreg Clayton //------------------------------------------------------------------
27723f8c95aSGreg Clayton // PluginInterface protocol
27823f8c95aSGreg Clayton //------------------------------------------------------------------
27923f8c95aSGreg Clayton lldb_private::ConstString
28023f8c95aSGreg Clayton ObjectFileJIT::GetPluginName()
28123f8c95aSGreg Clayton {
28223f8c95aSGreg Clayton     return GetPluginNameStatic();
28323f8c95aSGreg Clayton }
28423f8c95aSGreg Clayton 
28523f8c95aSGreg Clayton uint32_t
28623f8c95aSGreg Clayton ObjectFileJIT::GetPluginVersion()
28723f8c95aSGreg Clayton {
28823f8c95aSGreg Clayton     return 1;
28923f8c95aSGreg Clayton }
29023f8c95aSGreg Clayton 
29123f8c95aSGreg Clayton 
29223f8c95aSGreg Clayton bool
29323f8c95aSGreg Clayton ObjectFileJIT::SetLoadAddress (Target &target,
29423f8c95aSGreg Clayton                                lldb::addr_t value,
29523f8c95aSGreg Clayton                                bool value_is_offset)
29623f8c95aSGreg Clayton {
29723f8c95aSGreg Clayton     size_t num_loaded_sections = 0;
29823f8c95aSGreg Clayton     SectionList *section_list = GetSectionList ();
29923f8c95aSGreg Clayton     if (section_list)
30023f8c95aSGreg Clayton     {
30123f8c95aSGreg Clayton         const size_t num_sections = section_list->GetSize();
30223f8c95aSGreg Clayton         // "value" is an offset to apply to each top level segment
30323f8c95aSGreg Clayton         for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
30423f8c95aSGreg Clayton         {
30523f8c95aSGreg Clayton             // Iterate through the object file sections to find all
30623f8c95aSGreg Clayton             // of the sections that size on disk (to avoid __PAGEZERO)
30723f8c95aSGreg Clayton             // and load them
30823f8c95aSGreg Clayton             SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
30923f8c95aSGreg Clayton             if (section_sp &&
31023f8c95aSGreg Clayton                 section_sp->GetFileSize() > 0 &&
31123f8c95aSGreg Clayton                 section_sp->IsThreadSpecific() == false)
31223f8c95aSGreg Clayton             {
31323f8c95aSGreg Clayton                 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
31423f8c95aSGreg Clayton                     ++num_loaded_sections;
31523f8c95aSGreg Clayton             }
31623f8c95aSGreg Clayton         }
31723f8c95aSGreg Clayton     }
31823f8c95aSGreg Clayton     return num_loaded_sections > 0;
31923f8c95aSGreg Clayton }
32023f8c95aSGreg Clayton 
32123f8c95aSGreg Clayton 
32223f8c95aSGreg Clayton size_t
32323f8c95aSGreg Clayton ObjectFileJIT::ReadSectionData (const lldb_private::Section *section,
324a746e8e5SZachary Turner                                 lldb::offset_t section_offset,
32523f8c95aSGreg Clayton                                 void *dst,
32623f8c95aSGreg Clayton                                 size_t dst_len) const
32723f8c95aSGreg Clayton {
32823f8c95aSGreg Clayton     lldb::offset_t file_size = section->GetFileSize();
329a746e8e5SZachary Turner     if (section_offset < file_size)
33023f8c95aSGreg Clayton     {
331a746e8e5SZachary Turner         size_t src_len = file_size - section_offset;
33223f8c95aSGreg Clayton         if (src_len > dst_len)
33323f8c95aSGreg Clayton             src_len = dst_len;
33423f8c95aSGreg Clayton         const uint8_t *src = ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
33523f8c95aSGreg Clayton 
33623f8c95aSGreg Clayton         memcpy (dst, src, src_len);
33723f8c95aSGreg Clayton         return src_len;
33823f8c95aSGreg Clayton     }
33923f8c95aSGreg Clayton     return 0;
34023f8c95aSGreg Clayton }
341a746e8e5SZachary Turner 
34223f8c95aSGreg Clayton size_t
34323f8c95aSGreg Clayton ObjectFileJIT::ReadSectionData (const lldb_private::Section *section,
34423f8c95aSGreg Clayton                                 lldb_private::DataExtractor& section_data) const
34523f8c95aSGreg Clayton {
34623f8c95aSGreg Clayton     if (section->GetFileSize())
34723f8c95aSGreg Clayton     {
34823f8c95aSGreg Clayton         const void *src = (void *)(uintptr_t)section->GetFileOffset();
34923f8c95aSGreg Clayton 
35023f8c95aSGreg Clayton         DataBufferSP data_sp (new lldb_private::DataBufferHeap(src, section->GetFileSize()));
35123f8c95aSGreg Clayton         if (data_sp)
35223f8c95aSGreg Clayton         {
35323f8c95aSGreg Clayton             section_data.SetData (data_sp, 0, data_sp->GetByteSize());
35423f8c95aSGreg Clayton             section_data.SetByteOrder (GetByteOrder());
35523f8c95aSGreg Clayton             section_data.SetAddressByteSize (GetAddressByteSize());
35623f8c95aSGreg Clayton             return section_data.GetByteSize();
35723f8c95aSGreg Clayton         }
35823f8c95aSGreg Clayton     }
35923f8c95aSGreg Clayton     section_data.Clear();
36023f8c95aSGreg Clayton     return 0;
36123f8c95aSGreg Clayton }
36223f8c95aSGreg Clayton 
363