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