[buug] cannot execute binary file or use printf

Windsor Schmidt windsor.schmidt at gmail.com
Tue Apr 14 00:44:01 PDT 2009

John, some info based on a couple of tests using my i386 Linux box...

In your first example on the first line, try adding a period "."
before the word "text" so it reads:

.section .text

For your second example, passing the -lc and -lm options to ld didn't
help me with the undefined reference to printf and exit, so I used gcc
instead. After I changed the label name to "main", assembled with as,
and linked with gcc using something like the following, it worked fine
for me:

$ as -o add.o add.s
$ gcc add.o -o add
$ ./add
31 = 9 + 22

The listing, after I modified it, looked like this:

.section .data
       .asciz "%d = %d + %d\n"
.section .text
.globl main
       movl $9, %eax
       movl $22, %ebx
       pushl %ebx
       pushl %eax
       xadd %eax, %ebx
       pushl %ebx
       pushl $output
       call printf
       call exit
Hope that helps,


On Mon, Apr 13, 2009 at 7:43 PM, Ian Zimmerman <itz at buug.org> wrote:
> If I try to use printf I can't link:
> -------------------------------
> john at ldev:~/asm/test$ cat add.s
> .section .data
> output:
>        .asciz "%d = %d + %d\n"
> .section .text
> .globl _start
> _start:
>        nop
>        movl $9, %eax
>        movl $22, %ebx
>        pushl %ebx
>        pushl %eax
>        xadd %eax, %ebx
>        pushl %ebx
>        pushl $output
>        call printf
>        call exit
> john at ldev:~/asm/test$ as ./add.s -o add.o
> john at ldev:~/asm/test$ ld -dynamic-linker /lib/ld-linux.so.2 -o add add.o
> add.o: In function `_start':
> (.text+0x17): undefined reference to `printf'
> add.o: In function `_start':
> (.text+0x1c): undefined reference to `exit
> john at ldev:~/asm/test$
> If you execute ld explicitly (as opposed to having gcc execute it for you),
> you must also list all libraries, including libc.  So adding -lc (and possibly
> -lm) to your command should help.
> I assume your system is Linux .. you know better than not telling us :)
> Not sure exactly about the first problem, but it happens to me too.
> I also tried it with gcc:
> itz at matica:~$ gcc -nostartfiles -nostdlib -o jump jump.s
> itz at matica:~$ ./jump
> bash: ./jump: cannot execute binary file
> Have you upgraded the kernel recently?  I did.
> --
> Ian Zimmerman <itz at buug.org>
> gpg public key: 1024D/C6FF61AD
> fingerprint: 66DC D68F 5C1B 4D71 2EE5  BD03 8A00 786C C6FF 61AD
> Ham is for reading, not for eating.
> _______________________________________________
> Buug mailing list
> Buug at weak.org
> http://www.weak.org/mailman/listinfo/buug

More information about the buug mailing list