/src/llvm-project/clang/lib/Driver/ToolChains/OHOS.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- OHOS.cpp - OHOS 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 "OHOS.h" |
10 | | #include "Arch/ARM.h" |
11 | | #include "CommonArgs.h" |
12 | | #include "clang/Config/config.h" |
13 | | #include "clang/Driver/Compilation.h" |
14 | | #include "clang/Driver/Driver.h" |
15 | | #include "clang/Driver/DriverDiagnostic.h" |
16 | | #include "clang/Driver/Options.h" |
17 | | #include "clang/Driver/SanitizerArgs.h" |
18 | | #include "llvm/Option/ArgList.h" |
19 | | #include "llvm/ProfileData/InstrProf.h" |
20 | | #include "llvm/Support/FileSystem.h" |
21 | | #include "llvm/Support/Path.h" |
22 | | #include "llvm/Support/VirtualFileSystem.h" |
23 | | #include "llvm/Support/ScopedPrinter.h" |
24 | | |
25 | | using namespace clang::driver; |
26 | | using namespace clang::driver::toolchains; |
27 | | using namespace clang::driver::tools; |
28 | | using namespace clang; |
29 | | using namespace llvm::opt; |
30 | | using namespace clang::driver::tools::arm; |
31 | | |
32 | | using tools::addMultilibFlag; |
33 | | using tools::addPathIfExists; |
34 | | |
35 | | static bool findOHOSMuslMultilibs(const Multilib::flags_list &Flags, |
36 | 0 | DetectedMultilibs &Result) { |
37 | 0 | MultilibSet Multilibs; |
38 | 0 | Multilibs.push_back(Multilib()); |
39 | | // -mcpu=cortex-a7 |
40 | | // -mfloat-abi=soft -mfloat-abi=softfp -mfloat-abi=hard |
41 | | // -mfpu=neon-vfpv4 |
42 | 0 | Multilibs.push_back( |
43 | 0 | Multilib("/a7_soft", {}, {}, {"-mcpu=cortex-a7", "-mfloat-abi=soft"})); |
44 | |
|
45 | 0 | Multilibs.push_back( |
46 | 0 | Multilib("/a7_softfp_neon-vfpv4", {}, {}, |
47 | 0 | {"-mcpu=cortex-a7", "-mfloat-abi=softfp", "-mfpu=neon-vfpv4"})); |
48 | |
|
49 | 0 | Multilibs.push_back( |
50 | 0 | Multilib("/a7_hard_neon-vfpv4", {}, {}, |
51 | 0 | {"-mcpu=cortex-a7", "-mfloat-abi=hard", "-mfpu=neon-vfpv4"})); |
52 | |
|
53 | 0 | if (Multilibs.select(Flags, Result.SelectedMultilibs)) { |
54 | 0 | Result.Multilibs = Multilibs; |
55 | 0 | return true; |
56 | 0 | } |
57 | 0 | return false; |
58 | 0 | } |
59 | | |
60 | | static bool findOHOSMultilibs(const Driver &D, |
61 | | const ToolChain &TC, |
62 | | const llvm::Triple &TargetTriple, |
63 | | StringRef Path, const ArgList &Args, |
64 | 0 | DetectedMultilibs &Result) { |
65 | 0 | Multilib::flags_list Flags; |
66 | 0 | bool IsA7 = false; |
67 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) |
68 | 0 | IsA7 = A->getValue() == StringRef("cortex-a7"); |
69 | 0 | addMultilibFlag(IsA7, "-mcpu=cortex-a7", Flags); |
70 | |
|
71 | 0 | bool IsMFPU = false; |
72 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) |
73 | 0 | IsMFPU = A->getValue() == StringRef("neon-vfpv4"); |
74 | 0 | addMultilibFlag(IsMFPU, "-mfpu=neon-vfpv4", Flags); |
75 | |
|
76 | 0 | tools::arm::FloatABI ARMFloatABI = getARMFloatABI(D, TargetTriple, Args); |
77 | 0 | addMultilibFlag((ARMFloatABI == tools::arm::FloatABI::Soft), |
78 | 0 | "-mfloat-abi=soft", Flags); |
79 | 0 | addMultilibFlag((ARMFloatABI == tools::arm::FloatABI::SoftFP), |
80 | 0 | "-mfloat-abi=softfp", Flags); |
81 | 0 | addMultilibFlag((ARMFloatABI == tools::arm::FloatABI::Hard), |
82 | 0 | "-mfloat-abi=hard", Flags); |
83 | |
|
84 | 0 | return findOHOSMuslMultilibs(Flags, Result); |
85 | 0 | } |
86 | | |
87 | 0 | std::string OHOS::getMultiarchTriple(const llvm::Triple &T) const { |
88 | | // For most architectures, just use whatever we have rather than trying to be |
89 | | // clever. |
90 | 0 | switch (T.getArch()) { |
91 | 0 | default: |
92 | 0 | break; |
93 | | |
94 | | // We use the existence of '/lib/<triple>' as a directory to detect some |
95 | | // common linux triples that don't quite match the Clang triple for both |
96 | | // 32-bit and 64-bit targets. Multiarch fixes its install triples to these |
97 | | // regardless of what the actual target triple is. |
98 | 0 | case llvm::Triple::arm: |
99 | 0 | case llvm::Triple::thumb: |
100 | 0 | return T.isOSLiteOS() ? "arm-liteos-ohos" : "arm-linux-ohos"; |
101 | 0 | case llvm::Triple::riscv32: |
102 | 0 | return "riscv32-linux-ohos"; |
103 | 0 | case llvm::Triple::riscv64: |
104 | 0 | return "riscv64-linux-ohos"; |
105 | 0 | case llvm::Triple::mipsel: |
106 | 0 | return "mipsel-linux-ohos"; |
107 | 0 | case llvm::Triple::x86: |
108 | 0 | return "i686-linux-ohos"; |
109 | 0 | case llvm::Triple::x86_64: |
110 | 0 | return "x86_64-linux-ohos"; |
111 | 0 | case llvm::Triple::aarch64: |
112 | 0 | return "aarch64-linux-ohos"; |
113 | 0 | } |
114 | 0 | return T.str(); |
115 | 0 | } |
116 | | |
117 | | std::string OHOS::getMultiarchTriple(const Driver &D, |
118 | | const llvm::Triple &TargetTriple, |
119 | 0 | StringRef SysRoot) const { |
120 | 0 | return getMultiarchTriple(TargetTriple); |
121 | 0 | } |
122 | | |
123 | 0 | static std::string makePath(const std::initializer_list<std::string> &IL) { |
124 | 0 | SmallString<128> P; |
125 | 0 | for (const auto &S : IL) |
126 | 0 | llvm::sys::path::append(P, S); |
127 | 0 | return static_cast<std::string>(P.str()); |
128 | 0 | } |
129 | | |
130 | | /// OHOS Toolchain |
131 | | OHOS::OHOS(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) |
132 | 0 | : Generic_ELF(D, Triple, Args) { |
133 | 0 | std::string SysRoot = computeSysRoot(); |
134 | | |
135 | | // Select the correct multilib according to the given arguments. |
136 | 0 | DetectedMultilibs Result; |
137 | 0 | findOHOSMultilibs(D, *this, Triple, "", Args, Result); |
138 | 0 | Multilibs = Result.Multilibs; |
139 | 0 | SelectedMultilibs = Result.SelectedMultilibs; |
140 | 0 | if (!SelectedMultilibs.empty()) { |
141 | 0 | SelectedMultilib = SelectedMultilibs.back(); |
142 | 0 | } |
143 | |
|
144 | 0 | getFilePaths().clear(); |
145 | 0 | for (const auto &CandidateLibPath : getArchSpecificLibPaths()) |
146 | 0 | if (getVFS().exists(CandidateLibPath)) |
147 | 0 | getFilePaths().push_back(CandidateLibPath); |
148 | |
|
149 | 0 | getLibraryPaths().clear(); |
150 | 0 | for (auto &Path : getRuntimePaths()) |
151 | 0 | if (getVFS().exists(Path)) |
152 | 0 | getLibraryPaths().push_back(Path); |
153 | | |
154 | | // OHOS sysroots contain a library directory for each supported OS |
155 | | // version as well as some unversioned libraries in the usual multiarch |
156 | | // directory. Support --target=aarch64-linux-ohosX.Y.Z or |
157 | | // --target=aarch64-linux-ohosX.Y or --target=aarch64-linux-ohosX |
158 | 0 | path_list &Paths = getFilePaths(); |
159 | 0 | std::string SysRootLibPath = makePath({SysRoot, "usr", "lib"}); |
160 | 0 | std::string MultiarchTriple = getMultiarchTriple(getTriple()); |
161 | 0 | addPathIfExists(D, makePath({SysRootLibPath, SelectedMultilib.gccSuffix()}), |
162 | 0 | Paths); |
163 | 0 | addPathIfExists(D, |
164 | 0 | makePath({D.Dir, "..", "lib", MultiarchTriple, |
165 | 0 | SelectedMultilib.gccSuffix()}), |
166 | 0 | Paths); |
167 | |
|
168 | 0 | addPathIfExists( |
169 | 0 | D, |
170 | 0 | makePath({SysRootLibPath, MultiarchTriple, SelectedMultilib.gccSuffix()}), |
171 | 0 | Paths); |
172 | 0 | } |
173 | | |
174 | | ToolChain::RuntimeLibType OHOS::GetRuntimeLibType( |
175 | 0 | const ArgList &Args) const { |
176 | 0 | if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) { |
177 | 0 | StringRef Value = A->getValue(); |
178 | 0 | if (Value != "compiler-rt") |
179 | 0 | getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name) |
180 | 0 | << A->getAsString(Args); |
181 | 0 | } |
182 | |
|
183 | 0 | return ToolChain::RLT_CompilerRT; |
184 | 0 | } |
185 | | |
186 | | ToolChain::CXXStdlibType |
187 | 0 | OHOS::GetCXXStdlibType(const ArgList &Args) const { |
188 | 0 | if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { |
189 | 0 | StringRef Value = A->getValue(); |
190 | 0 | if (Value != "libc++") |
191 | 0 | getDriver().Diag(diag::err_drv_invalid_stdlib_name) |
192 | 0 | << A->getAsString(Args); |
193 | 0 | } |
194 | |
|
195 | 0 | return ToolChain::CST_Libcxx; |
196 | 0 | } |
197 | | |
198 | | void OHOS::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
199 | 0 | ArgStringList &CC1Args) const { |
200 | 0 | const Driver &D = getDriver(); |
201 | 0 | const llvm::Triple &Triple = getTriple(); |
202 | 0 | std::string SysRoot = computeSysRoot(); |
203 | |
|
204 | 0 | if (DriverArgs.hasArg(options::OPT_nostdinc)) |
205 | 0 | return; |
206 | | |
207 | 0 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
208 | 0 | SmallString<128> P(D.ResourceDir); |
209 | 0 | llvm::sys::path::append(P, "include"); |
210 | 0 | addSystemInclude(DriverArgs, CC1Args, P); |
211 | 0 | } |
212 | |
|
213 | 0 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
214 | 0 | return; |
215 | | |
216 | | // Check for configure-time C include directories. |
217 | 0 | StringRef CIncludeDirs(C_INCLUDE_DIRS); |
218 | 0 | if (CIncludeDirs != "") { |
219 | 0 | SmallVector<StringRef, 5> dirs; |
220 | 0 | CIncludeDirs.split(dirs, ":"); |
221 | 0 | for (StringRef dir : dirs) { |
222 | 0 | StringRef Prefix = |
223 | 0 | llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : ""; |
224 | 0 | addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); |
225 | 0 | } |
226 | 0 | return; |
227 | 0 | } |
228 | | |
229 | 0 | addExternCSystemInclude(DriverArgs, CC1Args, |
230 | 0 | SysRoot + "/usr/include/" + |
231 | 0 | getMultiarchTriple(Triple)); |
232 | 0 | addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); |
233 | 0 | addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); |
234 | 0 | } |
235 | | |
236 | | void OHOS::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, |
237 | 0 | ArgStringList &CC1Args) const { |
238 | 0 | if (DriverArgs.hasArg(options::OPT_nostdlibinc) || |
239 | 0 | DriverArgs.hasArg(options::OPT_nostdincxx)) |
240 | 0 | return; |
241 | | |
242 | 0 | switch (GetCXXStdlibType(DriverArgs)) { |
243 | 0 | case ToolChain::CST_Libcxx: { |
244 | 0 | std::string IncPath = makePath({getDriver().Dir, "..", "include"}); |
245 | 0 | std::string IncTargetPath = |
246 | 0 | makePath({IncPath, getMultiarchTriple(getTriple()), "c++", "v1"}); |
247 | 0 | if (getVFS().exists(IncTargetPath)) { |
248 | 0 | addSystemInclude(DriverArgs, CC1Args, makePath({IncPath, "c++", "v1"})); |
249 | 0 | addSystemInclude(DriverArgs, CC1Args, IncTargetPath); |
250 | 0 | } |
251 | 0 | break; |
252 | 0 | } |
253 | | |
254 | 0 | default: |
255 | 0 | llvm_unreachable("invalid stdlib name"); |
256 | 0 | } |
257 | 0 | } |
258 | | |
259 | | void OHOS::AddCXXStdlibLibArgs(const ArgList &Args, |
260 | 0 | ArgStringList &CmdArgs) const { |
261 | 0 | switch (GetCXXStdlibType(Args)) { |
262 | 0 | case ToolChain::CST_Libcxx: |
263 | 0 | CmdArgs.push_back("-lc++"); |
264 | 0 | CmdArgs.push_back("-lc++abi"); |
265 | 0 | CmdArgs.push_back("-lunwind"); |
266 | 0 | break; |
267 | | |
268 | 0 | case ToolChain::CST_Libstdcxx: |
269 | 0 | llvm_unreachable("invalid stdlib name"); |
270 | 0 | } |
271 | 0 | } |
272 | | |
273 | 0 | std::string OHOS::computeSysRoot() const { |
274 | 0 | std::string SysRoot = |
275 | 0 | !getDriver().SysRoot.empty() |
276 | 0 | ? getDriver().SysRoot |
277 | 0 | : makePath({getDriver().getInstalledDir(), "..", "..", "sysroot"}); |
278 | 0 | if (!llvm::sys::fs::exists(SysRoot)) |
279 | 0 | return std::string(); |
280 | | |
281 | 0 | std::string ArchRoot = makePath({SysRoot, getMultiarchTriple(getTriple())}); |
282 | 0 | return llvm::sys::fs::exists(ArchRoot) ? ArchRoot : SysRoot; |
283 | 0 | } |
284 | | |
285 | 0 | ToolChain::path_list OHOS::getRuntimePaths() const { |
286 | 0 | SmallString<128> P; |
287 | 0 | path_list Paths; |
288 | 0 | const Driver &D = getDriver(); |
289 | 0 | const llvm::Triple &Triple = getTriple(); |
290 | | |
291 | | // First try the triple passed to driver as --target=<triple>. |
292 | 0 | P.assign(D.ResourceDir); |
293 | 0 | llvm::sys::path::append(P, "lib", D.getTargetTriple(), SelectedMultilib.gccSuffix()); |
294 | 0 | Paths.push_back(P.c_str()); |
295 | | |
296 | | // Second try the normalized triple. |
297 | 0 | P.assign(D.ResourceDir); |
298 | 0 | llvm::sys::path::append(P, "lib", Triple.str(), SelectedMultilib.gccSuffix()); |
299 | 0 | Paths.push_back(P.c_str()); |
300 | | |
301 | | // Third try the effective triple. |
302 | 0 | P.assign(D.ResourceDir); |
303 | 0 | std::string SysRoot = computeSysRoot(); |
304 | 0 | llvm::sys::path::append(P, "lib", getMultiarchTriple(Triple), |
305 | 0 | SelectedMultilib.gccSuffix()); |
306 | 0 | Paths.push_back(P.c_str()); |
307 | |
|
308 | 0 | return Paths; |
309 | 0 | } |
310 | | |
311 | 0 | std::string OHOS::getDynamicLinker(const ArgList &Args) const { |
312 | 0 | const llvm::Triple &Triple = getTriple(); |
313 | 0 | const llvm::Triple::ArchType Arch = getArch(); |
314 | |
|
315 | 0 | assert(Triple.isMusl()); |
316 | 0 | std::string ArchName; |
317 | 0 | bool IsArm = false; |
318 | |
|
319 | 0 | switch (Arch) { |
320 | 0 | case llvm::Triple::arm: |
321 | 0 | case llvm::Triple::thumb: |
322 | 0 | ArchName = "arm"; |
323 | 0 | IsArm = true; |
324 | 0 | break; |
325 | 0 | case llvm::Triple::armeb: |
326 | 0 | case llvm::Triple::thumbeb: |
327 | 0 | ArchName = "armeb"; |
328 | 0 | IsArm = true; |
329 | 0 | break; |
330 | 0 | default: |
331 | 0 | ArchName = Triple.getArchName().str(); |
332 | 0 | } |
333 | 0 | if (IsArm && |
334 | 0 | (tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard)) |
335 | 0 | ArchName += "hf"; |
336 | |
|
337 | 0 | return "/lib/ld-musl-" + ArchName + ".so.1"; |
338 | 0 | } |
339 | | |
340 | | std::string OHOS::getCompilerRT(const ArgList &Args, StringRef Component, |
341 | 0 | FileType Type) const { |
342 | 0 | SmallString<128> Path(getDriver().ResourceDir); |
343 | 0 | llvm::sys::path::append(Path, "lib", getMultiarchTriple(getTriple()), |
344 | 0 | SelectedMultilib.gccSuffix()); |
345 | 0 | const char *Prefix = |
346 | 0 | Type == ToolChain::FT_Object ? "" : "lib"; |
347 | 0 | const char *Suffix; |
348 | 0 | switch (Type) { |
349 | 0 | case ToolChain::FT_Object: |
350 | 0 | Suffix = ".o"; |
351 | 0 | break; |
352 | 0 | case ToolChain::FT_Static: |
353 | 0 | Suffix = ".a"; |
354 | 0 | break; |
355 | 0 | case ToolChain::FT_Shared: |
356 | 0 | Suffix = ".so"; |
357 | 0 | break; |
358 | 0 | } |
359 | 0 | llvm::sys::path::append( |
360 | 0 | Path, Prefix + Twine("clang_rt.") + Component + Suffix); |
361 | 0 | return static_cast<std::string>(Path.str()); |
362 | 0 | } |
363 | | |
364 | 0 | void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { |
365 | 0 | CmdArgs.push_back("-z"); |
366 | 0 | CmdArgs.push_back("now"); |
367 | 0 | CmdArgs.push_back("-z"); |
368 | 0 | CmdArgs.push_back("relro"); |
369 | 0 | CmdArgs.push_back("-z"); |
370 | 0 | CmdArgs.push_back("max-page-size=4096"); |
371 | | // .gnu.hash section is not compatible with the MIPS target |
372 | 0 | if (getArch() != llvm::Triple::mipsel) |
373 | 0 | CmdArgs.push_back("--hash-style=both"); |
374 | | #ifdef ENABLE_LINKER_BUILD_ID |
375 | | CmdArgs.push_back("--build-id"); |
376 | | #endif |
377 | 0 | CmdArgs.push_back("--enable-new-dtags"); |
378 | 0 | } |
379 | | |
380 | 0 | SanitizerMask OHOS::getSupportedSanitizers() const { |
381 | 0 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
382 | 0 | Res |= SanitizerKind::Address; |
383 | 0 | Res |= SanitizerKind::PointerCompare; |
384 | 0 | Res |= SanitizerKind::PointerSubtract; |
385 | 0 | Res |= SanitizerKind::Fuzzer; |
386 | 0 | Res |= SanitizerKind::FuzzerNoLink; |
387 | 0 | Res |= SanitizerKind::Memory; |
388 | 0 | Res |= SanitizerKind::Vptr; |
389 | 0 | Res |= SanitizerKind::SafeStack; |
390 | 0 | Res |= SanitizerKind::Scudo; |
391 | | // TODO: kASAN for liteos ?? |
392 | | // TODO: Support TSAN and HWASAN and update mask. |
393 | 0 | return Res; |
394 | 0 | } |
395 | | |
396 | | // TODO: Make a base class for Linux and OHOS and move this there. |
397 | | void OHOS::addProfileRTLibs(const llvm::opt::ArgList &Args, |
398 | 0 | llvm::opt::ArgStringList &CmdArgs) const { |
399 | | // Add linker option -u__llvm_profile_runtime to cause runtime |
400 | | // initialization module to be linked in. |
401 | 0 | if (needsProfileRT(Args)) |
402 | 0 | CmdArgs.push_back(Args.MakeArgString( |
403 | 0 | Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); |
404 | 0 | ToolChain::addProfileRTLibs(Args, CmdArgs); |
405 | 0 | } |
406 | | |
407 | 0 | ToolChain::path_list OHOS::getArchSpecificLibPaths() const { |
408 | 0 | ToolChain::path_list Paths; |
409 | 0 | llvm::Triple Triple = getTriple(); |
410 | 0 | Paths.push_back( |
411 | 0 | makePath({getDriver().ResourceDir, "lib", getMultiarchTriple(Triple)})); |
412 | 0 | return Paths; |
413 | 0 | } |
414 | | |
415 | 0 | ToolChain::UnwindLibType OHOS::GetUnwindLibType(const llvm::opt::ArgList &Args) const { |
416 | 0 | if (Args.getLastArg(options::OPT_unwindlib_EQ)) |
417 | 0 | return Generic_ELF::GetUnwindLibType(Args); |
418 | 0 | return GetDefaultUnwindLibType(); |
419 | 0 | } |