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

Rewrite the linking section of "A Freestanding Rust Binary" #577

Merged
merged 1 commit into from
Apr 11, 2019

Conversation

phil-opp
Copy link
Owner

@phil-opp phil-opp commented Apr 9, 2019

This pull request rewrites the section about overwriting the entry point. Instead of recommending to pass platform-specific arguments to the linker, the post now solves the linker errors by compiling for the bare metal thumbv7em-none-eabihf target. This makes much more sense, since it is the same approach that we use in the second post (by using a custom bare metal target).

We keep the old section about the platform specific linker arguments, but perform the following changes:

  • The section is now optional and hidden by default. It was de facto optional before since the second post does not use the linker arguments at all. It's main motivation – getting a working cargo build at the end of the post – is now achieved by building for thumbv7em-none-eabihf, so we no longer need it for bringing the post to an end.
  • Instead of using the default entry point names on Windows and macOS, which leads to multiple entry points in our src/main.rs, we now use _start on these platforms as well by explicitly overriding the entry point name.
  • Instead of requiring both mainCRTStartup and main on Windows, we now specifcy the subsystem explicitly. Thus we no longer need to create a function named main so that the linker can infer a default subsystem.
  • Instead of linking the System library on macOS, we force a static binary by passing -static and -nostartfiles.
  • Since some people mentioned it in the comments, we now include a short subsection that shows how to set the linker arguments as rustflags keys in a .cargo/config file.
  • Since freestanding binaries on top of Windows or macOS do not make much sense, there is now a subsection that warns about undesirable effects and recommends using the #[start] attribute for no_std binaries that should run on top of existing OSs.

Further, this PR reorganize section levels so that we have more top level sections instead of keeping everything in the "Disabling the Standard Library" section.


Preview

PR to update post-01 branch: #578

@phil-opp phil-opp added the relnotes "Release notes" – Notable changes that are rendered on the blog. label Apr 9, 2019
phil-opp added a commit that referenced this pull request Apr 9, 2019
See #577 for information about the rewrite.

This commit updates the azure pipelines CI script in the following way:

- build on all three OSs
- build for thumbv7em-none-eabihf
@phil-opp phil-opp changed the title Rewrite "A Freestanding Rust Binary" Rewrite the linking section of "A Freestanding Rust Binary" Apr 9, 2019
@phil-opp phil-opp added this to In Progress in Roadmap Apr 9, 2019
- solve linker errors by compiling for thumbv7em-none-eabihf
- make old section about linker arguments optional content
- use different linker arguments on Windows and macOS to use the `_start` entry point on all OSs
- Add an (optional) section that explains how to set linker arguments in .cargo/config
- Add an (optional) section that discourages the linker arguments approach and recommends the `#[start]` attribute instead
- reorganize section levels
@phil-opp phil-opp merged commit 0b9ca84 into master Apr 11, 2019
Roadmap automation moved this from In Progress to Done Apr 11, 2019
@bors bors bot deleted the rewrite-post-01 branch April 11, 2019 13:28
phil-opp added a commit that referenced this pull request Apr 11, 2019
See #577 for information about the rewrite.

This commit updates the azure pipelines CI script in the following way:

- build on all three OSs
- build for thumbv7em-none-eabihf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes "Release notes" – Notable changes that are rendered on the blog.
Projects
Development

Successfully merging this pull request may close these issues.

None yet

1 participant