当程序启动时(linux,elf) – 在eax
, ebx
等中是否有零,或者可以有任何东西(我没有做任何调用或使用extern库)? 在我的机器上实际上是这样,编写asm程序时可以传递这样的行为吗?
这完全取决于每个平台的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上时,你可以指望所有的寄存器值等于零。 但是,这并不意味着你应该。 🙂
对于Linux上的AMD64或x86-64系统(64位), x86-64 ABI定义了寄存器的初始内容。
有类似的规格为i386 ABI , ARM ABI等。
请参阅ELF和ABI上的wikipedia页面
x86-64 System V ABI部分3.4.1“初始堆栈和注册状态” ( Basile链接到PDF ):
%rsp
指向堆栈
堆栈指针保存最低地址的字节地址,该地址是堆栈的一部分。 在进程入口处保证16字节对齐
%rdx
函数指针,应用程序应该用atexit注册,如果它不是零。
一个应用程序应该注册的函数指针
%rbp
未指定,但用户区应将其设置为基本框架。
这个寄存器的内容在进程初始化时是未指定的,但用户代码应该通过将帧指针设置为零来标记最深的堆栈帧。
其他一切未定义。
Linux然后遵循它“因为” LSB这样说。