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

Add x86_64-unknown-linux-none target #125023

Merged
merged 5 commits into from May 14, 2024
Merged

Conversation

morr0ne
Copy link
Contributor

@morr0ne morr0ne commented May 11, 2024

Adds a freestanding linux binary with no libc dependency. This is useful for writing programs written only in rust. It is also essential for writing low level stuff like libc or a dynamic linker.

Tier 3 policy:

A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)

I will be the designed maintainer for this target

Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.

The target triple is consistent with other targets

Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.

There is no confusion with other targets since it explicitly adds "none" at the end instead of omitting the environment

Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.

The target does not introduce any unusual requirement

The target must not introduce license incompatibilities.

There are no license incompatibilities

Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).

Everything added is under that license

The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.

There are no new dependencies

Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.

There is no proprietary dependencies

"onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.

No such terms exist for this target

Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.

This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.

Understood

Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.

The target already implements core. It might be possible in the future to add support for alloc and std by leveraging crates such as origin and rustix

The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.

I believe the proper docs are added

Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.

Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.

Understood

Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.

In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.

No other targets are effected

Tier 3 targets must be able to produce assembly using at least one of rustc's supported backends from any host target.

The same backends used by other linux targets work without issues

@morr0ne
Copy link
Contributor Author

morr0ne commented May 11, 2024

r? compiler-team

@rustbot
Copy link
Collaborator

rustbot commented May 11, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @GuillaumeGomez (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@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 May 11, 2024
@rustbot
Copy link
Collaborator

rustbot commented May 11, 2024

Some changes occurred in src/doc/rustc/src/platform-support

cc @Nilstrieb

These commits modify compiler targets.
(See the Target Tier Policy.)

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 11, 2024
@rustbot
Copy link
Collaborator

rustbot commented May 11, 2024

There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

You can start a rebase with the following commands:

$ # rebase
$ git pull --rebase https://github.com/rust-lang/rust.git master
$ git push --force-with-lease

The following commits are merge commits:

@GuillaumeGomez
Copy link
Member

r? nielstrieb

@rustbot
Copy link
Collaborator

rustbot commented May 11, 2024

Failed to set assignee to nielstrieb: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@GuillaumeGomez
Copy link
Member

r? nilstrieb

@rustbot rustbot assigned Nilstrieb and unassigned estebank May 11, 2024
@GuillaumeGomez
Copy link
Member

Arf didn't see that the reviewer was already changed... Putting back estebank.

r? estebank

@rustbot rustbot assigned estebank and unassigned Nilstrieb May 11, 2024
@rust-log-analyzer

This comment has been minimized.

@morr0ne
Copy link
Contributor Author

morr0ne commented May 11, 2024

Removed the merge commit, sorry about that

@rust-log-analyzer

This comment has been minimized.

@workingjubilee
Copy link
Contributor

@morr0ne could you more fully explain the intended distinction between this target and the x86_64-unknown-none and x86_86-unknown-linux-gnu (with #![no_std]) targets?

@workingjubilee
Copy link
Contributor

workingjubilee commented May 11, 2024

The target already implements core. It might be possible in the future to add support for alloc and std by leveraging crates such as origin and rustix

More particularly, I believe it should be clear from day 0 what the stdlib impls are supposed to be. It is not required that those implementations should be done, and it is fully possible for those to vary in details on the journey to implementation, but I believe it makes it much harder to follow platform support processes if "I intend to" is hedged into "might be possible maybe someday or not".

@morr0ne
Copy link
Contributor Author

morr0ne commented May 11, 2024

@morr0ne could you more fully explain the intended distinction between this target and the x86_64-unknown-none and x86_86-unknown-linux-gnu (with #![no_std]) targets?

The existing *-gnu targets make the assumption that the target system is running a gnu environment, which in pratice means assuming glibc and the corresponding linker are present. This new target is intended for targets that make zero assumption about the environment beside the underlining kernel (linux) conventions.
The intended use cases for this targets are mainly 2 at the moment:

  • Writing fully rust binaries that do not depend on libc
  • Writing low level code such a libc implementation itself

I have been using this target in my own projects for months at this point and the benefit is clear. Without this target there are workarounds to achieve the same goals but they piggyback off the existing gnu targets. This way we have a target that clearly states that we are running without a libc

More particularly, I believe it should be clear from day 0 what the stdlib impls are supposed to be. It is not required that those implementations should be done, and it is fully possible for those to vary in details on the journey to implementation, but I believe it makes it much harder to follow platform support processes if "I intend to" is hedged into "might be possible maybe someday or not".

Maybe I could have worded it better. Having std and alloc for this target is 100% possible, I have zero doubts about that. The "might" stems from me not knowing if that is within the scope of rust since that would require shipping an allocator with std. I can't confidently say "this target will support std and alloc" because I don't know if the rust projects wants such support for an enviroment-less target. What I can say is that if there's consensus and interest, it is possible to implement std and alloc

@morr0ne
Copy link
Contributor Author

morr0ne commented May 11, 2024

To further expand on my previous comment. The *-gnu targets assume the compiled binary will run on a GNU/Linux system. This new target is meant to target only Linux itself.

@workingjubilee
Copy link
Contributor

Maybe I could have worded it better. Having std and alloc for this target is 100% possible, I have zero doubts about that. The "might" stems from me not knowing if that is within the scope of rust since that would require shipping an allocator with std. I can't confidently say "this target will support std and alloc" because I don't know if the rust projects wants such support for an environment-less target. What I can say is that if there's consensus and interest, it is possible to implement std and alloc

Aha, okay! So you would be be happy to have this target with such implementations but also would be reasonably content without such implementations. Thank you, that clarifies the intent.

@morr0ne
Copy link
Contributor Author

morr0ne commented May 12, 2024

At the moment I can't really say I have strong requirements for a version. To actually be useful without a libc it definitely needs to rely on more modern versions of the kernel, at least compared to the existing *-gnu targets. It is hard to pin-point a specific version however and I am happy to leave it somewhat unspecified, at least until there is a clear path forward to implement std and alloc.

Generally speaking this target can be assumed to be somewhat bleeding edge. At the moment I work with the latest stable release of the kernel so it would be hard to notice if something is broken on older kernels

@estebank
Copy link
Contributor

@Nilstrieb do you want to snipe this review? I won't be around for a few days and this seems out of my wheelhouse. @workingjubilee thank you for taking on digging deeper on this!

@briansmith
Copy link
Contributor

At the moment I can't really say I have strong requirements for a version. To actually be useful without a libc it definitely needs to rely on more modern versions of the kernel, at least compared to the existing *-gnu targets. It is hard to pin-point a specific version however and I am happy to leave it somewhat unspecified, at least until there is a clear path forward to implement std and alloc.

It would be good to document that an unknown kernel version, which is known to be greater than 3.19, because this affects whether getrandom can be used.

On Linux, it is normally required to route all direct syscall calls through libc's syscall(2). Without a libc for this target, does this mean that we're supposed to issue syscalls directly, without any coordination from the runtime system? And further, are we guaranteed that the process will never be linked to any libc that requires syscalls to be routed through it? I think this is important to specify because there's no point in having target_os=linux without the ability to actually use Linux features.

In particular, how would we ask the OS to allocate memory for us to implement a heap?

What is the minimum CPU feature set for this target? Since we cannot use (IIUC) libstd's std::arch::is_x86_feature_detected, are we allowed to use the results of CPUID + xgetbv to dynamically determine which CPU features are available?

@tgross35
Copy link
Contributor

tgross35 commented May 14, 2024

Some background discussion at https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Direct.20syscalls.20on.20Linux, more in the vein of a std support. Having this target will be awesome.

@morr0ne
Copy link
Contributor Author

morr0ne commented May 14, 2024

It would be good to document that an unknown kernel version, which is known to be greater than 3.19, because this affects whether getrandom can be used.

It could be documented as such. But unless we have std we actually have no guarantee of such version. We might find out we actually can use a lower version, although unlikely.

On Linux, it is normally required to route all direct syscall calls through libc's syscall(2). Without a libc for this target, does this mean that we're supposed to issue syscalls directly, without any coordination from the runtime system?

Linux actually is the only mainstream os/kernel that allows us to make syscall directly. The userspace abi is stable and there is no standard libc. In a sense, there is no "runtime system" to coordinate with. There are many ways to make such syscalls but I believe the best solution is rustix, which I linked above.

And further, are we guaranteed that the process will never be linked to any libc that requires syscalls to be routed through it?

The point of this target is exactly to guarantee no libc is linked. Linux doesn't forces you to link to a libc implementation and even if that were the case, there is no issue calling direct syscalls even if linking to libc. There is no requirement to make syscalls from libc on linux, regardless of the target environment.

I think this is important to specify because there's no point in having target_os=linux without the ability to actually use Linux features.

The point of the target is to guarantee that we are running on top of the linux kernel. How syscalls are then made is up to the user in a no_std environments or up to std, however it decides to implement such calls.

In particular, how would we ask the OS to allocate memory for us to implement a heap?

We can ask the os via syscalls the same way the libc implementation does. Unlike other OSes, libc is not special on linux. There is nothing libc can do that we cannot do ourselves by calling direct syscalls or reading special virtual filesystems like /proc

What is the minimum CPU feature set for this target? Since we cannot use (IIUC) libstd's std::arch::is_x86_feature_detected, are we allowed to use the results of CPUID + xgetbv to dynamically determine which CPU features are available?

I do not know how std::arch::is_x86_feature_detected is implemented but I assume it is calling some libc function that can be replaced by our own implementation

Co-authored-by: Trevor Gross <t.gross35@gmail.com>
@Nilstrieb
Copy link
Member

There is no requirement to make syscalls from libc on linux, regardless of the target environment.

This is not entirely true, libcs generally do not appreciate you calling some syscalls directly without talking to them first. This is of course a non-issue when the libc is not in the address space at all, but when it is linked in some other way, that can lead to problems (one example being the forking syscalls). I assume that's what the person meant.

Adding this target as a tier 3 no_std target makes sense. Any library implementation for this target should go to the libs team first to decide whether this is something that is desired in the first place.

Given that the target won't really rely on Linux kernel features for now, documenting the minimum version doesn't make that much sense imo - it's as low as anyone wants (though you can write something there if you feel like it). It would of course also need to be specified when adding a library implementation or relying on features.

@morr0ne
Copy link
Contributor Author

morr0ne commented May 14, 2024

This is not entirely true, libcs generally do not appreciate you calling some syscalls directly without talking to them first. This is of course a non-issue when the libc is not in the address space at all, but when it is linked in some other way, that can lead to problems (one example being the forking syscalls). I assume that's what the person meant.

Maybe I missunderstood what they meant. Regardless this is a non issue since libc will not be linked in any way for this target.

Adding this target as a tier 3 no_std target makes sense. Any library implementation for this target should go to the libs team first to decide whether this is something that is desired in the first place.

That was exactly my thought process. Adding this as a tier 3 makes this easy to merge without issues and any library implementation can be properly discussed at a later point.

Given that the target won't really rely on Linux kernel features for now, documenting the minimum version doesn't make that much sense imo - it's as low as anyone wants (though you can write something there if you feel like it). It would of course also need to be specified when adding a library implementation or relying on features.

I fully agree with this and is exactly why I did not add a version in the documentation.

@Nilstrieb
Copy link
Member

In that case, this is good to merge.

I will approve this PR, adding a new tier 3 no_std target for bare Linux without libc. It does not commit to a rustix (or other)-based implementation of the standard library for bare Linux in the future (this should be discussed with the libs team first).
As a tier 3 target, no builds are available and -Zbuild-std should be used. As usual, targets do not come with stability guarantees and may be removed in the future, especially tier 3 targets.

@bors r+

@bors
Copy link
Contributor

bors commented May 14, 2024

📌 Commit 68407f9 has been approved by Nilstrieb

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 14, 2024
@programmerjake
Copy link
Member

programmerjake commented May 14, 2024

for things like fork and brk where libc expects you to call its functions instead of making syscalls, maybe you could add some optional dummy fork/brk/etc. functions to generate a linker error if you inadvertantly linked to libc.

@bors
Copy link
Contributor

bors commented May 14, 2024

⌛ Testing commit 68407f9 with merge 31026b7...

@bors
Copy link
Contributor

bors commented May 14, 2024

☀️ Test successful - checks-actions
Approved by: Nilstrieb
Pushing 31026b7 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 14, 2024
@bors bors merged commit 31026b7 into rust-lang:master May 14, 2024
7 checks passed
@rustbot rustbot added this to the 1.80.0 milestone May 14, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (31026b7): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.6% [0.2%, 1.0%] 3
Regressions ❌
(secondary)
0.4% [0.4%, 0.4%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.6% [0.2%, 1.0%] 3

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 676.788s -> 675.763s (-0.15%)
Artifact size: 316.11 MiB -> 315.97 MiB (-0.05%)

@rustbot rustbot added the perf-regression Performance regression. label May 14, 2024
@Nilstrieb
Copy link
Member

Nilstrieb commented May 14, 2024

no rustc-timer, that's noise

@Nilstrieb Nilstrieb added the perf-regression-triaged The performance regression has been triaged. label May 14, 2024
@Kobzol
Copy link
Contributor

Kobzol commented May 14, 2024

Noise, this shouldn't have changed perf.

@rustbot label: +perf-regression-triaged

@workingjubilee
Copy link
Contributor

workingjubilee commented May 15, 2024

Yeah, for the purpose of the version, I literally asked so that if some time months down the line someone has to archaeology up the original PR for this target, that there is indeed an intention captured there regarding what version was intended. Even like a... ballpark. It's of course fine to not write it down since it does not matter yet and doesn't reflect an actual promise.

I do not know how std::arch::is_x86_feature_detected is implemented but I assume it is calling some libc function that can be replaced by our own implementation

@morr0ne For x86-64, it actually is entirely assembly.

@morr0ne
Copy link
Contributor Author

morr0ne commented May 15, 2024

@morr0ne For x86-64, it actually is entirely assembly.

That is actually great to hear. It means we don't need to worry about the implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

None yet