Skip to content

linker error when the "weak" feature is enabled #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
japaric opened this issue Dec 12, 2016 · 0 comments · Fixed by #128
Closed

linker error when the "weak" feature is enabled #126

japaric opened this issue Dec 12, 2016 · 0 comments · Fixed by #128

Comments

@japaric
Copy link
Member

japaric commented Dec 12, 2016

STR

$ cargo new --bin app && cd $_
$ edit Cargo.toml && tail -n4 $_
[dependencies.compiler_builtins]
branch = "rustbuild"
features = ["weak"]
git = "https://github.com/japaric/compiler-builtins"
$ edit src/main.rs && cat $_
#![feature(compiler_builtins_lib)]
#![feature(core_intrinsics)]
#![feature(lang_items)]
#![no_main]
#![no_std]

extern crate compiler_builtins;

use core::intrinsics;

#[no_mangle]
pub fn _start() {
    unsafe {
        intrinsics::copy_nonoverlapping(0x0 as *const u32, 0x20 as *mut u32, 0x20);
    }
}

#[no_mangle]
pub fn __aeabi_unwind_cpp_pr0() {}

#[lang = "panic_fmt"]
extern "C" fn panic_fmt() {}
$ xargo rustc --target thumbv6m-none-eabi -- -C link-args=-nostartfiles
error: linking with `arm-none-eabi-gcc` failed: exit code: 1
  |
  = note: "arm-none-eabi-gcc" "-L" "/home/japaric/.xargo/lib/rustlib/thumbv6m-none-eabi/lib" "/home/japaric/tmp/app/target/thumbv6m-none-eabi/debug/deps/app-6d9087d3fe6b93f5.0.o" "-o" "/home/japaric/tmp/app/target/thumbv6m-none-eabi/debug/deps/app-6d9087d3fe6b93f5" "-Wl,--gc-sections" "-nodefaultlibs" "-L" "/home/japaric/tmp/app/target/thumbv6m-none-eabi/debug/deps" "-L" "/home/japaric/tmp/app/target/debug/deps" "-L" "/home/japaric/.xargo/lib/rustlib/thumbv6m-none-eabi/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "/home/japaric/tmp/app/target/thumbv6m-none-eabi/debug/deps/librlibc-ca4e4855ef16b6a5.rlib" "/home/japaric/.xargo/lib/rustlib/thumbv6m-none-eabi/lib/libcore-539305ece2e53820.rlib" "/home/japaric/tmp/app/target/thumbv6m-none-eabi/debug/deps/libcompiler_builtins-71942e6a7f812dde.rlib" "-nostartfiles"
  = note: /home/japaric/tmp/app/target/thumbv6m-none-eabi/debug/deps/libcompiler_builtins-71942e6a7f812dde.rlib(compiler_builtins-71942e6a7f812dde.0.o): In function `compiler_builtins::arm::__aeabi_memcpy4':
/home/japaric/.cargo/git/checkouts/compiler-builtins-299ea7fa6d11e5d0/b59c789/src/arm.rs:117: undefined reference to `memcpy'
/usr/lib/gcc/arm-none-eabi/6.2.0/../../../../arm-none-eabi/bin/ld: /home/japaric/tmp/app/target/thumbv6m-none-eabi/debug/deps/app-6d9087d3fe6b93f5: hidden symbol `memcpy' isn't defined
/usr/lib/gcc/arm-none-eabi/6.2.0/../../../../arm-none-eabi/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

Note the order of the linker arguments:

  • app.0.o
  • librlibc.rlib
  • libcore.rlib
  • libcompiler_builtins.rlib

And the relevant bits of running nm over these object files:

$ arm-none-eabi-nm -g **/app*.0.o
         U __aeabi_memcpy4

$ arm-none-eabi-nm -g **/librlibc*.rlib
00000000 W memcmp
00000000 W memcpy
00000000 W memmove
00000000 W memset

$ arm-none-eabi-nm -g ~/.xargo/lib/rustlib/thumbv6m-none-eabi/lib/libcore*.rlib
         U __aeabi_memclr4
         U __aeabi_memcpy
         U __aeabi_memcpy4
         U __aeabi_memset

$ arm-none-eabi-nm -g **/libcompiler_builtins*.rlib
         U memcpy
         U memmove
         U memset
00000000 T __aeabi_memclr
00000000 T __aeabi_memclr4
00000000 T __aeabi_memclr8
00000000 T __aeabi_memcpy
00000000 T __aeabi_memcpy4
00000000 T __aeabi_memcpy8
00000000 T __aeabi_memmove
00000000 T __aeabi_memmove4
00000000 T __aeabi_memmove8
00000000 T __aeabi_memset
00000000 T __aeabi_memset4
00000000 T __aeabi_memset8

For this to actually work, librlibc.rlib should be passed to the linker after libcompiler_builtins.rlib but that's not possible because compiler-builtins is marked as #![compiler_builtins] and will always be the last rlib to be passed to the linker.

Not quite sure how to solve this other than copy paste rlibc's mem* functions into compiler-builtins or special case rlibc in rustc ...

May be worth to mention that this is not a problem in no_std programs that link to libc and not use the "weak" Cargo feature as the linker arguments would look look like: libcore.rlib .. libcompiler_builtins.rlib -lc. In that case -lc would provide memcpy et al.

cc @alexcrichton

japaric pushed a commit to japaric/compiler-builtins that referenced this issue Dec 18, 2016
behind the "mem" Cargo feature, which used to be named "weak"

fixes rust-lang#126
japaric pushed a commit to japaric/compiler-builtins that referenced this issue Dec 18, 2016
behind the "mem" Cargo feature, which used to be named "weak"

fixes rust-lang#126
bors added a commit that referenced this issue Dec 18, 2016
add implementations of memcpy et al

behind the "mem" Cargo feature, which used to be named "weak"

fixes #126

Possible solution to #126
r? @alexcrichton
@bors bors closed this as completed in #128 Dec 18, 2016
tgross35 pushed a commit to tgross35/compiler-builtins that referenced this issue Feb 23, 2025
126: implement pow r=japaric a=japaric

cargo fmt version of rust-lang#125 
closes rust-lang#125 
closes rust-lang#124

Co-authored-by: Rahul Butani <[email protected]>
Co-authored-by: Jorge Aparicio <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant