Skip to content

Commit 0248b59

Browse files
authored
[clang][Driver] Fix safestack -u ordering (#98468)
When re-enabling safestack testing on Solaris after the unexplained b0260c5, all tests `FAIL`ed to link: ``` Undefined first referenced symbol in file __safestack_unsafe_stack_ptr buffer-copy-vla.o __safestack_init (command line) ld: fatal: symbol referencing errors ``` The problem is that `-u __safestack_init` was passed to the linker after the corresponding version of `libclang_rt.safestack-*.a`. Since the Solaris linker (like Unix linkers for decades) respects the command line argument order (unlike e.g. GNU ld which uses GNU getopt), this cannot work. Fixed by moving the `-u` arg further to the front. Two affected testcases were fixed accordingly. Tested on `amd64-pc-solaris2.11`, `sparcv9-sun-solaris2.11`, `x86_64-pc-linux-gnu`, and `sparc64-unknown-linux-gnu`.
1 parent e1bd337 commit 0248b59

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,12 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
15341534
RequiredSymbols);
15351535
}
15361536

1537+
// -u options must be added before the runtime libs that resolve them.
1538+
for (auto S : RequiredSymbols) {
1539+
CmdArgs.push_back("-u");
1540+
CmdArgs.push_back(Args.MakeArgString(S));
1541+
}
1542+
15371543
// Inject libfuzzer dependencies.
15381544
if (SanArgs.needsFuzzer() && SanArgs.linkRuntimes() &&
15391545
!Args.hasArg(options::OPT_shared)) {
@@ -1566,10 +1572,6 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
15661572
addSanitizerRuntime(TC, Args, CmdArgs, RT, false, false);
15671573
AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, RT);
15681574
}
1569-
for (auto S : RequiredSymbols) {
1570-
CmdArgs.push_back("-u");
1571-
CmdArgs.push_back(Args.MakeArgString(S));
1572-
}
15731575
// If there is a static runtime with no dynamic list, force all the symbols
15741576
// to be dynamic to be sure we export sanitizer interface functions.
15751577
if (AddExportDynamic)

clang/test/Driver/ohos.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@
9595
// RUN: | FileCheck %s -check-prefix=CHECK-SAFESTACK
9696
// CHECK-SAFESTACK: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
9797
// CHECK-SAFESTACK: "-fsanitize=safe-stack"
98-
// CHECK-SAFESTACK: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}arm-liteos-ohos{{/|\\\\}}libclang_rt.safestack.a"
9998
// CHECK-SAFESTACK: "__safestack_init"
99+
// CHECK-SAFESTACK: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}arm-liteos-ohos{{/|\\\\}}libclang_rt.safestack.a"
100100

101101
// RUN: %clang %s -### --target=arm-liteos \
102102
// RUN: -fsanitize=address 2>&1 \

clang/test/Driver/sanitizer-ld.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,8 @@
753753
// CHECK-SAFESTACK-LINUX: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
754754
// CHECK-SAFESTACK-LINUX-NOT: "-lc"
755755
// CHECK-SAFESTACK-LINUX-NOT: whole-archive
756-
// CHECK-SAFESTACK-LINUX: libclang_rt.safestack.a"
757756
// CHECK-SAFESTACK-LINUX: "-u" "__safestack_init"
757+
// CHECK-SAFESTACK-LINUX: libclang_rt.safestack.a"
758758
// CHECK-SAFESTACK-LINUX: "-lpthread"
759759
// CHECK-SAFESTACK-LINUX: "-ldl"
760760
// CHECK-SAFESTACK-LINUX: "-lresolv"

0 commit comments

Comments
 (0)