1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00

[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
This commit is contained in:
Georgii Rymar 2020-08-25 17:44:39 +03:00
parent 6ebc5b3f8a
commit 70f1e167ac
2 changed files with 21 additions and 0 deletions

View File

@ -372,6 +372,8 @@ public:
Expected<ArrayRef<XCOFFRelocation32>>
relocations(const XCOFFSectionHeader32 &) const;
static bool classof(const Binary *B) { return B->isXCOFF(); }
}; // XCOFFObjectFile
class XCOFFSymbolRef {

View File

@ -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}));