clang 17.0.0git
Go to the documentation of this file.
1//===--- CloudABI.cpp - CloudABI ToolChain Implementations ------*- C++ -*-===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9#include "CloudABI.h"
10#include "CommonArgs.h"
12#include "clang/Driver/Driver.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/Option/ArgList.h"
17#include "llvm/Support/Path.h"
19using namespace clang::driver;
20using namespace clang::driver::tools;
21using namespace clang::driver::toolchains;
22using namespace clang;
23using namespace llvm::opt;
26 const InputInfo &Output,
27 const InputInfoList &Inputs,
28 const ArgList &Args,
29 const char *LinkingOutput) const {
31 const Driver &D = ToolChain.getDriver();
32 ArgStringList CmdArgs;
34 // Silence warning for "clang -g foo.o -o foo"
35 Args.ClaimAllArgs(options::OPT_g_Group);
36 // and "clang -emit-llvm foo.o -o foo"
37 Args.ClaimAllArgs(options::OPT_emit_llvm);
38 // and for "clang -w foo.o -o foo". Other warning options are already
39 // handled somewhere else.
40 Args.ClaimAllArgs(options::OPT_w);
42 if (!D.SysRoot.empty())
43 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
45 // CloudABI only supports static linkage.
46 CmdArgs.push_back("-Bstatic");
47 CmdArgs.push_back("--no-dynamic-linker");
49 // Provide PIE linker flags in case PIE is default for the architecture.
50 if (ToolChain.isPIEDefault(Args)) {
51 CmdArgs.push_back("-pie");
52 CmdArgs.push_back("-zrelro");
53 }
55 CmdArgs.push_back("--eh-frame-hdr");
56 CmdArgs.push_back("--gc-sections");
58 if (Output.isFilename()) {
59 CmdArgs.push_back("-o");
60 CmdArgs.push_back(Output.getFilename());
61 } else {
62 assert(Output.isNothing() && "Invalid output.");
63 }
65 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
66 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
67 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
68 }
70 Args.AddAllArgs(CmdArgs, options::OPT_L);
71 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
72 Args.AddAllArgs(CmdArgs,
73 {options::OPT_T_Group, options::OPT_e, options::OPT_s,
74 options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
76 if (D.isUsingLTO()) {
77 assert(!Inputs.empty() && "Must have at least one input.");
78 addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
79 D.getLTOMode() == LTOK_Thin);
80 }
82 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
85 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
86 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
87 CmdArgs.push_back("-lc");
88 CmdArgs.push_back("-lcompiler_rt");
89 }
91 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
92 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
94 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
95 C.addCommand(std::make_unique<Command>(JA, *this,
97 Exec, CmdArgs, Inputs, Output));
100// CloudABI - CloudABI tool chain which can call ld(1) directly.
102CloudABI::CloudABI(const Driver &D, const llvm::Triple &Triple,
103 const ArgList &Args)
104 : Generic_ELF(D, Triple, Args) {
106 llvm::sys::path::append(P, "..", getTriple().str(), "lib");
107 getFilePaths().push_back(std::string(P.str()));
110void CloudABI::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
111 llvm::opt::ArgStringList &CC1Args) const {
113 llvm::sys::path::append(P, "..", getTriple().str(), "include/c++/v1");
114 addSystemInclude(DriverArgs, CC1Args, P.str());
117void CloudABI::AddCXXStdlibLibArgs(const ArgList &Args,
118 ArgStringList &CmdArgs) const {
119 CmdArgs.push_back("-lc++");
120 if (Args.hasArg(options::OPT_fexperimental_library))
121 CmdArgs.push_back("-lc++experimental");
122 CmdArgs.push_back("-lc++abi");
123 CmdArgs.push_back("-lunwind");
127 return new tools::cloudabi::Linker(*this);
130bool CloudABI::isPIEDefault(const llvm::opt::ArgList &Args) const {
131 // Only enable PIE on architectures that support PC-relative
132 // addressing. PC-relative addressing is required, as the process
133 // startup code must be able to relocate itself.
134 switch (getTriple().getArch()) {
135 case llvm::Triple::aarch64:
136 case llvm::Triple::x86_64:
137 return true;
138 default:
139 return false;
140 }
145 Res |= SanitizerKind::SafeStack;
146 return Res;
150 return SanitizerKind::SafeStack;
StringRef P
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
std::string SysRoot
sysroot, if present
Definition: Driver.h:183
LTOKind getLTOMode(bool IsOffload=false) const
Get the specific kind of LTO being performed.
Definition: Driver.h:704
bool isUsingLTO(bool IsOffload=false) const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:699
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
const char * getFilename() const
Definition: InputInfo.h:83
bool isNothing() const
Definition: InputInfo.h:74
bool isFilename() const
Definition: InputInfo.h:75
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:969
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:1071
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:660
path_list & getFilePaths()
Definition: ToolChain.h:278
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:252
const Driver & getDriver() const
Definition: ToolChain.h:236
virtual bool isPIEDefault(const llvm::opt::ArgList &Args) const =0
Test whether this toolchain defaults to PIE.
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:1077
const llvm::Triple & getTriple() const
Definition: ToolChain.h:238
std::string GetLinkerPath(bool *LinkerIsLLD=nullptr) const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name.
Definition: ToolChain.cpp:668
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:1096
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:1144
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
const ToolChain & getToolChain() const
Definition: Tool.h:52
void addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: CloudABI.cpp:110
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: CloudABI.cpp:117
SanitizerMask getDefaultSanitizers() const override
Return sanitizers which are enabled by default.
Definition: CloudABI.cpp:149
Tool * buildLinker() const override
Definition: CloudABI.cpp:126
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: CloudABI.cpp:143
CloudABI(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: CloudABI.cpp:102
bool isPIEDefault(const llvm::opt::ArgList &Args) const override
Test whether this toolchain defaults to PIE.
Definition: CloudABI.cpp:130
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: CloudABI.cpp:25
void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
@ C
Languages that the frontend can parse and compile.
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:92