clang 8.0.0

CloudABI.cpp
Go to the documentation of this file.
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/Driver/Compilation.h"
14 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/Options.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Support/Path.h"
19
20 using namespace clang::driver;
21 using namespace clang::driver::tools;
22 using namespace clang::driver::toolchains;
23 using namespace clang;
24 using namespace llvm::opt;
25
26 void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
27 const InputInfo &Output,
28 const InputInfoList &Inputs,
29 const ArgList &Args,
30 const char *LinkingOutput) const {
31 const ToolChain &ToolChain = getToolChain();
32 const Driver &D = ToolChain.getDriver();
33 ArgStringList CmdArgs;
34
35 // Silence warning for "clang -g foo.o -o foo"
36 Args.ClaimAllArgs(options::OPT_g_Group);
37 // and "clang -emit-llvm foo.o -o foo"
38 Args.ClaimAllArgs(options::OPT_emit_llvm);
39 // and for "clang -w foo.o -o foo". Other warning options are already
40 // handled somewhere else.
41 Args.ClaimAllArgs(options::OPT_w);
42
43 if (!D.SysRoot.empty())
44 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
45
46 // CloudABI only supports static linkage.
47 CmdArgs.push_back("-Bstatic");
48 CmdArgs.push_back("--no-dynamic-linker");
49
50 // Provide PIE linker flags in case PIE is default for the architecture.
51 if (ToolChain.isPIEDefault()) {
52 CmdArgs.push_back("-pie");
53 CmdArgs.push_back("-zrelro");
54 }
55
56 CmdArgs.push_back("--eh-frame-hdr");
57 CmdArgs.push_back("--gc-sections");
58
59 if (Output.isFilename()) {
60 CmdArgs.push_back("-o");
61 CmdArgs.push_back(Output.getFilename());
62 } else {
63 assert(Output.isNothing() && "Invalid output.");
64 }
65
66 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
67 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
68 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
69 }
70
71 Args.AddAllArgs(CmdArgs, options::OPT_L);
72 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
73 Args.AddAllArgs(CmdArgs,
74 {options::OPT_T_Group, options::OPT_e, options::OPT_s,
75 options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
76
77 if (D.isUsingLTO()) {
78 assert(!Inputs.empty() && "Must have at least one input.");
79 AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
80 D.getLTOMode() == LTOK_Thin);
81 }
82
83 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
84
85 if (ToolChain.ShouldLinkCXXStdlib(Args))
86 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
87 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
88 CmdArgs.push_back("-lc");
89 CmdArgs.push_back("-lcompiler_rt");
90 }
91
92 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
93 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
94
95 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
96 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
97 }
98
99 // CloudABI - CloudABI tool chain which can call ld(1) directly.
100
101 CloudABI::CloudABI(const Driver &D, const llvm::Triple &Triple,
102 const ArgList &Args)
103 : Generic_ELF(D, Triple, Args) {
104 SmallString<128> P(getDriver().Dir);
105 llvm::sys::path::append(P, "..", getTriple().str(), "lib");
106 getFilePaths().push_back(P.str());
107 }
108
109 void CloudABI::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
110 llvm::opt::ArgStringList &CC1Args) const {
111 SmallString<128> P(getDriver().Dir);
112 llvm::sys::path::append(P, "..", getTriple().str(), "include/c++/v1");
113 addSystemInclude(DriverArgs, CC1Args, P.str());
114 }
115
116 void CloudABI::AddCXXStdlibLibArgs(const ArgList &Args,
117 ArgStringList &CmdArgs) const {
118 CmdArgs.push_back("-lc++");
119 CmdArgs.push_back("-lc++abi");
120 CmdArgs.push_back("-lunwind");
121 }
122
123 Tool *CloudABI::buildLinker() const {
124 return new tools::cloudabi::Linker(*this);
125 }
126
127 bool CloudABI::isPIEDefault() const {
128 // Only enable PIE on architectures that support PC-relative
129 // addressing. PC-relative addressing is required, as the process
130 // startup code must be able to relocate itself.
131 switch (getTriple().getArch()) {
132 case llvm::Triple::aarch64:
133 case llvm::Triple::x86_64:
134 return true;
135 default:
136 return false;
137 }
138 }
139
140 SanitizerMask CloudABI::getSupportedSanitizers() const {
141 SanitizerMask Res = ToolChain::getSupportedSanitizers();
142 Res |= SanitizerKind::SafeStack;
143 return Res;
144 }
145
146 SanitizerMask CloudABI::getDefaultSanitizers() const {
147 return SanitizerKind::SafeStack;
148 }
CloudABI.h
clang::driver::toolchains::CloudABI::getDefaultSanitizers
SanitizerMask getDefaultSanitizers() const override
Return sanitizers which are enabled by default.
Definition: CloudABI.cpp:146
P
StringRef P
Definition: ASTMatchersInternal.cpp:439
clang::driver::Driver::isUsingLTO
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:520
clang::driver::Driver::getLTOMode
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:523
clang::driver::ToolChain::GetLinkerPath
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:439
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:84
CommonArgs.h
clang::driver::JobAction
Definition: Action.h:374
clang::driver::toolchains::CloudABI::AddCXXStdlibLibArgs
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:116
clang::driver::tools::AddGoldPlugin
void AddGoldPlugin(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
clang::driver::tools
Definition: AMDGPU.h:21
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
clang::driver::ToolChain::GetFilePath
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:431
clang::driver::ToolChain::getFilePaths
path_list & getFilePaths()
Definition: ToolChain.h:224
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:58
clang::driver::tools::cloudabi::Linker
Definition: CloudABI.h:23
llvm::SmallString
Definition: LLVM.h:35
Driver.h
clang::driver::toolchains::CloudABI::getSupportedSanitizers
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: CloudABI.cpp:140
Compilation.h
llvm::str
return Out str()
clang::driver::toolchains::Generic_ELF
Definition: Gnu.h:340
llvm::opt
Definition: Action.h:24
clang::driver::Compilation::addCommand
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:206
clang::driver::toolchains::CloudABI::isPIEDefault
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
Definition: CloudABI.cpp:127
clang::driver::tools::cloudabi::Linker::ConstructJob
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:26
clang::driver::ToolChain::getArch
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:202
clang::driver::ToolChain::AddFilePathLibArgs
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:778
clang::driver::toolchains::CloudABI::buildLinker
Tool * buildLinker() const override
Definition: CloudABI.cpp:123
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:186
llvm::SmallVector
Definition: LLVM.h:36
clang::driver::ToolChain::addSystemInclude
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:702
Options.h
clang::driver::ToolChain::ShouldLinkCXXStdlib
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:755
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::driver::LTOK_Thin
Definition: Driver.h:52
clang::driver::tools::AddLinkerInputs
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
InputInfo.h
clang::SanitizerMask
uint64_t SanitizerMask
Definition: Sanitizers.h:26
clang::driver::ToolChain::isPIEDefault
virtual bool isPIEDefault() const =0
Test whether this toolchain defaults to PIE.
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:46
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:188
clang::driver
Definition: Action.h:32
clang::driver::ToolChain::getSupportedSanitizers
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:818
clang::driver::toolchains
Definition: AMDGPU.h:41
clang::driver::InputInfo::isNothing
bool isNothing() const
Definition: InputInfo.h:75
clang::driver::InputInfo::isFilename
bool isFilename() const
Definition: InputInfo.h:76
clang::driver::toolchains::CloudABI::addLibCxxIncludePaths
void addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: CloudABI.cpp:109
clang::driver::ToolChain::AddCXXStdlibLibArgs
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:761
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:89