Index: llvm/include/llvm/Object/XCOFFObjectFile.h =================================================================== --- llvm/include/llvm/Object/XCOFFObjectFile.h +++ llvm/include/llvm/Object/XCOFFObjectFile.h @@ -372,6 +372,8 @@ Expected> relocations(const XCOFFSectionHeader32 &) const; + + static bool classof(const Binary *B) { return B->isXCOFF(); } }; // XCOFFObjectFile class XCOFFSymbolRef { Index: llvm/unittests/Object/XCOFFObjectFileTest.cpp =================================================================== --- llvm/unittests/Object/XCOFFObjectFileTest.cpp +++ llvm/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}));