xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/CrossWindows.cpp (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 //===-- CrossWindows.cpp - Cross Windows Tool Chain -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CrossWindows.h"
10 #include "CommonArgs.h"
11 #include "clang/Driver/Compilation.h"
12 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/Options.h"
14 #include "clang/Driver/SanitizerArgs.h"
15 #include "llvm/Option/ArgList.h"
16 #include "llvm/Support/Path.h"
17 
18 using namespace clang::driver;
19 using namespace clang::driver::toolchains;
20 
21 using llvm::opt::ArgList;
22 using llvm::opt::ArgStringList;
23 
24 void tools::CrossWindows::Assembler::ConstructJob(
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(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
61                                          Exec, CmdArgs, Inputs, Output));
62 }
63 
64 void tools::CrossWindows::Linker::ConstructJob(
65     Compilation &C, const JobAction &JA, const InputInfo &Output,
66     const InputInfoList &Inputs, const ArgList &Args,
67     const char *LinkingOutput) const {
68   const auto &TC =
69       static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
70   const llvm::Triple &T = TC.getTriple();
71   const Driver &D = TC.getDriver();
72   SmallString<128> EntryPoint;
73   ArgStringList CmdArgs;
74   const char *Exec;
75 
76   // Silence warning for "clang -g foo.o -o foo"
77   Args.ClaimAllArgs(options::OPT_g_Group);
78   // and "clang -emit-llvm foo.o -o foo"
79   Args.ClaimAllArgs(options::OPT_emit_llvm);
80   // and for "clang -w foo.o -o foo"
81   Args.ClaimAllArgs(options::OPT_w);
82   // Other warning options are already handled somewhere else.
83 
84   if (!D.SysRoot.empty())
85     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
86 
87   if (Args.hasArg(options::OPT_pie))
88     CmdArgs.push_back("-pie");
89   if (Args.hasArg(options::OPT_rdynamic))
90     CmdArgs.push_back("-export-dynamic");
91   if (Args.hasArg(options::OPT_s))
92     CmdArgs.push_back("--strip-all");
93 
94   CmdArgs.push_back("-m");
95   switch (TC.getArch()) {
96   default:
97     llvm_unreachable("unsupported architecture");
98   case llvm::Triple::arm:
99   case llvm::Triple::thumb:
100     // FIXME: this is incorrect for WinCE
101     CmdArgs.push_back("thumb2pe");
102     break;
103   case llvm::Triple::aarch64:
104     CmdArgs.push_back("arm64pe");
105     break;
106   case llvm::Triple::x86:
107     CmdArgs.push_back("i386pe");
108     EntryPoint.append("_");
109     break;
110   case llvm::Triple::x86_64:
111     CmdArgs.push_back("i386pep");
112     break;
113   }
114 
115   if (Args.hasArg(options::OPT_shared)) {
116     switch (T.getArch()) {
117     default:
118       llvm_unreachable("unsupported architecture");
119     case llvm::Triple::aarch64:
120     case llvm::Triple::arm:
121     case llvm::Triple::thumb:
122     case llvm::Triple::x86_64:
123       EntryPoint.append("_DllMainCRTStartup");
124       break;
125     case llvm::Triple::x86:
126       EntryPoint.append("_DllMainCRTStartup@12");
127       break;
128     }
129 
130     CmdArgs.push_back("-shared");
131     CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
132                                                        : "-Bdynamic");
133 
134     CmdArgs.push_back("--enable-auto-image-base");
135 
136     CmdArgs.push_back("--entry");
137     CmdArgs.push_back(Args.MakeArgString(EntryPoint));
138   } else {
139     EntryPoint.append("mainCRTStartup");
140 
141     CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
142                                                        : "-Bdynamic");
143 
144     if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
145       CmdArgs.push_back("--entry");
146       CmdArgs.push_back(Args.MakeArgString(EntryPoint));
147     }
148 
149     // FIXME: handle subsystem
150   }
151 
152   // NOTE: deal with multiple definitions on Windows (e.g. COMDAT)
153   CmdArgs.push_back("--allow-multiple-definition");
154 
155   CmdArgs.push_back("-o");
156   CmdArgs.push_back(Output.getFilename());
157 
158   if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_rdynamic)) {
159     SmallString<261> ImpLib(Output.getFilename());
160     llvm::sys::path::replace_extension(ImpLib, ".lib");
161 
162     CmdArgs.push_back("--out-implib");
163     CmdArgs.push_back(Args.MakeArgString(ImpLib));
164   }
165 
166   Args.AddAllArgs(CmdArgs, options::OPT_L);
167   TC.AddFilePathLibArgs(Args, CmdArgs);
168   AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
169 
170   if (TC.ShouldLinkCXXStdlib(Args)) {
171     bool StaticCXX = Args.hasArg(options::OPT_static_libstdcxx) &&
172                      !Args.hasArg(options::OPT_static);
173     if (StaticCXX)
174       CmdArgs.push_back("-Bstatic");
175     TC.AddCXXStdlibLibArgs(Args, CmdArgs);
176     if (StaticCXX)
177       CmdArgs.push_back("-Bdynamic");
178   }
179 
180   if (!Args.hasArg(options::OPT_nostdlib)) {
181     if (!Args.hasArg(options::OPT_nodefaultlibs)) {
182       // TODO handle /MT[d] /MD[d]
183       CmdArgs.push_back("-lmsvcrt");
184       AddRunTimeLibs(TC, D, CmdArgs, Args);
185     }
186   }
187 
188   if (TC.getSanitizerArgs(Args).needsAsanRt()) {
189     // TODO handle /MT[d] /MD[d]
190     if (Args.hasArg(options::OPT_shared)) {
191       CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dll_thunk"));
192     } else {
193       for (const auto &Lib : {"asan_dynamic", "asan_dynamic_runtime_thunk"})
194         CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
195       // Make sure the dynamic runtime thunk is not optimized out at link time
196       // to ensure proper SEH handling.
197       CmdArgs.push_back(Args.MakeArgString("--undefined"));
198       CmdArgs.push_back(Args.MakeArgString(TC.getArch() == llvm::Triple::x86
199                                                ? "___asan_seh_interceptor"
200                                                : "__asan_seh_interceptor"));
201     }
202   }
203 
204   Exec = Args.MakeArgString(TC.GetLinkerPath());
205 
206   C.addCommand(std::make_unique<Command>(JA, *this,
207                                          ResponseFileSupport::AtFileUTF8(),
208                                          Exec, CmdArgs, Inputs, Output));
209 }
210 
211 CrossWindowsToolChain::CrossWindowsToolChain(const Driver &D,
212                                              const llvm::Triple &T,
213                                              const llvm::opt::ArgList &Args)
214     : Generic_GCC(D, T, Args) {}
215 
216 ToolChain::UnwindTableLevel
217 CrossWindowsToolChain::getDefaultUnwindTableLevel(const ArgList &Args) const {
218   // FIXME: all non-x86 targets need unwind tables, however, LLVM currently does
219   // not know how to emit them.
220   return getArch() == llvm::Triple::x86_64 ? UnwindTableLevel::Asynchronous : UnwindTableLevel::None;
221 }
222 
223 bool CrossWindowsToolChain::isPICDefault() const {
224   return getArch() == llvm::Triple::x86_64;
225 }
226 
227 bool CrossWindowsToolChain::isPIEDefault(const llvm::opt::ArgList &Args) const {
228   return getArch() == llvm::Triple::x86_64;
229 }
230 
231 bool CrossWindowsToolChain::isPICDefaultForced() const {
232   return getArch() == llvm::Triple::x86_64;
233 }
234 
235 void CrossWindowsToolChain::
236 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
237                           llvm::opt::ArgStringList &CC1Args) const {
238   const Driver &D = getDriver();
239   const std::string &SysRoot = D.SysRoot;
240 
241   auto AddSystemAfterIncludes = [&]() {
242     for (const auto &P : DriverArgs.getAllArgValues(options::OPT_isystem_after))
243       addSystemInclude(DriverArgs, CC1Args, P);
244   };
245 
246   if (DriverArgs.hasArg(options::OPT_nostdinc)) {
247     AddSystemAfterIncludes();
248     return;
249   }
250 
251   addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
252   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
253     SmallString<128> ResourceDir(D.ResourceDir);
254     llvm::sys::path::append(ResourceDir, "include");
255     addSystemInclude(DriverArgs, CC1Args, ResourceDir);
256   }
257   AddSystemAfterIncludes();
258   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
259 }
260 
261 void CrossWindowsToolChain::
262 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
263                              llvm::opt::ArgStringList &CC1Args) const {
264   const std::string &SysRoot = getDriver().SysRoot;
265 
266   if (DriverArgs.hasArg(options::OPT_nostdinc) ||
267       DriverArgs.hasArg(options::OPT_nostdincxx))
268     return;
269 
270   if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx)
271     addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include/c++/v1");
272 }
273 
274 void CrossWindowsToolChain::
275 AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
276                     llvm::opt::ArgStringList &CmdArgs) const {
277   if (GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) {
278     CmdArgs.push_back("-lc++");
279     if (Args.hasArg(options::OPT_fexperimental_library))
280       CmdArgs.push_back("-lc++experimental");
281   }
282 }
283 
284 clang::SanitizerMask CrossWindowsToolChain::getSupportedSanitizers() const {
285   SanitizerMask Res = ToolChain::getSupportedSanitizers();
286   Res |= SanitizerKind::Address;
287   Res |= SanitizerKind::PointerCompare;
288   Res |= SanitizerKind::PointerSubtract;
289   return Res;
290 }
291 
292 Tool *CrossWindowsToolChain::buildLinker() const {
293   return new tools::CrossWindows::Linker(*this);
294 }
295 
296 Tool *CrossWindowsToolChain::buildAssembler() const {
297   return new tools::CrossWindows::Assembler(*this);
298 }
299