Using this code:
section .data
msg db "Most basic printf example in NASM", 0xA, 0xD, 0
len equ $-msg
section .bss
section .text
global _start
extern printf
_start:
mov rsi, msg
push rax ;; for stack alignment
call printf
fails with the error undefined reference to `printf'
I'm trying to create the simplest worked example of calling printf
from NASM.
I had read suggestions of adding -lc
to ld i.e.:
ld -o worked-example -lc worked-example.o
or
ld -o worked-example worked-example.o -lc
both prevent the error message - but then the file worked-example - can't be found with ./worked-example - even though it can be seen with cat
and ls
file worked-example
indicates:
worked-example: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld64.so.1, with debug_info, not stripped
EDIT: Based on comments below, I found the dynamic linker and tried to add that: ld worked-example.o -o worked-example -lc -dynamic-linker /usr/lib64/ld-linux-x86-64.so.2
which works. [With a segfault and nothing printed - but it compiles and runs]