Search code examples
assemblynasmld

Linux Assembly - Could not read symbols: File in wrong format


I am trying to compile an asm program on a linux server. I am new to writing asm programs, but I follow my school's tutorial and do everything they say but it has no success.

Here is my hello.asm file.

    SECTION .data
msg:    db "Hello World",10
len:    equ $-msg
    SECTION .text
        global main
main:
    mov edx,len
    mov ecx,msg
    mov ebx,1
    mov eax,4
    int 0x80
    mov ebx,0
    mov eax,1
    int 0x80

I compile this file using the command line nasm -f elf hello.asm which works completely fine and generates an object file. The problem is, when I try ld hello.o, it says that it cannot read symbols in the file and the file is in the wrong format.

Can anyone help me and tell me how I can compile a .asm file so it can run?


Solution

  • I've had a similar problem today, as a result of mixed 32-bit and 64-bit libraries (which is why I happen to have come upon your question). ld will complain if you try to link libraries of different types. On a 64-bit system, ld defaults to 64-bit.

    To specify the type you should be able to specify ld -m elf_i386 for 32-bit, or gcc -m32. (Or gcc -m32 -static -nostdlib for a static binary).

    If your asm source code is portable to 64-bit, you could assemble as 64-bit with nasm -f elf64. But it can assemble but fault at runtime from truncating a pointer, or have system calls return errors instead of working because the int 0x80 ABI is purely 32-bit.