David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 1 | //===--- OpenBSD.cpp - OpenBSD 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 "OpenBSD.h" |
| 11 | #include "Arch/Mips.h" |
| 12 | #include "Arch/Sparc.h" |
| 13 | #include "CommonArgs.h" |
| 14 | #include "clang/Driver/Compilation.h" |
| 15 | #include "clang/Driver/Options.h" |
Kamil Rytarowski | 3f7f960 | 2018-03-03 11:52:52 +0000 | [diff] [blame] | 16 | #include "clang/Driver/SanitizerArgs.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 17 | #include "llvm/Option/ArgList.h" |
| 18 | |
| 19 | using namespace clang::driver; |
| 20 | using namespace clang::driver::tools; |
| 21 | using namespace clang::driver::toolchains; |
| 22 | using namespace clang; |
| 23 | using namespace llvm::opt; |
| 24 | |
| 25 | void openbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA, |
| 26 | const InputInfo &Output, |
| 27 | const InputInfoList &Inputs, |
| 28 | const ArgList &Args, |
| 29 | const char *LinkingOutput) const { |
| 30 | claimNoWarnArgs(Args); |
| 31 | ArgStringList CmdArgs; |
| 32 | |
| 33 | switch (getToolChain().getArch()) { |
| 34 | case llvm::Triple::x86: |
| 35 | // When building 32-bit code on OpenBSD/amd64, we have to explicitly |
| 36 | // instruct as in the base system to assemble 32-bit code. |
| 37 | CmdArgs.push_back("--32"); |
| 38 | break; |
| 39 | |
| 40 | case llvm::Triple::ppc: |
| 41 | CmdArgs.push_back("-mppc"); |
| 42 | CmdArgs.push_back("-many"); |
| 43 | break; |
| 44 | |
| 45 | case llvm::Triple::sparc: |
| 46 | case llvm::Triple::sparcel: { |
| 47 | CmdArgs.push_back("-32"); |
| 48 | std::string CPU = getCPUName(Args, getToolChain().getTriple()); |
| 49 | CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple())); |
| 50 | AddAssemblerKPIC(getToolChain(), Args, CmdArgs); |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | case llvm::Triple::sparcv9: { |
| 55 | CmdArgs.push_back("-64"); |
| 56 | std::string CPU = getCPUName(Args, getToolChain().getTriple()); |
| 57 | CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple())); |
| 58 | AddAssemblerKPIC(getToolChain(), Args, CmdArgs); |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | case llvm::Triple::mips64: |
| 63 | case llvm::Triple::mips64el: { |
| 64 | StringRef CPUName; |
| 65 | StringRef ABIName; |
| 66 | mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName); |
| 67 | |
| 68 | CmdArgs.push_back("-mabi"); |
| 69 | CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data()); |
| 70 | |
Alexander Richardson | 742553d | 2018-06-25 16:49:52 +0000 | [diff] [blame] | 71 | if (getToolChain().getTriple().isLittleEndian()) |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 72 | CmdArgs.push_back("-EL"); |
Alexander Richardson | 742553d | 2018-06-25 16:49:52 +0000 | [diff] [blame] | 73 | else |
| 74 | CmdArgs.push_back("-EB"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 75 | |
| 76 | AddAssemblerKPIC(getToolChain(), Args, CmdArgs); |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); |
| 85 | |
| 86 | CmdArgs.push_back("-o"); |
| 87 | CmdArgs.push_back(Output.getFilename()); |
| 88 | |
| 89 | for (const auto &II : Inputs) |
| 90 | CmdArgs.push_back(II.getFilename()); |
| 91 | |
| 92 | const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); |
| 93 | C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); |
| 94 | } |
| 95 | |
| 96 | void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 97 | const InputInfo &Output, |
| 98 | const InputInfoList &Inputs, |
| 99 | const ArgList &Args, |
| 100 | const char *LinkingOutput) const { |
Kamil Rytarowski | 3f7f960 | 2018-03-03 11:52:52 +0000 | [diff] [blame] | 101 | const toolchains::OpenBSD &ToolChain = |
| 102 | static_cast<const toolchains::OpenBSD &>(getToolChain()); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 103 | const Driver &D = getToolChain().getDriver(); |
| 104 | ArgStringList CmdArgs; |
| 105 | |
| 106 | // Silence warning for "clang -g foo.o -o foo" |
| 107 | Args.ClaimAllArgs(options::OPT_g_Group); |
| 108 | // and "clang -emit-llvm foo.o -o foo" |
| 109 | Args.ClaimAllArgs(options::OPT_emit_llvm); |
| 110 | // and for "clang -w foo.o -o foo". Other warning options are already |
| 111 | // handled somewhere else. |
| 112 | Args.ClaimAllArgs(options::OPT_w); |
| 113 | |
| 114 | if (getToolChain().getArch() == llvm::Triple::mips64) |
| 115 | CmdArgs.push_back("-EB"); |
| 116 | else if (getToolChain().getArch() == llvm::Triple::mips64el) |
| 117 | CmdArgs.push_back("-EL"); |
| 118 | |
| 119 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) { |
| 120 | CmdArgs.push_back("-e"); |
| 121 | CmdArgs.push_back("__start"); |
| 122 | } |
| 123 | |
| 124 | CmdArgs.push_back("--eh-frame-hdr"); |
| 125 | if (Args.hasArg(options::OPT_static)) { |
| 126 | CmdArgs.push_back("-Bstatic"); |
| 127 | } else { |
| 128 | if (Args.hasArg(options::OPT_rdynamic)) |
| 129 | CmdArgs.push_back("-export-dynamic"); |
| 130 | CmdArgs.push_back("-Bdynamic"); |
| 131 | if (Args.hasArg(options::OPT_shared)) { |
| 132 | CmdArgs.push_back("-shared"); |
| 133 | } else { |
| 134 | CmdArgs.push_back("-dynamic-linker"); |
| 135 | CmdArgs.push_back("/usr/libexec/ld.so"); |
| 136 | } |
| 137 | } |
| 138 | |
Brad Smith | 580f8e6 | 2017-07-30 21:13:59 +0000 | [diff] [blame] | 139 | if (Args.hasArg(options::OPT_pie)) |
| 140 | CmdArgs.push_back("-pie"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 141 | if (Args.hasArg(options::OPT_nopie)) |
| 142 | CmdArgs.push_back("-nopie"); |
| 143 | |
| 144 | if (Output.isFilename()) { |
| 145 | CmdArgs.push_back("-o"); |
| 146 | CmdArgs.push_back(Output.getFilename()); |
| 147 | } else { |
| 148 | assert(Output.isNothing() && "Invalid output."); |
| 149 | } |
| 150 | |
| 151 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { |
| 152 | if (!Args.hasArg(options::OPT_shared)) { |
| 153 | if (Args.hasArg(options::OPT_pg)) |
| 154 | CmdArgs.push_back( |
| 155 | Args.MakeArgString(getToolChain().GetFilePath("gcrt0.o"))); |
| 156 | else if (Args.hasArg(options::OPT_static) && |
| 157 | !Args.hasArg(options::OPT_nopie)) |
| 158 | CmdArgs.push_back( |
| 159 | Args.MakeArgString(getToolChain().GetFilePath("rcrt0.o"))); |
| 160 | else |
| 161 | CmdArgs.push_back( |
| 162 | Args.MakeArgString(getToolChain().GetFilePath("crt0.o"))); |
| 163 | CmdArgs.push_back( |
| 164 | Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o"))); |
| 165 | } else { |
| 166 | CmdArgs.push_back( |
| 167 | Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o"))); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | std::string Triple = getToolChain().getTripleString(); |
| 172 | if (Triple.substr(0, 6) == "x86_64") |
| 173 | Triple.replace(0, 6, "amd64"); |
| 174 | CmdArgs.push_back( |
| 175 | Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple + "/4.2.1")); |
Kamil Rytarowski | 3f7f960 | 2018-03-03 11:52:52 +0000 | [diff] [blame] | 176 | CmdArgs.push_back(Args.MakeArgString("-L/usr/lib")); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 177 | |
| 178 | Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group, |
| 179 | options::OPT_e, options::OPT_s, options::OPT_t, |
| 180 | options::OPT_Z_Flag, options::OPT_r}); |
| 181 | |
Kamil Rytarowski | 3f7f960 | 2018-03-03 11:52:52 +0000 | [diff] [blame] | 182 | bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); |
Dean Michael Berris | 9b59233 | 2018-04-04 12:47:49 +0000 | [diff] [blame] | 183 | bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 184 | AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); |
| 185 | |
| 186 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { |
| 187 | if (D.CCCIsCXX()) { |
Nico Weber | 0ee47d9 | 2017-07-25 18:02:57 +0000 | [diff] [blame] | 188 | if (getToolChain().ShouldLinkCXXStdlib(Args)) |
| 189 | getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 190 | if (Args.hasArg(options::OPT_pg)) |
| 191 | CmdArgs.push_back("-lm_p"); |
| 192 | else |
| 193 | CmdArgs.push_back("-lm"); |
| 194 | } |
Kamil Rytarowski | 3f7f960 | 2018-03-03 11:52:52 +0000 | [diff] [blame] | 195 | if (NeedsSanitizerDeps) { |
| 196 | CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins", false)); |
| 197 | linkSanitizerRuntimeDeps(ToolChain, CmdArgs); |
| 198 | } |
Dean Michael Berris | 9b59233 | 2018-04-04 12:47:49 +0000 | [diff] [blame] | 199 | if (NeedsXRayDeps) { |
| 200 | CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins", false)); |
Dean Michael Berris | 6244037 | 2018-04-06 03:53:04 +0000 | [diff] [blame] | 201 | linkXRayRuntimeDeps(ToolChain, CmdArgs); |
Dean Michael Berris | 9b59233 | 2018-04-04 12:47:49 +0000 | [diff] [blame] | 202 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 203 | // FIXME: For some reason GCC passes -lgcc before adding |
| 204 | // the default system libraries. Just mimic this for now. |
| 205 | CmdArgs.push_back("-lgcc"); |
| 206 | |
| 207 | if (Args.hasArg(options::OPT_pthread)) { |
| 208 | if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg)) |
| 209 | CmdArgs.push_back("-lpthread_p"); |
| 210 | else |
| 211 | CmdArgs.push_back("-lpthread"); |
| 212 | } |
| 213 | |
| 214 | if (!Args.hasArg(options::OPT_shared)) { |
| 215 | if (Args.hasArg(options::OPT_pg)) |
| 216 | CmdArgs.push_back("-lc_p"); |
| 217 | else |
| 218 | CmdArgs.push_back("-lc"); |
| 219 | } |
| 220 | |
| 221 | CmdArgs.push_back("-lgcc"); |
| 222 | } |
| 223 | |
| 224 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { |
| 225 | if (!Args.hasArg(options::OPT_shared)) |
| 226 | CmdArgs.push_back( |
| 227 | Args.MakeArgString(getToolChain().GetFilePath("crtend.o"))); |
| 228 | else |
| 229 | CmdArgs.push_back( |
| 230 | Args.MakeArgString(getToolChain().GetFilePath("crtendS.o"))); |
| 231 | } |
| 232 | |
| 233 | const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath()); |
| 234 | C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); |
| 235 | } |
| 236 | |
Kamil Rytarowski | 3f7f960 | 2018-03-03 11:52:52 +0000 | [diff] [blame] | 237 | SanitizerMask OpenBSD::getSupportedSanitizers() const { |
| 238 | const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; |
| 239 | const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; |
| 240 | |
| 241 | // For future use, only UBsan at the moment |
| 242 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
| 243 | |
Dean Michael Berris | bfd98d0 | 2018-04-11 05:40:47 +0000 | [diff] [blame] | 244 | if (IsX86 || IsX86_64) { |
Kamil Rytarowski | 3f7f960 | 2018-03-03 11:52:52 +0000 | [diff] [blame] | 245 | Res |= SanitizerKind::Vptr; |
Dean Michael Berris | bfd98d0 | 2018-04-11 05:40:47 +0000 | [diff] [blame] | 246 | Res |= SanitizerKind::Fuzzer; |
| 247 | Res |= SanitizerKind::FuzzerNoLink; |
| 248 | } |
Kamil Rytarowski | 3f7f960 | 2018-03-03 11:52:52 +0000 | [diff] [blame] | 249 | |
| 250 | return Res; |
| 251 | } |
| 252 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 253 | /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly. |
| 254 | |
| 255 | OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple, |
| 256 | const ArgList &Args) |
| 257 | : Generic_ELF(D, Triple, Args) { |
| 258 | getFilePaths().push_back(getDriver().Dir + "/../lib"); |
| 259 | getFilePaths().push_back("/usr/lib"); |
| 260 | } |
| 261 | |
Dean Michael Berris | 8b7a0e1 | 2018-04-19 06:55:30 +0000 | [diff] [blame] | 262 | void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args, |
| 263 | ArgStringList &CmdArgs) const { |
| 264 | bool Profiling = Args.hasArg(options::OPT_pg); |
| 265 | |
| 266 | CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++"); |
| 267 | CmdArgs.push_back(Profiling ? "-lc++abi_p" : "-lc++abi"); |
| 268 | } |
| 269 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 270 | Tool *OpenBSD::buildAssembler() const { |
| 271 | return new tools::openbsd::Assembler(*this); |
| 272 | } |
| 273 | |
| 274 | Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); } |