Skip to content

nostd libc not link #32661

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
adjivas opened this issue Apr 1, 2016 · 3 comments
Closed

nostd libc not link #32661

adjivas opened this issue Apr 1, 2016 · 3 comments

Comments

@adjivas
Copy link

adjivas commented Apr 1, 2016

I tried this code:

  • src/main.rs:
#![feature(lang_items)]
#![feature(start)]
#![no_std]

extern crate libc;

#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
    let buffer: [u8; 1] = [0u8];

    unsafe {
        libc::read(0, buffer.as_ptr() as *mut libc::c_void, 1);
    }

    let character: u8 = unsafe { *buffer.as_ptr() } as u8;
    let _ = 255u8 - character; // The line problem.

    0
}

#[lang = "eh_personality"] extern fn eh_personality() {}
#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} }
  • Cargo.toml:
[package]
name = "nostd"
version = "0.1.0"
authors = ["adjivas <[email protected]>"]

[profile.release]
lto = true

[dependencies.libc]
version = "*"
default-features = false

I expected to see this happen:

$ cargo rustc -- -Z force-overflow-checks=off
    Updating registry `https://github.com/rust-lang/crates.io-index`
 Downloading libc v0.2.9
   Compiling libc v0.2.9
   Compiling nostd v0.1.0 (file:///home/travis/build/adjivas/nostd)
The command "cargo rustc -- -Z force-overflow-checks=off" exited with 0.
$ nm ./target/debug/nostd | grep " U "
                 U __libc_start_main@@GLIBC_2.2.5
                 U memset@@GLIBC_2.2.5
                 U read@@GLIBC_2.2.5

Instead, this happened:

$ cargo rustc -- -Z force-overflow-checks=off
    Updating registry `https://github.com/rust-lang/crates.io-index`
 Downloading libc v0.2.9
   Compiling libc v0.2.9
   Compiling nostd v0.1.0 (file:///home/travis/build/adjivas/nostd)
error: linking with `cc` failed: exit code: 1
note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/travis/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/travis/build/adjivas/nostd/target/debug/nostd.0.o" "-o" "/home/travis/build/adjivas/nostd/target/debug/nostd" "-Wl,--gc-sections" "-pie" "-nodefaultlibs" "-L" "/home/travis/build/adjivas/nostd/target/debug" "-L" "/home/travis/build/adjivas/nostd/target/debug/deps" "-L" "/home/travis/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "/home/travis/build/adjivas/nostd/target/debug/deps/liblibc-00ad8a4a4c184a2d.rlib" "/home/travis/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-18402db3.rlib" "-l" "compiler-rt"
note: /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x25): undefined reference to `__libc_start_main'
/home/travis/build/adjivas/nostd/target/debug/nostd.0.o: In function `nostd::start::hd7b9118d84e1f51b':
/home/travis/build/adjivas/nostd/src/main.rs:8: undefined reference to `memset'
/home/travis/build/adjivas/nostd/src/main.rs:12: undefined reference to `read'
@steveklabnik
Copy link
Member

@alexcrichton seemed to think this might be a bug in the libc crate around configs

@Aatch
Copy link
Contributor

Aatch commented Apr 1, 2016

Looks like it's not linking to libc (the real one). There's a nodefaultlibs in the link command, but no -l c.

@adjivas adjivas changed the title nostd libc not link with syscalls nostd libc not link Apr 1, 2016
alexcrichton added a commit to alexcrichton/libc that referenced this issue Apr 1, 2016
This crate currently relies on libraries being linked to come from libstd, but
if `use_std` is disabled that's not true! In that case we need to pull in the
libraries ourselves.

Closes rust-lang/rust#32661
@alexcrichton
Copy link
Member

Yes this is currently a bug with the libc crate, and I've opened a PR: rust-lang/libc#249

bors added a commit to rust-lang/libc that referenced this issue Apr 1, 2016
Link to libraries when disabling `use_std`

This crate currently relies on libraries being linked to come from libstd, but
if `use_std` is disabled that's not true! In that case we need to pull in the
libraries ourselves.

Closes rust-lang/rust#32661
alexcrichton added a commit to alexcrichton/libc that referenced this issue Apr 4, 2016
This crate currently relies on libraries being linked to come from libstd, but
if `use_std` is disabled that's not true! In that case we need to pull in the
libraries ourselves.

Closes rust-lang/rust#32661
bors added a commit to rust-lang/libc that referenced this issue Apr 4, 2016
Link to libraries when disabling `use_std`

This crate currently relies on libraries being linked to come from libstd, but
if `use_std` is disabled that's not true! In that case we need to pull in the
libraries ourselves.

Closes rust-lang/rust#32661
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

No branches or pull requests

4 participants