@@ -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
2727Note 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.
3636The function marked ` #[start] ` is passed the command line parameters
3737in 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
6570To override the compiler-inserted ` main ` shim, one has to disable it
6671with ` #![no_main] ` and then create the appropriate symbol with the
6772correct ABI and the correct name, which requires overriding the
6873compiler'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
7782extern 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
96115The first of these two functions, ` eh_personality ` , is used by the failure
97116mechanisms of the compiler. This is often mapped to GCC's personality function
98117(see the [ libstd implementation] [ unwind ] for more information), but crates
99118which 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 ` .
0 commit comments