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

Test linking and running no_std binaries #138904

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

madsmtm
Copy link
Contributor

@madsmtm madsmtm commented Mar 24, 2025

I looked around, but it seems that we do not have a test that tests a #![no_std] + #![no_main] binary. So now I've added one. Motivated by discussion in this Zulip thread.

r? @tgross35

I haven't tested on Windows, so:
try-run: dist-x86_64-msvc

And only tested lightly on Linux, so:
try-run: dist-x86_64-linux

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 24, 2025
Comment on lines 13 to 31
// Need to link libSystem on Apple platforms, otherwise the linker fails with:
// > ld: dynamic executables or dylibs must link with libSystem.dylib
//
// With the new linker introduced in Xcode 15, the error is instead:
// > Undefined symbols: "dyld_stub_binder", referenced from: <initial-undefines>
//
// This _can_ be worked around by raising the deployment target with
// MACOSX_DEPLOYMENT_TARGET=13.0, though it's a bit hard to test that while
// still allowing the test suite to support running with older Xcode versions.
#[cfg_attr(target_vendor = "apple", link(name = "System"))]
extern "C" {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just always link libSystem internally in rustc, but I'm not sure that's a good idea, since as noted, it is possible to create a fully no_std binary that doesn't link libSystem, it just requires the user to do some work.

Maybe better would be to document the limitation somewhere (where?)? Note that libc does the linking internally too (linking to libc.dylib also links to libSystem.dylib), so any code that does #![no_std] but uses libc works without this.

@folkertdev
Copy link
Contributor

For completeness, there is https://github.com/rust-lang/rust/blob/master/tests/run-make/thumb-none-qemu/example/src/main.rs so it's not completely untested, but coverage on more platforms is good of course.


//@ run-pass
//@ compile-flags: -Cpanic=abort
//@ ignore-wasm different `main` convention
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if we need to add more to the ignores here? Is something like ignore-embedded a thing, and would it make sense?

@madsmtm
Copy link
Contributor Author

madsmtm commented Mar 24, 2025

For completeness, there is https://github.com/rust-lang/rust/blob/master/tests/run-make/thumb-none-qemu/example/src/main.rs so it's not completely untested, but coverage on more platforms is good of course.

Ah nice. Do you think I should try to merge this test with that?

@rust-log-analyzer

This comment has been minimized.

@folkertdev
Copy link
Contributor

folkertdev commented Mar 24, 2025

I don't think so, that test is very specifically for checking that the embedded ecosystem continues to work. It uses qemu to run on a simulated microcontroller. So instead additionally having this test for many more platforms in the ui tests seems independently useful.

@madsmtm madsmtm force-pushed the apple-test-no-std branch from e74978d to f9770e7 Compare March 24, 2025 22:22

// # Linux
//
// Linking `libc` is required by crt1.o, otherwise the linker fails with:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be fun to have a _start lib-less linux binary as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants