tccelf: fix warning
[tinycc.git] / libtcc.c
blob6a880895b1abeda883b99195e11efc9574382643
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 /********************************************************/
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 #ifdef CONFIG_TCC_BACKTRACE
36 ST_DATA int num_callers = 6;
37 ST_DATA const char **rt_bound_error_msg;
38 ST_DATA void *rt_prog_main;
39 #endif
41 /********************************************************/
43 #ifndef NOTALLINONE
44 #include "tccpp.c"
45 #include "tccgen.c"
46 #include "tccelf.c"
47 #include "tccrun.c"
48 #ifdef TCC_TARGET_I386
49 #include "i386-gen.c"
50 #endif
51 #ifdef TCC_TARGET_ARM
52 #include "arm-gen.c"
53 #endif
54 #ifdef TCC_TARGET_C67
55 #include "c67-gen.c"
56 #endif
57 #ifdef TCC_TARGET_X86_64
58 #include "x86_64-gen.c"
59 #endif
60 #ifdef CONFIG_TCC_ASM
61 #include "tccasm.c"
62 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
63 #include "i386-asm.c"
64 #endif
65 #endif
66 #ifdef TCC_TARGET_COFF
67 #include "tcccoff.c"
68 #endif
69 #ifdef TCC_TARGET_PE
70 #include "tccpe.c"
71 #endif
72 #endif /* ALL_IN_ONE */
74 /********************************************************/
75 #ifndef CONFIG_TCC_ASM
76 ST_FUNC void asm_instr(void)
78 error("inline asm() not supported");
80 ST_FUNC void asm_global_instr(void)
82 error("inline asm() not supported");
84 #endif
86 /********************************************************/
88 #ifdef _WIN32
89 static char *normalize_slashes(char *path)
91 char *p;
92 for (p = path; *p; ++p)
93 if (*p == '\\')
94 *p = '/';
95 return path;
98 static HMODULE tcc_module;
100 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
101 static void tcc_set_lib_path_w32(TCCState *s)
103 char path[1024], *p;
104 GetModuleFileNameA(tcc_module, path, sizeof path);
105 p = tcc_basename(normalize_slashes(strlwr(path)));
106 if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
107 p -= 5;
108 else if (p > path)
109 p--;
110 *p = 0;
111 tcc_set_lib_path(s, path);
114 #ifndef CONFIG_TCC_STATIC
115 void dlclose(void *p)
117 FreeLibrary((HMODULE)p);
119 #endif
121 #ifdef LIBTCC_AS_DLL
122 BOOL WINAPI DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
124 if (DLL_PROCESS_ATTACH == dwReason)
125 tcc_module = hDll;
126 return TRUE;
128 #endif
129 #endif
131 /********************************************************/
132 /* copy a string and truncate it. */
133 PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
135 char *q, *q_end;
136 int c;
138 if (buf_size > 0) {
139 q = buf;
140 q_end = buf + buf_size - 1;
141 while (q < q_end) {
142 c = *s++;
143 if (c == '\0')
144 break;
145 *q++ = c;
147 *q = '\0';
149 return buf;
152 /* strcat and truncate. */
153 PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
155 int len;
156 len = strlen(buf);
157 if (len < buf_size)
158 pstrcpy(buf + len, buf_size - len, s);
159 return buf;
162 /* extract the basename of a file */
163 PUB_FUNC char *tcc_basename(const char *name)
165 char *p = strchr(name, 0);
166 while (p > name && !IS_PATHSEP(p[-1]))
167 --p;
168 return p;
171 PUB_FUNC char *tcc_fileextension (const char *name)
173 char *b = tcc_basename(name);
174 char *e = strrchr(b, '.');
175 return e ? e : strchr(b, 0);
178 /********************************************************/
179 /* memory management */
181 #undef free
182 #undef malloc
183 #undef realloc
185 #ifdef MEM_DEBUG
186 int mem_cur_size;
187 int mem_max_size;
188 unsigned malloc_usable_size(void*);
189 #endif
191 PUB_FUNC void tcc_free(void *ptr)
193 #ifdef MEM_DEBUG
194 mem_cur_size -= malloc_usable_size(ptr);
195 #endif
196 free(ptr);
199 PUB_FUNC void *tcc_malloc(unsigned long size)
201 void *ptr;
202 ptr = malloc(size);
203 if (!ptr && size)
204 error("memory full");
205 #ifdef MEM_DEBUG
206 mem_cur_size += malloc_usable_size(ptr);
207 if (mem_cur_size > mem_max_size)
208 mem_max_size = mem_cur_size;
209 #endif
210 return ptr;
213 PUB_FUNC void *tcc_mallocz(unsigned long size)
215 void *ptr;
216 ptr = tcc_malloc(size);
217 memset(ptr, 0, size);
218 return ptr;
221 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
223 void *ptr1;
224 #ifdef MEM_DEBUG
225 mem_cur_size -= malloc_usable_size(ptr);
226 #endif
227 ptr1 = realloc(ptr, size);
228 #ifdef MEM_DEBUG
229 /* NOTE: count not correct if alloc error, but not critical */
230 mem_cur_size += malloc_usable_size(ptr1);
231 if (mem_cur_size > mem_max_size)
232 mem_max_size = mem_cur_size;
233 #endif
234 return ptr1;
237 PUB_FUNC char *tcc_strdup(const char *str)
239 char *ptr;
240 ptr = tcc_malloc(strlen(str) + 1);
241 strcpy(ptr, str);
242 return ptr;
245 PUB_FUNC void tcc_memstats(void)
247 #ifdef MEM_DEBUG
248 printf("memory in use: %d\n", mem_cur_size);
249 #endif
252 #define free(p) use_tcc_free(p)
253 #define malloc(s) use_tcc_malloc(s)
254 #define realloc(p, s) use_tcc_realloc(p, s)
256 /********************************************************/
257 /* dynarrays */
259 PUB_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data)
261 int nb, nb_alloc;
262 void **pp;
264 nb = *nb_ptr;
265 pp = *ptab;
266 /* every power of two we double array size */
267 if ((nb & (nb - 1)) == 0) {
268 if (!nb)
269 nb_alloc = 1;
270 else
271 nb_alloc = nb * 2;
272 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
273 if (!pp)
274 error("memory full");
275 *ptab = pp;
277 pp[nb++] = data;
278 *nb_ptr = nb;
281 PUB_FUNC void dynarray_reset(void *pp, int *n)
283 void **p;
284 for (p = *(void***)pp; *n; ++p, --*n)
285 if (*p)
286 tcc_free(*p);
287 tcc_free(*(void**)pp);
288 *(void**)pp = NULL;
291 /* we use our own 'finite' function to avoid potential problems with
292 non standard math libs */
293 /* XXX: endianness dependent */
294 ST_FUNC int ieee_finite(double d)
296 int *p = (int *)&d;
297 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
300 /********************************************************/
302 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
304 Section *sec;
306 sec = tcc_mallocz(sizeof(Section) + strlen(name));
307 strcpy(sec->name, name);
308 sec->sh_type = sh_type;
309 sec->sh_flags = sh_flags;
310 switch(sh_type) {
311 case SHT_HASH:
312 case SHT_REL:
313 case SHT_RELA:
314 case SHT_DYNSYM:
315 case SHT_SYMTAB:
316 case SHT_DYNAMIC:
317 sec->sh_addralign = 4;
318 break;
319 case SHT_STRTAB:
320 sec->sh_addralign = 1;
321 break;
322 default:
323 sec->sh_addralign = 32; /* default conservative alignment */
324 break;
327 if (sh_flags & SHF_PRIVATE) {
328 dynarray_add((void ***)&s1->priv_sections, &s1->nb_priv_sections, sec);
329 } else {
330 sec->sh_num = s1->nb_sections;
331 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
334 return sec;
337 static void free_section(Section *s)
339 tcc_free(s->data);
342 /* realloc section and set its content to zero */
343 ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
345 unsigned long size;
346 unsigned char *data;
348 size = sec->data_allocated;
349 if (size == 0)
350 size = 1;
351 while (size < new_size)
352 size = size * 2;
353 data = tcc_realloc(sec->data, size);
354 if (!data)
355 error("memory full");
356 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
357 sec->data = data;
358 sec->data_allocated = size;
361 /* reserve at least 'size' bytes in section 'sec' from
362 sec->data_offset. */
363 ST_FUNC void *section_ptr_add(Section *sec, unsigned long size)
365 unsigned long offset, offset1;
367 offset = sec->data_offset;
368 offset1 = offset + size;
369 if (offset1 > sec->data_allocated)
370 section_realloc(sec, offset1);
371 sec->data_offset = offset1;
372 return sec->data + offset;
375 /* reserve at least 'size' bytes from section start */
376 ST_FUNC void section_reserve(Section *sec, unsigned long size)
378 if (size > sec->data_allocated)
379 section_realloc(sec, size);
380 if (size > sec->data_offset)
381 sec->data_offset = size;
384 /* return a reference to a section, and create it if it does not
385 exists */
386 ST_FUNC Section *find_section(TCCState *s1, const char *name)
388 Section *sec;
389 int i;
390 for(i = 1; i < s1->nb_sections; i++) {
391 sec = s1->sections[i];
392 if (!strcmp(name, sec->name))
393 return sec;
395 /* sections are created as PROGBITS */
396 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
399 /* update sym->c so that it points to an external symbol in section
400 'section' with value 'value' */
401 ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
402 unsigned long value, unsigned long size,
403 int can_add_underscore)
405 int sym_type, sym_bind, sh_num, info, other;
406 ElfW(Sym) *esym;
407 const char *name;
408 char buf1[256];
410 if (section == NULL)
411 sh_num = SHN_UNDEF;
412 else if (section == SECTION_ABS)
413 sh_num = SHN_ABS;
414 else
415 sh_num = section->sh_num;
417 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
418 sym_type = STT_FUNC;
419 } else if ((sym->type.t & VT_BTYPE) == VT_VOID) {
420 sym_type = STT_NOTYPE;
421 } else {
422 sym_type = STT_OBJECT;
425 if (sym->type.t & VT_STATIC)
426 sym_bind = STB_LOCAL;
427 else {
428 if (sym->type.ref && FUNC_WEAK(sym->type.ref->r))
429 sym_bind = STB_WEAK;
430 else
431 sym_bind = STB_GLOBAL;
434 if (!sym->c) {
435 name = get_tok_str(sym->v, NULL);
436 #ifdef CONFIG_TCC_BCHECK
437 if (tcc_state->do_bounds_check) {
438 char buf[32];
440 /* XXX: avoid doing that for statics ? */
441 /* if bound checking is activated, we change some function
442 names by adding the "__bound" prefix */
443 switch(sym->v) {
444 #ifdef TCC_TARGET_PE
445 /* XXX: we rely only on malloc hooks */
446 case TOK_malloc:
447 case TOK_free:
448 case TOK_realloc:
449 case TOK_memalign:
450 case TOK_calloc:
451 #endif
452 case TOK_memcpy:
453 case TOK_memmove:
454 case TOK_memset:
455 case TOK_strlen:
456 case TOK_strcpy:
457 case TOK_alloca:
458 strcpy(buf, "__bound_");
459 strcat(buf, name);
460 name = buf;
461 break;
464 #endif
465 other = 0;
467 #ifdef TCC_TARGET_PE
468 if (sym->type.t & VT_EXPORT)
469 other |= 1;
470 if (sym_type == STT_FUNC && sym->type.ref) {
471 int attr = sym->type.ref->r;
472 if (FUNC_EXPORT(attr))
473 other |= 1;
474 if (FUNC_CALL(attr) == FUNC_STDCALL && can_add_underscore) {
475 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr) * PTR_SIZE);
476 name = buf1;
477 other |= 2;
478 can_add_underscore = 0;
480 } else {
481 if (find_elf_sym(tcc_state->dynsymtab_section, name))
482 other |= 4;
483 if (sym->type.t & VT_IMPORT)
484 other |= 4;
486 #endif
487 if (tcc_state->leading_underscore && can_add_underscore) {
488 buf1[0] = '_';
489 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
490 name = buf1;
492 info = ELFW(ST_INFO)(sym_bind, sym_type);
493 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
494 } else {
495 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
496 esym->st_value = value;
497 esym->st_size = size;
498 esym->st_shndx = sh_num;
502 ST_FUNC void put_extern_sym(Sym *sym, Section *section,
503 unsigned long value, unsigned long size)
505 put_extern_sym2(sym, section, value, size, 1);
508 /* add a new relocation entry to symbol 'sym' in section 's' */
509 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
511 int c = 0;
512 if (sym) {
513 if (0 == sym->c)
514 put_extern_sym(sym, NULL, 0, 0);
515 c = sym->c;
517 /* now we can add ELF relocation info */
518 put_elf_reloc(symtab_section, s, offset, type, c);
521 /********************************************************/
523 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
525 int len;
526 len = strlen(buf);
527 vsnprintf(buf + len, buf_size - len, fmt, ap);
530 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
532 va_list ap;
533 va_start(ap, fmt);
534 strcat_vprintf(buf, buf_size, fmt, ap);
535 va_end(ap);
538 static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
540 char buf[2048];
541 BufferedFile **f;
543 buf[0] = '\0';
544 if (file) {
545 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
546 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
547 (*f)->filename, (*f)->line_num);
548 if (file->line_num > 0) {
549 strcat_printf(buf, sizeof(buf),
550 "%s:%d: ", file->filename, file->line_num);
551 } else {
552 strcat_printf(buf, sizeof(buf),
553 "%s: ", file->filename);
555 } else {
556 strcat_printf(buf, sizeof(buf),
557 "tcc: ");
559 if (is_warning)
560 strcat_printf(buf, sizeof(buf), "warning: ");
561 else
562 strcat_printf(buf, sizeof(buf), "error: ");
563 strcat_vprintf(buf, sizeof(buf), fmt, ap);
565 if (!s1->error_func) {
566 /* default case: stderr */
567 fprintf(stderr, "%s\n", buf);
568 } else {
569 s1->error_func(s1->error_opaque, buf);
571 if (!is_warning || s1->warn_error)
572 s1->nb_errors++;
575 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
576 void (*error_func)(void *opaque, const char *msg))
578 s->error_opaque = error_opaque;
579 s->error_func = error_func;
582 /* error without aborting current compilation */
583 PUB_FUNC void error_noabort(const char *fmt, ...)
585 TCCState *s1 = tcc_state;
586 va_list ap;
588 va_start(ap, fmt);
589 error1(s1, 0, fmt, ap);
590 va_end(ap);
593 PUB_FUNC void error(const char *fmt, ...)
595 TCCState *s1 = tcc_state;
596 va_list ap;
598 va_start(ap, fmt);
599 error1(s1, 0, fmt, ap);
600 va_end(ap);
601 /* better than nothing: in some cases, we accept to handle errors */
602 if (s1->error_set_jmp_enabled) {
603 longjmp(s1->error_jmp_buf, 1);
604 } else {
605 /* XXX: eliminate this someday */
606 exit(1);
610 PUB_FUNC void expect(const char *msg)
612 error("%s expected", msg);
615 PUB_FUNC void warning(const char *fmt, ...)
617 TCCState *s1 = tcc_state;
618 va_list ap;
620 if (s1->warn_none)
621 return;
623 va_start(ap, fmt);
624 error1(s1, 1, fmt, ap);
625 va_end(ap);
628 /********************************************************/
629 /* I/O layer */
631 ST_FUNC BufferedFile *tcc_open(TCCState *s1, const char *filename)
633 int fd;
634 BufferedFile *bf;
636 if (strcmp(filename, "-") == 0)
637 fd = 0, filename = "stdin";
638 else
639 fd = open(filename, O_RDONLY | O_BINARY);
640 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
641 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
642 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
643 if (fd < 0)
644 return NULL;
645 bf = tcc_malloc(sizeof(BufferedFile));
646 bf->fd = fd;
647 bf->buf_ptr = bf->buffer;
648 bf->buf_end = bf->buffer;
649 bf->buffer[0] = CH_EOB; /* put eob symbol */
650 pstrcpy(bf->filename, sizeof(bf->filename), filename);
651 #ifdef _WIN32
652 normalize_slashes(bf->filename);
653 #endif
654 bf->line_num = 1;
655 bf->ifndef_macro = 0;
656 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
657 // printf("opening '%s'\n", filename);
658 return bf;
661 ST_FUNC void tcc_close(BufferedFile *bf)
663 total_lines += bf->line_num;
664 close(bf->fd);
665 tcc_free(bf);
668 /* compile the C file opened in 'file'. Return non zero if errors. */
669 static int tcc_compile(TCCState *s1)
671 Sym *define_start;
672 char buf[512];
673 volatile int section_sym;
675 #ifdef INC_DEBUG
676 printf("%s: **** new file\n", file->filename);
677 #endif
678 preprocess_init(s1);
680 cur_text_section = NULL;
681 funcname = "";
682 anon_sym = SYM_FIRST_ANOM;
684 /* file info: full path + filename */
685 section_sym = 0; /* avoid warning */
686 if (s1->do_debug) {
687 section_sym = put_elf_sym(symtab_section, 0, 0,
688 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
689 text_section->sh_num, NULL);
690 getcwd(buf, sizeof(buf));
691 #ifdef _WIN32
692 normalize_slashes(buf);
693 #endif
694 pstrcat(buf, sizeof(buf), "/");
695 put_stabs_r(buf, N_SO, 0, 0,
696 text_section->data_offset, text_section, section_sym);
697 put_stabs_r(file->filename, N_SO, 0, 0,
698 text_section->data_offset, text_section, section_sym);
700 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
701 symbols can be safely used */
702 put_elf_sym(symtab_section, 0, 0,
703 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
704 SHN_ABS, file->filename);
706 /* define some often used types */
707 int_type.t = VT_INT;
709 char_pointer_type.t = VT_BYTE;
710 mk_pointer(&char_pointer_type);
712 func_old_type.t = VT_FUNC;
713 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
715 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
716 float_type.t = VT_FLOAT;
717 double_type.t = VT_DOUBLE;
719 func_float_type.t = VT_FUNC;
720 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
721 func_double_type.t = VT_FUNC;
722 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
723 #endif
725 #if 0
726 /* define 'void *alloca(unsigned int)' builtin function */
728 Sym *s1;
730 p = anon_sym++;
731 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
732 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
733 s1->next = NULL;
734 sym->next = s1;
735 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
737 #endif
739 define_start = define_stack;
740 nocode_wanted = 1;
742 if (setjmp(s1->error_jmp_buf) == 0) {
743 s1->nb_errors = 0;
744 s1->error_set_jmp_enabled = 1;
746 ch = file->buf_ptr[0];
747 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
748 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
749 next();
750 decl(VT_CONST);
751 if (tok != TOK_EOF)
752 expect("declaration");
754 /* end of translation unit info */
755 if (s1->do_debug) {
756 put_stabs_r(NULL, N_SO, 0, 0,
757 text_section->data_offset, text_section, section_sym);
760 s1->error_set_jmp_enabled = 0;
762 /* reset define stack, but leave -Dsymbols (may be incorrect if
763 they are undefined) */
764 free_defines(define_start);
766 gen_inline_functions();
768 sym_pop(&global_stack, NULL);
769 sym_pop(&local_stack, NULL);
771 return s1->nb_errors != 0 ? -1 : 0;
774 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
776 BufferedFile bf1, *bf = &bf1;
777 int ret, len;
778 char *buf;
780 /* init file structure */
781 bf->fd = -1;
782 /* XXX: avoid copying */
783 len = strlen(str);
784 buf = tcc_malloc(len + 1);
785 if (!buf)
786 return -1;
787 memcpy(buf, str, len);
788 buf[len] = CH_EOB;
789 bf->buf_ptr = buf;
790 bf->buf_end = buf + len;
791 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
792 bf->line_num = 1;
793 file = bf;
794 ret = tcc_compile(s);
795 file = NULL;
796 tcc_free(buf);
798 /* currently, no need to close */
799 return ret;
802 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
803 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
805 BufferedFile bf1, *bf = &bf1;
807 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
808 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
809 /* default value */
810 if (!value)
811 value = "1";
812 pstrcat(bf->buffer, IO_BUF_SIZE, value);
814 /* init file structure */
815 bf->fd = -1;
816 bf->buf_ptr = bf->buffer;
817 bf->buf_end = bf->buffer + strlen(bf->buffer);
818 *bf->buf_end = CH_EOB;
819 bf->filename[0] = '\0';
820 bf->line_num = 1;
821 file = bf;
823 s1->include_stack_ptr = s1->include_stack;
825 /* parse with define parser */
826 ch = file->buf_ptr[0];
827 next_nomacro();
828 parse_define();
829 file = NULL;
832 /* undefine a preprocessor symbol */
833 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
835 TokenSym *ts;
836 Sym *s;
837 ts = tok_alloc(sym, strlen(sym));
838 s = define_find(ts->tok);
839 /* undefine symbol by putting an invalid name */
840 if (s)
841 define_undef(s);
844 static void tcc_cleanup(void)
846 int i, n;
848 if (NULL == tcc_state)
849 return;
850 tcc_state = NULL;
852 /* free -D defines */
853 free_defines(NULL);
855 /* free tokens */
856 n = tok_ident - TOK_IDENT;
857 for(i = 0; i < n; i++)
858 tcc_free(table_ident[i]);
859 tcc_free(table_ident);
861 /* free sym_pools */
862 dynarray_reset(&sym_pools, &nb_sym_pools);
863 /* string buffer */
864 cstr_free(&tokcstr);
865 /* reset symbol stack */
866 sym_free_first = NULL;
867 /* cleanup from error/setjmp */
868 macro_ptr = NULL;
871 LIBTCCAPI TCCState *tcc_new(void)
873 TCCState *s;
874 char buffer[100];
875 int a,b,c;
877 tcc_cleanup();
879 s = tcc_mallocz(sizeof(TCCState));
880 if (!s)
881 return NULL;
882 tcc_state = s;
883 #ifdef _WIN32
884 tcc_set_lib_path_w32(s);
885 #else
886 tcc_set_lib_path(s, CONFIG_TCCDIR);
887 #endif
888 s->output_type = TCC_OUTPUT_MEMORY;
889 preprocess_new();
891 /* we add dummy defines for some special macros to speed up tests
892 and to have working defined() */
893 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
894 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
895 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
896 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
898 /* standard defines */
899 tcc_define_symbol(s, "__STDC__", NULL);
900 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
901 #if defined(TCC_TARGET_I386)
902 tcc_define_symbol(s, "__i386__", NULL);
903 tcc_define_symbol(s, "__i386", NULL);
904 tcc_define_symbol(s, "i386", NULL);
905 #endif
906 #if defined(TCC_TARGET_X86_64)
907 tcc_define_symbol(s, "__x86_64__", NULL);
908 #endif
909 #if defined(TCC_TARGET_ARM)
910 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
911 tcc_define_symbol(s, "__arm_elf__", NULL);
912 tcc_define_symbol(s, "__arm_elf", NULL);
913 tcc_define_symbol(s, "arm_elf", NULL);
914 tcc_define_symbol(s, "__arm__", NULL);
915 tcc_define_symbol(s, "__arm", NULL);
916 tcc_define_symbol(s, "arm", NULL);
917 tcc_define_symbol(s, "__APCS_32__", NULL);
918 #endif
919 #ifdef TCC_TARGET_PE
920 tcc_define_symbol(s, "_WIN32", NULL);
921 #ifdef TCC_TARGET_X86_64
922 tcc_define_symbol(s, "_WIN64", NULL);
923 #endif
924 #else
925 tcc_define_symbol(s, "__unix__", NULL);
926 tcc_define_symbol(s, "__unix", NULL);
927 tcc_define_symbol(s, "unix", NULL);
928 #if defined(__FreeBSD__)
929 #define str(s) #s
930 tcc_define_symbol(s, "__FreeBSD__", str( __FreeBSD__));
931 #undef str
932 #endif
933 #if defined(__linux)
934 tcc_define_symbol(s, "__linux__", NULL);
935 tcc_define_symbol(s, "__linux", NULL);
936 #endif
937 #endif
938 /* tiny C specific defines */
939 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
940 sprintf(buffer, "%d", a*10000 + b*100 + c);
941 tcc_define_symbol(s, "__TINYC__", buffer);
943 /* tiny C & gcc defines */
944 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
945 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
946 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
947 #else
948 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
949 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
950 #endif
952 #ifdef TCC_TARGET_PE
953 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
954 #else
955 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
956 #endif
958 #ifndef TCC_TARGET_PE
959 /* default library paths */
960 # if defined(TCC_TARGET_X86_64_CENTOS)
961 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/lib64");
962 tcc_add_library_path(s, CONFIG_SYSROOT "/lib64");
963 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/local/lib64");
964 # else
965 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/lib");
966 tcc_add_library_path(s, CONFIG_SYSROOT "/lib");
967 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/local/lib");
968 # endif
969 #endif
971 /* no section zero */
972 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
974 /* create standard sections */
975 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
976 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
977 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
979 /* symbols are always generated for linking stage */
980 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
981 ".strtab",
982 ".hashtab", SHF_PRIVATE);
983 strtab_section = symtab_section->link;
985 /* private symbol table for dynamic symbols */
986 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
987 ".dynstrtab",
988 ".dynhashtab", SHF_PRIVATE);
989 s->alacarte_link = 1;
990 s->nocommon = 1;
992 #ifdef CHAR_IS_UNSIGNED
993 s->char_is_unsigned = 1;
994 #endif
995 #if defined(TCC_TARGET_PE) && 0
996 /* XXX: currently the PE linker is not ready to support that */
997 s->leading_underscore = 1;
998 #endif
999 if (s->section_align == 0)
1000 s->section_align = ELF_PAGE_SIZE;
1001 #ifdef TCC_TARGET_I386
1002 s->seg_size = 32;
1003 #endif
1004 return s;
1007 LIBTCCAPI void tcc_delete(TCCState *s1)
1009 int i;
1011 tcc_cleanup();
1013 /* free all sections */
1014 for(i = 1; i < s1->nb_sections; i++)
1015 free_section(s1->sections[i]);
1016 dynarray_reset(&s1->sections, &s1->nb_sections);
1018 for(i = 0; i < s1->nb_priv_sections; i++)
1019 free_section(s1->priv_sections[i]);
1020 dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
1022 /* free any loaded DLLs */
1023 for ( i = 0; i < s1->nb_loaded_dlls; i++) {
1024 DLLReference *ref = s1->loaded_dlls[i];
1025 if ( ref->handle )
1026 dlclose(ref->handle);
1029 /* free loaded dlls array */
1030 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
1032 /* free library paths */
1033 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
1035 /* free include paths */
1036 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
1037 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
1038 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
1040 tcc_free(s1->tcc_lib_path);
1041 tcc_free(s1->runtime_mem);
1042 tcc_free(s1);
1045 LIBTCCAPI int tcc_add_include_path(TCCState *s1, const char *pathname)
1047 char *pathname1;
1049 pathname1 = tcc_strdup(pathname);
1050 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
1051 return 0;
1054 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
1056 char *pathname1;
1058 pathname1 = tcc_strdup(pathname);
1059 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
1060 return 0;
1063 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1065 const char *ext;
1066 ElfW(Ehdr) ehdr;
1067 int fd, ret, size;
1068 BufferedFile *saved_file;
1070 ret = -1;
1072 /* find source file type with extension */
1073 ext = tcc_fileextension(filename);
1074 if (ext[0])
1075 ext++;
1077 /* open the file */
1078 saved_file = file;
1079 file = tcc_open(s1, filename);
1080 if (!file) {
1081 if (flags & AFF_PRINT_ERROR)
1082 error_noabort("file '%s' not found", filename);
1083 goto the_end;
1086 if (flags & AFF_PREPROCESS) {
1087 ret = tcc_preprocess(s1);
1088 goto the_end;
1091 if (!ext[0] || !PATHCMP(ext, "c")) {
1092 /* C file assumed */
1093 ret = tcc_compile(s1);
1094 goto the_end;
1097 #ifdef CONFIG_TCC_ASM
1098 if (!strcmp(ext, "S")) {
1099 /* preprocessed assembler */
1100 ret = tcc_assemble(s1, 1);
1101 goto the_end;
1104 if (!strcmp(ext, "s")) {
1105 /* non preprocessed assembler */
1106 ret = tcc_assemble(s1, 0);
1107 goto the_end;
1109 #endif
1111 fd = file->fd;
1112 /* assume executable format: auto guess file type */
1113 size = read(fd, &ehdr, sizeof(ehdr));
1114 lseek(fd, 0, SEEK_SET);
1115 if (size <= 0) {
1116 error_noabort("could not read header");
1117 goto the_end;
1120 if (size == sizeof(ehdr) &&
1121 ehdr.e_ident[0] == ELFMAG0 &&
1122 ehdr.e_ident[1] == ELFMAG1 &&
1123 ehdr.e_ident[2] == ELFMAG2 &&
1124 ehdr.e_ident[3] == ELFMAG3) {
1126 /* do not display line number if error */
1127 file->line_num = 0;
1128 if (ehdr.e_type == ET_REL) {
1129 ret = tcc_load_object_file(s1, fd, 0);
1130 goto the_end;
1133 #ifndef TCC_TARGET_PE
1134 if (ehdr.e_type == ET_DYN) {
1135 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1136 void *h;
1137 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
1138 if (h)
1139 ret = 0;
1140 } else {
1141 ret = tcc_load_dll(s1, fd, filename,
1142 (flags & AFF_REFERENCED_DLL) != 0);
1144 goto the_end;
1146 #endif
1147 error_noabort("unrecognized ELF file");
1148 goto the_end;
1151 if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
1152 file->line_num = 0; /* do not display line number if error */
1153 ret = tcc_load_archive(s1, fd);
1154 goto the_end;
1157 #ifdef TCC_TARGET_COFF
1158 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
1159 ret = tcc_load_coff(s1, fd);
1160 goto the_end;
1162 #endif
1164 #ifdef TCC_TARGET_PE
1165 ret = pe_load_file(s1, filename, fd);
1166 #else
1167 /* as GNU ld, consider it is an ld script if not recognized */
1168 ret = tcc_load_ldscript(s1);
1169 #endif
1170 if (ret < 0)
1171 error_noabort("unrecognized file type");
1173 the_end:
1174 if (file)
1175 tcc_close(file);
1176 file = saved_file;
1177 return ret;
1180 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1182 if (s->output_type == TCC_OUTPUT_PREPROCESS)
1183 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS);
1184 else
1185 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
1188 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1190 char *pathname1;
1192 pathname1 = tcc_strdup(pathname);
1193 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
1194 return 0;
1197 /* find and load a dll. Return non zero if not found */
1198 /* XXX: add '-rpath' option support ? */
1199 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1201 char buf[1024];
1202 int i;
1204 for(i = 0; i < s->nb_library_paths; i++) {
1205 snprintf(buf, sizeof(buf), "%s/%s",
1206 s->library_paths[i], filename);
1207 if (tcc_add_file_internal(s, buf, flags) == 0)
1208 return 0;
1210 return -1;
1213 /* the library name is the same as the argument of the '-l' option */
1214 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1216 char buf[1024];
1217 int i;
1219 /* first we look for the dynamic library if not static linking */
1220 if (!s->static_link) {
1221 #ifdef TCC_TARGET_PE
1222 if (pe_add_dll(s, libraryname) == 0)
1223 return 0;
1224 #else
1225 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
1226 if (tcc_add_dll(s, buf, 0) == 0)
1227 return 0;
1228 #endif
1230 /* then we look for the static library */
1231 for(i = 0; i < s->nb_library_paths; i++) {
1232 snprintf(buf, sizeof(buf), "%s/lib%s.a",
1233 s->library_paths[i], libraryname);
1234 if (tcc_add_file_internal(s, buf, 0) == 0)
1235 return 0;
1237 return -1;
1240 LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
1242 #ifdef TCC_TARGET_PE
1243 pe_putimport(s, 0, name, val);
1244 #else
1245 add_elf_sym(symtab_section, (uplong)val, 0,
1246 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1247 SHN_ABS, name);
1248 #endif
1249 return 0;
1252 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
1254 char buf[1024];
1256 s->output_type = output_type;
1258 if (!s->nostdinc) {
1259 /* default include paths */
1260 /* XXX: reverse order needed if -isystem support */
1261 #ifndef TCC_TARGET_PE
1262 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/local/include");
1263 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/include");
1264 #endif
1265 snprintf(buf, sizeof(buf), "%s/include", s->tcc_lib_path);
1266 tcc_add_sysinclude_path(s, buf);
1267 #ifdef TCC_TARGET_PE
1268 snprintf(buf, sizeof(buf), "%s/include/winapi", s->tcc_lib_path);
1269 tcc_add_sysinclude_path(s, buf);
1270 #endif
1273 /* if bound checking, then add corresponding sections */
1274 #ifdef CONFIG_TCC_BCHECK
1275 if (s->do_bounds_check) {
1276 /* define symbol */
1277 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
1278 /* create bounds sections */
1279 bounds_section = new_section(s, ".bounds",
1280 SHT_PROGBITS, SHF_ALLOC);
1281 lbounds_section = new_section(s, ".lbounds",
1282 SHT_PROGBITS, SHF_ALLOC);
1284 #endif
1286 if (s->char_is_unsigned) {
1287 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
1290 /* add debug sections */
1291 if (s->do_debug) {
1292 /* stab symbols */
1293 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
1294 stab_section->sh_entsize = sizeof(Stab_Sym);
1295 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
1296 put_elf_str(stabstr_section, "");
1297 stab_section->link = stabstr_section;
1298 /* put first entry */
1299 put_stabs("", 0, 0, 0, 0);
1302 /* add libc crt1/crti objects */
1303 #ifndef TCC_TARGET_PE
1304 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
1305 !s->nostdlib) {
1306 if (output_type != TCC_OUTPUT_DLL)
1307 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
1308 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
1310 #endif
1312 #ifdef TCC_TARGET_PE
1313 snprintf(buf, sizeof(buf), "%s/lib", s->tcc_lib_path);
1314 tcc_add_library_path(s, buf);
1315 #ifdef _WIN32
1316 if (GetSystemDirectory(buf, sizeof buf))
1317 tcc_add_library_path(s, buf);
1318 #endif
1319 #endif
1321 return 0;
1324 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1325 #define FD_INVERT 0x0002 /* invert value before storing */
1327 typedef struct FlagDef {
1328 uint16_t offset;
1329 uint16_t flags;
1330 const char *name;
1331 } FlagDef;
1333 static const FlagDef warning_defs[] = {
1334 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1335 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1336 { offsetof(TCCState, warn_error), 0, "error" },
1337 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1338 "implicit-function-declaration" },
1341 ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
1342 const char *name, int value)
1344 int i;
1345 const FlagDef *p;
1346 const char *r;
1348 r = name;
1349 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
1350 r += 3;
1351 value = !value;
1353 for(i = 0, p = flags; i < nb_flags; i++, p++) {
1354 if (!strcmp(r, p->name))
1355 goto found;
1357 return -1;
1358 found:
1359 if (p->flags & FD_INVERT)
1360 value = !value;
1361 *(int *)((uint8_t *)s + p->offset) = value;
1362 return 0;
1365 /* set/reset a warning */
1366 LIBTCCAPI int tcc_set_warning(TCCState *s, const char *warning_name, int value)
1368 int i;
1369 const FlagDef *p;
1371 if (!strcmp(warning_name, "all")) {
1372 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
1373 if (p->flags & WD_ALL)
1374 *(int *)((uint8_t *)s + p->offset) = 1;
1376 return 0;
1377 } else {
1378 return set_flag(s, warning_defs, countof(warning_defs),
1379 warning_name, value);
1383 static const FlagDef flag_defs[] = {
1384 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1385 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1386 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1387 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1390 /* set/reset a flag */
1391 PUB_FUNC int tcc_set_flag(TCCState *s, const char *flag_name, int value)
1393 return set_flag(s, flag_defs, countof(flag_defs),
1394 flag_name, value);
1398 static int strstart(const char *str, const char *val, char **ptr)
1400 const char *p, *q;
1401 p = str;
1402 q = val;
1403 while (*q != '\0') {
1404 if (*p != *q)
1405 return 0;
1406 p++;
1407 q++;
1409 if (ptr)
1410 *ptr = (char *) p;
1411 return 1;
1414 /* set linker options */
1415 PUB_FUNC const char * tcc_set_linker(TCCState *s, char *option, int multi)
1417 char *p = option;
1418 char *end;
1420 while (option && *option) {
1421 end = NULL;
1422 if (strstart(option, "-Bsymbolic", &p)) {
1423 s->symbolic = TRUE;
1424 #ifdef TCC_TARGET_PE
1425 } else if (strstart(option, "--file-alignment,", &p)) {
1426 s->pe_file_align = strtoul(p, &end, 16);
1427 #endif
1428 } else if (strstart(option, "-fini,", &p)) {
1429 s->fini_symbol = p;
1430 if (s->warn_unsupported)
1431 warning("ignoring -fini %s", p);
1433 } else if (strstart(option, "--image-base,", &p)) {
1434 s->text_addr = strtoul(p, &end, 16);
1435 s->has_text_addr = 1;
1436 } else if (strstart(option, "-init,", &p)) {
1437 s->init_symbol = p;
1438 if (s->warn_unsupported)
1439 warning("ignoring -init %s", p);
1441 } else if (strstart(option, "--oformat,", &p)) {
1442 #if defined(TCC_TARGET_PE)
1443 if (strstart(p, "pe-", NULL)) {
1444 #else
1445 #if defined(TCC_TARGET_X86_64)
1446 if (strstart(p, "elf64-", NULL)) {
1447 #else
1448 if (strstart(p, "elf32-", NULL)) {
1449 #endif
1450 #endif
1451 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1452 } else if (!strcmp(p, "binary")) {
1453 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1454 } else
1455 #ifdef TCC_TARGET_COFF
1456 if (!strcmp(p, "coff")) {
1457 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1458 } else
1459 #endif
1461 return p;
1464 } else if (strstart(option, "-rpath=", &p)) {
1465 s->rpath = p;
1466 } else if (strstart(option, "--section-alignment,", &p)) {
1467 s->section_align = strtoul(p, &end, 16);
1468 } else if (strstart(option, "-soname,", &p)) {
1469 s->soname = p;
1470 multi = 0;
1471 #ifdef TCC_TARGET_PE
1472 } else if (strstart(option, "--subsystem,", &p)) {
1473 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1474 if (!strcmp(p, "native")) {
1475 s->pe_subsystem = 1;
1476 } else if (!strcmp(p, "console")) {
1477 s->pe_subsystem = 3;
1478 } else if (!strcmp(p, "gui")) {
1479 s->pe_subsystem = 2;
1480 } else if (!strcmp(p, "posix")) {
1481 s->pe_subsystem = 7;
1482 } else if (!strcmp(p, "efiapp")) {
1483 s->pe_subsystem = 10;
1484 } else if (!strcmp(p, "efiboot")) {
1485 s->pe_subsystem = 11;
1486 } else if (!strcmp(p, "efiruntime")) {
1487 s->pe_subsystem = 12;
1488 } else if (!strcmp(p, "efirom")) {
1489 s->pe_subsystem = 13;
1490 #elif defined(TCC_TARGET_ARM)
1491 if (!strcmp(p, "wince")) {
1492 s->pe_subsystem = 9;
1493 #endif
1494 } else {
1495 return p;
1497 #endif
1499 } else if (strstart(option, "-Ttext,", &p)) {
1500 s->text_addr = strtoul(p, &end, 16);
1501 s->has_text_addr = 1;
1503 } else {
1504 return option;
1507 if (multi) {
1508 option = NULL;
1509 p = strchr( (end) ? end : p, ',');
1510 if (p) {
1511 *p = 0; /* terminate last option */
1512 option = ++p;
1514 } else
1515 option = NULL;
1517 return NULL;
1520 PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time)
1522 double tt;
1523 tt = (double)total_time / 1000000.0;
1524 if (tt < 0.001)
1525 tt = 0.001;
1526 if (total_bytes < 1)
1527 total_bytes = 1;
1528 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
1529 tok_ident - TOK_IDENT, total_lines, total_bytes,
1530 tt, (int)(total_lines / tt),
1531 total_bytes / tt / 1000000.0);
1534 /* set CONFIG_TCCDIR at runtime */
1535 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1537 tcc_free(s->tcc_lib_path);
1538 s->tcc_lib_path = tcc_strdup(path);
1541 PUB_FUNC void set_num_callers(int n)
1543 #ifdef CONFIG_TCC_BACKTRACE
1544 num_callers = n;
1545 #endif