0

So I have been working with Assembly for a while now and I wanted to try using printf in assembly.

I looked up how to do it on multiple sites and the gist I got was something along the lines of this:

global main
extern printf

section .data
    msg db "Hello, World!", 0x0a, 0x00


section .text

_start:
    push ebp
    mov ebp, esp
    push msg
    call printf
    mov eax, 0
    leave
    ret

I then assemble with NASM using:

nasm -f elf32 printing.asm -o printing.o

And link with GCC using:

gcc -m32 printing.o -o printing

And I get this error:

/usr/bin/ld: final link failed: Operation not supported
collect2: error: ld returned 1 exit status

As far as I know the commands were right so how can I fix this error? I did look into it for a bit and found nothing relevant, the only conclusion I can think up is maybe I need to fix some references to ld?

  • 1
    Was that the *only* error message? No earlier messages from the `gcc` command? I'd expect "multiple definition of `_start`" because you defined your own but didn't use `-nostartfiles`. – Peter Cordes Jan 31 '21 at 00:05
  • Just rename `_start` to `main`. Since you are calling `printf` you want gcc to initialize the C library. – Piotr P. Karwasz Jan 31 '21 at 00:17
  • @PiotrP.Karwasz I have done that (my mistake) now I get`/usr/bin/ld: final link failed: Operation not supported` – lowlevellarry Jan 31 '21 at 00:25
  • 1
    Is that really the only line of output from your `gcc` command? That seems very unlikely. But if so, what OS and what GCC version / setup? I don't think missing lib32 packages could explain that error. – Peter Cordes Jan 31 '21 at 00:44
  • Ubuntu 20.04 and the base GCC – lowlevellarry Jan 31 '21 at 03:57

0 Answers0