Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nasm - Symbol `printf' causes overflow in R_X86_64_PC32 relocation [duplicate]

I am trying to create a simple program in nasm that should display the letter a. however, It is giving me a Segfault and saying this:

./a.out: Symbol `printf' causes overflow in R_X86_64_PC32 relocation
Segmentation fault (core dumped)

Basically, I am trying to move the value 0x61 (hex for letter a) into memory address 1234, and then pass that as an argument to printf. Here is my exact code:

extern printf
section .text
global main
main:
push rbp
mov rax,0
mov qword [1234], 0x61 ; move 0x61 into address 1234
mov rdi, qword [1234] ; mov address 1234 into rdi
call printf ; should print the letter a
pop rbp
mov rax,0
ret

I am running Linux x86_64

like image 901
Unknown Avatar asked Jan 03 '18 04:01

Unknown


2 Answers

try compiling with -no-pie, check out these posts for explanation: Assembling with GCC causes weird relocation error with regards to .data

in short:

Debian switched to PIC/PIE binaries in 64-bits mode & GCC in your case is trying to link your object as PIC, but it will encounter absolute address in mov $str, %rdi.

like image 81
brian Avatar answered Oct 18 '22 06:10

brian


Full credit to https://stackoverflow.com/users/3062591/brian

for figuring out how to get this to work. If you're like me and very new to nasm OR if for some reason you're new to nasm and have done little to nothing with gcc then you're going to need to run the command:

nasm -felf64 YOUR_FILE.asm && gcc -no-pie YOUR_FILE.o && ./a.out

I was able to get this using a

call printf

without needing to change it to

printf wrt ..got

which had on previous attempts with some NASM introductory examples but not all.

like image 5
Anton_of_Ternopil Avatar answered Oct 18 '22 07:10

Anton_of_Ternopil