clang  6.0.0svn
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/Config/config.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 
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) {
103  llvm::sys::path::append(P, "..", getTriple().str(), "lib");
104  getFilePaths().push_back(P.str());
105 }
106 
107 std::string CloudABI::findLibCxxIncludePath() const {
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 
121  return new tools::cloudabi::Linker(*this);
122 }
123 
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 
139  Res |= SanitizerKind::SafeStack;
140  return Res;
141 }
142 
144  return SanitizerKind::SafeStack;
145 }
SanitizerMask getDefaultSanitizers() const override
Return sanitizers which are enabled by default.
Definition: CloudABI.cpp:143
StringRef P
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:490
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:493
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:399
const char * getFilename() const
Definition: InputInfo.h:84
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:113
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:391
void AddGoldPlugin(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, bool IsThinLTO, const Driver &D)
path_list & getFilePaths()
Definition: ToolChain.h:202
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: CloudABI.cpp:137
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
Definition: CloudABI.cpp:124
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:27
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:183
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:740
Tool * buildLinker() const override
Definition: CloudABI.cpp:120
const Driver & getDriver() const
Definition: ToolChain.h:167
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:717
Dataflow Directional Tag Classes.
uint64_t SanitizerMask
Definition: Sanitizers.h:24
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:149
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
virtual bool isPIEDefault() const =0
Test whether this toolchain defaults to PIE.
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
const llvm::Triple & getTriple() const
Definition: ToolChain.h:169
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:776
bool isNothing() const
Definition: InputInfo.h:75
bool isFilename() const
Definition: InputInfo.h:76
std::string findLibCxxIncludePath() const override
Definition: CloudABI.cpp:107
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:723
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:73