How to change the entry point of a C program compiled with gcc ?
Just like in the following code
#include<stdio.h>
int entry() //entry is the entry point instead of main
{
return 0;
}
How to change the entry point of a C program compiled with gcc ?
Just like in the following code
#include<stdio.h>
int entry() //entry is the entry point instead of main
{
return 0;
}
It's a linker setting:
the
-Wl,...
thing passes arguments to the linker, and the linker takes a-e
argument to set the entry functionIf you are on a system that provides GNU Binutils (like Linux), you can use the
objcopy
command to make an arbitrary function the new entry point.Suppose a file called
program.c
containing theentry
function:You first compile it using
-c
to generate a relocatable object file:Then you redefine
entry
to bemain
:Now use gcc to compile the new object file:
NOTE: If your program already has a function called
main
, before step 2, you can perform a separateobjcopy
invocation:You can modify your source code as:
The ".interp" section will let your program able to call external shared library. The exit call will make your entry function to exit program instead of return.
Then build the program as a shared library which is executable: