本文參考:http://blog.chinaunix.net/uid-24807808-id-3070257.html
linux-0.00下載地址:http://oldlinux.org/Linux.old/bochs/linux-0.00-050613.zip
原來的Makefile改爲:
# Makefile for the simple example kernel.
AS86 =as86 -0 -a
LD86 =ld86 -0
AS =as
LD =ld -Ttext 0
LDFLAGS =-s -x -M
all: Image
Image: boot system
dd bs=32 if=boot of=Image skip=1
objcopy -O binary system head
cat head >> Image
disk: Image
dd bs=8192 if=Image of=/dev/fd0
sync;sync;sync
head.o: head.s
system: head.o
$(LD) $(LDFLAGS) head.o -o system > System.map
boot: boot.s
$(AS86) -o boot.o boot.s
$(LD86) -s -o boot boot.o
clean:
rm -f Image System.map core boot head *.o system
~
另外:最近在看趙炯老師的《Linux內核完全剖析》,在搭好bochs環境後,就想用用bochs,所以,按照書上說的,下載了一個測試版本,就做Linux-0.00內核。下面就說一下在bochs中編譯Linux-0.00內核遇到的一下問題。
首先,下載Linux-0.00內核源碼包,地址: http://oldlinux.org/Linux.old/bochs/linux-0.00-050613.zip
下載好後,進行解壓,解壓後的文件中有一個名爲linux-0.00的壓縮包,對其進行解壓
進入linux-0.00,make編譯。會發現make不成功,只有boot.s編譯通過,而head.s編譯卻沒有通過。
我們只需要根據提示的編譯錯誤對源碼進行修改:
1. 將 movl src_loc, %bx
改爲:movl src_loc, %ebx
原因:%ebx寄存器是32位,而%bx寄存器時16位。其實,%ebx是%bx的擴展,e就是extend
因爲過去的機子寄存器時16位,從80386以後,寄存器大小都改爲32位。
將 movl $65, %al
改爲:movb $65, %al
原因:由於字長的改變,現在只能將一個字節放入%ax的第八位%al中。
movl中的l是long, movb中的b是byte.
2. 錯誤:boot/head.s: Assembler messages:
boot/head.s:231: Error: alignment not a power of 2
make: *** [boot/head.o] Error 1
將head.s中所有的.align 2改爲.align 4, .align 3 改爲 .align 8
原因:.align 2 是彙編語言指示符,其含義是指存儲邊界對齊調整
“2”表示把隨後的代碼或數據的偏移位置調整到地址值最後2比特位爲零的位置(2^2),即按4字節對齊內存地址。
不過現在GNU as直接是寫出對齊的值而非2的次方值了。
3. 現在可以試着編譯,但是發現還會有關於startup的錯誤:
d: warning: cannot find entry symbol _start; defaulting to 0000000008048054
boot.o: In function `start': (.text+0x1): relocation truncated to fit: R_386_16 against `.text'
boot.o: In function `ok_load': (.text+0x3f): relocation truncated to fit: R_386_16 against `.text'
boot.o: In function `ok_load': (.text+0x44): relocation truncated to fit: R_386_16 against `.text'
boot.o: In function `gdt_48': (.text+0x71): relocation truncated to fit: R_386_16 against `.text'
可以將head.s中的 startup: 改爲 _startup: 指明程序的入口地址