linux - 如何使用 NASM 更改 x86-64 程序集的开始/主入口点?
问题描述
我有这个:
$ make build
read.o: In function `_start':
read.asm:(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'build' failed
make: *** [build] Error 1
从这个asm:
global main
section .text
main: mov rax, 1 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do the write
mov rax, 60 ; system call for exit
xor rdi, rdi ; exit code 0
syscall ; invoke operating system to exit
section .data
message: db "Hello, World", 10 ; note the newline at the end
我用这个运行它:
$ nasm -felf64 read.asm -o read.o && gcc read.o -o store && ./store
如何将单词更改为ormain
以外的内容,例如or ?我想定制它。它甚至可以定制吗?main
_start
begin
myentrypoint
解决方案
请注意,这main
不是入口点。最终调用的入口点_start
由它提供。你无法改变这一点。但是,您可以提供自己的启动代码来调用除.crt0.o
main
main
请注意,入口点本身可以设置为您喜欢的任何-e
符号ld
。详情请参阅手册。但是请注意,如果您更改此设置,C 运行时代码将不再正常工作。仅与您自己的运行时代码一起使用。
更改main
为其他内容的一种选择是设置main
为其他符号的别名,例如
.set main, mymain
在一些汇编文件中。或者,只需提供一个虚拟main
函数即可跳转到您的实际主函数:
global main
main: jmp mymain
推荐阅读
- reactjs - 如何在 ReactJS-PhpStorm 中使用“mobx”?谢谢
- html - 文字和图片并排
- css - 如何在移动设备上的 wordpress 主题上从滑动侧边栏中删除删除徽标
- facebook - Facebook Feed API 行为异常
- socket.io - 连接套接字服务器时出现“由于 xhr 轮询错误而导致的连接错误”
- mongoose - 如果给定参数,猫鼬聚合过滤器
- php - 以正确的方式重新格式化 json
- java - Docker + Maven 抱怨“没有为此构建指定目标”
- apexcharts - 在具有两个 y 轴的 ApexCharts 中,x 轴显示图表之外的数据标签。任何想法如何删除?
- typescript - 为什么一种循环样式会给我一个错误,而另一种却没有?