1 //===--- CloudABI.cpp - CloudABI ToolChain Implementations ------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "CloudABI.h" 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" 20 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; 26 27 void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA, 28 const InputInfo &Output, 29 const InputInfoList &Inputs, 30 const ArgList &Args, 31 const char *LinkingOutput) const { 32 const ToolChain &ToolChain = getToolChain(); 33 const Driver &D = ToolChain.getDriver(); 34 ArgStringList CmdArgs; 35 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); 43 44 if (!D.SysRoot.empty()) 45 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 46 47 // CloudABI only supports static linkage. 48 CmdArgs.push_back("-Bstatic"); 49 CmdArgs.push_back("--no-dynamic-linker"); 50 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"); 55 } 56 57 CmdArgs.push_back("--eh-frame-hdr"); 58 CmdArgs.push_back("--gc-sections"); 59 60 if (Output.isFilename()) { 61 CmdArgs.push_back("-o"); 62 CmdArgs.push_back(Output.getFilename()); 63 } else { 64 assert(Output.isNothing() && "Invalid output."); 65 } 66 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"))); 70 } 71 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}); 77 78 if (D.isUsingLTO()) 79 AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin, D); 80 81 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 82 83 if (ToolChain.ShouldLinkCXXStdlib(Args)) 84 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 85 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 86 CmdArgs.push_back("-lc"); 87 CmdArgs.push_back("-lcompiler_rt"); 88 } 89 90 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) 91 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o"))); 92 93 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 94 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 95 } 96 97 // CloudABI - CloudABI tool chain which can call ld(1) directly. 98 99 CloudABI::CloudABI(const Driver &D, const llvm::Triple &Triple, 100 const ArgList &Args) 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()); 105 } 106 107 std::string CloudABI::findLibCxxIncludePath() const { 108 SmallString<128> P(getDriver().Dir); 109 llvm::sys::path::append(P, "..", getTriple().str(), "include/c++/v1"); 110 return P.str(); 111 } 112 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"); 118 } 119 120 Tool *CloudABI::buildLinker() const { 121 return new tools::cloudabi::Linker(*this); 122 } 123 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: 131 return true; 132 default: 133 return false; 134 } 135 } 136 137 SanitizerMask CloudABI::getSupportedSanitizers() const { 138 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 139 Res |= SanitizerKind::SafeStack; 140 return Res; 141 } 142 143 SanitizerMask CloudABI::getDefaultSanitizers() const { 144 return SanitizerKind::SafeStack; 145 } 146