要开发自己的操作系统,首先就要搭建一个自己的调试开发环境,这里以Ubuntu系统(作者的是ubuntu 12.04.3)为例,来说明如何搭建交叉编译环境,有了交叉编译环境,才可以将系统内核编译为目标机子上...
$ sudo passwd root $ su - |
mkdir -p /mnt/zenglOX/opt/cross |
root@zengl:/mnt/zenglOX# tar xvf binutils-2.23.1.tar.bz2 root@zengl:/mnt/zenglOX# tar xvf gcc-4.7.2.tar.bz2 root@zengl:/mnt/zenglOX# tar xvf gmp-5.0.5.tar.xz root@zengl:/mnt/zenglOX# tar xvf mpfr-3.0.1.tar.xz root@zengl:/mnt/zenglOX# tar xvf mpc-1.0.1.tar.gz root@zengl:/mnt/zenglOX# tar xvf bochs-2.6.tar.gz |
root@zengl:/mnt/zenglOX# cd gcc-4.7.2 root@zengl:/mnt/zenglOX/gcc-4.7.2# ln -s ../gmp-5.0.5 gmp root@zengl:/mnt/zenglOX/gcc-4.7.2# ln -s ../mpc-1.0.1 mpc root@zengl:/mnt/zenglOX/gcc-4.7.2# ln -s ../mpfr-3.0.1 mpfr root@zengl:/mnt/zenglOX/gcc-4.7.2# cd .. root@zengl:/mnt/zenglOX# |
root@zengl:/mnt/zenglOX# mkdir binutils-build root@zengl:/mnt/zenglOX# cd binutils-build root@zengl:/mnt/zenglOX/binutils-build# ../binutils-2.23.1/configure --prefix=/mnt/zenglOX/opt/cross --target=i586-elf --disable-nls |
root@zengl:/mnt/zenglOX/binutils-build# make root@zengl:/mnt/zenglOX/binutils-build# make install |
root@zengl:/mnt/zenglOX/binutils-build# ls ../opt/cross/ bin/ i586-elf/ lib/ share/ root@zengl:/mnt/zenglOX/binutils-build# ls ../opt/cross/bin/ i586-elf-addr2line i586-elf-elfedit i586-elf-nm i586-elf-readelf i586-elf-ar i586-elf-gprof i586-elf-objcopy i586-elf-size i586-elf-as i586-elf-ld i586-elf-objdump i586-elf-strings i586-elf-c++filt i586-elf-ld.bfd i586-elf-ranlib i586-elf-strip |
root@zengl:/mnt/zenglOX# mkdir gcc-build root@zengl:/mnt/zenglOX# cd gcc-build root@zengl:/mnt/zenglOX/gcc-build# ../gcc-4.7.2/configure --prefix=/mnt/zenglOX/opt/cross --target=i586-elf --enable-languages=c,c++ --disable-nls --without-headers |
root@zengl:/mnt/zenglOX/gcc-build# make all-gcc root@zengl:/mnt/zenglOX/gcc-build# make install-gcc |
root@zengl:/mnt/zenglOX/gcc-build# ls ../opt/cross/bin/
i586-elf-addr2line i586-elf-g++ i586-elf-gprof i586-elf-readelf
i586-elf-ar i586-elf-gcc i586-elf-ld i586-elf-size
i586-elf-as i586-elf-gcc-4.7.2 i586-elf-ld.bfd i586-elf-strings
i586-elf-c++ i586-elf-gcc-ar i586-elf-nm i586-elf-strip
i586-elf-c++filt i586-elf-gcc-nm i586-elf-objcopy
i586-elf-cpp i586-elf-gcc-ranlib i586-elf-objdump
i586-elf-elfedit i586-elf-gcov i586-elf-ranlib
|
root@zengl:/mnt/zenglOX/gcc-build# make all-target-libgcc root@zengl:/mnt/zenglOX/gcc-build# make install-target-libgcc |
root@zengl:/mnt/zenglOX/gcc-build# ls ../opt/cross/lib/gcc/i586-elf/4.7.2/ -l
total 452
-rw-r--r-- 1 root root 2352 2月 23 16:42 crtbegin.o
-rw-r--r-- 1 root root 1316 2月 23 16:42 crtend.o
drwxr-xr-x 2 root root 4096 2月 23 16:42 include
drwxr-xr-x 2 root root 4096 2月 23 16:26 include-fixed
drwxr-xr-x 3 root root 4096 2月 23 16:36 install-tools
-rw-r--r-- 1 root root 403240 2月 23 16:42 libgcc.a
-rw-r--r-- 1 root root 30262 2月 23 16:42 libgcov.a
drwxr-xr-x 3 root root 4096 2月 23 16:36 plugin
|
root@zengl:/mnt/zenglOX/gcc-build# cd .. root@zengl:/mnt/zenglOX# |
root@zengl:/mnt/zenglOX# mkdir bochs-build root@zengl:/mnt/zenglOX# cd bochs-build root@zengl:/mnt/zenglOX/bochs-build# ../bochs-2.6/configure --enable-gdb-stub |
root@zengl:/mnt/zenglOX/bochs-build# apt-get install g++ root@zengl:/mnt/zenglOX/bochs-build# apt-get install xorg-dev |
root@zengl:/mnt/zenglOX/bochs-build# make root@zengl:/mnt/zenglOX/bochs-build# make install |
zengl@zengl:~/Downloads$ unzip zenglOX_v0.0.1.zip -d zenglOX_v0.0.1 zengl@zengl:~/Downloads$ cd zenglOX_v0.0.1 zengl@zengl:~/Downloads/zenglOX_v0.0.1$ |
#makefile for zenglOS GCC_PATH = /mnt/zenglOX/opt/cross/bin AS = $(GCC_PATH)/i586-elf-as CC = $(GCC_PATH)/i586-elf-gcc |
zengl@zengl:~/Downloads/zenglOX_v0.0.1$ make /mnt/zenglOX/opt/cross/bin/i586-elf-as zlox_boot.s -o zlox_boot.o -gdwarf-2 -g3 /mnt/zenglOX/opt/cross/bin/i586-elf-gcc -c zlox_kernel.c -o zlox_kernel.o -std=gnu99 -ffreestanding -gdwarf-2 -g3 -Wall -Wextra /mnt/zenglOX/opt/cross/bin/i586-elf-gcc -T linker.ld -o zenglOX.bin -ffreestanding -gdwarf-2 -g3 -nostdlib zlox_boot.o zlox_kernel.o zengl@zengl:~/Downloads/zenglOX_v0.0.1$ |
zengl@zengl:~/Downloads/zenglOX_v0.0.1$ make iso cp zenglOX.bin isodir/boot/zenglOX.bin cp grub.cfg isodir/boot/grub/grub.cfg grub-mkrescue -o zenglOX.iso isodir Enabling BIOS support ... xorriso 1.1.8 : RockRidge filesystem manipulator, libburnia project. |
zengl@zengl:~/Downloads/zenglOX_v0.0.1$ chmod +x startBochs |
romimage: file="/usr/local/share/bochs/BIOS-bochs-latest", address=0xfffe0000 vgaromimage: file="/usr/local/share/bochs/VGABIOS-elpin-2.40" |
zengl@zengl:~/Downloads/zenglOX_v0.0.1$ ./startBochs ======================================================================== Bochs x86 Emulator 2.6 Built from SVN snapshot on September 2nd, 2012 Compiled on Feb 23 2014 at 16:58:20 ======================================================================== 00000000000i[ ] reading configuration from bochsrc.txt 00000000000i[ ] Enabled gdbstub 00000000000e[ ] bochsrc.txt:16: 'vga_update_interval' will be replaced by new 'vga: update_freq' option. 00000000000e[ ] bochsrc.txt:25: 'i440fxsupport' will be replaced by new 'pci' option. 00000000000i[ ] installing x module as the Bochs GUI 00000000000i[ ] using log file bochsout.log Waiting for gdb connection on port 1234 |
zengl@zengl:~/Downloads/zenglOX_v0.0.1$ gdb -q zenglOX.bin Reading symbols from /home/zengl/Downloads/zenglOX_v0.0.1/zenglOX.bin...done. (gdb) break *_zlox_boot_start Breakpoint 1 at 0x100020: file zlox_boot.s, line 26. (gdb) target remote localhost:1234 Remote debugging using localhost:1234 0x0000fff0 in ?? () (gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x00100020 zlox_boot.s:26 (gdb) c Continuing. |
Breakpoint 1, code () at zlox_boot.s:26 26 pushl %ebx (gdb) |
(gdb) s 27 cli (gdb) s 28 call zlox_kernel_main (gdb) s zlox_kernel_main (mboot_ptr=0x10000) at zlox_kernel.c:4 4 return (int)mboot_ptr; (gdb) |
#zlox_boot.s -- zenglOX kernel start assemble .equ ZLOX_MBOOT_PAGE_ALIGN,1 #Load Kernel on a page boundary .equ ZLOX_MBOOT_GETMEM_INFO,1<<1 #Tell MBoot provide your kernel with memory info .equ ZLOX_MBOOT_MAGIC,0x1BADB002 #Multiboot Magic value .equ ZLOX_MBOOT_FLAGS,ZLOX_MBOOT_PAGE_ALIGN | ZLOX_MBOOT_GETMEM_INFO .equ ZLOX_MBOOT_CHECKSUM,-(ZLOX_MBOOT_MAGIC + ZLOX_MBOOT_FLAGS) .section .zlox_multiboot .align 4 .global _zlox_boot_mb_header _zlox_boot_mb_header: .long ZLOX_MBOOT_MAGIC .long ZLOX_MBOOT_FLAGS .long ZLOX_MBOOT_CHECKSUM .long _zlox_boot_mb_header .long _code .long _bss .long _end .long _zlox_boot_start |
+-------------------+ 0 | magic: 0x1BADB002 | (required) 4 | flags | (required) 8 | checksum | (required) +-------------------+ 8 | header_addr | (present if flags[16] is set) 12 | load_addr | (present if flags[16] is set) 16 | load_end_addr | (present if flags[16] is set) 20 | bss_end_addr | (present if flags[16] is set) 24 | entry_addr | (present if flags[16] is set) +-------------------+详情参考http://www.uruk.org/orig-grub/boot-proposal.html里的文章。
/* The bootloader will look at this image and start execution at the symbol designated as the entry point. */ ENTRY(_zlox_boot_start) /* Tell where the various sections of the object files will be put in the final kernel image. */ SECTIONS { /* Begin putting sections at 1 MiB, a conventional place for kernels to be loaded at by the bootloader. */ . = 1M; /* First put the multiboot header, as it is required to be put very early early in the image or the bootloader won't recognize the file format. Next we'll put the .text section. */ .text BLOCK(4K) : ALIGN(4K) { *(.zlox_multiboot) code = .; _code = .; __code = .; *(.text) } /* Read-only data. */ .rodata BLOCK(4K) : ALIGN(4K) { rodata = .; _rodata = .; __rodata = .; *(.rodata) } /* Read-write data (initialized) */ .data BLOCK(4K) : ALIGN(4K) { data = .; _data = .; __data = .; *(.data) } /* Read-write data (uninitialized) and stack */ .bss BLOCK(4K) : ALIGN(4K) { bss = .; _bss = .; __bss = .; *(COMMON) *(.bss) } end = .; _end = .; __end = .; /* The compiler may produce other sections, by default it will put them in a segment with the same name. Simply add stuff here as needed. */ } |