2014-01-11 07:29:12 +08:00
|
|
|
//===- lld/unittest/WinLinkModuleDefTest.cpp ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "lld/Driver/WinLinkModuleDef.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
|
|
|
|
class ParserTest : public testing::Test {
|
|
|
|
protected:
|
2014-01-11 09:33:42 +08:00
|
|
|
llvm::Optional<moduledef::Directive *> parse(const char *contents,
|
|
|
|
llvm::BumpPtrAllocator &alloc) {
|
2014-01-11 07:29:12 +08:00
|
|
|
auto membuf =
|
|
|
|
std::unique_ptr<MemoryBuffer>(MemoryBuffer::getMemBuffer(contents));
|
|
|
|
moduledef::Lexer lexer(std::move(membuf));
|
2014-01-11 09:33:42 +08:00
|
|
|
moduledef::Parser parser(lexer, alloc);
|
|
|
|
return parser.parse();
|
2014-01-11 07:29:12 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ParserTest, Exports) {
|
2014-01-11 09:33:42 +08:00
|
|
|
llvm::BumpPtrAllocator alloc;
|
|
|
|
llvm::Optional<moduledef::Directive *> dir = parse("EXPORTS\n"
|
|
|
|
" sym1\n"
|
|
|
|
" sym2 @5\n"
|
|
|
|
" sym3 @8 NONAME\n"
|
|
|
|
" sym4 DATA\n"
|
|
|
|
" sym5 @10 NONAME DATA\n",
|
|
|
|
alloc);
|
|
|
|
EXPECT_TRUE(dir.hasValue());
|
|
|
|
EXPECT_EQ(moduledef::Directive::Kind::exports, dir.getValue()->getKind());
|
|
|
|
|
|
|
|
auto *exportsDir = dyn_cast<moduledef::Exports>(dir.getValue());
|
|
|
|
EXPECT_TRUE(exportsDir != nullptr);
|
|
|
|
|
|
|
|
const std::vector<PECOFFLinkingContext::ExportDesc> &exports =
|
|
|
|
exportsDir->getExports();
|
2014-01-11 07:29:12 +08:00
|
|
|
EXPECT_EQ(5U, exports.size());
|
|
|
|
EXPECT_EQ(exports[0].name, "sym1");
|
|
|
|
EXPECT_EQ(exports[0].ordinal, -1);
|
|
|
|
EXPECT_EQ(exports[0].noname, false);
|
|
|
|
EXPECT_EQ(exports[0].isData, false);
|
|
|
|
EXPECT_EQ(exports[1].name, "sym2");
|
|
|
|
EXPECT_EQ(exports[1].ordinal, 5);
|
|
|
|
EXPECT_EQ(exports[1].noname, false);
|
|
|
|
EXPECT_EQ(exports[1].isData, false);
|
|
|
|
EXPECT_EQ(exports[2].name, "sym3");
|
|
|
|
EXPECT_EQ(exports[2].ordinal, 8);
|
|
|
|
EXPECT_EQ(exports[2].noname, true);
|
|
|
|
EXPECT_EQ(exports[2].isData, false);
|
|
|
|
EXPECT_EQ(exports[3].name, "sym4");
|
|
|
|
EXPECT_EQ(exports[3].ordinal, -1);
|
|
|
|
EXPECT_EQ(exports[3].noname, false);
|
|
|
|
EXPECT_EQ(exports[3].isData, true);
|
|
|
|
EXPECT_EQ(exports[4].name, "sym5");
|
|
|
|
EXPECT_EQ(exports[4].ordinal, 10);
|
|
|
|
EXPECT_EQ(exports[4].noname, true);
|
|
|
|
EXPECT_EQ(exports[4].isData, true);
|
|
|
|
}
|
2014-01-11 09:58:21 +08:00
|
|
|
|
2014-01-14 07:02:03 +08:00
|
|
|
TEST_F(ParserTest, Heapsize) {
|
2014-01-11 09:58:21 +08:00
|
|
|
llvm::BumpPtrAllocator alloc;
|
|
|
|
llvm::Optional<moduledef::Directive *> dir = parse("HEAPSIZE 65536", alloc);
|
|
|
|
EXPECT_TRUE(dir.hasValue());
|
|
|
|
auto *heapsize = dyn_cast<moduledef::Heapsize>(dir.getValue());
|
|
|
|
EXPECT_TRUE(heapsize != nullptr);
|
|
|
|
EXPECT_EQ(65536U, heapsize->getReserve());
|
|
|
|
EXPECT_EQ(0U, heapsize->getCommit());
|
|
|
|
}
|
|
|
|
|
2014-01-14 07:02:03 +08:00
|
|
|
TEST_F(ParserTest, Heapsize_WithCommit) {
|
2014-01-11 09:58:21 +08:00
|
|
|
llvm::BumpPtrAllocator alloc;
|
|
|
|
llvm::Optional<moduledef::Directive *> dir = parse("HEAPSIZE 65536, 8192", alloc);
|
|
|
|
EXPECT_TRUE(dir.hasValue());
|
|
|
|
auto *heapsize = dyn_cast<moduledef::Heapsize>(dir.getValue());
|
|
|
|
EXPECT_TRUE(heapsize != nullptr);
|
|
|
|
EXPECT_EQ(65536U, heapsize->getReserve());
|
|
|
|
EXPECT_EQ(8192U, heapsize->getCommit());
|
|
|
|
}
|
2014-01-12 05:53:46 +08:00
|
|
|
|
2014-01-14 07:02:03 +08:00
|
|
|
TEST_F(ParserTest, Name) {
|
2014-01-12 05:53:46 +08:00
|
|
|
llvm::BumpPtrAllocator alloc;
|
|
|
|
llvm::Optional<moduledef::Directive *> dir = parse("NAME foo.exe", alloc);
|
|
|
|
EXPECT_TRUE(dir.hasValue());
|
|
|
|
auto *name = dyn_cast<moduledef::Name>(dir.getValue());
|
|
|
|
EXPECT_TRUE(name != nullptr);
|
|
|
|
EXPECT_EQ("foo.exe", name->getOutputPath());
|
|
|
|
EXPECT_EQ(0U, name->getBaseAddress());
|
|
|
|
}
|
|
|
|
|
2014-01-14 07:02:03 +08:00
|
|
|
TEST_F(ParserTest, Name_WithBase) {
|
2014-01-12 05:53:46 +08:00
|
|
|
llvm::BumpPtrAllocator alloc;
|
|
|
|
llvm::Optional<moduledef::Directive *> dir = parse("NAME foo.exe BASE=4096", alloc);
|
|
|
|
EXPECT_TRUE(dir.hasValue());
|
|
|
|
auto *name = dyn_cast<moduledef::Name>(dir.getValue());
|
|
|
|
EXPECT_TRUE(name != nullptr);
|
|
|
|
EXPECT_EQ("foo.exe", name->getOutputPath());
|
|
|
|
EXPECT_EQ(4096U, name->getBaseAddress());
|
|
|
|
}
|
2014-01-12 06:45:00 +08:00
|
|
|
|
2014-01-14 07:02:03 +08:00
|
|
|
TEST_F(ParserTest, Name_LongFileName) {
|
2014-01-14 06:55:09 +08:00
|
|
|
llvm::BumpPtrAllocator alloc;
|
|
|
|
llvm::Optional<moduledef::Directive *> dir = parse(
|
|
|
|
"NAME \"a long file name.exe\"", alloc);
|
|
|
|
EXPECT_TRUE(dir.hasValue());
|
|
|
|
auto *name = dyn_cast<moduledef::Name>(dir.getValue());
|
|
|
|
EXPECT_TRUE(name != nullptr);
|
|
|
|
EXPECT_EQ("a long file name.exe", name->getOutputPath());
|
|
|
|
EXPECT_EQ(0U, name->getBaseAddress());
|
|
|
|
}
|
|
|
|
|
2014-01-14 07:02:03 +08:00
|
|
|
TEST_F(ParserTest, Version_Major) {
|
2014-01-12 06:45:00 +08:00
|
|
|
llvm::BumpPtrAllocator alloc;
|
|
|
|
llvm::Optional<moduledef::Directive *> dir = parse("VERSION 12", alloc);
|
|
|
|
EXPECT_TRUE(dir.hasValue());
|
|
|
|
auto *ver = dyn_cast<moduledef::Version>(dir.getValue());
|
|
|
|
EXPECT_TRUE(ver != nullptr);
|
|
|
|
EXPECT_EQ(12, ver->getMajorVersion());
|
|
|
|
EXPECT_EQ(0, ver->getMinorVersion());
|
|
|
|
}
|
|
|
|
|
2014-01-14 07:02:03 +08:00
|
|
|
TEST_F(ParserTest, Version_MajorMinor) {
|
2014-01-12 06:45:00 +08:00
|
|
|
llvm::BumpPtrAllocator alloc;
|
|
|
|
llvm::Optional<moduledef::Directive *> dir = parse("VERSION 12.34", alloc);
|
|
|
|
EXPECT_TRUE(dir.hasValue());
|
|
|
|
auto *ver = dyn_cast<moduledef::Version>(dir.getValue());
|
|
|
|
EXPECT_TRUE(ver != nullptr);
|
|
|
|
EXPECT_EQ(12, ver->getMajorVersion());
|
|
|
|
EXPECT_EQ(34, ver->getMinorVersion());
|
|
|
|
}
|