[转载]Linux 环境下编译 0.11版本内核 kernel

时间:2023-03-09 08:03:30
[转载]Linux 环境下编译 0.11版本内核 kernel

最近在看《、如果Clobber/Modify 为空,则其前面的冒号(:)必须省略。

2、如果Output,Input,Clobber/Modify都为空,Output,Input之前的冒号(:)既可以省略,也可以不省略。

3、如果Input,Clobber/Modify为空,但Output不为空,Input前的冒号(:)既可以省略,也可以不省略。

4、如果后面的部分不为空,而前面的部分为空,则前面的冒号(:)都必须保留,否则无法说明不为空的部分究竟是第几部分。

每一个Input和Output表达式都必须指定自己的操作约束Operation Constraint,这里将讨论在80386平台上所可能使用的操作约束。

当前的输入或输出需要借助一个寄存器时,需要为其指定一个寄存器约束,可以直接指定一个寄存器的名字。

常用的寄存器约束的缩写

约束 意义

r 表示使用一个通用寄存器,由 GCC 在%eax/%ax/%al,%ebx/%bx/%bl,%ecx/%cx/%cl,%edx/%dx/%dl中选取一个GCC认为合适的。

g 表示使用任意一个寄存器,由GCC在所有的可以使用的寄存器中选取一个GCC认为合适的。

q 表示使用一个通用寄存器,和约束r的意义相同。

a 表示使用%eax/%ax/%al

b 表示使用%ebx/%bx/%bl

c 表示使用%ecx/%cx/%cl

d 表示使用%edx/%dx/%dl

D 表示使用%edi/%di

S 表示使用%esi/%si

f 表示使用浮点寄存器

t 表示使用第一个浮点寄存器

u 表示使用第二个浮点寄存器

如果一个Input/Output 操作表达式的C/、make

In file included from stat.c:13:

../include/asm/segment.h: Assembler messages:

../include/asm/segment.h:27: Error: bad register name '%sil'

make[1]: *** [stat.o] Error 1

make[1]: Leaving directory '***/linux-0.11/fs'

make: *** [fs/fs.o] Error 2

出错原因:

fs 目录下的 Makefile 中编译选项使用了 -O 优化选项导致寄存器错误

解决方法:

将fs目录下的Makefile 文件中的

CFLAGS =-Wall -O -fstrength-reduce -fomit-frame-pointer \

修改为

CFLAGS =-Wall -fstrength-reduce -fomit-frame-pointer \

14、make

tools/build.c: In function 'main':

tools/build.c:75: warning: implicit declaration of function 'MAJOR'

tools/build.c:76: warning: implicit declaration of function 'MINOR'

tmp/ccsMKTAS.o: In function 'main':

build.c:(.text+0xe1): undefined reference to 'MAJOR'

build.c:(.text+0xf7): undefined reference to 'MINOR'

collect2: ld returned 1 exit status

出错原因:'MAJOR' 和 'MINOR' 未定义

解决办法:

我们可以在 include/linux/fs.h 文件中找到

#define MAJOR(a) (((unsigned)(a))>>8)

#define MINOR(a) ((a)&0xff)

而在 tools/build.c 中也有包含 #include <linux/fs.h>

那么再看第一层目录中的主 Makefile 文件

tools/build: tools/build.c

$(CC) $(CFLAGS) \

-o tools/build tools/build.c

好象确实没有引用头文件

简单的添加 -Iinclude

重新编译后出现一堆报标准C库头文件的错误

再添加 -nostdinc

又报 stderr fprintf 之类的错误

没折,只好将

#define MAJOR(a) (((unsigned)(a))>>8)

#define MINOR(a) ((a)&0xff)

添加到 tools/build.c 文件中,然后删除 #include <linux/fs.h>

15、make

make[1]: Leaving directory '***/linux-0.11/lib'

ld -s -x -M boot/head.o init/main.o \

kernel/kernel.o mm/mm.o fs/fs.o \

kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.a \

kernel/math/math.a \

lib/lib.a \

-o tools/system > System.map

ld: warning: cannot find entry symbol _start; defaulting to 08048a0

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer \

-o tools/build tools/build.c

tools/build boot/bootsect boot/setup tools/system /dev/hd6 > Image

/dev/hd6: No such file or directory

Couldn't stat root device.

make: *** [Image] Error 1

解决办法:

将第一层主 Makefile 文件中的

tools/system: boot/head.o init/main.o \

$(ARCHIVES) $(DRIVERS) $(MATH) $(LIBS)

$(LD) $(LDFLAGS) boot/head.o init/main.o \

$(ARCHIVES) \

$(DRIVERS) \

$(MATH) \

$(LIBS) \

-o tools/system > System.map

修改为

tools/system: boot/head.o init/main.o \

$(ARCHIVES) $(DRIVERS) $(MATH) $(LIBS)

$(LD) $(LDFLAGS) boot/head.o init/main.o \

$(ARCHIVES) \

$(DRIVERS) \

$(MATH) \

$(LIBS) \

-o tools/system

nm tools/system | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aU] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)'| sort > System.map

nm命令将目标文件中的各种符号列出来。

ROOT_DEV=/dev/hd6 修改为 ROOT_DEV=

16、make

/DISCARD/

*(.note.GNU-stack)

*(.gnu_debuglink)

*(.gnu.lto_*)

OUTPUT(tools/system elf32-i386)

nm tools/system | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aU] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)'| sort > System.map

nm: tools/system: no symbols

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer \

-o tools/build tools/build.c

tools/build boot/bootsect boot/setup tools/system > Image

Root device is (3, 6)

Boot sector 512 bytes.

Setup is 312 bytes.

Non-Gcc header of 'system'

make: *** [Image] Error 1

解决办法:

将第一层主 Makefile 文件中的

LDFLAGS =-s -x -M

修改为

LDFLAGS =-m elf_i386 -Ttext 0 -e startup_32

Image: boot/bootsect boot/setup tools/system tools/build

tools/build boot/bootsect boot/setup tools/system $(ROOT_DEV) > Image

sync

修改为

Image: boot/bootsect boot/setup tools/system tools/build

objcopy -O binary -R .note -R .comment tools/system tools/kernel

tools/build boot/bootsect boot/setup tools/kernel $(ROOT_DEV) > Image

rm tools/kernel -f

sync

objcopy命令能复制和转化目标文件

objcopy -O binary -R .note -R .comment tools/system tools/kernel

-O binary tools/system tools/kernel将 tools/system 生成二进制文件 tools/kernel

-R .note -R .comment 删除 .note段 和 .comment 段

将 tools/build.c 文件中的

if (((long *) buf)[5] != 0)

die("Non-GCC header of 'system'");

这段代码注释掉

//if (((long *) buf)[5] != 0)

// die("Non-GCC header of 'system'");

17、make

ld -m elf_i386 -Ttext 0 -e startup_32 boot/head.o init/main.o \

kernel/kernel.o mm/mm.o fs/fs.o \

kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.a \

kernel/math/math.a \

lib/lib.a \

-o tools/system

nm tools/system | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aU] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)'| sort > System.map

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer \

-o tools/build tools/build.c

objcopy -O binary -R .note -R .comment tools/system tools/kernel

tools/build boot/bootsect boot/setup tools/system > Image

Root device is (3, 6)

Boot sector 512 bytes.

Setup is 312 bytes.

System is 128323 bytes.

rm tools/kernel -f

sync

终于编译 linux 内核 0.11 版本成功了!

最 后也可以利用赵炯博士在http://www.oldlinux.org/Linux.old/kernel/0.1x/ 这里提供了修改 linux-0.11-060618-gcc4.tar.gz 好的 0.11版本的内核进行编译,只要修改以下 Makefile 里 -mcpu=i386 为 -march=i386 还需要将 kernel/blk_drv/blk.h 文件第87行 将 #elif 修改为 #else 就可以编译通过了。

总结:编译需要一个过程,学习也是同样需要一个过程。虽然可以利用赵博士修改好的 kernel-0.11 版快速的编译内核,但是那样就不会遇到太多有价值的编译问题,而解决这些问题就是一个学习过程,相信赵博士在编译0.11版本内核的时候也遇到了这些问 题。这样我想起了高中解数学难题的时候,高手解体时总是省略了一些因式分解的过程,而对于菜鸟来说这些省略的过程是非常重要的。