safety: replace occurrences of strcpy by pstrcpy
[tinycc.git] / tcc.h
blobb8b81c2b4917e53f62fbc91d5d35fab60c1656d2
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef _TCC_H
22 #define _TCC_H
24 #define _GNU_SOURCE
25 #include "config.h"
27 #ifdef CONFIG_TCCBOOT
29 #include "tccboot.h"
30 #define CONFIG_TCC_STATIC
32 #else
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <math.h>
40 #include <signal.h>
41 #include <fcntl.h>
42 #include <setjmp.h>
43 #include <time.h>
45 #ifdef _WIN32
46 #include <windows.h>
47 #include <sys/timeb.h>
48 #include <io.h> /* open, close etc. */
49 #include <direct.h> /* getcwd */
50 #define inline __inline
51 #define inp next_inp
52 #ifdef LIBTCC_AS_DLL
53 # define LIBTCCAPI __declspec(dllexport)
54 # define PUB_FUNC LIBTCCAPI
55 #endif
56 #endif
58 #ifndef _WIN32
59 #include <unistd.h>
60 #include <sys/time.h>
61 #include <sys/ucontext.h>
62 #include <sys/mman.h>
63 #include <dlfcn.h>
64 #endif
66 #endif /* !CONFIG_TCCBOOT */
68 #ifndef PAGESIZE
69 #define PAGESIZE 4096
70 #endif
72 #ifndef O_BINARY
73 #define O_BINARY 0
74 #endif
76 #ifndef SA_SIGINFO
77 #define SA_SIGINFO 0x00000004u
78 #endif
80 #include "elf.h"
81 #include "stab.h"
82 #include "libtcc.h"
84 /* parser debug */
85 //#define PARSE_DEBUG
86 /* preprocessor debug */
87 //#define PP_DEBUG
88 /* include file debug */
89 //#define INC_DEBUG
91 //#define MEM_DEBUG
93 /* assembler debug */
94 //#define ASM_DEBUG
96 /* target selection */
97 //#define TCC_TARGET_I386 /* i386 code generator */
98 //#define TCC_TARGET_ARM /* ARMv4 code generator */
99 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
100 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
102 /* default target is I386 */
103 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
104 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
105 #define TCC_TARGET_I386
106 #endif
108 #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
109 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
110 #define CONFIG_TCC_BCHECK /* enable bound checking code */
111 #endif
113 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
114 #define CONFIG_TCC_STATIC
115 #endif
117 /* define it to include assembler support */
118 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
119 #define CONFIG_TCC_ASM
120 #endif
122 /* object format selection */
123 #if defined(TCC_TARGET_C67)
124 #define TCC_TARGET_COFF
125 #endif
127 /* only native compiler supports -run */
128 #if defined _WIN32 == defined TCC_TARGET_PE
129 # if (defined __i386__ || defined _X86_) && defined TCC_TARGET_I386
130 # define TCC_IS_NATIVE
131 # elif (defined __x86_64__ || defined _AMD64_) && defined TCC_TARGET_X86_64
132 # define TCC_IS_NATIVE
133 # elif defined __arm__ && defined TCC_TARGET_ARM
134 # define TCC_IS_NATIVE
135 # endif
136 #endif
138 #if defined TCC_IS_NATIVE && !defined CONFIG_TCCBOOT
139 # define CONFIG_TCC_BACKTRACE
140 #endif
142 /* target address type */
143 #if defined TCC_TARGET_X86_64 && (!defined __x86_64__ || defined _WIN32)
144 # define uplong unsigned long long
145 #else
146 # define uplong unsigned long
147 #endif
149 /* ------------ path configuration ------------ */
151 #ifndef CONFIG_SYSROOT
152 # define CONFIG_SYSROOT ""
153 #endif
155 #ifdef CONFIG_MULTIARCHDIR
156 # define CONFIG_LDDIR "lib/" CONFIG_MULTIARCHDIR
157 #endif
159 #ifndef CONFIG_LDDIR
160 # define CONFIG_LDDIR "lib"
161 #endif
163 /* path to find crt1.o, crti.o and crtn.o */
164 #ifndef CONFIG_TCC_CRTPREFIX
165 # define CONFIG_TCC_CRTPREFIX CONFIG_SYSROOT "/usr/" CONFIG_LDDIR
166 #endif
168 /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
170 /* system include paths */
171 #ifndef CONFIG_TCC_SYSINCLUDEPATHS
172 # ifdef TCC_TARGET_PE
173 # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include;{B}/include/winapi"
174 # elif defined CONFIG_MULTIARCHDIR
175 # define CONFIG_TCC_SYSINCLUDEPATHS \
176 CONFIG_SYSROOT "/usr/local/include" \
177 ":" CONFIG_SYSROOT "/usr/local/include/" CONFIG_MULTIARCHDIR \
178 ":" CONFIG_SYSROOT "/usr/include" \
179 ":" CONFIG_SYSROOT "/usr/include/" CONFIG_MULTIARCHDIR \
180 ":" "{B}/include"
181 # else
182 # define CONFIG_TCC_SYSINCLUDEPATHS \
183 CONFIG_SYSROOT "/usr/local/include" \
184 ":" CONFIG_SYSROOT "/usr/include" \
185 ":" "{B}/include"
186 # endif
187 #endif
189 /* library search paths */
190 #ifndef CONFIG_TCC_LIBPATHS
191 # ifdef TCC_TARGET_PE
192 # define CONFIG_TCC_LIBPATHS "{B}/lib"
193 # else
194 # define CONFIG_TCC_LIBPATHS \
195 CONFIG_SYSROOT "/usr/" CONFIG_LDDIR \
196 ":" CONFIG_SYSROOT "/" CONFIG_LDDIR \
197 ":" CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR
198 # endif
199 #endif
201 /* name of ELF interpreter */
202 #ifndef CONFIG_TCC_ELFINTERP
203 # if defined __FreeBSD__
204 # define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
205 # elif defined __FreeBSD_kernel__
206 # define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
207 # elif defined TCC_ARM_HARDFLOAT
208 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux-armhf.so.3"
209 # elif defined TCC_ARM_EABI
210 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.3"
211 # elif defined(TCC_TARGET_X86_64)
212 # define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
213 # elif defined(TCC_UCLIBC)
214 # define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0"
215 # else
216 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
217 # endif
218 #endif
220 /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
221 #define TCC_LIBGCC CONFIG_SYSROOT "/" CONFIG_LDDIR "/libgcc_s.so.1"
223 /* -------------------------------------------- */
225 #define FALSE 0
226 #define false 0
227 #define TRUE 1
228 #define true 1
229 typedef int BOOL;
231 #define INCLUDE_STACK_SIZE 32
232 #define IFDEF_STACK_SIZE 64
233 #define VSTACK_SIZE 256
234 #define STRING_MAX_SIZE 1024
235 #define PACK_STACK_SIZE 8
237 #define TOK_HASH_SIZE 8192 /* must be a power of two */
238 #define TOK_ALLOC_INCR 512 /* must be a power of two */
239 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
241 /* token symbol management */
242 typedef struct TokenSym {
243 struct TokenSym *hash_next;
244 struct Sym *sym_define; /* direct pointer to define */
245 struct Sym *sym_label; /* direct pointer to label */
246 struct Sym *sym_struct; /* direct pointer to structure */
247 struct Sym *sym_identifier; /* direct pointer to identifier */
248 int tok; /* token number */
249 int len;
250 char str[1];
251 } TokenSym;
253 #ifdef TCC_TARGET_PE
254 typedef unsigned short nwchar_t;
255 #else
256 typedef int nwchar_t;
257 #endif
259 typedef struct CString {
260 int size; /* size in bytes */
261 void *data; /* either 'char *' or 'nwchar_t *' */
262 int size_allocated;
263 void *data_allocated; /* if non NULL, data has been malloced */
264 } CString;
266 /* type definition */
267 typedef struct CType {
268 int t;
269 struct Sym *ref;
270 } CType;
272 /* constant value */
273 typedef union CValue {
274 long double ld;
275 double d;
276 float f;
277 int i;
278 unsigned int ui;
279 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
280 long long ll;
281 unsigned long long ull;
282 struct CString *cstr;
283 void *ptr;
284 int tab[2];
285 } CValue;
287 /* value on stack */
288 typedef struct SValue {
289 CType type; /* type */
290 unsigned short r; /* register + flags */
291 unsigned short r2; /* second register, used for 'long long'
292 type. If not used, set to VT_CONST */
293 CValue c; /* constant, if VT_CONST */
294 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
295 } SValue;
297 /* symbol management */
298 typedef struct Sym {
299 int v; /* symbol token */
300 char *asm_label; /* associated asm label */
301 long r; /* associated register */
302 union {
303 long c; /* associated number */
304 int *d; /* define token stream */
306 CType type; /* associated type */
307 union {
308 struct Sym *next; /* next related symbol */
309 long jnext; /* next jump label */
311 struct Sym *prev; /* prev symbol in stack */
312 struct Sym *prev_tok; /* previous symbol for this token */
313 } Sym;
315 /* section definition */
316 /* XXX: use directly ELF structure for parameters ? */
317 /* special flag to indicate that the section should not be linked to
318 the other ones */
319 #define SHF_PRIVATE 0x80000000
321 /* special flag, too */
322 #define SECTION_ABS ((void *)1)
324 typedef struct Section {
325 unsigned long data_offset; /* current data offset */
326 unsigned char *data; /* section data */
327 unsigned long data_allocated; /* used for realloc() handling */
328 int sh_name; /* elf section name (only used during output) */
329 int sh_num; /* elf section number */
330 int sh_type; /* elf section type */
331 int sh_flags; /* elf section flags */
332 int sh_info; /* elf section info */
333 int sh_addralign; /* elf section alignment */
334 int sh_entsize; /* elf entry size */
335 unsigned long sh_size; /* section size (only used during output) */
336 uplong sh_addr; /* address at which the section is relocated */
337 unsigned long sh_offset; /* file offset */
338 int nb_hashed_syms; /* used to resize the hash table */
339 struct Section *link; /* link to another section */
340 struct Section *reloc; /* corresponding section for relocation, if any */
341 struct Section *hash; /* hash table for symbols */
342 struct Section *next;
343 char name[1]; /* section name */
344 } Section;
346 typedef struct DLLReference {
347 int level;
348 void *handle;
349 char name[1];
350 } DLLReference;
352 /* GNUC attribute definition */
353 typedef struct AttributeDef {
354 unsigned
355 func_call : 3, /* calling convention (0..5), see below */
356 aligned : 5, /* alignement (0..16) */
357 packed : 1,
358 func_export : 1,
359 func_import : 1,
360 func_args : 5,
361 mode : 4,
362 weak : 1,
363 fill : 11;
364 struct Section *section;
365 int alias_target; /* token */
366 } AttributeDef;
368 /* gr: wrappers for casting sym->r for other purposes */
369 #define FUNC_CALL(r) (((AttributeDef*)&(r))->func_call)
370 #define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export)
371 #define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import)
372 #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
373 #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
374 #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
375 #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
376 #define INT_ATTR(ad) (*(int*)(ad))
378 /* -------------------------------------------------- */
380 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
381 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
382 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
384 /* stored in 'Sym.c' field */
385 #define FUNC_NEW 1 /* ansi function prototype */
386 #define FUNC_OLD 2 /* old function prototype */
387 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
389 /* stored in 'Sym.r' field */
390 #define FUNC_CDECL 0 /* standard c call */
391 #define FUNC_STDCALL 1 /* pascal c call */
392 #define FUNC_FASTCALL1 2 /* first param in %eax */
393 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
394 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
395 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
397 /* field 'Sym.t' for macros */
398 #define MACRO_OBJ 0 /* object like macro */
399 #define MACRO_FUNC 1 /* function like macro */
401 /* field 'Sym.r' for C labels */
402 #define LABEL_DEFINED 0 /* label is defined */
403 #define LABEL_FORWARD 1 /* label is forward defined */
404 #define LABEL_DECLARED 2 /* label is declared but never used */
406 /* type_decl() types */
407 #define TYPE_ABSTRACT 1 /* type without variable */
408 #define TYPE_DIRECT 2 /* type with variable */
410 #define IO_BUF_SIZE 8192
412 typedef struct BufferedFile {
413 uint8_t *buf_ptr;
414 uint8_t *buf_end;
415 int fd;
416 struct BufferedFile *prev;
417 int line_num; /* current line number - here to simplify code */
418 int ifndef_macro; /* #ifndef macro / #endif search */
419 int ifndef_macro_saved; /* saved ifndef_macro */
420 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
421 char filename[1024]; /* filename */
422 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
423 } BufferedFile;
425 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
426 #define CH_EOF (-1) /* end of file */
428 /* parsing state (used to save parser state to reparse part of the
429 source several times) */
430 typedef struct ParseState {
431 const int *macro_ptr;
432 int line_num;
433 int tok;
434 CValue tokc;
435 } ParseState;
437 /* used to record tokens */
438 typedef struct TokenString {
439 int *str;
440 int len;
441 int allocated_len;
442 int last_line_num;
443 } TokenString;
445 /* inline functions */
446 typedef struct InlineFunc {
447 int *token_str;
448 Sym *sym;
449 char filename[1];
450 } InlineFunc;
452 /* include file cache, used to find files faster and also to eliminate
453 inclusion if the include file is protected by #ifndef ... #endif */
454 typedef struct CachedInclude {
455 int ifndef_macro;
456 int hash_next; /* -1 if none */
457 char filename[1]; /* path specified in #include */
458 } CachedInclude;
460 #define CACHED_INCLUDES_HASH_SIZE 512
462 #ifdef CONFIG_TCC_ASM
463 typedef struct ExprValue {
464 uint32_t v;
465 Sym *sym;
466 } ExprValue;
468 #define MAX_ASM_OPERANDS 30
469 typedef struct ASMOperand {
470 int id; /* GCC 3 optionnal identifier (0 if number only supported */
471 char *constraint;
472 char asm_str[16]; /* computed asm string for operand */
473 SValue *vt; /* C value of the expression */
474 int ref_index; /* if >= 0, gives reference to a output constraint */
475 int input_index; /* if >= 0, gives reference to an input constraint */
476 int priority; /* priority, used to assign registers */
477 int reg; /* if >= 0, register number used for this operand */
478 int is_llong; /* true if double register value */
479 int is_memory; /* true if memory operand */
480 int is_rw; /* for '+' modifier */
481 } ASMOperand;
482 #endif
484 struct sym_attr {
485 unsigned long got_offset;
486 #ifdef TCC_TARGET_ARM
487 unsigned char plt_thumb_stub:1;
488 #endif
491 struct TCCState {
492 unsigned output_type : 8;
493 unsigned reloc_output : 1;
495 BufferedFile **include_stack_ptr;
496 int *ifdef_stack_ptr;
498 /* include file handling */
499 char **include_paths;
500 int nb_include_paths;
501 char **sysinclude_paths;
502 int nb_sysinclude_paths;
503 CachedInclude **cached_includes;
504 int nb_cached_includes;
506 char **library_paths;
507 int nb_library_paths;
508 char **crt_paths;
509 int nb_crt_paths;
511 /* array of all loaded dlls (including those referenced by loaded
512 dlls) */
513 DLLReference **loaded_dlls;
514 int nb_loaded_dlls;
516 /* sections */
517 Section **sections;
518 int nb_sections; /* number of sections, including first dummy section */
520 Section **priv_sections;
521 int nb_priv_sections; /* number of private sections */
523 /* got & plt handling */
524 Section *got;
525 Section *plt;
526 struct sym_attr *sym_attrs;
527 int nb_sym_attrs;
528 /* give the correspondance from symtab indexes to dynsym indexes */
529 int *symtab_to_dynsym;
531 /* temporary dynamic symbol sections (for dll loading) */
532 Section *dynsymtab_section;
533 /* exported dynamic symbol section */
534 Section *dynsym;
536 /* copy of the gobal symtab_section variable */
537 Section *symtab;
539 int nostdinc; /* if true, no standard headers are added */
540 int nostdlib; /* if true, no standard libraries are added */
541 int nocommon; /* if true, do not use common symbols for .bss data */
543 /* if true, static linking is performed */
544 int static_link;
546 /* soname as specified on the command line (-soname) */
547 const char *soname;
548 /* rpath as specified on the command line (-Wl,-rpath=) */
549 const char *rpath;
551 /* if true, all symbols are exported */
552 int rdynamic;
554 /* if true, resolve symbols in the current module first (-Wl,Bsymbolic) */
555 int symbolic;
557 /* if true, only link in referenced objects from archive */
558 int alacarte_link;
560 /* address of text section */
561 uplong text_addr;
562 int has_text_addr;
564 /* symbols to call at load-time / unload-time */
565 const char *init_symbol;
566 const char *fini_symbol;
568 /* output format, see TCC_OUTPUT_FORMAT_xxx */
569 int output_format;
571 /* C language options */
572 int char_is_unsigned;
573 int leading_underscore;
575 /* warning switches */
576 int warn_write_strings;
577 int warn_unsupported;
578 int warn_error;
579 int warn_none;
580 int warn_implicit_function_declaration;
582 /* display some information during compilation */
583 int verbose;
584 /* compile with debug symbol (and use them if error during execution) */
585 int do_debug;
586 #ifdef CONFIG_TCC_BCHECK
587 /* compile with built-in memory and bounds checker */
588 int do_bounds_check;
589 #endif
590 /* give the path of the tcc libraries */
591 char *tcc_lib_path;
593 /* error handling */
594 void *error_opaque;
595 void (*error_func)(void *opaque, const char *msg);
596 int error_set_jmp_enabled;
597 jmp_buf error_jmp_buf;
598 int nb_errors;
600 /* tiny assembler state */
601 Sym *asm_labels;
603 /* see include_stack_ptr */
604 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
606 /* see ifdef_stack_ptr */
607 int ifdef_stack[IFDEF_STACK_SIZE];
609 /* see cached_includes */
610 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
612 /* pack stack */
613 int pack_stack[PACK_STACK_SIZE];
614 int *pack_stack_ptr;
616 /* output file for preprocessing */
617 FILE *outfile;
619 /* automatically collected dependencies for this compilation */
620 char **target_deps;
621 int nb_target_deps;
623 /* for tcc_relocate */
624 int runtime_added;
625 void *runtime_mem;
626 #ifdef HAVE_SELINUX
627 void *write_mem;
628 unsigned long mem_size;
629 #endif
631 struct InlineFunc **inline_fns;
632 int nb_inline_fns;
634 #ifdef TCC_TARGET_I386
635 int seg_size;
636 #endif
638 /* section alignment */
639 unsigned long section_align;
641 #ifdef TCC_TARGET_PE
642 /* PE info */
643 int pe_subsystem;
644 unsigned long pe_file_align;
645 unsigned long pe_stack_size;
646 #ifdef TCC_TARGET_X86_64
647 Section *uw_pdata;
648 int uw_sym;
649 unsigned uw_offs;
650 #endif
651 #endif
653 #ifndef TCC_TARGET_PE
654 #if defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM
655 /* write PLT and GOT here */
656 char *runtime_plt_and_got;
657 unsigned int runtime_plt_and_got_offset;
658 #endif
659 #endif
662 /* The current value can be: */
663 #define VT_VALMASK 0x003f
664 #define VT_CONST 0x0030 /* constant in vc
665 (must be first non register value) */
666 #define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
667 #define VT_LOCAL 0x0032 /* offset on stack */
668 #define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
669 #define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
670 #define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
671 #define VT_REF 0x0040 /* value is pointer to structure rather than address */
672 #define VT_LVAL 0x0100 /* var is an lvalue */
673 #define VT_SYM 0x0200 /* a symbol value is added */
674 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
675 char/short stored in integer registers) */
676 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
677 dereferencing value */
678 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
679 bounding function call point is in vc */
680 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
681 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
682 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
683 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
685 /* types */
686 #define VT_INT 0 /* integer type */
687 #define VT_BYTE 1 /* signed byte type */
688 #define VT_SHORT 2 /* short type */
689 #define VT_VOID 3 /* void type */
690 #define VT_PTR 4 /* pointer */
691 #define VT_ENUM 5 /* enum definition */
692 #define VT_FUNC 6 /* function type */
693 #define VT_STRUCT 7 /* struct/union definition */
694 #define VT_FLOAT 8 /* IEEE float */
695 #define VT_DOUBLE 9 /* IEEE double */
696 #define VT_LDOUBLE 10 /* IEEE long double */
697 #define VT_BOOL 11 /* ISOC99 boolean type */
698 #define VT_LLONG 12 /* 64 bit integer */
699 #define VT_LONG 13 /* long integer (NEVER USED as type, only
700 during parsing) */
701 #define VT_BTYPE 0x000f /* mask for basic type */
702 #define VT_UNSIGNED 0x0010 /* unsigned type */
703 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
704 #define VT_VLA 0x20000 /* VLA type (also has VT_PTR and VT_ARRAY) */
705 #define VT_BITFIELD 0x0040 /* bitfield modifier */
706 #define VT_CONSTANT 0x0800 /* const modifier */
707 #define VT_VOLATILE 0x1000 /* volatile modifier */
708 #define VT_SIGNED 0x2000 /* signed type */
710 /* storage */
711 #define VT_EXTERN 0x00000080 /* extern definition */
712 #define VT_STATIC 0x00000100 /* static variable */
713 #define VT_TYPEDEF 0x00000200 /* typedef definition */
714 #define VT_INLINE 0x00000400 /* inline definition */
715 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
716 #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
717 #define VT_WEAK 0x00010000 /* win32: data exported from dll */
719 #define VT_STRUCT_SHIFT 18 /* shift for bitfield shift values */
721 /* type mask (except storage) */
722 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK)
723 #define VT_TYPE (~(VT_STORAGE))
725 /* token values */
727 /* warning: the following compare tokens depend on i386 asm code */
728 #define TOK_ULT 0x92
729 #define TOK_UGE 0x93
730 #define TOK_EQ 0x94
731 #define TOK_NE 0x95
732 #define TOK_ULE 0x96
733 #define TOK_UGT 0x97
734 #define TOK_Nset 0x98
735 #define TOK_Nclear 0x99
736 #define TOK_LT 0x9c
737 #define TOK_GE 0x9d
738 #define TOK_LE 0x9e
739 #define TOK_GT 0x9f
741 #define TOK_LAND 0xa0
742 #define TOK_LOR 0xa1
744 #define TOK_DEC 0xa2
745 #define TOK_MID 0xa3 /* inc/dec, to void constant */
746 #define TOK_INC 0xa4
747 #define TOK_UDIV 0xb0 /* unsigned division */
748 #define TOK_UMOD 0xb1 /* unsigned modulo */
749 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
750 #define TOK_CINT 0xb3 /* number in tokc */
751 #define TOK_CCHAR 0xb4 /* char constant in tokc */
752 #define TOK_STR 0xb5 /* pointer to string in tokc */
753 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
754 #define TOK_LCHAR 0xb7
755 #define TOK_LSTR 0xb8
756 #define TOK_CFLOAT 0xb9 /* float constant */
757 #define TOK_LINENUM 0xba /* line number info */
758 #define TOK_CDOUBLE 0xc0 /* double constant */
759 #define TOK_CLDOUBLE 0xc1 /* long double constant */
760 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
761 #define TOK_ADDC1 0xc3 /* add with carry generation */
762 #define TOK_ADDC2 0xc4 /* add with carry use */
763 #define TOK_SUBC1 0xc5 /* add with carry generation */
764 #define TOK_SUBC2 0xc6 /* add with carry use */
765 #define TOK_CUINT 0xc8 /* unsigned int constant */
766 #define TOK_CLLONG 0xc9 /* long long constant */
767 #define TOK_CULLONG 0xca /* unsigned long long constant */
768 #define TOK_ARROW 0xcb
769 #define TOK_DOTS 0xcc /* three dots */
770 #define TOK_SHR 0xcd /* unsigned shift right */
771 #define TOK_PPNUM 0xce /* preprocessor number */
772 #define TOK_NOSUBST 0xcf /* means following token has already been pp'd */
774 #define TOK_SHL 0x01 /* shift left */
775 #define TOK_SAR 0x02 /* signed shift right */
777 /* assignement operators : normal operator or 0x80 */
778 #define TOK_A_MOD 0xa5
779 #define TOK_A_AND 0xa6
780 #define TOK_A_MUL 0xaa
781 #define TOK_A_ADD 0xab
782 #define TOK_A_SUB 0xad
783 #define TOK_A_DIV 0xaf
784 #define TOK_A_XOR 0xde
785 #define TOK_A_OR 0xfc
786 #define TOK_A_SHL 0x81
787 #define TOK_A_SAR 0x82
789 #ifndef offsetof
790 #define offsetof(type, field) ((size_t) &((type *)0)->field)
791 #endif
793 #ifndef countof
794 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
795 #endif
797 #define TOK_EOF (-1) /* end of file */
798 #define TOK_LINEFEED 10 /* line feed */
800 /* all identificators and strings have token above that */
801 #define TOK_IDENT 256
803 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
804 #define TOK_ASM_int TOK_INT
805 #define TOK_ASM_weak TOK_WEAK1
807 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
808 /* only used for i386 asm opcodes definitions */
809 #define DEF_BWL(x) \
810 DEF(TOK_ASM_ ## x ## b, #x "b") \
811 DEF(TOK_ASM_ ## x ## w, #x "w") \
812 DEF(TOK_ASM_ ## x ## l, #x "l") \
813 DEF(TOK_ASM_ ## x, #x)
814 #define DEF_WL(x) \
815 DEF(TOK_ASM_ ## x ## w, #x "w") \
816 DEF(TOK_ASM_ ## x ## l, #x "l") \
817 DEF(TOK_ASM_ ## x, #x)
818 #ifdef TCC_TARGET_X86_64
819 # define DEF_BWLQ(x) \
820 DEF(TOK_ASM_ ## x ## b, #x "b") \
821 DEF(TOK_ASM_ ## x ## w, #x "w") \
822 DEF(TOK_ASM_ ## x ## l, #x "l") \
823 DEF(TOK_ASM_ ## x ## q, #x "q") \
824 DEF(TOK_ASM_ ## x, #x)
825 # define DEF_WLQ(x) \
826 DEF(TOK_ASM_ ## x ## w, #x "w") \
827 DEF(TOK_ASM_ ## x ## l, #x "l") \
828 DEF(TOK_ASM_ ## x ## q, #x "q") \
829 DEF(TOK_ASM_ ## x, #x)
830 # define DEF_BWLX DEF_BWLQ
831 # define DEF_WLX DEF_WLQ
832 /* number of sizes + 1 */
833 # define NBWLX 5
834 #else
835 # define DEF_BWLX DEF_BWL
836 # define DEF_WLX DEF_WL
837 /* number of sizes + 1 */
838 # define NBWLX 4
839 #endif
841 #define DEF_FP1(x) \
842 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
843 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
844 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
845 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
847 #define DEF_FP(x) \
848 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
849 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
850 DEF_FP1(x)
852 #define DEF_ASMTEST(x) \
853 DEF_ASM(x ## o) \
854 DEF_ASM(x ## no) \
855 DEF_ASM(x ## b) \
856 DEF_ASM(x ## c) \
857 DEF_ASM(x ## nae) \
858 DEF_ASM(x ## nb) \
859 DEF_ASM(x ## nc) \
860 DEF_ASM(x ## ae) \
861 DEF_ASM(x ## e) \
862 DEF_ASM(x ## z) \
863 DEF_ASM(x ## ne) \
864 DEF_ASM(x ## nz) \
865 DEF_ASM(x ## be) \
866 DEF_ASM(x ## na) \
867 DEF_ASM(x ## nbe) \
868 DEF_ASM(x ## a) \
869 DEF_ASM(x ## s) \
870 DEF_ASM(x ## ns) \
871 DEF_ASM(x ## p) \
872 DEF_ASM(x ## pe) \
873 DEF_ASM(x ## np) \
874 DEF_ASM(x ## po) \
875 DEF_ASM(x ## l) \
876 DEF_ASM(x ## nge) \
877 DEF_ASM(x ## nl) \
878 DEF_ASM(x ## ge) \
879 DEF_ASM(x ## le) \
880 DEF_ASM(x ## ng) \
881 DEF_ASM(x ## nle) \
882 DEF_ASM(x ## g)
884 #endif // defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
886 enum tcc_token {
887 TOK_LAST = TOK_IDENT - 1,
888 #define DEF(id, str) id,
889 #include "tcctok.h"
890 #undef DEF
893 #define TOK_UIDENT TOK_DEFINE
895 #ifdef _WIN32
896 #define snprintf _snprintf
897 #define vsnprintf _vsnprintf
898 #ifndef __GNUC__
899 #define strtold (long double)strtod
900 #define strtof (float)strtod
901 #define strtoll _strtoi64
902 #define strtoull _strtoui64
903 #endif
904 #else
905 /* XXX: need to define this to use them in non ISOC99 context */
906 extern float strtof (const char *__nptr, char **__endptr);
907 extern long double strtold (const char *__nptr, char **__endptr);
908 #endif
910 #ifdef _WIN32
911 #define IS_DIRSEP(c) (c == '/' || c == '\\')
912 #define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
913 #define PATHCMP stricmp
914 #else
915 #define IS_DIRSEP(c) (c == '/')
916 #define IS_ABSPATH(p) IS_DIRSEP(p[0])
917 #define PATHCMP strcmp
918 #endif
920 #ifdef TCC_TARGET_PE
921 #define PATHSEP ';'
922 #else
923 #define PATHSEP ':'
924 #endif
926 /* space exlcuding newline */
927 static inline int is_space(int ch)
929 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
932 static inline int isid(int c)
934 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
937 static inline int isnum(int c)
939 return c >= '0' && c <= '9';
942 static inline int isoct(int c)
944 return c >= '0' && c <= '7';
947 static inline int toup(int c)
949 return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
952 #ifndef PUB_FUNC
953 # define PUB_FUNC
954 #endif
956 #ifdef ONE_SOURCE
957 #define ST_INLN static inline
958 #define ST_FUNC static
959 #define ST_DATA static
960 #else
961 #define ST_INLN
962 #define ST_FUNC
963 #define ST_DATA extern
964 #endif
966 /* ------------ libtcc.c ------------ */
968 /* use GNU C extensions */
969 ST_DATA int gnu_ext;
970 /* use Tiny C extensions */
971 ST_DATA int tcc_ext;
972 /* XXX: get rid of this ASAP */
973 ST_DATA struct TCCState *tcc_state;
975 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
976 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
977 #define AFF_PREPROCESS 0x0004 /* preprocess file */
979 /* public functions currently used by the tcc main function */
980 PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s);
981 PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s);
982 PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num);
983 PUB_FUNC char *tcc_basename(const char *name);
984 PUB_FUNC char *tcc_fileextension (const char *name);
985 PUB_FUNC void tcc_free(void *ptr);
986 PUB_FUNC void *tcc_malloc(unsigned long size);
987 PUB_FUNC void *tcc_mallocz(unsigned long size);
988 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
989 PUB_FUNC char *tcc_strdup(const char *str);
990 #define free(p) use_tcc_free(p)
991 #define malloc(s) use_tcc_malloc(s)
992 #define realloc(p, s) use_tcc_realloc(p, s)
993 #undef strdup
994 #define strdup(s) use_tcc_strdup(s)
995 PUB_FUNC void tcc_memstats(void);
996 PUB_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data);
997 PUB_FUNC void dynarray_reset(void *pp, int *n);
998 PUB_FUNC void tcc_error_noabort(const char *fmt, ...);
999 PUB_FUNC void tcc_error(const char *fmt, ...);
1000 PUB_FUNC void tcc_warning(const char *fmt, ...);
1002 /* other utilities */
1003 PUB_FUNC void cstr_ccat(CString *cstr, int ch);
1004 PUB_FUNC void cstr_cat(CString *cstr, const char *str);
1005 PUB_FUNC void cstr_wccat(CString *cstr, int ch);
1006 PUB_FUNC void cstr_new(CString *cstr);
1007 PUB_FUNC void cstr_free(CString *cstr);
1008 PUB_FUNC void cstr_reset(CString *cstr);
1010 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
1011 ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
1012 ST_FUNC void *section_ptr_add(Section *sec, unsigned long size);
1013 ST_FUNC void section_reserve(Section *sec, unsigned long size);
1014 ST_FUNC Section *find_section(TCCState *s1, const char *name);
1016 ST_FUNC void put_extern_sym2(Sym *sym, Section *section, uplong value, unsigned long size, int can_add_underscore);
1017 ST_FUNC void put_extern_sym(Sym *sym, Section *section, uplong value, unsigned long size);
1018 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);
1020 ST_INLN void sym_free(Sym *sym);
1021 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c);
1022 ST_FUNC Sym *sym_find2(Sym *s, int v);
1023 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c);
1024 ST_FUNC void sym_pop(Sym **ptop, Sym *b);
1025 ST_INLN Sym *struct_find(int v);
1026 ST_INLN Sym *sym_find(int v);
1027 ST_FUNC Sym *global_identifier_push(int v, int t, int c);
1029 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
1030 ST_FUNC int tcc_open(TCCState *s1, const char *filename);
1031 ST_FUNC void tcc_close(void);
1033 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
1034 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
1035 #ifndef TCC_TARGET_PE
1036 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
1037 #endif
1039 PUB_FUNC int tcc_set_flag(TCCState *s, const char *flag_name, int value);
1040 PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time);
1041 PUB_FUNC char *tcc_default_target(TCCState *s, const char *default_file);
1042 PUB_FUNC void tcc_gen_makedeps(TCCState *s, const char *target, const char *filename);
1044 /* ------------ tccpp.c ------------ */
1046 ST_DATA struct BufferedFile *file;
1047 ST_DATA int ch, tok;
1048 ST_DATA CValue tokc;
1049 ST_DATA const int *macro_ptr;
1050 ST_DATA int parse_flags;
1051 ST_DATA int tok_flags;
1052 ST_DATA CString tokcstr; /* current parsed string, if any */
1054 /* display benchmark infos */
1055 ST_DATA int total_lines;
1056 ST_DATA int total_bytes;
1057 ST_DATA int tok_ident;
1058 ST_DATA TokenSym **table_ident;
1060 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
1061 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
1062 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
1063 #define TOK_FLAG_EOF 0x0008 /* end of file */
1065 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
1066 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
1067 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
1068 token. line feed is also
1069 returned at eof */
1070 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
1071 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
1073 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
1074 ST_FUNC char *get_tok_str(int v, CValue *cv);
1075 ST_FUNC void save_parse_state(ParseState *s);
1076 ST_FUNC void restore_parse_state(ParseState *s);
1077 ST_INLN void tok_str_new(TokenString *s);
1078 ST_FUNC void tok_str_free(int *str);
1079 ST_FUNC void tok_str_add(TokenString *s, int t);
1080 ST_FUNC void tok_str_add_tok(TokenString *s);
1081 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
1082 ST_FUNC void define_undef(Sym *s);
1083 ST_INLN Sym *define_find(int v);
1084 ST_FUNC void free_defines(Sym *b);
1085 ST_FUNC Sym *label_find(int v);
1086 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
1087 ST_FUNC void label_pop(Sym **ptop, Sym *slast);
1088 ST_FUNC void parse_define(void);
1089 ST_FUNC void preprocess(int is_bof);
1090 ST_FUNC void next_nomacro(void);
1091 ST_FUNC void next(void);
1092 ST_INLN void unget_tok(int last_tok);
1093 ST_FUNC void preprocess_init(TCCState *s1);
1094 ST_FUNC void preprocess_new();
1095 ST_FUNC int tcc_preprocess(TCCState *s1);
1096 ST_FUNC void skip(int c);
1097 ST_FUNC void expect(const char *msg);
1099 /* ------------ tccgen.c ------------ */
1101 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
1102 ST_DATA Section *cur_text_section; /* current section where function code is generated */
1103 #ifdef CONFIG_TCC_ASM
1104 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
1105 #endif
1106 #ifdef CONFIG_TCC_BCHECK
1107 /* bound check related sections */
1108 ST_DATA Section *bounds_section; /* contains global data bound description */
1109 ST_DATA Section *lbounds_section; /* contains local data bound description */
1110 #endif
1111 /* symbol sections */
1112 ST_DATA Section *symtab_section, *strtab_section;
1113 /* debug sections */
1114 ST_DATA Section *stab_section, *stabstr_section;
1116 #define SYM_POOL_NB (8192 / sizeof(Sym))
1117 ST_DATA Sym *sym_free_first;
1118 ST_DATA void **sym_pools;
1119 ST_DATA int nb_sym_pools;
1121 ST_DATA Sym *global_stack;
1122 ST_DATA Sym *local_stack;
1123 ST_DATA Sym *local_label_stack;
1124 ST_DATA Sym *global_label_stack;
1125 ST_DATA Sym *define_stack;
1126 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
1127 ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop;
1128 #define vstack (__vstack + 1)
1129 ST_DATA int rsym, anon_sym, ind, loc;
1131 ST_DATA int const_wanted; /* true if constant wanted */
1132 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
1133 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
1134 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
1135 ST_DATA int func_vc;
1136 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
1137 ST_DATA char *funcname;
1139 ST_INLN int is_float(int t);
1140 ST_FUNC int ieee_finite(double d);
1141 ST_FUNC void test_lvalue(void);
1142 ST_FUNC void swap(int *p, int *q);
1143 ST_FUNC void vpushi(int v);
1144 ST_FUNC Sym *external_global_sym(int v, CType *type, int r);
1145 ST_FUNC void vset(CType *type, int r, int v);
1146 ST_FUNC void vswap(void);
1147 ST_FUNC void vpush_global_sym(CType *type, int v);
1148 ST_FUNC void vrote(SValue *e, int n);
1149 ST_FUNC void vrott(int n);
1150 ST_FUNC void vrotb(int n);
1151 #ifdef TCC_TARGET_ARM
1152 ST_FUNC int get_reg_ex(int rc, int rc2);
1153 ST_FUNC void lexpand_nr(void);
1154 #endif
1155 ST_FUNC void vpushv(SValue *v);
1156 ST_FUNC void save_reg(int r);
1157 ST_FUNC int get_reg(int rc);
1158 ST_FUNC void save_regs(int n);
1159 ST_FUNC int gv(int rc);
1160 ST_FUNC void gv2(int rc1, int rc2);
1161 ST_FUNC void vpop(void);
1162 ST_FUNC void gen_op(int op);
1163 ST_FUNC int type_size(CType *type, int *a);
1164 ST_FUNC void mk_pointer(CType *type);
1165 ST_FUNC void vstore(void);
1166 ST_FUNC void inc(int post, int c);
1167 ST_FUNC void parse_asm_str(CString *astr);
1168 ST_FUNC int lvalue_type(int t);
1169 ST_FUNC void indir(void);
1170 ST_FUNC void unary(void);
1171 ST_FUNC void expr_prod(void);
1172 ST_FUNC void expr_sum(void);
1173 ST_FUNC void gexpr(void);
1174 ST_FUNC int expr_const(void);
1175 ST_FUNC void gen_inline_functions(void);
1176 ST_FUNC void decl(int l);
1177 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1178 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size);
1179 #endif
1181 /* ------------ tccelf.c ------------ */
1183 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1185 typedef struct {
1186 unsigned int n_strx; /* index into string table of name */
1187 unsigned char n_type; /* type of symbol */
1188 unsigned char n_other; /* misc info (usually empty) */
1189 unsigned short n_desc; /* description field */
1190 unsigned int n_value; /* value of symbol */
1191 } Stab_Sym;
1193 ST_FUNC Section *new_symtab(TCCState *s1, const char *symtab_name, int sh_type, int sh_flags, const char *strtab_name, const char *hash_name, int hash_sh_flags);
1195 ST_FUNC int put_elf_str(Section *s, const char *sym);
1196 ST_FUNC int put_elf_sym(Section *s, uplong value, unsigned long size, int info, int other, int shndx, const char *name);
1197 ST_FUNC int add_elf_sym(Section *s, uplong value, unsigned long size, int info, int other, int sh_num, const char *name);
1198 ST_FUNC int find_elf_sym(Section *s, const char *name);
1199 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
1201 ST_FUNC void put_stabs(const char *str, int type, int other, int desc, unsigned long value);
1202 ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index);
1203 ST_FUNC void put_stabn(int type, int other, int desc, int value);
1204 ST_FUNC void put_stabd(int type, int other, int desc);
1206 ST_FUNC void relocate_common_syms(void);
1207 ST_FUNC void relocate_syms(TCCState *s1, int do_resolve);
1208 ST_FUNC void relocate_section(TCCState *s1, Section *s);
1210 ST_FUNC void tcc_add_linker_symbols(TCCState *s1);
1211 ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
1212 ST_FUNC int tcc_load_archive(TCCState *s1, int fd);
1213 ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
1214 ST_FUNC void tcc_add_bcheck(TCCState *s1);
1216 ST_FUNC void build_got_entries(TCCState *s1);
1217 ST_FUNC void tcc_add_runtime(TCCState *s1);
1219 #ifndef TCC_TARGET_PE
1220 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
1221 ST_FUNC int tcc_load_ldscript(TCCState *s1);
1222 ST_FUNC uint8_t *parse_comment(uint8_t *p);
1223 ST_FUNC void minp(void);
1224 ST_INLN void inp(void);
1225 ST_FUNC int handle_eob(void);
1226 #endif
1228 #ifdef TCC_TARGET_X86_64
1229 # define ELFCLASSW ELFCLASS64
1230 # define ElfW(type) Elf##64##_##type
1231 # define ELFW(type) ELF##64##_##type
1232 # define ElfW_Rel ElfW(Rela)
1233 # define SHT_RELX SHT_RELA
1234 # define REL_SECTION_FMT ".rela%s"
1235 /* XXX: DLL with PLT would only work with x86-64 for now */
1236 # define TCC_OUTPUT_DLL_WITH_PLT
1237 #else
1238 # define ELFCLASSW ELFCLASS32
1239 # define ElfW(type) Elf##32##_##type
1240 # define ELFW(type) ELF##32##_##type
1241 # define ElfW_Rel ElfW(Rel)
1242 # define SHT_RELX SHT_REL
1243 # define REL_SECTION_FMT ".rel%s"
1244 #endif
1246 /* ------------ xxx-gen.c ------------ */
1248 ST_FUNC void gsym_addr(int t, int a);
1249 ST_FUNC void gsym(int t);
1250 ST_FUNC void load(int r, SValue *sv);
1251 ST_FUNC void store(int r, SValue *v);
1252 ST_FUNC void gfunc_call(int nb_args);
1253 ST_FUNC void gfunc_prolog(CType *func_type);
1254 ST_FUNC void gfunc_epilog(void);
1255 ST_FUNC int gjmp(int t);
1256 ST_FUNC void gjmp_addr(int a);
1257 ST_FUNC int gtst(int inv, int t);
1258 ST_FUNC void gen_opi(int op);
1259 ST_FUNC void gen_opf(int op);
1260 ST_FUNC void gen_cvt_ftoi(int t);
1261 ST_FUNC void gen_cvt_ftof(int t);
1262 ST_FUNC void ggoto(void);
1263 #ifndef TCC_TARGET_C67
1264 ST_FUNC void o(unsigned int c);
1265 #endif
1266 #ifndef TCC_TARGET_ARM
1267 ST_FUNC void gen_cvt_itof(int t);
1268 #endif
1270 /* ------------ i386-gen.c ------------ */
1271 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1272 ST_FUNC void g(int c);
1273 ST_FUNC int oad(int c, int s);
1274 ST_FUNC void gen_le16(int c);
1275 ST_FUNC void gen_le32(int c);
1276 ST_FUNC void gen_addr32(int r, Sym *sym, int c);
1277 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c);
1278 #endif
1280 #ifdef CONFIG_TCC_BCHECK
1281 ST_FUNC void gen_bounded_ptr_add(void);
1282 ST_FUNC void gen_bounded_ptr_deref(void);
1283 #endif
1285 /* ------------ x86_64-gen.c ------------ */
1286 #ifdef TCC_TARGET_X86_64
1287 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c);
1288 #endif
1290 /* ------------ arm-gen.c ------------ */
1291 #ifdef TCC_TARGET_ARM
1292 ST_FUNC uint32_t encbranch(int pos, int addr, int fail);
1293 ST_FUNC void gen_cvt_itof1(int t);
1294 #endif
1296 /* ------------ c67-gen.c ------------ */
1297 #ifdef TCC_TARGET_C67
1298 #endif
1300 /* ------------ tcccoff.c ------------ */
1302 #ifdef TCC_TARGET_COFF
1303 ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f);
1304 ST_FUNC int tcc_load_coff(TCCState * s1, int fd);
1305 #endif
1307 /* ------------ tccasm.c ------------ */
1308 ST_FUNC void asm_instr(void);
1309 ST_FUNC void asm_global_instr(void);
1311 #ifdef CONFIG_TCC_ASM
1312 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
1313 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
1314 ST_FUNC int asm_int_expr(TCCState *s1);
1315 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
1316 /* ------------ i386-asm.c ------------ */
1317 ST_FUNC void gen_expr32(ExprValue *pe);
1318 ST_FUNC void asm_opcode(TCCState *s1, int opcode);
1319 ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs, int *pout_reg);
1320 ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier);
1321 ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, int out_reg);
1322 ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str);
1323 #endif
1324 /* ------------ tccpe.c -------------- */
1325 #ifdef TCC_TARGET_PE
1326 ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd);
1327 ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
1328 ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, const void *value);
1329 ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
1330 /* tiny_impdef.c */
1331 ST_FUNC char *get_export_names(FILE *fp);
1332 #ifdef TCC_TARGET_X86_64
1333 ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
1334 #endif
1335 #endif
1337 /* ------------ tccrun.c ----------------- */
1338 #ifdef CONFIG_TCC_STATIC
1339 #define RTLD_LAZY 0x001
1340 #define RTLD_NOW 0x002
1341 #define RTLD_GLOBAL 0x100
1342 #define RTLD_DEFAULT NULL
1343 /* dummy function for profiling */
1344 ST_FUNC void *dlopen(const char *filename, int flag);
1345 ST_FUNC void dlclose(void *p);
1346 //ST_FUNC const char *dlerror(void);
1347 ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
1348 #elif !defined TCC_TARGET_PE || !defined _WIN32
1349 ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
1350 #endif
1352 #ifdef CONFIG_TCC_BACKTRACE
1353 ST_DATA int rt_num_callers;
1354 ST_DATA const char **rt_bound_error_msg;
1355 ST_DATA void *rt_prog_main;
1356 PUB_FUNC void tcc_set_num_callers(int n);
1357 #endif
1359 /********************************************************/
1360 /* include the target specific definitions */
1362 #define TARGET_DEFS_ONLY
1363 #ifdef TCC_TARGET_I386
1364 #include "i386-gen.c"
1365 #endif
1366 #ifdef TCC_TARGET_X86_64
1367 #include "x86_64-gen.c"
1368 #endif
1369 #ifdef TCC_TARGET_ARM
1370 #include "arm-gen.c"
1371 #endif
1372 #ifdef TCC_TARGET_C67
1373 #include "coff.h"
1374 #include "c67-gen.c"
1375 #endif
1376 #undef TARGET_DEFS_ONLY
1378 ST_DATA const int reg_classes[];
1380 /********************************************************/
1381 #undef ST_DATA
1382 #ifdef ONE_SOURCE
1383 #define ST_DATA static
1384 #else
1385 #define ST_DATA
1386 #endif
1387 /********************************************************/
1388 #endif /* _TCC_H */