Tools.cpp revision 1b8f12d46c10169bb949372ec5fc4c58afc2ced1
1//===--- Tools.cpp - Tools Implementations --------------------------------===// 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 "Tools.h" 11#include "InputInfo.h" 12#include "ToolChains.h" 13#include "clang/Basic/ObjCRuntime.h" 14#include "clang/Basic/Version.h" 15#include "clang/Driver/Action.h" 16#include "clang/Driver/Compilation.h" 17#include "clang/Driver/Driver.h" 18#include "clang/Driver/DriverDiagnostic.h" 19#include "clang/Driver/Job.h" 20#include "clang/Driver/Options.h" 21#include "clang/Driver/SanitizerArgs.h" 22#include "clang/Driver/ToolChain.h" 23#include "clang/Driver/Util.h" 24#include "llvm/ADT/SmallString.h" 25#include "llvm/ADT/StringSwitch.h" 26#include "llvm/ADT/Twine.h" 27#include "llvm/Option/Arg.h" 28#include "llvm/Option/ArgList.h" 29#include "llvm/Option/Option.h" 30#include "llvm/Support/ErrorHandling.h" 31#include "llvm/Support/FileSystem.h" 32#include "llvm/Support/Format.h" 33#include "llvm/Support/Host.h" 34#include "llvm/Support/Program.h" 35#include "llvm/Support/Process.h" 36#include "llvm/Support/raw_ostream.h" 37#include <sys/stat.h> 38 39using namespace clang::driver; 40using namespace clang::driver::tools; 41using namespace clang; 42using namespace llvm::opt; 43 44/// CheckPreprocessingOptions - Perform some validation of preprocessing 45/// arguments that is shared with gcc. 46static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) { 47 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC)) 48 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP()) 49 D.Diag(diag::err_drv_argument_only_allowed_with) 50 << A->getAsString(Args) << "-E"; 51} 52 53/// CheckCodeGenerationOptions - Perform some validation of code generation 54/// arguments that is shared with gcc. 55static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) { 56 // In gcc, only ARM checks this, but it seems reasonable to check universally. 57 if (Args.hasArg(options::OPT_static)) 58 if (const Arg *A = Args.getLastArg(options::OPT_dynamic, 59 options::OPT_mdynamic_no_pic)) 60 D.Diag(diag::err_drv_argument_not_allowed_with) 61 << A->getAsString(Args) << "-static"; 62} 63 64// Quote target names for inclusion in GNU Make dependency files. 65// Only the characters '$', '#', ' ', '\t' are quoted. 66static void QuoteTarget(StringRef Target, 67 SmallVectorImpl<char> &Res) { 68 for (unsigned i = 0, e = Target.size(); i != e; ++i) { 69 switch (Target[i]) { 70 case ' ': 71 case '\t': 72 // Escape the preceding backslashes 73 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j) 74 Res.push_back('\\'); 75 76 // Escape the space/tab 77 Res.push_back('\\'); 78 break; 79 case '$': 80 Res.push_back('$'); 81 break; 82 case '#': 83 Res.push_back('\\'); 84 break; 85 default: 86 break; 87 } 88 89 Res.push_back(Target[i]); 90 } 91} 92 93static void addDirectoryList(const ArgList &Args, 94 ArgStringList &CmdArgs, 95 const char *ArgName, 96 const char *EnvVar) { 97 const char *DirList = ::getenv(EnvVar); 98 bool CombinedArg = false; 99 100 if (!DirList) 101 return; // Nothing to do. 102 103 StringRef Name(ArgName); 104 if (Name.equals("-I") || Name.equals("-L")) 105 CombinedArg = true; 106 107 StringRef Dirs(DirList); 108 if (Dirs.empty()) // Empty string should not add '.'. 109 return; 110 111 StringRef::size_type Delim; 112 while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) { 113 if (Delim == 0) { // Leading colon. 114 if (CombinedArg) { 115 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + ".")); 116 } else { 117 CmdArgs.push_back(ArgName); 118 CmdArgs.push_back("."); 119 } 120 } else { 121 if (CombinedArg) { 122 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim))); 123 } else { 124 CmdArgs.push_back(ArgName); 125 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim))); 126 } 127 } 128 Dirs = Dirs.substr(Delim + 1); 129 } 130 131 if (Dirs.empty()) { // Trailing colon. 132 if (CombinedArg) { 133 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + ".")); 134 } else { 135 CmdArgs.push_back(ArgName); 136 CmdArgs.push_back("."); 137 } 138 } else { // Add the last path. 139 if (CombinedArg) { 140 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs)); 141 } else { 142 CmdArgs.push_back(ArgName); 143 CmdArgs.push_back(Args.MakeArgString(Dirs)); 144 } 145 } 146} 147 148static void AddLinkerInputs(const ToolChain &TC, 149 const InputInfoList &Inputs, const ArgList &Args, 150 ArgStringList &CmdArgs) { 151 const Driver &D = TC.getDriver(); 152 153 // Add extra linker input arguments which are not treated as inputs 154 // (constructed via -Xarch_). 155 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input); 156 157 for (InputInfoList::const_iterator 158 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 159 const InputInfo &II = *it; 160 161 if (!TC.HasNativeLLVMSupport()) { 162 // Don't try to pass LLVM inputs unless we have native support. 163 if (II.getType() == types::TY_LLVM_IR || 164 II.getType() == types::TY_LTO_IR || 165 II.getType() == types::TY_LLVM_BC || 166 II.getType() == types::TY_LTO_BC) 167 D.Diag(diag::err_drv_no_linker_llvm_support) 168 << TC.getTripleString(); 169 } 170 171 // Add filenames immediately. 172 if (II.isFilename()) { 173 CmdArgs.push_back(II.getFilename()); 174 continue; 175 } 176 177 // Otherwise, this is a linker input argument. 178 const Arg &A = II.getInputArg(); 179 180 // Handle reserved library options. 181 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) { 182 TC.AddCXXStdlibLibArgs(Args, CmdArgs); 183 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) { 184 TC.AddCCKextLibArgs(Args, CmdArgs); 185 } else 186 A.renderAsInput(Args, CmdArgs); 187 } 188 189 // LIBRARY_PATH - included following the user specified library paths. 190 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH"); 191} 192 193/// \brief Determine whether Objective-C automated reference counting is 194/// enabled. 195static bool isObjCAutoRefCount(const ArgList &Args) { 196 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false); 197} 198 199/// \brief Determine whether we are linking the ObjC runtime. 200static bool isObjCRuntimeLinked(const ArgList &Args) { 201 if (isObjCAutoRefCount(Args)) { 202 Args.ClaimAllArgs(options::OPT_fobjc_link_runtime); 203 return true; 204 } 205 return Args.hasArg(options::OPT_fobjc_link_runtime); 206} 207 208static void addProfileRT(const ToolChain &TC, const ArgList &Args, 209 ArgStringList &CmdArgs, 210 llvm::Triple Triple) { 211 if (!(Args.hasArg(options::OPT_fprofile_arcs) || 212 Args.hasArg(options::OPT_fprofile_generate) || 213 Args.hasArg(options::OPT_fcreate_profile) || 214 Args.hasArg(options::OPT_coverage))) 215 return; 216 217 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to 218 // the link line. We cannot do the same thing because unlike gcov there is a 219 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is 220 // not supported by old linkers. 221 std::string ProfileRT = 222 std::string(TC.getDriver().Dir) + "/../lib/libprofile_rt.a"; 223 224 CmdArgs.push_back(Args.MakeArgString(ProfileRT)); 225} 226 227static bool forwardToGCC(const Option &O) { 228 // Don't forward inputs from the original command line. They are added from 229 // InputInfoList. 230 return O.getKind() != Option::InputClass && 231 !O.hasFlag(options::DriverOption) && 232 !O.hasFlag(options::LinkerInput); 233} 234 235void Clang::AddPreprocessingOptions(Compilation &C, 236 const JobAction &JA, 237 const Driver &D, 238 const ArgList &Args, 239 ArgStringList &CmdArgs, 240 const InputInfo &Output, 241 const InputInfoList &Inputs) const { 242 Arg *A; 243 244 CheckPreprocessingOptions(D, Args); 245 246 Args.AddLastArg(CmdArgs, options::OPT_C); 247 Args.AddLastArg(CmdArgs, options::OPT_CC); 248 249 // Handle dependency file generation. 250 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) || 251 (A = Args.getLastArg(options::OPT_MD)) || 252 (A = Args.getLastArg(options::OPT_MMD))) { 253 // Determine the output location. 254 const char *DepFile; 255 if (Arg *MF = Args.getLastArg(options::OPT_MF)) { 256 DepFile = MF->getValue(); 257 C.addFailureResultFile(DepFile, &JA); 258 } else if (Output.getType() == types::TY_Dependencies) { 259 DepFile = Output.getFilename(); 260 } else if (A->getOption().matches(options::OPT_M) || 261 A->getOption().matches(options::OPT_MM)) { 262 DepFile = "-"; 263 } else { 264 DepFile = getDependencyFileName(Args, Inputs); 265 C.addFailureResultFile(DepFile, &JA); 266 } 267 CmdArgs.push_back("-dependency-file"); 268 CmdArgs.push_back(DepFile); 269 270 // Add a default target if one wasn't specified. 271 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) { 272 const char *DepTarget; 273 274 // If user provided -o, that is the dependency target, except 275 // when we are only generating a dependency file. 276 Arg *OutputOpt = Args.getLastArg(options::OPT_o); 277 if (OutputOpt && Output.getType() != types::TY_Dependencies) { 278 DepTarget = OutputOpt->getValue(); 279 } else { 280 // Otherwise derive from the base input. 281 // 282 // FIXME: This should use the computed output file location. 283 SmallString<128> P(Inputs[0].getBaseInput()); 284 llvm::sys::path::replace_extension(P, "o"); 285 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P)); 286 } 287 288 CmdArgs.push_back("-MT"); 289 SmallString<128> Quoted; 290 QuoteTarget(DepTarget, Quoted); 291 CmdArgs.push_back(Args.MakeArgString(Quoted)); 292 } 293 294 if (A->getOption().matches(options::OPT_M) || 295 A->getOption().matches(options::OPT_MD)) 296 CmdArgs.push_back("-sys-header-deps"); 297 } 298 299 if (Args.hasArg(options::OPT_MG)) { 300 if (!A || A->getOption().matches(options::OPT_MD) || 301 A->getOption().matches(options::OPT_MMD)) 302 D.Diag(diag::err_drv_mg_requires_m_or_mm); 303 CmdArgs.push_back("-MG"); 304 } 305 306 Args.AddLastArg(CmdArgs, options::OPT_MP); 307 308 // Convert all -MQ <target> args to -MT <quoted target> 309 for (arg_iterator it = Args.filtered_begin(options::OPT_MT, 310 options::OPT_MQ), 311 ie = Args.filtered_end(); it != ie; ++it) { 312 const Arg *A = *it; 313 A->claim(); 314 315 if (A->getOption().matches(options::OPT_MQ)) { 316 CmdArgs.push_back("-MT"); 317 SmallString<128> Quoted; 318 QuoteTarget(A->getValue(), Quoted); 319 CmdArgs.push_back(Args.MakeArgString(Quoted)); 320 321 // -MT flag - no change 322 } else { 323 A->render(Args, CmdArgs); 324 } 325 } 326 327 // Add -i* options, and automatically translate to 328 // -include-pch/-include-pth for transparent PCH support. It's 329 // wonky, but we include looking for .gch so we can support seamless 330 // replacement into a build system already set up to be generating 331 // .gch files. 332 bool RenderedImplicitInclude = false; 333 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group), 334 ie = Args.filtered_end(); it != ie; ++it) { 335 const Arg *A = it; 336 337 if (A->getOption().matches(options::OPT_include)) { 338 bool IsFirstImplicitInclude = !RenderedImplicitInclude; 339 RenderedImplicitInclude = true; 340 341 // Use PCH if the user requested it. 342 bool UsePCH = D.CCCUsePCH; 343 344 bool FoundPTH = false; 345 bool FoundPCH = false; 346 SmallString<128> P(A->getValue()); 347 // We want the files to have a name like foo.h.pch. Add a dummy extension 348 // so that replace_extension does the right thing. 349 P += ".dummy"; 350 if (UsePCH) { 351 llvm::sys::path::replace_extension(P, "pch"); 352 if (llvm::sys::fs::exists(P.str())) 353 FoundPCH = true; 354 } 355 356 if (!FoundPCH) { 357 llvm::sys::path::replace_extension(P, "pth"); 358 if (llvm::sys::fs::exists(P.str())) 359 FoundPTH = true; 360 } 361 362 if (!FoundPCH && !FoundPTH) { 363 llvm::sys::path::replace_extension(P, "gch"); 364 if (llvm::sys::fs::exists(P.str())) { 365 FoundPCH = UsePCH; 366 FoundPTH = !UsePCH; 367 } 368 } 369 370 if (FoundPCH || FoundPTH) { 371 if (IsFirstImplicitInclude) { 372 A->claim(); 373 if (UsePCH) 374 CmdArgs.push_back("-include-pch"); 375 else 376 CmdArgs.push_back("-include-pth"); 377 CmdArgs.push_back(Args.MakeArgString(P.str())); 378 continue; 379 } else { 380 // Ignore the PCH if not first on command line and emit warning. 381 D.Diag(diag::warn_drv_pch_not_first_include) 382 << P.str() << A->getAsString(Args); 383 } 384 } 385 } 386 387 // Not translated, render as usual. 388 A->claim(); 389 A->render(Args, CmdArgs); 390 } 391 392 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U); 393 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F, 394 options::OPT_index_header_map); 395 396 // Add -Wp, and -Xassembler if using the preprocessor. 397 398 // FIXME: There is a very unfortunate problem here, some troubled 399 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To 400 // really support that we would have to parse and then translate 401 // those options. :( 402 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, 403 options::OPT_Xpreprocessor); 404 405 // -I- is a deprecated GCC feature, reject it. 406 if (Arg *A = Args.getLastArg(options::OPT_I_)) 407 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args); 408 409 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an 410 // -isysroot to the CC1 invocation. 411 StringRef sysroot = C.getSysRoot(); 412 if (sysroot != "") { 413 if (!Args.hasArg(options::OPT_isysroot)) { 414 CmdArgs.push_back("-isysroot"); 415 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot)); 416 } 417 } 418 419 // Parse additional include paths from environment variables. 420 // FIXME: We should probably sink the logic for handling these from the 421 // frontend into the driver. It will allow deleting 4 otherwise unused flags. 422 // CPATH - included following the user specified includes (but prior to 423 // builtin and standard includes). 424 addDirectoryList(Args, CmdArgs, "-I", "CPATH"); 425 // C_INCLUDE_PATH - system includes enabled when compiling C. 426 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH"); 427 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++. 428 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH"); 429 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC. 430 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH"); 431 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++. 432 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH"); 433 434 // Add C++ include arguments, if needed. 435 if (types::isCXX(Inputs[0].getType())) 436 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs); 437 438 // Add system include arguments. 439 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs); 440} 441 442/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular 443/// CPU. 444// 445// FIXME: This is redundant with -mcpu, why does LLVM use this. 446// FIXME: tblgen this, or kill it! 447static const char *getLLVMArchSuffixForARM(StringRef CPU) { 448 return llvm::StringSwitch<const char *>(CPU) 449 .Case("strongarm", "v4") 450 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t") 451 .Cases("arm720t", "arm9", "arm9tdmi", "v4t") 452 .Cases("arm920", "arm920t", "arm922t", "v4t") 453 .Cases("arm940t", "ep9312","v4t") 454 .Cases("arm10tdmi", "arm1020t", "v5") 455 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e") 456 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e") 457 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e") 458 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6") 459 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6") 460 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2") 461 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7") 462 .Cases("cortex-a9", "cortex-a15", "v7") 463 .Case("cortex-r5", "v7r") 464 .Case("cortex-m0", "v6m") 465 .Case("cortex-m3", "v7m") 466 .Case("cortex-m4", "v7em") 467 .Case("cortex-a9-mp", "v7f") 468 .Case("swift", "v7s") 469 .Default(""); 470} 471 472/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting. 473// 474// FIXME: tblgen this. 475static std::string getARMTargetCPU(const ArgList &Args, 476 const llvm::Triple &Triple) { 477 // FIXME: Warn on inconsistent use of -mcpu and -march. 478 479 // If we have -mcpu=, use that. 480 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 481 StringRef MCPU = A->getValue(); 482 // Handle -mcpu=native. 483 if (MCPU == "native") 484 return llvm::sys::getHostCPUName(); 485 else 486 return MCPU; 487 } 488 489 StringRef MArch; 490 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 491 // Otherwise, if we have -march= choose the base CPU for that arch. 492 MArch = A->getValue(); 493 } else { 494 // Otherwise, use the Arch from the triple. 495 MArch = Triple.getArchName(); 496 } 497 498 // Handle -march=native. 499 std::string NativeMArch; 500 if (MArch == "native") { 501 std::string CPU = llvm::sys::getHostCPUName(); 502 if (CPU != "generic") { 503 // Translate the native cpu into the architecture. The switch below will 504 // then chose the minimum cpu for that arch. 505 NativeMArch = std::string("arm") + getLLVMArchSuffixForARM(CPU); 506 MArch = NativeMArch; 507 } 508 } 509 510 return llvm::StringSwitch<const char *>(MArch) 511 .Cases("armv2", "armv2a","arm2") 512 .Case("armv3", "arm6") 513 .Case("armv3m", "arm7m") 514 .Case("armv4", "strongarm") 515 .Case("armv4t", "arm7tdmi") 516 .Cases("armv5", "armv5t", "arm10tdmi") 517 .Cases("armv5e", "armv5te", "arm1022e") 518 .Case("armv5tej", "arm926ej-s") 519 .Cases("armv6", "armv6k", "arm1136jf-s") 520 .Case("armv6j", "arm1136j-s") 521 .Cases("armv6z", "armv6zk", "arm1176jzf-s") 522 .Case("armv6t2", "arm1156t2-s") 523 .Cases("armv6m", "armv6-m", "cortex-m0") 524 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8") 525 .Cases("armv7em", "armv7e-m", "cortex-m4") 526 .Cases("armv7f", "armv7-f", "cortex-a9-mp") 527 .Cases("armv7s", "armv7-s", "swift") 528 .Cases("armv7r", "armv7-r", "cortex-r4") 529 .Cases("armv7m", "armv7-m", "cortex-m3") 530 .Cases("armv8", "armv8a", "armv8-a", "cortex-a53") 531 .Case("ep9312", "ep9312") 532 .Case("iwmmxt", "iwmmxt") 533 .Case("xscale", "xscale") 534 // If all else failed, return the most base CPU with thumb interworking 535 // supported by LLVM. 536 .Default("arm7tdmi"); 537} 538 539// FIXME: Move to target hook. 540static bool isSignedCharDefault(const llvm::Triple &Triple) { 541 switch (Triple.getArch()) { 542 default: 543 return true; 544 545 case llvm::Triple::aarch64: 546 case llvm::Triple::arm: 547 case llvm::Triple::ppc: 548 case llvm::Triple::ppc64: 549 if (Triple.isOSDarwin()) 550 return true; 551 return false; 552 553 case llvm::Triple::ppc64le: 554 case llvm::Triple::systemz: 555 case llvm::Triple::xcore: 556 return false; 557 } 558} 559 560static bool isNoCommonDefault(const llvm::Triple &Triple) { 561 switch (Triple.getArch()) { 562 default: 563 return false; 564 565 case llvm::Triple::xcore: 566 return true; 567 } 568} 569 570// Handle -mfpu=. 571// 572// FIXME: Centralize feature selection, defaulting shouldn't be also in the 573// frontend target. 574static void addFPUArgs(const Driver &D, const Arg *A, const ArgList &Args, 575 ArgStringList &CmdArgs) { 576 StringRef FPU = A->getValue(); 577 578 // Set the target features based on the FPU. 579 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") { 580 // Disable any default FPU support. 581 CmdArgs.push_back("-target-feature"); 582 CmdArgs.push_back("-vfp2"); 583 CmdArgs.push_back("-target-feature"); 584 CmdArgs.push_back("-vfp3"); 585 CmdArgs.push_back("-target-feature"); 586 CmdArgs.push_back("-neon"); 587 } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") { 588 CmdArgs.push_back("-target-feature"); 589 CmdArgs.push_back("+vfp3"); 590 CmdArgs.push_back("-target-feature"); 591 CmdArgs.push_back("+d16"); 592 CmdArgs.push_back("-target-feature"); 593 CmdArgs.push_back("-neon"); 594 } else if (FPU == "vfp") { 595 CmdArgs.push_back("-target-feature"); 596 CmdArgs.push_back("+vfp2"); 597 CmdArgs.push_back("-target-feature"); 598 CmdArgs.push_back("-neon"); 599 } else if (FPU == "vfp3" || FPU == "vfpv3") { 600 CmdArgs.push_back("-target-feature"); 601 CmdArgs.push_back("+vfp3"); 602 CmdArgs.push_back("-target-feature"); 603 CmdArgs.push_back("-neon"); 604 } else if (FPU == "fp-armv8") { 605 CmdArgs.push_back("-target-feature"); 606 CmdArgs.push_back("+v8fp"); 607 } else if (FPU == "neon-fp-armv8") { 608 CmdArgs.push_back("-target-feature"); 609 CmdArgs.push_back("+v8fp"); 610 CmdArgs.push_back("-target-feature"); 611 CmdArgs.push_back("+neon"); 612 } else if (FPU == "neon") { 613 CmdArgs.push_back("-target-feature"); 614 CmdArgs.push_back("+neon"); 615 } else 616 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 617} 618 619// Handle -mfpmath=. 620static void addFPMathArgs(const Driver &D, const Arg *A, const ArgList &Args, 621 ArgStringList &CmdArgs, StringRef CPU) { 622 StringRef FPMath = A->getValue(); 623 624 // Set the target features based on the FPMath. 625 if (FPMath == "neon") { 626 CmdArgs.push_back("-target-feature"); 627 CmdArgs.push_back("+neonfp"); 628 629 if (CPU != "cortex-a5" && CPU != "cortex-a7" && 630 CPU != "cortex-a8" && CPU != "cortex-a9" && 631 CPU != "cortex-a9-mp" && CPU != "cortex-a15") 632 D.Diag(diag::err_drv_invalid_feature) << "-mfpmath=neon" << CPU; 633 634 } else if (FPMath == "vfp" || FPMath == "vfp2" || FPMath == "vfp3" || 635 FPMath == "vfp4") { 636 CmdArgs.push_back("-target-feature"); 637 CmdArgs.push_back("-neonfp"); 638 639 // FIXME: Add warnings when disabling a feature not present for a given CPU. 640 } else 641 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 642} 643 644// Select the float ABI as determined by -msoft-float, -mhard-float, and 645// -mfloat-abi=. 646static StringRef getARMFloatABI(const Driver &D, 647 const ArgList &Args, 648 const llvm::Triple &Triple) { 649 StringRef FloatABI; 650 if (Arg *A = Args.getLastArg(options::OPT_msoft_float, 651 options::OPT_mhard_float, 652 options::OPT_mfloat_abi_EQ)) { 653 if (A->getOption().matches(options::OPT_msoft_float)) 654 FloatABI = "soft"; 655 else if (A->getOption().matches(options::OPT_mhard_float)) 656 FloatABI = "hard"; 657 else { 658 FloatABI = A->getValue(); 659 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") { 660 D.Diag(diag::err_drv_invalid_mfloat_abi) 661 << A->getAsString(Args); 662 FloatABI = "soft"; 663 } 664 } 665 } 666 667 // If unspecified, choose the default based on the platform. 668 if (FloatABI.empty()) { 669 switch (Triple.getOS()) { 670 case llvm::Triple::Darwin: 671 case llvm::Triple::MacOSX: 672 case llvm::Triple::IOS: { 673 // Darwin defaults to "softfp" for v6 and v7. 674 // 675 // FIXME: Factor out an ARM class so we can cache the arch somewhere. 676 std::string ArchName = 677 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple)); 678 if (StringRef(ArchName).startswith("v6") || 679 StringRef(ArchName).startswith("v7")) 680 FloatABI = "softfp"; 681 else 682 FloatABI = "soft"; 683 break; 684 } 685 686 case llvm::Triple::FreeBSD: 687 // FreeBSD defaults to soft float 688 FloatABI = "soft"; 689 break; 690 691 default: 692 switch(Triple.getEnvironment()) { 693 case llvm::Triple::GNUEABIHF: 694 FloatABI = "hard"; 695 break; 696 case llvm::Triple::GNUEABI: 697 FloatABI = "softfp"; 698 break; 699 case llvm::Triple::EABI: 700 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp 701 FloatABI = "softfp"; 702 break; 703 case llvm::Triple::Android: { 704 std::string ArchName = 705 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple)); 706 if (StringRef(ArchName).startswith("v7")) 707 FloatABI = "softfp"; 708 else 709 FloatABI = "soft"; 710 break; 711 } 712 default: 713 // Assume "soft", but warn the user we are guessing. 714 FloatABI = "soft"; 715 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft"; 716 break; 717 } 718 } 719 } 720 721 return FloatABI; 722} 723 724 725void Clang::AddARMTargetArgs(const ArgList &Args, 726 ArgStringList &CmdArgs, 727 bool KernelOrKext) const { 728 const Driver &D = getToolChain().getDriver(); 729 // Get the effective triple, which takes into account the deployment target. 730 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args); 731 llvm::Triple Triple(TripleStr); 732 std::string CPUName = getARMTargetCPU(Args, Triple); 733 734 // Select the ABI to use. 735 // 736 // FIXME: Support -meabi. 737 const char *ABIName = 0; 738 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { 739 ABIName = A->getValue(); 740 } else if (Triple.isOSDarwin()) { 741 // The backend is hardwired to assume AAPCS for M-class processors, ensure 742 // the frontend matches that. 743 if (StringRef(CPUName).startswith("cortex-m")) { 744 ABIName = "aapcs"; 745 } else { 746 ABIName = "apcs-gnu"; 747 } 748 } else { 749 // Select the default based on the platform. 750 switch(Triple.getEnvironment()) { 751 case llvm::Triple::Android: 752 case llvm::Triple::GNUEABI: 753 case llvm::Triple::GNUEABIHF: 754 ABIName = "aapcs-linux"; 755 break; 756 case llvm::Triple::EABI: 757 ABIName = "aapcs"; 758 break; 759 default: 760 ABIName = "apcs-gnu"; 761 } 762 } 763 CmdArgs.push_back("-target-abi"); 764 CmdArgs.push_back(ABIName); 765 766 // Set the CPU based on -march= and -mcpu=. 767 CmdArgs.push_back("-target-cpu"); 768 CmdArgs.push_back(Args.MakeArgString(CPUName)); 769 770 // Determine floating point ABI from the options & target defaults. 771 StringRef FloatABI = getARMFloatABI(D, Args, Triple); 772 if (FloatABI == "soft") { 773 // Floating point operations and argument passing are soft. 774 // 775 // FIXME: This changes CPP defines, we need -target-soft-float. 776 CmdArgs.push_back("-msoft-float"); 777 CmdArgs.push_back("-mfloat-abi"); 778 CmdArgs.push_back("soft"); 779 } else if (FloatABI == "softfp") { 780 // Floating point operations are hard, but argument passing is soft. 781 CmdArgs.push_back("-mfloat-abi"); 782 CmdArgs.push_back("soft"); 783 } else { 784 // Floating point operations and argument passing are hard. 785 assert(FloatABI == "hard" && "Invalid float abi!"); 786 CmdArgs.push_back("-mfloat-abi"); 787 CmdArgs.push_back("hard"); 788 } 789 790 // Set appropriate target features for floating point mode. 791 // 792 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these 793 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is 794 // stripped out by the ARM target. 795 796 // Use software floating point operations? 797 if (FloatABI == "soft") { 798 CmdArgs.push_back("-target-feature"); 799 CmdArgs.push_back("+soft-float"); 800 } 801 802 // Use software floating point argument passing? 803 if (FloatABI != "hard") { 804 CmdArgs.push_back("-target-feature"); 805 CmdArgs.push_back("+soft-float-abi"); 806 } 807 808 // Honor -mfpu=. 809 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) 810 addFPUArgs(D, A, Args, CmdArgs); 811 812 // Honor -mfpmath=. 813 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) 814 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple)); 815 816 // Setting -msoft-float effectively disables NEON because of the GCC 817 // implementation, although the same isn't true of VFP or VFP3. 818 if (FloatABI == "soft") { 819 CmdArgs.push_back("-target-feature"); 820 CmdArgs.push_back("-neon"); 821 } 822 823 // Kernel code has more strict alignment requirements. 824 if (KernelOrKext) { 825 if (Triple.getOS() != llvm::Triple::IOS || Triple.isOSVersionLT(6)) { 826 CmdArgs.push_back("-backend-option"); 827 CmdArgs.push_back("-arm-long-calls"); 828 } 829 830 CmdArgs.push_back("-backend-option"); 831 CmdArgs.push_back("-arm-strict-align"); 832 833 // The kext linker doesn't know how to deal with movw/movt. 834 CmdArgs.push_back("-backend-option"); 835 CmdArgs.push_back("-arm-use-movt=0"); 836 } 837 838 // Setting -mno-global-merge disables the codegen global merge pass. Setting 839 // -mglobal-merge has no effect as the pass is enabled by default. 840 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 841 options::OPT_mno_global_merge)) { 842 if (A->getOption().matches(options::OPT_mno_global_merge)) 843 CmdArgs.push_back("-mno-global-merge"); 844 } 845 846 if (!Args.hasFlag(options::OPT_mimplicit_float, 847 options::OPT_mno_implicit_float, 848 true)) 849 CmdArgs.push_back("-no-implicit-float"); 850} 851 852// Translate MIPS CPU name alias option to CPU name. 853static StringRef getMipsCPUFromAlias(const Arg &A) { 854 if (A.getOption().matches(options::OPT_mips32)) 855 return "mips32"; 856 if (A.getOption().matches(options::OPT_mips32r2)) 857 return "mips32r2"; 858 if (A.getOption().matches(options::OPT_mips64)) 859 return "mips64"; 860 if (A.getOption().matches(options::OPT_mips64r2)) 861 return "mips64r2"; 862 llvm_unreachable("Unexpected option"); 863 return ""; 864} 865 866// Get CPU and ABI names. They are not independent 867// so we have to calculate them together. 868static void getMipsCPUAndABI(const ArgList &Args, 869 const ToolChain &TC, 870 StringRef &CPUName, 871 StringRef &ABIName) { 872 const char *DefMips32CPU = "mips32"; 873 const char *DefMips64CPU = "mips64"; 874 875 if (Arg *A = Args.getLastArg(options::OPT_march_EQ, 876 options::OPT_mcpu_EQ, 877 options::OPT_mips_CPUs_Group)) { 878 if (A->getOption().matches(options::OPT_mips_CPUs_Group)) 879 CPUName = getMipsCPUFromAlias(*A); 880 else 881 CPUName = A->getValue(); 882 } 883 884 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { 885 ABIName = A->getValue(); 886 // Convert a GNU style Mips ABI name to the name 887 // accepted by LLVM Mips backend. 888 ABIName = llvm::StringSwitch<llvm::StringRef>(ABIName) 889 .Case("32", "o32") 890 .Case("64", "n64") 891 .Default(ABIName); 892 } 893 894 // Setup default CPU and ABI names. 895 if (CPUName.empty() && ABIName.empty()) { 896 switch (TC.getArch()) { 897 default: 898 llvm_unreachable("Unexpected triple arch name"); 899 case llvm::Triple::mips: 900 case llvm::Triple::mipsel: 901 CPUName = DefMips32CPU; 902 break; 903 case llvm::Triple::mips64: 904 case llvm::Triple::mips64el: 905 CPUName = DefMips64CPU; 906 break; 907 } 908 } 909 910 if (!ABIName.empty()) { 911 // Deduce CPU name from ABI name. 912 CPUName = llvm::StringSwitch<const char *>(ABIName) 913 .Cases("32", "o32", "eabi", DefMips32CPU) 914 .Cases("n32", "n64", "64", DefMips64CPU) 915 .Default(""); 916 } 917 else if (!CPUName.empty()) { 918 // Deduce ABI name from CPU name. 919 ABIName = llvm::StringSwitch<const char *>(CPUName) 920 .Cases("mips32", "mips32r2", "o32") 921 .Cases("mips64", "mips64r2", "n64") 922 .Default(""); 923 } 924 925 // FIXME: Warn on inconsistent cpu and abi usage. 926} 927 928// Convert ABI name to the GNU tools acceptable variant. 929static StringRef getGnuCompatibleMipsABIName(StringRef ABI) { 930 return llvm::StringSwitch<llvm::StringRef>(ABI) 931 .Case("o32", "32") 932 .Case("n64", "64") 933 .Default(ABI); 934} 935 936// Select the MIPS float ABI as determined by -msoft-float, -mhard-float, 937// and -mfloat-abi=. 938static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) { 939 StringRef FloatABI; 940 if (Arg *A = Args.getLastArg(options::OPT_msoft_float, 941 options::OPT_mhard_float, 942 options::OPT_mfloat_abi_EQ)) { 943 if (A->getOption().matches(options::OPT_msoft_float)) 944 FloatABI = "soft"; 945 else if (A->getOption().matches(options::OPT_mhard_float)) 946 FloatABI = "hard"; 947 else { 948 FloatABI = A->getValue(); 949 if (FloatABI != "soft" && FloatABI != "hard") { 950 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args); 951 FloatABI = "hard"; 952 } 953 } 954 } 955 956 // If unspecified, choose the default based on the platform. 957 if (FloatABI.empty()) { 958 // Assume "hard", because it's a default value used by gcc. 959 // When we start to recognize specific target MIPS processors, 960 // we will be able to select the default more correctly. 961 FloatABI = "hard"; 962 } 963 964 return FloatABI; 965} 966 967static void AddTargetFeature(const ArgList &Args, 968 ArgStringList &CmdArgs, 969 OptSpecifier OnOpt, 970 OptSpecifier OffOpt, 971 StringRef FeatureName) { 972 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) { 973 CmdArgs.push_back("-target-feature"); 974 if (A->getOption().matches(OnOpt)) 975 CmdArgs.push_back(Args.MakeArgString("+" + FeatureName)); 976 else 977 CmdArgs.push_back(Args.MakeArgString("-" + FeatureName)); 978 } 979} 980 981void Clang::AddMIPSTargetArgs(const ArgList &Args, 982 ArgStringList &CmdArgs) const { 983 const Driver &D = getToolChain().getDriver(); 984 StringRef CPUName; 985 StringRef ABIName; 986 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName); 987 988 CmdArgs.push_back("-target-cpu"); 989 CmdArgs.push_back(CPUName.data()); 990 991 CmdArgs.push_back("-target-abi"); 992 CmdArgs.push_back(ABIName.data()); 993 994 StringRef FloatABI = getMipsFloatABI(D, Args); 995 996 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL; 997 998 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) { 999 // Floating point operations and argument passing are soft. 1000 CmdArgs.push_back("-msoft-float"); 1001 CmdArgs.push_back("-mfloat-abi"); 1002 CmdArgs.push_back("soft"); 1003 1004 // FIXME: Note, this is a hack. We need to pass the selected float 1005 // mode to the MipsTargetInfoBase to define appropriate macros there. 1006 // Now it is the only method. 1007 CmdArgs.push_back("-target-feature"); 1008 CmdArgs.push_back("+soft-float"); 1009 1010 if (FloatABI == "hard" && IsMips16) { 1011 CmdArgs.push_back("-mllvm"); 1012 CmdArgs.push_back("-mips16-hard-float"); 1013 } 1014 } 1015 else { 1016 // Floating point operations and argument passing are hard. 1017 assert(FloatABI == "hard" && "Invalid float abi!"); 1018 CmdArgs.push_back("-mfloat-abi"); 1019 CmdArgs.push_back("hard"); 1020 } 1021 1022 AddTargetFeature(Args, CmdArgs, 1023 options::OPT_msingle_float, options::OPT_mdouble_float, 1024 "single-float"); 1025 AddTargetFeature(Args, CmdArgs, 1026 options::OPT_mips16, options::OPT_mno_mips16, 1027 "mips16"); 1028 AddTargetFeature(Args, CmdArgs, 1029 options::OPT_mmicromips, options::OPT_mno_micromips, 1030 "micromips"); 1031 AddTargetFeature(Args, CmdArgs, 1032 options::OPT_mdsp, options::OPT_mno_dsp, 1033 "dsp"); 1034 AddTargetFeature(Args, CmdArgs, 1035 options::OPT_mdspr2, options::OPT_mno_dspr2, 1036 "dspr2"); 1037 AddTargetFeature(Args, CmdArgs, 1038 options::OPT_mmsa, options::OPT_mno_msa, 1039 "msa"); 1040 1041 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) { 1042 if (A->getOption().matches(options::OPT_mxgot)) { 1043 CmdArgs.push_back("-mllvm"); 1044 CmdArgs.push_back("-mxgot"); 1045 } 1046 } 1047 1048 if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1, 1049 options::OPT_mno_ldc1_sdc1)) { 1050 if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) { 1051 CmdArgs.push_back("-mllvm"); 1052 CmdArgs.push_back("-mno-ldc1-sdc1"); 1053 } 1054 } 1055 1056 if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division, 1057 options::OPT_mno_check_zero_division)) { 1058 if (A->getOption().matches(options::OPT_mno_check_zero_division)) { 1059 CmdArgs.push_back("-mllvm"); 1060 CmdArgs.push_back("-mno-check-zero-division"); 1061 } 1062 } 1063 1064 if (Arg *A = Args.getLastArg(options::OPT_G)) { 1065 StringRef v = A->getValue(); 1066 CmdArgs.push_back("-mllvm"); 1067 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v)); 1068 A->claim(); 1069 } 1070} 1071 1072/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting. 1073static std::string getPPCTargetCPU(const ArgList &Args) { 1074 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 1075 StringRef CPUName = A->getValue(); 1076 1077 if (CPUName == "native") { 1078 std::string CPU = llvm::sys::getHostCPUName(); 1079 if (!CPU.empty() && CPU != "generic") 1080 return CPU; 1081 else 1082 return ""; 1083 } 1084 1085 return llvm::StringSwitch<const char *>(CPUName) 1086 .Case("common", "generic") 1087 .Case("440", "440") 1088 .Case("440fp", "440") 1089 .Case("450", "450") 1090 .Case("601", "601") 1091 .Case("602", "602") 1092 .Case("603", "603") 1093 .Case("603e", "603e") 1094 .Case("603ev", "603ev") 1095 .Case("604", "604") 1096 .Case("604e", "604e") 1097 .Case("620", "620") 1098 .Case("630", "pwr3") 1099 .Case("G3", "g3") 1100 .Case("7400", "7400") 1101 .Case("G4", "g4") 1102 .Case("7450", "7450") 1103 .Case("G4+", "g4+") 1104 .Case("750", "750") 1105 .Case("970", "970") 1106 .Case("G5", "g5") 1107 .Case("a2", "a2") 1108 .Case("a2q", "a2q") 1109 .Case("e500mc", "e500mc") 1110 .Case("e5500", "e5500") 1111 .Case("power3", "pwr3") 1112 .Case("power4", "pwr4") 1113 .Case("power5", "pwr5") 1114 .Case("power5x", "pwr5x") 1115 .Case("power6", "pwr6") 1116 .Case("power6x", "pwr6x") 1117 .Case("power7", "pwr7") 1118 .Case("pwr3", "pwr3") 1119 .Case("pwr4", "pwr4") 1120 .Case("pwr5", "pwr5") 1121 .Case("pwr5x", "pwr5x") 1122 .Case("pwr6", "pwr6") 1123 .Case("pwr6x", "pwr6x") 1124 .Case("pwr7", "pwr7") 1125 .Case("powerpc", "ppc") 1126 .Case("powerpc64", "ppc64") 1127 .Case("powerpc64le", "ppc64le") 1128 .Default(""); 1129 } 1130 1131 return ""; 1132} 1133 1134void Clang::AddPPCTargetArgs(const ArgList &Args, 1135 ArgStringList &CmdArgs) const { 1136 std::string TargetCPUName = getPPCTargetCPU(Args); 1137 1138 // LLVM may default to generating code for the native CPU, 1139 // but, like gcc, we default to a more generic option for 1140 // each architecture. (except on Darwin) 1141 llvm::Triple Triple = getToolChain().getTriple(); 1142 if (TargetCPUName.empty() && !Triple.isOSDarwin()) { 1143 if (Triple.getArch() == llvm::Triple::ppc64) 1144 TargetCPUName = "ppc64"; 1145 else if (Triple.getArch() == llvm::Triple::ppc64le) 1146 TargetCPUName = "ppc64le"; 1147 else 1148 TargetCPUName = "ppc"; 1149 } 1150 1151 if (!TargetCPUName.empty()) { 1152 CmdArgs.push_back("-target-cpu"); 1153 CmdArgs.push_back(Args.MakeArgString(TargetCPUName.c_str())); 1154 } 1155 1156 // Allow override of the Altivec feature. 1157 AddTargetFeature(Args, CmdArgs, 1158 options::OPT_faltivec, options::OPT_fno_altivec, 1159 "altivec"); 1160 1161 AddTargetFeature(Args, CmdArgs, 1162 options::OPT_mfprnd, options::OPT_mno_fprnd, 1163 "fprnd"); 1164 1165 // Note that gcc calls this mfcrf and LLVM calls this mfocrf. 1166 AddTargetFeature(Args, CmdArgs, 1167 options::OPT_mmfcrf, options::OPT_mno_mfcrf, 1168 "mfocrf"); 1169 1170 AddTargetFeature(Args, CmdArgs, 1171 options::OPT_mpopcntd, options::OPT_mno_popcntd, 1172 "popcntd"); 1173 1174 // It is really only possible to turn qpx off because turning qpx on is tied 1175 // to using the a2q CPU. 1176 if (Args.hasFlag(options::OPT_mno_qpx, options::OPT_mqpx, false)) { 1177 CmdArgs.push_back("-target-feature"); 1178 CmdArgs.push_back("-qpx"); 1179 } 1180} 1181 1182/// Get the (LLVM) name of the R600 gpu we are targeting. 1183static std::string getR600TargetGPU(const ArgList &Args) { 1184 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 1185 std::string GPUName = A->getValue(); 1186 return llvm::StringSwitch<const char *>(GPUName) 1187 .Cases("rv630", "rv635", "r600") 1188 .Cases("rv610", "rv620", "rs780", "rs880") 1189 .Case("rv740", "rv770") 1190 .Case("palm", "cedar") 1191 .Cases("sumo", "sumo2", "sumo") 1192 .Case("hemlock", "cypress") 1193 .Case("aruba", "cayman") 1194 .Default(GPUName.c_str()); 1195 } 1196 return ""; 1197} 1198 1199void Clang::AddR600TargetArgs(const ArgList &Args, 1200 ArgStringList &CmdArgs) const { 1201 std::string TargetGPUName = getR600TargetGPU(Args); 1202 CmdArgs.push_back("-target-cpu"); 1203 CmdArgs.push_back(Args.MakeArgString(TargetGPUName.c_str())); 1204} 1205 1206void Clang::AddSparcTargetArgs(const ArgList &Args, 1207 ArgStringList &CmdArgs) const { 1208 const Driver &D = getToolChain().getDriver(); 1209 1210 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 1211 CmdArgs.push_back("-target-cpu"); 1212 CmdArgs.push_back(A->getValue()); 1213 } 1214 1215 // Select the float ABI as determined by -msoft-float, -mhard-float, and 1216 StringRef FloatABI; 1217 if (Arg *A = Args.getLastArg(options::OPT_msoft_float, 1218 options::OPT_mhard_float)) { 1219 if (A->getOption().matches(options::OPT_msoft_float)) 1220 FloatABI = "soft"; 1221 else if (A->getOption().matches(options::OPT_mhard_float)) 1222 FloatABI = "hard"; 1223 } 1224 1225 // If unspecified, choose the default based on the platform. 1226 if (FloatABI.empty()) { 1227 // Assume "soft", but warn the user we are guessing. 1228 FloatABI = "soft"; 1229 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft"; 1230 } 1231 1232 if (FloatABI == "soft") { 1233 // Floating point operations and argument passing are soft. 1234 // 1235 // FIXME: This changes CPP defines, we need -target-soft-float. 1236 CmdArgs.push_back("-msoft-float"); 1237 CmdArgs.push_back("-target-feature"); 1238 CmdArgs.push_back("+soft-float"); 1239 } else { 1240 assert(FloatABI == "hard" && "Invalid float abi!"); 1241 CmdArgs.push_back("-mhard-float"); 1242 } 1243} 1244 1245static const char *getSystemZTargetCPU(const ArgList &Args) { 1246 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 1247 return A->getValue(); 1248 return "z10"; 1249} 1250 1251void Clang::AddSystemZTargetArgs(const ArgList &Args, 1252 ArgStringList &CmdArgs) const { 1253 const char *CPUName = getSystemZTargetCPU(Args); 1254 CmdArgs.push_back("-target-cpu"); 1255 CmdArgs.push_back(CPUName); 1256} 1257 1258static const char *getX86TargetCPU(const ArgList &Args, 1259 const llvm::Triple &Triple) { 1260 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 1261 if (StringRef(A->getValue()) != "native") 1262 return A->getValue(); 1263 1264 // FIXME: Reject attempts to use -march=native unless the target matches 1265 // the host. 1266 // 1267 // FIXME: We should also incorporate the detected target features for use 1268 // with -native. 1269 std::string CPU = llvm::sys::getHostCPUName(); 1270 if (!CPU.empty() && CPU != "generic") 1271 return Args.MakeArgString(CPU); 1272 } 1273 1274 // Select the default CPU if none was given (or detection failed). 1275 1276 if (Triple.getArch() != llvm::Triple::x86_64 && 1277 Triple.getArch() != llvm::Triple::x86) 1278 return 0; // This routine is only handling x86 targets. 1279 1280 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64; 1281 1282 // FIXME: Need target hooks. 1283 if (Triple.isOSDarwin()) 1284 return Is64Bit ? "core2" : "yonah"; 1285 1286 // Everything else goes to x86-64 in 64-bit mode. 1287 if (Is64Bit) 1288 return "x86-64"; 1289 1290 if (Triple.getOSName().startswith("haiku")) 1291 return "i586"; 1292 if (Triple.getOSName().startswith("openbsd")) 1293 return "i486"; 1294 if (Triple.getOSName().startswith("bitrig")) 1295 return "i686"; 1296 if (Triple.getOSName().startswith("freebsd")) 1297 return "i486"; 1298 if (Triple.getOSName().startswith("netbsd")) 1299 return "i486"; 1300 // All x86 devices running Android have core2 as their common 1301 // denominator. This makes a better choice than pentium4. 1302 if (Triple.getEnvironment() == llvm::Triple::Android) 1303 return "core2"; 1304 1305 // Fallback to p4. 1306 return "pentium4"; 1307} 1308 1309void Clang::AddX86TargetArgs(const ArgList &Args, 1310 ArgStringList &CmdArgs) const { 1311 if (!Args.hasFlag(options::OPT_mred_zone, 1312 options::OPT_mno_red_zone, 1313 true) || 1314 Args.hasArg(options::OPT_mkernel) || 1315 Args.hasArg(options::OPT_fapple_kext)) 1316 CmdArgs.push_back("-disable-red-zone"); 1317 1318 // Default to avoid implicit floating-point for kernel/kext code, but allow 1319 // that to be overridden with -mno-soft-float. 1320 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) || 1321 Args.hasArg(options::OPT_fapple_kext)); 1322 if (Arg *A = Args.getLastArg(options::OPT_msoft_float, 1323 options::OPT_mno_soft_float, 1324 options::OPT_mimplicit_float, 1325 options::OPT_mno_implicit_float)) { 1326 const Option &O = A->getOption(); 1327 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) || 1328 O.matches(options::OPT_msoft_float)); 1329 } 1330 if (NoImplicitFloat) 1331 CmdArgs.push_back("-no-implicit-float"); 1332 1333 if (const char *CPUName = getX86TargetCPU(Args, getToolChain().getTriple())) { 1334 CmdArgs.push_back("-target-cpu"); 1335 CmdArgs.push_back(CPUName); 1336 } 1337 1338 // The required algorithm here is slightly strange: the options are applied 1339 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets 1340 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse" 1341 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the 1342 // former correctly, but not the latter; handle directly-overridden 1343 // attributes here. 1344 llvm::StringMap<unsigned> PrevFeature; 1345 std::vector<const char*> Features; 1346 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group), 1347 ie = Args.filtered_end(); it != ie; ++it) { 1348 StringRef Name = (*it)->getOption().getName(); 1349 (*it)->claim(); 1350 1351 // Skip over "-m". 1352 assert(Name.startswith("m") && "Invalid feature name."); 1353 Name = Name.substr(1); 1354 1355 bool IsNegative = Name.startswith("no-"); 1356 if (IsNegative) 1357 Name = Name.substr(3); 1358 1359 unsigned& Prev = PrevFeature[Name]; 1360 if (Prev) 1361 Features[Prev - 1] = 0; 1362 Prev = Features.size() + 1; 1363 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name)); 1364 } 1365 for (unsigned i = 0; i < Features.size(); i++) { 1366 if (Features[i]) { 1367 CmdArgs.push_back("-target-feature"); 1368 CmdArgs.push_back(Features[i]); 1369 } 1370 } 1371} 1372 1373static inline bool HasPICArg(const ArgList &Args) { 1374 return Args.hasArg(options::OPT_fPIC) 1375 || Args.hasArg(options::OPT_fpic); 1376} 1377 1378static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) { 1379 return Args.getLastArg(options::OPT_G, 1380 options::OPT_G_EQ, 1381 options::OPT_msmall_data_threshold_EQ); 1382} 1383 1384static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) { 1385 std::string value; 1386 if (HasPICArg(Args)) 1387 value = "0"; 1388 else if (Arg *A = GetLastSmallDataThresholdArg(Args)) { 1389 value = A->getValue(); 1390 A->claim(); 1391 } 1392 return value; 1393} 1394 1395void Clang::AddHexagonTargetArgs(const ArgList &Args, 1396 ArgStringList &CmdArgs) const { 1397 llvm::Triple Triple = getToolChain().getTriple(); 1398 1399 CmdArgs.push_back("-target-cpu"); 1400 CmdArgs.push_back(Args.MakeArgString( 1401 "hexagon" 1402 + toolchains::Hexagon_TC::GetTargetCPU(Args))); 1403 CmdArgs.push_back("-fno-signed-char"); 1404 CmdArgs.push_back("-mqdsp6-compat"); 1405 CmdArgs.push_back("-Wreturn-type"); 1406 1407 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args); 1408 if (!SmallDataThreshold.empty()) { 1409 CmdArgs.push_back ("-mllvm"); 1410 CmdArgs.push_back(Args.MakeArgString( 1411 "-hexagon-small-data-threshold=" + SmallDataThreshold)); 1412 } 1413 1414 if (!Args.hasArg(options::OPT_fno_short_enums)) 1415 CmdArgs.push_back("-fshort-enums"); 1416 if (Args.getLastArg(options::OPT_mieee_rnd_near)) { 1417 CmdArgs.push_back ("-mllvm"); 1418 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near"); 1419 } 1420 CmdArgs.push_back ("-mllvm"); 1421 CmdArgs.push_back ("-machine-sink-split=0"); 1422} 1423 1424void Clang::AddAArch64TargetArgs(const ArgList &Args, 1425 ArgStringList &CmdArgs) const { 1426 const Driver &D = getToolChain().getDriver(); 1427 // Honor -mfpu=. 1428 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) 1429 addFPUArgs(D, A, Args, CmdArgs); 1430} 1431 1432static bool 1433shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, 1434 const llvm::Triple &Triple) { 1435 // We use the zero-cost exception tables for Objective-C if the non-fragile 1436 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and 1437 // later. 1438 if (runtime.isNonFragile()) 1439 return true; 1440 1441 if (!Triple.isOSDarwin()) 1442 return false; 1443 1444 return (!Triple.isMacOSXVersionLT(10,5) && 1445 (Triple.getArch() == llvm::Triple::x86_64 || 1446 Triple.getArch() == llvm::Triple::arm)); 1447} 1448 1449/// addExceptionArgs - Adds exception related arguments to the driver command 1450/// arguments. There's a master flag, -fexceptions and also language specific 1451/// flags to enable/disable C++ and Objective-C exceptions. 1452/// This makes it possible to for example disable C++ exceptions but enable 1453/// Objective-C exceptions. 1454static void addExceptionArgs(const ArgList &Args, types::ID InputType, 1455 const llvm::Triple &Triple, 1456 bool KernelOrKext, 1457 const ObjCRuntime &objcRuntime, 1458 ArgStringList &CmdArgs) { 1459 if (KernelOrKext) { 1460 // -mkernel and -fapple-kext imply no exceptions, so claim exception related 1461 // arguments now to avoid warnings about unused arguments. 1462 Args.ClaimAllArgs(options::OPT_fexceptions); 1463 Args.ClaimAllArgs(options::OPT_fno_exceptions); 1464 Args.ClaimAllArgs(options::OPT_fobjc_exceptions); 1465 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions); 1466 Args.ClaimAllArgs(options::OPT_fcxx_exceptions); 1467 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions); 1468 return; 1469 } 1470 1471 // Exceptions are enabled by default. 1472 bool ExceptionsEnabled = true; 1473 1474 // This keeps track of whether exceptions were explicitly turned on or off. 1475 bool DidHaveExplicitExceptionFlag = false; 1476 1477 if (Arg *A = Args.getLastArg(options::OPT_fexceptions, 1478 options::OPT_fno_exceptions)) { 1479 if (A->getOption().matches(options::OPT_fexceptions)) 1480 ExceptionsEnabled = true; 1481 else 1482 ExceptionsEnabled = false; 1483 1484 DidHaveExplicitExceptionFlag = true; 1485 } 1486 1487 bool ShouldUseExceptionTables = false; 1488 1489 // Exception tables and cleanups can be enabled with -fexceptions even if the 1490 // language itself doesn't support exceptions. 1491 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag) 1492 ShouldUseExceptionTables = true; 1493 1494 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This 1495 // is not necessarily sensible, but follows GCC. 1496 if (types::isObjC(InputType) && 1497 Args.hasFlag(options::OPT_fobjc_exceptions, 1498 options::OPT_fno_objc_exceptions, 1499 true)) { 1500 CmdArgs.push_back("-fobjc-exceptions"); 1501 1502 ShouldUseExceptionTables |= 1503 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple); 1504 } 1505 1506 if (types::isCXX(InputType)) { 1507 bool CXXExceptionsEnabled = ExceptionsEnabled; 1508 1509 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions, 1510 options::OPT_fno_cxx_exceptions, 1511 options::OPT_fexceptions, 1512 options::OPT_fno_exceptions)) { 1513 if (A->getOption().matches(options::OPT_fcxx_exceptions)) 1514 CXXExceptionsEnabled = true; 1515 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions)) 1516 CXXExceptionsEnabled = false; 1517 } 1518 1519 if (CXXExceptionsEnabled) { 1520 CmdArgs.push_back("-fcxx-exceptions"); 1521 1522 ShouldUseExceptionTables = true; 1523 } 1524 } 1525 1526 if (ShouldUseExceptionTables) 1527 CmdArgs.push_back("-fexceptions"); 1528} 1529 1530static bool ShouldDisableAutolink(const ArgList &Args, 1531 const ToolChain &TC) { 1532 bool Default = true; 1533 if (TC.getTriple().isOSDarwin()) { 1534 // The native darwin assembler doesn't support the linker_option directives, 1535 // so we disable them if we think the .s file will be passed to it. 1536 Default = TC.useIntegratedAs(); 1537 } 1538 return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink, 1539 Default); 1540} 1541 1542static bool ShouldDisableCFI(const ArgList &Args, 1543 const ToolChain &TC) { 1544 bool Default = true; 1545 if (TC.getTriple().isOSDarwin()) { 1546 // The native darwin assembler doesn't support cfi directives, so 1547 // we disable them if we think the .s file will be passed to it. 1548 Default = TC.useIntegratedAs(); 1549 } 1550 return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm, 1551 options::OPT_fno_dwarf2_cfi_asm, 1552 Default); 1553} 1554 1555static bool ShouldDisableDwarfDirectory(const ArgList &Args, 1556 const ToolChain &TC) { 1557 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm, 1558 options::OPT_fno_dwarf_directory_asm, 1559 TC.useIntegratedAs()); 1560 return !UseDwarfDirectory; 1561} 1562 1563/// \brief Check whether the given input tree contains any compilation actions. 1564static bool ContainsCompileAction(const Action *A) { 1565 if (isa<CompileJobAction>(A)) 1566 return true; 1567 1568 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it) 1569 if (ContainsCompileAction(*it)) 1570 return true; 1571 1572 return false; 1573} 1574 1575/// \brief Check if -relax-all should be passed to the internal assembler. 1576/// This is done by default when compiling non-assembler source with -O0. 1577static bool UseRelaxAll(Compilation &C, const ArgList &Args) { 1578 bool RelaxDefault = true; 1579 1580 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 1581 RelaxDefault = A->getOption().matches(options::OPT_O0); 1582 1583 if (RelaxDefault) { 1584 RelaxDefault = false; 1585 for (ActionList::const_iterator it = C.getActions().begin(), 1586 ie = C.getActions().end(); it != ie; ++it) { 1587 if (ContainsCompileAction(*it)) { 1588 RelaxDefault = true; 1589 break; 1590 } 1591 } 1592 } 1593 1594 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all, 1595 RelaxDefault); 1596} 1597 1598static void CollectArgsForIntegratedAssembler(Compilation &C, 1599 const ArgList &Args, 1600 ArgStringList &CmdArgs, 1601 const Driver &D) { 1602 if (UseRelaxAll(C, Args)) 1603 CmdArgs.push_back("-mrelax-all"); 1604 1605 // When using an integrated assembler, translate -Wa, and -Xassembler 1606 // options. 1607 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA, 1608 options::OPT_Xassembler), 1609 ie = Args.filtered_end(); it != ie; ++it) { 1610 const Arg *A = *it; 1611 A->claim(); 1612 1613 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) { 1614 StringRef Value = A->getValue(i); 1615 1616 if (Value == "-force_cpusubtype_ALL") { 1617 // Do nothing, this is the default and we don't support anything else. 1618 } else if (Value == "-L") { 1619 CmdArgs.push_back("-msave-temp-labels"); 1620 } else if (Value == "--fatal-warnings") { 1621 CmdArgs.push_back("-mllvm"); 1622 CmdArgs.push_back("-fatal-assembler-warnings"); 1623 } else if (Value == "--noexecstack") { 1624 CmdArgs.push_back("-mnoexecstack"); 1625 } else { 1626 D.Diag(diag::err_drv_unsupported_option_argument) 1627 << A->getOption().getName() << Value; 1628 } 1629 } 1630 } 1631} 1632 1633static void addProfileRTLinux( 1634 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) { 1635 if (!(Args.hasArg(options::OPT_fprofile_arcs) || 1636 Args.hasArg(options::OPT_fprofile_generate) || 1637 Args.hasArg(options::OPT_fcreate_profile) || 1638 Args.hasArg(options::OPT_coverage))) 1639 return; 1640 1641 // The profile runtime is located in the Linux library directory and has name 1642 // "libclang_rt.profile-<ArchName>.a". 1643 SmallString<128> LibProfile(TC.getDriver().ResourceDir); 1644 llvm::sys::path::append( 1645 LibProfile, "lib", "linux", 1646 Twine("libclang_rt.profile-") + TC.getArchName() + ".a"); 1647 1648 CmdArgs.push_back(Args.MakeArgString(LibProfile)); 1649} 1650 1651static void addSanitizerRTLinkFlagsLinux( 1652 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs, 1653 const StringRef Sanitizer, bool BeforeLibStdCXX, 1654 bool ExportSymbols = true) { 1655 // Sanitizer runtime is located in the Linux library directory and 1656 // has name "libclang_rt.<Sanitizer>-<ArchName>.a". 1657 SmallString<128> LibSanitizer(TC.getDriver().ResourceDir); 1658 llvm::sys::path::append( 1659 LibSanitizer, "lib", "linux", 1660 (Twine("libclang_rt.") + Sanitizer + "-" + TC.getArchName() + ".a")); 1661 1662 // Sanitizer runtime may need to come before -lstdc++ (or -lc++, libstdc++.a, 1663 // etc.) so that the linker picks custom versions of the global 'operator 1664 // new' and 'operator delete' symbols. We take the extreme (but simple) 1665 // strategy of inserting it at the front of the link command. It also 1666 // needs to be forced to end up in the executable, so wrap it in 1667 // whole-archive. 1668 SmallVector<const char *, 3> LibSanitizerArgs; 1669 LibSanitizerArgs.push_back("-whole-archive"); 1670 LibSanitizerArgs.push_back(Args.MakeArgString(LibSanitizer)); 1671 LibSanitizerArgs.push_back("-no-whole-archive"); 1672 1673 CmdArgs.insert(BeforeLibStdCXX ? CmdArgs.begin() : CmdArgs.end(), 1674 LibSanitizerArgs.begin(), LibSanitizerArgs.end()); 1675 1676 CmdArgs.push_back("-lpthread"); 1677 CmdArgs.push_back("-lrt"); 1678 CmdArgs.push_back("-ldl"); 1679 1680 // If possible, use a dynamic symbols file to export the symbols from the 1681 // runtime library. If we can't do so, use -export-dynamic instead to export 1682 // all symbols from the binary. 1683 if (ExportSymbols) { 1684 if (llvm::sys::fs::exists(LibSanitizer + ".syms")) 1685 CmdArgs.push_back( 1686 Args.MakeArgString("--dynamic-list=" + LibSanitizer + ".syms")); 1687 else 1688 CmdArgs.push_back("-export-dynamic"); 1689 } 1690} 1691 1692/// If AddressSanitizer is enabled, add appropriate linker flags (Linux). 1693/// This needs to be called before we add the C run-time (malloc, etc). 1694static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args, 1695 ArgStringList &CmdArgs) { 1696 if(TC.getTriple().getEnvironment() == llvm::Triple::Android) { 1697 SmallString<128> LibAsan(TC.getDriver().ResourceDir); 1698 llvm::sys::path::append(LibAsan, "lib", "linux", 1699 (Twine("libclang_rt.asan-") + 1700 TC.getArchName() + "-android.so")); 1701 CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibAsan)); 1702 } else { 1703 if (!Args.hasArg(options::OPT_shared)) 1704 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "asan", true); 1705 } 1706} 1707 1708/// If ThreadSanitizer is enabled, add appropriate linker flags (Linux). 1709/// This needs to be called before we add the C run-time (malloc, etc). 1710static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args, 1711 ArgStringList &CmdArgs) { 1712 if (!Args.hasArg(options::OPT_shared)) 1713 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "tsan", true); 1714} 1715 1716/// If MemorySanitizer is enabled, add appropriate linker flags (Linux). 1717/// This needs to be called before we add the C run-time (malloc, etc). 1718static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args, 1719 ArgStringList &CmdArgs) { 1720 if (!Args.hasArg(options::OPT_shared)) 1721 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "msan", true); 1722} 1723 1724/// If LeakSanitizer is enabled, add appropriate linker flags (Linux). 1725/// This needs to be called before we add the C run-time (malloc, etc). 1726static void addLsanRTLinux(const ToolChain &TC, const ArgList &Args, 1727 ArgStringList &CmdArgs) { 1728 if (!Args.hasArg(options::OPT_shared)) 1729 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "lsan", true); 1730} 1731 1732/// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags 1733/// (Linux). 1734static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args, 1735 ArgStringList &CmdArgs, bool IsCXX, 1736 bool HasOtherSanitizerRt) { 1737 // Need a copy of sanitizer_common. This could come from another sanitizer 1738 // runtime; if we're not including one, include our own copy. 1739 if (!HasOtherSanitizerRt) 1740 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "san", true, false); 1741 1742 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan", false); 1743 1744 // Only include the bits of the runtime which need a C++ ABI library if 1745 // we're linking in C++ mode. 1746 if (IsCXX) 1747 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan_cxx", false); 1748} 1749 1750static void addDfsanRTLinux(const ToolChain &TC, const ArgList &Args, 1751 ArgStringList &CmdArgs) { 1752 if (!Args.hasArg(options::OPT_shared)) 1753 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "dfsan", true); 1754} 1755 1756static bool shouldUseFramePointer(const ArgList &Args, 1757 const llvm::Triple &Triple) { 1758 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer, 1759 options::OPT_fomit_frame_pointer)) 1760 return A->getOption().matches(options::OPT_fno_omit_frame_pointer); 1761 1762 // Don't use a frame pointer on linux x86, x86_64 and z if optimizing. 1763 if ((Triple.getArch() == llvm::Triple::x86_64 || 1764 Triple.getArch() == llvm::Triple::x86 || 1765 Triple.getArch() == llvm::Triple::systemz) && 1766 Triple.getOS() == llvm::Triple::Linux) { 1767 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 1768 if (!A->getOption().matches(options::OPT_O0)) 1769 return false; 1770 } 1771 1772 if (Triple.getArch() == llvm::Triple::xcore) 1773 return false; 1774 1775 return true; 1776} 1777 1778static bool shouldUseLeafFramePointer(const ArgList &Args, 1779 const llvm::Triple &Triple) { 1780 if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer, 1781 options::OPT_momit_leaf_frame_pointer)) 1782 return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer); 1783 1784 // Don't use a leaf frame pointer on linux x86, x86_64 and z if optimizing. 1785 if ((Triple.getArch() == llvm::Triple::x86_64 || 1786 Triple.getArch() == llvm::Triple::x86 || 1787 Triple.getArch() == llvm::Triple::systemz) && 1788 Triple.getOS() == llvm::Triple::Linux) { 1789 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 1790 if (!A->getOption().matches(options::OPT_O0)) 1791 return false; 1792 } 1793 1794 if (Triple.getArch() == llvm::Triple::xcore) 1795 return false; 1796 1797 return true; 1798} 1799 1800/// Add a CC1 option to specify the debug compilation directory. 1801static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) { 1802 SmallString<128> cwd; 1803 if (!llvm::sys::fs::current_path(cwd)) { 1804 CmdArgs.push_back("-fdebug-compilation-dir"); 1805 CmdArgs.push_back(Args.MakeArgString(cwd)); 1806 } 1807} 1808 1809static const char *SplitDebugName(const ArgList &Args, 1810 const InputInfoList &Inputs) { 1811 Arg *FinalOutput = Args.getLastArg(options::OPT_o); 1812 if (FinalOutput && Args.hasArg(options::OPT_c)) { 1813 SmallString<128> T(FinalOutput->getValue()); 1814 llvm::sys::path::replace_extension(T, "dwo"); 1815 return Args.MakeArgString(T); 1816 } else { 1817 // Use the compilation dir. 1818 SmallString<128> T(Args.getLastArgValue(options::OPT_fdebug_compilation_dir)); 1819 SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput())); 1820 llvm::sys::path::replace_extension(F, "dwo"); 1821 T += F; 1822 return Args.MakeArgString(F); 1823 } 1824} 1825 1826static void SplitDebugInfo(const ToolChain &TC, Compilation &C, 1827 const Tool &T, const JobAction &JA, 1828 const ArgList &Args, const InputInfo &Output, 1829 const char *OutFile) { 1830 ArgStringList ExtractArgs; 1831 ExtractArgs.push_back("--extract-dwo"); 1832 1833 ArgStringList StripArgs; 1834 StripArgs.push_back("--strip-dwo"); 1835 1836 // Grabbing the output of the earlier compile step. 1837 StripArgs.push_back(Output.getFilename()); 1838 ExtractArgs.push_back(Output.getFilename()); 1839 ExtractArgs.push_back(OutFile); 1840 1841 const char *Exec = 1842 Args.MakeArgString(TC.GetProgramPath("objcopy")); 1843 1844 // First extract the dwo sections. 1845 C.addCommand(new Command(JA, T, Exec, ExtractArgs)); 1846 1847 // Then remove them from the original .o file. 1848 C.addCommand(new Command(JA, T, Exec, StripArgs)); 1849} 1850 1851static bool isOptimizationLevelFast(const ArgList &Args) { 1852 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 1853 if (A->getOption().matches(options::OPT_Ofast)) 1854 return true; 1855 return false; 1856} 1857 1858/// \brief Vectorize at all optimization levels greater than 1 except for -Oz. 1859static bool shouldEnableVectorizerAtOLevel(const ArgList &Args) { 1860 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 1861 if (A->getOption().matches(options::OPT_O4) || 1862 A->getOption().matches(options::OPT_Ofast)) 1863 return true; 1864 1865 if (A->getOption().matches(options::OPT_O0)) 1866 return false; 1867 1868 assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag"); 1869 1870 // Vectorize -O (which really is -O2), -Os. 1871 StringRef S(A->getValue()); 1872 if (S == "s" || S.empty()) 1873 return true; 1874 1875 // Don't vectorize -Oz. 1876 if (S == "z") 1877 return false; 1878 1879 unsigned OptLevel = 0; 1880 if (S.getAsInteger(10, OptLevel)) 1881 return false; 1882 1883 return OptLevel > 1; 1884 } 1885 1886 return false; 1887} 1888 1889void Clang::ConstructJob(Compilation &C, const JobAction &JA, 1890 const InputInfo &Output, 1891 const InputInfoList &Inputs, 1892 const ArgList &Args, 1893 const char *LinkingOutput) const { 1894 bool KernelOrKext = Args.hasArg(options::OPT_mkernel, 1895 options::OPT_fapple_kext); 1896 const Driver &D = getToolChain().getDriver(); 1897 ArgStringList CmdArgs; 1898 1899 assert(Inputs.size() == 1 && "Unable to handle multiple inputs."); 1900 1901 // Invoke ourselves in -cc1 mode. 1902 // 1903 // FIXME: Implement custom jobs for internal actions. 1904 CmdArgs.push_back("-cc1"); 1905 1906 // Add the "effective" target triple. 1907 CmdArgs.push_back("-triple"); 1908 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args); 1909 CmdArgs.push_back(Args.MakeArgString(TripleStr)); 1910 1911 // Select the appropriate action. 1912 RewriteKind rewriteKind = RK_None; 1913 1914 if (isa<AnalyzeJobAction>(JA)) { 1915 assert(JA.getType() == types::TY_Plist && "Invalid output type."); 1916 CmdArgs.push_back("-analyze"); 1917 } else if (isa<MigrateJobAction>(JA)) { 1918 CmdArgs.push_back("-migrate"); 1919 } else if (isa<PreprocessJobAction>(JA)) { 1920 if (Output.getType() == types::TY_Dependencies) 1921 CmdArgs.push_back("-Eonly"); 1922 else { 1923 CmdArgs.push_back("-E"); 1924 if (Args.hasArg(options::OPT_rewrite_objc) && 1925 !Args.hasArg(options::OPT_g_Group)) 1926 CmdArgs.push_back("-P"); 1927 } 1928 } else if (isa<AssembleJobAction>(JA)) { 1929 CmdArgs.push_back("-emit-obj"); 1930 1931 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D); 1932 1933 // Also ignore explicit -force_cpusubtype_ALL option. 1934 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL); 1935 } else if (isa<PrecompileJobAction>(JA)) { 1936 // Use PCH if the user requested it. 1937 bool UsePCH = D.CCCUsePCH; 1938 1939 if (JA.getType() == types::TY_Nothing) 1940 CmdArgs.push_back("-fsyntax-only"); 1941 else if (UsePCH) 1942 CmdArgs.push_back("-emit-pch"); 1943 else 1944 CmdArgs.push_back("-emit-pth"); 1945 } else { 1946 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool."); 1947 1948 if (JA.getType() == types::TY_Nothing) { 1949 CmdArgs.push_back("-fsyntax-only"); 1950 } else if (JA.getType() == types::TY_LLVM_IR || 1951 JA.getType() == types::TY_LTO_IR) { 1952 CmdArgs.push_back("-emit-llvm"); 1953 } else if (JA.getType() == types::TY_LLVM_BC || 1954 JA.getType() == types::TY_LTO_BC) { 1955 CmdArgs.push_back("-emit-llvm-bc"); 1956 } else if (JA.getType() == types::TY_PP_Asm) { 1957 CmdArgs.push_back("-S"); 1958 } else if (JA.getType() == types::TY_AST) { 1959 CmdArgs.push_back("-emit-pch"); 1960 } else if (JA.getType() == types::TY_ModuleFile) { 1961 CmdArgs.push_back("-module-file-info"); 1962 } else if (JA.getType() == types::TY_RewrittenObjC) { 1963 CmdArgs.push_back("-rewrite-objc"); 1964 rewriteKind = RK_NonFragile; 1965 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) { 1966 CmdArgs.push_back("-rewrite-objc"); 1967 rewriteKind = RK_Fragile; 1968 } else { 1969 assert(JA.getType() == types::TY_PP_Asm && 1970 "Unexpected output type!"); 1971 } 1972 } 1973 1974 // The make clang go fast button. 1975 CmdArgs.push_back("-disable-free"); 1976 1977 // Disable the verification pass in -asserts builds. 1978#ifdef NDEBUG 1979 CmdArgs.push_back("-disable-llvm-verifier"); 1980#endif 1981 1982 // Set the main file name, so that debug info works even with 1983 // -save-temps. 1984 CmdArgs.push_back("-main-file-name"); 1985 CmdArgs.push_back(getBaseInputName(Args, Inputs)); 1986 1987 // Some flags which affect the language (via preprocessor 1988 // defines). 1989 if (Args.hasArg(options::OPT_static)) 1990 CmdArgs.push_back("-static-define"); 1991 1992 if (isa<AnalyzeJobAction>(JA)) { 1993 // Enable region store model by default. 1994 CmdArgs.push_back("-analyzer-store=region"); 1995 1996 // Treat blocks as analysis entry points. 1997 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks"); 1998 1999 CmdArgs.push_back("-analyzer-eagerly-assume"); 2000 2001 // Add default argument set. 2002 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) { 2003 CmdArgs.push_back("-analyzer-checker=core"); 2004 2005 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32) 2006 CmdArgs.push_back("-analyzer-checker=unix"); 2007 2008 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple) 2009 CmdArgs.push_back("-analyzer-checker=osx"); 2010 2011 CmdArgs.push_back("-analyzer-checker=deadcode"); 2012 2013 if (types::isCXX(Inputs[0].getType())) 2014 CmdArgs.push_back("-analyzer-checker=cplusplus"); 2015 2016 // Enable the following experimental checkers for testing. 2017 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn"); 2018 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw"); 2019 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets"); 2020 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp"); 2021 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp"); 2022 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork"); 2023 } 2024 2025 // Set the output format. The default is plist, for (lame) historical 2026 // reasons. 2027 CmdArgs.push_back("-analyzer-output"); 2028 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output)) 2029 CmdArgs.push_back(A->getValue()); 2030 else 2031 CmdArgs.push_back("plist"); 2032 2033 // Disable the presentation of standard compiler warnings when 2034 // using --analyze. We only want to show static analyzer diagnostics 2035 // or frontend errors. 2036 CmdArgs.push_back("-w"); 2037 2038 // Add -Xanalyzer arguments when running as analyzer. 2039 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); 2040 } 2041 2042 CheckCodeGenerationOptions(D, Args); 2043 2044 bool PIE = getToolChain().isPIEDefault(); 2045 bool PIC = PIE || getToolChain().isPICDefault(); 2046 bool IsPICLevelTwo = PIC; 2047 2048 // For the PIC and PIE flag options, this logic is different from the 2049 // legacy logic in very old versions of GCC, as that logic was just 2050 // a bug no one had ever fixed. This logic is both more rational and 2051 // consistent with GCC's new logic now that the bugs are fixed. The last 2052 // argument relating to either PIC or PIE wins, and no other argument is 2053 // used. If the last argument is any flavor of the '-fno-...' arguments, 2054 // both PIC and PIE are disabled. Any PIE option implicitly enables PIC 2055 // at the same level. 2056 Arg *LastPICArg =Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC, 2057 options::OPT_fpic, options::OPT_fno_pic, 2058 options::OPT_fPIE, options::OPT_fno_PIE, 2059 options::OPT_fpie, options::OPT_fno_pie); 2060 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness 2061 // is forced, then neither PIC nor PIE flags will have no effect. 2062 if (!getToolChain().isPICDefaultForced()) { 2063 if (LastPICArg) { 2064 Option O = LastPICArg->getOption(); 2065 if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) || 2066 O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) { 2067 PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie); 2068 PIC = PIE || O.matches(options::OPT_fPIC) || 2069 O.matches(options::OPT_fpic); 2070 IsPICLevelTwo = O.matches(options::OPT_fPIE) || 2071 O.matches(options::OPT_fPIC); 2072 } else { 2073 PIE = PIC = false; 2074 } 2075 } 2076 } 2077 2078 // Inroduce a Darwin-specific hack. If the default is PIC but the flags 2079 // specified while enabling PIC enabled level 1 PIC, just force it back to 2080 // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my 2081 // informal testing). 2082 if (PIC && getToolChain().getTriple().isOSDarwin()) 2083 IsPICLevelTwo |= getToolChain().isPICDefault(); 2084 2085 // Note that these flags are trump-cards. Regardless of the order w.r.t. the 2086 // PIC or PIE options above, if these show up, PIC is disabled. 2087 llvm::Triple Triple(TripleStr); 2088 if (KernelOrKext && 2089 (Triple.getOS() != llvm::Triple::IOS || 2090 Triple.isOSVersionLT(6))) 2091 PIC = PIE = false; 2092 if (Args.hasArg(options::OPT_static)) 2093 PIC = PIE = false; 2094 2095 if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) { 2096 // This is a very special mode. It trumps the other modes, almost no one 2097 // uses it, and it isn't even valid on any OS but Darwin. 2098 if (!getToolChain().getTriple().isOSDarwin()) 2099 D.Diag(diag::err_drv_unsupported_opt_for_target) 2100 << A->getSpelling() << getToolChain().getTriple().str(); 2101 2102 // FIXME: Warn when this flag trumps some other PIC or PIE flag. 2103 2104 CmdArgs.push_back("-mrelocation-model"); 2105 CmdArgs.push_back("dynamic-no-pic"); 2106 2107 // Only a forced PIC mode can cause the actual compile to have PIC defines 2108 // etc., no flags are sufficient. This behavior was selected to closely 2109 // match that of llvm-gcc and Apple GCC before that. 2110 if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) { 2111 CmdArgs.push_back("-pic-level"); 2112 CmdArgs.push_back("2"); 2113 } 2114 } else { 2115 // Currently, LLVM only knows about PIC vs. static; the PIE differences are 2116 // handled in Clang's IRGen by the -pie-level flag. 2117 CmdArgs.push_back("-mrelocation-model"); 2118 CmdArgs.push_back(PIC ? "pic" : "static"); 2119 2120 if (PIC) { 2121 CmdArgs.push_back("-pic-level"); 2122 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1"); 2123 if (PIE) { 2124 CmdArgs.push_back("-pie-level"); 2125 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1"); 2126 } 2127 } 2128 } 2129 2130 if (!Args.hasFlag(options::OPT_fmerge_all_constants, 2131 options::OPT_fno_merge_all_constants)) 2132 CmdArgs.push_back("-fno-merge-all-constants"); 2133 2134 // LLVM Code Generator Options. 2135 2136 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 2137 CmdArgs.push_back("-mregparm"); 2138 CmdArgs.push_back(A->getValue()); 2139 } 2140 2141 if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return, 2142 options::OPT_freg_struct_return)) { 2143 if (getToolChain().getArch() != llvm::Triple::x86) { 2144 D.Diag(diag::err_drv_unsupported_opt_for_target) 2145 << A->getSpelling() << getToolChain().getTriple().str(); 2146 } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) { 2147 CmdArgs.push_back("-fpcc-struct-return"); 2148 } else { 2149 assert(A->getOption().matches(options::OPT_freg_struct_return)); 2150 CmdArgs.push_back("-freg-struct-return"); 2151 } 2152 } 2153 2154 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) 2155 CmdArgs.push_back("-mrtd"); 2156 2157 if (shouldUseFramePointer(Args, getToolChain().getTriple())) 2158 CmdArgs.push_back("-mdisable-fp-elim"); 2159 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss, 2160 options::OPT_fno_zero_initialized_in_bss)) 2161 CmdArgs.push_back("-mno-zero-initialized-in-bss"); 2162 2163 bool OFastEnabled = isOptimizationLevelFast(Args); 2164 // If -Ofast is the optimization level, then -fstrict-aliasing should be 2165 // enabled. This alias option is being used to simplify the hasFlag logic. 2166 OptSpecifier StrictAliasingAliasOption = OFastEnabled ? options::OPT_Ofast : 2167 options::OPT_fstrict_aliasing; 2168 if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption, 2169 options::OPT_fno_strict_aliasing, 2170 getToolChain().IsStrictAliasingDefault())) 2171 CmdArgs.push_back("-relaxed-aliasing"); 2172 if (Args.hasArg(options::OPT_fstruct_path_tbaa)) 2173 CmdArgs.push_back("-struct-path-tbaa"); 2174 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums, 2175 false)) 2176 CmdArgs.push_back("-fstrict-enums"); 2177 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls, 2178 options::OPT_fno_optimize_sibling_calls)) 2179 CmdArgs.push_back("-mdisable-tail-calls"); 2180 2181 // Handle segmented stacks. 2182 if (Args.hasArg(options::OPT_fsplit_stack)) 2183 CmdArgs.push_back("-split-stacks"); 2184 2185 // If -Ofast is the optimization level, then -ffast-math should be enabled. 2186 // This alias option is being used to simplify the getLastArg logic. 2187 OptSpecifier FastMathAliasOption = OFastEnabled ? options::OPT_Ofast : 2188 options::OPT_ffast_math; 2189 2190 // Handle various floating point optimization flags, mapping them to the 2191 // appropriate LLVM code generation flags. The pattern for all of these is to 2192 // default off the codegen optimizations, and if any flag enables them and no 2193 // flag disables them after the flag enabling them, enable the codegen 2194 // optimization. This is complicated by several "umbrella" flags. 2195 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2196 options::OPT_fno_fast_math, 2197 options::OPT_ffinite_math_only, 2198 options::OPT_fno_finite_math_only, 2199 options::OPT_fhonor_infinities, 2200 options::OPT_fno_honor_infinities)) 2201 if (A->getOption().getID() != options::OPT_fno_fast_math && 2202 A->getOption().getID() != options::OPT_fno_finite_math_only && 2203 A->getOption().getID() != options::OPT_fhonor_infinities) 2204 CmdArgs.push_back("-menable-no-infs"); 2205 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2206 options::OPT_fno_fast_math, 2207 options::OPT_ffinite_math_only, 2208 options::OPT_fno_finite_math_only, 2209 options::OPT_fhonor_nans, 2210 options::OPT_fno_honor_nans)) 2211 if (A->getOption().getID() != options::OPT_fno_fast_math && 2212 A->getOption().getID() != options::OPT_fno_finite_math_only && 2213 A->getOption().getID() != options::OPT_fhonor_nans) 2214 CmdArgs.push_back("-menable-no-nans"); 2215 2216 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes. 2217 bool MathErrno = getToolChain().IsMathErrnoDefault(); 2218 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2219 options::OPT_fno_fast_math, 2220 options::OPT_fmath_errno, 2221 options::OPT_fno_math_errno)) { 2222 // Turning on -ffast_math (with either flag) removes the need for MathErrno. 2223 // However, turning *off* -ffast_math merely restores the toolchain default 2224 // (which may be false). 2225 if (A->getOption().getID() == options::OPT_fno_math_errno || 2226 A->getOption().getID() == options::OPT_ffast_math || 2227 A->getOption().getID() == options::OPT_Ofast) 2228 MathErrno = false; 2229 else if (A->getOption().getID() == options::OPT_fmath_errno) 2230 MathErrno = true; 2231 } 2232 if (MathErrno) 2233 CmdArgs.push_back("-fmath-errno"); 2234 2235 // There are several flags which require disabling very specific 2236 // optimizations. Any of these being disabled forces us to turn off the 2237 // entire set of LLVM optimizations, so collect them through all the flag 2238 // madness. 2239 bool AssociativeMath = false; 2240 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2241 options::OPT_fno_fast_math, 2242 options::OPT_funsafe_math_optimizations, 2243 options::OPT_fno_unsafe_math_optimizations, 2244 options::OPT_fassociative_math, 2245 options::OPT_fno_associative_math)) 2246 if (A->getOption().getID() != options::OPT_fno_fast_math && 2247 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations && 2248 A->getOption().getID() != options::OPT_fno_associative_math) 2249 AssociativeMath = true; 2250 bool ReciprocalMath = false; 2251 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2252 options::OPT_fno_fast_math, 2253 options::OPT_funsafe_math_optimizations, 2254 options::OPT_fno_unsafe_math_optimizations, 2255 options::OPT_freciprocal_math, 2256 options::OPT_fno_reciprocal_math)) 2257 if (A->getOption().getID() != options::OPT_fno_fast_math && 2258 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations && 2259 A->getOption().getID() != options::OPT_fno_reciprocal_math) 2260 ReciprocalMath = true; 2261 bool SignedZeros = true; 2262 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2263 options::OPT_fno_fast_math, 2264 options::OPT_funsafe_math_optimizations, 2265 options::OPT_fno_unsafe_math_optimizations, 2266 options::OPT_fsigned_zeros, 2267 options::OPT_fno_signed_zeros)) 2268 if (A->getOption().getID() != options::OPT_fno_fast_math && 2269 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations && 2270 A->getOption().getID() != options::OPT_fsigned_zeros) 2271 SignedZeros = false; 2272 bool TrappingMath = true; 2273 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2274 options::OPT_fno_fast_math, 2275 options::OPT_funsafe_math_optimizations, 2276 options::OPT_fno_unsafe_math_optimizations, 2277 options::OPT_ftrapping_math, 2278 options::OPT_fno_trapping_math)) 2279 if (A->getOption().getID() != options::OPT_fno_fast_math && 2280 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations && 2281 A->getOption().getID() != options::OPT_ftrapping_math) 2282 TrappingMath = false; 2283 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros && 2284 !TrappingMath) 2285 CmdArgs.push_back("-menable-unsafe-fp-math"); 2286 2287 2288 // Validate and pass through -fp-contract option. 2289 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2290 options::OPT_fno_fast_math, 2291 options::OPT_ffp_contract)) { 2292 if (A->getOption().getID() == options::OPT_ffp_contract) { 2293 StringRef Val = A->getValue(); 2294 if (Val == "fast" || Val == "on" || Val == "off") { 2295 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val)); 2296 } else { 2297 D.Diag(diag::err_drv_unsupported_option_argument) 2298 << A->getOption().getName() << Val; 2299 } 2300 } else if (A->getOption().matches(options::OPT_ffast_math) || 2301 (OFastEnabled && A->getOption().matches(options::OPT_Ofast))) { 2302 // If fast-math is set then set the fp-contract mode to fast. 2303 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast")); 2304 } 2305 } 2306 2307 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags, 2308 // and if we find them, tell the frontend to provide the appropriate 2309 // preprocessor macros. This is distinct from enabling any optimizations as 2310 // these options induce language changes which must survive serialization 2311 // and deserialization, etc. 2312 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2313 options::OPT_fno_fast_math)) 2314 if (!A->getOption().matches(options::OPT_fno_fast_math)) 2315 CmdArgs.push_back("-ffast-math"); 2316 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only, options::OPT_fno_fast_math)) 2317 if (A->getOption().matches(options::OPT_ffinite_math_only)) 2318 CmdArgs.push_back("-ffinite-math-only"); 2319 2320 // Decide whether to use verbose asm. Verbose assembly is the default on 2321 // toolchains which have the integrated assembler on by default. 2322 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault(); 2323 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm, 2324 IsVerboseAsmDefault) || 2325 Args.hasArg(options::OPT_dA)) 2326 CmdArgs.push_back("-masm-verbose"); 2327 2328 if (Args.hasArg(options::OPT_fdebug_pass_structure)) { 2329 CmdArgs.push_back("-mdebug-pass"); 2330 CmdArgs.push_back("Structure"); 2331 } 2332 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) { 2333 CmdArgs.push_back("-mdebug-pass"); 2334 CmdArgs.push_back("Arguments"); 2335 } 2336 2337 // Enable -mconstructor-aliases except on darwin, where we have to 2338 // work around a linker bug; see <rdar://problem/7651567>. 2339 if (!getToolChain().getTriple().isOSDarwin()) 2340 CmdArgs.push_back("-mconstructor-aliases"); 2341 2342 // Darwin's kernel doesn't support guard variables; just die if we 2343 // try to use them. 2344 if (KernelOrKext && getToolChain().getTriple().isOSDarwin()) 2345 CmdArgs.push_back("-fforbid-guard-variables"); 2346 2347 if (Args.hasArg(options::OPT_mms_bitfields)) { 2348 CmdArgs.push_back("-mms-bitfields"); 2349 } 2350 2351 // This is a coarse approximation of what llvm-gcc actually does, both 2352 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more 2353 // complicated ways. 2354 bool AsynchronousUnwindTables = 2355 Args.hasFlag(options::OPT_fasynchronous_unwind_tables, 2356 options::OPT_fno_asynchronous_unwind_tables, 2357 getToolChain().IsUnwindTablesDefault() && 2358 !KernelOrKext); 2359 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables, 2360 AsynchronousUnwindTables)) 2361 CmdArgs.push_back("-munwind-tables"); 2362 2363 getToolChain().addClangTargetOptions(Args, CmdArgs); 2364 2365 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { 2366 CmdArgs.push_back("-mlimit-float-precision"); 2367 CmdArgs.push_back(A->getValue()); 2368 } 2369 2370 // FIXME: Handle -mtune=. 2371 (void) Args.hasArg(options::OPT_mtune_EQ); 2372 2373 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { 2374 CmdArgs.push_back("-mcode-model"); 2375 CmdArgs.push_back(A->getValue()); 2376 } 2377 2378 // Add target specific cpu and features flags. 2379 switch(getToolChain().getArch()) { 2380 default: 2381 break; 2382 2383 case llvm::Triple::arm: 2384 case llvm::Triple::thumb: 2385 AddARMTargetArgs(Args, CmdArgs, KernelOrKext); 2386 break; 2387 2388 case llvm::Triple::mips: 2389 case llvm::Triple::mipsel: 2390 case llvm::Triple::mips64: 2391 case llvm::Triple::mips64el: 2392 AddMIPSTargetArgs(Args, CmdArgs); 2393 break; 2394 2395 case llvm::Triple::ppc: 2396 case llvm::Triple::ppc64: 2397 case llvm::Triple::ppc64le: 2398 AddPPCTargetArgs(Args, CmdArgs); 2399 break; 2400 2401 case llvm::Triple::r600: 2402 AddR600TargetArgs(Args, CmdArgs); 2403 break; 2404 2405 case llvm::Triple::sparc: 2406 AddSparcTargetArgs(Args, CmdArgs); 2407 break; 2408 2409 case llvm::Triple::systemz: 2410 AddSystemZTargetArgs(Args, CmdArgs); 2411 break; 2412 2413 case llvm::Triple::x86: 2414 case llvm::Triple::x86_64: 2415 AddX86TargetArgs(Args, CmdArgs); 2416 break; 2417 2418 case llvm::Triple::hexagon: 2419 AddHexagonTargetArgs(Args, CmdArgs); 2420 break; 2421 2422 case llvm::Triple::aarch64: 2423 AddAArch64TargetArgs(Args, CmdArgs); 2424 break; 2425 } 2426 2427 // Add clang-cl arguments. 2428 if (getToolChain().getDriver().IsCLMode()) 2429 AddClangCLArgs(Args, CmdArgs); 2430 2431 // Pass the linker version in use. 2432 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) { 2433 CmdArgs.push_back("-target-linker-version"); 2434 CmdArgs.push_back(A->getValue()); 2435 } 2436 2437 if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple())) 2438 CmdArgs.push_back("-momit-leaf-frame-pointer"); 2439 2440 // Explicitly error on some things we know we don't support and can't just 2441 // ignore. 2442 types::ID InputType = Inputs[0].getType(); 2443 if (!Args.hasArg(options::OPT_fallow_unsupported)) { 2444 Arg *Unsupported; 2445 if (types::isCXX(InputType) && 2446 getToolChain().getTriple().isOSDarwin() && 2447 getToolChain().getArch() == llvm::Triple::x86) { 2448 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) || 2449 (Unsupported = Args.getLastArg(options::OPT_mkernel))) 2450 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386) 2451 << Unsupported->getOption().getName(); 2452 } 2453 } 2454 2455 Args.AddAllArgs(CmdArgs, options::OPT_v); 2456 Args.AddLastArg(CmdArgs, options::OPT_H); 2457 if (D.CCPrintHeaders && !D.CCGenDiagnostics) { 2458 CmdArgs.push_back("-header-include-file"); 2459 CmdArgs.push_back(D.CCPrintHeadersFilename ? 2460 D.CCPrintHeadersFilename : "-"); 2461 } 2462 Args.AddLastArg(CmdArgs, options::OPT_P); 2463 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout); 2464 2465 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) { 2466 CmdArgs.push_back("-diagnostic-log-file"); 2467 CmdArgs.push_back(D.CCLogDiagnosticsFilename ? 2468 D.CCLogDiagnosticsFilename : "-"); 2469 } 2470 2471 // Use the last option from "-g" group. "-gline-tables-only" 2472 // is preserved, all other debug options are substituted with "-g". 2473 Args.ClaimAllArgs(options::OPT_g_Group); 2474 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) { 2475 if (A->getOption().matches(options::OPT_gline_tables_only)) 2476 CmdArgs.push_back("-gline-tables-only"); 2477 else if (A->getOption().matches(options::OPT_gdwarf_2)) 2478 CmdArgs.push_back("-gdwarf-2"); 2479 else if (A->getOption().matches(options::OPT_gdwarf_3)) 2480 CmdArgs.push_back("-gdwarf-3"); 2481 else if (A->getOption().matches(options::OPT_gdwarf_4)) 2482 CmdArgs.push_back("-gdwarf-4"); 2483 else if (!A->getOption().matches(options::OPT_g0) && 2484 !A->getOption().matches(options::OPT_ggdb0)) { 2485 // Default is dwarf-2 for darwin. 2486 if (getToolChain().getTriple().isOSDarwin()) 2487 CmdArgs.push_back("-gdwarf-2"); 2488 else 2489 CmdArgs.push_back("-g"); 2490 } 2491 } 2492 2493 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now. 2494 Args.ClaimAllArgs(options::OPT_g_flags_Group); 2495 if (Args.hasArg(options::OPT_gcolumn_info)) 2496 CmdArgs.push_back("-dwarf-column-info"); 2497 2498 // -gsplit-dwarf should turn on -g and enable the backend dwarf 2499 // splitting and extraction. 2500 // FIXME: Currently only works on Linux. 2501 if (getToolChain().getTriple().getOS() == llvm::Triple::Linux && 2502 Args.hasArg(options::OPT_gsplit_dwarf)) { 2503 CmdArgs.push_back("-g"); 2504 CmdArgs.push_back("-backend-option"); 2505 CmdArgs.push_back("-split-dwarf=Enable"); 2506 } 2507 2508 2509 Args.AddAllArgs(CmdArgs, options::OPT_fdebug_types_section); 2510 2511 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections); 2512 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections); 2513 2514 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions); 2515 2516 if (Args.hasArg(options::OPT_ftest_coverage) || 2517 Args.hasArg(options::OPT_coverage)) 2518 CmdArgs.push_back("-femit-coverage-notes"); 2519 if (Args.hasArg(options::OPT_fprofile_arcs) || 2520 Args.hasArg(options::OPT_coverage)) 2521 CmdArgs.push_back("-femit-coverage-data"); 2522 2523 if (C.getArgs().hasArg(options::OPT_c) || 2524 C.getArgs().hasArg(options::OPT_S)) { 2525 if (Output.isFilename()) { 2526 CmdArgs.push_back("-coverage-file"); 2527 SmallString<128> CoverageFilename(Output.getFilename()); 2528 if (llvm::sys::path::is_relative(CoverageFilename.str())) { 2529 SmallString<128> Pwd; 2530 if (!llvm::sys::fs::current_path(Pwd)) { 2531 llvm::sys::path::append(Pwd, CoverageFilename.str()); 2532 CoverageFilename.swap(Pwd); 2533 } 2534 } 2535 CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 2536 } 2537 } 2538 2539 // Pass options for controlling the default header search paths. 2540 if (Args.hasArg(options::OPT_nostdinc)) { 2541 CmdArgs.push_back("-nostdsysteminc"); 2542 CmdArgs.push_back("-nobuiltininc"); 2543 } else { 2544 if (Args.hasArg(options::OPT_nostdlibinc)) 2545 CmdArgs.push_back("-nostdsysteminc"); 2546 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx); 2547 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc); 2548 } 2549 2550 // Pass the path to compiler resource files. 2551 CmdArgs.push_back("-resource-dir"); 2552 CmdArgs.push_back(D.ResourceDir.c_str()); 2553 2554 Args.AddLastArg(CmdArgs, options::OPT_working_directory); 2555 2556 bool ARCMTEnabled = false; 2557 if (!Args.hasArg(options::OPT_fno_objc_arc)) { 2558 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check, 2559 options::OPT_ccc_arcmt_modify, 2560 options::OPT_ccc_arcmt_migrate)) { 2561 ARCMTEnabled = true; 2562 switch (A->getOption().getID()) { 2563 default: 2564 llvm_unreachable("missed a case"); 2565 case options::OPT_ccc_arcmt_check: 2566 CmdArgs.push_back("-arcmt-check"); 2567 break; 2568 case options::OPT_ccc_arcmt_modify: 2569 CmdArgs.push_back("-arcmt-modify"); 2570 break; 2571 case options::OPT_ccc_arcmt_migrate: 2572 CmdArgs.push_back("-arcmt-migrate"); 2573 CmdArgs.push_back("-mt-migrate-directory"); 2574 CmdArgs.push_back(A->getValue()); 2575 2576 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output); 2577 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors); 2578 break; 2579 } 2580 } 2581 } else { 2582 Args.ClaimAllArgs(options::OPT_ccc_arcmt_check); 2583 Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify); 2584 Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate); 2585 } 2586 2587 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) { 2588 if (ARCMTEnabled) { 2589 D.Diag(diag::err_drv_argument_not_allowed_with) 2590 << A->getAsString(Args) << "-ccc-arcmt-migrate"; 2591 } 2592 CmdArgs.push_back("-mt-migrate-directory"); 2593 CmdArgs.push_back(A->getValue()); 2594 2595 if (!Args.hasArg(options::OPT_objcmt_migrate_literals, 2596 options::OPT_objcmt_migrate_subscripting, 2597 options::OPT_objcmt_migrate_property)) { 2598 // None specified, means enable them all. 2599 CmdArgs.push_back("-objcmt-migrate-literals"); 2600 CmdArgs.push_back("-objcmt-migrate-subscripting"); 2601 CmdArgs.push_back("-objcmt-migrate-property"); 2602 } else { 2603 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 2604 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 2605 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 2606 } 2607 } 2608 2609 // Add preprocessing options like -I, -D, etc. if we are using the 2610 // preprocessor. 2611 // 2612 // FIXME: Support -fpreprocessed 2613 if (types::getPreprocessedType(InputType) != types::TY_INVALID) 2614 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs); 2615 2616 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes 2617 // that "The compiler can only warn and ignore the option if not recognized". 2618 // When building with ccache, it will pass -D options to clang even on 2619 // preprocessed inputs and configure concludes that -fPIC is not supported. 2620 Args.ClaimAllArgs(options::OPT_D); 2621 2622 // Manually translate -O4 to -O3; let clang reject others. 2623 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 2624 if (A->getOption().matches(options::OPT_O4)) 2625 CmdArgs.push_back("-O3"); 2626 else 2627 A->render(Args, CmdArgs); 2628 } 2629 2630 // Don't warn about unused -flto. This can happen when we're preprocessing or 2631 // precompiling. 2632 Args.ClaimAllArgs(options::OPT_flto); 2633 2634 Args.AddAllArgs(CmdArgs, options::OPT_W_Group); 2635 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false)) 2636 CmdArgs.push_back("-pedantic"); 2637 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors); 2638 Args.AddLastArg(CmdArgs, options::OPT_w); 2639 2640 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} 2641 // (-ansi is equivalent to -std=c89 or -std=c++98). 2642 // 2643 // If a std is supplied, only add -trigraphs if it follows the 2644 // option. 2645 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) { 2646 if (Std->getOption().matches(options::OPT_ansi)) 2647 if (types::isCXX(InputType)) 2648 CmdArgs.push_back("-std=c++98"); 2649 else 2650 CmdArgs.push_back("-std=c89"); 2651 else 2652 Std->render(Args, CmdArgs); 2653 2654 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi, 2655 options::OPT_trigraphs)) 2656 if (A != Std) 2657 A->render(Args, CmdArgs); 2658 } else { 2659 // Honor -std-default. 2660 // 2661 // FIXME: Clang doesn't correctly handle -std= when the input language 2662 // doesn't match. For the time being just ignore this for C++ inputs; 2663 // eventually we want to do all the standard defaulting here instead of 2664 // splitting it between the driver and clang -cc1. 2665 if (!types::isCXX(InputType)) 2666 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, 2667 "-std=", /*Joined=*/true); 2668 else if (getToolChain().getTriple().getOS() == llvm::Triple::Win32) 2669 CmdArgs.push_back("-std=c++11"); 2670 2671 Args.AddLastArg(CmdArgs, options::OPT_trigraphs); 2672 } 2673 2674 // Map the bizarre '-Wwrite-strings' flag to a more sensible 2675 // '-fconst-strings'; this better indicates its actual behavior. 2676 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings, 2677 false)) { 2678 // For perfect compatibility with GCC, we do this even in the presence of 2679 // '-w'. This flag names something other than a warning for GCC. 2680 CmdArgs.push_back("-fconst-strings"); 2681 } 2682 2683 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active 2684 // during C++ compilation, which it is by default. GCC keeps this define even 2685 // in the presence of '-w', match this behavior bug-for-bug. 2686 if (types::isCXX(InputType) && 2687 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated, 2688 true)) { 2689 CmdArgs.push_back("-fdeprecated-macro"); 2690 } 2691 2692 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'. 2693 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) { 2694 if (Asm->getOption().matches(options::OPT_fasm)) 2695 CmdArgs.push_back("-fgnu-keywords"); 2696 else 2697 CmdArgs.push_back("-fno-gnu-keywords"); 2698 } 2699 2700 if (ShouldDisableCFI(Args, getToolChain())) 2701 CmdArgs.push_back("-fno-dwarf2-cfi-asm"); 2702 2703 if (ShouldDisableDwarfDirectory(Args, getToolChain())) 2704 CmdArgs.push_back("-fno-dwarf-directory-asm"); 2705 2706 if (ShouldDisableAutolink(Args, getToolChain())) 2707 CmdArgs.push_back("-fno-autolink"); 2708 2709 // Add in -fdebug-compilation-dir if necessary. 2710 addDebugCompDirArg(Args, CmdArgs); 2711 2712 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_, 2713 options::OPT_ftemplate_depth_EQ)) { 2714 CmdArgs.push_back("-ftemplate-depth"); 2715 CmdArgs.push_back(A->getValue()); 2716 } 2717 2718 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) { 2719 CmdArgs.push_back("-fconstexpr-depth"); 2720 CmdArgs.push_back(A->getValue()); 2721 } 2722 2723 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) { 2724 CmdArgs.push_back("-fconstexpr-steps"); 2725 CmdArgs.push_back(A->getValue()); 2726 } 2727 2728 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) { 2729 CmdArgs.push_back("-fbracket-depth"); 2730 CmdArgs.push_back(A->getValue()); 2731 } 2732 2733 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ, 2734 options::OPT_Wlarge_by_value_copy_def)) { 2735 if (A->getNumValues()) { 2736 StringRef bytes = A->getValue(); 2737 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes)); 2738 } else 2739 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value 2740 } 2741 2742 2743 if (Args.hasArg(options::OPT_relocatable_pch)) 2744 CmdArgs.push_back("-relocatable-pch"); 2745 2746 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) { 2747 CmdArgs.push_back("-fconstant-string-class"); 2748 CmdArgs.push_back(A->getValue()); 2749 } 2750 2751 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) { 2752 CmdArgs.push_back("-ftabstop"); 2753 CmdArgs.push_back(A->getValue()); 2754 } 2755 2756 CmdArgs.push_back("-ferror-limit"); 2757 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ)) 2758 CmdArgs.push_back(A->getValue()); 2759 else 2760 CmdArgs.push_back("19"); 2761 2762 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) { 2763 CmdArgs.push_back("-fmacro-backtrace-limit"); 2764 CmdArgs.push_back(A->getValue()); 2765 } 2766 2767 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) { 2768 CmdArgs.push_back("-ftemplate-backtrace-limit"); 2769 CmdArgs.push_back(A->getValue()); 2770 } 2771 2772 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) { 2773 CmdArgs.push_back("-fconstexpr-backtrace-limit"); 2774 CmdArgs.push_back(A->getValue()); 2775 } 2776 2777 // Pass -fmessage-length=. 2778 CmdArgs.push_back("-fmessage-length"); 2779 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) { 2780 CmdArgs.push_back(A->getValue()); 2781 } else { 2782 // If -fmessage-length=N was not specified, determine whether this is a 2783 // terminal and, if so, implicitly define -fmessage-length appropriately. 2784 unsigned N = llvm::sys::Process::StandardErrColumns(); 2785 CmdArgs.push_back(Args.MakeArgString(Twine(N))); 2786 } 2787 2788 // -fvisibility= and -fvisibility-ms-compat are of a piece. 2789 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ, 2790 options::OPT_fvisibility_ms_compat)) { 2791 if (A->getOption().matches(options::OPT_fvisibility_EQ)) { 2792 CmdArgs.push_back("-fvisibility"); 2793 CmdArgs.push_back(A->getValue()); 2794 } else { 2795 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat)); 2796 CmdArgs.push_back("-fvisibility"); 2797 CmdArgs.push_back("hidden"); 2798 CmdArgs.push_back("-ftype-visibility"); 2799 CmdArgs.push_back("default"); 2800 } 2801 } 2802 2803 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden); 2804 2805 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ); 2806 2807 // -fhosted is default. 2808 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) || 2809 KernelOrKext) 2810 CmdArgs.push_back("-ffreestanding"); 2811 2812 // Forward -f (flag) options which we can pass directly. 2813 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); 2814 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); 2815 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info); 2816 Args.AddLastArg(CmdArgs, options::OPT_fno_limit_debug_info); 2817 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names); 2818 // AltiVec language extensions aren't relevant for assembling. 2819 if (!isa<PreprocessJobAction>(JA) || 2820 Output.getType() != types::TY_PP_Asm) 2821 Args.AddLastArg(CmdArgs, options::OPT_faltivec); 2822 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree); 2823 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type); 2824 2825 const SanitizerArgs &Sanitize = D.getOrParseSanitizerArgs(Args); 2826 Sanitize.addArgs(getToolChain(), Args, CmdArgs); 2827 2828 if (!Args.hasFlag(options::OPT_fsanitize_recover, 2829 options::OPT_fno_sanitize_recover, 2830 true)) 2831 CmdArgs.push_back("-fno-sanitize-recover"); 2832 2833 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) || 2834 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error, 2835 options::OPT_fno_sanitize_undefined_trap_on_error, false)) 2836 CmdArgs.push_back("-fsanitize-undefined-trap-on-error"); 2837 2838 // Report an error for -faltivec on anything other than PowerPC. 2839 if (const Arg *A = Args.getLastArg(options::OPT_faltivec)) 2840 if (!(getToolChain().getArch() == llvm::Triple::ppc || 2841 getToolChain().getArch() == llvm::Triple::ppc64 || 2842 getToolChain().getArch() == llvm::Triple::ppc64le)) 2843 D.Diag(diag::err_drv_argument_only_allowed_with) 2844 << A->getAsString(Args) << "ppc/ppc64/ppc64le"; 2845 2846 if (getToolChain().SupportsProfiling()) 2847 Args.AddLastArg(CmdArgs, options::OPT_pg); 2848 2849 // -flax-vector-conversions is default. 2850 if (!Args.hasFlag(options::OPT_flax_vector_conversions, 2851 options::OPT_fno_lax_vector_conversions)) 2852 CmdArgs.push_back("-fno-lax-vector-conversions"); 2853 2854 if (Args.getLastArg(options::OPT_fapple_kext)) 2855 CmdArgs.push_back("-fapple-kext"); 2856 2857 if (Args.hasFlag(options::OPT_frewrite_includes, 2858 options::OPT_fno_rewrite_includes, false)) 2859 CmdArgs.push_back("-frewrite-includes"); 2860 2861 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch); 2862 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info); 2863 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits); 2864 Args.AddLastArg(CmdArgs, options::OPT_ftime_report); 2865 Args.AddLastArg(CmdArgs, options::OPT_ftrapv); 2866 2867 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) { 2868 CmdArgs.push_back("-ftrapv-handler"); 2869 CmdArgs.push_back(A->getValue()); 2870 } 2871 2872 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ); 2873 2874 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but 2875 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv. 2876 if (Arg *A = Args.getLastArg(options::OPT_fwrapv, 2877 options::OPT_fno_wrapv)) { 2878 if (A->getOption().matches(options::OPT_fwrapv)) 2879 CmdArgs.push_back("-fwrapv"); 2880 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow, 2881 options::OPT_fno_strict_overflow)) { 2882 if (A->getOption().matches(options::OPT_fno_strict_overflow)) 2883 CmdArgs.push_back("-fwrapv"); 2884 } 2885 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings); 2886 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops, 2887 options::OPT_fno_unroll_loops); 2888 2889 Args.AddLastArg(CmdArgs, options::OPT_pthread); 2890 2891 2892 // -stack-protector=0 is default. 2893 unsigned StackProtectorLevel = 0; 2894 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector, 2895 options::OPT_fstack_protector_all, 2896 options::OPT_fstack_protector)) { 2897 if (A->getOption().matches(options::OPT_fstack_protector)) 2898 StackProtectorLevel = 1; 2899 else if (A->getOption().matches(options::OPT_fstack_protector_all)) 2900 StackProtectorLevel = 2; 2901 } else { 2902 StackProtectorLevel = 2903 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext); 2904 } 2905 if (StackProtectorLevel) { 2906 CmdArgs.push_back("-stack-protector"); 2907 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel))); 2908 } 2909 2910 // --param ssp-buffer-size= 2911 for (arg_iterator it = Args.filtered_begin(options::OPT__param), 2912 ie = Args.filtered_end(); it != ie; ++it) { 2913 StringRef Str((*it)->getValue()); 2914 if (Str.startswith("ssp-buffer-size=")) { 2915 if (StackProtectorLevel) { 2916 CmdArgs.push_back("-stack-protector-buffer-size"); 2917 // FIXME: Verify the argument is a valid integer. 2918 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16))); 2919 } 2920 (*it)->claim(); 2921 } 2922 } 2923 2924 // Translate -mstackrealign 2925 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign, 2926 false)) { 2927 CmdArgs.push_back("-backend-option"); 2928 CmdArgs.push_back("-force-align-stack"); 2929 } 2930 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign, 2931 false)) { 2932 CmdArgs.push_back(Args.MakeArgString("-mstackrealign")); 2933 } 2934 2935 if (Args.hasArg(options::OPT_mstack_alignment)) { 2936 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment); 2937 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment)); 2938 } 2939 // -mkernel implies -mstrict-align; don't add the redundant option. 2940 if (Args.hasArg(options::OPT_mstrict_align) && !KernelOrKext) { 2941 CmdArgs.push_back("-backend-option"); 2942 CmdArgs.push_back("-arm-strict-align"); 2943 } 2944 2945 // Forward -f options with positive and negative forms; we translate 2946 // these by hand. 2947 2948 if (Args.hasArg(options::OPT_mkernel)) { 2949 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType)) 2950 CmdArgs.push_back("-fapple-kext"); 2951 if (!Args.hasArg(options::OPT_fbuiltin)) 2952 CmdArgs.push_back("-fno-builtin"); 2953 Args.ClaimAllArgs(options::OPT_fno_builtin); 2954 } 2955 // -fbuiltin is default. 2956 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin)) 2957 CmdArgs.push_back("-fno-builtin"); 2958 2959 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new, 2960 options::OPT_fno_assume_sane_operator_new)) 2961 CmdArgs.push_back("-fno-assume-sane-operator-new"); 2962 2963 // -fblocks=0 is default. 2964 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks, 2965 getToolChain().IsBlocksDefault()) || 2966 (Args.hasArg(options::OPT_fgnu_runtime) && 2967 Args.hasArg(options::OPT_fobjc_nonfragile_abi) && 2968 !Args.hasArg(options::OPT_fno_blocks))) { 2969 CmdArgs.push_back("-fblocks"); 2970 2971 if (!Args.hasArg(options::OPT_fgnu_runtime) && 2972 !getToolChain().hasBlocksRuntime()) 2973 CmdArgs.push_back("-fblocks-runtime-optional"); 2974 } 2975 2976 // -fmodules enables modules (off by default). However, for C++/Objective-C++, 2977 // users must also pass -fcxx-modules. The latter flag will disappear once the 2978 // modules implementation is solid for C++/Objective-C++ programs as well. 2979 bool HaveModules = false; 2980 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) { 2981 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules, 2982 options::OPT_fno_cxx_modules, 2983 false); 2984 if (AllowedInCXX || !types::isCXX(InputType)) { 2985 CmdArgs.push_back("-fmodules"); 2986 HaveModules = true; 2987 } 2988 } 2989 2990 // -fmodule-maps enables module map processing (off by default) for header 2991 // checking. It is implied by -fmodules. 2992 if (Args.hasFlag(options::OPT_fmodule_maps, options::OPT_fno_module_maps, 2993 false)) { 2994 CmdArgs.push_back("-fmodule-maps"); 2995 } 2996 2997 // If a module path was provided, pass it along. Otherwise, use a temporary 2998 // directory. 2999 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) { 3000 A->claim(); 3001 if (HaveModules) { 3002 A->render(Args, CmdArgs); 3003 } 3004 } else if (HaveModules) { 3005 SmallString<128> DefaultModuleCache; 3006 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false, 3007 DefaultModuleCache); 3008 llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang"); 3009 llvm::sys::path::append(DefaultModuleCache, "ModuleCache"); 3010 const char Arg[] = "-fmodules-cache-path="; 3011 DefaultModuleCache.insert(DefaultModuleCache.begin(), 3012 Arg, Arg + strlen(Arg)); 3013 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache)); 3014 } 3015 3016 // Pass through all -fmodules-ignore-macro arguments. 3017 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro); 3018 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval); 3019 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after); 3020 3021 // -faccess-control is default. 3022 if (Args.hasFlag(options::OPT_fno_access_control, 3023 options::OPT_faccess_control, 3024 false)) 3025 CmdArgs.push_back("-fno-access-control"); 3026 3027 // -felide-constructors is the default. 3028 if (Args.hasFlag(options::OPT_fno_elide_constructors, 3029 options::OPT_felide_constructors, 3030 false)) 3031 CmdArgs.push_back("-fno-elide-constructors"); 3032 3033 // -frtti is default. 3034 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) || 3035 KernelOrKext) { 3036 CmdArgs.push_back("-fno-rtti"); 3037 3038 // -fno-rtti cannot usefully be combined with -fsanitize=vptr. 3039 if (Sanitize.sanitizesVptr()) { 3040 std::string NoRttiArg = 3041 Args.getLastArg(options::OPT_mkernel, 3042 options::OPT_fapple_kext, 3043 options::OPT_fno_rtti)->getAsString(Args); 3044 D.Diag(diag::err_drv_argument_not_allowed_with) 3045 << "-fsanitize=vptr" << NoRttiArg; 3046 } 3047 } 3048 3049 // -fshort-enums=0 is default for all architectures except Hexagon. 3050 if (Args.hasFlag(options::OPT_fshort_enums, 3051 options::OPT_fno_short_enums, 3052 getToolChain().getArch() == 3053 llvm::Triple::hexagon)) 3054 CmdArgs.push_back("-fshort-enums"); 3055 3056 // -fsigned-char is default. 3057 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char, 3058 isSignedCharDefault(getToolChain().getTriple()))) 3059 CmdArgs.push_back("-fno-signed-char"); 3060 3061 // -fthreadsafe-static is default. 3062 if (!Args.hasFlag(options::OPT_fthreadsafe_statics, 3063 options::OPT_fno_threadsafe_statics)) 3064 CmdArgs.push_back("-fno-threadsafe-statics"); 3065 3066 // -fuse-cxa-atexit is default. 3067 if (!Args.hasFlag(options::OPT_fuse_cxa_atexit, 3068 options::OPT_fno_use_cxa_atexit, 3069 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin && 3070 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 && 3071 getToolChain().getArch() != llvm::Triple::hexagon) || 3072 KernelOrKext) 3073 CmdArgs.push_back("-fno-use-cxa-atexit"); 3074 3075 // -fms-extensions=0 is default. 3076 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, 3077 getToolChain().getTriple().getOS() == llvm::Triple::Win32)) 3078 CmdArgs.push_back("-fms-extensions"); 3079 3080 // -fms-compatibility=0 is default. 3081 if (Args.hasFlag(options::OPT_fms_compatibility, 3082 options::OPT_fno_ms_compatibility, 3083 (getToolChain().getTriple().getOS() == llvm::Triple::Win32 && 3084 Args.hasFlag(options::OPT_fms_extensions, 3085 options::OPT_fno_ms_extensions, 3086 true)))) 3087 CmdArgs.push_back("-fms-compatibility"); 3088 3089 // -fmsc-version=1300 is default. 3090 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, 3091 getToolChain().getTriple().getOS() == llvm::Triple::Win32) || 3092 Args.hasArg(options::OPT_fmsc_version)) { 3093 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version); 3094 if (msc_ver.empty()) 3095 CmdArgs.push_back("-fmsc-version=1300"); 3096 else 3097 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver)); 3098 } 3099 3100 3101 // -fno-borland-extensions is default. 3102 if (Args.hasFlag(options::OPT_fborland_extensions, 3103 options::OPT_fno_borland_extensions, false)) 3104 CmdArgs.push_back("-fborland-extensions"); 3105 3106 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL 3107 // needs it. 3108 if (Args.hasFlag(options::OPT_fdelayed_template_parsing, 3109 options::OPT_fno_delayed_template_parsing, 3110 getToolChain().getTriple().getOS() == llvm::Triple::Win32)) 3111 CmdArgs.push_back("-fdelayed-template-parsing"); 3112 3113 // -fgnu-keywords default varies depending on language; only pass if 3114 // specified. 3115 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords, 3116 options::OPT_fno_gnu_keywords)) 3117 A->render(Args, CmdArgs); 3118 3119 if (Args.hasFlag(options::OPT_fgnu89_inline, 3120 options::OPT_fno_gnu89_inline, 3121 false)) 3122 CmdArgs.push_back("-fgnu89-inline"); 3123 3124 if (Args.hasArg(options::OPT_fno_inline)) 3125 CmdArgs.push_back("-fno-inline"); 3126 3127 if (Args.hasArg(options::OPT_fno_inline_functions)) 3128 CmdArgs.push_back("-fno-inline-functions"); 3129 3130 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind); 3131 3132 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and 3133 // legacy is the default. 3134 if (objcRuntime.isNonFragile()) { 3135 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch, 3136 options::OPT_fno_objc_legacy_dispatch, 3137 objcRuntime.isLegacyDispatchDefaultForArch( 3138 getToolChain().getArch()))) { 3139 if (getToolChain().UseObjCMixedDispatch()) 3140 CmdArgs.push_back("-fobjc-dispatch-method=mixed"); 3141 else 3142 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy"); 3143 } 3144 } 3145 3146 // -fobjc-default-synthesize-properties=1 is default. This only has an effect 3147 // if the nonfragile objc abi is used. 3148 if (getToolChain().IsObjCDefaultSynthPropertiesDefault()) { 3149 CmdArgs.push_back("-fobjc-default-synthesize-properties"); 3150 } 3151 3152 // -fencode-extended-block-signature=1 is default. 3153 if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) { 3154 CmdArgs.push_back("-fencode-extended-block-signature"); 3155 } 3156 3157 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc. 3158 // NOTE: This logic is duplicated in ToolChains.cpp. 3159 bool ARC = isObjCAutoRefCount(Args); 3160 if (ARC) { 3161 getToolChain().CheckObjCARC(); 3162 3163 CmdArgs.push_back("-fobjc-arc"); 3164 3165 // FIXME: It seems like this entire block, and several around it should be 3166 // wrapped in isObjC, but for now we just use it here as this is where it 3167 // was being used previously. 3168 if (types::isCXX(InputType) && types::isObjC(InputType)) { 3169 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) 3170 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++"); 3171 else 3172 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++"); 3173 } 3174 3175 // Allow the user to enable full exceptions code emission. 3176 // We define off for Objective-CC, on for Objective-C++. 3177 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions, 3178 options::OPT_fno_objc_arc_exceptions, 3179 /*default*/ types::isCXX(InputType))) 3180 CmdArgs.push_back("-fobjc-arc-exceptions"); 3181 } 3182 3183 // -fobjc-infer-related-result-type is the default, except in the Objective-C 3184 // rewriter. 3185 if (rewriteKind != RK_None) 3186 CmdArgs.push_back("-fno-objc-infer-related-result-type"); 3187 3188 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only 3189 // takes precedence. 3190 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only); 3191 if (!GCArg) 3192 GCArg = Args.getLastArg(options::OPT_fobjc_gc); 3193 if (GCArg) { 3194 if (ARC) { 3195 D.Diag(diag::err_drv_objc_gc_arr) 3196 << GCArg->getAsString(Args); 3197 } else if (getToolChain().SupportsObjCGC()) { 3198 GCArg->render(Args, CmdArgs); 3199 } else { 3200 // FIXME: We should move this to a hard error. 3201 D.Diag(diag::warn_drv_objc_gc_unsupported) 3202 << GCArg->getAsString(Args); 3203 } 3204 } 3205 3206 // Add exception args. 3207 addExceptionArgs(Args, InputType, getToolChain().getTriple(), 3208 KernelOrKext, objcRuntime, CmdArgs); 3209 3210 if (getToolChain().UseSjLjExceptions()) 3211 CmdArgs.push_back("-fsjlj-exceptions"); 3212 3213 // C++ "sane" operator new. 3214 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new, 3215 options::OPT_fno_assume_sane_operator_new)) 3216 CmdArgs.push_back("-fno-assume-sane-operator-new"); 3217 3218 // -fconstant-cfstrings is default, and may be subject to argument translation 3219 // on Darwin. 3220 if (!Args.hasFlag(options::OPT_fconstant_cfstrings, 3221 options::OPT_fno_constant_cfstrings) || 3222 !Args.hasFlag(options::OPT_mconstant_cfstrings, 3223 options::OPT_mno_constant_cfstrings)) 3224 CmdArgs.push_back("-fno-constant-cfstrings"); 3225 3226 // -fshort-wchar default varies depending on platform; only 3227 // pass if specified. 3228 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar)) 3229 A->render(Args, CmdArgs); 3230 3231 // -fno-pascal-strings is default, only pass non-default. 3232 if (Args.hasFlag(options::OPT_fpascal_strings, 3233 options::OPT_fno_pascal_strings, 3234 false)) 3235 CmdArgs.push_back("-fpascal-strings"); 3236 3237 // Honor -fpack-struct= and -fpack-struct, if given. Note that 3238 // -fno-pack-struct doesn't apply to -fpack-struct=. 3239 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) { 3240 std::string PackStructStr = "-fpack-struct="; 3241 PackStructStr += A->getValue(); 3242 CmdArgs.push_back(Args.MakeArgString(PackStructStr)); 3243 } else if (Args.hasFlag(options::OPT_fpack_struct, 3244 options::OPT_fno_pack_struct, false)) { 3245 CmdArgs.push_back("-fpack-struct=1"); 3246 } 3247 3248 if (KernelOrKext || isNoCommonDefault(getToolChain().getTriple())) { 3249 if (!Args.hasArg(options::OPT_fcommon)) 3250 CmdArgs.push_back("-fno-common"); 3251 Args.ClaimAllArgs(options::OPT_fno_common); 3252 } 3253 3254 // -fcommon is default, only pass non-default. 3255 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common)) 3256 CmdArgs.push_back("-fno-common"); 3257 3258 // -fsigned-bitfields is default, and clang doesn't yet support 3259 // -funsigned-bitfields. 3260 if (!Args.hasFlag(options::OPT_fsigned_bitfields, 3261 options::OPT_funsigned_bitfields)) 3262 D.Diag(diag::warn_drv_clang_unsupported) 3263 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args); 3264 3265 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope. 3266 if (!Args.hasFlag(options::OPT_ffor_scope, 3267 options::OPT_fno_for_scope)) 3268 D.Diag(diag::err_drv_clang_unsupported) 3269 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args); 3270 3271 // -fcaret-diagnostics is default. 3272 if (!Args.hasFlag(options::OPT_fcaret_diagnostics, 3273 options::OPT_fno_caret_diagnostics, true)) 3274 CmdArgs.push_back("-fno-caret-diagnostics"); 3275 3276 // -fdiagnostics-fixit-info is default, only pass non-default. 3277 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info, 3278 options::OPT_fno_diagnostics_fixit_info)) 3279 CmdArgs.push_back("-fno-diagnostics-fixit-info"); 3280 3281 // Enable -fdiagnostics-show-option by default. 3282 if (Args.hasFlag(options::OPT_fdiagnostics_show_option, 3283 options::OPT_fno_diagnostics_show_option)) 3284 CmdArgs.push_back("-fdiagnostics-show-option"); 3285 3286 if (const Arg *A = 3287 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) { 3288 CmdArgs.push_back("-fdiagnostics-show-category"); 3289 CmdArgs.push_back(A->getValue()); 3290 } 3291 3292 if (const Arg *A = 3293 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) { 3294 CmdArgs.push_back("-fdiagnostics-format"); 3295 CmdArgs.push_back(A->getValue()); 3296 } 3297 3298 if (Arg *A = Args.getLastArg( 3299 options::OPT_fdiagnostics_show_note_include_stack, 3300 options::OPT_fno_diagnostics_show_note_include_stack)) { 3301 if (A->getOption().matches( 3302 options::OPT_fdiagnostics_show_note_include_stack)) 3303 CmdArgs.push_back("-fdiagnostics-show-note-include-stack"); 3304 else 3305 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack"); 3306 } 3307 3308 // Color diagnostics are the default, unless the terminal doesn't support 3309 // them. 3310 // Support both clang's -f[no-]color-diagnostics and gcc's 3311 // -f[no-]diagnostics-colors[=never|always|auto]. 3312 enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto; 3313 for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 3314 it != ie; ++it) { 3315 const Option &O = (*it)->getOption(); 3316 if (!O.matches(options::OPT_fcolor_diagnostics) && 3317 !O.matches(options::OPT_fdiagnostics_color) && 3318 !O.matches(options::OPT_fno_color_diagnostics) && 3319 !O.matches(options::OPT_fno_diagnostics_color) && 3320 !O.matches(options::OPT_fdiagnostics_color_EQ)) 3321 continue; 3322 3323 (*it)->claim(); 3324 if (O.matches(options::OPT_fcolor_diagnostics) || 3325 O.matches(options::OPT_fdiagnostics_color)) { 3326 ShowColors = Colors_On; 3327 } else if (O.matches(options::OPT_fno_color_diagnostics) || 3328 O.matches(options::OPT_fno_diagnostics_color)) { 3329 ShowColors = Colors_Off; 3330 } else { 3331 assert(O.matches(options::OPT_fdiagnostics_color_EQ)); 3332 StringRef value((*it)->getValue()); 3333 if (value == "always") 3334 ShowColors = Colors_On; 3335 else if (value == "never") 3336 ShowColors = Colors_Off; 3337 else if (value == "auto") 3338 ShowColors = Colors_Auto; 3339 else 3340 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 3341 << ("-fdiagnostics-color=" + value).str(); 3342 } 3343 } 3344 if (ShowColors == Colors_On || 3345 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors())) 3346 CmdArgs.push_back("-fcolor-diagnostics"); 3347 3348 if (!Args.hasFlag(options::OPT_fshow_source_location, 3349 options::OPT_fno_show_source_location)) 3350 CmdArgs.push_back("-fno-show-source-location"); 3351 3352 if (!Args.hasFlag(options::OPT_fshow_column, 3353 options::OPT_fno_show_column, 3354 true)) 3355 CmdArgs.push_back("-fno-show-column"); 3356 3357 if (!Args.hasFlag(options::OPT_fspell_checking, 3358 options::OPT_fno_spell_checking)) 3359 CmdArgs.push_back("-fno-spell-checking"); 3360 3361 3362 // -fno-asm-blocks is default. 3363 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks, 3364 false)) 3365 CmdArgs.push_back("-fasm-blocks"); 3366 3367 // Enable vectorization per default according to the optimization level 3368 // selected. For optimization levels that want vectorization we use the alias 3369 // option to simplify the hasFlag logic. 3370 bool EnableVec = shouldEnableVectorizerAtOLevel(Args); 3371 OptSpecifier VectorizeAliasOption = EnableVec ? options::OPT_O_Group : 3372 options::OPT_fvectorize; 3373 if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption, 3374 options::OPT_fno_vectorize, EnableVec)) 3375 CmdArgs.push_back("-vectorize-loops"); 3376 3377 // -fslp-vectorize is default. 3378 if (Args.hasFlag(options::OPT_fslp_vectorize, 3379 options::OPT_fno_slp_vectorize, true)) 3380 CmdArgs.push_back("-vectorize-slp"); 3381 3382 // -fno-slp-vectorize-aggressive is default. 3383 if (Args.hasFlag(options::OPT_fslp_vectorize_aggressive, 3384 options::OPT_fno_slp_vectorize_aggressive, false)) 3385 CmdArgs.push_back("-vectorize-slp-aggressive"); 3386 3387 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ)) 3388 A->render(Args, CmdArgs); 3389 3390 // -fdollars-in-identifiers default varies depending on platform and 3391 // language; only pass if specified. 3392 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers, 3393 options::OPT_fno_dollars_in_identifiers)) { 3394 if (A->getOption().matches(options::OPT_fdollars_in_identifiers)) 3395 CmdArgs.push_back("-fdollars-in-identifiers"); 3396 else 3397 CmdArgs.push_back("-fno-dollars-in-identifiers"); 3398 } 3399 3400 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for 3401 // practical purposes. 3402 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time, 3403 options::OPT_fno_unit_at_a_time)) { 3404 if (A->getOption().matches(options::OPT_fno_unit_at_a_time)) 3405 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args); 3406 } 3407 3408 if (Args.hasFlag(options::OPT_fapple_pragma_pack, 3409 options::OPT_fno_apple_pragma_pack, false)) 3410 CmdArgs.push_back("-fapple-pragma-pack"); 3411 3412 // le32-specific flags: 3413 // -fno-math-builtin: clang should not convert math builtins to intrinsics 3414 // by default. 3415 if (getToolChain().getArch() == llvm::Triple::le32) { 3416 CmdArgs.push_back("-fno-math-builtin"); 3417 } 3418 3419 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM. 3420 // 3421 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941. 3422#if 0 3423 if (getToolChain().getTriple().isOSDarwin() && 3424 (getToolChain().getArch() == llvm::Triple::arm || 3425 getToolChain().getArch() == llvm::Triple::thumb)) { 3426 if (!Args.hasArg(options::OPT_fbuiltin_strcat)) 3427 CmdArgs.push_back("-fno-builtin-strcat"); 3428 if (!Args.hasArg(options::OPT_fbuiltin_strcpy)) 3429 CmdArgs.push_back("-fno-builtin-strcpy"); 3430 } 3431#endif 3432 3433 // Only allow -traditional or -traditional-cpp outside in preprocessing modes. 3434 if (Arg *A = Args.getLastArg(options::OPT_traditional, 3435 options::OPT_traditional_cpp)) { 3436 if (isa<PreprocessJobAction>(JA)) 3437 CmdArgs.push_back("-traditional-cpp"); 3438 else 3439 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 3440 } 3441 3442 Args.AddLastArg(CmdArgs, options::OPT_dM); 3443 Args.AddLastArg(CmdArgs, options::OPT_dD); 3444 3445 // Handle serialized diagnostics. 3446 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) { 3447 CmdArgs.push_back("-serialize-diagnostic-file"); 3448 CmdArgs.push_back(Args.MakeArgString(A->getValue())); 3449 } 3450 3451 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers)) 3452 CmdArgs.push_back("-fretain-comments-from-system-headers"); 3453 3454 // Forward -fcomment-block-commands to -cc1. 3455 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); 3456 // Forward -fparse-all-comments to -cc1. 3457 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments); 3458 3459 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option 3460 // parser. 3461 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang); 3462 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm), 3463 ie = Args.filtered_end(); it != ie; ++it) { 3464 (*it)->claim(); 3465 3466 // We translate this by hand to the -cc1 argument, since nightly test uses 3467 // it and developers have been trained to spell it with -mllvm. 3468 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns") 3469 CmdArgs.push_back("-disable-llvm-optzns"); 3470 else 3471 (*it)->render(Args, CmdArgs); 3472 } 3473 3474 if (Output.getType() == types::TY_Dependencies) { 3475 // Handled with other dependency code. 3476 } else if (Output.isFilename()) { 3477 CmdArgs.push_back("-o"); 3478 CmdArgs.push_back(Output.getFilename()); 3479 } else { 3480 assert(Output.isNothing() && "Invalid output."); 3481 } 3482 3483 for (InputInfoList::const_iterator 3484 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 3485 const InputInfo &II = *it; 3486 CmdArgs.push_back("-x"); 3487 if (Args.hasArg(options::OPT_rewrite_objc)) 3488 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX)); 3489 else 3490 CmdArgs.push_back(types::getTypeName(II.getType())); 3491 if (II.isFilename()) 3492 CmdArgs.push_back(II.getFilename()); 3493 else 3494 II.getInputArg().renderAsInput(Args, CmdArgs); 3495 } 3496 3497 Args.AddAllArgs(CmdArgs, options::OPT_undef); 3498 3499 const char *Exec = getToolChain().getDriver().getClangProgramPath(); 3500 3501 // Optionally embed the -cc1 level arguments into the debug info, for build 3502 // analysis. 3503 if (getToolChain().UseDwarfDebugFlags()) { 3504 ArgStringList OriginalArgs; 3505 for (ArgList::const_iterator it = Args.begin(), 3506 ie = Args.end(); it != ie; ++it) 3507 (*it)->render(Args, OriginalArgs); 3508 3509 SmallString<256> Flags; 3510 Flags += Exec; 3511 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) { 3512 Flags += " "; 3513 Flags += OriginalArgs[i]; 3514 } 3515 CmdArgs.push_back("-dwarf-debug-flags"); 3516 CmdArgs.push_back(Args.MakeArgString(Flags.str())); 3517 } 3518 3519 // Add the split debug info name to the command lines here so we 3520 // can propagate it to the backend. 3521 bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) && 3522 (getToolChain().getTriple().getOS() == llvm::Triple::Linux) && 3523 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA)); 3524 const char *SplitDwarfOut; 3525 if (SplitDwarf) { 3526 CmdArgs.push_back("-split-dwarf-file"); 3527 SplitDwarfOut = SplitDebugName(Args, Inputs); 3528 CmdArgs.push_back(SplitDwarfOut); 3529 } 3530 3531 // Finally add the compile command to the compilation. 3532 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 3533 3534 // Handle the debug info splitting at object creation time if we're 3535 // creating an object. 3536 // TODO: Currently only works on linux with newer objcopy. 3537 if (SplitDwarf && !isa<CompileJobAction>(JA)) 3538 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut); 3539 3540 if (Arg *A = Args.getLastArg(options::OPT_pg)) 3541 if (Args.hasArg(options::OPT_fomit_frame_pointer)) 3542 D.Diag(diag::err_drv_argument_not_allowed_with) 3543 << "-fomit-frame-pointer" << A->getAsString(Args); 3544 3545 // Claim some arguments which clang supports automatically. 3546 3547 // -fpch-preprocess is used with gcc to add a special marker in the output to 3548 // include the PCH file. Clang's PTH solution is completely transparent, so we 3549 // do not need to deal with it at all. 3550 Args.ClaimAllArgs(options::OPT_fpch_preprocess); 3551 3552 // Claim some arguments which clang doesn't support, but we don't 3553 // care to warn the user about. 3554 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group); 3555 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group); 3556 3557 // Claim ignored clang-cl options. 3558 Args.ClaimAllArgs(options::OPT_cl_ignored_Group); 3559 3560 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c 3561 Args.ClaimAllArgs(options::OPT_use_gold_plugin); 3562 Args.ClaimAllArgs(options::OPT_emit_llvm); 3563} 3564 3565void ClangAs::AddARMTargetArgs(const ArgList &Args, 3566 ArgStringList &CmdArgs) const { 3567 const Driver &D = getToolChain().getDriver(); 3568 llvm::Triple Triple = getToolChain().getTriple(); 3569 3570 // Set the CPU based on -march= and -mcpu=. 3571 CmdArgs.push_back("-target-cpu"); 3572 CmdArgs.push_back(Args.MakeArgString(getARMTargetCPU(Args, Triple))); 3573 3574 // Honor -mfpu=. 3575 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) 3576 addFPUArgs(D, A, Args, CmdArgs); 3577 3578 // Honor -mfpmath=. 3579 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) 3580 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple)); 3581} 3582 3583void ClangAs::AddX86TargetArgs(const ArgList &Args, 3584 ArgStringList &CmdArgs) const { 3585 // Set the CPU based on -march=. 3586 if (const char *CPUName = getX86TargetCPU(Args, getToolChain().getTriple())) { 3587 CmdArgs.push_back("-target-cpu"); 3588 CmdArgs.push_back(CPUName); 3589 } 3590} 3591 3592/// Add options related to the Objective-C runtime/ABI. 3593/// 3594/// Returns true if the runtime is non-fragile. 3595ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, 3596 ArgStringList &cmdArgs, 3597 RewriteKind rewriteKind) const { 3598 // Look for the controlling runtime option. 3599 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime, 3600 options::OPT_fgnu_runtime, 3601 options::OPT_fobjc_runtime_EQ); 3602 3603 // Just forward -fobjc-runtime= to the frontend. This supercedes 3604 // options about fragility. 3605 if (runtimeArg && 3606 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) { 3607 ObjCRuntime runtime; 3608 StringRef value = runtimeArg->getValue(); 3609 if (runtime.tryParse(value)) { 3610 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime) 3611 << value; 3612 } 3613 3614 runtimeArg->render(args, cmdArgs); 3615 return runtime; 3616 } 3617 3618 // Otherwise, we'll need the ABI "version". Version numbers are 3619 // slightly confusing for historical reasons: 3620 // 1 - Traditional "fragile" ABI 3621 // 2 - Non-fragile ABI, version 1 3622 // 3 - Non-fragile ABI, version 2 3623 unsigned objcABIVersion = 1; 3624 // If -fobjc-abi-version= is present, use that to set the version. 3625 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) { 3626 StringRef value = abiArg->getValue(); 3627 if (value == "1") 3628 objcABIVersion = 1; 3629 else if (value == "2") 3630 objcABIVersion = 2; 3631 else if (value == "3") 3632 objcABIVersion = 3; 3633 else 3634 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 3635 << value; 3636 } else { 3637 // Otherwise, determine if we are using the non-fragile ABI. 3638 bool nonFragileABIIsDefault = 3639 (rewriteKind == RK_NonFragile || 3640 (rewriteKind == RK_None && 3641 getToolChain().IsObjCNonFragileABIDefault())); 3642 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi, 3643 options::OPT_fno_objc_nonfragile_abi, 3644 nonFragileABIIsDefault)) { 3645 // Determine the non-fragile ABI version to use. 3646#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO 3647 unsigned nonFragileABIVersion = 1; 3648#else 3649 unsigned nonFragileABIVersion = 2; 3650#endif 3651 3652 if (Arg *abiArg = args.getLastArg( 3653 options::OPT_fobjc_nonfragile_abi_version_EQ)) { 3654 StringRef value = abiArg->getValue(); 3655 if (value == "1") 3656 nonFragileABIVersion = 1; 3657 else if (value == "2") 3658 nonFragileABIVersion = 2; 3659 else 3660 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 3661 << value; 3662 } 3663 3664 objcABIVersion = 1 + nonFragileABIVersion; 3665 } else { 3666 objcABIVersion = 1; 3667 } 3668 } 3669 3670 // We don't actually care about the ABI version other than whether 3671 // it's non-fragile. 3672 bool isNonFragile = objcABIVersion != 1; 3673 3674 // If we have no runtime argument, ask the toolchain for its default runtime. 3675 // However, the rewriter only really supports the Mac runtime, so assume that. 3676 ObjCRuntime runtime; 3677 if (!runtimeArg) { 3678 switch (rewriteKind) { 3679 case RK_None: 3680 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 3681 break; 3682 case RK_Fragile: 3683 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple()); 3684 break; 3685 case RK_NonFragile: 3686 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 3687 break; 3688 } 3689 3690 // -fnext-runtime 3691 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) { 3692 // On Darwin, make this use the default behavior for the toolchain. 3693 if (getToolChain().getTriple().isOSDarwin()) { 3694 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 3695 3696 // Otherwise, build for a generic macosx port. 3697 } else { 3698 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 3699 } 3700 3701 // -fgnu-runtime 3702 } else { 3703 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime)); 3704 // Legacy behaviour is to target the gnustep runtime if we are i 3705 // non-fragile mode or the GCC runtime in fragile mode. 3706 if (isNonFragile) 3707 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6)); 3708 else 3709 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple()); 3710 } 3711 3712 cmdArgs.push_back(args.MakeArgString( 3713 "-fobjc-runtime=" + runtime.getAsString())); 3714 return runtime; 3715} 3716 3717void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const { 3718 unsigned RTOptionID = options::OPT__SLASH_MT; 3719 3720 if (Arg *A = Args.getLastArg(options::OPT__SLASH_MD, 3721 options::OPT__SLASH_MDd, 3722 options::OPT__SLASH_MT, 3723 options::OPT__SLASH_MTd)) { 3724 RTOptionID = A->getOption().getID(); 3725 } 3726 3727 switch(RTOptionID) { 3728 case options::OPT__SLASH_MD: 3729 CmdArgs.push_back("-D_MT"); 3730 CmdArgs.push_back("-D_DLL"); 3731 CmdArgs.push_back("--dependent-lib=msvcrt"); 3732 break; 3733 case options::OPT__SLASH_MDd: 3734 CmdArgs.push_back("-D_DEBUG"); 3735 CmdArgs.push_back("-D_MT"); 3736 CmdArgs.push_back("-D_DLL"); 3737 CmdArgs.push_back("--dependent-lib=msvcrtd"); 3738 break; 3739 case options::OPT__SLASH_MT: 3740 CmdArgs.push_back("-D_MT"); 3741 CmdArgs.push_back("--dependent-lib=libcmt"); 3742 break; 3743 case options::OPT__SLASH_MTd: 3744 CmdArgs.push_back("-D_DEBUG"); 3745 CmdArgs.push_back("-D_MT"); 3746 CmdArgs.push_back("--dependent-lib=libcmtd"); 3747 break; 3748 default: 3749 llvm_unreachable("Unexpected option ID."); 3750 } 3751 3752 // This provides POSIX compatibility (maps 'open' to '_open'), which most 3753 // users want. The /Za flag to cl.exe turns this off, but it's not 3754 // implemented in clang. 3755 CmdArgs.push_back("--dependent-lib=oldnames"); 3756 3757 // FIXME: Make this default for the win32 triple. 3758 CmdArgs.push_back("-cxx-abi"); 3759 CmdArgs.push_back("microsoft"); 3760 3761 if (Arg *A = Args.getLastArg(options::OPT_show_includes)) 3762 A->render(Args, CmdArgs); 3763} 3764 3765void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, 3766 const InputInfo &Output, 3767 const InputInfoList &Inputs, 3768 const ArgList &Args, 3769 const char *LinkingOutput) const { 3770 ArgStringList CmdArgs; 3771 3772 assert(Inputs.size() == 1 && "Unexpected number of inputs."); 3773 const InputInfo &Input = Inputs[0]; 3774 3775 // Don't warn about "clang -w -c foo.s" 3776 Args.ClaimAllArgs(options::OPT_w); 3777 // and "clang -emit-llvm -c foo.s" 3778 Args.ClaimAllArgs(options::OPT_emit_llvm); 3779 // and "clang -use-gold-plugin -c foo.s" 3780 Args.ClaimAllArgs(options::OPT_use_gold_plugin); 3781 3782 // Invoke ourselves in -cc1as mode. 3783 // 3784 // FIXME: Implement custom jobs for internal actions. 3785 CmdArgs.push_back("-cc1as"); 3786 3787 // Add the "effective" target triple. 3788 CmdArgs.push_back("-triple"); 3789 std::string TripleStr = 3790 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType()); 3791 CmdArgs.push_back(Args.MakeArgString(TripleStr)); 3792 3793 // Set the output mode, we currently only expect to be used as a real 3794 // assembler. 3795 CmdArgs.push_back("-filetype"); 3796 CmdArgs.push_back("obj"); 3797 3798 // Set the main file name, so that debug info works even with 3799 // -save-temps or preprocessed assembly. 3800 CmdArgs.push_back("-main-file-name"); 3801 CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs)); 3802 3803 // Add target specific cpu and features flags. 3804 switch(getToolChain().getArch()) { 3805 default: 3806 break; 3807 3808 case llvm::Triple::arm: 3809 case llvm::Triple::thumb: 3810 AddARMTargetArgs(Args, CmdArgs); 3811 break; 3812 3813 case llvm::Triple::x86: 3814 case llvm::Triple::x86_64: 3815 AddX86TargetArgs(Args, CmdArgs); 3816 break; 3817 } 3818 3819 // Ignore explicit -force_cpusubtype_ALL option. 3820 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL); 3821 3822 // Determine the original source input. 3823 const Action *SourceAction = &JA; 3824 while (SourceAction->getKind() != Action::InputClass) { 3825 assert(!SourceAction->getInputs().empty() && "unexpected root action!"); 3826 SourceAction = SourceAction->getInputs()[0]; 3827 } 3828 3829 // Forward -g and handle debug info related flags, assuming we are dealing 3830 // with an actual assembly file. 3831 if (SourceAction->getType() == types::TY_Asm || 3832 SourceAction->getType() == types::TY_PP_Asm) { 3833 Args.ClaimAllArgs(options::OPT_g_Group); 3834 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) 3835 if (!A->getOption().matches(options::OPT_g0)) 3836 CmdArgs.push_back("-g"); 3837 3838 // Add the -fdebug-compilation-dir flag if needed. 3839 addDebugCompDirArg(Args, CmdArgs); 3840 3841 // Set the AT_producer to the clang version when using the integrated 3842 // assembler on assembly source files. 3843 CmdArgs.push_back("-dwarf-debug-producer"); 3844 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion())); 3845 } 3846 3847 // Optionally embed the -cc1as level arguments into the debug info, for build 3848 // analysis. 3849 if (getToolChain().UseDwarfDebugFlags()) { 3850 ArgStringList OriginalArgs; 3851 for (ArgList::const_iterator it = Args.begin(), 3852 ie = Args.end(); it != ie; ++it) 3853 (*it)->render(Args, OriginalArgs); 3854 3855 SmallString<256> Flags; 3856 const char *Exec = getToolChain().getDriver().getClangProgramPath(); 3857 Flags += Exec; 3858 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) { 3859 Flags += " "; 3860 Flags += OriginalArgs[i]; 3861 } 3862 CmdArgs.push_back("-dwarf-debug-flags"); 3863 CmdArgs.push_back(Args.MakeArgString(Flags.str())); 3864 } 3865 3866 // FIXME: Add -static support, once we have it. 3867 3868 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, 3869 getToolChain().getDriver()); 3870 3871 Args.AddAllArgs(CmdArgs, options::OPT_mllvm); 3872 3873 assert(Output.isFilename() && "Unexpected lipo output."); 3874 CmdArgs.push_back("-o"); 3875 CmdArgs.push_back(Output.getFilename()); 3876 3877 assert(Input.isFilename() && "Invalid input."); 3878 CmdArgs.push_back(Input.getFilename()); 3879 3880 const char *Exec = getToolChain().getDriver().getClangProgramPath(); 3881 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 3882 3883 // Handle the debug info splitting at object creation time if we're 3884 // creating an object. 3885 // TODO: Currently only works on linux with newer objcopy. 3886 if (Args.hasArg(options::OPT_gsplit_dwarf) && 3887 (getToolChain().getTriple().getOS() == llvm::Triple::Linux)) 3888 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, 3889 SplitDebugName(Args, Inputs)); 3890} 3891 3892void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA, 3893 const InputInfo &Output, 3894 const InputInfoList &Inputs, 3895 const ArgList &Args, 3896 const char *LinkingOutput) const { 3897 const Driver &D = getToolChain().getDriver(); 3898 ArgStringList CmdArgs; 3899 3900 for (ArgList::const_iterator 3901 it = Args.begin(), ie = Args.end(); it != ie; ++it) { 3902 Arg *A = *it; 3903 if (forwardToGCC(A->getOption())) { 3904 // Don't forward any -g arguments to assembly steps. 3905 if (isa<AssembleJobAction>(JA) && 3906 A->getOption().matches(options::OPT_g_Group)) 3907 continue; 3908 3909 // It is unfortunate that we have to claim here, as this means 3910 // we will basically never report anything interesting for 3911 // platforms using a generic gcc, even if we are just using gcc 3912 // to get to the assembler. 3913 A->claim(); 3914 A->render(Args, CmdArgs); 3915 } 3916 } 3917 3918 RenderExtraToolArgs(JA, CmdArgs); 3919 3920 // If using a driver driver, force the arch. 3921 llvm::Triple::ArchType Arch = getToolChain().getArch(); 3922 if (getToolChain().getTriple().isOSDarwin()) { 3923 CmdArgs.push_back("-arch"); 3924 3925 // FIXME: Remove these special cases. 3926 if (Arch == llvm::Triple::ppc) 3927 CmdArgs.push_back("ppc"); 3928 else if (Arch == llvm::Triple::ppc64) 3929 CmdArgs.push_back("ppc64"); 3930 else if (Arch == llvm::Triple::ppc64le) 3931 CmdArgs.push_back("ppc64le"); 3932 else 3933 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName())); 3934 } 3935 3936 // Try to force gcc to match the tool chain we want, if we recognize 3937 // the arch. 3938 // 3939 // FIXME: The triple class should directly provide the information we want 3940 // here. 3941 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc) 3942 CmdArgs.push_back("-m32"); 3943 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64 || 3944 Arch == llvm::Triple::ppc64le) 3945 CmdArgs.push_back("-m64"); 3946 3947 if (Output.isFilename()) { 3948 CmdArgs.push_back("-o"); 3949 CmdArgs.push_back(Output.getFilename()); 3950 } else { 3951 assert(Output.isNothing() && "Unexpected output"); 3952 CmdArgs.push_back("-fsyntax-only"); 3953 } 3954 3955 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 3956 options::OPT_Xassembler); 3957 3958 // Only pass -x if gcc will understand it; otherwise hope gcc 3959 // understands the suffix correctly. The main use case this would go 3960 // wrong in is for linker inputs if they happened to have an odd 3961 // suffix; really the only way to get this to happen is a command 3962 // like '-x foobar a.c' which will treat a.c like a linker input. 3963 // 3964 // FIXME: For the linker case specifically, can we safely convert 3965 // inputs into '-Wl,' options? 3966 for (InputInfoList::const_iterator 3967 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 3968 const InputInfo &II = *it; 3969 3970 // Don't try to pass LLVM or AST inputs to a generic gcc. 3971 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR || 3972 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC) 3973 D.Diag(diag::err_drv_no_linker_llvm_support) 3974 << getToolChain().getTripleString(); 3975 else if (II.getType() == types::TY_AST) 3976 D.Diag(diag::err_drv_no_ast_support) 3977 << getToolChain().getTripleString(); 3978 else if (II.getType() == types::TY_ModuleFile) 3979 D.Diag(diag::err_drv_no_module_support) 3980 << getToolChain().getTripleString(); 3981 3982 if (types::canTypeBeUserSpecified(II.getType())) { 3983 CmdArgs.push_back("-x"); 3984 CmdArgs.push_back(types::getTypeName(II.getType())); 3985 } 3986 3987 if (II.isFilename()) 3988 CmdArgs.push_back(II.getFilename()); 3989 else { 3990 const Arg &A = II.getInputArg(); 3991 3992 // Reverse translate some rewritten options. 3993 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) { 3994 CmdArgs.push_back("-lstdc++"); 3995 continue; 3996 } 3997 3998 // Don't render as input, we need gcc to do the translations. 3999 A.render(Args, CmdArgs); 4000 } 4001 } 4002 4003 const std::string customGCCName = D.getCCCGenericGCCName(); 4004 const char *GCCName; 4005 if (!customGCCName.empty()) 4006 GCCName = customGCCName.c_str(); 4007 else if (D.CCCIsCXX()) { 4008 GCCName = "g++"; 4009 } else 4010 GCCName = "gcc"; 4011 4012 const char *Exec = 4013 Args.MakeArgString(getToolChain().GetProgramPath(GCCName)); 4014 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4015} 4016 4017void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA, 4018 ArgStringList &CmdArgs) const { 4019 CmdArgs.push_back("-E"); 4020} 4021 4022void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA, 4023 ArgStringList &CmdArgs) const { 4024 // The type is good enough. 4025} 4026 4027void gcc::Compile::RenderExtraToolArgs(const JobAction &JA, 4028 ArgStringList &CmdArgs) const { 4029 const Driver &D = getToolChain().getDriver(); 4030 4031 // If -flto, etc. are present then make sure not to force assembly output. 4032 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR || 4033 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC) 4034 CmdArgs.push_back("-c"); 4035 else { 4036 if (JA.getType() != types::TY_PP_Asm) 4037 D.Diag(diag::err_drv_invalid_gcc_output_type) 4038 << getTypeName(JA.getType()); 4039 4040 CmdArgs.push_back("-S"); 4041 } 4042} 4043 4044void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA, 4045 ArgStringList &CmdArgs) const { 4046 CmdArgs.push_back("-c"); 4047} 4048 4049void gcc::Link::RenderExtraToolArgs(const JobAction &JA, 4050 ArgStringList &CmdArgs) const { 4051 // The types are (hopefully) good enough. 4052} 4053 4054// Hexagon tools start. 4055void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA, 4056 ArgStringList &CmdArgs) const { 4057 4058} 4059void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 4060 const InputInfo &Output, 4061 const InputInfoList &Inputs, 4062 const ArgList &Args, 4063 const char *LinkingOutput) const { 4064 4065 const Driver &D = getToolChain().getDriver(); 4066 ArgStringList CmdArgs; 4067 4068 std::string MarchString = "-march="; 4069 MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args); 4070 CmdArgs.push_back(Args.MakeArgString(MarchString)); 4071 4072 RenderExtraToolArgs(JA, CmdArgs); 4073 4074 if (Output.isFilename()) { 4075 CmdArgs.push_back("-o"); 4076 CmdArgs.push_back(Output.getFilename()); 4077 } else { 4078 assert(Output.isNothing() && "Unexpected output"); 4079 CmdArgs.push_back("-fsyntax-only"); 4080 } 4081 4082 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args); 4083 if (!SmallDataThreshold.empty()) 4084 CmdArgs.push_back( 4085 Args.MakeArgString(std::string("-G") + SmallDataThreshold)); 4086 4087 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 4088 options::OPT_Xassembler); 4089 4090 // Only pass -x if gcc will understand it; otherwise hope gcc 4091 // understands the suffix correctly. The main use case this would go 4092 // wrong in is for linker inputs if they happened to have an odd 4093 // suffix; really the only way to get this to happen is a command 4094 // like '-x foobar a.c' which will treat a.c like a linker input. 4095 // 4096 // FIXME: For the linker case specifically, can we safely convert 4097 // inputs into '-Wl,' options? 4098 for (InputInfoList::const_iterator 4099 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 4100 const InputInfo &II = *it; 4101 4102 // Don't try to pass LLVM or AST inputs to a generic gcc. 4103 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR || 4104 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC) 4105 D.Diag(clang::diag::err_drv_no_linker_llvm_support) 4106 << getToolChain().getTripleString(); 4107 else if (II.getType() == types::TY_AST) 4108 D.Diag(clang::diag::err_drv_no_ast_support) 4109 << getToolChain().getTripleString(); 4110 else if (II.getType() == types::TY_ModuleFile) 4111 D.Diag(diag::err_drv_no_module_support) 4112 << getToolChain().getTripleString(); 4113 4114 if (II.isFilename()) 4115 CmdArgs.push_back(II.getFilename()); 4116 else 4117 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ? 4118 II.getInputArg().render(Args, CmdArgs); 4119 } 4120 4121 const char *GCCName = "hexagon-as"; 4122 const char *Exec = 4123 Args.MakeArgString(getToolChain().GetProgramPath(GCCName)); 4124 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4125 4126} 4127void hexagon::Link::RenderExtraToolArgs(const JobAction &JA, 4128 ArgStringList &CmdArgs) const { 4129 // The types are (hopefully) good enough. 4130} 4131 4132void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA, 4133 const InputInfo &Output, 4134 const InputInfoList &Inputs, 4135 const ArgList &Args, 4136 const char *LinkingOutput) const { 4137 4138 const toolchains::Hexagon_TC& ToolChain = 4139 static_cast<const toolchains::Hexagon_TC&>(getToolChain()); 4140 const Driver &D = ToolChain.getDriver(); 4141 4142 ArgStringList CmdArgs; 4143 4144 //---------------------------------------------------------------------------- 4145 // 4146 //---------------------------------------------------------------------------- 4147 bool hasStaticArg = Args.hasArg(options::OPT_static); 4148 bool buildingLib = Args.hasArg(options::OPT_shared); 4149 bool buildPIE = Args.hasArg(options::OPT_pie); 4150 bool incStdLib = !Args.hasArg(options::OPT_nostdlib); 4151 bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles); 4152 bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs); 4153 bool useShared = buildingLib && !hasStaticArg; 4154 4155 //---------------------------------------------------------------------------- 4156 // Silence warnings for various options 4157 //---------------------------------------------------------------------------- 4158 4159 Args.ClaimAllArgs(options::OPT_g_Group); 4160 Args.ClaimAllArgs(options::OPT_emit_llvm); 4161 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already 4162 // handled somewhere else. 4163 Args.ClaimAllArgs(options::OPT_static_libgcc); 4164 4165 //---------------------------------------------------------------------------- 4166 // 4167 //---------------------------------------------------------------------------- 4168 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(), 4169 e = ToolChain.ExtraOpts.end(); 4170 i != e; ++i) 4171 CmdArgs.push_back(i->c_str()); 4172 4173 std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args); 4174 CmdArgs.push_back(Args.MakeArgString("-m" + MarchString)); 4175 4176 if (buildingLib) { 4177 CmdArgs.push_back("-shared"); 4178 CmdArgs.push_back("-call_shared"); // should be the default, but doing as 4179 // hexagon-gcc does 4180 } 4181 4182 if (hasStaticArg) 4183 CmdArgs.push_back("-static"); 4184 4185 if (buildPIE && !buildingLib) 4186 CmdArgs.push_back("-pie"); 4187 4188 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args); 4189 if (!SmallDataThreshold.empty()) { 4190 CmdArgs.push_back( 4191 Args.MakeArgString(std::string("-G") + SmallDataThreshold)); 4192 } 4193 4194 //---------------------------------------------------------------------------- 4195 // 4196 //---------------------------------------------------------------------------- 4197 CmdArgs.push_back("-o"); 4198 CmdArgs.push_back(Output.getFilename()); 4199 4200 const std::string MarchSuffix = "/" + MarchString; 4201 const std::string G0Suffix = "/G0"; 4202 const std::string MarchG0Suffix = MarchSuffix + G0Suffix; 4203 const std::string RootDir = toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir) 4204 + "/"; 4205 const std::string StartFilesDir = RootDir 4206 + "hexagon/lib" 4207 + (buildingLib 4208 ? MarchG0Suffix : MarchSuffix); 4209 4210 //---------------------------------------------------------------------------- 4211 // moslib 4212 //---------------------------------------------------------------------------- 4213 std::vector<std::string> oslibs; 4214 bool hasStandalone= false; 4215 4216 for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ), 4217 ie = Args.filtered_end(); it != ie; ++it) { 4218 (*it)->claim(); 4219 oslibs.push_back((*it)->getValue()); 4220 hasStandalone = hasStandalone || (oslibs.back() == "standalone"); 4221 } 4222 if (oslibs.empty()) { 4223 oslibs.push_back("standalone"); 4224 hasStandalone = true; 4225 } 4226 4227 //---------------------------------------------------------------------------- 4228 // Start Files 4229 //---------------------------------------------------------------------------- 4230 if (incStdLib && incStartFiles) { 4231 4232 if (!buildingLib) { 4233 if (hasStandalone) { 4234 CmdArgs.push_back( 4235 Args.MakeArgString(StartFilesDir + "/crt0_standalone.o")); 4236 } 4237 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o")); 4238 } 4239 std::string initObj = useShared ? "/initS.o" : "/init.o"; 4240 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj)); 4241 } 4242 4243 //---------------------------------------------------------------------------- 4244 // Library Search Paths 4245 //---------------------------------------------------------------------------- 4246 const ToolChain::path_list &LibPaths = ToolChain.getFilePaths(); 4247 for (ToolChain::path_list::const_iterator 4248 i = LibPaths.begin(), 4249 e = LibPaths.end(); 4250 i != e; 4251 ++i) 4252 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i)); 4253 4254 //---------------------------------------------------------------------------- 4255 // 4256 //---------------------------------------------------------------------------- 4257 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 4258 Args.AddAllArgs(CmdArgs, options::OPT_e); 4259 Args.AddAllArgs(CmdArgs, options::OPT_s); 4260 Args.AddAllArgs(CmdArgs, options::OPT_t); 4261 Args.AddAllArgs(CmdArgs, options::OPT_u_Group); 4262 4263 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs); 4264 4265 //---------------------------------------------------------------------------- 4266 // Libraries 4267 //---------------------------------------------------------------------------- 4268 if (incStdLib && incDefLibs) { 4269 if (D.CCCIsCXX()) { 4270 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 4271 CmdArgs.push_back("-lm"); 4272 } 4273 4274 CmdArgs.push_back("--start-group"); 4275 4276 if (!buildingLib) { 4277 for(std::vector<std::string>::iterator i = oslibs.begin(), 4278 e = oslibs.end(); i != e; ++i) 4279 CmdArgs.push_back(Args.MakeArgString("-l" + *i)); 4280 CmdArgs.push_back("-lc"); 4281 } 4282 CmdArgs.push_back("-lgcc"); 4283 4284 CmdArgs.push_back("--end-group"); 4285 } 4286 4287 //---------------------------------------------------------------------------- 4288 // End files 4289 //---------------------------------------------------------------------------- 4290 if (incStdLib && incStartFiles) { 4291 std::string finiObj = useShared ? "/finiS.o" : "/fini.o"; 4292 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj)); 4293 } 4294 4295 std::string Linker = ToolChain.GetProgramPath("hexagon-ld"); 4296 C.addCommand( 4297 new Command( 4298 JA, *this, 4299 Args.MakeArgString(Linker), CmdArgs)); 4300} 4301// Hexagon tools end. 4302 4303llvm::Triple::ArchType darwin::getArchTypeForDarwinArchName(StringRef Str) { 4304 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for 4305 // archs which Darwin doesn't use. 4306 4307 // The matching this routine does is fairly pointless, since it is neither the 4308 // complete architecture list, nor a reasonable subset. The problem is that 4309 // historically the driver driver accepts this and also ties its -march= 4310 // handling to the architecture name, so we need to be careful before removing 4311 // support for it. 4312 4313 // This code must be kept in sync with Clang's Darwin specific argument 4314 // translation. 4315 4316 return llvm::StringSwitch<llvm::Triple::ArchType>(Str) 4317 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc) 4318 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc) 4319 .Case("ppc64", llvm::Triple::ppc64) 4320 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86) 4321 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4", 4322 llvm::Triple::x86) 4323 .Case("x86_64", llvm::Triple::x86_64) 4324 // This is derived from the driver driver. 4325 .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm) 4326 .Cases("armv7", "armv7em", "armv7f", "armv7k", "armv7m", llvm::Triple::arm) 4327 .Cases("armv7s", "xscale", llvm::Triple::arm) 4328 .Case("r600", llvm::Triple::r600) 4329 .Case("nvptx", llvm::Triple::nvptx) 4330 .Case("nvptx64", llvm::Triple::nvptx64) 4331 .Case("amdil", llvm::Triple::amdil) 4332 .Case("spir", llvm::Triple::spir) 4333 .Default(llvm::Triple::UnknownArch); 4334} 4335 4336const char *Clang::getBaseInputName(const ArgList &Args, 4337 const InputInfoList &Inputs) { 4338 return Args.MakeArgString( 4339 llvm::sys::path::filename(Inputs[0].getBaseInput())); 4340} 4341 4342const char *Clang::getBaseInputStem(const ArgList &Args, 4343 const InputInfoList &Inputs) { 4344 const char *Str = getBaseInputName(Args, Inputs); 4345 4346 if (const char *End = strrchr(Str, '.')) 4347 return Args.MakeArgString(std::string(Str, End)); 4348 4349 return Str; 4350} 4351 4352const char *Clang::getDependencyFileName(const ArgList &Args, 4353 const InputInfoList &Inputs) { 4354 // FIXME: Think about this more. 4355 std::string Res; 4356 4357 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 4358 std::string Str(OutputOpt->getValue()); 4359 Res = Str.substr(0, Str.rfind('.')); 4360 } else { 4361 Res = getBaseInputStem(Args, Inputs); 4362 } 4363 return Args.MakeArgString(Res + ".d"); 4364} 4365 4366void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 4367 const InputInfo &Output, 4368 const InputInfoList &Inputs, 4369 const ArgList &Args, 4370 const char *LinkingOutput) const { 4371 ArgStringList CmdArgs; 4372 4373 assert(Inputs.size() == 1 && "Unexpected number of inputs."); 4374 const InputInfo &Input = Inputs[0]; 4375 4376 // Determine the original source input. 4377 const Action *SourceAction = &JA; 4378 while (SourceAction->getKind() != Action::InputClass) { 4379 assert(!SourceAction->getInputs().empty() && "unexpected root action!"); 4380 SourceAction = SourceAction->getInputs()[0]; 4381 } 4382 4383 // Forward -g, assuming we are dealing with an actual assembly file. 4384 if (SourceAction->getType() == types::TY_Asm || 4385 SourceAction->getType() == types::TY_PP_Asm) { 4386 if (Args.hasArg(options::OPT_gstabs)) 4387 CmdArgs.push_back("--gstabs"); 4388 else if (Args.hasArg(options::OPT_g_Group)) 4389 CmdArgs.push_back("-g"); 4390 } 4391 4392 // Derived from asm spec. 4393 AddDarwinArch(Args, CmdArgs); 4394 4395 // Use -force_cpusubtype_ALL on x86 by default. 4396 if (getToolChain().getArch() == llvm::Triple::x86 || 4397 getToolChain().getArch() == llvm::Triple::x86_64 || 4398 Args.hasArg(options::OPT_force__cpusubtype__ALL)) 4399 CmdArgs.push_back("-force_cpusubtype_ALL"); 4400 4401 if (getToolChain().getArch() != llvm::Triple::x86_64 && 4402 (((Args.hasArg(options::OPT_mkernel) || 4403 Args.hasArg(options::OPT_fapple_kext)) && 4404 (!getDarwinToolChain().isTargetIPhoneOS() || 4405 getDarwinToolChain().isIPhoneOSVersionLT(6, 0))) || 4406 Args.hasArg(options::OPT_static))) 4407 CmdArgs.push_back("-static"); 4408 4409 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 4410 options::OPT_Xassembler); 4411 4412 assert(Output.isFilename() && "Unexpected lipo output."); 4413 CmdArgs.push_back("-o"); 4414 CmdArgs.push_back(Output.getFilename()); 4415 4416 assert(Input.isFilename() && "Invalid input."); 4417 CmdArgs.push_back(Input.getFilename()); 4418 4419 // asm_final spec is empty. 4420 4421 const char *Exec = 4422 Args.MakeArgString(getToolChain().GetProgramPath("as")); 4423 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4424} 4425 4426void darwin::DarwinTool::anchor() {} 4427 4428void darwin::DarwinTool::AddDarwinArch(const ArgList &Args, 4429 ArgStringList &CmdArgs) const { 4430 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args); 4431 4432 // Derived from darwin_arch spec. 4433 CmdArgs.push_back("-arch"); 4434 CmdArgs.push_back(Args.MakeArgString(ArchName)); 4435 4436 // FIXME: Is this needed anymore? 4437 if (ArchName == "arm") 4438 CmdArgs.push_back("-force_cpusubtype_ALL"); 4439} 4440 4441bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const { 4442 // We only need to generate a temp path for LTO if we aren't compiling object 4443 // files. When compiling source files, we run 'dsymutil' after linking. We 4444 // don't run 'dsymutil' when compiling object files. 4445 for (InputInfoList::const_iterator 4446 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) 4447 if (it->getType() != types::TY_Object) 4448 return true; 4449 4450 return false; 4451} 4452 4453void darwin::Link::AddLinkArgs(Compilation &C, 4454 const ArgList &Args, 4455 ArgStringList &CmdArgs, 4456 const InputInfoList &Inputs) const { 4457 const Driver &D = getToolChain().getDriver(); 4458 const toolchains::Darwin &DarwinTC = getDarwinToolChain(); 4459 4460 unsigned Version[3] = { 0, 0, 0 }; 4461 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) { 4462 bool HadExtra; 4463 if (!Driver::GetReleaseVersion(A->getValue(), Version[0], 4464 Version[1], Version[2], HadExtra) || 4465 HadExtra) 4466 D.Diag(diag::err_drv_invalid_version_number) 4467 << A->getAsString(Args); 4468 } 4469 4470 // Newer linkers support -demangle, pass it if supported and not disabled by 4471 // the user. 4472 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) { 4473 // Don't pass -demangle to ld_classic. 4474 // 4475 // FIXME: This is a temporary workaround, ld should be handling this. 4476 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 && 4477 Args.hasArg(options::OPT_static)); 4478 if (getToolChain().getArch() == llvm::Triple::x86) { 4479 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker, 4480 options::OPT_Wl_COMMA), 4481 ie = Args.filtered_end(); it != ie; ++it) { 4482 const Arg *A = *it; 4483 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) 4484 if (StringRef(A->getValue(i)) == "-kext") 4485 UsesLdClassic = true; 4486 } 4487 } 4488 if (!UsesLdClassic) 4489 CmdArgs.push_back("-demangle"); 4490 } 4491 4492 if (Args.hasArg(options::OPT_rdynamic) && Version[0] >= 137) 4493 CmdArgs.push_back("-export_dynamic"); 4494 4495 // If we are using LTO, then automatically create a temporary file path for 4496 // the linker to use, so that it's lifetime will extend past a possible 4497 // dsymutil step. 4498 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) { 4499 const char *TmpPath = C.getArgs().MakeArgString( 4500 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object))); 4501 C.addTempFile(TmpPath); 4502 CmdArgs.push_back("-object_path_lto"); 4503 CmdArgs.push_back(TmpPath); 4504 } 4505 4506 // Derived from the "link" spec. 4507 Args.AddAllArgs(CmdArgs, options::OPT_static); 4508 if (!Args.hasArg(options::OPT_static)) 4509 CmdArgs.push_back("-dynamic"); 4510 if (Args.hasArg(options::OPT_fgnu_runtime)) { 4511 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu 4512 // here. How do we wish to handle such things? 4513 } 4514 4515 if (!Args.hasArg(options::OPT_dynamiclib)) { 4516 AddDarwinArch(Args, CmdArgs); 4517 // FIXME: Why do this only on this path? 4518 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL); 4519 4520 Args.AddLastArg(CmdArgs, options::OPT_bundle); 4521 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader); 4522 Args.AddAllArgs(CmdArgs, options::OPT_client__name); 4523 4524 Arg *A; 4525 if ((A = Args.getLastArg(options::OPT_compatibility__version)) || 4526 (A = Args.getLastArg(options::OPT_current__version)) || 4527 (A = Args.getLastArg(options::OPT_install__name))) 4528 D.Diag(diag::err_drv_argument_only_allowed_with) 4529 << A->getAsString(Args) << "-dynamiclib"; 4530 4531 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace); 4532 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs); 4533 Args.AddLastArg(CmdArgs, options::OPT_private__bundle); 4534 } else { 4535 CmdArgs.push_back("-dylib"); 4536 4537 Arg *A; 4538 if ((A = Args.getLastArg(options::OPT_bundle)) || 4539 (A = Args.getLastArg(options::OPT_bundle__loader)) || 4540 (A = Args.getLastArg(options::OPT_client__name)) || 4541 (A = Args.getLastArg(options::OPT_force__flat__namespace)) || 4542 (A = Args.getLastArg(options::OPT_keep__private__externs)) || 4543 (A = Args.getLastArg(options::OPT_private__bundle))) 4544 D.Diag(diag::err_drv_argument_not_allowed_with) 4545 << A->getAsString(Args) << "-dynamiclib"; 4546 4547 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version, 4548 "-dylib_compatibility_version"); 4549 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version, 4550 "-dylib_current_version"); 4551 4552 AddDarwinArch(Args, CmdArgs); 4553 4554 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name, 4555 "-dylib_install_name"); 4556 } 4557 4558 Args.AddLastArg(CmdArgs, options::OPT_all__load); 4559 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client); 4560 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load); 4561 if (DarwinTC.isTargetIPhoneOS()) 4562 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal); 4563 Args.AddLastArg(CmdArgs, options::OPT_dead__strip); 4564 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms); 4565 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file); 4566 Args.AddLastArg(CmdArgs, options::OPT_dynamic); 4567 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list); 4568 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace); 4569 Args.AddAllArgs(CmdArgs, options::OPT_force__load); 4570 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names); 4571 Args.AddAllArgs(CmdArgs, options::OPT_image__base); 4572 Args.AddAllArgs(CmdArgs, options::OPT_init); 4573 4574 // Add the deployment target. 4575 VersionTuple TargetVersion = DarwinTC.getTargetVersion(); 4576 4577 // If we had an explicit -mios-simulator-version-min argument, honor that, 4578 // otherwise use the traditional deployment targets. We can't just check the 4579 // is-sim attribute because existing code follows this path, and the linker 4580 // may not handle the argument. 4581 // 4582 // FIXME: We may be able to remove this, once we can verify no one depends on 4583 // it. 4584 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ)) 4585 CmdArgs.push_back("-ios_simulator_version_min"); 4586 else if (DarwinTC.isTargetIPhoneOS()) 4587 CmdArgs.push_back("-iphoneos_version_min"); 4588 else 4589 CmdArgs.push_back("-macosx_version_min"); 4590 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString())); 4591 4592 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs); 4593 Args.AddLastArg(CmdArgs, options::OPT_multi__module); 4594 Args.AddLastArg(CmdArgs, options::OPT_single__module); 4595 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined); 4596 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused); 4597 4598 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE, 4599 options::OPT_fno_pie, 4600 options::OPT_fno_PIE)) { 4601 if (A->getOption().matches(options::OPT_fpie) || 4602 A->getOption().matches(options::OPT_fPIE)) 4603 CmdArgs.push_back("-pie"); 4604 else 4605 CmdArgs.push_back("-no_pie"); 4606 } 4607 4608 Args.AddLastArg(CmdArgs, options::OPT_prebind); 4609 Args.AddLastArg(CmdArgs, options::OPT_noprebind); 4610 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding); 4611 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules); 4612 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs); 4613 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate); 4614 Args.AddAllArgs(CmdArgs, options::OPT_sectorder); 4615 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr); 4616 Args.AddAllArgs(CmdArgs, options::OPT_segprot); 4617 Args.AddAllArgs(CmdArgs, options::OPT_segaddr); 4618 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr); 4619 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr); 4620 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table); 4621 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename); 4622 Args.AddAllArgs(CmdArgs, options::OPT_sub__library); 4623 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella); 4624 4625 // Give --sysroot= preference, over the Apple specific behavior to also use 4626 // --isysroot as the syslibroot. 4627 StringRef sysroot = C.getSysRoot(); 4628 if (sysroot != "") { 4629 CmdArgs.push_back("-syslibroot"); 4630 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot)); 4631 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { 4632 CmdArgs.push_back("-syslibroot"); 4633 CmdArgs.push_back(A->getValue()); 4634 } 4635 4636 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace); 4637 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints); 4638 Args.AddAllArgs(CmdArgs, options::OPT_umbrella); 4639 Args.AddAllArgs(CmdArgs, options::OPT_undefined); 4640 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list); 4641 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches); 4642 Args.AddLastArg(CmdArgs, options::OPT_X_Flag); 4643 Args.AddAllArgs(CmdArgs, options::OPT_y); 4644 Args.AddLastArg(CmdArgs, options::OPT_w); 4645 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size); 4646 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__); 4647 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit); 4648 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit); 4649 Args.AddAllArgs(CmdArgs, options::OPT_sectalign); 4650 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols); 4651 Args.AddAllArgs(CmdArgs, options::OPT_segcreate); 4652 Args.AddLastArg(CmdArgs, options::OPT_whyload); 4653 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded); 4654 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name); 4655 Args.AddLastArg(CmdArgs, options::OPT_dylinker); 4656 Args.AddLastArg(CmdArgs, options::OPT_Mach); 4657} 4658 4659void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA, 4660 const InputInfo &Output, 4661 const InputInfoList &Inputs, 4662 const ArgList &Args, 4663 const char *LinkingOutput) const { 4664 assert(Output.getType() == types::TY_Image && "Invalid linker output type."); 4665 4666 // The logic here is derived from gcc's behavior; most of which 4667 // comes from specs (starting with link_command). Consult gcc for 4668 // more information. 4669 ArgStringList CmdArgs; 4670 4671 /// Hack(tm) to ignore linking errors when we are doing ARC migration. 4672 if (Args.hasArg(options::OPT_ccc_arcmt_check, 4673 options::OPT_ccc_arcmt_migrate)) { 4674 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I) 4675 (*I)->claim(); 4676 const char *Exec = 4677 Args.MakeArgString(getToolChain().GetProgramPath("touch")); 4678 CmdArgs.push_back(Output.getFilename()); 4679 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4680 return; 4681 } 4682 4683 // I'm not sure why this particular decomposition exists in gcc, but 4684 // we follow suite for ease of comparison. 4685 AddLinkArgs(C, Args, CmdArgs, Inputs); 4686 4687 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag); 4688 Args.AddAllArgs(CmdArgs, options::OPT_s); 4689 Args.AddAllArgs(CmdArgs, options::OPT_t); 4690 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 4691 Args.AddAllArgs(CmdArgs, options::OPT_u_Group); 4692 Args.AddLastArg(CmdArgs, options::OPT_e); 4693 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate); 4694 Args.AddAllArgs(CmdArgs, options::OPT_r); 4695 4696 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading 4697 // members of static archive libraries which implement Objective-C classes or 4698 // categories. 4699 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX)) 4700 CmdArgs.push_back("-ObjC"); 4701 4702 CmdArgs.push_back("-o"); 4703 CmdArgs.push_back(Output.getFilename()); 4704 4705 if (!Args.hasArg(options::OPT_nostdlib) && 4706 !Args.hasArg(options::OPT_nostartfiles)) { 4707 // Derived from startfile spec. 4708 if (Args.hasArg(options::OPT_dynamiclib)) { 4709 // Derived from darwin_dylib1 spec. 4710 if (getDarwinToolChain().isTargetIOSSimulator()) { 4711 // The simulator doesn't have a versioned crt1 file. 4712 CmdArgs.push_back("-ldylib1.o"); 4713 } else if (getDarwinToolChain().isTargetIPhoneOS()) { 4714 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1)) 4715 CmdArgs.push_back("-ldylib1.o"); 4716 } else { 4717 if (getDarwinToolChain().isMacosxVersionLT(10, 5)) 4718 CmdArgs.push_back("-ldylib1.o"); 4719 else if (getDarwinToolChain().isMacosxVersionLT(10, 6)) 4720 CmdArgs.push_back("-ldylib1.10.5.o"); 4721 } 4722 } else { 4723 if (Args.hasArg(options::OPT_bundle)) { 4724 if (!Args.hasArg(options::OPT_static)) { 4725 // Derived from darwin_bundle1 spec. 4726 if (getDarwinToolChain().isTargetIOSSimulator()) { 4727 // The simulator doesn't have a versioned crt1 file. 4728 CmdArgs.push_back("-lbundle1.o"); 4729 } else if (getDarwinToolChain().isTargetIPhoneOS()) { 4730 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1)) 4731 CmdArgs.push_back("-lbundle1.o"); 4732 } else { 4733 if (getDarwinToolChain().isMacosxVersionLT(10, 6)) 4734 CmdArgs.push_back("-lbundle1.o"); 4735 } 4736 } 4737 } else { 4738 if (Args.hasArg(options::OPT_pg) && 4739 getToolChain().SupportsProfiling()) { 4740 if (Args.hasArg(options::OPT_static) || 4741 Args.hasArg(options::OPT_object) || 4742 Args.hasArg(options::OPT_preload)) { 4743 CmdArgs.push_back("-lgcrt0.o"); 4744 } else { 4745 CmdArgs.push_back("-lgcrt1.o"); 4746 4747 // darwin_crt2 spec is empty. 4748 } 4749 // By default on OS X 10.8 and later, we don't link with a crt1.o 4750 // file and the linker knows to use _main as the entry point. But, 4751 // when compiling with -pg, we need to link with the gcrt1.o file, 4752 // so pass the -no_new_main option to tell the linker to use the 4753 // "start" symbol as the entry point. 4754 if (getDarwinToolChain().isTargetMacOS() && 4755 !getDarwinToolChain().isMacosxVersionLT(10, 8)) 4756 CmdArgs.push_back("-no_new_main"); 4757 } else { 4758 if (Args.hasArg(options::OPT_static) || 4759 Args.hasArg(options::OPT_object) || 4760 Args.hasArg(options::OPT_preload)) { 4761 CmdArgs.push_back("-lcrt0.o"); 4762 } else { 4763 // Derived from darwin_crt1 spec. 4764 if (getDarwinToolChain().isTargetIOSSimulator()) { 4765 // The simulator doesn't have a versioned crt1 file. 4766 CmdArgs.push_back("-lcrt1.o"); 4767 } else if (getDarwinToolChain().isTargetIPhoneOS()) { 4768 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1)) 4769 CmdArgs.push_back("-lcrt1.o"); 4770 else if (getDarwinToolChain().isIPhoneOSVersionLT(6, 0)) 4771 CmdArgs.push_back("-lcrt1.3.1.o"); 4772 } else { 4773 if (getDarwinToolChain().isMacosxVersionLT(10, 5)) 4774 CmdArgs.push_back("-lcrt1.o"); 4775 else if (getDarwinToolChain().isMacosxVersionLT(10, 6)) 4776 CmdArgs.push_back("-lcrt1.10.5.o"); 4777 else if (getDarwinToolChain().isMacosxVersionLT(10, 8)) 4778 CmdArgs.push_back("-lcrt1.10.6.o"); 4779 4780 // darwin_crt2 spec is empty. 4781 } 4782 } 4783 } 4784 } 4785 } 4786 4787 if (!getDarwinToolChain().isTargetIPhoneOS() && 4788 Args.hasArg(options::OPT_shared_libgcc) && 4789 getDarwinToolChain().isMacosxVersionLT(10, 5)) { 4790 const char *Str = 4791 Args.MakeArgString(getToolChain().GetFilePath("crt3.o")); 4792 CmdArgs.push_back(Str); 4793 } 4794 } 4795 4796 Args.AddAllArgs(CmdArgs, options::OPT_L); 4797 4798 const SanitizerArgs &Sanitize = 4799 getToolChain().getDriver().getOrParseSanitizerArgs(Args); 4800 // If we're building a dynamic lib with -fsanitize=address, 4801 // unresolved symbols may appear. Mark all 4802 // of them as dynamic_lookup. Linking executables is handled in 4803 // lib/Driver/ToolChains.cpp. 4804 if (Sanitize.needsAsanRt()) { 4805 if (Args.hasArg(options::OPT_dynamiclib) || 4806 Args.hasArg(options::OPT_bundle)) { 4807 CmdArgs.push_back("-undefined"); 4808 CmdArgs.push_back("dynamic_lookup"); 4809 } 4810 } 4811 4812 if (Args.hasArg(options::OPT_fopenmp)) 4813 // This is more complicated in gcc... 4814 CmdArgs.push_back("-lgomp"); 4815 4816 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 4817 4818 if (isObjCRuntimeLinked(Args) && 4819 !Args.hasArg(options::OPT_nostdlib) && 4820 !Args.hasArg(options::OPT_nodefaultlibs)) { 4821 // Avoid linking compatibility stubs on i386 mac. 4822 if (!getDarwinToolChain().isTargetMacOS() || 4823 getDarwinToolChain().getArch() != llvm::Triple::x86) { 4824 // If we don't have ARC or subscripting runtime support, link in the 4825 // runtime stubs. We have to do this *before* adding any of the normal 4826 // linker inputs so that its initializer gets run first. 4827 ObjCRuntime runtime = 4828 getDarwinToolChain().getDefaultObjCRuntime(/*nonfragile*/ true); 4829 // We use arclite library for both ARC and subscripting support. 4830 if ((!runtime.hasNativeARC() && isObjCAutoRefCount(Args)) || 4831 !runtime.hasSubscripting()) 4832 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs); 4833 } 4834 CmdArgs.push_back("-framework"); 4835 CmdArgs.push_back("Foundation"); 4836 // Link libobj. 4837 CmdArgs.push_back("-lobjc"); 4838 } 4839 4840 if (LinkingOutput) { 4841 CmdArgs.push_back("-arch_multiple"); 4842 CmdArgs.push_back("-final_output"); 4843 CmdArgs.push_back(LinkingOutput); 4844 } 4845 4846 if (Args.hasArg(options::OPT_fnested_functions)) 4847 CmdArgs.push_back("-allow_stack_execute"); 4848 4849 if (!Args.hasArg(options::OPT_nostdlib) && 4850 !Args.hasArg(options::OPT_nodefaultlibs)) { 4851 if (getToolChain().getDriver().CCCIsCXX()) 4852 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 4853 4854 // link_ssp spec is empty. 4855 4856 // Let the tool chain choose which runtime library to link. 4857 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs); 4858 } 4859 4860 if (!Args.hasArg(options::OPT_nostdlib) && 4861 !Args.hasArg(options::OPT_nostartfiles)) { 4862 // endfile_spec is empty. 4863 } 4864 4865 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 4866 Args.AddAllArgs(CmdArgs, options::OPT_F); 4867 4868 const char *Exec = 4869 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 4870 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4871} 4872 4873void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA, 4874 const InputInfo &Output, 4875 const InputInfoList &Inputs, 4876 const ArgList &Args, 4877 const char *LinkingOutput) const { 4878 ArgStringList CmdArgs; 4879 4880 CmdArgs.push_back("-create"); 4881 assert(Output.isFilename() && "Unexpected lipo output."); 4882 4883 CmdArgs.push_back("-output"); 4884 CmdArgs.push_back(Output.getFilename()); 4885 4886 for (InputInfoList::const_iterator 4887 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 4888 const InputInfo &II = *it; 4889 assert(II.isFilename() && "Unexpected lipo input."); 4890 CmdArgs.push_back(II.getFilename()); 4891 } 4892 const char *Exec = 4893 Args.MakeArgString(getToolChain().GetProgramPath("lipo")); 4894 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4895} 4896 4897void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA, 4898 const InputInfo &Output, 4899 const InputInfoList &Inputs, 4900 const ArgList &Args, 4901 const char *LinkingOutput) const { 4902 ArgStringList CmdArgs; 4903 4904 CmdArgs.push_back("-o"); 4905 CmdArgs.push_back(Output.getFilename()); 4906 4907 assert(Inputs.size() == 1 && "Unable to handle multiple inputs."); 4908 const InputInfo &Input = Inputs[0]; 4909 assert(Input.isFilename() && "Unexpected dsymutil input."); 4910 CmdArgs.push_back(Input.getFilename()); 4911 4912 const char *Exec = 4913 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil")); 4914 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4915} 4916 4917void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA, 4918 const InputInfo &Output, 4919 const InputInfoList &Inputs, 4920 const ArgList &Args, 4921 const char *LinkingOutput) const { 4922 ArgStringList CmdArgs; 4923 CmdArgs.push_back("--verify"); 4924 CmdArgs.push_back("--debug-info"); 4925 CmdArgs.push_back("--eh-frame"); 4926 CmdArgs.push_back("--quiet"); 4927 4928 assert(Inputs.size() == 1 && "Unable to handle multiple inputs."); 4929 const InputInfo &Input = Inputs[0]; 4930 assert(Input.isFilename() && "Unexpected verify input"); 4931 4932 // Grabbing the output of the earlier dsymutil run. 4933 CmdArgs.push_back(Input.getFilename()); 4934 4935 const char *Exec = 4936 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump")); 4937 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4938} 4939 4940void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 4941 const InputInfo &Output, 4942 const InputInfoList &Inputs, 4943 const ArgList &Args, 4944 const char *LinkingOutput) const { 4945 ArgStringList CmdArgs; 4946 4947 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 4948 options::OPT_Xassembler); 4949 4950 CmdArgs.push_back("-o"); 4951 CmdArgs.push_back(Output.getFilename()); 4952 4953 for (InputInfoList::const_iterator 4954 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 4955 const InputInfo &II = *it; 4956 CmdArgs.push_back(II.getFilename()); 4957 } 4958 4959 const char *Exec = 4960 Args.MakeArgString(getToolChain().GetProgramPath("as")); 4961 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4962} 4963 4964 4965void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA, 4966 const InputInfo &Output, 4967 const InputInfoList &Inputs, 4968 const ArgList &Args, 4969 const char *LinkingOutput) const { 4970 // FIXME: Find a real GCC, don't hard-code versions here 4971 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/"; 4972 const llvm::Triple &T = getToolChain().getTriple(); 4973 std::string LibPath = "/usr/lib/"; 4974 llvm::Triple::ArchType Arch = T.getArch(); 4975 switch (Arch) { 4976 case llvm::Triple::x86: 4977 GCCLibPath += ("i386-" + T.getVendorName() + "-" + 4978 T.getOSName()).str() + "/4.5.2/"; 4979 break; 4980 case llvm::Triple::x86_64: 4981 GCCLibPath += ("i386-" + T.getVendorName() + "-" + 4982 T.getOSName()).str(); 4983 GCCLibPath += "/4.5.2/amd64/"; 4984 LibPath += "amd64/"; 4985 break; 4986 default: 4987 assert(0 && "Unsupported architecture"); 4988 } 4989 4990 ArgStringList CmdArgs; 4991 4992 // Demangle C++ names in errors 4993 CmdArgs.push_back("-C"); 4994 4995 if ((!Args.hasArg(options::OPT_nostdlib)) && 4996 (!Args.hasArg(options::OPT_shared))) { 4997 CmdArgs.push_back("-e"); 4998 CmdArgs.push_back("_start"); 4999 } 5000 5001 if (Args.hasArg(options::OPT_static)) { 5002 CmdArgs.push_back("-Bstatic"); 5003 CmdArgs.push_back("-dn"); 5004 } else { 5005 CmdArgs.push_back("-Bdynamic"); 5006 if (Args.hasArg(options::OPT_shared)) { 5007 CmdArgs.push_back("-shared"); 5008 } else { 5009 CmdArgs.push_back("--dynamic-linker"); 5010 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1")); 5011 } 5012 } 5013 5014 if (Output.isFilename()) { 5015 CmdArgs.push_back("-o"); 5016 CmdArgs.push_back(Output.getFilename()); 5017 } else { 5018 assert(Output.isNothing() && "Invalid output."); 5019 } 5020 5021 if (!Args.hasArg(options::OPT_nostdlib) && 5022 !Args.hasArg(options::OPT_nostartfiles)) { 5023 if (!Args.hasArg(options::OPT_shared)) { 5024 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o")); 5025 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o")); 5026 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o")); 5027 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o")); 5028 } else { 5029 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o")); 5030 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o")); 5031 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o")); 5032 } 5033 if (getToolChain().getDriver().CCCIsCXX()) 5034 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o")); 5035 } 5036 5037 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath)); 5038 5039 Args.AddAllArgs(CmdArgs, options::OPT_L); 5040 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5041 Args.AddAllArgs(CmdArgs, options::OPT_e); 5042 Args.AddAllArgs(CmdArgs, options::OPT_r); 5043 5044 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5045 5046 if (!Args.hasArg(options::OPT_nostdlib) && 5047 !Args.hasArg(options::OPT_nodefaultlibs)) { 5048 if (getToolChain().getDriver().CCCIsCXX()) 5049 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 5050 CmdArgs.push_back("-lgcc_s"); 5051 if (!Args.hasArg(options::OPT_shared)) { 5052 CmdArgs.push_back("-lgcc"); 5053 CmdArgs.push_back("-lc"); 5054 CmdArgs.push_back("-lm"); 5055 } 5056 } 5057 5058 if (!Args.hasArg(options::OPT_nostdlib) && 5059 !Args.hasArg(options::OPT_nostartfiles)) { 5060 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o")); 5061 } 5062 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o")); 5063 5064 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 5065 5066 const char *Exec = 5067 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5068 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5069} 5070 5071void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5072 const InputInfo &Output, 5073 const InputInfoList &Inputs, 5074 const ArgList &Args, 5075 const char *LinkingOutput) const { 5076 ArgStringList CmdArgs; 5077 5078 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5079 options::OPT_Xassembler); 5080 5081 CmdArgs.push_back("-o"); 5082 CmdArgs.push_back(Output.getFilename()); 5083 5084 for (InputInfoList::const_iterator 5085 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5086 const InputInfo &II = *it; 5087 CmdArgs.push_back(II.getFilename()); 5088 } 5089 5090 const char *Exec = 5091 Args.MakeArgString(getToolChain().GetProgramPath("gas")); 5092 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5093} 5094 5095void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA, 5096 const InputInfo &Output, 5097 const InputInfoList &Inputs, 5098 const ArgList &Args, 5099 const char *LinkingOutput) const { 5100 ArgStringList CmdArgs; 5101 5102 if ((!Args.hasArg(options::OPT_nostdlib)) && 5103 (!Args.hasArg(options::OPT_shared))) { 5104 CmdArgs.push_back("-e"); 5105 CmdArgs.push_back("_start"); 5106 } 5107 5108 if (Args.hasArg(options::OPT_static)) { 5109 CmdArgs.push_back("-Bstatic"); 5110 CmdArgs.push_back("-dn"); 5111 } else { 5112// CmdArgs.push_back("--eh-frame-hdr"); 5113 CmdArgs.push_back("-Bdynamic"); 5114 if (Args.hasArg(options::OPT_shared)) { 5115 CmdArgs.push_back("-shared"); 5116 } else { 5117 CmdArgs.push_back("--dynamic-linker"); 5118 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1 5119 } 5120 } 5121 5122 if (Output.isFilename()) { 5123 CmdArgs.push_back("-o"); 5124 CmdArgs.push_back(Output.getFilename()); 5125 } else { 5126 assert(Output.isNothing() && "Invalid output."); 5127 } 5128 5129 if (!Args.hasArg(options::OPT_nostdlib) && 5130 !Args.hasArg(options::OPT_nostartfiles)) { 5131 if (!Args.hasArg(options::OPT_shared)) { 5132 CmdArgs.push_back(Args.MakeArgString( 5133 getToolChain().GetFilePath("crt1.o"))); 5134 CmdArgs.push_back(Args.MakeArgString( 5135 getToolChain().GetFilePath("crti.o"))); 5136 CmdArgs.push_back(Args.MakeArgString( 5137 getToolChain().GetFilePath("crtbegin.o"))); 5138 } else { 5139 CmdArgs.push_back(Args.MakeArgString( 5140 getToolChain().GetFilePath("crti.o"))); 5141 } 5142 CmdArgs.push_back(Args.MakeArgString( 5143 getToolChain().GetFilePath("crtn.o"))); 5144 } 5145 5146 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/" 5147 + getToolChain().getTripleString() 5148 + "/4.2.4")); 5149 5150 Args.AddAllArgs(CmdArgs, options::OPT_L); 5151 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5152 Args.AddAllArgs(CmdArgs, options::OPT_e); 5153 5154 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5155 5156 if (!Args.hasArg(options::OPT_nostdlib) && 5157 !Args.hasArg(options::OPT_nodefaultlibs)) { 5158 // FIXME: For some reason GCC passes -lgcc before adding 5159 // the default system libraries. Just mimic this for now. 5160 CmdArgs.push_back("-lgcc"); 5161 5162 if (Args.hasArg(options::OPT_pthread)) 5163 CmdArgs.push_back("-pthread"); 5164 if (!Args.hasArg(options::OPT_shared)) 5165 CmdArgs.push_back("-lc"); 5166 CmdArgs.push_back("-lgcc"); 5167 } 5168 5169 if (!Args.hasArg(options::OPT_nostdlib) && 5170 !Args.hasArg(options::OPT_nostartfiles)) { 5171 if (!Args.hasArg(options::OPT_shared)) 5172 CmdArgs.push_back(Args.MakeArgString( 5173 getToolChain().GetFilePath("crtend.o"))); 5174 } 5175 5176 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 5177 5178 const char *Exec = 5179 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5180 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5181} 5182 5183void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5184 const InputInfo &Output, 5185 const InputInfoList &Inputs, 5186 const ArgList &Args, 5187 const char *LinkingOutput) const { 5188 ArgStringList CmdArgs; 5189 5190 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5191 options::OPT_Xassembler); 5192 5193 CmdArgs.push_back("-o"); 5194 CmdArgs.push_back(Output.getFilename()); 5195 5196 for (InputInfoList::const_iterator 5197 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5198 const InputInfo &II = *it; 5199 CmdArgs.push_back(II.getFilename()); 5200 } 5201 5202 const char *Exec = 5203 Args.MakeArgString(getToolChain().GetProgramPath("as")); 5204 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5205} 5206 5207void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA, 5208 const InputInfo &Output, 5209 const InputInfoList &Inputs, 5210 const ArgList &Args, 5211 const char *LinkingOutput) const { 5212 const Driver &D = getToolChain().getDriver(); 5213 ArgStringList CmdArgs; 5214 5215 // Silence warning for "clang -g foo.o -o foo" 5216 Args.ClaimAllArgs(options::OPT_g_Group); 5217 // and "clang -emit-llvm foo.o -o foo" 5218 Args.ClaimAllArgs(options::OPT_emit_llvm); 5219 // and for "clang -w foo.o -o foo". Other warning options are already 5220 // handled somewhere else. 5221 Args.ClaimAllArgs(options::OPT_w); 5222 5223 if ((!Args.hasArg(options::OPT_nostdlib)) && 5224 (!Args.hasArg(options::OPT_shared))) { 5225 CmdArgs.push_back("-e"); 5226 CmdArgs.push_back("__start"); 5227 } 5228 5229 if (Args.hasArg(options::OPT_static)) { 5230 CmdArgs.push_back("-Bstatic"); 5231 } else { 5232 if (Args.hasArg(options::OPT_rdynamic)) 5233 CmdArgs.push_back("-export-dynamic"); 5234 CmdArgs.push_back("--eh-frame-hdr"); 5235 CmdArgs.push_back("-Bdynamic"); 5236 if (Args.hasArg(options::OPT_shared)) { 5237 CmdArgs.push_back("-shared"); 5238 } else { 5239 CmdArgs.push_back("-dynamic-linker"); 5240 CmdArgs.push_back("/usr/libexec/ld.so"); 5241 } 5242 } 5243 5244 if (Args.hasArg(options::OPT_nopie)) 5245 CmdArgs.push_back("-nopie"); 5246 5247 if (Output.isFilename()) { 5248 CmdArgs.push_back("-o"); 5249 CmdArgs.push_back(Output.getFilename()); 5250 } else { 5251 assert(Output.isNothing() && "Invalid output."); 5252 } 5253 5254 if (!Args.hasArg(options::OPT_nostdlib) && 5255 !Args.hasArg(options::OPT_nostartfiles)) { 5256 if (!Args.hasArg(options::OPT_shared)) { 5257 if (Args.hasArg(options::OPT_pg)) 5258 CmdArgs.push_back(Args.MakeArgString( 5259 getToolChain().GetFilePath("gcrt0.o"))); 5260 else 5261 CmdArgs.push_back(Args.MakeArgString( 5262 getToolChain().GetFilePath("crt0.o"))); 5263 CmdArgs.push_back(Args.MakeArgString( 5264 getToolChain().GetFilePath("crtbegin.o"))); 5265 } else { 5266 CmdArgs.push_back(Args.MakeArgString( 5267 getToolChain().GetFilePath("crtbeginS.o"))); 5268 } 5269 } 5270 5271 std::string Triple = getToolChain().getTripleString(); 5272 if (Triple.substr(0, 6) == "x86_64") 5273 Triple.replace(0, 6, "amd64"); 5274 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple + 5275 "/4.2.1")); 5276 5277 Args.AddAllArgs(CmdArgs, options::OPT_L); 5278 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5279 Args.AddAllArgs(CmdArgs, options::OPT_e); 5280 Args.AddAllArgs(CmdArgs, options::OPT_s); 5281 Args.AddAllArgs(CmdArgs, options::OPT_t); 5282 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 5283 Args.AddAllArgs(CmdArgs, options::OPT_r); 5284 5285 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5286 5287 if (!Args.hasArg(options::OPT_nostdlib) && 5288 !Args.hasArg(options::OPT_nodefaultlibs)) { 5289 if (D.CCCIsCXX()) { 5290 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 5291 if (Args.hasArg(options::OPT_pg)) 5292 CmdArgs.push_back("-lm_p"); 5293 else 5294 CmdArgs.push_back("-lm"); 5295 } 5296 5297 // FIXME: For some reason GCC passes -lgcc before adding 5298 // the default system libraries. Just mimic this for now. 5299 CmdArgs.push_back("-lgcc"); 5300 5301 if (Args.hasArg(options::OPT_pthread)) { 5302 if (!Args.hasArg(options::OPT_shared) && 5303 Args.hasArg(options::OPT_pg)) 5304 CmdArgs.push_back("-lpthread_p"); 5305 else 5306 CmdArgs.push_back("-lpthread"); 5307 } 5308 5309 if (!Args.hasArg(options::OPT_shared)) { 5310 if (Args.hasArg(options::OPT_pg)) 5311 CmdArgs.push_back("-lc_p"); 5312 else 5313 CmdArgs.push_back("-lc"); 5314 } 5315 5316 CmdArgs.push_back("-lgcc"); 5317 } 5318 5319 if (!Args.hasArg(options::OPT_nostdlib) && 5320 !Args.hasArg(options::OPT_nostartfiles)) { 5321 if (!Args.hasArg(options::OPT_shared)) 5322 CmdArgs.push_back(Args.MakeArgString( 5323 getToolChain().GetFilePath("crtend.o"))); 5324 else 5325 CmdArgs.push_back(Args.MakeArgString( 5326 getToolChain().GetFilePath("crtendS.o"))); 5327 } 5328 5329 const char *Exec = 5330 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5331 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5332} 5333 5334void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5335 const InputInfo &Output, 5336 const InputInfoList &Inputs, 5337 const ArgList &Args, 5338 const char *LinkingOutput) const { 5339 ArgStringList CmdArgs; 5340 5341 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5342 options::OPT_Xassembler); 5343 5344 CmdArgs.push_back("-o"); 5345 CmdArgs.push_back(Output.getFilename()); 5346 5347 for (InputInfoList::const_iterator 5348 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5349 const InputInfo &II = *it; 5350 CmdArgs.push_back(II.getFilename()); 5351 } 5352 5353 const char *Exec = 5354 Args.MakeArgString(getToolChain().GetProgramPath("as")); 5355 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5356} 5357 5358void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA, 5359 const InputInfo &Output, 5360 const InputInfoList &Inputs, 5361 const ArgList &Args, 5362 const char *LinkingOutput) const { 5363 const Driver &D = getToolChain().getDriver(); 5364 ArgStringList CmdArgs; 5365 5366 if ((!Args.hasArg(options::OPT_nostdlib)) && 5367 (!Args.hasArg(options::OPT_shared))) { 5368 CmdArgs.push_back("-e"); 5369 CmdArgs.push_back("__start"); 5370 } 5371 5372 if (Args.hasArg(options::OPT_static)) { 5373 CmdArgs.push_back("-Bstatic"); 5374 } else { 5375 if (Args.hasArg(options::OPT_rdynamic)) 5376 CmdArgs.push_back("-export-dynamic"); 5377 CmdArgs.push_back("--eh-frame-hdr"); 5378 CmdArgs.push_back("-Bdynamic"); 5379 if (Args.hasArg(options::OPT_shared)) { 5380 CmdArgs.push_back("-shared"); 5381 } else { 5382 CmdArgs.push_back("-dynamic-linker"); 5383 CmdArgs.push_back("/usr/libexec/ld.so"); 5384 } 5385 } 5386 5387 if (Output.isFilename()) { 5388 CmdArgs.push_back("-o"); 5389 CmdArgs.push_back(Output.getFilename()); 5390 } else { 5391 assert(Output.isNothing() && "Invalid output."); 5392 } 5393 5394 if (!Args.hasArg(options::OPT_nostdlib) && 5395 !Args.hasArg(options::OPT_nostartfiles)) { 5396 if (!Args.hasArg(options::OPT_shared)) { 5397 if (Args.hasArg(options::OPT_pg)) 5398 CmdArgs.push_back(Args.MakeArgString( 5399 getToolChain().GetFilePath("gcrt0.o"))); 5400 else 5401 CmdArgs.push_back(Args.MakeArgString( 5402 getToolChain().GetFilePath("crt0.o"))); 5403 CmdArgs.push_back(Args.MakeArgString( 5404 getToolChain().GetFilePath("crtbegin.o"))); 5405 } else { 5406 CmdArgs.push_back(Args.MakeArgString( 5407 getToolChain().GetFilePath("crtbeginS.o"))); 5408 } 5409 } 5410 5411 Args.AddAllArgs(CmdArgs, options::OPT_L); 5412 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5413 Args.AddAllArgs(CmdArgs, options::OPT_e); 5414 5415 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5416 5417 if (!Args.hasArg(options::OPT_nostdlib) && 5418 !Args.hasArg(options::OPT_nodefaultlibs)) { 5419 if (D.CCCIsCXX()) { 5420 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 5421 if (Args.hasArg(options::OPT_pg)) 5422 CmdArgs.push_back("-lm_p"); 5423 else 5424 CmdArgs.push_back("-lm"); 5425 } 5426 5427 if (Args.hasArg(options::OPT_pthread)) { 5428 if (!Args.hasArg(options::OPT_shared) && 5429 Args.hasArg(options::OPT_pg)) 5430 CmdArgs.push_back("-lpthread_p"); 5431 else 5432 CmdArgs.push_back("-lpthread"); 5433 } 5434 5435 if (!Args.hasArg(options::OPT_shared)) { 5436 if (Args.hasArg(options::OPT_pg)) 5437 CmdArgs.push_back("-lc_p"); 5438 else 5439 CmdArgs.push_back("-lc"); 5440 } 5441 5442 std::string myarch = "-lclang_rt."; 5443 const llvm::Triple &T = getToolChain().getTriple(); 5444 llvm::Triple::ArchType Arch = T.getArch(); 5445 switch (Arch) { 5446 case llvm::Triple::arm: 5447 myarch += ("arm"); 5448 break; 5449 case llvm::Triple::x86: 5450 myarch += ("i386"); 5451 break; 5452 case llvm::Triple::x86_64: 5453 myarch += ("amd64"); 5454 break; 5455 default: 5456 assert(0 && "Unsupported architecture"); 5457 } 5458 CmdArgs.push_back(Args.MakeArgString(myarch)); 5459 } 5460 5461 if (!Args.hasArg(options::OPT_nostdlib) && 5462 !Args.hasArg(options::OPT_nostartfiles)) { 5463 if (!Args.hasArg(options::OPT_shared)) 5464 CmdArgs.push_back(Args.MakeArgString( 5465 getToolChain().GetFilePath("crtend.o"))); 5466 else 5467 CmdArgs.push_back(Args.MakeArgString( 5468 getToolChain().GetFilePath("crtendS.o"))); 5469 } 5470 5471 const char *Exec = 5472 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5473 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5474} 5475 5476void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5477 const InputInfo &Output, 5478 const InputInfoList &Inputs, 5479 const ArgList &Args, 5480 const char *LinkingOutput) const { 5481 ArgStringList CmdArgs; 5482 5483 // When building 32-bit code on FreeBSD/amd64, we have to explicitly 5484 // instruct as in the base system to assemble 32-bit code. 5485 if (getToolChain().getArch() == llvm::Triple::x86) 5486 CmdArgs.push_back("--32"); 5487 else if (getToolChain().getArch() == llvm::Triple::ppc) 5488 CmdArgs.push_back("-a32"); 5489 else if (getToolChain().getArch() == llvm::Triple::mips || 5490 getToolChain().getArch() == llvm::Triple::mipsel || 5491 getToolChain().getArch() == llvm::Triple::mips64 || 5492 getToolChain().getArch() == llvm::Triple::mips64el) { 5493 StringRef CPUName; 5494 StringRef ABIName; 5495 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName); 5496 5497 CmdArgs.push_back("-march"); 5498 CmdArgs.push_back(CPUName.data()); 5499 5500 CmdArgs.push_back("-mabi"); 5501 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data()); 5502 5503 if (getToolChain().getArch() == llvm::Triple::mips || 5504 getToolChain().getArch() == llvm::Triple::mips64) 5505 CmdArgs.push_back("-EB"); 5506 else 5507 CmdArgs.push_back("-EL"); 5508 5509 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC, 5510 options::OPT_fpic, options::OPT_fno_pic, 5511 options::OPT_fPIE, options::OPT_fno_PIE, 5512 options::OPT_fpie, options::OPT_fno_pie); 5513 if (LastPICArg && 5514 (LastPICArg->getOption().matches(options::OPT_fPIC) || 5515 LastPICArg->getOption().matches(options::OPT_fpic) || 5516 LastPICArg->getOption().matches(options::OPT_fPIE) || 5517 LastPICArg->getOption().matches(options::OPT_fpie))) { 5518 CmdArgs.push_back("-KPIC"); 5519 } 5520 } else if (getToolChain().getArch() == llvm::Triple::arm || 5521 getToolChain().getArch() == llvm::Triple::thumb) { 5522 CmdArgs.push_back("-mfpu=softvfp"); 5523 switch(getToolChain().getTriple().getEnvironment()) { 5524 case llvm::Triple::GNUEABI: 5525 case llvm::Triple::EABI: 5526 CmdArgs.push_back("-meabi=5"); 5527 break; 5528 5529 default: 5530 CmdArgs.push_back("-matpcs"); 5531 } 5532 } 5533 5534 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5535 options::OPT_Xassembler); 5536 5537 CmdArgs.push_back("-o"); 5538 CmdArgs.push_back(Output.getFilename()); 5539 5540 for (InputInfoList::const_iterator 5541 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5542 const InputInfo &II = *it; 5543 CmdArgs.push_back(II.getFilename()); 5544 } 5545 5546 const char *Exec = 5547 Args.MakeArgString(getToolChain().GetProgramPath("as")); 5548 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5549} 5550 5551void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA, 5552 const InputInfo &Output, 5553 const InputInfoList &Inputs, 5554 const ArgList &Args, 5555 const char *LinkingOutput) const { 5556 const toolchains::FreeBSD& ToolChain = 5557 static_cast<const toolchains::FreeBSD&>(getToolChain()); 5558 const Driver &D = ToolChain.getDriver(); 5559 ArgStringList CmdArgs; 5560 5561 // Silence warning for "clang -g foo.o -o foo" 5562 Args.ClaimAllArgs(options::OPT_g_Group); 5563 // and "clang -emit-llvm foo.o -o foo" 5564 Args.ClaimAllArgs(options::OPT_emit_llvm); 5565 // and for "clang -w foo.o -o foo". Other warning options are already 5566 // handled somewhere else. 5567 Args.ClaimAllArgs(options::OPT_w); 5568 5569 if (!D.SysRoot.empty()) 5570 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 5571 5572 if (Args.hasArg(options::OPT_pie)) 5573 CmdArgs.push_back("-pie"); 5574 5575 if (Args.hasArg(options::OPT_static)) { 5576 CmdArgs.push_back("-Bstatic"); 5577 } else { 5578 if (Args.hasArg(options::OPT_rdynamic)) 5579 CmdArgs.push_back("-export-dynamic"); 5580 CmdArgs.push_back("--eh-frame-hdr"); 5581 if (Args.hasArg(options::OPT_shared)) { 5582 CmdArgs.push_back("-Bshareable"); 5583 } else { 5584 CmdArgs.push_back("-dynamic-linker"); 5585 CmdArgs.push_back("/libexec/ld-elf.so.1"); 5586 } 5587 if (ToolChain.getTriple().getOSMajorVersion() >= 9) { 5588 llvm::Triple::ArchType Arch = ToolChain.getArch(); 5589 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc || 5590 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 5591 CmdArgs.push_back("--hash-style=both"); 5592 } 5593 } 5594 CmdArgs.push_back("--enable-new-dtags"); 5595 } 5596 5597 // When building 32-bit code on FreeBSD/amd64, we have to explicitly 5598 // instruct ld in the base system to link 32-bit code. 5599 if (ToolChain.getArch() == llvm::Triple::x86) { 5600 CmdArgs.push_back("-m"); 5601 CmdArgs.push_back("elf_i386_fbsd"); 5602 } 5603 5604 if (ToolChain.getArch() == llvm::Triple::ppc) { 5605 CmdArgs.push_back("-m"); 5606 CmdArgs.push_back("elf32ppc_fbsd"); 5607 } 5608 5609 if (Output.isFilename()) { 5610 CmdArgs.push_back("-o"); 5611 CmdArgs.push_back(Output.getFilename()); 5612 } else { 5613 assert(Output.isNothing() && "Invalid output."); 5614 } 5615 5616 if (!Args.hasArg(options::OPT_nostdlib) && 5617 !Args.hasArg(options::OPT_nostartfiles)) { 5618 const char *crt1 = NULL; 5619 if (!Args.hasArg(options::OPT_shared)) { 5620 if (Args.hasArg(options::OPT_pg)) 5621 crt1 = "gcrt1.o"; 5622 else if (Args.hasArg(options::OPT_pie)) 5623 crt1 = "Scrt1.o"; 5624 else 5625 crt1 = "crt1.o"; 5626 } 5627 if (crt1) 5628 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1))); 5629 5630 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); 5631 5632 const char *crtbegin = NULL; 5633 if (Args.hasArg(options::OPT_static)) 5634 crtbegin = "crtbeginT.o"; 5635 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) 5636 crtbegin = "crtbeginS.o"; 5637 else 5638 crtbegin = "crtbegin.o"; 5639 5640 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); 5641 } 5642 5643 Args.AddAllArgs(CmdArgs, options::OPT_L); 5644 const ToolChain::path_list Paths = ToolChain.getFilePaths(); 5645 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end(); 5646 i != e; ++i) 5647 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i)); 5648 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5649 Args.AddAllArgs(CmdArgs, options::OPT_e); 5650 Args.AddAllArgs(CmdArgs, options::OPT_s); 5651 Args.AddAllArgs(CmdArgs, options::OPT_t); 5652 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 5653 Args.AddAllArgs(CmdArgs, options::OPT_r); 5654 5655 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs); 5656 5657 if (!Args.hasArg(options::OPT_nostdlib) && 5658 !Args.hasArg(options::OPT_nodefaultlibs)) { 5659 if (D.CCCIsCXX()) { 5660 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 5661 if (Args.hasArg(options::OPT_pg)) 5662 CmdArgs.push_back("-lm_p"); 5663 else 5664 CmdArgs.push_back("-lm"); 5665 } 5666 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding 5667 // the default system libraries. Just mimic this for now. 5668 if (Args.hasArg(options::OPT_pg)) 5669 CmdArgs.push_back("-lgcc_p"); 5670 else 5671 CmdArgs.push_back("-lgcc"); 5672 if (Args.hasArg(options::OPT_static)) { 5673 CmdArgs.push_back("-lgcc_eh"); 5674 } else if (Args.hasArg(options::OPT_pg)) { 5675 CmdArgs.push_back("-lgcc_eh_p"); 5676 } else { 5677 CmdArgs.push_back("--as-needed"); 5678 CmdArgs.push_back("-lgcc_s"); 5679 CmdArgs.push_back("--no-as-needed"); 5680 } 5681 5682 if (Args.hasArg(options::OPT_pthread)) { 5683 if (Args.hasArg(options::OPT_pg)) 5684 CmdArgs.push_back("-lpthread_p"); 5685 else 5686 CmdArgs.push_back("-lpthread"); 5687 } 5688 5689 if (Args.hasArg(options::OPT_pg)) { 5690 if (Args.hasArg(options::OPT_shared)) 5691 CmdArgs.push_back("-lc"); 5692 else 5693 CmdArgs.push_back("-lc_p"); 5694 CmdArgs.push_back("-lgcc_p"); 5695 } else { 5696 CmdArgs.push_back("-lc"); 5697 CmdArgs.push_back("-lgcc"); 5698 } 5699 5700 if (Args.hasArg(options::OPT_static)) { 5701 CmdArgs.push_back("-lgcc_eh"); 5702 } else if (Args.hasArg(options::OPT_pg)) { 5703 CmdArgs.push_back("-lgcc_eh_p"); 5704 } else { 5705 CmdArgs.push_back("--as-needed"); 5706 CmdArgs.push_back("-lgcc_s"); 5707 CmdArgs.push_back("--no-as-needed"); 5708 } 5709 } 5710 5711 if (!Args.hasArg(options::OPT_nostdlib) && 5712 !Args.hasArg(options::OPT_nostartfiles)) { 5713 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) 5714 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o"))); 5715 else 5716 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o"))); 5717 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 5718 } 5719 5720 addProfileRT(ToolChain, Args, CmdArgs, ToolChain.getTriple()); 5721 5722 const char *Exec = 5723 Args.MakeArgString(ToolChain.GetProgramPath("ld")); 5724 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5725} 5726 5727void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5728 const InputInfo &Output, 5729 const InputInfoList &Inputs, 5730 const ArgList &Args, 5731 const char *LinkingOutput) const { 5732 ArgStringList CmdArgs; 5733 5734 // When building 32-bit code on NetBSD/amd64, we have to explicitly 5735 // instruct as in the base system to assemble 32-bit code. 5736 if (getToolChain().getArch() == llvm::Triple::x86) 5737 CmdArgs.push_back("--32"); 5738 5739 // Set byte order explicitly 5740 if (getToolChain().getArch() == llvm::Triple::mips) 5741 CmdArgs.push_back("-EB"); 5742 else if (getToolChain().getArch() == llvm::Triple::mipsel) 5743 CmdArgs.push_back("-EL"); 5744 5745 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5746 options::OPT_Xassembler); 5747 5748 CmdArgs.push_back("-o"); 5749 CmdArgs.push_back(Output.getFilename()); 5750 5751 for (InputInfoList::const_iterator 5752 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5753 const InputInfo &II = *it; 5754 CmdArgs.push_back(II.getFilename()); 5755 } 5756 5757 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as"))); 5758 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5759} 5760 5761void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA, 5762 const InputInfo &Output, 5763 const InputInfoList &Inputs, 5764 const ArgList &Args, 5765 const char *LinkingOutput) const { 5766 const Driver &D = getToolChain().getDriver(); 5767 ArgStringList CmdArgs; 5768 5769 if (!D.SysRoot.empty()) 5770 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 5771 5772 if (Args.hasArg(options::OPT_static)) { 5773 CmdArgs.push_back("-Bstatic"); 5774 } else { 5775 if (Args.hasArg(options::OPT_rdynamic)) 5776 CmdArgs.push_back("-export-dynamic"); 5777 CmdArgs.push_back("--eh-frame-hdr"); 5778 if (Args.hasArg(options::OPT_shared)) { 5779 CmdArgs.push_back("-Bshareable"); 5780 } else { 5781 CmdArgs.push_back("-dynamic-linker"); 5782 CmdArgs.push_back("/libexec/ld.elf_so"); 5783 } 5784 } 5785 5786 // When building 32-bit code on NetBSD/amd64, we have to explicitly 5787 // instruct ld in the base system to link 32-bit code. 5788 if (getToolChain().getArch() == llvm::Triple::x86) { 5789 CmdArgs.push_back("-m"); 5790 CmdArgs.push_back("elf_i386"); 5791 } 5792 5793 if (Output.isFilename()) { 5794 CmdArgs.push_back("-o"); 5795 CmdArgs.push_back(Output.getFilename()); 5796 } else { 5797 assert(Output.isNothing() && "Invalid output."); 5798 } 5799 5800 if (!Args.hasArg(options::OPT_nostdlib) && 5801 !Args.hasArg(options::OPT_nostartfiles)) { 5802 if (!Args.hasArg(options::OPT_shared)) { 5803 CmdArgs.push_back(Args.MakeArgString( 5804 getToolChain().GetFilePath("crt0.o"))); 5805 CmdArgs.push_back(Args.MakeArgString( 5806 getToolChain().GetFilePath("crti.o"))); 5807 CmdArgs.push_back(Args.MakeArgString( 5808 getToolChain().GetFilePath("crtbegin.o"))); 5809 } else { 5810 CmdArgs.push_back(Args.MakeArgString( 5811 getToolChain().GetFilePath("crti.o"))); 5812 CmdArgs.push_back(Args.MakeArgString( 5813 getToolChain().GetFilePath("crtbeginS.o"))); 5814 } 5815 } 5816 5817 Args.AddAllArgs(CmdArgs, options::OPT_L); 5818 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5819 Args.AddAllArgs(CmdArgs, options::OPT_e); 5820 Args.AddAllArgs(CmdArgs, options::OPT_s); 5821 Args.AddAllArgs(CmdArgs, options::OPT_t); 5822 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 5823 Args.AddAllArgs(CmdArgs, options::OPT_r); 5824 5825 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5826 5827 if (!Args.hasArg(options::OPT_nostdlib) && 5828 !Args.hasArg(options::OPT_nodefaultlibs)) { 5829 if (D.CCCIsCXX()) { 5830 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 5831 CmdArgs.push_back("-lm"); 5832 } 5833 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding 5834 // the default system libraries. Just mimic this for now. 5835 if (Args.hasArg(options::OPT_static)) { 5836 CmdArgs.push_back("-lgcc_eh"); 5837 } else { 5838 CmdArgs.push_back("--as-needed"); 5839 CmdArgs.push_back("-lgcc_s"); 5840 CmdArgs.push_back("--no-as-needed"); 5841 } 5842 CmdArgs.push_back("-lgcc"); 5843 5844 if (Args.hasArg(options::OPT_pthread)) 5845 CmdArgs.push_back("-lpthread"); 5846 CmdArgs.push_back("-lc"); 5847 5848 CmdArgs.push_back("-lgcc"); 5849 if (Args.hasArg(options::OPT_static)) { 5850 CmdArgs.push_back("-lgcc_eh"); 5851 } else { 5852 CmdArgs.push_back("--as-needed"); 5853 CmdArgs.push_back("-lgcc_s"); 5854 CmdArgs.push_back("--no-as-needed"); 5855 } 5856 } 5857 5858 if (!Args.hasArg(options::OPT_nostdlib) && 5859 !Args.hasArg(options::OPT_nostartfiles)) { 5860 if (!Args.hasArg(options::OPT_shared)) 5861 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath( 5862 "crtend.o"))); 5863 else 5864 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath( 5865 "crtendS.o"))); 5866 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath( 5867 "crtn.o"))); 5868 } 5869 5870 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 5871 5872 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5873 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5874} 5875 5876void gnutools::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5877 const InputInfo &Output, 5878 const InputInfoList &Inputs, 5879 const ArgList &Args, 5880 const char *LinkingOutput) const { 5881 ArgStringList CmdArgs; 5882 5883 // Add --32/--64 to make sure we get the format we want. 5884 // This is incomplete 5885 if (getToolChain().getArch() == llvm::Triple::x86) { 5886 CmdArgs.push_back("--32"); 5887 } else if (getToolChain().getArch() == llvm::Triple::x86_64) { 5888 CmdArgs.push_back("--64"); 5889 } else if (getToolChain().getArch() == llvm::Triple::ppc) { 5890 CmdArgs.push_back("-a32"); 5891 CmdArgs.push_back("-mppc"); 5892 CmdArgs.push_back("-many"); 5893 } else if (getToolChain().getArch() == llvm::Triple::ppc64) { 5894 CmdArgs.push_back("-a64"); 5895 CmdArgs.push_back("-mppc64"); 5896 CmdArgs.push_back("-many"); 5897 } else if (getToolChain().getArch() == llvm::Triple::ppc64le) { 5898 CmdArgs.push_back("-a64"); 5899 CmdArgs.push_back("-mppc64le"); 5900 CmdArgs.push_back("-many"); 5901 } else if (getToolChain().getArch() == llvm::Triple::arm) { 5902 StringRef MArch = getToolChain().getArchName(); 5903 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a") 5904 CmdArgs.push_back("-mfpu=neon"); 5905 5906 StringRef ARMFloatABI = getARMFloatABI(getToolChain().getDriver(), Args, 5907 getToolChain().getTriple()); 5908 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI)); 5909 5910 Args.AddLastArg(CmdArgs, options::OPT_march_EQ); 5911 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ); 5912 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ); 5913 } else if (getToolChain().getArch() == llvm::Triple::mips || 5914 getToolChain().getArch() == llvm::Triple::mipsel || 5915 getToolChain().getArch() == llvm::Triple::mips64 || 5916 getToolChain().getArch() == llvm::Triple::mips64el) { 5917 StringRef CPUName; 5918 StringRef ABIName; 5919 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName); 5920 5921 CmdArgs.push_back("-march"); 5922 CmdArgs.push_back(CPUName.data()); 5923 5924 CmdArgs.push_back("-mabi"); 5925 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data()); 5926 5927 if (getToolChain().getArch() == llvm::Triple::mips || 5928 getToolChain().getArch() == llvm::Triple::mips64) 5929 CmdArgs.push_back("-EB"); 5930 else 5931 CmdArgs.push_back("-EL"); 5932 5933 Args.AddLastArg(CmdArgs, options::OPT_mips16, options::OPT_mno_mips16); 5934 Args.AddLastArg(CmdArgs, options::OPT_mmicromips, 5935 options::OPT_mno_micromips); 5936 Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp); 5937 Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2); 5938 5939 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC, 5940 options::OPT_fpic, options::OPT_fno_pic, 5941 options::OPT_fPIE, options::OPT_fno_PIE, 5942 options::OPT_fpie, options::OPT_fno_pie); 5943 if (LastPICArg && 5944 (LastPICArg->getOption().matches(options::OPT_fPIC) || 5945 LastPICArg->getOption().matches(options::OPT_fpic) || 5946 LastPICArg->getOption().matches(options::OPT_fPIE) || 5947 LastPICArg->getOption().matches(options::OPT_fpie))) { 5948 CmdArgs.push_back("-KPIC"); 5949 } 5950 } else if (getToolChain().getArch() == llvm::Triple::systemz) { 5951 // Always pass an -march option, since our default of z10 is later 5952 // than the GNU assembler's default. 5953 StringRef CPUName = getSystemZTargetCPU(Args); 5954 CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName)); 5955 } 5956 5957 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5958 options::OPT_Xassembler); 5959 5960 CmdArgs.push_back("-o"); 5961 CmdArgs.push_back(Output.getFilename()); 5962 5963 for (InputInfoList::const_iterator 5964 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5965 const InputInfo &II = *it; 5966 CmdArgs.push_back(II.getFilename()); 5967 } 5968 5969 const char *Exec = 5970 Args.MakeArgString(getToolChain().GetProgramPath("as")); 5971 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5972 5973 // Handle the debug info splitting at object creation time if we're 5974 // creating an object. 5975 // TODO: Currently only works on linux with newer objcopy. 5976 if (Args.hasArg(options::OPT_gsplit_dwarf) && 5977 (getToolChain().getTriple().getOS() == llvm::Triple::Linux)) 5978 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, 5979 SplitDebugName(Args, Inputs)); 5980} 5981 5982static void AddLibgcc(llvm::Triple Triple, const Driver &D, 5983 ArgStringList &CmdArgs, const ArgList &Args) { 5984 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android; 5985 bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) || 5986 Args.hasArg(options::OPT_static); 5987 if (!D.CCCIsCXX()) 5988 CmdArgs.push_back("-lgcc"); 5989 5990 if (StaticLibgcc || isAndroid) { 5991 if (D.CCCIsCXX()) 5992 CmdArgs.push_back("-lgcc"); 5993 } else { 5994 if (!D.CCCIsCXX()) 5995 CmdArgs.push_back("--as-needed"); 5996 CmdArgs.push_back("-lgcc_s"); 5997 if (!D.CCCIsCXX()) 5998 CmdArgs.push_back("--no-as-needed"); 5999 } 6000 6001 if (StaticLibgcc && !isAndroid) 6002 CmdArgs.push_back("-lgcc_eh"); 6003 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX()) 6004 CmdArgs.push_back("-lgcc"); 6005 6006 // According to Android ABI, we have to link with libdl if we are 6007 // linking with non-static libgcc. 6008 // 6009 // NOTE: This fixes a link error on Android MIPS as well. The non-static 6010 // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl. 6011 if (isAndroid && !StaticLibgcc) 6012 CmdArgs.push_back("-ldl"); 6013} 6014 6015static bool hasMipsN32ABIArg(const ArgList &Args) { 6016 Arg *A = Args.getLastArg(options::OPT_mabi_EQ); 6017 return A && (A->getValue() == StringRef("n32")); 6018} 6019 6020static StringRef getLinuxDynamicLinker(const ArgList &Args, 6021 const toolchains::Linux &ToolChain) { 6022 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::Android) 6023 return "/system/bin/linker"; 6024 else if (ToolChain.getArch() == llvm::Triple::x86) 6025 return "/lib/ld-linux.so.2"; 6026 else if (ToolChain.getArch() == llvm::Triple::aarch64) 6027 return "/lib/ld-linux-aarch64.so.1"; 6028 else if (ToolChain.getArch() == llvm::Triple::arm || 6029 ToolChain.getArch() == llvm::Triple::thumb) { 6030 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 6031 return "/lib/ld-linux-armhf.so.3"; 6032 else 6033 return "/lib/ld-linux.so.3"; 6034 } else if (ToolChain.getArch() == llvm::Triple::mips || 6035 ToolChain.getArch() == llvm::Triple::mipsel) 6036 return "/lib/ld.so.1"; 6037 else if (ToolChain.getArch() == llvm::Triple::mips64 || 6038 ToolChain.getArch() == llvm::Triple::mips64el) { 6039 if (hasMipsN32ABIArg(Args)) 6040 return "/lib32/ld.so.1"; 6041 else 6042 return "/lib64/ld.so.1"; 6043 } else if (ToolChain.getArch() == llvm::Triple::ppc) 6044 return "/lib/ld.so.1"; 6045 else if (ToolChain.getArch() == llvm::Triple::ppc64 || 6046 ToolChain.getArch() == llvm::Triple::ppc64le || 6047 ToolChain.getArch() == llvm::Triple::systemz) 6048 return "/lib64/ld64.so.1"; 6049 else 6050 return "/lib64/ld-linux-x86-64.so.2"; 6051} 6052 6053void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA, 6054 const InputInfo &Output, 6055 const InputInfoList &Inputs, 6056 const ArgList &Args, 6057 const char *LinkingOutput) const { 6058 const toolchains::Linux& ToolChain = 6059 static_cast<const toolchains::Linux&>(getToolChain()); 6060 const Driver &D = ToolChain.getDriver(); 6061 const bool isAndroid = 6062 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android; 6063 const SanitizerArgs &Sanitize = D.getOrParseSanitizerArgs(Args); 6064 const bool IsPIE = 6065 !Args.hasArg(options::OPT_shared) && 6066 (Args.hasArg(options::OPT_pie) || Sanitize.hasZeroBaseShadow(ToolChain)); 6067 6068 ArgStringList CmdArgs; 6069 6070 // Silence warning for "clang -g foo.o -o foo" 6071 Args.ClaimAllArgs(options::OPT_g_Group); 6072 // and "clang -emit-llvm foo.o -o foo" 6073 Args.ClaimAllArgs(options::OPT_emit_llvm); 6074 // and for "clang -w foo.o -o foo". Other warning options are already 6075 // handled somewhere else. 6076 Args.ClaimAllArgs(options::OPT_w); 6077 6078 if (!D.SysRoot.empty()) 6079 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 6080 6081 if (IsPIE) 6082 CmdArgs.push_back("-pie"); 6083 6084 if (Args.hasArg(options::OPT_rdynamic)) 6085 CmdArgs.push_back("-export-dynamic"); 6086 6087 if (Args.hasArg(options::OPT_s)) 6088 CmdArgs.push_back("-s"); 6089 6090 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(), 6091 e = ToolChain.ExtraOpts.end(); 6092 i != e; ++i) 6093 CmdArgs.push_back(i->c_str()); 6094 6095 if (!Args.hasArg(options::OPT_static)) { 6096 CmdArgs.push_back("--eh-frame-hdr"); 6097 } 6098 6099 CmdArgs.push_back("-m"); 6100 if (ToolChain.getArch() == llvm::Triple::x86) 6101 CmdArgs.push_back("elf_i386"); 6102 else if (ToolChain.getArch() == llvm::Triple::aarch64) 6103 CmdArgs.push_back("aarch64linux"); 6104 else if (ToolChain.getArch() == llvm::Triple::arm 6105 || ToolChain.getArch() == llvm::Triple::thumb) 6106 CmdArgs.push_back("armelf_linux_eabi"); 6107 else if (ToolChain.getArch() == llvm::Triple::ppc) 6108 CmdArgs.push_back("elf32ppclinux"); 6109 else if (ToolChain.getArch() == llvm::Triple::ppc64) 6110 CmdArgs.push_back("elf64ppc"); 6111 else if (ToolChain.getArch() == llvm::Triple::mips) 6112 CmdArgs.push_back("elf32btsmip"); 6113 else if (ToolChain.getArch() == llvm::Triple::mipsel) 6114 CmdArgs.push_back("elf32ltsmip"); 6115 else if (ToolChain.getArch() == llvm::Triple::mips64) { 6116 if (hasMipsN32ABIArg(Args)) 6117 CmdArgs.push_back("elf32btsmipn32"); 6118 else 6119 CmdArgs.push_back("elf64btsmip"); 6120 } 6121 else if (ToolChain.getArch() == llvm::Triple::mips64el) { 6122 if (hasMipsN32ABIArg(Args)) 6123 CmdArgs.push_back("elf32ltsmipn32"); 6124 else 6125 CmdArgs.push_back("elf64ltsmip"); 6126 } 6127 else if (ToolChain.getArch() == llvm::Triple::systemz) 6128 CmdArgs.push_back("elf64_s390"); 6129 else 6130 CmdArgs.push_back("elf_x86_64"); 6131 6132 if (Args.hasArg(options::OPT_static)) { 6133 if (ToolChain.getArch() == llvm::Triple::arm 6134 || ToolChain.getArch() == llvm::Triple::thumb) 6135 CmdArgs.push_back("-Bstatic"); 6136 else 6137 CmdArgs.push_back("-static"); 6138 } else if (Args.hasArg(options::OPT_shared)) { 6139 CmdArgs.push_back("-shared"); 6140 if (isAndroid) { 6141 CmdArgs.push_back("-Bsymbolic"); 6142 } 6143 } 6144 6145 if (ToolChain.getArch() == llvm::Triple::arm || 6146 ToolChain.getArch() == llvm::Triple::thumb || 6147 (!Args.hasArg(options::OPT_static) && 6148 !Args.hasArg(options::OPT_shared))) { 6149 CmdArgs.push_back("-dynamic-linker"); 6150 CmdArgs.push_back(Args.MakeArgString( 6151 D.DyldPrefix + getLinuxDynamicLinker(Args, ToolChain))); 6152 } 6153 6154 CmdArgs.push_back("-o"); 6155 CmdArgs.push_back(Output.getFilename()); 6156 6157 if (!Args.hasArg(options::OPT_nostdlib) && 6158 !Args.hasArg(options::OPT_nostartfiles)) { 6159 if (!isAndroid) { 6160 const char *crt1 = NULL; 6161 if (!Args.hasArg(options::OPT_shared)){ 6162 if (Args.hasArg(options::OPT_pg)) 6163 crt1 = "gcrt1.o"; 6164 else if (IsPIE) 6165 crt1 = "Scrt1.o"; 6166 else 6167 crt1 = "crt1.o"; 6168 } 6169 if (crt1) 6170 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1))); 6171 6172 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); 6173 } 6174 6175 const char *crtbegin; 6176 if (Args.hasArg(options::OPT_static)) 6177 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o"; 6178 else if (Args.hasArg(options::OPT_shared)) 6179 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o"; 6180 else if (IsPIE) 6181 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o"; 6182 else 6183 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o"; 6184 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); 6185 6186 // Add crtfastmath.o if available and fast math is enabled. 6187 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs); 6188 } 6189 6190 Args.AddAllArgs(CmdArgs, options::OPT_L); 6191 6192 const ToolChain::path_list Paths = ToolChain.getFilePaths(); 6193 6194 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end(); 6195 i != e; ++i) 6196 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i)); 6197 6198 // Tell the linker to load the plugin. This has to come before AddLinkerInputs 6199 // as gold requires -plugin to come before any -plugin-opt that -Wl might 6200 // forward. 6201 if (D.IsUsingLTO(Args) || Args.hasArg(options::OPT_use_gold_plugin)) { 6202 CmdArgs.push_back("-plugin"); 6203 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so"; 6204 CmdArgs.push_back(Args.MakeArgString(Plugin)); 6205 6206 // Try to pass driver level flags relevant to LTO code generation down to 6207 // the plugin. 6208 6209 // Handle architecture-specific flags for selecting CPU variants. 6210 if (ToolChain.getArch() == llvm::Triple::x86 || 6211 ToolChain.getArch() == llvm::Triple::x86_64) 6212 CmdArgs.push_back( 6213 Args.MakeArgString(Twine("-plugin-opt=mcpu=") + 6214 getX86TargetCPU(Args, ToolChain.getTriple()))); 6215 else if (ToolChain.getArch() == llvm::Triple::arm || 6216 ToolChain.getArch() == llvm::Triple::thumb) 6217 CmdArgs.push_back( 6218 Args.MakeArgString(Twine("-plugin-opt=mcpu=") + 6219 getARMTargetCPU(Args, ToolChain.getTriple()))); 6220 6221 // FIXME: Factor out logic for MIPS, PPC, and other targets to support this 6222 // as well. 6223 } 6224 6225 6226 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) 6227 CmdArgs.push_back("--no-demangle"); 6228 6229 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs); 6230 6231 // Call these before we add the C++ ABI library. 6232 if (Sanitize.needsUbsanRt()) 6233 addUbsanRTLinux(getToolChain(), Args, CmdArgs, D.CCCIsCXX(), 6234 Sanitize.needsAsanRt() || Sanitize.needsTsanRt() || 6235 Sanitize.needsMsanRt() || Sanitize.needsLsanRt()); 6236 if (Sanitize.needsAsanRt()) 6237 addAsanRTLinux(getToolChain(), Args, CmdArgs); 6238 if (Sanitize.needsTsanRt()) 6239 addTsanRTLinux(getToolChain(), Args, CmdArgs); 6240 if (Sanitize.needsMsanRt()) 6241 addMsanRTLinux(getToolChain(), Args, CmdArgs); 6242 if (Sanitize.needsLsanRt()) 6243 addLsanRTLinux(getToolChain(), Args, CmdArgs); 6244 if (Sanitize.needsDfsanRt()) 6245 addDfsanRTLinux(getToolChain(), Args, CmdArgs); 6246 6247 // The profile runtime also needs access to system libraries. 6248 addProfileRTLinux(getToolChain(), Args, CmdArgs); 6249 6250 if (D.CCCIsCXX() && 6251 !Args.hasArg(options::OPT_nostdlib) && 6252 !Args.hasArg(options::OPT_nodefaultlibs)) { 6253 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) && 6254 !Args.hasArg(options::OPT_static); 6255 if (OnlyLibstdcxxStatic) 6256 CmdArgs.push_back("-Bstatic"); 6257 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 6258 if (OnlyLibstdcxxStatic) 6259 CmdArgs.push_back("-Bdynamic"); 6260 CmdArgs.push_back("-lm"); 6261 } 6262 6263 if (!Args.hasArg(options::OPT_nostdlib)) { 6264 if (!Args.hasArg(options::OPT_nodefaultlibs)) { 6265 if (Args.hasArg(options::OPT_static)) 6266 CmdArgs.push_back("--start-group"); 6267 6268 bool OpenMP = Args.hasArg(options::OPT_fopenmp); 6269 if (OpenMP) { 6270 CmdArgs.push_back("-lgomp"); 6271 6272 // FIXME: Exclude this for platforms whith libgomp that doesn't require 6273 // librt. Most modern Linux platfroms require it, but some may not. 6274 CmdArgs.push_back("-lrt"); 6275 } 6276 6277 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args); 6278 6279 if (Args.hasArg(options::OPT_pthread) || 6280 Args.hasArg(options::OPT_pthreads) || OpenMP) 6281 CmdArgs.push_back("-lpthread"); 6282 6283 CmdArgs.push_back("-lc"); 6284 6285 if (Args.hasArg(options::OPT_static)) 6286 CmdArgs.push_back("--end-group"); 6287 else 6288 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args); 6289 } 6290 6291 if (!Args.hasArg(options::OPT_nostartfiles)) { 6292 const char *crtend; 6293 if (Args.hasArg(options::OPT_shared)) 6294 crtend = isAndroid ? "crtend_so.o" : "crtendS.o"; 6295 else if (IsPIE) 6296 crtend = isAndroid ? "crtend_android.o" : "crtendS.o"; 6297 else 6298 crtend = isAndroid ? "crtend_android.o" : "crtend.o"; 6299 6300 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend))); 6301 if (!isAndroid) 6302 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 6303 } 6304 } 6305 6306 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs)); 6307} 6308 6309void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 6310 const InputInfo &Output, 6311 const InputInfoList &Inputs, 6312 const ArgList &Args, 6313 const char *LinkingOutput) const { 6314 ArgStringList CmdArgs; 6315 6316 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 6317 options::OPT_Xassembler); 6318 6319 CmdArgs.push_back("-o"); 6320 CmdArgs.push_back(Output.getFilename()); 6321 6322 for (InputInfoList::const_iterator 6323 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 6324 const InputInfo &II = *it; 6325 CmdArgs.push_back(II.getFilename()); 6326 } 6327 6328 const char *Exec = 6329 Args.MakeArgString(getToolChain().GetProgramPath("as")); 6330 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6331} 6332 6333void minix::Link::ConstructJob(Compilation &C, const JobAction &JA, 6334 const InputInfo &Output, 6335 const InputInfoList &Inputs, 6336 const ArgList &Args, 6337 const char *LinkingOutput) const { 6338 const Driver &D = getToolChain().getDriver(); 6339 ArgStringList CmdArgs; 6340 6341 if (Output.isFilename()) { 6342 CmdArgs.push_back("-o"); 6343 CmdArgs.push_back(Output.getFilename()); 6344 } else { 6345 assert(Output.isNothing() && "Invalid output."); 6346 } 6347 6348 if (!Args.hasArg(options::OPT_nostdlib) && 6349 !Args.hasArg(options::OPT_nostartfiles)) { 6350 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o"))); 6351 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o"))); 6352 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o"))); 6353 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o"))); 6354 } 6355 6356 Args.AddAllArgs(CmdArgs, options::OPT_L); 6357 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 6358 Args.AddAllArgs(CmdArgs, options::OPT_e); 6359 6360 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 6361 6362 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 6363 6364 if (!Args.hasArg(options::OPT_nostdlib) && 6365 !Args.hasArg(options::OPT_nodefaultlibs)) { 6366 if (D.CCCIsCXX()) { 6367 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 6368 CmdArgs.push_back("-lm"); 6369 } 6370 } 6371 6372 if (!Args.hasArg(options::OPT_nostdlib) && 6373 !Args.hasArg(options::OPT_nostartfiles)) { 6374 if (Args.hasArg(options::OPT_pthread)) 6375 CmdArgs.push_back("-lpthread"); 6376 CmdArgs.push_back("-lc"); 6377 CmdArgs.push_back("-lCompilerRT-Generic"); 6378 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib"); 6379 CmdArgs.push_back( 6380 Args.MakeArgString(getToolChain().GetFilePath("crtend.o"))); 6381 } 6382 6383 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld")); 6384 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6385} 6386 6387/// DragonFly Tools 6388 6389// For now, DragonFly Assemble does just about the same as for 6390// FreeBSD, but this may change soon. 6391void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 6392 const InputInfo &Output, 6393 const InputInfoList &Inputs, 6394 const ArgList &Args, 6395 const char *LinkingOutput) const { 6396 ArgStringList CmdArgs; 6397 6398 // When building 32-bit code on DragonFly/pc64, we have to explicitly 6399 // instruct as in the base system to assemble 32-bit code. 6400 if (getToolChain().getArch() == llvm::Triple::x86) 6401 CmdArgs.push_back("--32"); 6402 6403 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 6404 options::OPT_Xassembler); 6405 6406 CmdArgs.push_back("-o"); 6407 CmdArgs.push_back(Output.getFilename()); 6408 6409 for (InputInfoList::const_iterator 6410 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 6411 const InputInfo &II = *it; 6412 CmdArgs.push_back(II.getFilename()); 6413 } 6414 6415 const char *Exec = 6416 Args.MakeArgString(getToolChain().GetProgramPath("as")); 6417 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6418} 6419 6420void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA, 6421 const InputInfo &Output, 6422 const InputInfoList &Inputs, 6423 const ArgList &Args, 6424 const char *LinkingOutput) const { 6425 bool UseGCC47 = false; 6426 const Driver &D = getToolChain().getDriver(); 6427 ArgStringList CmdArgs; 6428 6429 if (llvm::sys::fs::exists("/usr/lib/gcc47", UseGCC47)) 6430 UseGCC47 = false; 6431 6432 if (!D.SysRoot.empty()) 6433 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 6434 6435 CmdArgs.push_back("--eh-frame-hdr"); 6436 if (Args.hasArg(options::OPT_static)) { 6437 CmdArgs.push_back("-Bstatic"); 6438 } else { 6439 if (Args.hasArg(options::OPT_rdynamic)) 6440 CmdArgs.push_back("-export-dynamic"); 6441 if (Args.hasArg(options::OPT_shared)) 6442 CmdArgs.push_back("-Bshareable"); 6443 else { 6444 CmdArgs.push_back("-dynamic-linker"); 6445 CmdArgs.push_back("/usr/libexec/ld-elf.so.2"); 6446 } 6447 CmdArgs.push_back("--hash-style=both"); 6448 } 6449 6450 // When building 32-bit code on DragonFly/pc64, we have to explicitly 6451 // instruct ld in the base system to link 32-bit code. 6452 if (getToolChain().getArch() == llvm::Triple::x86) { 6453 CmdArgs.push_back("-m"); 6454 CmdArgs.push_back("elf_i386"); 6455 } 6456 6457 if (Output.isFilename()) { 6458 CmdArgs.push_back("-o"); 6459 CmdArgs.push_back(Output.getFilename()); 6460 } else { 6461 assert(Output.isNothing() && "Invalid output."); 6462 } 6463 6464 if (!Args.hasArg(options::OPT_nostdlib) && 6465 !Args.hasArg(options::OPT_nostartfiles)) { 6466 if (!Args.hasArg(options::OPT_shared)) { 6467 if (Args.hasArg(options::OPT_pg)) 6468 CmdArgs.push_back(Args.MakeArgString( 6469 getToolChain().GetFilePath("gcrt1.o"))); 6470 else { 6471 if (Args.hasArg(options::OPT_pie)) 6472 CmdArgs.push_back(Args.MakeArgString( 6473 getToolChain().GetFilePath("Scrt1.o"))); 6474 else 6475 CmdArgs.push_back(Args.MakeArgString( 6476 getToolChain().GetFilePath("crt1.o"))); 6477 } 6478 } 6479 CmdArgs.push_back(Args.MakeArgString( 6480 getToolChain().GetFilePath("crti.o"))); 6481 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) 6482 CmdArgs.push_back(Args.MakeArgString( 6483 getToolChain().GetFilePath("crtbeginS.o"))); 6484 else 6485 CmdArgs.push_back(Args.MakeArgString( 6486 getToolChain().GetFilePath("crtbegin.o"))); 6487 } 6488 6489 Args.AddAllArgs(CmdArgs, options::OPT_L); 6490 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 6491 Args.AddAllArgs(CmdArgs, options::OPT_e); 6492 6493 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 6494 6495 if (!Args.hasArg(options::OPT_nostdlib) && 6496 !Args.hasArg(options::OPT_nodefaultlibs)) { 6497 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of 6498 // rpaths 6499 if (UseGCC47) 6500 CmdArgs.push_back("-L/usr/lib/gcc47"); 6501 else 6502 CmdArgs.push_back("-L/usr/lib/gcc44"); 6503 6504 if (!Args.hasArg(options::OPT_static)) { 6505 if (UseGCC47) { 6506 CmdArgs.push_back("-rpath"); 6507 CmdArgs.push_back("/usr/lib/gcc47"); 6508 } else { 6509 CmdArgs.push_back("-rpath"); 6510 CmdArgs.push_back("/usr/lib/gcc44"); 6511 } 6512 } 6513 6514 if (D.CCCIsCXX()) { 6515 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 6516 CmdArgs.push_back("-lm"); 6517 } 6518 6519 if (Args.hasArg(options::OPT_pthread)) 6520 CmdArgs.push_back("-lpthread"); 6521 6522 if (!Args.hasArg(options::OPT_nolibc)) { 6523 CmdArgs.push_back("-lc"); 6524 } 6525 6526 if (UseGCC47) { 6527 if (Args.hasArg(options::OPT_static) || 6528 Args.hasArg(options::OPT_static_libgcc)) { 6529 CmdArgs.push_back("-lgcc"); 6530 CmdArgs.push_back("-lgcc_eh"); 6531 } else { 6532 if (Args.hasArg(options::OPT_shared_libgcc)) { 6533 CmdArgs.push_back("-lgcc_pic"); 6534 if (!Args.hasArg(options::OPT_shared)) 6535 CmdArgs.push_back("-lgcc"); 6536 } else { 6537 CmdArgs.push_back("-lgcc"); 6538 CmdArgs.push_back("--as-needed"); 6539 CmdArgs.push_back("-lgcc_pic"); 6540 CmdArgs.push_back("--no-as-needed"); 6541 } 6542 } 6543 } else { 6544 if (Args.hasArg(options::OPT_shared)) { 6545 CmdArgs.push_back("-lgcc_pic"); 6546 } else { 6547 CmdArgs.push_back("-lgcc"); 6548 } 6549 } 6550 } 6551 6552 if (!Args.hasArg(options::OPT_nostdlib) && 6553 !Args.hasArg(options::OPT_nostartfiles)) { 6554 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) 6555 CmdArgs.push_back(Args.MakeArgString( 6556 getToolChain().GetFilePath("crtendS.o"))); 6557 else 6558 CmdArgs.push_back(Args.MakeArgString( 6559 getToolChain().GetFilePath("crtend.o"))); 6560 CmdArgs.push_back(Args.MakeArgString( 6561 getToolChain().GetFilePath("crtn.o"))); 6562 } 6563 6564 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 6565 6566 const char *Exec = 6567 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 6568 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6569} 6570 6571void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA, 6572 const InputInfo &Output, 6573 const InputInfoList &Inputs, 6574 const ArgList &Args, 6575 const char *LinkingOutput) const { 6576 ArgStringList CmdArgs; 6577 6578 if (Output.isFilename()) { 6579 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") + 6580 Output.getFilename())); 6581 } else { 6582 assert(Output.isNothing() && "Invalid output."); 6583 } 6584 6585 if (!Args.hasArg(options::OPT_nostdlib) && 6586 !Args.hasArg(options::OPT_nostartfiles) && 6587 !C.getDriver().IsCLMode()) { 6588 CmdArgs.push_back("-defaultlib:libcmt"); 6589 } 6590 6591 CmdArgs.push_back("-nologo"); 6592 6593 Args.AddAllArgValues(CmdArgs, options::OPT_l); 6594 Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link); 6595 6596 // Add filenames immediately. 6597 for (InputInfoList::const_iterator 6598 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 6599 if (it->isFilename()) 6600 CmdArgs.push_back(it->getFilename()); 6601 else 6602 it->getInputArg().renderAsInput(Args, CmdArgs); 6603 } 6604 6605 const char *Exec = 6606 Args.MakeArgString(getToolChain().GetProgramPath("link.exe")); 6607 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6608} 6609