Can't compile asm hello world with gcc

纵然是瞬间 提交于 2020-11-29 09:45:31

问题


This is the code in 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   

I run gcc hello.s -o hello and get this error:

/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: error: ld returned 1 exit status

Then I've tried running gcc hello.s -fPIC -o hello, but it didn't do anything, the error is the same.

What am I doing wrong? Ubuntu 17.04, GCC 6.3.0.


回答1:


You're trying to compile i386 code in amd64 mode, which doesn't work. Try this instead:

gcc -m32 hellos. -o hello

...to force i386 mode.

Edit: I recognised this because I know what i386 and amd64 code looks like, but a better clue is in the relocation name, R_X86_64_32. X86_64 is another name for the amd64 architecture; so what this is saying is that it's a 32 bit relocation for the X86_64 architecture. Given that you're not writing code for that architecture, that's a reasonable sign that it's the wrong compiler.



来源:https://stackoverflow.com/questions/44934982/cant-compile-asm-hello-world-with-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!