I copied the sample program on assembler and compiled it using gcc, but I got the following output:
gcc hello.s -o hello1
/usr/bin/ld: /tmp/ccPs5dcq.o: relocation R_X86_64_32 against `.data' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: final link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status
Code:
.data
hello_str:
.string "Hello, world!\n"
.set hello_str_length, . - hello_str - 1
.text
.globl main
.type main, @function
main:
movl $4, %eax
movl $1, %ebx
movl $hello_str, %ecx
movl $hello_str_length, %edx
int $0x80
movl $1, %eax
movl $0, %ebx
int $0x80
.size main, . - main
What am i doing wrong?
P.S. I'm absolutely beginner in assembler, just trying to parse the example
-fno-pie
. Let me see if I can find a good duplicate for this one. – fuzmovl $hello_str, %ecx
needs to know the address ofhello_str
at link time. This is not the case when creating PIE (position independent) executables, so the linker shouts at you. It is possible to write the same code in a position-independent way, but in 32 bit code, it's a lot more difficult to do, so it's best to ignore this for now. Also, given that you write 32 bit code, you need to assemble and link with-m32
or weird problems are going to occur. – fuzR_X86_64_32
relocation: the problem is no support for 32-bit absolute fixups in 64-bit mode. With-m32
the dynamic linker can fixup a 32-bit absolute to hold any pointer in the whole address space, so it is supported. The error message is a duplicate of 32-bit absolute addresses no longer allowed in x86-64 Linux?, except that the real fix is-m32
(and probably also-no-pie
is a good idea even if not required). – Peter Cordes