【发布时间】:2022-01-29 16:12:33
【问题描述】:
我有以下汇编程序,它通过适当的系统调用将“hello world”打印到屏幕上:
.global _start
.text
_start:
# write(1, message, 13)
mov $1, %rax # system call 1 is write
mov $1, %rdi # file handle 1 is stdout
mov $message, %rsi # address of string to output
mov $13, %rdx # number of bytes
syscall # invoke operating system to do the write
# exit(0)
mov $60, %rax # system call 60 is exit
xor %rdi, %rdi # we want return code 0
syscall # invoke operating system to exit
message:
.ascii "Hello, world\n"
如果我在 ubuntu 终端中发出命令 gcc hello.s,汇编代码是否会像网页中的某个人所说的那样与 C 库链接?如果是,那是为什么?汇编代码似乎没有在任何地方引用 C 代码。
【问题讨论】:
-
gcc是as和ld的 Gnu C 编译器和包装器。as是汇编程序。ld是链接器。调用 gcc 会做预处理、编译组装和链接,具体取决于选项。 -
这一切我都知道。我的问题是命令'gcc hello.s'是否会链接到C库?反汇编输出可执行文件有什么帮助吗?
-
执行
gcc -v hello.s,它将显示它运行的所有步骤:as 和 ld。尝试与作为 C 库的一部分的 crt1.o 链接失败。 -
如果你以这种方式构建,它会是,但你不需要也不应该不应该。使用
gcc -nostdlib -static foo.s,因为您正在定义自己的_start并使用原始系统调用。另请参阅Assembling 32-bit binaries on a 64-bit system (GNU toolchain)(但省略 -m32)