ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi
main() is the important function without which a C pgm cannot be run.
I need to override the basic functionality of main() and
use my own function say, mymain() in replacement of main()
how to perform this.
thanks and regards,
joe.
hi shankar,
thanks for u'r reply,
but overriding main function can be done for sure.
I need to program in this format...
//...
joemain( ) { /* Here main should be replaced by my own function joemain */
//.....
functioncall ( );
//......
return val;
}
//.....
functioncall( ) {
//....
}
where in, if main() function is called, compiler may return 'main function not found / defined'.
(I am not sure of the above result, but sure that main can be overriden)
I suppose you understood.
Still, I don't believe you can do that. I'm not a gcc expert, but the embedded systems compilers I've used probably work in a similar manner to gcc. There our linker combined a small assembly file start.a with the program being linked, and it implicitly linked this start.a code in first, before your .o files, so its entry point was exactly at the top of the code segment. The start.a code did things like initialize the program stack, and then finally it jumped to main(). The linker resolved main because that symbol was object of the final jmp in the start.a file. I don't see how you can change that without changing the internals of the linker. What's to gain by doing this again?
This is not possible because the underlying linker (as randyding mentioned) will not resolve main () so you'll get a linker error while building your project. What's the big deal anyway to change main to joemain? It doesn't enhance your code in any way, does it?
Well, it can be done, but is obviously useless ...
Anyway, I liked the challenge, so here's how I did it:
I first build my own crt1.o file where the main symbol has been replaced by the new one.
I just patched crt1.o, replaced "main" by "pain" in its symbol table, and saved the new version in crtz.o.
Then I linked my object file with this crtz.o instead of the original crt1.o, that's it.
Code:
$ cat a.c
pain()
{
printf("ouch\n");
}
$ gcc -c a.c
$ /usr/bin/ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 \
/usr/lib/crti.o crtz.o \
/usr/lib/gcc-lib/i586-mandrake-linux-gnu/2.96/crtbegin.o \
-L/usr/lib/gcc-lib/i586-mandrake-linux-gnu/2.96 \
-L/usr/lib/ a.o -lgcc -lc -lgcc \
/usr/lib/gcc-lib/i586-mandrake-linux-gnu/2.96/crtend.o \
/usr/lib/crtn.o -o a
$ ./a
ouch
$ gdb a
GNU gdb 5.1.1
...
(gdb) b main
Function "main" not defined.
(gdb) b pain
Breakpoint 1 at 0x8048406
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.