cgccmakefilelinkerlibc

Compiling without a standard library but with the _start label (entry point)


I am building my own 32-bit x86 OS in which I load user programs from ELF files. When compiling the user C programs I use -nostdlib to exclude GLIBC and link my own LIBC instead. The problem is that by doing so, the program compiles without a _start label, causing the entry (defined in the ELF header) to point at the first piece of code in the file and not the actual entry of the program.

I am not sure how can I compile without the standard library but still have a valid entry point for the program.

When compiling with the -nostdlib flag I get:

/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000008049000

ELF file:

  Entry point address:               0x8049000

How can _start be generated with no stdlib? Should and can I write it myself and link it at compile time with the user program?


Solution

  • My confusion was driven from the source of the _start function. Apparently, the _start function is part of the standard library, located in the crt0.S file.

    crt0 (also known as c0) is a set of execution startup routines linked into a C program that performs any initialization work required before calling the program's main function.

    When compiled with the -nostdlib flag, GCC removes GLIBC from the program together with its _start routine - causing the linker to raise an error telling it couldn't find a symbol to set the entry to.

    /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000008049000

    I fixed this by creating my own crt0.S file in my libc and in it a custom _start function to be linked with the user program.

    So to my questions I would answer:

    How can _start be generated with no stdlib? Should and can I write it myself and link it at compile time with the user program?

    You should supply your own definition of the _start function in the standard library of your OS and link it with the user program.