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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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
If you click on a user's profile, such as joeyBig, you get a pulldown menu where you can see their profile. In the profiles it shows the last activity by that user. In this case joeyBig was last seen logged into LQ back in 2004. And also were the most recent replies to this thread.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.