@@ -21,7 +21,7 @@ this using our `Cargo.toml` file:
21
21
22
22
``` toml
23
23
[dependencies ]
24
- libc = { version = " 0.2.11 " , default-features = false }
24
+ libc = { version = " 0.2.14 " , default-features = false }
25
25
```
26
26
27
27
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.
36
36
The function marked ` #[start] ` is passed the command line parameters
37
37
in the same format as C:
38
38
39
- ``` rust
40
- # #![feature(libc)]
39
+ ``` rust,ignore
41
40
#![feature(lang_items)]
42
41
#![feature(start)]
43
42
#![no_std]
@@ -51,53 +50,77 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
51
50
0
52
51
}
53
52
54
- // These functions and traits are used by the compiler, but not
53
+ // These functions are used by the compiler, but not
55
54
// for a bare-bones hello world. These are normally
56
55
// 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
+ }
63
68
```
64
69
65
70
To override the compiler-inserted ` main ` shim, one has to disable it
66
71
with ` #![no_main] ` and then create the appropriate symbol with the
67
72
correct ABI and the correct name, which requires overriding the
68
73
compiler's name mangling too:
69
74
70
- ``` rust
71
- # #![feature(libc)]
75
+ ``` rust,ignore
72
76
#![feature(lang_items)]
73
77
#![feature(start)]
74
78
#![no_std]
75
79
#![no_main]
76
80
81
+ // Pull in the system libc library for what crt0.o likely requires
77
82
extern crate libc;
78
83
84
+ // Entry point for this program
79
85
#[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 {
81
87
0
82
88
}
83
89
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
+ }
90
105
```
91
106
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.
95
114
96
115
The first of these two functions, ` eh_personality ` , is used by the failure
97
116
mechanisms of the compiler. This is often mapped to GCC's personality function
98
117
(see the [ libstd implementation] [ unwind ] for more information), but crates
99
118
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
+
103
121
[ 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