如何编译内核? [英] How to compile kernel?

查看:104
本文介绍了如何编译内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的c内核,它使用nasm打印Hello world和另一个目标文件。我的问题是如何使用gcc编译c内核,然后通过链接两个目标文件并放置地址0x7e12来创建平面二进制内核。它应该是32位内核。我在Lubuntu 14.04



我尝试过:



i有看到一些样品并厌倦了这个,但这没有给出正确的答案。



Compile.sh:

I have created a simple c kernel that prints Hello world and an another object file using nasm. my question is how to compile c kernel with gcc and after that create flat binary kernel by linking both object files and put on address 0x7e12. It should be 32bit kernel. I am on Lubuntu 14.04

What I have tried:

i have seen some samples and tired this but this does not give correct thing.

Compile.sh:

gcc -c kernel.c -o kernel.o
<br />nasm base.asm -f elf -o base.o
<br />ld -oformat=binary kernel.o base.o -Ttext=0x7e12





base.asm:



base.asm:

[bits 32]
[extern _start]

call _start





kernel.c:



kernel.c:

void start { char *v = (char*) 0xb8000; *v="M";}

推荐答案

第一步是修复报告的错误和警告。

The first step is to fix the reported errors and warnings.
# gcc -c kernel.c
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token

之前预期'=',',',';','asm'或'__attribute__'它必须是

It must be

void start() { char *v = (char*) 0xb8000; *v="M";}



接下来尝试:


Next try:

# gcc -c kernel.c
warning: assignment makes integer from pointer without a cast

理解这个错误有点棘手。它抱怨 * v =M因为M是指针(指向常量字符串)。您的代码将无法按预期工作。您必须使用复制字符串或分配每个字符的函数:

Understanding this error is a bit tricky. It complains about *v="M" because "M" is a pointer (to a constant string). Your code won't work as expected. You have to use a function that copies a string or assign each character:

void start() 
{ 
    char *v = (char*)0xb8000; 
    v[0] = 'M';
    v[1] = '\0';
}



nasm 无处安装,所以我无法查看。



关于链接阅读文档。 -T 选项后面必须跟一个脚本文件。


I have nasm nowhere installed so I can't check it.

Regarding the linking read the documentation. The -T option requires to be followed by a script file.


这篇关于如何编译内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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