66

I'm trying to create a simple kernel using Ubuntu. In the terminal I typed

ld -Ttext 0x1000 -o kernel.bin loader.o main.o Video.o

But I got the following error message in return:

ld: i386 architecture of input file `loader.o' is incompatible with i386:x86-64 output
ld: warning: cannot find entry symbol _start; defaulting to 0000000000001000
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
MEv2.0
  • 743
  • 2
  • 7
  • 8
  • 2
    [same question](https://askubuntu.com/questions/85978/building-a-32-bit-app-in-64-bit-ubuntu/85979#85979?newreg=9746c4d60a7740aa9d0d68f29667feb4) it would help you ,maybe! – Shangbin Dong Aug 02 '19 at 10:53

5 Answers5

89

If want compile the file as 32 bits, you can use:

ld -m elf_i386 -s -o file file.o
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Leandro Andrade
  • 993
  • 6
  • 9
  • 9
    This was the solution. When compiling/linking 32-bit apps on x86_64, setting emulation to `elf_i386` provides the correct elf format. So, for example, if you compile an assembler app with `nasm -f elf file.asm -o file.o`, the link command is `ld -m elf_i386 -o exename file.o`. – David C. Rankin Sep 04 '14 at 03:38
  • 4
    Just don't forget to replace `-o file.o file` with `-o file file.o`. – Ruslan Apr 21 '16 at 21:10
  • It would be great to set this as the solution if it works indeed. – Balázs Börcsök Aug 16 '21 at 16:05
53

Use 64 bits instead of 32 for your loader and compile it with the following command:

nasm -f elf64 loader.asm -o loader.o

This should solve your error

Romasz
  • 29,662
  • 13
  • 79
  • 154
Drill
  • 537
  • 4
  • 3
7

When compiling/linking 32-bit apps on x86_64, setting emulation to elf_i386 provides the correct elf format. So, for example, if you compile an assembler app with nasm -f elf file.asm -o file.o, the link command is ld -m elf_i386 -o exename file.o Courtesy: David

4aRk Kn1gh7
  • 4,259
  • 1
  • 30
  • 41
1

I also faced the same problem, i figured out that i am 32 bit registers(eax,ecx,edx,ebx,esp,ebp,esi,edi) insist of 64 bit registers (rax,rcx,rdx,rbx,rsp,rbp,rsi,rdi), in my 64 bit computer. Then use these command to compile my program-

nasm -felf64 hello.asm
ld hello.o
./a.out
  • The 32 bit registers still exists in 64 bit mode, but you cannot just compile a 32 bit assembly programm to 64 bit without rewriting it. Porting to 64 bit can also make it necessary to adjust the size of data structures, as pointers become twice as large. – fcdt Aug 26 '20 at 23:32
  • Exactly , you are right .But when we want to write code 0x86-64 (64 bit) assembly program, we have to use 64 bit registers.Basically faced the same problem and got the solution using these instruction .Correct if am wrong – Shanu Verma Aug 27 '20 at 13:29
1

On Windows, I faced following issue

ld: i386 architecture of input file `file.o' is incompatible with i386:x86-64 output

ld -m i386pe -s -o file.exe file.o , worked for me

Rajeshwar_
  • 11
  • 2