diff --git a/include/llvm/Object/XCOFFObjectFile.h b/include/llvm/Object/XCOFFObjectFile.h index 53caeae0bdc..fbc542fd3e6 100644 --- a/include/llvm/Object/XCOFFObjectFile.h +++ b/include/llvm/Object/XCOFFObjectFile.h @@ -372,6 +372,8 @@ public: Expected> relocations(const XCOFFSectionHeader32 &) const; + + static bool classof(const Binary *B) { return B->isXCOFF(); } }; // XCOFFObjectFile class XCOFFSymbolRef { diff --git a/unittests/Object/XCOFFObjectFileTest.cpp b/unittests/Object/XCOFFObjectFileTest.cpp index cb0004585d2..47e200c12cf 100644 --- a/unittests/Object/XCOFFObjectFileTest.cpp +++ b/unittests/Object/XCOFFObjectFileTest.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/XCOFFObjectFile.h" #include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" @@ -13,6 +14,24 @@ using namespace llvm; using namespace llvm::object; +TEST(XCOFFObjectFileTest, XCOFFObjectType) { + // Create an arbitrary object of a non-XCOFF type and test that + // dyn_cast returns null for it. + char Buf[sizeof(typename ELF64LE::Ehdr)] = {}; + memcpy(Buf, "\177ELF", 4); + + auto *EHdr = reinterpret_cast(Buf); + EHdr->e_ident[llvm::ELF::EI_CLASS] = llvm::ELF::ELFCLASS64; + EHdr->e_ident[llvm::ELF::EI_DATA] = llvm::ELF::ELFDATA2LSB; + + MemoryBufferRef Source(StringRef(Buf, sizeof(Buf)), "non-XCOFF"); + Expected> ObjOrErr = + ObjectFile::createObjectFile(Source); + ASSERT_THAT_EXPECTED(ObjOrErr, Succeeded()); + + EXPECT_TRUE(dyn_cast((*ObjOrErr).get()) == nullptr); +} + TEST(XCOFFObjectFileTest, doesXCOFFTracebackTableBegin) { EXPECT_TRUE(doesXCOFFTracebackTableBegin({0, 0, 0, 0})); EXPECT_TRUE(doesXCOFFTracebackTableBegin({0, 0, 0, 0, 1}));