{"id":251908,"date":"2022-09-12T09:49:20","date_gmt":"2022-09-12T01:49:20","guid":{"rendered":"https:\/\/gulass.cn\/?p=251908"},"modified":"2022-09-05T04:50:15","modified_gmt":"2022-09-04T20:50:15","slug":"clever-way-to-implement-variety-main","status":"publish","type":"post","link":"https:\/\/gulass.cn\/clever-way-to-implement-variety-main.html","title":{"rendered":"\u5de7\u5999\u65b9\u6cd5\u6559\u4f60\u5b9e\u73b0\u591a\u79cdmain()"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5927\u5bb6\u90fd\u77e5\u9053\uff0c\u6211\u662f\u505a\u4e0a\u5c42\u5e94\u7528\u7684\uff0c\u5bf9\u5e95\u5c42\u4e0d\u662f\u5f88\u4e86\u89e3\uff0c\u66f4\u522b\u8bf4\u90a3\u5e2e\u4eba\u5728\u8ba8\u8bba\u5185\u6838\u7684\u65f6\u5019\uff0c\u6839\u672c\u63d2\u4e0d\u4e0a\u8bdd\u3002\u66f4\u591a\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u9ed8\u9ed8\u8bb0\u7b14\u8bb0\uff0c\u7d27\u8ddf\u5927\u4f6c\u4eec\u7684\u6b65\u4f10😁\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5927\u5bb6\u90fd\u77e5\u9053\uff0c\u6211\u662f\u505a\u4e0a\u5c42\u5e94\u7528\u7684\uff0c\u5bf9\u5e95\u5c42\u4e0d\u662f\u5f88\u4e86\u89e3\uff0c\u66f4\u522b\u8bf4\u90a3\u5e2e\u4eba\u5728\u8ba8\u8bba\u5185\u6838\u7684\u65f6\u5019\uff0c\u6839\u672c\u63d2\u4e0d\u4e0a\u8bdd\u3002\u66f4\u591a\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u9ed8\u9ed8\u8bb0\u7b14\u8bb0\uff0c\u7d27\u8ddf\u5927\u4f6c\u4eec\u7684\u6b65\u4f10😁\u3002
\n\u4e8e\u662f\uff0c\u4e3a\u4e86\u8c03\u7814\u8fd9\u4e2a\u95ee\u9898\uff0c\u4e5f\u67e5\u4e86\u76f8\u5173\u8d44\u6599\u3002\u4eca\u5929\u501f\u52a9\u672c\u6587\uff0c\u6765\u5206\u6790\u4e0bC\u8bed\u8a00\u4e2dmain()\u7684\u5b9e\u73b0\uff0c\u987a\u4fbf\u89e3\u7b54\u4e0b\u7fa4\u91cc\u7684\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n

\u5b9a\u4e49<\/strong><\/div>\n

\u4f5c\u4e3aC\/C++\u5f00\u53d1\u4eba\u5458\uff0c\u90fd\u77e5\u9053main()\u51fd\u6570\u662f\u4e00\u4e2a\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u5165\u53e3\u51fd\u6570\uff0c\u5927\u90fd\u4f1a\u50cf\u5982\u4e0b\u8fd9\u6837\u5199:
\n\"\"<\/p>\n

\r\nint main() {}\r\nint main(int argc, char *argv[]) {}\r\n<\/pre>\n

\u4f46\u662f\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5f00\u53d1\u8001\u6cb9\u6761\uff0c\u4e5f\u4ec5\u4ec5\u77e5\u9053\u662f\u8fd9\u6837\u505a\u7684\uff0c\u5f53\u770b\u5230\u4e8c\u54e5\u63d0\u51fa\u8fd9\u4e2a\u95ee\u9898\u7684\u65f6\u5019\uff0c\u7b2c\u4e00\u53cd\u5e94\u662f\u91cd\u8f7d\uff0c\u4f46\u662f\u5927\u5bb6\u90fd\u77e5\u9053C\u8bed\u8a00\u662f\u4e0d\u652f\u6301\u91cd\u8f7d\u7684\uff0c\u90a3\u4e48\u6709\u6ca1\u6709\u53ef\u80fd\u4f7f\u7528\u7684\u662f\u9ed8\u8ba4\u53c2\u6570\u5462\uff1f\u5982\u4e0b\u8fd9\u79cd\uff1a<\/p>\n

\r\nint main(int argc = 1, char **argv = NULL)\r\n<\/pre>\n

\u597d\u4e86\uff0c\u4e3a\u4e86\u9a8c\u8bc1\u6211\u7684\u7591\u95ee\uff0c\u54b1\u4eec\u7740\u624b\u5f00\u59cb\u8fdb\u884c\u5206\u6790\u3002<\/p>\n

ps:\u5728cppreference\u4e0a\u5bf9\u4e8emain()\u7684\u58f0\u660e\u6709\u7b2c\u4e09\u4e2a\u53c2\u6570\u5373char *envp[],\u8be5\u53c2\u6570\u662f\u73af\u5883\u53d8\u91cf\u76f8\u5173\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u66f4\u591a\u7684\u662f\u4e0d\u6d89\u53ca\u6b64\u53c2\u6570\u7684\u65b9\u5f0f\uff0c\u6240\u4ee5\u8be5\u53c2\u6570\u4e0d\u5728\u672c\u6587\u7684\u8ba8\u8bba\u8303\u56f4\u5185\u3002<\/p>\n

\u65ad\u70b9\u8c03\u8bd5<\/strong><\/div>\n

\u4e3a\u4e86\u80fd\u591f\u66f4\u6e05\u6670\u7684\u7406\u89e3main()\u51fd\u6570\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\uff0c\u901a\u8fc7gdb\u67e5\u770b\u5806\u6808\u4fe1\u606f\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nint main() {\r\n return 0;\r\n}\r\n<\/pre>\n

\u7f16\u8bd1\u4e4b\u540e\uff0c\u6211\u4eec\u901a\u8fc7gdb\u8fdb\u884c\u8c03\u8bd5\uff0c\u5728main()\u51fd\u6570\u5904\u8bbe\u7f6e\u65ad\u70b9\uff0c\u7136\u540e\u770b\u5806\u6808\u4fe1\u606f\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\n(gdb) bt\r\n#0  main () at main.c:2\r\n(gdb)\r\n<\/pre>\n

\u4ece\u4e0a\u8ff0gdb\u4fe1\u606f\uff0c\u6211\u4eec\u770b\u51famain()\u4f4d\u4e8e\u6808\u9876\uff0c\u663e\u7136\uff0c\u6211\u4eec\u7684\u76ee\u7684\u662f\u5206\u6790main()\u7684\u8c03\u7528\u5806\u6808\u4fe1\u606f\uff0c\u800c\u8fd9\u79cdmain()\u5728\u6808\u9876\u7684\u65b9\u5f0f\u663e\u7136\u4e0d\u8db3\u4ee5\u89e3\u7b54\u6211\u7684\u7591\u95ee\u3002<\/p>\n

\u4e8e\u662f\uff0c\u67e5\u9605\u4e86\u76f8\u5173\u8d44\u6599\u540e\uff0c\u53d1\u73b0\u53ef\u4ee5\u901a\u8fc7\u5176\u5b83\u65b9\u5f0f\u6253\u5370\u51fa\u66f4\u8be6\u7ec6\u7684\u5806\u6808\u4fe1\u606f\u3002<\/p>\n

\u7f16\u8bd1\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n

\r\ngcc -gdwarf-5 main.c  -o main\r\n<\/pre>\n

\u7136\u540egdb\u7684\u76f8\u5173\u547d\u4ee4(\u5177\u4f53\u7684\u547d\u4ee4\u53ef\u4ee5\u7f51\u4e0a\u67e5\u9605\uff0c\u6b64\u5904\u4e0d\u505a\u8fc7\u591a\u5206\u6790):<\/p>\n

\r\ngdb .\/main -q\r\nReading symbols from \/mtad\/main...done.\r\n(gdb) set backtrace past-entry\r\n(gdb) set backtrace past-main\r\n(gdb) show backtrace past-entry\r\nWhether backtraces should continue past the entry point of a program is on.\r\n(gdb) show backtrace past-main\r\nWhether backtraces should continue past \"main\" is on.\r\n<\/pre>\n

\u7136\u540e\u5728main()\u5904\u8bbe\u7f6e\u65ad\u70b9\uff0c\u8fd0\u884c\uff0c\u67e5\u770b\u5806\u6808\u4fe1\u606f\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\n(gdb) bt\r\n#0  main () at main.c:2\r\n#1  0x00007ffff7a2f555 in __libc_start_main () from \/lib64\/libc.so.6\r\n#2  0x0000000000400429 in _start ()\r\n(gdb)\r\n<\/pre>\n

\u901a\u8fc7\u5982\u4e0a\u5806\u6808\u4fe1\u606f\uff0c\u6211\u4eec\u770b\u5230_start()-->__libc_start_main()-->main()\uff0c\u770b\u6765\u5e94\u8be5\u5728\u8fd9\u4fe9\u51fd\u6570\u4e2d\uff0c\u5f00\u59cb\u5206\u6790~~<\/p>\n

_start()<\/strong><\/div>\n

\u4e3a\u4e86\u67e5\u770b_start()\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u7ee7\u7eed\u5728_start()\u51fd\u6570\u5904\u6253\u4e0a\u65ad\u70b9\uff0c\u7136\u540e\u5206\u6790\u67e5\u770b\uff1a<\/p>\n

\r\n(gdb) r\r\nStarting program: xxx\r\nMissing separate debuginfos, use: debuginfo-install glibc-2.17-317.el7.x86_64\r\nBreakpoint 1, 0x0000000000400400 in _start ()\r\n(gdb) s\r\nSingle stepping until exit from function _start,\r\nwhich has no line number information.\r\n0x00007ffff7a2f460 in __libc_start_main () from \/lib64\/libc.so.6\r\n<\/pre>\n

\u901a\u8fc7\u5982\u4e0a\u5206\u6790\uff0c\u6ca1\u6709\u770b\u5230_start()\u51fd\u6570\u7684\u53ef\u6267\u884c\u4ee3\u7801\uff0c\u4e8e\u662f\u901a\u8fc7\u7f51\u4e0a\u641c\u7d22\uff0c\u53d1\u73b0_start()\u662f\u7528\u6c47\u7f16\u7f16\u5199\uff0c\u4e8e\u662f\u4e0b\u8f7d\u4e86glibc2.5\u6e90\u7801\uff0c\u5728\u8def\u5f84\u5904sysdeps\/i386\/elf\/start.S<\/p>\n

\r\n#include \"bp-sym.h\"\r\n   .text\r\n   .globl _start\r\n   .type _start,@function\r\n_start:\r\n   \/* Clear the frame pointer.  The ABI suggests this be done, to mark\r\n      the outermost frame obviously.  *\/\r\n   xorl %ebp, %ebp\r\n   \/* Extract the arguments as encoded on the stack and set up\r\n      the arguments for `main': argc, argv.  envp will be determined\r\n      later in __libc_start_main.  *\/\r\n   popl %esi  \/* Pop the argument count.  *\/\r\n   movl %esp, %ecx  \/* argv starts just at the current stack top.*\/\r\n   \/* Before pushing the arguments align the stack to a 16-byte\r\n   (SSE needs 16-byte alignment) boundary to avoid penalties from\r\n   misaligned accesses.  Thanks to Edward Seidl \r\n   for pointing this out.  *\/\r\n   andl $0xfffffff0, %esp\r\n   pushl %eax  \/* Push garbage because we allocate\r\n                  28 more bytes.  *\/\r\n   \/* Provide the highest stack address to the user code (for stacks\r\n      which grow downwards).  *\/\r\n   pushl %esp\r\n   pushl %edx  \/* Push address of the shared library\r\n                  termination function.  *\/\r\n#ifdef SHARED\r\n   \/* Load PIC register.  *\/\r\n   call 1f\r\n   addl $_GLOBAL_OFFSET_TABLE_, %ebx\r\n   \/* Push address of our own entry points to .fini and .init.  *\/\r\n   leal __libc_csu_fini@GOTOFF(%ebx), %eax\r\n   pushl %eax\r\n   leal __libc_csu_init@GOTOFF(%ebx), %eax\r\n   pushl %eax\r\n   pushl %ecx  \/* Push second argument: argv.  *\/\r\n   pushl %esi  \/* Push first argument: argc.  *\/\r\n   pushl BP_SYM (main)@GOT(%ebx)\r\n   \/* Call the user's main function, and exit with its value.\r\n      But let the libc call main.    *\/\r\n   call BP_SYM (__libc_start_main)@PLT\r\n#else\r\n   \/* Push address of our own entry points to .fini and .init.  *\/\r\n   pushl $__libc_csu_fini\r\n   pushl $__libc_csu_init\r\n   pushl %ecx  \/* Push second argument: argv.  *\/\r\n   pushl %esi  \/* Push first argument: argc.  *\/\r\n   pushl $BP_SYM (main)\r\n   \/* Call the user's main function, and exit with its value.\r\n      But let the libc call main.    *\/\r\n   call BP_SYM (__libc_start_main)\r\n#endif\r\n   hlt   \/* Crash if somehow `exit' does return.  *\/\r\n#ifdef SHARED\r\n1: movl (%esp), %ebx\r\n   ret\r\n#endif\r\n\/* To fulfill the System V\/i386 ABI we need this symbol.  Yuck, it's so\r\n  meaningless since we don't support machines < 80386.  *\/\r\n   .section .rodata\r\n   .globl _fp_hw\r\n_fp_hw: .long 3\r\n   .size _fp_hw, 4\r\n   .type _fp_hw,@object\r\n\/* Define a symbol for the first piece of initialized data.  *\/\r\n   .data\r\n   .globl __data_start\r\n__data_start:\r\n   .long 0\r\n   .weak data_start\r\n   data_start = __data_start\r\n<\/pre>\n

\u4e0a\u8ff0\u5b9e\u73b0\u4e5f\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff1a<\/p>\n

xorl %ebp, %ebp\uff1a\u5c06ebp\u5bc4\u5b58\u5668\u6e05\u96f6\u3002<\/p>\n

popl %esi\u3001movl %esp, %ecx\uff1a\u88c5\u8f7d\u5668\u628a\u7528\u6237\u7684\u53c2\u6570\u548c\u73af\u5883\u53d8\u91cf\u538b\u6808\uff0c\u5b9e\u9645\u4e0a\u6309\u7167\u538b\u6808\u7684\u65b9\u6cd5\uff0c\u6808\u9876\u7684\u5143\u7d20\u5c31\u662fargc\uff0c\u63a5\u7740\u5176\u4e0b\u5c31\u662fargv\u548c\u73af\u5883\u53d8\u91cf\u7684\u6570\u7ec4\u3002\u8fd9\u4e24\u53e5\u76f8\u5f53\u4e8eint argc = pop from stack; char **argv = top of stack\u3002<\/p>\n

call BP_SYM (__libc_start_main)\uff1a\u76f8\u5f53\u4e8e\u8c03\u7528__libc_start_main\uff0c\u8c03\u7528\u7684\u65f6\u5019\u4f20\u5165\u53c2\u6570\uff0c\u5305\u62ecargc\u3001argv\u3002<\/p>\n

\u4e0a\u8ff0\u903b\u8f91\u529f\u80fd\uff0c\u4f2a\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n

\r\nvoid _start() {\r\n %ebp = 0;\r\n int argc = pop from stack\r\n char ** argv = top of stack;\r\n __libc_start_main(main, argc, argv, __libc_csu_init, __linc_csu_fini,\r\n edx, top of stack);\r\n}\r\n<\/pre>\n
__libc_start_main<\/strong><\/div>\n

\u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u5230\uff0c_start()\u624d\u662f\u6574\u4e2a\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u5165\u53e3\u51fd\u6570\uff0c\u5728_start()\u51fd\u6570\u4e2d\u8c03\u7528__libc_start_main()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u58f0\u660e\u5982\u4e0b\uff1a<\/p>\n

\r\nSTATIC int\r\nLIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),\r\n                int argc, char *__unbounded *__unbounded ubp_av,\r\n#ifdef LIBC_START_MAIN_AUXVEC_ARG\r\n                ElfW(auxv_t) *__unbounded auxvec,\r\n#endif\r\n                __typeof (main) init,\r\n                void (*fini) (void),\r\n                void (*rtld_fini) (void), void *__unbounded stack_end)\r\n{\r\n#if __BOUNDED_POINTERS__\r\n char **argv;\r\n#else\r\n# define argv ubp_av\r\n#endif\r\n \/* Result of the 'main' function.  *\/\r\n int result;\r\n __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;\r\n...\r\n...\r\n if (init)\r\n   (*init) (argc, argv, __environ MAIN_AUXVEC_PARAM);\r\n...\r\n result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);\r\n exit (result);\r\n}\r\n<\/pre>\n

\u53ef\u4ee5\u770b\u51fa\uff0c\u5728\u8be5\u51fd\u6570\u4e2d\uff0c\u6700\u7ec8\u8c03\u7528\u4e86main()\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u4e86\u76f8\u5173\u547d\u4ee4\u884c\u3002\uff08result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);\uff09<\/p>\n

\u622a\u6b62\u5230\u6b64\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u6574\u4e2amain()\u51fd\u6570\u7684\u8c03\u7528\u8fc7\u7a0b\uff0c\u4f46\u662f\uff0c\u4ecd\u7136\u6ca1\u6709\u56de\u7b54\u4e8c\u54e5\u7684\u95ee\u9898\uff0cmain()\u662f\u5982\u4f55\u5b9e\u73b0\u6709\u53c2\u548c\u65e0\u53c2\u4e24\u79cd\u65b9\u5f0f\u7684\uff0c\u5176\u5b9e\u8bf4\u767d\u4e86\uff0c\u5728\u6807\u51c6\u4e2d\uff0cmain()\u53ea\u6709\u4e00\u79cd\u58f0\u660e\u65b9\u5f0f\uff0c\u5373\u6709\u53c2\u65b9\u5f0f\u3002\u65e0\u8bba\u662f\u5426\u6709\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u90fd\u8c03\u7528\u8be5\u51fd\u6570\u3002\u5982\u679c\u6709\u53c2\u6570\uff0c\u5219\u901a\u8fc7\u538b\u6808\u51fa\u6808(\u5bf9\u4e8ex86 32\u4f4d)\u6216\u8005\u5bc4\u5b58\u5668(x86 64\u4f4d)\u7684\u65b9\u5f0f\u83b7\u53d6\u53c2\u6570\uff0c\u7136\u540e\u4f20\u5165main()\uff0c\u5982\u679c\u547d\u4ee4\u884c\u4e3a\u7a7a\uff0c\u5219\u5bf9\u5e94\u7684\u5b57\u6bb5\u4e3a\u7a7a\uff08\u5373\u6ca1\u6709\u4ece\u6808\u4e0a\u53d6\u5f97\u5bf9\u5e94\u7684\u6570\u636e\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5927\u5bb6\u90fd\u77e5\u9053\uff0c\u6211\u662f\u505a\u4e0a\u5c42\u5e94\u7528\u7684\uff0c\u5bf9\u5e95\u5c42\u4e0d\u662f\u5f88\u4e86\u89e3\uff0c\u66f4\u522b\u8bf4\u90a3\u5e2e\u4eba\u5728\u8ba8\u8bba\u5185\u6838\u7684\u65f6\u5019\uff0c\u6839\u672c\u63d2\u4e0d\u4e0a\u8bdd\u3002\u66f4\u591a\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u9ed8 […]<\/p>\n","protected":false},"author":1898,"featured_media":251909,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[925],"class_list":["post-251908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-main"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/251908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=251908"}],"version-history":[{"count":5,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/251908\/revisions"}],"predecessor-version":[{"id":251915,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/251908\/revisions\/251915"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/251909"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=251908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=251908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=251908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}