1 //===--- NaCl.cpp - Native Client ToolChain Implementations -----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "NaCl.h" 11 #include "InputInfo.h" 12 #include "CommonArgs.h" 13 #include "clang/Driver/Compilation.h" 14 #include "clang/Driver/Driver.h" 15 #include "clang/Driver/DriverDiagnostic.h" 16 #include "clang/Driver/Options.h" 17 #include "llvm/Option/ArgList.h" 18 #include "llvm/Support/Path.h" 19 20 using namespace clang::driver; 21 using namespace clang::driver::tools; 22 using namespace clang::driver::toolchains; 23 using namespace clang; 24 using namespace llvm::opt; 25 26 // NaCl ARM assembly (inline or standalone) can be written with a set of macros 27 // for the various SFI requirements like register masking. The assembly tool 28 // inserts the file containing the macros as an input into all the assembly 29 // jobs. 30 void nacltools::AssemblerARM::ConstructJob(Compilation &C, const JobAction &JA, 31 const InputInfo &Output, 32 const InputInfoList &Inputs, 33 const ArgList &Args, 34 const char *LinkingOutput) const { 35 const toolchains::NaClToolChain &ToolChain = 36 static_cast<const toolchains::NaClToolChain &>(getToolChain()); 37 InputInfo NaClMacros(types::TY_PP_Asm, ToolChain.GetNaClArmMacrosPath(), 38 "nacl-arm-macros.s"); 39 InputInfoList NewInputs; 40 NewInputs.push_back(NaClMacros); 41 NewInputs.append(Inputs.begin(), Inputs.end()); 42 gnutools::Assembler::ConstructJob(C, JA, Output, NewInputs, Args, 43 LinkingOutput); 44 } 45 46 // This is quite similar to gnutools::Linker::ConstructJob with changes that 47 // we use static by default, do not yet support sanitizers or LTO, and a few 48 // others. Eventually we can support more of that and hopefully migrate back 49 // to gnutools::Linker. 50 void nacltools::Linker::ConstructJob(Compilation &C, const JobAction &JA, 51 const InputInfo &Output, 52 const InputInfoList &Inputs, 53 const ArgList &Args, 54 const char *LinkingOutput) const { 55 56 const toolchains::NaClToolChain &ToolChain = 57 static_cast<const toolchains::NaClToolChain &>(getToolChain()); 58 const Driver &D = ToolChain.getDriver(); 59 const llvm::Triple::ArchType Arch = ToolChain.getArch(); 60 const bool IsStatic = 61 !Args.hasArg(options::OPT_dynamic) && !Args.hasArg(options::OPT_shared); 62 63 ArgStringList CmdArgs; 64 65 // Silence warning for "clang -g foo.o -o foo" 66 Args.ClaimAllArgs(options::OPT_g_Group); 67 // and "clang -emit-llvm foo.o -o foo" 68 Args.ClaimAllArgs(options::OPT_emit_llvm); 69 // and for "clang -w foo.o -o foo". Other warning options are already 70 // handled somewhere else. 71 Args.ClaimAllArgs(options::OPT_w); 72 73 if (!D.SysRoot.empty()) 74 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 75 76 if (Args.hasArg(options::OPT_rdynamic)) 77 CmdArgs.push_back("-export-dynamic"); 78 79 if (Args.hasArg(options::OPT_s)) 80 CmdArgs.push_back("-s"); 81 82 // NaClToolChain doesn't have ExtraOpts like Linux; the only relevant flag 83 // from there is --build-id, which we do want. 84 CmdArgs.push_back("--build-id"); 85 86 if (!IsStatic) 87 CmdArgs.push_back("--eh-frame-hdr"); 88 89 CmdArgs.push_back("-m"); 90 if (Arch == llvm::Triple::x86) 91 CmdArgs.push_back("elf_i386_nacl"); 92 else if (Arch == llvm::Triple::arm) 93 CmdArgs.push_back("armelf_nacl"); 94 else if (Arch == llvm::Triple::x86_64) 95 CmdArgs.push_back("elf_x86_64_nacl"); 96 else if (Arch == llvm::Triple::mipsel) 97 CmdArgs.push_back("mipselelf_nacl"); 98 else 99 D.Diag(diag::err_target_unsupported_arch) << ToolChain.getArchName() 100 << "Native Client"; 101 102 if (IsStatic) 103 CmdArgs.push_back("-static"); 104 else if (Args.hasArg(options::OPT_shared)) 105 CmdArgs.push_back("-shared"); 106 107 CmdArgs.push_back("-o"); 108 CmdArgs.push_back(Output.getFilename()); 109 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 110 if (!Args.hasArg(options::OPT_shared)) 111 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o"))); 112 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); 113 114 const char *crtbegin; 115 if (IsStatic) 116 crtbegin = "crtbeginT.o"; 117 else if (Args.hasArg(options::OPT_shared)) 118 crtbegin = "crtbeginS.o"; 119 else 120 crtbegin = "crtbegin.o"; 121 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); 122 } 123 124 Args.AddAllArgs(CmdArgs, options::OPT_L); 125 Args.AddAllArgs(CmdArgs, options::OPT_u); 126 127 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 128 129 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) 130 CmdArgs.push_back("--no-demangle"); 131 132 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 133 134 if (D.CCCIsCXX() && 135 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 136 bool OnlyLibstdcxxStatic = 137 Args.hasArg(options::OPT_static_libstdcxx) && !IsStatic; 138 if (OnlyLibstdcxxStatic) 139 CmdArgs.push_back("-Bstatic"); 140 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 141 if (OnlyLibstdcxxStatic) 142 CmdArgs.push_back("-Bdynamic"); 143 CmdArgs.push_back("-lm"); 144 } 145 146 if (!Args.hasArg(options::OPT_nostdlib)) { 147 if (!Args.hasArg(options::OPT_nodefaultlibs)) { 148 // Always use groups, since it has no effect on dynamic libraries. 149 CmdArgs.push_back("--start-group"); 150 CmdArgs.push_back("-lc"); 151 // NaCl's libc++ currently requires libpthread, so just always include it 152 // in the group for C++. 153 if (Args.hasArg(options::OPT_pthread) || 154 Args.hasArg(options::OPT_pthreads) || D.CCCIsCXX()) { 155 // Gold, used by Mips, handles nested groups differently than ld, and 156 // without '-lnacl' it prefers symbols from libpthread.a over libnacl.a, 157 // which is not a desired behaviour here. 158 // See https://sourceware.org/ml/binutils/2015-03/msg00034.html 159 if (getToolChain().getArch() == llvm::Triple::mipsel) 160 CmdArgs.push_back("-lnacl"); 161 162 CmdArgs.push_back("-lpthread"); 163 } 164 165 CmdArgs.push_back("-lgcc"); 166 CmdArgs.push_back("--as-needed"); 167 if (IsStatic) 168 CmdArgs.push_back("-lgcc_eh"); 169 else 170 CmdArgs.push_back("-lgcc_s"); 171 CmdArgs.push_back("--no-as-needed"); 172 173 // Mips needs to create and use pnacl_legacy library that contains 174 // definitions from bitcode/pnaclmm.c and definitions for 175 // __nacl_tp_tls_offset() and __nacl_tp_tdb_offset(). 176 if (getToolChain().getArch() == llvm::Triple::mipsel) 177 CmdArgs.push_back("-lpnacl_legacy"); 178 179 CmdArgs.push_back("--end-group"); 180 } 181 182 if (!Args.hasArg(options::OPT_nostartfiles)) { 183 const char *crtend; 184 if (Args.hasArg(options::OPT_shared)) 185 crtend = "crtendS.o"; 186 else 187 crtend = "crtend.o"; 188 189 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend))); 190 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 191 } 192 } 193 194 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 195 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 196 } 197 198 /// NaCl Toolchain 199 NaClToolChain::NaClToolChain(const Driver &D, const llvm::Triple &Triple, 200 const ArgList &Args) 201 : Generic_ELF(D, Triple, Args) { 202 203 // Remove paths added by Generic_GCC. NaCl Toolchain cannot use the 204 // default paths, and must instead only use the paths provided 205 // with this toolchain based on architecture. 206 path_list &file_paths = getFilePaths(); 207 path_list &prog_paths = getProgramPaths(); 208 209 file_paths.clear(); 210 prog_paths.clear(); 211 212 // Path for library files (libc.a, ...) 213 std::string FilePath(getDriver().Dir + "/../"); 214 215 // Path for tools (clang, ld, etc..) 216 std::string ProgPath(getDriver().Dir + "/../"); 217 218 // Path for toolchain libraries (libgcc.a, ...) 219 std::string ToolPath(getDriver().ResourceDir + "/lib/"); 220 221 switch (Triple.getArch()) { 222 case llvm::Triple::x86: 223 file_paths.push_back(FilePath + "x86_64-nacl/lib32"); 224 file_paths.push_back(FilePath + "i686-nacl/usr/lib"); 225 prog_paths.push_back(ProgPath + "x86_64-nacl/bin"); 226 file_paths.push_back(ToolPath + "i686-nacl"); 227 break; 228 case llvm::Triple::x86_64: 229 file_paths.push_back(FilePath + "x86_64-nacl/lib"); 230 file_paths.push_back(FilePath + "x86_64-nacl/usr/lib"); 231 prog_paths.push_back(ProgPath + "x86_64-nacl/bin"); 232 file_paths.push_back(ToolPath + "x86_64-nacl"); 233 break; 234 case llvm::Triple::arm: 235 file_paths.push_back(FilePath + "arm-nacl/lib"); 236 file_paths.push_back(FilePath + "arm-nacl/usr/lib"); 237 prog_paths.push_back(ProgPath + "arm-nacl/bin"); 238 file_paths.push_back(ToolPath + "arm-nacl"); 239 break; 240 case llvm::Triple::mipsel: 241 file_paths.push_back(FilePath + "mipsel-nacl/lib"); 242 file_paths.push_back(FilePath + "mipsel-nacl/usr/lib"); 243 prog_paths.push_back(ProgPath + "bin"); 244 file_paths.push_back(ToolPath + "mipsel-nacl"); 245 break; 246 default: 247 break; 248 } 249 250 NaClArmMacrosPath = GetFilePath("nacl-arm-macros.s"); 251 } 252 253 void NaClToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 254 ArgStringList &CC1Args) const { 255 const Driver &D = getDriver(); 256 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 257 return; 258 259 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 260 SmallString<128> P(D.ResourceDir); 261 llvm::sys::path::append(P, "include"); 262 addSystemInclude(DriverArgs, CC1Args, P.str()); 263 } 264 265 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 266 return; 267 268 SmallString<128> P(D.Dir + "/../"); 269 switch (getTriple().getArch()) { 270 case llvm::Triple::x86: 271 // x86 is special because multilib style uses x86_64-nacl/include for libc 272 // headers but the SDK wants i686-nacl/usr/include. The other architectures 273 // have the same substring. 274 llvm::sys::path::append(P, "i686-nacl/usr/include"); 275 addSystemInclude(DriverArgs, CC1Args, P.str()); 276 llvm::sys::path::remove_filename(P); 277 llvm::sys::path::remove_filename(P); 278 llvm::sys::path::remove_filename(P); 279 llvm::sys::path::append(P, "x86_64-nacl/include"); 280 addSystemInclude(DriverArgs, CC1Args, P.str()); 281 return; 282 case llvm::Triple::arm: 283 llvm::sys::path::append(P, "arm-nacl/usr/include"); 284 break; 285 case llvm::Triple::x86_64: 286 llvm::sys::path::append(P, "x86_64-nacl/usr/include"); 287 break; 288 case llvm::Triple::mipsel: 289 llvm::sys::path::append(P, "mipsel-nacl/usr/include"); 290 break; 291 default: 292 return; 293 } 294 295 addSystemInclude(DriverArgs, CC1Args, P.str()); 296 llvm::sys::path::remove_filename(P); 297 llvm::sys::path::remove_filename(P); 298 llvm::sys::path::append(P, "include"); 299 addSystemInclude(DriverArgs, CC1Args, P.str()); 300 } 301 302 void NaClToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 303 ArgStringList &CmdArgs) const { 304 // Check for -stdlib= flags. We only support libc++ but this consumes the arg 305 // if the value is libc++, and emits an error for other values. 306 GetCXXStdlibType(Args); 307 CmdArgs.push_back("-lc++"); 308 } 309 310 std::string NaClToolChain::findLibCxxIncludePath() const { 311 const Driver &D = getDriver(); 312 313 SmallString<128> P(D.Dir + "/../"); 314 switch (getTriple().getArch()) { 315 case llvm::Triple::arm: 316 llvm::sys::path::append(P, "arm-nacl/include/c++/v1"); 317 return P.str(); 318 case llvm::Triple::x86: 319 llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1"); 320 return P.str(); 321 case llvm::Triple::x86_64: 322 llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1"); 323 return P.str(); 324 case llvm::Triple::mipsel: 325 llvm::sys::path::append(P, "mipsel-nacl/include/c++/v1"); 326 return P.str(); 327 default: 328 return ""; 329 } 330 } 331 332 ToolChain::CXXStdlibType 333 NaClToolChain::GetCXXStdlibType(const ArgList &Args) const { 334 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 335 StringRef Value = A->getValue(); 336 if (Value == "libc++") 337 return ToolChain::CST_Libcxx; 338 getDriver().Diag(clang::diag::err_drv_invalid_stdlib_name) 339 << A->getAsString(Args); 340 } 341 342 return ToolChain::CST_Libcxx; 343 } 344 345 std::string 346 NaClToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 347 types::ID InputType) const { 348 llvm::Triple TheTriple(ComputeLLVMTriple(Args, InputType)); 349 if (TheTriple.getArch() == llvm::Triple::arm && 350 TheTriple.getEnvironment() == llvm::Triple::UnknownEnvironment) 351 TheTriple.setEnvironment(llvm::Triple::GNUEABIHF); 352 return TheTriple.getTriple(); 353 } 354 355 Tool *NaClToolChain::buildLinker() const { 356 return new tools::nacltools::Linker(*this); 357 } 358 359 Tool *NaClToolChain::buildAssembler() const { 360 if (getTriple().getArch() == llvm::Triple::arm) 361 return new tools::nacltools::AssemblerARM(*this); 362 return new tools::gnutools::Assembler(*this); 363 } 364