小编典典
这完全取决于每个平台的ABI。既然您提到了eax,ebx让我们看看x86的情况是什么。在fs/binfmt_elf.c内部的#972行中load_elf_binary(),内核检查ABI
在程序加载时是否对寄存器值指定了任何要求:
/*
* The ABI may specify that certain registers be set up in special
* ways (on i386 %edx is the address of a DT_FINI function, for
* example. In addition, it may also specify (eg, PowerPC64 ELF)
* that the e_entry field is the address of the function descriptor
* for the startup routine, rather than the address of the startup
* routine itself. This macro performs whatever initialization to
* the regs structure is required as well as any relocations to the
* function descriptor entries when executing dynamically links apps.
*/
然后调用ELF_PLAT_INIT,这是为中的每个体系结构定义的宏arch/xxx/include/elf.h。对于x86,它执行以下操作:
#define ELF_PLAT_INIT(_r, load_addr) \
do { \
_r->bx = 0; _r->cx = 0; _r->dx = 0; \
_r->si = 0; _r->di = 0; _r->bp = 0; \
_r->ax = 0; \
} while (0)
因此,当您将静态链接的ELF二进制文件加载到Linux x86上时,您可以指望所有等于零的寄存器值。不过,这并不意味着您应该这样做。:-)
动态链接
请注意,执行 动态 链接的二进制文件实际上会在执行到达您的_start(ELF入口点)之前在您的进程中运行动态链接程序代码。
这可以并且确实在ABI的允许下在寄存器中留下垃圾。 当然,除了堆栈指针ESP / RSP和atexit挂钩EDX / RDX。
2020-06-03