1 //===--- AIX.cpp - AIX ToolChain Implementations ----------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "AIX.h" 10 #include "Arch/PPC.h" 11 #include "CommonArgs.h" 12 #include "clang/Driver/Compilation.h" 13 #include "clang/Driver/Options.h" 14 #include "clang/Driver/SanitizerArgs.h" 15 #include "llvm/Option/ArgList.h" 16 #include "llvm/Support/Path.h" 17 18 using AIX = clang::driver::toolchains::AIX; 19 using namespace clang::driver; 20 using namespace clang::driver::tools; 21 using namespace clang::driver::toolchains; 22 23 using namespace llvm::opt; 24 using namespace llvm::sys; 25 26 void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 27 const InputInfo &Output, 28 const InputInfoList &Inputs, 29 const ArgList &Args, 30 const char *LinkingOutput) const { 31 ArgStringList CmdArgs; 32 33 const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit(); 34 const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit(); 35 // Only support 32 and 64 bit. 36 if (!IsArch32Bit && !IsArch64Bit) 37 llvm_unreachable("Unsupported bit width value."); 38 39 // Specify the mode in which the as(1) command operates. 40 if (IsArch32Bit) { 41 CmdArgs.push_back("-a32"); 42 } else { 43 // Must be 64-bit, otherwise asserted already. 44 CmdArgs.push_back("-a64"); 45 } 46 47 // Accept an undefined symbol as an extern so that an error message is not 48 // displayed. Otherwise, undefined symbols are flagged with error messages. 49 // FIXME: This should be removed when the assembly generation from the 50 // compiler is able to write externs properly. 51 CmdArgs.push_back("-u"); 52 53 // Accept any mixture of instructions. 54 // On Power for AIX and Linux, this behaviour matches that of GCC for both the 55 // user-provided assembler source case and the compiler-produced assembler 56 // source case. Yet XL with user-provided assembler source would not add this. 57 CmdArgs.push_back("-many"); 58 59 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 60 61 // Specify assembler output file. 62 assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); 63 if (Output.isFilename()) { 64 CmdArgs.push_back("-o"); 65 CmdArgs.push_back(Output.getFilename()); 66 } 67 68 // Specify assembler input file. 69 // The system assembler on AIX takes exactly one input file. The driver is 70 // expected to invoke as(1) separately for each assembler source input file. 71 if (Inputs.size() != 1) 72 llvm_unreachable("Invalid number of input files."); 73 const InputInfo &II = Inputs[0]; 74 assert((II.isFilename() || II.isNothing()) && "Invalid input."); 75 if (II.isFilename()) 76 CmdArgs.push_back(II.getFilename()); 77 78 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); 79 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 80 Exec, CmdArgs, Inputs)); 81 } 82 83 void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA, 84 const InputInfo &Output, 85 const InputInfoList &Inputs, const ArgList &Args, 86 const char *LinkingOutput) const { 87 const AIX &ToolChain = static_cast<const AIX &>(getToolChain()); 88 const Driver &D = ToolChain.getDriver(); 89 ArgStringList CmdArgs; 90 91 const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit(); 92 const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit(); 93 // Only support 32 and 64 bit. 94 if (!(IsArch32Bit || IsArch64Bit)) 95 llvm_unreachable("Unsupported bit width value."); 96 97 // Force static linking when "-static" is present. 98 if (Args.hasArg(options::OPT_static)) 99 CmdArgs.push_back("-bnso"); 100 101 // Specify linker output file. 102 assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); 103 if (Output.isFilename()) { 104 CmdArgs.push_back("-o"); 105 CmdArgs.push_back(Output.getFilename()); 106 } 107 108 // Set linking mode (i.e., 32/64-bit) and the address of 109 // text and data sections based on arch bit width. 110 if (IsArch32Bit) { 111 CmdArgs.push_back("-b32"); 112 CmdArgs.push_back("-bpT:0x10000000"); 113 CmdArgs.push_back("-bpD:0x20000000"); 114 } else { 115 // Must be 64-bit, otherwise asserted already. 116 CmdArgs.push_back("-b64"); 117 CmdArgs.push_back("-bpT:0x100000000"); 118 CmdArgs.push_back("-bpD:0x110000000"); 119 } 120 121 auto getCrt0Basename = [&Args, IsArch32Bit] { 122 // Enable gprofiling when "-pg" is specified. 123 if (Args.hasArg(options::OPT_pg)) 124 return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o"; 125 // Enable profiling when "-p" is specified. 126 else if (Args.hasArg(options::OPT_p)) 127 return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o"; 128 else 129 return IsArch32Bit ? "crt0.o" : "crt0_64.o"; 130 }; 131 132 if (!Args.hasArg(options::OPT_nostdlib)) { 133 CmdArgs.push_back( 134 Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename()))); 135 } 136 137 // Collect all static constructor and destructor functions in CXX mode. This 138 // has to come before AddLinkerInputs as the implied option needs to precede 139 // any other '-bcdtors' settings or '-bnocdtors' that '-Wl' might forward. 140 if (D.CCCIsCXX()) 141 CmdArgs.push_back("-bcdtors:all:0:s"); 142 143 // Specify linker input file(s). 144 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 145 146 // Add directory to library search path. 147 Args.AddAllArgs(CmdArgs, options::OPT_L); 148 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 149 150 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 151 // Support POSIX threads if "-pthreads" or "-pthread" is present. 152 if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread)) 153 CmdArgs.push_back("-lpthreads"); 154 155 CmdArgs.push_back("-lc"); 156 } 157 158 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 159 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 160 Exec, CmdArgs, Inputs)); 161 } 162 163 /// AIX - AIX tool chain which can call as(1) and ld(1) directly. 164 AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 165 : ToolChain(D, Triple, Args) { 166 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib"); 167 } 168 169 // Returns the effective header sysroot path to use. 170 // This comes from either -isysroot or --sysroot. 171 llvm::StringRef 172 AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const { 173 if (DriverArgs.hasArg(options::OPT_isysroot)) 174 return DriverArgs.getLastArgValue(options::OPT_isysroot); 175 if (!getDriver().SysRoot.empty()) 176 return getDriver().SysRoot; 177 return "/"; 178 } 179 180 void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 181 ArgStringList &CC1Args) const { 182 // Return if -nostdinc is specified as a driver option. 183 if (DriverArgs.hasArg(options::OPT_nostdinc)) 184 return; 185 186 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs); 187 const Driver &D = getDriver(); 188 189 // Add the Clang builtin headers (<resource>/include). 190 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 191 SmallString<128> P(D.ResourceDir); 192 path::append(P, "/include"); 193 addSystemInclude(DriverArgs, CC1Args, P.str()); 194 } 195 196 // Return if -nostdlibinc is specified as a driver option. 197 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 198 return; 199 200 // Add <sysroot>/usr/include. 201 SmallString<128> UP(Sysroot); 202 path::append(UP, "/usr/include"); 203 addSystemInclude(DriverArgs, CC1Args, UP.str()); 204 } 205 206 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); } 207 208 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); } 209