Skip to content

Commit 9685681

Browse files
[PS5][Driver] Supply libraries and CRT objects to the linker (#115497)
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 8781a43 commit 9685681

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

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

363-
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
364-
TC.addSanitizerArgs(Args, CmdArgs, "-l", "");
365-
366364
TC.AddFilePathLibArgs(Args, CmdArgs);
367365
Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
368366
options::OPT_s, options::OPT_t});
369367

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

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

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

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

386430
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
@@ -102,6 +102,60 @@
102102
// Check the default library name.
103103
// CHECK-JMC: "--push-state" "--whole-archive" "-lSceJmc_nosubmission" "--pop-state"
104104

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

107161
// RUN: %clang --target=x86_64-sie-ps5 -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG %s
@@ -127,7 +181,8 @@
127181
// CHECK-LDOT-SAME: "-L."
128182

129183
// Test that <sdk-root>/target/lib is added to library search paths, if it
130-
// exists and no --sysroot is specified.
184+
// exists and no --sysroot is specified. Also confirm that CRT objects are
185+
// found there.
131186

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

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

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

0 commit comments

Comments
 (0)