Skip to content

Commit 51c8b1b

Browse files
authored
Use an LTO-compatible solution for cabi_realloc (#865)
This switches to using `#[used]` in rustc which will force the symbol reference to survived LTO. Closes #864
1 parent 3e2722d commit 51c8b1b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

crates/guest-rust/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,17 +377,20 @@ pub mod rt {
377377
) -> *mut u8;
378378
}
379379
// Force the `cabi_realloc` symbol to be referenced from here. This
380-
// function isn't ever actually used at runtime but the symbol needs
381-
// to be used here to force it to get referenced all the way through
382-
// to the linker. By the time we get to the linker this symbol will
383-
// be discarded due to it never actually being used by
384-
// `cabi_realloc` will have already been referenced at which point
385-
// its export name annotation will ensure it's exported regardless.
380+
// is done with a `#[used]` Rust `static` to ensure that this
381+
// reference makes it all the way to the linker before it's
382+
// considered for garbage collection. When the linker sees it it'll
383+
// remove this `static` here (due to it not actually being needed)
384+
// but the linker will have at that point seen the `cabi_realloc`
385+
// symbol and it should get exported.
386386
#[cfg(feature = "realloc")]
387-
unsafe {
388-
cabi_realloc(core::ptr::null_mut(), 0, 1, 1);
389-
}
390-
unreachable!();
387+
#[used]
388+
static _NAME_DOES_NOT_MATTER: unsafe extern "C" fn(
389+
*mut u8,
390+
usize,
391+
usize,
392+
usize,
393+
) -> *mut u8 = cabi_realloc;
391394
}
392395
}
393396

0 commit comments

Comments
 (0)