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
26 #ifdef TCC_TARGET_I386
28 #include "i386-link.c"
30 #elif defined(TCC_TARGET_ARM)
34 #elif defined(TCC_TARGET_ARM64)
35 #include "arm64-gen.c"
36 #include "arm64-link.c"
38 #elif defined(TCC_TARGET_C67)
42 #elif defined(TCC_TARGET_X86_64)
43 #include "x86_64-gen.c"
44 #include "x86_64-link.c"
46 #elif defined(TCC_TARGET_RISCV64)
47 #include "riscv64-gen.c"
48 #include "riscv64-link.c"
49 #include "riscv64-asm.c"
59 #ifdef TCC_TARGET_MACHO
62 #endif /* ONE_SOURCE */
66 /********************************************************/
67 /* global variables */
69 /* XXX: get rid of this ASAP (or maybe not) */
70 ST_DATA
struct TCCState
*tcc_state
;
76 /********************************************************/
78 ST_FUNC
char *normalize_slashes(char *path
)
81 for (p
= path
; *p
; ++p
)
87 static HMODULE tcc_module
;
89 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
90 static void tcc_set_lib_path_w32(TCCState
*s
)
93 GetModuleFileNameA(tcc_module
, path
, sizeof path
);
94 p
= tcc_basename(normalize_slashes(strlwr(path
)));
98 tcc_set_lib_path(s
, path
);
102 static void tcc_add_systemdir(TCCState
*s
)
105 GetSystemDirectory(buf
, sizeof buf
);
106 tcc_add_library_path(s
, normalize_slashes(buf
));
111 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
113 if (DLL_PROCESS_ATTACH
== dwReason
)
120 /********************************************************/
121 #if CONFIG_TCC_SEMLOCK == 0
125 static int tcc_sem_init
;
126 static CRITICAL_SECTION tcc_cr
;
127 static void wait_sem(void)
130 InitializeCriticalSection(&tcc_cr
), tcc_sem_init
= 1;
131 EnterCriticalSection(&tcc_cr
);
133 #define WAIT_SEM() wait_sem()
134 #define POST_SEM() LeaveCriticalSection(&tcc_cr);
135 #elif defined __APPLE__
136 /* Half-compatible MacOS doesn't have non-shared (process local)
137 semaphores. Use the dispatch framework for lightweight locks. */
138 #include <dispatch/dispatch.h>
139 static int tcc_sem_init
;
140 static dispatch_semaphore_t tcc_sem
;
141 static void wait_sem(void)
144 tcc_sem
= dispatch_semaphore_create(1), tcc_sem_init
= 1;
145 dispatch_semaphore_wait(tcc_sem
, DISPATCH_TIME_FOREVER
);
147 #define WAIT_SEM() wait_sem()
148 #define POST_SEM() dispatch_semaphore_signal(tcc_sem)
150 #include <semaphore.h>
151 static int tcc_sem_init
;
152 static sem_t tcc_sem
;
153 static void wait_sem(void)
156 sem_init(&tcc_sem
, 0, 1), tcc_sem_init
= 1;
157 while (sem_wait (&tcc_sem
) < 0 && errno
== EINTR
);
159 #define WAIT_SEM() wait_sem()
160 #define POST_SEM() sem_post(&tcc_sem)
163 /********************************************************/
164 /* copy a string and truncate it. */
165 ST_FUNC
char *pstrcpy(char *buf
, size_t buf_size
, const char *s
)
172 q_end
= buf
+ buf_size
- 1;
184 /* strcat and truncate. */
185 ST_FUNC
char *pstrcat(char *buf
, size_t buf_size
, const char *s
)
190 pstrcpy(buf
+ len
, buf_size
- len
, s
);
194 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
196 memcpy(out
, in
, num
);
201 /* extract the basename of a file */
202 PUB_FUNC
char *tcc_basename(const char *name
)
204 char *p
= strchr(name
, 0);
205 while (p
> name
&& !IS_DIRSEP(p
[-1]))
210 /* extract extension part of a file
212 * (if no extension, return pointer to end-of-string)
214 PUB_FUNC
char *tcc_fileextension (const char *name
)
216 char *b
= tcc_basename(name
);
217 char *e
= strrchr(b
, '.');
218 return e
? e
: strchr(b
, 0);
221 /********************************************************/
222 /* memory management */
230 PUB_FUNC
void tcc_free(void *ptr
)
235 PUB_FUNC
void *tcc_malloc(unsigned long size
)
240 _tcc_error("memory full (malloc)");
244 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
247 ptr
= tcc_malloc(size
);
249 memset(ptr
, 0, size
);
253 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
256 ptr1
= realloc(ptr
, size
);
258 _tcc_error("memory full (realloc)");
262 PUB_FUNC
char *tcc_strdup(const char *str
)
265 ptr
= tcc_malloc(strlen(str
) + 1);
272 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
273 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
274 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
275 #define MEM_DEBUG_FILE_LEN 40
276 #define MEM_DEBUG_CHECK3(header) \
277 ((mem_debug_header_t*)((char*)header + header->size))->magic3
278 #define MEM_USER_PTR(header) \
279 ((char *)header + offsetof(mem_debug_header_t, magic3))
280 #define MEM_HEADER_PTR(ptr) \
281 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
283 struct mem_debug_header
{
286 struct mem_debug_header
*prev
;
287 struct mem_debug_header
*next
;
289 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
291 ALIGNED(16) unsigned magic3
;
294 typedef struct mem_debug_header mem_debug_header_t
;
296 static mem_debug_header_t
*mem_debug_chain
;
297 static unsigned mem_cur_size
;
298 static unsigned mem_max_size
;
300 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
302 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
303 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
304 header
->magic2
!= MEM_DEBUG_MAGIC2
||
305 MEM_DEBUG_CHECK3(header
) != MEM_DEBUG_MAGIC3
||
306 header
->size
== (unsigned)-1) {
307 fprintf(stderr
, "%s check failed\n", msg
);
308 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
309 fprintf(stderr
, "%s:%u: block allocated here.\n",
310 header
->file_name
, header
->line_num
);
316 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
319 mem_debug_header_t
*header
;
321 header
= malloc(sizeof(mem_debug_header_t
) + size
);
323 _tcc_error("memory full (malloc)");
325 header
->magic1
= MEM_DEBUG_MAGIC1
;
326 header
->magic2
= MEM_DEBUG_MAGIC2
;
328 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
329 header
->line_num
= line
;
330 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
331 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
332 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
334 header
->next
= mem_debug_chain
;
337 header
->next
->prev
= header
;
338 mem_debug_chain
= header
;
340 mem_cur_size
+= size
;
341 if (mem_cur_size
> mem_max_size
)
342 mem_max_size
= mem_cur_size
;
344 return MEM_USER_PTR(header
);
347 PUB_FUNC
void tcc_free_debug(void *ptr
)
349 mem_debug_header_t
*header
;
352 header
= malloc_check(ptr
, "tcc_free");
353 mem_cur_size
-= header
->size
;
354 header
->size
= (unsigned)-1;
356 header
->next
->prev
= header
->prev
;
358 header
->prev
->next
= header
->next
;
359 if (header
== mem_debug_chain
)
360 mem_debug_chain
= header
->next
;
364 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
367 ptr
= tcc_malloc_debug(size
,file
,line
);
368 memset(ptr
, 0, size
);
372 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
374 mem_debug_header_t
*header
;
375 int mem_debug_chain_update
= 0;
377 return tcc_malloc_debug(size
, file
, line
);
378 header
= malloc_check(ptr
, "tcc_realloc");
379 mem_cur_size
-= header
->size
;
380 mem_debug_chain_update
= (header
== mem_debug_chain
);
381 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
383 _tcc_error("memory full (realloc)");
385 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
387 header
->next
->prev
= header
;
389 header
->prev
->next
= header
;
390 if (mem_debug_chain_update
)
391 mem_debug_chain
= header
;
392 mem_cur_size
+= size
;
393 if (mem_cur_size
> mem_max_size
)
394 mem_max_size
= mem_cur_size
;
395 return MEM_USER_PTR(header
);
398 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
401 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
406 PUB_FUNC
void tcc_memcheck(void)
409 mem_debug_header_t
*header
= mem_debug_chain
;
410 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
411 mem_cur_size
, mem_max_size
);
413 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
414 header
->file_name
, header
->line_num
, header
->size
);
415 header
= header
->next
;
422 #endif /* MEM_DEBUG */
424 #define free(p) use_tcc_free(p)
425 #define malloc(s) use_tcc_malloc(s)
426 #define realloc(p, s) use_tcc_realloc(p, s)
428 /********************************************************/
431 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
437 pp
= *(void ***)ptab
;
438 /* every power of two we double array size */
439 if ((nb
& (nb
- 1)) == 0) {
444 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
451 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
454 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
457 tcc_free(*(void**)pp
);
461 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
469 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
470 if (c
== '{' && p
[1] && p
[2] == '}') {
473 cstr_cat(&str
, s
->tcc_lib_path
, -1);
474 if (c
== 'f' && file
) {
475 /* substitute current file's dir */
476 const char *f
= file
->true_filename
;
477 const char *b
= tcc_basename(f
);
479 cstr_cat(&str
, f
, b
- f
- 1);
481 cstr_cat(&str
, ".", 1);
488 cstr_ccat(&str
, '\0');
489 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
496 /********************************************************/
498 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
502 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
505 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
509 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
514 #define ERROR_NOABORT 1
515 #define ERROR_ERROR 2
517 PUB_FUNC
void tcc_enter_state(TCCState
*s1
)
523 PUB_FUNC
void tcc_exit_state(void)
529 static void error1(int mode
, const char *fmt
, va_list ap
)
532 BufferedFile
**pf
, *f
;
533 TCCState
*s1
= tcc_state
;
537 /* can happen only if called from tcc_malloc(): 'out of memory' */
540 if (s1
&& !s1
->error_set_jmp_enabled
)
541 /* tcc_state just was set by tcc_enter_state() */
544 if (mode
== ERROR_WARN
) {
552 if (s1
->error_set_jmp_enabled
) { /* we're called while parsing a file */
553 /* use upper file if inline ":asm:" or token ":paste:" */
554 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
558 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
559 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
560 (*pf
)->filename
, (*pf
)->line_num
);
561 strcat_printf(buf
, sizeof(buf
), "%s:%d: ",
562 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
563 } else if (s1
->current_filename
) {
564 strcat_printf(buf
, sizeof(buf
), "%s: ", s1
->current_filename
);
569 strcat_printf(buf
, sizeof(buf
), "tcc: ");
570 if (mode
== ERROR_WARN
)
571 strcat_printf(buf
, sizeof(buf
), "warning: ");
573 strcat_printf(buf
, sizeof(buf
), "error: ");
574 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
575 if (!s1
|| !s1
->error_func
) {
576 /* default case: stderr */
577 if (s1
&& s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
578 /* print a newline during tcc -E */
579 printf("\n"), fflush(stdout
);
580 fflush(stdout
); /* flush -v output */
581 fprintf(stderr
, "%s\n", buf
);
582 fflush(stderr
); /* print error/warning now (win32) */
584 s1
->error_func(s1
->error_opaque
, buf
);
587 if (mode
!= ERROR_WARN
)
589 if (mode
!= ERROR_ERROR
)
591 if (s1
->error_set_jmp_enabled
)
592 longjmp(s1
->error_jmp_buf
, 1);
597 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
, TCCErrorFunc error_func
)
599 s
->error_opaque
= error_opaque
;
600 s
->error_func
= error_func
;
603 LIBTCCAPI TCCErrorFunc
tcc_get_error_func(TCCState
*s
)
605 return s
->error_func
;
608 LIBTCCAPI
void *tcc_get_error_opaque(TCCState
*s
)
610 return s
->error_opaque
;
613 /* error without aborting current compilation */
614 PUB_FUNC
void _tcc_error_noabort(const char *fmt
, ...)
618 error1(ERROR_NOABORT
, fmt
, ap
);
622 PUB_FUNC
void _tcc_error(const char *fmt
, ...)
626 for (;;) error1(ERROR_ERROR
, fmt
, ap
);
629 PUB_FUNC
void _tcc_warning(const char *fmt
, ...)
633 error1(ERROR_WARN
, fmt
, ap
);
637 /********************************************************/
640 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
643 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
645 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
646 bf
->buf_ptr
= bf
->buffer
;
647 bf
->buf_end
= bf
->buffer
+ initlen
;
648 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
649 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
651 normalize_slashes(bf
->filename
);
653 bf
->true_filename
= bf
->filename
;
655 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
659 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
662 ST_FUNC
void tcc_close(void)
664 TCCState
*s1
= tcc_state
;
665 BufferedFile
*bf
= file
;
668 total_lines
+= bf
->line_num
;
670 if (bf
->true_filename
!= bf
->filename
)
671 tcc_free(bf
->true_filename
);
676 static int _tcc_open(TCCState
*s1
, const char *filename
)
679 if (strcmp(filename
, "-") == 0)
680 fd
= 0, filename
= "<stdin>";
682 fd
= open(filename
, O_RDONLY
| O_BINARY
);
683 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
684 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
685 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
689 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
691 int fd
= _tcc_open(s1
, filename
);
694 tcc_open_bf(s1
, filename
, 0);
699 /* compile the file opened in 'file'. Return non zero if errors. */
700 static int tcc_compile(TCCState
*s1
, int filetype
, const char *str
, int fd
)
702 /* Here we enter the code section where we use the global variables for
703 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
704 Other threads need to wait until we're done.
706 Alternatively we could use thread local storage for those global
707 variables, which may or may not have advantages */
711 if (setjmp(s1
->error_jmp_buf
) == 0) {
712 s1
->error_set_jmp_enabled
= 1;
716 int len
= strlen(str
);
717 tcc_open_bf(s1
, "<string>", len
);
718 memcpy(file
->buffer
, str
, len
);
720 tcc_open_bf(s1
, str
, 0);
724 tccelf_begin_file(s1
);
725 preprocess_start(s1
, filetype
);
727 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
729 } else if (filetype
& (AFF_TYPE_ASM
| AFF_TYPE_ASMPP
)) {
730 #ifdef CONFIG_TCC_ASM
731 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
733 tcc_error_noabort("asm not supported");
739 s1
->error_set_jmp_enabled
= 0;
745 return s1
->nb_errors
!= 0 ? -1 : 0;
748 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
750 return tcc_compile(s
, s
->filetype
, str
, -1);
753 /* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
754 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
757 if (NULL
== (eq
= strchr(sym
, '=')))
760 value
= *eq
? eq
+ 1 : "1";
761 cstr_printf(&s1
->cmdline_defs
, "#define %.*s %s\n", (int)(eq
-sym
), sym
, value
);
764 /* undefine a preprocessor symbol */
765 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
767 cstr_printf(&s1
->cmdline_defs
, "#undef %s\n", sym
);
771 LIBTCCAPI TCCState
*tcc_new(void)
775 s
= tcc_mallocz(sizeof(TCCState
));
787 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
788 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
789 s
->warn_implicit_function_declaration
= 1;
790 s
->ms_extensions
= 1;
792 #ifdef CHAR_IS_UNSIGNED
793 s
->char_is_unsigned
= 1;
795 #ifdef TCC_TARGET_I386
798 /* enable this if you want symbols with leading underscore on windows: */
799 #if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
800 s
->leading_underscore
= 1;
802 #ifdef TCC_TARGET_ARM
803 s
->float_abi
= ARM_FLOAT_ABI
;
807 /* might be used in error() before preprocess_start() */
808 s
->include_stack_ptr
= s
->include_stack
;
813 tcc_set_lib_path_w32(s
);
815 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
820 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
825 /* free library paths */
826 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
827 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
829 /* free include paths */
830 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
831 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
833 tcc_free(s1
->tcc_lib_path
);
834 tcc_free(s1
->soname
);
836 tcc_free(s1
->init_symbol
);
837 tcc_free(s1
->fini_symbol
);
838 tcc_free(s1
->outfile
);
839 tcc_free(s1
->deps_outfile
);
840 dynarray_reset(&s1
->files
, &s1
->nb_files
);
841 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
842 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
843 dynarray_reset(&s1
->argv
, &s1
->argc
);
844 cstr_free(&s1
->cmdline_defs
);
845 cstr_free(&s1
->cmdline_incl
);
847 /* free runtime memory */
853 if (0 == --nb_states
)
858 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
860 s
->output_type
= output_type
;
862 /* always elf for objects */
863 if (output_type
== TCC_OUTPUT_OBJ
)
864 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
867 /* default include paths */
868 /* -isystem paths have already been handled */
869 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
872 #ifdef CONFIG_TCC_BCHECK
873 if (s
->do_bounds_check
) {
874 /* if bound checking, then add corresponding sections */
875 tccelf_bounds_new(s
);
879 /* add debug sections */
883 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
887 if (!s
->nostdlib
&& output_type
!= TCC_OUTPUT_OBJ
)
888 tcc_add_systemdir(s
);
891 /* paths for crt objects */
892 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
893 /* add libc crt1/crti objects */
894 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
896 #if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD
898 if (output_type
!= TCC_OUTPUT_DLL
)
899 tcc_add_crt(s
, "crt0.o");
900 #elif TARGETOS_FreeBSD
901 if (output_type
!= TCC_OUTPUT_DLL
)
902 tcc_add_crt(s
, "crt1.o");
903 tcc_add_crt(s
, "crti.o");
904 #elif TARGETOS_NetBSD
905 if (output_type
!= TCC_OUTPUT_DLL
)
906 tcc_add_crt(s
, "crt0.o");
907 tcc_add_crt(s
, "crti.o");
910 tcc_add_crt(s
, "crtbeginT.o");
911 else if (output_type
== TCC_OUTPUT_DLL
)
912 tcc_add_crt(s
, "crtbeginS.o");
914 tcc_add_crt(s
, "crtbegin.o");
915 #elif !TCC_TARGET_MACHO
916 /* Mach-O with LC_MAIN doesn't need any crt startup code. */
917 if (output_type
!= TCC_OUTPUT_DLL
)
918 tcc_add_crt(s
, "crt1.o");
919 tcc_add_crt(s
, "crti.o");
926 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
928 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
932 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
934 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
938 #if !defined TCC_TARGET_MACHO || defined TCC_IS_NATIVE
939 ST_FUNC DLLReference
*tcc_add_dllref(TCCState
*s1
, const char *dllname
)
941 DLLReference
*ref
= tcc_mallocz(sizeof(DLLReference
) + strlen(dllname
));
942 strcpy(ref
->name
, dllname
);
943 dynarray_add(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, ref
);
948 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
953 fd
= _tcc_open(s1
, filename
);
955 if (flags
& AFF_PRINT_ERROR
)
956 tcc_error_noabort("file '%s' not found", filename
);
960 s1
->current_filename
= filename
;
961 if (flags
& AFF_TYPE_BIN
) {
965 obj_type
= tcc_object_type(fd
, &ehdr
);
966 lseek(fd
, 0, SEEK_SET
);
968 #ifdef TCC_TARGET_MACHO
969 if (0 == obj_type
&& 0 == strcmp(tcc_fileextension(filename
), ".dylib"))
970 obj_type
= AFF_BINTYPE_DYN
;
975 case AFF_BINTYPE_REL
:
976 ret
= tcc_load_object_file(s1
, fd
, 0);
980 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
985 ret
= pe_load_file(s1
, fd
, filename
);
987 case AFF_BINTYPE_DYN
:
988 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
990 void *dl
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
992 tcc_add_dllref(s1
, filename
)->handle
= dl
;
998 #ifdef TCC_TARGET_MACHO
999 ret
= macho_load_dll(s1
, fd
, filename
,
1000 (flags
& AFF_REFERENCED_DLL
) != 0);
1002 ret
= tcc_load_dll(s1
, fd
, filename
,
1003 (flags
& AFF_REFERENCED_DLL
) != 0);
1007 #ifdef TCC_TARGET_COFF
1008 case AFF_BINTYPE_C67
:
1009 ret
= tcc_load_coff(s1
, fd
);
1013 #ifndef TCC_TARGET_MACHO
1014 /* as GNU ld, consider it is an ld script if not recognized */
1015 ret
= tcc_load_ldscript(s1
, fd
);
1018 #endif /* !TCC_TARGET_PE */
1020 tcc_error_noabort("%s: unrecognized file type", filename
);
1025 /* update target deps */
1026 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
, tcc_strdup(filename
));
1027 ret
= tcc_compile(s1
, flags
, filename
, fd
);
1029 s1
->current_filename
= NULL
;
1033 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1035 int filetype
= s
->filetype
;
1036 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1037 /* use a file extension to detect a filetype */
1038 const char *ext
= tcc_fileextension(filename
);
1041 if (!strcmp(ext
, "S"))
1042 filetype
= AFF_TYPE_ASMPP
;
1043 else if (!strcmp(ext
, "s"))
1044 filetype
= AFF_TYPE_ASM
;
1045 else if (!PATHCMP(ext
, "c") || !PATHCMP(ext
, "i"))
1046 filetype
= AFF_TYPE_C
;
1048 filetype
|= AFF_TYPE_BIN
;
1050 filetype
= AFF_TYPE_C
;
1053 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1056 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1058 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1062 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1063 const char *filename
, int flags
, char **paths
, int nb_paths
)
1068 for(i
= 0; i
< nb_paths
; i
++) {
1069 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1070 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1076 #ifndef TCC_TARGET_MACHO
1077 /* find and load a dll. Return non zero if not found */
1078 /* XXX: add '-rpath' option support ? */
1079 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1081 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1082 s
->library_paths
, s
->nb_library_paths
);
1086 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
1087 ST_FUNC
int tcc_add_crt(TCCState
*s1
, const char *filename
)
1089 if (-1 == tcc_add_library_internal(s1
, "%s/%s",
1090 filename
, 0, s1
->crt_paths
, s1
->nb_crt_paths
))
1091 tcc_error_noabort("file '%s' not found", filename
);
1096 /* the library name is the same as the argument of the '-l' option */
1097 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1099 #if defined TCC_TARGET_PE
1100 const char *libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1101 const char **pp
= s
->static_link
? libs
+ 4 : libs
;
1102 #elif defined TCC_TARGET_MACHO
1103 const char *libs
[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL
};
1104 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1106 const char *libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1107 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1109 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1111 if (0 == tcc_add_library_internal(s
, *pp
,
1112 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1119 PUB_FUNC
int tcc_add_library_err(TCCState
*s1
, const char *libname
)
1121 int ret
= tcc_add_library(s1
, libname
);
1123 tcc_error_noabort("library '%s' not found", libname
);
1127 /* handle #pragma comment(lib,) */
1128 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1131 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1132 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1135 LIBTCCAPI
int tcc_add_symbol(TCCState
*s1
, const char *name
, const void *val
)
1137 #ifdef TCC_TARGET_PE
1138 /* On x86_64 'val' might not be reachable with a 32bit offset.
1139 So it is handled here as if it were in a DLL. */
1140 pe_putimport(s1
, 0, name
, (uintptr_t)val
);
1143 if (s1
->leading_underscore
) {
1145 pstrcpy(buf
+ 1, sizeof(buf
) - 1, name
);
1148 set_global_sym(s1
, name
, NULL
, (addr_t
)(uintptr_t)val
); /* NULL: SHN_ABS */
1153 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1155 tcc_free(s
->tcc_lib_path
);
1156 s
->tcc_lib_path
= tcc_strdup(path
);
1159 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1160 #define FD_INVERT 0x0002 /* invert value before storing */
1162 typedef struct FlagDef
{
1168 static int no_flag(const char **pp
)
1170 const char *p
= *pp
;
1171 if (*p
!= 'n' || *++p
!= 'o' || *++p
!= '-')
1177 ST_FUNC
int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1188 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1190 if (strcmp(r
, p
->name
))
1193 if (0 == (p
->flags
& WD_ALL
))
1197 *((unsigned char *)s
+ p
->offset
) =
1198 p
->flags
& FD_INVERT
? !value
: value
;
1208 static int strstart(const char *val
, const char **str
)
1223 /* Like strstart, but automatically takes into account that ld options can
1225 * - start with double or single dash (e.g. '--soname' or '-soname')
1226 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1227 * or '-Wl,-soname=x.so')
1229 * you provide `val` always in 'option[=]' form (no leading -)
1231 static int link_option(const char *str
, const char *val
, const char **ptr
)
1236 /* there should be 1 or 2 dashes */
1242 /* then str & val should match (potentially up to '=') */
1253 while (*q
!= '\0' && *q
!= '=') {
1260 /* '=' near eos means ',' or '=' is ok */
1264 if (*p
!= ',' && *p
!= '=')
1274 static const char *skip_linker_arg(const char **str
)
1276 const char *s1
= *str
;
1277 const char *s2
= strchr(s1
, ',');
1278 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1282 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1288 p
[l
= strlen(p
)] = sep
, ++l
;
1289 skip_linker_arg(&q
);
1290 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1293 /* set linker options */
1294 static int tcc_set_linker(TCCState
*s
, const char *option
)
1299 const char *p
= NULL
;
1304 if (link_option(option
, "Bsymbolic", &p
)) {
1306 } else if (link_option(option
, "nostdlib", &p
)) {
1308 } else if (link_option(option
, "fini=", &p
)) {
1309 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1311 } else if (link_option(option
, "image-base=", &p
)
1312 || link_option(option
, "Ttext=", &p
)) {
1313 s
->text_addr
= strtoull(p
, &end
, 16);
1314 s
->has_text_addr
= 1;
1315 } else if (link_option(option
, "init=", &p
)) {
1316 copy_linker_arg(&s
->init_symbol
, p
, 0);
1318 } else if (link_option(option
, "oformat=", &p
)) {
1319 #if defined(TCC_TARGET_PE)
1320 if (strstart("pe-", &p
)) {
1322 if (strstart("elf64-", &p
)) {
1324 if (strstart("elf32-", &p
)) {
1326 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1327 } else if (!strcmp(p
, "binary")) {
1328 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1329 #ifdef TCC_TARGET_COFF
1330 } else if (!strcmp(p
, "coff")) {
1331 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1336 } else if (link_option(option
, "as-needed", &p
)) {
1338 } else if (link_option(option
, "O", &p
)) {
1340 } else if (link_option(option
, "export-all-symbols", &p
)) {
1342 } else if (link_option(option
, "export-dynamic", &p
)) {
1344 } else if (link_option(option
, "rpath=", &p
)) {
1345 copy_linker_arg(&s
->rpath
, p
, ':');
1346 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1347 s
->enable_new_dtags
= 1;
1348 } else if (link_option(option
, "section-alignment=", &p
)) {
1349 s
->section_align
= strtoul(p
, &end
, 16);
1350 } else if (link_option(option
, "soname=", &p
)) {
1351 copy_linker_arg(&s
->soname
, p
, 0);
1352 #ifdef TCC_TARGET_PE
1353 } else if (link_option(option
, "large-address-aware", &p
)) {
1354 s
->pe_characteristics
|= 0x20;
1355 } else if (link_option(option
, "file-alignment=", &p
)) {
1356 s
->pe_file_align
= strtoul(p
, &end
, 16);
1357 } else if (link_option(option
, "stack=", &p
)) {
1358 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1359 } else if (link_option(option
, "subsystem=", &p
)) {
1360 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1361 if (!strcmp(p
, "native")) {
1362 s
->pe_subsystem
= 1;
1363 } else if (!strcmp(p
, "console")) {
1364 s
->pe_subsystem
= 3;
1365 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1366 s
->pe_subsystem
= 2;
1367 } else if (!strcmp(p
, "posix")) {
1368 s
->pe_subsystem
= 7;
1369 } else if (!strcmp(p
, "efiapp")) {
1370 s
->pe_subsystem
= 10;
1371 } else if (!strcmp(p
, "efiboot")) {
1372 s
->pe_subsystem
= 11;
1373 } else if (!strcmp(p
, "efiruntime")) {
1374 s
->pe_subsystem
= 12;
1375 } else if (!strcmp(p
, "efirom")) {
1376 s
->pe_subsystem
= 13;
1377 #elif defined(TCC_TARGET_ARM)
1378 if (!strcmp(p
, "wince")) {
1379 s
->pe_subsystem
= 9;
1384 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1386 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1388 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1393 tcc_error("unsupported linker option '%s'", option
);
1396 if (ignoring
&& s
->warn_unsupported
)
1397 tcc_warning("unsupported linker option '%s'", option
);
1399 option
= skip_linker_arg(&p
);
1404 typedef struct TCCOption
{
1427 TCC_OPTION_dumpversion
,
1436 TCC_OPTION_traditional
,
1441 TCC_OPTION_mfloat_abi
,
1445 TCC_OPTION_iwithprefix
,
1447 TCC_OPTION_nostdinc
,
1448 TCC_OPTION_nostdlib
,
1449 TCC_OPTION_print_search_dirs
,
1450 TCC_OPTION_rdynamic
,
1452 TCC_OPTION_pedantic
,
1466 #define TCC_OPTION_HAS_ARG 0x0001
1467 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1469 static const TCCOption tcc_options
[] = {
1470 { "h", TCC_OPTION_HELP
, 0 },
1471 { "-help", TCC_OPTION_HELP
, 0 },
1472 { "?", TCC_OPTION_HELP
, 0 },
1473 { "hh", TCC_OPTION_HELP2
, 0 },
1474 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1475 { "-version", TCC_OPTION_v
, 0 }, /* handle as verbose, also prints version*/
1476 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1477 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1478 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1479 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1480 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1481 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1482 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1483 { "bench", TCC_OPTION_bench
, 0 },
1484 #ifdef CONFIG_TCC_BACKTRACE
1485 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1487 #ifdef CONFIG_TCC_BCHECK
1488 { "b", TCC_OPTION_b
, 0 },
1490 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1491 { "c", TCC_OPTION_c
, 0 },
1492 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1493 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1494 { "static", TCC_OPTION_static
, 0 },
1495 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1496 { "shared", TCC_OPTION_shared
, 0 },
1497 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1498 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1499 { "-param", TCC_OPTION_param
, TCC_OPTION_HAS_ARG
},
1500 { "pedantic", TCC_OPTION_pedantic
, 0},
1501 { "pthread", TCC_OPTION_pthread
, 0},
1502 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1503 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1504 { "r", TCC_OPTION_r
, 0 },
1505 { "s", TCC_OPTION_s
, 0 },
1506 { "traditional", TCC_OPTION_traditional
, 0 },
1507 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1508 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1509 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1510 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1511 #ifdef TCC_TARGET_ARM
1512 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1514 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1515 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1516 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1517 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1518 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1519 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1520 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1521 { "w", TCC_OPTION_w
, 0 },
1522 { "pipe", TCC_OPTION_pipe
, 0},
1523 { "E", TCC_OPTION_E
, 0},
1524 { "MD", TCC_OPTION_MD
, 0},
1525 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1526 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1527 { "ar", TCC_OPTION_ar
, 0},
1528 #ifdef TCC_TARGET_PE
1529 { "impdef", TCC_OPTION_impdef
, 0},
1531 { "C", TCC_OPTION_C
, 0},
1535 static const FlagDef options_W
[] = {
1537 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1538 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1539 { offsetof(TCCState
, warn_error
), 0, "error" },
1540 { offsetof(TCCState
, warn_gcc_compat
), 0, "gcc-compat" },
1541 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
1542 "implicit-function-declaration" },
1546 static const FlagDef options_f
[] = {
1547 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1548 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1549 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1550 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1551 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1552 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1556 static const FlagDef options_m
[] = {
1557 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1558 #ifdef TCC_TARGET_X86_64
1559 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1564 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1566 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1568 strcpy(f
->name
, filename
);
1569 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1572 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1577 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1583 while (c
= (unsigned char)*r
, c
) {
1585 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1587 } else if (c
== '"') {
1590 } else if (q
== 0 && c
<= ' ') {
1596 //printf("<%s>\n", str.data), fflush(stdout);
1597 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1604 /* read list file */
1605 static void args_parser_listfile(TCCState
*s
,
1606 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1615 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1617 tcc_error("listfile '%s' not found", filename
);
1619 len
= lseek(fd
, 0, SEEK_END
);
1620 p
= tcc_malloc(len
+ 1), p
[len
] = 0;
1621 lseek(fd
, 0, SEEK_SET
), read(fd
, p
, len
), close(fd
);
1623 for (i
= 0; i
< *pargc
; ++i
)
1625 args_parser_make_argv(p
, &argc
, &argv
);
1627 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1630 dynarray_reset(&s
->argv
, &s
->argc
);
1631 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1634 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1637 const TCCOption
*popt
;
1638 const char *optarg
, *r
;
1639 const char *run
= NULL
;
1641 CString linker_arg
; /* collect -Wl options */
1642 int tool
= 0, arg_start
= 0, noaction
= optind
;
1643 char **argv
= *pargv
;
1646 cstr_new(&linker_arg
);
1648 while (optind
< argc
) {
1650 if (r
[0] == '@' && r
[1] != '\0') {
1651 args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
);
1656 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1661 if (r
[0] != '-' || r
[1] == '\0') {
1662 if (r
[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1663 args_parser_add_file(s
, r
, s
->filetype
);
1665 tcc_set_options(s
, run
);
1666 arg_start
= optind
- 1;
1672 /* find option in table */
1673 for(popt
= tcc_options
; ; ++popt
) {
1674 const char *p1
= popt
->name
;
1675 const char *r1
= r
+ 1;
1677 tcc_error("invalid option -- '%s'", r
);
1678 if (!strstart(p1
, &r1
))
1681 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1682 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1685 tcc_error("argument to '%s' is missing", r
);
1686 optarg
= argv
[optind
++];
1688 } else if (*r1
!= '\0')
1693 switch(popt
->index
) {
1694 case TCC_OPTION_HELP
:
1697 case TCC_OPTION_HELP2
:
1701 tcc_add_include_path(s
, optarg
);
1704 tcc_define_symbol(s
, optarg
, NULL
);
1707 tcc_undefine_symbol(s
, optarg
);
1710 tcc_add_library_path(s
, optarg
);
1713 /* set tcc utilities path (mainly for tcc development) */
1714 tcc_set_lib_path(s
, optarg
);
1717 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1720 case TCC_OPTION_pthread
:
1721 s
->option_pthread
= 1;
1723 case TCC_OPTION_bench
:
1726 #ifdef CONFIG_TCC_BACKTRACE
1728 s
->rt_num_callers
= atoi(optarg
);
1729 s
->do_backtrace
= 1;
1733 #ifdef CONFIG_TCC_BCHECK
1735 s
->do_bounds_check
= 1;
1736 s
->do_backtrace
= 1;
1747 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1753 else if (*optarg
== 'M')
1755 else if (*optarg
== 't')
1757 else if (isnum(*optarg
))
1758 s
->g_debug
|= atoi(optarg
);
1760 goto unsupported_option
;
1762 case TCC_OPTION_static
:
1765 case TCC_OPTION_std
:
1766 if (strcmp(optarg
, "=c11") == 0)
1767 s
->cversion
= 201112;
1769 case TCC_OPTION_shared
:
1771 goto set_output_type
;
1772 case TCC_OPTION_soname
:
1773 s
->soname
= tcc_strdup(optarg
);
1777 tcc_warning("multiple -o option");
1778 tcc_free(s
->outfile
);
1780 s
->outfile
= tcc_strdup(optarg
);
1783 /* generate a .o merging several output files */
1786 goto set_output_type
;
1787 case TCC_OPTION_isystem
:
1788 tcc_add_sysinclude_path(s
, optarg
);
1790 case TCC_OPTION_include
:
1791 cstr_printf(&s
->cmdline_incl
, "#include \"%s\"\n", optarg
);
1793 case TCC_OPTION_nostdinc
:
1796 case TCC_OPTION_nostdlib
:
1799 case TCC_OPTION_run
:
1800 #ifndef TCC_IS_NATIVE
1801 tcc_error("-run is not available in a cross compiler");
1804 x
= TCC_OUTPUT_MEMORY
;
1805 goto set_output_type
;
1807 do ++s
->verbose
; while (*optarg
++ == 'v');
1811 if (set_flag(s
, options_f
, optarg
) < 0)
1812 goto unsupported_option
;
1814 #ifdef TCC_TARGET_ARM
1815 case TCC_OPTION_mfloat_abi
:
1816 /* tcc doesn't support soft float yet */
1817 if (!strcmp(optarg
, "softfp")) {
1818 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1819 } else if (!strcmp(optarg
, "hard"))
1820 s
->float_abi
= ARM_HARD_FLOAT
;
1822 tcc_error("unsupported float abi '%s'", optarg
);
1826 if (set_flag(s
, options_m
, optarg
) < 0) {
1827 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
1828 goto unsupported_option
;
1829 if (PTR_SIZE
!= x
/8)
1836 if (optarg
[0] && set_flag(s
, options_W
, optarg
) < 0)
1837 goto unsupported_option
;
1842 case TCC_OPTION_rdynamic
:
1846 if (linker_arg
.size
)
1847 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
1848 cstr_cat(&linker_arg
, optarg
, 0);
1849 if (tcc_set_linker(s
, linker_arg
.data
))
1850 cstr_free(&linker_arg
);
1856 x
= TCC_OUTPUT_PREPROCESS
;
1857 goto set_output_type
;
1859 s
->Pflag
= atoi(optarg
) + 1;
1865 s
->deps_outfile
= tcc_strdup(optarg
);
1867 case TCC_OPTION_dumpversion
:
1868 printf ("%s\n", TCC_VERSION
);
1875 else if (*optarg
== 'a')
1877 else if (*optarg
== 'b')
1879 else if (*optarg
== 'n')
1882 tcc_warning("unsupported language '%s'", optarg
);
1883 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
1886 s
->optimize
= atoi(optarg
);
1888 case TCC_OPTION_print_search_dirs
:
1891 case TCC_OPTION_impdef
:
1897 arg_start
= optind
- 1;
1898 if (arg_start
!= noaction
)
1899 tcc_error("cannot parse %s here", r
);
1902 case TCC_OPTION_traditional
:
1903 case TCC_OPTION_pedantic
:
1904 case TCC_OPTION_pipe
:
1911 if (s
->warn_unsupported
)
1912 tcc_warning("unsupported option '%s'", r
);
1916 if (linker_arg
.size
) {
1917 r
= linker_arg
.data
;
1920 *pargc
= argc
- arg_start
;
1921 *pargv
= argv
+ arg_start
;
1924 if (optind
!= noaction
)
1926 if (s
->verbose
== 2)
1927 return OPT_PRINT_DIRS
;
1933 LIBTCCAPI
void tcc_set_options(TCCState
*s
, const char *r
)
1937 args_parser_make_argv(r
, &argc
, &argv
);
1938 tcc_parse_args(s
, &argc
, &argv
, 0);
1939 dynarray_reset(&argv
, &argc
);
1942 PUB_FUNC
void tcc_print_stats(TCCState
*s1
, unsigned total_time
)
1946 if (total_bytes
< 1)
1948 fprintf(stderr
, "* %d idents, %d lines, %d bytes\n"
1949 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
1950 total_idents
, total_lines
, total_bytes
,
1951 (double)total_time
/1000,
1952 (unsigned)total_lines
*1000/total_time
,
1953 (double)total_bytes
/1000/total_time
);
1954 fprintf(stderr
, "* text %d, data %d, bss %d bytes\n",
1955 s1
->total_output
[0], s1
->total_output
[1], s1
->total_output
[2]);
1957 fprintf(stderr
, "* %d bytes memory used\n", mem_max_size
);