这是hello.s中的代码
.data
hello_str:
.string "Hello, world!\n"
.set hello_str_length, . - hello_str - 1
.text
.globl main
.type main, @function
main:
movl $4, %eax
movl $1, %ebx
movl $hello_str, %ecx
movl $hello_str_length, %edx
int $0x80
movl $1, %eax
movl $0, %ebx
int $0x80
.size main, . - main
我运行 gcc hello.s -o hello 并得到这个错误:
/usr/bin/ld: /tmp/cc6ILJpd.o: relocation R_X86_64_32 against '.data' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2:错误:ld 返回了 1 个退出状态
然后我尝试运行gcc hello.s -fPIC -o hello,但它什么也没做,错误是一样的。
我做错了什么? Ubuntu 17.04,海湾合作委员会 6.3.0。
最佳答案
您正在尝试在 amd64 模式下编译 i386 代码,但这是行不通的。试试这个:
gcc -m32 hellos. -o hello
...强制 i386 模式。
编辑:我认出了这一点,因为我知道 i386 和 amd64 代码是什么样的,但更好的线索是重定位名称 R_X86_64_32
。 X86_64 是 amd64 架构的别称;所以这就是说它是 X86_64 架构的 32 位重定位。鉴于您没有为该架构编写代码,这是一个合理的迹象,表明它是错误的编译器。
关于gcc - 无法用gcc编译asm hello world,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44934982/