The word main
is a legal name for any variable. The typical use case is to provide a function of the name main
to a compiler, which compiles it to an object file, which in turn is linked to with crt0.o
that provides initialization for run-time (stack allocation etc.) and jumps to the label main
.
In C object files the symbols are not associated with prototypes and the linker succeeds in linking a global variable int main;
as the main program to be jumped to. This program, however, is garbage. It’s most likely initialized as zeros, but soon the processor encounters either a random instruction that accesses memory outside the programs allocated data space (stack + heap), or the instruction flow reaches the limits of the reserved code space.
Both will cause a segmentation fault. And actually, if the system runs on an architecture with eXecution flags, the program segfaults at the first attempt to jump to data segment or page without execution permission.
Further reading to support the discussion in the comments: Data Execute Prevention, NX_bit