help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing entry point function


From: Paul Pluzhnikov
Subject: Re: Changing entry point function
Date: Wed, 08 Aug 2007 18:15:05 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

Premkumar <contactprem@gmail.com> writes:

> g++ -Wl,-emymain__Fv  a.o
>
> Now the linker says:
> /usr/lib/crt1.o(.text+0x18): In function `_start':
> : undefined reference to `main'
> collect2: ld returned 1 exit status
>
> I also tried the following
> g++ -Wl,--verbose -Wl,-emymain__Fv -Wl,--defsym -Wl,start=mymain__Fv a.o
> g++ -Wl,--verbose -Wl,-emymain__Fv -Wl,--defsym -Wl,_start=mymain__Fv a.o
>
> What mistake am I making here ?

Your understanding of what happens between execve() creating a new
process and the first instruction of main, is severely lacking.

You appear to think that that's where the a.out starts running.
In fact, on Linux/x86, more than 10,000 instructions execute before
the first instruction of main is hit. Majority of these are inside of
the dynamic loader, but many others are in the "C runtime startup"
crt0.o, which is added to your link line by gcc/g++ automatically.

Run "nm crt0.o" -- you'll see that it defines _start and calls main.
You'll need to disassemble _start to see what all the things that
must be done before main on your platform.

You can ask gcc not to add crt0.o (-nostdlib), and make liker set
entry point to mymain(), but the result will likely crash either
before or after mymain. For example, on my system:

    $ g++ -g junk.cpp -nostdlib -lstdc++ -lc -Wl,-emymain__Fv
    $ ./a.out
    my-main
    Segmentation fault (core dumped)

This particular crash is happening because mymain() returns to
address 0x00000001, which is actually argc, pushed onto stack by
the kernel.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]