1

I have a problem with linking files in order to get my Operating System (Thunder) to work. I compile start.o with:

nasm -f aout start.o start.asm

When I link the bootloader and kernel together it gives me this error:

start.o: File not recognised:file format not recognised.

Any suggestions on why I'm getting this error and how I can fix it?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • You need to show us the code, and how you link the files. What platform are you on? If the `file` command is available it would be curious to know what `file start.o` returns. – Michael Petch May 15 '17 at 19:52
  • @CoryKramer. This is marked as a duplicate of another question but the two things aren't related. This may be a poor question but it's not a duplicate of _What is an undefined reference/unresolved external symbol error and how do I fix it?_ . This question is really about why the linker doesn't like the object files format. It isn't about a symbol that can't be found, it is about an object file that can't be processed because the file format isn't recognized. – Michael Petch May 15 '17 at 19:55
  • Michael, it outputs that it is a Linux i686 file. – Timothy Williams May 15 '17 at 20:01
  • 1
    Can you copy and paste the exact output of `file start.o` (i'm assuming your system has the program `file`). As well, you are you on Windows? (If on Windows are you using Cygwin or MinGW?) Or are you on Linux or OS/X or something else? – Michael Petch May 15 '17 at 20:03
  • You originally had this question tagged c++. Given that the error seems from a GNU C/C++ toolchain (_LD_), it has me wondering. Did you compile `start.o` with some command that is similar to this (may have other options and different file names): `g++ -c start.cpp somefile.h -o start.o` . If you use _G++_ to compile (with `-c`) and specify a header file on the command line with an output file specified (a `-o` option) then _G++_ will create a precompiled header file and place it in `start.o`. Those can't be linked. If you have header files as part of _G++_ compilation lines - remove them. – Michael Petch May 15 '17 at 22:01
  • Id you send me your `start.o` file in email to mpetch@gmail.com I should be able to tell why it isn't an acceptable format. – Michael Petch May 15 '17 at 22:05
  • I use this:ld -T link.ld -o kernel.bin boot.o. I am using Cygwin on Windows 10.0.14393 – Timothy Williams May 23 '17 at 20:47
  • I received your `start.o` file. It is very peculiar. The type is listed as "Linux/i386 impure executable (OMAGIC)" . Impure executable is rather odd. I'm not sure how Cygwin handles things, but I'm wondering if you are generating `start.o` from `start.c` incorrectly and the linker doesn't know how to deal with Linux/i386 files like this. Can you tell me what command you use to compile `start.c` so that it becomes `start.o`? – Michael Petch May 23 '17 at 21:13
  • That is NASM, not C. I use nasm -f aout start.o start.asm. – Timothy Williams May 24 '17 at 07:17
  • Well since you don't show a minimal complete example I can't tell what it is. Well that is the problem `a.out` is not a file that can be linked by all linkers. I don't think the Cygwin variants of _LD_ (GNU Linker) support the format. Have you tried possibly `win32` or `elf32` instead of `a.out`? – Michael Petch May 24 '17 at 13:27
  • Win32 worked mate. I used MinGW and it now works! – Timothy Williams May 24 '17 at 21:00

1 Answers1

2

The error you are getting suggests that LD doesn't understand the file format of start.o and can't process it as an object file. When I ran file start.o on your object it gave me this output:

start.o: Linux/i386 impure executable (OMAGIC)

You are using Cygwin on Windows. You say you used this line to assemble start.asm to start.o:

nasm -f aout start.o start.asm

The Cygwin linker (LD) doesn't understand the aout format as a valid object file format. What you can do is have NASM output a Microsoft Win32 (i386) object file by using the -fwin32 parameter. LD should be able to use that object file format during the linking stage.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • this should be the answer `nasm -f aout -o start.o start.asm -fwin32` Btw I am using windows linux subsytem (WSL). then i got this error `i386 architecture of input file start.o is incompatible with i386:x86-64 output` when using `ld`, the solution for this here https://stackoverflow.com/questions/19200333/architecture-of-i386-input-file-is-incompatible-with-i386x86-64. – Adam Mudianto Dec 14 '20 at 10:40