#else /* Nothing fancy, just call the function. */ result = main (argc, argv, __environ MAIN_AUXVEC_PARAM); #endif
exit (result);
_start hacking
Compiler
在Unix-like操作系统中,_start符号被作为程序入口点的默认值。
我们是不是可不可以不用这个默认值呢
#include<stdio.h> intmy_main() { printf("This is a program without a main() function! printf"); return0; }
gcc -nostartfiles -e my_main -g -o test test.c
-nostartfiles:Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib, -nolibc, or -nodefaultlibs is used.
-e:Specify that the program entry point is entry. The argument is interpreted by the linker; the GNU linker accepts either a symbol name or an address.
grxer@grxer /m/h/S/c/p/io_file> gcc -nostartfiles -e my_main -g -o test test.c grxer@grxer /m/h/S/c/p/io_file> ./test fish: “./test” terminated by signal SIGSEGV(Address boundary error)
#include<stdio.h> intmy_main() { puts("This is a program without a main() function! printf"); return0; }
grxer@grxer /m/h/S/c/p/io_file> gcc -nostartfiles -e my_main -g -o test test.c grxer@grxer /m/h/S/c/p/io_file> ./test This is a program without a main() function! puts fish: “./test” terminated by signal SIGSEGV(Address boundary error)
#include<stdio.h> intmy_main() { printf("This is a program without a main() function! printf"); exit(0); }
如我们不用return返回,而是直接调用exit终止应该就可以正常刷新缓冲区,并退出
grxer@grxer /m/h/S/c/p/io_file> gcc -nostartfiles -e my_main -g -o test test.c grxer@grxer /m/h/S/c/p/io_file> ./test This is a program without a main() function! printf
printf也被刷新缓冲区输出了,正常退出
我们也可以利用默认_start入口点去做
#include<stdio.h> #include<stdlib.h> void _start() { int ret = my_main(); exit(ret); } intmy_main() {
puts("This is a program without a main() function!"); } //gcc -nostartfiles -g -o test test.c
grxer@grxer /m/h/S/c/p/io_file> gcc -nostartfiles -g -o test test.c test.c: In function ‘_start’: test.c:4:13: warning: implicit declaration of function ‘my_main’ [-Wimplicit-function-declaration] int ret = my_main(); ^ grxer@grxer /m/h/S/c/p/io_file> ./test This is a program without a main() function!