clang  13.0.0git
DragonFly.cpp
Go to the documentation of this file.
1 //===--- DragonFly.cpp - DragonFly 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 "DragonFly.h"
10 #include "CommonArgs.h"
12 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/Option/ArgList.h"
15 
16 using namespace clang::driver;
17 using namespace clang::driver::tools;
18 using namespace clang::driver::toolchains;
19 using namespace clang;
20 using namespace llvm::opt;
21 
22 /// DragonFly Tools
23 
24 // For now, DragonFly Assemble does just about the same as for
25 // FreeBSD, but this may change soon.
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  // When building 32-bit code on DragonFly/pc64, we have to explicitly
35  // instruct as in the base system to assemble 32-bit code.
36  if (getToolChain().getArch() == llvm::Triple::x86)
37  CmdArgs.push_back("--32");
38 
39  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
40 
41  CmdArgs.push_back("-o");
42  CmdArgs.push_back(Output.getFilename());
43 
44  for (const auto &II : Inputs)
45  CmdArgs.push_back(II.getFilename());
46 
47  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
48  C.addCommand(std::make_unique<Command>(JA, *this,
50  Exec, CmdArgs, Inputs, Output));
51 }
52 
54  const InputInfo &Output,
55  const InputInfoList &Inputs,
56  const ArgList &Args,
57  const char *LinkingOutput) const {
58  const Driver &D = getToolChain().getDriver();
59  ArgStringList CmdArgs;
60 
61  if (!D.SysRoot.empty())
62  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
63 
64  CmdArgs.push_back("--eh-frame-hdr");
65  if (Args.hasArg(options::OPT_static)) {
66  CmdArgs.push_back("-Bstatic");
67  } else {
68  if (Args.hasArg(options::OPT_rdynamic))
69  CmdArgs.push_back("-export-dynamic");
70  if (Args.hasArg(options::OPT_shared))
71  CmdArgs.push_back("-Bshareable");
72  else {
73  CmdArgs.push_back("-dynamic-linker");
74  CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
75  }
76  CmdArgs.push_back("--hash-style=gnu");
77  CmdArgs.push_back("--enable-new-dtags");
78  }
79 
80  // When building 32-bit code on DragonFly/pc64, we have to explicitly
81  // instruct ld in the base system to link 32-bit code.
82  if (getToolChain().getArch() == llvm::Triple::x86) {
83  CmdArgs.push_back("-m");
84  CmdArgs.push_back("elf_i386");
85  }
86 
87  if (Output.isFilename()) {
88  CmdArgs.push_back("-o");
89  CmdArgs.push_back(Output.getFilename());
90  } else {
91  assert(Output.isNothing() && "Invalid output.");
92  }
93 
94  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
95  if (!Args.hasArg(options::OPT_shared)) {
96  if (Args.hasArg(options::OPT_pg))
97  CmdArgs.push_back(
98  Args.MakeArgString(getToolChain().GetFilePath("gcrt1.o")));
99  else {
100  if (Args.hasArg(options::OPT_pie))
101  CmdArgs.push_back(
102  Args.MakeArgString(getToolChain().GetFilePath("Scrt1.o")));
103  else
104  CmdArgs.push_back(
105  Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
106  }
107  }
108  CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
109  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
110  CmdArgs.push_back(
111  Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
112  else
113  CmdArgs.push_back(
114  Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
115  }
116 
117  Args.AddAllArgs(CmdArgs,
118  {options::OPT_L, options::OPT_T_Group, options::OPT_e});
119 
120  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
121 
122  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
123  CmdArgs.push_back("-L/usr/lib/gcc80");
124 
125  if (!Args.hasArg(options::OPT_static)) {
126  CmdArgs.push_back("-rpath");
127  CmdArgs.push_back("/usr/lib/gcc80");
128  }
129 
130  if (D.CCCIsCXX()) {
131  if (getToolChain().ShouldLinkCXXStdlib(Args))
132  getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
133  CmdArgs.push_back("-lm");
134  }
135 
136  if (Args.hasArg(options::OPT_pthread))
137  CmdArgs.push_back("-lpthread");
138 
139  if (!Args.hasArg(options::OPT_nolibc)) {
140  CmdArgs.push_back("-lc");
141  }
142 
143  if (Args.hasArg(options::OPT_static) ||
144  Args.hasArg(options::OPT_static_libgcc)) {
145  CmdArgs.push_back("-lgcc");
146  CmdArgs.push_back("-lgcc_eh");
147  } else {
148  if (Args.hasArg(options::OPT_shared_libgcc)) {
149  CmdArgs.push_back("-lgcc_pic");
150  if (!Args.hasArg(options::OPT_shared))
151  CmdArgs.push_back("-lgcc");
152  } else {
153  CmdArgs.push_back("-lgcc");
154  CmdArgs.push_back("--as-needed");
155  CmdArgs.push_back("-lgcc_pic");
156  CmdArgs.push_back("--no-as-needed");
157  }
158  }
159  }
160 
161  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
162  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
163  CmdArgs.push_back(
164  Args.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
165  else
166  CmdArgs.push_back(
167  Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
168  CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
169  }
170 
171  getToolChain().addProfileRTLibs(Args, CmdArgs);
172 
173  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
174  C.addCommand(std::make_unique<Command>(JA, *this,
176  Exec, CmdArgs, Inputs, Output));
177 }
178 
179 /// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
180 
181 DragonFly::DragonFly(const Driver &D, const llvm::Triple &Triple,
182  const ArgList &Args)
183  : Generic_ELF(D, Triple, Args) {
184 
185  // Path mangling to find libexec
186  getProgramPaths().push_back(getDriver().getInstalledDir());
187  if (getDriver().getInstalledDir() != getDriver().Dir)
188  getProgramPaths().push_back(getDriver().Dir);
189 
190  getFilePaths().push_back(getDriver().Dir + "/../lib");
191  getFilePaths().push_back("/usr/lib");
192  getFilePaths().push_back("/usr/lib/gcc80");
193 }
194 
196  return new tools::dragonfly::Assembler(*this);
197 }
198 
200  return new tools::dragonfly::Linker(*this);
201 }
clang::driver::toolchains
Definition: AIX.h:55
clang::driver::ToolChain::getProgramPaths
path_list & getProgramPaths()
Definition: ToolChain.h:255
Driver.h
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::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
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::toolchains::DragonFly::buildAssembler
Tool * buildAssembler() const override
Definition: DragonFly.cpp:195
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::tools::dragonfly::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: DragonFly.cpp:53
clang::driver::ResponseFileSupport::AtFileCurCP
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:91
clang::driver::InputInfo::isNothing
bool isNothing() const
Definition: InputInfo.h:74
clang::driver::tools::dragonfly::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
DragonFly Tools.
Definition: DragonFly.cpp:26
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::dragonfly::Linker
Definition: DragonFly.h:34
Compilation.h
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::tools::dragonfly::Assembler
Definition: DragonFly.h:21
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
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
Definition: Action.h:31
clang::driver::toolchains::DragonFly::buildLinker
Tool * buildLinker() const override
Definition: DragonFly.cpp:199
clang::driver::JobAction
Definition: Action.h:380
DragonFly.h