1 //===--- FreeBSD.cpp - FreeBSD 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/DriverDiagnostic.h"
17 #include "clang/Driver/Options.h"
18 #include "clang/Driver/SanitizerArgs.h"
19 #include "llvm/Option/ArgList.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 freebsd::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 FreeBSD
&>(getToolChain());
34 const auto &D
= getToolChain().getDriver();
35 const llvm::Triple
&Triple
= ToolChain
.getTriple();
36 ArgStringList CmdArgs
;
38 claimNoWarnArgs(Args
);
40 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
41 // instruct as in the base system to assemble 32-bit code.
42 switch (ToolChain
.getArch()) {
45 case llvm::Triple::x86
:
46 CmdArgs
.push_back("--32");
48 case llvm::Triple::ppc
:
49 case llvm::Triple::ppcle
:
50 CmdArgs
.push_back("-a32");
52 case llvm::Triple::mips
:
53 case llvm::Triple::mipsel
:
54 case llvm::Triple::mips64
:
55 case llvm::Triple::mips64el
: {
58 mips::getMipsCPUAndABI(Args
, Triple
, CPUName
, ABIName
);
60 CmdArgs
.push_back("-march");
61 CmdArgs
.push_back(CPUName
.data());
63 CmdArgs
.push_back("-mabi");
64 CmdArgs
.push_back(mips::getGnuCompatibleMipsABIName(ABIName
).data());
66 if (Triple
.isLittleEndian())
67 CmdArgs
.push_back("-EL");
69 CmdArgs
.push_back("-EB");
71 if (Arg
*A
= Args
.getLastArg(options::OPT_G
)) {
72 StringRef v
= A
->getValue();
73 CmdArgs
.push_back(Args
.MakeArgString("-G" + v
));
77 AddAssemblerKPIC(ToolChain
, Args
, CmdArgs
);
80 case llvm::Triple::arm
:
81 case llvm::Triple::armeb
:
82 case llvm::Triple::thumb
:
83 case llvm::Triple::thumbeb
: {
84 arm::FloatABI ABI
= arm::getARMFloatABI(ToolChain
, Args
);
86 if (ABI
== arm::FloatABI::Hard
)
87 CmdArgs
.push_back("-mfpu=vfp");
89 CmdArgs
.push_back("-mfpu=softvfp");
91 CmdArgs
.push_back("-meabi=5");
94 case llvm::Triple::sparcv9
: {
95 std::string CPU
= getCPUName(D
, Args
, Triple
);
96 CmdArgs
.push_back(sparc::getSparcAsmModeForCPU(CPU
, Triple
));
97 AddAssemblerKPIC(ToolChain
, Args
, CmdArgs
);
102 for (const Arg
*A
: Args
.filtered(options::OPT_ffile_prefix_map_EQ
,
103 options::OPT_fdebug_prefix_map_EQ
)) {
104 StringRef Map
= A
->getValue();
105 if (!Map
.contains('='))
106 D
.Diag(diag::err_drv_invalid_argument_to_option
)
107 << Map
<< A
->getOption().getName();
109 CmdArgs
.push_back(Args
.MakeArgString("--debug-prefix-map"));
110 CmdArgs
.push_back(Args
.MakeArgString(Map
));
115 Args
.AddAllArgValues(CmdArgs
, options::OPT_Wa_COMMA
, options::OPT_Xassembler
);
117 CmdArgs
.push_back("-o");
118 CmdArgs
.push_back(Output
.getFilename());
120 for (const auto &II
: Inputs
)
121 CmdArgs
.push_back(II
.getFilename());
123 const char *Exec
= Args
.MakeArgString(ToolChain
.GetProgramPath("as"));
124 C
.addCommand(std::make_unique
<Command
>(JA
, *this,
125 ResponseFileSupport::AtFileCurCP(),
126 Exec
, CmdArgs
, Inputs
, Output
));
129 void freebsd::Linker::ConstructJob(Compilation
&C
, const JobAction
&JA
,
130 const InputInfo
&Output
,
131 const InputInfoList
&Inputs
,
133 const char *LinkingOutput
) const {
134 const auto &ToolChain
= static_cast<const FreeBSD
&>(getToolChain());
135 const Driver
&D
= ToolChain
.getDriver();
136 const llvm::Triple::ArchType Arch
= ToolChain
.getArch();
138 !Args
.hasArg(options::OPT_shared
) &&
139 (Args
.hasArg(options::OPT_pie
) || ToolChain
.isPIEDefault(Args
));
140 ArgStringList CmdArgs
;
142 // Silence warning for "clang -g foo.o -o foo"
143 Args
.ClaimAllArgs(options::OPT_g_Group
);
144 // and "clang -emit-llvm foo.o -o foo"
145 Args
.ClaimAllArgs(options::OPT_emit_llvm
);
146 // and for "clang -w foo.o -o foo". Other warning options are already
147 // handled somewhere else.
148 Args
.ClaimAllArgs(options::OPT_w
);
150 if (!D
.SysRoot
.empty())
151 CmdArgs
.push_back(Args
.MakeArgString("--sysroot=" + D
.SysRoot
));
154 CmdArgs
.push_back("-pie");
156 CmdArgs
.push_back("--eh-frame-hdr");
157 if (Args
.hasArg(options::OPT_static
)) {
158 CmdArgs
.push_back("-Bstatic");
160 if (Args
.hasArg(options::OPT_rdynamic
))
161 CmdArgs
.push_back("-export-dynamic");
162 if (Args
.hasArg(options::OPT_shared
)) {
163 CmdArgs
.push_back("-shared");
164 } else if (!Args
.hasArg(options::OPT_r
)) {
165 CmdArgs
.push_back("-dynamic-linker");
166 CmdArgs
.push_back("/libexec/ld-elf.so.1");
168 const llvm::Triple
&T
= ToolChain
.getTriple();
169 if (Arch
== llvm::Triple::arm
|| T
.isX86())
170 CmdArgs
.push_back("--hash-style=both");
171 CmdArgs
.push_back("--enable-new-dtags");
174 // Explicitly set the linker emulation for platforms that might not
175 // be the default emulation for the linker.
177 case llvm::Triple::x86
:
178 CmdArgs
.push_back("-m");
179 CmdArgs
.push_back("elf_i386_fbsd");
181 case llvm::Triple::ppc
:
182 CmdArgs
.push_back("-m");
183 CmdArgs
.push_back("elf32ppc_fbsd");
185 case llvm::Triple::ppcle
:
186 CmdArgs
.push_back("-m");
187 // Use generic -- only usage is for freestanding.
188 CmdArgs
.push_back("elf32lppc");
190 case llvm::Triple::mips
:
191 CmdArgs
.push_back("-m");
192 CmdArgs
.push_back("elf32btsmip_fbsd");
194 case llvm::Triple::mipsel
:
195 CmdArgs
.push_back("-m");
196 CmdArgs
.push_back("elf32ltsmip_fbsd");
198 case llvm::Triple::mips64
:
199 CmdArgs
.push_back("-m");
200 if (tools::mips::hasMipsAbiArg(Args
, "n32"))
201 CmdArgs
.push_back("elf32btsmipn32_fbsd");
203 CmdArgs
.push_back("elf64btsmip_fbsd");
205 case llvm::Triple::mips64el
:
206 CmdArgs
.push_back("-m");
207 if (tools::mips::hasMipsAbiArg(Args
, "n32"))
208 CmdArgs
.push_back("elf32ltsmipn32_fbsd");
210 CmdArgs
.push_back("elf64ltsmip_fbsd");
212 case llvm::Triple::riscv64
:
213 CmdArgs
.push_back("-m");
214 CmdArgs
.push_back("elf64lriscv");
215 CmdArgs
.push_back("-X");
221 if (Arg
*A
= Args
.getLastArg(options::OPT_G
)) {
222 if (ToolChain
.getTriple().isMIPS()) {
223 StringRef v
= A
->getValue();
224 CmdArgs
.push_back(Args
.MakeArgString("-G" + v
));
229 assert((Output
.isFilename() || Output
.isNothing()) && "Invalid output.");
230 if (Output
.isFilename()) {
231 CmdArgs
.push_back("-o");
232 CmdArgs
.push_back(Output
.getFilename());
235 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
237 const char *crt1
= nullptr;
238 if (!Args
.hasArg(options::OPT_shared
)) {
239 if (Args
.hasArg(options::OPT_pg
))
247 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath(crt1
)));
249 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath("crti.o")));
251 const char *crtbegin
= nullptr;
252 if (Args
.hasArg(options::OPT_static
))
253 crtbegin
= "crtbeginT.o";
254 else if (Args
.hasArg(options::OPT_shared
) || IsPIE
)
255 crtbegin
= "crtbeginS.o";
257 crtbegin
= "crtbegin.o";
259 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath(crtbegin
)));
262 Args
.AddAllArgs(CmdArgs
, options::OPT_L
);
263 ToolChain
.AddFilePathLibArgs(Args
, CmdArgs
);
264 Args
.addAllArgs(CmdArgs
, {options::OPT_T_Group
, options::OPT_s
,
265 options::OPT_t
, options::OPT_r
});
267 if (D
.isUsingLTO()) {
268 assert(!Inputs
.empty() && "Must have at least one input.");
269 // Find the first filename InputInfo object.
270 auto Input
= llvm::find_if(
271 Inputs
, [](const InputInfo
&II
) -> bool { return II
.isFilename(); });
272 if (Input
== Inputs
.end())
273 // For a very rare case, all of the inputs to the linker are
274 // InputArg. If that happens, just use the first InputInfo.
275 Input
= Inputs
.begin();
277 addLTOOptions(ToolChain
, Args
, CmdArgs
, Output
, *Input
,
278 D
.getLTOMode() == LTOK_Thin
);
281 bool NeedsSanitizerDeps
= addSanitizerRuntimes(ToolChain
, Args
, CmdArgs
);
282 bool NeedsXRayDeps
= addXRayRuntime(ToolChain
, Args
, CmdArgs
);
283 addLinkerCompressDebugSectionsOption(ToolChain
, Args
, CmdArgs
);
284 AddLinkerInputs(ToolChain
, Inputs
, Args
, CmdArgs
, JA
);
286 unsigned Major
= ToolChain
.getTriple().getOSMajorVersion();
287 bool Profiling
= Args
.hasArg(options::OPT_pg
) && Major
!= 0 && Major
< 14;
288 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nodefaultlibs
,
290 // Use the static OpenMP runtime with -static-openmp
291 bool StaticOpenMP
= Args
.hasArg(options::OPT_static_openmp
) &&
292 !Args
.hasArg(options::OPT_static
);
293 addOpenMPRuntime(CmdArgs
, ToolChain
, Args
, StaticOpenMP
);
296 if (ToolChain
.ShouldLinkCXXStdlib(Args
))
297 ToolChain
.AddCXXStdlibLibArgs(Args
, CmdArgs
);
299 CmdArgs
.push_back("-lm_p");
301 CmdArgs
.push_back("-lm");
304 // Silence warnings when linking C code with a C++ '-stdlib' argument.
305 Args
.ClaimAllArgs(options::OPT_stdlib_EQ
);
307 // Additional linker set-up and flags for Fortran. This is required in order
308 // to generate executables. As Fortran runtime depends on the C runtime,
309 // these dependencies need to be listed before the C runtime below (i.e.
311 if (D
.IsFlangMode()) {
312 addFortranRuntimeLibraryPath(ToolChain
, Args
, CmdArgs
);
313 addFortranRuntimeLibs(ToolChain
, Args
, CmdArgs
);
315 CmdArgs
.push_back("-lm_p");
317 CmdArgs
.push_back("-lm");
320 if (NeedsSanitizerDeps
)
321 linkSanitizerRuntimeDeps(ToolChain
, Args
, CmdArgs
);
323 linkXRayRuntimeDeps(ToolChain
, Args
, CmdArgs
);
324 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
325 // the default system libraries. Just mimic this for now.
327 CmdArgs
.push_back("-lgcc_p");
329 CmdArgs
.push_back("-lgcc");
330 if (Args
.hasArg(options::OPT_static
)) {
331 CmdArgs
.push_back("-lgcc_eh");
332 } else if (Profiling
) {
333 CmdArgs
.push_back("-lgcc_eh_p");
335 CmdArgs
.push_back("--as-needed");
336 CmdArgs
.push_back("-lgcc_s");
337 CmdArgs
.push_back("--no-as-needed");
340 if (Args
.hasArg(options::OPT_pthread
)) {
342 CmdArgs
.push_back("-lpthread_p");
344 CmdArgs
.push_back("-lpthread");
348 if (Args
.hasArg(options::OPT_shared
))
349 CmdArgs
.push_back("-lc");
351 CmdArgs
.push_back("-lc_p");
352 CmdArgs
.push_back("-lgcc_p");
354 CmdArgs
.push_back("-lc");
355 CmdArgs
.push_back("-lgcc");
358 if (Args
.hasArg(options::OPT_static
)) {
359 CmdArgs
.push_back("-lgcc_eh");
360 } else if (Profiling
) {
361 CmdArgs
.push_back("-lgcc_eh_p");
363 CmdArgs
.push_back("--as-needed");
364 CmdArgs
.push_back("-lgcc_s");
365 CmdArgs
.push_back("--no-as-needed");
369 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
371 const char *crtend
= nullptr;
372 if (Args
.hasArg(options::OPT_shared
) || IsPIE
)
373 crtend
= "crtendS.o";
376 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath(crtend
)));
377 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath("crtn.o")));
380 ToolChain
.addProfileRTLibs(Args
, CmdArgs
);
382 const char *Exec
= Args
.MakeArgString(getToolChain().GetLinkerPath());
383 C
.addCommand(std::make_unique
<Command
>(JA
, *this,
384 ResponseFileSupport::AtFileCurCP(),
385 Exec
, CmdArgs
, Inputs
, Output
));
388 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
390 FreeBSD::FreeBSD(const Driver
&D
, const llvm::Triple
&Triple
,
392 : Generic_ELF(D
, Triple
, Args
) {
394 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
395 // back to '/usr/lib' if it doesn't exist.
396 if (Triple
.isArch32Bit() &&
397 D
.getVFS().exists(concat(getDriver().SysRoot
, "/usr/lib32/crt1.o")))
398 getFilePaths().push_back(concat(getDriver().SysRoot
, "/usr/lib32"));
400 getFilePaths().push_back(concat(getDriver().SysRoot
, "/usr/lib"));
403 void FreeBSD::AddClangSystemIncludeArgs(
404 const llvm::opt::ArgList
&DriverArgs
,
405 llvm::opt::ArgStringList
&CC1Args
) const {
406 const Driver
&D
= getDriver();
408 if (DriverArgs
.hasArg(clang::driver::options::OPT_nostdinc
))
411 if (!DriverArgs
.hasArg(options::OPT_nobuiltininc
)) {
412 SmallString
<128> Dir(D
.ResourceDir
);
413 llvm::sys::path::append(Dir
, "include");
414 addSystemInclude(DriverArgs
, CC1Args
, Dir
.str());
417 if (DriverArgs
.hasArg(options::OPT_nostdlibinc
))
420 // Check for configure-time C include directories.
421 StringRef
CIncludeDirs(C_INCLUDE_DIRS
);
422 if (CIncludeDirs
!= "") {
423 SmallVector
<StringRef
, 5> dirs
;
424 CIncludeDirs
.split(dirs
, ":");
425 for (StringRef dir
: dirs
) {
427 llvm::sys::path::is_absolute(dir
) ? StringRef(D
.SysRoot
) : "";
428 addExternCSystemInclude(DriverArgs
, CC1Args
, Prefix
+ dir
);
433 addExternCSystemInclude(DriverArgs
, CC1Args
,
434 concat(D
.SysRoot
, "/usr/include"));
437 void FreeBSD::addLibCxxIncludePaths(const llvm::opt::ArgList
&DriverArgs
,
438 llvm::opt::ArgStringList
&CC1Args
) const {
439 addSystemInclude(DriverArgs
, CC1Args
,
440 concat(getDriver().SysRoot
, "/usr/include/c++/v1"));
443 void FreeBSD::AddCXXStdlibLibArgs(const ArgList
&Args
,
444 ArgStringList
&CmdArgs
) const {
445 unsigned Major
= getTriple().getOSMajorVersion();
446 bool Profiling
= Args
.hasArg(options::OPT_pg
) && Major
!= 0 && Major
< 14;
448 CmdArgs
.push_back(Profiling
? "-lc++_p" : "-lc++");
449 if (Args
.hasArg(options::OPT_fexperimental_library
))
450 CmdArgs
.push_back("-lc++experimental");
453 void FreeBSD::AddCudaIncludeArgs(const ArgList
&DriverArgs
,
454 ArgStringList
&CC1Args
) const {
455 CudaInstallation
->AddCudaIncludeArgs(DriverArgs
, CC1Args
);
458 void FreeBSD::AddHIPIncludeArgs(const ArgList
&DriverArgs
,
459 ArgStringList
&CC1Args
) const {
460 RocmInstallation
->AddHIPIncludeArgs(DriverArgs
, CC1Args
);
463 Tool
*FreeBSD::buildAssembler() const {
464 return new tools::freebsd::Assembler(*this);
467 Tool
*FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
469 bool FreeBSD::HasNativeLLVMSupport() const { return true; }
471 ToolChain::UnwindTableLevel
472 FreeBSD::getDefaultUnwindTableLevel(const ArgList
&Args
) const {
473 return UnwindTableLevel::Asynchronous
;
476 bool FreeBSD::isPIEDefault(const llvm::opt::ArgList
&Args
) const {
477 return getSanitizerArgs(Args
).requiresPIE();
480 SanitizerMask
FreeBSD::getSupportedSanitizers() const {
481 const bool IsAArch64
= getTriple().getArch() == llvm::Triple::aarch64
;
482 const bool IsX86
= getTriple().getArch() == llvm::Triple::x86
;
483 const bool IsX86_64
= getTriple().getArch() == llvm::Triple::x86_64
;
484 const bool IsMIPS64
= getTriple().isMIPS64();
485 SanitizerMask Res
= ToolChain::getSupportedSanitizers();
486 Res
|= SanitizerKind::Address
;
487 Res
|= SanitizerKind::PointerCompare
;
488 Res
|= SanitizerKind::PointerSubtract
;
489 Res
|= SanitizerKind::Vptr
;
490 if (IsAArch64
|| IsX86_64
|| IsMIPS64
) {
491 Res
|= SanitizerKind::Leak
;
492 Res
|= SanitizerKind::Thread
;
494 if (IsAArch64
|| IsX86
|| IsX86_64
) {
495 Res
|= SanitizerKind::SafeStack
;
496 Res
|= SanitizerKind::Fuzzer
;
497 Res
|= SanitizerKind::FuzzerNoLink
;
499 if (IsAArch64
|| IsX86_64
) {
500 Res
|= SanitizerKind::KernelAddress
;
501 Res
|= SanitizerKind::KernelMemory
;
502 Res
|= SanitizerKind::Memory
;