我正在编写一个小型库,用于替代小型应用程序中的libc。我已经阅读了主要libc替代方案的来源,但我无法获得传递给x86_64体系结构的参数。
库不需要_start和main之间的任何初始化步骤。由于libc及其替代方案确实使用了初始化步骤,而且我的组装知识有限,因此我怀疑参数重新排序会给我带来麻烦。
这就是我所得到的,其中包含来自各种实现的程序集:
.text
.global _start
_start:
/* Mark the outmost frame by clearing the frame pointer. */
xorl %ebp, %ebp
/* Pop the argument count of the stack and place it
* in the first parameter-passing register. */
popq %rdi
/* Place the argument array in the second parameter-passing register. */
movq %rsi, %rsp
/* Align the stack at a 16-byte boundary. */
andq $~15, %rsp
/* Invoke main (defined by the host program). */
call main
/* Request process termination by the kernel. This
* is x86 assembly but it works for now. */
mov %ebx, %eax
mov %eax, 1
int $80入口点是普通的主签名:int main(int argc, char* argv[])。此特定项目不需要环境变量等。
AMD64 ABI表示,rdi用于第一个参数,rsi用于第二个参数。
如何正确设置堆栈并在Linux main上将参数传递给x86_64?谢谢!
参考文献:
64/elf/start.S?view=标记
64/crt1.S
发布于 2011-12-10 14:55:55
我想你有
/* Place the argument array in the second parameter-passing register. */
movq %rsi, %rsp不对。它应该是
movq %rsp, %rsi # move argv to rsi, the second parameter in x86_64 abi发布于 2011-12-12 06:05:26
main由crt0.o调用;另见这个问题
内核按照训斥文档(特定于体系结构)中的指定,在ABI之后设置初始堆栈和进程环境;crt0 (和相关的)代码负责调用main。
https://stackoverflow.com/questions/8457132
复制相似问题