x86-64: fix flags and zero-pad long doubles
[tinycc.git] / libtcc.c
blob41eab8100386db8e30188943f3244c5c25def95b
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 #ifdef ONE_SOURCE
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 /* ONE_SOURCE */
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 static void tcc_add_systemdir(TCCState *s)
116 char buf[1000];
117 GetSystemDirectory(buf, sizeof buf);
118 tcc_add_library_path(s, buf);
121 #ifndef CONFIG_TCC_STATIC
122 void dlclose(void *p)
124 FreeLibrary((HMODULE)p);
126 #endif
128 #ifdef LIBTCC_AS_DLL
129 BOOL WINAPI DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
131 if (DLL_PROCESS_ATTACH == dwReason)
132 tcc_module = hDll;
133 return TRUE;
135 #endif
136 #endif
138 /********************************************************/
139 /* copy a string and truncate it. */
140 PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
142 char *q, *q_end;
143 int c;
145 if (buf_size > 0) {
146 q = buf;
147 q_end = buf + buf_size - 1;
148 while (q < q_end) {
149 c = *s++;
150 if (c == '\0')
151 break;
152 *q++ = c;
154 *q = '\0';
156 return buf;
159 /* strcat and truncate. */
160 PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
162 int len;
163 len = strlen(buf);
164 if (len < buf_size)
165 pstrcpy(buf + len, buf_size - len, s);
166 return buf;
169 PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num)
171 memcpy(out, in, num);
172 out[num] = '\0';
173 return out;
176 /* extract the basename of a file */
177 PUB_FUNC char *tcc_basename(const char *name)
179 char *p = strchr(name, 0);
180 while (p > name && !IS_DIRSEP(p[-1]))
181 --p;
182 return p;
185 /* extract extension part of a file
187 * (if no extension, return pointer to end-of-string)
189 PUB_FUNC char *tcc_fileextension (const char *name)
191 char *b = tcc_basename(name);
192 char *e = strrchr(b, '.');
193 return e ? e : strchr(b, 0);
196 /********************************************************/
197 /* memory management */
199 #undef free
200 #undef malloc
201 #undef realloc
203 #ifdef MEM_DEBUG
204 int mem_cur_size;
205 int mem_max_size;
206 unsigned malloc_usable_size(void*);
207 #endif
209 PUB_FUNC void tcc_free(void *ptr)
211 #ifdef MEM_DEBUG
212 mem_cur_size -= malloc_usable_size(ptr);
213 #endif
214 free(ptr);
217 PUB_FUNC void *tcc_malloc(unsigned long size)
219 void *ptr;
220 ptr = malloc(size);
221 if (!ptr && size)
222 error("memory full");
223 #ifdef MEM_DEBUG
224 mem_cur_size += malloc_usable_size(ptr);
225 if (mem_cur_size > mem_max_size)
226 mem_max_size = mem_cur_size;
227 #endif
228 return ptr;
231 PUB_FUNC void *tcc_mallocz(unsigned long size)
233 void *ptr;
234 ptr = tcc_malloc(size);
235 memset(ptr, 0, size);
236 return ptr;
239 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
241 void *ptr1;
242 #ifdef MEM_DEBUG
243 mem_cur_size -= malloc_usable_size(ptr);
244 #endif
245 ptr1 = realloc(ptr, size);
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 in use: %d\n", mem_cur_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 PUB_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 if (!pp)
292 error("memory full");
293 *ptab = pp;
295 pp[nb++] = data;
296 *nb_ptr = nb;
299 PUB_FUNC void dynarray_reset(void *pp, int *n)
301 void **p;
302 for (p = *(void***)pp; *n; ++p, --*n)
303 if (*p)
304 tcc_free(*p);
305 tcc_free(*(void**)pp);
306 *(void**)pp = NULL;
309 static void tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char *in)
311 const char *p;
312 do {
313 int c;
314 CString str;
316 cstr_new(&str);
317 for (p = in; c = *p, c != '\0' && c != PATHSEP; ++p) {
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 if (!data)
385 error("memory full");
386 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
387 sec->data = data;
388 sec->data_allocated = size;
391 /* reserve at least 'size' bytes in section 'sec' from
392 sec->data_offset. */
393 ST_FUNC void *section_ptr_add(Section *sec, unsigned long size)
395 unsigned long offset, offset1;
397 offset = sec->data_offset;
398 offset1 = offset + size;
399 if (offset1 > sec->data_allocated)
400 section_realloc(sec, offset1);
401 sec->data_offset = offset1;
402 return sec->data + offset;
405 /* reserve at least 'size' bytes from section start */
406 ST_FUNC void section_reserve(Section *sec, unsigned long size)
408 if (size > sec->data_allocated)
409 section_realloc(sec, size);
410 if (size > sec->data_offset)
411 sec->data_offset = size;
414 /* return a reference to a section, and create it if it does not
415 exists */
416 ST_FUNC Section *find_section(TCCState *s1, const char *name)
418 Section *sec;
419 int i;
420 for(i = 1; i < s1->nb_sections; i++) {
421 sec = s1->sections[i];
422 if (!strcmp(name, sec->name))
423 return sec;
425 /* sections are created as PROGBITS */
426 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
429 /* update sym->c so that it points to an external symbol in section
430 'section' with value 'value' */
431 ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
432 unsigned long value, unsigned long size,
433 int can_add_underscore)
435 int sym_type, sym_bind, sh_num, info, other;
436 ElfW(Sym) *esym;
437 const char *name;
438 char buf1[256];
440 if (section == NULL)
441 sh_num = SHN_UNDEF;
442 else if (section == SECTION_ABS)
443 sh_num = SHN_ABS;
444 else
445 sh_num = section->sh_num;
447 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
448 sym_type = STT_FUNC;
449 } else if ((sym->type.t & VT_BTYPE) == VT_VOID) {
450 sym_type = STT_NOTYPE;
451 } else {
452 sym_type = STT_OBJECT;
455 if (sym->type.t & VT_STATIC)
456 sym_bind = STB_LOCAL;
457 else {
458 if (sym->type.t & VT_WEAK)
459 sym_bind = STB_WEAK;
460 else
461 sym_bind = STB_GLOBAL;
464 if (!sym->c) {
465 name = get_tok_str(sym->v, NULL);
466 #ifdef CONFIG_TCC_BCHECK
467 if (tcc_state->do_bounds_check) {
468 char buf[32];
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 |= 1;
500 if (sym_type == STT_FUNC && sym->type.ref) {
501 int attr = sym->type.ref->r;
502 if (FUNC_EXPORT(attr))
503 other |= 1;
504 if (FUNC_CALL(attr) == FUNC_STDCALL && can_add_underscore) {
505 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr) * PTR_SIZE);
506 name = buf1;
507 other |= 2;
508 can_add_underscore = 0;
510 } else {
511 if (find_elf_sym(tcc_state->dynsymtab_section, name))
512 other |= 4;
513 if (sym->type.t & VT_IMPORT)
514 other |= 4;
516 #endif
517 if (tcc_state->leading_underscore && can_add_underscore) {
518 buf1[0] = '_';
519 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
520 name = buf1;
522 if (sym->asm_label) {
523 name = sym->asm_label;
525 info = ELFW(ST_INFO)(sym_bind, sym_type);
526 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
527 } else {
528 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
529 esym->st_value = value;
530 esym->st_size = size;
531 esym->st_shndx = sh_num;
535 ST_FUNC void put_extern_sym(Sym *sym, Section *section,
536 unsigned long value, unsigned long size)
538 put_extern_sym2(sym, section, value, size, 1);
541 /* add a new relocation entry to symbol 'sym' in section 's' */
542 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
544 int c = 0;
545 if (sym) {
546 if (0 == sym->c)
547 put_extern_sym(sym, NULL, 0, 0);
548 c = sym->c;
550 /* now we can add ELF relocation info */
551 put_elf_reloc(symtab_section, s, offset, type, c);
554 /********************************************************/
556 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
558 int len;
559 len = strlen(buf);
560 vsnprintf(buf + len, buf_size - len, fmt, ap);
563 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
565 va_list ap;
566 va_start(ap, fmt);
567 strcat_vprintf(buf, buf_size, fmt, ap);
568 va_end(ap);
571 static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
573 char buf[2048];
574 BufferedFile **f;
576 buf[0] = '\0';
577 if (file) {
578 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
579 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
580 (*f)->filename, (*f)->line_num);
581 if (file->line_num > 0) {
582 strcat_printf(buf, sizeof(buf),
583 "%s:%d: ", file->filename, file->line_num);
584 } else {
585 strcat_printf(buf, sizeof(buf),
586 "%s: ", file->filename);
588 } else {
589 strcat_printf(buf, sizeof(buf),
590 "tcc: ");
592 if (is_warning)
593 strcat_printf(buf, sizeof(buf), "warning: ");
594 else
595 strcat_printf(buf, sizeof(buf), "error: ");
596 strcat_vprintf(buf, sizeof(buf), fmt, ap);
598 if (!s1->error_func) {
599 /* default case: stderr */
600 fprintf(stderr, "%s\n", buf);
601 } else {
602 s1->error_func(s1->error_opaque, buf);
604 if (!is_warning || s1->warn_error)
605 s1->nb_errors++;
608 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
609 void (*error_func)(void *opaque, const char *msg))
611 s->error_opaque = error_opaque;
612 s->error_func = error_func;
615 /* error without aborting current compilation */
616 PUB_FUNC void error_noabort(const char *fmt, ...)
618 TCCState *s1 = tcc_state;
619 va_list ap;
621 va_start(ap, fmt);
622 error1(s1, 0, fmt, ap);
623 va_end(ap);
626 PUB_FUNC void error(const char *fmt, ...)
628 TCCState *s1 = tcc_state;
629 va_list ap;
631 va_start(ap, fmt);
632 error1(s1, 0, fmt, ap);
633 va_end(ap);
634 /* better than nothing: in some cases, we accept to handle errors */
635 if (s1->error_set_jmp_enabled) {
636 longjmp(s1->error_jmp_buf, 1);
637 } else {
638 /* XXX: eliminate this someday */
639 exit(1);
643 PUB_FUNC void expect(const char *msg)
645 error("%s expected", msg);
648 PUB_FUNC void warning(const char *fmt, ...)
650 TCCState *s1 = tcc_state;
651 va_list ap;
653 if (s1->warn_none)
654 return;
656 va_start(ap, fmt);
657 error1(s1, 1, fmt, ap);
658 va_end(ap);
661 /********************************************************/
662 /* I/O layer */
664 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
666 BufferedFile *bf;
667 int buflen = initlen ? initlen : IO_BUF_SIZE;
669 bf = tcc_malloc(sizeof(BufferedFile) + buflen);
670 bf->buf_ptr = bf->buffer;
671 bf->buf_end = bf->buffer + initlen;
672 bf->buf_end[0] = CH_EOB; /* put eob symbol */
673 pstrcpy(bf->filename, sizeof(bf->filename), filename);
674 #ifdef _WIN32
675 normalize_slashes(bf->filename);
676 #endif
677 bf->line_num = 1;
678 bf->ifndef_macro = 0;
679 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
680 bf->fd = -1;
681 bf->prev = file;
682 file = bf;
685 ST_FUNC void tcc_close(void)
687 BufferedFile *bf = file;
688 if (bf->fd > 0) {
689 close(bf->fd);
690 total_lines += bf->line_num;
692 file = bf->prev;
693 tcc_free(bf);
696 ST_FUNC int tcc_open(TCCState *s1, const char *filename)
698 int fd;
699 if (strcmp(filename, "-") == 0)
700 fd = 0, filename = "stdin";
701 else
702 fd = open(filename, O_RDONLY | O_BINARY);
703 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
704 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
705 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
706 if (fd < 0)
707 return -1;
709 tcc_open_bf(s1, filename, 0);
710 file->fd = fd;
711 return fd;
714 /* compile the C file opened in 'file'. Return non zero if errors. */
715 static int tcc_compile(TCCState *s1)
717 Sym *define_start;
718 SValue *pvtop;
719 char buf[512];
720 volatile int section_sym;
722 #ifdef INC_DEBUG
723 printf("%s: **** new file\n", file->filename);
724 #endif
725 preprocess_init(s1);
727 cur_text_section = NULL;
728 funcname = "";
729 anon_sym = SYM_FIRST_ANOM;
731 /* file info: full path + filename */
732 section_sym = 0; /* avoid warning */
733 if (s1->do_debug) {
734 section_sym = put_elf_sym(symtab_section, 0, 0,
735 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
736 text_section->sh_num, NULL);
737 getcwd(buf, sizeof(buf));
738 #ifdef _WIN32
739 normalize_slashes(buf);
740 #endif
741 pstrcat(buf, sizeof(buf), "/");
742 put_stabs_r(buf, N_SO, 0, 0,
743 text_section->data_offset, text_section, section_sym);
744 put_stabs_r(file->filename, N_SO, 0, 0,
745 text_section->data_offset, text_section, section_sym);
747 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
748 symbols can be safely used */
749 put_elf_sym(symtab_section, 0, 0,
750 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
751 SHN_ABS, file->filename);
753 /* define some often used types */
754 int_type.t = VT_INT;
756 char_pointer_type.t = VT_BYTE;
757 mk_pointer(&char_pointer_type);
759 func_old_type.t = VT_FUNC;
760 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
762 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
763 float_type.t = VT_FLOAT;
764 double_type.t = VT_DOUBLE;
766 func_float_type.t = VT_FUNC;
767 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
768 func_double_type.t = VT_FUNC;
769 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
770 #endif
772 #if 0
773 /* define 'void *alloca(unsigned int)' builtin function */
775 Sym *s1;
777 p = anon_sym++;
778 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
779 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
780 s1->next = NULL;
781 sym->next = s1;
782 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
784 #endif
786 define_start = define_stack;
787 nocode_wanted = 1;
789 if (setjmp(s1->error_jmp_buf) == 0) {
790 s1->nb_errors = 0;
791 s1->error_set_jmp_enabled = 1;
793 ch = file->buf_ptr[0];
794 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
795 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
796 pvtop = vtop;
797 next();
798 decl(VT_CONST);
799 if (tok != TOK_EOF)
800 expect("declaration");
801 if (pvtop != vtop)
802 warning("internal compiler error: vstack leak? (%d)", vtop - pvtop);
804 /* end of translation unit info */
805 if (s1->do_debug) {
806 put_stabs_r(NULL, N_SO, 0, 0,
807 text_section->data_offset, text_section, section_sym);
811 s1->error_set_jmp_enabled = 0;
813 /* reset define stack, but leave -Dsymbols (may be incorrect if
814 they are undefined) */
815 free_defines(define_start);
817 gen_inline_functions();
819 sym_pop(&global_stack, NULL);
820 sym_pop(&local_stack, NULL);
822 return s1->nb_errors != 0 ? -1 : 0;
825 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
827 int len, ret;
828 len = strlen(str);
830 tcc_open_bf(s, "<string>", len);
831 memcpy(file->buffer, str, len);
832 ret = tcc_compile(s);
833 tcc_close();
834 return ret;
837 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
838 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
840 int len1, len2;
841 /* default value */
842 if (!value)
843 value = "1";
844 len1 = strlen(sym);
845 len2 = strlen(value);
847 /* init file structure */
848 tcc_open_bf(s1, "<define>", len1 + len2 + 1);
849 memcpy(file->buffer, sym, len1);
850 file->buffer[len1] = ' ';
851 memcpy(file->buffer + len1 + 1, value, len2);
853 /* parse with define parser */
854 ch = file->buf_ptr[0];
855 next_nomacro();
856 parse_define();
858 tcc_close();
861 /* undefine a preprocessor symbol */
862 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
864 TokenSym *ts;
865 Sym *s;
866 ts = tok_alloc(sym, strlen(sym));
867 s = define_find(ts->tok);
868 /* undefine symbol by putting an invalid name */
869 if (s)
870 define_undef(s);
873 static void tcc_cleanup(void)
875 int i, n;
877 if (NULL == tcc_state)
878 return;
879 tcc_state = NULL;
881 /* free -D defines */
882 free_defines(NULL);
884 /* free tokens */
885 n = tok_ident - TOK_IDENT;
886 for(i = 0; i < n; i++)
887 tcc_free(table_ident[i]);
888 tcc_free(table_ident);
890 /* free sym_pools */
891 dynarray_reset(&sym_pools, &nb_sym_pools);
892 /* string buffer */
893 cstr_free(&tokcstr);
894 /* reset symbol stack */
895 sym_free_first = NULL;
896 /* cleanup from error/setjmp */
897 macro_ptr = NULL;
900 LIBTCCAPI TCCState *tcc_new(void)
902 TCCState *s;
903 char buffer[100];
904 int a,b,c;
906 tcc_cleanup();
908 s = tcc_mallocz(sizeof(TCCState));
909 if (!s)
910 return NULL;
911 tcc_state = s;
912 #ifdef _WIN32
913 tcc_set_lib_path_w32(s);
914 #else
915 tcc_set_lib_path(s, CONFIG_TCCDIR);
916 #endif
917 s->output_type = TCC_OUTPUT_MEMORY;
918 preprocess_new();
919 s->include_stack_ptr = s->include_stack;
921 /* we add dummy defines for some special macros to speed up tests
922 and to have working defined() */
923 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
924 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
925 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
926 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
928 /* standard defines */
929 tcc_define_symbol(s, "__STDC__", NULL);
930 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
931 #if defined(TCC_TARGET_I386)
932 tcc_define_symbol(s, "__i386__", NULL);
933 tcc_define_symbol(s, "__i386", NULL);
934 tcc_define_symbol(s, "i386", NULL);
935 #endif
936 #if defined(TCC_TARGET_X86_64)
937 tcc_define_symbol(s, "__x86_64__", NULL);
938 #endif
939 #if defined(TCC_TARGET_ARM)
940 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
941 tcc_define_symbol(s, "__arm_elf__", NULL);
942 tcc_define_symbol(s, "__arm_elf", NULL);
943 tcc_define_symbol(s, "arm_elf", NULL);
944 tcc_define_symbol(s, "__arm__", NULL);
945 tcc_define_symbol(s, "__arm", NULL);
946 tcc_define_symbol(s, "arm", NULL);
947 tcc_define_symbol(s, "__APCS_32__", NULL);
948 #endif
949 #ifdef TCC_TARGET_PE
950 tcc_define_symbol(s, "_WIN32", NULL);
951 #ifdef TCC_TARGET_X86_64
952 tcc_define_symbol(s, "_WIN64", NULL);
953 #endif
954 #else
955 tcc_define_symbol(s, "__unix__", NULL);
956 tcc_define_symbol(s, "__unix", NULL);
957 tcc_define_symbol(s, "unix", NULL);
958 #if defined(__FreeBSD__)
959 #define str(s) #s
960 tcc_define_symbol(s, "__FreeBSD__", str( __FreeBSD__));
961 #undef str
962 #endif
963 #if defined(__FreeBSD_kernel__)
964 tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
965 #endif
966 #if defined(__linux)
967 tcc_define_symbol(s, "__linux__", NULL);
968 tcc_define_symbol(s, "__linux", NULL);
969 #endif
970 #endif
971 /* tiny C specific defines */
972 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
973 sprintf(buffer, "%d", a*10000 + b*100 + c);
974 tcc_define_symbol(s, "__TINYC__", buffer);
976 /* tiny C & gcc defines */
977 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
978 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
979 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
980 #else
981 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
982 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
983 #endif
985 #ifdef TCC_TARGET_PE
986 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
987 #else
988 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
989 #endif
991 /* glibc defines */
992 tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)", "name proto __asm__ (#alias) __THROW");
994 #ifndef TCC_TARGET_PE
995 /* default library paths */
996 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
997 #endif
999 /* no section zero */
1000 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
1002 /* create standard sections */
1003 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
1004 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
1005 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
1007 /* symbols are always generated for linking stage */
1008 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
1009 ".strtab",
1010 ".hashtab", SHF_PRIVATE);
1011 strtab_section = symtab_section->link;
1013 /* private symbol table for dynamic symbols */
1014 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
1015 ".dynstrtab",
1016 ".dynhashtab", SHF_PRIVATE);
1017 s->alacarte_link = 1;
1018 s->nocommon = 1;
1020 #ifdef CHAR_IS_UNSIGNED
1021 s->char_is_unsigned = 1;
1022 #endif
1023 /* enable this if you want symbols with leading underscore on windows: */
1024 #if defined(TCC_TARGET_PE) && 0
1025 s->leading_underscore = 1;
1026 #endif
1027 if (s->section_align == 0)
1028 s->section_align = ELF_PAGE_SIZE;
1029 #ifdef TCC_TARGET_I386
1030 s->seg_size = 32;
1031 #endif
1032 return s;
1035 LIBTCCAPI void tcc_delete(TCCState *s1)
1037 int i;
1039 tcc_cleanup();
1041 /* free all sections */
1042 for(i = 1; i < s1->nb_sections; i++)
1043 free_section(s1->sections[i]);
1044 dynarray_reset(&s1->sections, &s1->nb_sections);
1046 for(i = 0; i < s1->nb_priv_sections; i++)
1047 free_section(s1->priv_sections[i]);
1048 dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
1050 /* free any loaded DLLs */
1051 for ( i = 0; i < s1->nb_loaded_dlls; i++) {
1052 DLLReference *ref = s1->loaded_dlls[i];
1053 if ( ref->handle )
1054 dlclose(ref->handle);
1057 /* free loaded dlls array */
1058 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
1060 /* free library paths */
1061 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
1063 /* free include paths */
1064 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
1065 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
1066 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
1068 tcc_free(s1->tcc_lib_path);
1070 dynarray_reset(&s1->input_files, &s1->nb_input_files);
1071 dynarray_reset(&s1->input_libs, &s1->nb_input_libs);
1072 dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
1074 #ifdef HAVE_SELINUX
1075 munmap (s1->write_mem, s1->mem_size);
1076 munmap (s1->runtime_mem, s1->mem_size);
1077 #else
1078 tcc_free(s1->runtime_mem);
1079 #endif
1080 tcc_free(s1);
1083 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
1085 tcc_split_path(s, (void ***)&s->include_paths, &s->nb_include_paths, pathname);
1086 return 0;
1089 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
1091 tcc_split_path(s, (void ***)&s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
1092 return 0;
1095 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1097 const char *ext;
1098 ElfW(Ehdr) ehdr;
1099 int fd, ret, size;
1101 /* find source file type with extension */
1102 ext = tcc_fileextension(filename);
1103 if (ext[0])
1104 ext++;
1106 #ifdef CONFIG_TCC_ASM
1107 /* if .S file, define __ASSEMBLER__ like gcc does */
1108 if (!strcmp(ext, "S"))
1109 tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
1110 #endif
1112 /* open the file */
1113 ret = tcc_open(s1, filename);
1114 if (ret < 0) {
1115 if (flags & AFF_PRINT_ERROR)
1116 error_noabort("file '%s' not found", filename);
1117 return ret;
1120 /* update target deps */
1121 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1122 tcc_strdup(filename));
1124 if (flags & AFF_PREPROCESS) {
1125 ret = tcc_preprocess(s1);
1126 goto the_end;
1129 if (!ext[0] || !PATHCMP(ext, "c")) {
1130 /* C file assumed */
1131 ret = tcc_compile(s1);
1132 goto the_end;
1135 #ifdef CONFIG_TCC_ASM
1136 if (!strcmp(ext, "S")) {
1137 /* preprocessed assembler */
1138 ret = tcc_assemble(s1, 1);
1139 goto the_end;
1142 if (!strcmp(ext, "s")) {
1143 /* non preprocessed assembler */
1144 ret = tcc_assemble(s1, 0);
1145 goto the_end;
1147 #endif
1149 fd = file->fd;
1150 /* assume executable format: auto guess file type */
1151 size = read(fd, &ehdr, sizeof(ehdr));
1152 lseek(fd, 0, SEEK_SET);
1153 if (size <= 0) {
1154 error_noabort("could not read header");
1155 goto the_end;
1158 if (size == sizeof(ehdr) &&
1159 ehdr.e_ident[0] == ELFMAG0 &&
1160 ehdr.e_ident[1] == ELFMAG1 &&
1161 ehdr.e_ident[2] == ELFMAG2 &&
1162 ehdr.e_ident[3] == ELFMAG3) {
1164 /* do not display line number if error */
1165 file->line_num = 0;
1166 if (ehdr.e_type == ET_REL) {
1167 ret = tcc_load_object_file(s1, fd, 0);
1168 goto the_end;
1171 #ifndef TCC_TARGET_PE
1172 if (ehdr.e_type == ET_DYN) {
1173 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1174 void *h;
1175 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
1176 if (h)
1177 ret = 0;
1178 } else {
1179 ret = tcc_load_dll(s1, fd, filename,
1180 (flags & AFF_REFERENCED_DLL) != 0);
1182 goto the_end;
1184 #endif
1185 error_noabort("unrecognized ELF file");
1186 goto the_end;
1189 if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
1190 file->line_num = 0; /* do not display line number if error */
1191 ret = tcc_load_archive(s1, fd);
1192 goto the_end;
1195 #ifdef TCC_TARGET_COFF
1196 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
1197 ret = tcc_load_coff(s1, fd);
1198 goto the_end;
1200 #endif
1202 #ifdef TCC_TARGET_PE
1203 ret = pe_load_file(s1, filename, fd);
1204 #else
1205 /* as GNU ld, consider it is an ld script if not recognized */
1206 ret = tcc_load_ldscript(s1);
1207 #endif
1208 if (ret < 0)
1209 error_noabort("unrecognized file type");
1211 the_end:
1212 tcc_close();
1213 return ret;
1216 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1218 dynarray_add((void ***)&s->input_files, &s->nb_input_files, tcc_strdup(filename));
1219 if (s->output_type == TCC_OUTPUT_PREPROCESS)
1220 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS);
1221 else
1222 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
1225 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1227 tcc_split_path(s, (void ***)&s->library_paths, &s->nb_library_paths, pathname);
1228 return 0;
1231 /* find and load a dll. Return non zero if not found */
1232 /* XXX: add '-rpath' option support ? */
1233 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1235 char buf[1024];
1236 int i;
1238 for(i = 0; i < s->nb_library_paths; i++) {
1239 snprintf(buf, sizeof(buf), "%s/%s",
1240 s->library_paths[i], filename);
1241 if (tcc_add_file_internal(s, buf, flags) == 0)
1242 return 0;
1244 return -1;
1247 /* the library name is the same as the argument of the '-l' option */
1248 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1250 char buf[1024];
1251 int i;
1253 dynarray_add((void ***)&s->input_libs, &s->nb_input_libs, tcc_strdup(libraryname));
1255 /* first we look for the dynamic library if not static linking */
1256 if (!s->static_link) {
1257 #ifdef TCC_TARGET_PE
1258 if (pe_add_dll(s, libraryname) == 0)
1259 return 0;
1260 #else
1261 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
1262 if (tcc_add_dll(s, buf, 0) == 0)
1263 return 0;
1264 #endif
1266 /* then we look for the static library */
1267 for(i = 0; i < s->nb_library_paths; i++) {
1268 snprintf(buf, sizeof(buf), "%s/lib%s.a",
1269 s->library_paths[i], libraryname);
1270 if (tcc_add_file_internal(s, buf, 0) == 0)
1271 return 0;
1273 return -1;
1276 LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
1278 #ifdef TCC_TARGET_PE
1279 pe_putimport(s, 0, name, val);
1280 #else
1281 add_elf_sym(symtab_section, (uplong)val, 0,
1282 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1283 SHN_ABS, name);
1284 #endif
1285 return 0;
1288 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
1290 s->output_type = output_type;
1292 if (!s->nostdinc) {
1293 /* default include paths */
1294 /* -isystem paths have already been handled */
1295 tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
1298 /* if bound checking, then add corresponding sections */
1299 #ifdef CONFIG_TCC_BCHECK
1300 if (s->do_bounds_check) {
1301 /* define symbol */
1302 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
1303 /* create bounds sections */
1304 bounds_section = new_section(s, ".bounds",
1305 SHT_PROGBITS, SHF_ALLOC);
1306 lbounds_section = new_section(s, ".lbounds",
1307 SHT_PROGBITS, SHF_ALLOC);
1309 #endif
1311 if (s->char_is_unsigned) {
1312 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
1315 /* add debug sections */
1316 if (s->do_debug) {
1317 /* stab symbols */
1318 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
1319 stab_section->sh_entsize = sizeof(Stab_Sym);
1320 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
1321 put_elf_str(stabstr_section, "");
1322 stab_section->link = stabstr_section;
1323 /* put first entry */
1324 put_stabs("", 0, 0, 0, 0);
1327 #ifdef TCC_TARGET_PE
1328 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
1329 # ifdef _WIN32
1330 tcc_add_systemdir(s);
1331 # endif
1332 #else
1333 /* add libc crt1/crti objects */
1334 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
1335 !s->nostdlib) {
1336 if (output_type != TCC_OUTPUT_DLL)
1337 tcc_add_file(s, TCC_CRTO("crt1.o"));
1338 tcc_add_file(s, TCC_CRTO("crti.o"));
1340 #endif
1341 return 0;
1344 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1345 #define FD_INVERT 0x0002 /* invert value before storing */
1347 typedef struct FlagDef {
1348 uint16_t offset;
1349 uint16_t flags;
1350 const char *name;
1351 } FlagDef;
1353 static const FlagDef warning_defs[] = {
1354 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1355 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1356 { offsetof(TCCState, warn_error), 0, "error" },
1357 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1358 "implicit-function-declaration" },
1361 ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
1362 const char *name, int value)
1364 int i;
1365 const FlagDef *p;
1366 const char *r;
1368 r = name;
1369 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
1370 r += 3;
1371 value = !value;
1373 for(i = 0, p = flags; i < nb_flags; i++, p++) {
1374 if (!strcmp(r, p->name))
1375 goto found;
1377 return -1;
1378 found:
1379 if (p->flags & FD_INVERT)
1380 value = !value;
1381 *(int *)((uint8_t *)s + p->offset) = value;
1382 return 0;
1385 /* set/reset a warning */
1386 LIBTCCAPI int tcc_set_warning(TCCState *s, const char *warning_name, int value)
1388 int i;
1389 const FlagDef *p;
1391 if (!strcmp(warning_name, "all")) {
1392 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
1393 if (p->flags & WD_ALL)
1394 *(int *)((uint8_t *)s + p->offset) = 1;
1396 return 0;
1397 } else {
1398 return set_flag(s, warning_defs, countof(warning_defs),
1399 warning_name, value);
1403 static const FlagDef flag_defs[] = {
1404 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1405 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1406 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1407 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1410 /* set/reset a flag */
1411 PUB_FUNC int tcc_set_flag(TCCState *s, const char *flag_name, int value)
1413 return set_flag(s, flag_defs, countof(flag_defs),
1414 flag_name, value);
1418 static int strstart(const char *str, const char *val, char **ptr)
1420 const char *p, *q;
1421 p = str;
1422 q = val;
1423 while (*q != '\0') {
1424 if (*p != *q)
1425 return 0;
1426 p++;
1427 q++;
1429 if (ptr)
1430 *ptr = (char *) p;
1431 return 1;
1435 /* Like strstart, but automatically takes into account that ld options can
1437 * - start with double or single dash (e.g. '--soname' or '-soname')
1438 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1439 * or '-Wl,-soname=x.so')
1441 * you provide `val` always in 'option[=]' form (no leading -)
1443 static int link_option(const char *str, const char *val, char **ptr)
1445 const char *p, *q;
1447 /* there should be 1 or 2 dashes */
1448 if (*str++ != '-')
1449 return 0;
1450 if (*str == '-')
1451 str++;
1453 /* then str & val should match (potentialy up to '=') */
1454 p = str;
1455 q = val;
1457 while (*q != '\0' && *q != '=') {
1458 if (*p != *q)
1459 return 0;
1460 p++;
1461 q++;
1464 /* '=' near eos means ',' or '=' is ok */
1465 if (*q == '=') {
1466 if (*p != ',' && *p != '=')
1467 return 0;
1468 p++;
1469 q++;
1472 if (ptr)
1473 *ptr = (char *) p;
1474 return 1;
1478 /* set linker options */
1479 PUB_FUNC const char * tcc_set_linker(TCCState *s, char *option, int multi)
1481 char *p = option;
1482 char *end;
1484 while (option && *option) {
1485 end = NULL;
1486 if (link_option(option, "Bsymbolic", &p)) {
1487 s->symbolic = TRUE;
1488 } else if (link_option(option, "fini=", &p)) {
1489 s->fini_symbol = p;
1490 if (s->warn_unsupported)
1491 warning("ignoring -fini %s", p);
1492 } else if (link_option(option, "image-base=", &p)) {
1493 s->text_addr = strtoul(p, &end, 16);
1494 s->has_text_addr = 1;
1495 } else if (link_option(option, "init=", &p)) {
1496 s->init_symbol = p;
1497 if (s->warn_unsupported)
1498 warning("ignoring -init %s", p);
1499 } else if (link_option(option, "oformat=", &p)) {
1500 #if defined(TCC_TARGET_PE)
1501 if (strstart(p, "pe-", NULL)) {
1502 #else
1503 #if defined(TCC_TARGET_X86_64)
1504 if (strstart(p, "elf64-", NULL)) {
1505 #else
1506 if (strstart(p, "elf32-", NULL)) {
1507 #endif
1508 #endif
1509 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1510 } else if (!strcmp(p, "binary")) {
1511 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1512 } else
1513 #ifdef TCC_TARGET_COFF
1514 if (!strcmp(p, "coff")) {
1515 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1516 } else
1517 #endif
1519 return p;
1522 } else if (link_option(option, "rpath=", &p)) {
1523 s->rpath = p;
1524 } else if (link_option(option, "section-alignment=", &p)) {
1525 s->section_align = strtoul(p, &end, 16);
1526 } else if (link_option(option, "soname=", &p)) {
1527 s->soname = p;
1528 multi = 0;
1529 #ifdef TCC_TARGET_PE
1530 } else if (link_option(option, "file-alignment=", &p)) {
1531 s->pe_file_align = strtoul(p, &end, 16);
1532 } else if (link_option(option, "stack=", &p)) {
1533 s->pe_stack_size = strtoul(p, &end, 10);
1534 } else if (link_option(option, "subsystem=", &p)) {
1535 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1536 if (!strcmp(p, "native")) {
1537 s->pe_subsystem = 1;
1538 } else if (!strcmp(p, "console")) {
1539 s->pe_subsystem = 3;
1540 } else if (!strcmp(p, "gui")) {
1541 s->pe_subsystem = 2;
1542 } else if (!strcmp(p, "posix")) {
1543 s->pe_subsystem = 7;
1544 } else if (!strcmp(p, "efiapp")) {
1545 s->pe_subsystem = 10;
1546 } else if (!strcmp(p, "efiboot")) {
1547 s->pe_subsystem = 11;
1548 } else if (!strcmp(p, "efiruntime")) {
1549 s->pe_subsystem = 12;
1550 } else if (!strcmp(p, "efirom")) {
1551 s->pe_subsystem = 13;
1552 #elif defined(TCC_TARGET_ARM)
1553 if (!strcmp(p, "wince")) {
1554 s->pe_subsystem = 9;
1555 #endif
1556 } else {
1557 return p;
1559 #endif
1561 } else if (link_option(option, "Ttext=", &p)) {
1562 s->text_addr = strtoul(p, &end, 16);
1563 s->has_text_addr = 1;
1565 } else {
1566 return option;
1569 if (multi) {
1570 option = NULL;
1571 p = strchr( (end) ? end : p, ',');
1572 if (p) {
1573 *p = 0; /* terminate last option */
1574 option = ++p;
1576 } else
1577 option = NULL;
1579 return NULL;
1582 PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time)
1584 double tt;
1585 tt = (double)total_time / 1000000.0;
1586 if (tt < 0.001)
1587 tt = 0.001;
1588 if (total_bytes < 1)
1589 total_bytes = 1;
1590 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
1591 tok_ident - TOK_IDENT, total_lines, total_bytes,
1592 tt, (int)(total_lines / tt),
1593 total_bytes / tt / 1000000.0);
1596 /* set CONFIG_TCCDIR at runtime */
1597 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1599 tcc_free(s->tcc_lib_path);
1600 s->tcc_lib_path = tcc_strdup(path);
1603 PUB_FUNC void set_num_callers(int n)
1605 #ifdef CONFIG_TCC_BACKTRACE
1606 num_callers = n;
1607 #endif
1611 LIBTCCAPI const char *tcc_default_target(TCCState *s)
1613 /* FIXME will break in multithreaded case */
1614 static char outfile_default[1024];
1616 char *ext;
1617 const char *name =
1618 strcmp(s->input_files[0], "-") == 0 ? "a"
1619 : tcc_basename(s->input_files[0]);
1620 pstrcpy(outfile_default, sizeof(outfile_default), name);
1621 ext = tcc_fileextension(outfile_default);
1622 #ifdef TCC_TARGET_PE
1623 if (s->output_type == TCC_OUTPUT_DLL)
1624 strcpy(ext, ".dll");
1625 else
1626 if (s->output_type == TCC_OUTPUT_EXE)
1627 strcpy(ext, ".exe");
1628 else
1629 #endif
1630 if (( (s->output_type == TCC_OUTPUT_OBJ && !s->reloc_output) ||
1631 (s->output_type == TCC_OUTPUT_PREPROCESS) )
1632 && *ext)
1633 strcpy(ext, ".o");
1634 else
1635 pstrcpy(outfile_default, sizeof(outfile_default), "a.out");
1637 return outfile_default;
1641 LIBTCCAPI void tcc_gen_makedeps(TCCState *s, const char *target, const char *filename)
1643 FILE *depout;
1644 char buf[1024], *ext;
1645 int i;
1647 if (!target)
1648 target = tcc_default_target(s);
1650 if (!filename) {
1651 /* compute filename automatically
1652 * dir/file.o -> dir/file.d */
1653 pstrcpy(buf, sizeof(buf), target);
1654 ext = tcc_fileextension(buf);
1655 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
1656 filename = buf;
1659 if (s->verbose)
1660 printf("<- %s\n", filename);
1662 /* XXX return err codes instead of error() ? */
1663 depout = fopen(filename, "w");
1664 if (!depout)
1665 error("could not open '%s'", filename);
1667 fprintf(depout, "%s : \\\n", target);
1668 for (i=0; i<s->nb_target_deps; ++i)
1669 fprintf(depout, "\t%s \\\n", s->target_deps[i]);
1670 fprintf(depout, "\n");
1671 fclose(depout);