Skip to content

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

Closed
@japaric

Description

@japaric

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions