clang  13.0.0git
OpenBSD.cpp
Go to the documentation of this file.
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"
15 #include "clang/Driver/Options.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 
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,
87  Exec, CmdArgs, Inputs, Output));
88 }
89 
91  const InputInfo &Output,
92  const InputInfoList &Inputs,
93  const ArgList &Args,
94  const char *LinkingOutput) const {
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  if (D.CCCIsCXX()) {
178  if (ToolChain.ShouldLinkCXXStdlib(Args))
179  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
180  if (Args.hasArg(options::OPT_pg))
181  CmdArgs.push_back("-lm_p");
182  else
183  CmdArgs.push_back("-lm");
184  }
185  if (NeedsSanitizerDeps) {
186  CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
188  }
189  if (NeedsXRayDeps) {
190  CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
191  linkXRayRuntimeDeps(ToolChain, CmdArgs);
192  }
193  // FIXME: For some reason GCC passes -lgcc before adding
194  // the default system libraries. Just mimic this for now.
195  CmdArgs.push_back("-lcompiler_rt");
196 
197  if (Args.hasArg(options::OPT_pthread)) {
198  if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
199  CmdArgs.push_back("-lpthread_p");
200  else
201  CmdArgs.push_back("-lpthread");
202  }
203 
204  if (!Args.hasArg(options::OPT_shared)) {
205  if (Args.hasArg(options::OPT_pg))
206  CmdArgs.push_back("-lc_p");
207  else
208  CmdArgs.push_back("-lc");
209  }
210 
211  CmdArgs.push_back("-lcompiler_rt");
212  }
213 
214  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
215  const char *crtend = nullptr;
216  if (!Args.hasArg(options::OPT_shared))
217  crtend = "crtend.o";
218  else
219  crtend = "crtendS.o";
220 
221  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
222  }
223 
224  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
225  C.addCommand(std::make_unique<Command>(JA, *this,
227  Exec, CmdArgs, Inputs, Output));
228 }
229 
230 SanitizerMask OpenBSD::getSupportedSanitizers() const {
231  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
232  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
233 
234  // For future use, only UBsan at the moment
236 
237  if (IsX86 || IsX86_64) {
238  Res |= SanitizerKind::Vptr;
239  Res |= SanitizerKind::Fuzzer;
240  Res |= SanitizerKind::FuzzerNoLink;
241  }
242 
243  return Res;
244 }
245 
246 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
247 
248 OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
249  const ArgList &Args)
250  : Generic_ELF(D, Triple, Args) {
251  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
252 }
253 
255  const llvm::opt::ArgList &DriverArgs,
256  llvm::opt::ArgStringList &CC1Args) const {
257  const Driver &D = getDriver();
258 
259  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
260  return;
261 
262  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
264  llvm::sys::path::append(Dir, "include");
265  addSystemInclude(DriverArgs, CC1Args, Dir.str());
266  }
267 
268  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
269  return;
270 
271  // Check for configure-time C include directories.
272  StringRef CIncludeDirs(C_INCLUDE_DIRS);
273  if (CIncludeDirs != "") {
275  CIncludeDirs.split(dirs, ":");
276  for (StringRef dir : dirs) {
277  StringRef Prefix =
278  llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
279  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
280  }
281  return;
282  }
283 
284  addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
285 }
286 
287 void OpenBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
288  llvm::opt::ArgStringList &CC1Args) const {
289  addSystemInclude(DriverArgs, CC1Args,
290  getDriver().SysRoot + "/usr/include/c++/v1");
291 }
292 
293 void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args,
294  ArgStringList &CmdArgs) const {
295  bool Profiling = Args.hasArg(options::OPT_pg);
296 
297  CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
298  CmdArgs.push_back(Profiling ? "-lc++abi_p" : "-lc++abi");
299  CmdArgs.push_back(Profiling ? "-lpthread_p" : "-lpthread");
300 }
301 
303  StringRef Component,
304  FileType Type) const {
305  SmallString<128> Path(getDriver().SysRoot);
306  llvm::sys::path::append(Path, "/usr/lib/libcompiler_rt.a");
307  return std::string(Path.str());
308 }
309 
311  return new tools::openbsd::Assembler(*this);
312 }
313 
314 Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
315 
316 bool OpenBSD::HasNativeLLVMSupport() const { return true; }
clang::driver::tools::mips::getGnuCompatibleMipsABIName
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:143
clang::driver::toolchains
Definition: AIX.h:55
Mips.h
clang::driver::toolchains::OpenBSD::addLibCxxIncludePaths
void addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: OpenBSD.cpp:287
clang::driver::tools::AddAssemblerKPIC
void AddAssemblerKPIC(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::tools
Definition: AIX.h:17
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:214
clang::driver::toolchains::OpenBSD::AddClangSystemIncludeArgs
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: OpenBSD.cpp:254
Sparc.h
clang::driver::ToolChain::GetFilePath
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:557
clang::driver::ToolChain::GetLinkerPath
std::string GetLinkerPath(bool *LinkerIsLLD=nullptr, bool *LinkerIsLLDDarwinNew=nullptr) const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name.
Definition: ToolChain.cpp:565
clang::driver::tools::openbsd::Assembler::ConstructJob
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: OpenBSD.cpp:26
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
clang::driver::toolchains::OpenBSD::getCompilerRT
std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const override
Definition: OpenBSD.cpp:302
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1478
clang::driver::tools::AddLinkerInputs
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
clang::driver::ToolChain::getFilePaths
path_list & getFilePaths()
Definition: ToolChain.h:252
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
clang::driver::toolchains::Generic_ELF
Definition: Gnu.h:367
clang::driver::tools::claimNoWarnArgs
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
Options.h
clang::driver::InputInfo::isFilename
bool isFilename() const
Definition: InputInfo.h:75
llvm::opt
Definition: DiagnosticOptions.h:19
clang::driver::ResponseFileSupport::AtFileCurCP
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:91
clang::driver::tools::openbsd::Linker
Definition: OpenBSD.h:36
clang::driver::InputInfo::isNothing
bool isNothing() const
Definition: InputInfo.h:74
llvm::SmallString< 128 >
OpenBSD.h
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
clang::driver::Driver::CCCIsCXX
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:176
clang::driver::tools::openbsd::Linker::ConstructJob
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: OpenBSD.cpp:90
clang::driver::tools::addXRayRuntime
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
SanitizerArgs.h
clang::driver::Driver::ResourceDir
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:132
clang::driver::ToolChain::getArch
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:230
clang::driver::ToolChain::AddFilePathLibArgs
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:972
clang::driver::ToolChain::ShouldLinkCXXStdlib
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:949
clang::driver::tools::openbsd::Assembler
Definition: OpenBSD.h:23
clang::driver::ToolChain::getSupportedSanitizers
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:1015
clang::driver::ToolChain::AddCXXStdlibLibArgs
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:955
Compilation.h
clang::driver::ToolChain::getCompilerRTArgString
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
Definition: ToolChain.cpp:478
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::tools::getCPUName
std::string getCPUName(const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
clang::driver::toolchains::OpenBSD::AddCXXStdlibLibArgs
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: OpenBSD.cpp:293
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::ToolChain::FileType
FileType
Definition: ToolChain.h:116
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
clang::driver::tools::mips::getMipsCPUAndABI
void getMipsCPUAndABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, StringRef &CPUName, StringRef &ABIName)
clang::driver::tools::linkSanitizerRuntimeDeps
void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
CommonArgs.h
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
clang::driver::tools::addSanitizerRuntimes
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
clang::driver::toolchains::OpenBSD::HasNativeLLVMSupport
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: OpenBSD.cpp:316
clang::driver::tools::linkXRayRuntimeDeps
void linkXRayRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
clang::driver::toolchains::OpenBSD::buildLinker
Tool * buildLinker() const override
Definition: OpenBSD.cpp:314
clang::driver::ToolChain::addExternCSystemInclude
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments.
Definition: ToolChain.cpp:877
clang::driver::toolchains::OpenBSD::buildAssembler
Tool * buildAssembler() const override
Definition: OpenBSD.cpp:310
clang::driver
Definition: Action.h:31
clang::driver::ToolChain::addSystemInclude
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:862
clang::driver::toolchains::OpenBSD
Definition: OpenBSD.h:53
clang::driver::JobAction
Definition: Action.h:380
clang::driver::tools::sparc::getSparcAsmModeForCPU
const char * getSparcAsmModeForCPU(llvm::StringRef Name, const llvm::Triple &Triple)
clang::SanitizerMask
Definition: Sanitizers.h:30