Situation
Enviroment
Arch Linux x86-64 (4.2.3-1-ARCH)
GCC
gcc (GCC) 5.2.0
Command
gcc -Wall -g -o asm_printf asm_printf.s
Error
/usr/bin/ld: /tmp/cct4fa.o: Relocation R_X86_64_32S against '.data' can not be used when making a shared object; recompile with -fPIC /tmp/cct4fa.o:err adding symbols: Bad value collect2: error: ld returned 1 exit status
Code
.section .data msg: .asciz "Printf In Assembly!!\n" .section .text .globl main main: pushq $msg call printf addq $8 %esp pushq $0 call exit
Question
I tried to use gcc
to compile the program in the above Code section using the command in the above Command section and ended up with a error in the Error Section.
Note that I am not compiling a shared library.
- What is this error?
- How do i fix this?
The specific error is due to the
push
instruction only supporting 32 bit immediates and you tried to use that for a 64 bit address.However, the whole code is wrong. It's unclear whether you want 32 or 64 bit code. Most of the code seems to be 32 bit, except for the
pushq
so I assume you really wanted 32 bit code. For that, change all those topush
(which is a good idea anyway) and compile usinggcc -m32
. Also, you only need to remove 4 bytes from the stack, so useaddl $4, %esp
. (Thanks to @Employed Russian for pointing this out.)The 64 bit calling convention is different from 32 bit, so to create a 64 bit version you must make more changes. Since I assume you really wanted 32 bit, here it is for illustration only: