1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include "Fuchsia.h" |
10 | #include "CommonArgs.h" |
11 | #include "clang/Config/config.h" |
12 | #include "clang/Driver/Compilation.h" |
13 | #include "clang/Driver/Driver.h" |
14 | #include "clang/Driver/DriverDiagnostic.h" |
15 | #include "clang/Driver/Options.h" |
16 | #include "clang/Driver/SanitizerArgs.h" |
17 | #include "llvm/Option/ArgList.h" |
18 | #include "llvm/Support/Path.h" |
19 | |
20 | using namespace clang::driver; |
21 | using namespace clang::driver::toolchains; |
22 | using namespace clang::driver::tools; |
23 | using namespace clang; |
24 | using namespace llvm::opt; |
25 | |
26 | void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
27 | const InputInfo &Output, |
28 | const InputInfoList &Inputs, |
29 | const ArgList &Args, |
30 | const char *LinkingOutput) const { |
31 | const toolchains::Fuchsia &ToolChain = |
32 | static_cast<const toolchains::Fuchsia &>(getToolChain()); |
33 | const Driver &D = ToolChain.getDriver(); |
34 | |
35 | ArgStringList CmdArgs; |
36 | |
37 | |
38 | Args.ClaimAllArgs(options::OPT_g_Group); |
39 | |
40 | Args.ClaimAllArgs(options::OPT_emit_llvm); |
41 | |
42 | |
43 | Args.ClaimAllArgs(options::OPT_w); |
44 | |
45 | const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); |
46 | if (llvm::sys::path::filename(Exec).equals_lower("ld.lld") || |
47 | llvm::sys::path::stem(Exec).equals_lower("ld.lld")) { |
48 | CmdArgs.push_back("-z"); |
49 | CmdArgs.push_back("rodynamic"); |
50 | } |
51 | |
52 | if (!D.SysRoot.empty()) |
53 | CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); |
54 | |
55 | if (!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_r)) |
56 | CmdArgs.push_back("-pie"); |
57 | |
58 | if (Args.hasArg(options::OPT_rdynamic)) |
59 | CmdArgs.push_back("-export-dynamic"); |
60 | |
61 | if (Args.hasArg(options::OPT_s)) |
62 | CmdArgs.push_back("-s"); |
63 | |
64 | if (Args.hasArg(options::OPT_r)) { |
65 | CmdArgs.push_back("-r"); |
66 | } else { |
67 | CmdArgs.push_back("--build-id"); |
68 | CmdArgs.push_back("--hash-style=gnu"); |
69 | } |
70 | |
71 | CmdArgs.push_back("--eh-frame-hdr"); |
72 | |
73 | if (Args.hasArg(options::OPT_static)) |
74 | CmdArgs.push_back("-Bstatic"); |
75 | else if (Args.hasArg(options::OPT_shared)) |
76 | CmdArgs.push_back("-shared"); |
77 | |
78 | const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(); |
79 | |
80 | if (!Args.hasArg(options::OPT_shared)) { |
81 | std::string Dyld = D.DyldPrefix; |
82 | if (SanArgs.needsAsanRt() && SanArgs.needsSharedRt()) |
83 | Dyld += "asan/"; |
84 | Dyld += "ld.so.1"; |
85 | CmdArgs.push_back("-dynamic-linker"); |
86 | CmdArgs.push_back(Args.MakeArgString(Dyld)); |
87 | } |
88 | |
89 | CmdArgs.push_back("-o"); |
90 | CmdArgs.push_back(Output.getFilename()); |
91 | |
92 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { |
93 | if (!Args.hasArg(options::OPT_shared)) { |
94 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o"))); |
95 | } |
96 | } |
97 | |
98 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
99 | Args.AddAllArgs(CmdArgs, options::OPT_u); |
100 | |
101 | addSanitizerPathLibArgs(ToolChain, Args, CmdArgs); |
102 | |
103 | ToolChain.AddFilePathLibArgs(Args, CmdArgs); |
104 | |
105 | if (D.isUsingLTO()) { |
106 | (0) . __assert_fail ("!Inputs.empty() && \"Must have at least one input.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/ToolChains/Fuchsia.cpp", 106, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!Inputs.empty() && "Must have at least one input."); |
107 | AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0], |
108 | D.getLTOMode() == LTOK_Thin); |
109 | } |
110 | |
111 | bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); |
112 | bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); |
113 | AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); |
114 | ToolChain.addProfileRTLibs(Args, CmdArgs); |
115 | |
116 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { |
117 | if (Args.hasArg(options::OPT_static)) |
118 | CmdArgs.push_back("-Bdynamic"); |
119 | |
120 | if (D.CCCIsCXX()) { |
121 | if (ToolChain.ShouldLinkCXXStdlib(Args)) { |
122 | bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) && |
123 | !Args.hasArg(options::OPT_static); |
124 | CmdArgs.push_back("--push-state"); |
125 | CmdArgs.push_back("--as-needed"); |
126 | if (OnlyLibstdcxxStatic) |
127 | CmdArgs.push_back("-Bstatic"); |
128 | ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); |
129 | if (OnlyLibstdcxxStatic) |
130 | CmdArgs.push_back("-Bdynamic"); |
131 | CmdArgs.push_back("-lm"); |
132 | CmdArgs.push_back("--pop-state"); |
133 | } |
134 | } |
135 | |
136 | if (NeedsSanitizerDeps) |
137 | linkSanitizerRuntimeDeps(ToolChain, CmdArgs); |
138 | |
139 | if (NeedsXRayDeps) |
140 | linkXRayRuntimeDeps(ToolChain, CmdArgs); |
141 | |
142 | AddRunTimeLibs(ToolChain, D, CmdArgs, Args); |
143 | |
144 | if (Args.hasArg(options::OPT_pthread) || |
145 | Args.hasArg(options::OPT_pthreads)) |
146 | CmdArgs.push_back("-lpthread"); |
147 | |
148 | if (Args.hasArg(options::OPT_fsplit_stack)) |
149 | CmdArgs.push_back("--wrap=pthread_create"); |
150 | |
151 | if (!Args.hasArg(options::OPT_nolibc)) |
152 | CmdArgs.push_back("-lc"); |
153 | } |
154 | |
155 | C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); |
156 | } |
157 | |
158 | |
159 | |
160 | Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple, |
161 | const ArgList &Args) |
162 | : ToolChain(D, Triple, Args) { |
163 | getProgramPaths().push_back(getDriver().getInstalledDir()); |
164 | if (getDriver().getInstalledDir() != D.Dir) |
165 | getProgramPaths().push_back(D.Dir); |
166 | |
167 | if (!D.SysRoot.empty()) { |
168 | SmallString<128> P(D.SysRoot); |
169 | llvm::sys::path::append(P, "lib"); |
170 | getFilePaths().push_back(P.str()); |
171 | } |
172 | } |
173 | |
174 | std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args, |
175 | types::ID InputType) const { |
176 | llvm::Triple Triple(ComputeLLVMTriple(Args, InputType)); |
177 | return (Triple.getArchName() + "-" + Triple.getOSName()).str(); |
178 | } |
179 | |
180 | Tool *Fuchsia::buildLinker() const { |
181 | return new tools::fuchsia::Linker(*this); |
182 | } |
183 | |
184 | ToolChain::RuntimeLibType Fuchsia::GetRuntimeLibType( |
185 | const ArgList &Args) const { |
186 | if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) { |
187 | StringRef Value = A->getValue(); |
188 | if (Value != "compiler-rt") |
189 | getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name) |
190 | << A->getAsString(Args); |
191 | } |
192 | |
193 | return ToolChain::RLT_CompilerRT; |
194 | } |
195 | |
196 | ToolChain::CXXStdlibType |
197 | Fuchsia::GetCXXStdlibType(const ArgList &Args) const { |
198 | if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { |
199 | StringRef Value = A->getValue(); |
200 | if (Value != "libc++") |
201 | getDriver().Diag(diag::err_drv_invalid_stdlib_name) |
202 | << A->getAsString(Args); |
203 | } |
204 | |
205 | return ToolChain::CST_Libcxx; |
206 | } |
207 | |
208 | void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs, |
209 | ArgStringList &CC1Args, |
210 | Action::OffloadKind) const { |
211 | if (DriverArgs.hasFlag(options::OPT_fuse_init_array, |
212 | options::OPT_fno_use_init_array, true)) |
213 | CC1Args.push_back("-fuse-init-array"); |
214 | } |
215 | |
216 | void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
217 | ArgStringList &CC1Args) const { |
218 | const Driver &D = getDriver(); |
219 | |
220 | if (DriverArgs.hasArg(options::OPT_nostdinc)) |
221 | return; |
222 | |
223 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
224 | SmallString<128> P(D.ResourceDir); |
225 | llvm::sys::path::append(P, "include"); |
226 | addSystemInclude(DriverArgs, CC1Args, P); |
227 | } |
228 | |
229 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
230 | return; |
231 | |
232 | |
233 | StringRef CIncludeDirs(C_INCLUDE_DIRS); |
234 | if (CIncludeDirs != "") { |
235 | SmallVector<StringRef, 5> dirs; |
236 | CIncludeDirs.split(dirs, ":"); |
237 | for (StringRef dir : dirs) { |
238 | StringRef Prefix = |
239 | llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : ""; |
240 | addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); |
241 | } |
242 | return; |
243 | } |
244 | |
245 | if (!D.SysRoot.empty()) { |
246 | SmallString<128> P(D.SysRoot); |
247 | llvm::sys::path::append(P, "include"); |
248 | addExternCSystemInclude(DriverArgs, CC1Args, P.str()); |
249 | } |
250 | } |
251 | |
252 | void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, |
253 | ArgStringList &CC1Args) const { |
254 | if (DriverArgs.hasArg(options::OPT_nostdlibinc) || |
255 | DriverArgs.hasArg(options::OPT_nostdincxx)) |
256 | return; |
257 | |
258 | switch (GetCXXStdlibType(DriverArgs)) { |
259 | case ToolChain::CST_Libcxx: { |
260 | SmallString<128> P(getDriver().ResourceDir); |
261 | llvm::sys::path::append(P, "include", "c++", "v1"); |
262 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
263 | break; |
264 | } |
265 | |
266 | default: |
267 | llvm_unreachable("invalid stdlib name"); |
268 | } |
269 | } |
270 | |
271 | void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args, |
272 | ArgStringList &CmdArgs) const { |
273 | switch (GetCXXStdlibType(Args)) { |
274 | case ToolChain::CST_Libcxx: |
275 | CmdArgs.push_back("-lc++"); |
276 | break; |
277 | |
278 | case ToolChain::CST_Libstdcxx: |
279 | llvm_unreachable("invalid stdlib name"); |
280 | } |
281 | } |
282 | |
283 | SanitizerMask Fuchsia::getSupportedSanitizers() const { |
284 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
285 | Res |= SanitizerKind::Address; |
286 | Res |= SanitizerKind::Fuzzer; |
287 | Res |= SanitizerKind::FuzzerNoLink; |
288 | Res |= SanitizerKind::SafeStack; |
289 | Res |= SanitizerKind::Scudo; |
290 | return Res; |
291 | } |
292 | |
293 | SanitizerMask Fuchsia::getDefaultSanitizers() const { |
294 | return SanitizerKind::SafeStack; |
295 | } |
296 | |