move minor things from libtcc.c to other files
[tinycc.git] / libtcc.c
blobc9505d697f2bc7fc7d64d9a63798a81e71bec979
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 #include "tcc.h"
23 /* parser */
24 static struct BufferedFile *file;
25 static int ch, tok;
26 static CValue tokc;
27 static CString tokcstr; /* current parsed string, if any */
28 /* additional informations about token */
29 static int tok_flags;
30 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
31 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
32 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
33 #define TOK_FLAG_EOF 0x0008 /* end of file */
35 static int *macro_ptr, *macro_ptr_allocated;
36 static int *unget_saved_macro_ptr;
37 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
38 static int unget_buffer_enabled;
39 static int parse_flags;
40 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
41 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
42 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
43 token. line feed is also
44 returned at eof */
45 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
46 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
48 static Section *text_section, *data_section, *bss_section; /* predefined sections */
49 static Section *cur_text_section; /* current section where function code is
50 generated */
51 #ifdef CONFIG_TCC_ASM
52 static Section *last_text_section; /* to handle .previous asm directive */
53 #endif
54 /* bound check related sections */
55 static Section *bounds_section; /* contains global data bound description */
56 static Section *lbounds_section; /* contains local data bound description */
57 /* symbol sections */
58 static Section *symtab_section, *strtab_section;
60 /* debug sections */
61 static Section *stab_section, *stabstr_section;
63 /* loc : local variable index
64 ind : output code index
65 rsym: return symbol
66 anon_sym: anonymous symbol index
68 static int rsym, anon_sym, ind, loc;
69 /* expression generation modifiers */
70 static int const_wanted; /* true if constant wanted */
71 static int nocode_wanted; /* true if no code generation wanted for an expression */
72 static int global_expr; /* true if compound literals must be allocated
73 globally (used during initializers parsing */
74 static CType func_vt; /* current function return type (used by return
75 instruction) */
76 static int func_vc;
77 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
78 static int tok_ident;
79 static TokenSym **table_ident;
80 static TokenSym *hash_ident[TOK_HASH_SIZE];
81 static char token_buf[STRING_MAX_SIZE + 1];
82 static char *funcname;
83 static Sym *global_stack, *local_stack;
84 static Sym *define_stack;
85 static Sym *global_label_stack, *local_label_stack;
86 /* symbol allocator */
87 #define SYM_POOL_NB (8192 / sizeof(Sym))
88 static Sym *sym_free_first;
89 static void **sym_pools;
90 static int nb_sym_pools;
92 static SValue vstack[VSTACK_SIZE], *vtop;
93 /* some predefined types */
94 static CType char_pointer_type, func_old_type, int_type;
96 /* display some information during compilation */
97 static int verbose = 0;
99 /* compile with debug symbol (and use them if error during execution) */
100 static int do_debug = 0;
102 /* compile with built-in memory and bounds checker */
103 static int do_bounds_check = 0;
105 /* display benchmark infos */
106 static int total_lines;
107 static int total_bytes;
109 /* use GNU C extensions */
110 static int gnu_ext = 1;
112 /* use Tiny C extensions */
113 static int tcc_ext = 1;
115 /* max number of callers shown if error */
116 #ifdef CONFIG_TCC_BACKTRACE
117 static int num_callers = 6;
118 static const char **rt_bound_error_msg;
119 #endif
121 /* XXX: get rid of this ASAP */
122 static struct TCCState *tcc_state;
124 /* give the path of the tcc libraries */
125 static const char *tcc_lib_path = CONFIG_TCCDIR;
129 #ifdef TCC_TARGET_I386
130 #include "i386-gen.c"
131 #endif
133 #ifdef TCC_TARGET_ARM
134 #include "arm-gen.c"
135 #endif
137 #ifdef TCC_TARGET_C67
138 #include "c67-gen.c"
139 #endif
141 #ifdef TCC_TARGET_X86_64
142 #include "x86_64-gen.c"
143 #endif
145 #ifdef CONFIG_TCC_STATIC
147 #define RTLD_LAZY 0x001
148 #define RTLD_NOW 0x002
149 #define RTLD_GLOBAL 0x100
150 #define RTLD_DEFAULT NULL
152 /* dummy function for profiling */
153 void *dlopen(const char *filename, int flag)
155 return NULL;
158 void dlclose(void *p)
162 const char *dlerror(void)
164 return "error";
167 typedef struct TCCSyms {
168 char *str;
169 void *ptr;
170 } TCCSyms;
172 #define TCCSYM(a) { #a, &a, },
174 /* add the symbol you want here if no dynamic linking is done */
175 static TCCSyms tcc_syms[] = {
176 #if !defined(CONFIG_TCCBOOT)
177 TCCSYM(printf)
178 TCCSYM(fprintf)
179 TCCSYM(fopen)
180 TCCSYM(fclose)
181 #endif
182 { NULL, NULL },
185 void *resolve_sym(TCCState *s1, const char *symbol, int type)
187 TCCSyms *p;
188 p = tcc_syms;
189 while (p->str != NULL) {
190 if (!strcmp(p->str, symbol))
191 return p->ptr;
192 p++;
194 return NULL;
197 #elif !defined(_WIN32)
199 #include <dlfcn.h>
201 void *resolve_sym(TCCState *s1, const char *sym, int type)
203 return dlsym(RTLD_DEFAULT, sym);
206 #endif
208 /********************************************************/
210 /* we use our own 'finite' function to avoid potential problems with
211 non standard math libs */
212 /* XXX: endianness dependent */
213 int ieee_finite(double d)
215 int *p = (int *)&d;
216 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
219 /* copy a string and truncate it. */
220 static char *pstrcpy(char *buf, int buf_size, const char *s)
222 char *q, *q_end;
223 int c;
225 if (buf_size > 0) {
226 q = buf;
227 q_end = buf + buf_size - 1;
228 while (q < q_end) {
229 c = *s++;
230 if (c == '\0')
231 break;
232 *q++ = c;
234 *q = '\0';
236 return buf;
239 /* strcat and truncate. */
240 static char *pstrcat(char *buf, int buf_size, const char *s)
242 int len;
243 len = strlen(buf);
244 if (len < buf_size)
245 pstrcpy(buf + len, buf_size - len, s);
246 return buf;
249 /* extract the basename of a file */
250 static char *tcc_basename(const char *name)
252 char *p = strchr(name, 0);
253 while (p > name && !IS_PATHSEP(p[-1]))
254 --p;
255 return p;
258 static char *tcc_fileextension (const char *name)
260 char *b = tcc_basename(name);
261 char *e = strrchr(b, '.');
262 return e ? e : strchr(b, 0);
265 #ifdef _WIN32
266 char *normalize_slashes(char *path)
268 char *p;
269 for (p = path; *p; ++p)
270 if (*p == '\\')
271 *p = '/';
272 return path;
275 void tcc_set_lib_path_w32(TCCState *s)
277 /* on win32, we suppose the lib and includes are at the location
278 of 'tcc.exe' */
279 char path[1024], *p;
280 GetModuleFileNameA(NULL, path, sizeof path);
281 p = tcc_basename(normalize_slashes(strlwr(path)));
282 if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
283 p -= 5;
284 else if (p > path)
285 p--;
286 *p = 0;
287 tcc_set_lib_path(s, path);
289 #endif
291 void set_pages_executable(void *ptr, unsigned long length)
293 #ifdef _WIN32
294 unsigned long old_protect;
295 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
296 #else
297 unsigned long start, end;
298 start = (unsigned long)ptr & ~(PAGESIZE - 1);
299 end = (unsigned long)ptr + length;
300 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
301 mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
302 #endif
305 /* memory management */
306 #ifdef MEM_DEBUG
307 int mem_cur_size;
308 int mem_max_size;
309 unsigned malloc_usable_size(void*);
310 #endif
312 static inline void tcc_free(void *ptr)
314 #ifdef MEM_DEBUG
315 mem_cur_size -= malloc_usable_size(ptr);
316 #endif
317 free(ptr);
320 static void *tcc_malloc(unsigned long size)
322 void *ptr;
323 ptr = malloc(size);
324 if (!ptr && size)
325 error("memory full");
326 #ifdef MEM_DEBUG
327 mem_cur_size += malloc_usable_size(ptr);
328 if (mem_cur_size > mem_max_size)
329 mem_max_size = mem_cur_size;
330 #endif
331 return ptr;
334 static void *tcc_mallocz(unsigned long size)
336 void *ptr;
337 ptr = tcc_malloc(size);
338 memset(ptr, 0, size);
339 return ptr;
342 static inline void *tcc_realloc(void *ptr, unsigned long size)
344 void *ptr1;
345 #ifdef MEM_DEBUG
346 mem_cur_size -= malloc_usable_size(ptr);
347 #endif
348 ptr1 = realloc(ptr, size);
349 #ifdef MEM_DEBUG
350 /* NOTE: count not correct if alloc error, but not critical */
351 mem_cur_size += malloc_usable_size(ptr1);
352 if (mem_cur_size > mem_max_size)
353 mem_max_size = mem_cur_size;
354 #endif
355 return ptr1;
358 static char *tcc_strdup(const char *str)
360 char *ptr;
361 ptr = tcc_malloc(strlen(str) + 1);
362 strcpy(ptr, str);
363 return ptr;
366 #define free(p) use_tcc_free(p)
367 #define malloc(s) use_tcc_malloc(s)
368 #define realloc(p, s) use_tcc_realloc(p, s)
370 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
372 int nb, nb_alloc;
373 void **pp;
375 nb = *nb_ptr;
376 pp = *ptab;
377 /* every power of two we double array size */
378 if ((nb & (nb - 1)) == 0) {
379 if (!nb)
380 nb_alloc = 1;
381 else
382 nb_alloc = nb * 2;
383 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
384 if (!pp)
385 error("memory full");
386 *ptab = pp;
388 pp[nb++] = data;
389 *nb_ptr = nb;
392 static void dynarray_reset(void *pp, int *n)
394 void **p;
395 for (p = *(void***)pp; *n; ++p, --*n)
396 if (*p)
397 tcc_free(*p);
398 tcc_free(*(void**)pp);
399 *(void**)pp = NULL;
402 /* symbol allocator */
403 static Sym *__sym_malloc(void)
405 Sym *sym_pool, *sym, *last_sym;
406 int i;
408 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
409 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
411 last_sym = sym_free_first;
412 sym = sym_pool;
413 for(i = 0; i < SYM_POOL_NB; i++) {
414 sym->next = last_sym;
415 last_sym = sym;
416 sym++;
418 sym_free_first = last_sym;
419 return last_sym;
422 static inline Sym *sym_malloc(void)
424 Sym *sym;
425 sym = sym_free_first;
426 if (!sym)
427 sym = __sym_malloc();
428 sym_free_first = sym->next;
429 return sym;
432 static inline void sym_free(Sym *sym)
434 sym->next = sym_free_first;
435 sym_free_first = sym;
438 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
440 Section *sec;
442 sec = tcc_mallocz(sizeof(Section) + strlen(name));
443 strcpy(sec->name, name);
444 sec->sh_type = sh_type;
445 sec->sh_flags = sh_flags;
446 switch(sh_type) {
447 case SHT_HASH:
448 case SHT_REL:
449 case SHT_RELA:
450 case SHT_DYNSYM:
451 case SHT_SYMTAB:
452 case SHT_DYNAMIC:
453 sec->sh_addralign = 4;
454 break;
455 case SHT_STRTAB:
456 sec->sh_addralign = 1;
457 break;
458 default:
459 sec->sh_addralign = 32; /* default conservative alignment */
460 break;
463 if (sh_flags & SHF_PRIVATE) {
464 dynarray_add((void ***)&s1->priv_sections, &s1->nb_priv_sections, sec);
465 } else {
466 sec->sh_num = s1->nb_sections;
467 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
470 return sec;
473 static void free_section(Section *s)
475 tcc_free(s->data);
478 /* realloc section and set its content to zero */
479 static void section_realloc(Section *sec, unsigned long new_size)
481 unsigned long size;
482 unsigned char *data;
484 size = sec->data_allocated;
485 if (size == 0)
486 size = 1;
487 while (size < new_size)
488 size = size * 2;
489 data = tcc_realloc(sec->data, size);
490 if (!data)
491 error("memory full");
492 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
493 sec->data = data;
494 sec->data_allocated = size;
497 /* reserve at least 'size' bytes in section 'sec' from
498 sec->data_offset. */
499 static void *section_ptr_add(Section *sec, unsigned long size)
501 unsigned long offset, offset1;
503 offset = sec->data_offset;
504 offset1 = offset + size;
505 if (offset1 > sec->data_allocated)
506 section_realloc(sec, offset1);
507 sec->data_offset = offset1;
508 return sec->data + offset;
511 /* return a reference to a section, and create it if it does not
512 exists */
513 Section *find_section(TCCState *s1, const char *name)
515 Section *sec;
516 int i;
517 for(i = 1; i < s1->nb_sections; i++) {
518 sec = s1->sections[i];
519 if (!strcmp(name, sec->name))
520 return sec;
522 /* sections are created as PROGBITS */
523 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
526 /* update sym->c so that it points to an external symbol in section
527 'section' with value 'value' */
528 static void put_extern_sym2(Sym *sym, Section *section,
529 unsigned long value, unsigned long size,
530 int can_add_underscore)
532 int sym_type, sym_bind, sh_num, info, other, attr;
533 ElfW(Sym) *esym;
534 const char *name;
535 char buf1[256];
537 if (section == NULL)
538 sh_num = SHN_UNDEF;
539 else if (section == SECTION_ABS)
540 sh_num = SHN_ABS;
541 else
542 sh_num = section->sh_num;
544 other = attr = 0;
546 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
547 sym_type = STT_FUNC;
548 #ifdef TCC_TARGET_PE
549 if (sym->type.ref)
550 attr = sym->type.ref->r;
551 if (FUNC_EXPORT(attr))
552 other |= 1;
553 if (FUNC_CALL(attr) == FUNC_STDCALL)
554 other |= 2;
555 #endif
556 } else {
557 sym_type = STT_OBJECT;
560 if (sym->type.t & VT_STATIC)
561 sym_bind = STB_LOCAL;
562 else
563 sym_bind = STB_GLOBAL;
565 if (!sym->c) {
566 name = get_tok_str(sym->v, NULL);
567 #ifdef CONFIG_TCC_BCHECK
568 if (do_bounds_check) {
569 char buf[32];
571 /* XXX: avoid doing that for statics ? */
572 /* if bound checking is activated, we change some function
573 names by adding the "__bound" prefix */
574 switch(sym->v) {
575 #if 0
576 /* XXX: we rely only on malloc hooks */
577 case TOK_malloc:
578 case TOK_free:
579 case TOK_realloc:
580 case TOK_memalign:
581 case TOK_calloc:
582 #endif
583 case TOK_memcpy:
584 case TOK_memmove:
585 case TOK_memset:
586 case TOK_strlen:
587 case TOK_strcpy:
588 case TOK__alloca:
589 strcpy(buf, "__bound_");
590 strcat(buf, name);
591 name = buf;
592 break;
595 #endif
597 #ifdef TCC_TARGET_PE
598 if ((other & 2) && can_add_underscore) {
599 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr));
600 name = buf1;
601 } else
602 #endif
603 if (tcc_state->leading_underscore && can_add_underscore) {
604 buf1[0] = '_';
605 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
606 name = buf1;
608 info = ELFW(ST_INFO)(sym_bind, sym_type);
609 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
610 } else {
611 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
612 esym->st_value = value;
613 esym->st_size = size;
614 esym->st_shndx = sh_num;
615 esym->st_other |= other;
619 static void put_extern_sym(Sym *sym, Section *section,
620 unsigned long value, unsigned long size)
622 put_extern_sym2(sym, section, value, size, 1);
625 /* add a new relocation entry to symbol 'sym' in section 's' */
626 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
628 if (!sym->c)
629 put_extern_sym(sym, NULL, 0, 0);
630 /* now we can add ELF relocation info */
631 put_elf_reloc(symtab_section, s, offset, type, sym->c);
634 static inline int isid(int c)
636 return (c >= 'a' && c <= 'z') ||
637 (c >= 'A' && c <= 'Z') ||
638 c == '_';
641 static inline int isnum(int c)
643 return c >= '0' && c <= '9';
646 static inline int isoct(int c)
648 return c >= '0' && c <= '7';
651 static inline int toup(int c)
653 if (c >= 'a' && c <= 'z')
654 return c - 'a' + 'A';
655 else
656 return c;
659 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
661 int len;
662 len = strlen(buf);
663 vsnprintf(buf + len, buf_size - len, fmt, ap);
666 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
668 va_list ap;
669 va_start(ap, fmt);
670 strcat_vprintf(buf, buf_size, fmt, ap);
671 va_end(ap);
674 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
676 char buf[2048];
677 BufferedFile **f;
679 buf[0] = '\0';
680 if (file) {
681 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
682 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
683 (*f)->filename, (*f)->line_num);
684 if (file->line_num > 0) {
685 strcat_printf(buf, sizeof(buf),
686 "%s:%d: ", file->filename, file->line_num);
687 } else {
688 strcat_printf(buf, sizeof(buf),
689 "%s: ", file->filename);
691 } else {
692 strcat_printf(buf, sizeof(buf),
693 "tcc: ");
695 if (is_warning)
696 strcat_printf(buf, sizeof(buf), "warning: ");
697 strcat_vprintf(buf, sizeof(buf), fmt, ap);
699 if (!s1->error_func) {
700 /* default case: stderr */
701 fprintf(stderr, "%s\n", buf);
702 } else {
703 s1->error_func(s1->error_opaque, buf);
705 if (!is_warning || s1->warn_error)
706 s1->nb_errors++;
709 #ifdef LIBTCC
710 void tcc_set_error_func(TCCState *s, void *error_opaque,
711 void (*error_func)(void *opaque, const char *msg))
713 s->error_opaque = error_opaque;
714 s->error_func = error_func;
716 #endif
718 /* error without aborting current compilation */
719 void error_noabort(const char *fmt, ...)
721 TCCState *s1 = tcc_state;
722 va_list ap;
724 va_start(ap, fmt);
725 error1(s1, 0, fmt, ap);
726 va_end(ap);
729 void error(const char *fmt, ...)
731 TCCState *s1 = tcc_state;
732 va_list ap;
734 va_start(ap, fmt);
735 error1(s1, 0, fmt, ap);
736 va_end(ap);
737 /* better than nothing: in some cases, we accept to handle errors */
738 if (s1->error_set_jmp_enabled) {
739 longjmp(s1->error_jmp_buf, 1);
740 } else {
741 /* XXX: eliminate this someday */
742 exit(1);
746 void expect(const char *msg)
748 error("%s expected", msg);
751 void warning(const char *fmt, ...)
753 TCCState *s1 = tcc_state;
754 va_list ap;
756 if (s1->warn_none)
757 return;
759 va_start(ap, fmt);
760 error1(s1, 1, fmt, ap);
761 va_end(ap);
764 void skip(int c)
766 if (tok != c)
767 error("'%c' expected", c);
768 next();
771 static void test_lvalue(void)
773 if (!(vtop->r & VT_LVAL))
774 expect("lvalue");
777 /* CString handling */
779 static void cstr_realloc(CString *cstr, int new_size)
781 int size;
782 void *data;
784 size = cstr->size_allocated;
785 if (size == 0)
786 size = 8; /* no need to allocate a too small first string */
787 while (size < new_size)
788 size = size * 2;
789 data = tcc_realloc(cstr->data_allocated, size);
790 if (!data)
791 error("memory full");
792 cstr->data_allocated = data;
793 cstr->size_allocated = size;
794 cstr->data = data;
797 /* add a byte */
798 static inline void cstr_ccat(CString *cstr, int ch)
800 int size;
801 size = cstr->size + 1;
802 if (size > cstr->size_allocated)
803 cstr_realloc(cstr, size);
804 ((unsigned char *)cstr->data)[size - 1] = ch;
805 cstr->size = size;
808 static void cstr_cat(CString *cstr, const char *str)
810 int c;
811 for(;;) {
812 c = *str;
813 if (c == '\0')
814 break;
815 cstr_ccat(cstr, c);
816 str++;
820 /* add a wide char */
821 static void cstr_wccat(CString *cstr, int ch)
823 int size;
824 size = cstr->size + sizeof(nwchar_t);
825 if (size > cstr->size_allocated)
826 cstr_realloc(cstr, size);
827 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
828 cstr->size = size;
831 static void cstr_new(CString *cstr)
833 memset(cstr, 0, sizeof(CString));
836 /* free string and reset it to NULL */
837 static void cstr_free(CString *cstr)
839 tcc_free(cstr->data_allocated);
840 cstr_new(cstr);
843 #define cstr_reset(cstr) cstr_free(cstr)
845 /* XXX: unicode ? */
846 static void add_char(CString *cstr, int c)
848 if (c == '\'' || c == '\"' || c == '\\') {
849 /* XXX: could be more precise if char or string */
850 cstr_ccat(cstr, '\\');
852 if (c >= 32 && c <= 126) {
853 cstr_ccat(cstr, c);
854 } else {
855 cstr_ccat(cstr, '\\');
856 if (c == '\n') {
857 cstr_ccat(cstr, 'n');
858 } else {
859 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
860 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
861 cstr_ccat(cstr, '0' + (c & 7));
866 /* push, without hashing */
867 static Sym *sym_push2(Sym **ps, int v, int t, long c)
869 Sym *s;
870 s = sym_malloc();
871 s->v = v;
872 s->type.t = t;
873 s->c = c;
874 s->next = NULL;
875 /* add in stack */
876 s->prev = *ps;
877 *ps = s;
878 return s;
881 /* find a symbol and return its associated structure. 's' is the top
882 of the symbol stack */
883 static Sym *sym_find2(Sym *s, int v)
885 while (s) {
886 if (s->v == v)
887 return s;
888 s = s->prev;
890 return NULL;
893 /* structure lookup */
894 static inline Sym *struct_find(int v)
896 v -= TOK_IDENT;
897 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
898 return NULL;
899 return table_ident[v]->sym_struct;
902 /* find an identifier */
903 static inline Sym *sym_find(int v)
905 v -= TOK_IDENT;
906 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
907 return NULL;
908 return table_ident[v]->sym_identifier;
911 /* push a given symbol on the symbol stack */
912 static Sym *sym_push(int v, CType *type, int r, int c)
914 Sym *s, **ps;
915 TokenSym *ts;
917 if (local_stack)
918 ps = &local_stack;
919 else
920 ps = &global_stack;
921 s = sym_push2(ps, v, type->t, c);
922 s->type.ref = type->ref;
923 s->r = r;
924 /* don't record fields or anonymous symbols */
925 /* XXX: simplify */
926 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
927 /* record symbol in token array */
928 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
929 if (v & SYM_STRUCT)
930 ps = &ts->sym_struct;
931 else
932 ps = &ts->sym_identifier;
933 s->prev_tok = *ps;
934 *ps = s;
936 return s;
939 /* push a global identifier */
940 static Sym *global_identifier_push(int v, int t, int c)
942 Sym *s, **ps;
943 s = sym_push2(&global_stack, v, t, c);
944 /* don't record anonymous symbol */
945 if (v < SYM_FIRST_ANOM) {
946 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
947 /* modify the top most local identifier, so that
948 sym_identifier will point to 's' when popped */
949 while (*ps != NULL)
950 ps = &(*ps)->prev_tok;
951 s->prev_tok = NULL;
952 *ps = s;
954 return s;
957 /* pop symbols until top reaches 'b' */
958 static void sym_pop(Sym **ptop, Sym *b)
960 Sym *s, *ss, **ps;
961 TokenSym *ts;
962 int v;
964 s = *ptop;
965 while(s != b) {
966 ss = s->prev;
967 v = s->v;
968 /* remove symbol in token array */
969 /* XXX: simplify */
970 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
971 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
972 if (v & SYM_STRUCT)
973 ps = &ts->sym_struct;
974 else
975 ps = &ts->sym_identifier;
976 *ps = s->prev_tok;
978 sym_free(s);
979 s = ss;
981 *ptop = b;
984 /* I/O layer */
986 BufferedFile *tcc_open(TCCState *s1, const char *filename)
988 int fd;
989 BufferedFile *bf;
991 if (strcmp(filename, "-") == 0)
992 fd = 0, filename = "stdin";
993 else
994 fd = open(filename, O_RDONLY | O_BINARY);
995 if ((verbose == 2 && fd >= 0) || verbose == 3)
996 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
997 (s1->include_stack_ptr - s1->include_stack), "", filename);
998 if (fd < 0)
999 return NULL;
1000 bf = tcc_malloc(sizeof(BufferedFile));
1001 bf->fd = fd;
1002 bf->buf_ptr = bf->buffer;
1003 bf->buf_end = bf->buffer;
1004 bf->buffer[0] = CH_EOB; /* put eob symbol */
1005 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1006 #ifdef _WIN32
1007 normalize_slashes(bf->filename);
1008 #endif
1009 bf->line_num = 1;
1010 bf->ifndef_macro = 0;
1011 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1012 // printf("opening '%s'\n", filename);
1013 return bf;
1016 void tcc_close(BufferedFile *bf)
1018 total_lines += bf->line_num;
1019 close(bf->fd);
1020 tcc_free(bf);
1023 #include "tccpp.c"
1024 #include "tccgen.c"
1027 /* compile the C file opened in 'file'. Return non zero if errors. */
1028 static int tcc_compile(TCCState *s1)
1030 Sym *define_start;
1031 char buf[512];
1032 volatile int section_sym;
1034 #ifdef INC_DEBUG
1035 printf("%s: **** new file\n", file->filename);
1036 #endif
1037 preprocess_init(s1);
1039 cur_text_section = NULL;
1040 funcname = "";
1041 anon_sym = SYM_FIRST_ANOM;
1043 /* file info: full path + filename */
1044 section_sym = 0; /* avoid warning */
1045 if (do_debug) {
1046 section_sym = put_elf_sym(symtab_section, 0, 0,
1047 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
1048 text_section->sh_num, NULL);
1049 getcwd(buf, sizeof(buf));
1050 #ifdef _WIN32
1051 normalize_slashes(buf);
1052 #endif
1053 pstrcat(buf, sizeof(buf), "/");
1054 put_stabs_r(buf, N_SO, 0, 0,
1055 text_section->data_offset, text_section, section_sym);
1056 put_stabs_r(file->filename, N_SO, 0, 0,
1057 text_section->data_offset, text_section, section_sym);
1059 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
1060 symbols can be safely used */
1061 put_elf_sym(symtab_section, 0, 0,
1062 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
1063 SHN_ABS, file->filename);
1065 /* define some often used types */
1066 int_type.t = VT_INT;
1068 char_pointer_type.t = VT_BYTE;
1069 mk_pointer(&char_pointer_type);
1071 func_old_type.t = VT_FUNC;
1072 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
1074 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
1075 float_type.t = VT_FLOAT;
1076 double_type.t = VT_DOUBLE;
1078 func_float_type.t = VT_FUNC;
1079 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
1080 func_double_type.t = VT_FUNC;
1081 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
1082 #endif
1084 #if 0
1085 /* define 'void *alloca(unsigned int)' builtin function */
1087 Sym *s1;
1089 p = anon_sym++;
1090 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
1091 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
1092 s1->next = NULL;
1093 sym->next = s1;
1094 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
1096 #endif
1098 define_start = define_stack;
1099 nocode_wanted = 1;
1101 if (setjmp(s1->error_jmp_buf) == 0) {
1102 s1->nb_errors = 0;
1103 s1->error_set_jmp_enabled = 1;
1105 ch = file->buf_ptr[0];
1106 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
1107 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
1108 next();
1109 decl(VT_CONST);
1110 if (tok != TOK_EOF)
1111 expect("declaration");
1113 /* end of translation unit info */
1114 if (do_debug) {
1115 put_stabs_r(NULL, N_SO, 0, 0,
1116 text_section->data_offset, text_section, section_sym);
1119 s1->error_set_jmp_enabled = 0;
1121 /* reset define stack, but leave -Dsymbols (may be incorrect if
1122 they are undefined) */
1123 free_defines(define_start);
1125 gen_inline_functions();
1127 sym_pop(&global_stack, NULL);
1128 sym_pop(&local_stack, NULL);
1130 return s1->nb_errors != 0 ? -1 : 0;
1133 #ifdef LIBTCC
1134 int tcc_compile_string(TCCState *s, const char *str)
1136 BufferedFile bf1, *bf = &bf1;
1137 int ret, len;
1138 char *buf;
1140 /* init file structure */
1141 bf->fd = -1;
1142 /* XXX: avoid copying */
1143 len = strlen(str);
1144 buf = tcc_malloc(len + 1);
1145 if (!buf)
1146 return -1;
1147 memcpy(buf, str, len);
1148 buf[len] = CH_EOB;
1149 bf->buf_ptr = buf;
1150 bf->buf_end = buf + len;
1151 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
1152 bf->line_num = 1;
1153 file = bf;
1154 ret = tcc_compile(s);
1155 file = NULL;
1156 tcc_free(buf);
1158 /* currently, no need to close */
1159 return ret;
1161 #endif
1163 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
1164 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
1166 BufferedFile bf1, *bf = &bf1;
1168 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
1169 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
1170 /* default value */
1171 if (!value)
1172 value = "1";
1173 pstrcat(bf->buffer, IO_BUF_SIZE, value);
1175 /* init file structure */
1176 bf->fd = -1;
1177 bf->buf_ptr = bf->buffer;
1178 bf->buf_end = bf->buffer + strlen(bf->buffer);
1179 *bf->buf_end = CH_EOB;
1180 bf->filename[0] = '\0';
1181 bf->line_num = 1;
1182 file = bf;
1184 s1->include_stack_ptr = s1->include_stack;
1186 /* parse with define parser */
1187 ch = file->buf_ptr[0];
1188 next_nomacro();
1189 parse_define();
1190 file = NULL;
1193 /* undefine a preprocessor symbol */
1194 void tcc_undefine_symbol(TCCState *s1, const char *sym)
1196 TokenSym *ts;
1197 Sym *s;
1198 ts = tok_alloc(sym, strlen(sym));
1199 s = define_find(ts->tok);
1200 /* undefine symbol by putting an invalid name */
1201 if (s)
1202 define_undef(s);
1205 #ifdef CONFIG_TCC_ASM
1207 #ifdef TCC_TARGET_I386
1208 #include "i386-asm.c"
1209 #endif
1210 #include "tccasm.c"
1212 #else
1213 static void asm_instr(void)
1215 error("inline asm() not supported");
1217 static void asm_global_instr(void)
1219 error("inline asm() not supported");
1221 #endif
1223 #include "tccelf.c"
1225 #ifdef TCC_TARGET_COFF
1226 #include "tcccoff.c"
1227 #endif
1229 #ifdef TCC_TARGET_PE
1230 #include "tccpe.c"
1231 #endif
1233 #ifdef CONFIG_TCC_BACKTRACE
1234 /* print the position in the source file of PC value 'pc' by reading
1235 the stabs debug information */
1236 static void rt_printline(unsigned long wanted_pc)
1238 Stab_Sym *sym, *sym_end;
1239 char func_name[128], last_func_name[128];
1240 unsigned long func_addr, last_pc, pc;
1241 const char *incl_files[INCLUDE_STACK_SIZE];
1242 int incl_index, len, last_line_num, i;
1243 const char *str, *p;
1245 fprintf(stderr, "0x%08lx:", wanted_pc);
1247 func_name[0] = '\0';
1248 func_addr = 0;
1249 incl_index = 0;
1250 last_func_name[0] = '\0';
1251 last_pc = 0xffffffff;
1252 last_line_num = 1;
1253 sym = (Stab_Sym *)stab_section->data + 1;
1254 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
1255 while (sym < sym_end) {
1256 switch(sym->n_type) {
1257 /* function start or end */
1258 case N_FUN:
1259 if (sym->n_strx == 0) {
1260 /* we test if between last line and end of function */
1261 pc = sym->n_value + func_addr;
1262 if (wanted_pc >= last_pc && wanted_pc < pc)
1263 goto found;
1264 func_name[0] = '\0';
1265 func_addr = 0;
1266 } else {
1267 str = stabstr_section->data + sym->n_strx;
1268 p = strchr(str, ':');
1269 if (!p) {
1270 pstrcpy(func_name, sizeof(func_name), str);
1271 } else {
1272 len = p - str;
1273 if (len > sizeof(func_name) - 1)
1274 len = sizeof(func_name) - 1;
1275 memcpy(func_name, str, len);
1276 func_name[len] = '\0';
1278 func_addr = sym->n_value;
1280 break;
1281 /* line number info */
1282 case N_SLINE:
1283 pc = sym->n_value + func_addr;
1284 if (wanted_pc >= last_pc && wanted_pc < pc)
1285 goto found;
1286 last_pc = pc;
1287 last_line_num = sym->n_desc;
1288 /* XXX: slow! */
1289 strcpy(last_func_name, func_name);
1290 break;
1291 /* include files */
1292 case N_BINCL:
1293 str = stabstr_section->data + sym->n_strx;
1294 add_incl:
1295 if (incl_index < INCLUDE_STACK_SIZE) {
1296 incl_files[incl_index++] = str;
1298 break;
1299 case N_EINCL:
1300 if (incl_index > 1)
1301 incl_index--;
1302 break;
1303 case N_SO:
1304 if (sym->n_strx == 0) {
1305 incl_index = 0; /* end of translation unit */
1306 } else {
1307 str = stabstr_section->data + sym->n_strx;
1308 /* do not add path */
1309 len = strlen(str);
1310 if (len > 0 && str[len - 1] != '/')
1311 goto add_incl;
1313 break;
1315 sym++;
1318 /* second pass: we try symtab symbols (no line number info) */
1319 incl_index = 0;
1321 ElfW(Sym) *sym, *sym_end;
1322 int type;
1324 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
1325 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
1326 sym < sym_end;
1327 sym++) {
1328 type = ELFW(ST_TYPE)(sym->st_info);
1329 if (type == STT_FUNC) {
1330 if (wanted_pc >= sym->st_value &&
1331 wanted_pc < sym->st_value + sym->st_size) {
1332 pstrcpy(last_func_name, sizeof(last_func_name),
1333 strtab_section->data + sym->st_name);
1334 goto found;
1339 /* did not find any info: */
1340 fprintf(stderr, " ???\n");
1341 return;
1342 found:
1343 if (last_func_name[0] != '\0') {
1344 fprintf(stderr, " %s()", last_func_name);
1346 if (incl_index > 0) {
1347 fprintf(stderr, " (%s:%d",
1348 incl_files[incl_index - 1], last_line_num);
1349 for(i = incl_index - 2; i >= 0; i--)
1350 fprintf(stderr, ", included from %s", incl_files[i]);
1351 fprintf(stderr, ")");
1353 fprintf(stderr, "\n");
1356 #ifdef __i386__
1357 /* fix for glibc 2.1 */
1358 #ifndef REG_EIP
1359 #define REG_EIP EIP
1360 #define REG_EBP EBP
1361 #endif
1363 /* return the PC at frame level 'level'. Return non zero if not found */
1364 static int rt_get_caller_pc(unsigned long *paddr,
1365 ucontext_t *uc, int level)
1367 unsigned long fp;
1368 int i;
1370 if (level == 0) {
1371 #if defined(__FreeBSD__)
1372 *paddr = uc->uc_mcontext.mc_eip;
1373 #elif defined(__dietlibc__)
1374 *paddr = uc->uc_mcontext.eip;
1375 #else
1376 *paddr = uc->uc_mcontext.gregs[REG_EIP];
1377 #endif
1378 return 0;
1379 } else {
1380 #if defined(__FreeBSD__)
1381 fp = uc->uc_mcontext.mc_ebp;
1382 #elif defined(__dietlibc__)
1383 fp = uc->uc_mcontext.ebp;
1384 #else
1385 fp = uc->uc_mcontext.gregs[REG_EBP];
1386 #endif
1387 for(i=1;i<level;i++) {
1388 /* XXX: check address validity with program info */
1389 if (fp <= 0x1000 || fp >= 0xc0000000)
1390 return -1;
1391 fp = ((unsigned long *)fp)[0];
1393 *paddr = ((unsigned long *)fp)[1];
1394 return 0;
1397 #elif defined(__x86_64__)
1398 /* return the PC at frame level 'level'. Return non zero if not found */
1399 static int rt_get_caller_pc(unsigned long *paddr,
1400 ucontext_t *uc, int level)
1402 unsigned long fp;
1403 int i;
1405 if (level == 0) {
1406 /* XXX: only support linux */
1407 *paddr = uc->uc_mcontext.gregs[REG_RIP];
1408 return 0;
1409 } else {
1410 fp = uc->uc_mcontext.gregs[REG_RBP];
1411 for(i=1;i<level;i++) {
1412 /* XXX: check address validity with program info */
1413 if (fp <= 0x1000)
1414 return -1;
1415 fp = ((unsigned long *)fp)[0];
1417 *paddr = ((unsigned long *)fp)[1];
1418 return 0;
1421 #else
1422 #warning add arch specific rt_get_caller_pc()
1423 static int rt_get_caller_pc(unsigned long *paddr,
1424 ucontext_t *uc, int level)
1426 return -1;
1428 #endif
1430 /* emit a run time error at position 'pc' */
1431 void rt_error(ucontext_t *uc, const char *fmt, ...)
1433 va_list ap;
1434 unsigned long pc;
1435 int i;
1437 va_start(ap, fmt);
1438 fprintf(stderr, "Runtime error: ");
1439 vfprintf(stderr, fmt, ap);
1440 fprintf(stderr, "\n");
1441 for(i=0;i<num_callers;i++) {
1442 if (rt_get_caller_pc(&pc, uc, i) < 0)
1443 break;
1444 if (i == 0)
1445 fprintf(stderr, "at ");
1446 else
1447 fprintf(stderr, "by ");
1448 rt_printline(pc);
1450 exit(255);
1451 va_end(ap);
1454 /* signal handler for fatal errors */
1455 static void sig_error(int signum, siginfo_t *siginf, void *puc)
1457 ucontext_t *uc = puc;
1459 switch(signum) {
1460 case SIGFPE:
1461 switch(siginf->si_code) {
1462 case FPE_INTDIV:
1463 case FPE_FLTDIV:
1464 rt_error(uc, "division by zero");
1465 break;
1466 default:
1467 rt_error(uc, "floating point exception");
1468 break;
1470 break;
1471 case SIGBUS:
1472 case SIGSEGV:
1473 if (rt_bound_error_msg && *rt_bound_error_msg)
1474 rt_error(uc, *rt_bound_error_msg);
1475 else
1476 rt_error(uc, "dereferencing invalid pointer");
1477 break;
1478 case SIGILL:
1479 rt_error(uc, "illegal instruction");
1480 break;
1481 case SIGABRT:
1482 rt_error(uc, "abort() called");
1483 break;
1484 default:
1485 rt_error(uc, "caught signal %d", signum);
1486 break;
1488 exit(255);
1491 #endif
1493 /* copy code into memory passed in by the caller and do all relocations
1494 (needed before using tcc_get_symbol()).
1495 returns -1 on error and required size if ptr is NULL */
1496 int tcc_relocate(TCCState *s1, void *ptr)
1498 Section *s;
1499 unsigned long offset, length, mem;
1500 int i;
1502 if (0 == s1->runtime_added) {
1503 s1->runtime_added = 1;
1504 s1->nb_errors = 0;
1505 #ifdef TCC_TARGET_PE
1506 pe_add_runtime(s1);
1507 relocate_common_syms();
1508 tcc_add_linker_symbols(s1);
1509 #else
1510 tcc_add_runtime(s1);
1511 relocate_common_syms();
1512 tcc_add_linker_symbols(s1);
1513 build_got_entries(s1);
1514 #endif
1517 offset = 0, mem = (unsigned long)ptr;
1518 for(i = 1; i < s1->nb_sections; i++) {
1519 s = s1->sections[i];
1520 if (0 == (s->sh_flags & SHF_ALLOC))
1521 continue;
1522 length = s->data_offset;
1523 s->sh_addr = mem ? (mem + offset + 15) & ~15 : 0;
1524 offset = (offset + length + 15) & ~15;
1527 /* relocate symbols */
1528 relocate_syms(s1, 1);
1529 if (s1->nb_errors)
1530 return -1;
1532 #ifdef TCC_TARGET_X86_64
1533 s1->runtime_plt_and_got_offset = 0;
1534 s1->runtime_plt_and_got = (char *)(mem + offset);
1535 /* double the size of the buffer for got and plt entries
1536 XXX: calculate exact size for them? */
1537 offset *= 2;
1538 #endif
1540 if (0 == mem)
1541 return offset + 15;
1543 /* relocate each section */
1544 for(i = 1; i < s1->nb_sections; i++) {
1545 s = s1->sections[i];
1546 if (s->reloc)
1547 relocate_section(s1, s);
1550 for(i = 1; i < s1->nb_sections; i++) {
1551 s = s1->sections[i];
1552 if (0 == (s->sh_flags & SHF_ALLOC))
1553 continue;
1554 length = s->data_offset;
1555 // printf("%-12s %08x %04x\n", s->name, s->sh_addr, length);
1556 ptr = (void*)s->sh_addr;
1557 if (NULL == s->data || s->sh_type == SHT_NOBITS)
1558 memset(ptr, 0, length);
1559 else
1560 memcpy(ptr, s->data, length);
1561 /* mark executable sections as executable in memory */
1562 if (s->sh_flags & SHF_EXECINSTR)
1563 set_pages_executable(ptr, length);
1565 #ifdef TCC_TARGET_X86_64
1566 set_pages_executable(s1->runtime_plt_and_got,
1567 s1->runtime_plt_and_got_offset);
1568 #endif
1569 return 0;
1572 /* launch the compiled program with the given arguments */
1573 int tcc_run(TCCState *s1, int argc, char **argv)
1575 int (*prog_main)(int, char **);
1576 void *ptr;
1577 int ret;
1579 ret = tcc_relocate(s1, NULL);
1580 if (ret < 0)
1581 return -1;
1582 ptr = tcc_malloc(ret);
1583 tcc_relocate(s1, ptr);
1585 prog_main = tcc_get_symbol_err(s1, "main");
1587 if (do_debug) {
1588 #ifdef CONFIG_TCC_BACKTRACE
1589 struct sigaction sigact;
1590 /* install TCC signal handlers to print debug info on fatal
1591 runtime errors */
1592 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
1593 sigact.sa_sigaction = sig_error;
1594 sigemptyset(&sigact.sa_mask);
1595 sigaction(SIGFPE, &sigact, NULL);
1596 sigaction(SIGILL, &sigact, NULL);
1597 sigaction(SIGSEGV, &sigact, NULL);
1598 sigaction(SIGBUS, &sigact, NULL);
1599 sigaction(SIGABRT, &sigact, NULL);
1600 #else
1601 error("debug mode not available");
1602 #endif
1605 #ifdef CONFIG_TCC_BCHECK
1606 if (do_bounds_check) {
1607 void (*bound_init)(void);
1609 /* set error function */
1610 rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
1612 /* XXX: use .init section so that it also work in binary ? */
1613 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
1614 bound_init();
1616 #endif
1617 ret = (*prog_main)(argc, argv);
1618 tcc_free(ptr);
1619 return ret;
1622 void tcc_memstats(void)
1624 #ifdef MEM_DEBUG
1625 printf("memory in use: %d\n", mem_cur_size);
1626 #endif
1629 static void tcc_cleanup(void)
1631 int i, n;
1633 if (NULL == tcc_state)
1634 return;
1635 tcc_state = NULL;
1637 /* free -D defines */
1638 free_defines(NULL);
1640 /* free tokens */
1641 n = tok_ident - TOK_IDENT;
1642 for(i = 0; i < n; i++)
1643 tcc_free(table_ident[i]);
1644 tcc_free(table_ident);
1646 /* free sym_pools */
1647 dynarray_reset(&sym_pools, &nb_sym_pools);
1648 /* string buffer */
1649 cstr_free(&tokcstr);
1650 /* reset symbol stack */
1651 sym_free_first = NULL;
1652 /* cleanup from error/setjmp */
1653 macro_ptr = NULL;
1656 TCCState *tcc_new(void)
1658 TCCState *s;
1660 tcc_cleanup();
1662 s = tcc_mallocz(sizeof(TCCState));
1663 if (!s)
1664 return NULL;
1665 tcc_state = s;
1666 s->output_type = TCC_OUTPUT_MEMORY;
1668 preprocess_new();
1670 /* we add dummy defines for some special macros to speed up tests
1671 and to have working defined() */
1672 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
1673 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
1674 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
1675 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
1677 /* standard defines */
1678 tcc_define_symbol(s, "__STDC__", NULL);
1679 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
1680 #if defined(TCC_TARGET_I386)
1681 tcc_define_symbol(s, "__i386__", NULL);
1682 #endif
1683 #if defined(TCC_TARGET_X86_64)
1684 tcc_define_symbol(s, "__x86_64__", NULL);
1685 #endif
1686 #if defined(TCC_TARGET_ARM)
1687 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
1688 tcc_define_symbol(s, "__arm_elf__", NULL);
1689 tcc_define_symbol(s, "__arm_elf", NULL);
1690 tcc_define_symbol(s, "arm_elf", NULL);
1691 tcc_define_symbol(s, "__arm__", NULL);
1692 tcc_define_symbol(s, "__arm", NULL);
1693 tcc_define_symbol(s, "arm", NULL);
1694 tcc_define_symbol(s, "__APCS_32__", NULL);
1695 #endif
1696 #ifdef TCC_TARGET_PE
1697 tcc_define_symbol(s, "_WIN32", NULL);
1698 #else
1699 tcc_define_symbol(s, "__unix__", NULL);
1700 tcc_define_symbol(s, "__unix", NULL);
1701 #if defined(__linux)
1702 tcc_define_symbol(s, "__linux__", NULL);
1703 tcc_define_symbol(s, "__linux", NULL);
1704 #endif
1705 #endif
1706 /* tiny C specific defines */
1707 tcc_define_symbol(s, "__TINYC__", NULL);
1709 /* tiny C & gcc defines */
1710 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
1711 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
1712 #ifdef TCC_TARGET_PE
1713 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
1714 #else
1715 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
1716 #endif
1718 #ifndef TCC_TARGET_PE
1719 /* default library paths */
1720 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/local/lib");
1721 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/lib");
1722 tcc_add_library_path(s, CONFIG_SYSROOT "/lib");
1723 #endif
1725 /* no section zero */
1726 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
1728 /* create standard sections */
1729 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
1730 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
1731 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
1733 /* symbols are always generated for linking stage */
1734 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
1735 ".strtab",
1736 ".hashtab", SHF_PRIVATE);
1737 strtab_section = symtab_section->link;
1739 /* private symbol table for dynamic symbols */
1740 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
1741 ".dynstrtab",
1742 ".dynhashtab", SHF_PRIVATE);
1743 s->alacarte_link = 1;
1745 #ifdef CHAR_IS_UNSIGNED
1746 s->char_is_unsigned = 1;
1747 #endif
1748 #if defined(TCC_TARGET_PE) && 0
1749 /* XXX: currently the PE linker is not ready to support that */
1750 s->leading_underscore = 1;
1751 #endif
1752 return s;
1755 void tcc_delete(TCCState *s1)
1757 int i;
1759 tcc_cleanup();
1761 /* free all sections */
1762 for(i = 1; i < s1->nb_sections; i++)
1763 free_section(s1->sections[i]);
1764 dynarray_reset(&s1->sections, &s1->nb_sections);
1766 for(i = 0; i < s1->nb_priv_sections; i++)
1767 free_section(s1->priv_sections[i]);
1768 dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
1770 /* free any loaded DLLs */
1771 for ( i = 0; i < s1->nb_loaded_dlls; i++) {
1772 DLLReference *ref = s1->loaded_dlls[i];
1773 if ( ref->handle )
1774 dlclose(ref->handle);
1777 /* free loaded dlls array */
1778 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
1780 /* free library paths */
1781 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
1783 /* free include paths */
1784 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
1785 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
1786 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
1788 tcc_free(s1);
1791 int tcc_add_include_path(TCCState *s1, const char *pathname)
1793 char *pathname1;
1795 pathname1 = tcc_strdup(pathname);
1796 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
1797 return 0;
1800 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
1802 char *pathname1;
1804 pathname1 = tcc_strdup(pathname);
1805 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
1806 return 0;
1809 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1811 const char *ext;
1812 ElfW(Ehdr) ehdr;
1813 int fd, ret;
1814 BufferedFile *saved_file;
1816 /* find source file type with extension */
1817 ext = tcc_fileextension(filename);
1818 if (ext[0])
1819 ext++;
1821 /* open the file */
1822 saved_file = file;
1823 file = tcc_open(s1, filename);
1824 if (!file) {
1825 if (flags & AFF_PRINT_ERROR) {
1826 error_noabort("file '%s' not found", filename);
1828 ret = -1;
1829 goto fail1;
1832 if (flags & AFF_PREPROCESS) {
1833 ret = tcc_preprocess(s1);
1834 } else if (!ext[0] || !PATHCMP(ext, "c")) {
1835 /* C file assumed */
1836 ret = tcc_compile(s1);
1837 } else
1838 #ifdef CONFIG_TCC_ASM
1839 if (!strcmp(ext, "S")) {
1840 /* preprocessed assembler */
1841 ret = tcc_assemble(s1, 1);
1842 } else if (!strcmp(ext, "s")) {
1843 /* non preprocessed assembler */
1844 ret = tcc_assemble(s1, 0);
1845 } else
1846 #endif
1847 #ifdef TCC_TARGET_PE
1848 if (!PATHCMP(ext, "def")) {
1849 ret = pe_load_def_file(s1, file->fd);
1850 } else
1851 #endif
1853 fd = file->fd;
1854 /* assume executable format: auto guess file type */
1855 ret = read(fd, &ehdr, sizeof(ehdr));
1856 lseek(fd, 0, SEEK_SET);
1857 if (ret <= 0) {
1858 error_noabort("could not read header");
1859 goto fail;
1860 } else if (ret != sizeof(ehdr)) {
1861 goto try_load_script;
1864 if (ehdr.e_ident[0] == ELFMAG0 &&
1865 ehdr.e_ident[1] == ELFMAG1 &&
1866 ehdr.e_ident[2] == ELFMAG2 &&
1867 ehdr.e_ident[3] == ELFMAG3) {
1868 file->line_num = 0; /* do not display line number if error */
1869 if (ehdr.e_type == ET_REL) {
1870 ret = tcc_load_object_file(s1, fd, 0);
1871 } else if (ehdr.e_type == ET_DYN) {
1872 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1873 #ifdef TCC_TARGET_PE
1874 ret = -1;
1875 #else
1876 void *h;
1877 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
1878 if (h)
1879 ret = 0;
1880 else
1881 ret = -1;
1882 #endif
1883 } else {
1884 ret = tcc_load_dll(s1, fd, filename,
1885 (flags & AFF_REFERENCED_DLL) != 0);
1887 } else {
1888 error_noabort("unrecognized ELF file");
1889 goto fail;
1891 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
1892 file->line_num = 0; /* do not display line number if error */
1893 ret = tcc_load_archive(s1, fd);
1894 } else
1895 #ifdef TCC_TARGET_COFF
1896 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
1897 ret = tcc_load_coff(s1, fd);
1898 } else
1899 #endif
1900 #ifdef TCC_TARGET_PE
1901 if (pe_test_res_file(&ehdr, ret)) {
1902 ret = pe_load_res_file(s1, fd);
1903 } else
1904 #endif
1906 /* as GNU ld, consider it is an ld script if not recognized */
1907 try_load_script:
1908 ret = tcc_load_ldscript(s1);
1909 if (ret < 0) {
1910 error_noabort("unrecognized file type");
1911 goto fail;
1915 the_end:
1916 tcc_close(file);
1917 fail1:
1918 file = saved_file;
1919 return ret;
1920 fail:
1921 ret = -1;
1922 goto the_end;
1925 int tcc_add_file(TCCState *s, const char *filename)
1927 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
1930 int tcc_add_library_path(TCCState *s, const char *pathname)
1932 char *pathname1;
1934 pathname1 = tcc_strdup(pathname);
1935 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
1936 return 0;
1939 /* find and load a dll. Return non zero if not found */
1940 /* XXX: add '-rpath' option support ? */
1941 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
1943 char buf[1024];
1944 int i;
1946 for(i = 0; i < s->nb_library_paths; i++) {
1947 snprintf(buf, sizeof(buf), "%s/%s",
1948 s->library_paths[i], filename);
1949 if (tcc_add_file_internal(s, buf, flags) == 0)
1950 return 0;
1952 return -1;
1955 /* the library name is the same as the argument of the '-l' option */
1956 int tcc_add_library(TCCState *s, const char *libraryname)
1958 char buf[1024];
1959 int i;
1961 /* first we look for the dynamic library if not static linking */
1962 if (!s->static_link) {
1963 #ifdef TCC_TARGET_PE
1964 snprintf(buf, sizeof(buf), "%s.def", libraryname);
1965 #else
1966 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
1967 #endif
1968 if (tcc_add_dll(s, buf, 0) == 0)
1969 return 0;
1972 /* then we look for the static library */
1973 for(i = 0; i < s->nb_library_paths; i++) {
1974 snprintf(buf, sizeof(buf), "%s/lib%s.a",
1975 s->library_paths[i], libraryname);
1976 if (tcc_add_file_internal(s, buf, 0) == 0)
1977 return 0;
1979 return -1;
1982 int tcc_add_symbol(TCCState *s, const char *name, void *val)
1984 add_elf_sym(symtab_section, (unsigned long)val, 0,
1985 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1986 SHN_ABS, name);
1987 return 0;
1990 int tcc_set_output_type(TCCState *s, int output_type)
1992 char buf[1024];
1994 s->output_type = output_type;
1996 if (!s->nostdinc) {
1997 /* default include paths */
1998 /* XXX: reverse order needed if -isystem support */
1999 #ifndef TCC_TARGET_PE
2000 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/local/include");
2001 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/include");
2002 #endif
2003 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
2004 tcc_add_sysinclude_path(s, buf);
2005 #ifdef TCC_TARGET_PE
2006 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
2007 tcc_add_sysinclude_path(s, buf);
2008 #endif
2011 /* if bound checking, then add corresponding sections */
2012 #ifdef CONFIG_TCC_BCHECK
2013 if (do_bounds_check) {
2014 /* define symbol */
2015 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
2016 /* create bounds sections */
2017 bounds_section = new_section(s, ".bounds",
2018 SHT_PROGBITS, SHF_ALLOC);
2019 lbounds_section = new_section(s, ".lbounds",
2020 SHT_PROGBITS, SHF_ALLOC);
2022 #endif
2024 if (s->char_is_unsigned) {
2025 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
2028 /* add debug sections */
2029 if (do_debug) {
2030 /* stab symbols */
2031 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
2032 stab_section->sh_entsize = sizeof(Stab_Sym);
2033 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
2034 put_elf_str(stabstr_section, "");
2035 stab_section->link = stabstr_section;
2036 /* put first entry */
2037 put_stabs("", 0, 0, 0, 0);
2040 /* add libc crt1/crti objects */
2041 #ifndef TCC_TARGET_PE
2042 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
2043 !s->nostdlib) {
2044 if (output_type != TCC_OUTPUT_DLL)
2045 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
2046 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
2048 #endif
2050 #ifdef TCC_TARGET_PE
2051 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
2052 tcc_add_library_path(s, buf);
2053 #endif
2055 return 0;
2058 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
2059 #define FD_INVERT 0x0002 /* invert value before storing */
2061 typedef struct FlagDef {
2062 uint16_t offset;
2063 uint16_t flags;
2064 const char *name;
2065 } FlagDef;
2067 static const FlagDef warning_defs[] = {
2068 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
2069 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
2070 { offsetof(TCCState, warn_error), 0, "error" },
2071 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
2072 "implicit-function-declaration" },
2075 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
2076 const char *name, int value)
2078 int i;
2079 const FlagDef *p;
2080 const char *r;
2082 r = name;
2083 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
2084 r += 3;
2085 value = !value;
2087 for(i = 0, p = flags; i < nb_flags; i++, p++) {
2088 if (!strcmp(r, p->name))
2089 goto found;
2091 return -1;
2092 found:
2093 if (p->flags & FD_INVERT)
2094 value = !value;
2095 *(int *)((uint8_t *)s + p->offset) = value;
2096 return 0;
2100 /* set/reset a warning */
2101 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
2103 int i;
2104 const FlagDef *p;
2106 if (!strcmp(warning_name, "all")) {
2107 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
2108 if (p->flags & WD_ALL)
2109 *(int *)((uint8_t *)s + p->offset) = 1;
2111 return 0;
2112 } else {
2113 return set_flag(s, warning_defs, countof(warning_defs),
2114 warning_name, value);
2118 static const FlagDef flag_defs[] = {
2119 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
2120 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
2121 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
2122 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
2125 /* set/reset a flag */
2126 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
2128 return set_flag(s, flag_defs, countof(flag_defs),
2129 flag_name, value);
2132 /* set CONFIG_TCCDIR at runtime */
2133 void tcc_set_lib_path(TCCState *s, const char *path)
2135 tcc_lib_path = tcc_strdup(path);