feat: opt a target triple's stdlib out of the bundled libc - #245
Open
lsjostro wants to merge 1 commit into
Open
Conversation
rustc puts the prebuilt stdlib's `self-contained` directory on the linker search path ahead of the CC toolchain's `-L`, so `-lc` always resolves to the libc rustc ships. A workspace that builds its own sysroot -- a distro, an OS image, an embedded target -- silently links the wrong libc, and any patch it carries in that libc never reaches a Rust binary. `-C link-self-contained=no` does not help: it leaves the search path in place (verified on 1.95.0, byte-identical output). The files have to leave the sandbox instead, so the stdlib repo grows a second filegroup without the archives, and `declare_rustc_toolchains` grows a triple list that selects it. The CRT objects stay in the filtered filegroup. Remove the directory altogether and rustc stops using self-contained CRT, emitting bare `rcrt1.o` / `crti.o` names that the clang driver cannot resolve.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
rustc puts the prebuilt stdlib's
self-containeddirectory on the linker search path ahead of the CC toolchain's-L, so-lcalways resolves to the libc rustc ships. Anyone who builds their own sysroot — a distro, an OS image, an embedded target — silently links the wrong libc, and any patch carried in that libc (hardening flags, an allocator swap) never reaches a Rust binary.Measured on 1.95.0,
x86_64-unknown-linux-musl, with a CC toolchain whose--sysroot,-Land-Ball point at a locally built musl.ld.lld -tshows:The
-Lforself-containedsits at position 3 in lld's search list; the CC toolchain's is at position 10.-C link-self-contained=nodoes not fix it — it leaves that-Lin place, and the output is byte-identical with and without the flag. Dropping the files from the sandbox is the only lever I found.Change
stdlib_repositoryalso emitsrust_std-<triple>-external-libc: the same stdlib without the archives underself-contained.declare_rustc_toolchainstakesexternal_libc_triples, a list of target triples that should use it. Explicitrust_stdentries still win.The CRT objects deliberately stay in the filtered filegroup. Remove the directory altogether and rustc stops using self-contained CRT, emitting bare
rcrt1.o/crti.onames that the clang driver cannot resolve — that took a couple of iterations to find, so it is called out in a comment.Usage
Verification
Applied via
local_path_overridein a workspace that builds musl from source and registers a CC toolchain against that sysroot. Before, the link pulled the bundledself-contained/libc.a; withexternal_libc_triplesset and no other change, the same link pulls:— the workspace's libc, rustc's CRT. The resulting static-pie binaries run, and the workspace's test suite (including a VM boot of the produced image, both libc flavors) passes.
This replaced a local repo rule that symlinked the stdlib tree and regenerated a filtered filegroup, which is what motivated the PR.
Note
Independent of #244, though a consumer calling
declare_rustc_toolchainsfrom a workspace package rather than a generated repo wants that one too.