5

I try to create a shared library on x86-64 but fail. The problem boils down to the following code (please don't mind, that it does not make a lot of sense):

.section .data
newline:
    .ascii "\n"

.section .text
.globl write_newline
    .type write_newline, @function
write_newline: 
    mov  $newline, %rax    
    ret

building as follows:

as minimal.s -o minimal.o
ld -shared minimal.o -o libmin.so

leads to the following error:

ld: minimal.o: relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
minimal.o: error adding symbols: Bad value

But gas doesn't know option -fPIC, so I cannot recompile with it:

as -fPIC minimal.s -o minimal.o
as: unrecognized option '-PIC'

What can be done instead?

3
  • -fpic would be an option for a compiler, like gcc. if you're writing asm, you'd have to write the pic stuff yourself.
    – Marc B
    Jul 4, 2016 at 22:05
  • @MarcB How could that be done? Where should I start?
    – ead
    Jul 4, 2016 at 22:09
  • 3
    @ead For starters, write mov newline@GOTPCREL(%rip),%rax. Check what output your C compiler generates to get an idea for how to write PIC.
    – fuz
    Jul 5, 2016 at 8:35

1 Answer 1

6

As Marc B said in the comments, the error message assumes you were using GCC or so some other compiler. Since you've written the code in assembly its your responsibility to ensure that your code is position independent.

On x86-64 targets you can resolve this error using RIP relative addressing:

        .section .data
newline:
    .ascii "\n"

.section .text
.globl write_newline
    .type write_newline, @function
write_newline:
    lea  newline(%rip), %rax
    ret

Note that you don't need to use the GOT here because newline is a local symbol. If it were global, and you wanted the reference to newline in your code to able to refer to some other definition of newline in some other runtime component, then you would need to use an @GOTPCREL relocation. That would allow the dynamic linker to change the entry in the GOT to point to a different definition of newline.

3
  • Thanks, that worked! But why there is no need for @ GOTPCREL as in newline@GOTPCREL(%rip) as gcc would do and @FUZxxl has suggested?
    – ead
    Jul 5, 2016 at 22:36
  • Well, GCC wouldn't generate a @GOTPCREL for the equivalent C code. It would use an LEA instruction and RIP relative addressing because newline isn't global. If it were global then dynamic linking mean some other global newline definition in some other module takes precedence so it'll access it through the GOT.
    – Ross Ridge
    Jul 5, 2016 at 23:31
  • You are right, my test C-code has a global variable but assembler-code a local variable. Glad to have asked you. Thank you very much!
    – ead
    Jul 6, 2016 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.