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"
11 #include "clang/Driver/Compilation.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.
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const26 void dragonfly::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
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,
49 ResponseFileSupport::AtFileCurCP(),
50 Exec, CmdArgs, Inputs, Output));
51 }
52
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const53 void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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,
175 ResponseFileSupport::AtFileCurCP(),
176 Exec, CmdArgs, Inputs, Output));
177 }
178
179 /// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
180
DragonFly(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)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
buildAssembler() const195 Tool *DragonFly::buildAssembler() const {
196 return new tools::dragonfly::Assembler(*this);
197 }
198
buildLinker() const199 Tool *DragonFly::buildLinker() const {
200 return new tools::dragonfly::Linker(*this);
201 }
202