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
);
76 /********************************************************/
78 ST_FUNC
char *normalize_slashes(char *path
)
81 for (p
= path
; *p
; ++p
)
87 #if defined LIBTCC_AS_DLL && !defined CONFIG_TCCDIR
88 static HMODULE tcc_module
;
89 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
91 if (DLL_PROCESS_ATTACH
== dwReason
)
96 #define tcc_module NULL /* NULL means executable itself */
100 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
101 static inline char *config_tccdir_w32(char *path
)
104 GetModuleFileName(tcc_module
, path
, MAX_PATH
);
105 p
= tcc_basename(normalize_slashes(strlwr(path
)));
111 #define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))
115 static void tcc_add_systemdir(TCCState
*s
)
118 GetSystemDirectory(buf
, sizeof buf
);
119 tcc_add_library_path(s
, normalize_slashes(buf
));
124 /********************************************************/
125 #if CONFIG_TCC_SEMLOCK
127 ST_FUNC
void wait_sem(TCCSem
*p
)
130 InitializeCriticalSection(&p
->cr
), p
->init
= 1;
131 EnterCriticalSection(&p
->cr
);
133 ST_FUNC
void post_sem(TCCSem
*p
)
135 LeaveCriticalSection(&p
->cr
);
137 #elif defined __APPLE__
138 /* Half-compatible MacOS doesn't have non-shared (process local)
139 semaphores. Use the dispatch framework for lightweight locks. */
140 ST_FUNC
void wait_sem(TCCSem
*p
)
143 p
->sem
= dispatch_semaphore_create(1), p
->init
= 1;
144 dispatch_semaphore_wait(p
->sem
, DISPATCH_TIME_FOREVER
);
146 ST_FUNC
void post_sem(TCCSem
*p
)
148 dispatch_semaphore_signal(p
->sem
);
151 ST_FUNC
void wait_sem(TCCSem
*p
)
154 sem_init(&p
->sem
, 0, 1), p
->init
= 1;
155 while (sem_wait(&p
->sem
) < 0 && errno
== EINTR
);
157 ST_FUNC
void post_sem(TCCSem
*p
)
164 PUB_FUNC
void tcc_enter_state(TCCState
*s1
)
166 if (s1
->error_set_jmp_enabled
)
168 WAIT_SEM(&tcc_compile_sem
);
172 PUB_FUNC
void tcc_exit_state(TCCState
*s1
)
174 if (s1
->error_set_jmp_enabled
)
177 POST_SEM(&tcc_compile_sem
);
180 /********************************************************/
181 /* copy a string and truncate it. */
182 ST_FUNC
char *pstrcpy(char *buf
, size_t buf_size
, const char *s
)
189 q_end
= buf
+ buf_size
- 1;
201 /* strcat and truncate. */
202 ST_FUNC
char *pstrcat(char *buf
, size_t buf_size
, const char *s
)
207 pstrcpy(buf
+ len
, buf_size
- len
, s
);
211 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
213 memcpy(out
, in
, num
);
218 /* extract the basename of a file */
219 PUB_FUNC
char *tcc_basename(const char *name
)
221 char *p
= strchr(name
, 0);
222 while (p
> name
&& !IS_DIRSEP(p
[-1]))
227 /* extract extension part of a file
229 * (if no extension, return pointer to end-of-string)
231 PUB_FUNC
char *tcc_fileextension (const char *name
)
233 char *b
= tcc_basename(name
);
234 char *e
= strrchr(b
, '.');
235 return e
? e
: strchr(b
, 0);
238 ST_FUNC
char *tcc_load_text(int fd
)
240 int len
= lseek(fd
, 0, SEEK_END
);
241 char *buf
= load_data(fd
, 0, len
+ 1);
246 /********************************************************/
247 /* memory management */
255 PUB_FUNC
void tcc_free(void *ptr
)
260 PUB_FUNC
void *tcc_malloc(unsigned long size
)
265 _tcc_error("memory full (malloc)");
269 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
272 ptr
= tcc_malloc(size
);
274 memset(ptr
, 0, size
);
278 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
281 ptr1
= realloc(ptr
, size
);
283 _tcc_error("memory full (realloc)");
287 PUB_FUNC
char *tcc_strdup(const char *str
)
290 ptr
= tcc_malloc(strlen(str
) + 1);
297 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
298 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
299 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
300 #define MEM_DEBUG_FILE_LEN 40
301 #define MEM_DEBUG_CHECK3(header) \
302 ((mem_debug_header_t*)((char*)header + header->size))->magic3
303 #define MEM_USER_PTR(header) \
304 ((char *)header + offsetof(mem_debug_header_t, magic3))
305 #define MEM_HEADER_PTR(ptr) \
306 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
308 struct mem_debug_header
{
311 struct mem_debug_header
*prev
;
312 struct mem_debug_header
*next
;
314 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
316 ALIGNED(16) unsigned char magic3
[4];
319 typedef struct mem_debug_header mem_debug_header_t
;
321 static mem_debug_header_t
*mem_debug_chain
;
322 static unsigned mem_cur_size
;
323 static unsigned mem_max_size
;
325 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
327 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
328 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
329 header
->magic2
!= MEM_DEBUG_MAGIC2
||
330 read32le(MEM_DEBUG_CHECK3(header
)) != MEM_DEBUG_MAGIC3
||
331 header
->size
== (unsigned)-1) {
332 fprintf(stderr
, "%s check failed\n", msg
);
333 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
334 fprintf(stderr
, "%s:%u: block allocated here.\n",
335 header
->file_name
, header
->line_num
);
341 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
344 mem_debug_header_t
*header
;
346 header
= malloc(sizeof(mem_debug_header_t
) + size
);
348 _tcc_error("memory full (malloc)");
350 header
->magic1
= MEM_DEBUG_MAGIC1
;
351 header
->magic2
= MEM_DEBUG_MAGIC2
;
353 write32le(MEM_DEBUG_CHECK3(header
), MEM_DEBUG_MAGIC3
);
354 header
->line_num
= line
;
355 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
356 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
357 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
359 header
->next
= mem_debug_chain
;
362 header
->next
->prev
= header
;
363 mem_debug_chain
= header
;
365 mem_cur_size
+= size
;
366 if (mem_cur_size
> mem_max_size
)
367 mem_max_size
= mem_cur_size
;
369 return MEM_USER_PTR(header
);
372 PUB_FUNC
void tcc_free_debug(void *ptr
)
374 mem_debug_header_t
*header
;
377 header
= malloc_check(ptr
, "tcc_free");
378 mem_cur_size
-= header
->size
;
379 header
->size
= (unsigned)-1;
381 header
->next
->prev
= header
->prev
;
383 header
->prev
->next
= header
->next
;
384 if (header
== mem_debug_chain
)
385 mem_debug_chain
= header
->next
;
389 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
392 ptr
= tcc_malloc_debug(size
,file
,line
);
393 memset(ptr
, 0, size
);
397 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
399 mem_debug_header_t
*header
;
400 int mem_debug_chain_update
= 0;
402 return tcc_malloc_debug(size
, file
, line
);
403 header
= malloc_check(ptr
, "tcc_realloc");
404 mem_cur_size
-= header
->size
;
405 mem_debug_chain_update
= (header
== mem_debug_chain
);
406 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
408 _tcc_error("memory full (realloc)");
410 write32le(MEM_DEBUG_CHECK3(header
), MEM_DEBUG_MAGIC3
);
412 header
->next
->prev
= header
;
414 header
->prev
->next
= header
;
415 if (mem_debug_chain_update
)
416 mem_debug_chain
= header
;
417 mem_cur_size
+= size
;
418 if (mem_cur_size
> mem_max_size
)
419 mem_max_size
= mem_cur_size
;
420 return MEM_USER_PTR(header
);
423 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
426 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
431 PUB_FUNC
void tcc_memcheck(void)
434 mem_debug_header_t
*header
= mem_debug_chain
;
435 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
436 mem_cur_size
, mem_max_size
);
438 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
439 header
->file_name
, header
->line_num
, header
->size
);
440 header
= header
->next
;
447 #endif /* MEM_DEBUG */
449 #define free(p) use_tcc_free(p)
450 #define malloc(s) use_tcc_malloc(s)
451 #define realloc(p, s) use_tcc_realloc(p, s)
453 /********************************************************/
456 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
462 pp
= *(void ***)ptab
;
463 /* every power of two we double array size */
464 if ((nb
& (nb
- 1)) == 0) {
469 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
476 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
479 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
482 tcc_free(*(void**)pp
);
486 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
494 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
495 if (c
== '{' && p
[1] && p
[2] == '}') {
498 cstr_cat(&str
, s
->tcc_lib_path
, -1);
500 cstr_cat(&str
, CONFIG_SYSROOT
, -1);
501 if (c
== 'f' && file
) {
502 /* substitute current file's dir */
503 const char *f
= file
->true_filename
;
504 const char *b
= tcc_basename(f
);
506 cstr_cat(&str
, f
, b
- f
- 1);
508 cstr_cat(&str
, ".", 1);
515 cstr_ccat(&str
, '\0');
516 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
523 /********************************************************/
524 /* warning / error */
526 /* warn_... option bits */
527 #define WARN_ON 1 /* warning is on (-Woption) */
528 #define WARN_ERR 2 /* warning is an error (-Werror=option) */
529 #define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
532 enum { ERROR_WARN
, ERROR_NOABORT
, ERROR_ERROR
};
534 static void error1(int mode
, const char *fmt
, va_list ap
)
536 BufferedFile
**pf
, *f
;
537 TCCState
*s1
= tcc_state
;
543 /* can happen only if called from tcc_malloc(): 'out of memory' */
548 if (mode
== ERROR_WARN
) {
552 /* handle tcc_warning_c(warn_option)(fmt, ...) */
553 int wopt
= *(&s1
->warn_none
+ s1
->warn_num
);
555 if (0 == (wopt
& WARN_ON
))
567 if (s1
->error_set_jmp_enabled
) { /* we're called while parsing a file */
568 /* use upper file if inline ":asm:" or token ":paste:" */
569 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
573 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
574 cstr_printf(&cs
, "In file included from %s:%d:\n",
575 (*pf
)->filename
, (*pf
)->line_num
);
576 cstr_printf(&cs
, "%s:%d: ",
577 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
578 } else if (s1
->current_filename
) {
579 cstr_printf(&cs
, "%s: ", s1
->current_filename
);
584 cstr_printf(&cs
, "tcc: ");
585 cstr_printf(&cs
, mode
== ERROR_WARN
? "warning: " : "error: ");
586 cstr_vprintf(&cs
, fmt
, ap
);
587 if (!s1
|| !s1
->error_func
) {
588 /* default case: stderr */
589 if (s1
&& s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
590 printf("\n"); /* print a newline during tcc -E */
591 fflush(stdout
); /* flush -v output */
592 fprintf(stderr
, "%s\n", (char*)cs
.data
);
593 fflush(stderr
); /* print error/warning now (win32) */
595 s1
->error_func(s1
->error_opaque
, (char*)cs
.data
);
599 if (mode
!= ERROR_WARN
)
601 if (mode
!= ERROR_ERROR
)
603 if (s1
->error_set_jmp_enabled
)
604 longjmp(s1
->error_jmp_buf
, 1);
609 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
, TCCErrorFunc error_func
)
611 s
->error_opaque
= error_opaque
;
612 s
->error_func
= error_func
;
615 LIBTCCAPI TCCErrorFunc
tcc_get_error_func(TCCState
*s
)
617 return s
->error_func
;
620 LIBTCCAPI
void *tcc_get_error_opaque(TCCState
*s
)
622 return s
->error_opaque
;
625 /* error without aborting current compilation */
626 PUB_FUNC
void _tcc_error_noabort(const char *fmt
, ...)
630 error1(ERROR_NOABORT
, fmt
, ap
);
634 PUB_FUNC
void _tcc_error(const char *fmt
, ...)
638 for (;;) error1(ERROR_ERROR
, fmt
, ap
);
641 PUB_FUNC
void _tcc_warning(const char *fmt
, ...)
645 error1(ERROR_WARN
, fmt
, ap
);
649 /********************************************************/
652 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
655 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
657 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
658 bf
->buf_ptr
= bf
->buffer
;
659 bf
->buf_end
= bf
->buffer
+ initlen
;
660 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
661 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
663 normalize_slashes(bf
->filename
);
665 bf
->true_filename
= bf
->filename
;
667 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
671 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
674 ST_FUNC
void tcc_close(void)
676 TCCState
*s1
= tcc_state
;
677 BufferedFile
*bf
= file
;
680 total_lines
+= bf
->line_num
;
682 if (bf
->true_filename
!= bf
->filename
)
683 tcc_free(bf
->true_filename
);
688 static int _tcc_open(TCCState
*s1
, const char *filename
)
691 if (strcmp(filename
, "-") == 0)
692 fd
= 0, filename
= "<stdin>";
694 fd
= open(filename
, O_RDONLY
| O_BINARY
);
695 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
696 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
697 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
701 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
703 int fd
= _tcc_open(s1
, filename
);
706 tcc_open_bf(s1
, filename
, 0);
711 /* compile the file opened in 'file'. Return non zero if errors. */
712 static int tcc_compile(TCCState
*s1
, int filetype
, const char *str
, int fd
)
714 /* Here we enter the code section where we use the global variables for
715 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
716 Other threads need to wait until we're done.
718 Alternatively we could use thread local storage for those global
719 variables, which may or may not have advantages */
722 s1
->error_set_jmp_enabled
= 1;
724 if (setjmp(s1
->error_jmp_buf
) == 0) {
728 int len
= strlen(str
);
729 tcc_open_bf(s1
, "<string>", len
);
730 memcpy(file
->buffer
, str
, len
);
732 tcc_open_bf(s1
, str
, 0);
736 tccelf_begin_file(s1
);
737 preprocess_start(s1
, filetype
);
739 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
741 } else if (filetype
& (AFF_TYPE_ASM
| AFF_TYPE_ASMPP
)) {
742 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
750 s1
->error_set_jmp_enabled
= 0;
754 return s1
->nb_errors
!= 0 ? -1 : 0;
757 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
759 return tcc_compile(s
, s
->filetype
, str
, -1);
762 /* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
763 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
766 if (NULL
== (eq
= strchr(sym
, '=')))
769 value
= *eq
? eq
+ 1 : "1";
770 cstr_printf(&s1
->cmdline_defs
, "#define %.*s %s\n", (int)(eq
-sym
), sym
, value
);
773 /* undefine a preprocessor symbol */
774 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
776 cstr_printf(&s1
->cmdline_defs
, "#undef %s\n", sym
);
780 LIBTCCAPI TCCState
*tcc_new(void)
784 s
= tcc_mallocz(sizeof(TCCState
));
796 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
797 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
798 s
->warn_implicit_function_declaration
= 1;
799 s
->warn_discarded_qualifiers
= 1;
800 s
->ms_extensions
= 1;
802 #ifdef CHAR_IS_UNSIGNED
803 s
->char_is_unsigned
= 1;
805 #ifdef TCC_TARGET_I386
808 /* enable this if you want symbols with leading underscore on windows: */
809 #if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
810 s
->leading_underscore
= 1;
812 #ifdef TCC_TARGET_ARM
813 s
->float_abi
= ARM_FLOAT_ABI
;
815 #ifdef CONFIG_NEW_DTAGS
816 s
->enable_new_dtags
= 1;
819 /* might be used in error() before preprocess_start() */
820 s
->include_stack_ptr
= s
->include_stack
;
824 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
828 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
833 /* free library paths */
834 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
835 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
837 /* free include paths */
838 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
839 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
841 tcc_free(s1
->tcc_lib_path
);
842 tcc_free(s1
->soname
);
844 tcc_free(s1
->elf_entryname
);
845 tcc_free(s1
->init_symbol
);
846 tcc_free(s1
->fini_symbol
);
847 tcc_free(s1
->mapfile
);
848 tcc_free(s1
->outfile
);
849 tcc_free(s1
->deps_outfile
);
850 dynarray_reset(&s1
->files
, &s1
->nb_files
);
851 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
852 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
853 dynarray_reset(&s1
->argv
, &s1
->argc
);
854 cstr_free(&s1
->cmdline_defs
);
855 cstr_free(&s1
->cmdline_incl
);
857 /* free runtime memory */
860 tcc_free(s1
->dState
);
863 if (0 == --nb_states
)
868 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
870 #ifdef CONFIG_TCC_PIE
871 if (output_type
== TCC_OUTPUT_EXE
)
872 output_type
|= TCC_OUTPUT_DYN
;
874 s
->output_type
= output_type
;
877 /* default include paths */
878 /* -isystem paths have already been handled */
879 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
882 if (output_type
== TCC_OUTPUT_PREPROCESS
)
885 #ifdef CONFIG_TCC_BCHECK
886 if (s
->do_bounds_check
) {
887 /* if bound checking, then add corresponding sections */
888 tccelf_bounds_new(s
);
892 /* add debug sections */
895 if (output_type
== TCC_OUTPUT_OBJ
) {
896 /* always elf for objects */
897 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
901 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
905 /* allow linking with system dll's directly */
906 tcc_add_systemdir(s
);
908 /* target PE has its own startup code in libtcc1.a */
911 #elif defined TCC_TARGET_MACHO
912 # ifdef TCC_IS_NATIVE
913 tcc_add_macos_sdkpath(s
);
915 /* Mach-O with LC_MAIN doesn't need any crt startup code. */
919 /* paths for crt objects */
920 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
922 /* add libc crt1/crti objects */
923 if (output_type
!= TCC_OUTPUT_MEMORY
&& !s
->nostdlib
) {
925 if (output_type
!= TCC_OUTPUT_DLL
)
926 tcc_add_crt(s
, "crt0.o");
927 if (output_type
& TCC_OUTPUT_DYN
)
928 tcc_add_crt(s
, "crtbeginS.o");
930 tcc_add_crt(s
, "crtbegin.o");
931 #elif TARGETOS_FreeBSD
932 if (output_type
!= TCC_OUTPUT_DLL
)
933 tcc_add_crt(s
, "crt1.o");
934 tcc_add_crt(s
, "crti.o");
936 tcc_add_crt(s
, "crtbeginT.o");
937 else if (output_type
& TCC_OUTPUT_DYN
)
938 tcc_add_crt(s
, "crtbeginS.o");
940 tcc_add_crt(s
, "crtbegin.o");
941 #elif TARGETOS_NetBSD
942 if (output_type
!= TCC_OUTPUT_DLL
)
943 tcc_add_crt(s
, "crt0.o");
944 tcc_add_crt(s
, "crti.o");
946 tcc_add_crt(s
, "crtbeginT.o");
947 else if (output_type
& TCC_OUTPUT_DYN
)
948 tcc_add_crt(s
, "crtbeginS.o");
950 tcc_add_crt(s
, "crtbegin.o");
951 #elif defined TARGETOS_ANDROID
952 if (output_type
!= TCC_OUTPUT_DLL
)
953 tcc_add_crt(s
, "crtbegin_dynamic.o");
955 tcc_add_crt(s
, "crtbegin_so.o");
957 if (output_type
!= TCC_OUTPUT_DLL
)
958 tcc_add_crt(s
, "crt1.o");
959 tcc_add_crt(s
, "crti.o");
966 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
968 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
972 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
974 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
978 /* add/update a 'DLLReference', Just find if level == -1 */
979 ST_FUNC DLLReference
*tcc_add_dllref(TCCState
*s1
, const char *dllname
, int level
)
981 DLLReference
*ref
= NULL
;
983 for (i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
984 if (0 == strcmp(s1
->loaded_dlls
[i
]->name
, dllname
)) {
985 ref
= s1
->loaded_dlls
[i
];
991 if (level
< ref
->level
)
996 ref
= tcc_mallocz(sizeof(DLLReference
) + strlen(dllname
));
997 strcpy(ref
->name
, dllname
);
998 dynarray_add(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, ref
);
1000 ref
->index
= s1
->nb_loaded_dlls
;
1004 /* OpenBSD: choose latest from libxxx.so.x.y versions */
1005 #if defined TARGETOS_OpenBSD && !defined _WIN32
1007 static int tcc_glob_so(TCCState
*s1
, const char *pattern
, char *buf
, int size
)
1012 int i
, v
, v1
, v2
, v3
;
1014 star
= strchr(pattern
, '*');
1015 if (!star
|| glob(pattern
, 0, NULL
, &g
))
1017 for (v
= -1, i
= 0; i
< g
.gl_pathc
; ++i
) {
1019 if (2 != sscanf(p
+ (star
- pattern
), "%d.%d.%d", &v1
, &v2
, &v3
))
1021 if ((v1
= v1
* 1000 + v2
) > v
)
1022 v
= v1
, pstrcpy(buf
, size
, p
);
1029 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
1033 #if defined TARGETOS_OpenBSD && !defined _WIN32
1035 if (tcc_glob_so(s1
, filename
, buf
, sizeof buf
) >= 0)
1040 fd
= _tcc_open(s1
, filename
);
1042 if (flags
& AFF_PRINT_ERROR
)
1043 tcc_error_noabort("file '%s' not found", filename
);
1047 s1
->current_filename
= filename
;
1048 if (flags
& AFF_TYPE_BIN
) {
1052 obj_type
= tcc_object_type(fd
, &ehdr
);
1053 lseek(fd
, 0, SEEK_SET
);
1057 case AFF_BINTYPE_REL
:
1058 ret
= tcc_load_object_file(s1
, fd
, 0);
1061 case AFF_BINTYPE_AR
:
1062 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
1065 #ifdef TCC_TARGET_PE
1067 ret
= pe_load_file(s1
, fd
, filename
);
1070 #elif defined TCC_TARGET_MACHO
1071 case AFF_BINTYPE_DYN
:
1073 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1074 #ifdef TCC_IS_NATIVE
1076 const char* soname
= filename
;
1077 if (obj_type
!= AFF_BINTYPE_DYN
)
1078 soname
= macho_tbd_soname(filename
);
1079 dl
= dlopen(soname
, RTLD_GLOBAL
| RTLD_LAZY
);
1081 tcc_add_dllref(s1
, soname
, 0)->handle
= dl
, ret
= 0;
1082 if (filename
!= soname
)
1083 tcc_free((void *)soname
);
1085 } else if (obj_type
== AFF_BINTYPE_DYN
) {
1086 ret
= macho_load_dll(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1088 ret
= macho_load_tbd(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1093 const char *ext
= tcc_fileextension(filename
);
1094 if (!strcmp(ext
, ".tbd"))
1095 goto case_dyn_or_tbd
;
1096 if (!strcmp(ext
, ".dylib")) {
1097 obj_type
= AFF_BINTYPE_DYN
;
1098 goto case_dyn_or_tbd
;
1104 case AFF_BINTYPE_DYN
:
1105 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1106 #ifdef TCC_IS_NATIVE
1107 void* dl
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
1109 tcc_add_dllref(s1
, filename
, 0)->handle
= dl
, ret
= 0;
1112 ret
= tcc_load_dll(s1
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
1116 /* as GNU ld, consider it is an ld script if not recognized */
1117 ret
= tcc_load_ldscript(s1
, fd
);
1120 #endif /* pe / macos / unix */
1124 tcc_error_noabort("%s: unrecognized file type", filename
);
1127 #ifdef TCC_TARGET_COFF
1128 case AFF_BINTYPE_C67
:
1129 ret
= tcc_load_coff(s1
, fd
);
1135 /* update target deps */
1136 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
, tcc_strdup(filename
));
1137 ret
= tcc_compile(s1
, flags
, filename
, fd
);
1139 s1
->current_filename
= NULL
;
1143 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1145 int filetype
= s
->filetype
;
1146 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1147 /* use a file extension to detect a filetype */
1148 const char *ext
= tcc_fileextension(filename
);
1151 if (!strcmp(ext
, "S"))
1152 filetype
= AFF_TYPE_ASMPP
;
1153 else if (!strcmp(ext
, "s"))
1154 filetype
= AFF_TYPE_ASM
;
1155 else if (!PATHCMP(ext
, "c")
1156 || !PATHCMP(ext
, "h")
1157 || !PATHCMP(ext
, "i"))
1158 filetype
= AFF_TYPE_C
;
1160 filetype
|= AFF_TYPE_BIN
;
1162 filetype
= AFF_TYPE_C
;
1165 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1168 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1170 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1174 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1175 const char *filename
, int flags
, char **paths
, int nb_paths
)
1180 for(i
= 0; i
< nb_paths
; i
++) {
1181 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1182 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1188 /* find and load a dll. Return non zero if not found */
1189 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1191 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1192 s
->library_paths
, s
->nb_library_paths
);
1195 /* find [cross-]libtcc1.a and tcc helper objects in library path */
1196 ST_FUNC
void tcc_add_support(TCCState
*s1
, const char *filename
)
1198 #ifdef CONFIG_TCC_CROSSPREFIX
1200 snprintf(buf
, sizeof buf
, "%s%s", CONFIG_TCC_CROSSPREFIX
, filename
);
1203 if (tcc_add_dll(s1
, filename
, 0) < 0)
1204 tcc_error_noabort("%s not found", filename
);
1207 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
1208 ST_FUNC
int tcc_add_crt(TCCState
*s1
, const char *filename
)
1210 if (-1 == tcc_add_library_internal(s1
, "%s/%s",
1211 filename
, 0, s1
->crt_paths
, s1
->nb_crt_paths
))
1212 tcc_error_noabort("file '%s' not found", filename
);
1217 /* the library name is the same as the argument of the '-l' option */
1218 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1220 #if defined TCC_TARGET_PE
1221 static const char * const libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1222 const char * const *pp
= s
->static_link
? libs
+ 4 : libs
;
1223 #elif defined TCC_TARGET_MACHO
1224 static const char * const libs
[] = { "%s/lib%s.dylib", "%s/lib%s.tbd", "%s/lib%s.a", NULL
};
1225 const char * const *pp
= s
->static_link
? libs
+ 2 : libs
;
1226 #elif defined TARGETOS_OpenBSD
1227 static const char * const libs
[] = { "%s/lib%s.so.*", "%s/lib%s.a", NULL
};
1228 const char * const *pp
= s
->static_link
? libs
+ 1 : libs
;
1230 static const char * const libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1231 const char * const *pp
= s
->static_link
? libs
+ 1 : libs
;
1233 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1235 if (0 == tcc_add_library_internal(s
, *pp
,
1236 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1243 PUB_FUNC
int tcc_add_library_err(TCCState
*s1
, const char *libname
)
1245 int ret
= tcc_add_library(s1
, libname
);
1247 tcc_error_noabort("library '%s' not found", libname
);
1251 /* handle #pragma comment(lib,) */
1252 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1255 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1256 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1259 LIBTCCAPI
int tcc_add_symbol(TCCState
*s1
, const char *name
, const void *val
)
1261 #ifdef TCC_TARGET_PE
1262 /* On x86_64 'val' might not be reachable with a 32bit offset.
1263 So it is handled here as if it were in a DLL. */
1264 pe_putimport(s1
, 0, name
, (uintptr_t)val
);
1267 if (s1
->leading_underscore
) {
1269 pstrcpy(buf
+ 1, sizeof(buf
) - 1, name
);
1272 set_global_sym(s1
, name
, NULL
, (addr_t
)(uintptr_t)val
); /* NULL: SHN_ABS */
1277 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1279 tcc_free(s
->tcc_lib_path
);
1280 s
->tcc_lib_path
= tcc_strdup(path
);
1283 /********************************************************/
1284 /* options parser */
1286 static int strstart(const char *val
, const char **str
)
1301 /* Like strstart, but automatically takes into account that ld options can
1303 * - start with double or single dash (e.g. '--soname' or '-soname')
1304 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1305 * or '-Wl,-soname=x.so')
1307 * you provide `val` always in 'option[=]' form (no leading -)
1309 static int link_option(const char *str
, const char *val
, const char **ptr
)
1314 /* there should be 1 or 2 dashes */
1320 /* then str & val should match (potentially up to '=') */
1327 if (strstart("no-", &p
))
1331 while (*q
!= '\0' && *q
!= '=') {
1338 /* '=' near eos means ',' or '=' is ok */
1342 if (*p
!= ',' && *p
!= '=')
1352 static const char *skip_linker_arg(const char **str
)
1354 const char *s1
= *str
;
1355 const char *s2
= strchr(s1
, ',');
1356 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1360 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1366 p
[l
= strlen(p
)] = sep
, ++l
;
1367 skip_linker_arg(&q
);
1368 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1371 /* set linker options */
1372 static int tcc_set_linker(TCCState
*s
, const char *option
)
1377 const char *p
= NULL
;
1382 if (link_option(option
, "Bsymbolic", &p
)) {
1384 } else if (link_option(option
, "nostdlib", &p
)) {
1386 } else if (link_option(option
, "e=", &p
)
1387 || link_option(option
, "entry=", &p
)) {
1388 copy_linker_arg(&s
->elf_entryname
, p
, 0);
1389 } else if (link_option(option
, "fini=", &p
)) {
1390 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1392 } else if (link_option(option
, "image-base=", &p
)
1393 || link_option(option
, "Ttext=", &p
)) {
1394 s
->text_addr
= strtoull(p
, &end
, 16);
1395 s
->has_text_addr
= 1;
1396 } else if (link_option(option
, "init=", &p
)) {
1397 copy_linker_arg(&s
->init_symbol
, p
, 0);
1399 } else if (link_option(option
, "Map=", &p
)) {
1400 copy_linker_arg(&s
->mapfile
, p
, 0);
1402 } else if (link_option(option
, "oformat=", &p
)) {
1403 #if defined(TCC_TARGET_PE)
1404 if (strstart("pe-", &p
)) {
1406 if (strstart("elf64-", &p
)) {
1408 if (strstart("elf32-", &p
)) {
1410 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1411 } else if (!strcmp(p
, "binary")) {
1412 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1413 #ifdef TCC_TARGET_COFF
1414 } else if (!strcmp(p
, "coff")) {
1415 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1420 } else if (link_option(option
, "as-needed", &p
)) {
1422 } else if (link_option(option
, "O", &p
)) {
1424 } else if (link_option(option
, "export-all-symbols", &p
)) {
1426 } else if (link_option(option
, "export-dynamic", &p
)) {
1428 } else if (link_option(option
, "rpath=", &p
)) {
1429 copy_linker_arg(&s
->rpath
, p
, ':');
1430 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1431 s
->enable_new_dtags
= 1;
1432 } else if (link_option(option
, "section-alignment=", &p
)) {
1433 s
->section_align
= strtoul(p
, &end
, 16);
1434 } else if (link_option(option
, "soname=", &p
)) {
1435 copy_linker_arg(&s
->soname
, p
, 0);
1436 #ifdef TCC_TARGET_PE
1437 } else if (link_option(option
, "large-address-aware", &p
)) {
1438 s
->pe_characteristics
|= 0x20;
1439 } else if (link_option(option
, "file-alignment=", &p
)) {
1440 s
->pe_file_align
= strtoul(p
, &end
, 16);
1441 } else if (link_option(option
, "stack=", &p
)) {
1442 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1443 } else if (link_option(option
, "subsystem=", &p
)) {
1444 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1445 if (!strcmp(p
, "native")) {
1446 s
->pe_subsystem
= 1;
1447 } else if (!strcmp(p
, "console")) {
1448 s
->pe_subsystem
= 3;
1449 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1450 s
->pe_subsystem
= 2;
1451 } else if (!strcmp(p
, "posix")) {
1452 s
->pe_subsystem
= 7;
1453 } else if (!strcmp(p
, "efiapp")) {
1454 s
->pe_subsystem
= 10;
1455 } else if (!strcmp(p
, "efiboot")) {
1456 s
->pe_subsystem
= 11;
1457 } else if (!strcmp(p
, "efiruntime")) {
1458 s
->pe_subsystem
= 12;
1459 } else if (!strcmp(p
, "efirom")) {
1460 s
->pe_subsystem
= 13;
1461 #elif defined(TCC_TARGET_ARM)
1462 if (!strcmp(p
, "wince")) {
1463 s
->pe_subsystem
= 9;
1468 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1470 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1472 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1473 } else if (link_option(option
, "z=", &p
)) {
1479 tcc_error("unsupported linker option '%s'", option
);
1482 tcc_warning_c(warn_unsupported
)("unsupported linker option '%s'", option
);
1483 option
= skip_linker_arg(&p
);
1488 typedef struct TCCOption
{
1495 TCC_OPTION_ignored
= 0,
1512 TCC_OPTION_dumpversion
,
1524 TCC_OPTION_mfloat_abi
,
1528 TCC_OPTION_iwithprefix
,
1530 TCC_OPTION_nostdinc
,
1531 TCC_OPTION_nostdlib
,
1532 TCC_OPTION_print_search_dirs
,
1533 TCC_OPTION_rdynamic
,
1548 #define TCC_OPTION_HAS_ARG 0x0001
1549 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1551 static const TCCOption tcc_options
[] = {
1552 { "h", TCC_OPTION_HELP
, 0 },
1553 { "-help", TCC_OPTION_HELP
, 0 },
1554 { "?", TCC_OPTION_HELP
, 0 },
1555 { "hh", TCC_OPTION_HELP2
, 0 },
1556 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1557 { "-version", TCC_OPTION_v
, 0 }, /* handle as verbose, also prints version*/
1558 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1559 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1560 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1561 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1562 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1563 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1564 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1565 { "bench", TCC_OPTION_bench
, 0 },
1566 #ifdef CONFIG_TCC_BACKTRACE
1567 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1569 #ifdef CONFIG_TCC_BCHECK
1570 { "b", TCC_OPTION_b
, 0 },
1572 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1573 { "c", TCC_OPTION_c
, 0 },
1574 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1575 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1576 { "static", TCC_OPTION_static
, 0 },
1577 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1578 { "shared", TCC_OPTION_shared
, 0 },
1579 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1580 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1581 { "pthread", TCC_OPTION_pthread
, 0},
1582 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1583 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1584 { "r", TCC_OPTION_r
, 0 },
1585 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1586 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1587 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1588 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1589 #ifdef TCC_TARGET_ARM
1590 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1592 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1593 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1594 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1595 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1596 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1597 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1598 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1599 { "w", TCC_OPTION_w
, 0 },
1600 { "E", TCC_OPTION_E
, 0},
1601 { "M", TCC_OPTION_M
, 0},
1602 { "MD", TCC_OPTION_MD
, 0},
1603 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1604 { "MM", TCC_OPTION_MM
, 0},
1605 { "MMD", TCC_OPTION_MMD
, 0},
1606 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1607 { "ar", TCC_OPTION_ar
, 0},
1608 #ifdef TCC_TARGET_PE
1609 { "impdef", TCC_OPTION_impdef
, 0},
1611 /* ignored (silently, except after -Wunsupported) */
1612 { "arch", 0, TCC_OPTION_HAS_ARG
},
1614 { "-param", 0, TCC_OPTION_HAS_ARG
},
1615 { "pedantic", 0, 0 },
1618 { "traditional", 0, 0 },
1622 typedef struct FlagDef
{
1628 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1629 #define FD_INVERT 0x0002 /* invert value before storing */
1631 static const FlagDef options_W
[] = {
1632 { offsetof(TCCState
, warn_all
), WD_ALL
, "all" },
1633 { offsetof(TCCState
, warn_error
), 0, "error" },
1634 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1635 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1636 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
, "implicit-function-declaration" },
1637 { offsetof(TCCState
, warn_discarded_qualifiers
), WD_ALL
, "discarded-qualifiers" },
1641 static const FlagDef options_f
[] = {
1642 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1643 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1644 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1645 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1646 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1647 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1648 { offsetof(TCCState
, test_coverage
), 0, "test-coverage" },
1652 static const FlagDef options_m
[] = {
1653 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1654 #ifdef TCC_TARGET_X86_64
1655 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1660 static int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1662 int value
, mask
, ret
;
1667 r
= name
, value
= !strstart("no-", &r
), mask
= 0;
1669 /* when called with options_W, look for -W[no-]error=<option> */
1670 if ((flags
->flags
& WD_ALL
) && strstart("error=", &r
))
1671 value
= value
? WARN_ON
|WARN_ERR
: WARN_NOE
, mask
= WARN_ON
;
1673 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1675 if (strcmp(r
, p
->name
))
1678 if (0 == (p
->flags
& WD_ALL
))
1682 f
= (unsigned char *)s
+ p
->offset
;
1683 *f
= (*f
& mask
) | (value
^ !!(p
->flags
& FD_INVERT
));
1687 if (strcmp(r
, "all"))
1694 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1696 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1698 strcpy(f
->name
, filename
);
1699 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1702 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1707 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1713 while (c
= (unsigned char)*r
, c
) {
1715 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1717 } else if (c
== '"') {
1720 } else if (q
== 0 && c
<= ' ') {
1726 //printf("<%s>\n", str.data), fflush(stdout);
1727 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1734 /* read list file */
1735 static void args_parser_listfile(TCCState
*s
,
1736 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1744 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1746 tcc_error("listfile '%s' not found", filename
);
1748 p
= tcc_load_text(fd
);
1749 for (i
= 0; i
< *pargc
; ++i
)
1751 args_parser_make_argv(p
, &argc
, &argv
);
1753 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1756 dynarray_reset(&s
->argv
, &s
->argc
);
1757 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1760 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1763 const TCCOption
*popt
;
1764 const char *optarg
, *r
;
1765 const char *run
= NULL
;
1767 CString linker_arg
; /* collect -Wl options */
1768 int tool
= 0, arg_start
= 0, noaction
= optind
;
1769 char **argv
= *pargv
;
1772 cstr_new(&linker_arg
);
1774 while (optind
< argc
) {
1776 if (r
[0] == '@' && r
[1] != '\0') {
1777 args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
);
1782 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1787 if (r
[0] != '-' || r
[1] == '\0') {
1788 if (r
[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1789 args_parser_add_file(s
, r
, s
->filetype
);
1791 tcc_set_options(s
, run
);
1792 arg_start
= optind
- 1;
1798 /* find option in table */
1799 for(popt
= tcc_options
; ; ++popt
) {
1800 const char *p1
= popt
->name
;
1801 const char *r1
= r
+ 1;
1803 tcc_error("invalid option -- '%s'", r
);
1804 if (!strstart(p1
, &r1
))
1807 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1808 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1811 tcc_error("argument to '%s' is missing", r
);
1812 optarg
= argv
[optind
++];
1814 } else if (*r1
!= '\0')
1819 switch(popt
->index
) {
1820 case TCC_OPTION_HELP
:
1823 case TCC_OPTION_HELP2
:
1827 tcc_add_include_path(s
, optarg
);
1830 tcc_define_symbol(s
, optarg
, NULL
);
1833 tcc_undefine_symbol(s
, optarg
);
1836 tcc_add_library_path(s
, optarg
);
1839 /* set tcc utilities path (mainly for tcc development) */
1840 tcc_set_lib_path(s
, optarg
);
1843 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1846 case TCC_OPTION_pthread
:
1847 s
->option_pthread
= 1;
1849 case TCC_OPTION_bench
:
1852 #ifdef CONFIG_TCC_BACKTRACE
1854 s
->rt_num_callers
= atoi(optarg
);
1855 s
->do_backtrace
= 1;
1857 s
->dwarf
= DWARF_VERSION
;
1860 #ifdef CONFIG_TCC_BCHECK
1862 s
->do_bounds_check
= 1;
1863 s
->do_backtrace
= 1;
1865 s
->dwarf
= DWARF_VERSION
;
1870 s
->dwarf
= DWARF_VERSION
;
1871 if (strstart("dwarf-", &optarg
))
1872 s
->dwarf
= atoi(optarg
);
1878 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1884 else if (*optarg
== 'M')
1886 else if (*optarg
== 't')
1888 else if (isnum(*optarg
))
1889 s
->g_debug
|= atoi(optarg
);
1891 goto unsupported_option
;
1893 case TCC_OPTION_static
:
1896 case TCC_OPTION_std
:
1897 if (strcmp(optarg
, "=c11") == 0)
1898 s
->cversion
= 201112;
1900 case TCC_OPTION_shared
:
1902 goto set_output_type
;
1903 case TCC_OPTION_soname
:
1904 s
->soname
= tcc_strdup(optarg
);
1908 tcc_warning("multiple -o option");
1909 tcc_free(s
->outfile
);
1911 s
->outfile
= tcc_strdup(optarg
);
1914 /* generate a .o merging several output files */
1917 goto set_output_type
;
1918 case TCC_OPTION_isystem
:
1919 tcc_add_sysinclude_path(s
, optarg
);
1921 case TCC_OPTION_include
:
1922 cstr_printf(&s
->cmdline_incl
, "#include \"%s\"\n", optarg
);
1924 case TCC_OPTION_nostdinc
:
1927 case TCC_OPTION_nostdlib
:
1930 case TCC_OPTION_run
:
1931 #ifndef TCC_IS_NATIVE
1932 tcc_error("-run is not available in a cross compiler");
1935 x
= TCC_OUTPUT_MEMORY
;
1936 goto set_output_type
;
1938 do ++s
->verbose
; while (*optarg
++ == 'v');
1942 if (set_flag(s
, options_f
, optarg
) < 0)
1943 goto unsupported_option
;
1945 #ifdef TCC_TARGET_ARM
1946 case TCC_OPTION_mfloat_abi
:
1947 /* tcc doesn't support soft float yet */
1948 if (!strcmp(optarg
, "softfp")) {
1949 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1950 } else if (!strcmp(optarg
, "hard"))
1951 s
->float_abi
= ARM_HARD_FLOAT
;
1953 tcc_error("unsupported float abi '%s'", optarg
);
1957 if (set_flag(s
, options_m
, optarg
) < 0) {
1958 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
1959 goto unsupported_option
;
1960 if (PTR_SIZE
!= x
/8)
1967 if (optarg
[0] && set_flag(s
, options_W
, optarg
) < 0)
1968 goto unsupported_option
;
1973 case TCC_OPTION_rdynamic
:
1977 if (linker_arg
.size
)
1978 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
1979 cstr_cat(&linker_arg
, optarg
, 0);
1980 if (tcc_set_linker(s
, linker_arg
.data
))
1981 cstr_free(&linker_arg
);
1987 x
= TCC_OUTPUT_PREPROCESS
;
1988 goto set_output_type
;
1990 s
->Pflag
= atoi(optarg
) + 1;
1993 s
->include_sys_deps
= 1;
1997 if(!s
->deps_outfile
)
1998 s
->deps_outfile
= tcc_strdup("-");
2000 case TCC_OPTION_MMD
:
2005 s
->include_sys_deps
= 1;
2008 s
->deps_outfile
= tcc_strdup(optarg
);
2010 case TCC_OPTION_dumpversion
:
2011 printf ("%s\n", TCC_VERSION
);
2018 else if (*optarg
== 'a')
2020 else if (*optarg
== 'b')
2022 else if (*optarg
== 'n')
2025 tcc_warning("unsupported language '%s'", optarg
);
2026 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
2029 s
->optimize
= atoi(optarg
);
2031 case TCC_OPTION_print_search_dirs
:
2034 case TCC_OPTION_impdef
:
2040 arg_start
= optind
- 1;
2041 if (arg_start
!= noaction
)
2042 tcc_error("cannot parse %s here", r
);
2047 tcc_warning_c(warn_unsupported
)("unsupported option '%s'", r
);
2051 if (linker_arg
.size
) {
2052 r
= linker_arg
.data
;
2055 *pargc
= argc
- arg_start
;
2056 *pargv
= argv
+ arg_start
;
2059 if (optind
!= noaction
)
2061 if (s
->verbose
== 2)
2062 return OPT_PRINT_DIRS
;
2068 LIBTCCAPI
void tcc_set_options(TCCState
*s
, const char *r
)
2072 args_parser_make_argv(r
, &argc
, &argv
);
2073 tcc_parse_args(s
, &argc
, &argv
, 0);
2074 dynarray_reset(&argv
, &argc
);
2077 PUB_FUNC
void tcc_print_stats(TCCState
*s1
, unsigned total_time
)
2081 if (total_bytes
< 1)
2083 fprintf(stderr
, "* %d idents, %d lines, %d bytes\n"
2084 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2085 total_idents
, total_lines
, total_bytes
,
2086 (double)total_time
/1000,
2087 (unsigned)total_lines
*1000/total_time
,
2088 (double)total_bytes
/1000/total_time
);
2089 fprintf(stderr
, "* text %d, data.rw %d, data.ro %d, bss %d bytes\n",
2090 s1
->total_output
[0],
2091 s1
->total_output
[1],
2092 s1
->total_output
[2],
2096 fprintf(stderr
, "* %d bytes memory used\n", mem_max_size
);