0

I've started writing my very own kernel and am developing on Windows 10.

I am using the following tools:

  • gcc 8.1.0 elf x86_64 for compiling my C code, and for linking.
  • nasm 2.14.02 for compiling my assembly code.

I am using the following command to build my freestanding kernel code:

gcc -c -m64 common/src/kernel.c -o common/build/kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra`

I am using the following command for compiling the root assembly code:

nasm -f elf64 targets/x86_64/src/main.asm -o targets/x86_64/build/main.o

Finally I link the object files together:

gcc -o dist/x86_64/main.bin -ffreestanding -O2 targets/x86_64/build/main.o common/build/kernel.o -lgcc

I haven't a clue why, but the following error is stopping the linking process from working:

... undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

This doesn't make sense to me because my compilation and linking is in freestanding mode and I am not attempting to use any windows APIs.

How do I fix this?


If it matters, here is my very simple code:

main.asm:

EXTERN kernel_main
call kernel_main
jmp $

; padding and magic number
times 510-($-$$) db 0
dw 0xaa55

kernel.c:

void kernel_main()
{
  // empty
}

Edit: It looks like I might need the x86_64-elf-gcc compiler

Edit 2: I tried the x86_64-w64-mingw32-gcc compiler, same error occurs

David Callanan
  • 5,601
  • 7
  • 63
  • 105
  • Apart from the linker error (that you can fix with `-nostdlib`), what you are trying to do will not work in this form. The boot sector needs to be 16 bit real mode code which then switches to 64 bit long mode to be able to run your 64 bit C kernel. Also you need to make sure your asm stuff will be placed at the beginning of the binary and it will have to load the kernel at runtime since the BIOS only loads the first sector. – Jester Mar 23 '20 at 19:25
  • @Jester Thanks for reminding me about that – David Callanan Mar 23 '20 at 19:28
  • Oh, and your output will likely be a PE executable, not a flat binary. – Jester Mar 23 '20 at 19:35

1 Answers1

0

I was missing the -nostdlib flag on the linker. This seems to have solved the problem.

David Callanan
  • 5,601
  • 7
  • 63
  • 105