Skip to content

Minimal program w/o std, libc creates binary that seg faults #16089

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
mikedilger opened this issue Jul 29, 2014 · 1 comment
Closed

Minimal program w/o std, libc creates binary that seg faults #16089

mikedilger opened this issue Jul 29, 2014 · 1 comment

Comments

@mikedilger
Copy link
Contributor

I'm trying to create a program without std and without libc. Based on this page: http://doc.rust-lang.org/guide-unsafe.html (and elsewhere). I still cannot get a minimal program to work. It compiles, but the binary segmentation faults (on x86_64 linux).

Compiled with: rustc --crate-type=bin -C link-args="-nostdlib" -C relocation-model=static start.rs

Program is:

#![feature(lang_items)]
#![no_std]

extern crate core;
extern crate rlibc;

#[lang = "stack_exhausted"]
extern fn stack_exhausted() {}

#[lang = "eh_personality" ]
extern fn eh_personality() {}

#[lang = "begin_unwind" ]
unsafe extern fn begin_unwind(args: &core::fmt::Arguments,
                       file: &str,
                       line: uint) -> ! {
    loop {}
}

#[start]
fn main(argc: int, argv: *const *const u8) -> int
{
    0
}
@alexcrichton
Copy link
Member

This is likely not a rust-specific problem but rather how you're compiling and driving the linker. Most linux distributions need libc to be linked in to have the necessary startup routines, and one may likely be missing from the binary.

For example, if you compile a similar C program you get an odd error:

$ cat foo.c
int main() { return 0; }
$ gcc foo.c -o foo -nostdlib
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400144
$ ./foo
zsh: segmentation fault (core dumped)  ./foo

Even if the main symbol is switched to _start, this still segfaults. In general a bare bones binary such as this is not meant to run on the host system as it's missing necessary library support.

I don't think this is related to rust, however, so I'm going to close this for now.

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

2 participants