Skip to content

Commit 112e3e7

Browse files
[PS5][Driver] Supply libraries and CRT objects to the linker
Until now, these have been hardcoded as a downstream patches in lld. Add them to the driver so that the private patches can be removed. PS5 only. On PS4, the equivalent hardcoded configuration will remain in the proprietary linker. SIE tracker: TOOLCHAIN-16704
1 parent cff2199 commit 112e3e7

File tree

2 files changed

+113
-12
lines changed

2 files changed

+113
-12
lines changed

clang/lib/Driver/ToolChains/PS4CPU.cpp

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ void tools::PS4cpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
183183
CmdArgs.push_back(
184184
Args.MakeArgString(Twine("-lto-debug-options=") + LTOArgs));
185185

186+
// Sanitizer runtimes must be supplied before all other objects and libs.
186187
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
187188
TC.addSanitizerArgs(Args, CmdArgs, "-l", "");
188189

@@ -358,27 +359,70 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
358359
if (StringRef Jobs = getLTOParallelism(Args, D); !Jobs.empty())
359360
AddLTOFlag(Twine("jobs=") + Jobs);
360361

361-
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
362-
TC.addSanitizerArgs(Args, CmdArgs, "-l", "");
363-
364362
TC.AddFilePathLibArgs(Args, CmdArgs);
365363
Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
366364
options::OPT_s, options::OPT_t});
367365

368366
if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
369367
CmdArgs.push_back("--no-demangle");
370368

371-
AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
369+
// Sanitizer runtimes must be supplied before all other objects and libs.
370+
if (!Args.hasArg(options::OPT_nodefaultlibs, options::OPT_nostdlib))
371+
TC.addSanitizerArgs(Args, CmdArgs, "-l", "");
372372

373-
if (Args.hasArg(options::OPT_pthread)) {
374-
CmdArgs.push_back("-lpthread");
373+
const bool AddStartFiles =
374+
!Relocatable &&
375+
!Args.hasArg(options::OPT_nostartfiles, options::OPT_nostdlib);
376+
377+
auto AddCRTObject = [&](const char *Name) {
378+
CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(Name)));
379+
};
380+
381+
if (AddStartFiles) {
382+
if (!Shared)
383+
AddCRTObject("crt1.o");
384+
AddCRTObject("crti.o");
385+
AddCRTObject(Shared ? "crtbeginS.o"
386+
: Static ? "crtbeginT.o"
387+
: "crtbegin.o");
375388
}
376389

377-
if (UseJMC) {
378-
CmdArgs.push_back("--push-state");
379-
CmdArgs.push_back("--whole-archive");
380-
CmdArgs.push_back("-lSceJmc_nosubmission");
381-
CmdArgs.push_back("--pop-state");
390+
AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
391+
392+
if (!Relocatable &&
393+
!Args.hasArg(options::OPT_nodefaultlibs, options::OPT_nostdlib)) {
394+
395+
if (UseJMC) {
396+
CmdArgs.push_back("--push-state");
397+
CmdArgs.push_back("--whole-archive");
398+
CmdArgs.push_back("-lSceJmc_nosubmission");
399+
CmdArgs.push_back("--pop-state");
400+
}
401+
402+
if (Args.hasArg(options::OPT_pthread))
403+
CmdArgs.push_back("-lpthread");
404+
405+
if (Static) {
406+
if (!Args.hasArg(options::OPT_nostdlibxx))
407+
CmdArgs.push_back("-lstdc++");
408+
if (!Args.hasArg(options::OPT_nolibc)) {
409+
CmdArgs.push_back("-lm");
410+
CmdArgs.push_back("-lc");
411+
}
412+
413+
CmdArgs.push_back("-lcompiler_rt");
414+
CmdArgs.push_back("-lkernel");
415+
} else {
416+
// The C and C++ libraries are combined.
417+
if (!Args.hasArg(options::OPT_nolibc, options::OPT_nostdlibxx))
418+
CmdArgs.push_back("-lc_stub_weak");
419+
420+
CmdArgs.push_back("-lkernel_stub_weak");
421+
}
422+
}
423+
if (AddStartFiles) {
424+
AddCRTObject(Shared ? "crtendS.o" : "crtend.o");
425+
AddCRTObject("crtn.o");
382426
}
383427

384428
if (Args.hasArg(options::OPT_fuse_ld_EQ)) {

clang/test/Driver/ps5-linker.c

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,60 @@
9797
// Check the default library name.
9898
// CHECK-JMC: "--push-state" "--whole-archive" "-lSceJmc_nosubmission" "--pop-state"
9999

100+
// Test that CRT objects and libraries are supplied to the linker and can be
101+
// omitted with -noxxx options. These switches have some interaction with
102+
// sanitizer RT libraries. That's checked in fsanitize.c
103+
104+
// RUN: %clang --target=x86_64-sie-ps5 %s -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-MAIN-CRT,CHECK-DYNAMIC-LIBC,CHECK-DYNAMIC-CORE-LIBS %s
105+
// RUN: %clang --target=x86_64-sie-ps5 %s -shared -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-SHARED-CRT,CHECK-DYNAMIC-LIBC,CHECK-DYNAMIC-CORE-LIBS %s
106+
// RUN: %clang --target=x86_64-sie-ps5 %s -static -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-STATIC-CRT,CHECK-STATIC-LIBCPP,CHECK-STATIC-LIBC,CHECK-STATIC-CORE-LIBS %s
107+
// RUN: %clang --target=x86_64-sie-ps5 %s -r -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-NO-CRT,CHECK-NO-LIBS %s
108+
109+
// RUN: %clang --target=x86_64-sie-ps5 %s -pthread -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-PTHREAD %s
110+
111+
// RUN: %clang --target=x86_64-sie-ps5 %s -nostartfiles -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-NO-CRT,CHECK-DYNAMIC-LIBC,CHECK-DYNAMIC-CORE-LIBS %s
112+
// RUN: %clang --target=x86_64-sie-ps5 %s -nostartfiles -shared -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-NO-CRT,CHECK-DYNAMIC-LIBC,CHECK-DYNAMIC-CORE-LIBS %s
113+
// RUN: %clang --target=x86_64-sie-ps5 %s -nostartfiles -static -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-NO-CRT,CHECK-STATIC-LIBCPP,CHECK-STATIC-LIBC,CHECK-STATIC-CORE-LIBS %s
114+
115+
// RUN: %clang --target=x86_64-sie-ps5 %s -nodefaultlibs -pthread -fjmc -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-MAIN-CRT,CHECK-NO-LIBS %s
116+
// RUN: %clang --target=x86_64-sie-ps5 %s -nodefaultlibs -pthread -fjmc -shared -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-SHARED-CRT,CHECK-NO-LIBS %s
117+
// RUN: %clang --target=x86_64-sie-ps5 %s -nodefaultlibs -pthread -fjmc -static -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-STATIC-CRT,CHECK-NO-LIBS %s
118+
119+
// RUN: %clang --target=x86_64-sie-ps5 %s -nostdlib -pthread -fjmc -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-NO-CRT,CHECK-NO-LIBS %s
120+
// RUN: %clang --target=x86_64-sie-ps5 %s -nostdlib -pthread -fjmc -shared -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-NO-CRT,CHECK-NO-LIBS %s
121+
// RUN: %clang --target=x86_64-sie-ps5 %s -nostdlib -pthread -fjmc -static -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-NO-CRT,CHECK-NO-LIBS %s
122+
123+
// RUN: %clang --target=x86_64-sie-ps5 %s -nolibc -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-MAIN-CRT,CHECK-NO-LIBC,CHECK-DYNAMIC-CORE-LIBS %s
124+
// RUN: %clang --target=x86_64-sie-ps5 %s -nolibc -shared -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-SHARED-CRT,CHECK-NO-LIBC,CHECK-DYNAMIC-CORE-LIBS %s
125+
// RUN: %clang --target=x86_64-sie-ps5 %s -nolibc -static -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-STATIC-CRT,CHECK-STATIC-LIBCPP,CHECK-NO-LIBC,CHECK-STATIC-CORE-LIBS %s
126+
127+
// RUN: %clang --target=x86_64-sie-ps5 %s -nostdlib++ -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-MAIN-CRT,CHECK-NO-LIBCPP,CHECK-DYNAMIC-CORE-LIBS %s
128+
// RUN: %clang --target=x86_64-sie-ps5 %s -nostdlib++ -shared -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-SHARED-CRT,CHECK-NO-LIBCPP,CHECK-DYNAMIC-CORE-LIBS %s
129+
// RUN: %clang --target=x86_64-sie-ps5 %s -nostdlib++ -static -### 2>&1 | FileCheck --check-prefixes=CHECK-LD,CHECK-STATIC-CRT,CHECK-NO-LIBCPP,CHECK-STATIC-LIBC,CHECK-STATIC-CORE-LIBS %s
130+
131+
// CHECK-LD: {{ld(\.exe)?}}"
132+
// CHECK-MAIN-CRT-SAME: "crt1.o" "crti.o" "crtbegin.o"
133+
// CHECK-SHARED-CRT-SAME: "crti.o" "crtbeginS.o"
134+
// CHECK-STATIC-CRT-SAMW: "crt1.o" "crti.o" "crtbeginT.o"
135+
136+
// CHECK-NO-LIBC-NOT: "-lc{{(_stub_weak)?}}"
137+
// CHECK-NO-LIBCPP-NOT: "-l{{c_stub_weak|stdc\+\+}}"
138+
139+
// CHECK-DYNAMIC-LIBC-SAME: "-lc_stub_weak"
140+
// CHECK-DYNAMIC-CORE-LIBS-SAME: "-lkernel_stub_weak"
141+
// CHECK-STATIC-LIBCPP-SAME: "-lstdc++"
142+
// CHECK-STATIC-LIBC-SAME: "-lm" "-lc"
143+
// CHECK-STATIC-CORE-LIBS-SAME: "-lcompiler_rt" "-lkernel"
144+
145+
// CHECK-PTHREAD-SAME: "-lpthread"
146+
147+
// CHECK-MAIN-CRT-SAME: "crtend.o" "crtn.o"
148+
// CHECK-SHARED-CRT-SAME: "crtendS.o" "crtn.o"
149+
// CHECK-STATIC-CRT-SAME: "crtend.o" "crtn.o"
150+
151+
// CHECK-NO-CRT-NOT: "crt{{[^"]*}}.o"
152+
// CHECK-NO-LIBS-NOT: "-l{{[^"]*}}"
153+
100154
// Test the driver's control over the -fcrash-diagnostics-dir behavior with linker flags.
101155

102156
// RUN: %clang --target=x86_64-sie-ps5 -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG %s
@@ -122,7 +176,8 @@
122176
// CHECK-LDOT-SAME: "-L."
123177

124178
// Test that <sdk-root>/target/lib is added to library search paths, if it
125-
// exists and no --sysroot is specified.
179+
// exists and no --sysroot is specified. Also confirm that CRT objects are
180+
// found there.
126181

127182
// RUN: rm -rf %t.dir && mkdir %t.dir
128183
// RUN: env SCE_PROSPERO_SDK_DIR=%t.dir %clang --target=x64_64-sie-ps5 %s -### 2>&1 | FileCheck --check-prefixes=CHECK-NO-TARGETLIB %s
@@ -132,7 +187,9 @@
132187
// CHECK-NO-TARGETLIB-NOT: "-L{{.*[/\\]}}target/lib"
133188

134189
// RUN: mkdir -p %t.dir/target/lib
190+
// RUN: touch %t.dir/target/lib/crti.o
135191
// RUN: env SCE_PROSPERO_SDK_DIR=%t.dir %clang --target=x64_64-sie-ps5 %s -### 2>&1 | FileCheck --check-prefixes=CHECK-TARGETLIB %s
136192

137193
// CHECK-TARGETLIB: {{ld(\.exe)?}}"
138194
// CHECK-TARGETLIB-SAME: "-L{{.*[/\\]}}target/lib"
195+
// CHECK-TARGETLIB-SAME: "{{.*[/\\]}}target{{/|\\\\}}lib{{/|\\\\}}crti.o"

0 commit comments

Comments
 (0)