From 7e76aa93196cf24a51f296ba99346a7f63510752 Mon Sep 17 00:00:00 2001 From: imlk Date: Thu, 19 Aug 2021 23:22:16 +0800 Subject: [PATCH] fix: fix undefined reference to `memset' --- Cargo.toml | 6 ------ loader-shim/src/main.rs | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7a967e..591118c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,9 +8,3 @@ members = [ [profile.release] strip = true - -[profile.dev.package.loader-shim] -opt-level = 1 - -[profile.release.package.loader-shim] -opt-level = "s" diff --git a/loader-shim/src/main.rs b/loader-shim/src/main.rs index 60389bb..ee938a9 100644 --- a/loader-shim/src/main.rs +++ b/loader-shim/src/main.rs @@ -17,7 +17,10 @@ // system standard libraries. // See https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html // -// Since _start is defined in the system startup files, with this option +// The `-nostdlib` flag is much like a combination of `-nostartfiles` and +// `-nodefaultlibs`. +// +// Since `_start` is defined in the system startup files, with this option // we can use our own `_start` function to override the program entry point. #[link_args = "-nostdlib"] #[link_args = "-ffreestanding"] @@ -28,6 +31,18 @@ #[cfg_attr(target_arch = "aarch64", link_args = "-Wl,-Ttext=0x2000000000")] extern "C" {} +// The compiler may emit a call to the `memset()` function even if there is +// no such call in our code. However, since we use `-nostdlib` or +// `-nodefaultlibs`, this means we will not link to libc, which provides the +// implementation of `memset()`. +// +// In this case, we will get an `undefined reference to \`memset'` error. +// Fortunately, the crate `rlibc` provides an unoptimized implementation of +// `memset()`. +// +// See `-nodefaultlibs` at https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html +extern crate rlibc; + mod script; use core::{fmt::Write, panic::PanicInfo};