Author Topic: Writing a shared library: ERROR relocation R_X86_64_PC32 against symbol  (Read 7581 times)

Offline dduong

  • New Member
  • Posts: 1
Hello, I am having some trouble writing a shared library.

func1.s
Code: [Select]
SECTION .text
GLOBAL  func1

func1:
mov rax, rdi
ret

func2.s
Code: [Select]
SECTION .text
EXTERN  func1
GLOBAL  func2

func2:
call    func1
add     rax, rsi
ret

Code: [Select]
huy@xguard:~/shared$ nasm -f elf64 func1.s
huy@xguard:~/shared$ nasm -f elf64 func2.s
huy@xguard:~/shared$ ld -shared -o libfunc.so func1.o func2.o
ld: func2.o: relocation R_X86_64_PC32 against symbol `func1' can not be used when making a shared object; recompile with -fPIC
ld: final link failed: Bad value

Under mac os x when I do ld -dylib, I don't have any issue. But with linux, I have this error. Could you help me with this issue ?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Writing a shared library: ERROR relocation R_X86_64_PC32 against symbol
« Reply #1 on: January 08, 2014, 11:21:21 PM »
I don't know if it's right, but simply adding "-pic" to ld's command line seems to get it past that point - complains about not finding an entrypoint. I would have thought that "-shared" implied "-pic", but perhaps not. I'm not sure a shared library should need an entrypoint. The second thing I might try is adding "default rel" to one or both files. I'm too new to 64-bit code to know if that's any help or not.

Best,
Frank