Linking a file using ld to output a binary file gives error in OS development

吃可爱长大的小学妹 提交于 2019-12-04 13:09:40

Your Windows version of LD likely doesn't support anything more than windows PE types. One way around this is to output to PE and then use objcopy to convert from the PE file to binary.

For this to work you will have to rename main to _main. With -ffreestanding GCC will emit an object without the Windows ABI convention of prepending a leading underscore to non-static functions. We'll be using your LD to output a Windows PE file first and it will complain about __main entry point not being defined. To get around this you rename main to _main so the linker doesn't complain.

Use these instructions for generating the kernel binary:

gcc -ffreestanding -c -m16 kernel.c -o kernel.o
ld -Ttext 0x10000 -o kernel.pe kernel.o
objcopy -O binary kernel.pe kernel.bin

The LD command outputs to a file called kernel.pe. objcopy converts kernel.pe to binary with -O binary outputting to kernel.bin

This means that ld itself is not configured at its compile time to support output formats other than PE, Portable Executable - the native Windows executable file format.

Find the one that supports or build it yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!