Skip to content

Commit 3b2813d

Browse files
committed
Rename main to origin_main.
This isn't the C ABI `main` function, so give it a different name. Also, make it an `unsafe fn`, and add the `envp` arguments so that we don't rely on the ABI to ignore extra arguments.
1 parent 7dcbe72 commit 3b2813d

8 files changed

Lines changed: 24 additions & 23 deletions

File tree

example-crates/basic/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use origin::thread::*;
21
use origin::program::*;
2+
use origin::thread::*;
33

44
fn main() {
55
eprintln!("Hello from main thread");

example-crates/external-start/src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ extern crate libc;
1313
use alloc::boxed::Box;
1414
use atomic_dbg::{dbg, eprintln};
1515
use core::sync::atomic::{AtomicBool, Ordering};
16-
use origin::thread::*;
1716
use origin::program::*;
17+
use origin::thread::*;
1818

1919
#[panic_handler]
2020
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
@@ -56,7 +56,7 @@ static EARLY_INIT_ARRAY: unsafe extern "C" fn(i32, *mut *mut u8) = {
5656
};
5757

5858
#[no_mangle]
59-
fn main(_argc: i32, _argv: *const *const u8) -> i32 {
59+
unsafe fn origin_main(_argc: i32, _argv: *const *const u8, _envp: *const *const u8) -> i32 {
6060
eprintln!("Hello from main thread");
6161

6262
at_exit(Box::new(|| eprintln!("Hello from an at_exit handler")));
@@ -77,10 +77,15 @@ fn main(_argc: i32, _argv: *const *const u8) -> i32 {
7777
)
7878
.unwrap();
7979

80-
unsafe {
81-
join_thread(thread);
82-
}
80+
join_thread(thread);
8381

8482
eprintln!("Goodbye from main");
8583
exit(0);
8684
}
85+
86+
// Libc calls `main` so we need to provide a definition to satisfy the
87+
// linker, however origin gains control before libc can call this `main`.
88+
#[no_mangle]
89+
unsafe fn main(_argc: i32, _argv: *const *const u8, _envp: *const *const u8) -> i32 {
90+
core::intrinsics::abort();
91+
}

example-crates/no-std/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ extern crate alloc;
1010

1111
use alloc::boxed::Box;
1212
use atomic_dbg::{dbg, eprintln};
13-
use origin::thread::*;
1413
use origin::program::*;
14+
use origin::thread::*;
1515

1616
#[panic_handler]
1717
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {

example-crates/origin-start-lto/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern "C" fn eh_personality() {}
2727
static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::GlobalDlmalloc;
2828

2929
#[no_mangle]
30-
fn main(_argc: i32, _argv: *const *const u8) -> i32 {
30+
unsafe fn origin_main(_argc: i32, _argv: *const *const u8, _envp: *const *const u8) -> i32 {
3131
eprintln!("Hello from main thread");
3232

3333
at_exit(Box::new(|| eprintln!("Hello from an at_exit handler")));
@@ -48,9 +48,7 @@ fn main(_argc: i32, _argv: *const *const u8) -> i32 {
4848
)
4949
.unwrap();
5050

51-
unsafe {
52-
join_thread(thread);
53-
}
51+
join_thread(thread);
5452

5553
eprintln!("Goodbye from main");
5654
exit(0);

example-crates/origin-start-no-alloc/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
2121
extern "C" fn eh_personality() {}
2222

2323
#[no_mangle]
24-
fn main(_argc: i32, _argv: *const *const u8) -> i32 {
24+
unsafe fn origin_main(_argc: i32, _argv: *const *const u8, _envp: *const *const u8) -> i32 {
2525
eprintln!("Hello!");
2626

2727
// Unlike origin-start, this example can't create threads because origin's

example-crates/origin-start/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern "C" fn eh_personality() {}
2727
static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::GlobalDlmalloc;
2828

2929
#[no_mangle]
30-
fn main(_argc: i32, _argv: *const *const u8) -> i32 {
30+
unsafe fn origin_main(_argc: i32, _argv: *const *const u8, _envp: *const *const u8) -> i32 {
3131
eprintln!("Hello from main thread");
3232

3333
at_exit(Box::new(|| eprintln!("Hello from an at_exit handler")));
@@ -48,9 +48,7 @@ fn main(_argc: i32, _argv: *const *const u8) -> i32 {
4848
)
4949
.unwrap();
5050

51-
unsafe {
52-
join_thread(thread);
53-
}
51+
join_thread(thread);
5452

5553
eprintln!("Goodbye from main");
5654
exit(0);

example-crates/tiny/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#![feature(lang_items)]
77
#![feature(core_intrinsics)]
88

9-
extern crate origin;
109
extern crate compiler_builtins;
10+
extern crate origin;
1111

1212
#[panic_handler]
1313
fn panic(_panic: &core::panic::PanicInfo<'_>) -> ! {
@@ -18,6 +18,6 @@ fn panic(_panic: &core::panic::PanicInfo<'_>) -> ! {
1818
extern "C" fn eh_personality() {}
1919

2020
#[no_mangle]
21-
fn main(_argc: i32, _argv: *const *const u8) -> i32 {
21+
unsafe fn origin_main(_argc: i32, _argv: *const *const u8, _envp: *const *const u8) -> i32 {
2222
42
2323
}

src/program.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,21 @@ unsafe fn init_runtime(mem: *mut usize, envp: *mut *mut u8) {
117117
#[allow(unused_variables)]
118118
unsafe fn call_user_code(argc: c_int, argv: *mut *mut u8, envp: *mut *mut u8) -> i32 {
119119
extern "Rust" {
120-
fn main(argc: c_int, argv: *mut *mut u8, envp: *mut *mut u8) -> c_int;
120+
fn origin_main(argc: c_int, argv: *mut *mut u8, envp: *mut *mut u8) -> c_int;
121121
}
122122

123123
// Call the functions registered via `.init_array`.
124124
#[cfg(feature = "init-fini-arrays")]
125125
call_ctors(argc, argv, envp);
126126

127127
#[cfg(feature = "log")]
128-
log::trace!("Calling `main({:?}, {:?}, {:?})`", argc, argv, envp);
128+
log::trace!("Calling `origin_main({:?}, {:?}, {:?})`", argc, argv, envp);
129129

130-
// Call `main`.
131-
let status = main(argc, argv, envp);
130+
// Call `origin_main`.
131+
let status = origin_main(argc, argv, envp);
132132

133133
#[cfg(feature = "log")]
134-
log::trace!("`main` returned `{:?}`", status);
134+
log::trace!("`origin_main` returned `{:?}`", status);
135135

136136
status
137137
}

0 commit comments

Comments
 (0)