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-sym
flag ofobjcopy
on the object file can be used-nostartfiles
and-Wl,--entry=<function name>
flags ofgcc
can be used.- compiler directive can be used as
-D<function name>=main
in thegcc
command 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 hello
or the gcc
can be used as follows
gcc -nostartfiles -Wl,--entry=say_hello -o hello hello.c
or
gcc -Dsay_hello=main -o hello hello.c