using write() instead of puts() in a trivial program?

E

Erik S. Bartul

perhaps the programmer intended to write this simple program in a complex
manner, simply because it is a simple program. :) why you ask? because he
can.
 
?

=?iso-8859-1?q?Jos=E9_de_Paula?=

Em Mon, 12 Jan 2004 21:10:59 +0000, Alexander Bartolich escreveu:


write(2) is a system call.
_exit(2) is a system call.


By using nothing but system calls the total size of the resulting
binary can be minimized. See option -nostdlib of gcc.

That's it. /sbin/nologin isn't linked to any library, not even libc. The
result?

$ ls -lh /sbin/nologin
-r-xr-xr-x 1 root wheel 1,9K 4 Jun 2003 /sbin/nologin

FreeBSD 5.1 i386. A hand-tweaked assembly program could hardly be smaller.
Well, it could, but that's another topic.
 
D

Dan Pop

In said:
Em Mon, 12 Jan 2004 21:10:59 +0000, Alexander Bartolich escreveu:



That's it. /sbin/nologin isn't linked to any library, not even libc.

Then, the linker would complain about the unresolved write and _exit
symbols. Even this program requires minimal library support, write() and
_exit() usually being wrappers to syscall().

Dan
 
D

Dan Pop

In said:
1900 bytes just to make two system calls?

It could be made much smaller :)

The size of the smallest C program is determined by the size of the
startup module:

fangorn:~/tmp 110> cat test.c
int main(void)
{
return 0;
}
fangorn:~/tmp 111> gcc -s test.c
fangorn:~/tmp 112> ls -l a.out
-rwxr-xr-x 1 danpop sysprog 2912 Jan 19 15:50 a.out*

If I want to get a smaller binary, that still works, I have to start
cheating:

fangorn:~/tmp 123> cat cheat.c
int main()
{
_exit(0);
}
fangorn:~/tmp 124> gcc -s -static -nostdlib cheat.c -lc
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 08048080
fangorn:~/tmp 125> ls -l a.out
-rwxr-xr-x 1 danpop sysprog 740 Jan 19 15:56 a.out*
fangorn:~/tmp 126> ./a.out
fangorn:~/tmp 127> echo $status
0

The ld warning was caused by the missing startup module, but this
minimal program doesn't really need it. The first program above, however,
does:

fangorn:~/tmp 128> gcc -s -static -nostdlib test.c -lc
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 08048080
fangorn:~/tmp 129> ./a.out
Segmentation fault
fangorn:~/tmp 130> ls -l a.out
-rwxr-xr-x 1 danpop sysprog 588 Jan 19 16:00 a.out*

The missing syscall support from the statically linked executable
made it both shorter and non-functional.

Dan
 
G

Grumble

Dan said:
The size of the smallest C program is determined by the size of the
startup module:

And the size of the smallest ELF binary is determined by the size of
the ELF header ;-)

(Jose mentioned hand-tweaked assembly under FreeBSD.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top