转自: http://wiki.osdev.org/Fasm-TCC_BareBones
The purpose of this article is to explain how to make a sample ELF kernel with FASM and Tiny C Compiler (aka TCC). TCC is a small and fast C compiler, which produces x86, x86_64 or ARM code, and generates PE or ELF executables. TCC is heading toward full ISOC99 compliance, and can compile itself, like FASM. Here are all features of TCC:
- TCC compiles C code about 9 times faster than GCC.
- TCC generates averagely optimized x86 code.
- TCC supports the ISO C99 standard (even if this support is not as complete as PCC).
- Under Linux, TCC can be used as a C interpreter (just add '#!/usr/local/bin/tcc -run' at the first line of your C source, and execute it directly from the command line).
- TCC has few dependencies. Only libc is required for its compilation.
- TCC includes a linker and an assembler (for x86 only).
Contents |
A small kernel example
This little example builds a small kernel in ELF format, which can be booted by Grub.
Note: The Windows version of TCC doesn't produces ELF executables, but only object files. You need to recompile TCC without PE support, if you want to use this tutorial on Windows.
start32.asm
; Tutorial: A small kernel with Fasm & TCC ; By Tommy. format elf use32 ; ; Equates ; MULTIBOOT_PAGE_ALIGN equ (1 shl 0) MULTIBOOT_MEMORY_INFO equ (1 shl 1) MULTIBOOT_AOUT_KLUDGE equ (1 shl 16) MULTIBOOT_HEADER_MAGIC equ 0x1BADB002 MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN or MULTIBOOT_MEMORY_INFO MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) section '.text' executable ; ; Multiboot header ; dd MULTIBOOT_HEADER_MAGIC dd MULTIBOOT_HEADER_FLAGS dd MULTIBOOT_CHECKSUM ; ; Kernel entry point. ; public _start extrn kmain _start: ; Call the main kernel function. call kmain @@: jmp @b
kernel.c
/* Tutorial: A small kernel with Fasm & TCC * By Tommy. */ /* * Main kernel function. */ void kmain (void) { *((unsigned char *) 0xB8000) = 'H'; *((unsigned char *) 0xB8001) = 0x1F; *((unsigned char *) 0xB8002) = 'E'; *((unsigned char *) 0xB8003) = 0x1F; *((unsigned char *) 0xB8004) = 'L'; *((unsigned char *) 0xB8005) = 0x1F; *((unsigned char *) 0xB8006) = 'L'; *((unsigned char *) 0xB8007) = 0x1F; *((unsigned char *) 0xB8008) = 'O'; *((unsigned char *) 0xB8009) = 0x1F; }
Compiling and linking
Assemble start32.asm with:
fasm start32.asm
Compile kernel.c with:
tcc -c kernel.c
Then link the whole thing with:
tcc -nostdlib -Wl,-Ttext,0x100000 start32.o kernel.o -o kernel-i386.elf
That's all!