Parcourir la source

保护进阶, 向内核迈进

runningwater il y a 11 mois
Parent
commit
635bc3a2c9
6 fichiers modifiés avec 103 ajouts et 2 suppressions
  1. 1 1
      bochsrc
  2. 3 0
      build.sh
  3. 1 1
      clean.sh
  4. 2 0
      kernel/.gitignore
  5. 90 0
      kernel/kernel-crosscompiler.md
  6. 6 0
      kernel/main.c

+ 1 - 1
bochsrc

@@ -14,7 +14,7 @@ ata0-master: type=disk, mode=flat, path="hd30M.img", cylinders=60, heads=16, spt
 
 boot: disk
 
-log: bochsout.txt
+log: bochs_log.txt
 
 # logprefix: %t%e%d
 

+ 3 - 0
build.sh

@@ -6,12 +6,15 @@ bximage -q -func=create -hd=30M hd30M.img
 echo "Compiling..."
 nasm -I boot/include/ -o boot/mbr.bin boot/mbr.S
 nasm -I boot/include/ -o boot/loader.bin boot/loader.S
+i386-elf-gcc -c -o kernel/main.o kernel/main.c && i386-elf-ld kernel/main.o -Ttext 0xc0001500 -e main -o kernel/kernel.bin
 
 echo "Installing to disk image..."
 echo " 0. Writing mbr to disk..."
 dd if=boot/mbr.bin of=hd30M.img bs=512 count=1 conv=notrunc
 echo " 1. Writing loader to disk..."
 dd if=boot/loader.bin of=hd30M.img bs=512 count=4 seek=2 conv=notrunc
+echo " 2. Writing kernel to disk..."
+dd if=kernel/kernel.bin of=hd30M.img bs=512 count=2000 seek=9 conv=notrunc
 # echo " 2. Writing kernel to disk..."
 # TODO: Add code to write the kernel to the disk image
 echo "Disk image created successfully."

+ 1 - 1
clean.sh

@@ -1,3 +1,3 @@
 #!/bin/sh
 
-rm -rf bochsout.txt hd30M.img *.bin boot/*.bin
+rm -rf bochsout.txt hd30M.img *.bin boot/*.bin kernel/kernel.bin main.o

+ 2 - 0
kernel/.gitignore

@@ -0,0 +1,2 @@
+*.o
+*.bin

+ 90 - 0
kernel/kernel-crosscompiler.md

@@ -0,0 +1,90 @@
+
+# 目标:  创建开发环境,构建内核
+
+  如果你使用的是 Mac 系统,就需要些步骤。一旦我们使用高级语言(C 语言) 就需要跨平台编译,[具体参见][1]。
+
+  我们使用 GCC 来进行编译,请参考教程 [GCC Cross-Compiler][2]。因为 Mac 系统中 gcc 已经被 clang 所取代,所以我们需要自己安装 gcc, 并设置环境。
+
+# 安装步骤
+
+## 0. 安装依赖包
+
+  首先安装 [brew][0] 工具, 然后使用 `brew install`  安装如下包
+
+  1. gmp - GNU multiple precision arithmetic library
+  2. mpfr - C library for multiple-precision floating-point computations
+  3. libmpc - C library for the arithmetic of high precision complex numbers
+
+  >>>
+   configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+
+  >>>
+
+## 1. 设置安装目录及前缀
+
+  我的安装目录设置为 `/usr/local/i386elfgcc`,  然后把需要安装的 gcc  和 binutils 工具的命令都加上前缀`i386-elf-`, 避免跟系统的起冲突。
+
+  ```sh
+    export PREFIX="/usr/local/i386elfgcc"
+    export TARGET=i386-elf
+    export PATH="$PREFIX/bin:$PATH"
+  ```
+
+## 2. 下载 binutils 和 gcc 源码包
+  
+   创建目录 `mkdir /tmp/src`
+
+  ```sh
+    cd /tmp/src
+    curl -O http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz
+    tar xf binutils-2.24.tar.gz
+
+    curl -O https://ftp.gnu.org/gnu/gcc/gcc-13.3.0/gcc-13.3.0.tar.xz
+    tar xzf gcc-13.3.0.tar.xz    
+  ```
+
+## 3. 编译和安装
+
+  ```sh
+    cd /tmp/src
+    mkdir binutils-build
+    cd binutils-build
+    ../binutils-2.24/configure --target=$TARGET --enable-interwork --enable-multilib --disable-nls --disable-werror --prefix=$PREFIX 2>&1 | tee configure.log
+    make all install 2>&1 | tee make.log
+
+    cd /tmp/src
+    mkdir gcc-build
+    cd gcc-build
+    ../gcc-13.3.0/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-libssp --enable-languages=c --without-headers
+    make all-gcc 
+    make all-target-libgcc 
+    make install-gcc 
+    make install-target-libgcc
+  ```
+
+# 使用
+
+   使用的命令如下:
+   ![cross-gcc](https://img2023.cnblogs.com/blog/376834/202412/376834-20241205164135401-1223605732.png)
+
+## 编译
+
+  ```sh
+  i386-elf-gcc -ffreestanding -c kernel/main.c -o kernel/main.o
+  ```
+
+   使用 `i386-elf-objdump -d main.o` 来检查编译出来的机器码
+
+  ![comile-oper](https://img2023.cnblogs.com/blog/376834/202412/376834-20241205174838571-1626027210.png)
+
+## 链接
+  
+   使用命令 `i386-elf-ld -o kernel.bin -Ttext 0x0 -e main  main.o`  来链接
+ ![img](https://img2023.cnblogs.com/blog/376834/202412/376834-20241205175337429-2093414393.png)
+
+## 反汇编 
+
+  `ndisasm -b 32  kernel.bin`
+
+[0]: https://brew.sh/
+[1]: https://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F
+[2]: https://wiki.osdev.org/GCC_Cross-Compiler

+ 6 - 0
kernel/main.c

@@ -0,0 +1,6 @@
+int main(void)
+{
+    while (1)
+        ;
+    return 0;
+}