TCC
This article describes 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.
TCC includes also a linker and an assembler (only x86). But this assembler is limited: no 16/64-bit support, instructions up to MMX are supported.
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!