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
27 #ifdef TCC_TARGET_I386
29 #include "i386-link.c"
31 #elif defined(TCC_TARGET_ARM)
35 #elif defined(TCC_TARGET_ARM64)
36 #include "arm64-gen.c"
37 #include "arm64-link.c"
39 #elif defined(TCC_TARGET_C67)
43 #elif defined(TCC_TARGET_X86_64)
44 #include "x86_64-gen.c"
45 #include "x86_64-link.c"
47 #elif defined(TCC_TARGET_RISCV64)
48 #include "riscv64-gen.c"
49 #include "riscv64-link.c"
50 #include "riscv64-asm.c"
57 #ifdef TCC_TARGET_MACHO
60 #endif /* ONE_SOURCE */
64 /********************************************************/
65 /* global variables */
67 /* XXX: get rid of this ASAP (or maybe not) */
68 ST_DATA
struct TCCState
*tcc_state
;
69 TCC_SEM(static tcc_compile_sem
);
75 /********************************************************/
77 ST_FUNC
char *normalize_slashes(char *path
)
80 for (p
= path
; *p
; ++p
)
86 #if defined LIBTCC_AS_DLL && !defined CONFIG_TCCDIR
87 static HMODULE tcc_module
;
88 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
90 if (DLL_PROCESS_ATTACH
== dwReason
)
95 #define tcc_module NULL /* NULL means executable itself */
99 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
100 static inline char *config_tccdir_w32(char *path
)
103 GetModuleFileName(tcc_module
, path
, MAX_PATH
);
104 p
= tcc_basename(normalize_slashes(strlwr(path
)));
110 #define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))
114 static void tcc_add_systemdir(TCCState
*s
)
117 GetSystemDirectory(buf
, sizeof buf
);
118 tcc_add_library_path(s
, normalize_slashes(buf
));
123 /********************************************************/
124 #if CONFIG_TCC_SEMLOCK
126 ST_FUNC
void wait_sem(TCCSem
*p
)
129 InitializeCriticalSection(&p
->cr
), p
->init
= 1;
130 EnterCriticalSection(&p
->cr
);
132 ST_FUNC
void post_sem(TCCSem
*p
)
134 LeaveCriticalSection(&p
->cr
);
136 #elif defined __APPLE__
137 /* Half-compatible MacOS doesn't have non-shared (process local)
138 semaphores. Use the dispatch framework for lightweight locks. */
139 ST_FUNC
void wait_sem(TCCSem
*p
)
142 p
->sem
= dispatch_semaphore_create(1), p
->init
= 1;
143 dispatch_semaphore_wait(p
->sem
, DISPATCH_TIME_FOREVER
);
145 ST_FUNC
void post_sem(TCCSem
*p
)
147 dispatch_semaphore_signal(p
->sem
);
150 ST_FUNC
void wait_sem(TCCSem
*p
)
153 sem_init(&p
->sem
, 0, 1), p
->init
= 1;
154 while (sem_wait(&p
->sem
) < 0 && errno
== EINTR
);
156 ST_FUNC
void post_sem(TCCSem
*p
)
163 PUB_FUNC
void tcc_enter_state(TCCState
*s1
)
165 if (s1
->error_set_jmp_enabled
)
167 WAIT_SEM(&tcc_compile_sem
);
171 PUB_FUNC
void tcc_exit_state(TCCState
*s1
)
173 if (s1
->error_set_jmp_enabled
)
176 POST_SEM(&tcc_compile_sem
);
179 /********************************************************/
180 /* copy a string and truncate it. */
181 ST_FUNC
char *pstrcpy(char *buf
, size_t buf_size
, const char *s
)
188 q_end
= buf
+ buf_size
- 1;
200 /* strcat and truncate. */
201 ST_FUNC
char *pstrcat(char *buf
, size_t buf_size
, const char *s
)
206 pstrcpy(buf
+ len
, buf_size
- len
, s
);
210 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
212 memcpy(out
, in
, num
);
217 /* extract the basename of a file */
218 PUB_FUNC
char *tcc_basename(const char *name
)
220 char *p
= strchr(name
, 0);
221 while (p
> name
&& !IS_DIRSEP(p
[-1]))
226 /* extract extension part of a file
228 * (if no extension, return pointer to end-of-string)
230 PUB_FUNC
char *tcc_fileextension (const char *name
)
232 char *b
= tcc_basename(name
);
233 char *e
= strrchr(b
, '.');
234 return e
? e
: strchr(b
, 0);
237 ST_FUNC
char *tcc_load_text(int fd
)
239 int len
= lseek(fd
, 0, SEEK_END
);
240 char *buf
= load_data(fd
, 0, len
+ 1);
245 /********************************************************/
246 /* memory management */
254 PUB_FUNC
void tcc_free(void *ptr
)
259 PUB_FUNC
void *tcc_malloc(unsigned long size
)
264 _tcc_error("memory full (malloc)");
268 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
271 ptr
= tcc_malloc(size
);
273 memset(ptr
, 0, size
);
277 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
280 ptr1
= realloc(ptr
, size
);
282 _tcc_error("memory full (realloc)");
286 PUB_FUNC
char *tcc_strdup(const char *str
)
289 ptr
= tcc_malloc(strlen(str
) + 1);
296 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
297 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
298 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
299 #define MEM_DEBUG_FILE_LEN 40
300 #define MEM_DEBUG_CHECK3(header) \
301 ((mem_debug_header_t*)((char*)header + header->size))->magic3
302 #define MEM_USER_PTR(header) \
303 ((char *)header + offsetof(mem_debug_header_t, magic3))
304 #define MEM_HEADER_PTR(ptr) \
305 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
307 struct mem_debug_header
{
310 struct mem_debug_header
*prev
;
311 struct mem_debug_header
*next
;
313 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
315 ALIGNED(16) unsigned char magic3
[4];
318 typedef struct mem_debug_header mem_debug_header_t
;
320 static mem_debug_header_t
*mem_debug_chain
;
321 static unsigned mem_cur_size
;
322 static unsigned mem_max_size
;
324 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
326 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
327 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
328 header
->magic2
!= MEM_DEBUG_MAGIC2
||
329 read32le(MEM_DEBUG_CHECK3(header
)) != MEM_DEBUG_MAGIC3
||
330 header
->size
== (unsigned)-1) {
331 fprintf(stderr
, "%s check failed\n", msg
);
332 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
333 fprintf(stderr
, "%s:%u: block allocated here.\n",
334 header
->file_name
, header
->line_num
);
340 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
343 mem_debug_header_t
*header
;
345 header
= malloc(sizeof(mem_debug_header_t
) + size
);
347 _tcc_error("memory full (malloc)");
349 header
->magic1
= MEM_DEBUG_MAGIC1
;
350 header
->magic2
= MEM_DEBUG_MAGIC2
;
352 write32le(MEM_DEBUG_CHECK3(header
), MEM_DEBUG_MAGIC3
);
353 header
->line_num
= line
;
354 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
355 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
356 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
358 header
->next
= mem_debug_chain
;
361 header
->next
->prev
= header
;
362 mem_debug_chain
= header
;
364 mem_cur_size
+= size
;
365 if (mem_cur_size
> mem_max_size
)
366 mem_max_size
= mem_cur_size
;
368 return MEM_USER_PTR(header
);
371 PUB_FUNC
void tcc_free_debug(void *ptr
)
373 mem_debug_header_t
*header
;
376 header
= malloc_check(ptr
, "tcc_free");
377 mem_cur_size
-= header
->size
;
378 header
->size
= (unsigned)-1;
380 header
->next
->prev
= header
->prev
;
382 header
->prev
->next
= header
->next
;
383 if (header
== mem_debug_chain
)
384 mem_debug_chain
= header
->next
;
388 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
391 ptr
= tcc_malloc_debug(size
,file
,line
);
392 memset(ptr
, 0, size
);
396 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
398 mem_debug_header_t
*header
;
399 int mem_debug_chain_update
= 0;
401 return tcc_malloc_debug(size
, file
, line
);
402 header
= malloc_check(ptr
, "tcc_realloc");
403 mem_cur_size
-= header
->size
;
404 mem_debug_chain_update
= (header
== mem_debug_chain
);
405 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
407 _tcc_error("memory full (realloc)");
409 write32le(MEM_DEBUG_CHECK3(header
), MEM_DEBUG_MAGIC3
);
411 header
->next
->prev
= header
;
413 header
->prev
->next
= header
;
414 if (mem_debug_chain_update
)
415 mem_debug_chain
= header
;
416 mem_cur_size
+= size
;
417 if (mem_cur_size
> mem_max_size
)
418 mem_max_size
= mem_cur_size
;
419 return MEM_USER_PTR(header
);
422 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
425 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
430 PUB_FUNC
void tcc_memcheck(void)
433 mem_debug_header_t
*header
= mem_debug_chain
;
434 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
435 mem_cur_size
, mem_max_size
);
437 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
438 header
->file_name
, header
->line_num
, header
->size
);
439 header
= header
->next
;
446 #endif /* MEM_DEBUG */
448 #define free(p) use_tcc_free(p)
449 #define malloc(s) use_tcc_malloc(s)
450 #define realloc(p, s) use_tcc_realloc(p, s)
452 /********************************************************/
455 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
461 pp
= *(void ***)ptab
;
462 /* every power of two we double array size */
463 if ((nb
& (nb
- 1)) == 0) {
468 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
475 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
478 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
481 tcc_free(*(void**)pp
);
485 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
493 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
494 if (c
== '{' && p
[1] && p
[2] == '}') {
497 cstr_cat(&str
, s
->tcc_lib_path
, -1);
498 if (c
== 'f' && file
) {
499 /* substitute current file's dir */
500 const char *f
= file
->true_filename
;
501 const char *b
= tcc_basename(f
);
503 cstr_cat(&str
, f
, b
- f
- 1);
505 cstr_cat(&str
, ".", 1);
512 cstr_ccat(&str
, '\0');
513 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
520 /********************************************************/
521 /* warning / error */
523 /* warn_... option bits */
524 #define WARN_ON 1 /* warning is on (-Woption) */
525 #define WARN_ERR 2 /* warning is an error (-Werror=option) */
526 #define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
529 enum { ERROR_WARN
, ERROR_NOABORT
, ERROR_ERROR
};
531 static void error1(int mode
, const char *fmt
, va_list ap
)
533 BufferedFile
**pf
, *f
;
534 TCCState
*s1
= tcc_state
;
540 /* can happen only if called from tcc_malloc(): 'out of memory' */
545 if (mode
== ERROR_WARN
) {
549 /* handle tcc_warning_c(warn_option)(fmt, ...) */
550 int wopt
= *(&s1
->warn_none
+ s1
->warn_num
);
552 if (0 == (wopt
& WARN_ON
))
564 if (s1
->error_set_jmp_enabled
) { /* we're called while parsing a file */
565 /* use upper file if inline ":asm:" or token ":paste:" */
566 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
570 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
571 cstr_printf(&cs
, "In file included from %s:%d:\n",
572 (*pf
)->filename
, (*pf
)->line_num
);
573 cstr_printf(&cs
, "%s:%d: ",
574 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
575 } else if (s1
->current_filename
) {
576 cstr_printf(&cs
, "%s: ", s1
->current_filename
);
581 cstr_printf(&cs
, "tcc: ");
582 cstr_printf(&cs
, mode
== ERROR_WARN
? "warning: " : "error: ");
583 cstr_vprintf(&cs
, fmt
, ap
);
584 if (!s1
|| !s1
->error_func
) {
585 /* default case: stderr */
586 if (s1
&& s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
587 printf("\n"); /* print a newline during tcc -E */
588 fflush(stdout
); /* flush -v output */
589 fprintf(stderr
, "%s\n", (char*)cs
.data
);
590 fflush(stderr
); /* print error/warning now (win32) */
592 s1
->error_func(s1
->error_opaque
, (char*)cs
.data
);
596 if (mode
!= ERROR_WARN
)
598 if (mode
!= ERROR_ERROR
)
600 if (s1
->error_set_jmp_enabled
)
601 longjmp(s1
->error_jmp_buf
, 1);
606 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
, TCCErrorFunc error_func
)
608 s
->error_opaque
= error_opaque
;
609 s
->error_func
= error_func
;
612 LIBTCCAPI TCCErrorFunc
tcc_get_error_func(TCCState
*s
)
614 return s
->error_func
;
617 LIBTCCAPI
void *tcc_get_error_opaque(TCCState
*s
)
619 return s
->error_opaque
;
622 /* error without aborting current compilation */
623 PUB_FUNC
void _tcc_error_noabort(const char *fmt
, ...)
627 error1(ERROR_NOABORT
, fmt
, ap
);
631 PUB_FUNC
void _tcc_error(const char *fmt
, ...)
635 for (;;) error1(ERROR_ERROR
, fmt
, ap
);
638 PUB_FUNC
void _tcc_warning(const char *fmt
, ...)
642 error1(ERROR_WARN
, fmt
, ap
);
646 /********************************************************/
649 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
652 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
654 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
655 bf
->buf_ptr
= bf
->buffer
;
656 bf
->buf_end
= bf
->buffer
+ initlen
;
657 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
658 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
660 normalize_slashes(bf
->filename
);
662 bf
->true_filename
= bf
->filename
;
664 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
668 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
671 ST_FUNC
void tcc_close(void)
673 TCCState
*s1
= tcc_state
;
674 BufferedFile
*bf
= file
;
677 total_lines
+= bf
->line_num
;
679 if (bf
->true_filename
!= bf
->filename
)
680 tcc_free(bf
->true_filename
);
685 static int _tcc_open(TCCState
*s1
, const char *filename
)
688 if (strcmp(filename
, "-") == 0)
689 fd
= 0, filename
= "<stdin>";
691 fd
= open(filename
, O_RDONLY
| O_BINARY
);
692 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
693 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
694 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
698 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
700 int fd
= _tcc_open(s1
, filename
);
703 tcc_open_bf(s1
, filename
, 0);
708 /* compile the file opened in 'file'. Return non zero if errors. */
709 static int tcc_compile(TCCState
*s1
, int filetype
, const char *str
, int fd
)
711 /* Here we enter the code section where we use the global variables for
712 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
713 Other threads need to wait until we're done.
715 Alternatively we could use thread local storage for those global
716 variables, which may or may not have advantages */
719 s1
->error_set_jmp_enabled
= 1;
721 if (setjmp(s1
->error_jmp_buf
) == 0) {
725 int len
= strlen(str
);
726 tcc_open_bf(s1
, "<string>", len
);
727 memcpy(file
->buffer
, str
, len
);
729 tcc_open_bf(s1
, str
, 0);
733 tccelf_begin_file(s1
);
734 preprocess_start(s1
, filetype
);
736 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
738 } else if (filetype
& (AFF_TYPE_ASM
| AFF_TYPE_ASMPP
)) {
739 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
747 s1
->error_set_jmp_enabled
= 0;
751 return s1
->nb_errors
!= 0 ? -1 : 0;
754 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
756 return tcc_compile(s
, s
->filetype
, str
, -1);
759 /* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
760 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
763 if (NULL
== (eq
= strchr(sym
, '=')))
766 value
= *eq
? eq
+ 1 : "1";
767 cstr_printf(&s1
->cmdline_defs
, "#define %.*s %s\n", (int)(eq
-sym
), sym
, value
);
770 /* undefine a preprocessor symbol */
771 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
773 cstr_printf(&s1
->cmdline_defs
, "#undef %s\n", sym
);
777 LIBTCCAPI TCCState
*tcc_new(void)
781 s
= tcc_mallocz(sizeof(TCCState
));
793 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
794 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
795 s
->warn_implicit_function_declaration
= 1;
796 s
->warn_discarded_qualifiers
= 1;
797 s
->ms_extensions
= 1;
799 #ifdef CHAR_IS_UNSIGNED
800 s
->char_is_unsigned
= 1;
802 #ifdef TCC_TARGET_I386
805 /* enable this if you want symbols with leading underscore on windows: */
806 #if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
807 s
->leading_underscore
= 1;
809 #ifdef TCC_TARGET_ARM
810 s
->float_abi
= ARM_FLOAT_ABI
;
814 /* might be used in error() before preprocess_start() */
815 s
->include_stack_ptr
= s
->include_stack
;
819 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
823 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
828 /* free library paths */
829 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
830 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
832 /* free include paths */
833 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
834 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
836 tcc_free(s1
->tcc_lib_path
);
837 tcc_free(s1
->soname
);
839 tcc_free(s1
->init_symbol
);
840 tcc_free(s1
->fini_symbol
);
841 tcc_free(s1
->outfile
);
842 tcc_free(s1
->deps_outfile
);
843 dynarray_reset(&s1
->files
, &s1
->nb_files
);
844 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
845 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
846 dynarray_reset(&s1
->argv
, &s1
->argc
);
847 cstr_free(&s1
->cmdline_defs
);
848 cstr_free(&s1
->cmdline_incl
);
850 /* free runtime memory */
856 if (0 == --nb_states
)
861 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
863 s
->output_type
= output_type
;
866 /* default include paths */
867 /* -isystem paths have already been handled */
868 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
870 #ifdef CONFIG_TCC_BCHECK
871 if (s
->do_bounds_check
) {
872 /* if bound checking, then add corresponding sections */
873 tccelf_bounds_new(s
);
877 /* add debug sections */
880 if (output_type
== TCC_OUTPUT_OBJ
) {
881 /* always elf for objects */
882 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
886 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
890 /* allow linking with system dll's directly */
891 tcc_add_systemdir(s
);
893 /* target PE has its own startup code in libtcc1.a */
896 #elif defined TCC_TARGET_MACHO
897 # ifdef TCC_IS_NATIVE
898 tcc_add_macos_sdkpath(s
);
900 /* Mach-O with LC_MAIN doesn't need any crt startup code. */
904 /* paths for crt objects */
905 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
906 /* add libc crt1/crti objects */
907 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
910 if (output_type
!= TCC_OUTPUT_DLL
)
911 tcc_add_crt(s
, "crt0.o");
912 if (output_type
== TCC_OUTPUT_DLL
)
913 tcc_add_crt(s
, "crtbeginS.o");
915 tcc_add_crt(s
, "crtbegin.o");
916 #elif TARGETOS_FreeBSD
917 if (output_type
!= TCC_OUTPUT_DLL
)
918 tcc_add_crt(s
, "crt1.o");
919 tcc_add_crt(s
, "crti.o");
921 tcc_add_crt(s
, "crtbeginT.o");
922 else if (output_type
== TCC_OUTPUT_DLL
)
923 tcc_add_crt(s
, "crtbeginS.o");
925 tcc_add_crt(s
, "crtbegin.o");
926 #elif TARGETOS_NetBSD
927 if (output_type
!= TCC_OUTPUT_DLL
)
928 tcc_add_crt(s
, "crt0.o");
929 tcc_add_crt(s
, "crti.o");
931 tcc_add_crt(s
, "crtbeginT.o");
932 else if (output_type
== TCC_OUTPUT_DLL
)
933 tcc_add_crt(s
, "crtbeginS.o");
935 tcc_add_crt(s
, "crtbegin.o");
937 if (output_type
!= TCC_OUTPUT_DLL
)
938 tcc_add_crt(s
, "crt1.o");
939 tcc_add_crt(s
, "crti.o");
946 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
948 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
952 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
954 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
958 ST_FUNC DLLReference
*tcc_add_dllref(TCCState
*s1
, const char *dllname
)
960 DLLReference
*ref
= tcc_mallocz(sizeof(DLLReference
) + strlen(dllname
));
961 strcpy(ref
->name
, dllname
);
962 dynarray_add(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, ref
);
966 /* OpenBSD: choose latest from libxxx.so.x.y versions */
967 #if defined TARGETOS_OpenBSD && !defined _WIN32
969 static int tcc_glob_so(TCCState
*s1
, const char *pattern
, char *buf
, int size
)
974 int i
, v
, v1
, v2
, v3
;
976 star
= strchr(pattern
, '*');
977 if (!star
|| glob(pattern
, 0, NULL
, &g
))
979 for (v
= -1, i
= 0; i
< g
.gl_pathc
; ++i
) {
981 if (2 != sscanf(p
+ (star
- pattern
), "%d.%d.%d", &v1
, &v2
, &v3
))
983 if ((v1
= v1
* 1000 + v2
) > v
)
984 v
= v1
, pstrcpy(buf
, size
, p
);
991 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
995 #if defined TARGETOS_OpenBSD && !defined _WIN32
997 if (tcc_glob_so(s1
, filename
, buf
, sizeof buf
) >= 0)
1002 fd
= _tcc_open(s1
, filename
);
1004 if (flags
& AFF_PRINT_ERROR
)
1005 tcc_error_noabort("file '%s' not found", filename
);
1009 s1
->current_filename
= filename
;
1010 if (flags
& AFF_TYPE_BIN
) {
1014 obj_type
= tcc_object_type(fd
, &ehdr
);
1015 lseek(fd
, 0, SEEK_SET
);
1019 case AFF_BINTYPE_REL
:
1020 ret
= tcc_load_object_file(s1
, fd
, 0);
1023 case AFF_BINTYPE_AR
:
1024 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
1027 #ifdef TCC_TARGET_PE
1029 ret
= pe_load_file(s1
, fd
, filename
);
1032 #elif defined TCC_TARGET_MACHO
1033 case AFF_BINTYPE_DYN
:
1035 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1036 #ifdef TCC_IS_NATIVE
1038 const char* soname
= filename
;
1039 if (obj_type
!= AFF_BINTYPE_DYN
)
1040 soname
= macho_tbd_soname(filename
);
1041 dl
= dlopen(soname
, RTLD_GLOBAL
| RTLD_LAZY
);
1043 tcc_add_dllref(s1
, soname
)->handle
= dl
, ret
= 0;
1044 if (filename
!= soname
)
1045 tcc_free((void *)soname
);
1047 } else if (obj_type
== AFF_BINTYPE_DYN
) {
1048 ret
= macho_load_dll(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1050 ret
= macho_load_tbd(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1055 const char *ext
= tcc_fileextension(filename
);
1056 if (!strcmp(ext
, ".tbd"))
1057 goto case_dyn_or_tbd
;
1058 if (!strcmp(ext
, ".dylib")) {
1059 obj_type
= AFF_BINTYPE_DYN
;
1060 goto case_dyn_or_tbd
;
1066 case AFF_BINTYPE_DYN
:
1067 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1068 #ifdef TCC_IS_NATIVE
1069 void* dl
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
1071 tcc_add_dllref(s1
, filename
)->handle
= dl
, ret
= 0;
1074 ret
= tcc_load_dll(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1078 /* as GNU ld, consider it is an ld script if not recognized */
1079 ret
= tcc_load_ldscript(s1
, fd
);
1082 #endif /* pe / macos / unix */
1086 tcc_error_noabort("%s: unrecognized file type", filename
);
1089 #ifdef TCC_TARGET_COFF
1090 case AFF_BINTYPE_C67
:
1091 ret
= tcc_load_coff(s1
, fd
);
1097 /* update target deps */
1098 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
, tcc_strdup(filename
));
1099 ret
= tcc_compile(s1
, flags
, filename
, fd
);
1101 s1
->current_filename
= NULL
;
1105 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1107 int filetype
= s
->filetype
;
1108 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1109 /* use a file extension to detect a filetype */
1110 const char *ext
= tcc_fileextension(filename
);
1113 if (!strcmp(ext
, "S"))
1114 filetype
= AFF_TYPE_ASMPP
;
1115 else if (!strcmp(ext
, "s"))
1116 filetype
= AFF_TYPE_ASM
;
1117 else if (!PATHCMP(ext
, "c")
1118 || !PATHCMP(ext
, "h")
1119 || !PATHCMP(ext
, "i"))
1120 filetype
= AFF_TYPE_C
;
1122 filetype
|= AFF_TYPE_BIN
;
1124 filetype
= AFF_TYPE_C
;
1127 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1130 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1132 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1136 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1137 const char *filename
, int flags
, char **paths
, int nb_paths
)
1142 for(i
= 0; i
< nb_paths
; i
++) {
1143 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1144 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1150 #ifndef TCC_TARGET_MACHO
1151 /* find and load a dll. Return non zero if not found */
1152 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1154 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1155 s
->library_paths
, s
->nb_library_paths
);
1159 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
1160 ST_FUNC
int tcc_add_crt(TCCState
*s1
, const char *filename
)
1162 if (-1 == tcc_add_library_internal(s1
, "%s/%s",
1163 filename
, 0, s1
->crt_paths
, s1
->nb_crt_paths
))
1164 tcc_error_noabort("file '%s' not found", filename
);
1169 /* the library name is the same as the argument of the '-l' option */
1170 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1172 #if defined TCC_TARGET_PE
1173 static const char * const libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1174 const char * const *pp
= s
->static_link
? libs
+ 4 : libs
;
1175 #elif defined TCC_TARGET_MACHO
1176 static const char * const libs
[] = { "%s/lib%s.dylib", "%s/lib%s.tbd", "%s/lib%s.a", NULL
};
1177 const char * const *pp
= s
->static_link
? libs
+ 2 : libs
;
1178 #elif defined TARGETOS_OpenBSD
1179 static const char * const libs
[] = { "%s/lib%s.so.*", "%s/lib%s.a", NULL
};
1180 const char * const *pp
= s
->static_link
? libs
+ 1 : libs
;
1182 static const char * const libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1183 const char * const *pp
= s
->static_link
? libs
+ 1 : libs
;
1185 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1187 if (0 == tcc_add_library_internal(s
, *pp
,
1188 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1195 PUB_FUNC
int tcc_add_library_err(TCCState
*s1
, const char *libname
)
1197 int ret
= tcc_add_library(s1
, libname
);
1199 tcc_error_noabort("library '%s' not found", libname
);
1203 /* handle #pragma comment(lib,) */
1204 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1207 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1208 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1211 LIBTCCAPI
int tcc_add_symbol(TCCState
*s1
, const char *name
, const void *val
)
1213 #ifdef TCC_TARGET_PE
1214 /* On x86_64 'val' might not be reachable with a 32bit offset.
1215 So it is handled here as if it were in a DLL. */
1216 pe_putimport(s1
, 0, name
, (uintptr_t)val
);
1219 if (s1
->leading_underscore
) {
1221 pstrcpy(buf
+ 1, sizeof(buf
) - 1, name
);
1224 set_global_sym(s1
, name
, NULL
, (addr_t
)(uintptr_t)val
); /* NULL: SHN_ABS */
1229 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1231 tcc_free(s
->tcc_lib_path
);
1232 s
->tcc_lib_path
= tcc_strdup(path
);
1235 /********************************************************/
1236 /* options parser */
1238 static int strstart(const char *val
, const char **str
)
1253 /* Like strstart, but automatically takes into account that ld options can
1255 * - start with double or single dash (e.g. '--soname' or '-soname')
1256 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1257 * or '-Wl,-soname=x.so')
1259 * you provide `val` always in 'option[=]' form (no leading -)
1261 static int link_option(const char *str
, const char *val
, const char **ptr
)
1266 /* there should be 1 or 2 dashes */
1272 /* then str & val should match (potentially up to '=') */
1279 if (strstart("no-", &p
))
1283 while (*q
!= '\0' && *q
!= '=') {
1290 /* '=' near eos means ',' or '=' is ok */
1294 if (*p
!= ',' && *p
!= '=')
1304 static const char *skip_linker_arg(const char **str
)
1306 const char *s1
= *str
;
1307 const char *s2
= strchr(s1
, ',');
1308 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1312 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1318 p
[l
= strlen(p
)] = sep
, ++l
;
1319 skip_linker_arg(&q
);
1320 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1323 /* set linker options */
1324 static int tcc_set_linker(TCCState
*s
, const char *option
)
1329 const char *p
= NULL
;
1334 if (link_option(option
, "Bsymbolic", &p
)) {
1336 } else if (link_option(option
, "nostdlib", &p
)) {
1338 } else if (link_option(option
, "e=", &p
)) {
1339 copy_linker_arg(&s
->elf_entryname
, p
, 0);
1340 } else if (link_option(option
, "fini=", &p
)) {
1341 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1343 } else if (link_option(option
, "image-base=", &p
)
1344 || link_option(option
, "Ttext=", &p
)) {
1345 s
->text_addr
= strtoull(p
, &end
, 16);
1346 s
->has_text_addr
= 1;
1347 } else if (link_option(option
, "init=", &p
)) {
1348 copy_linker_arg(&s
->init_symbol
, p
, 0);
1350 } else if (link_option(option
, "oformat=", &p
)) {
1351 #if defined(TCC_TARGET_PE)
1352 if (strstart("pe-", &p
)) {
1354 if (strstart("elf64-", &p
)) {
1356 if (strstart("elf32-", &p
)) {
1358 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1359 } else if (!strcmp(p
, "binary")) {
1360 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1361 #ifdef TCC_TARGET_COFF
1362 } else if (!strcmp(p
, "coff")) {
1363 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1368 } else if (link_option(option
, "as-needed", &p
)) {
1370 } else if (link_option(option
, "O", &p
)) {
1372 } else if (link_option(option
, "export-all-symbols", &p
)) {
1374 } else if (link_option(option
, "export-dynamic", &p
)) {
1376 } else if (link_option(option
, "rpath=", &p
)) {
1377 copy_linker_arg(&s
->rpath
, p
, ':');
1378 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1379 s
->enable_new_dtags
= 1;
1380 } else if (link_option(option
, "section-alignment=", &p
)) {
1381 s
->section_align
= strtoul(p
, &end
, 16);
1382 } else if (link_option(option
, "soname=", &p
)) {
1383 copy_linker_arg(&s
->soname
, p
, 0);
1384 #ifdef TCC_TARGET_PE
1385 } else if (link_option(option
, "large-address-aware", &p
)) {
1386 s
->pe_characteristics
|= 0x20;
1387 } else if (link_option(option
, "file-alignment=", &p
)) {
1388 s
->pe_file_align
= strtoul(p
, &end
, 16);
1389 } else if (link_option(option
, "stack=", &p
)) {
1390 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1391 } else if (link_option(option
, "subsystem=", &p
)) {
1392 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1393 if (!strcmp(p
, "native")) {
1394 s
->pe_subsystem
= 1;
1395 } else if (!strcmp(p
, "console")) {
1396 s
->pe_subsystem
= 3;
1397 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1398 s
->pe_subsystem
= 2;
1399 } else if (!strcmp(p
, "posix")) {
1400 s
->pe_subsystem
= 7;
1401 } else if (!strcmp(p
, "efiapp")) {
1402 s
->pe_subsystem
= 10;
1403 } else if (!strcmp(p
, "efiboot")) {
1404 s
->pe_subsystem
= 11;
1405 } else if (!strcmp(p
, "efiruntime")) {
1406 s
->pe_subsystem
= 12;
1407 } else if (!strcmp(p
, "efirom")) {
1408 s
->pe_subsystem
= 13;
1409 #elif defined(TCC_TARGET_ARM)
1410 if (!strcmp(p
, "wince")) {
1411 s
->pe_subsystem
= 9;
1416 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1418 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1420 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1421 } else if (link_option(option
, "z=", &p
)) {
1427 tcc_error("unsupported linker option '%s'", option
);
1430 tcc_warning_c(warn_unsupported
)("unsupported linker option '%s'", option
);
1431 option
= skip_linker_arg(&p
);
1436 typedef struct TCCOption
{
1443 TCC_OPTION_ignored
= 0,
1460 TCC_OPTION_dumpversion
,
1472 TCC_OPTION_mfloat_abi
,
1476 TCC_OPTION_iwithprefix
,
1478 TCC_OPTION_nostdinc
,
1479 TCC_OPTION_nostdlib
,
1480 TCC_OPTION_print_search_dirs
,
1481 TCC_OPTION_rdynamic
,
1496 #define TCC_OPTION_HAS_ARG 0x0001
1497 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1499 static const TCCOption tcc_options
[] = {
1500 { "h", TCC_OPTION_HELP
, 0 },
1501 { "-help", TCC_OPTION_HELP
, 0 },
1502 { "?", TCC_OPTION_HELP
, 0 },
1503 { "hh", TCC_OPTION_HELP2
, 0 },
1504 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1505 { "-version", TCC_OPTION_v
, 0 }, /* handle as verbose, also prints version*/
1506 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1507 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1508 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1509 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1510 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1511 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1512 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1513 { "bench", TCC_OPTION_bench
, 0 },
1514 #ifdef CONFIG_TCC_BACKTRACE
1515 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1517 #ifdef CONFIG_TCC_BCHECK
1518 { "b", TCC_OPTION_b
, 0 },
1520 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1521 { "c", TCC_OPTION_c
, 0 },
1522 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1523 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1524 { "static", TCC_OPTION_static
, 0 },
1525 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1526 { "shared", TCC_OPTION_shared
, 0 },
1527 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1528 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1529 { "pthread", TCC_OPTION_pthread
, 0},
1530 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1531 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1532 { "r", TCC_OPTION_r
, 0 },
1533 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1534 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1535 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1536 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1537 #ifdef TCC_TARGET_ARM
1538 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1540 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1541 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1542 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1543 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1544 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1545 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1546 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1547 { "w", TCC_OPTION_w
, 0 },
1548 { "E", TCC_OPTION_E
, 0},
1549 { "M", TCC_OPTION_M
, 0},
1550 { "MD", TCC_OPTION_MD
, 0},
1551 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1552 { "MM", TCC_OPTION_MM
, 0},
1553 { "MMD", TCC_OPTION_MMD
, 0},
1554 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1555 { "ar", TCC_OPTION_ar
, 0},
1556 #ifdef TCC_TARGET_PE
1557 { "impdef", TCC_OPTION_impdef
, 0},
1559 /* ignored (silently, except after -Wunsupported) */
1560 { "arch", 0, TCC_OPTION_HAS_ARG
},
1562 { "-param", 0, TCC_OPTION_HAS_ARG
},
1563 { "pedantic", 0, 0 },
1566 { "traditional", 0, 0 },
1570 typedef struct stdvalue
{
1575 /* accepted values for the -std= option */
1576 static const stdvalue std_values
[] = {
1581 { 199901, "gnu99" },
1582 { 201112, "gnu11" },
1583 { 201710, "gnu17" },
1587 typedef struct FlagDef
{
1593 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1594 #define FD_INVERT 0x0002 /* invert value before storing */
1596 static const FlagDef options_W
[] = {
1597 { offsetof(TCCState
, warn_all
), WD_ALL
, "all" },
1598 { offsetof(TCCState
, warn_error
), 0, "error" },
1599 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1600 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1601 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
, "implicit-function-declaration" },
1602 { offsetof(TCCState
, warn_discarded_qualifiers
), WD_ALL
, "discarded-qualifiers" },
1606 static const FlagDef options_f
[] = {
1607 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1608 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1609 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1610 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1611 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1612 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1613 { offsetof(TCCState
, test_coverage
), 0, "test-coverage" },
1617 static const FlagDef options_m
[] = {
1618 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1619 #ifdef TCC_TARGET_X86_64
1620 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1625 static int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1627 int value
, mask
, ret
;
1632 r
= name
, value
= !strstart("no-", &r
), mask
= 0;
1634 /* when called with options_W, look for -W[no-]error=<option> */
1635 if ((flags
->flags
& WD_ALL
) && strstart("error=", &r
))
1636 value
= value
? WARN_ON
|WARN_ERR
: WARN_NOE
, mask
= WARN_ON
;
1638 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1640 if (strcmp(r
, p
->name
))
1643 if (0 == (p
->flags
& WD_ALL
))
1647 f
= (unsigned char *)s
+ p
->offset
;
1648 *f
= (*f
& mask
) | (value
^ !!(p
->flags
& FD_INVERT
));
1652 if (strcmp(r
, "all"))
1659 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1661 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1663 strcpy(f
->name
, filename
);
1664 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1667 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1672 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1678 while (c
= (unsigned char)*r
, c
) {
1680 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1682 } else if (c
== '"') {
1685 } else if (q
== 0 && c
<= ' ') {
1691 //printf("<%s>\n", str.data), fflush(stdout);
1692 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1699 /* read list file */
1700 static void args_parser_listfile(TCCState
*s
,
1701 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1709 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1711 tcc_error("listfile '%s' not found", filename
);
1713 p
= tcc_load_text(fd
);
1714 for (i
= 0; i
< *pargc
; ++i
)
1716 args_parser_make_argv(p
, &argc
, &argv
);
1718 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1721 dynarray_reset(&s
->argv
, &s
->argc
);
1722 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1725 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1728 const TCCOption
*popt
;
1729 const char *optarg
, *r
;
1730 const char *run
= NULL
;
1732 CString linker_arg
; /* collect -Wl options */
1733 int tool
= 0, arg_start
= 0, noaction
= optind
;
1734 char **argv
= *pargv
;
1737 cstr_new(&linker_arg
);
1739 while (optind
< argc
) {
1741 if (r
[0] == '@' && r
[1] != '\0') {
1742 args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
);
1747 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1752 if (r
[0] != '-' || r
[1] == '\0') {
1753 if (r
[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1754 args_parser_add_file(s
, r
, s
->filetype
);
1756 tcc_set_options(s
, run
);
1757 arg_start
= optind
- 1;
1763 /* find option in table */
1764 for(popt
= tcc_options
; ; ++popt
) {
1765 const char *p1
= popt
->name
;
1766 const char *r1
= r
+ 1;
1768 tcc_error("invalid option -- '%s'", r
);
1769 if (!strstart(p1
, &r1
))
1772 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1773 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1776 tcc_error("argument to '%s' is missing", r
);
1777 optarg
= argv
[optind
++];
1779 } else if (*r1
!= '\0')
1784 switch(popt
->index
) {
1785 case TCC_OPTION_HELP
:
1788 case TCC_OPTION_HELP2
:
1792 tcc_add_include_path(s
, optarg
);
1795 tcc_define_symbol(s
, optarg
, NULL
);
1798 tcc_undefine_symbol(s
, optarg
);
1801 tcc_add_library_path(s
, optarg
);
1804 /* set tcc utilities path (mainly for tcc development) */
1805 tcc_set_lib_path(s
, optarg
);
1808 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1811 case TCC_OPTION_pthread
:
1812 s
->option_pthread
= 1;
1814 case TCC_OPTION_bench
:
1817 #ifdef CONFIG_TCC_BACKTRACE
1819 s
->rt_num_callers
= atoi(optarg
);
1820 s
->do_backtrace
= 1;
1824 #ifdef CONFIG_TCC_BCHECK
1826 s
->do_bounds_check
= 1;
1827 s
->do_backtrace
= 1;
1832 /* Use "-g" as alias for "-g1". Use "-g0" to disable debug */
1833 /* Other common used values: "-g0", "-g1", "-g2" and "-g3" */
1834 /* no failure with unsupported options */
1836 s
->do_debug
= atoi(optarg
);
1844 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1850 else if (*optarg
== 'M')
1852 else if (*optarg
== 't')
1854 else if (isnum(*optarg
))
1855 s
->g_debug
|= atoi(optarg
);
1857 goto unsupported_option
;
1859 case TCC_OPTION_static
:
1862 case TCC_OPTION_std
:
1864 if (*optarg
== '=') {
1866 if (strcmp(std_values
[x
].name
, &optarg
[1]) == 0) {
1867 x
= std_values
[x
].cversion
;
1871 } while (x
< (sizeof(std_values
)/sizeof(stdvalue
)));
1873 if (x
> (sizeof(std_values
)/sizeof(stdvalue
)))
1876 goto unsupported_option
;
1879 case TCC_OPTION_shared
:
1881 goto set_output_type
;
1882 case TCC_OPTION_soname
:
1883 s
->soname
= tcc_strdup(optarg
);
1887 tcc_warning("multiple -o option");
1888 tcc_free(s
->outfile
);
1890 s
->outfile
= tcc_strdup(optarg
);
1893 /* generate a .o merging several output files */
1896 goto set_output_type
;
1897 case TCC_OPTION_isystem
:
1898 tcc_add_sysinclude_path(s
, optarg
);
1900 case TCC_OPTION_include
:
1901 cstr_printf(&s
->cmdline_incl
, "#include \"%s\"\n", optarg
);
1903 case TCC_OPTION_nostdinc
:
1906 case TCC_OPTION_nostdlib
:
1909 case TCC_OPTION_run
:
1910 #ifndef TCC_IS_NATIVE
1911 tcc_error("-run is not available in a cross compiler");
1914 x
= TCC_OUTPUT_MEMORY
;
1915 goto set_output_type
;
1917 do ++s
->verbose
; while (*optarg
++ == 'v');
1921 if (set_flag(s
, options_f
, optarg
) < 0)
1922 goto unsupported_option
;
1924 #ifdef TCC_TARGET_ARM
1925 case TCC_OPTION_mfloat_abi
:
1926 /* tcc doesn't support soft float yet */
1927 if (!strcmp(optarg
, "softfp")) {
1928 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1929 } else if (!strcmp(optarg
, "hard"))
1930 s
->float_abi
= ARM_HARD_FLOAT
;
1932 tcc_error("unsupported float abi '%s'", optarg
);
1936 if (set_flag(s
, options_m
, optarg
) < 0) {
1937 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
1938 goto unsupported_option
;
1939 if (PTR_SIZE
!= x
/8)
1946 if (optarg
[0] && set_flag(s
, options_W
, optarg
) < 0)
1947 goto unsupported_option
;
1952 case TCC_OPTION_rdynamic
:
1956 if (linker_arg
.size
)
1957 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
1958 cstr_cat(&linker_arg
, optarg
, 0);
1959 if (tcc_set_linker(s
, linker_arg
.data
))
1960 cstr_free(&linker_arg
);
1966 x
= TCC_OUTPUT_PREPROCESS
;
1967 goto set_output_type
;
1969 s
->Pflag
= atoi(optarg
) + 1;
1972 s
->include_sys_deps
= 1;
1976 if(!s
->deps_outfile
)
1977 s
->deps_outfile
= tcc_strdup("-");
1979 case TCC_OPTION_MMD
:
1984 s
->include_sys_deps
= 1;
1987 s
->deps_outfile
= tcc_strdup(optarg
);
1989 case TCC_OPTION_dumpversion
:
1990 printf ("%s\n", TCC_VERSION
);
1997 else if (*optarg
== 'a')
1999 else if (*optarg
== 'b')
2001 else if (*optarg
== 'n')
2004 tcc_warning("unsupported language '%s'", optarg
);
2005 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
2008 /* Use "-O" as alias for "-O1". */
2009 /* Other common used values: "-O0", "-O1", "-O2", "-O3" and "-Os" */
2010 /* no failure with unsupported options */
2012 s
->optimize
= isnum(x
) ? atoi(optarg
) : (x
) ? x
: 1;
2014 case TCC_OPTION_print_search_dirs
:
2017 case TCC_OPTION_impdef
:
2023 arg_start
= optind
- 1;
2024 if (arg_start
!= noaction
)
2025 tcc_error("cannot parse %s here", r
);
2030 tcc_warning_c(warn_unsupported
)("unsupported option '%s'", r
);
2034 if (linker_arg
.size
) {
2035 r
= linker_arg
.data
;
2038 *pargc
= argc
- arg_start
;
2039 *pargv
= argv
+ arg_start
;
2042 if (optind
!= noaction
)
2044 if (s
->verbose
== 2)
2045 return OPT_PRINT_DIRS
;
2051 LIBTCCAPI
void tcc_set_options(TCCState
*s
, const char *r
)
2055 args_parser_make_argv(r
, &argc
, &argv
);
2056 tcc_parse_args(s
, &argc
, &argv
, 0);
2057 dynarray_reset(&argv
, &argc
);
2060 PUB_FUNC
void tcc_print_stats(TCCState
*s1
, unsigned total_time
)
2064 if (total_bytes
< 1)
2066 fprintf(stderr
, "* %d idents, %d lines, %d bytes\n"
2067 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2068 total_idents
, total_lines
, total_bytes
,
2069 (double)total_time
/1000,
2070 (unsigned)total_lines
*1000/total_time
,
2071 (double)total_bytes
/1000/total_time
);
2072 fprintf(stderr
, "* text %d, data.rw %d, data.ro %d, bss %d bytes\n",
2073 s1
->total_output
[0],
2074 s1
->total_output
[1],
2075 s1
->total_output
[2],
2079 fprintf(stderr
, "* %d bytes memory used\n", mem_max_size
);