I have the following program to print a number in assembly:
.section .rodata
format: .ascii "Your number is: %d.\n\0"
.section .text
.globl main
main:
lea format(%rip), %rdi
mov $55, %rsi
mov $0, %eax
sub $8, %rsp
call printf
add $8, %rsp
mov $0, %eax
ret
And it works fine when being compiled via gcc on ubuntu with -no-pie
:
ubuntu$ gcc -no-pie int.s -o int; ./int
Your number is: 55.
However, when I remove -no-pie
it jumps to the wrong address for the printf
statement. Single-stepping in gdb
shows that when it does the call printf
instruction it returns something along the lines of:
Cannot access memory at address 0x5554f7a48f00
Or, running it directly from the command-line I get:
$ gcc int.s -o int; ./int ./int: Symbol `printf' causes overflow in R_X86_64_PC32 relocation
Why does this occur, and how would I make the above program work without -no-pie
in the command-line?