xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/OpenBSD.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
1 //===--- OpenBSD.cpp - OpenBSD ToolChain Implementations --------*- C++ -*-===//
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 "OpenBSD.h"
10 #include "Arch/ARM.h"
11 #include "Arch/Mips.h"
12 #include "Arch/Sparc.h"
13 #include "CommonArgs.h"
14 #include "clang/Config/config.h"
15 #include "clang/Driver/Compilation.h"
16 #include "clang/Driver/Options.h"
17 #include "clang/Driver/SanitizerArgs.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 
27 void openbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
28                                       const InputInfo &Output,
29                                       const InputInfoList &Inputs,
30                                       const ArgList &Args,
31                                       const char *LinkingOutput) const {
32   const toolchains::OpenBSD &ToolChain =
33       static_cast<const toolchains::OpenBSD &>(getToolChain());
34   const Driver &D = ToolChain.getDriver();
35   const llvm::Triple &Triple = ToolChain.getTriple();
36 
37   claimNoWarnArgs(Args);
38   ArgStringList CmdArgs;
39 
40   switch (ToolChain.getArch()) {
41   case llvm::Triple::x86:
42     // When building 32-bit code on OpenBSD/amd64, we have to explicitly
43     // instruct as in the base system to assemble 32-bit code.
44     CmdArgs.push_back("--32");
45     break;
46 
47   case llvm::Triple::arm:
48   case llvm::Triple::armeb: {
49     StringRef MArch, MCPU;
50     arm::getARMArchCPUFromArgs(Args, MArch, MCPU, /*FromAs*/ true);
51     std::string Arch = arm::getARMTargetCPU(MCPU, MArch, Triple);
52     CmdArgs.push_back(Args.MakeArgString("-mcpu=" + Arch));
53     break;
54   }
55 
56   case llvm::Triple::ppc:
57     CmdArgs.push_back("-mppc");
58     CmdArgs.push_back("-many");
59     break;
60 
61   case llvm::Triple::sparcv9: {
62     CmdArgs.push_back("-64");
63     std::string CPU = getCPUName(D, Args, Triple);
64     CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, Triple));
65     AddAssemblerKPIC(ToolChain, Args, CmdArgs);
66     break;
67   }
68 
69   case llvm::Triple::mips64:
70   case llvm::Triple::mips64el: {
71     StringRef CPUName;
72     StringRef ABIName;
73     mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
74 
75     CmdArgs.push_back("-march");
76     CmdArgs.push_back(CPUName.data());
77 
78     CmdArgs.push_back("-mabi");
79     CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
80 
81     if (Triple.isLittleEndian())
82       CmdArgs.push_back("-EL");
83     else
84       CmdArgs.push_back("-EB");
85 
86     AddAssemblerKPIC(ToolChain, Args, CmdArgs);
87     break;
88   }
89 
90   default:
91     break;
92   }
93 
94   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
95 
96   CmdArgs.push_back("-o");
97   CmdArgs.push_back(Output.getFilename());
98 
99   for (const auto &II : Inputs)
100     CmdArgs.push_back(II.getFilename());
101 
102   const char *Exec = Args.MakeArgString(ToolChain.GetProgramPath("as"));
103   C.addCommand(std::make_unique<Command>(JA, *this,
104                                          ResponseFileSupport::AtFileCurCP(),
105                                          Exec, CmdArgs, Inputs, Output));
106 }
107 
108 void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
109                                    const InputInfo &Output,
110                                    const InputInfoList &Inputs,
111                                    const ArgList &Args,
112                                    const char *LinkingOutput) const {
113   const toolchains::OpenBSD &ToolChain =
114       static_cast<const toolchains::OpenBSD &>(getToolChain());
115   const Driver &D = ToolChain.getDriver();
116   ArgStringList CmdArgs;
117 
118   // Silence warning for "clang -g foo.o -o foo"
119   Args.ClaimAllArgs(options::OPT_g_Group);
120   // and "clang -emit-llvm foo.o -o foo"
121   Args.ClaimAllArgs(options::OPT_emit_llvm);
122   // and for "clang -w foo.o -o foo". Other warning options are already
123   // handled somewhere else.
124   Args.ClaimAllArgs(options::OPT_w);
125 
126   if (ToolChain.getArch() == llvm::Triple::mips64)
127     CmdArgs.push_back("-EB");
128   else if (ToolChain.getArch() == llvm::Triple::mips64el)
129     CmdArgs.push_back("-EL");
130 
131   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) {
132     CmdArgs.push_back("-e");
133     CmdArgs.push_back("__start");
134   }
135 
136   CmdArgs.push_back("--eh-frame-hdr");
137   if (Args.hasArg(options::OPT_static)) {
138     CmdArgs.push_back("-Bstatic");
139   } else {
140     if (Args.hasArg(options::OPT_rdynamic))
141       CmdArgs.push_back("-export-dynamic");
142     CmdArgs.push_back("-Bdynamic");
143     if (Args.hasArg(options::OPT_shared)) {
144       CmdArgs.push_back("-shared");
145     } else {
146       CmdArgs.push_back("-dynamic-linker");
147       CmdArgs.push_back("/usr/libexec/ld.so");
148     }
149   }
150 
151   if (Args.hasArg(options::OPT_pie))
152     CmdArgs.push_back("-pie");
153   if (Args.hasArg(options::OPT_nopie) || Args.hasArg(options::OPT_pg))
154     CmdArgs.push_back("-nopie");
155 
156   if (Output.isFilename()) {
157     CmdArgs.push_back("-o");
158     CmdArgs.push_back(Output.getFilename());
159   } else {
160     assert(Output.isNothing() && "Invalid output.");
161   }
162 
163   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
164     const char *crt0 = nullptr;
165     const char *crtbegin = nullptr;
166     if (!Args.hasArg(options::OPT_shared)) {
167       if (Args.hasArg(options::OPT_pg))
168         crt0 = "gcrt0.o";
169       else if (Args.hasArg(options::OPT_static) &&
170                !Args.hasArg(options::OPT_nopie))
171         crt0 = "rcrt0.o";
172       else
173         crt0 = "crt0.o";
174       crtbegin = "crtbegin.o";
175     } else {
176       crtbegin = "crtbeginS.o";
177     }
178 
179     if (crt0)
180       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt0)));
181     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
182   }
183 
184   Args.AddAllArgs(CmdArgs, options::OPT_L);
185   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
186   Args.AddAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_e,
187                             options::OPT_s, options::OPT_t,
188                             options::OPT_Z_Flag, options::OPT_r});
189 
190   bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
191   bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
192   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
193 
194   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
195     // Use the static OpenMP runtime with -static-openmp
196     bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
197                         !Args.hasArg(options::OPT_static);
198     addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
199 
200     if (D.CCCIsCXX()) {
201       if (ToolChain.ShouldLinkCXXStdlib(Args))
202         ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
203       if (Args.hasArg(options::OPT_pg))
204         CmdArgs.push_back("-lm_p");
205       else
206         CmdArgs.push_back("-lm");
207     }
208     if (NeedsSanitizerDeps) {
209       CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
210       linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
211     }
212     if (NeedsXRayDeps) {
213       CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
214       linkXRayRuntimeDeps(ToolChain, CmdArgs);
215     }
216     // FIXME: For some reason GCC passes -lgcc before adding
217     // the default system libraries. Just mimic this for now.
218     CmdArgs.push_back("-lcompiler_rt");
219 
220     if (Args.hasArg(options::OPT_pthread)) {
221       if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
222         CmdArgs.push_back("-lpthread_p");
223       else
224         CmdArgs.push_back("-lpthread");
225     }
226 
227     if (!Args.hasArg(options::OPT_shared)) {
228       if (Args.hasArg(options::OPT_pg))
229         CmdArgs.push_back("-lc_p");
230       else
231         CmdArgs.push_back("-lc");
232     }
233 
234     CmdArgs.push_back("-lcompiler_rt");
235   }
236 
237   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
238     const char *crtend = nullptr;
239     if (!Args.hasArg(options::OPT_shared))
240       crtend = "crtend.o";
241     else
242       crtend = "crtendS.o";
243 
244     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
245   }
246 
247   ToolChain.addProfileRTLibs(Args, CmdArgs);
248 
249   const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
250   C.addCommand(std::make_unique<Command>(JA, *this,
251                                          ResponseFileSupport::AtFileCurCP(),
252                                          Exec, CmdArgs, Inputs, Output));
253 }
254 
255 SanitizerMask OpenBSD::getSupportedSanitizers() const {
256   const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
257   const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
258 
259   // For future use, only UBsan at the moment
260   SanitizerMask Res = ToolChain::getSupportedSanitizers();
261 
262   if (IsX86 || IsX86_64) {
263     Res |= SanitizerKind::Vptr;
264     Res |= SanitizerKind::Fuzzer;
265     Res |= SanitizerKind::FuzzerNoLink;
266   }
267 
268   return Res;
269 }
270 
271 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
272 
273 OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
274                  const ArgList &Args)
275     : Generic_ELF(D, Triple, Args) {
276   getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
277 }
278 
279 void OpenBSD::AddClangSystemIncludeArgs(
280     const llvm::opt::ArgList &DriverArgs,
281     llvm::opt::ArgStringList &CC1Args) const {
282   const Driver &D = getDriver();
283 
284   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
285     return;
286 
287   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
288     SmallString<128> Dir(D.ResourceDir);
289     llvm::sys::path::append(Dir, "include");
290     addSystemInclude(DriverArgs, CC1Args, Dir.str());
291   }
292 
293   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
294     return;
295 
296   // Check for configure-time C include directories.
297   StringRef CIncludeDirs(C_INCLUDE_DIRS);
298   if (CIncludeDirs != "") {
299     SmallVector<StringRef, 5> dirs;
300     CIncludeDirs.split(dirs, ":");
301     for (StringRef dir : dirs) {
302       StringRef Prefix =
303           llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
304       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
305     }
306     return;
307   }
308 
309   addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
310 }
311 
312 void OpenBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
313                                     llvm::opt::ArgStringList &CC1Args) const {
314   addSystemInclude(DriverArgs, CC1Args,
315                    getDriver().SysRoot + "/usr/include/c++/v1");
316 }
317 
318 void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args,
319                                   ArgStringList &CmdArgs) const {
320   bool Profiling = Args.hasArg(options::OPT_pg);
321 
322   CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
323   CmdArgs.push_back(Profiling ? "-lc++abi_p" : "-lc++abi");
324   CmdArgs.push_back(Profiling ? "-lpthread_p" : "-lpthread");
325 }
326 
327 std::string OpenBSD::getCompilerRT(const ArgList &Args,
328                                    StringRef Component,
329                                    FileType Type) const {
330   SmallString<128> Path(getDriver().SysRoot);
331   llvm::sys::path::append(Path, "/usr/lib/libcompiler_rt.a");
332   return std::string(Path.str());
333 }
334 
335 Tool *OpenBSD::buildAssembler() const {
336   return new tools::openbsd::Assembler(*this);
337 }
338 
339 Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
340 
341 bool OpenBSD::HasNativeLLVMSupport() const { return true; }
342