lpsantil / rt0

A minimal C runtime for Linux i386 & x86_64
BSD 2-Clause "Simplified" License
579 stars 30 forks source link

Add --whole-archive link flag #1

Closed emaste closed 9 years ago

emaste commented 9 years ago

Building on FreeBSD I get:

cc t/hello.o -L. -lrt0 -Os -nostdlib -fomit-frame-pointer -s -o t/hello.exe
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000004000e8

Nothing references _start to bring 00_start.o in.

Explicitly adding the --whole-archive flag forces the four objects to be linked into the binary:

%  cc t/hello.o -L. -Wl,--whole-archive -lrt0 -Os -nostdlib -fomit-frame-pointer -s -o t/hello.exe
% t/hello.exe 
Hello World!
lpsantil commented 9 years ago

Curious, does LDFLAGS="-s -Wl,-e_start" make runtest work as well? Do the *BSDs setup the stack/registers the same way as Linux upon loading an ELF image into memory?

emaste commented 9 years ago

-Wl,-e_start doesn't help, but forcing the undefined reference with -Wl,-u_start does. I tried -Wl,--whole-archive since the C startup routines are traditionally linked explicitly as .os on the command line.

Anyhow, t/test.exe does work too. The notable difference is the way errors are returned from system calls: Linux returns negative errno, BSD sets the carry flag and returns errno. Fortunately the sample code here doesn't really care about errno :)

lpsantil commented 9 years ago

Awesome. Thanks for the feedback. I'll add this change to the Makefile. Eventually, I'll need to setup a FreeBSD VM. I've only ever really played with NetBSD but that was on an old Dreamcast and as a stripped down router.