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