'`relocation R_X86_64_32S against symbol` error when accessing global variables in dynamic library with assembly codes

I'm trying to add a global data label to 64-bit assembly code which i'd like to assemble to a shared library. Part of code is as followed.

# 64bit out.s
.section .text
...

.globl export_func
.type   export_func, @function
export_func:
pushq %rbp
movq %rsp,%rbp
pushq %rax
movzbl export_func_input_0,%eax
movsbl %al,%eax
mov %eax,%edi
callq S_0x400607
mov %al, export_func_output
add $0,%rsp
popq %rax
popq %rbp
ret

...

.section .data
.globl export_func_input_0
.type export_func_input_0, @object
export_func_input_0:
.byte 0x41

...

I'm trying to workThen i got error like this

$ gcc -fPIC -shared out.s -o libout.so
/usr/bin/ld: /tmp/ccqrtdKg.o: relocation R_X86_64_32S against symbol `export_func_input_0' 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

But for assembly codes with the same function of 32bit, No errors like this occured.

# 32bit out.s
.section .text
...

.globl export_func
.type   export_func, @function
export_func:
pushl %ebp
movl %esp,%ebp
pushl %eax
pushl %edx
movzbl export_func_input_0,%eax
movsbl %al,%eax
push %eax
call S_0x8048506
mov %al, export_func_output
add $4,%esp
popl %edx
popl %eax
popl %ebp
ret

...
.section .data
.globl export_func_input_0
.type export_func_input_0, @object
export_func_input_0:
.byte 0x41

...

Apology if i've asked a very simple question. I'm totally new in this region.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source