execute a different function before main()

A

aarklon

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....??????
 
R

Richard Bos

my question is there a standard way in ANSI-C
to execute a different function before main() is called....??????

No. But why would you? Call that function as the first thing you do in
main() itself. The only reason I can think of why that wouldn't work is
when you call main() recursively, and
a) that's a very bad idea in the first place;
b) the trivial workaround is left as an exercise for the reader.

Richard
 
E

Eric Sosman

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."

2) Use an editor to change the name of your existing
main() to something else, and to change the name of your
desired startup function to main().

(Yes, this is somewhat tongue in cheek. But there's a
serious point behind it: If you can modify the program source,
you are in control of what main() does. You can therefore
make it do whatever you wanted this other, magical function
to do -- so you don't need any magic.)
 
M

Morris Dovey

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..???

Perhaps, but your co-workers may not be happy...
 
A

aarklon

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..???
 
S

santosh

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..???

If you have the source, yes. But you'll need to recompile and reinstall
the library. Just what is your actual problem that apparently seems to
be forcing you to massive hacks?
 
W

Walter Roberson

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..???

It depends on the environment.

[OT]

For example on SGI IRIX, there is the following linker option:

-init ifuncsymbol
Puts a call to ifuncsymbol into the code that rld(5)
exercises before the main program begins. The ifuncsymbol
contains initialization code to be called before control is
passed to main. In the case of a DSO opened with dlopen(3C)
or sgidladd(3C) an initialization function is called after
the DSO is loaded but before it is referenced.
 
G

George Peter Staplin

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,


George
 
A

aarklon

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
 
S

santosh

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.
 
K

Kenneth Brody

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 [...]
my question is there a standard way in ANSI-C
to execute a different function before main() is called....??????

I assume you want something equivalent to atexit(), which lets you
specify functions to call after exit() [or a return from main()]?
Obviously, unlike atexit(), you can't have your function call a
function to tell it to do so.

You can't do this in any standard way. The closest thing you can
do is to simply call them at the very top of main().

Can you specify more than one "#pragma startup", and call more than
one function before main()?

[Warning: Untested code follows. Typos may be present. I leave it
as an exercise for the reader to fix them.]

If you only need a single function, why not declare some global
variable:

extern void (*RunAtStartup)(void);

At the start of main, add:

if ( RunAtStartup != NULL )
(*RunAtStartup)();

Then, in whatever module needs this special startup function, you
include:

extern void TheFunctionINeed(void);
void (*RunAtStartup)(void) = TheFunctionINeed;

Unfortunately, this only works if there is only a single function
that needs to be run. If you need more than one, then you need to
build an array of function pointers, all in one place, in which
case you might as well just call the functions.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top