1 //===--- CloudABI.cpp - CloudABI ToolChain Implementations ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "InputInfo.h"
12 #include "CommonArgs.h"
13 #include "clang/Config/config.h"
14 #include "clang/Driver/Compilation.h"
15 #include "clang/Driver/Driver.h"
16 #include "clang/Driver/Options.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Support/Path.h"
21 using namespace clang::driver;
22 using namespace clang::driver::tools;
23 using namespace clang::driver::toolchains;
24 using namespace clang;
25 using namespace llvm::opt;
27 void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
28 const InputInfo &Output,
29 const InputInfoList &Inputs,
31 const char *LinkingOutput) const {
32 const ToolChain &ToolChain = getToolChain();
33 const Driver &D = ToolChain.getDriver();
34 ArgStringList CmdArgs;
36 // Silence warning for "clang -g foo.o -o foo"
37 Args.ClaimAllArgs(options::OPT_g_Group);
38 // and "clang -emit-llvm foo.o -o foo"
39 Args.ClaimAllArgs(options::OPT_emit_llvm);
40 // and for "clang -w foo.o -o foo". Other warning options are already
41 // handled somewhere else.
42 Args.ClaimAllArgs(options::OPT_w);
44 if (!D.SysRoot.empty())
45 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
47 // CloudABI only supports static linkage.
48 CmdArgs.push_back("-Bstatic");
49 CmdArgs.push_back("--no-dynamic-linker");
51 // Provide PIE linker flags in case PIE is default for the architecture.
52 if (ToolChain.isPIEDefault()) {
53 CmdArgs.push_back("-pie");
54 CmdArgs.push_back("-zrelro");
57 CmdArgs.push_back("--eh-frame-hdr");
58 CmdArgs.push_back("--gc-sections");
60 if (Output.isFilename()) {
61 CmdArgs.push_back("-o");
62 CmdArgs.push_back(Output.getFilename());
64 assert(Output.isNothing() && "Invalid output.");
67 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
68 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
69 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
72 Args.AddAllArgs(CmdArgs, options::OPT_L);
73 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
74 Args.AddAllArgs(CmdArgs,
75 {options::OPT_T_Group, options::OPT_e, options::OPT_s,
76 options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
79 AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin, D);
81 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
83 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
85 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
86 CmdArgs.push_back("-lc");
87 CmdArgs.push_back("-lcompiler_rt");
90 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
91 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
93 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
94 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
97 // CloudABI - CloudABI tool chain which can call ld(1) directly.
99 CloudABI::CloudABI(const Driver &D, const llvm::Triple &Triple,
101 : Generic_ELF(D, Triple, Args) {
102 SmallString<128> P(getDriver().Dir);
103 llvm::sys::path::append(P, "..", getTriple().str(), "lib");
104 getFilePaths().push_back(P.str());
107 std::string CloudABI::findLibCxxIncludePath() const {
108 SmallString<128> P(getDriver().Dir);
109 llvm::sys::path::append(P, "..", getTriple().str(), "include/c++/v1");
113 void CloudABI::AddCXXStdlibLibArgs(const ArgList &Args,
114 ArgStringList &CmdArgs) const {
115 CmdArgs.push_back("-lc++");
116 CmdArgs.push_back("-lc++abi");
117 CmdArgs.push_back("-lunwind");
120 Tool *CloudABI::buildLinker() const {
121 return new tools::cloudabi::Linker(*this);
124 bool CloudABI::isPIEDefault() const {
125 // Only enable PIE on architectures that support PC-relative
126 // addressing. PC-relative addressing is required, as the process
127 // startup code must be able to relocate itself.
128 switch (getTriple().getArch()) {
129 case llvm::Triple::aarch64:
130 case llvm::Triple::x86_64:
137 SanitizerMask CloudABI::getSupportedSanitizers() const {
138 SanitizerMask Res = ToolChain::getSupportedSanitizers();
139 Res |= SanitizerKind::SafeStack;
143 SanitizerMask CloudABI::getDefaultSanitizers() const {
144 return SanitizerKind::SafeStack;