1

Here is my program:

void f(){
    printf("here\n");
}
void main(){
}

I compiled it with gcc -ef *.c. When I run it gives the following output:

here
Segmentation fault (core dumped)

The backtrace shows this:

#0  0x0000000000000001 in ?? ()
#1  0x00007fffffffdf9e in ?? ()
#2  0x0000000000000000 in ?? ()

What is the problem here?

nomem
  • 1,568
  • 4
  • 17
  • 33
  • When you see a useless backtrace like that, you should add `-g` to our compiler options and try again. – John Zwinck Dec 20 '15 at 10:42
  • @JohnZwinck I tried with -g too. It shows the same backtrace. Could you suggest anything more? – nomem Dec 20 '15 at 10:47
  • 1
    You are compiling *all* C files in the current directory with `*.c`. We don't know what you have in those files. Please post a [mcve]. – P.P Dec 20 '15 at 10:51
  • @l3x I am compiling the only file in the directory. You can replace * with and it gives the same result. I have alread given the minimal verifiable code. – nomem Dec 20 '15 at 10:55
  • 1
    Maybe this will help? http://stackoverflow.com/questions/7494244/how-to-change-entry-point-of-c-program-with-gcc – John Zwinck Dec 20 '15 at 10:56
  • @JohnZwinck I tried both `-Wl` and 'interp'. It is still the same. – nomem Dec 20 '15 at 11:15
  • is there any reason to prefer `void main() {}` over `int main(void) {}` ? (short answer: there isn't) – wildplasser Dec 20 '15 at 11:32

1 Answers1

2

The problem is that you're changing the default entry point of the executable, which is typically _start. _start does some things for you: setting up the C runtime environment, grabbing argv, and calling libc_start_main which will exit with process using exit() when main() returns (among many other things). Your program needs to call exit() to properly leave the process... otherwise, you're returning to nowhere.

Just for reference, you can find the exit() call in glibc here.

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79