Skip to content

Commit 7e76aa9

Browse files
imlk0oxr463
authored andcommitted
fix: fix undefined reference to `memset'
1 parent fd39e38 commit 7e76aa9

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,3 @@ members = [
88

99
[profile.release]
1010
strip = true
11-
12-
[profile.dev.package.loader-shim]
13-
opt-level = 1
14-
15-
[profile.release.package.loader-shim]
16-
opt-level = "s"

loader-shim/src/main.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
// system standard libraries.
1818
// See https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
1919
//
20-
// Since _start is defined in the system startup files, with this option
20+
// The `-nostdlib` flag is much like a combination of `-nostartfiles` and
21+
// `-nodefaultlibs`.
22+
//
23+
// Since `_start` is defined in the system startup files, with this option
2124
// we can use our own `_start` function to override the program entry point.
2225
#[link_args = "-nostdlib"]
2326
#[link_args = "-ffreestanding"]
@@ -28,6 +31,18 @@
2831
#[cfg_attr(target_arch = "aarch64", link_args = "-Wl,-Ttext=0x2000000000")]
2932
extern "C" {}
3033

34+
// The compiler may emit a call to the `memset()` function even if there is
35+
// no such call in our code. However, since we use `-nostdlib` or
36+
// `-nodefaultlibs`, this means we will not link to libc, which provides the
37+
// implementation of `memset()`.
38+
//
39+
// In this case, we will get an `undefined reference to \`memset'` error.
40+
// Fortunately, the crate `rlibc` provides an unoptimized implementation of
41+
// `memset()`.
42+
//
43+
// See `-nodefaultlibs` at https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
44+
extern crate rlibc;
45+
3146
mod script;
3247

3348
use core::{fmt::Write, panic::PanicInfo};

0 commit comments

Comments
 (0)