程序启动时的默认寄存器状态是什么(asm、linux)? [英] What is default register state when program launches (asm, linux)?

查看:20
本文介绍了程序启动时的默认寄存器状态是什么(asm、linux)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当程序启动时 (linux, elf) - eaxebx 等中是否有零或可以有任何东西(我没有做任何调用或使用外部库)?在我的机器上确实如此,我可以在编写 asm 程序时中继这种行为吗?

When the program launches (linux, elf) - is there zeros in eax, ebx, etc. or there can be anything (i'm not doing any calls or using extern libraryies)? On my machine it is really so, can I relay on such behavior when writing asm programms?

推荐答案

这完全取决于每个平台的 ABI.既然你提到了 eaxebx 让我们看看 x86 的情况.在 fs/binfmt_elf.c 行 #972 中,在 load_elf_binary() 中,内核检查 ABI 是否指定了任何 要求 程序加载时的寄存器值:

This depends entirely on the ABI for each platform. Since you mention eax and ebx let's see what's the case for x86. In fs/binfmt_elf.c line #972, inside load_elf_binary(), the kernel checks if the ABI specifies any requirements for register values at program loading:

/*
 * 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,它执行 以下:

It then calls ELF_PLAT_INIT, which is a macro defined for each architecture in arch/xxx/include/elf.h. For x86, it does the following:

#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)

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

So, when your statically-linked ELF binary is loaded on Linux x86, you could count on all register values being equal to zero. Doesn't mean you should, though. :-)

请注意,在执行到达_start(ELF 入口点)之前,执行动态 链接的二进制文件实际上会在您的进程中运行动态链接器代码.这可以并且确实在 ABI 允许的情况下在寄存器中留下垃圾. 当然除了堆栈指针 ESP/RSP 和 atexit 钩子 EDX/RDX.

Note that executing a dynamically linked binary actually runs dynamic linker code in your process before execution reaches your _start (ELF entry point). This can and does leave garbage in registers, as allowed by the ABI. Except of course for the stack pointer ESP/RSP and atexit hook EDX/RDX.

这篇关于程序启动时的默认寄存器状态是什么(asm、linux)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆