clang  6.0.0svn
CrossWindows.cpp
Go to the documentation of this file.
1 //===--- CrossWindowsToolChain.cpp - Cross Windows Tool Chain -------------===//
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 "CrossWindows.h"
11 #include "CommonArgs.h"
13 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/Options.h"
16 #include "llvm/Option/ArgList.h"
17 #include "llvm/Support/Path.h"
18 
19 using namespace clang::driver;
20 using namespace clang::driver::toolchains;
21 
22 using llvm::opt::ArgList;
23 
25  Compilation &C, const JobAction &JA, const InputInfo &Output,
26  const InputInfoList &Inputs, const ArgList &Args,
27  const char *LinkingOutput) const {
28  claimNoWarnArgs(Args);
29  const auto &TC =
30  static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
31  ArgStringList CmdArgs;
32  const char *Exec;
33 
34  switch (TC.getArch()) {
35  default:
36  llvm_unreachable("unsupported architecture");
37  case llvm::Triple::arm:
38  case llvm::Triple::thumb:
39  case llvm::Triple::aarch64:
40  break;
41  case llvm::Triple::x86:
42  CmdArgs.push_back("--32");
43  break;
44  case llvm::Triple::x86_64:
45  CmdArgs.push_back("--64");
46  break;
47  }
48 
49  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
50 
51  CmdArgs.push_back("-o");
52  CmdArgs.push_back(Output.getFilename());
53 
54  for (const auto &Input : Inputs)
55  CmdArgs.push_back(Input.getFilename());
56 
57  const std::string Assembler = TC.GetProgramPath("as");
58  Exec = Args.MakeArgString(Assembler);
59 
60  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
61 }
62 
64  Compilation &C, const JobAction &JA, const InputInfo &Output,
65  const InputInfoList &Inputs, const ArgList &Args,
66  const char *LinkingOutput) const {
67  const auto &TC =
68  static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
69  const llvm::Triple &T = TC.getTriple();
70  const Driver &D = TC.getDriver();
71  SmallString<128> EntryPoint;
72  ArgStringList CmdArgs;
73  const char *Exec;
74 
75  // Silence warning for "clang -g foo.o -o foo"
76  Args.ClaimAllArgs(options::OPT_g_Group);
77  // and "clang -emit-llvm foo.o -o foo"
78  Args.ClaimAllArgs(options::OPT_emit_llvm);
79  // and for "clang -w foo.o -o foo"
80  Args.ClaimAllArgs(options::OPT_w);
81  // Other warning options are already handled somewhere else.
82 
83  if (!D.SysRoot.empty())
84  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
85 
86  if (Args.hasArg(options::OPT_pie))
87  CmdArgs.push_back("-pie");
88  if (Args.hasArg(options::OPT_rdynamic))
89  CmdArgs.push_back("-export-dynamic");
90  if (Args.hasArg(options::OPT_s))
91  CmdArgs.push_back("--strip-all");
92 
93  CmdArgs.push_back("-m");
94  switch (TC.getArch()) {
95  default:
96  llvm_unreachable("unsupported architecture");
97  case llvm::Triple::arm:
98  case llvm::Triple::thumb:
99  // FIXME: this is incorrect for WinCE
100  CmdArgs.push_back("thumb2pe");
101  break;
102  case llvm::Triple::aarch64:
103  CmdArgs.push_back("arm64pe");
104  break;
105  case llvm::Triple::x86:
106  CmdArgs.push_back("i386pe");
107  EntryPoint.append("_");
108  break;
109  case llvm::Triple::x86_64:
110  CmdArgs.push_back("i386pep");
111  break;
112  }
113 
114  if (Args.hasArg(options::OPT_shared)) {
115  switch (T.getArch()) {
116  default:
117  llvm_unreachable("unsupported architecture");
118  case llvm::Triple::aarch64:
119  case llvm::Triple::arm:
120  case llvm::Triple::thumb:
121  case llvm::Triple::x86_64:
122  EntryPoint.append("_DllMainCRTStartup");
123  break;
124  case llvm::Triple::x86:
125  EntryPoint.append("_DllMainCRTStartup@12");
126  break;
127  }
128 
129  CmdArgs.push_back("-shared");
130  CmdArgs.push_back("-Bdynamic");
131 
132  CmdArgs.push_back("--enable-auto-image-base");
133 
134  CmdArgs.push_back("--entry");
135  CmdArgs.push_back(Args.MakeArgString(EntryPoint));
136  } else {
137  EntryPoint.append("mainCRTStartup");
138 
139  CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
140  : "-Bdynamic");
141 
142  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
143  CmdArgs.push_back("--entry");
144  CmdArgs.push_back(Args.MakeArgString(EntryPoint));
145  }
146 
147  // FIXME: handle subsystem
148  }
149 
150  // NOTE: deal with multiple definitions on Windows (e.g. COMDAT)
151  CmdArgs.push_back("--allow-multiple-definition");
152 
153  CmdArgs.push_back("-o");
154  CmdArgs.push_back(Output.getFilename());
155 
156  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_rdynamic)) {
157  SmallString<261> ImpLib(Output.getFilename());
158  llvm::sys::path::replace_extension(ImpLib, ".lib");
159 
160  CmdArgs.push_back("--out-implib");
161  CmdArgs.push_back(Args.MakeArgString(ImpLib));
162  }
163 
164  Args.AddAllArgs(CmdArgs, options::OPT_L);
165  TC.AddFilePathLibArgs(Args, CmdArgs);
166  AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
167 
168  if (TC.ShouldLinkCXXStdlib(Args)) {
169  bool StaticCXX = Args.hasArg(options::OPT_static_libstdcxx) &&
170  !Args.hasArg(options::OPT_static);
171  if (StaticCXX)
172  CmdArgs.push_back("-Bstatic");
173  TC.AddCXXStdlibLibArgs(Args, CmdArgs);
174  if (StaticCXX)
175  CmdArgs.push_back("-Bdynamic");
176  }
177 
178  if (!Args.hasArg(options::OPT_nostdlib)) {
179  if (!Args.hasArg(options::OPT_nodefaultlibs)) {
180  // TODO handle /MT[d] /MD[d]
181  CmdArgs.push_back("-lmsvcrt");
182  AddRunTimeLibs(TC, D, CmdArgs, Args);
183  }
184  }
185 
186  if (TC.getSanitizerArgs().needsAsanRt()) {
187  // TODO handle /MT[d] /MD[d]
188  if (Args.hasArg(options::OPT_shared)) {
189  CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dll_thunk"));
190  } else {
191  for (const auto &Lib : {"asan_dynamic", "asan_dynamic_runtime_thunk"})
192  CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
193  // Make sure the dynamic runtime thunk is not optimized out at link time
194  // to ensure proper SEH handling.
195  CmdArgs.push_back(Args.MakeArgString("--undefined"));
196  CmdArgs.push_back(Args.MakeArgString(TC.getArch() == llvm::Triple::x86
197  ? "___asan_seh_interceptor"
198  : "__asan_seh_interceptor"));
199  }
200  }
201 
202  Exec = Args.MakeArgString(TC.GetLinkerPath());
203 
204  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
205 }
206 
208  const llvm::Triple &T,
209  const llvm::opt::ArgList &Args)
210  : Generic_GCC(D, T, Args) {}
211 
212 bool CrossWindowsToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
213  // FIXME: all non-x86 targets need unwind tables, however, LLVM currently does
214  // not know how to emit them.
215  return getArch() == llvm::Triple::x86_64;
216 }
217 
219  return getArch() == llvm::Triple::x86_64;
220 }
221 
223  return getArch() == llvm::Triple::x86_64;
224 }
225 
227  return getArch() == llvm::Triple::x86_64;
228 }
229 
231 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
232  llvm::opt::ArgStringList &CC1Args) const {
233  const Driver &D = getDriver();
234  const std::string &SysRoot = D.SysRoot;
235 
236  auto AddSystemAfterIncludes = [&]() {
237  for (const auto &P : DriverArgs.getAllArgValues(options::OPT_isystem_after))
238  addSystemInclude(DriverArgs, CC1Args, P);
239  };
240 
241  if (DriverArgs.hasArg(options::OPT_nostdinc)) {
242  AddSystemAfterIncludes();
243  return;
244  }
245 
246  addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
247  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
248  SmallString<128> ResourceDir(D.ResourceDir);
249  llvm::sys::path::append(ResourceDir, "include");
250  addSystemInclude(DriverArgs, CC1Args, ResourceDir);
251  }
252  AddSystemAfterIncludes();
253  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
254 }
255 
257 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
258  llvm::opt::ArgStringList &CC1Args) const {
259  const std::string &SysRoot = getDriver().SysRoot;
260 
261  if (DriverArgs.hasArg(options::OPT_nostdinc) ||
262  DriverArgs.hasArg(options::OPT_nostdincxx))
263  return;
264 
265  if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx)
266  addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include/c++/v1");
267 }
268 
270 AddCXXStdlibLibArgs(const llvm::opt::ArgList &DriverArgs,
271  llvm::opt::ArgStringList &CC1Args) const {
272  if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx)
273  CC1Args.push_back("-lc++");
274 }
275 
278  Res |= SanitizerKind::Address;
279  return Res;
280 }
281 
283  return new tools::CrossWindows::Linker(*this);
284 }
285 
287  return new tools::CrossWindows::Assembler(*this);
288 }
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments...
Definition: ToolChain.cpp:633
Generic_GCC - A tool chain using the &#39;gcc&#39; command to perform all subcommands; this relies on gcc tra...
Definition: Gnu.h:140
StringRef P
bool isPICDefault() const override
Test whether this toolchain defaults to PIC.
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
const char * getFilename() const
Definition: InputInfo.h:84
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
CrossWindowsToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args)
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
const FunctionProtoType * T
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
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...
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:175
const Driver & getDriver() const
Definition: ToolChain.h:164
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override
IsUnwindTablesDefault - Does this tool chain use -funwind-tables by default.
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:618
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
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:730
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...
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:599
bool isPICDefaultForced() const override
Tests whether this toolchain forces its default for PIC, PIE or non-PIC.
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...
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:139