1//===--- FreeBSD.cpp - FreeBSD 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 "FreeBSD.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/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"
21
22using namespace clang::driver;
23using namespace clang::driver::tools;
24using namespace clang::driver::toolchains;
25using namespace clang;
26using namespace llvm::opt;
27
28void freebsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
29 const InputInfo &Output,
30 const InputInfoList &Inputs,
31 const ArgList &Args,
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;
37
38 claimNoWarnArgs(Args);
39
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()) {
43 default:
44 break;
45 case llvm::Triple::x86:
46 CmdArgs.push_back(Elt: "--32");
47 break;
48 case llvm::Triple::ppc:
49 case llvm::Triple::ppcle:
50 CmdArgs.push_back(Elt: "-a32");
51 break;
52 case llvm::Triple::mips:
53 case llvm::Triple::mipsel:
54 case llvm::Triple::mips64:
55 case llvm::Triple::mips64el: {
56 StringRef CPUName;
57 StringRef ABIName;
58 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
59
60 CmdArgs.push_back(Elt: "-march");
61 CmdArgs.push_back(Elt: CPUName.data());
62
63 CmdArgs.push_back(Elt: "-mabi");
64 CmdArgs.push_back(Elt: mips::getGnuCompatibleMipsABIName(ABI: ABIName).data());
65
66 if (Triple.isLittleEndian())
67 CmdArgs.push_back(Elt: "-EL");
68 else
69 CmdArgs.push_back(Elt: "-EB");
70
71 if (Arg *A = Args.getLastArg(options::OPT_G)) {
72 StringRef v = A->getValue();
73 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-G" + v));
74 A->claim();
75 }
76
77 AddAssemblerKPIC(ToolChain, Args, CmdArgs);
78 break;
79 }
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(TC: ToolChain, Args);
85
86 if (ABI == arm::FloatABI::Hard)
87 CmdArgs.push_back(Elt: "-mfpu=vfp");
88 else
89 CmdArgs.push_back(Elt: "-mfpu=softvfp");
90
91 CmdArgs.push_back(Elt: "-meabi=5");
92 break;
93 }
94 case llvm::Triple::sparcv9: {
95 std::string CPU = getCPUName(D, Args, T: Triple);
96 CmdArgs.push_back(Elt: sparc::getSparcAsmModeForCPU(Name: CPU, Triple));
97 AddAssemblerKPIC(ToolChain, Args, CmdArgs);
98 break;
99 }
100 }
101
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();
108 else {
109 CmdArgs.push_back(Args.MakeArgString("--debug-prefix-map"));
110 CmdArgs.push_back(Args.MakeArgString(Map));
111 }
112 A->claim();
113 }
114
115 Args.AddAllArgValues(Output&: CmdArgs, options::Id0: OPT_Wa_COMMA, options::Id1: OPT_Xassembler);
116
117 CmdArgs.push_back(Elt: "-o");
118 CmdArgs.push_back(Elt: Output.getFilename());
119
120 for (const auto &II : Inputs)
121 CmdArgs.push_back(Elt: II.getFilename());
122
123 const char *Exec = Args.MakeArgString(Str: ToolChain.GetProgramPath(Name: "as"));
124 C.addCommand(C: std::make_unique<Command>(args: JA, args: *this,
125 args: ResponseFileSupport::AtFileCurCP(),
126 args&: Exec, args&: CmdArgs, args: Inputs, args: Output));
127}
128
129void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
130 const InputInfo &Output,
131 const InputInfoList &Inputs,
132 const ArgList &Args,
133 const char *LinkingOutput) const {
134 const auto &ToolChain = static_cast<const FreeBSD &>(getToolChain());
135 const Driver &D = ToolChain.getDriver();
136 const llvm::Triple &Triple = ToolChain.getTriple();
137 const llvm::Triple::ArchType Arch = ToolChain.getArch();
138 const bool IsPIE =
139 !Args.hasArg(options::OPT_shared) &&
140 (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault(Args));
141 ArgStringList CmdArgs;
142
143 // Silence warning for "clang -g foo.o -o foo"
144 Args.ClaimAllArgs(options::OPT_g_Group);
145 // and "clang -emit-llvm foo.o -o foo"
146 Args.ClaimAllArgs(options::OPT_emit_llvm);
147 // and for "clang -w foo.o -o foo". Other warning options are already
148 // handled somewhere else.
149 Args.ClaimAllArgs(options::OPT_w);
150
151 if (!D.SysRoot.empty())
152 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--sysroot=" + D.SysRoot));
153
154 if (IsPIE)
155 CmdArgs.push_back(Elt: "-pie");
156
157 CmdArgs.push_back(Elt: "--eh-frame-hdr");
158 if (Args.hasArg(options::OPT_static)) {
159 CmdArgs.push_back(Elt: "-Bstatic");
160 } else {
161 if (Args.hasArg(options::OPT_rdynamic))
162 CmdArgs.push_back(Elt: "-export-dynamic");
163 if (Args.hasArg(options::OPT_shared)) {
164 CmdArgs.push_back(Elt: "-shared");
165 } else if (!Args.hasArg(options::OPT_r)) {
166 CmdArgs.push_back(Elt: "-dynamic-linker");
167 CmdArgs.push_back(Elt: "/libexec/ld-elf.so.1");
168 }
169 if (Arch == llvm::Triple::arm || Triple.isX86())
170 CmdArgs.push_back(Elt: "--hash-style=both");
171 CmdArgs.push_back(Elt: "--enable-new-dtags");
172 }
173
174 // Explicitly set the linker emulation for platforms that might not
175 // be the default emulation for the linker.
176 switch (Arch) {
177 case llvm::Triple::x86:
178 CmdArgs.push_back(Elt: "-m");
179 CmdArgs.push_back(Elt: "elf_i386_fbsd");
180 break;
181 case llvm::Triple::ppc:
182 CmdArgs.push_back(Elt: "-m");
183 CmdArgs.push_back(Elt: "elf32ppc_fbsd");
184 break;
185 case llvm::Triple::ppcle:
186 CmdArgs.push_back(Elt: "-m");
187 // Use generic -- only usage is for freestanding.
188 CmdArgs.push_back(Elt: "elf32lppc");
189 break;
190 case llvm::Triple::mips:
191 CmdArgs.push_back(Elt: "-m");
192 CmdArgs.push_back(Elt: "elf32btsmip_fbsd");
193 break;
194 case llvm::Triple::mipsel:
195 CmdArgs.push_back(Elt: "-m");
196 CmdArgs.push_back(Elt: "elf32ltsmip_fbsd");
197 break;
198 case llvm::Triple::mips64:
199 CmdArgs.push_back(Elt: "-m");
200 if (tools::mips::hasMipsAbiArg(Args, Value: "n32"))
201 CmdArgs.push_back(Elt: "elf32btsmipn32_fbsd");
202 else
203 CmdArgs.push_back(Elt: "elf64btsmip_fbsd");
204 break;
205 case llvm::Triple::mips64el:
206 CmdArgs.push_back(Elt: "-m");
207 if (tools::mips::hasMipsAbiArg(Args, Value: "n32"))
208 CmdArgs.push_back(Elt: "elf32ltsmipn32_fbsd");
209 else
210 CmdArgs.push_back(Elt: "elf64ltsmip_fbsd");
211 break;
212 case llvm::Triple::riscv64:
213 CmdArgs.push_back(Elt: "-m");
214 CmdArgs.push_back(Elt: "elf64lriscv");
215 break;
216 default:
217 break;
218 }
219
220 if (Triple.isRISCV64()) {
221 CmdArgs.push_back(Elt: "-X");
222 if (Args.hasArg(options::OPT_mno_relax))
223 CmdArgs.push_back(Elt: "--no-relax");
224 }
225
226 if (Arg *A = Args.getLastArg(options::OPT_G)) {
227 if (ToolChain.getTriple().isMIPS()) {
228 StringRef v = A->getValue();
229 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-G" + v));
230 A->claim();
231 }
232 }
233
234 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
235 if (Output.isFilename()) {
236 CmdArgs.push_back(Elt: "-o");
237 CmdArgs.push_back(Elt: Output.getFilename());
238 }
239
240 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
241 options::OPT_r)) {
242 const char *crt1 = nullptr;
243 if (!Args.hasArg(options::OPT_shared)) {
244 if (Args.hasArg(options::OPT_pg))
245 crt1 = "gcrt1.o";
246 else if (IsPIE)
247 crt1 = "Scrt1.o";
248 else
249 crt1 = "crt1.o";
250 }
251 if (crt1)
252 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: crt1)));
253
254 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "crti.o")));
255
256 const char *crtbegin = nullptr;
257 if (Args.hasArg(options::OPT_static))
258 crtbegin = "crtbeginT.o";
259 else if (Args.hasArg(options::OPT_shared) || IsPIE)
260 crtbegin = "crtbeginS.o";
261 else
262 crtbegin = "crtbegin.o";
263
264 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: crtbegin)));
265 }
266
267 Args.AddAllArgs(Output&: CmdArgs, options::Id0: OPT_L);
268 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
269 Args.addAllArgs(CmdArgs,
270 {options::OPT_T_Group, options::OPT_s, options::OPT_t});
271
272 if (D.isUsingLTO()) {
273 assert(!Inputs.empty() && "Must have at least one input.");
274 // Find the first filename InputInfo object.
275 auto Input = llvm::find_if(
276 Range: Inputs, P: [](const InputInfo &II) -> bool { return II.isFilename(); });
277 if (Input == Inputs.end())
278 // For a very rare case, all of the inputs to the linker are
279 // InputArg. If that happens, just use the first InputInfo.
280 Input = Inputs.begin();
281
282 addLTOOptions(ToolChain, Args, CmdArgs, Output, Input: *Input,
283 IsThinLTO: D.getLTOMode() == LTOK_Thin);
284 }
285
286 bool NeedsSanitizerDeps = addSanitizerRuntimes(TC: ToolChain, Args, CmdArgs);
287 bool NeedsXRayDeps = addXRayRuntime(TC: ToolChain, Args, CmdArgs);
288 addLinkerCompressDebugSectionsOption(TC: ToolChain, Args, CmdArgs);
289 AddLinkerInputs(TC: ToolChain, Inputs, Args, CmdArgs, JA);
290
291 unsigned Major = ToolChain.getTriple().getOSMajorVersion();
292 bool Profiling = Args.hasArg(options::OPT_pg) && Major != 0 && Major < 14;
293 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
294 options::OPT_r)) {
295 // Use the static OpenMP runtime with -static-openmp
296 bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
297 !Args.hasArg(options::OPT_static);
298 addOpenMPRuntime(C, CmdArgs, TC: ToolChain, Args, ForceStaticHostRuntime: StaticOpenMP);
299
300 if (D.CCCIsCXX()) {
301 if (ToolChain.ShouldLinkCXXStdlib(Args))
302 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
303 if (Profiling)
304 CmdArgs.push_back(Elt: "-lm_p");
305 else
306 CmdArgs.push_back(Elt: "-lm");
307 }
308
309 // Silence warnings when linking C code with a C++ '-stdlib' argument.
310 Args.ClaimAllArgs(options::OPT_stdlib_EQ);
311
312 // Additional linker set-up and flags for Fortran. This is required in order
313 // to generate executables. As Fortran runtime depends on the C runtime,
314 // these dependencies need to be listed before the C runtime below (i.e.
315 // AddRunTimeLibs).
316 if (D.IsFlangMode()) {
317 addFortranRuntimeLibraryPath(TC: ToolChain, Args, CmdArgs);
318 addFortranRuntimeLibs(TC: ToolChain, Args, CmdArgs);
319 if (Profiling)
320 CmdArgs.push_back(Elt: "-lm_p");
321 else
322 CmdArgs.push_back(Elt: "-lm");
323 }
324
325 if (NeedsSanitizerDeps)
326 linkSanitizerRuntimeDeps(TC: ToolChain, Args, CmdArgs);
327 if (NeedsXRayDeps)
328 linkXRayRuntimeDeps(TC: ToolChain, Args, CmdArgs);
329 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
330 // the default system libraries. Just mimic this for now.
331 if (Profiling)
332 CmdArgs.push_back(Elt: "-lgcc_p");
333 else
334 CmdArgs.push_back(Elt: "-lgcc");
335 if (Args.hasArg(options::OPT_static)) {
336 CmdArgs.push_back(Elt: "-lgcc_eh");
337 } else if (Profiling) {
338 CmdArgs.push_back(Elt: "-lgcc_eh_p");
339 } else {
340 CmdArgs.push_back(Elt: "--as-needed");
341 CmdArgs.push_back(Elt: "-lgcc_s");
342 CmdArgs.push_back(Elt: "--no-as-needed");
343 }
344
345 if (Args.hasArg(options::OPT_pthread)) {
346 if (Profiling)
347 CmdArgs.push_back(Elt: "-lpthread_p");
348 else
349 CmdArgs.push_back(Elt: "-lpthread");
350 }
351
352 if (Profiling) {
353 if (Args.hasArg(options::OPT_shared))
354 CmdArgs.push_back(Elt: "-lc");
355 else
356 CmdArgs.push_back(Elt: "-lc_p");
357 CmdArgs.push_back(Elt: "-lgcc_p");
358 } else {
359 CmdArgs.push_back(Elt: "-lc");
360 CmdArgs.push_back(Elt: "-lgcc");
361 }
362
363 if (Args.hasArg(options::OPT_static)) {
364 CmdArgs.push_back(Elt: "-lgcc_eh");
365 } else if (Profiling) {
366 CmdArgs.push_back(Elt: "-lgcc_eh_p");
367 } else {
368 CmdArgs.push_back(Elt: "--as-needed");
369 CmdArgs.push_back(Elt: "-lgcc_s");
370 CmdArgs.push_back(Elt: "--no-as-needed");
371 }
372 }
373
374 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
375 options::OPT_r)) {
376 const char *crtend = nullptr;
377 if (Args.hasArg(options::OPT_shared) || IsPIE)
378 crtend = "crtendS.o";
379 else
380 crtend = "crtend.o";
381 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: crtend)));
382 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "crtn.o")));
383 }
384
385 ToolChain.addProfileRTLibs(Args, CmdArgs);
386
387 const char *Exec = Args.MakeArgString(Str: getToolChain().GetLinkerPath());
388 C.addCommand(C: std::make_unique<Command>(args: JA, args: *this,
389 args: ResponseFileSupport::AtFileCurCP(),
390 args&: Exec, args&: CmdArgs, args: Inputs, args: Output));
391}
392
393/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
394
395FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple,
396 const ArgList &Args)
397 : Generic_ELF(D, Triple, Args) {
398
399 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
400 // back to '/usr/lib' if it doesn't exist.
401 if (Triple.isArch32Bit() &&
402 D.getVFS().exists(Path: concat(Path: getDriver().SysRoot, A: "/usr/lib32/crt1.o")))
403 getFilePaths().push_back(Elt: concat(Path: getDriver().SysRoot, A: "/usr/lib32"));
404 else
405 getFilePaths().push_back(Elt: concat(Path: getDriver().SysRoot, A: "/usr/lib"));
406}
407
408void FreeBSD::AddClangSystemIncludeArgs(
409 const llvm::opt::ArgList &DriverArgs,
410 llvm::opt::ArgStringList &CC1Args) const {
411 const Driver &D = getDriver();
412
413 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
414 return;
415
416 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
417 SmallString<128> Dir(D.ResourceDir);
418 llvm::sys::path::append(path&: Dir, a: "include");
419 addSystemInclude(DriverArgs, CC1Args, Path: Dir.str());
420 }
421
422 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
423 return;
424
425 // Check for configure-time C include directories.
426 StringRef CIncludeDirs(C_INCLUDE_DIRS);
427 if (CIncludeDirs != "") {
428 SmallVector<StringRef, 5> dirs;
429 CIncludeDirs.split(A&: dirs, Separator: ":");
430 for (StringRef dir : dirs) {
431 StringRef Prefix =
432 llvm::sys::path::is_absolute(path: dir) ? StringRef(D.SysRoot) : "";
433 addExternCSystemInclude(DriverArgs, CC1Args, Path: Prefix + dir);
434 }
435 return;
436 }
437
438 addExternCSystemInclude(DriverArgs, CC1Args,
439 Path: concat(Path: D.SysRoot, A: "/usr/include"));
440}
441
442void FreeBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
443 llvm::opt::ArgStringList &CC1Args) const {
444 addSystemInclude(DriverArgs, CC1Args,
445 Path: concat(Path: getDriver().SysRoot, A: "/usr/include/c++/v1"));
446}
447
448void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
449 ArgStringList &CmdArgs) const {
450 unsigned Major = getTriple().getOSMajorVersion();
451 bool Profiling = Args.hasArg(options::OPT_pg) && Major != 0 && Major < 14;
452
453 CmdArgs.push_back(Elt: Profiling ? "-lc++_p" : "-lc++");
454 if (Args.hasArg(options::OPT_fexperimental_library))
455 CmdArgs.push_back(Elt: "-lc++experimental");
456}
457
458void FreeBSD::AddCudaIncludeArgs(const ArgList &DriverArgs,
459 ArgStringList &CC1Args) const {
460 CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args);
461}
462
463void FreeBSD::AddHIPIncludeArgs(const ArgList &DriverArgs,
464 ArgStringList &CC1Args) const {
465 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
466}
467
468Tool *FreeBSD::buildAssembler() const {
469 return new tools::freebsd::Assembler(*this);
470}
471
472Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
473
474bool FreeBSD::HasNativeLLVMSupport() const { return true; }
475
476ToolChain::UnwindTableLevel
477FreeBSD::getDefaultUnwindTableLevel(const ArgList &Args) const {
478 return UnwindTableLevel::Asynchronous;
479}
480
481bool FreeBSD::isPIEDefault(const llvm::opt::ArgList &Args) const {
482 return getSanitizerArgs(JobArgs: Args).requiresPIE();
483}
484
485SanitizerMask FreeBSD::getSupportedSanitizers() const {
486 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64;
487 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
488 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
489 const bool IsMIPS64 = getTriple().isMIPS64();
490 SanitizerMask Res = ToolChain::getSupportedSanitizers();
491 Res |= SanitizerKind::Address;
492 Res |= SanitizerKind::PointerCompare;
493 Res |= SanitizerKind::PointerSubtract;
494 Res |= SanitizerKind::Vptr;
495 if (IsAArch64 || IsX86_64 || IsMIPS64) {
496 Res |= SanitizerKind::Leak;
497 Res |= SanitizerKind::Thread;
498 }
499 if (IsAArch64 || IsX86 || IsX86_64) {
500 Res |= SanitizerKind::SafeStack;
501 Res |= SanitizerKind::Fuzzer;
502 Res |= SanitizerKind::FuzzerNoLink;
503 }
504 if (IsAArch64 || IsX86_64) {
505 Res |= SanitizerKind::KernelAddress;
506 Res |= SanitizerKind::KernelMemory;
507 Res |= SanitizerKind::Memory;
508 }
509 return Res;
510}
511

source code of clang/lib/Driver/ToolChains/FreeBSD.cpp