40

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;
 }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
asitm9
  • 853
  • 4
  • 12
  • 21
  • @iandotkelly Not OP but I needed to use a checkpointing library which required me to change main() to something else ;) – Nubcake May 18 '17 at 16:52
  • @Nubcake Do you know the linker's option `--wrap=xxx`? It replaces all references of `xxx` by `__wrap_xxx` and all definitions of `xxx` by `__real_xxx`. I used it successfully for testing a `main()`. – the busybee Oct 29 '19 at 19:44

4 Answers4

49

It's a linker setting:

-Wl,-eentry

the -Wl,... thing passes arguments to the linker, and the linker takes a -e argument to set the entry function

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • 3
    This is what I got when I compiled a hello world C program with this option: `$ gcc -Wl,-emymain t27.c /usr/lib/gcc/i686-redhat-linux/4.8.2/../../../crt1.o: In function '_start': (.text+0x18): undefined reference to 'main'` – Lee Duhem Mar 08 '14 at 09:11
  • 5
    @LeeDuhem, this is because you link your program with a standard c runtime library. An entry point in a program linked with c runtime library is _start. Start has a reference to main() of your program (it expects your program to have main() func instead of your custom func). Try not to link to a crt and specify an entry point as your_main and see what happens. – PaulD Oct 01 '14 at 13:03
  • @LeeDuhem, -nostdlibs that flag is. – PaulD Oct 01 '14 at 13:08
  • 8
    `-nostartfiles` is the actual flag you want, this omits the crt*.o files containing _start, but still allows you to use a libc (unless you also use -nostdlib or -nodefaultlibs, in which case you can still manually specify them using -lc -lgcc etc...) – technosaurus Jul 17 '15 at 06:38
  • 1
    I believe there is a typo: should be `-Wl,--entry="MyCutomEntryFunction"` or `-Wl,-e="MyCutomEntryFunction"` – Lila Viollette Oct 22 '18 at 17:21
  • @RomainVIOLLETTE it works fine for me: https://stackoverflow.com/a/58613764/895245 on Ubuntu 20.10. – Ciro Santilli OurBigBook.com Jan 06 '21 at 11:31
9

You can modify your source code as:

#include<stdio.h>

const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";

int entry()  //entry is the entry point instead of main
{
   exit(0);
}

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:

$ gcc -shared -fPIC -e entry test_main.c -o test_main.so
$ ./test_main
Sam Liao
  • 43,637
  • 15
  • 53
  • 61
  • What is the calling convention of the `entry` function? Is the C-runtime initialized in this way? – harper Oct 20 '14 at 06:38
  • could you please explain what this mean *"build the program as a **shared library** which is **executable**"* How can it be both? – Nawaz Sep 18 '15 at 08:56
  • @harper: It's not a C compatible calling convention. The ELF entry point ABI has its primary argument in the stack pointer, pointing to an "array" consisting of argc, argv pointers, null, env pointers, null, aux vector table. Some archs have additional args in specific registers. – R.. GitHub STOP HELPING ICE Jun 06 '17 at 10:32
  • doesn't work.. I get: **cannot execute binary file: Exec format error** – Zibri Mar 14 '20 at 11:14
9

If 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 the entry function:

$ cat > program.c
#include <stdio.h>
int entry()
{
    return 0;
}
^D
  1. You first compile it using -c to generate a relocatable object file:

    $ gcc -c program.c -o program.o
    
  2. Then you redefine entry to be main:

    $ objcopy --redefine-sym entry=main program.o
    
  3. Now use gcc to compile the new object file:

    $ gcc program.o -o program
    

NOTE: If your program already has a function called main, before step 2, you can perform a separate objcopy invocation:

objcopy --redefine-sym oldmain=main program.o
Rudy Matela
  • 6,310
  • 2
  • 32
  • 37
3

Minimal runnable example and notes on other answers

main.c

#include <stdio.h>
#include <stdlib.h>

int mymain(void) {
    puts("hello");
    exit(0);
}

compile and run:

gcc -nostartfiles -Wl,--entry=mymain -o main.out main.c
# or -Wl,-emymain
./main.out 1 2 3

The notes:

  • without -nostartfiles, the link fails with:

    /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    collect2: error: ld returned 1 exit status
    

    presumably because the glibc setup code that runs before main in _start normally calls main.

  • command line arguments are not setup for you, presumably because they would be setup by the glibc code that runs before main, so trying to use them prints undefined values. I haven't found a method that works for them.

Tested in Ubuntu 20.10.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985