by femto · 13y ago · 0 comments
$ touch a.c
$ gcc -c a.c
$ ld a.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400078
$ ./a.out
Segmentation fault
gcc -nostdlib ./empty.c -o ./empty
Edit: This one actually runs correctly:
$ touch empty.c
$ gcc -static -nostartfiles ./empty.c -e_exit -o ./empty
$ ./empty && echo $?
> 0$ rm -rf a
$ cp a.c a
$ chmod +x a
$ ./a
$
zsh: exec format error: ./aThe file is marked as executable, so the shell very reasonably tries to execute it by calling some well-chosen member of the exec() family (http://linux.die.net/man/3/exec).
The exec() function then needs to open and parse the file according to the formats it supports, which of course fails since the file is empty.
Do you simply mean that you expected the shell to validate this, and not try to execute empty files?
$ ld a.o
ld: warning: -macosx_version_min not specified, assuming 10.7
Undefined symbols for architecture x86_64:
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64gcc -nostdlib ./empty.c -e0 -o ./empty
$ ld a.o -U _main -U start