1 //===-- ObjectFileXCOFF.cpp
2 //-------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #include "ObjectFileXCOFF.h"
15 #include <unordered_map>
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/ModuleSpec.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Progress.h"
21 #include "lldb/Core/Section.h"
22 #include "lldb/Host/FileSystem.h"
23 #include "lldb/Symbol/SymbolContext.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Utility/ArchSpec.h"
27 #include "lldb/Utility/DataBufferHeap.h"
28 #include "lldb/Utility/FileSpecList.h"
29 #include "lldb/Utility/LLDBLog.h"
30 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/RangeMap.h"
32 #include "lldb/Utility/Status.h"
33 #include "lldb/Utility/Stream.h"
34 #include "llvm/ADT/StringRef.h"
35 #include "llvm/BinaryFormat/XCOFF.h"
36 #include "llvm/Object/XCOFFObjectFile.h"
37 #include "llvm/Support/MemoryBuffer.h"
41 using namespace lldb_private
;
43 LLDB_PLUGIN_DEFINE(ObjectFileXCOFF
)
45 // FIXME: target 64bit at this moment.
48 void ObjectFileXCOFF::Initialize() {
49 PluginManager::RegisterPlugin(GetPluginNameStatic(),
50 GetPluginDescriptionStatic(), CreateInstance
,
51 CreateMemoryInstance
, GetModuleSpecifications
);
54 void ObjectFileXCOFF::Terminate() {
55 PluginManager::UnregisterPlugin(CreateInstance
);
58 ObjectFile
*ObjectFileXCOFF::CreateInstance(const lldb::ModuleSP
&module_sp
,
60 lldb::offset_t data_offset
,
61 const lldb_private::FileSpec
*file
,
62 lldb::offset_t file_offset
,
63 lldb::offset_t length
) {
65 data_sp
= MapFileData(*file
, length
, file_offset
);
70 if (!ObjectFileXCOFF::MagicBytesMatch(data_sp
, data_offset
, length
))
72 // Update the data to contain the entire file if it doesn't already
73 if (data_sp
->GetByteSize() < length
) {
74 data_sp
= MapFileData(*file
, length
, file_offset
);
79 auto objfile_up
= std::make_unique
<ObjectFileXCOFF
>(
80 module_sp
, data_sp
, data_offset
, file
, file_offset
, length
);
84 return objfile_up
.release();
87 ObjectFile
*ObjectFileXCOFF::CreateMemoryInstance(
88 const lldb::ModuleSP
&module_sp
, WritableDataBufferSP data_sp
,
89 const lldb::ProcessSP
&process_sp
, lldb::addr_t header_addr
) {
93 size_t ObjectFileXCOFF::GetModuleSpecifications(
94 const lldb_private::FileSpec
&file
, lldb::DataBufferSP
&data_sp
,
95 lldb::offset_t data_offset
, lldb::offset_t file_offset
,
96 lldb::offset_t length
, lldb_private::ModuleSpecList
&specs
) {
97 const size_t initial_count
= specs
.GetSize();
99 if (ObjectFileXCOFF::MagicBytesMatch(data_sp
, 0, data_sp
->GetByteSize())) {
101 ArchSpec(eArchTypeXCOFF
, XCOFF::TCPU_PPC64
, LLDB_INVALID_CPUTYPE
);
102 ModuleSpec
spec(file
, arch_spec
);
103 spec
.GetArchitecture().SetArchitecture(eArchTypeXCOFF
, XCOFF::TCPU_PPC64
,
104 LLDB_INVALID_CPUTYPE
,
108 return specs
.GetSize() - initial_count
;
111 static uint32_t XCOFFHeaderSizeFromMagic(uint32_t magic
) {
113 /* TODO: 32bit not supported yet
115 return sizeof(struct llvm::object::XCOFFFileHeader32);
119 return sizeof(struct llvm::object::XCOFFFileHeader64
);
128 bool ObjectFileXCOFF::MagicBytesMatch(DataBufferSP
&data_sp
,
129 lldb::addr_t data_offset
,
130 lldb::addr_t data_length
) {
131 lldb_private::DataExtractor data
;
132 data
.SetData(data_sp
, data_offset
, data_length
);
133 data
.SetByteOrder(eByteOrderBig
);
134 lldb::offset_t offset
= 0;
135 uint16_t magic
= data
.GetU16(&offset
);
136 return XCOFFHeaderSizeFromMagic(magic
) != 0;
139 bool ObjectFileXCOFF::ParseHeader() { return false; }
141 ByteOrder
ObjectFileXCOFF::GetByteOrder() const { return eByteOrderBig
; }
143 bool ObjectFileXCOFF::IsExecutable() const { return true; }
145 uint32_t ObjectFileXCOFF::GetAddressByteSize() const { return 8; }
147 void ObjectFileXCOFF::ParseSymtab(Symtab
&lldb_symtab
) {}
149 bool ObjectFileXCOFF::IsStripped() { return false; }
151 void ObjectFileXCOFF::CreateSections(SectionList
&unified_section_list
) {}
153 void ObjectFileXCOFF::Dump(Stream
*s
) {}
155 ArchSpec
ObjectFileXCOFF::GetArchitecture() {
157 ArchSpec(eArchTypeXCOFF
, XCOFF::TCPU_PPC64
, LLDB_INVALID_CPUTYPE
);
161 UUID
ObjectFileXCOFF::GetUUID() { return UUID(); }
163 uint32_t ObjectFileXCOFF::GetDependentModules(FileSpecList
&files
) { return 0; }
165 ObjectFile::Type
ObjectFileXCOFF::CalculateType() { return eTypeExecutable
; }
167 ObjectFile::Strata
ObjectFileXCOFF::CalculateStrata() { return eStrataUnknown
; }
169 lldb::WritableDataBufferSP
170 ObjectFileXCOFF::MapFileDataWritable(const FileSpec
&file
, uint64_t Size
,
172 return FileSystem::Instance().CreateWritableDataBuffer(file
.GetPath(), Size
,
176 ObjectFileXCOFF::ObjectFileXCOFF(const lldb::ModuleSP
&module_sp
,
177 DataBufferSP data_sp
,
178 lldb::offset_t data_offset
,
179 const FileSpec
*file
,
180 lldb::offset_t file_offset
,
181 lldb::offset_t length
)
182 : ObjectFile(module_sp
, file
, file_offset
, length
, data_sp
, data_offset
) {
187 ObjectFileXCOFF::ObjectFileXCOFF(const lldb::ModuleSP
&module_sp
,
188 DataBufferSP header_data_sp
,
189 const lldb::ProcessSP
&process_sp
,
191 : ObjectFile(module_sp
, process_sp
, header_addr
, header_data_sp
) {}