Why does declaring main as an array compile?

前端 未结 6 1933
一整个雨季
一整个雨季 2021-01-31 07:35

I saw a snippet of code on CodeGolf that\'s intended as a compiler bomb, where main is declared as a huge array. I tried the following (non-bomb) version:



        
相关标签:
6条回答
  • 2021-01-31 07:42
    const int main[1] = { 0xc3c3c3c3 };
    

    This compiles and executes on x86_64... does nothing just return :D

    0 讨论(0)
  • 2021-01-31 07:46

    The problem is that main is not a reserved identifier. The C standard only says that in hosted systems there is usually a function called main. But nothing in the standard prevents you from abusing the same identifier for other sinister purposes.

    GCC gives you a smug warning "main is usually a function", hinting that the use of the identifier main for other unrelated purposes isn't a brilliant idea.


    Silly example:

    #include <stdio.h>
    
    int main (void)
    {
      int main = 5;
      main:
    
      printf("%d\n", main);
      main--;
    
      if(main)
      {
        goto main;
      }
      else
      {
        int main (void);
        main();
      }
    }
    

    This program will repeatedly print the numbers 5,4,3,2,1 until it gets a stack overflow and crashes (don't try this at home). Unfortunately, the above program is a strictly conforming C program and the compiler can't stop you from writing it.

    0 讨论(0)
  • 2021-01-31 07:47

    It's because C allows for "non-hosted" or freestanding environment which doesn't require the main function. This means that the name main is freed for other uses. This is why the language as such allows for such declarations. Most compilers are designed to support both (the difference is mostly how linking is done) and therefore they don't disallow constructs that would be illegal in hosted environment.

    The section you refers to in the standard refers to hosted environment, the corresponding for freestanding is:

    in a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.

    If you then link it as usual it will go bad since the linker normally has little knowledge about the nature of the symbols (what type it has or even if it's a function or variable). In this case the linker will happily resolve calls to main to the variable named main. If the symbol is not found it will result in link error.

    If you're linking it as usual you're basically trying to use the compiler in hosted operation and then not defining main as you're supposed to means undefined behavior as per appendix J.2:

    the behavior is undefined in the following circumstances:

    • ...
    • program in a hosted environment does not define a function named main using one of the specified forms (5.1.2.2.1)

    The purpose of the freestanding possibility is to be able to use C in environments where (for example) standard libraries or CRT initialization is not given. This means that the code that is run before main is called (that's the CRT initialization that initializes the C runtime) might not provided and you would be expected to provide that yourself (and you may decide to have a main or may decide not to).

    0 讨论(0)
  • 2021-01-31 07:47

    main is - after compiling - just another symbol in an object file like many others (global functions, global variables, etc).

    The linker will link the symbol main regardless of its type. Indeed, the linker cannot see the type of the symbol at all (he can see, that it isn't in the .text-section however, but he doesn't care ;))

    Using gcc, the standard entry point is _start, which in turn calls main() after preparing the runtime environment. So it will jump to the address of the integer array, which usually will result in a bad instruction, segfault or some other bad behaviour.

    This all of course has nothing to do with the C-standard.

    0 讨论(0)
  • 2021-01-31 07:55

    If you are interested how to create program in main array: https://jroweboy.github.io/c/asm/2015/01/26/when-is-main-not-a-function.html. The example source there just contains a char (and later int) array called main which is filled with machine instructions.

    The main steps and problems were:

    • Obtain the machine instructions of a main function from a gdb memory dump and copy it into the array
    • Tag the data in main[] executable by declaring it const (data is apparently either writable or executable)
    • Last detail: Change an address for actual string data.

    The resulting C code is just

    const int main[] = {
        -443987883, 440, 113408, -1922629632,
        4149, 899584, 84869120, 15544,
        266023168, 1818576901, 1461743468, 1684828783,
        -1017312735
    };
    

    but results in an executable program on a 64 bit PC:

    $ gcc -Wall final_array.c -o sixth
    final_array.c:1:11: warning: ‘main’ is usually a function [-Wmain]
     const int main[] = {
               ^
    $ ./sixth 
    Hello World!
    
    0 讨论(0)
  • 2021-01-31 07:55

    It only compiles because you don't use the proper options (and works because linkers sometimes only care for the names of symbols, not their type).

    $ gcc -std=c89 -pedantic -Wall x.c
    x.c:1:5: warning: ISO C forbids zero-size array ‘main’ [-Wpedantic]
     int main[0];
         ^
    x.c:1:5: warning: ‘main’ is usually a function [-Wmain]
    
    0 讨论(0)
提交回复
热议问题