1- // ===-- ObjectFileXCOFF.cpp -------------------------------------------------===//
1+ // ===-- ObjectFileXCOFF.cpp
2+ // -------------------------------------------------===//
23//
34// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45// See https://llvm.org/LICENSE.txt for license information.
78// ===----------------------------------------------------------------------===//
89
910#include " ObjectFileXCOFF.h"
10-
11- #include < algorithm>
12- #include < cassert>
13- #include < unordered_map>
14- #include < string.h>
15-
16- #include " lldb/Utility/FileSpecList.h"
1711#include " lldb/Core/Module.h"
1812#include " lldb/Core/ModuleSpec.h"
1913#include " lldb/Core/PluginManager.h"
2822#include " lldb/Target/Target.h"
2923#include " lldb/Utility/ArchSpec.h"
3024#include " lldb/Utility/DataBufferHeap.h"
25+ #include " lldb/Utility/FileSpecList.h"
3126#include " lldb/Utility/LLDBLog.h"
3227#include " lldb/Utility/Log.h"
3328#include " lldb/Utility/RangeMap.h"
3833#include " llvm/ADT/PointerUnion.h"
3934#include " llvm/ADT/StringRef.h"
4035#include " llvm/BinaryFormat/XCOFF.h"
36+ #include " llvm/Object/XCOFFObjectFile.h"
4137#include " llvm/Object/Decompressor.h"
4238#include " llvm/Support/CRC.h"
4339#include " llvm/Support/FormatVariadic.h"
4440#include " llvm/Support/MathExtras.h"
4541#include " llvm/Support/MemoryBuffer.h"
46- #include " llvm/Object/XCOFFObjectFile.h"
42+ #include < algorithm>
43+ #include < cassert>
44+ #include < cstring>
45+ #include < unordered_map>
4746
4847using namespace llvm ;
4948using namespace lldb ;
@@ -69,21 +68,19 @@ void ObjectFileXCOFF::Terminate() {
6968bool UGLY_FLAG_FOR_AIX __attribute__ ((weak)) = false;
7069
7170ObjectFile *ObjectFileXCOFF::CreateInstance (const lldb::ModuleSP &module_sp,
72- DataBufferSP data_sp,
73- lldb::offset_t data_offset,
74- const lldb_private::FileSpec *file,
75- lldb::offset_t file_offset,
76- lldb::offset_t length) {
71+ DataBufferSP data_sp,
72+ lldb::offset_t data_offset,
73+ const lldb_private::FileSpec *file,
74+ lldb::offset_t file_offset,
75+ lldb::offset_t length) {
7776 if (!data_sp) {
7877 data_sp = MapFileData (*file, length, file_offset);
7978 if (!data_sp)
8079 return nullptr ;
8180 data_offset = 0 ;
8281 }
83-
8482 if (!ObjectFileXCOFF::MagicBytesMatch (data_sp, data_offset, length))
8583 return nullptr ;
86-
8784 // Update the data to contain the entire file if it doesn't already
8885 if (data_sp->GetByteSize () < length) {
8986 data_sp = MapFileData (*file, length, file_offset);
@@ -114,15 +111,15 @@ bool ObjectFileXCOFF::CreateBinary() {
114111
115112 Log *log = GetLog (LLDBLog::Object);
116113
117- auto binary = llvm::object::XCOFFObjectFile::createObjectFile (llvm::MemoryBufferRef (
118- toStringRef (m_data.GetData ()), m_file.GetFilename ().GetStringRef ()),
119- file_magic::xcoff_object_64);
114+ auto binary = llvm::object::ObjectFile::createObjectFile (
115+ llvm::MemoryBufferRef (toStringRef (m_data.GetData ()),
116+ m_file.GetFilename ().GetStringRef ()),
117+ file_magic::xcoff_object_64);
120118 if (!binary) {
121119 LLDB_LOG_ERROR (log, binary.takeError (),
122120 " Failed to create binary for file ({1}): {0}" , m_file);
123121 return false ;
124122 }
125-
126123 // Make sure we only handle COFF format.
127124 m_binary =
128125 llvm::unique_dyn_cast<llvm::object::XCOFFObjectFile>(std::move (*binary));
@@ -132,6 +129,7 @@ bool ObjectFileXCOFF::CreateBinary() {
132129 LLDB_LOG (log, " this = {0}, module = {1} ({2}), file = {3}, binary = {4}" ,
133130 this , GetModule ().get (), GetModule ()->GetSpecificationDescription (),
134131 m_file.GetPath (), m_binary.get ());
132+
135133 return true ;
136134}
137135
@@ -148,21 +146,22 @@ size_t ObjectFileXCOFF::GetModuleSpecifications(
148146 const size_t initial_count = specs.GetSize ();
149147
150148 if (ObjectFileXCOFF::MagicBytesMatch (data_sp, 0 , data_sp->GetByteSize ())) {
151- ArchSpec arch_spec = ArchSpec (eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE);
149+ ArchSpec arch_spec =
150+ ArchSpec (eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE);
152151 ModuleSpec spec (file, arch_spec);
153- spec.GetArchitecture ().SetArchitecture (eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE, llvm::Triple::AIX);
152+ spec.GetArchitecture ().SetArchitecture (eArchTypeXCOFF, XCOFF::TCPU_PPC64,
153+ LLDB_INVALID_CPUTYPE,
154+ llvm::Triple::AIX);
154155 specs.Append (spec);
155156 }
156157 return specs.GetSize () - initial_count;
157158}
158159
159160static uint32_t XCOFFHeaderSizeFromMagic (uint32_t magic) {
160161 switch (magic) {
161- /* TODO: 32bit not supported yet
162- case XCOFF::XCOFF32:
163- return sizeof(struct llvm::object::XCOFFFileHeader32);
164- */
165-
162+ // TODO: 32bit not supported.
163+ // case XCOFF::XCOFF32:
164+ // return sizeof(struct llvm::object::XCOFFFileHeader32);
166165 case XCOFF::XCOFF64:
167166 return sizeof (struct llvm ::object::XCOFFFileHeader64);
168167 break ;
@@ -174,10 +173,12 @@ static uint32_t XCOFFHeaderSizeFromMagic(uint32_t magic) {
174173}
175174
176175bool ObjectFileXCOFF::MagicBytesMatch (DataBufferSP &data_sp,
177- lldb::addr_t data_offset,
178- lldb::addr_t data_length) {
179- lldb_private::DataExtractor data;
176+ lldb::addr_t data_offset,
177+ lldb::addr_t data_length) {
178+ lldb_private::DataExtractor data;
180179 data.SetData (data_sp, data_offset, data_length);
180+ // Need to set this as XCOFF is only compatible with Big Endian
181+ data.SetByteOrder (eByteOrderBig);
181182 lldb::offset_t offset = 0 ;
182183 uint16_t magic = data.GetU16 (&offset);
183184 return XCOFFHeaderSizeFromMagic (magic) != 0 ;
@@ -386,13 +387,10 @@ bool ObjectFileXCOFF::SetLoadAddressByType(Target &target, lldb::addr_t value,
386387 return changed;
387388}
388389
389- ByteOrder ObjectFileXCOFF::GetByteOrder () const {
390- return eByteOrderBig;
391- }
392390
393- bool ObjectFileXCOFF::IsExecutable () const {
394- return true ;
395- }
391+ ByteOrder ObjectFileXCOFF::GetByteOrder () const { return eByteOrderBig; }
392+
393+ bool ObjectFileXCOFF::IsExecutable () const { return true ; }
396394
397395uint32_t ObjectFileXCOFF::GetAddressByteSize () const {
398396 if (m_xcoff_header.magic == XCOFF::XCOFF64)
@@ -592,13 +590,12 @@ void ObjectFileXCOFF::Dump(Stream *s) {
592590}
593591
594592ArchSpec ObjectFileXCOFF::GetArchitecture () {
595- ArchSpec arch_spec = ArchSpec (eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE);
593+ ArchSpec arch_spec =
594+ ArchSpec (eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE);
596595 return arch_spec;
597596}
598597
599- UUID ObjectFileXCOFF::GetUUID () {
600- return UUID ();
601- }
598+ UUID ObjectFileXCOFF::GetUUID () { return UUID (); }
602599
603600std::optional<FileSpec> ObjectFileXCOFF::GetDebugLink () {
604601 return std::nullopt ;
@@ -724,16 +721,14 @@ lldb_private::Address ObjectFileXCOFF::GetBaseAddress() {
724721}
725722
726723ObjectFile::Type ObjectFileXCOFF::CalculateType () {
727- if (m_xcoff_header. flags & XCOFF::F_EXEC)
724+ if (m_binary-> fileHeader64 ()-> Flags & XCOFF::F_EXEC)
728725 return eTypeExecutable;
729- else if (m_xcoff_header. flags & XCOFF::F_SHROBJ)
726+ else if (m_binary-> fileHeader64 ()-> Flags & XCOFF::F_SHROBJ)
730727 return eTypeSharedLibrary;
731728 return eTypeUnknown;
732729}
733730
734- ObjectFile::Strata ObjectFileXCOFF::CalculateStrata () {
735- return eStrataUnknown;
736- }
731+ ObjectFile::Strata ObjectFileXCOFF::CalculateStrata () { return eStrataUnknown; }
737732
738733llvm::StringRef
739734ObjectFileXCOFF::StripLinkerSymbolAnnotations (llvm::StringRef symbol_name) const {
@@ -752,7 +747,7 @@ ObjectFileXCOFF::GetLoadableData(Target &target) {
752747
753748lldb::WritableDataBufferSP
754749ObjectFileXCOFF::MapFileDataWritable (const FileSpec &file, uint64_t Size,
755- uint64_t Offset) {
750+ uint64_t Offset) {
756751 return FileSystem::Instance ().CreateWritableDataBuffer (file.GetPath (), Size,
757752 Offset);
758753}
0 commit comments