程序启动时的默认寄存器状态是什么(asm,linux)?

14 linux assembly elf cpu-registers

当程序启动(Linux中,小精灵) -有归零的eax,ebx等等,或可以有任何东西(我没有做任何电话或使用的extern libraryies)?在我的机器上真的如此,在编写asm程序时我可以继续这种行为吗?

Mic*_*kis 19

这完全取决于每个平台的ABI.既然你提到eaxebx让我们看看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.
 */
Run Code Online (Sandbox Code Playgroud)

然后调用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)
Run Code Online (Sandbox Code Playgroud)

因此,当您的ELF二进制文件在Linux x86上加载时,您可以指望所有寄存器值等于零.但这并不意味着你应该这样做.:-)

  • 此归零仍然发生(2015),但ABI不要求.(见Ciro对Basile答案的评论). (3认同)

Bas*_*tch 10

对于Linux上的AMD64或x86-64系统(64位),x86-64 ABI定义了寄存器的初始内容.

i386 ABI,ARM ABI等有类似的规格.

请参阅ELFABI上的维基百科页面


Cir*_*四事件 5

x86-64 System V ABI3.4.1"初始堆栈和寄存器状态"(Basile链接到PDF):

  1. %rsp 指向堆栈

    堆栈指针保存具有最低地址的字节的地址,该地址是堆栈的一部分.保证在进程输入时对齐16字节

  2. %rdx 应用程序应该使用atexit注册的函数指针,如果它不为零.

    应用程序应注册的函数指针

  3. %rbp 未指定,但userland应将其设置为基础框架.

    在进程初始化时未指定该寄存器的内容,但用户代码应通过将帧指针设置为零来标记最深的堆栈帧.

  4. 其他一切都未定义.

然后Linux跟随它"因为" LSB这么说.

  • i386 ABI (http://www.sco.com/developers/devspecs/abi386-4.pdf) 对相应的 32 位寄存器有相同的要求:`%esp`、`%edx`、`%ebp`、其他一切都是未定义的。 (2认同)