Skip to content

Commit bb6a27f

Browse files
steven-wan-yudaltenty
authored andcommitted
Add AIX toolchain and basic linker functionality
Summary: This patch adds AIX toolchain infrastructure into driver, and enables AIX system linker invocation with some basic functionality support Reviewers: daltenty, hubert.reinterpretcast, jasonliu, Xiangling_L Reviewed By: jasonliu Subscribers: Xiangling_L, jasonliu, ormris, wuzish, nemanjai, mgorny, kbarton, jfb, jsji, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D68340
1 parent 7b3de1e commit bb6a27f

File tree

14 files changed

+346
-0
lines changed

14 files changed

+346
-0
lines changed

clang/lib/Driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_clang_library(clangDriver
3030
ToolChains/Arch/Sparc.cpp
3131
ToolChains/Arch/SystemZ.cpp
3232
ToolChains/Arch/X86.cpp
33+
ToolChains/AIX.cpp
3334
ToolChains/Ananas.cpp
3435
ToolChains/AMDGPU.cpp
3536
ToolChains/AVR.cpp

clang/lib/Driver/Driver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "clang/Driver/Driver.h"
1010
#include "InputInfo.h"
11+
#include "ToolChains/AIX.h"
1112
#include "ToolChains/AMDGPU.h"
1213
#include "ToolChains/AVR.h"
1314
#include "ToolChains/Ananas.h"
@@ -4699,6 +4700,9 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
46994700
auto &TC = ToolChains[Target.str()];
47004701
if (!TC) {
47014702
switch (Target.getOS()) {
4703+
case llvm::Triple::AIX:
4704+
TC = std::make_unique<toolchains::AIX>(*this, Target, Args);
4705+
break;
47024706
case llvm::Triple::Haiku:
47034707
TC = std::make_unique<toolchains::Haiku>(*this, Target, Args);
47044708
break;

clang/lib/Driver/ToolChains/AIX.cpp

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

clang/lib/Driver/ToolChains/AIX.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- AIX.h - 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+
#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AIX_H
10+
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AIX_H
11+
12+
#include "clang/Driver/Tool.h"
13+
#include "clang/Driver/ToolChain.h"
14+
15+
namespace clang {
16+
namespace driver {
17+
namespace tools {
18+
19+
/// aix -- Directly call system default linker.
20+
// TODO: Enable direct call to system default assembler.
21+
namespace aix {
22+
23+
class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
24+
public:
25+
Linker(const ToolChain &TC) : Tool("aix::Linker", "linker", TC) {}
26+
27+
bool hasIntegratedCPP() const override { return false; }
28+
bool isLinkJob() const override { return true; }
29+
30+
void ConstructJob(Compilation &C, const JobAction &JA,
31+
const InputInfo &Output, const InputInfoList &Inputs,
32+
const llvm::opt::ArgList &TCArgs,
33+
const char *LinkingOutput) const override;
34+
};
35+
36+
} // end namespace aix
37+
38+
} // end namespace tools
39+
} // end namespace driver
40+
} // end namespace clang
41+
42+
namespace clang {
43+
namespace driver {
44+
namespace toolchains {
45+
46+
class LLVM_LIBRARY_VISIBILITY AIX : public ToolChain {
47+
public:
48+
AIX(const Driver &D, const llvm::Triple &Triple,
49+
const llvm::opt::ArgList &Args);
50+
51+
bool isPICDefault() const override { return true; }
52+
bool isPIEDefault() const override { return false; }
53+
bool isPICDefaultForced() const override { return true; }
54+
55+
protected:
56+
Tool *buildLinker() const override;
57+
};
58+
59+
} // end namespace toolchains
60+
} // end namespace driver
61+
} // end namespace clang
62+
63+
#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AIX_H

clang/test/Driver/Inputs/aix_ppc_tree/powerpc-ibm-aix7.1.0.0/dummy.a

Whitespace-only changes.

clang/test/Driver/Inputs/aix_ppc_tree/usr/lib/crt0.o

Whitespace-only changes.

clang/test/Driver/Inputs/aix_ppc_tree/usr/lib/crt0_64.o

Whitespace-only changes.

clang/test/Driver/Inputs/aix_ppc_tree/usr/lib/crti.o

Whitespace-only changes.

clang/test/Driver/Inputs/aix_ppc_tree/usr/lib/crti_64.o

Whitespace-only changes.

clang/test/Driver/Inputs/aix_ppc_tree/usr/lib/gcrt0.o

Whitespace-only changes.

0 commit comments

Comments
 (0)