2013-06-12 07:07:43 +08:00
|
|
|
//===- lld/unittest/DarwinLdDriverTest.cpp --------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Darwin's ld driver tests.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-02-29 04:56:34 +08:00
|
|
|
#include "lld/Driver/Driver.h"
|
2013-08-07 06:31:59 +08:00
|
|
|
#include "lld/ReaderWriter/MachOLinkingContext.h"
|
2014-10-18 13:33:55 +08:00
|
|
|
#include "llvm/Support/MachO.h"
|
2016-02-29 04:56:34 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "gtest/gtest.h"
|
2013-06-12 07:07:43 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
|
2016-03-03 03:08:05 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace mach_o {
|
|
|
|
bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx,
|
|
|
|
raw_ostream &diagnostics);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 07:07:43 +08:00
|
|
|
namespace {
|
2016-02-29 04:56:34 +08:00
|
|
|
class DarwinLdParserTest : public testing::Test {
|
2013-06-12 07:07:43 +08:00
|
|
|
protected:
|
2016-02-29 04:56:34 +08:00
|
|
|
int inputFileCount() { return _ctx.getNodes().size(); }
|
|
|
|
|
|
|
|
std::string inputFile(int index) {
|
|
|
|
Node &node = *_ctx.getNodes()[index];
|
|
|
|
if (node.kind() == Node::Kind::File)
|
|
|
|
return cast<FileNode>(&node)->getFile()->path();
|
|
|
|
llvm_unreachable("not handling other types of input files");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse(std::vector<const char *> args) {
|
|
|
|
args.insert(args.begin(), "ld");
|
|
|
|
std::string errorMessage;
|
|
|
|
raw_string_ostream os(errorMessage);
|
2016-03-03 03:08:05 +08:00
|
|
|
return mach_o::parse(args, _ctx, os);
|
2016-02-29 04:56:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MachOLinkingContext _ctx;
|
2013-06-12 07:07:43 +08:00
|
|
|
};
|
2014-03-28 07:34:32 +08:00
|
|
|
}
|
2013-06-12 07:07:43 +08:00
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, Basic) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"foo.o", "bar.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_FALSE(_ctx.allowRemainingUndefines());
|
|
|
|
EXPECT_FALSE(_ctx.deadStrip());
|
2013-07-17 02:45:57 +08:00
|
|
|
EXPECT_EQ(2, inputFileCount());
|
|
|
|
EXPECT_EQ("foo.o", inputFile(0));
|
|
|
|
EXPECT_EQ("bar.o", inputFile(1));
|
2013-06-12 07:07:43 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:13:13 +08:00
|
|
|
TEST_F(DarwinLdParserTest, Output) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-o", "my.out", "foo.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ("my.out", _ctx.outputPath());
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
2013-06-12 07:07:43 +08:00
|
|
|
TEST_F(DarwinLdParserTest, Dylib) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-dylib", "foo.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(llvm::MachO::MH_DYLIB, _ctx.outputMachOType());
|
2013-06-12 07:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, Relocatable) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-r", "foo.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(llvm::MachO::MH_OBJECT, _ctx.outputMachOType());
|
2013-06-12 07:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, Bundle) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-bundle", "foo.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(llvm::MachO::MH_BUNDLE, _ctx.outputMachOType());
|
2013-06-12 07:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, Preload) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-preload", "foo.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(llvm::MachO::MH_PRELOAD, _ctx.outputMachOType());
|
2013-06-12 07:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, Static) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-static", "foo.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(llvm::MachO::MH_EXECUTE, _ctx.outputMachOType());
|
2013-06-12 07:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, Entry) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-e", "entryFunc", "foo.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ("entryFunc", _ctx.entrySymbolName());
|
2013-06-12 07:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, DeadStrip) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_TRUE(_ctx.deadStrip());
|
2013-06-12 07:07:43 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:13:13 +08:00
|
|
|
TEST_F(DarwinLdParserTest, DeadStripRootsExe) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip", "foo.o"}));
|
2016-01-23 05:13:24 +08:00
|
|
|
EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, DeadStripRootsRelocatable) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "x86_64", "-r", "-dead_strip", "foo.o"}));
|
2016-01-23 05:13:24 +08:00
|
|
|
EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicExe) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(
|
|
|
|
parse({"-arch", "x86_64", "-dead_strip", "-export_dynamic", "foo.o"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
2016-01-23 05:13:24 +08:00
|
|
|
TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicDylib) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip",
|
|
|
|
"-export_dynamic", "foo.o"}));
|
2016-01-23 05:13:24 +08:00
|
|
|
EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicRelocatable) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse(
|
|
|
|
{"-arch", "x86_64", "-r", "-dead_strip", "-export_dynamic", "foo.o"}));
|
2016-01-23 05:13:24 +08:00
|
|
|
EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
|
|
|
|
}
|
|
|
|
|
2013-06-12 07:07:43 +08:00
|
|
|
TEST_F(DarwinLdParserTest, Arch) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "x86_64", "foo.o"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::arch_x86_64, _ctx.arch());
|
|
|
|
EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_X86_64, _ctx.getCPUType());
|
|
|
|
EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_64_ALL, _ctx.getCPUSubType());
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, Arch_x86) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "i386", "foo.o"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::arch_x86, _ctx.arch());
|
|
|
|
EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_I386, _ctx.getCPUType());
|
|
|
|
EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_ALL, _ctx.getCPUSubType());
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, Arch_armv6) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "armv6", "foo.o"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::arch_armv6, _ctx.arch());
|
|
|
|
EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
|
|
|
|
EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V6, _ctx.getCPUSubType());
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, Arch_armv7) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "armv7", "foo.o"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::arch_armv7, _ctx.arch());
|
|
|
|
EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
|
|
|
|
EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7, _ctx.getCPUSubType());
|
2013-06-12 07:07:43 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:13:13 +08:00
|
|
|
TEST_F(DarwinLdParserTest, Arch_armv7s) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-arch", "armv7s", "foo.o"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::arch_armv7s, _ctx.arch());
|
|
|
|
EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
|
|
|
|
EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7S, _ctx.getCPUSubType());
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, MinMacOSX10_7) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(
|
|
|
|
parse({"-macosx_version_min", "10.7", "foo.o", "-arch", "x86_64"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
|
|
|
|
EXPECT_TRUE(_ctx.minOS("10.7", ""));
|
|
|
|
EXPECT_FALSE(_ctx.minOS("10.8", ""));
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, MinMacOSX10_8) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(
|
|
|
|
parse({"-macosx_version_min", "10.8.3", "foo.o", "-arch", "x86_64"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
|
|
|
|
EXPECT_TRUE(_ctx.minOS("10.7", ""));
|
|
|
|
EXPECT_TRUE(_ctx.minOS("10.8", ""));
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, iOS5) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-ios_version_min", "5.0", "foo.o", "-arch", "armv7"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
|
|
|
|
EXPECT_TRUE(_ctx.minOS("", "5.0"));
|
|
|
|
EXPECT_FALSE(_ctx.minOS("", "6.0"));
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, iOS6) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse({"-ios_version_min", "6.0", "foo.o", "-arch", "armv7"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
|
|
|
|
EXPECT_TRUE(_ctx.minOS("", "5.0"));
|
|
|
|
EXPECT_TRUE(_ctx.minOS("", "6.0"));
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, iOS_Simulator5) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(
|
|
|
|
parse({"-ios_simulator_version_min", "5.0", "a.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
|
|
|
|
EXPECT_TRUE(_ctx.minOS("", "5.0"));
|
|
|
|
EXPECT_FALSE(_ctx.minOS("", "6.0"));
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, iOS_Simulator6) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(
|
|
|
|
parse({"-ios_simulator_version_min", "6.0", "a.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
|
|
|
|
EXPECT_TRUE(_ctx.minOS("", "5.0"));
|
|
|
|
EXPECT_TRUE(_ctx.minOS("", "6.0"));
|
2013-07-19 07:13:13 +08:00
|
|
|
}
|
|
|
|
|
2013-09-11 07:55:14 +08:00
|
|
|
TEST_F(DarwinLdParserTest, compatibilityVersion) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_TRUE(parse(
|
|
|
|
{"-dylib", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(_ctx.compatibilityVersion(), 0x10203U);
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, compatibilityVersionInvalidType) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_FALSE(parse(
|
|
|
|
{"-bundle", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, compatibilityVersionInvalidValue) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_FALSE(parse(
|
|
|
|
{"-bundle", "-compatibility_version", "1,2,3", "a.o", "-arch", "i386"}));
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, currentVersion) {
|
2013-09-25 07:26:34 +08:00
|
|
|
EXPECT_TRUE(
|
2016-02-29 04:56:34 +08:00
|
|
|
parse({"-dylib", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(_ctx.currentVersion(), 0x10203U);
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, currentVersionInvalidType) {
|
2013-09-25 07:26:34 +08:00
|
|
|
EXPECT_FALSE(
|
2016-02-29 04:56:34 +08:00
|
|
|
parse({"-bundle", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, currentVersionInvalidValue) {
|
2013-09-25 07:26:34 +08:00
|
|
|
EXPECT_FALSE(
|
2016-02-29 04:56:34 +08:00
|
|
|
parse({"-bundle", "-current_version", "1,2,3", "a.o", "-arch", "i386"}));
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, bundleLoader) {
|
2013-09-25 07:26:34 +08:00
|
|
|
EXPECT_TRUE(
|
2016-02-29 04:56:34 +08:00
|
|
|
parse({"-bundle", "-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(_ctx.bundleLoader(), "/bin/ls");
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, bundleLoaderInvalidType) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_FALSE(parse({"-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, deadStrippableDylib) {
|
2013-09-25 07:26:34 +08:00
|
|
|
EXPECT_TRUE(
|
2016-02-29 04:56:34 +08:00
|
|
|
parse({"-dylib", "-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
|
2015-02-11 05:28:52 +08:00
|
|
|
EXPECT_EQ(true, _ctx.deadStrippableDylib());
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DarwinLdParserTest, deadStrippableDylibInvalidType) {
|
2016-02-29 04:56:34 +08:00
|
|
|
EXPECT_FALSE(parse({"-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|