| 1 | //===-- BareMetal.cpp - Bare Metal ToolChain --------------------*- 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 "BareMetal.h" |
| 10 | |
| 11 | #include "Gnu.h" |
| 12 | #include "clang/Driver/CommonArgs.h" |
| 13 | #include "clang/Driver/InputInfo.h" |
| 14 | |
| 15 | #include "Arch/AArch64.h" |
| 16 | #include "Arch/ARM.h" |
| 17 | #include "Arch/RISCV.h" |
| 18 | #include "clang/Driver/Compilation.h" |
| 19 | #include "clang/Driver/Driver.h" |
| 20 | #include "clang/Driver/MultilibBuilder.h" |
| 21 | #include "clang/Options/Options.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
| 23 | #include "llvm/Option/ArgList.h" |
| 24 | #include "llvm/Support/Path.h" |
| 25 | #include "llvm/Support/VirtualFileSystem.h" |
| 26 | |
| 27 | using namespace llvm::opt; |
| 28 | using namespace clang; |
| 29 | using namespace clang::driver; |
| 30 | using namespace clang::driver::tools; |
| 31 | using namespace clang::driver::toolchains; |
| 32 | |
| 33 | static bool isRISCVBareMetal(const llvm::Triple &Triple) { |
| 34 | if (!Triple.isRISCV()) |
| 35 | return false; |
| 36 | |
| 37 | if (Triple.getVendor() != llvm::Triple::UnknownVendor) |
| 38 | return false; |
| 39 | |
| 40 | if (Triple.getOS() != llvm::Triple::UnknownOS) |
| 41 | return false; |
| 42 | |
| 43 | return Triple.getEnvironmentName() == "elf" ; |
| 44 | } |
| 45 | |
| 46 | /// Is the triple powerpc[64][le]-*-none-eabi? |
| 47 | static bool isPPCBareMetal(const llvm::Triple &Triple) { |
| 48 | return Triple.isPPC() && Triple.getOS() == llvm::Triple::UnknownOS && |
| 49 | Triple.getEnvironment() == llvm::Triple::EABI; |
| 50 | } |
| 51 | |
| 52 | /// Is the triple {ix86,x86_64}-*-none-elf? |
| 53 | static bool isX86BareMetal(const llvm::Triple &Triple) { |
| 54 | return Triple.isX86() && Triple.getOS() == llvm::Triple::UnknownOS && |
| 55 | Triple.getEnvironmentName() == "elf" ; |
| 56 | } |
| 57 | |
| 58 | static bool findRISCVMultilibs(const Driver &D, |
| 59 | const llvm::Triple &TargetTriple, |
| 60 | const ArgList &Args, DetectedMultilibs &Result) { |
| 61 | Multilib::flags_list Flags; |
| 62 | std::string Arch = riscv::getRISCVArch(Args, Triple: TargetTriple); |
| 63 | StringRef Abi = tools::riscv::getRISCVABI(Args, Triple: TargetTriple); |
| 64 | |
| 65 | if (TargetTriple.isRISCV64()) { |
| 66 | MultilibBuilder Imac = |
| 67 | MultilibBuilder().flag(Flag: "-march=rv64imac" ).flag(Flag: "-mabi=lp64" ); |
| 68 | MultilibBuilder Imafdc = MultilibBuilder("/rv64imafdc/lp64d" ) |
| 69 | .flag(Flag: "-march=rv64imafdc" ) |
| 70 | .flag(Flag: "-mabi=lp64d" ); |
| 71 | |
| 72 | // Multilib reuse |
| 73 | bool UseImafdc = |
| 74 | (Arch == "rv64imafdc" ) || (Arch == "rv64gc" ); // gc => imafdc |
| 75 | |
| 76 | addMultilibFlag(Enabled: (Arch == "rv64imac" ), Flag: "-march=rv64imac" , Flags); |
| 77 | addMultilibFlag(Enabled: UseImafdc, Flag: "-march=rv64imafdc" , Flags); |
| 78 | addMultilibFlag(Enabled: Abi == "lp64" , Flag: "-mabi=lp64" , Flags); |
| 79 | addMultilibFlag(Enabled: Abi == "lp64d" , Flag: "-mabi=lp64d" , Flags); |
| 80 | |
| 81 | Result.Multilibs = |
| 82 | MultilibSetBuilder().Either(M1: Imac, M2: Imafdc).makeMultilibSet(); |
| 83 | return Result.Multilibs.select(D, Flags, Result.SelectedMultilibs); |
| 84 | } |
| 85 | if (TargetTriple.isRISCV32()) { |
| 86 | MultilibBuilder Imac = |
| 87 | MultilibBuilder().flag(Flag: "-march=rv32imac" ).flag(Flag: "-mabi=ilp32" ); |
| 88 | MultilibBuilder I = MultilibBuilder("/rv32i/ilp32" ) |
| 89 | .flag(Flag: "-march=rv32i" ) |
| 90 | .flag(Flag: "-mabi=ilp32" ); |
| 91 | MultilibBuilder Im = MultilibBuilder("/rv32im/ilp32" ) |
| 92 | .flag(Flag: "-march=rv32im" ) |
| 93 | .flag(Flag: "-mabi=ilp32" ); |
| 94 | MultilibBuilder Iac = MultilibBuilder("/rv32iac/ilp32" ) |
| 95 | .flag(Flag: "-march=rv32iac" ) |
| 96 | .flag(Flag: "-mabi=ilp32" ); |
| 97 | MultilibBuilder Imafc = MultilibBuilder("/rv32imafc/ilp32f" ) |
| 98 | .flag(Flag: "-march=rv32imafc" ) |
| 99 | .flag(Flag: "-mabi=ilp32f" ); |
| 100 | |
| 101 | // Multilib reuse |
| 102 | bool UseI = (Arch == "rv32i" ) || (Arch == "rv32ic" ); // ic => i |
| 103 | bool UseIm = (Arch == "rv32im" ) || (Arch == "rv32imc" ); // imc => im |
| 104 | bool UseImafc = (Arch == "rv32imafc" ) || (Arch == "rv32imafdc" ) || |
| 105 | (Arch == "rv32gc" ); // imafdc,gc => imafc |
| 106 | |
| 107 | addMultilibFlag(Enabled: UseI, Flag: "-march=rv32i" , Flags); |
| 108 | addMultilibFlag(Enabled: UseIm, Flag: "-march=rv32im" , Flags); |
| 109 | addMultilibFlag(Enabled: (Arch == "rv32iac" ), Flag: "-march=rv32iac" , Flags); |
| 110 | addMultilibFlag(Enabled: (Arch == "rv32imac" ), Flag: "-march=rv32imac" , Flags); |
| 111 | addMultilibFlag(Enabled: UseImafc, Flag: "-march=rv32imafc" , Flags); |
| 112 | addMultilibFlag(Enabled: Abi == "ilp32" , Flag: "-mabi=ilp32" , Flags); |
| 113 | addMultilibFlag(Enabled: Abi == "ilp32f" , Flag: "-mabi=ilp32f" , Flags); |
| 114 | |
| 115 | Result.Multilibs = |
| 116 | MultilibSetBuilder().Either(M1: I, M2: Im, M3: Iac, M4: Imac, M5: Imafc).makeMultilibSet(); |
| 117 | return Result.Multilibs.select(D, Flags, Result.SelectedMultilibs); |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | static std::string computeClangRuntimesSysRoot(const Driver &D, |
| 123 | bool IncludeTriple) { |
| 124 | if (!D.SysRoot.empty()) |
| 125 | return D.SysRoot; |
| 126 | |
| 127 | SmallString<128> SysRootDir(D.Dir); |
| 128 | llvm::sys::path::append(path&: SysRootDir, a: ".." , b: "lib" , c: "clang-runtimes" ); |
| 129 | |
| 130 | if (IncludeTriple) |
| 131 | llvm::sys::path::append(path&: SysRootDir, a: D.getTargetTriple()); |
| 132 | |
| 133 | return std::string(SysRootDir); |
| 134 | } |
| 135 | |
| 136 | // Only consider the GCC toolchain based on the values provided through the |
| 137 | // `--gcc-toolchain` and `--gcc-install-dir` flags. The function below returns |
| 138 | // whether the GCC toolchain was initialized successfully. |
| 139 | bool BareMetal::initGCCInstallation(const llvm::Triple &Triple, |
| 140 | const llvm::opt::ArgList &Args) { |
| 141 | if (Args.getLastArg(Ids: options::OPT_gcc_toolchain) || |
| 142 | Args.getLastArg(Ids: clang::options::OPT_gcc_install_dir_EQ)) { |
| 143 | GCCInstallation.init(TargetTriple: Triple, Args); |
| 144 | return GCCInstallation.isValid(); |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | // This logic is adapted from RISCVToolChain.cpp as part of the ongoing effort |
| 150 | // to merge RISCVToolChain into the Baremetal toolchain. It infers the presence |
| 151 | // of a valid GCC toolchain by checking whether the `crt0.o` file exists in the |
| 152 | // `bin/../<target-triple>/lib` directory. |
| 153 | static bool detectGCCToolchainAdjacent(const Driver &D) { |
| 154 | SmallString<128> GCCDir; |
| 155 | llvm::sys::path::append(path&: GCCDir, a: D.Dir, b: ".." , c: D.getTargetTriple(), |
| 156 | d: "lib/crt0.o" ); |
| 157 | return llvm::sys::fs::exists(Path: GCCDir); |
| 158 | } |
| 159 | |
| 160 | // If no sysroot is provided the driver will first attempt to infer it from the |
| 161 | // values of `--gcc-install-dir` or `--gcc-toolchain`, which specify the |
| 162 | // location of a GCC toolchain. |
| 163 | // If neither flag is used, the sysroot defaults to either: |
| 164 | // Â Â - `bin/../<target-triple>` |
| 165 | // Â Â - `bin/../lib/clang-runtimes/<target-triple>` |
| 166 | // |
| 167 | // To use the `clang-runtimes` path, ensure that `../<target-triple>/lib/crt0.o` |
| 168 | // does not exist relative to the driver. |
| 169 | std::string BareMetal::computeSysRoot() const { |
| 170 | // Use Baremetal::sysroot if it has already been set. |
| 171 | if (!SysRoot.empty()) |
| 172 | return SysRoot; |
| 173 | |
| 174 | // Use the sysroot specified via the `--sysroot` command-line flag, if |
| 175 | // provided. |
| 176 | const Driver &D = getDriver(); |
| 177 | if (!D.SysRoot.empty()) |
| 178 | return D.SysRoot; |
| 179 | |
| 180 | // Attempt to infer sysroot from a valid GCC installation. |
| 181 | // If no valid GCC installation, check for a GCC toolchain alongside Clang. |
| 182 | SmallString<128> inferredSysRoot; |
| 183 | if (IsGCCInstallationValid) { |
| 184 | llvm::sys::path::append(path&: inferredSysRoot, a: GCCInstallation.getParentLibPath(), |
| 185 | b: ".." , c: GCCInstallation.getTriple().str()); |
| 186 | } else if (detectGCCToolchainAdjacent(D)) { |
| 187 | // Use the triple as provided to the driver. Unlike the parsed triple |
| 188 | // this has not been normalized to always contain every field. |
| 189 | llvm::sys::path::append(path&: inferredSysRoot, a: D.Dir, b: ".." , c: D.getTargetTriple()); |
| 190 | } |
| 191 | // If a valid sysroot was inferred and exists, use it |
| 192 | if (!inferredSysRoot.empty() && llvm::sys::fs::exists(Path: inferredSysRoot)) |
| 193 | return std::string(inferredSysRoot); |
| 194 | |
| 195 | // Use the clang-runtimes path. |
| 196 | return computeClangRuntimesSysRoot(D, /*IncludeTriple*/ true); |
| 197 | } |
| 198 | |
| 199 | std::string BareMetal::getCompilerRTPath() const { |
| 200 | const Driver &D = getDriver(); |
| 201 | if (IsGCCInstallationValid || detectGCCToolchainAdjacent(D: getDriver())) { |
| 202 | SmallString<128> Path(D.ResourceDir); |
| 203 | llvm::sys::path::append(path&: Path, a: "lib" ); |
| 204 | return std::string(Path.str()); |
| 205 | } |
| 206 | return ToolChain::getCompilerRTPath(); |
| 207 | } |
| 208 | |
| 209 | static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs, |
| 210 | const Multilib &Multilib, |
| 211 | StringRef InstallPath, |
| 212 | ToolChain::path_list &Paths) { |
| 213 | if (const auto &PathsCallback = Multilibs.filePathsCallback()) |
| 214 | for (const auto &Path : PathsCallback(Multilib)) |
| 215 | addPathIfExists(D, Path: InstallPath + Path, Paths); |
| 216 | } |
| 217 | |
| 218 | // GCC mutltilibs will only work for those targets that have their multlib |
| 219 | // structure encoded into GCCInstallation. Baremetal toolchain supports ARM, |
| 220 | // AArch64, RISCV and PPC and of these only RISCV have GCC multilibs hardcoded |
| 221 | // in GCCInstallation. |
| 222 | BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple, |
| 223 | const ArgList &Args) |
| 224 | : Generic_ELF(D, Triple, Args) { |
| 225 | IsGCCInstallationValid = initGCCInstallation(Triple, Args); |
| 226 | std::string ComputedSysRoot = computeSysRoot(); |
| 227 | if (IsGCCInstallationValid) { |
| 228 | if (!isRISCVBareMetal(Triple)) |
| 229 | D.Diag(DiagID: clang::diag::warn_drv_multilib_not_available_for_target); |
| 230 | |
| 231 | Multilibs = GCCInstallation.getMultilibs(); |
| 232 | SelectedMultilibs.assign(IL: {GCCInstallation.getMultilib()}); |
| 233 | |
| 234 | path_list &Paths = getFilePaths(); |
| 235 | // Add toolchain/multilib specific file paths. |
| 236 | addMultilibsFilePaths(D, Multilibs, Multilib: SelectedMultilibs.back(), |
| 237 | InstallPath: GCCInstallation.getInstallPath(), Paths); |
| 238 | // Adding filepath for locating crt{begin,end}.o files. |
| 239 | Paths.push_back(Elt: GCCInstallation.getInstallPath().str()); |
| 240 | // Adding filepath for locating crt0.o file. |
| 241 | Paths.push_back(Elt: ComputedSysRoot + "/lib" ); |
| 242 | |
| 243 | ToolChain::path_list &PPaths = getProgramPaths(); |
| 244 | // Multilib cross-compiler GCC installations put ld in a triple-prefixed |
| 245 | // directory off of the parent of the GCC installation. |
| 246 | PPaths.push_back(Elt: Twine(GCCInstallation.getParentLibPath() + "/../" + |
| 247 | GCCInstallation.getTriple().str() + "/bin" ) |
| 248 | .str()); |
| 249 | PPaths.push_back(Elt: (GCCInstallation.getParentLibPath() + "/../bin" ).str()); |
| 250 | } else { |
| 251 | getProgramPaths().push_back(Elt: getDriver().Dir); |
| 252 | findMultilibs(D, Triple, Args); |
| 253 | const SmallString<128> SysRootDir(computeSysRoot()); |
| 254 | if (!SysRootDir.empty()) { |
| 255 | for (const Multilib &M : getOrderedMultilibs()) { |
| 256 | SmallString<128> Dir(SysRootDir); |
| 257 | llvm::sys::path::append(path&: Dir, a: M.osSuffix(), b: "lib" ); |
| 258 | getFilePaths().push_back(Elt: std::string(Dir)); |
| 259 | getLibraryPaths().push_back(Elt: std::string(Dir)); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void BareMetal::findMultilibs(const Driver &D, const llvm::Triple &Triple, |
| 266 | const ArgList &Args) { |
| 267 | // Look for a multilib.yaml before trying target-specific hardwired logic. |
| 268 | std::string FallbackDir = |
| 269 | computeClangRuntimesSysRoot(D, /*IncludeTriple=*/false); |
| 270 | if (loadMultilibsFromYAML(Args, D, Fallback: FallbackDir)) { |
| 271 | SysRoot = FallbackDir; |
| 272 | } else if (isRISCVBareMetal(Triple) && !detectGCCToolchainAdjacent(D)) { |
| 273 | DetectedMultilibs Result; |
| 274 | if (findRISCVMultilibs(D, TargetTriple: Triple, Args, Result)) { |
| 275 | SelectedMultilibs = Result.SelectedMultilibs; |
| 276 | Multilibs = Result.Multilibs; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | bool BareMetal::handlesTarget(const llvm::Triple &Triple) { |
| 282 | return arm::isARMEABIBareMetal(Triple) || |
| 283 | aarch64::isAArch64BareMetal(Triple) || isRISCVBareMetal(Triple) || |
| 284 | isPPCBareMetal(Triple) || isX86BareMetal(Triple); |
| 285 | } |
| 286 | |
| 287 | Tool *BareMetal::buildLinker() const { |
| 288 | return new tools::baremetal::Linker(*this); |
| 289 | } |
| 290 | |
| 291 | Tool *BareMetal::buildStaticLibTool() const { |
| 292 | return new tools::baremetal::StaticLibTool(*this); |
| 293 | } |
| 294 | |
| 295 | ToolChain::CXXStdlibType BareMetal::GetDefaultCXXStdlibType() const { |
| 296 | if (getTriple().isRISCV() && IsGCCInstallationValid) |
| 297 | return ToolChain::CST_Libstdcxx; |
| 298 | return ToolChain::CST_Libcxx; |
| 299 | } |
| 300 | |
| 301 | ToolChain::RuntimeLibType BareMetal::GetDefaultRuntimeLibType() const { |
| 302 | if (getTriple().isRISCV() && IsGCCInstallationValid) |
| 303 | return ToolChain::RLT_Libgcc; |
| 304 | return ToolChain::RLT_CompilerRT; |
| 305 | } |
| 306 | |
| 307 | // TODO: Add a validity check for GCCInstallation. |
| 308 | // If valid, use `UNW_Libgcc`; otherwise, use `UNW_None`. |
| 309 | ToolChain::UnwindLibType |
| 310 | BareMetal::GetUnwindLibType(const llvm::opt::ArgList &Args) const { |
| 311 | if (getTriple().isRISCV()) |
| 312 | return ToolChain::UNW_None; |
| 313 | |
| 314 | return ToolChain::GetUnwindLibType(Args); |
| 315 | } |
| 316 | |
| 317 | void BareMetal::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 318 | ArgStringList &CC1Args) const { |
| 319 | if (DriverArgs.hasArg(Ids: options::OPT_nostdinc)) |
| 320 | return; |
| 321 | |
| 322 | if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) { |
| 323 | SmallString<128> Dir(getDriver().ResourceDir); |
| 324 | llvm::sys::path::append(path&: Dir, a: "include" ); |
| 325 | addSystemInclude(DriverArgs, CC1Args, Path: Dir.str()); |
| 326 | } |
| 327 | |
| 328 | if (DriverArgs.hasArg(Ids: options::OPT_nostdlibinc)) |
| 329 | return; |
| 330 | |
| 331 | const Driver &D = getDriver(); |
| 332 | |
| 333 | if (std::optional<std::string> Path = getStdlibIncludePath()) |
| 334 | addSystemInclude(DriverArgs, CC1Args, Path: *Path); |
| 335 | |
| 336 | const SmallString<128> SysRootDir(computeSysRoot()); |
| 337 | if (!SysRootDir.empty()) { |
| 338 | for (const Multilib &M : getOrderedMultilibs()) { |
| 339 | SmallString<128> Dir(SysRootDir); |
| 340 | llvm::sys::path::append(path&: Dir, a: M.includeSuffix()); |
| 341 | llvm::sys::path::append(path&: Dir, a: "include" ); |
| 342 | addSystemInclude(DriverArgs, CC1Args, Path: Dir.str()); |
| 343 | } |
| 344 | SmallString<128> Dir(SysRootDir); |
| 345 | llvm::sys::path::append(path&: Dir, a: getTripleString()); |
| 346 | if (D.getVFS().exists(Path: Dir)) { |
| 347 | llvm::sys::path::append(path&: Dir, a: "include" ); |
| 348 | addSystemInclude(DriverArgs, CC1Args, Path: Dir.str()); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void BareMetal::addClangTargetOptions(const ArgList &DriverArgs, |
| 354 | ArgStringList &CC1Args, BoundArch BA, |
| 355 | Action::OffloadKind) const { |
| 356 | CC1Args.push_back(Elt: "-nostdsysteminc" ); |
| 357 | } |
| 358 | |
| 359 | void BareMetal::addLibStdCxxIncludePaths( |
| 360 | const llvm::opt::ArgList &DriverArgs, |
| 361 | llvm::opt::ArgStringList &CC1Args) const { |
| 362 | if (!IsGCCInstallationValid) |
| 363 | return; |
| 364 | const GCCVersion &Version = GCCInstallation.getVersion(); |
| 365 | StringRef TripleStr = GCCInstallation.getTriple().str(); |
| 366 | const Multilib &Multilib = GCCInstallation.getMultilib(); |
| 367 | addLibStdCXXIncludePaths(IncludeDir: computeSysRoot() + "/include/c++/" + Version.Text, |
| 368 | Triple: TripleStr, IncludeSuffix: Multilib.includeSuffix(), DriverArgs, |
| 369 | CC1Args); |
| 370 | } |
| 371 | |
| 372 | void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, |
| 373 | ArgStringList &CC1Args) const { |
| 374 | if (DriverArgs.hasArg(Ids: options::OPT_nostdinc, Ids: options::OPT_nostdlibinc, |
| 375 | Ids: options::OPT_nostdincxx)) |
| 376 | return; |
| 377 | |
| 378 | const Driver &D = getDriver(); |
| 379 | StringRef Target = getTripleString(); |
| 380 | |
| 381 | auto AddCXXIncludePath = [&](StringRef Path) { |
| 382 | std::string Version = detectLibcxxVersion(IncludePath: Path); |
| 383 | if (Version.empty()) |
| 384 | return; |
| 385 | |
| 386 | { |
| 387 | // First the per-target include dir: include/<target>/c++/v1. |
| 388 | SmallString<128> TargetDir(Path); |
| 389 | llvm::sys::path::append(path&: TargetDir, a: Target, b: "c++" , c: Version); |
| 390 | addSystemInclude(DriverArgs, CC1Args, Path: TargetDir); |
| 391 | } |
| 392 | |
| 393 | { |
| 394 | // Then the generic dir: include/c++/v1. |
| 395 | SmallString<128> Dir(Path); |
| 396 | llvm::sys::path::append(path&: Dir, a: "c++" , b: Version); |
| 397 | addSystemInclude(DriverArgs, CC1Args, Path: Dir); |
| 398 | } |
| 399 | }; |
| 400 | |
| 401 | switch (GetCXXStdlibType(Args: DriverArgs)) { |
| 402 | case ToolChain::CST_Libcxx: { |
| 403 | SmallString<128> P(D.Dir); |
| 404 | llvm::sys::path::append(path&: P, a: ".." , b: "include" ); |
| 405 | AddCXXIncludePath(P); |
| 406 | break; |
| 407 | } |
| 408 | case ToolChain::CST_Libstdcxx: |
| 409 | addLibStdCxxIncludePaths(DriverArgs, CC1Args); |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | std::string SysRootDir(computeSysRoot()); |
| 414 | if (SysRootDir.empty()) |
| 415 | return; |
| 416 | |
| 417 | for (const Multilib &M : getOrderedMultilibs()) { |
| 418 | SmallString<128> Dir(SysRootDir); |
| 419 | llvm::sys::path::append(path&: Dir, a: M.gccSuffix()); |
| 420 | switch (GetCXXStdlibType(Args: DriverArgs)) { |
| 421 | case ToolChain::CST_Libcxx: { |
| 422 | // First check sysroot/usr/include/c++/v1 if it exists. |
| 423 | SmallString<128> TargetDir(Dir); |
| 424 | llvm::sys::path::append(path&: TargetDir, a: "usr" , b: "include" , c: "c++" , d: "v1" ); |
| 425 | if (D.getVFS().exists(Path: TargetDir)) { |
| 426 | addSystemInclude(DriverArgs, CC1Args, Path: TargetDir.str()); |
| 427 | break; |
| 428 | } |
| 429 | // Add generic paths if nothing else succeeded so far. |
| 430 | llvm::sys::path::append(path&: Dir, a: "include" , b: "c++" , c: "v1" ); |
| 431 | addSystemInclude(DriverArgs, CC1Args, Path: Dir.str()); |
| 432 | break; |
| 433 | } |
| 434 | case ToolChain::CST_Libstdcxx: { |
| 435 | llvm::sys::path::append(path&: Dir, a: "include" , b: "c++" ); |
| 436 | std::error_code EC; |
| 437 | Generic_GCC::GCCVersion Version = {.Text: "" , .Major: -1, .Minor: -1, .Patch: -1, .MajorStr: "" , .MinorStr: "" , .PatchSuffix: "" }; |
| 438 | // Walk the subdirs, and find the one with the newest gcc version: |
| 439 | for (llvm::vfs::directory_iterator |
| 440 | LI = D.getVFS().dir_begin(Dir: Dir.str(), EC), |
| 441 | LE; |
| 442 | !EC && LI != LE; LI = LI.increment(EC)) { |
| 443 | StringRef VersionText = llvm::sys::path::filename(path: LI->path()); |
| 444 | auto CandidateVersion = Generic_GCC::GCCVersion::Parse(VersionText); |
| 445 | if (CandidateVersion.Major == -1) |
| 446 | continue; |
| 447 | if (CandidateVersion <= Version) |
| 448 | continue; |
| 449 | Version = CandidateVersion; |
| 450 | } |
| 451 | if (Version.Major != -1) { |
| 452 | llvm::sys::path::append(path&: Dir, a: Version.Text); |
| 453 | addSystemInclude(DriverArgs, CC1Args, Path: Dir.str()); |
| 454 | } |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | switch (GetCXXStdlibType(Args: DriverArgs)) { |
| 460 | case ToolChain::CST_Libcxx: { |
| 461 | SmallString<128> Dir(SysRootDir); |
| 462 | llvm::sys::path::append(path&: Dir, a: Target, b: "include" , c: "c++" , d: "v1" ); |
| 463 | if (D.getVFS().exists(Path: Dir)) |
| 464 | addSystemInclude(DriverArgs, CC1Args, Path: Dir.str()); |
| 465 | break; |
| 466 | } |
| 467 | case ToolChain::CST_Libstdcxx: |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void baremetal::StaticLibTool::ConstructJob(Compilation &C, const JobAction &JA, |
| 473 | const InputInfo &Output, |
| 474 | const InputInfoList &Inputs, |
| 475 | const ArgList &Args, |
| 476 | const char *LinkingOutput) const { |
| 477 | const Driver &D = getToolChain().getDriver(); |
| 478 | |
| 479 | // Silence warning for "clang -g foo.o -o foo" |
| 480 | Args.ClaimAllArgs(Id0: options::OPT_g_Group); |
| 481 | // and "clang -emit-llvm foo.o -o foo" |
| 482 | Args.ClaimAllArgs(Id0: options::OPT_emit_llvm); |
| 483 | // and for "clang -w foo.o -o foo". Other warning options are already |
| 484 | // handled somewhere else. |
| 485 | Args.ClaimAllArgs(Id0: options::OPT_w); |
| 486 | // Silence warnings when linking C code with a C++ '-stdlib' argument. |
| 487 | Args.ClaimAllArgs(Id0: options::OPT_stdlib_EQ); |
| 488 | |
| 489 | // ar tool command "llvm-ar <options> <output_file> <input_files>". |
| 490 | ArgStringList CmdArgs; |
| 491 | // Create and insert file members with a deterministic index. |
| 492 | CmdArgs.push_back(Elt: "rcsD" ); |
| 493 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 494 | |
| 495 | for (const auto &II : Inputs) { |
| 496 | if (II.isFilename()) { |
| 497 | CmdArgs.push_back(Elt: II.getFilename()); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // Delete old output archive file if it already exists before generating a new |
| 502 | // archive file. |
| 503 | const char *OutputFileName = Output.getFilename(); |
| 504 | if (Output.isFilename() && llvm::sys::fs::exists(Path: OutputFileName)) { |
| 505 | if (std::error_code EC = llvm::sys::fs::remove(path: OutputFileName)) { |
| 506 | D.Diag(DiagID: diag::err_drv_unable_to_remove_file) << EC.message(); |
| 507 | return; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | const char *Exec = Args.MakeArgString(Str: getToolChain().GetStaticLibToolPath()); |
| 512 | C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this, |
| 513 | args: ResponseFileSupport::AtFileCurCP(), |
| 514 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 515 | } |
| 516 | |
| 517 | void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 518 | const InputInfo &Output, |
| 519 | const InputInfoList &Inputs, |
| 520 | const ArgList &Args, |
| 521 | const char *LinkingOutput) const { |
| 522 | ArgStringList CmdArgs; |
| 523 | |
| 524 | auto &TC = static_cast<const toolchains::BareMetal &>(getToolChain()); |
| 525 | const Driver &D = getToolChain().getDriver(); |
| 526 | const llvm::Triple::ArchType Arch = TC.getArch(); |
| 527 | const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); |
| 528 | const bool IsStaticPIE = getStaticPIE(Args, TC); |
| 529 | |
| 530 | if (!D.SysRoot.empty()) |
| 531 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--sysroot=" + D.SysRoot)); |
| 532 | |
| 533 | CmdArgs.push_back(Elt: "-Bstatic" ); |
| 534 | if (IsStaticPIE) { |
| 535 | CmdArgs.push_back(Elt: "-pie" ); |
| 536 | CmdArgs.push_back(Elt: "--no-dynamic-linker" ); |
| 537 | CmdArgs.push_back(Elt: "-z" ); |
| 538 | CmdArgs.push_back(Elt: "text" ); |
| 539 | } |
| 540 | |
| 541 | if (const char *LDMOption = getLDMOption(T: TC.getTriple(), Args)) { |
| 542 | CmdArgs.push_back(Elt: "-m" ); |
| 543 | CmdArgs.push_back(Elt: LDMOption); |
| 544 | } else { |
| 545 | D.Diag(DiagID: diag::err_target_unknown_triple) << Triple.str(); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | if (Triple.isRISCV()) { |
| 550 | CmdArgs.push_back(Elt: "-X" ); |
| 551 | if (Args.hasArg(Ids: options::OPT_mno_relax)) |
| 552 | CmdArgs.push_back(Elt: "--no-relax" ); |
| 553 | } |
| 554 | |
| 555 | if (Triple.isARM() || Triple.isThumb()) { |
| 556 | bool IsBigEndian = arm::isARMBigEndian(Triple, Args); |
| 557 | if (IsBigEndian) |
| 558 | arm::appendBE8LinkFlag(Args, CmdArgs, Triple); |
| 559 | CmdArgs.push_back(Elt: IsBigEndian ? "-EB" : "-EL" ); |
| 560 | } else if (Triple.isAArch64()) { |
| 561 | CmdArgs.push_back(Elt: Arch == llvm::Triple::aarch64_be ? "-EB" : "-EL" ); |
| 562 | } |
| 563 | |
| 564 | bool NeedCRTs = |
| 565 | !Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nostartfiles); |
| 566 | |
| 567 | const char *CRTBegin, *CRTEnd; |
| 568 | if (NeedCRTs) { |
| 569 | if (!Args.hasArg(Ids: options::OPT_r)) { |
| 570 | const char *crt = "crt0.o" ; |
| 571 | if (IsStaticPIE) |
| 572 | crt = "rcrt1.o" ; |
| 573 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: TC.GetFilePath(Name: crt))); |
| 574 | } |
| 575 | if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) { |
| 576 | auto RuntimeLib = TC.GetRuntimeLibType(Args); |
| 577 | switch (RuntimeLib) { |
| 578 | case (ToolChain::RLT_Libgcc): { |
| 579 | CRTBegin = IsStaticPIE ? "crtbeginS.o" : "crtbegin.o" ; |
| 580 | CRTEnd = IsStaticPIE ? "crtendS.o" : "crtend.o" ; |
| 581 | break; |
| 582 | } |
| 583 | case (ToolChain::RLT_CompilerRT): { |
| 584 | CRTBegin = |
| 585 | TC.getCompilerRTArgString(Args, Component: "crtbegin" , Type: ToolChain::FT_Object); |
| 586 | CRTEnd = |
| 587 | TC.getCompilerRTArgString(Args, Component: "crtend" , Type: ToolChain::FT_Object); |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: TC.GetFilePath(Name: CRTBegin))); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | Args.addAllArgs(Output&: CmdArgs, Ids: {options::OPT_L}); |
| 596 | TC.AddFilePathLibArgs(Args, CmdArgs); |
| 597 | Args.addAllArgs(Output&: CmdArgs, Ids: {options::OPT_u, options::OPT_T_Group, |
| 598 | options::OPT_s, options::OPT_t, options::OPT_r}); |
| 599 | |
| 600 | for (const auto &LibPath : TC.getLibraryPaths()) |
| 601 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: llvm::Twine("-L" , LibPath))); |
| 602 | |
| 603 | if (auto LTO = TC.getLTOMode(Args); LTO != LTOK_None) |
| 604 | addLTOOptions(ToolChain: TC, Args, CmdArgs, Output, Inputs, IsThinLTO: LTO == LTOK_Thin); |
| 605 | |
| 606 | AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA); |
| 607 | TC.addProfileRTLibs(Args, CmdArgs); |
| 608 | |
| 609 | if (TC.ShouldLinkCXXStdlib(Args)) { |
| 610 | bool OnlyLibstdcxxStatic = Args.hasArg(Ids: options::OPT_static_libstdcxx) && |
| 611 | !Args.hasArg(Ids: options::OPT_static); |
| 612 | if (OnlyLibstdcxxStatic) |
| 613 | CmdArgs.push_back(Elt: "-Bstatic" ); |
| 614 | TC.AddCXXStdlibLibArgs(Args, CmdArgs); |
| 615 | if (OnlyLibstdcxxStatic) |
| 616 | CmdArgs.push_back(Elt: "-Bdynamic" ); |
| 617 | CmdArgs.push_back(Elt: "-lm" ); |
| 618 | } |
| 619 | |
| 620 | if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) { |
| 621 | CmdArgs.push_back(Elt: "--start-group" ); |
| 622 | AddRunTimeLibs(TC, D, CmdArgs, Args); |
| 623 | if (!Args.hasArg(Ids: options::OPT_nolibc)) |
| 624 | CmdArgs.push_back(Elt: "-lc" ); |
| 625 | if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) |
| 626 | CmdArgs.push_back(Elt: "-lgloss" ); |
| 627 | CmdArgs.push_back(Elt: "--end-group" ); |
| 628 | } |
| 629 | |
| 630 | if ((TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) && |
| 631 | NeedCRTs) |
| 632 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: TC.GetFilePath(Name: CRTEnd))); |
| 633 | |
| 634 | // The R_ARM_TARGET2 relocation must be treated as R_ARM_REL32 on arm*-*-elf |
| 635 | // and arm*-*-eabi (the default is R_ARM_GOT_PREL, used on arm*-*-linux and |
| 636 | // arm*-*-*bsd). |
| 637 | if (arm::isARMEABIBareMetal(Triple: TC.getTriple())) |
| 638 | CmdArgs.push_back(Elt: "--target2=rel" ); |
| 639 | |
| 640 | CmdArgs.push_back(Elt: "-o" ); |
| 641 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 642 | |
| 643 | C.addCommand(Cmd: std::make_unique<Command>( |
| 644 | args: JA, args: *this, args: ResponseFileSupport::AtFileCurCP(), |
| 645 | args: Args.MakeArgString(Str: TC.GetLinkerPath()), args&: CmdArgs, args: Inputs, args: Output)); |
| 646 | } |
| 647 | |
| 648 | // BareMetal toolchain allows all sanitizers where the compiler generates valid |
| 649 | // code, ignoring all runtime library support issues on the assumption that |
| 650 | // baremetal targets typically implement their own runtime support. |
| 651 | SanitizerMask |
| 652 | BareMetal::getSupportedSanitizers(BoundArch BA, |
| 653 | Action::OffloadKind DeviceOffloadKind) const { |
| 654 | const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; |
| 655 | const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || |
| 656 | getTriple().getArch() == llvm::Triple::aarch64_be; |
| 657 | const bool IsRISCV64 = getTriple().isRISCV64(); |
| 658 | SanitizerMask Res = ToolChain::getSupportedSanitizers(BA, DeviceOffloadKind); |
| 659 | Res |= SanitizerKind::Address; |
| 660 | Res |= SanitizerKind::KernelAddress; |
| 661 | Res |= SanitizerKind::PointerCompare; |
| 662 | Res |= SanitizerKind::PointerSubtract; |
| 663 | Res |= SanitizerKind::Fuzzer; |
| 664 | Res |= SanitizerKind::FuzzerNoLink; |
| 665 | Res |= SanitizerKind::Vptr; |
| 666 | Res |= SanitizerKind::SafeStack; |
| 667 | Res |= SanitizerKind::Thread; |
| 668 | Res |= SanitizerKind::Scudo; |
| 669 | if (IsX86_64 || IsAArch64 || IsRISCV64) { |
| 670 | Res |= SanitizerKind::HWAddress; |
| 671 | Res |= SanitizerKind::KernelHWAddress; |
| 672 | } |
| 673 | return Res; |
| 674 | } |
| 675 | |