This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: is main() provided by a library valid C ?


> 
> On Thu, May 19, 2005 at 10:40:07PM +0200, Joakim Tjernlund wrote:
> > If my memory serves me right, to build libc w/ -z defs, main needs to be declared
> > weak in libc.
> 
> Incorrect.  Libc doesn't need to mention main at all.  Please look at
> glibc's CRT files.

Ahh, now I understand why you pass main as argument from start.S to libc, right?
Is this the only reason you do that or is there more to it?

While we are at the at it, I don't understand the alignment code in start.S for i386
This code:
/* Before pushing the arguments align the stack to a 16-byte
        (SSE needs 16-byte alignment) boundary to avoid penalties from
        misaligned accesses.  Thanks to Edward Seidl <seidl@janed.com>
        for pointing this out.  */
        andl $0xfffffff0, %esp

Aligns the stack to 16-byte, then the code below does 8 pushl at which point the
stack is 16-byte aligned again and finally a call to __libc_start_main. The call to 
__libc_start_main will add 4 bytes to the stack ptr and now the stack isn't 16-byte
aligned when __libc_start_main starts to execute. Is this the way it is supposed to work?

        pushl %eax                /* Push garbage because we allocate
                                   28 more bytes.  */

        /* Provide the highest stack address to the user code (for stacks
           which grow downwards).  */
        pushl %esp

        pushl %edx                /* Push address of the shared library
                                   termination function.  */

#ifdef SHARED
        /* Load PIC register.  */
        call 1f
        addl $_GLOBAL_OFFSET_TABLE_, %ebx

        /* Push address of our own entry points to .fini and .init.  */
        leal __libc_csu_fini@GOTOFF(%ebx), %eax
        pushl %eax
        leal __libc_csu_init@GOTOFF(%ebx), %eax
        pushl %eax

        pushl %ecx                /* Push second argument: argv.  */
        pushl %esi                /* Push first argument: argc.  */

        pushl BP_SYM (main)@GOT(%ebx)

        /* Call the user's main function, and exit with its value.
           But let the libc call main.    */
        call BP_SYM (__libc_start_main)@PLT


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]