无法使用gcc编译asm hello世界 [英] Can't compile asm hello world with gcc

查看:122
本文介绍了无法使用gcc编译asm hello世界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 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 并收到以下错误消息:

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:错误:ld返回1个退出状态

collect2: error: ld returned 1 exit status

然后我尝试运行 gcc hello.s -fPIC -o hello ,但是它什么也没做,错误是相同的.

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

我做错了什么?Ubuntu 17.04,GCC 6.3.0.

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

推荐答案

您正在尝试以amd64模式编译i386代码,该方法不起作用.尝试以下方法:

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

gcc -m32 hellos. -o hello

...强制执行i386模式.

...to force i386 mode.

我认识到这一点是因为我知道i386和amd64代码的样子,但是更好的线索是重定位名称 R_X86_64_32 .X86_64是amd64架构的另一个名称;所以这就是说X86_64体系结构是32位重定位.鉴于您没有为该体系结构编写代码,因此有一个合理的迹象表明它是错误的编译器.

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.

这篇关于无法使用gcc编译asm hello世界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆