implement #pragma comment(lib,...)
[tinycc.git] / libtcc.c
blob0d8496b6e4aa2834c86c137e96094d6265491f8a
1 /*
2 * TCC - Tiny C Compiler
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 /********************************************************/
24 /* global variables */
26 /* use GNU C extensions */
27 ST_DATA int gnu_ext = 1;
29 /* use TinyCC extensions */
30 ST_DATA int tcc_ext = 1;
32 /* XXX: get rid of this ASAP */
33 ST_DATA struct TCCState *tcc_state;
35 /********************************************************/
37 #ifdef ONE_SOURCE
38 #include "tccpp.c"
39 #include "tccgen.c"
40 #include "tccelf.c"
41 #include "tccrun.c"
42 #ifdef TCC_TARGET_I386
43 #include "i386-gen.c"
44 #endif
45 #ifdef TCC_TARGET_ARM
46 #include "arm-gen.c"
47 #endif
48 #ifdef TCC_TARGET_ARM64
49 #include "arm64-gen.c"
50 #endif
51 #ifdef TCC_TARGET_C67
52 #include "c67-gen.c"
53 #endif
54 #ifdef TCC_TARGET_X86_64
55 #include "x86_64-gen.c"
56 #endif
57 #ifdef CONFIG_TCC_ASM
58 #include "tccasm.c"
59 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
60 #include "i386-asm.c"
61 #endif
62 #endif
63 #ifdef TCC_TARGET_COFF
64 #include "tcccoff.c"
65 #endif
66 #ifdef TCC_TARGET_PE
67 #include "tccpe.c"
68 #endif
69 #endif /* ONE_SOURCE */
71 /********************************************************/
72 #ifndef CONFIG_TCC_ASM
73 ST_FUNC void asm_instr(void)
75 tcc_error("inline asm() not supported");
77 ST_FUNC void asm_global_instr(void)
79 tcc_error("inline asm() not supported");
81 #endif
83 /********************************************************/
84 #ifdef _WIN32
85 static char *normalize_slashes(char *path)
87 char *p;
88 for (p = path; *p; ++p)
89 if (*p == '\\')
90 *p = '/';
91 return path;
94 static HMODULE tcc_module;
96 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
97 static void tcc_set_lib_path_w32(TCCState *s)
99 char path[1024], *p;
100 GetModuleFileNameA(tcc_module, path, sizeof path);
101 p = tcc_basename(normalize_slashes(strlwr(path)));
102 if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
103 p -= 5;
104 else if (p > path)
105 p--;
106 *p = 0;
107 tcc_set_lib_path(s, path);
110 #ifdef TCC_TARGET_PE
111 static void tcc_add_systemdir(TCCState *s)
113 char buf[1000];
114 GetSystemDirectory(buf, sizeof buf);
115 tcc_add_library_path(s, normalize_slashes(buf));
117 #endif
119 #ifndef CONFIG_TCC_STATIC
120 void dlclose(void *p)
122 FreeLibrary((HMODULE)p);
124 #endif
126 #ifdef LIBTCC_AS_DLL
127 BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
129 if (DLL_PROCESS_ATTACH == dwReason)
130 tcc_module = hDll;
131 return TRUE;
133 #endif
134 #endif
136 /********************************************************/
137 /* copy a string and truncate it. */
138 PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
140 char *q, *q_end;
141 int c;
143 if (buf_size > 0) {
144 q = buf;
145 q_end = buf + buf_size - 1;
146 while (q < q_end) {
147 c = *s++;
148 if (c == '\0')
149 break;
150 *q++ = c;
152 *q = '\0';
154 return buf;
157 /* strcat and truncate. */
158 PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
160 int len;
161 len = strlen(buf);
162 if (len < buf_size)
163 pstrcpy(buf + len, buf_size - len, s);
164 return buf;
167 PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num)
169 memcpy(out, in, num);
170 out[num] = '\0';
171 return out;
174 /* extract the basename of a file */
175 PUB_FUNC char *tcc_basename(const char *name)
177 char *p = strchr(name, 0);
178 while (p > name && !IS_DIRSEP(p[-1]))
179 --p;
180 return p;
183 /* extract extension part of a file
185 * (if no extension, return pointer to end-of-string)
187 PUB_FUNC char *tcc_fileextension (const char *name)
189 char *b = tcc_basename(name);
190 char *e = strrchr(b, '.');
191 return e ? e : strchr(b, 0);
194 /********************************************************/
195 /* memory management */
197 #undef free
198 #undef malloc
199 #undef realloc
201 #ifdef MEM_DEBUG
202 ST_DATA int mem_cur_size;
203 ST_DATA int mem_max_size;
204 unsigned malloc_usable_size(void*);
205 #endif
207 PUB_FUNC void tcc_free(void *ptr)
209 #ifdef MEM_DEBUG
210 mem_cur_size -= malloc_usable_size(ptr);
211 #endif
212 free(ptr);
215 PUB_FUNC void *tcc_malloc(unsigned long size)
217 void *ptr;
218 ptr = malloc(size);
219 if (!ptr && size)
220 tcc_error("memory full (malloc)");
221 #ifdef MEM_DEBUG
222 mem_cur_size += malloc_usable_size(ptr);
223 if (mem_cur_size > mem_max_size)
224 mem_max_size = mem_cur_size;
225 #endif
226 return ptr;
229 PUB_FUNC void *tcc_mallocz(unsigned long size)
231 void *ptr;
232 ptr = tcc_malloc(size);
233 memset(ptr, 0, size);
234 return ptr;
237 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
239 void *ptr1;
240 #ifdef MEM_DEBUG
241 mem_cur_size -= malloc_usable_size(ptr);
242 #endif
243 ptr1 = realloc(ptr, size);
244 if (!ptr1 && size)
245 tcc_error("memory full (realloc)");
246 #ifdef MEM_DEBUG
247 /* NOTE: count not correct if alloc error, but not critical */
248 mem_cur_size += malloc_usable_size(ptr1);
249 if (mem_cur_size > mem_max_size)
250 mem_max_size = mem_cur_size;
251 #endif
252 return ptr1;
255 PUB_FUNC char *tcc_strdup(const char *str)
257 char *ptr;
258 ptr = tcc_malloc(strlen(str) + 1);
259 strcpy(ptr, str);
260 return ptr;
263 PUB_FUNC void tcc_memstats(void)
265 #ifdef MEM_DEBUG
266 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
267 #endif
270 #define free(p) use_tcc_free(p)
271 #define malloc(s) use_tcc_malloc(s)
272 #define realloc(p, s) use_tcc_realloc(p, s)
274 /********************************************************/
275 /* dynarrays */
277 ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data)
279 int nb, nb_alloc;
280 void **pp;
282 nb = *nb_ptr;
283 pp = *ptab;
284 /* every power of two we double array size */
285 if ((nb & (nb - 1)) == 0) {
286 if (!nb)
287 nb_alloc = 1;
288 else
289 nb_alloc = nb * 2;
290 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
291 *ptab = pp;
293 pp[nb++] = data;
294 *nb_ptr = nb;
297 ST_FUNC void dynarray_reset(void *pp, int *n)
299 void **p;
300 for (p = *(void***)pp; *n; ++p, --*n)
301 if (*p)
302 tcc_free(*p);
303 tcc_free(*(void**)pp);
304 *(void**)pp = NULL;
307 static void tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char *in)
309 const char *p;
310 do {
311 int c;
312 CString str;
314 cstr_new(&str);
315 for (p = in; c = *p, c != '\0' && c != PATHSEP; ++p) {
316 if (c == '{' && p[1] && p[2] == '}') {
317 c = p[1], p += 2;
318 if (c == 'B')
319 cstr_cat(&str, s->tcc_lib_path);
320 } else {
321 cstr_ccat(&str, c);
324 cstr_ccat(&str, '\0');
325 dynarray_add(p_ary, p_nb_ary, str.data);
326 in = p+1;
327 } while (*p);
330 /********************************************************/
332 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
334 Section *sec;
336 sec = tcc_mallocz(sizeof(Section) + strlen(name));
337 strcpy(sec->name, name);
338 sec->sh_type = sh_type;
339 sec->sh_flags = sh_flags;
340 switch(sh_type) {
341 case SHT_HASH:
342 case SHT_REL:
343 case SHT_RELA:
344 case SHT_DYNSYM:
345 case SHT_SYMTAB:
346 case SHT_DYNAMIC:
347 sec->sh_addralign = 4;
348 break;
349 case SHT_STRTAB:
350 sec->sh_addralign = 1;
351 break;
352 default:
353 sec->sh_addralign = 32; /* default conservative alignment */
354 break;
357 if (sh_flags & SHF_PRIVATE) {
358 dynarray_add((void ***)&s1->priv_sections, &s1->nb_priv_sections, sec);
359 } else {
360 sec->sh_num = s1->nb_sections;
361 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
364 return sec;
367 static void free_section(Section *s)
369 tcc_free(s->data);
372 /* realloc section and set its content to zero */
373 ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
375 unsigned long size;
376 unsigned char *data;
378 size = sec->data_allocated;
379 if (size == 0)
380 size = 1;
381 while (size < new_size)
382 size = size * 2;
383 data = tcc_realloc(sec->data, size);
384 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
385 sec->data = data;
386 sec->data_allocated = size;
389 /* reserve at least 'size' bytes in section 'sec' from
390 sec->data_offset. */
391 ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
393 size_t offset, offset1;
395 offset = sec->data_offset;
396 offset1 = offset + size;
397 if (offset1 > sec->data_allocated)
398 section_realloc(sec, offset1);
399 sec->data_offset = offset1;
400 return sec->data + offset;
403 /* reserve at least 'size' bytes from section start */
404 ST_FUNC void section_reserve(Section *sec, unsigned long size)
406 if (size > sec->data_allocated)
407 section_realloc(sec, size);
408 if (size > sec->data_offset)
409 sec->data_offset = size;
412 /* return a reference to a section, and create it if it does not
413 exists */
414 ST_FUNC Section *find_section(TCCState *s1, const char *name)
416 Section *sec;
417 int i;
418 for(i = 1; i < s1->nb_sections; i++) {
419 sec = s1->sections[i];
420 if (!strcmp(name, sec->name))
421 return sec;
423 /* sections are created as PROGBITS */
424 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
427 /* update sym->c so that it points to an external symbol in section
428 'section' with value 'value' */
429 ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
430 addr_t value, unsigned long size,
431 int can_add_underscore)
433 int sym_type, sym_bind, sh_num, info, other;
434 ElfW(Sym) *esym;
435 const char *name;
436 char buf1[256];
438 #ifdef CONFIG_TCC_BCHECK
439 char buf[32];
440 #endif
442 if (section == NULL)
443 sh_num = SHN_UNDEF;
444 else if (section == SECTION_ABS)
445 sh_num = SHN_ABS;
446 else
447 sh_num = section->sh_num;
449 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
450 sym_type = STT_FUNC;
451 } else if ((sym->type.t & VT_BTYPE) == VT_VOID) {
452 sym_type = STT_NOTYPE;
453 } else {
454 sym_type = STT_OBJECT;
457 if (sym->type.t & VT_STATIC)
458 sym_bind = STB_LOCAL;
459 else {
460 if (sym->type.t & VT_WEAK)
461 sym_bind = STB_WEAK;
462 else
463 sym_bind = STB_GLOBAL;
466 if (!sym->c) {
467 name = get_tok_str(sym->v, NULL);
468 #ifdef CONFIG_TCC_BCHECK
469 if (tcc_state->do_bounds_check) {
470 /* XXX: avoid doing that for statics ? */
471 /* if bound checking is activated, we change some function
472 names by adding the "__bound" prefix */
473 switch(sym->v) {
474 #ifdef TCC_TARGET_PE
475 /* XXX: we rely only on malloc hooks */
476 case TOK_malloc:
477 case TOK_free:
478 case TOK_realloc:
479 case TOK_memalign:
480 case TOK_calloc:
481 #endif
482 case TOK_memcpy:
483 case TOK_memmove:
484 case TOK_memset:
485 case TOK_strlen:
486 case TOK_strcpy:
487 case TOK_alloca:
488 strcpy(buf, "__bound_");
489 strcat(buf, name);
490 name = buf;
491 break;
494 #endif
495 other = 0;
497 #ifdef TCC_TARGET_PE
498 if (sym->type.t & VT_EXPORT)
499 other |= ST_PE_EXPORT;
500 if (sym_type == STT_FUNC && sym->type.ref) {
501 Sym *ref = sym->type.ref;
502 if (ref->a.func_export)
503 other |= ST_PE_EXPORT;
504 if (ref->a.func_call == FUNC_STDCALL && can_add_underscore) {
505 sprintf(buf1, "_%s@%d", name, ref->a.func_args * PTR_SIZE);
506 name = buf1;
507 other |= ST_PE_STDCALL;
508 can_add_underscore = 0;
510 } else {
511 if (find_elf_sym(tcc_state->dynsymtab_section, name))
512 other |= ST_PE_IMPORT;
513 if (sym->type.t & VT_IMPORT)
514 other |= ST_PE_IMPORT;
516 #else
517 if (! (sym->type.t & VT_STATIC))
518 other = (sym->type.t & VT_VIS_MASK) >> VT_VIS_SHIFT;
519 #endif
520 if (tcc_state->leading_underscore && can_add_underscore) {
521 buf1[0] = '_';
522 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
523 name = buf1;
525 if (sym->asm_label) {
526 name = sym->asm_label;
528 info = ELFW(ST_INFO)(sym_bind, sym_type);
529 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
530 } else {
531 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
532 esym->st_value = value;
533 esym->st_size = size;
534 esym->st_shndx = sh_num;
538 ST_FUNC void put_extern_sym(Sym *sym, Section *section,
539 addr_t value, unsigned long size)
541 put_extern_sym2(sym, section, value, size, 1);
544 /* add a new relocation entry to symbol 'sym' in section 's' */
545 ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type,
546 addr_t addend)
548 int c = 0;
549 if (sym) {
550 if (0 == sym->c)
551 put_extern_sym(sym, NULL, 0, 0);
552 c = sym->c;
554 /* now we can add ELF relocation info */
555 put_elf_reloca(symtab_section, s, offset, type, c, addend);
558 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
560 greloca(s, sym, offset, type, 0);
563 /********************************************************/
565 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
567 int len;
568 len = strlen(buf);
569 vsnprintf(buf + len, buf_size - len, fmt, ap);
572 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
574 va_list ap;
575 va_start(ap, fmt);
576 strcat_vprintf(buf, buf_size, fmt, ap);
577 va_end(ap);
580 static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
582 char buf[2048];
583 BufferedFile **pf, *f;
585 buf[0] = '\0';
586 /* use upper file if inline ":asm:" or token ":paste:" */
587 for (f = file; f && f->filename[0] == ':'; f = f->prev)
589 if (f) {
590 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
591 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
592 (*pf)->filename, (*pf)->line_num);
593 if (f->line_num > 0) {
594 strcat_printf(buf, sizeof(buf), "%s:%d: ",
595 f->filename, f->line_num);
596 } else {
597 strcat_printf(buf, sizeof(buf), "%s: ",
598 f->filename);
600 } else {
601 strcat_printf(buf, sizeof(buf), "tcc: ");
603 if (is_warning)
604 strcat_printf(buf, sizeof(buf), "warning: ");
605 else
606 strcat_printf(buf, sizeof(buf), "error: ");
607 strcat_vprintf(buf, sizeof(buf), fmt, ap);
609 if (!s1->error_func) {
610 /* default case: stderr */
611 fprintf(stderr, "%s\n", buf);
612 } else {
613 s1->error_func(s1->error_opaque, buf);
615 if (!is_warning || s1->warn_error)
616 s1->nb_errors++;
619 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
620 void (*error_func)(void *opaque, const char *msg))
622 s->error_opaque = error_opaque;
623 s->error_func = error_func;
626 /* error without aborting current compilation */
627 PUB_FUNC void tcc_error_noabort(const char *fmt, ...)
629 TCCState *s1 = tcc_state;
630 va_list ap;
632 va_start(ap, fmt);
633 error1(s1, 0, fmt, ap);
634 va_end(ap);
637 PUB_FUNC void tcc_error(const char *fmt, ...)
639 TCCState *s1 = tcc_state;
640 va_list ap;
642 va_start(ap, fmt);
643 error1(s1, 0, fmt, ap);
644 va_end(ap);
645 /* better than nothing: in some cases, we accept to handle errors */
646 if (s1->error_set_jmp_enabled) {
647 longjmp(s1->error_jmp_buf, 1);
648 } else {
649 /* XXX: eliminate this someday */
650 exit(1);
654 PUB_FUNC void tcc_warning(const char *fmt, ...)
656 TCCState *s1 = tcc_state;
657 va_list ap;
659 if (s1->warn_none)
660 return;
662 va_start(ap, fmt);
663 error1(s1, 1, fmt, ap);
664 va_end(ap);
667 /********************************************************/
668 /* I/O layer */
670 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
672 BufferedFile *bf;
673 int buflen = initlen ? initlen : IO_BUF_SIZE;
675 bf = tcc_malloc(sizeof(BufferedFile) + buflen);
676 bf->buf_ptr = bf->buffer;
677 bf->buf_end = bf->buffer + initlen;
678 bf->buf_end[0] = CH_EOB; /* put eob symbol */
679 pstrcpy(bf->filename, sizeof(bf->filename), filename);
680 #ifdef _WIN32
681 normalize_slashes(bf->filename);
682 #endif
683 bf->line_num = 1;
684 bf->ifndef_macro = 0;
685 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
686 bf->fd = -1;
687 bf->prev = file;
688 file = bf;
691 ST_FUNC void tcc_close(void)
693 BufferedFile *bf = file;
694 if (bf->fd > 0) {
695 close(bf->fd);
696 total_lines += bf->line_num;
698 file = bf->prev;
699 tcc_free(bf);
702 ST_FUNC int tcc_open(TCCState *s1, const char *filename)
704 int fd;
705 if (strcmp(filename, "-") == 0)
706 fd = 0, filename = "stdin";
707 else
708 fd = open(filename, O_RDONLY | O_BINARY);
709 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
710 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
711 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
712 if (fd < 0)
713 return -1;
715 tcc_open_bf(s1, filename, 0);
716 file->fd = fd;
717 return fd;
720 /* compile the C file opened in 'file'. Return non zero if errors. */
721 static int tcc_compile(TCCState *s1)
723 Sym *define_start;
724 SValue *pvtop;
725 char buf[512];
726 volatile int section_sym;
728 #ifdef INC_DEBUG
729 printf("%s: **** new file\n", file->filename);
730 #endif
731 preprocess_init(s1);
733 cur_text_section = NULL;
734 funcname = "";
735 anon_sym = SYM_FIRST_ANOM;
737 /* file info: full path + filename */
738 section_sym = 0; /* avoid warning */
739 if (s1->do_debug) {
740 section_sym = put_elf_sym(symtab_section, 0, 0,
741 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
742 text_section->sh_num, NULL);
743 getcwd(buf, sizeof(buf));
744 #ifdef _WIN32
745 normalize_slashes(buf);
746 #endif
747 pstrcat(buf, sizeof(buf), "/");
748 put_stabs_r(buf, N_SO, 0, 0,
749 text_section->data_offset, text_section, section_sym);
750 put_stabs_r(file->filename, N_SO, 0, 0,
751 text_section->data_offset, text_section, section_sym);
753 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
754 symbols can be safely used */
755 put_elf_sym(symtab_section, 0, 0,
756 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
757 SHN_ABS, file->filename);
759 /* define some often used types */
760 int_type.t = VT_INT;
762 char_pointer_type.t = VT_BYTE;
763 mk_pointer(&char_pointer_type);
765 #if PTR_SIZE == 4
766 size_type.t = VT_INT;
767 #else
768 size_type.t = VT_LLONG;
769 #endif
771 func_old_type.t = VT_FUNC;
772 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
773 #ifdef TCC_TARGET_ARM
774 arm_init(s1);
775 #endif
777 #if 0
778 /* define 'void *alloca(unsigned int)' builtin function */
780 Sym *s1;
782 p = anon_sym++;
783 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
784 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
785 s1->next = NULL;
786 sym->next = s1;
787 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
789 #endif
791 define_start = define_stack;
792 nocode_wanted = 1;
794 if (setjmp(s1->error_jmp_buf) == 0) {
795 s1->nb_errors = 0;
796 s1->error_set_jmp_enabled = 1;
798 ch = file->buf_ptr[0];
799 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
800 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
801 pvtop = vtop;
802 next();
803 decl(VT_CONST);
804 if (tok != TOK_EOF)
805 expect("declaration");
806 if (pvtop != vtop)
807 tcc_warning("internal compiler error: vstack leak? (%d)", vtop - pvtop);
809 /* end of translation unit info */
810 if (s1->do_debug) {
811 put_stabs_r(NULL, N_SO, 0, 0,
812 text_section->data_offset, text_section, section_sym);
816 s1->error_set_jmp_enabled = 0;
818 /* reset define stack, but leave -Dsymbols (may be incorrect if
819 they are undefined) */
820 free_defines(define_start);
822 gen_inline_functions();
824 sym_pop(&global_stack, NULL);
825 sym_pop(&local_stack, NULL);
827 return s1->nb_errors != 0 ? -1 : 0;
830 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
832 int i;
833 int len, ret;
834 len = strlen(str);
836 tcc_open_bf(s, "<string>", len);
837 memcpy(file->buffer, str, len);
839 len = s->nb_files;
840 ret = tcc_compile(s);
841 tcc_close();
843 /* habdle #pragma comment(lib,) */
844 for(i = len; i < s->nb_files; i++) {
845 /* int filetype = *(unsigned char *)s->files[i]; */
846 const char *filename = s->files[i] + 1;
847 if (filename[0] == '-' && filename[1] == 'l') {
848 if (tcc_add_library(s, filename + 2) < 0) {
849 tcc_warning("cannot find '%s'", filename+2);
850 ret++;
853 tcc_free(s->files[i]);
855 s->nb_files = len;
857 return ret;
860 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
861 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
863 int len1, len2;
864 /* default value */
865 if (!value)
866 value = "1";
867 len1 = strlen(sym);
868 len2 = strlen(value);
870 /* init file structure */
871 tcc_open_bf(s1, "<define>", len1 + len2 + 1);
872 memcpy(file->buffer, sym, len1);
873 file->buffer[len1] = ' ';
874 memcpy(file->buffer + len1 + 1, value, len2);
876 /* parse with define parser */
877 ch = file->buf_ptr[0];
878 next_nomacro();
879 parse_define();
881 tcc_close();
884 /* undefine a preprocessor symbol */
885 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
887 TokenSym *ts;
888 Sym *s;
889 ts = tok_alloc(sym, strlen(sym));
890 s = define_find(ts->tok);
891 /* undefine symbol by putting an invalid name */
892 if (s)
893 define_undef(s);
894 tcc_free(ts);
897 /* cleanup all static data used during compilation */
898 static void tcc_cleanup(void)
900 int i, n;
901 if (NULL == tcc_state)
902 return;
903 tcc_state = NULL;
905 /* free -D defines */
906 free_defines(NULL);
908 /* free tokens */
909 n = tok_ident - TOK_IDENT;
910 for(i = 0; i < n; i++)
911 tcc_free(table_ident[i]);
912 tcc_free(table_ident);
913 table_ident = NULL;
915 /* free sym_pools */
916 dynarray_reset(&sym_pools, &nb_sym_pools);
917 /* string buffer */
918 cstr_free(&tokcstr);
919 /* reset symbol stack */
920 sym_free_first = NULL;
921 /* cleanup from error/setjmp */
922 macro_ptr = NULL;
925 LIBTCCAPI TCCState *tcc_new(void)
927 TCCState *s;
928 char buffer[100];
929 int a,b,c;
931 tcc_cleanup();
933 s = tcc_mallocz(sizeof(TCCState));
934 if (!s)
935 return NULL;
936 tcc_state = s;
937 #ifdef _WIN32
938 tcc_set_lib_path_w32(s);
939 #else
940 tcc_set_lib_path(s, CONFIG_TCCDIR);
941 #endif
942 s->output_type = 0;
943 preprocess_new();
944 s->include_stack_ptr = s->include_stack;
946 /* we add dummy defines for some special macros to speed up tests
947 and to have working defined() */
948 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
949 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
950 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
951 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
953 /* define __TINYC__ 92X */
954 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
955 sprintf(buffer, "%d", a*10000 + b*100 + c);
956 tcc_define_symbol(s, "__TINYC__", buffer);
958 /* standard defines */
959 tcc_define_symbol(s, "__STDC__", NULL);
960 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
961 tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
963 /* target defines */
964 #if defined(TCC_TARGET_I386)
965 tcc_define_symbol(s, "__i386__", NULL);
966 tcc_define_symbol(s, "__i386", NULL);
967 tcc_define_symbol(s, "i386", NULL);
968 #elif defined(TCC_TARGET_X86_64)
969 tcc_define_symbol(s, "__x86_64__", NULL);
970 #elif defined(TCC_TARGET_ARM)
971 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
972 tcc_define_symbol(s, "__arm_elf__", NULL);
973 tcc_define_symbol(s, "__arm_elf", NULL);
974 tcc_define_symbol(s, "arm_elf", NULL);
975 tcc_define_symbol(s, "__arm__", NULL);
976 tcc_define_symbol(s, "__arm", NULL);
977 tcc_define_symbol(s, "arm", NULL);
978 tcc_define_symbol(s, "__APCS_32__", NULL);
979 tcc_define_symbol(s, "__ARMEL__", NULL);
980 #if defined(TCC_ARM_EABI)
981 tcc_define_symbol(s, "__ARM_EABI__", NULL);
982 #endif
983 #if defined(TCC_ARM_HARDFLOAT)
984 s->float_abi = ARM_HARD_FLOAT;
985 tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
986 #else
987 s->float_abi = ARM_SOFTFP_FLOAT;
988 #endif
989 #elif defined(TCC_TARGET_ARM64)
990 tcc_define_symbol(s, "__aarch64__", NULL);
991 #endif
993 #ifdef TCC_TARGET_PE
994 tcc_define_symbol(s, "_WIN32", NULL);
995 # ifdef TCC_TARGET_X86_64
996 tcc_define_symbol(s, "_WIN64", NULL);
997 # endif
998 #else
999 tcc_define_symbol(s, "__unix__", NULL);
1000 tcc_define_symbol(s, "__unix", NULL);
1001 tcc_define_symbol(s, "unix", NULL);
1002 # if defined(__linux)
1003 tcc_define_symbol(s, "__linux__", NULL);
1004 tcc_define_symbol(s, "__linux", NULL);
1005 # endif
1006 # if defined(__FreeBSD__)
1007 # define str(s) #s
1008 tcc_define_symbol(s, "__FreeBSD__", str( __FreeBSD__));
1009 # undef str
1010 # endif
1011 # if defined(__FreeBSD_kernel__)
1012 tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
1013 # endif
1014 #endif
1016 /* TinyCC & gcc defines */
1017 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
1018 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
1019 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
1020 #else
1021 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
1022 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
1023 #endif
1025 #ifdef TCC_TARGET_PE
1026 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
1027 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
1028 #else
1029 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
1030 /* wint_t is unsigned int by default, but (signed) int on BSDs
1031 and unsigned short on windows. Other OSes might have still
1032 other conventions, sigh. */
1033 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
1034 tcc_define_symbol(s, "__WINT_TYPE__", "int");
1035 #else
1036 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
1037 #endif
1038 #endif
1040 #ifndef TCC_TARGET_PE
1041 /* glibc defines */
1042 tcc_define_symbol(s, "__REDIRECT(name, proto, alias)", "name proto __asm__ (#alias)");
1043 tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)", "name proto __asm__ (#alias) __THROW");
1044 /* paths for crt objects */
1045 tcc_split_path(s, (void ***)&s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
1046 #endif
1048 /* no section zero */
1049 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
1051 /* create standard sections */
1052 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
1053 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
1054 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
1056 /* symbols are always generated for linking stage */
1057 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
1058 ".strtab",
1059 ".hashtab", SHF_PRIVATE);
1060 strtab_section = symtab_section->link;
1061 s->symtab = symtab_section;
1063 /* private symbol table for dynamic symbols */
1064 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
1065 ".dynstrtab",
1066 ".dynhashtab", SHF_PRIVATE);
1067 s->alacarte_link = 1;
1068 s->nocommon = 1;
1069 s->warn_implicit_function_declaration = 1;
1071 #ifdef CHAR_IS_UNSIGNED
1072 s->char_is_unsigned = 1;
1073 #endif
1074 /* enable this if you want symbols with leading underscore on windows: */
1075 #if 0 /* def TCC_TARGET_PE */
1076 s->leading_underscore = 1;
1077 #endif
1078 #ifdef TCC_TARGET_I386
1079 s->seg_size = 32;
1080 #endif
1081 #ifdef TCC_IS_NATIVE
1082 s->runtime_main = "main";
1083 #endif
1084 return s;
1087 LIBTCCAPI void tcc_delete(TCCState *s1)
1089 int i;
1091 tcc_cleanup();
1093 /* free all sections */
1094 for(i = 1; i < s1->nb_sections; i++)
1095 free_section(s1->sections[i]);
1096 dynarray_reset(&s1->sections, &s1->nb_sections);
1098 for(i = 0; i < s1->nb_priv_sections; i++)
1099 free_section(s1->priv_sections[i]);
1100 dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
1102 /* free any loaded DLLs */
1103 #ifdef TCC_IS_NATIVE
1104 for ( i = 0; i < s1->nb_loaded_dlls; i++) {
1105 DLLReference *ref = s1->loaded_dlls[i];
1106 if ( ref->handle )
1107 dlclose(ref->handle);
1109 #endif
1111 /* free loaded dlls array */
1112 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
1114 /* free library paths */
1115 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
1116 dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
1118 /* free include paths */
1119 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
1120 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
1121 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
1123 tcc_free(s1->tcc_lib_path);
1124 tcc_free(s1->soname);
1125 tcc_free(s1->rpath);
1126 tcc_free(s1->init_symbol);
1127 tcc_free(s1->fini_symbol);
1128 tcc_free(s1->outfile);
1129 tcc_free(s1->deps_outfile);
1130 dynarray_reset(&s1->files, &s1->nb_files);
1131 dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
1133 #ifdef TCC_IS_NATIVE
1134 # ifdef HAVE_SELINUX
1135 munmap (s1->write_mem, s1->mem_size);
1136 munmap (s1->runtime_mem, s1->mem_size);
1137 # else
1138 tcc_free(s1->runtime_mem);
1139 # endif
1140 #endif
1142 if(s1->sym_attrs) tcc_free(s1->sym_attrs);
1144 tcc_free(s1);
1147 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
1149 tcc_split_path(s, (void ***)&s->include_paths, &s->nb_include_paths, pathname);
1150 return 0;
1153 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
1155 tcc_split_path(s, (void ***)&s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
1156 return 0;
1159 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype)
1161 ElfW(Ehdr) ehdr;
1162 int fd, ret, size;
1164 parse_flags = 0;
1165 #ifdef CONFIG_TCC_ASM
1166 /* if .S file, define __ASSEMBLER__ like gcc does */
1167 if ((filetype == TCC_FILETYPE_ASM) || (filetype == TCC_FILETYPE_ASM_PP)) {
1168 tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
1169 parse_flags = PARSE_FLAG_ASM_FILE;
1171 #endif
1173 /* open the file */
1174 ret = tcc_open(s1, filename);
1175 if (ret < 0) {
1176 if (flags & AFF_PRINT_ERROR)
1177 tcc_error_noabort("file '%s' not found", filename);
1178 return ret;
1181 /* update target deps */
1182 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1183 tcc_strdup(filename));
1185 if (flags & AFF_PREPROCESS) {
1186 ret = tcc_preprocess(s1);
1187 goto the_end;
1190 if (filetype == TCC_FILETYPE_C) {
1191 /* C file assumed */
1192 ret = tcc_compile(s1);
1193 goto the_end;
1196 #ifdef CONFIG_TCC_ASM
1197 if (filetype == TCC_FILETYPE_ASM_PP) {
1198 /* non preprocessed assembler */
1199 ret = tcc_assemble(s1, 1);
1200 goto the_end;
1203 if (filetype == TCC_FILETYPE_ASM) {
1204 /* preprocessed assembler */
1205 ret = tcc_assemble(s1, 0);
1206 goto the_end;
1208 #endif
1210 fd = file->fd;
1211 /* assume executable format: auto guess file type */
1212 size = read(fd, &ehdr, sizeof(ehdr));
1213 lseek(fd, 0, SEEK_SET);
1214 if (size <= 0) {
1215 tcc_error_noabort("could not read header");
1216 goto the_end;
1219 if (size == sizeof(ehdr) &&
1220 ehdr.e_ident[0] == ELFMAG0 &&
1221 ehdr.e_ident[1] == ELFMAG1 &&
1222 ehdr.e_ident[2] == ELFMAG2 &&
1223 ehdr.e_ident[3] == ELFMAG3) {
1225 /* do not display line number if error */
1226 file->line_num = 0;
1227 if (ehdr.e_type == ET_REL) {
1228 ret = tcc_load_object_file(s1, fd, 0);
1229 goto the_end;
1232 #ifndef TCC_TARGET_PE
1233 if (ehdr.e_type == ET_DYN) {
1234 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1235 #ifdef TCC_IS_NATIVE
1236 void *h;
1237 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
1238 if (h)
1239 #endif
1240 ret = 0;
1241 } else {
1242 ret = tcc_load_dll(s1, fd, filename,
1243 (flags & AFF_REFERENCED_DLL) != 0);
1245 goto the_end;
1247 #endif
1248 tcc_error_noabort("unrecognized ELF file");
1249 goto the_end;
1252 if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
1253 file->line_num = 0; /* do not display line number if error */
1254 ret = tcc_load_archive(s1, fd);
1255 goto the_end;
1258 #ifdef TCC_TARGET_COFF
1259 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
1260 ret = tcc_load_coff(s1, fd);
1261 goto the_end;
1263 #endif
1265 #ifdef TCC_TARGET_PE
1266 ret = pe_load_file(s1, filename, fd);
1267 #else
1268 /* as GNU ld, consider it is an ld script if not recognized */
1269 ret = tcc_load_ldscript(s1);
1270 #endif
1271 if (ret < 0)
1272 tcc_error_noabort("unrecognized file type");
1274 the_end:
1275 tcc_close();
1276 return ret;
1279 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, int filetype)
1281 if (s->output_type == TCC_OUTPUT_PREPROCESS)
1282 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS, filetype);
1283 else
1284 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR, filetype);
1287 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1289 tcc_split_path(s, (void ***)&s->library_paths, &s->nb_library_paths, pathname);
1290 return 0;
1293 static int tcc_add_library_internal(TCCState *s, const char *fmt,
1294 const char *filename, int flags, char **paths, int nb_paths)
1296 char buf[1024];
1297 int i;
1299 for(i = 0; i < nb_paths; i++) {
1300 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1301 if (tcc_add_file_internal(s, buf, flags, TCC_FILETYPE_BINARY) == 0)
1302 return 0;
1304 return -1;
1307 /* find and load a dll. Return non zero if not found */
1308 /* XXX: add '-rpath' option support ? */
1309 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1311 return tcc_add_library_internal(s, "%s/%s", filename, flags,
1312 s->library_paths, s->nb_library_paths);
1315 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename)
1317 if (-1 == tcc_add_library_internal(s, "%s/%s",
1318 filename, 0, s->crt_paths, s->nb_crt_paths))
1319 tcc_error_noabort("file '%s' not found", filename);
1320 return 0;
1323 /* the library name is the same as the argument of the '-l' option */
1324 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1326 #ifdef TCC_TARGET_PE
1327 const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
1328 const char **pp = s->static_link ? libs + 4 : libs;
1329 #else
1330 const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
1331 const char **pp = s->static_link ? libs + 1 : libs;
1332 #endif
1333 while (*pp) {
1334 if (0 == tcc_add_library_internal(s, *pp,
1335 libraryname, 0, s->library_paths, s->nb_library_paths))
1336 return 0;
1337 ++pp;
1339 return -1;
1342 LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
1344 #ifdef TCC_TARGET_PE
1345 /* On x86_64 'val' might not be reachable with a 32bit offset.
1346 So it is handled here as if it were in a DLL. */
1347 pe_putimport(s, 0, name, (uintptr_t)val);
1348 #else
1349 add_elf_sym(symtab_section, (uintptr_t)val, 0,
1350 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1351 SHN_ABS, name);
1352 #endif
1353 return 0;
1356 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
1358 s->output_type = output_type;
1359 if (output_type == TCC_OUTPUT_PREPROCESS)
1360 print_defines();
1362 if (!s->nostdinc) {
1363 /* default include paths */
1364 /* -isystem paths have already been handled */
1365 tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
1368 /* if bound checking, then add corresponding sections */
1369 #ifdef CONFIG_TCC_BCHECK
1370 if (s->do_bounds_check) {
1371 /* define symbol */
1372 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
1373 /* create bounds sections */
1374 bounds_section = new_section(s, ".bounds",
1375 SHT_PROGBITS, SHF_ALLOC);
1376 lbounds_section = new_section(s, ".lbounds",
1377 SHT_PROGBITS, SHF_ALLOC);
1379 #endif
1381 if (s->char_is_unsigned) {
1382 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
1385 /* add debug sections */
1386 if (s->do_debug) {
1387 /* stab symbols */
1388 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
1389 stab_section->sh_entsize = sizeof(Stab_Sym);
1390 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
1391 put_elf_str(stabstr_section, "");
1392 stab_section->link = stabstr_section;
1393 /* put first entry */
1394 put_stabs("", 0, 0, 0, 0);
1397 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
1398 #ifdef TCC_TARGET_PE
1399 # ifdef _WIN32
1400 tcc_add_systemdir(s);
1401 # endif
1402 #else
1403 /* add libc crt1/crti objects */
1404 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
1405 !s->nostdlib) {
1406 if (output_type != TCC_OUTPUT_DLL)
1407 tcc_add_crt(s, "crt1.o");
1408 tcc_add_crt(s, "crti.o");
1410 #endif
1412 #ifdef CONFIG_TCC_BCHECK
1413 if (s->do_bounds_check && (output_type == TCC_OUTPUT_EXE))
1415 /* force a bcheck.o linking */
1416 addr_t func = TOK___bound_init;
1417 Sym *sym = external_global_sym(func, &func_old_type, 0);
1418 if (!sym->c)
1419 put_extern_sym(sym, NULL, 0, 0);
1421 #endif
1422 return 0;
1425 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1427 tcc_free(s->tcc_lib_path);
1428 s->tcc_lib_path = tcc_strdup(path);
1431 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1432 #define FD_INVERT 0x0002 /* invert value before storing */
1434 typedef struct FlagDef {
1435 uint16_t offset;
1436 uint16_t flags;
1437 const char *name;
1438 } FlagDef;
1440 static const FlagDef warning_defs[] = {
1441 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1442 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1443 { offsetof(TCCState, warn_error), 0, "error" },
1444 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1445 "implicit-function-declaration" },
1448 ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
1449 const char *name, int value)
1451 int i;
1452 const FlagDef *p;
1453 const char *r;
1455 r = name;
1456 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
1457 r += 3;
1458 value = !value;
1460 for(i = 0, p = flags; i < nb_flags; i++, p++) {
1461 if (!strcmp(r, p->name))
1462 goto found;
1464 return -1;
1465 found:
1466 if (p->flags & FD_INVERT)
1467 value = !value;
1468 *(int *)((uint8_t *)s + p->offset) = value;
1469 return 0;
1472 /* set/reset a warning */
1473 static int tcc_set_warning(TCCState *s, const char *warning_name, int value)
1475 int i;
1476 const FlagDef *p;
1478 if (!strcmp(warning_name, "all")) {
1479 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
1480 if (p->flags & WD_ALL)
1481 *(int *)((uint8_t *)s + p->offset) = 1;
1483 return 0;
1484 } else {
1485 return set_flag(s, warning_defs, countof(warning_defs),
1486 warning_name, value);
1490 static const FlagDef flag_defs[] = {
1491 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1492 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1493 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1494 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1495 { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
1496 { offsetof(TCCState, old_struct_init_code), 0, "old-struct-init-code" },
1497 { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
1500 /* set/reset a flag */
1501 static int tcc_set_flag(TCCState *s, const char *flag_name, int value)
1503 return set_flag(s, flag_defs, countof(flag_defs),
1504 flag_name, value);
1508 static int strstart(const char *val, const char **str)
1510 const char *p, *q;
1511 p = *str;
1512 q = val;
1513 while (*q) {
1514 if (*p != *q)
1515 return 0;
1516 p++;
1517 q++;
1519 *str = p;
1520 return 1;
1523 /* Like strstart, but automatically takes into account that ld options can
1525 * - start with double or single dash (e.g. '--soname' or '-soname')
1526 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1527 * or '-Wl,-soname=x.so')
1529 * you provide `val` always in 'option[=]' form (no leading -)
1531 static int link_option(const char *str, const char *val, const char **ptr)
1533 const char *p, *q;
1535 /* there should be 1 or 2 dashes */
1536 if (*str++ != '-')
1537 return 0;
1538 if (*str == '-')
1539 str++;
1541 /* then str & val should match (potentialy up to '=') */
1542 p = str;
1543 q = val;
1545 while (*q != '\0' && *q != '=') {
1546 if (*p != *q)
1547 return 0;
1548 p++;
1549 q++;
1552 /* '=' near eos means ',' or '=' is ok */
1553 if (*q == '=') {
1554 if (*p != ',' && *p != '=')
1555 return 0;
1556 p++;
1557 q++;
1560 if (ptr)
1561 *ptr = p;
1562 return 1;
1565 static const char *skip_linker_arg(const char **str)
1567 const char *s1 = *str;
1568 const char *s2 = strchr(s1, ',');
1569 *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
1570 return s2;
1573 static char *copy_linker_arg(const char *p)
1575 const char *q = p;
1576 skip_linker_arg(&q);
1577 return pstrncpy(tcc_malloc(q - p + 1), p, q - p);
1580 /* set linker options */
1581 static int tcc_set_linker(TCCState *s, const char *option)
1583 while (option && *option) {
1585 const char *p = option;
1586 char *end = NULL;
1587 int ignoring = 0;
1589 if (link_option(option, "Bsymbolic", &p)) {
1590 s->symbolic = 1;
1591 } else if (link_option(option, "nostdlib", &p)) {
1592 s->nostdlib = 1;
1593 } else if (link_option(option, "fini=", &p)) {
1594 s->fini_symbol = copy_linker_arg(p);
1595 ignoring = 1;
1596 } else if (link_option(option, "image-base=", &p)
1597 || link_option(option, "Ttext=", &p)) {
1598 s->text_addr = strtoull(p, &end, 16);
1599 s->has_text_addr = 1;
1600 } else if (link_option(option, "init=", &p)) {
1601 s->init_symbol = copy_linker_arg(p);
1602 ignoring = 1;
1603 } else if (link_option(option, "oformat=", &p)) {
1604 #if defined(TCC_TARGET_PE)
1605 if (strstart("pe-", &p)) {
1606 #elif defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1607 if (strstart("elf64-", &p)) {
1608 #else
1609 if (strstart("elf32-", &p)) {
1610 #endif
1611 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1612 } else if (!strcmp(p, "binary")) {
1613 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1614 #ifdef TCC_TARGET_COFF
1615 } else if (!strcmp(p, "coff")) {
1616 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1617 #endif
1618 } else
1619 goto err;
1621 } else if (link_option(option, "as-needed", &p)) {
1622 ignoring = 1;
1623 } else if (link_option(option, "O", &p)) {
1624 ignoring = 1;
1625 } else if (link_option(option, "rpath=", &p)) {
1626 s->rpath = copy_linker_arg(p);
1627 } else if (link_option(option, "section-alignment=", &p)) {
1628 s->section_align = strtoul(p, &end, 16);
1629 } else if (link_option(option, "soname=", &p)) {
1630 s->soname = copy_linker_arg(p);
1631 #ifdef TCC_TARGET_PE
1632 } else if (link_option(option, "file-alignment=", &p)) {
1633 s->pe_file_align = strtoul(p, &end, 16);
1634 } else if (link_option(option, "stack=", &p)) {
1635 s->pe_stack_size = strtoul(p, &end, 10);
1636 } else if (link_option(option, "subsystem=", &p)) {
1637 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1638 if (!strcmp(p, "native")) {
1639 s->pe_subsystem = 1;
1640 } else if (!strcmp(p, "console")) {
1641 s->pe_subsystem = 3;
1642 } else if (!strcmp(p, "gui")) {
1643 s->pe_subsystem = 2;
1644 } else if (!strcmp(p, "posix")) {
1645 s->pe_subsystem = 7;
1646 } else if (!strcmp(p, "efiapp")) {
1647 s->pe_subsystem = 10;
1648 } else if (!strcmp(p, "efiboot")) {
1649 s->pe_subsystem = 11;
1650 } else if (!strcmp(p, "efiruntime")) {
1651 s->pe_subsystem = 12;
1652 } else if (!strcmp(p, "efirom")) {
1653 s->pe_subsystem = 13;
1654 #elif defined(TCC_TARGET_ARM)
1655 if (!strcmp(p, "wince")) {
1656 s->pe_subsystem = 9;
1657 #endif
1658 } else
1659 goto err;
1660 #endif
1661 } else
1662 goto err;
1664 if (ignoring && s->warn_unsupported) err: {
1665 char buf[100], *e;
1666 pstrcpy(buf, sizeof buf, e = copy_linker_arg(option)), tcc_free(e);
1667 if (ignoring)
1668 tcc_warning("unsupported linker option '%s'", buf);
1669 else
1670 tcc_error("unsupported linker option '%s'", buf);
1672 option = skip_linker_arg(&p);
1674 return 0;
1677 typedef struct TCCOption {
1678 const char *name;
1679 uint16_t index;
1680 uint16_t flags;
1681 } TCCOption;
1683 enum {
1684 TCC_OPTION_HELP,
1685 TCC_OPTION_I,
1686 TCC_OPTION_D,
1687 TCC_OPTION_U,
1688 TCC_OPTION_P,
1689 TCC_OPTION_L,
1690 TCC_OPTION_B,
1691 TCC_OPTION_l,
1692 TCC_OPTION_bench,
1693 TCC_OPTION_bt,
1694 TCC_OPTION_b,
1695 TCC_OPTION_g,
1696 TCC_OPTION_c,
1697 TCC_OPTION_dumpversion,
1698 TCC_OPTION_d,
1699 TCC_OPTION_float_abi,
1700 TCC_OPTION_static,
1701 TCC_OPTION_std,
1702 TCC_OPTION_shared,
1703 TCC_OPTION_soname,
1704 TCC_OPTION_o,
1705 TCC_OPTION_r,
1706 TCC_OPTION_s,
1707 TCC_OPTION_Wl,
1708 TCC_OPTION_W,
1709 TCC_OPTION_O,
1710 TCC_OPTION_m,
1711 TCC_OPTION_f,
1712 TCC_OPTION_isystem,
1713 TCC_OPTION_nostdinc,
1714 TCC_OPTION_nostdlib,
1715 TCC_OPTION_print_search_dirs,
1716 TCC_OPTION_rdynamic,
1717 TCC_OPTION_pedantic,
1718 TCC_OPTION_pthread,
1719 TCC_OPTION_run,
1720 TCC_OPTION_v,
1721 TCC_OPTION_w,
1722 TCC_OPTION_pipe,
1723 TCC_OPTION_E,
1724 TCC_OPTION_MD,
1725 TCC_OPTION_MF,
1726 TCC_OPTION_x,
1729 #define TCC_OPTION_HAS_ARG 0x0001
1730 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1732 static const TCCOption tcc_options[] = {
1733 { "h", TCC_OPTION_HELP, 0 },
1734 { "-help", TCC_OPTION_HELP, 0 },
1735 { "?", TCC_OPTION_HELP, 0 },
1736 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
1737 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
1738 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
1739 { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1740 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
1741 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
1742 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1743 { "bench", TCC_OPTION_bench, 0 },
1744 #ifdef CONFIG_TCC_BACKTRACE
1745 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
1746 #endif
1747 #ifdef CONFIG_TCC_BCHECK
1748 { "b", TCC_OPTION_b, 0 },
1749 #endif
1750 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1751 { "c", TCC_OPTION_c, 0 },
1752 { "dumpversion", TCC_OPTION_dumpversion, 0},
1753 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1754 #ifdef TCC_TARGET_ARM
1755 { "mfloat-abi", TCC_OPTION_float_abi, TCC_OPTION_HAS_ARG },
1756 #endif
1757 { "static", TCC_OPTION_static, 0 },
1758 { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1759 { "shared", TCC_OPTION_shared, 0 },
1760 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
1761 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
1762 { "pedantic", TCC_OPTION_pedantic, 0},
1763 { "pthread", TCC_OPTION_pthread, 0},
1764 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1765 { "rdynamic", TCC_OPTION_rdynamic, 0 },
1766 { "r", TCC_OPTION_r, 0 },
1767 { "s", TCC_OPTION_s, 0 },
1768 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1769 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1770 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1771 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
1772 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1773 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
1774 { "nostdinc", TCC_OPTION_nostdinc, 0 },
1775 { "nostdlib", TCC_OPTION_nostdlib, 0 },
1776 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
1777 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1778 { "w", TCC_OPTION_w, 0 },
1779 { "pipe", TCC_OPTION_pipe, 0},
1780 { "E", TCC_OPTION_E, 0},
1781 { "MD", TCC_OPTION_MD, 0},
1782 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
1783 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
1784 { NULL, 0, 0 },
1787 static void parse_option_D(TCCState *s1, const char *optarg)
1789 char *sym = tcc_strdup(optarg);
1790 char *value = strchr(sym, '=');
1791 if (value)
1792 *value++ = '\0';
1793 tcc_define_symbol(s1, sym, value);
1794 tcc_free(sym);
1797 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
1799 int len = strlen(filename);
1800 char *p = tcc_malloc(len + 2);
1801 if (filetype) {
1802 *p = filetype;
1804 else {
1805 /* use a file extension to detect a filetype */
1806 const char *ext = tcc_fileextension(filename);
1807 if (ext[0]) {
1808 ext++;
1809 if (!strcmp(ext, "S"))
1810 *p = TCC_FILETYPE_ASM_PP;
1811 else
1812 if (!strcmp(ext, "s"))
1813 *p = TCC_FILETYPE_ASM;
1814 else
1815 if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
1816 *p = TCC_FILETYPE_C;
1817 else
1818 *p = TCC_FILETYPE_BINARY;
1820 else {
1821 *p = TCC_FILETYPE_C;
1824 strcpy(p+1, filename);
1825 dynarray_add((void ***)&s->files, &s->nb_files, p);
1828 PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
1830 const TCCOption *popt;
1831 const char *optarg, *r;
1832 int run = 0;
1833 int pthread = 0;
1834 int optind = 0;
1835 int filetype = 0;
1837 /* collect -Wl options for input such as "-Wl,-rpath -Wl,<path>" */
1838 CString linker_arg;
1839 cstr_new(&linker_arg);
1841 while (optind < argc) {
1843 r = argv[optind++];
1844 if (r[0] != '-' || r[1] == '\0') {
1845 args_parser_add_file(s, r, filetype);
1846 if (run) {
1847 optind--;
1848 /* argv[0] will be this file */
1849 break;
1851 continue;
1854 /* find option in table */
1855 for(popt = tcc_options; ; ++popt) {
1856 const char *p1 = popt->name;
1857 const char *r1 = r + 1;
1858 if (p1 == NULL)
1859 tcc_error("invalid option -- '%s'", r);
1860 if (!strstart(p1, &r1))
1861 continue;
1862 optarg = r1;
1863 if (popt->flags & TCC_OPTION_HAS_ARG) {
1864 if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
1865 if (optind >= argc)
1866 tcc_error("argument to '%s' is missing", r);
1867 optarg = argv[optind++];
1869 } else if (*r1 != '\0')
1870 continue;
1871 break;
1874 switch(popt->index) {
1875 case TCC_OPTION_HELP:
1876 return 0;
1877 case TCC_OPTION_I:
1878 if (tcc_add_include_path(s, optarg) < 0)
1879 tcc_error("too many include paths");
1880 break;
1881 case TCC_OPTION_D:
1882 parse_option_D(s, optarg);
1883 break;
1884 case TCC_OPTION_U:
1885 tcc_undefine_symbol(s, optarg);
1886 break;
1887 case TCC_OPTION_L:
1888 tcc_add_library_path(s, optarg);
1889 break;
1890 case TCC_OPTION_B:
1891 /* set tcc utilities path (mainly for tcc development) */
1892 tcc_set_lib_path(s, optarg);
1893 break;
1894 case TCC_OPTION_l:
1895 args_parser_add_file(s, r, TCC_FILETYPE_BINARY);
1896 s->nb_libraries++;
1897 break;
1898 case TCC_OPTION_pthread:
1899 parse_option_D(s, "_REENTRANT");
1900 pthread = 1;
1901 break;
1902 case TCC_OPTION_bench:
1903 s->do_bench = 1;
1904 break;
1905 #ifdef CONFIG_TCC_BACKTRACE
1906 case TCC_OPTION_bt:
1907 tcc_set_num_callers(atoi(optarg));
1908 break;
1909 #endif
1910 #ifdef CONFIG_TCC_BCHECK
1911 case TCC_OPTION_b:
1912 s->do_bounds_check = 1;
1913 s->do_debug = 1;
1914 break;
1915 #endif
1916 case TCC_OPTION_g:
1917 s->do_debug = 1;
1918 break;
1919 case TCC_OPTION_c:
1920 if (s->output_type)
1921 tcc_warning("-c: some compiler action already specified (%d)", s->output_type);
1922 s->output_type = TCC_OUTPUT_OBJ;
1923 break;
1924 case TCC_OPTION_d:
1925 if (*optarg != 'D') {
1926 if (s->warn_unsupported)
1927 goto unsupported_option;
1928 tcc_error("invalid option -- '%s'", r);
1930 s->dflag = 1;
1931 break;
1932 #ifdef TCC_TARGET_ARM
1933 case TCC_OPTION_float_abi:
1934 /* tcc doesn't support soft float yet */
1935 if (!strcmp(optarg, "softfp")) {
1936 s->float_abi = ARM_SOFTFP_FLOAT;
1937 tcc_undefine_symbol(s, "__ARM_PCS_VFP");
1938 } else if (!strcmp(optarg, "hard"))
1939 s->float_abi = ARM_HARD_FLOAT;
1940 else
1941 tcc_error("unsupported float abi '%s'", optarg);
1942 break;
1943 #endif
1944 case TCC_OPTION_static:
1945 s->static_link = 1;
1946 break;
1947 case TCC_OPTION_std:
1948 /* silently ignore, a current purpose:
1949 allow to use a tcc as a reference compiler for "make test" */
1950 break;
1951 case TCC_OPTION_shared:
1952 if (s->output_type)
1953 tcc_warning("-shared: some compiler action already specified (%d)", s->output_type);
1954 s->output_type = TCC_OUTPUT_DLL;
1955 break;
1956 case TCC_OPTION_soname:
1957 s->soname = tcc_strdup(optarg);
1958 break;
1959 case TCC_OPTION_m:
1960 s->option_m = tcc_strdup(optarg);
1961 break;
1962 case TCC_OPTION_o:
1963 s->outfile = tcc_strdup(optarg);
1964 break;
1965 case TCC_OPTION_r:
1966 /* generate a .o merging several output files */
1967 if (s->output_type)
1968 tcc_warning("-r: some compiler action already specified (%d)", s->output_type);
1969 s->option_r = 1;
1970 s->output_type = TCC_OUTPUT_OBJ;
1971 break;
1972 case TCC_OPTION_isystem:
1973 tcc_add_sysinclude_path(s, optarg);
1974 break;
1975 case TCC_OPTION_nostdinc:
1976 s->nostdinc = 1;
1977 break;
1978 case TCC_OPTION_nostdlib:
1979 s->nostdlib = 1;
1980 break;
1981 case TCC_OPTION_print_search_dirs:
1982 s->print_search_dirs = 1;
1983 break;
1984 case TCC_OPTION_run:
1985 if (s->output_type)
1986 tcc_warning("-run: some compiler action already specified (%d)", s->output_type);
1987 s->output_type = TCC_OUTPUT_MEMORY;
1988 tcc_set_options(s, optarg);
1989 run = 1;
1990 break;
1991 case TCC_OPTION_v:
1992 do ++s->verbose; while (*optarg++ == 'v');
1993 break;
1994 case TCC_OPTION_f:
1995 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
1996 goto unsupported_option;
1997 break;
1998 case TCC_OPTION_W:
1999 if (tcc_set_warning(s, optarg, 1) < 0 &&
2000 s->warn_unsupported)
2001 goto unsupported_option;
2002 break;
2003 case TCC_OPTION_w:
2004 s->warn_none = 1;
2005 break;
2006 case TCC_OPTION_rdynamic:
2007 s->rdynamic = 1;
2008 break;
2009 case TCC_OPTION_Wl:
2010 if (linker_arg.size)
2011 --linker_arg.size, cstr_ccat(&linker_arg, ',');
2012 cstr_cat(&linker_arg, optarg);
2013 cstr_ccat(&linker_arg, '\0');
2014 break;
2015 case TCC_OPTION_E:
2016 if (s->output_type)
2017 tcc_warning("-E: some compiler action already specified (%d)", s->output_type);
2018 s->output_type = TCC_OUTPUT_PREPROCESS;
2019 break;
2020 case TCC_OPTION_P:
2021 s->Pflag = atoi(optarg) + 1;
2022 break;
2023 case TCC_OPTION_MD:
2024 s->gen_deps = 1;
2025 break;
2026 case TCC_OPTION_MF:
2027 s->deps_outfile = tcc_strdup(optarg);
2028 break;
2029 case TCC_OPTION_dumpversion:
2030 printf ("%s\n", TCC_VERSION);
2031 exit(0);
2032 case TCC_OPTION_s:
2033 s->do_strip = 1;
2034 break;
2035 case TCC_OPTION_x:
2036 if (*optarg == 'c')
2037 filetype = TCC_FILETYPE_C;
2038 else
2039 if (*optarg == 'a')
2040 filetype = TCC_FILETYPE_ASM_PP;
2041 else
2042 if (*optarg == 'n')
2043 filetype = 0;
2044 else
2045 tcc_warning("unsupported language '%s'", optarg);
2046 break;
2047 case TCC_OPTION_O:
2048 case TCC_OPTION_pedantic:
2049 case TCC_OPTION_pipe:
2050 /* ignored */
2051 break;
2052 default:
2053 if (s->warn_unsupported) {
2054 unsupported_option:
2055 tcc_warning("unsupported option '%s'", r);
2057 break;
2061 if (pthread && s->output_type != TCC_OUTPUT_OBJ)
2062 tcc_set_options(s, "-lpthread");
2064 tcc_set_linker(s, (const char *)linker_arg.data);
2065 cstr_free(&linker_arg);
2067 return optind;
2070 LIBTCCAPI int tcc_set_options(TCCState *s, const char *str)
2072 const char *s1;
2073 char **argv, *arg;
2074 int argc, len;
2075 int ret;
2077 argc = 0, argv = NULL;
2078 for(;;) {
2079 while (is_space(*str))
2080 str++;
2081 if (*str == '\0')
2082 break;
2083 s1 = str;
2084 while (*str != '\0' && !is_space(*str))
2085 str++;
2086 len = str - s1;
2087 arg = tcc_malloc(len + 1);
2088 pstrncpy(arg, s1, len);
2089 dynarray_add((void ***)&argv, &argc, arg);
2091 ret = tcc_parse_args(s, argc, argv);
2092 dynarray_reset(&argv, &argc);
2093 return ret;
2096 PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time)
2098 double tt;
2099 tt = (double)total_time / 1000000.0;
2100 if (tt < 0.001)
2101 tt = 0.001;
2102 if (total_bytes < 1)
2103 total_bytes = 1;
2104 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
2105 tok_ident - TOK_IDENT, total_lines, total_bytes,
2106 tt, (int)(total_lines / tt),
2107 total_bytes / tt / 1000000.0);
2110 PUB_FUNC void tcc_set_environment(TCCState *s)
2112 char * path;
2114 path = getenv("C_INCLUDE_PATH");
2115 if(path != NULL) {
2116 tcc_add_include_path(s, path);
2118 path = getenv("CPATH");
2119 if(path != NULL) {
2120 tcc_add_include_path(s, path);
2122 path = getenv("LIBRARY_PATH");
2123 if(path != NULL) {
2124 tcc_add_library_path(s, path);