TargetLibraryInfo: Use pointer index size to determine getSizeTSize(). (#118747)
[llvm-project.git] / lldb / source / Plugins / ObjectFile / XCOFF / ObjectFileXCOFF.cpp
blob3be900f9a4bc9fa51656588626521b117f250bad
1 //===-- ObjectFileXCOFF.cpp
2 //-------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
10 #include "ObjectFileXCOFF.h"
12 #include <algorithm>
13 #include <cassert>
14 #include <cstring>
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"
39 using namespace llvm;
40 using namespace lldb;
41 using namespace lldb_private;
43 LLDB_PLUGIN_DEFINE(ObjectFileXCOFF)
45 // FIXME: target 64bit at this moment.
47 // Static methods.
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,
59 DataBufferSP data_sp,
60 lldb::offset_t data_offset,
61 const lldb_private::FileSpec *file,
62 lldb::offset_t file_offset,
63 lldb::offset_t length) {
64 if (!data_sp) {
65 data_sp = MapFileData(*file, length, file_offset);
66 if (!data_sp)
67 return nullptr;
68 data_offset = 0;
70 if (!ObjectFileXCOFF::MagicBytesMatch(data_sp, data_offset, length))
71 return nullptr;
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);
75 if (!data_sp)
76 return nullptr;
77 data_offset = 0;
79 auto objfile_up = std::make_unique<ObjectFileXCOFF>(
80 module_sp, data_sp, data_offset, file, file_offset, length);
81 if (!objfile_up)
82 return nullptr;
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) {
90 return nullptr;
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())) {
100 ArchSpec arch_spec =
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,
105 llvm::Triple::AIX);
106 specs.Append(spec);
108 return specs.GetSize() - initial_count;
111 static uint32_t XCOFFHeaderSizeFromMagic(uint32_t magic) {
112 switch (magic) {
113 /* TODO: 32bit not supported yet
114 case XCOFF::XCOFF32:
115 return sizeof(struct llvm::object::XCOFFFileHeader32);
118 case XCOFF::XCOFF64:
119 return sizeof(struct llvm::object::XCOFFFileHeader64);
120 break;
122 default:
123 break;
125 return 0;
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() {
156 ArchSpec arch_spec =
157 ArchSpec(eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE);
158 return arch_spec;
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,
171 uint64_t Offset) {
172 return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size,
173 Offset);
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) {
183 if (file)
184 m_file = *file;
187 ObjectFileXCOFF::ObjectFileXCOFF(const lldb::ModuleSP &module_sp,
188 DataBufferSP header_data_sp,
189 const lldb::ProcessSP &process_sp,
190 addr_t header_addr)
191 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}