0

I was following the osdev barebones tutorial and made a custom Makefile to it, but when I ran make kernel to compile the system into a .bin file, I ran into an error while linking.

Linker output: src/kernel.o: file not recognized: file format not recognized

Linker command: $HOME/opt/cross/bin/i686-elf-gcc -T linker.ld -o kernel.bin -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc

I took a quick look into it, and I found out that i686-elf-gcc cross-compiler produced a 64-bit file. Weird. I tried compiling kernel.c with the same flags, and it worked just fine. What could the problem be?

CPARAMS: -std=gnu99 -ffreestanding -O2 -Wall -Wextra

MAKEFILE: $(GCC) $(CPARAMS) -c $@ -o $<

So the actual command looks like this $HOME/opt/cross/bin/i686-elf-gcc -std=gnu99 -ffreestanding -O2 -Wall -Wextra -c kernel.c -o kernel.o

  • File produced by running compiler via make: kernel.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

  • File produced by running compiler from shell: kernel.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

  • Did you tried with `-m32`? – Eraklon Mar 23 '20 at 17:51
  • yes, I have tried, but I still get 64-bit file. This doesn't make any sense to me. – Jozef Nagy Mar 23 '20 at 17:55
  • I'd run `file` or `readelf` on `src/kernel.o` to see what arch/format it is. But, your makefile produces 64 bit and, by hand, you get [what you want] 32 bit. This seems to indicate that something about your makefile [_or_ the environment `make` produces] causes the issue. I'd write a thin wrapper script that passes stuff to the real `gcc` but prints the environment variables [and arguments] to `stderr`. Also, it's a bit labor intensive, but you could use `strace` on the target `gcc` [wrapper could do this]. I'm not familiar with `-ffreestanding`. Do you get better results if you leave it off? – Craig Estey Mar 23 '20 at 18:50
  • That is strange. What is the output of this command `$HOME/opt/cross/bin/i686-elf-gcc -v 2>&1 | grep Target` . What does your `linker.ld` have in it? Almost seems like your ELF i686 tool chain may have been built incorrectly or you aren't running the GCC you think you are. – Michael Petch Mar 23 '20 at 18:55
  • In fact can you just give all the output for `$HOME/opt/cross/bin/i686-elf-gcc -v` command? I'd like to see the options that it thinks were used for configuring the build of GCC you are using. – Michael Petch Mar 23 '20 at 18:56
  • Can you post your makefile [and linker script and/or anything else that is part of your build process that you created]? – Craig Estey Mar 23 '20 at 18:57
  • What is the exact origin of the cross compiler [and the other cross development tools]? (e.g.) Did you build them yourself from source, or what package did you install? – Craig Estey Mar 23 '20 at 19:05
  • I agree with Craig - show us the Makefile . I'm wondering if the GCC your makefile is running is the system one called `gcc` rather than the cross compiler version. Would explain why one produces 32-bit code and the other doesn't. – Michael Petch Mar 23 '20 at 19:33
  • [Here is the whole project](https://github.com/wisfor/w-agos) – Jozef Nagy Mar 23 '20 at 19:50
  • Michael, the target is i686-elf. – Jozef Nagy Mar 23 '20 at 19:51
  • Craig, I compiled the cross-compile toolchain on my computer from source. The links to the exact tarballs are in the README.md – Jozef Nagy Mar 23 '20 at 19:52

0 Answers0