6

I'm absolutely green in this but during classes, teacher gave us file he wrote just for us to run it and it worked fine then, but when I try to do it at home (I use Linux on VirtualBox) and use:

nasm -f elf64 hello.asm -o hello.o
gcc hello.o -o hello

I get an error "relocation R_X86_64_32S against `.bss' can not be used when making a shared object; recompile with -fPIC”. Can someone please explain what to do to make it work?

global main
extern printf

section .data
napis:      db ' Hello world! - po raz %ld',10,0

liczba_iteracji: equ 5

section .bss
licznik: resb 1

section .text

main:

push    rbp
mov rbp,rsp

mov byte [licznik],0

petla:              ;naiwna!

inc byte [licznik]

mov rdi, qword napis
mov rsi, qword [licznik]
mov rax, 0
call    printf

cmp byte [licznik],liczba_iteracji
jnz petla

mov rsp,rbp
pop rbp

mov rax,1           ;SYS_EXIT
mov rbx,0
int 80h
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
overflow
  • 85
  • 1
  • 4
  • 1
    Possible duplicate of [What do R\_X86\_64\_32S and R\_X86\_64\_64 relocation mean?](http://stackoverflow.com/questions/6093547/what-do-r-x86-64-32s-and-r-x86-64-64-relocation-mean) – Michael Foukarakis Nov 26 '16 at 17:13
  • 1
    The answer about using gcc -shared -fPIC foo.c -o libfoo.so doesnt solve my problem – overflow Nov 26 '16 at 17:24

2 Answers2

3

I had the same issue. The reason GCC gives this error is because it assumes (version 6.3.0 here) you are building a shared object (when, clearly, you are not), therefore presence of .bss makes it crazy. So you can either fix this by passing -static option: gcc hello.o -static -o hello (worked in my case), or using Clang as a linker: clang hello.o -o hello. No complaints from the latter.

AlexDarkVoid
  • 485
  • 3
  • 12
  • PIE executable actually *are* ELF shared objects. [32-bit absolute addresses no longer allowed in x86-64 Linux?](//stackoverflow.com/q/43367427) The better fix is to avoid 32-bit absolute addressing where possible by using `default rel`, or by using `-fno-pie -no-pie` if there are cases where your code can be more efficient with absolute addressing. – Peter Cordes Jul 25 '19 at 16:14
2

You need to make certain you're writing position independent code. The idea of PIC is that to make code truly position-independent, you need at least one level of indirection. That level of indirection is IP-relative addressing, and when that is not enough, you will need a second layer, the Global Offset Table or GOT.

In NASM, you will find the DEFAULT REL directive(s) useful.

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
  • PIE executables don't need to indirect through the GOT for access to their own functions / vars; that's only to support symbol interposition. `gcc -fPIC` implies shared-library symbol interposition, but the modern default of `gcc -fpie -pie` only needs to avoid 32-bit absolute addressing. So yes, `default rel`. [32-bit absolute addresses no longer allowed in x86-64 Linux?](//stackoverflow.com/q/43367427) – Peter Cordes Jul 25 '19 at 15:46