17330f729Sjoerg //===--- CloudABI.cpp - CloudABI ToolChain Implementations ------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg
97330f729Sjoerg #include "CloudABI.h"
107330f729Sjoerg #include "InputInfo.h"
117330f729Sjoerg #include "CommonArgs.h"
127330f729Sjoerg #include "clang/Driver/Compilation.h"
137330f729Sjoerg #include "clang/Driver/Driver.h"
147330f729Sjoerg #include "clang/Driver/Options.h"
157330f729Sjoerg #include "llvm/ADT/SmallString.h"
167330f729Sjoerg #include "llvm/Option/ArgList.h"
177330f729Sjoerg #include "llvm/Support/Path.h"
187330f729Sjoerg
197330f729Sjoerg using namespace clang::driver;
207330f729Sjoerg using namespace clang::driver::tools;
217330f729Sjoerg using namespace clang::driver::toolchains;
227330f729Sjoerg using namespace clang;
237330f729Sjoerg using namespace llvm::opt;
247330f729Sjoerg
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const257330f729Sjoerg void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
267330f729Sjoerg const InputInfo &Output,
277330f729Sjoerg const InputInfoList &Inputs,
287330f729Sjoerg const ArgList &Args,
297330f729Sjoerg const char *LinkingOutput) const {
307330f729Sjoerg const ToolChain &ToolChain = getToolChain();
317330f729Sjoerg const Driver &D = ToolChain.getDriver();
327330f729Sjoerg ArgStringList CmdArgs;
337330f729Sjoerg
347330f729Sjoerg // Silence warning for "clang -g foo.o -o foo"
357330f729Sjoerg Args.ClaimAllArgs(options::OPT_g_Group);
367330f729Sjoerg // and "clang -emit-llvm foo.o -o foo"
377330f729Sjoerg Args.ClaimAllArgs(options::OPT_emit_llvm);
387330f729Sjoerg // and for "clang -w foo.o -o foo". Other warning options are already
397330f729Sjoerg // handled somewhere else.
407330f729Sjoerg Args.ClaimAllArgs(options::OPT_w);
417330f729Sjoerg
427330f729Sjoerg if (!D.SysRoot.empty())
437330f729Sjoerg CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
447330f729Sjoerg
457330f729Sjoerg // CloudABI only supports static linkage.
467330f729Sjoerg CmdArgs.push_back("-Bstatic");
477330f729Sjoerg CmdArgs.push_back("--no-dynamic-linker");
487330f729Sjoerg
497330f729Sjoerg // Provide PIE linker flags in case PIE is default for the architecture.
507330f729Sjoerg if (ToolChain.isPIEDefault()) {
517330f729Sjoerg CmdArgs.push_back("-pie");
527330f729Sjoerg CmdArgs.push_back("-zrelro");
537330f729Sjoerg }
547330f729Sjoerg
557330f729Sjoerg CmdArgs.push_back("--eh-frame-hdr");
567330f729Sjoerg CmdArgs.push_back("--gc-sections");
577330f729Sjoerg
587330f729Sjoerg if (Output.isFilename()) {
597330f729Sjoerg CmdArgs.push_back("-o");
607330f729Sjoerg CmdArgs.push_back(Output.getFilename());
617330f729Sjoerg } else {
627330f729Sjoerg assert(Output.isNothing() && "Invalid output.");
637330f729Sjoerg }
647330f729Sjoerg
657330f729Sjoerg if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
667330f729Sjoerg CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
677330f729Sjoerg CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
687330f729Sjoerg }
697330f729Sjoerg
707330f729Sjoerg Args.AddAllArgs(CmdArgs, options::OPT_L);
717330f729Sjoerg ToolChain.AddFilePathLibArgs(Args, CmdArgs);
727330f729Sjoerg Args.AddAllArgs(CmdArgs,
737330f729Sjoerg {options::OPT_T_Group, options::OPT_e, options::OPT_s,
747330f729Sjoerg options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
757330f729Sjoerg
767330f729Sjoerg if (D.isUsingLTO()) {
777330f729Sjoerg assert(!Inputs.empty() && "Must have at least one input.");
78*e038c9c4Sjoerg addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
797330f729Sjoerg D.getLTOMode() == LTOK_Thin);
807330f729Sjoerg }
817330f729Sjoerg
827330f729Sjoerg AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
837330f729Sjoerg
847330f729Sjoerg if (ToolChain.ShouldLinkCXXStdlib(Args))
857330f729Sjoerg ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
867330f729Sjoerg if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
877330f729Sjoerg CmdArgs.push_back("-lc");
887330f729Sjoerg CmdArgs.push_back("-lcompiler_rt");
897330f729Sjoerg }
907330f729Sjoerg
917330f729Sjoerg if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
927330f729Sjoerg CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
937330f729Sjoerg
947330f729Sjoerg const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
95*e038c9c4Sjoerg C.addCommand(std::make_unique<Command>(JA, *this,
96*e038c9c4Sjoerg ResponseFileSupport::AtFileCurCP(),
97*e038c9c4Sjoerg Exec, CmdArgs, Inputs, Output));
987330f729Sjoerg }
997330f729Sjoerg
1007330f729Sjoerg // CloudABI - CloudABI tool chain which can call ld(1) directly.
1017330f729Sjoerg
CloudABI(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)1027330f729Sjoerg CloudABI::CloudABI(const Driver &D, const llvm::Triple &Triple,
1037330f729Sjoerg const ArgList &Args)
1047330f729Sjoerg : Generic_ELF(D, Triple, Args) {
1057330f729Sjoerg SmallString<128> P(getDriver().Dir);
1067330f729Sjoerg llvm::sys::path::append(P, "..", getTriple().str(), "lib");
107*e038c9c4Sjoerg getFilePaths().push_back(std::string(P.str()));
1087330f729Sjoerg }
1097330f729Sjoerg
addLibCxxIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const1107330f729Sjoerg void CloudABI::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
1117330f729Sjoerg llvm::opt::ArgStringList &CC1Args) const {
1127330f729Sjoerg SmallString<128> P(getDriver().Dir);
1137330f729Sjoerg llvm::sys::path::append(P, "..", getTriple().str(), "include/c++/v1");
1147330f729Sjoerg addSystemInclude(DriverArgs, CC1Args, P.str());
1157330f729Sjoerg }
1167330f729Sjoerg
AddCXXStdlibLibArgs(const ArgList & Args,ArgStringList & CmdArgs) const1177330f729Sjoerg void CloudABI::AddCXXStdlibLibArgs(const ArgList &Args,
1187330f729Sjoerg ArgStringList &CmdArgs) const {
1197330f729Sjoerg CmdArgs.push_back("-lc++");
1207330f729Sjoerg CmdArgs.push_back("-lc++abi");
1217330f729Sjoerg CmdArgs.push_back("-lunwind");
1227330f729Sjoerg }
1237330f729Sjoerg
buildLinker() const1247330f729Sjoerg Tool *CloudABI::buildLinker() const {
1257330f729Sjoerg return new tools::cloudabi::Linker(*this);
1267330f729Sjoerg }
1277330f729Sjoerg
isPIEDefault() const1287330f729Sjoerg bool CloudABI::isPIEDefault() const {
1297330f729Sjoerg // Only enable PIE on architectures that support PC-relative
1307330f729Sjoerg // addressing. PC-relative addressing is required, as the process
1317330f729Sjoerg // startup code must be able to relocate itself.
1327330f729Sjoerg switch (getTriple().getArch()) {
1337330f729Sjoerg case llvm::Triple::aarch64:
1347330f729Sjoerg case llvm::Triple::x86_64:
1357330f729Sjoerg return true;
1367330f729Sjoerg default:
1377330f729Sjoerg return false;
1387330f729Sjoerg }
1397330f729Sjoerg }
1407330f729Sjoerg
getSupportedSanitizers() const1417330f729Sjoerg SanitizerMask CloudABI::getSupportedSanitizers() const {
1427330f729Sjoerg SanitizerMask Res = ToolChain::getSupportedSanitizers();
1437330f729Sjoerg Res |= SanitizerKind::SafeStack;
1447330f729Sjoerg return Res;
1457330f729Sjoerg }
1467330f729Sjoerg
getDefaultSanitizers() const1477330f729Sjoerg SanitizerMask CloudABI::getDefaultSanitizers() const {
1487330f729Sjoerg return SanitizerKind::SafeStack;
1497330f729Sjoerg }
150