blob: 50450b7deb567ce7b7d865b5e21e099d755a9572 [file] [log] [blame]
stevewanbb6a27f2019-10-24 14:47:32 -04001//===--- 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
stevewanbb6a27f2019-10-24 14:47:32 -040017using AIX = clang::driver::toolchains::AIX;
David Tenty76ee21e2019-10-24 15:47:08 -040018using namespace clang::driver;
19using namespace clang::driver::tools;
stevewanbb6a27f2019-10-24 14:47:32 -040020
21using namespace llvm::opt;
22
23void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
24 const InputInfo &Output,
25 const InputInfoList &Inputs, const ArgList &Args,
26 const char *LinkingOutput) const {
27 const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
28 ArgStringList CmdArgs;
29
30 const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
31 const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
32 // Only support 32 and 64 bit.
33 if (!(IsArch32Bit || IsArch64Bit))
34 llvm_unreachable("Unsupported bit width value.");
35
36 // Force static linking when "-static" is present.
37 if (Args.hasArg(options::OPT_static))
38 CmdArgs.push_back("-bnso");
39
40 // Specify linker output file.
41 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
42 if (Output.isFilename()) {
43 CmdArgs.push_back("-o");
44 CmdArgs.push_back(Output.getFilename());
45 }
46
47 // Set linking mode (i.e., 32/64-bit) and the address of
48 // text and data sections based on arch bit width.
49 if (IsArch32Bit) {
50 CmdArgs.push_back("-b32");
51 CmdArgs.push_back("-bpT:0x10000000");
52 CmdArgs.push_back("-bpD:0x20000000");
53 } else {
54 // Must be 64-bit, otherwise asserted already.
55 CmdArgs.push_back("-b64");
56 CmdArgs.push_back("-bpT:0x100000000");
57 CmdArgs.push_back("-bpD:0x110000000");
58 }
59
60 auto getCrt0Basename = [&Args, IsArch32Bit] {
61 // Enable gprofiling when "-pg" is specified.
62 if (Args.hasArg(options::OPT_pg))
63 return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
64 // Enable profiling when "-p" is specified.
65 else if (Args.hasArg(options::OPT_p))
66 return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
67 else
68 return IsArch32Bit ? "crt0.o" : "crt0_64.o";
69 };
70
71 if (!Args.hasArg(options::OPT_nostdlib)) {
72 CmdArgs.push_back(
73 Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
74 }
75
76 // Specify linker input file(s).
77 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
78
79 // Add directory to library search path.
80 Args.AddAllArgs(CmdArgs, options::OPT_L);
81 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
82
83 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
84 // Support POSIX threads if "-pthreads" or "-pthread" is present.
85 if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
86 CmdArgs.push_back("-lpthreads");
87
88 CmdArgs.push_back("-lc");
89 }
90
91 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
92 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
93}
94
95/// AIX - AIX tool chain which can call ld(1) directly.
96// TODO: Enable direct call to as(1).
97AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
98 : ToolChain(D, Triple, Args) {
99 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
100}
101
102auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }