llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Angel J (iamanaws)

<details>
<summary>Changes</summary>

## Summary

Pass `-pie` explicitly when the OpenBSD driver selects the static PIE
startup path.

OpenBSD uses `rcrt0.o` for static PIE executables. This startup object
references the linker-defined `_DYNAMIC` symbol.

OpenBSD's system linker defaults to PIE, which previously masked the
missing driver flag. An LLD cross-linker built on a non-OpenBSD host
does not share that default. Consequently,
`clang --target=...-openbsd -static` selects `rcrt0.o`, but LLD does not
create `_DYNAMIC`, causing the link to fail.

Passing `-pie` explicitly makes the target behavior independent of the
linker's build host. Shared, relocatable, profiling, and explicit
`-nopie` links remain unchanged.

## Testing

- `clang-format --dry-run --Werror clang/lib/Driver/ToolChains/OpenBSD.cpp`
- `git diff --check`
- built `OpenBSD.cpp.o` in a minimal Clang/LLVM build
- verified that the new `FileCheck` assertion detects the unpatched driver


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


2 Files Affected:

- (modified) clang/lib/Driver/ToolChains/OpenBSD.cpp (+6-2) 
- (modified) clang/test/Driver/openbsd.c (+1) 


``````````diff
diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp 
b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index 14680dc4b0e5b..fa36726534bed 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -119,6 +119,8 @@ void openbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   const bool Pie = Args.hasArg(options::OPT_pie);
   const bool Nopie = Args.hasArg(options::OPT_no_pie, options::OPT_nopie);
   const bool Relocatable = Args.hasArg(options::OPT_r);
+  const bool StaticPie =
+      Static && !Shared && !Profiling && !Nopie && !Relocatable;
   ArgStringList CmdArgs;
 
   // Silence warning for "clang -g foo.o -o foo"
@@ -156,7 +158,9 @@ void openbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
     }
   }
 
-  if (Pie)
+  // OpenBSD's system linker defaults to PIE, but cross-linkers may not.
+  // Explicitly pass -pie so that rcrt0.o's reference to _DYNAMIC is resolved.
+  if (Pie || StaticPie)
     CmdArgs.push_back("-pie");
   if (Nopie || Profiling)
     CmdArgs.push_back("-nopie");
@@ -180,7 +184,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
     if (!Shared) {
       if (Profiling)
         crt0 = "gcrt0.o";
-      else if (Static && !Nopie)
+      else if (StaticPie)
         crt0 = "rcrt0.o";
       else
         crt0 = "crt0.o";
diff --git a/clang/test/Driver/openbsd.c b/clang/test/Driver/openbsd.c
index 1f12cfca9488b..e5e7f528e8fe7 100644
--- a/clang/test/Driver/openbsd.c
+++ b/clang/test/Driver/openbsd.c
@@ -104,6 +104,7 @@
 // CHECK-PIE: "{{.*}}crt0.o"
 // CHECK-PIE-NOT: "-nopie"
 // CHECK-PIE-FLAG: "-pie"
+// CHECK-STATIC-PIE: "-pie"
 // CHECK-STATIC-PIE: "{{.*}}rcrt0.o"
 // CHECK-STATIC-PIE-NOT: "-nopie"
 // CHECK-NOPIE: "-nopie" "{{.*}}crt0.o"

``````````

</details>


https://github.com/llvm/llvm-project/pull/216907
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to