如何编译组件,其切入点是不是主要用gcc? [英] How to compile assembly whose entry point is not main with gcc?

查看:214
本文介绍了如何编译组件,其切入点是不是主要用gcc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.text
    .globl _start
_start:
     pushq %rbp
     movq %rsp,%rbp
     movq $2, %rax
     leaveq
     retq

我与 -nostdlib 编译:

[root@ test]# gcc -nostdlib -Wall minimal.S &&./a.out 
Segmentation fault

什么是错在这里?

What's wrong here?

顺便说一句,是有可能使入口点其他的名字比 _start

BTW,is it possible to make the entry point other names than main and _start?

推荐答案

由于@jaquadro提到,您可以指定命令行链接器的入口点(或使用链接脚本): GCC -Wall -Wextra -nostdlib轮候册,-eMyEntry minimal.S和放大器;&安培; ./a.out

As @jaquadro mentions, you can specify the entry point on the command line to the linker (or use a link script): gcc -Wall -Wextra -nostdlib -Wl,-eMyEntry minimal.S && ./a.out

原因你的程序设计缺陷是,既然你不使用标准库,你无处可回返回( retq )。除了使用正确的系统调用(在这种情况下,它是60,它被放入 RAX ,调用exit第一个(也是唯一一个)参数放入 RDI

The reason your program segfaults is, that since you're not using the standard library there is nowhere to return back to (retq). Instead call exit using the correct syscall (in this case it is 60, which is put into rax, the first (and only) parameter is put into rdi.

例如:


.text
.globl MyEntry
MyEntry:
    # Use Syscall 60 (exit) to exit with error code 42
    movq $60, %rax
    movq $42, %rdi
    syscall

关于如何执行在x86_64 系统调用相关的问题

这篇关于如何编译组件,其切入点是不是主要用gcc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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