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
28 #ifdef TCC_TARGET_I386
30 #include "i386-link.c"
32 #elif defined(TCC_TARGET_ARM)
36 #elif defined(TCC_TARGET_ARM64)
37 #include "arm64-gen.c"
38 #include "arm64-link.c"
40 #elif defined(TCC_TARGET_C67)
44 #elif defined(TCC_TARGET_X86_64)
45 #include "x86_64-gen.c"
46 #include "x86_64-link.c"
48 #elif defined(TCC_TARGET_RISCV64)
49 #include "riscv64-gen.c"
50 #include "riscv64-link.c"
51 #include "riscv64-asm.c"
58 #ifdef TCC_TARGET_MACHO
61 #endif /* ONE_SOURCE */
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
);
71 /* an array of pointers to memory to be free'd after errors */
72 ST_DATA
void** stk_data
;
73 ST_DATA
int nb_stk_data
;
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
)
106 GetModuleFileName(tcc_module
, path
, MAX_PATH
);
107 p
= tcc_basename(normalize_slashes(strlwr(path
)));
114 * See if we are perhaps in a "bin/" subfolder of the
115 * installation path, in which case the real root of
116 * the installation is one level up. We can test this
117 * by looking for the 'include' folder.
119 strncpy(temp
, path
, sizeof(temp
)-1);
120 strcat(temp
, "/include");
122 if (_access(temp
, 0) != 0) {
123 /* No 'include' folder found, so go up one level. */
124 strncpy(temp
, path
, sizeof(temp
)-1);
126 /* Try this for several "levels" up. */
127 for (c
= 0; c
< 4; c
++) {
128 p
= tcc_basename(temp
);
134 strncpy(try, temp
, sizeof(try)-1);
135 strcat(try, "/include");
137 if (_access(try, 0) == 0) {
148 #define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))
152 static void tcc_add_systemdir(TCCState
*s
)
155 GetSystemDirectory(buf
, sizeof buf
);
156 tcc_add_library_path(s
, normalize_slashes(buf
));
161 /********************************************************/
162 #if CONFIG_TCC_SEMLOCK
164 ST_FUNC
void wait_sem(TCCSem
*p
)
167 InitializeCriticalSection(&p
->cr
), p
->init
= 1;
168 EnterCriticalSection(&p
->cr
);
170 ST_FUNC
void post_sem(TCCSem
*p
)
172 LeaveCriticalSection(&p
->cr
);
174 #elif defined __APPLE__
175 /* Half-compatible MacOS doesn't have non-shared (process local)
176 semaphores. Use the dispatch framework for lightweight locks. */
177 ST_FUNC
void wait_sem(TCCSem
*p
)
180 p
->sem
= dispatch_semaphore_create(1), p
->init
= 1;
181 dispatch_semaphore_wait(p
->sem
, DISPATCH_TIME_FOREVER
);
183 ST_FUNC
void post_sem(TCCSem
*p
)
185 dispatch_semaphore_signal(p
->sem
);
188 ST_FUNC
void wait_sem(TCCSem
*p
)
191 sem_init(&p
->sem
, 0, 1), p
->init
= 1;
192 while (sem_wait(&p
->sem
) < 0 && errno
== EINTR
);
194 ST_FUNC
void post_sem(TCCSem
*p
)
201 PUB_FUNC
void tcc_enter_state(TCCState
*s1
)
203 if (s1
->error_set_jmp_enabled
)
205 WAIT_SEM(&tcc_compile_sem
);
209 PUB_FUNC
void tcc_exit_state(TCCState
*s1
)
211 if (s1
->error_set_jmp_enabled
)
214 POST_SEM(&tcc_compile_sem
);
217 /********************************************************/
218 /* copy a string and truncate it. */
219 ST_FUNC
char *pstrcpy(char *buf
, size_t buf_size
, const char *s
)
226 q_end
= buf
+ buf_size
- 1;
238 /* strcat and truncate. */
239 ST_FUNC
char *pstrcat(char *buf
, size_t buf_size
, const char *s
)
244 pstrcpy(buf
+ len
, buf_size
- len
, s
);
248 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
250 memcpy(out
, in
, num
);
255 /* extract the basename of a file */
256 PUB_FUNC
char *tcc_basename(const char *name
)
258 char *p
= strchr(name
, 0);
259 while (p
> name
&& !IS_DIRSEP(p
[-1]))
264 /* extract extension part of a file
266 * (if no extension, return pointer to end-of-string)
268 PUB_FUNC
char *tcc_fileextension (const char *name
)
270 char *b
= tcc_basename(name
);
271 char *e
= strrchr(b
, '.');
272 return e
? e
: strchr(b
, 0);
275 ST_FUNC
char *tcc_load_text(int fd
)
277 int len
= lseek(fd
, 0, SEEK_END
);
278 char *buf
= load_data(fd
, 0, len
+ 1);
283 /********************************************************/
284 /* memory management */
290 void mem_error(const char *msg
)
292 fprintf(stderr
, "%s\n", msg
);
298 PUB_FUNC
void tcc_free(void *ptr
)
303 PUB_FUNC
void *tcc_malloc(unsigned long size
)
308 mem_error("memory full (malloc)");
312 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
315 ptr
= tcc_malloc(size
);
317 memset(ptr
, 0, size
);
321 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
324 ptr1
= realloc(ptr
, size
);
326 mem_error("memory full (realloc)");
330 PUB_FUNC
char *tcc_strdup(const char *str
)
333 ptr
= tcc_malloc(strlen(str
) + 1);
340 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
341 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
342 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
343 #define MEM_DEBUG_FILE_LEN 40
344 #define MEM_DEBUG_CHECK3(header) \
345 ((mem_debug_header_t*)((char*)header + header->size))->magic3
346 #define MEM_USER_PTR(header) \
347 ((char *)header + offsetof(mem_debug_header_t, magic3))
348 #define MEM_HEADER_PTR(ptr) \
349 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
351 struct mem_debug_header
{
354 struct mem_debug_header
*prev
;
355 struct mem_debug_header
*next
;
357 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
359 ALIGNED(16) unsigned char magic3
[4];
362 typedef struct mem_debug_header mem_debug_header_t
;
364 TCC_SEM(static mem_sem
);
365 static mem_debug_header_t
*mem_debug_chain
;
366 static unsigned mem_cur_size
;
367 static unsigned mem_max_size
;
368 static int nb_states
;
370 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
372 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
373 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
374 header
->magic2
!= MEM_DEBUG_MAGIC2
||
375 read32le(MEM_DEBUG_CHECK3(header
)) != MEM_DEBUG_MAGIC3
||
376 header
->size
== (unsigned)-1) {
377 fprintf(stderr
, "%s check failed\n", msg
);
378 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
379 fprintf(stderr
, "%s:%u: block allocated here.\n",
380 header
->file_name
, header
->line_num
);
386 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
389 mem_debug_header_t
*header
;
391 header
= malloc(sizeof(mem_debug_header_t
) + size
);
393 mem_error("memory full (malloc)");
395 header
->magic1
= MEM_DEBUG_MAGIC1
;
396 header
->magic2
= MEM_DEBUG_MAGIC2
;
398 write32le(MEM_DEBUG_CHECK3(header
), MEM_DEBUG_MAGIC3
);
399 header
->line_num
= line
;
400 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
401 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
402 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
405 header
->next
= mem_debug_chain
;
408 header
->next
->prev
= header
;
409 mem_debug_chain
= header
;
410 mem_cur_size
+= size
;
411 if (mem_cur_size
> mem_max_size
)
412 mem_max_size
= mem_cur_size
;
415 return MEM_USER_PTR(header
);
418 PUB_FUNC
void tcc_free_debug(void *ptr
)
420 mem_debug_header_t
*header
;
423 header
= malloc_check(ptr
, "tcc_free");
426 mem_cur_size
-= header
->size
;
427 header
->size
= (unsigned)-1;
429 header
->next
->prev
= header
->prev
;
431 header
->prev
->next
= header
->next
;
432 if (header
== mem_debug_chain
)
433 mem_debug_chain
= header
->next
;
438 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
441 ptr
= tcc_malloc_debug(size
,file
,line
);
442 memset(ptr
, 0, size
);
446 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
448 mem_debug_header_t
*header
;
449 int mem_debug_chain_update
= 0;
451 return tcc_malloc_debug(size
, file
, line
);
452 header
= malloc_check(ptr
, "tcc_realloc");
455 mem_cur_size
-= header
->size
;
456 mem_debug_chain_update
= (header
== mem_debug_chain
);
457 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
459 mem_error("memory full (realloc)");
461 write32le(MEM_DEBUG_CHECK3(header
), MEM_DEBUG_MAGIC3
);
463 header
->next
->prev
= header
;
465 header
->prev
->next
= header
;
466 if (mem_debug_chain_update
)
467 mem_debug_chain
= header
;
468 mem_cur_size
+= size
;
469 if (mem_cur_size
> mem_max_size
)
470 mem_max_size
= mem_cur_size
;
473 return MEM_USER_PTR(header
);
476 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
479 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
484 PUB_FUNC
void tcc_memcheck(int d
)
488 if (0 == nb_states
&& mem_cur_size
) {
489 mem_debug_header_t
*header
= mem_debug_chain
;
491 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
492 mem_cur_size
, mem_max_size
);
494 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
495 header
->file_name
, header
->line_num
, header
->size
);
496 header
= header
->next
;
500 mem_debug_chain
= NULL
;
508 #endif /* MEM_DEBUG */
511 # define realpath(file, buf) _fullpath(buf, file, 260)
514 /* for #pragma once */
515 ST_FUNC
int normalized_PATHCMP(const char *f1
, const char *f2
)
519 if (!!(p1
= realpath(f1
, NULL
))) {
520 if (!!(p2
= realpath(f2
, NULL
))) {
521 ret
= PATHCMP(p1
, p2
);
522 free(p2
); /* using original free */
529 #define free(p) use_tcc_free(p)
530 #define malloc(s) use_tcc_malloc(s)
531 #define realloc(p, s) use_tcc_realloc(p, s)
533 /********************************************************/
536 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
542 pp
= *(void ***)ptab
;
543 /* every power of two we double array size */
544 if ((nb
& (nb
- 1)) == 0) {
549 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
556 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
559 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
562 tcc_free(*(void**)pp
);
566 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
574 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
575 if (c
== '{' && p
[1] && p
[2] == '}') {
578 cstr_cat(&str
, s
->tcc_lib_path
, -1);
580 cstr_cat(&str
, CONFIG_SYSROOT
, -1);
581 if (c
== 'f' && file
) {
582 /* substitute current file's dir */
583 const char *f
= file
->true_filename
;
584 const char *b
= tcc_basename(f
);
586 cstr_cat(&str
, f
, b
- f
- 1);
588 cstr_cat(&str
, ".", 1);
595 cstr_ccat(&str
, '\0');
596 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
603 /********************************************************/
604 /* warning / error */
606 /* warn_... option bits */
607 #define WARN_ON 1 /* warning is on (-Woption) */
608 #define WARN_ERR 2 /* warning is an error (-Werror=option) */
609 #define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
612 enum { ERROR_WARN
, ERROR_NOABORT
, ERROR_ERROR
};
614 static void error1(int mode
, const char *fmt
, va_list ap
)
616 BufferedFile
**pf
, *f
;
617 TCCState
*s1
= tcc_state
;
622 if (mode
== ERROR_WARN
) {
626 /* handle tcc_warning_c(warn_option)(fmt, ...) */
627 int wopt
= *(&s1
->warn_none
+ s1
->warn_num
);
629 if (0 == (wopt
& WARN_ON
))
642 if (s1
->error_set_jmp_enabled
) { /* we're called while parsing a file */
643 /* use upper file if inline ":asm:" or token ":paste:" */
644 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
648 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
649 cstr_printf(&cs
, "In file included from %s:%d:\n",
650 (*pf
)->filename
, (*pf
)->line_num
- 1);
651 cstr_printf(&cs
, "%s:%d: ",
652 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
653 } else if (s1
->current_filename
) {
654 cstr_printf(&cs
, "%s: ", s1
->current_filename
);
657 cstr_printf(&cs
, "tcc: ");
658 cstr_printf(&cs
, mode
== ERROR_WARN
? "warning: " : "error: ");
659 cstr_vprintf(&cs
, fmt
, ap
);
660 if (!s1
|| !s1
->error_func
) {
661 /* default case: stderr */
662 if (s1
&& s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
663 printf("\n"); /* print a newline during tcc -E */
664 fflush(stdout
); /* flush -v output */
665 fprintf(stderr
, "%s\n", (char*)cs
.data
);
666 fflush(stderr
); /* print error/warning now (win32) */
668 s1
->error_func(s1
->error_opaque
, (char*)cs
.data
);
671 if (mode
!= ERROR_WARN
)
673 if (mode
== ERROR_ERROR
&& s1
->error_set_jmp_enabled
) {
675 tcc_free(*(void**)stk_data
[--nb_stk_data
]);
676 longjmp(s1
->error_jmp_buf
, 1);
680 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
, TCCErrorFunc error_func
)
682 s
->error_opaque
= error_opaque
;
683 s
->error_func
= error_func
;
686 LIBTCCAPI TCCErrorFunc
tcc_get_error_func(TCCState
*s
)
688 return s
->error_func
;
691 LIBTCCAPI
void *tcc_get_error_opaque(TCCState
*s
)
693 return s
->error_opaque
;
696 /* error without aborting current compilation */
697 PUB_FUNC
int _tcc_error_noabort(const char *fmt
, ...)
701 error1(ERROR_NOABORT
, fmt
, ap
);
707 PUB_FUNC
void _tcc_error(const char *fmt
, ...)
711 error1(ERROR_ERROR
, fmt
, ap
);
714 #define _tcc_error use_tcc_error_noabort
716 PUB_FUNC
void _tcc_warning(const char *fmt
, ...)
720 error1(ERROR_WARN
, fmt
, ap
);
725 /********************************************************/
728 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
731 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
733 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
734 bf
->buf_ptr
= bf
->buffer
;
735 bf
->buf_end
= bf
->buffer
+ initlen
;
736 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
737 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
739 normalize_slashes(bf
->filename
);
741 bf
->true_filename
= bf
->filename
;
743 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
747 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
750 ST_FUNC
void tcc_close(void)
752 TCCState
*s1
= tcc_state
;
753 BufferedFile
*bf
= file
;
756 total_lines
+= bf
->line_num
- 1;
758 if (bf
->true_filename
!= bf
->filename
)
759 tcc_free(bf
->true_filename
);
764 static int _tcc_open(TCCState
*s1
, const char *filename
)
767 if (strcmp(filename
, "-") == 0)
768 fd
= 0, filename
= "<stdin>";
770 fd
= open(filename
, O_RDONLY
| O_BINARY
);
771 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
772 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
773 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
777 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
779 int fd
= _tcc_open(s1
, filename
);
782 tcc_open_bf(s1
, filename
, 0);
787 /* compile the file opened in 'file'. Return non zero if errors. */
788 static int tcc_compile(TCCState
*s1
, int filetype
, const char *str
, int fd
)
790 /* Here we enter the code section where we use the global variables for
791 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
792 Other threads need to wait until we're done.
794 Alternatively we could use thread local storage for those global
795 variables, which may or may not have advantages */
798 s1
->error_set_jmp_enabled
= 1;
800 if (setjmp(s1
->error_jmp_buf
) == 0) {
804 int len
= strlen(str
);
805 tcc_open_bf(s1
, "<string>", len
);
806 memcpy(file
->buffer
, str
, len
);
808 tcc_open_bf(s1
, str
, 0);
812 preprocess_start(s1
, filetype
);
815 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
818 tccelf_begin_file(s1
);
819 if (filetype
& (AFF_TYPE_ASM
| AFF_TYPE_ASMPP
)) {
820 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
829 s1
->error_set_jmp_enabled
= 0;
831 return s1
->nb_errors
!= 0 ? -1 : 0;
834 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
836 return tcc_compile(s
, s
->filetype
, str
, -1);
839 /* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
840 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
843 if (NULL
== (eq
= strchr(sym
, '=')))
846 value
= *eq
? eq
+ 1 : "1";
847 cstr_printf(&s1
->cmdline_defs
, "#define %.*s %s\n", (int)(eq
-sym
), sym
, value
);
850 /* undefine a preprocessor symbol */
851 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
853 cstr_printf(&s1
->cmdline_defs
, "#undef %s\n", sym
);
857 LIBTCCAPI TCCState
*tcc_new(void)
861 s
= tcc_mallocz(sizeof(TCCState
));
873 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
874 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
875 s
->warn_implicit_function_declaration
= 1;
876 s
->warn_discarded_qualifiers
= 1;
877 s
->ms_extensions
= 1;
879 #ifdef CHAR_IS_UNSIGNED
880 s
->char_is_unsigned
= 1;
882 #ifdef TCC_TARGET_I386
885 /* enable this if you want symbols with leading underscore on windows: */
886 #if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
887 s
->leading_underscore
= 1;
889 #ifdef TCC_TARGET_ARM
890 s
->float_abi
= ARM_FLOAT_ABI
;
892 #ifdef CONFIG_NEW_DTAGS
893 s
->enable_new_dtags
= 1;
896 /* might be used in error() before preprocess_start() */
897 s
->include_stack_ptr
= s
->include_stack
;
899 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
903 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
908 /* free library paths */
909 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
910 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
912 /* free include paths */
913 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
914 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
916 tcc_free(s1
->tcc_lib_path
);
917 tcc_free(s1
->soname
);
919 tcc_free(s1
->elf_entryname
);
920 tcc_free(s1
->init_symbol
);
921 tcc_free(s1
->fini_symbol
);
922 tcc_free(s1
->mapfile
);
923 tcc_free(s1
->outfile
);
924 tcc_free(s1
->deps_outfile
);
925 #if defined TCC_TARGET_MACHO
926 tcc_free(s1
->install_name
);
928 dynarray_reset(&s1
->files
, &s1
->nb_files
);
929 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
930 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
931 dynarray_reset(&s1
->argv
, &s1
->argc
);
932 cstr_free(&s1
->cmdline_defs
);
933 cstr_free(&s1
->cmdline_incl
);
934 cstr_free(&s1
->linker_arg
);
936 /* free runtime memory */
939 tcc_free(s1
->dState
);
946 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
948 #ifdef CONFIG_TCC_PIE
949 if (output_type
== TCC_OUTPUT_EXE
)
950 output_type
|= TCC_OUTPUT_DYN
;
952 s
->output_type
= output_type
;
955 /* default include paths */
956 /* -isystem paths have already been handled */
957 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
960 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
967 /* add debug sections */
970 #ifdef CONFIG_TCC_BCHECK
971 if (s
->do_bounds_check
) {
972 /* if bound checking, then add corresponding sections */
973 tccelf_bounds_new(s
);
977 if (output_type
== TCC_OUTPUT_OBJ
) {
978 /* always elf for objects */
979 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
983 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
987 /* allow linking with system dll's directly */
988 tcc_add_systemdir(s
);
990 /* target PE has its own startup code in libtcc1.a */
993 #elif defined TCC_TARGET_MACHO
994 # ifdef TCC_IS_NATIVE
995 tcc_add_macos_sdkpath(s
);
997 /* Mach-O with LC_MAIN doesn't need any crt startup code. */
1001 /* paths for crt objects */
1002 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
1004 /* add libc crt1/crti objects */
1005 if (output_type
!= TCC_OUTPUT_MEMORY
&& !s
->nostdlib
) {
1006 #if TARGETOS_OpenBSD
1007 if (output_type
!= TCC_OUTPUT_DLL
)
1008 tcc_add_crt(s
, "crt0.o");
1009 if (output_type
== TCC_OUTPUT_DLL
)
1010 tcc_add_crt(s
, "crtbeginS.o");
1012 tcc_add_crt(s
, "crtbegin.o");
1013 #elif TARGETOS_FreeBSD
1014 if (output_type
!= TCC_OUTPUT_DLL
)
1015 tcc_add_crt(s
, "crt1.o");
1016 tcc_add_crt(s
, "crti.o");
1018 tcc_add_crt(s
, "crtbeginT.o");
1019 else if (output_type
& TCC_OUTPUT_DYN
)
1020 tcc_add_crt(s
, "crtbeginS.o");
1022 tcc_add_crt(s
, "crtbegin.o");
1023 #elif TARGETOS_NetBSD
1024 if (output_type
!= TCC_OUTPUT_DLL
)
1025 tcc_add_crt(s
, "crt0.o");
1026 tcc_add_crt(s
, "crti.o");
1028 tcc_add_crt(s
, "crtbeginT.o");
1029 else if (output_type
& TCC_OUTPUT_DYN
)
1030 tcc_add_crt(s
, "crtbeginS.o");
1032 tcc_add_crt(s
, "crtbegin.o");
1033 #elif defined TARGETOS_ANDROID
1034 if (output_type
!= TCC_OUTPUT_DLL
)
1035 tcc_add_crt(s
, "crtbegin_dynamic.o");
1037 tcc_add_crt(s
, "crtbegin_so.o");
1039 if (output_type
!= TCC_OUTPUT_DLL
)
1040 tcc_add_crt(s
, "crt1.o");
1041 tcc_add_crt(s
, "crti.o");
1048 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
1050 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
1054 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
1056 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
1060 /* add/update a 'DLLReference', Just find if level == -1 */
1061 ST_FUNC DLLReference
*tcc_add_dllref(TCCState
*s1
, const char *dllname
, int level
)
1063 DLLReference
*ref
= NULL
;
1065 for (i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
1066 if (0 == strcmp(s1
->loaded_dlls
[i
]->name
, dllname
)) {
1067 ref
= s1
->loaded_dlls
[i
];
1073 if (level
< ref
->level
)
1078 ref
= tcc_mallocz(sizeof(DLLReference
) + strlen(dllname
));
1079 strcpy(ref
->name
, dllname
);
1080 dynarray_add(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, ref
);
1082 ref
->index
= s1
->nb_loaded_dlls
;
1086 /* OpenBSD: choose latest from libxxx.so.x.y versions */
1087 #if defined TARGETOS_OpenBSD && !defined _WIN32
1089 static int tcc_glob_so(TCCState
*s1
, const char *pattern
, char *buf
, int size
)
1094 int i
, v
, v1
, v2
, v3
;
1096 star
= strchr(pattern
, '*');
1097 if (!star
|| glob(pattern
, 0, NULL
, &g
))
1099 for (v
= -1, i
= 0; i
< g
.gl_pathc
; ++i
) {
1101 if (2 != sscanf(p
+ (star
- pattern
), "%d.%d.%d", &v1
, &v2
, &v3
))
1103 if ((v1
= v1
* 1000 + v2
) > v
)
1104 v
= v1
, pstrcpy(buf
, size
, p
);
1111 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
1115 #if defined TARGETOS_OpenBSD && !defined _WIN32
1117 if (tcc_glob_so(s1
, filename
, buf
, sizeof buf
) >= 0)
1121 /* ignore binary files with -E */
1122 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
1123 && (flags
& AFF_TYPE_BIN
))
1127 fd
= _tcc_open(s1
, filename
);
1129 if (flags
& AFF_PRINT_ERROR
)
1130 tcc_error_noabort("file '%s' not found", filename
);
1134 s1
->current_filename
= filename
;
1135 if (flags
& AFF_TYPE_BIN
) {
1139 obj_type
= tcc_object_type(fd
, &ehdr
);
1140 lseek(fd
, 0, SEEK_SET
);
1144 case AFF_BINTYPE_REL
:
1145 ret
= tcc_load_object_file(s1
, fd
, 0);
1148 case AFF_BINTYPE_AR
:
1149 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
1152 #ifdef TCC_TARGET_PE
1154 ret
= pe_load_file(s1
, fd
, filename
);
1157 #elif defined TCC_TARGET_MACHO
1158 case AFF_BINTYPE_DYN
:
1160 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1161 #ifdef TCC_IS_NATIVE
1163 const char* soname
= filename
;
1164 if (obj_type
!= AFF_BINTYPE_DYN
)
1165 soname
= macho_tbd_soname(filename
);
1166 dl
= dlopen(soname
, RTLD_GLOBAL
| RTLD_LAZY
);
1168 tcc_add_dllref(s1
, soname
, 0)->handle
= dl
, ret
= 0;
1169 if (filename
!= soname
)
1170 tcc_free((void *)soname
);
1172 } else if (obj_type
== AFF_BINTYPE_DYN
) {
1173 ret
= macho_load_dll(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1175 ret
= macho_load_tbd(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1180 const char *ext
= tcc_fileextension(filename
);
1181 if (!strcmp(ext
, ".tbd"))
1182 goto case_dyn_or_tbd
;
1183 if (!strcmp(ext
, ".dylib")) {
1184 obj_type
= AFF_BINTYPE_DYN
;
1185 goto case_dyn_or_tbd
;
1191 case AFF_BINTYPE_DYN
:
1192 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1193 #ifdef TCC_IS_NATIVE
1194 void* dl
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
1196 tcc_add_dllref(s1
, filename
, 0)->handle
= dl
, ret
= 0;
1199 ret
= tcc_load_dll(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1203 /* as GNU ld, consider it is an ld script if not recognized */
1204 ret
= tcc_load_ldscript(s1
, fd
);
1207 #endif /* pe / macos / unix */
1211 tcc_error_noabort("%s: unrecognized file type", filename
);
1214 #ifdef TCC_TARGET_COFF
1215 case AFF_BINTYPE_C67
:
1216 ret
= tcc_load_coff(s1
, fd
);
1222 /* update target deps */
1223 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
, tcc_strdup(filename
));
1224 ret
= tcc_compile(s1
, flags
, filename
, fd
);
1226 s1
->current_filename
= NULL
;
1230 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1232 int filetype
= s
->filetype
;
1233 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1234 /* use a file extension to detect a filetype */
1235 const char *ext
= tcc_fileextension(filename
);
1238 if (!strcmp(ext
, "S"))
1239 filetype
= AFF_TYPE_ASMPP
;
1240 else if (!strcmp(ext
, "s"))
1241 filetype
= AFF_TYPE_ASM
;
1242 else if (!PATHCMP(ext
, "c")
1243 || !PATHCMP(ext
, "h")
1244 || !PATHCMP(ext
, "i"))
1245 filetype
= AFF_TYPE_C
;
1247 filetype
|= AFF_TYPE_BIN
;
1249 filetype
= AFF_TYPE_C
;
1252 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1255 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1257 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1261 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1262 const char *filename
, int flags
, char **paths
, int nb_paths
)
1267 for(i
= 0; i
< nb_paths
; i
++) {
1268 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1269 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1275 /* find and load a dll. Return non zero if not found */
1276 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1278 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1279 s
->library_paths
, s
->nb_library_paths
);
1282 /* find [cross-]libtcc1.a and tcc helper objects in library path */
1283 ST_FUNC
void tcc_add_support(TCCState
*s1
, const char *filename
)
1286 if (CONFIG_TCC_CROSSPREFIX
[0])
1287 filename
= strcat(strcpy(buf
, CONFIG_TCC_CROSSPREFIX
), filename
);
1288 if (tcc_add_dll(s1
, filename
, 0) < 0)
1289 tcc_error_noabort("%s not found", filename
);
1292 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
1293 ST_FUNC
int tcc_add_crt(TCCState
*s1
, const char *filename
)
1295 if (-1 == tcc_add_library_internal(s1
, "%s/%s",
1296 filename
, 0, s1
->crt_paths
, s1
->nb_crt_paths
))
1297 return tcc_error_noabort("file '%s' not found", filename
);
1302 /* the library name is the same as the argument of the '-l' option */
1303 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1305 #if defined TCC_TARGET_PE
1306 static const char * const libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1307 const char * const *pp
= s
->static_link
? libs
+ 4 : libs
;
1308 #elif defined TCC_TARGET_MACHO
1309 static const char * const libs
[] = { "%s/lib%s.dylib", "%s/lib%s.tbd", "%s/lib%s.a", NULL
};
1310 const char * const *pp
= s
->static_link
? libs
+ 2 : libs
;
1311 #elif defined TARGETOS_OpenBSD
1312 static const char * const libs
[] = { "%s/lib%s.so.*", "%s/lib%s.a", NULL
};
1313 const char * const *pp
= s
->static_link
? libs
+ 1 : libs
;
1315 static const char * const libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1316 const char * const *pp
= s
->static_link
? libs
+ 1 : libs
;
1318 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1320 if (0 == tcc_add_library_internal(s
, *pp
,
1321 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1328 PUB_FUNC
int tcc_add_library_err(TCCState
*s1
, const char *libname
)
1330 int ret
= tcc_add_library(s1
, libname
);
1332 tcc_error_noabort("library '%s' not found", libname
);
1336 /* handle #pragma comment(lib,) */
1337 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1340 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1341 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1344 LIBTCCAPI
int tcc_add_symbol(TCCState
*s1
, const char *name
, const void *val
)
1346 #ifdef TCC_TARGET_PE
1347 /* On x86_64 'val' might not be reachable with a 32bit offset.
1348 So it is handled here as if it were in a DLL. */
1349 pe_putimport(s1
, 0, name
, (uintptr_t)val
);
1352 if (s1
->leading_underscore
) {
1354 pstrcpy(buf
+ 1, sizeof(buf
) - 1, name
);
1357 set_global_sym(s1
, name
, NULL
, (addr_t
)(uintptr_t)val
); /* NULL: SHN_ABS */
1362 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1364 tcc_free(s
->tcc_lib_path
);
1365 s
->tcc_lib_path
= tcc_strdup(path
);
1368 /********************************************************/
1369 /* options parser */
1371 static int strstart(const char *val
, const char **str
)
1386 /* Like strstart, but automatically takes into account that ld options can
1388 * - start with double or single dash (e.g. '--soname' or '-soname')
1389 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1390 * or '-Wl,-soname=x.so')
1392 * you provide `val` always in 'option[=]' form (no leading -)
1394 static int link_option(const char *str
, const char *val
, const char **ptr
)
1399 /* there should be 1 or 2 dashes */
1405 /* then str & val should match (potentially up to '=') */
1412 if (strstart("no-", &p
))
1416 while (*q
!= '\0' && *q
!= '=') {
1423 /* '=' near eos means ',' or '=' is ok */
1427 if (*p
!= ',' && *p
!= '=')
1437 static const char *skip_linker_arg(const char **str
)
1439 const char *s1
= *str
;
1440 const char *s2
= strchr(s1
, ',');
1441 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1445 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1451 p
[l
= strlen(p
)] = sep
, ++l
;
1452 skip_linker_arg(&q
);
1453 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1456 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1458 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1460 strcpy(f
->name
, filename
);
1461 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1464 /* set linker options */
1465 static int tcc_set_linker(TCCState
*s
, const char *option
)
1470 const char *p
= NULL
;
1475 if (link_option(option
, "Bsymbolic", &p
)) {
1477 } else if (link_option(option
, "nostdlib", &p
)) {
1479 } else if (link_option(option
, "e=", &p
)
1480 || link_option(option
, "entry=", &p
)) {
1481 copy_linker_arg(&s
->elf_entryname
, p
, 0);
1482 } else if (link_option(option
, "fini=", &p
)) {
1483 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1485 } else if (link_option(option
, "image-base=", &p
)
1486 || link_option(option
, "Ttext=", &p
)) {
1487 s
->text_addr
= strtoull(p
, &end
, 16);
1488 s
->has_text_addr
= 1;
1489 } else if (link_option(option
, "init=", &p
)) {
1490 copy_linker_arg(&s
->init_symbol
, p
, 0);
1492 } else if (link_option(option
, "Map=", &p
)) {
1493 copy_linker_arg(&s
->mapfile
, p
, 0);
1495 } else if (link_option(option
, "oformat=", &p
)) {
1496 #if defined(TCC_TARGET_PE)
1497 if (strstart("pe-", &p
)) {
1499 if (strstart("elf64-", &p
)) {
1501 if (strstart("elf32-", &p
)) {
1503 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1504 } else if (!strcmp(p
, "binary")) {
1505 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1506 #ifdef TCC_TARGET_COFF
1507 } else if (!strcmp(p
, "coff")) {
1508 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1513 } else if (link_option(option
, "as-needed", &p
)) {
1515 } else if (link_option(option
, "O", &p
)) {
1517 } else if (link_option(option
, "export-all-symbols", &p
)) {
1519 } else if (link_option(option
, "export-dynamic", &p
)) {
1521 } else if (link_option(option
, "rpath=", &p
)) {
1522 copy_linker_arg(&s
->rpath
, p
, ':');
1523 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1524 s
->enable_new_dtags
= 1;
1525 } else if (link_option(option
, "section-alignment=", &p
)) {
1526 s
->section_align
= strtoul(p
, &end
, 16);
1527 } else if (link_option(option
, "soname=", &p
)) {
1528 copy_linker_arg(&s
->soname
, p
, 0);
1529 } else if (link_option(option
, "install_name=", &p
)) {
1530 copy_linker_arg(&s
->soname
, p
, 0);
1531 #ifdef TCC_TARGET_PE
1532 } else if (link_option(option
, "large-address-aware", &p
)) {
1533 s
->pe_characteristics
|= 0x20;
1534 } else if (link_option(option
, "file-alignment=", &p
)) {
1535 s
->pe_file_align
= strtoul(p
, &end
, 16);
1536 } else if (link_option(option
, "stack=", &p
)) {
1537 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1538 } else if (link_option(option
, "subsystem=", &p
)) {
1539 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1540 if (!strcmp(p
, "native")) {
1541 s
->pe_subsystem
= 1;
1542 } else if (!strcmp(p
, "console")) {
1543 s
->pe_subsystem
= 3;
1544 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1545 s
->pe_subsystem
= 2;
1546 } else if (!strcmp(p
, "posix")) {
1547 s
->pe_subsystem
= 7;
1548 } else if (!strcmp(p
, "efiapp")) {
1549 s
->pe_subsystem
= 10;
1550 } else if (!strcmp(p
, "efiboot")) {
1551 s
->pe_subsystem
= 11;
1552 } else if (!strcmp(p
, "efiruntime")) {
1553 s
->pe_subsystem
= 12;
1554 } else if (!strcmp(p
, "efirom")) {
1555 s
->pe_subsystem
= 13;
1556 #elif defined(TCC_TARGET_ARM)
1557 if (!strcmp(p
, "wince")) {
1558 s
->pe_subsystem
= 9;
1563 #ifdef TCC_TARGET_MACHO
1564 } else if (link_option(option
, "all_load", &p
)) {
1565 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1566 } else if (link_option(option
, "force_load", &p
)) {
1567 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1568 args_parser_add_file(s
, p
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1570 } else if (link_option(option
, "single_module", &p
)) {
1573 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1575 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1577 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1578 } else if (link_option(option
, "z=", &p
)) {
1584 return tcc_error_noabort("unsupported linker option '%s'", option
);
1587 tcc_warning_c(warn_unsupported
)("unsupported linker option '%s'", option
);
1588 option
= skip_linker_arg(&p
);
1593 typedef struct TCCOption
{
1600 TCC_OPTION_ignored
= 0,
1617 TCC_OPTION_dumpversion
,
1629 TCC_OPTION_mfloat_abi
,
1633 TCC_OPTION_iwithprefix
,
1635 TCC_OPTION_nostdinc
,
1636 TCC_OPTION_nostdlib
,
1637 TCC_OPTION_print_search_dirs
,
1638 TCC_OPTION_rdynamic
,
1651 TCC_OPTION_dynamiclib
,
1652 TCC_OPTION_flat_namespace
,
1653 TCC_OPTION_two_levelnamespace
,
1654 TCC_OPTION_undefined
,
1655 TCC_OPTION_install_name
,
1656 TCC_OPTION_compatibility_version
,
1657 TCC_OPTION_current_version
,
1660 #define TCC_OPTION_HAS_ARG 0x0001
1661 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1663 static const TCCOption tcc_options
[] = {
1664 { "h", TCC_OPTION_HELP
, 0 },
1665 { "-help", TCC_OPTION_HELP
, 0 },
1666 { "?", TCC_OPTION_HELP
, 0 },
1667 { "hh", TCC_OPTION_HELP2
, 0 },
1668 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1669 { "-version", TCC_OPTION_v
, 0 }, /* handle as verbose, also prints version*/
1670 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1671 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1672 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1673 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1674 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1675 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1676 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1677 { "bench", TCC_OPTION_bench
, 0 },
1678 #ifdef CONFIG_TCC_BACKTRACE
1679 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1681 #ifdef CONFIG_TCC_BCHECK
1682 { "b", TCC_OPTION_b
, 0 },
1684 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1685 #ifdef TCC_TARGET_MACHO
1686 { "compatibility_version", TCC_OPTION_compatibility_version
, TCC_OPTION_HAS_ARG
},
1687 { "current_version", TCC_OPTION_current_version
, TCC_OPTION_HAS_ARG
},
1689 { "c", TCC_OPTION_c
, 0 },
1690 #ifdef TCC_TARGET_MACHO
1691 { "dynamiclib", TCC_OPTION_dynamiclib
, 0 },
1693 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1694 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1695 { "static", TCC_OPTION_static
, 0 },
1696 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1697 { "shared", TCC_OPTION_shared
, 0 },
1698 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1699 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1700 { "pthread", TCC_OPTION_pthread
, 0},
1701 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1702 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1703 { "r", TCC_OPTION_r
, 0 },
1704 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1705 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1706 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1707 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1708 #ifdef TCC_TARGET_ARM
1709 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1711 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1712 #ifdef TCC_TARGET_MACHO
1713 { "flat_namespace", TCC_OPTION_flat_namespace
, 0 },
1715 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1716 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1717 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1718 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1719 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1720 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1721 { "w", TCC_OPTION_w
, 0 },
1722 { "E", TCC_OPTION_E
, 0},
1723 { "M", TCC_OPTION_M
, 0},
1724 { "MD", TCC_OPTION_MD
, 0},
1725 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1726 { "MM", TCC_OPTION_MM
, 0},
1727 { "MMD", TCC_OPTION_MMD
, 0},
1728 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1729 { "ar", TCC_OPTION_ar
, 0},
1730 #ifdef TCC_TARGET_PE
1731 { "impdef", TCC_OPTION_impdef
, 0},
1733 #ifdef TCC_TARGET_MACHO
1734 { "install_name", TCC_OPTION_install_name
, TCC_OPTION_HAS_ARG
},
1735 { "two_levelnamespace", TCC_OPTION_two_levelnamespace
, 0 },
1736 { "undefined", TCC_OPTION_undefined
, TCC_OPTION_HAS_ARG
},
1738 /* ignored (silently, except after -Wunsupported) */
1739 { "arch", 0, TCC_OPTION_HAS_ARG
},
1741 { "-param", 0, TCC_OPTION_HAS_ARG
},
1742 { "pedantic", 0, 0 },
1745 { "traditional", 0, 0 },
1749 typedef struct FlagDef
{
1755 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1756 #define FD_INVERT 0x0002 /* invert value before storing */
1758 static const FlagDef options_W
[] = {
1759 { offsetof(TCCState
, warn_all
), WD_ALL
, "all" },
1760 { offsetof(TCCState
, warn_error
), 0, "error" },
1761 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1762 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1763 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
, "implicit-function-declaration" },
1764 { offsetof(TCCState
, warn_discarded_qualifiers
), WD_ALL
, "discarded-qualifiers" },
1768 static const FlagDef options_f
[] = {
1769 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1770 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1771 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1772 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1773 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1774 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1775 { offsetof(TCCState
, test_coverage
), 0, "test-coverage" },
1779 static const FlagDef options_m
[] = {
1780 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1781 #ifdef TCC_TARGET_X86_64
1782 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1787 static int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1789 int value
, mask
, ret
;
1794 r
= name
, value
= !strstart("no-", &r
), mask
= 0;
1796 /* when called with options_W, look for -W[no-]error=<option> */
1797 if ((flags
->flags
& WD_ALL
) && strstart("error=", &r
))
1798 value
= value
? WARN_ON
|WARN_ERR
: WARN_NOE
, mask
= WARN_ON
;
1800 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1802 if (strcmp(r
, p
->name
))
1805 if (0 == (p
->flags
& WD_ALL
))
1809 f
= (unsigned char *)s
+ p
->offset
;
1810 *f
= (*f
& mask
) | (value
^ !!(p
->flags
& FD_INVERT
));
1814 if (strcmp(r
, "all"))
1821 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1826 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1832 while (c
= (unsigned char)*r
, c
) {
1834 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1836 } else if (c
== '"') {
1839 } else if (q
== 0 && c
<= ' ') {
1845 //printf("<%s>\n", str.data), fflush(stdout);
1846 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1853 /* read list file */
1854 static int args_parser_listfile(TCCState
*s
,
1855 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1863 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1865 return tcc_error_noabort("listfile '%s' not found", filename
);
1867 p
= tcc_load_text(fd
);
1868 for (i
= 0; i
< *pargc
; ++i
)
1870 args_parser_make_argv(p
, &argc
, &argv
);
1872 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1875 dynarray_reset(&s
->argv
, &s
->argc
);
1876 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1880 #if defined TCC_TARGET_MACHO
1881 static uint32_t parse_version(TCCState
*s1
, const char *version
)
1888 a
= strtoul(version
, &last
, 10);
1890 b
= strtoul(&last
[1], &last
, 10);
1892 c
= strtoul(&last
[1], &last
, 10);
1894 if (*last
|| a
> 0xffff || b
> 0xff || c
> 0xff)
1895 tcc_error_noabort("version a.b.c not correct: %s", version
);
1896 return (a
<< 16) | (b
<< 8) | c
;
1900 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1903 const TCCOption
*popt
;
1904 const char *optarg
, *r
;
1905 const char *run
= NULL
;
1907 int tool
= 0, arg_start
= 0, noaction
= optind
;
1908 char **argv
= *pargv
;
1911 cstr_reset(&s
->linker_arg
);
1913 while (optind
< argc
) {
1915 if (r
[0] == '@' && r
[1] != '\0') {
1916 if (args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
))
1922 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1927 if (r
[0] != '-' || r
[1] == '\0') {
1928 args_parser_add_file(s
, r
, s
->filetype
);
1931 if (tcc_set_options(s
, run
))
1933 arg_start
= optind
- 1;
1939 /* allow "tcc files... -run -- args ..." */
1940 if (r
[1] == '-' && r
[2] == '\0' && run
)
1943 /* find option in table */
1944 for(popt
= tcc_options
; ; ++popt
) {
1945 const char *p1
= popt
->name
;
1946 const char *r1
= r
+ 1;
1948 return tcc_error_noabort("invalid option -- '%s'", r
);
1949 if (!strstart(p1
, &r1
))
1952 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1953 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1956 return tcc_error_noabort("argument to '%s' is missing", r
);
1957 optarg
= argv
[optind
++];
1959 } else if (*r1
!= '\0')
1964 switch(popt
->index
) {
1965 case TCC_OPTION_HELP
:
1968 case TCC_OPTION_HELP2
:
1972 tcc_add_include_path(s
, optarg
);
1975 tcc_define_symbol(s
, optarg
, NULL
);
1978 tcc_undefine_symbol(s
, optarg
);
1981 tcc_add_library_path(s
, optarg
);
1984 /* set tcc utilities path (mainly for tcc development) */
1985 tcc_set_lib_path(s
, optarg
);
1989 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1992 case TCC_OPTION_pthread
:
1993 s
->option_pthread
= 1;
1995 case TCC_OPTION_bench
:
1998 #ifdef CONFIG_TCC_BACKTRACE
2000 s
->rt_num_callers
= atoi(optarg
);
2001 s
->do_backtrace
= 1;
2003 s
->dwarf
= DWARF_VERSION
;
2006 #ifdef CONFIG_TCC_BCHECK
2008 s
->do_bounds_check
= 1;
2009 s
->do_backtrace
= 1;
2011 s
->dwarf
= DWARF_VERSION
;
2016 s
->dwarf
= DWARF_VERSION
;
2018 if (strstart("dwarf", &optarg
))
2019 s
->dwarf
= (*optarg
) ? (0 - atoi(optarg
)) : DEFAULT_DWARF_VERSION
;
2025 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
2031 else if (*optarg
== 'M')
2033 else if (*optarg
== 't')
2035 else if (isnum(*optarg
))
2036 s
->g_debug
|= atoi(optarg
);
2038 goto unsupported_option
;
2040 case TCC_OPTION_static
:
2043 case TCC_OPTION_std
:
2044 if (strcmp(optarg
, "=c11") == 0)
2045 s
->cversion
= 201112;
2047 case TCC_OPTION_shared
:
2049 goto set_output_type
;
2050 case TCC_OPTION_soname
:
2051 s
->soname
= tcc_strdup(optarg
);
2055 tcc_warning("multiple -o option");
2056 tcc_free(s
->outfile
);
2058 s
->outfile
= tcc_strdup(optarg
);
2061 /* generate a .o merging several output files */
2064 goto set_output_type
;
2065 case TCC_OPTION_isystem
:
2066 tcc_add_sysinclude_path(s
, optarg
);
2068 case TCC_OPTION_include
:
2069 cstr_printf(&s
->cmdline_incl
, "#include \"%s\"\n", optarg
);
2071 case TCC_OPTION_nostdinc
:
2074 case TCC_OPTION_nostdlib
:
2077 case TCC_OPTION_run
:
2078 #ifndef TCC_IS_NATIVE
2079 return tcc_error_noabort("-run is not available in a cross compiler");
2082 x
= TCC_OUTPUT_MEMORY
;
2083 goto set_output_type
;
2086 do ++s
->verbose
; while (*optarg
++ == 'v');
2090 if (set_flag(s
, options_f
, optarg
) < 0)
2091 goto unsupported_option
;
2093 #ifdef TCC_TARGET_ARM
2094 case TCC_OPTION_mfloat_abi
:
2095 /* tcc doesn't support soft float yet */
2096 if (!strcmp(optarg
, "softfp")) {
2097 s
->float_abi
= ARM_SOFTFP_FLOAT
;
2098 } else if (!strcmp(optarg
, "hard"))
2099 s
->float_abi
= ARM_HARD_FLOAT
;
2101 return tcc_error_noabort("unsupported float abi '%s'", optarg
);
2105 if (set_flag(s
, options_m
, optarg
) < 0) {
2106 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
2107 goto unsupported_option
;
2108 if (PTR_SIZE
!= x
/8)
2115 if (optarg
[0] && set_flag(s
, options_W
, optarg
) < 0)
2116 goto unsupported_option
;
2121 case TCC_OPTION_rdynamic
:
2125 if (s
->linker_arg
.size
)
2126 ((char*)s
->linker_arg
.data
)[s
->linker_arg
.size
- 1] = ',';
2127 cstr_cat(&s
->linker_arg
, optarg
, 0);
2128 x
= tcc_set_linker(s
, s
->linker_arg
.data
);
2130 cstr_reset(&s
->linker_arg
);
2138 x
= TCC_OUTPUT_PREPROCESS
;
2139 goto set_output_type
;
2141 s
->Pflag
= atoi(optarg
) + 1;
2144 s
->include_sys_deps
= 1;
2148 if(!s
->deps_outfile
)
2149 s
->deps_outfile
= tcc_strdup("-");
2151 case TCC_OPTION_MMD
:
2156 s
->include_sys_deps
= 1;
2159 s
->deps_outfile
= tcc_strdup(optarg
);
2161 case TCC_OPTION_dumpversion
:
2162 printf ("%s\n", TCC_VERSION
);
2169 else if (*optarg
== 'a')
2171 else if (*optarg
== 'b')
2173 else if (*optarg
== 'n')
2176 tcc_warning("unsupported language '%s'", optarg
);
2177 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
2180 s
->optimize
= atoi(optarg
);
2182 case TCC_OPTION_print_search_dirs
:
2185 case TCC_OPTION_impdef
:
2188 #if defined TCC_TARGET_MACHO
2189 case TCC_OPTION_dynamiclib
:
2191 goto set_output_type
;
2192 case TCC_OPTION_flat_namespace
:
2194 case TCC_OPTION_two_levelnamespace
:
2196 case TCC_OPTION_undefined
:
2198 case TCC_OPTION_install_name
:
2199 s
->install_name
= tcc_strdup(optarg
);
2201 case TCC_OPTION_compatibility_version
:
2202 s
->compatibility_version
= parse_version(s
, optarg
);
2204 case TCC_OPTION_current_version
:
2205 s
->current_version
= parse_version(s
, optarg
);;
2211 arg_start
= optind
- 1;
2212 if (arg_start
!= noaction
)
2213 return tcc_error_noabort("cannot parse %s here", r
);
2218 tcc_warning_c(warn_unsupported
)("unsupported option '%s'", r
);
2222 if (s
->linker_arg
.size
) {
2223 r
= s
->linker_arg
.data
;
2226 *pargc
= argc
- arg_start
;
2227 *pargv
= argv
+ arg_start
;
2230 if (optind
!= noaction
)
2232 if (s
->verbose
== 2)
2233 return OPT_PRINT_DIRS
;
2239 LIBTCCAPI
int tcc_set_options(TCCState
*s
, const char *r
)
2243 args_parser_make_argv(r
, &argc
, &argv
);
2244 ret
= tcc_parse_args(s
, &argc
, &argv
, 0);
2245 dynarray_reset(&argv
, &argc
);
2246 return ret
< 0 ? ret
: 0;
2249 PUB_FUNC
void tcc_print_stats(TCCState
*s1
, unsigned total_time
)
2253 fprintf(stderr
, "# %d idents, %d lines, %u bytes\n"
2254 "# %0.3f s, %u lines/s, %0.1f MB/s\n",
2255 total_idents
, total_lines
, total_bytes
,
2256 (double)total_time
/1000,
2257 (unsigned)total_lines
*1000/total_time
,
2258 (double)total_bytes
/1000/total_time
);
2259 fprintf(stderr
, "# text %u, data.rw %u, data.ro %u, bss %u bytes\n",
2260 s1
->total_output
[0],
2261 s1
->total_output
[1],
2262 s1
->total_output
[2],
2266 fprintf(stderr
, "# %d bytes memory used\n", mem_max_size
);