链接使用LD输出二进制文件的文件给出了操作系统的开发错误 [英] Linking a file using ld to output a binary file gives error in OS development

查看:380
本文介绍了链接使用LD输出二进制文件的文件给出了操作系统的开发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习操作系统教程。我创建了2个文件。

I am learning Operating system tutorials. I created 2 files.


  1. boot.asm

  2. kernel.c

的kernel.c如下:

The kernel.c is as follows :

int main()
{
  char *src = (char *)0xB8000000L;
  *src = 'M';
  src += 2;
  *src = 'D';
  return 0;
}

内核是用来写一个字符文本模式视频显示区域。内核采用的 GCC 的Windows版本的编译:

The kernel is used to write a character to the text mode video display area. The kernel was compiled using Windows version of GCC with:

gcc -ffreestanding -c -m16 kernel.c -o kernel.o

我使用的内核对象链接到一个二进制文件的 LD 的:

ld -Ttext 0x10000 --oformat binary -o kernel.bin kernel.o

我得到的错误是:

The error I get is:

LD:不能链接这不是PE文件的可执行类型

ld : cannot link the file which is not PE executable type

有人能解决这个问题?


  • OS中使用:窗户

  • 编译器:GCC

  • 链接器:LD

推荐答案

您的Windows版本的 LD 的可能不支持除Windows PE种什么。围绕这一种方法是输出到PE,然后使用 objcopy把从PE文件的二进制转换。

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.

对于这个工作,你将不得不重新命名 _main 。随着 -ffreestanding GCC 的将发出一个对象的没有 $ P $的ppending领先Windows的ABI会议强调非-static功能。我们将使用您的 LD 的输出一个Windows PE文件第一个,它会抱怨主要__ 切入点没有被定义的。为了解决这个问题,你重命名 _main 因此链接器不抱怨。

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

LD 的命令输出到一个名为 kernel.pe objcopy把转换 kernel.pe 二进制-O二进制输出到 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

这篇关于链接使用LD输出二进制文件的文件给出了操作系统的开发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆