summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2015-02-03 00:42:36 +0000
committerRui Ueyama <ruiu@google.com>2015-02-03 00:42:36 +0000
commita6d38a326a60d18bc18ce5f01812118a031b8520 (patch)
tree9046fe5bbd025bcaeb466776f4ba5d3ceee78409
parenta432d176b54fa4a15a1476a8e525df281b81b357 (diff)
downloadbcm5719-llvm-a6d38a326a60d18bc18ce5f01812118a031b8520.tar.gz
bcm5719-llvm-a6d38a326a60d18bc18ce5f01812118a031b8520.zip
ELF: Improve linker script unit tests.
This patch is to enable to write unit tests for linker script with less boilerplate code. llvm-svn: 227902
-rw-r--r--lld/include/lld/Driver/Driver.h5
-rw-r--r--lld/lib/Driver/GnuLdDriver.cpp9
-rw-r--r--lld/unittests/DriverTests/GnuLdDriverTest.cpp99
3 files changed, 52 insertions, 61 deletions
diff --git a/lld/include/lld/Driver/Driver.h b/lld/include/lld/Driver/Driver.h
index b4ff7e40687..9eaa4b4785f 100644
--- a/lld/include/lld/Driver/Driver.h
+++ b/lld/include/lld/Driver/Driver.h
@@ -82,6 +82,10 @@ public:
std::unique_ptr<MemoryBuffer> mb,
raw_ostream &diag);
+ /// A factory method to create an instance of ELFLinkingContext.
+ static std::unique_ptr<ELFLinkingContext>
+ createELFLinkingContext(llvm::Triple triple);
+
private:
static llvm::Triple getDefaultTarget(const char *progName);
static bool applyEmulation(llvm::Triple &triple,
@@ -137,7 +141,6 @@ private:
/// Driver for lld unit tests
class CoreDriver : public Driver {
public:
-
/// Parses command line arguments same as lld-core and performs link.
/// Returns true iff there was an error.
static bool link(int argc, const char *argv[],
diff --git a/lld/lib/Driver/GnuLdDriver.cpp b/lld/lib/Driver/GnuLdDriver.cpp
index 7d975af8339..e4f757e6d1a 100644
--- a/lld/lib/Driver/GnuLdDriver.cpp
+++ b/lld/lib/Driver/GnuLdDriver.cpp
@@ -311,22 +311,21 @@ bool GnuLdDriver::applyEmulation(llvm::Triple &triple,
return true;
}
-#define LLVM_TARGET(targetName) \
- if ((p = elf::targetName##LinkingContext::create(triple))) return p;
-
std::unique_ptr<ELFLinkingContext>
-createELFLinkingContext(llvm::Triple triple) {
+GnuLdDriver::createELFLinkingContext(llvm::Triple triple) {
std::unique_ptr<ELFLinkingContext> p;
// FIXME: #include "llvm/Config/Targets.def"
+#define LLVM_TARGET(targetName) \
+ if ((p = elf::targetName##LinkingContext::create(triple))) return p;
LLVM_TARGET(AArch64)
LLVM_TARGET(ARM)
LLVM_TARGET(Hexagon)
LLVM_TARGET(Mips)
LLVM_TARGET(X86)
LLVM_TARGET(X86_64)
+#undef LLVM_TARGET
return nullptr;
}
-#undef LLVM_TARGET
bool GnuLdDriver::parse(int argc, const char *argv[],
std::unique_ptr<ELFLinkingContext> &context,
diff --git a/lld/unittests/DriverTests/GnuLdDriverTest.cpp b/lld/unittests/DriverTests/GnuLdDriverTest.cpp
index 337b84e984a..b9c2569be30 100644
--- a/lld/unittests/DriverTests/GnuLdDriverTest.cpp
+++ b/lld/unittests/DriverTests/GnuLdDriverTest.cpp
@@ -20,12 +20,31 @@ using namespace llvm;
using namespace lld;
namespace {
+
class GnuLdParserTest
: public ParserTest<GnuLdDriver, std::unique_ptr<ELFLinkingContext>> {
protected:
const LinkingContext *linkingContext() override { return _context.get(); }
};
-}
+
+class LinkerScriptTest : public testing::Test {
+protected:
+ void parse(StringRef script) {
+ llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
+ _ctx = std::move(GnuLdDriver::createELFLinkingContext(triple));
+ std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
+ script, "foo.so");
+ std::string s;
+ raw_string_ostream out(s);
+ std::error_code ec = GnuLdDriver::evalLinkerScript(
+ *_ctx, std::move(mb), out);
+ EXPECT_FALSE(ec);
+ };
+
+ std::unique_ptr<ELFLinkingContext> _ctx;
+};
+
+} // anonymous namespace
TEST_F(GnuLdParserTest, Empty) {
EXPECT_FALSE(parse("ld", nullptr));
@@ -162,58 +181,28 @@ TEST_F(GnuLdParserTest, AsNeeded) {
// Linker script
-TEST_F(GnuLdParserTest, LinkerScriptGroup) {
- parse("ld", "a.o", nullptr);
- std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
- "GROUP(/x /y)", "foo.so");
- std::string s;
- raw_string_ostream out(s);
- std::error_code ec = GnuLdDriver::evalLinkerScript(
- *_context, std::move(mb), out);
- EXPECT_FALSE(ec);
- std::vector<std::unique_ptr<Node>> &nodes = _context->getNodes();
- EXPECT_EQ((size_t)4, nodes.size());
- EXPECT_EQ("/x", cast<FileNode>(nodes[1].get())->getFile()->path());
- EXPECT_EQ("/y", cast<FileNode>(nodes[2].get())->getFile()->path());
- EXPECT_EQ(2, cast<GroupEnd>(nodes[3].get())->getSize());
-}
-
-TEST_F(GnuLdParserTest, LinkerScriptSearchDir) {
- parse("ld", "a.o", nullptr);
- std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
- "SEARCH_DIR(\"/foo/bar\")", "foo.so");
- std::string s;
- raw_string_ostream out(s);
- std::error_code ec = GnuLdDriver::evalLinkerScript(
- *_context, std::move(mb), out);
- EXPECT_FALSE(ec);
- std::vector<StringRef> searchPaths = _context->getSearchPaths();
- EXPECT_EQ((size_t)2, searchPaths.size());
- EXPECT_EQ("/foo/bar", searchPaths[1]);
-}
-
-TEST_F(GnuLdParserTest, LinkerScriptEntry) {
- parse("ld", "a.o", nullptr);
- std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
- "ENTRY(blah)", "foo.so");
- std::string s;
- raw_string_ostream out(s);
- std::error_code ec = GnuLdDriver::evalLinkerScript(
- *_context, std::move(mb), out);
- EXPECT_FALSE(ec);
- StringRef entrySymbol = _context->entrySymbolName();
- EXPECT_EQ("blah", entrySymbol);
-}
-
-TEST_F(GnuLdParserTest, LinkerScriptOutput) {
- parse("ld", "a.o", nullptr);
- std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
- "OUTPUT(\"/path/to/output\")", "foo.so");
- std::string s;
- raw_string_ostream out(s);
- std::error_code ec = GnuLdDriver::evalLinkerScript(
- *_context, std::move(mb), out);
- EXPECT_FALSE(ec);
- StringRef output = _context->outputPath();
- EXPECT_EQ("/path/to/output", output);
+TEST_F(LinkerScriptTest, Group) {
+ parse("GROUP(/x /y)");
+ std::vector<std::unique_ptr<Node>> &nodes = _ctx->getNodes();
+ EXPECT_EQ((size_t)3, nodes.size());
+ EXPECT_EQ("/x", cast<FileNode>(nodes[0].get())->getFile()->path());
+ EXPECT_EQ("/y", cast<FileNode>(nodes[1].get())->getFile()->path());
+ EXPECT_EQ(2, cast<GroupEnd>(nodes[2].get())->getSize());
+}
+
+TEST_F(LinkerScriptTest, SearchDir) {
+ parse("SEARCH_DIR(\"/foo/bar\")");
+ std::vector<StringRef> paths = _ctx->getSearchPaths();
+ EXPECT_EQ((size_t)1, paths.size());
+ EXPECT_EQ("/foo/bar", paths[0]);
+}
+
+TEST_F(LinkerScriptTest, Entry) {
+ parse("ENTRY(blah)");
+ EXPECT_EQ("blah", _ctx->entrySymbolName());
+}
+
+TEST_F(LinkerScriptTest, Output) {
+ parse("OUTPUT(\"/path/to/output\")");
+ EXPECT_EQ("/path/to/output", _ctx->outputPath());
}
OpenPOWER on IntegriCloud