Skip to content

Commit fcecdac

Browse files
committed
Fix up documentation around no_std
1. Fix the sections in the book to have the correct signatures. I've also marked them as `ignore`; there's no way to set the `no_std` feature for libc, so it pulls in the stdlib, so this wasn't even testing the actual thing it was testing. Better to just ignore. 2. Correcting libcore's docs for factual inaccuracy, and add a note about language items. Fixes #33677
1 parent a120ae7 commit fcecdac

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

src/doc/book/no-stdlib.md

+48-25
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ this using our `Cargo.toml` file:
2121

2222
```toml
2323
[dependencies]
24-
libc = { version = "0.2.11", default-features = false }
24+
libc = { version = "0.2.14", default-features = false }
2525
```
2626

2727
Note that the default features have been disabled. This is a critical step -
@@ -36,8 +36,7 @@ or overriding the default shim for the C `main` function with your own.
3636
The function marked `#[start]` is passed the command line parameters
3737
in the same format as C:
3838

39-
```rust
40-
# #![feature(libc)]
39+
```rust,ignore
4140
#![feature(lang_items)]
4241
#![feature(start)]
4342
#![no_std]
@@ -51,53 +50,77 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
5150
0
5251
}
5352
54-
// These functions and traits are used by the compiler, but not
53+
// These functions are used by the compiler, but not
5554
// for a bare-bones hello world. These are normally
5655
// provided by libstd.
57-
#[lang = "eh_personality"] extern fn eh_personality() {}
58-
#[lang = "panic_fmt"] extern fn panic_fmt() -> ! { loop {} }
59-
# #[lang = "eh_unwind_resume"] extern fn rust_eh_unwind_resume() {}
60-
# #[no_mangle] pub extern fn rust_eh_register_frames () {}
61-
# #[no_mangle] pub extern fn rust_eh_unregister_frames () {}
62-
# // fn main() {} tricked you, rustdoc!
56+
#[lang = "eh_personality"]
57+
#[no_mangle]
58+
pub extern fn eh_personality() {
59+
}
60+
61+
#[lang = "panic_fmt"]
62+
#[no_mangle]
63+
pub extern fn rust_begin_panic(_msg: core::fmt::Arguments,
64+
_file: &'static str,
65+
_line: u32) -> ! {
66+
loop {}
67+
}
6368
```
6469

6570
To override the compiler-inserted `main` shim, one has to disable it
6671
with `#![no_main]` and then create the appropriate symbol with the
6772
correct ABI and the correct name, which requires overriding the
6873
compiler's name mangling too:
6974

70-
```rust
71-
# #![feature(libc)]
75+
```rust,ignore
7276
#![feature(lang_items)]
7377
#![feature(start)]
7478
#![no_std]
7579
#![no_main]
7680
81+
// Pull in the system libc library for what crt0.o likely requires
7782
extern crate libc;
7883
84+
// Entry point for this program
7985
#[no_mangle] // ensure that this symbol is called `main` in the output
80-
pub extern fn main(argc: i32, argv: *const *const u8) -> i32 {
86+
pub extern fn main(_argc: i32, _argv: *const *const u8) -> i32 {
8187
0
8288
}
8389
84-
#[lang = "eh_personality"] extern fn eh_personality() {}
85-
#[lang = "panic_fmt"] extern fn panic_fmt() -> ! { loop {} }
86-
# #[lang = "eh_unwind_resume"] extern fn rust_eh_unwind_resume() {}
87-
# #[no_mangle] pub extern fn rust_eh_register_frames () {}
88-
# #[no_mangle] pub extern fn rust_eh_unregister_frames () {}
89-
# // fn main() {} tricked you, rustdoc!
90+
// These functions and traits are used by the compiler, but not
91+
// for a bare-bones hello world. These are normally
92+
// provided by libstd.
93+
#[lang = "eh_personality"]
94+
#[no_mangle]
95+
pub extern fn eh_personality() {
96+
}
97+
98+
#[lang = "panic_fmt"]
99+
#[no_mangle]
100+
pub extern fn rust_begin_panic(_msg: core::fmt::Arguments,
101+
_file: &'static str,
102+
_line: u32) -> ! {
103+
loop {}
104+
}
90105
```
91106

92-
The compiler currently makes a few assumptions about symbols which are available
93-
in the executable to call. Normally these functions are provided by the standard
94-
library, but without it you must define your own.
107+
## More about the langauge items
108+
109+
The compiler currently makes a few assumptions about symbols which are
110+
available in the executable to call. Normally these functions are provided by
111+
the standard library, but without it you must define your own. These symbols
112+
are called "language items", and they each have an internal name, and then a
113+
signature that an implementation must conform to.
95114

96115
The first of these two functions, `eh_personality`, is used by the failure
97116
mechanisms of the compiler. This is often mapped to GCC's personality function
98117
(see the [libstd implementation][unwind] for more information), but crates
99118
which do not trigger a panic can be assured that this function is never
100-
called. The second function, `panic_fmt`, is also used by the failure
101-
mechanisms of the compiler.
102-
119+
called. Both the language item and the symbol name are `eh_personality`.
120+
103121
[unwind]: https://github.com/rust-lang/rust/blob/master/src/libpanic_unwind/gcc.rs
122+
123+
The second function, `panic_fmt`, is also used by the failure mechanisms of the
124+
compiler. When a panic happens, this controls the message that's displayed on
125+
the screen. While the language item's name is `panic_fmt`, the symbol name is
126+
`rust_begin_panic`.

src/libcore/lib.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
//!
2626
//! # How to use the core library
2727
//!
28+
//! Please note that all of these details are currently not considered stable.
29+
//!
2830
// FIXME: Fill me in with more detail when the interface settles
2931
//! This library is built on the assumption of a few existing symbols:
3032
//!
@@ -34,11 +36,12 @@
3436
//! These functions are often provided by the system libc, but can also be
3537
//! provided by the [rlibc crate](https://crates.io/crates/rlibc).
3638
//!
37-
//! * `rust_begin_unwind` - This function takes three arguments, a
38-
//! `fmt::Arguments`, a `&str`, and a `u32`. These three arguments dictate
39-
//! the panic message, the file at which panic was invoked, and the line.
40-
//! It is up to consumers of this core library to define this panic
41-
//! function; it is only required to never return.
39+
//! * `rust_begin_panic` - This function takes three arguments, a
40+
//! `fmt::Arguments`, a `&'static str`, and a `u32`. These three arguments
41+
//! dictate the panic message, the file at which panic was invoked, and the
42+
//! line. It is up to consumers of this core library to define this panic
43+
//! function; it is only required to never return. This requires a `lang`
44+
//! attribute named `panic_fmt`.
4245
4346
// Since libcore defines many fundamental lang items, all tests live in a
4447
// separate crate, libcoretest, to avoid bizarre issues.

0 commit comments

Comments
 (0)