relocation R_X86_64_32S against

0

Hi, I am programming in assembler and when compiling it shows me the error of the title, this is all the error that generates me

/ usr / bin / ld: /tmp/ccbjmJPt.o: relocation R_X86_64_32S against '.data' can not be used when making a shared object; recompile with -fPIC / usr / bin / ld: the final link failed: Section not shown in the output collect2: error: ld returned the exit status 1

I use the functions of the c library. To compile I occupy

gcc hello.s -o hello

I have also tried the following

gcc -c hello.s

which generates a hello.o file and then I link it with

ld -share -fpic -s -o hello hello.o

and I get the same error, in other machines I compile perfectly, but in which I am currently working, not

And the code

.global main
.text
main:
    mov   $msg, %rdi
    call  puts
    ret

msg:
    .asciz  "holaa"
    
asked by 0and6 08.08.2017 в 06:00
source

1 answer

1

The error message says that the code must be position independent to generate a shared object.

To generate position independent code (PIC), the references can not be absolute, they must be relative to something known.

To avoid jumps and calls to absolute addresses in a library that we do not know where to load, we will make calls indirectly through the procedure link table (PLT), which creates the dynamic linker. This is achieved, for example, by using call puts@PLT instead of call puts .

To avoid absolute references to data, we will use references related to the program counter. For example, instead of mov $msg, %edi (or equivalently lea msg, %edi ), we will use lea msg(%rip), %edi .

We have left:

.global main
.text
main:
        lea   msg(%rip), %rdi
        call  puts@PLT
        mov   $0, %rdi
        call  exit@PLT
        ret

.data
msg:
        .asciz  "holaa"
    
answered by 09.08.2017 / 13:21
source