-
Notifications
You must be signed in to change notification settings - Fork 20
/
tcc-doc.texi
1378 lines (1039 loc) · 38.1 KB
/
tcc-doc.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\input texinfo @c -*- texinfo -*-
@c %**start of header
@setfilename tcc-doc.info
@settitle Tiny C Compiler Reference Documentation
@dircategory Software development
@direntry
* TCC: (tcc-doc). The Tiny C Compiler.
@end direntry
@c %**end of header
@include config.texi
@iftex
@titlepage
@afourpaper
@sp 7
@center @titlefont{Tiny C Compiler Reference Documentation}
@sp 3
@end titlepage
@headings double
@end iftex
@contents
@node Top, Introduction, (dir), (dir)
@top Tiny C Compiler Reference Documentation
This manual documents version @value{VERSION} of the Tiny C Compiler.
@menu
* Introduction:: Introduction to tcc.
* Invoke:: Invocation of tcc (command line, options).
* Clang:: ANSI C and extensions.
* asm:: Assembler syntax.
* linker:: Output file generation and supported targets.
* Bounds:: Automatic bounds-checking of C code.
* Libtcc:: The libtcc library.
* devel:: Guide for Developers.
@end menu
@node Introduction
@chapter Introduction
TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C
compilers, it is meant to be self-relying: you do not need an
external assembler or linker because TCC does that for you.
TCC compiles so @emph{fast} that even for big projects @code{Makefile}s may
not be necessary.
TCC not only supports ANSI C, but also most of the new ISO C99
standard and many GNUC extensions including inline assembly.
TCC can also be used to make @emph{C scripts}, i.e. pieces of C source
that you run as a Perl or Python script. Compilation is so fast that
your script will be as fast as if it was an executable.
TCC can also automatically generate memory and bound checks
(@pxref{Bounds}) while allowing all C pointers operations. TCC can do
these checks even if non patched libraries are used.
With @code{libtcc}, you can use TCC as a backend for dynamic code
generation (@pxref{Libtcc}).
TCC mainly supports the i386 target on Linux and Windows. There are alpha
ports for the ARM (@code{arm-tcc}) and the TMS320C67xx targets
(@code{c67-tcc}). More information about the ARM port is available at
@url{http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html}.
For usage on Windows, see also @url{tcc-win32.txt}.
@node Invoke
@chapter Command line invocation
@section Quick start
@example
@c man begin SYNOPSIS
usage: tcc [options] [@var{infile1} @var{infile2}@dots{}] [@option{-run} @var{infile} @var{args}@dots{}]
@c man end
@end example
@noindent
@c man begin DESCRIPTION
TCC options are a very much like gcc options. The main difference is that TCC
can also execute directly the resulting program and give it runtime
arguments.
Here are some examples to understand the logic:
@table @code
@item @samp{tcc -run a.c}
Compile @file{a.c} and execute it directly
@item @samp{tcc -run a.c arg1}
Compile a.c and execute it directly. arg1 is given as first argument to
the @code{main()} of a.c.
@item @samp{tcc a.c -run b.c arg1}
Compile @file{a.c} and @file{b.c}, link them together and execute them. arg1 is given
as first argument to the @code{main()} of the resulting program.
@ignore
Because multiple C files are specified, @option{--} are necessary to clearly
separate the program arguments from the TCC options.
@end ignore
@item @samp{tcc -o myprog a.c b.c}
Compile @file{a.c} and @file{b.c}, link them and generate the executable @file{myprog}.
@item @samp{tcc -o myprog a.o b.o}
link @file{a.o} and @file{b.o} together and generate the executable @file{myprog}.
@item @samp{tcc -c a.c}
Compile @file{a.c} and generate object file @file{a.o}.
@item @samp{tcc -c asmfile.S}
Preprocess with C preprocess and assemble @file{asmfile.S} and generate
object file @file{asmfile.o}.
@item @samp{tcc -c asmfile.s}
Assemble (but not preprocess) @file{asmfile.s} and generate object file
@file{asmfile.o}.
@item @samp{tcc -r -o ab.o a.c b.c}
Compile @file{a.c} and @file{b.c}, link them together and generate the object file @file{ab.o}.
@end table
Scripting:
TCC can be invoked from @emph{scripts}, just as shell scripts. You just
need to add @code{#!/usr/local/bin/tcc -run} at the start of your C source:
@example
#!/usr/local/bin/tcc -run
#include <stdio.h>
int main()
@{
printf("Hello World\n");
return 0;
@}
@end example
TCC can read C source code from @emph{standard input} when @option{-} is used in
place of @option{infile}. Example:
@example
echo 'main()@{puts("hello");@}' | tcc -run -
@end example
@c man end
@section Option summary
General Options:
@c man begin OPTIONS
@table @option
@item -c
Generate an object file.
@item -o outfile
Put object file, executable, or dll into output file @file{outfile}.
@item -run source [args...]
Compile file @var{source} and run it with the command line arguments
@var{args}. In order to be able to give more than one argument to a
script, several TCC options can be given @emph{after} the
@option{-run} option, separated by spaces:
@example
tcc "-run -L/usr/X11R6/lib -lX11" ex4.c
@end example
In a script, it gives the following header:
@example
#!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11
@end example
@item -v
Display TCC version.
@item -vv
Show included files. As sole argument, print search dirs. -vvv shows tries too.
@item -bench
Display compilation statistics.
@end table
Preprocessor options:
@table @option
@item -Idir
Specify an additional include path. Include paths are searched in the
order they are specified.
System include paths are always searched after. The default system
include paths are: @file{/usr/local/include}, @file{/usr/include}
and @file{PREFIX/lib/tcc/include}. (@file{PREFIX} is usually
@file{/usr} or @file{/usr/local}).
@item -Dsym[=val]
Define preprocessor symbol @samp{sym} to
val. If val is not present, its value is @samp{1}. Function-like macros can
also be defined: @option{-DF(a)=a+1}
@item -Usym
Undefine preprocessor symbol @samp{sym}.
@item -E
Preprocess only, to stdout or file (with -o).
@end table
Compilation flags:
Note: each of the following options has a negative form beginning with
@option{-fno-}.
@table @option
@item -funsigned-char
Let the @code{char} type be unsigned.
@item -fsigned-char
Let the @code{char} type be signed.
@item -fno-common
Do not generate common symbols for uninitialized data.
@item -fleading-underscore
Add a leading underscore at the beginning of each C symbol.
@item -fms-extensions
Allow a MS C compiler extensions to the language. Currently this
assumes a nested named structure declaration without an identifier
behaves like an unnamed one.
@item -fdollars-in-identifiers
Allow dollar signs in identifiers
@end table
Warning options:
@table @option
@item -w
Disable all warnings.
@end table
Note: each of the following warning options has a negative form beginning with
@option{-Wno-}.
@table @option
@item -Wimplicit-function-declaration
Warn about implicit function declaration.
@item -Wunsupported
Warn about unsupported GCC features that are ignored by TCC.
@item -Wwrite-strings
Make string constants be of type @code{const char *} instead of @code{char
*}.
@item -Werror
Abort compilation if warnings are issued.
@item -Wall
Activate all warnings, except @option{-Werror}, @option{-Wunusupported} and
@option{-Wwrite-strings}.
@end table
Linker options:
@table @option
@item -Ldir
Specify an additional static library path for the @option{-l} option. The
default library paths are @file{/usr/local/lib}, @file{/usr/lib} and @file{/lib}.
@item -lxxx
Link your program with dynamic library libxxx.so or static library
libxxx.a. The library is searched in the paths specified by the
@option{-L} option and @env{LIBRARY_PATH} variable.
@item -Bdir
Set the path where the tcc internal libraries (and include files) can be
found (default is @file{PREFIX/lib/tcc}).
@item -shared
Generate a shared library instead of an executable.
@item -soname name
set name for shared library to be used at runtime
@item -static
Generate a statically linked executable (default is a shared linked
executable).
@item -rdynamic
Export global symbols to the dynamic linker. It is useful when a library
opened with @code{dlopen()} needs to access executable symbols.
@item -r
Generate an object file combining all input files.
@item -Wl,-rpath=path
Put custom search path for dynamic libraries into executable.
@item -Wl,--enable-new-dtags
When putting a custom search path for dynamic libraries into the executable,
create the new ELF dynamic tag DT_RUNPATH instead of the old legacy DT_RPATH.
@item -Wl,--oformat=fmt
Use @var{fmt} as output format. The supported output formats are:
@table @code
@item elf32-i386
ELF output format (default)
@item binary
Binary image (only for executable output)
@item coff
COFF output format (only for executable output for TMS320C67xx target)
@end table
@item -Wl,--export-all-symbols
@item -Wl,--export-dynamic
Export global symbols to the dynamic linker. It is useful when a library
opened with @code{dlopen()} needs to access executable symbols.
@item -Wl,-subsystem=console/gui/wince/...
Set type for PE (Windows) executables.
@item -Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#]
Modify executable layout.
@item -Wl,-Bsymbolic
Set DT_SYMBOLIC tag.
@item -Wl,-(no-)whole-archive
Turn on/off linking of all objects in archives.
@end table
Debugger options:
@table @option
@item -g
Generate run time debug information so that you get clear run time
error messages: @code{ test.c:68: in function 'test5()': dereferencing
invalid pointer} instead of the laconic @code{Segmentation
fault}.
@item -b
Generate additional support code to check memory allocations and array/pointer
bounds (@pxref{Bounds}). @option{-g} is implied.
@item -bt[N]
Display N callers in stack traces. This is useful with @option{-g} or @option{-b}.
With executables, additional support for stack traces is included.
A function @code{ int tcc_backtrace(const char *fmt, ...); } is provided
to trigger a stack trace with a message on demand.
@end table
Misc options:
@table @option
@item -MD
Generate makefile fragment with dependencies.
@item -MF depfile
Use @file{depfile} as output for -MD.
@item -print-search-dirs
Print the configured installation directory and a list of library
and include directories tcc will search.
@item -dumpversion
Print version.
@end table
Target specific options:
@table @option
@item -mms-bitfields
Use an algorithm for bitfield alignment consistent with MSVC. Default is
gcc's algorithm.
@item -mfloat-abi (ARM only)
Select the float ABI. Possible values: @code{softfp} and @code{hard}
@item -mno-sse
Do not use sse registers on x86_64
@item -m32, -m64
Pass command line to the i386/x86_64 cross compiler.
@end table
Note: GCC options @option{-Ox}, @option{-fx} and @option{-mx} are
ignored.
@c man end
@c man begin ENVIRONMENT
Environment variables that affect how tcc operates.
@table @option
@item CPATH
@item C_INCLUDE_PATH
A colon-separated list of directories searched for include files,
directories given with @option{-I} are searched first.
@item LIBRARY_PATH
A colon-separated list of directories searched for libraries for the
@option{-l} option, directories given with @option{-L} are searched first.
@end table
@c man end
@ignore
@setfilename tcc
@settitle Tiny C Compiler
@c man begin SEEALSO
cpp(1),
gcc(1)
@c man end
@c man begin AUTHOR
Fabrice Bellard
@c man end
@end ignore
@node Clang
@chapter C language support
@section ANSI C
TCC implements all the ANSI C standard, including structure bit fields
and floating point numbers (@code{long double}, @code{double}, and
@code{float} fully supported).
@section ISOC99 extensions
TCC implements many features of the new C standard: ISO C99. Currently
missing items are: complex and imaginary numbers.
Currently implemented ISOC99 features:
@itemize
@item variable length arrays.
@item 64 bit @code{long long} types are fully supported.
@item The boolean type @code{_Bool} is supported.
@item @code{__func__} is a string variable containing the current
function name.
@item Variadic macros: @code{__VA_ARGS__} can be used for
function-like macros:
@example
#define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)
@end example
@noindent
@code{dprintf} can then be used with a variable number of parameters.
@item Declarations can appear anywhere in a block (as in C++).
@item Array and struct/union elements can be initialized in any order by
using designators:
@example
struct @{ int x, y; @} st[10] = @{ [0].x = 1, [0].y = 2 @};
int tab[10] = @{ 1, 2, [5] = 5, [9] = 9@};
@end example
@item Compound initializers are supported:
@example
int *p = (int [])@{ 1, 2, 3 @};
@end example
to initialize a pointer pointing to an initialized array. The same
works for structures and strings.
@item Hexadecimal floating point constants are supported:
@example
double d = 0x1234p10;
@end example
@noindent
is the same as writing
@example
double d = 4771840.0;
@end example
@item @code{inline} keyword is ignored.
@item @code{restrict} keyword is ignored.
@end itemize
@section GNU C extensions
TCC implements some GNU C extensions:
@itemize
@item array designators can be used without '=':
@example
int a[10] = @{ [0] 1, [5] 2, 3, 4 @};
@end example
@item Structure field designators can be a label:
@example
struct @{ int x, y; @} st = @{ x: 1, y: 1@};
@end example
instead of
@example
struct @{ int x, y; @} st = @{ .x = 1, .y = 1@};
@end example
@item @code{\e} is ASCII character 27.
@item case ranges : ranges can be used in @code{case}s:
@example
switch(a) @{
case 1 @dots{} 9:
printf("range 1 to 9\n");
break;
default:
printf("unexpected\n");
break;
@}
@end example
@cindex aligned attribute
@cindex packed attribute
@cindex section attribute
@cindex unused attribute
@cindex cdecl attribute
@cindex stdcall attribute
@cindex regparm attribute
@cindex dllexport attribute
@cindex nodecorate attribute
@item The keyword @code{__attribute__} is handled to specify variable or
function attributes. The following attributes are supported:
@itemize
@item @code{aligned(n)}: align a variable or a structure field to n bytes
(must be a power of two).
@item @code{packed}: force alignment of a variable or a structure field to
1.
@item @code{section(name)}: generate function or data in assembly section
name (name is a string containing the section name) instead of the default
section.
@item @code{unused}: specify that the variable or the function is unused.
@item @code{cdecl}: use standard C calling convention (default).
@item @code{stdcall}: use Pascal-like calling convention.
@item @code{regparm(n)}: use fast i386 calling convention. @var{n} must be
between 1 and 3. The first @var{n} function parameters are respectively put in
registers @code{%eax}, @code{%edx} and @code{%ecx}.
@item @code{dllexport}: export function from dll/executable (win32 only)
@item @code{nodecorate}: do not apply any decorations that would otherwise be applied when exporting function from dll/executable (win32 only)
@end itemize
Here are some examples:
@example
int a __attribute__ ((aligned(8), section(".mysection")));
@end example
@noindent
align variable @code{a} to 8 bytes and put it in section @code{.mysection}.
@example
int my_add(int a, int b) __attribute__ ((section(".mycodesection")))
@{
return a + b;
@}
@end example
@noindent
generate function @code{my_add} in section @code{.mycodesection}.
@item GNU style variadic macros:
@example
#define dprintf(fmt, args@dots{}) printf(fmt, ## args)
dprintf("no arg\n");
dprintf("one arg %d\n", 1);
@end example
@item @code{__FUNCTION__} is interpreted as C99 @code{__func__}
(so it has not exactly the same semantics as string literal GNUC
where it is a string literal).
@item The @code{__alignof__} keyword can be used as @code{sizeof}
to get the alignment of a type or an expression.
@item The @code{typeof(x)} returns the type of @code{x}.
@code{x} is an expression or a type.
@item Computed gotos: @code{&&label} returns a pointer of type
@code{void *} on the goto label @code{label}. @code{goto *expr} can be
used to jump on the pointer resulting from @code{expr}.
@item Inline assembly with asm instruction:
@cindex inline assembly
@cindex assembly, inline
@cindex __asm__
@example
static inline void * my_memcpy(void * to, const void * from, size_t n)
@{
int d0, d1, d2;
__asm__ __volatile__(
"rep ; movsl\n\t"
"testb $2,%b4\n\t"
"je 1f\n\t"
"movsw\n"
"1:\ttestb $1,%b4\n\t"
"je 2f\n\t"
"movsb\n"
"2:"
: "=&c" (d0), "=&D" (d1), "=&S" (d2)
:"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
: "memory");
return (to);
@}
@end example
@noindent
@cindex gas
TCC includes its own x86 inline assembler with a @code{gas}-like (GNU
assembler) syntax. No intermediate files are generated. GCC 3.x named
operands are supported.
@item @code{__builtin_types_compatible_p()} and @code{__builtin_constant_p()}
are supported.
@item @code{#pragma pack} is supported for win32 compatibility.
@end itemize
@section TinyCC extensions
@itemize
@item @code{__TINYC__} is a predefined macro to indicate that you use TCC.
@item @code{#!} at the start of a line is ignored to allow scripting.
@item Binary digits can be entered (@code{0b101} instead of
@code{5}).
@end itemize
@node asm
@chapter TinyCC Assembler
Since version 0.9.16, TinyCC integrates its own assembler. TinyCC
assembler supports a gas-like syntax (GNU assembler). You can
deactivate assembler support if you want a smaller TinyCC executable
(the C compiler does not rely on the assembler).
TinyCC Assembler is used to handle files with @file{.S} (C
preprocessed assembler) and @file{.s} extensions. It is also used to
handle the GNU inline assembler with the @code{asm} keyword.
@section Syntax
TinyCC Assembler supports most of the gas syntax. The tokens are the
same as C.
@itemize
@item C and C++ comments are supported.
@item Identifiers are the same as C, so you cannot use '.' or '$'.
@item Only 32 bit integer numbers are supported.
@end itemize
@section Expressions
@itemize
@item Integers in decimal, octal and hexa are supported.
@item Unary operators: +, -, ~.
@item Binary operators in decreasing priority order:
@enumerate
@item *, /, %
@item &, |, ^
@item +, -
@end enumerate
@item A value is either an absolute number or a label plus an offset.
All operators accept absolute values except '+' and '-'. '+' or '-' can be
used to add an offset to a label. '-' supports two labels only if they
are the same or if they are both defined and in the same section.
@end itemize
@section Labels
@itemize
@item All labels are considered as local, except undefined ones.
@item Numeric labels can be used as local @code{gas}-like labels.
They can be defined several times in the same source. Use 'b'
(backward) or 'f' (forward) as suffix to reference them:
@example
1:
jmp 1b /* jump to '1' label before */
jmp 1f /* jump to '1' label after */
1:
@end example
@end itemize
@section Directives
@cindex assembler directives
@cindex directives, assembler
@cindex align directive
@cindex skip directive
@cindex space directive
@cindex byte directive
@cindex word directive
@cindex short directive
@cindex int directive
@cindex long directive
@cindex quad directive
@cindex globl directive
@cindex global directive
@cindex section directive
@cindex text directive
@cindex data directive
@cindex bss directive
@cindex fill directive
@cindex org directive
@cindex previous directive
@cindex string directive
@cindex asciz directive
@cindex ascii directive
All directives are preceded by a '.'. The following directives are
supported:
@itemize
@item .align n[,value]
@item .skip n[,value]
@item .space n[,value]
@item .byte value1[,...]
@item .word value1[,...]
@item .short value1[,...]
@item .int value1[,...]
@item .long value1[,...]
@item .quad immediate_value1[,...]
@item .globl symbol
@item .global symbol
@item .section section
@item .text
@item .data
@item .bss
@item .fill repeat[,size[,value]]
@item .org n
@item .previous
@item .string string[,...]
@item .asciz string[,...]
@item .ascii string[,...]
@end itemize
@section X86 Assembler
@cindex assembler
All X86 opcodes are supported. Only ATT syntax is supported (source
then destination operand order). If no size suffix is given, TinyCC
tries to guess it from the operand sizes.
Currently, MMX opcodes are supported but not SSE ones.
@node linker
@chapter TinyCC Linker
@cindex linker
@section ELF file generation
@cindex ELF
TCC can directly output relocatable ELF files (object files),
executable ELF files and dynamic ELF libraries without relying on an
external linker.
Dynamic ELF libraries can be output but the C compiler does not generate
position independent code (PIC). It means that the dynamic library
code generated by TCC cannot be factorized among processes yet.
TCC linker eliminates unreferenced object code in libraries. A single pass is
done on the object and library list, so the order in which object files and
libraries are specified is important (same constraint as GNU ld). No grouping
options (@option{--start-group} and @option{--end-group}) are supported.
@section ELF file loader
TCC can load ELF object files, archives (.a files) and dynamic
libraries (.so).
@section PE-i386 file generation
@cindex PE-i386
TCC for Windows supports the native Win32 executable file format (PE-i386). It
generates EXE files (console and gui) and DLL files.
For usage on Windows, see also tcc-win32.txt.
@section GNU Linker Scripts
@cindex scripts, linker
@cindex linker scripts
@cindex GROUP, linker command
@cindex FILE, linker command
@cindex OUTPUT_FORMAT, linker command
@cindex TARGET, linker command
Because on many Linux systems some dynamic libraries (such as
@file{/usr/lib/libc.so}) are in fact GNU ld link scripts (horrible!),
the TCC linker also supports a subset of GNU ld scripts.
The @code{GROUP} and @code{FILE} commands are supported. @code{OUTPUT_FORMAT}
and @code{TARGET} are ignored.
Example from @file{/usr/lib/libc.so}:
@example
/* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily. */
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
@end example
@node Bounds
@chapter TinyCC Memory and Bound checks
@cindex bound checks
@cindex memory checks
This feature is activated with the @option{-b} option (@pxref{Invoke}).
Here are some examples of caught errors:
@table @asis
@item Invalid range with standard string function:
@example
@{
char tab[10];
memset(tab, 0, 11);
@}
@end example
@item Out of bounds-error in global or local arrays:
@example
@{
int tab[10];
for(i=0;i<11;i++) @{
sum += tab[i];
@}
@}
@end example
@item Out of bounds-error in malloc'ed data:
@example
@{
int *tab;
tab = malloc(20 * sizeof(int));
for(i=0;i<21;i++) @{
sum += tab[i];
@}
free(tab);
@}
@end example
@item Access of freed memory:
@example
@{
int *tab;
tab = malloc(20 * sizeof(int));
free(tab);
for(i=0;i<20;i++) @{
sum += tab[i];
@}
@}
@end example
@item Double free:
@example
@{
int *tab;
tab = malloc(20 * sizeof(int));
free(tab);
free(tab);
@}
@end example
@end table
TCC defines @code{__BOUNDS_CHECKING_ON} if activated.
There are five environment variables that can be used to control the behavior:
@itemize
@item TCC_BOUNDS_WARN_POINTER_ADD
- Print warning when pointer add creates an illegal pointer.
@item TCC_BOUNDS_PRINT_CALLS
- Print bound checking calls. Can be used for debugging.
@item TCC_BOUNDS_PRINT_HEAP
- Print heap objects that are not freed at exit of program.
@item TCC_BOUNDS_PRINT_STATISTIC
- Print statistic information at exit of program.
@item TCC_BOUNDS_NEVER_FATAL
- Try to continue in case of a bound checking error.
@end itemize
Also, a function @code{__bounds_checking(x)} can be used to turn off/on bounds
checking from usercode (see below).
Notes:
@itemize
@item Only available on i386 (linux and windows), x86_64 (linux and windows),
arm, arm64 and riscv64 for the moment.
@item The generated code is slower and bigger.
@item The bound checking code is not included in shared libraries. The main
executable should always be compiled with the @option{-b}.
@item Pointer size is @emph{unchanged} and code generated with bound checks is
@emph{fully compatible} with unchecked code. When a pointer comes from
unchecked code, it is assumed to be valid. Even very obscure C code with
casts should work correctly.
@item Signal handlers are not compatible with bounds checking. The
bounds checking code disables checking in signal/sigaction handlers.
The fork() function call in a multi threaded application is also a problem.
The bound checking code fixes this for the child process.
@item The reason that signals and fork have problems is that we use locking
inside the bounds checking code.
Inside a signal handler we can not use locks. Also in a multi threaded
application after a fork the child process can have the lock set
by another thread.
@item The BOUNDS_CHECKING_OFF and BOUNDS_CHECKING_ON can also be used to
disable bounds checking for some code.
@item The __bounds_checking call adds a value to a thread local value.
The value starts at 0. If the value is not 0 the code is not checked
for bounds checking errors.
@end itemize
@example
#if defined(__TINYC__) && __BOUNDS_CHECKING_ON
extern void __bounds_checking (int x);
# define BOUNDS_CHECKING_OFF __bounds_checking(1)
# define BOUNDS_CHECKING_ON __bounds_checking(-1)
#else
# define BOUNDS_CHECKING_OFF
# define BOUNDS_CHECKING_ON
#endif
@end example
For more information about the ideas behind this method, see
@url{http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html}.
@node Libtcc
@chapter The @code{libtcc} library
The @code{libtcc} library enables you to use TCC as a backend for
dynamic code generation.
Read the @file{libtcc.h} to have an overview of the API. Read
@file{libtcc_test.c} to have a very simple example.
The idea consists in giving a C string containing the program you want
to compile directly to @code{libtcc}. Then you can access to any global
symbol (function or variable) defined.
@node devel
@chapter Developer's guide
This chapter gives some hints to understand how TCC works. You can skip
it if you do not intend to modify the TCC code.