To change the entry point of the Program

I want to change the entry point of the program. Here is the sample code for which works fine in DEC.

void __init_mystart ()
{
printf ("The Execution Point starts Here\n");
}

main ()
{
printf ("Inside Main\n");
}

This will work fine in DEC because there is option in ld if the function starts with __init DEC will call the function before main. If i want to do that in Solaris I have to complie the with -e options i.e, -e __init_mystart. If i run the program it is give Segmentation Fault core dump
Can someboddy help me.

Thanks...Srini I guess something is wrong with ld.so.1 or the crtx.o's (runtime loader linker).

Here is my experience on the issue:
1:: Binary img compiles good with -e
2:: Entire program executes fine.
3:: On exit, when the pgm finished execution, there is coredump.

crt1.o actually defines main as external and has 'call main' in _start. After main is finished executing it does exit(main) and then
_exit()'s from crt1.o. So symbols like _start, _fini, main and _init are defined in crt1.o, crtn.o and crti.o for the compiler
back-end driver.

This I found in notes that I did not know until I read it today:
=> The search order for program entry point is as follows in solaris::
1. -e epsym [defined on ld command line]
2. main
3. _start
4. MAIN
5. The first text procedure block, if none of the above.

In case of 5, in simple words, compiler just refers to the first function as program entry point if none of the above are present
even without -e, I checked it out, but after execution I got the core dump.

Problem of coredump seems to be in the exit routine that does exit(main). And since main is not loaded as pgm entry point the
call fails at execution address 0x0().

This is all I could help you with solaris right now. Let me see if I can find you more information on this issue. This may very well
be compiler backend bug. You may want to log it with your local Sun support center.

To see what I'm talking, play with cc using the '-#' option. This will set the verbose mode in SUNWspro compiler product, and
you can actually see what the compiler is doing. You can also try difference compiler options from ther verbose output.

Have fun

I actually stumbled upon this information while investigating other stuff.

Solaris C compiler (SUNWSpro pkg) defines 2 #pragma preprocessor directives that can change the entry and exit point of the program.

#pragma init (function ,[...function])
#pragma fini (function, [...function])

You can have a list here. Sample program below will explain better...

#include <stdio.h>

void ajay();

#pragma init (ajay)
#pragma fini (ajay)

main() {
printf("Now calling main\n");

}

void ajay() { printf("Hello world\n"); }

Hope this solves your problem.

Have fun.

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.