|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 "Managarm.h" |
| 10 | +#include "Arch/ARM.h" |
| 11 | +#include "Arch/RISCV.h" |
| 12 | +#include "clang/Config/config.h" |
| 13 | +#include "clang/Driver/CommonArgs.h" |
| 14 | +#include "clang/Driver/Driver.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; |
| 23 | +using namespace llvm::opt; |
| 24 | + |
| 25 | +using tools::addPathIfExists; |
| 26 | + |
| 27 | +std::string Managarm::getMultiarchTriple(const Driver &D, |
| 28 | + const llvm::Triple &TargetTriple, |
| 29 | + StringRef SysRoot) const { |
| 30 | + switch (TargetTriple.getArch()) { |
| 31 | + default: |
| 32 | + return TargetTriple.str(); |
| 33 | + case llvm::Triple::x86_64: |
| 34 | + return "x86_64-managarm-" + TargetTriple.getEnvironmentName().str(); |
| 35 | + case llvm::Triple::aarch64: |
| 36 | + return "aarch64-managarm-" + TargetTriple.getEnvironmentName().str(); |
| 37 | + case llvm::Triple::riscv64: |
| 38 | + return "riscv64-managarm-" + TargetTriple.getEnvironmentName().str(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { |
| 43 | + // It happens that only x86, PPC and SPARC use the 'lib32' variant of |
| 44 | + // oslibdir, and using that variant while targeting other architectures causes |
| 45 | + // problems because the libraries are laid out in shared system roots that |
| 46 | + // can't cope with a 'lib32' library search path being considered. So we only |
| 47 | + // enable them when we know we may need it. |
| 48 | + // |
| 49 | + // FIXME: This is a bit of a hack. We should really unify this code for |
| 50 | + // reasoning about oslibdir spellings with the lib dir spellings in the |
| 51 | + // GCCInstallationDetector, but that is a more significant refactoring. |
| 52 | + if (Triple.getArch() == llvm::Triple::x86 || Triple.isPPC32() || |
| 53 | + Triple.getArch() == llvm::Triple::sparc) |
| 54 | + return "lib32"; |
| 55 | + |
| 56 | + if (Triple.getArch() == llvm::Triple::x86_64 && Triple.isX32()) |
| 57 | + return "libx32"; |
| 58 | + |
| 59 | + if (Triple.getArch() == llvm::Triple::riscv32) |
| 60 | + return "lib32"; |
| 61 | + |
| 62 | + return Triple.isArch32Bit() ? "lib" : "lib64"; |
| 63 | +} |
| 64 | + |
| 65 | +Managarm::Managarm(const Driver &D, const llvm::Triple &Triple, |
| 66 | + const ArgList &Args) |
| 67 | + : Generic_ELF(D, Triple, Args) { |
| 68 | + GCCInstallation.init(Triple, Args); |
| 69 | + Multilibs = GCCInstallation.getMultilibs(); |
| 70 | + SelectedMultilibs.assign({GCCInstallation.getMultilib()}); |
| 71 | + std::string SysRoot = computeSysRoot(); |
| 72 | + |
| 73 | + ToolChain::path_list &PPaths = getProgramPaths(); |
| 74 | + |
| 75 | + Generic_GCC::PushPPaths(PPaths); |
| 76 | + |
| 77 | +#ifdef ENABLE_LINKER_BUILD_ID |
| 78 | + ExtraOpts.push_back("--build-id"); |
| 79 | +#endif |
| 80 | + |
| 81 | + // The selection of paths to try here is designed to match the patterns which |
| 82 | + // the GCC driver itself uses, as this is part of the GCC-compatible driver. |
| 83 | + // This was determined by running GCC in a fake filesystem, creating all |
| 84 | + // possible permutations of these directories, and seeing which ones it added |
| 85 | + // to the link paths. |
| 86 | + path_list &Paths = getFilePaths(); |
| 87 | + |
| 88 | + const std::string OSLibDir = std::string(getOSLibDir(Triple, Args)); |
| 89 | + const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot); |
| 90 | + |
| 91 | + Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths); |
| 92 | + |
| 93 | + addPathIfExists(D, concat(SysRoot, "/lib", MultiarchTriple), Paths); |
| 94 | + addPathIfExists(D, concat(SysRoot, "/lib/..", OSLibDir), Paths); |
| 95 | + addPathIfExists(D, concat(SysRoot, "/usr/lib", MultiarchTriple), Paths); |
| 96 | + addPathIfExists(D, concat(SysRoot, "/usr", OSLibDir), Paths); |
| 97 | + |
| 98 | + Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths); |
| 99 | + |
| 100 | + addPathIfExists(D, concat(SysRoot, "/lib"), Paths); |
| 101 | + addPathIfExists(D, concat(SysRoot, "/usr/lib"), Paths); |
| 102 | +} |
| 103 | + |
| 104 | +bool Managarm::HasNativeLLVMSupport() const { return true; } |
| 105 | + |
| 106 | +Tool *Managarm::buildLinker() const { |
| 107 | + return new tools::gnutools::Linker(*this); |
| 108 | +} |
| 109 | + |
| 110 | +Tool *Managarm::buildAssembler() const { |
| 111 | + return new tools::gnutools::Assembler(*this); |
| 112 | +} |
| 113 | + |
| 114 | +std::string Managarm::computeSysRoot() const { |
| 115 | + if (!getDriver().SysRoot.empty()) |
| 116 | + return getDriver().SysRoot; |
| 117 | + return std::string(); |
| 118 | +} |
| 119 | + |
| 120 | +std::string Managarm::getDynamicLinker(const ArgList &Args) const { |
| 121 | + switch (getTriple().getArch()) { |
| 122 | + case llvm::Triple::aarch64: |
| 123 | + return "/lib/aarch64-managarm/ld.so"; |
| 124 | + case llvm::Triple::riscv64: { |
| 125 | + StringRef ABIName = tools::riscv::getRISCVABI(Args, getTriple()); |
| 126 | + return ("/lib/riscv64-managarm/ld-riscv64-" + ABIName + ".so").str(); |
| 127 | + } |
| 128 | + case llvm::Triple::x86_64: |
| 129 | + return "/lib/x86_64-managarm/ld.so"; |
| 130 | + default: |
| 131 | + llvm_unreachable("unsupported architecture"); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +void Managarm::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 136 | + ArgStringList &CC1Args) const { |
| 137 | + const Driver &D = getDriver(); |
| 138 | + std::string SysRoot = computeSysRoot(); |
| 139 | + |
| 140 | + if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) |
| 141 | + return; |
| 142 | + |
| 143 | + if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) |
| 144 | + addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); |
| 145 | + |
| 146 | + // Add 'include' in the resource directory, which is similar to |
| 147 | + // GCC_INCLUDE_DIR (private headers) in GCC. |
| 148 | + if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
| 149 | + SmallString<128> ResourceDirInclude(D.ResourceDir); |
| 150 | + llvm::sys::path::append(ResourceDirInclude, "include"); |
| 151 | + addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude); |
| 152 | + } |
| 153 | + |
| 154 | + if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
| 155 | + return; |
| 156 | + |
| 157 | + // TOOL_INCLUDE_DIR |
| 158 | + AddMultilibIncludeArgs(DriverArgs, CC1Args); |
| 159 | + |
| 160 | + // Check for configure-time C include directories. |
| 161 | + StringRef CIncludeDirs(C_INCLUDE_DIRS); |
| 162 | + if (CIncludeDirs != "") { |
| 163 | + SmallVector<StringRef, 5> dirs; |
| 164 | + CIncludeDirs.split(dirs, ":"); |
| 165 | + for (StringRef dir : dirs) { |
| 166 | + StringRef Prefix = |
| 167 | + llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : ""; |
| 168 | + addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); |
| 169 | + } |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + // On systems using multiarch, add /usr/include/$triple before |
| 174 | + // /usr/include. |
| 175 | + std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot); |
| 176 | + if (!MultiarchIncludeDir.empty()) |
| 177 | + addExternCSystemInclude( |
| 178 | + DriverArgs, CC1Args, |
| 179 | + concat(SysRoot, "/usr/include", MultiarchIncludeDir)); |
| 180 | + |
| 181 | + // Add an include of '/include' directly. This isn't provided by default by |
| 182 | + // system GCCs, but is often used with cross-compiling GCCs, and harmless to |
| 183 | + // add even when Clang is acting as-if it were a system compiler. |
| 184 | + addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/include")); |
| 185 | + |
| 186 | + addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/include")); |
| 187 | +} |
| 188 | + |
| 189 | +void Managarm::addLibStdCxxIncludePaths( |
| 190 | + const llvm::opt::ArgList &DriverArgs, |
| 191 | + llvm::opt::ArgStringList &CC1Args) const { |
| 192 | + // We need a detected GCC installation on Managarm to provide libstdc++'s |
| 193 | + // headers. |
| 194 | + if (!GCCInstallation.isValid()) |
| 195 | + return; |
| 196 | + |
| 197 | + StringRef TripleStr = GCCInstallation.getTriple().str(); |
| 198 | + |
| 199 | + // Try generic GCC detection. |
| 200 | + Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args, TripleStr); |
| 201 | +} |
| 202 | + |
| 203 | +SanitizerMask Managarm::getSupportedSanitizers() const { |
| 204 | + const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; |
| 205 | + SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
| 206 | + Res |= SanitizerKind::PointerCompare; |
| 207 | + Res |= SanitizerKind::PointerSubtract; |
| 208 | + Res |= SanitizerKind::KernelAddress; |
| 209 | + Res |= SanitizerKind::Vptr; |
| 210 | + if (IsX86_64) |
| 211 | + Res |= SanitizerKind::KernelMemory; |
| 212 | + return Res; |
| 213 | +} |
| 214 | + |
| 215 | +void Managarm::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { |
| 216 | + for (const auto &Opt : ExtraOpts) |
| 217 | + CmdArgs.push_back(Opt.c_str()); |
| 218 | +} |
0 commit comments