TCC: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
m (Fasm-TCC BareBones moved to TCC)
No edit summary
Line 1: Line 1:
TCC (Tiny C Compiler) 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.
{{In Progress}}

Version 0.9.25 (released May 2009) added support for x86_64. Work is being done towards a 0.9.26 release, but progress is comparatively slow. However, TinyCC has a couple of advantages:


The purpose of this article is to explain how to make a sample ELF kernel with [[FASM]] and the [http://tinycc.org 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 compiles C code about 9 times faster than GCC.
* 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). (This currently works only for 32bit.)
* 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 has few dependencies. Only libc is required for its compilation.
* TCC includes a linker and an assembler (for x86 only).
* TCC includes a linker and an assembler (for x86 only).


Disadvantages are:


* only averagely optimized x86 code.
==A small kernel example==
* ISO C99 support is not complete.


The Windows version of TCC doesn't produces ELF executables. If you want those, you have to recompile TCC as a cross-compiler.
This little example builds a small kernel in ELF format, which can be booted by Grub.


TCC takes similar options as [[GCC]]:
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===
<source lang="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

</source>

===kernel.c===
<source lang="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;
}

</source>

==Compiling and linking==
Assemble start32.asm with:
<source lang="bash">
<source lang="bash">
tcc -c kernel.c
fasm start32.asm
</source>
</source>


Compile kernel.c with:
<source lang="bash">
<source lang="bash">
tcc -nostdlib -Wl,-Ttext,0x100000 loader.o kernel.o -o kernel.bin
tcc -c kernel.c
</source>
</source>


== See Also ==
Then link the whole thing with:
<source lang="bash">
tcc -nostdlib -Wl,-Ttext,0x100000 start32.o kernel.o -o kernel-i386.elf
</source>


* [http://tinycc.org Tiny C Compiler project page]
That's all!

Revision as of 06:17, 13 December 2011

TCC (Tiny C Compiler) 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.

Version 0.9.25 (released May 2009) added support for x86_64. Work is being done towards a 0.9.26 release, but progress is comparatively slow. However, TinyCC has a couple of advantages:

  • TCC compiles C code about 9 times faster than GCC.
  • 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). (This currently works only for 32bit.)
  • TCC has few dependencies. Only libc is required for its compilation.
  • TCC includes a linker and an assembler (for x86 only).

Disadvantages are:

  • only averagely optimized x86 code.
  • ISO C99 support is not complete.

The Windows version of TCC doesn't produces ELF executables. If you want those, you have to recompile TCC as a cross-compiler.

TCC takes similar options as GCC:

tcc -c kernel.c
tcc -nostdlib -Wl,-Ttext,0x100000 loader.o kernel.o -o kernel.bin

See Also