Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][Driver] Fix safestack -u ordering #98468

Merged
merged 1 commit into from
Jul 11, 2024

Conversation

rorth
Copy link
Collaborator

@rorth rorth commented Jul 11, 2024

When re-enabling safestack testing on Solaris after the unexplained b0260c5, all tests FAILed 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.

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`.
@rorth rorth requested review from MaskRay and vitalybuka July 11, 2024 11:59
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Jul 11, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 11, 2024

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Rainer Orth (rorth)

Changes

When re-enabling safestack testing on Solaris after the unexplained b0260c5, all tests FAILed 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.


Full diff: https://github.com/llvm/llvm-project/pull/98468.diff

3 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+6-4)
  • (modified) clang/test/Driver/ohos.c (+1-1)
  • (modified) clang/test/Driver/sanitizer-ld.c (+1-1)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 80a2b2bf31183..019df16a909f4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1534,6 +1534,12 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
                              RequiredSymbols);
   }
 
+  // -u options must be added before the runtime libs that resolve them.
+  for (auto S : RequiredSymbols) {
+    CmdArgs.push_back("-u");
+    CmdArgs.push_back(Args.MakeArgString(S));
+  }
+
   // Inject libfuzzer dependencies.
   if (SanArgs.needsFuzzer() && SanArgs.linkRuntimes() &&
       !Args.hasArg(options::OPT_shared)) {
@@ -1566,10 +1572,6 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
     addSanitizerRuntime(TC, Args, CmdArgs, RT, false, false);
     AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, RT);
   }
-  for (auto S : RequiredSymbols) {
-    CmdArgs.push_back("-u");
-    CmdArgs.push_back(Args.MakeArgString(S));
-  }
   // If there is a static runtime with no dynamic list, force all the symbols
   // to be dynamic to be sure we export sanitizer interface functions.
   if (AddExportDynamic)
diff --git a/clang/test/Driver/ohos.c b/clang/test/Driver/ohos.c
index b1ce61e7227b6..8de4e6de57f7f 100644
--- a/clang/test/Driver/ohos.c
+++ b/clang/test/Driver/ohos.c
@@ -95,8 +95,8 @@
 // RUN:     | FileCheck %s -check-prefix=CHECK-SAFESTACK
 // CHECK-SAFESTACK: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
 // CHECK-SAFESTACK: "-fsanitize=safe-stack"
-// CHECK-SAFESTACK: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}arm-liteos-ohos{{/|\\\\}}libclang_rt.safestack.a"
 // CHECK-SAFESTACK: "__safestack_init"
+// CHECK-SAFESTACK: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}arm-liteos-ohos{{/|\\\\}}libclang_rt.safestack.a"
 
 // RUN: %clang %s -### --target=arm-liteos \
 // RUN:     -fsanitize=address 2>&1 \
diff --git a/clang/test/Driver/sanitizer-ld.c b/clang/test/Driver/sanitizer-ld.c
index 48e4ea1f7dcb5..c83066a334001 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/Driver/sanitizer-ld.c
@@ -753,8 +753,8 @@
 // CHECK-SAFESTACK-LINUX: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
 // CHECK-SAFESTACK-LINUX-NOT: "-lc"
 // CHECK-SAFESTACK-LINUX-NOT: whole-archive
-// CHECK-SAFESTACK-LINUX: libclang_rt.safestack.a"
 // CHECK-SAFESTACK-LINUX: "-u" "__safestack_init"
+// CHECK-SAFESTACK-LINUX: libclang_rt.safestack.a"
 // CHECK-SAFESTACK-LINUX: "-lpthread"
 // CHECK-SAFESTACK-LINUX: "-ldl"
 // CHECK-SAFESTACK-LINUX: "-lresolv"

@rorth
Copy link
Collaborator Author

rorth commented Jul 11, 2024

This patch was already approved in [safestack] Various Solaris fixes #98001 , therefore I'll commit it shortly.

@rorth rorth merged commit 0248b59 into llvm:main Jul 11, 2024
7 of 9 checks passed
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants