首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从程序集调用主

从程序集调用主
EN

Stack Overflow用户
提问于 2011-12-10 14:20:06
回答 2查看 2.2K关注 0票数 2

我正在编写一个小型库,用于替代小型应用程序中的libc。我已经阅读了主要libc替代方案的来源,但我无法获得传递给x86_64体系结构的参数。

库不需要_startmain之间的任何初始化步骤。由于libc及其替代方案确实使用了初始化步骤,而且我的组装知识有限,因此我怀疑参数重新排序会给我带来麻烦。

这就是我所得到的,其中包含来自各种实现的程序集:

代码语言:javascript
复制
.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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-10 14:55:55

我想你有

代码语言:javascript
复制
 /* Place the argument array in the second parameter-passing register. */
    movq %rsi, %rsp

不对。它应该是

代码语言:javascript
复制
movq %rsp, %rsi      # move argv to rsi, the second parameter in x86_64 abi
票数 1
EN

Stack Overflow用户

发布于 2011-12-12 06:05:26

maincrt0.o调用;另见这个问题

内核按照训斥文档(特定于体系结构)中的指定,在ABI之后设置初始堆栈和进程环境;crt0 (和相关的)代码负责调用main

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8457132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档