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
;
507 #endif /* MEM_DEBUG */
509 #define free(p) use_tcc_free(p)
510 #define malloc(s) use_tcc_malloc(s)
511 #define realloc(p, s) use_tcc_realloc(p, s)
513 /********************************************************/
516 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
522 pp
= *(void ***)ptab
;
523 /* every power of two we double array size */
524 if ((nb
& (nb
- 1)) == 0) {
529 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
536 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
539 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
542 tcc_free(*(void**)pp
);
546 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
554 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
555 if (c
== '{' && p
[1] && p
[2] == '}') {
558 cstr_cat(&str
, s
->tcc_lib_path
, -1);
560 cstr_cat(&str
, CONFIG_SYSROOT
, -1);
561 if (c
== 'f' && file
) {
562 /* substitute current file's dir */
563 const char *f
= file
->true_filename
;
564 const char *b
= tcc_basename(f
);
566 cstr_cat(&str
, f
, b
- f
- 1);
568 cstr_cat(&str
, ".", 1);
575 cstr_ccat(&str
, '\0');
576 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
583 /********************************************************/
584 /* warning / error */
586 /* warn_... option bits */
587 #define WARN_ON 1 /* warning is on (-Woption) */
588 #define WARN_ERR 2 /* warning is an error (-Werror=option) */
589 #define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
592 enum { ERROR_WARN
, ERROR_NOABORT
, ERROR_ERROR
};
594 static void error1(int mode
, const char *fmt
, va_list ap
)
596 BufferedFile
**pf
, *f
;
597 TCCState
*s1
= tcc_state
;
602 if (mode
== ERROR_WARN
) {
606 /* handle tcc_warning_c(warn_option)(fmt, ...) */
607 int wopt
= *(&s1
->warn_none
+ s1
->warn_num
);
609 if (0 == (wopt
& WARN_ON
))
622 if (s1
->error_set_jmp_enabled
) { /* we're called while parsing a file */
623 /* use upper file if inline ":asm:" or token ":paste:" */
624 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
628 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
629 cstr_printf(&cs
, "In file included from %s:%d:\n",
630 (*pf
)->filename
, (*pf
)->line_num
- 1);
631 cstr_printf(&cs
, "%s:%d: ",
632 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
633 } else if (s1
->current_filename
) {
634 cstr_printf(&cs
, "%s: ", s1
->current_filename
);
637 cstr_printf(&cs
, "tcc: ");
638 cstr_printf(&cs
, mode
== ERROR_WARN
? "warning: " : "error: ");
639 cstr_vprintf(&cs
, fmt
, ap
);
640 if (!s1
|| !s1
->error_func
) {
641 /* default case: stderr */
642 if (s1
&& s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
643 printf("\n"); /* print a newline during tcc -E */
644 fflush(stdout
); /* flush -v output */
645 fprintf(stderr
, "%s\n", (char*)cs
.data
);
646 fflush(stderr
); /* print error/warning now (win32) */
648 s1
->error_func(s1
->error_opaque
, (char*)cs
.data
);
651 if (mode
!= ERROR_WARN
)
653 if (mode
== ERROR_ERROR
&& s1
->error_set_jmp_enabled
) {
655 tcc_free(*(void**)stk_data
[--nb_stk_data
]);
656 longjmp(s1
->error_jmp_buf
, 1);
660 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
, TCCErrorFunc error_func
)
662 s
->error_opaque
= error_opaque
;
663 s
->error_func
= error_func
;
666 LIBTCCAPI TCCErrorFunc
tcc_get_error_func(TCCState
*s
)
668 return s
->error_func
;
671 LIBTCCAPI
void *tcc_get_error_opaque(TCCState
*s
)
673 return s
->error_opaque
;
676 /* error without aborting current compilation */
677 PUB_FUNC
int _tcc_error_noabort(const char *fmt
, ...)
681 error1(ERROR_NOABORT
, fmt
, ap
);
687 PUB_FUNC
void _tcc_error(const char *fmt
, ...)
691 error1(ERROR_ERROR
, fmt
, ap
);
694 #define _tcc_error use_tcc_error_noabort
696 PUB_FUNC
void _tcc_warning(const char *fmt
, ...)
700 error1(ERROR_WARN
, fmt
, ap
);
705 /********************************************************/
708 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
711 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
713 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
714 bf
->buf_ptr
= bf
->buffer
;
715 bf
->buf_end
= bf
->buffer
+ initlen
;
716 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
717 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
719 normalize_slashes(bf
->filename
);
721 bf
->true_filename
= bf
->filename
;
723 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
727 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
730 ST_FUNC
void tcc_close(void)
732 TCCState
*s1
= tcc_state
;
733 BufferedFile
*bf
= file
;
736 total_lines
+= bf
->line_num
- 1;
738 if (bf
->true_filename
!= bf
->filename
)
739 tcc_free(bf
->true_filename
);
744 static int _tcc_open(TCCState
*s1
, const char *filename
)
747 if (strcmp(filename
, "-") == 0)
748 fd
= 0, filename
= "<stdin>";
750 fd
= open(filename
, O_RDONLY
| O_BINARY
);
751 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
752 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
753 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
757 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
759 int fd
= _tcc_open(s1
, filename
);
762 tcc_open_bf(s1
, filename
, 0);
767 /* compile the file opened in 'file'. Return non zero if errors. */
768 static int tcc_compile(TCCState
*s1
, int filetype
, const char *str
, int fd
)
770 /* Here we enter the code section where we use the global variables for
771 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
772 Other threads need to wait until we're done.
774 Alternatively we could use thread local storage for those global
775 variables, which may or may not have advantages */
778 s1
->error_set_jmp_enabled
= 1;
780 if (setjmp(s1
->error_jmp_buf
) == 0) {
784 int len
= strlen(str
);
785 tcc_open_bf(s1
, "<string>", len
);
786 memcpy(file
->buffer
, str
, len
);
788 tcc_open_bf(s1
, str
, 0);
792 preprocess_start(s1
, filetype
);
795 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
798 tccelf_begin_file(s1
);
799 if (filetype
& (AFF_TYPE_ASM
| AFF_TYPE_ASMPP
)) {
800 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
809 s1
->error_set_jmp_enabled
= 0;
811 return s1
->nb_errors
!= 0 ? -1 : 0;
814 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
816 return tcc_compile(s
, s
->filetype
, str
, -1);
819 /* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
820 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
823 if (NULL
== (eq
= strchr(sym
, '=')))
826 value
= *eq
? eq
+ 1 : "1";
827 cstr_printf(&s1
->cmdline_defs
, "#define %.*s %s\n", (int)(eq
-sym
), sym
, value
);
830 /* undefine a preprocessor symbol */
831 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
833 cstr_printf(&s1
->cmdline_defs
, "#undef %s\n", sym
);
837 LIBTCCAPI TCCState
*tcc_new(void)
841 s
= tcc_mallocz(sizeof(TCCState
));
853 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
854 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
855 s
->warn_implicit_function_declaration
= 1;
856 s
->warn_discarded_qualifiers
= 1;
857 s
->ms_extensions
= 1;
859 #ifdef CHAR_IS_UNSIGNED
860 s
->char_is_unsigned
= 1;
862 #ifdef TCC_TARGET_I386
865 /* enable this if you want symbols with leading underscore on windows: */
866 #if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
867 s
->leading_underscore
= 1;
869 #ifdef TCC_TARGET_ARM
870 s
->float_abi
= ARM_FLOAT_ABI
;
872 #ifdef CONFIG_NEW_DTAGS
873 s
->enable_new_dtags
= 1;
876 /* might be used in error() before preprocess_start() */
877 s
->include_stack_ptr
= s
->include_stack
;
879 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
883 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
888 /* free library paths */
889 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
890 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
892 /* free include paths */
893 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
894 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
896 tcc_free(s1
->tcc_lib_path
);
897 tcc_free(s1
->soname
);
899 tcc_free(s1
->elf_entryname
);
900 tcc_free(s1
->init_symbol
);
901 tcc_free(s1
->fini_symbol
);
902 tcc_free(s1
->mapfile
);
903 tcc_free(s1
->outfile
);
904 tcc_free(s1
->deps_outfile
);
905 #if defined TCC_TARGET_MACHO
906 tcc_free(s1
->install_name
);
908 dynarray_reset(&s1
->files
, &s1
->nb_files
);
909 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
910 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
911 dynarray_reset(&s1
->argv
, &s1
->argc
);
912 cstr_free(&s1
->cmdline_defs
);
913 cstr_free(&s1
->cmdline_incl
);
914 cstr_free(&s1
->linker_arg
);
916 /* free runtime memory */
919 tcc_free(s1
->dState
);
926 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
928 #ifdef CONFIG_TCC_PIE
929 if (output_type
== TCC_OUTPUT_EXE
)
930 output_type
|= TCC_OUTPUT_DYN
;
932 s
->output_type
= output_type
;
935 /* default include paths */
936 /* -isystem paths have already been handled */
937 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
940 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
947 /* add debug sections */
950 #ifdef CONFIG_TCC_BCHECK
951 if (s
->do_bounds_check
) {
952 /* if bound checking, then add corresponding sections */
953 tccelf_bounds_new(s
);
957 if (output_type
== TCC_OUTPUT_OBJ
) {
958 /* always elf for objects */
959 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
963 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
967 /* allow linking with system dll's directly */
968 tcc_add_systemdir(s
);
970 /* target PE has its own startup code in libtcc1.a */
973 #elif defined TCC_TARGET_MACHO
974 # ifdef TCC_IS_NATIVE
975 tcc_add_macos_sdkpath(s
);
977 /* Mach-O with LC_MAIN doesn't need any crt startup code. */
981 /* paths for crt objects */
982 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
984 /* add libc crt1/crti objects */
985 if (output_type
!= TCC_OUTPUT_MEMORY
&& !s
->nostdlib
) {
987 if (output_type
!= TCC_OUTPUT_DLL
)
988 tcc_add_crt(s
, "crt0.o");
989 if (output_type
== TCC_OUTPUT_DLL
)
990 tcc_add_crt(s
, "crtbeginS.o");
992 tcc_add_crt(s
, "crtbegin.o");
993 #elif TARGETOS_FreeBSD
994 if (output_type
!= TCC_OUTPUT_DLL
)
995 tcc_add_crt(s
, "crt1.o");
996 tcc_add_crt(s
, "crti.o");
998 tcc_add_crt(s
, "crtbeginT.o");
999 else if (output_type
& TCC_OUTPUT_DYN
)
1000 tcc_add_crt(s
, "crtbeginS.o");
1002 tcc_add_crt(s
, "crtbegin.o");
1003 #elif TARGETOS_NetBSD
1004 if (output_type
!= TCC_OUTPUT_DLL
)
1005 tcc_add_crt(s
, "crt0.o");
1006 tcc_add_crt(s
, "crti.o");
1008 tcc_add_crt(s
, "crtbeginT.o");
1009 else if (output_type
& TCC_OUTPUT_DYN
)
1010 tcc_add_crt(s
, "crtbeginS.o");
1012 tcc_add_crt(s
, "crtbegin.o");
1013 #elif defined TARGETOS_ANDROID
1014 if (output_type
!= TCC_OUTPUT_DLL
)
1015 tcc_add_crt(s
, "crtbegin_dynamic.o");
1017 tcc_add_crt(s
, "crtbegin_so.o");
1019 if (output_type
!= TCC_OUTPUT_DLL
)
1020 tcc_add_crt(s
, "crt1.o");
1021 tcc_add_crt(s
, "crti.o");
1028 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
1030 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
1034 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
1036 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
1040 /* add/update a 'DLLReference', Just find if level == -1 */
1041 ST_FUNC DLLReference
*tcc_add_dllref(TCCState
*s1
, const char *dllname
, int level
)
1043 DLLReference
*ref
= NULL
;
1045 for (i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
1046 if (0 == strcmp(s1
->loaded_dlls
[i
]->name
, dllname
)) {
1047 ref
= s1
->loaded_dlls
[i
];
1053 if (level
< ref
->level
)
1058 ref
= tcc_mallocz(sizeof(DLLReference
) + strlen(dllname
));
1059 strcpy(ref
->name
, dllname
);
1060 dynarray_add(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, ref
);
1062 ref
->index
= s1
->nb_loaded_dlls
;
1066 /* OpenBSD: choose latest from libxxx.so.x.y versions */
1067 #if defined TARGETOS_OpenBSD && !defined _WIN32
1069 static int tcc_glob_so(TCCState
*s1
, const char *pattern
, char *buf
, int size
)
1074 int i
, v
, v1
, v2
, v3
;
1076 star
= strchr(pattern
, '*');
1077 if (!star
|| glob(pattern
, 0, NULL
, &g
))
1079 for (v
= -1, i
= 0; i
< g
.gl_pathc
; ++i
) {
1081 if (2 != sscanf(p
+ (star
- pattern
), "%d.%d.%d", &v1
, &v2
, &v3
))
1083 if ((v1
= v1
* 1000 + v2
) > v
)
1084 v
= v1
, pstrcpy(buf
, size
, p
);
1091 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
1095 #if defined TARGETOS_OpenBSD && !defined _WIN32
1097 if (tcc_glob_so(s1
, filename
, buf
, sizeof buf
) >= 0)
1101 /* ignore binary files with -E */
1102 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
1103 && (flags
& AFF_TYPE_BIN
))
1107 fd
= _tcc_open(s1
, filename
);
1109 if (flags
& AFF_PRINT_ERROR
)
1110 tcc_error_noabort("file '%s' not found", filename
);
1114 s1
->current_filename
= filename
;
1115 if (flags
& AFF_TYPE_BIN
) {
1119 obj_type
= tcc_object_type(fd
, &ehdr
);
1120 lseek(fd
, 0, SEEK_SET
);
1124 case AFF_BINTYPE_REL
:
1125 ret
= tcc_load_object_file(s1
, fd
, 0);
1128 case AFF_BINTYPE_AR
:
1129 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
1132 #ifdef TCC_TARGET_PE
1134 ret
= pe_load_file(s1
, fd
, filename
);
1137 #elif defined TCC_TARGET_MACHO
1138 case AFF_BINTYPE_DYN
:
1140 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1141 #ifdef TCC_IS_NATIVE
1143 const char* soname
= filename
;
1144 if (obj_type
!= AFF_BINTYPE_DYN
)
1145 soname
= macho_tbd_soname(filename
);
1146 dl
= dlopen(soname
, RTLD_GLOBAL
| RTLD_LAZY
);
1148 tcc_add_dllref(s1
, soname
, 0)->handle
= dl
, ret
= 0;
1149 if (filename
!= soname
)
1150 tcc_free((void *)soname
);
1152 } else if (obj_type
== AFF_BINTYPE_DYN
) {
1153 ret
= macho_load_dll(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1155 ret
= macho_load_tbd(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1160 const char *ext
= tcc_fileextension(filename
);
1161 if (!strcmp(ext
, ".tbd"))
1162 goto case_dyn_or_tbd
;
1163 if (!strcmp(ext
, ".dylib")) {
1164 obj_type
= AFF_BINTYPE_DYN
;
1165 goto case_dyn_or_tbd
;
1171 case AFF_BINTYPE_DYN
:
1172 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1173 #ifdef TCC_IS_NATIVE
1174 void* dl
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
1176 tcc_add_dllref(s1
, filename
, 0)->handle
= dl
, ret
= 0;
1179 ret
= tcc_load_dll(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1183 /* as GNU ld, consider it is an ld script if not recognized */
1184 ret
= tcc_load_ldscript(s1
, fd
);
1187 #endif /* pe / macos / unix */
1191 tcc_error_noabort("%s: unrecognized file type", filename
);
1194 #ifdef TCC_TARGET_COFF
1195 case AFF_BINTYPE_C67
:
1196 ret
= tcc_load_coff(s1
, fd
);
1202 /* update target deps */
1203 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
, tcc_strdup(filename
));
1204 ret
= tcc_compile(s1
, flags
, filename
, fd
);
1206 s1
->current_filename
= NULL
;
1210 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1212 int filetype
= s
->filetype
;
1213 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1214 /* use a file extension to detect a filetype */
1215 const char *ext
= tcc_fileextension(filename
);
1218 if (!strcmp(ext
, "S"))
1219 filetype
= AFF_TYPE_ASMPP
;
1220 else if (!strcmp(ext
, "s"))
1221 filetype
= AFF_TYPE_ASM
;
1222 else if (!PATHCMP(ext
, "c")
1223 || !PATHCMP(ext
, "h")
1224 || !PATHCMP(ext
, "i"))
1225 filetype
= AFF_TYPE_C
;
1227 filetype
|= AFF_TYPE_BIN
;
1229 filetype
= AFF_TYPE_C
;
1232 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1235 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1237 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1241 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1242 const char *filename
, int flags
, char **paths
, int nb_paths
)
1247 for(i
= 0; i
< nb_paths
; i
++) {
1248 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1249 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1255 /* find and load a dll. Return non zero if not found */
1256 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1258 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1259 s
->library_paths
, s
->nb_library_paths
);
1262 /* find [cross-]libtcc1.a and tcc helper objects in library path */
1263 ST_FUNC
void tcc_add_support(TCCState
*s1
, const char *filename
)
1266 if (CONFIG_TCC_CROSSPREFIX
[0])
1267 filename
= strcat(strcpy(buf
, CONFIG_TCC_CROSSPREFIX
), filename
);
1268 if (tcc_add_dll(s1
, filename
, 0) < 0)
1269 tcc_error_noabort("%s not found", filename
);
1272 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
1273 ST_FUNC
int tcc_add_crt(TCCState
*s1
, const char *filename
)
1275 if (-1 == tcc_add_library_internal(s1
, "%s/%s",
1276 filename
, 0, s1
->crt_paths
, s1
->nb_crt_paths
))
1277 return tcc_error_noabort("file '%s' not found", filename
);
1282 /* the library name is the same as the argument of the '-l' option */
1283 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1285 #if defined TCC_TARGET_PE
1286 static const char * const libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1287 const char * const *pp
= s
->static_link
? libs
+ 4 : libs
;
1288 #elif defined TCC_TARGET_MACHO
1289 static const char * const libs
[] = { "%s/lib%s.dylib", "%s/lib%s.tbd", "%s/lib%s.a", NULL
};
1290 const char * const *pp
= s
->static_link
? libs
+ 2 : libs
;
1291 #elif defined TARGETOS_OpenBSD
1292 static const char * const libs
[] = { "%s/lib%s.so.*", "%s/lib%s.a", NULL
};
1293 const char * const *pp
= s
->static_link
? libs
+ 1 : libs
;
1295 static const char * const libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1296 const char * const *pp
= s
->static_link
? libs
+ 1 : libs
;
1298 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1300 if (0 == tcc_add_library_internal(s
, *pp
,
1301 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1308 PUB_FUNC
int tcc_add_library_err(TCCState
*s1
, const char *libname
)
1310 int ret
= tcc_add_library(s1
, libname
);
1312 tcc_error_noabort("library '%s' not found", libname
);
1316 /* handle #pragma comment(lib,) */
1317 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1320 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1321 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1324 LIBTCCAPI
int tcc_add_symbol(TCCState
*s1
, const char *name
, const void *val
)
1326 #ifdef TCC_TARGET_PE
1327 /* On x86_64 'val' might not be reachable with a 32bit offset.
1328 So it is handled here as if it were in a DLL. */
1329 pe_putimport(s1
, 0, name
, (uintptr_t)val
);
1332 if (s1
->leading_underscore
) {
1334 pstrcpy(buf
+ 1, sizeof(buf
) - 1, name
);
1337 set_global_sym(s1
, name
, NULL
, (addr_t
)(uintptr_t)val
); /* NULL: SHN_ABS */
1342 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1344 tcc_free(s
->tcc_lib_path
);
1345 s
->tcc_lib_path
= tcc_strdup(path
);
1348 /********************************************************/
1349 /* options parser */
1351 static int strstart(const char *val
, const char **str
)
1366 /* Like strstart, but automatically takes into account that ld options can
1368 * - start with double or single dash (e.g. '--soname' or '-soname')
1369 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1370 * or '-Wl,-soname=x.so')
1372 * you provide `val` always in 'option[=]' form (no leading -)
1374 static int link_option(const char *str
, const char *val
, const char **ptr
)
1379 /* there should be 1 or 2 dashes */
1385 /* then str & val should match (potentially up to '=') */
1392 if (strstart("no-", &p
))
1396 while (*q
!= '\0' && *q
!= '=') {
1403 /* '=' near eos means ',' or '=' is ok */
1407 if (*p
!= ',' && *p
!= '=')
1417 static const char *skip_linker_arg(const char **str
)
1419 const char *s1
= *str
;
1420 const char *s2
= strchr(s1
, ',');
1421 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1425 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1431 p
[l
= strlen(p
)] = sep
, ++l
;
1432 skip_linker_arg(&q
);
1433 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1436 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1438 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1440 strcpy(f
->name
, filename
);
1441 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1444 /* set linker options */
1445 static int tcc_set_linker(TCCState
*s
, const char *option
)
1450 const char *p
= NULL
;
1455 if (link_option(option
, "Bsymbolic", &p
)) {
1457 } else if (link_option(option
, "nostdlib", &p
)) {
1459 } else if (link_option(option
, "e=", &p
)
1460 || link_option(option
, "entry=", &p
)) {
1461 copy_linker_arg(&s
->elf_entryname
, p
, 0);
1462 } else if (link_option(option
, "fini=", &p
)) {
1463 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1465 } else if (link_option(option
, "image-base=", &p
)
1466 || link_option(option
, "Ttext=", &p
)) {
1467 s
->text_addr
= strtoull(p
, &end
, 16);
1468 s
->has_text_addr
= 1;
1469 } else if (link_option(option
, "init=", &p
)) {
1470 copy_linker_arg(&s
->init_symbol
, p
, 0);
1472 } else if (link_option(option
, "Map=", &p
)) {
1473 copy_linker_arg(&s
->mapfile
, p
, 0);
1475 } else if (link_option(option
, "oformat=", &p
)) {
1476 #if defined(TCC_TARGET_PE)
1477 if (strstart("pe-", &p
)) {
1479 if (strstart("elf64-", &p
)) {
1481 if (strstart("elf32-", &p
)) {
1483 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1484 } else if (!strcmp(p
, "binary")) {
1485 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1486 #ifdef TCC_TARGET_COFF
1487 } else if (!strcmp(p
, "coff")) {
1488 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1493 } else if (link_option(option
, "as-needed", &p
)) {
1495 } else if (link_option(option
, "O", &p
)) {
1497 } else if (link_option(option
, "export-all-symbols", &p
)) {
1499 } else if (link_option(option
, "export-dynamic", &p
)) {
1501 } else if (link_option(option
, "rpath=", &p
)) {
1502 copy_linker_arg(&s
->rpath
, p
, ':');
1503 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1504 s
->enable_new_dtags
= 1;
1505 } else if (link_option(option
, "section-alignment=", &p
)) {
1506 s
->section_align
= strtoul(p
, &end
, 16);
1507 } else if (link_option(option
, "soname=", &p
)) {
1508 copy_linker_arg(&s
->soname
, p
, 0);
1509 } else if (link_option(option
, "install_name=", &p
)) {
1510 copy_linker_arg(&s
->soname
, p
, 0);
1511 #ifdef TCC_TARGET_PE
1512 } else if (link_option(option
, "large-address-aware", &p
)) {
1513 s
->pe_characteristics
|= 0x20;
1514 } else if (link_option(option
, "file-alignment=", &p
)) {
1515 s
->pe_file_align
= strtoul(p
, &end
, 16);
1516 } else if (link_option(option
, "stack=", &p
)) {
1517 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1518 } else if (link_option(option
, "subsystem=", &p
)) {
1519 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1520 if (!strcmp(p
, "native")) {
1521 s
->pe_subsystem
= 1;
1522 } else if (!strcmp(p
, "console")) {
1523 s
->pe_subsystem
= 3;
1524 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1525 s
->pe_subsystem
= 2;
1526 } else if (!strcmp(p
, "posix")) {
1527 s
->pe_subsystem
= 7;
1528 } else if (!strcmp(p
, "efiapp")) {
1529 s
->pe_subsystem
= 10;
1530 } else if (!strcmp(p
, "efiboot")) {
1531 s
->pe_subsystem
= 11;
1532 } else if (!strcmp(p
, "efiruntime")) {
1533 s
->pe_subsystem
= 12;
1534 } else if (!strcmp(p
, "efirom")) {
1535 s
->pe_subsystem
= 13;
1536 #elif defined(TCC_TARGET_ARM)
1537 if (!strcmp(p
, "wince")) {
1538 s
->pe_subsystem
= 9;
1543 #ifdef TCC_TARGET_MACHO
1544 } else if (link_option(option
, "all_load", &p
)) {
1545 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1546 } else if (link_option(option
, "force_load", &p
)) {
1547 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1548 args_parser_add_file(s
, p
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1550 } else if (link_option(option
, "single_module", &p
)) {
1553 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1555 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1557 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1558 } else if (link_option(option
, "z=", &p
)) {
1564 return tcc_error_noabort("unsupported linker option '%s'", option
);
1567 tcc_warning_c(warn_unsupported
)("unsupported linker option '%s'", option
);
1568 option
= skip_linker_arg(&p
);
1573 typedef struct TCCOption
{
1580 TCC_OPTION_ignored
= 0,
1597 TCC_OPTION_dumpversion
,
1609 TCC_OPTION_mfloat_abi
,
1613 TCC_OPTION_iwithprefix
,
1615 TCC_OPTION_nostdinc
,
1616 TCC_OPTION_nostdlib
,
1617 TCC_OPTION_print_search_dirs
,
1618 TCC_OPTION_rdynamic
,
1631 TCC_OPTION_dynamiclib
,
1632 TCC_OPTION_flat_namespace
,
1633 TCC_OPTION_two_levelnamespace
,
1634 TCC_OPTION_undefined
,
1635 TCC_OPTION_install_name
,
1636 TCC_OPTION_compatibility_version
,
1637 TCC_OPTION_current_version
,
1640 #define TCC_OPTION_HAS_ARG 0x0001
1641 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1643 static const TCCOption tcc_options
[] = {
1644 { "h", TCC_OPTION_HELP
, 0 },
1645 { "-help", TCC_OPTION_HELP
, 0 },
1646 { "?", TCC_OPTION_HELP
, 0 },
1647 { "hh", TCC_OPTION_HELP2
, 0 },
1648 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1649 { "-version", TCC_OPTION_v
, 0 }, /* handle as verbose, also prints version*/
1650 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1651 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1652 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1653 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1654 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1655 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1656 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1657 { "bench", TCC_OPTION_bench
, 0 },
1658 #ifdef CONFIG_TCC_BACKTRACE
1659 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1661 #ifdef CONFIG_TCC_BCHECK
1662 { "b", TCC_OPTION_b
, 0 },
1664 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1665 #ifdef TCC_TARGET_MACHO
1666 { "compatibility_version", TCC_OPTION_compatibility_version
, TCC_OPTION_HAS_ARG
},
1667 { "current_version", TCC_OPTION_current_version
, TCC_OPTION_HAS_ARG
},
1669 { "c", TCC_OPTION_c
, 0 },
1670 #ifdef TCC_TARGET_MACHO
1671 { "dynamiclib", TCC_OPTION_dynamiclib
, 0 },
1673 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1674 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1675 { "static", TCC_OPTION_static
, 0 },
1676 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1677 { "shared", TCC_OPTION_shared
, 0 },
1678 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1679 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1680 { "pthread", TCC_OPTION_pthread
, 0},
1681 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1682 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1683 { "r", TCC_OPTION_r
, 0 },
1684 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1685 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1686 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1687 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1688 #ifdef TCC_TARGET_ARM
1689 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1691 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1692 #ifdef TCC_TARGET_MACHO
1693 { "flat_namespace", TCC_OPTION_flat_namespace
, 0 },
1695 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1696 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1697 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1698 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1699 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1700 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1701 { "w", TCC_OPTION_w
, 0 },
1702 { "E", TCC_OPTION_E
, 0},
1703 { "M", TCC_OPTION_M
, 0},
1704 { "MD", TCC_OPTION_MD
, 0},
1705 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1706 { "MM", TCC_OPTION_MM
, 0},
1707 { "MMD", TCC_OPTION_MMD
, 0},
1708 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1709 { "ar", TCC_OPTION_ar
, 0},
1710 #ifdef TCC_TARGET_PE
1711 { "impdef", TCC_OPTION_impdef
, 0},
1713 #ifdef TCC_TARGET_MACHO
1714 { "install_name", TCC_OPTION_install_name
, TCC_OPTION_HAS_ARG
},
1715 { "two_levelnamespace", TCC_OPTION_two_levelnamespace
, 0 },
1716 { "undefined", TCC_OPTION_undefined
, TCC_OPTION_HAS_ARG
},
1718 /* ignored (silently, except after -Wunsupported) */
1719 { "arch", 0, TCC_OPTION_HAS_ARG
},
1721 { "-param", 0, TCC_OPTION_HAS_ARG
},
1722 { "pedantic", 0, 0 },
1725 { "traditional", 0, 0 },
1729 typedef struct FlagDef
{
1735 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1736 #define FD_INVERT 0x0002 /* invert value before storing */
1738 static const FlagDef options_W
[] = {
1739 { offsetof(TCCState
, warn_all
), WD_ALL
, "all" },
1740 { offsetof(TCCState
, warn_error
), 0, "error" },
1741 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1742 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1743 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
, "implicit-function-declaration" },
1744 { offsetof(TCCState
, warn_discarded_qualifiers
), WD_ALL
, "discarded-qualifiers" },
1748 static const FlagDef options_f
[] = {
1749 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1750 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1751 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1752 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1753 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1754 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1755 { offsetof(TCCState
, test_coverage
), 0, "test-coverage" },
1759 static const FlagDef options_m
[] = {
1760 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1761 #ifdef TCC_TARGET_X86_64
1762 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1767 static int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1769 int value
, mask
, ret
;
1774 r
= name
, value
= !strstart("no-", &r
), mask
= 0;
1776 /* when called with options_W, look for -W[no-]error=<option> */
1777 if ((flags
->flags
& WD_ALL
) && strstart("error=", &r
))
1778 value
= value
? WARN_ON
|WARN_ERR
: WARN_NOE
, mask
= WARN_ON
;
1780 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1782 if (strcmp(r
, p
->name
))
1785 if (0 == (p
->flags
& WD_ALL
))
1789 f
= (unsigned char *)s
+ p
->offset
;
1790 *f
= (*f
& mask
) | (value
^ !!(p
->flags
& FD_INVERT
));
1794 if (strcmp(r
, "all"))
1801 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1806 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1812 while (c
= (unsigned char)*r
, c
) {
1814 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1816 } else if (c
== '"') {
1819 } else if (q
== 0 && c
<= ' ') {
1825 //printf("<%s>\n", str.data), fflush(stdout);
1826 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1833 /* read list file */
1834 static int args_parser_listfile(TCCState
*s
,
1835 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1843 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1845 return tcc_error_noabort("listfile '%s' not found", filename
);
1847 p
= tcc_load_text(fd
);
1848 for (i
= 0; i
< *pargc
; ++i
)
1850 args_parser_make_argv(p
, &argc
, &argv
);
1852 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1855 dynarray_reset(&s
->argv
, &s
->argc
);
1856 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1860 #if defined TCC_TARGET_MACHO
1861 static uint32_t parse_version(TCCState
*s1
, const char *version
)
1868 a
= strtoul(version
, &last
, 10);
1870 b
= strtoul(&last
[1], &last
, 10);
1872 c
= strtoul(&last
[1], &last
, 10);
1874 if (*last
|| a
> 0xffff || b
> 0xff || c
> 0xff)
1875 tcc_error_noabort("version a.b.c not correct: %s", version
);
1876 return (a
<< 16) | (b
<< 8) | c
;
1880 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1883 const TCCOption
*popt
;
1884 const char *optarg
, *r
;
1885 const char *run
= NULL
;
1887 int tool
= 0, arg_start
= 0, noaction
= optind
;
1888 char **argv
= *pargv
;
1891 cstr_reset(&s
->linker_arg
);
1893 while (optind
< argc
) {
1895 if (r
[0] == '@' && r
[1] != '\0') {
1896 if (args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
))
1902 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1907 if (r
[0] != '-' || r
[1] == '\0') {
1908 args_parser_add_file(s
, r
, s
->filetype
);
1911 if (tcc_set_options(s
, run
))
1913 arg_start
= optind
- 1;
1919 /* allow "tcc files... -run -- args ..." */
1920 if (r
[1] == '-' && r
[2] == '\0' && run
)
1923 /* find option in table */
1924 for(popt
= tcc_options
; ; ++popt
) {
1925 const char *p1
= popt
->name
;
1926 const char *r1
= r
+ 1;
1928 return tcc_error_noabort("invalid option -- '%s'", r
);
1929 if (!strstart(p1
, &r1
))
1932 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1933 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1936 return tcc_error_noabort("argument to '%s' is missing", r
);
1937 optarg
= argv
[optind
++];
1939 } else if (*r1
!= '\0')
1944 switch(popt
->index
) {
1945 case TCC_OPTION_HELP
:
1948 case TCC_OPTION_HELP2
:
1952 tcc_add_include_path(s
, optarg
);
1955 tcc_define_symbol(s
, optarg
, NULL
);
1958 tcc_undefine_symbol(s
, optarg
);
1961 tcc_add_library_path(s
, optarg
);
1964 /* set tcc utilities path (mainly for tcc development) */
1965 tcc_set_lib_path(s
, optarg
);
1969 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1972 case TCC_OPTION_pthread
:
1973 s
->option_pthread
= 1;
1975 case TCC_OPTION_bench
:
1978 #ifdef CONFIG_TCC_BACKTRACE
1980 s
->rt_num_callers
= atoi(optarg
);
1981 s
->do_backtrace
= 1;
1983 s
->dwarf
= DWARF_VERSION
;
1986 #ifdef CONFIG_TCC_BCHECK
1988 s
->do_bounds_check
= 1;
1989 s
->do_backtrace
= 1;
1991 s
->dwarf
= DWARF_VERSION
;
1996 s
->dwarf
= DWARF_VERSION
;
1998 if (strstart("dwarf", &optarg
))
1999 s
->dwarf
= (*optarg
) ? (0 - atoi(optarg
)) : DEFAULT_DWARF_VERSION
;
2005 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
2011 else if (*optarg
== 'M')
2013 else if (*optarg
== 't')
2015 else if (isnum(*optarg
))
2016 s
->g_debug
|= atoi(optarg
);
2018 goto unsupported_option
;
2020 case TCC_OPTION_static
:
2023 case TCC_OPTION_std
:
2024 if (strcmp(optarg
, "=c11") == 0)
2025 s
->cversion
= 201112;
2027 case TCC_OPTION_shared
:
2029 goto set_output_type
;
2030 case TCC_OPTION_soname
:
2031 s
->soname
= tcc_strdup(optarg
);
2035 tcc_warning("multiple -o option");
2036 tcc_free(s
->outfile
);
2038 s
->outfile
= tcc_strdup(optarg
);
2041 /* generate a .o merging several output files */
2044 goto set_output_type
;
2045 case TCC_OPTION_isystem
:
2046 tcc_add_sysinclude_path(s
, optarg
);
2048 case TCC_OPTION_include
:
2049 cstr_printf(&s
->cmdline_incl
, "#include \"%s\"\n", optarg
);
2051 case TCC_OPTION_nostdinc
:
2054 case TCC_OPTION_nostdlib
:
2057 case TCC_OPTION_run
:
2058 #ifndef TCC_IS_NATIVE
2059 return tcc_error_noabort("-run is not available in a cross compiler");
2062 x
= TCC_OUTPUT_MEMORY
;
2063 goto set_output_type
;
2066 do ++s
->verbose
; while (*optarg
++ == 'v');
2070 if (set_flag(s
, options_f
, optarg
) < 0)
2071 goto unsupported_option
;
2073 #ifdef TCC_TARGET_ARM
2074 case TCC_OPTION_mfloat_abi
:
2075 /* tcc doesn't support soft float yet */
2076 if (!strcmp(optarg
, "softfp")) {
2077 s
->float_abi
= ARM_SOFTFP_FLOAT
;
2078 } else if (!strcmp(optarg
, "hard"))
2079 s
->float_abi
= ARM_HARD_FLOAT
;
2081 return tcc_error_noabort("unsupported float abi '%s'", optarg
);
2085 if (set_flag(s
, options_m
, optarg
) < 0) {
2086 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
2087 goto unsupported_option
;
2088 if (PTR_SIZE
!= x
/8)
2095 if (optarg
[0] && set_flag(s
, options_W
, optarg
) < 0)
2096 goto unsupported_option
;
2101 case TCC_OPTION_rdynamic
:
2105 if (s
->linker_arg
.size
)
2106 ((char*)s
->linker_arg
.data
)[s
->linker_arg
.size
- 1] = ',';
2107 cstr_cat(&s
->linker_arg
, optarg
, 0);
2108 x
= tcc_set_linker(s
, s
->linker_arg
.data
);
2110 cstr_reset(&s
->linker_arg
);
2118 x
= TCC_OUTPUT_PREPROCESS
;
2119 goto set_output_type
;
2121 s
->Pflag
= atoi(optarg
) + 1;
2124 s
->include_sys_deps
= 1;
2128 if(!s
->deps_outfile
)
2129 s
->deps_outfile
= tcc_strdup("-");
2131 case TCC_OPTION_MMD
:
2136 s
->include_sys_deps
= 1;
2139 s
->deps_outfile
= tcc_strdup(optarg
);
2141 case TCC_OPTION_dumpversion
:
2142 printf ("%s\n", TCC_VERSION
);
2149 else if (*optarg
== 'a')
2151 else if (*optarg
== 'b')
2153 else if (*optarg
== 'n')
2156 tcc_warning("unsupported language '%s'", optarg
);
2157 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
2160 s
->optimize
= atoi(optarg
);
2162 case TCC_OPTION_print_search_dirs
:
2165 case TCC_OPTION_impdef
:
2168 #if defined TCC_TARGET_MACHO
2169 case TCC_OPTION_dynamiclib
:
2171 goto set_output_type
;
2172 case TCC_OPTION_flat_namespace
:
2174 case TCC_OPTION_two_levelnamespace
:
2176 case TCC_OPTION_undefined
:
2178 case TCC_OPTION_install_name
:
2179 s
->install_name
= tcc_strdup(optarg
);
2181 case TCC_OPTION_compatibility_version
:
2182 s
->compatibility_version
= parse_version(s
, optarg
);
2184 case TCC_OPTION_current_version
:
2185 s
->current_version
= parse_version(s
, optarg
);;
2191 arg_start
= optind
- 1;
2192 if (arg_start
!= noaction
)
2193 return tcc_error_noabort("cannot parse %s here", r
);
2198 tcc_warning_c(warn_unsupported
)("unsupported option '%s'", r
);
2202 if (s
->linker_arg
.size
) {
2203 r
= s
->linker_arg
.data
;
2206 *pargc
= argc
- arg_start
;
2207 *pargv
= argv
+ arg_start
;
2210 if (optind
!= noaction
)
2212 if (s
->verbose
== 2)
2213 return OPT_PRINT_DIRS
;
2219 LIBTCCAPI
int tcc_set_options(TCCState
*s
, const char *r
)
2223 args_parser_make_argv(r
, &argc
, &argv
);
2224 ret
= tcc_parse_args(s
, &argc
, &argv
, 0);
2225 dynarray_reset(&argv
, &argc
);
2226 return ret
< 0 ? ret
: 0;
2229 PUB_FUNC
void tcc_print_stats(TCCState
*s1
, unsigned total_time
)
2233 fprintf(stderr
, "# %d idents, %d lines, %u bytes\n"
2234 "# %0.3f s, %u lines/s, %0.1f MB/s\n",
2235 total_idents
, total_lines
, total_bytes
,
2236 (double)total_time
/1000,
2237 (unsigned)total_lines
*1000/total_time
,
2238 (double)total_bytes
/1000/total_time
);
2239 fprintf(stderr
, "# text %u, data.rw %u, data.ro %u, bss %u bytes\n",
2240 s1
->total_output
[0],
2241 s1
->total_output
[1],
2242 s1
->total_output
[2],
2246 fprintf(stderr
, "# %d bytes memory used\n", mem_max_size
);