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