Skip to content

Add -z origin to bsd linkopts - #855

Closed
keith wants to merge 1 commit into
bazelbuild:mainfrom
keith:ks/add-z-origin-to-bsd-linkopts
Closed

keith wants to merge 1 commit into
bazelbuild:mainfrom
keith:ks/add-z-origin-to-bsd-linkopts

Conversation

@keith

@keith keith commented Aug 28, 2026

Copy link
Copy Markdown
Member

This is enforced on bsd in order to use $ORIGIN in library paths. Linux
does nothing with this so we don't set it there.

Fixes #854

This is enforced on bsd in order to use $ORIGIN in library paths. Linux
does nothing with this so we don't set it there.

Fixes bazelbuild#854
@keith

keith commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

i tested on openbsd to verify

@copybara-service copybara-service Bot closed this in b1771c1 Sep 2, 2026
zakinko added a commit to zakinko/rules_cc that referenced this pull request Sep 16, 2026
FreeBSD and OpenBSD have been routed to a static toolchain since 2016, one
that writes in the compiler path, the builtin include directories and the
link libraries, while every other Unix gets those probed. NetBSD and
DragonFly never got a name at all and fell through to the autoconfigured
path by accident, which works apart from the toolchain being called "k8".

Run the autoconfiguration on all four. Four things it did not know how to
do there:

- Find the C driver, since the BSDs install it as cc and a gcc-first search
  would prefer one from ports or pkgsrc over the compiler the system was
  built with.
- Name the BSD libcs for //cc/libc, which stopped at NetBSD.
- Set DF_ORIGIN. Bazel puts $ORIGIN in the RPATH it generates and OpenBSD's
  ld.so only expands it when that flag is set; glibc and musl expand it
  either way, so the need is invisible on Linux. The static toolchain
  carried -z origin (bazelbuild#855, for bazelbuild#854) and the autoconfigured one did not.
- Leave the C++ standard libraries outside --as-needed on OpenBSD. The
  driver expands -lstdc++ to -lc++ -lc++abi -lpthread there, and libpthread
  is only reached through the other two, so wrapping the expansion drops it
  and the binary dies at startup on undefined pthread_* symbols (bazelbuild#857).

I had claimed the link libraries needed no change, on the strength of
cc -### showing the driver substituting the platform's C++ runtime for
-lstdc++. That was the wrong measurement: cc -### prints what the driver
passes to the linker, not what survives the link. @c2qd caught it with an
objdump of the NEEDED entries and a binary that failed to start.

The static toolchain stays behind BAZEL_USE_LEGACY_BSD_TOOLCHAIN=1, the
way the legacy macOS toolchain does. It is offered only for FreeBSD and
OpenBSD, which are the two BUILD.static.bsd describes.
zakinko added a commit to zakinko/rules_cc that referenced this pull request Sep 17, 2026
FreeBSD and OpenBSD have been routed to a static toolchain since 2016, one
that writes in the compiler path, the builtin include directories and the
link libraries, while every other Unix gets those probed. NetBSD and
DragonFly never got a name at all and fell through to the autoconfigured
path by accident, which works apart from the toolchain being called "k8".

Run the autoconfiguration on all four. Four things it did not know how to
do there:

- Find the C driver, since the BSDs install it as cc and a gcc-first search
  would prefer one from ports or pkgsrc over the compiler the system was
  built with.
- Name the BSD libcs for //cc/libc, which stopped at NetBSD.
- Set DF_ORIGIN. Bazel puts $ORIGIN in the RPATH it generates and OpenBSD's
  ld.so only expands it when that flag is set; glibc and musl expand it
  either way, so the need is invisible on Linux. The static toolchain
  carried -z origin (bazelbuild#855, for bazelbuild#854) and the autoconfigured one did not.
- Leave the C++ standard libraries outside --as-needed on OpenBSD. The
  driver expands -lstdc++ to -lc++ -lc++abi -lpthread there, and libpthread
  is only reached through the other two, so wrapping the expansion drops it
  and the binary dies at startup on undefined pthread_* symbols (bazelbuild#857).

I had claimed the link libraries needed no change, on the strength of
cc -### showing the driver substituting the platform's C++ runtime for
-lstdc++. That was the wrong measurement: cc -### prints what the driver
passes to the linker, not what survives the link. @c2qd caught it with an
objdump of the NEEDED entries and a binary that failed to start.

The static toolchain stays behind BAZEL_USE_LEGACY_BSD_TOOLCHAIN=1, the
way the legacy macOS toolchain does. It is offered only for FreeBSD and
OpenBSD, which are the two BUILD.static.bsd describes.
zakinko added a commit to zakinko/rules_cc that referenced this pull request Sep 23, 2026
FreeBSD and OpenBSD have been routed to a static toolchain since 2016, one
that writes in the compiler path, the builtin include directories and the
link libraries, while every other Unix gets those probed. NetBSD and
DragonFly never got a name at all and fell through to the autoconfigured
path by accident, which works apart from the toolchain being called "k8".

Run the autoconfiguration on all four. Four things it did not know how to
do there:

- Find the C driver, since the BSDs install it as cc and a gcc-first search
  would prefer one from ports or pkgsrc over the compiler the system was
  built with.
- Name the BSD libcs for //cc/libc, which stopped at NetBSD.
- Set DF_ORIGIN. Bazel puts $ORIGIN in the RPATH it generates and OpenBSD's
  ld.so only expands it when that flag is set; glibc and musl expand it
  either way, so the need is invisible on Linux. The static toolchain
  carried -z origin (bazelbuild#855, for bazelbuild#854) and the autoconfigured one did not.
- Leave the C++ standard libraries outside --as-needed on OpenBSD. The
  driver expands -lstdc++ to -lc++ -lc++abi -lpthread there, and libpthread
  is only reached through the other two, so wrapping the expansion drops it
  and the binary dies at startup on undefined pthread_* symbols (bazelbuild#857).

I had claimed the link libraries needed no change, on the strength of
cc -### showing the driver substituting the platform's C++ runtime for
-lstdc++. That was the wrong measurement: cc -### prints what the driver
passes to the linker, not what survives the link. @c2qd caught it with an
objdump of the NEEDED entries and a binary that failed to start.

The static toolchain stays behind BAZEL_USE_LEGACY_BSD_TOOLCHAIN=1, the
way the legacy macOS toolchain does. It is offered only for FreeBSD and
OpenBSD, which are the two BUILD.static.bsd describes.
3405691582 added a commit to 3405691582/android-cuttlefish that referenced this pull request Oct 7, 2026
On OpenBSD the BSD toolchain in rules_cc drives the link with clang
rather than clang++ and only passes -lc++, so libc++abi, libpthread and
libm are missing from every C++ link. rules_cc fixed this in 0.2.24
(bazelbuild/rules_cc#859: -lc++abi and -lpthread when cpu is openbsd,
-lm on both BSDs) and also links with -z origin there
(bazelbuild/rules_cc#855), which OpenBSD's ld.so needs before it will
expand $ORIGIN in a run path. 0.2.25 is the first version with both on
the registry and the one Bazel pins at head. Bazel 8.5.1 and 9.2.0 both
generate @local_config_cc from rules_cc, so the toolchain config
changes on Linux as well.

While we are here, move the bazel_skylib and bazel_features pins up to
what the graph already resolves to, 1.9.0 (wanted by apple_support,
which the new rules_cc brings in) and 1.50.0 (wanted by rules_cc
itself); that silences the two --check_direct_dependencies warnings
without changing the resolved module graph.
3405691582 added a commit to 3405691582/android-cuttlefish that referenced this pull request Oct 8, 2026
On OpenBSD the BSD toolchain in rules_cc drives the link with clang
rather than clang++ and only passes -lc++, so libc++abi, libpthread and
libm are missing from every C++ link. rules_cc fixed this in 0.2.24
(bazelbuild/rules_cc#859: -lc++abi and -lpthread when cpu is openbsd,
-lm on both BSDs) and also links with -z origin there
(bazelbuild/rules_cc#855), which OpenBSD's ld.so needs before it will
expand $ORIGIN in a run path. 0.2.25 is the first version with both on
the registry and the one Bazel pins at head. Bazel 8.5.1 and 9.2.0 both
generate @local_config_cc from rules_cc, so the toolchain config
changes on Linux as well.

While we are here, move the bazel_skylib and bazel_features pins up to
what the graph already resolves to, 1.9.0 (wanted by apple_support,
which the new rules_cc brings in) and 1.50.0 (wanted by rules_cc
itself); that silences the two --check_direct_dependencies warnings
without changing the resolved module graph.
3405691582 added a commit to 3405691582/android-cuttlefish that referenced this pull request Oct 10, 2026
On OpenBSD the BSD toolchain in rules_cc drives the link with clang
rather than clang++ and only passes -lc++, so libc++abi, libpthread and
libm are missing from every C++ link. rules_cc fixed this in 0.2.24
(bazelbuild/rules_cc#859: -lc++abi and -lpthread when cpu is openbsd,
-lm on both BSDs) and also links with -z origin there
(bazelbuild/rules_cc#855), which OpenBSD's ld.so needs before it will
expand $ORIGIN in a run path. 0.2.25 is the first version with both on
the registry and the one Bazel pins at head. Bazel 8.5.1 and 9.2.0 both
generate @local_config_cc from rules_cc, so the toolchain config
changes on Linux as well.

While we are here, move the bazel_skylib and bazel_features pins up to
what the graph already resolves to, 1.9.0 (wanted by apple_support,
which the new rules_cc brings in) and 1.50.0 (wanted by rules_cc
itself); that silences the two --check_direct_dependencies warnings
without changing the resolved module graph.
zakinko added a commit to zakinko/rules_cc that referenced this pull request Nov 14, 2026
FreeBSD and OpenBSD have been routed to a static toolchain since 2016, one
that writes in the compiler path, the builtin include directories and the
link libraries, while every other Unix gets those probed. NetBSD and
DragonFly never got a name at all and fell through to the autoconfigured
path by accident, which works apart from the toolchain being called "k8".

Run the autoconfiguration on all four. Four things it did not know how to
do there:

- Find the C driver, since the BSDs install it as cc and a gcc-first search
  would prefer one from ports or pkgsrc over the compiler the system was
  built with.
- Name the BSD libcs for //cc/libc, which stopped at NetBSD.
- Set DF_ORIGIN. Bazel puts $ORIGIN in the RPATH it generates and OpenBSD's
  ld.so only expands it when that flag is set; glibc and musl expand it
  either way, so the need is invisible on Linux. The static toolchain
  carried -z origin (bazelbuild#855, for bazelbuild#854) and the autoconfigured one did not.
- Leave the C++ standard libraries outside --as-needed on OpenBSD. The
  driver expands -lstdc++ to -lc++ -lc++abi -lpthread there, and libpthread
  is only reached through the other two, so wrapping the expansion drops it
  and the binary dies at startup on undefined pthread_* symbols (bazelbuild#857).

I had claimed the link libraries needed no change, on the strength of
cc -### showing the driver substituting the platform's C++ runtime for
-lstdc++. That was the wrong measurement: cc -### prints what the driver
passes to the linker, not what survives the link. @c2qd caught it with an
objdump of the NEEDED entries and a binary that failed to start.

The static toolchain stays behind BAZEL_USE_LEGACY_BSD_TOOLCHAIN=1, the
way the legacy macOS toolchain does. It is offered only for FreeBSD and
OpenBSD, which are the two BUILD.static.bsd describes.
@keith
keith deleted the ks/add-z-origin-to-bsd-linkopts branch November 16, 2026 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When generating an RPATH/RUNPATH containing $ORIGIN, the DF_ORIGIN flag also needs to be set

2 participants