[llvm] 92c527e - [llvm/Object] - Make dyn_cast<XCOFFObjectFile> work as it should.
Georgii Rymar via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 26 05:10:34 PDT 2020
Author: Georgii Rymar
Date: 2020-08-26T15:09:55+03:00
New Revision: 92c527e5a2b49fb1213ceda97738d4caf414666a
URL: https://github.com/llvm/llvm-project/commit/92c527e5a2b49fb1213ceda97738d4caf414666a
DIFF: https://github.com/llvm/llvm-project/commit/92c527e5a2b49fb1213ceda97738d4caf414666a.diff
LOG: [llvm/Object] - Make dyn_cast<XCOFFObjectFile> work as it should.
Currently, `dyn_cast<XCOFFObjectFile>` always does cast and returns a pointer,
even when we pass `ELF`/`Wasm`/`Mach-O` or `COFF` instead of `XCOFF`.
It happens because `XCOFFObjectFile` class does not implement `classof`.
I've fixed it and added a unit test.
Differential revision: https://reviews.llvm.org/D86542
Added:
Modified:
llvm/include/llvm/Object/XCOFFObjectFile.h
llvm/unittests/Object/XCOFFObjectFileTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h
index 53caeae0bdcc..fbc542fd3e6d 100644
--- a/llvm/include/llvm/Object/XCOFFObjectFile.h
+++ b/llvm/include/llvm/Object/XCOFFObjectFile.h
@@ -372,6 +372,8 @@ class XCOFFObjectFile : public ObjectFile {
Expected<ArrayRef<XCOFFRelocation32>>
relocations(const XCOFFSectionHeader32 &) const;
+
+ static bool classof(const Binary *B) { return B->isXCOFF(); }
}; // XCOFFObjectFile
class XCOFFSymbolRef {
diff --git a/llvm/unittests/Object/XCOFFObjectFileTest.cpp b/llvm/unittests/Object/XCOFFObjectFileTest.cpp
index cb0004585d23..47e200c12cfe 100644
--- a/llvm/unittests/Object/XCOFFObjectFileTest.cpp
+++ b/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<XCOFFObjectFile> returns null for it.
+ char Buf[sizeof(typename ELF64LE::Ehdr)] = {};
+ memcpy(Buf, "\177ELF", 4);
+
+ auto *EHdr = reinterpret_cast<typename ELF64LE::Ehdr *>(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<std::unique_ptr<ObjectFile>> ObjOrErr =
+ ObjectFile::createObjectFile(Source);
+ ASSERT_THAT_EXPECTED(ObjOrErr, Succeeded());
+
+ EXPECT_TRUE(dyn_cast<XCOFFObjectFile>((*ObjOrErr).get()) == nullptr);
+}
+
TEST(XCOFFObjectFileTest, doesXCOFFTracebackTableBegin) {
EXPECT_TRUE(doesXCOFFTracebackTableBegin({0, 0, 0, 0}));
EXPECT_TRUE(doesXCOFFTracebackTableBegin({0, 0, 0, 0, 1}));
More information about the llvm-commits
mailing list