1 //===--- OpenBSD.cpp - OpenBSD ToolChain Implementations --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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 #include "llvm/Support/VirtualFileSystem.h"
22 using namespace clang::driver
;
23 using namespace clang::driver::tools
;
24 using namespace clang::driver::toolchains
;
25 using namespace clang
;
26 using namespace llvm::opt
;
28 void openbsd::Assembler::ConstructJob(Compilation
&C
, const JobAction
&JA
,
29 const InputInfo
&Output
,
30 const InputInfoList
&Inputs
,
32 const char *LinkingOutput
) const {
33 const auto &ToolChain
= static_cast<const OpenBSD
&>(getToolChain());
34 const Driver
&D
= ToolChain
.getDriver();
35 const llvm::Triple
&Triple
= ToolChain
.getTriple();
37 claimNoWarnArgs(Args
);
38 ArgStringList CmdArgs
;
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");
47 case llvm::Triple::arm
: {
48 StringRef MArch
, MCPU
;
49 arm::getARMArchCPUFromArgs(Args
, MArch
, MCPU
, /*FromAs*/ true);
50 std::string Arch
= arm::getARMTargetCPU(MCPU
, MArch
, Triple
);
51 CmdArgs
.push_back(Args
.MakeArgString("-mcpu=" + Arch
));
55 case llvm::Triple::ppc
:
56 CmdArgs
.push_back("-mppc");
57 CmdArgs
.push_back("-many");
60 case llvm::Triple::sparcv9
: {
61 CmdArgs
.push_back("-64");
62 std::string CPU
= getCPUName(D
, Args
, Triple
);
63 CmdArgs
.push_back(sparc::getSparcAsmModeForCPU(CPU
, Triple
));
64 AddAssemblerKPIC(ToolChain
, Args
, CmdArgs
);
68 case llvm::Triple::mips64
:
69 case llvm::Triple::mips64el
: {
72 mips::getMipsCPUAndABI(Args
, Triple
, CPUName
, ABIName
);
74 CmdArgs
.push_back("-march");
75 CmdArgs
.push_back(CPUName
.data());
77 CmdArgs
.push_back("-mabi");
78 CmdArgs
.push_back(mips::getGnuCompatibleMipsABIName(ABIName
).data());
80 if (Triple
.isLittleEndian())
81 CmdArgs
.push_back("-EL");
83 CmdArgs
.push_back("-EB");
85 AddAssemblerKPIC(ToolChain
, Args
, CmdArgs
);
93 Args
.AddAllArgValues(CmdArgs
, options::OPT_Wa_COMMA
, options::OPT_Xassembler
);
95 CmdArgs
.push_back("-o");
96 CmdArgs
.push_back(Output
.getFilename());
98 for (const auto &II
: Inputs
)
99 CmdArgs
.push_back(II
.getFilename());
101 const char *Exec
= Args
.MakeArgString(ToolChain
.GetProgramPath("as"));
102 C
.addCommand(std::make_unique
<Command
>(JA
, *this,
103 ResponseFileSupport::AtFileCurCP(),
104 Exec
, CmdArgs
, Inputs
, Output
));
107 void openbsd::Linker::ConstructJob(Compilation
&C
, const JobAction
&JA
,
108 const InputInfo
&Output
,
109 const InputInfoList
&Inputs
,
111 const char *LinkingOutput
) const {
112 const auto &ToolChain
= static_cast<const OpenBSD
&>(getToolChain());
113 const Driver
&D
= ToolChain
.getDriver();
114 const llvm::Triple::ArchType Arch
= ToolChain
.getArch();
115 ArgStringList CmdArgs
;
116 bool Static
= Args
.hasArg(options::OPT_static
);
117 bool Shared
= Args
.hasArg(options::OPT_shared
);
118 bool Profiling
= Args
.hasArg(options::OPT_pg
);
119 bool Pie
= Args
.hasArg(options::OPT_pie
);
120 bool Nopie
= Args
.hasArg(options::OPT_nopie
);
121 const bool Relocatable
= Args
.hasArg(options::OPT_r
);
123 // Silence warning for "clang -g foo.o -o foo"
124 Args
.ClaimAllArgs(options::OPT_g_Group
);
125 // and "clang -emit-llvm foo.o -o foo"
126 Args
.ClaimAllArgs(options::OPT_emit_llvm
);
127 // and for "clang -w foo.o -o foo". Other warning options are already
128 // handled somewhere else.
129 Args
.ClaimAllArgs(options::OPT_w
);
131 if (!D
.SysRoot
.empty())
132 CmdArgs
.push_back(Args
.MakeArgString("--sysroot=" + D
.SysRoot
));
134 if (Arch
== llvm::Triple::mips64
)
135 CmdArgs
.push_back("-EB");
136 else if (Arch
== llvm::Triple::mips64el
)
137 CmdArgs
.push_back("-EL");
139 if (!Args
.hasArg(options::OPT_nostdlib
) && !Shared
&& !Relocatable
) {
140 CmdArgs
.push_back("-e");
141 CmdArgs
.push_back("__start");
144 CmdArgs
.push_back("--eh-frame-hdr");
146 CmdArgs
.push_back("-Bstatic");
148 if (Args
.hasArg(options::OPT_rdynamic
))
149 CmdArgs
.push_back("-export-dynamic");
151 CmdArgs
.push_back("-shared");
152 } else if (!Relocatable
) {
153 CmdArgs
.push_back("-dynamic-linker");
154 CmdArgs
.push_back("/usr/libexec/ld.so");
159 CmdArgs
.push_back("-pie");
160 if (Nopie
|| Profiling
)
161 CmdArgs
.push_back("-nopie");
163 if (Arch
== llvm::Triple::riscv64
)
164 CmdArgs
.push_back("-X");
166 assert((Output
.isFilename() || Output
.isNothing()) && "Invalid output.");
167 if (Output
.isFilename()) {
168 CmdArgs
.push_back("-o");
169 CmdArgs
.push_back(Output
.getFilename());
172 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
174 const char *crt0
= nullptr;
175 const char *crtbegin
= nullptr;
179 else if (Static
&& !Nopie
)
183 crtbegin
= "crtbegin.o";
185 crtbegin
= "crtbeginS.o";
189 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath(crt0
)));
190 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath(crtbegin
)));
193 Args
.AddAllArgs(CmdArgs
, options::OPT_L
);
194 ToolChain
.AddFilePathLibArgs(Args
, CmdArgs
);
195 Args
.addAllArgs(CmdArgs
, {options::OPT_T_Group
, options::OPT_s
,
196 options::OPT_t
, options::OPT_r
});
198 bool NeedsSanitizerDeps
= addSanitizerRuntimes(ToolChain
, Args
, CmdArgs
);
199 bool NeedsXRayDeps
= addXRayRuntime(ToolChain
, Args
, CmdArgs
);
200 AddLinkerInputs(ToolChain
, Inputs
, Args
, CmdArgs
, JA
);
202 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nodefaultlibs
,
204 // Use the static OpenMP runtime with -static-openmp
205 bool StaticOpenMP
= Args
.hasArg(options::OPT_static_openmp
) && !Static
;
206 addOpenMPRuntime(CmdArgs
, ToolChain
, Args
, StaticOpenMP
);
209 if (ToolChain
.ShouldLinkCXXStdlib(Args
))
210 ToolChain
.AddCXXStdlibLibArgs(Args
, CmdArgs
);
212 CmdArgs
.push_back("-lm_p");
214 CmdArgs
.push_back("-lm");
217 // Silence warnings when linking C code with a C++ '-stdlib' argument.
218 Args
.ClaimAllArgs(options::OPT_stdlib_EQ
);
220 // Additional linker set-up and flags for Fortran. This is required in order
221 // to generate executables. As Fortran runtime depends on the C runtime,
222 // these dependencies need to be listed before the C runtime below (i.e.
224 if (D
.IsFlangMode()) {
225 addFortranRuntimeLibraryPath(ToolChain
, Args
, CmdArgs
);
226 addFortranRuntimeLibs(ToolChain
, CmdArgs
);
228 CmdArgs
.push_back("-lm_p");
230 CmdArgs
.push_back("-lm");
233 if (NeedsSanitizerDeps
) {
234 CmdArgs
.push_back(ToolChain
.getCompilerRTArgString(Args
, "builtins"));
235 linkSanitizerRuntimeDeps(ToolChain
, Args
, CmdArgs
);
238 CmdArgs
.push_back(ToolChain
.getCompilerRTArgString(Args
, "builtins"));
239 linkXRayRuntimeDeps(ToolChain
, Args
, CmdArgs
);
241 // FIXME: For some reason GCC passes -lgcc before adding
242 // the default system libraries. Just mimic this for now.
243 CmdArgs
.push_back("-lcompiler_rt");
245 if (Args
.hasArg(options::OPT_pthread
)) {
246 if (!Shared
&& Profiling
)
247 CmdArgs
.push_back("-lpthread_p");
249 CmdArgs
.push_back("-lpthread");
254 CmdArgs
.push_back("-lc_p");
256 CmdArgs
.push_back("-lc");
259 CmdArgs
.push_back("-lcompiler_rt");
262 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
264 const char *crtend
= nullptr;
268 crtend
= "crtendS.o";
270 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath(crtend
)));
273 ToolChain
.addProfileRTLibs(Args
, CmdArgs
);
275 const char *Exec
= Args
.MakeArgString(ToolChain
.GetLinkerPath());
276 C
.addCommand(std::make_unique
<Command
>(JA
, *this,
277 ResponseFileSupport::AtFileCurCP(),
278 Exec
, CmdArgs
, Inputs
, Output
));
281 SanitizerMask
OpenBSD::getSupportedSanitizers() const {
282 const bool IsX86
= getTriple().getArch() == llvm::Triple::x86
;
283 const bool IsX86_64
= getTriple().getArch() == llvm::Triple::x86_64
;
285 // For future use, only UBsan at the moment
286 SanitizerMask Res
= ToolChain::getSupportedSanitizers();
288 if (IsX86
|| IsX86_64
) {
289 Res
|= SanitizerKind::Vptr
;
290 Res
|= SanitizerKind::Fuzzer
;
291 Res
|= SanitizerKind::FuzzerNoLink
;
297 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
299 OpenBSD::OpenBSD(const Driver
&D
, const llvm::Triple
&Triple
,
301 : Generic_ELF(D
, Triple
, Args
) {
302 getFilePaths().push_back(concat(getDriver().SysRoot
, "/usr/lib"));
305 void OpenBSD::AddClangSystemIncludeArgs(
306 const llvm::opt::ArgList
&DriverArgs
,
307 llvm::opt::ArgStringList
&CC1Args
) const {
308 const Driver
&D
= getDriver();
310 if (DriverArgs
.hasArg(clang::driver::options::OPT_nostdinc
))
313 if (!DriverArgs
.hasArg(options::OPT_nobuiltininc
)) {
314 SmallString
<128> Dir(D
.ResourceDir
);
315 llvm::sys::path::append(Dir
, "include");
316 addSystemInclude(DriverArgs
, CC1Args
, Dir
.str());
319 if (DriverArgs
.hasArg(options::OPT_nostdlibinc
))
322 // Check for configure-time C include directories.
323 StringRef
CIncludeDirs(C_INCLUDE_DIRS
);
324 if (CIncludeDirs
!= "") {
325 SmallVector
<StringRef
, 5> dirs
;
326 CIncludeDirs
.split(dirs
, ":");
327 for (StringRef dir
: dirs
) {
329 llvm::sys::path::is_absolute(dir
) ? StringRef(D
.SysRoot
) : "";
330 addExternCSystemInclude(DriverArgs
, CC1Args
, Prefix
+ dir
);
335 addExternCSystemInclude(DriverArgs
, CC1Args
,
336 concat(D
.SysRoot
, "/usr/include"));
339 void OpenBSD::addLibCxxIncludePaths(const llvm::opt::ArgList
&DriverArgs
,
340 llvm::opt::ArgStringList
&CC1Args
) const {
341 addSystemInclude(DriverArgs
, CC1Args
,
342 concat(getDriver().SysRoot
, "/usr/include/c++/v1"));
345 void OpenBSD::AddCXXStdlibLibArgs(const ArgList
&Args
,
346 ArgStringList
&CmdArgs
) const {
347 bool Profiling
= Args
.hasArg(options::OPT_pg
);
349 CmdArgs
.push_back(Profiling
? "-lc++_p" : "-lc++");
350 if (Args
.hasArg(options::OPT_fexperimental_library
))
351 CmdArgs
.push_back("-lc++experimental");
352 CmdArgs
.push_back(Profiling
? "-lc++abi_p" : "-lc++abi");
353 CmdArgs
.push_back(Profiling
? "-lpthread_p" : "-lpthread");
356 std::string
OpenBSD::getCompilerRT(const ArgList
&Args
, StringRef Component
,
357 FileType Type
) const {
358 if (Component
== "builtins") {
359 SmallString
<128> Path(getDriver().SysRoot
);
360 llvm::sys::path::append(Path
, "/usr/lib/libcompiler_rt.a");
361 return std::string(Path
.str());
363 SmallString
<128> P(getDriver().ResourceDir
);
364 std::string CRTBasename
=
365 buildCompilerRTBasename(Args
, Component
, Type
, /*AddArch=*/false);
366 llvm::sys::path::append(P
, "lib", CRTBasename
);
367 // Checks if this is the base system case which uses a different location.
368 if (getVFS().exists(P
))
369 return std::string(P
.str());
370 return ToolChain::getCompilerRT(Args
, Component
, Type
);
373 Tool
*OpenBSD::buildAssembler() const {
374 return new tools::openbsd::Assembler(*this);
377 Tool
*OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
379 bool OpenBSD::HasNativeLLVMSupport() const { return true; }
381 ToolChain::UnwindTableLevel
382 OpenBSD::getDefaultUnwindTableLevel(const ArgList
&Args
) const {
384 case llvm::Triple::arm
:
385 return UnwindTableLevel::None
;
387 return UnwindTableLevel::Asynchronous
;