David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 1 | //===--- CommonArgs.cpp - Args handling for multiple toolchains -*- 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 "CommonArgs.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 11 | #include "Arch/AArch64.h" |
| 12 | #include "Arch/ARM.h" |
| 13 | #include "Arch/Mips.h" |
| 14 | #include "Arch/PPC.h" |
| 15 | #include "Arch/SystemZ.h" |
| 16 | #include "Arch/X86.h" |
Florian Hahn | 2e081d1 | 2018-04-20 12:50:10 +0000 | [diff] [blame] | 17 | #include "Hexagon.h" |
| 18 | #include "InputInfo.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
| 21 | #include "clang/Basic/ObjCRuntime.h" |
| 22 | #include "clang/Basic/Version.h" |
| 23 | #include "clang/Basic/VirtualFileSystem.h" |
| 24 | #include "clang/Config/config.h" |
| 25 | #include "clang/Driver/Action.h" |
| 26 | #include "clang/Driver/Compilation.h" |
| 27 | #include "clang/Driver/Driver.h" |
| 28 | #include "clang/Driver/DriverDiagnostic.h" |
| 29 | #include "clang/Driver/Job.h" |
| 30 | #include "clang/Driver/Options.h" |
| 31 | #include "clang/Driver/SanitizerArgs.h" |
| 32 | #include "clang/Driver/ToolChain.h" |
| 33 | #include "clang/Driver/Util.h" |
Dean Michael Berris | 6244037 | 2018-04-06 03:53:04 +0000 | [diff] [blame] | 34 | #include "clang/Driver/XRayArgs.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
| 36 | #include "llvm/ADT/SmallString.h" |
| 37 | #include "llvm/ADT/StringExtras.h" |
| 38 | #include "llvm/ADT/StringSwitch.h" |
| 39 | #include "llvm/ADT/Twine.h" |
| 40 | #include "llvm/Option/Arg.h" |
| 41 | #include "llvm/Option/ArgList.h" |
| 42 | #include "llvm/Option/Option.h" |
| 43 | #include "llvm/Support/CodeGen.h" |
| 44 | #include "llvm/Support/Compression.h" |
Florian Hahn | 2e081d1 | 2018-04-20 12:50:10 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Debug.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 46 | #include "llvm/Support/ErrorHandling.h" |
| 47 | #include "llvm/Support/FileSystem.h" |
| 48 | #include "llvm/Support/Host.h" |
| 49 | #include "llvm/Support/Path.h" |
| 50 | #include "llvm/Support/Process.h" |
| 51 | #include "llvm/Support/Program.h" |
| 52 | #include "llvm/Support/ScopedPrinter.h" |
| 53 | #include "llvm/Support/TargetParser.h" |
| 54 | #include "llvm/Support/YAMLParser.h" |
| 55 | |
| 56 | using namespace clang::driver; |
| 57 | using namespace clang::driver::tools; |
| 58 | using namespace clang; |
| 59 | using namespace llvm::opt; |
| 60 | |
| 61 | void tools::addPathIfExists(const Driver &D, const Twine &Path, |
| 62 | ToolChain::path_list &Paths) { |
| 63 | if (D.getVFS().exists(Path)) |
| 64 | Paths.push_back(Path.str()); |
| 65 | } |
| 66 | |
| 67 | void tools::handleTargetFeaturesGroup(const ArgList &Args, |
| 68 | std::vector<StringRef> &Features, |
| 69 | OptSpecifier Group) { |
| 70 | for (const Arg *A : Args.filtered(Group)) { |
| 71 | StringRef Name = A->getOption().getName(); |
| 72 | A->claim(); |
| 73 | |
| 74 | // Skip over "-m". |
| 75 | assert(Name.startswith("m") && "Invalid feature name."); |
| 76 | Name = Name.substr(1); |
| 77 | |
| 78 | bool IsNegative = Name.startswith("no-"); |
| 79 | if (IsNegative) |
| 80 | Name = Name.substr(3); |
| 81 | Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void tools::addDirectoryList(const ArgList &Args, ArgStringList &CmdArgs, |
| 86 | const char *ArgName, const char *EnvVar) { |
| 87 | const char *DirList = ::getenv(EnvVar); |
| 88 | bool CombinedArg = false; |
| 89 | |
| 90 | if (!DirList) |
| 91 | return; // Nothing to do. |
| 92 | |
| 93 | StringRef Name(ArgName); |
| 94 | if (Name.equals("-I") || Name.equals("-L")) |
| 95 | CombinedArg = true; |
| 96 | |
| 97 | StringRef Dirs(DirList); |
| 98 | if (Dirs.empty()) // Empty string should not add '.'. |
| 99 | return; |
| 100 | |
| 101 | StringRef::size_type Delim; |
| 102 | while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) { |
| 103 | if (Delim == 0) { // Leading colon. |
| 104 | if (CombinedArg) { |
| 105 | CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + ".")); |
| 106 | } else { |
| 107 | CmdArgs.push_back(ArgName); |
| 108 | CmdArgs.push_back("."); |
| 109 | } |
| 110 | } else { |
| 111 | if (CombinedArg) { |
| 112 | CmdArgs.push_back( |
| 113 | Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim))); |
| 114 | } else { |
| 115 | CmdArgs.push_back(ArgName); |
| 116 | CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim))); |
| 117 | } |
| 118 | } |
| 119 | Dirs = Dirs.substr(Delim + 1); |
| 120 | } |
| 121 | |
| 122 | if (Dirs.empty()) { // Trailing colon. |
| 123 | if (CombinedArg) { |
| 124 | CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + ".")); |
| 125 | } else { |
| 126 | CmdArgs.push_back(ArgName); |
| 127 | CmdArgs.push_back("."); |
| 128 | } |
| 129 | } else { // Add the last path. |
| 130 | if (CombinedArg) { |
| 131 | CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs)); |
| 132 | } else { |
| 133 | CmdArgs.push_back(ArgName); |
| 134 | CmdArgs.push_back(Args.MakeArgString(Dirs)); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, |
| 140 | const ArgList &Args, ArgStringList &CmdArgs, |
| 141 | const JobAction &JA) { |
| 142 | const Driver &D = TC.getDriver(); |
| 143 | |
| 144 | // Add extra linker input arguments which are not treated as inputs |
| 145 | // (constructed via -Xarch_). |
| 146 | Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input); |
| 147 | |
| 148 | for (const auto &II : Inputs) { |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 149 | // If the current tool chain refers to an OpenMP or HIP offloading host, we |
| 150 | // should ignore inputs that refer to OpenMP or HIP offloading devices - |
| 151 | // they will be embedded according to a proper linker script. |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 152 | if (auto *IA = II.getAction()) |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 153 | if ((JA.isHostOffloading(Action::OFK_OpenMP) && |
| 154 | IA->isDeviceOffloading(Action::OFK_OpenMP)) || |
| 155 | (JA.isHostOffloading(Action::OFK_HIP) && |
| 156 | IA->isDeviceOffloading(Action::OFK_HIP))) |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 157 | continue; |
| 158 | |
| 159 | if (!TC.HasNativeLLVMSupport() && types::isLLVMIR(II.getType())) |
| 160 | // Don't try to pass LLVM inputs unless we have native support. |
| 161 | D.Diag(diag::err_drv_no_linker_llvm_support) << TC.getTripleString(); |
| 162 | |
| 163 | // Add filenames immediately. |
| 164 | if (II.isFilename()) { |
| 165 | CmdArgs.push_back(II.getFilename()); |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | // Otherwise, this is a linker input argument. |
| 170 | const Arg &A = II.getInputArg(); |
| 171 | |
| 172 | // Handle reserved library options. |
| 173 | if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) |
| 174 | TC.AddCXXStdlibLibArgs(Args, CmdArgs); |
| 175 | else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) |
| 176 | TC.AddCCKextLibArgs(Args, CmdArgs); |
| 177 | else if (A.getOption().matches(options::OPT_z)) { |
| 178 | // Pass -z prefix for gcc linker compatibility. |
| 179 | A.claim(); |
| 180 | A.render(Args, CmdArgs); |
| 181 | } else { |
| 182 | A.renderAsInput(Args, CmdArgs); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // LIBRARY_PATH - included following the user specified library paths. |
| 187 | // and only supported on native toolchains. |
| 188 | if (!TC.isCrossCompiling()) { |
| 189 | addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH"); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void tools::AddTargetFeature(const ArgList &Args, |
| 194 | std::vector<StringRef> &Features, |
| 195 | OptSpecifier OnOpt, OptSpecifier OffOpt, |
| 196 | StringRef FeatureName) { |
| 197 | if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) { |
| 198 | if (A->getOption().matches(OnOpt)) |
| 199 | Features.push_back(Args.MakeArgString("+" + FeatureName)); |
| 200 | else |
| 201 | Features.push_back(Args.MakeArgString("-" + FeatureName)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /// Get the (LLVM) name of the R600 gpu we are targeting. |
| 206 | static std::string getR600TargetGPU(const ArgList &Args) { |
| 207 | if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { |
| 208 | const char *GPUName = A->getValue(); |
| 209 | return llvm::StringSwitch<const char *>(GPUName) |
| 210 | .Cases("rv630", "rv635", "r600") |
| 211 | .Cases("rv610", "rv620", "rs780", "rs880") |
| 212 | .Case("rv740", "rv770") |
| 213 | .Case("palm", "cedar") |
| 214 | .Cases("sumo", "sumo2", "sumo") |
| 215 | .Case("hemlock", "cypress") |
| 216 | .Case("aruba", "cayman") |
| 217 | .Default(GPUName); |
| 218 | } |
| 219 | return ""; |
| 220 | } |
| 221 | |
Nikolai Bozhenov | 35d3c35 | 2017-06-27 09:48:24 +0000 | [diff] [blame] | 222 | static std::string getNios2TargetCPU(const ArgList &Args) { |
| 223 | Arg *A = Args.getLastArg(options::OPT_mcpu_EQ); |
| 224 | if (!A) |
| 225 | A = Args.getLastArg(options::OPT_march_EQ); |
| 226 | |
| 227 | if (!A) |
| 228 | return ""; |
| 229 | |
| 230 | const char *name = A->getValue(); |
| 231 | return llvm::StringSwitch<const char *>(name) |
| 232 | .Case("r1", "nios2r1") |
| 233 | .Case("r2", "nios2r2") |
| 234 | .Default(name); |
| 235 | } |
| 236 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 237 | static std::string getLanaiTargetCPU(const ArgList &Args) { |
| 238 | if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { |
| 239 | return A->getValue(); |
| 240 | } |
| 241 | return ""; |
| 242 | } |
| 243 | |
| 244 | /// Get the (LLVM) name of the WebAssembly cpu we are targeting. |
| 245 | static StringRef getWebAssemblyTargetCPU(const ArgList &Args) { |
| 246 | // If we have -mcpu=, use that. |
| 247 | if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { |
| 248 | StringRef CPU = A->getValue(); |
| 249 | |
| 250 | #ifdef __wasm__ |
| 251 | // Handle "native" by examining the host. "native" isn't meaningful when |
| 252 | // cross compiling, so only support this when the host is also WebAssembly. |
| 253 | if (CPU == "native") |
| 254 | return llvm::sys::getHostCPUName(); |
| 255 | #endif |
| 256 | |
| 257 | return CPU; |
| 258 | } |
| 259 | |
| 260 | return "generic"; |
| 261 | } |
| 262 | |
| 263 | std::string tools::getCPUName(const ArgList &Args, const llvm::Triple &T, |
| 264 | bool FromAs) { |
| 265 | Arg *A; |
| 266 | |
| 267 | switch (T.getArch()) { |
| 268 | default: |
| 269 | return ""; |
| 270 | |
| 271 | case llvm::Triple::aarch64: |
| 272 | case llvm::Triple::aarch64_be: |
| 273 | return aarch64::getAArch64TargetCPU(Args, A); |
| 274 | |
| 275 | case llvm::Triple::arm: |
| 276 | case llvm::Triple::armeb: |
| 277 | case llvm::Triple::thumb: |
| 278 | case llvm::Triple::thumbeb: { |
| 279 | StringRef MArch, MCPU; |
| 280 | arm::getARMArchCPUFromArgs(Args, MArch, MCPU, FromAs); |
| 281 | return arm::getARMTargetCPU(MCPU, MArch, T); |
| 282 | } |
Leslie Zhai | ff04109 | 2017-04-20 04:23:24 +0000 | [diff] [blame] | 283 | |
| 284 | case llvm::Triple::avr: |
| 285 | if (const Arg *A = Args.getLastArg(options::OPT_mmcu_EQ)) |
| 286 | return A->getValue(); |
| 287 | return ""; |
| 288 | |
Nikolai Bozhenov | 35d3c35 | 2017-06-27 09:48:24 +0000 | [diff] [blame] | 289 | case llvm::Triple::nios2: { |
| 290 | return getNios2TargetCPU(Args); |
| 291 | } |
| 292 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 293 | case llvm::Triple::mips: |
| 294 | case llvm::Triple::mipsel: |
| 295 | case llvm::Triple::mips64: |
| 296 | case llvm::Triple::mips64el: { |
| 297 | StringRef CPUName; |
| 298 | StringRef ABIName; |
| 299 | mips::getMipsCPUAndABI(Args, T, CPUName, ABIName); |
| 300 | return CPUName; |
| 301 | } |
| 302 | |
| 303 | case llvm::Triple::nvptx: |
| 304 | case llvm::Triple::nvptx64: |
| 305 | if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) |
| 306 | return A->getValue(); |
| 307 | return ""; |
| 308 | |
| 309 | case llvm::Triple::ppc: |
| 310 | case llvm::Triple::ppc64: |
| 311 | case llvm::Triple::ppc64le: { |
| 312 | std::string TargetCPUName = ppc::getPPCTargetCPU(Args); |
| 313 | // LLVM may default to generating code for the native CPU, |
| 314 | // but, like gcc, we default to a more generic option for |
| 315 | // each architecture. (except on Darwin) |
| 316 | if (TargetCPUName.empty() && !T.isOSDarwin()) { |
| 317 | if (T.getArch() == llvm::Triple::ppc64) |
| 318 | TargetCPUName = "ppc64"; |
| 319 | else if (T.getArch() == llvm::Triple::ppc64le) |
| 320 | TargetCPUName = "ppc64le"; |
| 321 | else |
| 322 | TargetCPUName = "ppc"; |
| 323 | } |
| 324 | return TargetCPUName; |
| 325 | } |
| 326 | |
Yonghong Song | c4ea101 | 2017-08-23 04:26:17 +0000 | [diff] [blame] | 327 | case llvm::Triple::bpfel: |
| 328 | case llvm::Triple::bpfeb: |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 329 | case llvm::Triple::sparc: |
| 330 | case llvm::Triple::sparcel: |
| 331 | case llvm::Triple::sparcv9: |
| 332 | if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) |
| 333 | return A->getValue(); |
| 334 | return ""; |
| 335 | |
| 336 | case llvm::Triple::x86: |
| 337 | case llvm::Triple::x86_64: |
| 338 | return x86::getX86TargetCPU(Args, T); |
| 339 | |
| 340 | case llvm::Triple::hexagon: |
| 341 | return "hexagon" + |
| 342 | toolchains::HexagonToolChain::GetTargetCPUVersion(Args).str(); |
| 343 | |
| 344 | case llvm::Triple::lanai: |
| 345 | return getLanaiTargetCPU(Args); |
| 346 | |
| 347 | case llvm::Triple::systemz: |
| 348 | return systemz::getSystemZTargetCPU(Args); |
| 349 | |
| 350 | case llvm::Triple::r600: |
| 351 | case llvm::Triple::amdgcn: |
| 352 | return getR600TargetGPU(Args); |
| 353 | |
| 354 | case llvm::Triple::wasm32: |
| 355 | case llvm::Triple::wasm64: |
| 356 | return getWebAssemblyTargetCPU(Args); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | unsigned tools::getLTOParallelism(const ArgList &Args, const Driver &D) { |
| 361 | unsigned Parallelism = 0; |
| 362 | Arg *LtoJobsArg = Args.getLastArg(options::OPT_flto_jobs_EQ); |
| 363 | if (LtoJobsArg && |
| 364 | StringRef(LtoJobsArg->getValue()).getAsInteger(10, Parallelism)) |
| 365 | D.Diag(diag::err_drv_invalid_int_value) << LtoJobsArg->getAsString(Args) |
| 366 | << LtoJobsArg->getValue(); |
| 367 | return Parallelism; |
| 368 | } |
| 369 | |
Sam Clegg | 7892ae4 | 2018-01-31 18:55:22 +0000 | [diff] [blame] | 370 | // CloudABI uses -ffunction-sections and -fdata-sections by default. |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 371 | bool tools::isUseSeparateSections(const llvm::Triple &Triple) { |
Sam Clegg | 7892ae4 | 2018-01-31 18:55:22 +0000 | [diff] [blame] | 372 | return Triple.getOS() == llvm::Triple::CloudABI; |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void tools::AddGoldPlugin(const ToolChain &ToolChain, const ArgList &Args, |
Florian Hahn | 2e081d1 | 2018-04-20 12:50:10 +0000 | [diff] [blame] | 376 | ArgStringList &CmdArgs, const InputInfo &Output, |
| 377 | const InputInfo &Input, bool IsThinLTO) { |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 378 | // Tell the linker to load the plugin. This has to come before AddLinkerInputs |
| 379 | // as gold requires -plugin to come before any -plugin-opt that -Wl might |
| 380 | // forward. |
| 381 | CmdArgs.push_back("-plugin"); |
Dan Albert | c3a11d5 | 2017-08-22 21:05:01 +0000 | [diff] [blame] | 382 | |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 383 | #if defined(_WIN32) |
Dan Albert | c3a11d5 | 2017-08-22 21:05:01 +0000 | [diff] [blame] | 384 | const char *Suffix = ".dll"; |
| 385 | #elif defined(__APPLE__) |
| 386 | const char *Suffix = ".dylib"; |
| 387 | #else |
| 388 | const char *Suffix = ".so"; |
| 389 | #endif |
| 390 | |
| 391 | SmallString<1024> Plugin; |
| 392 | llvm::sys::path::native(Twine(ToolChain.getDriver().Dir) + |
| 393 | "/../lib" CLANG_LIBDIR_SUFFIX "/LLVMgold" + |
| 394 | Suffix, |
| 395 | Plugin); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 396 | CmdArgs.push_back(Args.MakeArgString(Plugin)); |
| 397 | |
| 398 | // Try to pass driver level flags relevant to LTO code generation down to |
| 399 | // the plugin. |
| 400 | |
| 401 | // Handle flags for selecting CPU variants. |
| 402 | std::string CPU = getCPUName(Args, ToolChain.getTriple()); |
| 403 | if (!CPU.empty()) |
| 404 | CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=mcpu=") + CPU)); |
| 405 | |
| 406 | if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { |
| 407 | StringRef OOpt; |
| 408 | if (A->getOption().matches(options::OPT_O4) || |
| 409 | A->getOption().matches(options::OPT_Ofast)) |
| 410 | OOpt = "3"; |
| 411 | else if (A->getOption().matches(options::OPT_O)) |
| 412 | OOpt = A->getValue(); |
| 413 | else if (A->getOption().matches(options::OPT_O0)) |
| 414 | OOpt = "0"; |
| 415 | if (!OOpt.empty()) |
| 416 | CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=O") + OOpt)); |
| 417 | } |
| 418 | |
Yunlian Jiang | 87c88cc | 2018-06-25 23:05:27 +0000 | [diff] [blame] | 419 | if (Args.hasArg(options::OPT_gsplit_dwarf)) { |
| 420 | CmdArgs.push_back( |
| 421 | Args.MakeArgString(Twine("-plugin-opt=dwo_dir=") + |
| 422 | Output.getFilename() + "_dwo")); |
| 423 | } |
| 424 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 425 | if (IsThinLTO) |
| 426 | CmdArgs.push_back("-plugin-opt=thinlto"); |
| 427 | |
Florian Hahn | 2e081d1 | 2018-04-20 12:50:10 +0000 | [diff] [blame] | 428 | if (unsigned Parallelism = getLTOParallelism(Args, ToolChain.getDriver())) |
Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 429 | CmdArgs.push_back( |
| 430 | Args.MakeArgString("-plugin-opt=jobs=" + Twine(Parallelism))); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 431 | |
| 432 | // If an explicit debugger tuning argument appeared, pass it along. |
| 433 | if (Arg *A = Args.getLastArg(options::OPT_gTune_Group, |
| 434 | options::OPT_ggdbN_Group)) { |
| 435 | if (A->getOption().matches(options::OPT_glldb)) |
| 436 | CmdArgs.push_back("-plugin-opt=-debugger-tune=lldb"); |
| 437 | else if (A->getOption().matches(options::OPT_gsce)) |
| 438 | CmdArgs.push_back("-plugin-opt=-debugger-tune=sce"); |
| 439 | else |
| 440 | CmdArgs.push_back("-plugin-opt=-debugger-tune=gdb"); |
| 441 | } |
| 442 | |
| 443 | bool UseSeparateSections = |
| 444 | isUseSeparateSections(ToolChain.getEffectiveTriple()); |
| 445 | |
| 446 | if (Args.hasFlag(options::OPT_ffunction_sections, |
| 447 | options::OPT_fno_function_sections, UseSeparateSections)) { |
| 448 | CmdArgs.push_back("-plugin-opt=-function-sections"); |
| 449 | } |
| 450 | |
| 451 | if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections, |
| 452 | UseSeparateSections)) { |
| 453 | CmdArgs.push_back("-plugin-opt=-data-sections"); |
| 454 | } |
| 455 | |
Dehao Chen | ea4b78f | 2017-03-21 21:40:53 +0000 | [diff] [blame] | 456 | if (Arg *A = getLastProfileSampleUseArg(Args)) { |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 457 | StringRef FName = A->getValue(); |
| 458 | if (!llvm::sys::fs::exists(FName)) |
Florian Hahn | 2e081d1 | 2018-04-20 12:50:10 +0000 | [diff] [blame] | 459 | ToolChain.getDriver().Diag(diag::err_drv_no_such_file) << FName; |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 460 | else |
| 461 | CmdArgs.push_back( |
| 462 | Args.MakeArgString(Twine("-plugin-opt=sample-profile=") + FName)); |
| 463 | } |
Sean Fertile | 03e77c6 | 2017-10-05 01:50:48 +0000 | [diff] [blame] | 464 | |
| 465 | // Need this flag to turn on new pass manager via Gold plugin. |
| 466 | if (Args.hasFlag(options::OPT_fexperimental_new_pass_manager, |
| 467 | options::OPT_fno_experimental_new_pass_manager, |
Petr Hosek | c3aa97a | 2018-04-06 00:53:00 +0000 | [diff] [blame] | 468 | /* Default */ ENABLE_EXPERIMENTAL_NEW_PASS_MANAGER)) { |
Sean Fertile | 03e77c6 | 2017-10-05 01:50:48 +0000 | [diff] [blame] | 469 | CmdArgs.push_back("-plugin-opt=new-pass-manager"); |
| 470 | } |
| 471 | |
Florian Hahn | 2e081d1 | 2018-04-20 12:50:10 +0000 | [diff] [blame] | 472 | // Setup statistics file output. |
| 473 | SmallString<128> StatsFile = |
| 474 | getStatsFileName(Args, Output, Input, ToolChain.getDriver()); |
| 475 | if (!StatsFile.empty()) |
| 476 | CmdArgs.push_back( |
| 477 | Args.MakeArgString(Twine("-plugin-opt=stats-file=") + StatsFile)); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args, |
| 481 | ArgStringList &CmdArgs) { |
Petr Hosek | bae4856 | 2018-04-02 23:36:14 +0000 | [diff] [blame] | 482 | if (!Args.hasFlag(options::OPT_frtlib_add_rpath, |
| 483 | options::OPT_fno_rtlib_add_rpath, false)) |
| 484 | return; |
| 485 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 486 | std::string CandidateRPath = TC.getArchSpecificLibPath(); |
| 487 | if (TC.getVFS().exists(CandidateRPath)) { |
| 488 | CmdArgs.push_back("-rpath"); |
| 489 | CmdArgs.push_back(Args.MakeArgString(CandidateRPath.c_str())); |
| 490 | } |
| 491 | } |
| 492 | |
Jonas Hahnfeld | 8ea76fa | 2017-04-19 13:55:39 +0000 | [diff] [blame] | 493 | bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC, |
| 494 | const ArgList &Args, bool IsOffloadingHost, |
| 495 | bool GompNeedsRT) { |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 496 | if (!Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ, |
| 497 | options::OPT_fno_openmp, false)) |
Jonas Hahnfeld | 8ea76fa | 2017-04-19 13:55:39 +0000 | [diff] [blame] | 498 | return false; |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 499 | |
| 500 | switch (TC.getDriver().getOpenMPRuntime(Args)) { |
| 501 | case Driver::OMPRT_OMP: |
| 502 | CmdArgs.push_back("-lomp"); |
| 503 | break; |
| 504 | case Driver::OMPRT_GOMP: |
| 505 | CmdArgs.push_back("-lgomp"); |
Jonas Hahnfeld | 8ea76fa | 2017-04-19 13:55:39 +0000 | [diff] [blame] | 506 | |
| 507 | if (GompNeedsRT) |
| 508 | CmdArgs.push_back("-lrt"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 509 | break; |
| 510 | case Driver::OMPRT_IOMP5: |
| 511 | CmdArgs.push_back("-liomp5"); |
| 512 | break; |
| 513 | case Driver::OMPRT_Unknown: |
| 514 | // Already diagnosed. |
Jonas Hahnfeld | 8ea76fa | 2017-04-19 13:55:39 +0000 | [diff] [blame] | 515 | return false; |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Jonas Hahnfeld | 8ea76fa | 2017-04-19 13:55:39 +0000 | [diff] [blame] | 518 | if (IsOffloadingHost) |
| 519 | CmdArgs.push_back("-lomptarget"); |
| 520 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 521 | addArchSpecificRPath(TC, Args, CmdArgs); |
Jonas Hahnfeld | 8ea76fa | 2017-04-19 13:55:39 +0000 | [diff] [blame] | 522 | |
| 523 | return true; |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args, |
| 527 | ArgStringList &CmdArgs, StringRef Sanitizer, |
| 528 | bool IsShared, bool IsWhole) { |
| 529 | // Wrap any static runtimes that must be forced into executable in |
| 530 | // whole-archive. |
Alex Shlyapnikov | 85da0f6 | 2018-02-05 23:59:13 +0000 | [diff] [blame] | 531 | if (IsWhole) CmdArgs.push_back("--whole-archive"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 532 | CmdArgs.push_back(TC.getCompilerRTArgString(Args, Sanitizer, IsShared)); |
Alex Shlyapnikov | 85da0f6 | 2018-02-05 23:59:13 +0000 | [diff] [blame] | 533 | if (IsWhole) CmdArgs.push_back("--no-whole-archive"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 534 | |
| 535 | if (IsShared) { |
| 536 | addArchSpecificRPath(TC, Args, CmdArgs); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Tries to use a file with the list of dynamic symbols that need to be exported |
| 541 | // from the runtime library. Returns true if the file was found. |
| 542 | static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args, |
| 543 | ArgStringList &CmdArgs, |
| 544 | StringRef Sanitizer) { |
Alex Shlyapnikov | 85da0f6 | 2018-02-05 23:59:13 +0000 | [diff] [blame] | 545 | // Solaris ld defaults to --export-dynamic behaviour but doesn't support |
| 546 | // the option, so don't try to pass it. |
| 547 | if (TC.getTriple().getOS() == llvm::Triple::Solaris) |
| 548 | return true; |
Walter Lee | a5cc222 | 2018-05-17 18:04:39 +0000 | [diff] [blame] | 549 | // Myriad is static linking only. Furthermore, some versions of its |
| 550 | // linker have the bug where --export-dynamic overrides -static, so |
| 551 | // don't use --export-dynamic on that platform. |
| 552 | if (TC.getTriple().getVendor() == llvm::Triple::Myriad) |
| 553 | return true; |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 554 | SmallString<128> SanRT(TC.getCompilerRT(Args, Sanitizer)); |
| 555 | if (llvm::sys::fs::exists(SanRT + ".syms")) { |
| 556 | CmdArgs.push_back(Args.MakeArgString("--dynamic-list=" + SanRT + ".syms")); |
| 557 | return true; |
| 558 | } |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | void tools::linkSanitizerRuntimeDeps(const ToolChain &TC, |
| 563 | ArgStringList &CmdArgs) { |
| 564 | // Force linking against the system libraries sanitizers depends on |
| 565 | // (see PR15823 why this is necessary). |
| 566 | CmdArgs.push_back("--no-as-needed"); |
| 567 | // There's no libpthread or librt on RTEMS. |
| 568 | if (TC.getTriple().getOS() != llvm::Triple::RTEMS) { |
| 569 | CmdArgs.push_back("-lpthread"); |
Kamil Rytarowski | 150a377 | 2018-03-03 11:47:27 +0000 | [diff] [blame] | 570 | if (TC.getTriple().getOS() != llvm::Triple::OpenBSD) |
| 571 | CmdArgs.push_back("-lrt"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 572 | } |
| 573 | CmdArgs.push_back("-lm"); |
Kamil Rytarowski | bb9a852 | 2017-12-08 17:38:25 +0000 | [diff] [blame] | 574 | // There's no libdl on all OSes. |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 575 | if (TC.getTriple().getOS() != llvm::Triple::FreeBSD && |
Kamil Rytarowski | 2eef475 | 2017-07-04 19:55:56 +0000 | [diff] [blame] | 576 | TC.getTriple().getOS() != llvm::Triple::NetBSD && |
Kamil Rytarowski | 150a377 | 2018-03-03 11:47:27 +0000 | [diff] [blame] | 577 | TC.getTriple().getOS() != llvm::Triple::OpenBSD && |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 578 | TC.getTriple().getOS() != llvm::Triple::RTEMS) |
| 579 | CmdArgs.push_back("-ldl"); |
Kamil Rytarowski | e21475e | 2017-12-19 07:10:33 +0000 | [diff] [blame] | 580 | // Required for backtrace on some OSes |
Kamil Rytarowski | a7ef6a6 | 2018-01-24 23:08:49 +0000 | [diff] [blame] | 581 | if (TC.getTriple().getOS() == llvm::Triple::NetBSD || |
| 582 | TC.getTriple().getOS() == llvm::Triple::FreeBSD) |
Kamil Rytarowski | e21475e | 2017-12-19 07:10:33 +0000 | [diff] [blame] | 583 | CmdArgs.push_back("-lexecinfo"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static void |
| 587 | collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, |
| 588 | SmallVectorImpl<StringRef> &SharedRuntimes, |
| 589 | SmallVectorImpl<StringRef> &StaticRuntimes, |
| 590 | SmallVectorImpl<StringRef> &NonWholeStaticRuntimes, |
| 591 | SmallVectorImpl<StringRef> &HelperStaticRuntimes, |
| 592 | SmallVectorImpl<StringRef> &RequiredSymbols) { |
| 593 | const SanitizerArgs &SanArgs = TC.getSanitizerArgs(); |
| 594 | // Collect shared runtimes. |
Evgeniy Stepanov | 0876cfb | 2017-10-05 20:14:00 +0000 | [diff] [blame] | 595 | if (SanArgs.needsSharedRt()) { |
| 596 | if (SanArgs.needsAsanRt()) { |
| 597 | SharedRuntimes.push_back("asan"); |
| 598 | if (!Args.hasArg(options::OPT_shared) && !TC.getTriple().isAndroid()) |
| 599 | HelperStaticRuntimes.push_back("asan-preinit"); |
| 600 | } |
Evgeniy Stepanov | 0876cfb | 2017-10-05 20:14:00 +0000 | [diff] [blame] | 601 | if (SanArgs.needsUbsanRt()) { |
Kostya Kortchinsky | 64d8093 | 2018-06-22 14:31:30 +0000 | [diff] [blame] | 602 | if (SanArgs.requiresMinimalRuntime()) |
Evgeniy Stepanov | 0876cfb | 2017-10-05 20:14:00 +0000 | [diff] [blame] | 603 | SharedRuntimes.push_back("ubsan_minimal"); |
Kostya Kortchinsky | 64d8093 | 2018-06-22 14:31:30 +0000 | [diff] [blame] | 604 | else |
Evgeniy Stepanov | 0876cfb | 2017-10-05 20:14:00 +0000 | [diff] [blame] | 605 | SharedRuntimes.push_back("ubsan_standalone"); |
Evgeniy Stepanov | 0876cfb | 2017-10-05 20:14:00 +0000 | [diff] [blame] | 606 | } |
Kostya Kortchinsky | 64d8093 | 2018-06-22 14:31:30 +0000 | [diff] [blame] | 607 | if (SanArgs.needsScudoRt()) { |
| 608 | if (SanArgs.requiresMinimalRuntime()) |
| 609 | SharedRuntimes.push_back("scudo_minimal"); |
| 610 | else |
| 611 | SharedRuntimes.push_back("scudo"); |
| 612 | } |
Evgeniy Stepanov | 12817e5 | 2017-12-09 01:32:07 +0000 | [diff] [blame] | 613 | if (SanArgs.needsHwasanRt()) |
| 614 | SharedRuntimes.push_back("hwasan"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 615 | } |
Evgeniy Stepanov | 6d2b6f0 | 2017-08-29 20:03:51 +0000 | [diff] [blame] | 616 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 617 | // The stats_client library is also statically linked into DSOs. |
| 618 | if (SanArgs.needsStatsRt()) |
| 619 | StaticRuntimes.push_back("stats_client"); |
| 620 | |
| 621 | // Collect static runtimes. |
Evgeniy Stepanov | 0876cfb | 2017-10-05 20:14:00 +0000 | [diff] [blame] | 622 | if (Args.hasArg(options::OPT_shared) || SanArgs.needsSharedRt()) { |
| 623 | // Don't link static runtimes into DSOs or if -shared-libasan. |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 624 | return; |
| 625 | } |
| 626 | if (SanArgs.needsAsanRt()) { |
Evgeniy Stepanov | 0876cfb | 2017-10-05 20:14:00 +0000 | [diff] [blame] | 627 | StaticRuntimes.push_back("asan"); |
| 628 | if (SanArgs.linkCXXRuntimes()) |
| 629 | StaticRuntimes.push_back("asan_cxx"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 630 | } |
Evgeniy Stepanov | 12817e5 | 2017-12-09 01:32:07 +0000 | [diff] [blame] | 631 | |
| 632 | if (SanArgs.needsHwasanRt()) { |
| 633 | StaticRuntimes.push_back("hwasan"); |
| 634 | if (SanArgs.linkCXXRuntimes()) |
| 635 | StaticRuntimes.push_back("hwasan_cxx"); |
| 636 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 637 | if (SanArgs.needsDfsanRt()) |
| 638 | StaticRuntimes.push_back("dfsan"); |
| 639 | if (SanArgs.needsLsanRt()) |
| 640 | StaticRuntimes.push_back("lsan"); |
| 641 | if (SanArgs.needsMsanRt()) { |
| 642 | StaticRuntimes.push_back("msan"); |
| 643 | if (SanArgs.linkCXXRuntimes()) |
| 644 | StaticRuntimes.push_back("msan_cxx"); |
| 645 | } |
| 646 | if (SanArgs.needsTsanRt()) { |
| 647 | StaticRuntimes.push_back("tsan"); |
| 648 | if (SanArgs.linkCXXRuntimes()) |
| 649 | StaticRuntimes.push_back("tsan_cxx"); |
| 650 | } |
| 651 | if (SanArgs.needsUbsanRt()) { |
Evgeniy Stepanov | 6d2b6f0 | 2017-08-29 20:03:51 +0000 | [diff] [blame] | 652 | if (SanArgs.requiresMinimalRuntime()) { |
| 653 | StaticRuntimes.push_back("ubsan_minimal"); |
| 654 | } else { |
| 655 | StaticRuntimes.push_back("ubsan_standalone"); |
| 656 | if (SanArgs.linkCXXRuntimes()) |
| 657 | StaticRuntimes.push_back("ubsan_standalone_cxx"); |
| 658 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 659 | } |
| 660 | if (SanArgs.needsSafeStackRt()) { |
| 661 | NonWholeStaticRuntimes.push_back("safestack"); |
| 662 | RequiredSymbols.push_back("__safestack_init"); |
| 663 | } |
| 664 | if (SanArgs.needsCfiRt()) |
| 665 | StaticRuntimes.push_back("cfi"); |
| 666 | if (SanArgs.needsCfiDiagRt()) { |
| 667 | StaticRuntimes.push_back("cfi_diag"); |
| 668 | if (SanArgs.linkCXXRuntimes()) |
| 669 | StaticRuntimes.push_back("ubsan_standalone_cxx"); |
| 670 | } |
| 671 | if (SanArgs.needsStatsRt()) { |
| 672 | NonWholeStaticRuntimes.push_back("stats"); |
| 673 | RequiredSymbols.push_back("__sanitizer_stats_register"); |
| 674 | } |
| 675 | if (SanArgs.needsEsanRt()) |
| 676 | StaticRuntimes.push_back("esan"); |
Kostya Kortchinsky | 8acdc98 | 2017-11-03 17:04:13 +0000 | [diff] [blame] | 677 | if (SanArgs.needsScudoRt()) { |
Kostya Kortchinsky | 64d8093 | 2018-06-22 14:31:30 +0000 | [diff] [blame] | 678 | if (SanArgs.requiresMinimalRuntime()) { |
| 679 | StaticRuntimes.push_back("scudo_minimal"); |
| 680 | if (SanArgs.linkCXXRuntimes()) |
| 681 | StaticRuntimes.push_back("scudo_cxx_minimal"); |
| 682 | } else { |
| 683 | StaticRuntimes.push_back("scudo"); |
| 684 | if (SanArgs.linkCXXRuntimes()) |
| 685 | StaticRuntimes.push_back("scudo_cxx"); |
| 686 | } |
Kostya Kortchinsky | 8acdc98 | 2017-11-03 17:04:13 +0000 | [diff] [blame] | 687 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | // Should be called before we add system libraries (C++ ABI, libstdc++/libc++, |
| 691 | // C runtime, etc). Returns true if sanitizer system deps need to be linked in. |
| 692 | bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, |
| 693 | ArgStringList &CmdArgs) { |
| 694 | SmallVector<StringRef, 4> SharedRuntimes, StaticRuntimes, |
| 695 | NonWholeStaticRuntimes, HelperStaticRuntimes, RequiredSymbols; |
| 696 | collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes, |
| 697 | NonWholeStaticRuntimes, HelperStaticRuntimes, |
| 698 | RequiredSymbols); |
George Karpenkov | 9f6f74c | 2017-08-21 23:25:19 +0000 | [diff] [blame] | 699 | |
George Karpenkov | f2fc5b0 | 2017-04-24 18:23:24 +0000 | [diff] [blame] | 700 | // Inject libfuzzer dependencies. |
George Karpenkov | 2363fdd | 2017-06-29 19:52:33 +0000 | [diff] [blame] | 701 | if (TC.getSanitizerArgs().needsFuzzer() |
| 702 | && !Args.hasArg(options::OPT_shared)) { |
George Karpenkov | 9f6f74c | 2017-08-21 23:25:19 +0000 | [diff] [blame] | 703 | |
| 704 | addSanitizerRuntime(TC, Args, CmdArgs, "fuzzer", false, true); |
| 705 | if (!Args.hasArg(clang::driver::options::OPT_nostdlibxx)) |
| 706 | TC.AddCXXStdlibLibArgs(Args, CmdArgs); |
George Karpenkov | f2fc5b0 | 2017-04-24 18:23:24 +0000 | [diff] [blame] | 707 | } |
| 708 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 709 | for (auto RT : SharedRuntimes) |
| 710 | addSanitizerRuntime(TC, Args, CmdArgs, RT, true, false); |
| 711 | for (auto RT : HelperStaticRuntimes) |
| 712 | addSanitizerRuntime(TC, Args, CmdArgs, RT, false, true); |
| 713 | bool AddExportDynamic = false; |
| 714 | for (auto RT : StaticRuntimes) { |
| 715 | addSanitizerRuntime(TC, Args, CmdArgs, RT, false, true); |
| 716 | AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, RT); |
| 717 | } |
| 718 | for (auto RT : NonWholeStaticRuntimes) { |
| 719 | addSanitizerRuntime(TC, Args, CmdArgs, RT, false, false); |
| 720 | AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, RT); |
| 721 | } |
| 722 | for (auto S : RequiredSymbols) { |
| 723 | CmdArgs.push_back("-u"); |
| 724 | CmdArgs.push_back(Args.MakeArgString(S)); |
| 725 | } |
| 726 | // If there is a static runtime with no dynamic list, force all the symbols |
| 727 | // to be dynamic to be sure we export sanitizer interface functions. |
| 728 | if (AddExportDynamic) |
Alex Shlyapnikov | 85da0f6 | 2018-02-05 23:59:13 +0000 | [diff] [blame] | 729 | CmdArgs.push_back("--export-dynamic"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 730 | |
| 731 | const SanitizerArgs &SanArgs = TC.getSanitizerArgs(); |
| 732 | if (SanArgs.hasCrossDsoCfi() && !AddExportDynamic) |
| 733 | CmdArgs.push_back("-export-dynamic-symbol=__cfi_check"); |
| 734 | |
| 735 | return !StaticRuntimes.empty() || !NonWholeStaticRuntimes.empty(); |
| 736 | } |
| 737 | |
Dean Michael Berris | 6244037 | 2018-04-06 03:53:04 +0000 | [diff] [blame] | 738 | bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { |
| 739 | if (Args.hasArg(options::OPT_shared)) |
| 740 | return false; |
| 741 | |
| 742 | if (TC.getXRayArgs().needsXRayRt()) { |
| 743 | CmdArgs.push_back("-whole-archive"); |
| 744 | CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false)); |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame] | 745 | for (const auto &Mode : TC.getXRayArgs().modeList()) |
| 746 | CmdArgs.push_back(TC.getCompilerRTArgString(Args, Mode, false)); |
Dean Michael Berris | 6244037 | 2018-04-06 03:53:04 +0000 | [diff] [blame] | 747 | CmdArgs.push_back("-no-whole-archive"); |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | void tools::linkXRayRuntimeDeps(const ToolChain &TC, ArgStringList &CmdArgs) { |
| 755 | CmdArgs.push_back("--no-as-needed"); |
| 756 | CmdArgs.push_back("-lpthread"); |
| 757 | if (TC.getTriple().getOS() != llvm::Triple::OpenBSD) |
| 758 | CmdArgs.push_back("-lrt"); |
| 759 | CmdArgs.push_back("-lm"); |
| 760 | |
| 761 | if (TC.getTriple().getOS() != llvm::Triple::FreeBSD && |
| 762 | TC.getTriple().getOS() != llvm::Triple::NetBSD && |
| 763 | TC.getTriple().getOS() != llvm::Triple::OpenBSD) |
| 764 | CmdArgs.push_back("-ldl"); |
| 765 | } |
| 766 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 767 | bool tools::areOptimizationsEnabled(const ArgList &Args) { |
| 768 | // Find the last -O arg and see if it is non-zero. |
| 769 | if (Arg *A = Args.getLastArg(options::OPT_O_Group)) |
| 770 | return !A->getOption().matches(options::OPT_O0); |
| 771 | // Defaults to -O0. |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | const char *tools::SplitDebugName(const ArgList &Args, const InputInfo &Input) { |
| 776 | Arg *FinalOutput = Args.getLastArg(options::OPT_o); |
| 777 | if (FinalOutput && Args.hasArg(options::OPT_c)) { |
| 778 | SmallString<128> T(FinalOutput->getValue()); |
| 779 | llvm::sys::path::replace_extension(T, "dwo"); |
| 780 | return Args.MakeArgString(T); |
| 781 | } else { |
| 782 | // Use the compilation dir. |
| 783 | SmallString<128> T( |
| 784 | Args.getLastArgValue(options::OPT_fdebug_compilation_dir)); |
| 785 | SmallString<128> F(llvm::sys::path::stem(Input.getBaseInput())); |
| 786 | llvm::sys::path::replace_extension(F, "dwo"); |
| 787 | T += F; |
| 788 | return Args.MakeArgString(F); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | void tools::SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T, |
| 793 | const JobAction &JA, const ArgList &Args, |
| 794 | const InputInfo &Output, const char *OutFile) { |
| 795 | ArgStringList ExtractArgs; |
| 796 | ExtractArgs.push_back("--extract-dwo"); |
| 797 | |
| 798 | ArgStringList StripArgs; |
| 799 | StripArgs.push_back("--strip-dwo"); |
| 800 | |
| 801 | // Grabbing the output of the earlier compile step. |
| 802 | StripArgs.push_back(Output.getFilename()); |
| 803 | ExtractArgs.push_back(Output.getFilename()); |
| 804 | ExtractArgs.push_back(OutFile); |
| 805 | |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 806 | const char *Exec = |
| 807 | Args.MakeArgString(TC.GetProgramPath(CLANG_DEFAULT_OBJCOPY)); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 808 | InputInfo II(types::TY_Object, Output.getFilename(), Output.getFilename()); |
| 809 | |
| 810 | // First extract the dwo sections. |
| 811 | C.addCommand(llvm::make_unique<Command>(JA, T, Exec, ExtractArgs, II)); |
| 812 | |
| 813 | // Then remove them from the original .o file. |
| 814 | C.addCommand(llvm::make_unique<Command>(JA, T, Exec, StripArgs, II)); |
| 815 | } |
| 816 | |
| 817 | // Claim options we don't want to warn if they are unused. We do this for |
| 818 | // options that build systems might add but are unused when assembling or only |
| 819 | // running the preprocessor for example. |
| 820 | void tools::claimNoWarnArgs(const ArgList &Args) { |
| 821 | // Don't warn about unused -f(no-)?lto. This can happen when we're |
| 822 | // preprocessing, precompiling or assembling. |
| 823 | Args.ClaimAllArgs(options::OPT_flto_EQ); |
| 824 | Args.ClaimAllArgs(options::OPT_flto); |
| 825 | Args.ClaimAllArgs(options::OPT_fno_lto); |
| 826 | } |
| 827 | |
| 828 | Arg *tools::getLastProfileUseArg(const ArgList &Args) { |
| 829 | auto *ProfileUseArg = Args.getLastArg( |
| 830 | options::OPT_fprofile_instr_use, options::OPT_fprofile_instr_use_EQ, |
| 831 | options::OPT_fprofile_use, options::OPT_fprofile_use_EQ, |
| 832 | options::OPT_fno_profile_instr_use); |
| 833 | |
| 834 | if (ProfileUseArg && |
| 835 | ProfileUseArg->getOption().matches(options::OPT_fno_profile_instr_use)) |
| 836 | ProfileUseArg = nullptr; |
| 837 | |
| 838 | return ProfileUseArg; |
| 839 | } |
| 840 | |
Dehao Chen | ea4b78f | 2017-03-21 21:40:53 +0000 | [diff] [blame] | 841 | Arg *tools::getLastProfileSampleUseArg(const ArgList &Args) { |
| 842 | auto *ProfileSampleUseArg = Args.getLastArg( |
| 843 | options::OPT_fprofile_sample_use, options::OPT_fprofile_sample_use_EQ, |
| 844 | options::OPT_fauto_profile, options::OPT_fauto_profile_EQ, |
| 845 | options::OPT_fno_profile_sample_use, options::OPT_fno_auto_profile); |
| 846 | |
| 847 | if (ProfileSampleUseArg && |
| 848 | (ProfileSampleUseArg->getOption().matches( |
| 849 | options::OPT_fno_profile_sample_use) || |
| 850 | ProfileSampleUseArg->getOption().matches(options::OPT_fno_auto_profile))) |
| 851 | return nullptr; |
| 852 | |
| 853 | return Args.getLastArg(options::OPT_fprofile_sample_use_EQ, |
| 854 | options::OPT_fauto_profile_EQ); |
| 855 | } |
| 856 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 857 | /// Parses the various -fpic/-fPIC/-fpie/-fPIE arguments. Then, |
| 858 | /// smooshes them together with platform defaults, to decide whether |
| 859 | /// this compile should be using PIC mode or not. Returns a tuple of |
| 860 | /// (RelocationModel, PICLevel, IsPIE). |
| 861 | std::tuple<llvm::Reloc::Model, unsigned, bool> |
| 862 | tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) { |
| 863 | const llvm::Triple &EffectiveTriple = ToolChain.getEffectiveTriple(); |
| 864 | const llvm::Triple &Triple = ToolChain.getTriple(); |
| 865 | |
| 866 | bool PIE = ToolChain.isPIEDefault(); |
| 867 | bool PIC = PIE || ToolChain.isPICDefault(); |
| 868 | // The Darwin/MachO default to use PIC does not apply when using -static. |
| 869 | if (Triple.isOSBinFormatMachO() && Args.hasArg(options::OPT_static)) |
| 870 | PIE = PIC = false; |
| 871 | bool IsPICLevelTwo = PIC; |
| 872 | |
| 873 | bool KernelOrKext = |
| 874 | Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext); |
| 875 | |
| 876 | // Android-specific defaults for PIC/PIE |
| 877 | if (Triple.isAndroid()) { |
| 878 | switch (Triple.getArch()) { |
| 879 | case llvm::Triple::arm: |
| 880 | case llvm::Triple::armeb: |
| 881 | case llvm::Triple::thumb: |
| 882 | case llvm::Triple::thumbeb: |
| 883 | case llvm::Triple::aarch64: |
| 884 | case llvm::Triple::mips: |
| 885 | case llvm::Triple::mipsel: |
| 886 | case llvm::Triple::mips64: |
| 887 | case llvm::Triple::mips64el: |
| 888 | PIC = true; // "-fpic" |
| 889 | break; |
| 890 | |
| 891 | case llvm::Triple::x86: |
| 892 | case llvm::Triple::x86_64: |
| 893 | PIC = true; // "-fPIC" |
| 894 | IsPICLevelTwo = true; |
| 895 | break; |
| 896 | |
| 897 | default: |
| 898 | break; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | // OpenBSD-specific defaults for PIE |
| 903 | if (Triple.getOS() == llvm::Triple::OpenBSD) { |
| 904 | switch (ToolChain.getArch()) { |
Brad Smith | 3f2b1d7 | 2017-03-31 22:13:17 +0000 | [diff] [blame] | 905 | case llvm::Triple::arm: |
| 906 | case llvm::Triple::aarch64: |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 907 | case llvm::Triple::mips64: |
| 908 | case llvm::Triple::mips64el: |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 909 | case llvm::Triple::x86: |
| 910 | case llvm::Triple::x86_64: |
| 911 | IsPICLevelTwo = false; // "-fpie" |
| 912 | break; |
| 913 | |
| 914 | case llvm::Triple::ppc: |
| 915 | case llvm::Triple::sparc: |
Brad Smith | 3f2b1d7 | 2017-03-31 22:13:17 +0000 | [diff] [blame] | 916 | case llvm::Triple::sparcel: |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 917 | case llvm::Triple::sparcv9: |
| 918 | IsPICLevelTwo = true; // "-fPIE" |
| 919 | break; |
| 920 | |
| 921 | default: |
| 922 | break; |
| 923 | } |
| 924 | } |
| 925 | |
Konstantin Zhuravlyov | b4c83a0 | 2018-02-15 01:01:53 +0000 | [diff] [blame] | 926 | // AMDGPU-specific defaults for PIC. |
| 927 | if (Triple.getArch() == llvm::Triple::amdgcn) |
| 928 | PIC = true; |
| 929 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 930 | // The last argument relating to either PIC or PIE wins, and no |
| 931 | // other argument is used. If the last argument is any flavor of the |
| 932 | // '-fno-...' arguments, both PIC and PIE are disabled. Any PIE |
| 933 | // option implicitly enables PIC at the same level. |
| 934 | Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC, |
| 935 | options::OPT_fpic, options::OPT_fno_pic, |
| 936 | options::OPT_fPIE, options::OPT_fno_PIE, |
| 937 | options::OPT_fpie, options::OPT_fno_pie); |
| 938 | if (Triple.isOSWindows() && LastPICArg && |
| 939 | LastPICArg == |
| 940 | Args.getLastArg(options::OPT_fPIC, options::OPT_fpic, |
| 941 | options::OPT_fPIE, options::OPT_fpie)) { |
| 942 | ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target) |
| 943 | << LastPICArg->getSpelling() << Triple.str(); |
| 944 | if (Triple.getArch() == llvm::Triple::x86_64) |
| 945 | return std::make_tuple(llvm::Reloc::PIC_, 2U, false); |
| 946 | return std::make_tuple(llvm::Reloc::Static, 0U, false); |
| 947 | } |
| 948 | |
| 949 | // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness |
| 950 | // is forced, then neither PIC nor PIE flags will have no effect. |
| 951 | if (!ToolChain.isPICDefaultForced()) { |
| 952 | if (LastPICArg) { |
| 953 | Option O = LastPICArg->getOption(); |
| 954 | if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) || |
| 955 | O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) { |
| 956 | PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie); |
| 957 | PIC = |
| 958 | PIE || O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic); |
| 959 | IsPICLevelTwo = |
| 960 | O.matches(options::OPT_fPIE) || O.matches(options::OPT_fPIC); |
| 961 | } else { |
| 962 | PIE = PIC = false; |
| 963 | if (EffectiveTriple.isPS4CPU()) { |
| 964 | Arg *ModelArg = Args.getLastArg(options::OPT_mcmodel_EQ); |
| 965 | StringRef Model = ModelArg ? ModelArg->getValue() : ""; |
| 966 | if (Model != "kernel") { |
| 967 | PIC = true; |
| 968 | ToolChain.getDriver().Diag(diag::warn_drv_ps4_force_pic) |
| 969 | << LastPICArg->getSpelling(); |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | // Introduce a Darwin and PS4-specific hack. If the default is PIC, but the |
| 977 | // PIC level would've been set to level 1, force it back to level 2 PIC |
| 978 | // instead. |
| 979 | if (PIC && (Triple.isOSDarwin() || EffectiveTriple.isPS4CPU())) |
| 980 | IsPICLevelTwo |= ToolChain.isPICDefault(); |
| 981 | |
| 982 | // This kernel flags are a trump-card: they will disable PIC/PIE |
| 983 | // generation, independent of the argument order. |
| 984 | if (KernelOrKext && |
| 985 | ((!EffectiveTriple.isiOS() || EffectiveTriple.isOSVersionLT(6)) && |
| 986 | !EffectiveTriple.isWatchOS())) |
| 987 | PIC = PIE = false; |
| 988 | |
| 989 | if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) { |
| 990 | // This is a very special mode. It trumps the other modes, almost no one |
| 991 | // uses it, and it isn't even valid on any OS but Darwin. |
| 992 | if (!Triple.isOSDarwin()) |
| 993 | ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target) |
| 994 | << A->getSpelling() << Triple.str(); |
| 995 | |
| 996 | // FIXME: Warn when this flag trumps some other PIC or PIE flag. |
| 997 | |
| 998 | // Only a forced PIC mode can cause the actual compile to have PIC defines |
| 999 | // etc., no flags are sufficient. This behavior was selected to closely |
| 1000 | // match that of llvm-gcc and Apple GCC before that. |
| 1001 | PIC = ToolChain.isPICDefault() && ToolChain.isPICDefaultForced(); |
| 1002 | |
| 1003 | return std::make_tuple(llvm::Reloc::DynamicNoPIC, PIC ? 2U : 0U, false); |
| 1004 | } |
| 1005 | |
| 1006 | bool EmbeddedPISupported; |
| 1007 | switch (Triple.getArch()) { |
| 1008 | case llvm::Triple::arm: |
| 1009 | case llvm::Triple::armeb: |
| 1010 | case llvm::Triple::thumb: |
| 1011 | case llvm::Triple::thumbeb: |
| 1012 | EmbeddedPISupported = true; |
| 1013 | break; |
| 1014 | default: |
| 1015 | EmbeddedPISupported = false; |
| 1016 | break; |
| 1017 | } |
| 1018 | |
| 1019 | bool ROPI = false, RWPI = false; |
| 1020 | Arg* LastROPIArg = Args.getLastArg(options::OPT_fropi, options::OPT_fno_ropi); |
| 1021 | if (LastROPIArg && LastROPIArg->getOption().matches(options::OPT_fropi)) { |
| 1022 | if (!EmbeddedPISupported) |
| 1023 | ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target) |
| 1024 | << LastROPIArg->getSpelling() << Triple.str(); |
| 1025 | ROPI = true; |
| 1026 | } |
| 1027 | Arg *LastRWPIArg = Args.getLastArg(options::OPT_frwpi, options::OPT_fno_rwpi); |
| 1028 | if (LastRWPIArg && LastRWPIArg->getOption().matches(options::OPT_frwpi)) { |
| 1029 | if (!EmbeddedPISupported) |
| 1030 | ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target) |
| 1031 | << LastRWPIArg->getSpelling() << Triple.str(); |
| 1032 | RWPI = true; |
| 1033 | } |
| 1034 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 1035 | // ROPI and RWPI are not compatible with PIC or PIE. |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 1036 | if ((ROPI || RWPI) && (PIC || PIE)) |
| 1037 | ToolChain.getDriver().Diag(diag::err_drv_ropi_rwpi_incompatible_with_pic); |
| 1038 | |
Alexander Richardson | 742553d | 2018-06-25 16:49:52 +0000 | [diff] [blame] | 1039 | if (Triple.isMIPS()) { |
Aleksandar Beserminji | 20d603b | 2018-05-07 14:30:49 +0000 | [diff] [blame] | 1040 | StringRef CPUName; |
| 1041 | StringRef ABIName; |
| 1042 | mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); |
| 1043 | // When targeting the N64 ABI, PIC is the default, except in the case |
| 1044 | // when the -mno-abicalls option is used. In that case we exit |
| 1045 | // at next check regardless of PIC being set below. |
| 1046 | if (ABIName == "n64") |
| 1047 | PIC = true; |
Aleksandar Beserminji | 7ca42e8 | 2018-04-16 10:21:24 +0000 | [diff] [blame] | 1048 | // When targettng MIPS with -mno-abicalls, it's always static. |
| 1049 | if(Args.hasArg(options::OPT_mno_abicalls)) |
| 1050 | return std::make_tuple(llvm::Reloc::Static, 0U, false); |
| 1051 | // Unlike other architectures, MIPS, even with -fPIC/-mxgot/multigot, |
| 1052 | // does not use PIC level 2 for historical reasons. |
| 1053 | IsPICLevelTwo = false; |
| 1054 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 1055 | |
| 1056 | if (PIC) |
| 1057 | return std::make_tuple(llvm::Reloc::PIC_, IsPICLevelTwo ? 2U : 1U, PIE); |
| 1058 | |
| 1059 | llvm::Reloc::Model RelocM = llvm::Reloc::Static; |
| 1060 | if (ROPI && RWPI) |
| 1061 | RelocM = llvm::Reloc::ROPI_RWPI; |
| 1062 | else if (ROPI) |
| 1063 | RelocM = llvm::Reloc::ROPI; |
| 1064 | else if (RWPI) |
| 1065 | RelocM = llvm::Reloc::RWPI; |
| 1066 | |
| 1067 | return std::make_tuple(RelocM, 0U, false); |
| 1068 | } |
| 1069 | |
Saleem Abdulrasool | 3fe5b7a | 2018-04-19 23:14:57 +0000 | [diff] [blame] | 1070 | // `-falign-functions` indicates that the functions should be aligned to a |
| 1071 | // 16-byte boundary. |
| 1072 | // |
| 1073 | // `-falign-functions=1` is the same as `-fno-align-functions`. |
| 1074 | // |
| 1075 | // The scalar `n` in `-falign-functions=n` must be an integral value between |
| 1076 | // [0, 65536]. If the value is not a power-of-two, it will be rounded up to |
| 1077 | // the nearest power-of-two. |
| 1078 | // |
| 1079 | // If we return `0`, the frontend will default to the backend's preferred |
| 1080 | // alignment. |
| 1081 | // |
| 1082 | // NOTE: icc only allows values between [0, 4096]. icc uses `-falign-functions` |
| 1083 | // to mean `-falign-functions=16`. GCC defaults to the backend's preferred |
| 1084 | // alignment. For unaligned functions, we default to the backend's preferred |
| 1085 | // alignment. |
| 1086 | unsigned tools::ParseFunctionAlignment(const ToolChain &TC, |
| 1087 | const ArgList &Args) { |
| 1088 | const Arg *A = Args.getLastArg(options::OPT_falign_functions, |
| 1089 | options::OPT_falign_functions_EQ, |
| 1090 | options::OPT_fno_align_functions); |
| 1091 | if (!A || A->getOption().matches(options::OPT_fno_align_functions)) |
| 1092 | return 0; |
| 1093 | |
| 1094 | if (A->getOption().matches(options::OPT_falign_functions)) |
| 1095 | return 0; |
| 1096 | |
| 1097 | unsigned Value = 0; |
| 1098 | if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536) |
| 1099 | TC.getDriver().Diag(diag::err_drv_invalid_int_value) |
| 1100 | << A->getAsString(Args) << A->getValue(); |
| 1101 | return Value ? llvm::Log2_32_Ceil(std::min(Value, 65536u)) : Value; |
| 1102 | } |
| 1103 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 1104 | void tools::AddAssemblerKPIC(const ToolChain &ToolChain, const ArgList &Args, |
| 1105 | ArgStringList &CmdArgs) { |
| 1106 | llvm::Reloc::Model RelocationModel; |
| 1107 | unsigned PICLevel; |
| 1108 | bool IsPIE; |
| 1109 | std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(ToolChain, Args); |
| 1110 | |
| 1111 | if (RelocationModel != llvm::Reloc::Static) |
| 1112 | CmdArgs.push_back("-KPIC"); |
| 1113 | } |
| 1114 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1115 | /// Determine whether Objective-C automated reference counting is |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 1116 | /// enabled. |
| 1117 | bool tools::isObjCAutoRefCount(const ArgList &Args) { |
| 1118 | return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false); |
| 1119 | } |
| 1120 | |
| 1121 | static void AddLibgcc(const llvm::Triple &Triple, const Driver &D, |
| 1122 | ArgStringList &CmdArgs, const ArgList &Args) { |
| 1123 | bool isAndroid = Triple.isAndroid(); |
| 1124 | bool isCygMing = Triple.isOSCygMing(); |
| 1125 | bool IsIAMCU = Triple.isOSIAMCU(); |
| 1126 | bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) || |
| 1127 | Args.hasArg(options::OPT_static); |
| 1128 | if (!D.CCCIsCXX()) |
| 1129 | CmdArgs.push_back("-lgcc"); |
| 1130 | |
| 1131 | if (StaticLibgcc || isAndroid) { |
| 1132 | if (D.CCCIsCXX()) |
| 1133 | CmdArgs.push_back("-lgcc"); |
| 1134 | } else { |
| 1135 | if (!D.CCCIsCXX() && !isCygMing) |
| 1136 | CmdArgs.push_back("--as-needed"); |
| 1137 | CmdArgs.push_back("-lgcc_s"); |
| 1138 | if (!D.CCCIsCXX() && !isCygMing) |
| 1139 | CmdArgs.push_back("--no-as-needed"); |
| 1140 | } |
| 1141 | |
| 1142 | if (StaticLibgcc && !isAndroid && !IsIAMCU) |
| 1143 | CmdArgs.push_back("-lgcc_eh"); |
| 1144 | else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX()) |
| 1145 | CmdArgs.push_back("-lgcc"); |
| 1146 | |
| 1147 | // According to Android ABI, we have to link with libdl if we are |
| 1148 | // linking with non-static libgcc. |
| 1149 | // |
| 1150 | // NOTE: This fixes a link error on Android MIPS as well. The non-static |
| 1151 | // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl. |
| 1152 | if (isAndroid && !StaticLibgcc) |
| 1153 | CmdArgs.push_back("-ldl"); |
| 1154 | } |
| 1155 | |
| 1156 | void tools::AddRunTimeLibs(const ToolChain &TC, const Driver &D, |
| 1157 | ArgStringList &CmdArgs, const ArgList &Args) { |
| 1158 | // Make use of compiler-rt if --rtlib option is used |
| 1159 | ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args); |
| 1160 | |
| 1161 | switch (RLT) { |
| 1162 | case ToolChain::RLT_CompilerRT: |
Sam Clegg | a08631e | 2017-10-27 00:26:07 +0000 | [diff] [blame] | 1163 | CmdArgs.push_back(TC.getCompilerRTArgString(Args, "builtins")); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 1164 | break; |
| 1165 | case ToolChain::RLT_Libgcc: |
| 1166 | // Make sure libgcc is not used under MSVC environment by default |
| 1167 | if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { |
| 1168 | // Issue error diagnostic if libgcc is explicitly specified |
| 1169 | // through command line as --rtlib option argument. |
| 1170 | if (Args.hasArg(options::OPT_rtlib_EQ)) { |
| 1171 | TC.getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform) |
| 1172 | << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "MSVC"; |
| 1173 | } |
| 1174 | } else |
| 1175 | AddLibgcc(TC.getTriple(), D, CmdArgs, Args); |
| 1176 | break; |
| 1177 | } |
| 1178 | } |
Gheorghe-Teodor Bercea | 2c92693 | 2017-08-08 14:33:05 +0000 | [diff] [blame] | 1179 | |
| 1180 | /// Add OpenMP linker script arguments at the end of the argument list so that |
| 1181 | /// the fat binary is built by embedding each of the device images into the |
| 1182 | /// host. The linker script also defines a few symbols required by the code |
| 1183 | /// generation so that the images can be easily retrieved at runtime by the |
| 1184 | /// offloading library. This should be used only in tool chains that support |
| 1185 | /// linker scripts. |
| 1186 | void tools::AddOpenMPLinkerScript(const ToolChain &TC, Compilation &C, |
| 1187 | const InputInfo &Output, |
| 1188 | const InputInfoList &Inputs, |
| 1189 | const ArgList &Args, ArgStringList &CmdArgs, |
| 1190 | const JobAction &JA) { |
| 1191 | |
| 1192 | // If this is not an OpenMP host toolchain, we don't need to do anything. |
| 1193 | if (!JA.isHostOffloading(Action::OFK_OpenMP)) |
| 1194 | return; |
| 1195 | |
| 1196 | // Create temporary linker script. Keep it if save-temps is enabled. |
| 1197 | const char *LKS; |
| 1198 | SmallString<256> Name = llvm::sys::path::filename(Output.getFilename()); |
| 1199 | if (C.getDriver().isSaveTempsEnabled()) { |
| 1200 | llvm::sys::path::replace_extension(Name, "lk"); |
| 1201 | LKS = C.getArgs().MakeArgString(Name.c_str()); |
| 1202 | } else { |
| 1203 | llvm::sys::path::replace_extension(Name, ""); |
| 1204 | Name = C.getDriver().GetTemporaryPath(Name, "lk"); |
| 1205 | LKS = C.addTempFile(C.getArgs().MakeArgString(Name.c_str())); |
| 1206 | } |
| 1207 | |
| 1208 | // Add linker script option to the command. |
| 1209 | CmdArgs.push_back("-T"); |
| 1210 | CmdArgs.push_back(LKS); |
| 1211 | |
| 1212 | // Create a buffer to write the contents of the linker script. |
| 1213 | std::string LksBuffer; |
| 1214 | llvm::raw_string_ostream LksStream(LksBuffer); |
| 1215 | |
| 1216 | // Get the OpenMP offload tool chains so that we can extract the triple |
| 1217 | // associated with each device input. |
| 1218 | auto OpenMPToolChains = C.getOffloadToolChains<Action::OFK_OpenMP>(); |
| 1219 | assert(OpenMPToolChains.first != OpenMPToolChains.second && |
| 1220 | "No OpenMP toolchains??"); |
| 1221 | |
| 1222 | // Track the input file name and device triple in order to build the script, |
| 1223 | // inserting binaries in the designated sections. |
| 1224 | SmallVector<std::pair<std::string, const char *>, 8> InputBinaryInfo; |
| 1225 | |
| 1226 | // Add commands to embed target binaries. We ensure that each section and |
| 1227 | // image is 16-byte aligned. This is not mandatory, but increases the |
| 1228 | // likelihood of data to be aligned with a cache block in several main host |
| 1229 | // machines. |
| 1230 | LksStream << "/*\n"; |
| 1231 | LksStream << " OpenMP Offload Linker Script\n"; |
| 1232 | LksStream << " *** Automatically generated by Clang ***\n"; |
| 1233 | LksStream << "*/\n"; |
| 1234 | LksStream << "TARGET(binary)\n"; |
| 1235 | auto DTC = OpenMPToolChains.first; |
| 1236 | for (auto &II : Inputs) { |
| 1237 | const Action *A = II.getAction(); |
| 1238 | // Is this a device linking action? |
| 1239 | if (A && isa<LinkJobAction>(A) && |
| 1240 | A->isDeviceOffloading(Action::OFK_OpenMP)) { |
| 1241 | assert(DTC != OpenMPToolChains.second && |
| 1242 | "More device inputs than device toolchains??"); |
| 1243 | InputBinaryInfo.push_back(std::make_pair( |
| 1244 | DTC->second->getTriple().normalize(), II.getFilename())); |
| 1245 | ++DTC; |
| 1246 | LksStream << "INPUT(" << II.getFilename() << ")\n"; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | assert(DTC == OpenMPToolChains.second && |
| 1251 | "Less device inputs than device toolchains??"); |
| 1252 | |
| 1253 | LksStream << "SECTIONS\n"; |
| 1254 | LksStream << "{\n"; |
| 1255 | |
| 1256 | // Put each target binary into a separate section. |
| 1257 | for (const auto &BI : InputBinaryInfo) { |
| 1258 | LksStream << " .omp_offloading." << BI.first << " :\n"; |
| 1259 | LksStream << " ALIGN(0x10)\n"; |
| 1260 | LksStream << " {\n"; |
| 1261 | LksStream << " PROVIDE_HIDDEN(.omp_offloading.img_start." << BI.first |
| 1262 | << " = .);\n"; |
| 1263 | LksStream << " " << BI.second << "\n"; |
| 1264 | LksStream << " PROVIDE_HIDDEN(.omp_offloading.img_end." << BI.first |
| 1265 | << " = .);\n"; |
| 1266 | LksStream << " }\n"; |
| 1267 | } |
| 1268 | |
| 1269 | // Add commands to define host entries begin and end. We use 1-byte subalign |
| 1270 | // so that the linker does not add any padding and the elements in this |
| 1271 | // section form an array. |
| 1272 | LksStream << " .omp_offloading.entries :\n"; |
| 1273 | LksStream << " ALIGN(0x10)\n"; |
| 1274 | LksStream << " SUBALIGN(0x01)\n"; |
| 1275 | LksStream << " {\n"; |
| 1276 | LksStream << " PROVIDE_HIDDEN(.omp_offloading.entries_begin = .);\n"; |
| 1277 | LksStream << " *(.omp_offloading.entries)\n"; |
| 1278 | LksStream << " PROVIDE_HIDDEN(.omp_offloading.entries_end = .);\n"; |
| 1279 | LksStream << " }\n"; |
| 1280 | LksStream << "}\n"; |
| 1281 | LksStream << "INSERT BEFORE .data\n"; |
| 1282 | LksStream.flush(); |
| 1283 | |
| 1284 | // Dump the contents of the linker script if the user requested that. We |
| 1285 | // support this option to enable testing of behavior with -###. |
| 1286 | if (C.getArgs().hasArg(options::OPT_fopenmp_dump_offload_linker_script)) |
| 1287 | llvm::errs() << LksBuffer; |
| 1288 | |
| 1289 | // If this is a dry run, do not create the linker script file. |
| 1290 | if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) |
| 1291 | return; |
| 1292 | |
| 1293 | // Open script file and write the contents. |
| 1294 | std::error_code EC; |
| 1295 | llvm::raw_fd_ostream Lksf(LKS, EC, llvm::sys::fs::F_None); |
| 1296 | |
| 1297 | if (EC) { |
| 1298 | C.getDriver().Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 1299 | return; |
| 1300 | } |
| 1301 | |
| 1302 | Lksf << LksBuffer; |
| 1303 | } |
Florian Hahn | 2e081d1 | 2018-04-20 12:50:10 +0000 | [diff] [blame] | 1304 | |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 1305 | /// Add HIP linker script arguments at the end of the argument list so that |
| 1306 | /// the fat binary is built by embedding the device images into the host. The |
| 1307 | /// linker script also defines a symbol required by the code generation so that |
| 1308 | /// the image can be retrieved at runtime. This should be used only in tool |
| 1309 | /// chains that support linker scripts. |
| 1310 | void tools::AddHIPLinkerScript(const ToolChain &TC, Compilation &C, |
| 1311 | const InputInfo &Output, |
| 1312 | const InputInfoList &Inputs, const ArgList &Args, |
| 1313 | ArgStringList &CmdArgs, const JobAction &JA, |
| 1314 | const Tool &T) { |
| 1315 | |
| 1316 | // If this is not a HIP host toolchain, we don't need to do anything. |
| 1317 | if (!JA.isHostOffloading(Action::OFK_HIP)) |
| 1318 | return; |
| 1319 | |
| 1320 | // Create temporary linker script. Keep it if save-temps is enabled. |
| 1321 | const char *LKS; |
| 1322 | SmallString<256> Name = llvm::sys::path::filename(Output.getFilename()); |
| 1323 | if (C.getDriver().isSaveTempsEnabled()) { |
| 1324 | llvm::sys::path::replace_extension(Name, "lk"); |
| 1325 | LKS = C.getArgs().MakeArgString(Name.c_str()); |
| 1326 | } else { |
| 1327 | llvm::sys::path::replace_extension(Name, ""); |
| 1328 | Name = C.getDriver().GetTemporaryPath(Name, "lk"); |
| 1329 | LKS = C.addTempFile(C.getArgs().MakeArgString(Name.c_str())); |
| 1330 | } |
| 1331 | |
| 1332 | // Add linker script option to the command. |
| 1333 | CmdArgs.push_back("-T"); |
| 1334 | CmdArgs.push_back(LKS); |
| 1335 | |
| 1336 | // Create a buffer to write the contents of the linker script. |
| 1337 | std::string LksBuffer; |
| 1338 | llvm::raw_string_ostream LksStream(LksBuffer); |
| 1339 | |
| 1340 | // Get the HIP offload tool chain. |
| 1341 | auto *HIPTC = static_cast<const toolchains::CudaToolChain *>( |
| 1342 | C.getSingleOffloadToolChain<Action::OFK_HIP>()); |
| 1343 | assert(HIPTC->getTriple().getArch() == llvm::Triple::amdgcn && |
| 1344 | "Wrong platform"); |
Eric Liu | 7112fe6 | 2018-05-18 16:29:42 +0000 | [diff] [blame] | 1345 | (void)HIPTC; |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 1346 | |
| 1347 | // Construct clang-offload-bundler command to bundle object files for |
| 1348 | // for different GPU archs. |
| 1349 | ArgStringList BundlerArgs; |
| 1350 | BundlerArgs.push_back(Args.MakeArgString("-type=o")); |
| 1351 | |
| 1352 | // ToDo: Remove the dummy host binary entry which is required by |
| 1353 | // clang-offload-bundler. |
| 1354 | std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux"; |
| 1355 | std::string BundlerInputArg = "-inputs=/dev/null"; |
| 1356 | |
| 1357 | for (const auto &II : Inputs) { |
| 1358 | const Action *A = II.getAction(); |
| 1359 | // Is this a device linking action? |
| 1360 | if (A && isa<LinkJobAction>(A) && A->isDeviceOffloading(Action::OFK_HIP)) { |
| 1361 | BundlerTargetArg = BundlerTargetArg + ",hip-amdgcn-amd-amdhsa-" + |
| 1362 | StringRef(A->getOffloadingArch()).str(); |
| 1363 | BundlerInputArg = BundlerInputArg + "," + II.getFilename(); |
| 1364 | } |
| 1365 | } |
| 1366 | BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg)); |
| 1367 | BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg)); |
| 1368 | |
| 1369 | std::string BundleFileName = C.getDriver().GetTemporaryPath("BUNDLE", "o"); |
| 1370 | const char *BundleFile = |
| 1371 | C.addTempFile(C.getArgs().MakeArgString(BundleFileName.c_str())); |
| 1372 | auto BundlerOutputArg = |
| 1373 | Args.MakeArgString(std::string("-outputs=").append(BundleFile)); |
| 1374 | BundlerArgs.push_back(BundlerOutputArg); |
| 1375 | |
| 1376 | SmallString<128> BundlerPath(C.getDriver().Dir); |
| 1377 | llvm::sys::path::append(BundlerPath, "clang-offload-bundler"); |
| 1378 | const char *Bundler = Args.MakeArgString(BundlerPath); |
| 1379 | C.addCommand(llvm::make_unique<Command>(JA, T, Bundler, BundlerArgs, Inputs)); |
| 1380 | |
| 1381 | // Add commands to embed target binaries. We ensure that each section and |
| 1382 | // image is 16-byte aligned. This is not mandatory, but increases the |
| 1383 | // likelihood of data to be aligned with a cache block in several main host |
| 1384 | // machines. |
| 1385 | LksStream << "/*\n"; |
| 1386 | LksStream << " HIP Offload Linker Script\n"; |
| 1387 | LksStream << " *** Automatically generated by Clang ***\n"; |
| 1388 | LksStream << "*/\n"; |
| 1389 | LksStream << "TARGET(binary)\n"; |
| 1390 | LksStream << "INPUT(" << BundleFileName << ")\n"; |
| 1391 | LksStream << "SECTIONS\n"; |
| 1392 | LksStream << "{\n"; |
| 1393 | LksStream << " .hip_fatbin :\n"; |
| 1394 | LksStream << " ALIGN(0x10)\n"; |
| 1395 | LksStream << " {\n"; |
| 1396 | LksStream << " PROVIDE_HIDDEN(__hip_fatbin = .);\n"; |
| 1397 | LksStream << " " << BundleFileName << "\n"; |
| 1398 | LksStream << " }\n"; |
| 1399 | LksStream << "}\n"; |
| 1400 | LksStream << "INSERT BEFORE .data\n"; |
| 1401 | LksStream.flush(); |
| 1402 | |
| 1403 | // Dump the contents of the linker script if the user requested that. We |
| 1404 | // support this option to enable testing of behavior with -###. |
| 1405 | if (C.getArgs().hasArg(options::OPT_fhip_dump_offload_linker_script)) |
| 1406 | llvm::errs() << LksBuffer; |
| 1407 | |
| 1408 | // If this is a dry run, do not create the linker script file. |
| 1409 | if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) |
| 1410 | return; |
| 1411 | |
| 1412 | // Open script file and write the contents. |
| 1413 | std::error_code EC; |
| 1414 | llvm::raw_fd_ostream Lksf(LKS, EC, llvm::sys::fs::F_None); |
| 1415 | |
| 1416 | if (EC) { |
| 1417 | C.getDriver().Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 1418 | return; |
| 1419 | } |
| 1420 | |
| 1421 | Lksf << LksBuffer; |
| 1422 | } |
| 1423 | |
Florian Hahn | 2e081d1 | 2018-04-20 12:50:10 +0000 | [diff] [blame] | 1424 | SmallString<128> tools::getStatsFileName(const llvm::opt::ArgList &Args, |
| 1425 | const InputInfo &Output, |
| 1426 | const InputInfo &Input, |
| 1427 | const Driver &D) { |
| 1428 | const Arg *A = Args.getLastArg(options::OPT_save_stats_EQ); |
| 1429 | if (!A) |
| 1430 | return {}; |
| 1431 | |
| 1432 | StringRef SaveStats = A->getValue(); |
| 1433 | SmallString<128> StatsFile; |
| 1434 | if (SaveStats == "obj" && Output.isFilename()) { |
| 1435 | StatsFile.assign(Output.getFilename()); |
| 1436 | llvm::sys::path::remove_filename(StatsFile); |
| 1437 | } else if (SaveStats != "cwd") { |
| 1438 | D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << SaveStats; |
| 1439 | return {}; |
| 1440 | } |
| 1441 | |
| 1442 | StringRef BaseName = llvm::sys::path::filename(Input.getBaseInput()); |
| 1443 | llvm::sys::path::append(StatsFile, BaseName); |
| 1444 | llvm::sys::path::replace_extension(StatsFile, "stats"); |
| 1445 | return StatsFile; |
| 1446 | } |