Skip to content

Commit 3acd1a4

Browse files
committed
Fix calling convention for CRT startup
My PR #81478 used the wrong calling convention for a set of functions that are called by the CRT. These functions need to use `extern "C"`. This would only affect x86, which is the only target (that I know of) that has multiple calling conventions.
1 parent 0e63af5 commit 3acd1a4

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

library/std/src/sys/windows/compat.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,24 @@ macro_rules! compat_fn {
7474
/// used, and would remove it.
7575
#[used]
7676
#[link_section = ".CRT$XCU"]
77-
static INIT_TABLE_ENTRY: fn() = init;
77+
static INIT_TABLE_ENTRY: unsafe extern "C" fn() = init;
7878

79-
fn init() {
79+
unsafe extern "C" fn init() {
8080
// There is no locking here. This code is executed before main() is entered, and
8181
// is guaranteed to be single-threaded.
8282
//
8383
// DO NOT do anything interesting or complicated in this function! DO NOT call
8484
// any Rust functions or CRT functions, if those functions touch any global state,
8585
// because this function runs during global initialization. For example, DO NOT
8686
// do any dynamic allocation, don't call LoadLibrary, etc.
87-
unsafe {
88-
let module_name: *const u8 = concat!($module, "\0").as_ptr();
89-
let symbol_name: *const u8 = concat!(stringify!($symbol), "\0").as_ptr();
90-
let module_handle = $crate::sys::c::GetModuleHandleA(module_name as *const i8);
91-
if !module_handle.is_null() {
92-
match $crate::sys::c::GetProcAddress(module_handle, symbol_name as *const i8) as usize {
93-
0 => {}
94-
n => {
95-
PTR = Some(mem::transmute::<usize, F>(n));
96-
}
87+
let module_name: *const u8 = concat!($module, "\0").as_ptr();
88+
let symbol_name: *const u8 = concat!(stringify!($symbol), "\0").as_ptr();
89+
let module_handle = $crate::sys::c::GetModuleHandleA(module_name as *const i8);
90+
if !module_handle.is_null() {
91+
match $crate::sys::c::GetProcAddress(module_handle, symbol_name as *const i8) as usize {
92+
0 => {}
93+
n => {
94+
PTR = Some(mem::transmute::<usize, F>(n));
9795
}
9896
}
9997
}

0 commit comments

Comments
 (0)