mingw32-g++ 编译器如何知道在 WIN32 机器可执行文件中注入系统调用的位置?

How mingw32-g++ compiler know where to inject system calls in the WIN32 machine executable?

今天,仅出于测试目的,我有了以下想法,在 CodeBlocks 中创建和编译原始源代码,使用 Release 目标删除不必要的调试代码, 一个 main 函数,三个 nop 操作只是为了更快地找到 main 函数的入口点所在的位置。

CodeBlocks 示例朴素程序:

使用IDA反汇编器,我看到了一些奇怪的东西,OS实际上可以在主函数中添加额外的机器代码调用(隐式添加),对驻留在kernel32.dll中的系统函数的调用什么用于 OS 线程处理。

IDA程序查看:

在仅出于测试原因的机器代码中,三个 "nop" (90) 被 "and esp, 0FFFFFFF0h" 替换,程序再次被重新打包,这就是为什么 "no operation" 操作码不是不负责任的看法。

观察到的行为:

为每个打开的进程创建一个新线程是合乎逻辑的,因为我们可以在 TaskManager 中探索它,进程 运行 在它自己的线程中,这就是编译器添加此代码的原因 (隐式默认线程)。

我的问题:

编译器如何知道 "inject" 自动调用代码的位置?

为什么这个调用之前没有在上层函数(sub_401B8C)中调用,它将路由到主函数入口点?

引用gcc manual

If no init section is available, when GCC compiles any function called main (or more accurately, any function designated as a program entry point by the language front end calling expand_main_function), it inserts a procedure call to __main as the first executable code after the function prologue. The __main function is defined in libgcc2.c and runs the global constructors.