gcc - 无法用gcc编译asm hello world

标签 gcc assembly gnu-assembler

这是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/

相关文章:

assembly - 对非常量 TSS 段使用 jmp 指令

c++ - main in反汇编后的说明

c++ - 所选处理器不支持 `swp x1,x1,[x0]'

assembly - 在 x86 汇编中读取磁盘时如何处理错误?

gcc - 在 NASM 和 GCC 中提供 incbin 路径作为预定义宏

assembly - ARM 组装难题

gcc - calloc 崩溃

android - 如何通过我的 PC 终端在我的 android 设备上编译和运行 C 程序。?

macos - OSX 上的 LD_PRELOAD 的确切等效项是什么?

c - `push' 的操作数类型不匹配