Re: execute a different function before main()
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Wed, 12 Mar 2008 14:43:05 +0530
aarklon@xxxxxxxxx wrote:
On Mar 12, 7:28 am, George Peter Staplin
<georgepsSPAMME...@xxxxxxxxxxxx> wrote:
aark...@xxxxxxxxx wrote:
On Mar 11, 9:34 pm, Eric Sosman <Eric.Sos...@xxxxxxx> wrote:
aark...@xxxxxxxxx wrote:
Hi all,
recently i read the following passage in a book
it is a compiler dependent thing if some feature is available
that allows you to execute some function before main() is
called. In turbo C compiler, there's one such fearture....the
#pragma startup directive.
I m giving an example of its use:-
#include<stdio.h>
void fun();
#pragma startup fun
int main()
{
printf("Arun");
}
void fun()
{
printf("Anand \n");
}
The o/p of the program given above will be :- Anand
my question is there a standard way in ANSI-C
to execute a different function before main() is
called....??????
Yes. Two ways, in fact:
1) Run your program in a freestanding environment, where
"the name and type of the function called at program startup
are implementation-defined."
is it possible to modify the start up code in a hosted environment
so as to change the name and type of the function called at
program
startup..???
Yes, it's possible with a hosted environment but not from standard C.
When a program is loaded with most (Windows and Unix-like) systems,
the OS jumps to an offset for _start in assembly code.
If you're using gcc in Windows or a POSIX system you can see what
startup files gcc links with via: gcc -v somefile.c
The startup files are typically named like crt0.o. They are
implemented in assembly code usually and the various crt*.o do tasks
like implementing exit(main(argc,argv)); and setting up the
environment,
Can any one the general outline of the life cycle of the execution a
C program, in similar lines to the "LInux boot sequence"
given here
http://www.debianhelp.co.uk/boot.htm
No, because different implementations will do this differently. Even for
Linux the start-up sequence is very different under x86 and Itanium.
For hosted implementations the system will typically jump into a
predefined entry point in your program. This normally enters the
initialisation code of your C runtime library that will linked in with
your program. Often the entry point is specified in source with the
label _start:. This code does some system specific initialisation and
ten calls the function called main, which of course, then enter your
program. After you return from main, fall of it's end, or call exit,
_Exit or abort, execution once again enters the shutdown code in your C
library, which may do a whole host of things like flushing streams,
closing file handles, calling handlers registered with atexit etc.,
before finally handing control over to the host system.
To know the details you need to look at the source for your system C
runtime library. The details are very system specific and no one
explanation will suffice.
.
- References:
- execute a different function before main()
- From: aarklon
- Re: execute a different function before main()
- From: Eric Sosman
- Re: execute a different function before main()
- From: aarklon
- Re: execute a different function before main()
- From: George Peter Staplin
- Re: execute a different function before main()
- From: aarklon
- execute a different function before main()
- Prev by Date: Re: Topicality was: What is a stack frame?
- Next by Date: Re: Topicality was: What is a stack frame?
- Previous by thread: Re: execute a different function before main()
- Next by thread: Re: execute a different function before main()
- Index(es):
Relevant Pages
|
|