By default compilers (gcc and llvm) looks for the function main for the entry point.
To have a different function as an entry point,
--redefine-symflag ofobjcopyon the object file can be used-nostartfilesand-Wl,--entry=<function name>flags ofgcccan be used.- compiler directive can be used as
-D<function name>=mainin thegcccommand can be used.
for example,
if the c code looks like
// hello.c
#include <stdio.h>
int say_hello(int argc, char **argv){
if (argc >= 2){
printf("Hello %s\n", argv[1]);
}
else{
printf("Hell oh world!\n");
}
return 0;
}To compile and execute this code where say_hello is used as the entry point.
# create an object file
gcc -c hello.c -o hello.o
# redefine the symbol for main (entry function)
objcopy --redefine-sym say_hello=main hello.o
# create a binary
gcc hello.o -o helloor the gcc can be used as follows
gcc -nostartfiles -Wl,--entry=say_hello -o hello hello.cor
gcc -Dsay_hello=main -o hello hello.c