Skip to content

[Driver] Corrections for linker flags passed with relocatable linking on OpenBSD #67254

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

Merged
merged 1 commit into from
Oct 20, 2023

Conversation

brad0
Copy link
Contributor

@brad0 brad0 commented Sep 24, 2023

The entry point symbol handling matches our GCC link spec.. %{!shared:%{!nostdlib:%{!r:%{!e*:-e __start}}}}

Remove usage of -Bdynamic as it is the default for the linker anyway.

Came up in discussion here #65644

@brad0 brad0 requested a review from MaskRay September 24, 2023 04:26
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Sep 24, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2023

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Changes

The entry point symbol handling matches our GCC link spec.. %{!shared:%{!nostdlib:%{!r:%{!e*:-e __start}}}}

Came up in discussion here #65644


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

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/OpenBSD.cpp (+5-3)
  • (modified) clang/test/Driver/openbsd.c (+3-1)
diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index 91fe3837b813333..8d88379ef4c10e7 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -121,6 +121,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   bool Profiling = Args.hasArg(options::OPT_pg);
   bool Pie = Args.hasArg(options::OPT_pie);
   bool Nopie = Args.hasArg(options::OPT_nopie);
+  bool Relocatable = Args.hasArg(options::OPT_r);
 
   // Silence warning for "clang -g foo.o -o foo"
   Args.ClaimAllArgs(options::OPT_g_Group);
@@ -138,7 +139,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   else if (Arch == llvm::Triple::mips64el)
     CmdArgs.push_back("-EL");
 
-  if (!Args.hasArg(options::OPT_nostdlib) && !Shared) {
+  if (!Args.hasArg(options::OPT_nostdlib) && !Shared && !Relocatable) {
     CmdArgs.push_back("-e");
     CmdArgs.push_back("__start");
   }
@@ -149,10 +150,11 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   } else {
     if (Args.hasArg(options::OPT_rdynamic))
       CmdArgs.push_back("-export-dynamic");
-    CmdArgs.push_back("-Bdynamic");
+    if (!Relocatable)
+      CmdArgs.push_back("-Bdynamic");
     if (Shared) {
       CmdArgs.push_back("-shared");
-    } else if (!Args.hasArg(options::OPT_r)) {
+    } else if (!Relocatable) {
       CmdArgs.push_back("-dynamic-linker");
       CmdArgs.push_back("/usr/libexec/ld.so");
     }
diff --git a/clang/test/Driver/openbsd.c b/clang/test/Driver/openbsd.c
index 05d290a309c40c0..a8db20200cd473d 100644
--- a/clang/test/Driver/openbsd.c
+++ b/clang/test/Driver/openbsd.c
@@ -36,10 +36,12 @@
 // RUN:   | FileCheck --check-prefix=CHECK-MIPS64-LD %s
 // RUN: %clang --target=mips64el-unknown-openbsd -### %s 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-MIPS64EL-LD %s
-// CHECK-LD-R:     "-r"
+// CHECK-LD-R-NOT: "-e" "__start"
+// CHECK-LD-R-NOT: "-Bdynamic"
 // CHECK-LD-R-NOT: "-dynamic-linker"
 // CHECK-LD-R-NOT: "-l
 // CHECK-LD-R-NOT: crt{{[^./\\]+}}.o
+// CHECK-LD-R:     "-r"
 // CHECK-LD-S: "-cc1" "-triple" "i686-pc-openbsd"
 // CHECK-LD-S: ld{{.*}}" "-e" "__start" "--eh-frame-hdr" "-Bdynamic" "-dynamic-linker" "{{.*}}ld.so" "-o" "a.out" "{{.*}}crt0.o" "{{.*}}crtbegin.o" "-L{{.*}}" "-s" "{{.*}}.o" "-lcompiler_rt" "-lc" "-lcompiler_rt" "{{.*}}crtend.o"
 // CHECK-LD-T: "-cc1" "-triple" "i686-pc-openbsd"

@tambry
Copy link
Contributor

tambry commented Sep 24, 2023

reloctable→relocatable

Just a fly-by, but IMO the commit message ought to describe the change – "some changes" doesn't give much insight.

@brad0 brad0 force-pushed the clang_driver_openbsd_relocatable branch from 688d4e9 to fdcdea6 Compare September 24, 2023 17:54
@brad0 brad0 changed the title [Driver] Some adjustments for reloctable linking on OpenBSD [Driver] Some adjustments for relocatable linking on OpenBSD Sep 24, 2023
@brad0 brad0 force-pushed the clang_driver_openbsd_relocatable branch from fdcdea6 to c638c2f Compare September 25, 2023 03:50
@brad0 brad0 changed the title [Driver] Some adjustments for relocatable linking on OpenBSD [Driver] Corrections for linker flags passed with relocatable linking on OpenBSD Sep 25, 2023
@brad0
Copy link
Contributor Author

brad0 commented Sep 29, 2023

@MaskRay Can you comment on the -Bdynamic part? Is that appropriate for relocatable linking?

@brad0
Copy link
Contributor Author

brad0 commented Oct 12, 2023

@MaskRay ping.

@MaskRay
Copy link
Member

MaskRay commented Oct 20, 2023

-Bdynamic is the linker default, so you can remove it. Gnu.cpp uses -Bdynamic like ) to close -Bstatic: -Bstatic ... -Bdynamic`

… on OpenBSD

The entry point symbol handling matches our GCC link spec..
%{!shared:%{!nostdlib:%{!r:%{!e*:-e __start}}}}

Remove usage of -Bdynamic as it is the default for the linker anyway.
@brad0 brad0 force-pushed the clang_driver_openbsd_relocatable branch from c638c2f to d16c447 Compare October 20, 2023 08:18
@brad0 brad0 merged commit 1d4601a into llvm:main Oct 20, 2023
@brad0 brad0 deleted the clang_driver_openbsd_relocatable branch October 20, 2023 19:54
Guzhu-AMD pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Oct 26, 2023
Local branch amd-gfx 13ef517 Merged main:71bdd2c2380d into amd-gfx:319c66a13c02
Remote branch main 1d4601a [Driver] Corrections for linker flags passed with relocatable linking on OpenBSD (llvm#67254)
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.

4 participants