Problems creating tiny, statically linked application

Problems creating tiny, statically linked application

Post by Mats Liljegre » Thu, 01 Jul 1999 04:00:00

I want to create a very small (preferably ~20KB) statically linked
application, which is used during boot sequence. This application will
be on a minimal distribution of a Linux system.

When I linked my application with -static and -s parameters, the size
was around 120KB. Of those, my application consists of around 20KB, and
uses exit(), malloc() and free(). Of those, I can do without malloc()
and free() with a little re-design.

But somehow, I doubt that the code for exit(), malloc() and free() is
100KB big! So I made a map file to see what happens. Well, the entry and
exit code needs the hole file library (which I don't use) and also
printf() with all the things that takes with it!!

I looked at the sources for the library (glibc-2.1.1pre3) and tried to
see if I could do my own version, with just the entry and exit code. But
I don't understand the code.

As for entry code, it seems to be like this:
ctr0.o -> _start (/sysdeps/i386/elf/start.S)
  _start -> __libc_start_main (/sysdeps/generic/libc_start.c)
  __libc_start_main -> __libc_init_first (/sysdeps/i386/init-first.c)
  __libc_start_main -> (*init)()
  __libc_start_main -> (*main)()
  __libc_start_main -> exit()
  exit() -> _cleanup()
  exit() -> _exit()

As far as I can see, I could redefine __libc_start_main to make it skip
the call to __libc_init_first, as I do not have any use of argc, argv or
argp. So far so good.

But I cannot find _exit()!! I'm running on a elf i386 Linux platform,
and there seems to be _exit() defined for all other platforms except

So my questions are:
1) Is there something bad about skipping __libc_init_first in my case?
2) Where is i386 version of _exit()?

Thanks in advance for any help!



1. Creating tiny applications

I try to do a minimal application, but I have a problem with linking. As
my application has to be self-contained (I have only the kernel when my
application runs), it has to be statically linked. This is my problem.
Even though I only use malloc, free and exit from the standard library,
the static link adds something like 100KB. This is way too much!

I use the -s option when linking (which reduced the size of my
application from about 400KB to something like 120KB...). But my
application is still too large.

If I use the option -nostdlib, the linker cannot find _start, malloc or
free. I know of a way avoid malloc and free (I think...), but I
apparently have to have the startup code which GCC adds.

So my questions is:

1) Where is the startup-code? I can't find it anywhere...
2) What libraries does GCC exclude when I give the -nostdlib option?

If anyone wonders, my application will be the first thing running after
the kernel has be loaded, and the application will be responsible for
creating the root file system. By having this application I can compress
my root filesystem with bzip2 instead of gzip, as bzip2 compress better.
My application will run as an initrd application.

Thanks in advance for any help!


2. man pages

3. Linking an X/Motif application STATICALLY

4. getting notified on file change?

5. Creating a statically linked executable

6. Sound card!!

7. Converting Dynamicly linked programs to statically

8. Unable to type '#' under xterm

9. Statically Linking a Dynamically Linked Binary Image

10. Statically re-linking a dinamically linked apps?

11. statically linked tcl conflict with dynamically linked tcl error

12. Problem to create statically bound binary on AIX 3.2.5

13. problems creating a statically bound executable