Fix test90 for 32 bits targets
[tinycc.git] / libtcc.c
blobd5a33d56fdd158ba53a892531d9e5bb24f2f1f36
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 #if !defined ONE_SOURCE || ONE_SOURCE
22 #include "tccpp.c"
23 #include "tccgen.c"
24 #include "tccdbg.c"
25 #include "tccasm.c"
26 #include "tccelf.c"
27 #include "tccrun.c"
28 #ifdef TCC_TARGET_I386
29 #include "i386-gen.c"
30 #include "i386-link.c"
31 #include "i386-asm.c"
32 #elif defined(TCC_TARGET_ARM)
33 #include "arm-gen.c"
34 #include "arm-link.c"
35 #include "arm-asm.c"
36 #elif defined(TCC_TARGET_ARM64)
37 #include "arm64-gen.c"
38 #include "arm64-link.c"
39 #include "arm-asm.c"
40 #elif defined(TCC_TARGET_C67)
41 #include "c67-gen.c"
42 #include "c67-link.c"
43 #include "tcccoff.c"
44 #elif defined(TCC_TARGET_X86_64)
45 #include "x86_64-gen.c"
46 #include "x86_64-link.c"
47 #include "i386-asm.c"
48 #elif defined(TCC_TARGET_RISCV64)
49 #include "riscv64-gen.c"
50 #include "riscv64-link.c"
51 #include "riscv64-asm.c"
52 #else
53 #error unknown target
54 #endif
55 #ifdef TCC_TARGET_PE
56 #include "tccpe.c"
57 #endif
58 #ifdef TCC_TARGET_MACHO
59 #include "tccmacho.c"
60 #endif
61 #endif /* ONE_SOURCE */
63 #include "tcc.h"
65 /********************************************************/
66 /* global variables */
68 /* XXX: get rid of this ASAP (or maybe not) */
69 ST_DATA struct TCCState *tcc_state;
70 TCC_SEM(static tcc_compile_sem);
72 #ifdef MEM_DEBUG
73 static int nb_states;
74 #endif
76 /********************************************************/
77 #ifdef _WIN32
78 ST_FUNC char *normalize_slashes(char *path)
80 char *p;
81 for (p = path; *p; ++p)
82 if (*p == '\\')
83 *p = '/';
84 return path;
87 #if defined LIBTCC_AS_DLL && !defined CONFIG_TCCDIR
88 static HMODULE tcc_module;
89 BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
91 if (DLL_PROCESS_ATTACH == dwReason)
92 tcc_module = hDll;
93 return TRUE;
95 #else
96 #define tcc_module NULL /* NULL means executable itself */
97 #endif
99 #ifndef CONFIG_TCCDIR
100 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
101 static inline char *config_tccdir_w32(char *path)
103 char *p;
104 GetModuleFileName(tcc_module, path, MAX_PATH);
105 p = tcc_basename(normalize_slashes(strlwr(path)));
106 if (p > path)
107 --p;
108 *p = 0;
109 return path;
111 #define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))
112 #endif
114 #ifdef TCC_TARGET_PE
115 static void tcc_add_systemdir(TCCState *s)
117 char buf[1000];
118 GetSystemDirectory(buf, sizeof buf);
119 tcc_add_library_path(s, normalize_slashes(buf));
121 #endif
122 #endif
124 /********************************************************/
125 #if CONFIG_TCC_SEMLOCK
126 #if defined _WIN32
127 ST_FUNC void wait_sem(TCCSem *p)
129 if (!p->init)
130 InitializeCriticalSection(&p->cr), p->init = 1;
131 EnterCriticalSection(&p->cr);
133 ST_FUNC void post_sem(TCCSem *p)
135 LeaveCriticalSection(&p->cr);
137 #elif defined __APPLE__
138 /* Half-compatible MacOS doesn't have non-shared (process local)
139 semaphores. Use the dispatch framework for lightweight locks. */
140 ST_FUNC void wait_sem(TCCSem *p)
142 if (!p->init)
143 p->sem = dispatch_semaphore_create(1), p->init = 1;
144 dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
146 ST_FUNC void post_sem(TCCSem *p)
148 dispatch_semaphore_signal(p->sem);
150 #else
151 ST_FUNC void wait_sem(TCCSem *p)
153 if (!p->init)
154 sem_init(&p->sem, 0, 1), p->init = 1;
155 while (sem_wait(&p->sem) < 0 && errno == EINTR);
157 ST_FUNC void post_sem(TCCSem *p)
159 sem_post(&p->sem);
161 #endif
162 #endif
164 PUB_FUNC void tcc_enter_state(TCCState *s1)
166 if (s1->error_set_jmp_enabled)
167 return;
168 WAIT_SEM(&tcc_compile_sem);
169 tcc_state = s1;
172 PUB_FUNC void tcc_exit_state(TCCState *s1)
174 if (s1->error_set_jmp_enabled)
175 return;
176 tcc_state = NULL;
177 POST_SEM(&tcc_compile_sem);
180 /********************************************************/
181 /* copy a string and truncate it. */
182 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
184 char *q, *q_end;
185 int c;
187 if (buf_size > 0) {
188 q = buf;
189 q_end = buf + buf_size - 1;
190 while (q < q_end) {
191 c = *s++;
192 if (c == '\0')
193 break;
194 *q++ = c;
196 *q = '\0';
198 return buf;
201 /* strcat and truncate. */
202 ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s)
204 size_t len;
205 len = strlen(buf);
206 if (len < buf_size)
207 pstrcpy(buf + len, buf_size - len, s);
208 return buf;
211 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
213 memcpy(out, in, num);
214 out[num] = '\0';
215 return out;
218 /* extract the basename of a file */
219 PUB_FUNC char *tcc_basename(const char *name)
221 char *p = strchr(name, 0);
222 while (p > name && !IS_DIRSEP(p[-1]))
223 --p;
224 return p;
227 /* extract extension part of a file
229 * (if no extension, return pointer to end-of-string)
231 PUB_FUNC char *tcc_fileextension (const char *name)
233 char *b = tcc_basename(name);
234 char *e = strrchr(b, '.');
235 return e ? e : strchr(b, 0);
238 ST_FUNC char *tcc_load_text(int fd)
240 int len = lseek(fd, 0, SEEK_END);
241 char *buf = load_data(fd, 0, len + 1);
242 buf[len] = 0;
243 return buf;
246 /********************************************************/
247 /* memory management */
249 #undef free
250 #undef malloc
251 #undef realloc
253 #ifndef MEM_DEBUG
255 PUB_FUNC void tcc_free(void *ptr)
257 free(ptr);
260 PUB_FUNC void *tcc_malloc(unsigned long size)
262 void *ptr;
263 ptr = malloc(size);
264 if (!ptr && size)
265 _tcc_error("memory full (malloc)");
266 return ptr;
269 PUB_FUNC void *tcc_mallocz(unsigned long size)
271 void *ptr;
272 ptr = tcc_malloc(size);
273 if (size)
274 memset(ptr, 0, size);
275 return ptr;
278 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
280 void *ptr1;
281 ptr1 = realloc(ptr, size);
282 if (!ptr1 && size)
283 _tcc_error("memory full (realloc)");
284 return ptr1;
287 PUB_FUNC char *tcc_strdup(const char *str)
289 char *ptr;
290 ptr = tcc_malloc(strlen(str) + 1);
291 strcpy(ptr, str);
292 return ptr;
295 #else
297 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
298 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
299 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
300 #define MEM_DEBUG_FILE_LEN 40
301 #define MEM_DEBUG_CHECK3(header) \
302 ((mem_debug_header_t*)((char*)header + header->size))->magic3
303 #define MEM_USER_PTR(header) \
304 ((char *)header + offsetof(mem_debug_header_t, magic3))
305 #define MEM_HEADER_PTR(ptr) \
306 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
308 struct mem_debug_header {
309 unsigned magic1;
310 unsigned size;
311 struct mem_debug_header *prev;
312 struct mem_debug_header *next;
313 int line_num;
314 char file_name[MEM_DEBUG_FILE_LEN + 1];
315 unsigned magic2;
316 ALIGNED(16) unsigned char magic3[4];
319 typedef struct mem_debug_header mem_debug_header_t;
321 static mem_debug_header_t *mem_debug_chain;
322 static unsigned mem_cur_size;
323 static unsigned mem_max_size;
325 static mem_debug_header_t *malloc_check(void *ptr, const char *msg)
327 mem_debug_header_t * header = MEM_HEADER_PTR(ptr);
328 if (header->magic1 != MEM_DEBUG_MAGIC1 ||
329 header->magic2 != MEM_DEBUG_MAGIC2 ||
330 read32le(MEM_DEBUG_CHECK3(header)) != MEM_DEBUG_MAGIC3 ||
331 header->size == (unsigned)-1) {
332 fprintf(stderr, "%s check failed\n", msg);
333 if (header->magic1 == MEM_DEBUG_MAGIC1)
334 fprintf(stderr, "%s:%u: block allocated here.\n",
335 header->file_name, header->line_num);
336 exit(1);
338 return header;
341 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
343 int ofs;
344 mem_debug_header_t *header;
346 header = malloc(sizeof(mem_debug_header_t) + size);
347 if (!header)
348 _tcc_error("memory full (malloc)");
350 header->magic1 = MEM_DEBUG_MAGIC1;
351 header->magic2 = MEM_DEBUG_MAGIC2;
352 header->size = size;
353 write32le(MEM_DEBUG_CHECK3(header), MEM_DEBUG_MAGIC3);
354 header->line_num = line;
355 ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
356 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
357 header->file_name[MEM_DEBUG_FILE_LEN] = 0;
359 header->next = mem_debug_chain;
360 header->prev = NULL;
361 if (header->next)
362 header->next->prev = header;
363 mem_debug_chain = header;
365 mem_cur_size += size;
366 if (mem_cur_size > mem_max_size)
367 mem_max_size = mem_cur_size;
369 return MEM_USER_PTR(header);
372 PUB_FUNC void tcc_free_debug(void *ptr)
374 mem_debug_header_t *header;
375 if (!ptr)
376 return;
377 header = malloc_check(ptr, "tcc_free");
378 mem_cur_size -= header->size;
379 header->size = (unsigned)-1;
380 if (header->next)
381 header->next->prev = header->prev;
382 if (header->prev)
383 header->prev->next = header->next;
384 if (header == mem_debug_chain)
385 mem_debug_chain = header->next;
386 free(header);
389 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
391 void *ptr;
392 ptr = tcc_malloc_debug(size,file,line);
393 memset(ptr, 0, size);
394 return ptr;
397 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
399 mem_debug_header_t *header;
400 int mem_debug_chain_update = 0;
401 if (!ptr)
402 return tcc_malloc_debug(size, file, line);
403 header = malloc_check(ptr, "tcc_realloc");
404 mem_cur_size -= header->size;
405 mem_debug_chain_update = (header == mem_debug_chain);
406 header = realloc(header, sizeof(mem_debug_header_t) + size);
407 if (!header)
408 _tcc_error("memory full (realloc)");
409 header->size = size;
410 write32le(MEM_DEBUG_CHECK3(header), MEM_DEBUG_MAGIC3);
411 if (header->next)
412 header->next->prev = header;
413 if (header->prev)
414 header->prev->next = header;
415 if (mem_debug_chain_update)
416 mem_debug_chain = header;
417 mem_cur_size += size;
418 if (mem_cur_size > mem_max_size)
419 mem_max_size = mem_cur_size;
420 return MEM_USER_PTR(header);
423 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
425 char *ptr;
426 ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
427 strcpy(ptr, str);
428 return ptr;
431 PUB_FUNC void tcc_memcheck(void)
433 if (mem_cur_size) {
434 mem_debug_header_t *header = mem_debug_chain;
435 fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
436 mem_cur_size, mem_max_size);
437 while (header) {
438 fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
439 header->file_name, header->line_num, header->size);
440 header = header->next;
442 #if MEM_DEBUG-0 == 2
443 exit(2);
444 #endif
447 #endif /* MEM_DEBUG */
449 #define free(p) use_tcc_free(p)
450 #define malloc(s) use_tcc_malloc(s)
451 #define realloc(p, s) use_tcc_realloc(p, s)
453 /********************************************************/
454 /* dynarrays */
456 ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data)
458 int nb, nb_alloc;
459 void **pp;
461 nb = *nb_ptr;
462 pp = *(void ***)ptab;
463 /* every power of two we double array size */
464 if ((nb & (nb - 1)) == 0) {
465 if (!nb)
466 nb_alloc = 1;
467 else
468 nb_alloc = nb * 2;
469 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
470 *(void***)ptab = pp;
472 pp[nb++] = data;
473 *nb_ptr = nb;
476 ST_FUNC void dynarray_reset(void *pp, int *n)
478 void **p;
479 for (p = *(void***)pp; *n; ++p, --*n)
480 if (*p)
481 tcc_free(*p);
482 tcc_free(*(void**)pp);
483 *(void**)pp = NULL;
486 static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in)
488 const char *p;
489 do {
490 int c;
491 CString str;
493 cstr_new(&str);
494 for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) {
495 if (c == '{' && p[1] && p[2] == '}') {
496 c = p[1], p += 2;
497 if (c == 'B')
498 cstr_cat(&str, s->tcc_lib_path, -1);
499 if (c == 'R')
500 cstr_cat(&str, CONFIG_SYSROOT, -1);
501 if (c == 'f' && file) {
502 /* substitute current file's dir */
503 const char *f = file->true_filename;
504 const char *b = tcc_basename(f);
505 if (b > f)
506 cstr_cat(&str, f, b - f - 1);
507 else
508 cstr_cat(&str, ".", 1);
510 } else {
511 cstr_ccat(&str, c);
514 if (str.size) {
515 cstr_ccat(&str, '\0');
516 dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
518 cstr_free(&str);
519 in = p+1;
520 } while (*p);
523 /********************************************************/
524 /* warning / error */
526 /* warn_... option bits */
527 #define WARN_ON 1 /* warning is on (-Woption) */
528 #define WARN_ERR 2 /* warning is an error (-Werror=option) */
529 #define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
531 /* error1() modes */
532 enum { ERROR_WARN, ERROR_NOABORT, ERROR_ERROR };
534 static void error1(int mode, const char *fmt, va_list ap)
536 BufferedFile **pf, *f;
537 TCCState *s1 = tcc_state;
538 CString cs;
540 cstr_new(&cs);
542 if (s1 == NULL)
543 /* can happen only if called from tcc_malloc(): 'out of memory' */
544 goto no_file;
546 tcc_exit_state(s1);
548 if (mode == ERROR_WARN) {
549 if (s1->warn_error)
550 mode = ERROR_ERROR;
551 if (s1->warn_num) {
552 /* handle tcc_warning_c(warn_option)(fmt, ...) */
553 int wopt = *(&s1->warn_none + s1->warn_num);
554 s1->warn_num = 0;
555 if (0 == (wopt & WARN_ON))
556 return;
557 if (wopt & WARN_ERR)
558 mode = ERROR_ERROR;
559 if (wopt & WARN_NOE)
560 mode = ERROR_WARN;
562 if (s1->warn_none)
563 return;
566 f = NULL;
567 if (s1->error_set_jmp_enabled) { /* we're called while parsing a file */
568 /* use upper file if inline ":asm:" or token ":paste:" */
569 for (f = file; f && f->filename[0] == ':'; f = f->prev)
572 if (f) {
573 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
574 cstr_printf(&cs, "In file included from %s:%d:\n",
575 (*pf)->filename, (*pf)->line_num - 1);
576 cstr_printf(&cs, "%s:%d: ",
577 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
578 } else if (s1->current_filename) {
579 cstr_printf(&cs, "%s: ", s1->current_filename);
582 no_file:
583 if (0 == cs.size)
584 cstr_printf(&cs, "tcc: ");
585 cstr_printf(&cs, mode == ERROR_WARN ? "warning: " : "error: ");
586 cstr_vprintf(&cs, fmt, ap);
587 if (!s1 || !s1->error_func) {
588 /* default case: stderr */
589 if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
590 printf("\n"); /* print a newline during tcc -E */
591 fflush(stdout); /* flush -v output */
592 fprintf(stderr, "%s\n", (char*)cs.data);
593 fflush(stderr); /* print error/warning now (win32) */
594 } else {
595 s1->error_func(s1->error_opaque, (char*)cs.data);
597 cstr_free(&cs);
598 if (s1) {
599 if (mode != ERROR_WARN)
600 s1->nb_errors++;
601 if (mode != ERROR_ERROR)
602 return;
603 if (s1->error_set_jmp_enabled)
604 longjmp(s1->error_jmp_buf, 1);
606 exit(1);
609 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func)
611 s->error_opaque = error_opaque;
612 s->error_func = error_func;
615 LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *s)
617 return s->error_func;
620 LIBTCCAPI void *tcc_get_error_opaque(TCCState *s)
622 return s->error_opaque;
625 /* error without aborting current compilation */
626 PUB_FUNC void _tcc_error_noabort(const char *fmt, ...)
628 va_list ap;
629 va_start(ap, fmt);
630 error1(ERROR_NOABORT, fmt, ap);
631 va_end(ap);
634 PUB_FUNC void _tcc_error(const char *fmt, ...)
636 va_list ap;
637 va_start(ap, fmt);
638 for (;;) error1(ERROR_ERROR, fmt, ap);
641 PUB_FUNC void _tcc_warning(const char *fmt, ...)
643 va_list ap;
644 va_start(ap, fmt);
645 error1(ERROR_WARN, fmt, ap);
646 va_end(ap);
649 /********************************************************/
650 /* I/O layer */
652 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
654 BufferedFile *bf;
655 int buflen = initlen ? initlen : IO_BUF_SIZE;
657 bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
658 bf->buf_ptr = bf->buffer;
659 bf->buf_end = bf->buffer + initlen;
660 bf->buf_end[0] = CH_EOB; /* put eob symbol */
661 pstrcpy(bf->filename, sizeof(bf->filename), filename);
662 #ifdef _WIN32
663 normalize_slashes(bf->filename);
664 #endif
665 bf->true_filename = bf->filename;
666 bf->line_num = 1;
667 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
668 bf->fd = -1;
669 bf->prev = file;
670 file = bf;
671 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
674 ST_FUNC void tcc_close(void)
676 TCCState *s1 = tcc_state;
677 BufferedFile *bf = file;
678 if (bf->fd > 0) {
679 close(bf->fd);
680 total_lines += bf->line_num;
682 if (bf->true_filename != bf->filename)
683 tcc_free(bf->true_filename);
684 file = bf->prev;
685 tcc_free(bf);
688 static int _tcc_open(TCCState *s1, const char *filename)
690 int fd;
691 if (strcmp(filename, "-") == 0)
692 fd = 0, filename = "<stdin>";
693 else
694 fd = open(filename, O_RDONLY | O_BINARY);
695 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
696 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
697 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
698 return fd;
701 ST_FUNC int tcc_open(TCCState *s1, const char *filename)
703 int fd = _tcc_open(s1, filename);
704 if (fd < 0)
705 return -1;
706 tcc_open_bf(s1, filename, 0);
707 file->fd = fd;
708 return 0;
711 /* compile the file opened in 'file'. Return non zero if errors. */
712 static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd)
714 /* Here we enter the code section where we use the global variables for
715 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
716 Other threads need to wait until we're done.
718 Alternatively we could use thread local storage for those global
719 variables, which may or may not have advantages */
721 tcc_enter_state(s1);
722 s1->error_set_jmp_enabled = 1;
724 if (setjmp(s1->error_jmp_buf) == 0) {
725 s1->nb_errors = 0;
727 if (fd == -1) {
728 int len = strlen(str);
729 tcc_open_bf(s1, "<string>", len);
730 memcpy(file->buffer, str, len);
731 } else {
732 tcc_open_bf(s1, str, 0);
733 file->fd = fd;
736 preprocess_start(s1, filetype);
737 tccgen_init(s1);
739 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
740 tcc_preprocess(s1);
741 } else {
742 tccelf_begin_file(s1);
743 if (filetype & (AFF_TYPE_ASM | AFF_TYPE_ASMPP)) {
744 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
745 } else {
746 tccgen_compile(s1);
748 tccelf_end_file(s1);
751 tccgen_finish(s1);
752 preprocess_end(s1);
753 s1->error_set_jmp_enabled = 0;
754 tcc_exit_state(s1);
755 return s1->nb_errors != 0 ? -1 : 0;
758 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
760 return tcc_compile(s, s->filetype, str, -1);
763 /* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
764 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
766 const char *eq;
767 if (NULL == (eq = strchr(sym, '=')))
768 eq = strchr(sym, 0);
769 if (NULL == value)
770 value = *eq ? eq + 1 : "1";
771 cstr_printf(&s1->cmdline_defs, "#define %.*s %s\n", (int)(eq-sym), sym, value);
774 /* undefine a preprocessor symbol */
775 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
777 cstr_printf(&s1->cmdline_defs, "#undef %s\n", sym);
781 LIBTCCAPI TCCState *tcc_new(void)
783 TCCState *s;
785 s = tcc_mallocz(sizeof(TCCState));
786 if (!s)
787 return NULL;
788 #ifdef MEM_DEBUG
789 ++nb_states;
790 #endif
792 #undef gnu_ext
794 s->gnu_ext = 1;
795 s->tcc_ext = 1;
796 s->nocommon = 1;
797 s->dollars_in_identifiers = 1; /*on by default like in gcc/clang*/
798 s->cversion = 199901; /* default unless -std=c11 is supplied */
799 s->warn_implicit_function_declaration = 1;
800 s->warn_discarded_qualifiers = 1;
801 s->ms_extensions = 1;
803 #ifdef CHAR_IS_UNSIGNED
804 s->char_is_unsigned = 1;
805 #endif
806 #ifdef TCC_TARGET_I386
807 s->seg_size = 32;
808 #endif
809 /* enable this if you want symbols with leading underscore on windows: */
810 #if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
811 s->leading_underscore = 1;
812 #endif
813 #ifdef TCC_TARGET_ARM
814 s->float_abi = ARM_FLOAT_ABI;
815 #endif
816 #ifdef CONFIG_NEW_DTAGS
817 s->enable_new_dtags = 1;
818 #endif
819 s->ppfp = stdout;
820 /* might be used in error() before preprocess_start() */
821 s->include_stack_ptr = s->include_stack;
823 tcc_set_lib_path(s, CONFIG_TCCDIR);
824 return s;
827 LIBTCCAPI void tcc_delete(TCCState *s1)
829 /* free sections */
830 tccelf_delete(s1);
832 /* free library paths */
833 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
834 dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
836 /* free include paths */
837 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
838 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
840 tcc_free(s1->tcc_lib_path);
841 tcc_free(s1->soname);
842 tcc_free(s1->rpath);
843 tcc_free(s1->elf_entryname);
844 tcc_free(s1->init_symbol);
845 tcc_free(s1->fini_symbol);
846 tcc_free(s1->mapfile);
847 tcc_free(s1->outfile);
848 tcc_free(s1->deps_outfile);
849 #if defined TCC_TARGET_MACHO
850 tcc_free(s1->install_name);
851 #endif
852 dynarray_reset(&s1->files, &s1->nb_files);
853 dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
854 dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
855 dynarray_reset(&s1->argv, &s1->argc);
856 cstr_free(&s1->cmdline_defs);
857 cstr_free(&s1->cmdline_incl);
858 #ifdef TCC_IS_NATIVE
859 /* free runtime memory */
860 tcc_run_free(s1);
861 #endif
862 tcc_free(s1->dState);
863 tcc_free(s1);
864 #ifdef MEM_DEBUG
865 if (0 == --nb_states)
866 tcc_memcheck();
867 #endif
870 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
872 #ifdef CONFIG_TCC_PIE
873 if (output_type == TCC_OUTPUT_EXE)
874 output_type |= TCC_OUTPUT_DYN;
875 #endif
876 s->output_type = output_type;
878 if (!s->nostdinc) {
879 /* default include paths */
880 /* -isystem paths have already been handled */
881 tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
884 if (output_type == TCC_OUTPUT_PREPROCESS) {
885 s->do_debug = 0;
886 return 0;
889 tccelf_new(s);
890 if (s->do_debug) {
891 /* add debug sections */
892 tcc_debug_new(s);
894 #ifdef CONFIG_TCC_BCHECK
895 if (s->do_bounds_check) {
896 /* if bound checking, then add corresponding sections */
897 tccelf_bounds_new(s);
899 #endif
901 if (output_type == TCC_OUTPUT_OBJ) {
902 /* always elf for objects */
903 s->output_format = TCC_OUTPUT_FORMAT_ELF;
904 return 0;
907 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
909 #ifdef TCC_TARGET_PE
910 # ifdef _WIN32
911 /* allow linking with system dll's directly */
912 tcc_add_systemdir(s);
913 # endif
914 /* target PE has its own startup code in libtcc1.a */
915 return 0;
917 #elif defined TCC_TARGET_MACHO
918 # ifdef TCC_IS_NATIVE
919 tcc_add_macos_sdkpath(s);
920 # endif
921 /* Mach-O with LC_MAIN doesn't need any crt startup code. */
922 return 0;
924 #else
925 /* paths for crt objects */
926 tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
928 /* add libc crt1/crti objects */
929 if (output_type != TCC_OUTPUT_MEMORY && !s->nostdlib) {
930 #if TARGETOS_OpenBSD
931 if (output_type != TCC_OUTPUT_DLL)
932 tcc_add_crt(s, "crt0.o");
933 if (output_type == TCC_OUTPUT_DLL)
934 tcc_add_crt(s, "crtbeginS.o");
935 else
936 tcc_add_crt(s, "crtbegin.o");
937 #elif TARGETOS_FreeBSD
938 if (output_type != TCC_OUTPUT_DLL)
939 tcc_add_crt(s, "crt1.o");
940 tcc_add_crt(s, "crti.o");
941 if (s->static_link)
942 tcc_add_crt(s, "crtbeginT.o");
943 else if (output_type & TCC_OUTPUT_DYN)
944 tcc_add_crt(s, "crtbeginS.o");
945 else
946 tcc_add_crt(s, "crtbegin.o");
947 #elif TARGETOS_NetBSD
948 if (output_type != TCC_OUTPUT_DLL)
949 tcc_add_crt(s, "crt0.o");
950 tcc_add_crt(s, "crti.o");
951 if (s->static_link)
952 tcc_add_crt(s, "crtbeginT.o");
953 else if (output_type & TCC_OUTPUT_DYN)
954 tcc_add_crt(s, "crtbeginS.o");
955 else
956 tcc_add_crt(s, "crtbegin.o");
957 #elif defined TARGETOS_ANDROID
958 if (output_type != TCC_OUTPUT_DLL)
959 tcc_add_crt(s, "crtbegin_dynamic.o");
960 else
961 tcc_add_crt(s, "crtbegin_so.o");
962 #else
963 if (output_type != TCC_OUTPUT_DLL)
964 tcc_add_crt(s, "crt1.o");
965 tcc_add_crt(s, "crti.o");
966 #endif
968 return 0;
969 #endif
972 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
974 tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname);
975 return 0;
978 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
980 tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
981 return 0;
984 /* add/update a 'DLLReference', Just find if level == -1 */
985 ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int level)
987 DLLReference *ref = NULL;
988 int i;
989 for (i = 0; i < s1->nb_loaded_dlls; i++)
990 if (0 == strcmp(s1->loaded_dlls[i]->name, dllname)) {
991 ref = s1->loaded_dlls[i];
992 break;
994 if (level == -1)
995 return ref;
996 if (ref) {
997 if (level < ref->level)
998 ref->level = level;
999 ref->found = 1;
1000 return ref;
1002 ref = tcc_mallocz(sizeof(DLLReference) + strlen(dllname));
1003 strcpy(ref->name, dllname);
1004 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, ref);
1005 ref->level = level;
1006 ref->index = s1->nb_loaded_dlls;
1007 return ref;
1010 /* OpenBSD: choose latest from libxxx.so.x.y versions */
1011 #if defined TARGETOS_OpenBSD && !defined _WIN32
1012 #include <glob.h>
1013 static int tcc_glob_so(TCCState *s1, const char *pattern, char *buf, int size)
1015 const char *star;
1016 glob_t g;
1017 char *p;
1018 int i, v, v1, v2, v3;
1020 star = strchr(pattern, '*');
1021 if (!star || glob(pattern, 0, NULL, &g))
1022 return -1;
1023 for (v = -1, i = 0; i < g.gl_pathc; ++i) {
1024 p = g.gl_pathv[i];
1025 if (2 != sscanf(p + (star - pattern), "%d.%d.%d", &v1, &v2, &v3))
1026 continue;
1027 if ((v1 = v1 * 1000 + v2) > v)
1028 v = v1, pstrcpy(buf, size, p);
1030 globfree(&g);
1031 return v;
1033 #endif
1035 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1037 int fd, ret = -1;
1039 #if defined TARGETOS_OpenBSD && !defined _WIN32
1040 char buf[1024];
1041 if (tcc_glob_so(s1, filename, buf, sizeof buf) >= 0)
1042 filename = buf;
1043 #endif
1045 /* ignore binary files with -E */
1046 if (s1->output_type == TCC_OUTPUT_PREPROCESS
1047 && (flags & AFF_TYPE_BIN))
1048 return 0;
1050 /* open the file */
1051 fd = _tcc_open(s1, filename);
1052 if (fd < 0) {
1053 if (flags & AFF_PRINT_ERROR)
1054 tcc_error_noabort("file '%s' not found", filename);
1055 return ret;
1058 s1->current_filename = filename;
1059 if (flags & AFF_TYPE_BIN) {
1060 ElfW(Ehdr) ehdr;
1061 int obj_type;
1063 obj_type = tcc_object_type(fd, &ehdr);
1064 lseek(fd, 0, SEEK_SET);
1066 switch (obj_type) {
1068 case AFF_BINTYPE_REL:
1069 ret = tcc_load_object_file(s1, fd, 0);
1070 break;
1072 case AFF_BINTYPE_AR:
1073 ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
1074 break;
1076 #ifdef TCC_TARGET_PE
1077 default:
1078 ret = pe_load_file(s1, fd, filename);
1079 goto check_success;
1081 #elif defined TCC_TARGET_MACHO
1082 case AFF_BINTYPE_DYN:
1083 case_dyn_or_tbd:
1084 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1085 #ifdef TCC_IS_NATIVE
1086 void* dl;
1087 const char* soname = filename;
1088 if (obj_type != AFF_BINTYPE_DYN)
1089 soname = macho_tbd_soname(filename);
1090 dl = dlopen(soname, RTLD_GLOBAL | RTLD_LAZY);
1091 if (dl)
1092 tcc_add_dllref(s1, soname, 0)->handle = dl, ret = 0;
1093 if (filename != soname)
1094 tcc_free((void *)soname);
1095 #endif
1096 } else if (obj_type == AFF_BINTYPE_DYN) {
1097 ret = macho_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0);
1098 } else {
1099 ret = macho_load_tbd(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0);
1101 break;
1102 default:
1104 const char *ext = tcc_fileextension(filename);
1105 if (!strcmp(ext, ".tbd"))
1106 goto case_dyn_or_tbd;
1107 if (!strcmp(ext, ".dylib")) {
1108 obj_type = AFF_BINTYPE_DYN;
1109 goto case_dyn_or_tbd;
1111 goto check_success;
1114 #else /* unix */
1115 case AFF_BINTYPE_DYN:
1116 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1117 #ifdef TCC_IS_NATIVE
1118 void* dl = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
1119 if (dl)
1120 tcc_add_dllref(s1, filename, 0)->handle = dl, ret = 0;
1121 #endif
1122 } else
1123 ret = tcc_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0);
1124 break;
1126 default:
1127 /* as GNU ld, consider it is an ld script if not recognized */
1128 ret = tcc_load_ldscript(s1, fd);
1129 goto check_success;
1131 #endif /* pe / macos / unix */
1133 check_success:
1134 if (ret < 0)
1135 tcc_error_noabort("%s: unrecognized file type", filename);
1136 break;
1138 #ifdef TCC_TARGET_COFF
1139 case AFF_BINTYPE_C67:
1140 ret = tcc_load_coff(s1, fd);
1141 break;
1142 #endif
1144 close(fd);
1145 } else {
1146 /* update target deps */
1147 dynarray_add(&s1->target_deps, &s1->nb_target_deps, tcc_strdup(filename));
1148 ret = tcc_compile(s1, flags, filename, fd);
1150 s1->current_filename = NULL;
1151 return ret;
1154 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1156 int filetype = s->filetype;
1157 if (0 == (filetype & AFF_TYPE_MASK)) {
1158 /* use a file extension to detect a filetype */
1159 const char *ext = tcc_fileextension(filename);
1160 if (ext[0]) {
1161 ext++;
1162 if (!strcmp(ext, "S"))
1163 filetype = AFF_TYPE_ASMPP;
1164 else if (!strcmp(ext, "s"))
1165 filetype = AFF_TYPE_ASM;
1166 else if (!PATHCMP(ext, "c")
1167 || !PATHCMP(ext, "h")
1168 || !PATHCMP(ext, "i"))
1169 filetype = AFF_TYPE_C;
1170 else
1171 filetype |= AFF_TYPE_BIN;
1172 } else {
1173 filetype = AFF_TYPE_C;
1176 return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
1179 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1181 tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
1182 return 0;
1185 static int tcc_add_library_internal(TCCState *s, const char *fmt,
1186 const char *filename, int flags, char **paths, int nb_paths)
1188 char buf[1024];
1189 int i;
1191 for(i = 0; i < nb_paths; i++) {
1192 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1193 if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
1194 return 0;
1196 return -1;
1199 /* find and load a dll. Return non zero if not found */
1200 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1202 return tcc_add_library_internal(s, "%s/%s", filename, flags,
1203 s->library_paths, s->nb_library_paths);
1206 /* find [cross-]libtcc1.a and tcc helper objects in library path */
1207 ST_FUNC void tcc_add_support(TCCState *s1, const char *filename)
1209 char buf[100];
1210 if (CONFIG_TCC_CROSSPREFIX[0])
1211 filename = strcat(strcpy(buf, CONFIG_TCC_CROSSPREFIX), filename);
1212 if (tcc_add_dll(s1, filename, 0) < 0)
1213 tcc_error_noabort("%s not found", filename);
1216 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
1217 ST_FUNC int tcc_add_crt(TCCState *s1, const char *filename)
1219 if (-1 == tcc_add_library_internal(s1, "%s/%s",
1220 filename, 0, s1->crt_paths, s1->nb_crt_paths))
1221 tcc_error_noabort("file '%s' not found", filename);
1222 return 0;
1224 #endif
1226 /* the library name is the same as the argument of the '-l' option */
1227 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1229 #if defined TCC_TARGET_PE
1230 static const char * const libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
1231 const char * const *pp = s->static_link ? libs + 4 : libs;
1232 #elif defined TCC_TARGET_MACHO
1233 static const char * const libs[] = { "%s/lib%s.dylib", "%s/lib%s.tbd", "%s/lib%s.a", NULL };
1234 const char * const *pp = s->static_link ? libs + 2 : libs;
1235 #elif defined TARGETOS_OpenBSD
1236 static const char * const libs[] = { "%s/lib%s.so.*", "%s/lib%s.a", NULL };
1237 const char * const *pp = s->static_link ? libs + 1 : libs;
1238 #else
1239 static const char * const libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
1240 const char * const *pp = s->static_link ? libs + 1 : libs;
1241 #endif
1242 int flags = s->filetype & AFF_WHOLE_ARCHIVE;
1243 while (*pp) {
1244 if (0 == tcc_add_library_internal(s, *pp,
1245 libraryname, flags, s->library_paths, s->nb_library_paths))
1246 return 0;
1247 ++pp;
1249 return -1;
1252 PUB_FUNC int tcc_add_library_err(TCCState *s1, const char *libname)
1254 int ret = tcc_add_library(s1, libname);
1255 if (ret < 0)
1256 tcc_error_noabort("library '%s' not found", libname);
1257 return ret;
1260 /* handle #pragma comment(lib,) */
1261 ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
1263 int i;
1264 for (i = 0; i < s1->nb_pragma_libs; i++)
1265 tcc_add_library_err(s1, s1->pragma_libs[i]);
1268 LIBTCCAPI int tcc_add_symbol(TCCState *s1, const char *name, const void *val)
1270 #ifdef TCC_TARGET_PE
1271 /* On x86_64 'val' might not be reachable with a 32bit offset.
1272 So it is handled here as if it were in a DLL. */
1273 pe_putimport(s1, 0, name, (uintptr_t)val);
1274 #else
1275 char buf[256];
1276 if (s1->leading_underscore) {
1277 buf[0] = '_';
1278 pstrcpy(buf + 1, sizeof(buf) - 1, name);
1279 name = buf;
1281 set_global_sym(s1, name, NULL, (addr_t)(uintptr_t)val); /* NULL: SHN_ABS */
1282 #endif
1283 return 0;
1286 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1288 tcc_free(s->tcc_lib_path);
1289 s->tcc_lib_path = tcc_strdup(path);
1292 /********************************************************/
1293 /* options parser */
1295 static int strstart(const char *val, const char **str)
1297 const char *p, *q;
1298 p = *str;
1299 q = val;
1300 while (*q) {
1301 if (*p != *q)
1302 return 0;
1303 p++;
1304 q++;
1306 *str = p;
1307 return 1;
1310 /* Like strstart, but automatically takes into account that ld options can
1312 * - start with double or single dash (e.g. '--soname' or '-soname')
1313 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1314 * or '-Wl,-soname=x.so')
1316 * you provide `val` always in 'option[=]' form (no leading -)
1318 static int link_option(const char *str, const char *val, const char **ptr)
1320 const char *p, *q;
1321 int ret;
1323 /* there should be 1 or 2 dashes */
1324 if (*str++ != '-')
1325 return 0;
1326 if (*str == '-')
1327 str++;
1329 /* then str & val should match (potentially up to '=') */
1330 p = str;
1331 q = val;
1333 ret = 1;
1334 if (q[0] == '?') {
1335 ++q;
1336 if (strstart("no-", &p))
1337 ret = -1;
1340 while (*q != '\0' && *q != '=') {
1341 if (*p != *q)
1342 return 0;
1343 p++;
1344 q++;
1347 /* '=' near eos means ',' or '=' is ok */
1348 if (*q == '=') {
1349 if (*p == 0)
1350 *ptr = p;
1351 if (*p != ',' && *p != '=')
1352 return 0;
1353 p++;
1354 } else if (*p) {
1355 return 0;
1357 *ptr = p;
1358 return ret;
1361 static const char *skip_linker_arg(const char **str)
1363 const char *s1 = *str;
1364 const char *s2 = strchr(s1, ',');
1365 *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
1366 return s2;
1369 static void copy_linker_arg(char **pp, const char *s, int sep)
1371 const char *q = s;
1372 char *p = *pp;
1373 int l = 0;
1374 if (p && sep)
1375 p[l = strlen(p)] = sep, ++l;
1376 skip_linker_arg(&q);
1377 pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
1380 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
1382 struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
1383 f->type = filetype;
1384 strcpy(f->name, filename);
1385 dynarray_add(&s->files, &s->nb_files, f);
1388 /* set linker options */
1389 static int tcc_set_linker(TCCState *s, const char *option)
1391 TCCState *s1 = s;
1392 while (*option) {
1394 const char *p = NULL;
1395 char *end = NULL;
1396 int ignoring = 0;
1397 int ret;
1399 if (link_option(option, "Bsymbolic", &p)) {
1400 s->symbolic = 1;
1401 } else if (link_option(option, "nostdlib", &p)) {
1402 s->nostdlib = 1;
1403 } else if (link_option(option, "e=", &p)
1404 || link_option(option, "entry=", &p)) {
1405 copy_linker_arg(&s->elf_entryname, p, 0);
1406 } else if (link_option(option, "fini=", &p)) {
1407 copy_linker_arg(&s->fini_symbol, p, 0);
1408 ignoring = 1;
1409 } else if (link_option(option, "image-base=", &p)
1410 || link_option(option, "Ttext=", &p)) {
1411 s->text_addr = strtoull(p, &end, 16);
1412 s->has_text_addr = 1;
1413 } else if (link_option(option, "init=", &p)) {
1414 copy_linker_arg(&s->init_symbol, p, 0);
1415 ignoring = 1;
1416 } else if (link_option(option, "Map=", &p)) {
1417 copy_linker_arg(&s->mapfile, p, 0);
1418 ignoring = 1;
1419 } else if (link_option(option, "oformat=", &p)) {
1420 #if defined(TCC_TARGET_PE)
1421 if (strstart("pe-", &p)) {
1422 #elif PTR_SIZE == 8
1423 if (strstart("elf64-", &p)) {
1424 #else
1425 if (strstart("elf32-", &p)) {
1426 #endif
1427 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1428 } else if (!strcmp(p, "binary")) {
1429 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1430 #ifdef TCC_TARGET_COFF
1431 } else if (!strcmp(p, "coff")) {
1432 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1433 #endif
1434 } else
1435 goto err;
1437 } else if (link_option(option, "as-needed", &p)) {
1438 ignoring = 1;
1439 } else if (link_option(option, "O", &p)) {
1440 ignoring = 1;
1441 } else if (link_option(option, "export-all-symbols", &p)) {
1442 s->rdynamic = 1;
1443 } else if (link_option(option, "export-dynamic", &p)) {
1444 s->rdynamic = 1;
1445 } else if (link_option(option, "rpath=", &p)) {
1446 copy_linker_arg(&s->rpath, p, ':');
1447 } else if (link_option(option, "enable-new-dtags", &p)) {
1448 s->enable_new_dtags = 1;
1449 } else if (link_option(option, "section-alignment=", &p)) {
1450 s->section_align = strtoul(p, &end, 16);
1451 } else if (link_option(option, "soname=", &p)) {
1452 copy_linker_arg(&s->soname, p, 0);
1453 } else if (link_option(option, "install_name=", &p)) {
1454 copy_linker_arg(&s->soname, p, 0);
1455 #ifdef TCC_TARGET_PE
1456 } else if (link_option(option, "large-address-aware", &p)) {
1457 s->pe_characteristics |= 0x20;
1458 } else if (link_option(option, "file-alignment=", &p)) {
1459 s->pe_file_align = strtoul(p, &end, 16);
1460 } else if (link_option(option, "stack=", &p)) {
1461 s->pe_stack_size = strtoul(p, &end, 10);
1462 } else if (link_option(option, "subsystem=", &p)) {
1463 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1464 if (!strcmp(p, "native")) {
1465 s->pe_subsystem = 1;
1466 } else if (!strcmp(p, "console")) {
1467 s->pe_subsystem = 3;
1468 } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
1469 s->pe_subsystem = 2;
1470 } else if (!strcmp(p, "posix")) {
1471 s->pe_subsystem = 7;
1472 } else if (!strcmp(p, "efiapp")) {
1473 s->pe_subsystem = 10;
1474 } else if (!strcmp(p, "efiboot")) {
1475 s->pe_subsystem = 11;
1476 } else if (!strcmp(p, "efiruntime")) {
1477 s->pe_subsystem = 12;
1478 } else if (!strcmp(p, "efirom")) {
1479 s->pe_subsystem = 13;
1480 #elif defined(TCC_TARGET_ARM)
1481 if (!strcmp(p, "wince")) {
1482 s->pe_subsystem = 9;
1483 #endif
1484 } else
1485 goto err;
1486 #endif
1487 #ifdef TCC_TARGET_MACHO
1488 } else if (link_option(option, "all_load", &p)) {
1489 s->filetype |= AFF_WHOLE_ARCHIVE;
1490 } else if (link_option(option, "force_load", &p)) {
1491 s->filetype |= AFF_WHOLE_ARCHIVE;
1492 args_parser_add_file(s, p, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
1493 s->nb_libraries++;
1494 } else if (link_option(option, "single_module", &p)) {
1495 ignoring = 1;
1496 #endif
1497 } else if (ret = link_option(option, "?whole-archive", &p), ret) {
1498 if (ret > 0)
1499 s->filetype |= AFF_WHOLE_ARCHIVE;
1500 else
1501 s->filetype &= ~AFF_WHOLE_ARCHIVE;
1502 } else if (link_option(option, "z=", &p)) {
1503 ignoring = 1;
1504 } else if (p) {
1505 return 0;
1506 } else {
1507 err:
1508 tcc_error("unsupported linker option '%s'", option);
1510 if (ignoring)
1511 tcc_warning_c(warn_unsupported)("unsupported linker option '%s'", option);
1512 option = skip_linker_arg(&p);
1514 return 1;
1517 typedef struct TCCOption {
1518 const char *name;
1519 uint16_t index;
1520 uint16_t flags;
1521 } TCCOption;
1523 enum {
1524 TCC_OPTION_ignored = 0,
1525 TCC_OPTION_HELP,
1526 TCC_OPTION_HELP2,
1527 TCC_OPTION_v,
1528 TCC_OPTION_I,
1529 TCC_OPTION_D,
1530 TCC_OPTION_U,
1531 TCC_OPTION_P,
1532 TCC_OPTION_L,
1533 TCC_OPTION_B,
1534 TCC_OPTION_l,
1535 TCC_OPTION_bench,
1536 TCC_OPTION_bt,
1537 TCC_OPTION_b,
1538 TCC_OPTION_ba,
1539 TCC_OPTION_g,
1540 TCC_OPTION_c,
1541 TCC_OPTION_dumpversion,
1542 TCC_OPTION_d,
1543 TCC_OPTION_static,
1544 TCC_OPTION_std,
1545 TCC_OPTION_shared,
1546 TCC_OPTION_soname,
1547 TCC_OPTION_o,
1548 TCC_OPTION_r,
1549 TCC_OPTION_Wl,
1550 TCC_OPTION_Wp,
1551 TCC_OPTION_W,
1552 TCC_OPTION_O,
1553 TCC_OPTION_mfloat_abi,
1554 TCC_OPTION_m,
1555 TCC_OPTION_f,
1556 TCC_OPTION_isystem,
1557 TCC_OPTION_iwithprefix,
1558 TCC_OPTION_include,
1559 TCC_OPTION_nostdinc,
1560 TCC_OPTION_nostdlib,
1561 TCC_OPTION_print_search_dirs,
1562 TCC_OPTION_rdynamic,
1563 TCC_OPTION_pthread,
1564 TCC_OPTION_run,
1565 TCC_OPTION_w,
1566 TCC_OPTION_E,
1567 TCC_OPTION_M,
1568 TCC_OPTION_MD,
1569 TCC_OPTION_MF,
1570 TCC_OPTION_MM,
1571 TCC_OPTION_MMD,
1572 TCC_OPTION_x,
1573 TCC_OPTION_ar,
1574 TCC_OPTION_impdef,
1575 TCC_OPTION_dynamiclib,
1576 TCC_OPTION_flat_namespace,
1577 TCC_OPTION_two_levelnamespace,
1578 TCC_OPTION_undefined,
1579 TCC_OPTION_install_name,
1580 TCC_OPTION_compatibility_version ,
1581 TCC_OPTION_current_version,
1584 #define TCC_OPTION_HAS_ARG 0x0001
1585 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1587 static const TCCOption tcc_options[] = {
1588 { "h", TCC_OPTION_HELP, 0 },
1589 { "-help", TCC_OPTION_HELP, 0 },
1590 { "?", TCC_OPTION_HELP, 0 },
1591 { "hh", TCC_OPTION_HELP2, 0 },
1592 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1593 { "-version", TCC_OPTION_v, 0 }, /* handle as verbose, also prints version*/
1594 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
1595 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
1596 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
1597 { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1598 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
1599 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
1600 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG },
1601 { "bench", TCC_OPTION_bench, 0 },
1602 #ifdef CONFIG_TCC_BACKTRACE
1603 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1604 #endif
1605 #ifdef CONFIG_TCC_BCHECK
1606 { "b", TCC_OPTION_b, 0 },
1607 #endif
1608 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1609 #ifdef TCC_TARGET_MACHO
1610 { "compatibility_version", TCC_OPTION_compatibility_version, TCC_OPTION_HAS_ARG },
1611 { "current_version", TCC_OPTION_current_version, TCC_OPTION_HAS_ARG },
1612 #endif
1613 { "c", TCC_OPTION_c, 0 },
1614 #ifdef TCC_TARGET_MACHO
1615 { "dynamiclib", TCC_OPTION_dynamiclib, 0 },
1616 #endif
1617 { "dumpversion", TCC_OPTION_dumpversion, 0},
1618 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1619 { "static", TCC_OPTION_static, 0 },
1620 { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1621 { "shared", TCC_OPTION_shared, 0 },
1622 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
1623 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
1624 { "pthread", TCC_OPTION_pthread, 0},
1625 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1626 { "rdynamic", TCC_OPTION_rdynamic, 0 },
1627 { "r", TCC_OPTION_r, 0 },
1628 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1629 { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1630 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1631 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1632 #ifdef TCC_TARGET_ARM
1633 { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
1634 #endif
1635 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1636 #ifdef TCC_TARGET_MACHO
1637 { "flat_namespace", TCC_OPTION_flat_namespace, 0 },
1638 #endif
1639 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1640 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
1641 { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
1642 { "nostdinc", TCC_OPTION_nostdinc, 0 },
1643 { "nostdlib", TCC_OPTION_nostdlib, 0 },
1644 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
1645 { "w", TCC_OPTION_w, 0 },
1646 { "E", TCC_OPTION_E, 0},
1647 { "M", TCC_OPTION_M, 0},
1648 { "MD", TCC_OPTION_MD, 0},
1649 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
1650 { "MM", TCC_OPTION_MM, 0},
1651 { "MMD", TCC_OPTION_MMD, 0},
1652 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
1653 { "ar", TCC_OPTION_ar, 0},
1654 #ifdef TCC_TARGET_PE
1655 { "impdef", TCC_OPTION_impdef, 0},
1656 #endif
1657 #ifdef TCC_TARGET_MACHO
1658 { "install_name", TCC_OPTION_install_name, TCC_OPTION_HAS_ARG },
1659 { "two_levelnamespace", TCC_OPTION_two_levelnamespace, 0 },
1660 { "undefined", TCC_OPTION_undefined, TCC_OPTION_HAS_ARG },
1661 #endif
1662 /* ignored (silently, except after -Wunsupported) */
1663 { "arch", 0, TCC_OPTION_HAS_ARG},
1664 { "C", 0, 0 },
1665 { "-param", 0, TCC_OPTION_HAS_ARG },
1666 { "pedantic", 0, 0 },
1667 { "pipe", 0, 0 },
1668 { "s", 0, 0 },
1669 { "traditional", 0, 0 },
1670 { NULL, 0, 0 },
1673 typedef struct FlagDef {
1674 uint16_t offset;
1675 uint16_t flags;
1676 const char *name;
1677 } FlagDef;
1679 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1680 #define FD_INVERT 0x0002 /* invert value before storing */
1682 static const FlagDef options_W[] = {
1683 { offsetof(TCCState, warn_all), WD_ALL, "all" },
1684 { offsetof(TCCState, warn_error), 0, "error" },
1685 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1686 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1687 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL, "implicit-function-declaration" },
1688 { offsetof(TCCState, warn_discarded_qualifiers), WD_ALL, "discarded-qualifiers" },
1689 { 0, 0, NULL }
1692 static const FlagDef options_f[] = {
1693 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1694 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1695 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1696 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1697 { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
1698 { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
1699 { offsetof(TCCState, test_coverage), 0, "test-coverage" },
1700 { 0, 0, NULL }
1703 static const FlagDef options_m[] = {
1704 { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
1705 #ifdef TCC_TARGET_X86_64
1706 { offsetof(TCCState, nosse), FD_INVERT, "sse" },
1707 #endif
1708 { 0, 0, NULL }
1711 static int set_flag(TCCState *s, const FlagDef *flags, const char *name)
1713 int value, mask, ret;
1714 const FlagDef *p;
1715 const char *r;
1716 unsigned char *f;
1718 r = name, value = !strstart("no-", &r), mask = 0;
1720 /* when called with options_W, look for -W[no-]error=<option> */
1721 if ((flags->flags & WD_ALL) && strstart("error=", &r))
1722 value = value ? WARN_ON|WARN_ERR : WARN_NOE, mask = WARN_ON;
1724 for (ret = -1, p = flags; p->name; ++p) {
1725 if (ret) {
1726 if (strcmp(r, p->name))
1727 continue;
1728 } else {
1729 if (0 == (p->flags & WD_ALL))
1730 continue;
1733 f = (unsigned char *)s + p->offset;
1734 *f = (*f & mask) | (value ^ !!(p->flags & FD_INVERT));
1736 if (ret) {
1737 ret = 0;
1738 if (strcmp(r, "all"))
1739 break;
1742 return ret;
1745 static int args_parser_make_argv(const char *r, int *argc, char ***argv)
1747 int ret = 0, q, c;
1748 CString str;
1749 for(;;) {
1750 while (c = (unsigned char)*r, c && c <= ' ')
1751 ++r;
1752 if (c == 0)
1753 break;
1754 q = 0;
1755 cstr_new(&str);
1756 while (c = (unsigned char)*r, c) {
1757 ++r;
1758 if (c == '\\' && (*r == '"' || *r == '\\')) {
1759 c = *r++;
1760 } else if (c == '"') {
1761 q = !q;
1762 continue;
1763 } else if (q == 0 && c <= ' ') {
1764 break;
1766 cstr_ccat(&str, c);
1768 cstr_ccat(&str, 0);
1769 //printf("<%s>\n", str.data), fflush(stdout);
1770 dynarray_add(argv, argc, tcc_strdup(str.data));
1771 cstr_free(&str);
1772 ++ret;
1774 return ret;
1777 /* read list file */
1778 static void args_parser_listfile(TCCState *s,
1779 const char *filename, int optind, int *pargc, char ***pargv)
1781 TCCState *s1 = s;
1782 int fd, i;
1783 char *p;
1784 int argc = 0;
1785 char **argv = NULL;
1787 fd = open(filename, O_RDONLY | O_BINARY);
1788 if (fd < 0)
1789 tcc_error("listfile '%s' not found", filename);
1791 p = tcc_load_text(fd);
1792 for (i = 0; i < *pargc; ++i)
1793 if (i == optind)
1794 args_parser_make_argv(p, &argc, &argv);
1795 else
1796 dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
1798 tcc_free(p);
1799 dynarray_reset(&s->argv, &s->argc);
1800 *pargc = s->argc = argc, *pargv = s->argv = argv;
1803 #if defined TCC_TARGET_MACHO
1804 static uint32_t parse_version(TCCState *s1, const char *version)
1806 uint32_t a = 0;
1807 uint32_t b = 0;
1808 uint32_t c = 0;
1809 char* last;
1811 a = strtoul(version, &last, 10);
1812 if (*last == '.') {
1813 b = strtoul(&last[1], &last, 10);
1814 if (*last == '.')
1815 c = strtoul(&last[1], &last, 10);
1817 if (*last || a > 0xffff || b > 0xff || c > 0xff)
1818 tcc_error("version a.b.c not correct: %s", version);
1819 return (a << 16) | (b << 8) | c;
1821 #endif
1823 PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
1825 TCCState *s1 = s;
1826 const TCCOption *popt;
1827 const char *optarg, *r;
1828 const char *run = NULL;
1829 int x;
1830 CString linker_arg; /* collect -Wl options */
1831 int tool = 0, arg_start = 0, noaction = optind;
1832 char **argv = *pargv;
1833 int argc = *pargc;
1835 cstr_new(&linker_arg);
1837 while (optind < argc) {
1838 r = argv[optind];
1839 if (r[0] == '@' && r[1] != '\0') {
1840 args_parser_listfile(s, r + 1, optind, &argc, &argv);
1841 continue;
1843 optind++;
1844 if (tool) {
1845 if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
1846 ++s->verbose;
1847 continue;
1849 reparse:
1850 if (r[0] != '-' || r[1] == '\0') {
1851 if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1852 args_parser_add_file(s, r, s->filetype);
1853 if (run) {
1854 tcc_set_options(s, run);
1855 arg_start = optind - 1;
1856 break;
1858 continue;
1861 /* find option in table */
1862 for(popt = tcc_options; ; ++popt) {
1863 const char *p1 = popt->name;
1864 const char *r1 = r + 1;
1865 if (p1 == NULL)
1866 tcc_error("invalid option -- '%s'", r);
1867 if (!strstart(p1, &r1))
1868 continue;
1869 optarg = r1;
1870 if (popt->flags & TCC_OPTION_HAS_ARG) {
1871 if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
1872 if (optind >= argc)
1873 arg_err:
1874 tcc_error("argument to '%s' is missing", r);
1875 optarg = argv[optind++];
1877 } else if (*r1 != '\0')
1878 continue;
1879 break;
1882 switch(popt->index) {
1883 case TCC_OPTION_HELP:
1884 x = OPT_HELP;
1885 goto extra_action;
1886 case TCC_OPTION_HELP2:
1887 x = OPT_HELP2;
1888 goto extra_action;
1889 case TCC_OPTION_I:
1890 tcc_add_include_path(s, optarg);
1891 break;
1892 case TCC_OPTION_D:
1893 tcc_define_symbol(s, optarg, NULL);
1894 break;
1895 case TCC_OPTION_U:
1896 tcc_undefine_symbol(s, optarg);
1897 break;
1898 case TCC_OPTION_L:
1899 tcc_add_library_path(s, optarg);
1900 break;
1901 case TCC_OPTION_B:
1902 /* set tcc utilities path (mainly for tcc development) */
1903 tcc_set_lib_path(s, optarg);
1904 ++noaction;
1905 break;
1906 case TCC_OPTION_l:
1907 args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
1908 s->nb_libraries++;
1909 break;
1910 case TCC_OPTION_pthread:
1911 s->option_pthread = 1;
1912 break;
1913 case TCC_OPTION_bench:
1914 s->do_bench = 1;
1915 break;
1916 #ifdef CONFIG_TCC_BACKTRACE
1917 case TCC_OPTION_bt:
1918 s->rt_num_callers = atoi(optarg);
1919 s->do_backtrace = 1;
1920 s->do_debug = 1;
1921 s->dwarf = DWARF_VERSION;
1922 break;
1923 #endif
1924 #ifdef CONFIG_TCC_BCHECK
1925 case TCC_OPTION_b:
1926 s->do_bounds_check = 1;
1927 s->do_backtrace = 1;
1928 s->do_debug = 1;
1929 s->dwarf = DWARF_VERSION;
1930 break;
1931 #endif
1932 case TCC_OPTION_g:
1933 s->do_debug = 1;
1934 s->dwarf = DWARF_VERSION;
1936 if (strstart("dwarf", &optarg))
1937 s->dwarf = (*optarg) ? (0 - atoi(optarg)) : DEFAULT_DWARF_VERSION;
1938 break;
1939 case TCC_OPTION_c:
1940 x = TCC_OUTPUT_OBJ;
1941 set_output_type:
1942 if (s->output_type)
1943 tcc_warning("-%s: overriding compiler action already specified", popt->name);
1944 s->output_type = x;
1945 break;
1946 case TCC_OPTION_d:
1947 if (*optarg == 'D')
1948 s->dflag = 3;
1949 else if (*optarg == 'M')
1950 s->dflag = 7;
1951 else if (*optarg == 't')
1952 s->dflag = 16;
1953 else if (isnum(*optarg))
1954 s->g_debug |= atoi(optarg);
1955 else
1956 goto unsupported_option;
1957 break;
1958 case TCC_OPTION_static:
1959 s->static_link = 1;
1960 break;
1961 case TCC_OPTION_std:
1962 if (strcmp(optarg, "=c11") == 0)
1963 s->cversion = 201112;
1964 break;
1965 case TCC_OPTION_shared:
1966 x = TCC_OUTPUT_DLL;
1967 goto set_output_type;
1968 case TCC_OPTION_soname:
1969 s->soname = tcc_strdup(optarg);
1970 break;
1971 case TCC_OPTION_o:
1972 if (s->outfile) {
1973 tcc_warning("multiple -o option");
1974 tcc_free(s->outfile);
1976 s->outfile = tcc_strdup(optarg);
1977 break;
1978 case TCC_OPTION_r:
1979 /* generate a .o merging several output files */
1980 s->option_r = 1;
1981 x = TCC_OUTPUT_OBJ;
1982 goto set_output_type;
1983 case TCC_OPTION_isystem:
1984 tcc_add_sysinclude_path(s, optarg);
1985 break;
1986 case TCC_OPTION_include:
1987 cstr_printf(&s->cmdline_incl, "#include \"%s\"\n", optarg);
1988 break;
1989 case TCC_OPTION_nostdinc:
1990 s->nostdinc = 1;
1991 break;
1992 case TCC_OPTION_nostdlib:
1993 s->nostdlib = 1;
1994 break;
1995 case TCC_OPTION_run:
1996 #ifndef TCC_IS_NATIVE
1997 tcc_error("-run is not available in a cross compiler");
1998 #endif
1999 run = optarg;
2000 x = TCC_OUTPUT_MEMORY;
2001 goto set_output_type;
2002 case TCC_OPTION_v:
2003 do ++s->verbose; while (*optarg++ == 'v');
2004 ++noaction;
2005 break;
2006 case TCC_OPTION_f:
2007 if (set_flag(s, options_f, optarg) < 0)
2008 goto unsupported_option;
2009 break;
2010 #ifdef TCC_TARGET_ARM
2011 case TCC_OPTION_mfloat_abi:
2012 /* tcc doesn't support soft float yet */
2013 if (!strcmp(optarg, "softfp")) {
2014 s->float_abi = ARM_SOFTFP_FLOAT;
2015 } else if (!strcmp(optarg, "hard"))
2016 s->float_abi = ARM_HARD_FLOAT;
2017 else
2018 tcc_error("unsupported float abi '%s'", optarg);
2019 break;
2020 #endif
2021 case TCC_OPTION_m:
2022 if (set_flag(s, options_m, optarg) < 0) {
2023 if (x = atoi(optarg), x != 32 && x != 64)
2024 goto unsupported_option;
2025 if (PTR_SIZE != x/8)
2026 return x;
2027 ++noaction;
2029 break;
2030 case TCC_OPTION_W:
2031 s->warn_none = 0;
2032 if (optarg[0] && set_flag(s, options_W, optarg) < 0)
2033 goto unsupported_option;
2034 break;
2035 case TCC_OPTION_w:
2036 s->warn_none = 1;
2037 break;
2038 case TCC_OPTION_rdynamic:
2039 s->rdynamic = 1;
2040 break;
2041 case TCC_OPTION_Wl:
2042 if (linker_arg.size)
2043 --linker_arg.size, cstr_ccat(&linker_arg, ',');
2044 cstr_cat(&linker_arg, optarg, 0);
2045 if (tcc_set_linker(s, linker_arg.data))
2046 cstr_free(&linker_arg);
2047 break;
2048 case TCC_OPTION_Wp:
2049 r = optarg;
2050 goto reparse;
2051 case TCC_OPTION_E:
2052 x = TCC_OUTPUT_PREPROCESS;
2053 goto set_output_type;
2054 case TCC_OPTION_P:
2055 s->Pflag = atoi(optarg) + 1;
2056 break;
2057 case TCC_OPTION_M:
2058 s->include_sys_deps = 1;
2059 // fall through
2060 case TCC_OPTION_MM:
2061 s->just_deps = 1;
2062 if(!s->deps_outfile)
2063 s->deps_outfile = tcc_strdup("-");
2064 // fall through
2065 case TCC_OPTION_MMD:
2066 s->gen_deps = 1;
2067 break;
2068 case TCC_OPTION_MD:
2069 s->gen_deps = 1;
2070 s->include_sys_deps = 1;
2071 break;
2072 case TCC_OPTION_MF:
2073 s->deps_outfile = tcc_strdup(optarg);
2074 break;
2075 case TCC_OPTION_dumpversion:
2076 printf ("%s\n", TCC_VERSION);
2077 exit(0);
2078 break;
2079 case TCC_OPTION_x:
2080 x = 0;
2081 if (*optarg == 'c')
2082 x = AFF_TYPE_C;
2083 else if (*optarg == 'a')
2084 x = AFF_TYPE_ASMPP;
2085 else if (*optarg == 'b')
2086 x = AFF_TYPE_BIN;
2087 else if (*optarg == 'n')
2088 x = AFF_TYPE_NONE;
2089 else
2090 tcc_warning("unsupported language '%s'", optarg);
2091 s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
2092 break;
2093 case TCC_OPTION_O:
2094 s->optimize = atoi(optarg);
2095 break;
2096 case TCC_OPTION_print_search_dirs:
2097 x = OPT_PRINT_DIRS;
2098 goto extra_action;
2099 case TCC_OPTION_impdef:
2100 x = OPT_IMPDEF;
2101 goto extra_action;
2102 #if defined TCC_TARGET_MACHO
2103 case TCC_OPTION_dynamiclib:
2104 x = TCC_OUTPUT_DLL;
2105 goto set_output_type;
2106 case TCC_OPTION_flat_namespace:
2107 break;
2108 case TCC_OPTION_two_levelnamespace:
2109 break;
2110 case TCC_OPTION_undefined:
2111 break;
2112 case TCC_OPTION_install_name:
2113 s->install_name = tcc_strdup(optarg);
2114 break;
2115 case TCC_OPTION_compatibility_version:
2116 s->compatibility_version = parse_version(s, optarg);
2117 break;
2118 case TCC_OPTION_current_version:
2119 s->current_version = parse_version(s, optarg);;
2120 break;
2121 #endif
2122 case TCC_OPTION_ar:
2123 x = OPT_AR;
2124 extra_action:
2125 arg_start = optind - 1;
2126 if (arg_start != noaction)
2127 tcc_error("cannot parse %s here", r);
2128 tool = x;
2129 break;
2130 default:
2131 unsupported_option:
2132 tcc_warning_c(warn_unsupported)("unsupported option '%s'", r);
2133 break;
2136 if (linker_arg.size) {
2137 r = linker_arg.data;
2138 goto arg_err;
2140 *pargc = argc - arg_start;
2141 *pargv = argv + arg_start;
2142 if (tool)
2143 return tool;
2144 if (optind != noaction)
2145 return 0;
2146 if (s->verbose == 2)
2147 return OPT_PRINT_DIRS;
2148 if (s->verbose)
2149 return OPT_V;
2150 return OPT_HELP;
2153 LIBTCCAPI void tcc_set_options(TCCState *s, const char *r)
2155 char **argv = NULL;
2156 int argc = 0;
2157 args_parser_make_argv(r, &argc, &argv);
2158 tcc_parse_args(s, &argc, &argv, 0);
2159 dynarray_reset(&argv, &argc);
2162 PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time)
2164 if (total_time < 1)
2165 total_time = 1;
2166 if (total_bytes < 1)
2167 total_bytes = 1;
2168 fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
2169 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2170 total_idents, total_lines, total_bytes,
2171 (double)total_time/1000,
2172 (unsigned)total_lines*1000/total_time,
2173 (double)total_bytes/1000/total_time);
2174 fprintf(stderr, "* text %u, data.rw %u, data.ro %u, bss %u bytes\n",
2175 s1->total_output[0],
2176 s1->total_output[1],
2177 s1->total_output[2],
2178 s1->total_output[3]
2180 #ifdef MEM_DEBUG
2181 fprintf(stderr, "* %d bytes memory used\n", mem_max_size);
2182 #endif