LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to Override main function in C (https://www.linuxquestions.org/questions/programming-9/how-to-override-main-function-in-c-259390/)

joeyBig 11-25-2004 11:56 PM

How to Override main function in C
 
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.

vharishankar 11-26-2004 12:20 AM

I don't think you can. However, you can workaround in this fashion

Code:

// ...
// ...
int mymain ()
{
      // your program here
      //...
      //...
      return code;
}

int main ()
{
      return ( mymain () );
}


joeyBig 11-26-2004 12:35 AM

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.

regards,
joe.

randyding 11-26-2004 01:14 AM

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?

vharishankar 11-26-2004 01:45 AM

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?

jlliagre 11-26-2004 02:02 AM

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


Anshu Prince 03-19-2018 01:12 AM

using #pragma directive
 
#include<stdio.h>
#include<conio.h>

void School();
void College() ;

#pragma startup School 105
#pragma startup College
#pragma exit College
#pragma exit School 105

void main(){
printf("\nI am in main");
getch();
}

void School(){
printf("\nI am in School");
getch();
}

void College(){
printf("\nI am in College");
getch();
}
Output :

I am in College
I am in School
I am in main
I am in School
I am in College

NevemTeve 03-19-2018 04:19 AM

It this a nonstandard compiler-extension, or a Microsoft-specific feature?

rtmistler 03-19-2018 06:37 AM

Hi Some things to check Anshu Prince,

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.

However, fine answer for the question. :)


All times are GMT -5. The time now is 04:56 AM.