使用GCC进行组装会导致与.data相关的奇怪重定位错误 [英] Assembling with GCC causes weird relocation error with regards to .data

查看:171
本文介绍了使用GCC进行组装会导致与.data相关的奇怪重定位错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个从未发生过的问题。我很确信这可能是我的软件包回购问题(我最近重新安装了我的Arch系统,而这只是刚刚开始)



我写了一个小小的Hello World在x86_64中:

  .data 
str:.ascizTest

.text
.globl main
main:
sub $ 8,%rsp
mov $ str,%rdi
call puts
add $ 8,%rsp
ret

然后我尝试使用GCC进行汇编和链接 - 就像我在

lockquote
gcc test.s -o test

然后输出这个错误:

lockquote
/ usr / bin / ld重新编译:最终链接失败:输出上的非代表性部分
collect2:错误:ld返回1退出状态

这个错误从来没有发生过。我试图通过搜索相同的错误消息来解决这个问题,但它提出了非常具体的事情,而我认为这是一个普遍问题。我试过重新安装base-devel和整个GCC工具链。我不知道我还能做什么(请不要建议使用nasm,这就是 heresy )。



我想我认为我'我错过了一些显而易见的东西,但是我很长时间以来一直使用GCC来满足我的程序集需求。 解决方案

错误是产生一个 no-pie (非位置独立可执行文件)可执行文件:

  gcc -no-pie test.s -o test 

这种行为的原因是由@ Ped7g解释:


Debian转换为64位模式下的PIC / PIE二进制文件& GCC在你的情况下试图链接你的对象作为PIC,但它会遇到绝对地址在 mov $ str,%rdi


This is an issue that didn't used to ever occur. I'm pretty convinced it's probably an issue with my package repos (I recently reinstalled my Arch system and this has only just started happening).

I wrote a small hello world in x86_64:

.data
str:    .asciz  "Test"

.text
.globl main
main:
    sub $8, %rsp
    mov $str, %rdi
    call puts
    add $8, %rsp
    ret

and then I attempt to assembly and link using GCC - like I have done many times in the past - with, simply:

gcc test.s -o test

and then this error is outputted:

/usr/bin/ld: /tmp/ccAKVV4D.o: relocation R_X86_64_32S 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

This error has never occured for me ever. I've tried to fix the issue by googling the same error message but it comes up with things that are so specific whereas I'd consider this a general issue. I've tried reinstalling base-devel and the entire GCC toolchain. I dunno what else I can do (please don't suggest using nasm, that's heresy).

I'd like to think I'm missing something obvious but I've used GCC for my assembly needs for a long time.

解决方案

The way to get around this error is to generate a no-pie (Non Position Independent executable) executable :

gcc -no-pie test.s -o test

The reason for this behaviour is as explained by @Ped7g :

Debian switched to PIC/PIE binaries in 64-bits mode & GCC in your case is trying to link your object as PIC, but it will encounter absolute address in mov $str, %rdi.

这篇关于使用GCC进行组装会导致与.data相关的奇怪重定位错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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