xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/OpenBSD.cpp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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                    options::OPT_r)) {
165     const char *crt0 = nullptr;
166     const char *crtbegin = nullptr;
167     if (!Args.hasArg(options::OPT_shared)) {
168       if (Args.hasArg(options::OPT_pg))
169         crt0 = "gcrt0.o";
170       else if (Args.hasArg(options::OPT_static) &&
171                !Args.hasArg(options::OPT_nopie))
172         crt0 = "rcrt0.o";
173       else
174         crt0 = "crt0.o";
175       crtbegin = "crtbegin.o";
176     } else {
177       crtbegin = "crtbeginS.o";
178     }
179 
180     if (crt0)
181       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt0)));
182     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
183   }
184 
185   Args.AddAllArgs(CmdArgs, options::OPT_L);
186   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
187   Args.AddAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_e,
188                             options::OPT_s, options::OPT_t,
189                             options::OPT_Z_Flag, options::OPT_r});
190 
191   bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
192   bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
193   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
194 
195   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
196                    options::OPT_r)) {
197     // Use the static OpenMP runtime with -static-openmp
198     bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
199                         !Args.hasArg(options::OPT_static);
200     addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
201 
202     if (D.CCCIsCXX()) {
203       if (ToolChain.ShouldLinkCXXStdlib(Args))
204         ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
205       if (Args.hasArg(options::OPT_pg))
206         CmdArgs.push_back("-lm_p");
207       else
208         CmdArgs.push_back("-lm");
209     }
210     if (NeedsSanitizerDeps) {
211       CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
212       linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
213     }
214     if (NeedsXRayDeps) {
215       CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
216       linkXRayRuntimeDeps(ToolChain, CmdArgs);
217     }
218     // FIXME: For some reason GCC passes -lgcc before adding
219     // the default system libraries. Just mimic this for now.
220     CmdArgs.push_back("-lcompiler_rt");
221 
222     if (Args.hasArg(options::OPT_pthread)) {
223       if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
224         CmdArgs.push_back("-lpthread_p");
225       else
226         CmdArgs.push_back("-lpthread");
227     }
228 
229     if (!Args.hasArg(options::OPT_shared)) {
230       if (Args.hasArg(options::OPT_pg))
231         CmdArgs.push_back("-lc_p");
232       else
233         CmdArgs.push_back("-lc");
234     }
235 
236     CmdArgs.push_back("-lcompiler_rt");
237   }
238 
239   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
240                    options::OPT_r)) {
241     const char *crtend = nullptr;
242     if (!Args.hasArg(options::OPT_shared))
243       crtend = "crtend.o";
244     else
245       crtend = "crtendS.o";
246 
247     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
248   }
249 
250   ToolChain.addProfileRTLibs(Args, CmdArgs);
251 
252   const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
253   C.addCommand(std::make_unique<Command>(JA, *this,
254                                          ResponseFileSupport::AtFileCurCP(),
255                                          Exec, CmdArgs, Inputs, Output));
256 }
257 
258 SanitizerMask OpenBSD::getSupportedSanitizers() const {
259   const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
260   const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
261 
262   // For future use, only UBsan at the moment
263   SanitizerMask Res = ToolChain::getSupportedSanitizers();
264 
265   if (IsX86 || IsX86_64) {
266     Res |= SanitizerKind::Vptr;
267     Res |= SanitizerKind::Fuzzer;
268     Res |= SanitizerKind::FuzzerNoLink;
269   }
270 
271   return Res;
272 }
273 
274 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
275 
276 OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
277                  const ArgList &Args)
278     : Generic_ELF(D, Triple, Args) {
279   getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
280 }
281 
282 void OpenBSD::AddClangSystemIncludeArgs(
283     const llvm::opt::ArgList &DriverArgs,
284     llvm::opt::ArgStringList &CC1Args) const {
285   const Driver &D = getDriver();
286 
287   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
288     return;
289 
290   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
291     SmallString<128> Dir(D.ResourceDir);
292     llvm::sys::path::append(Dir, "include");
293     addSystemInclude(DriverArgs, CC1Args, Dir.str());
294   }
295 
296   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
297     return;
298 
299   // Check for configure-time C include directories.
300   StringRef CIncludeDirs(C_INCLUDE_DIRS);
301   if (CIncludeDirs != "") {
302     SmallVector<StringRef, 5> dirs;
303     CIncludeDirs.split(dirs, ":");
304     for (StringRef dir : dirs) {
305       StringRef Prefix =
306           llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
307       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
308     }
309     return;
310   }
311 
312   addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
313 }
314 
315 void OpenBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
316                                     llvm::opt::ArgStringList &CC1Args) const {
317   addSystemInclude(DriverArgs, CC1Args,
318                    getDriver().SysRoot + "/usr/include/c++/v1");
319 }
320 
321 void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args,
322                                   ArgStringList &CmdArgs) const {
323   bool Profiling = Args.hasArg(options::OPT_pg);
324 
325   CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
326   CmdArgs.push_back(Profiling ? "-lc++abi_p" : "-lc++abi");
327   CmdArgs.push_back(Profiling ? "-lpthread_p" : "-lpthread");
328 }
329 
330 std::string OpenBSD::getCompilerRT(const ArgList &Args,
331                                    StringRef Component,
332                                    FileType Type) const {
333   SmallString<128> Path(getDriver().SysRoot);
334   llvm::sys::path::append(Path, "/usr/lib/libcompiler_rt.a");
335   return std::string(Path.str());
336 }
337 
338 Tool *OpenBSD::buildAssembler() const {
339   return new tools::openbsd::Assembler(*this);
340 }
341 
342 Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
343 
344 bool OpenBSD::HasNativeLLVMSupport() const { return true; }
345 
346 bool OpenBSD::IsUnwindTablesDefault(const ArgList &Args) const {
347     switch (getArch()) {
348       case llvm::Triple::arm:
349         return false;
350       default:
351         return true;
352     }
353 }
354