History

tcc_add_file(): preserve s->filetype

This is supposed to fix a bug where libtcc eventually was trying to compile libtcc1.a as C source code. Anyway, there is now only two functions that refer to s->filetype, tcc_add_file() and tcc_add_library().

grischka committed 8 years ago

Commit ace12251 parent 671dcac5 files · +40 −40Browse files

Unified · Split

5 files
  1. libtcc.c +24 −21
  2. tcc.c +2 −5
  3. tcc.h +6 −7
  4. tccelf.c +7 −4
  5. tccpe.c +1 −3
libtcc.c+24−21
@@ -621,14 +621,13 @@ ST_FUNC int tcc_open(TCCState *s1, const char *filename) · 620 unmodified lines
621621 }
622622
623623 /* compile the file opened in 'file'. Return non zero if errors. */
624−static int tcc_compile(TCCState *s1)
624+static int tcc_compile(TCCState *s1, int filetype)
625625 {
626626 Sym *define_start;
627− int filetype, is_asm;
627+ int is_asm;
628628
629629 define_start = define_stack;
630− filetype = s1->filetype;
631− is_asm = filetype == AFF_TYPE_ASM || filetype == AFF_TYPE_ASMPP;
630+ is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
632631 tccelf_begin_file(s1);
633632
634633 if (setjmp(s1->error_jmp_buf) == 0) {
@@ -640,7 +639,7 @@ static int tcc_compile(TCCState *s1) · 5 unmodified lines
640639 tcc_preprocess(s1);
641640 } else if (is_asm) {
642641 #ifdef CONFIG_TCC_ASM
643− tcc_assemble(s1, filetype == AFF_TYPE_ASMPP);
642+ tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
644643 #else
645644 tcc_error_noabort("asm not supported");
646645 #endif
@@ -667,7 +666,7 @@ LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str) · 20 unmodified lines
667666 len = strlen(str);
668667 tcc_open_bf(s, "<string>", len);
669668 memcpy(file->buffer, str, len);
670− ret = tcc_compile(s);
669+ ret = tcc_compile(s, s->filetype);
671670 tcc_close();
672671 return ret;
673672 }
@@ -733,7 +732,6 @@ LIBTCCAPI TCCState *tcc_new(void) · 59 unmodified lines
733732 tcc_state = s;
734733 ++nb_states;
735734
736− s->alacarte_link = 1;
737735 s->nocommon = 1;
738736 s->warn_implicit_function_declaration = 1;
739737 s->ms_extensions = 1;
@@ -1042,7 +1040,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 302 unmodified lines
10421040 break;
10431041 #endif
10441042 case AFF_BINTYPE_AR:
1045− ret = tcc_load_archive(s1, fd);
1043+ ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
10461044 break;
10471045 #ifdef TCC_TARGET_COFF
10481046 case AFF_BINTYPE_C67:
@@ -1061,7 +1059,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 12 unmodified lines
10611059 break;
10621060 }
10631061 } else {
1064− ret = tcc_compile(s1);
1062+ ret = tcc_compile(s1, flags);
10651063 }
10661064 tcc_close();
10671065 return ret;
@@ -1070,8 +1068,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 2 unmodified lines
10701068 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
10711069 {
10721070 int filetype = s->filetype;
1073− int flags = AFF_PRINT_ERROR;
1074− if (filetype == 0) {
1071+ if (0 == (filetype & AFF_TYPE_MASK)) {
10751072 /* use a file extension to detect a filetype */
10761073 const char *ext = tcc_fileextension(filename);
10771074 if (ext[0]) {
@@ -1083,13 +1080,12 @@ LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename) · 5 unmodified lines
10831080 else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
10841081 filetype = AFF_TYPE_C;
10851082 else
1086− flags |= AFF_TYPE_BIN;
1083+ filetype |= AFF_TYPE_BIN;
10871084 } else {
10881085 filetype = AFF_TYPE_C;
10891086 }
1090− s->filetype = filetype;
10911087 }
1092− return tcc_add_file_internal(s, filename, flags);
1088+ return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
10931089 }
10941090
10951091 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
@@ -1141,9 +1137,10 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) · 45 unmodified lines
11411137 const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
11421138 const char **pp = s->static_link ? libs + 1 : libs;
11431139 #endif
1140+ int flags = s->filetype & AFF_WHOLE_ARCHIVE;
11441141 while (*pp) {
11451142 if (0 == tcc_add_library_internal(s, *pp,
1146− libraryname, 0, s->library_paths, s->nb_library_paths))
1143+ libraryname, flags, s->library_paths, s->nb_library_paths))
11471144 return 0;
11481145 ++pp;
11491146 }
@@ -1409,7 +1406,10 @@ static int tcc_set_linker(TCCState *s, const char *option) · 259 unmodified lines
14091406 goto err;
14101407 #endif
14111408 } else if (ret = link_option(option, "?whole-archive", &p), ret) {
1412− s->alacarte_link = ret < 0;
1409+ if (ret > 0)
1410+ s->filetype |= AFF_WHOLE_ARCHIVE;
1411+ else
1412+ s->filetype &= ~AFF_WHOLE_ARCHIVE;
14131413 } else if (p) {
14141414 return 0;
14151415 } else {
@@ -1595,7 +1595,6 @@ static void args_parser_add_file(TCCState *s, const char* filename, int filetype · 179 unmodified lines
15951595 {
15961596 struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
15971597 f->type = filetype;
1598− f->alacarte = s->alacarte_link;
15991598 strcpy(f->name, filename);
16001599 dynarray_add(&s->files, &s->nb_files, f);
16011600 }
@@ -1742,7 +1741,7 @@ reparse: · 140 unmodified lines
17421741 tcc_set_lib_path(s, optarg);
17431742 break;
17441743 case TCC_OPTION_l:
1745− args_parser_add_file(s, optarg, AFF_TYPE_LIB);
1744+ args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
17461745 s->nb_libraries++;
17471746 break;
17481747 case TCC_OPTION_pthread:
@@ -1896,14 +1895,18 @@ reparse: · 147 unmodified lines
18961895 exit(0);
18971896 break;
18981897 case TCC_OPTION_x:
1898+ x = 0;
18991899 if (*optarg == 'c')
1900− s->filetype = AFF_TYPE_C;
1900+ x = AFF_TYPE_C;
19011901 else if (*optarg == 'a')
1902− s->filetype = AFF_TYPE_ASMPP;
1902+ x = AFF_TYPE_ASMPP;
1903+ else if (*optarg == 'b')
1904+ x = AFF_TYPE_BIN;
19031905 else if (*optarg == 'n')
1904− s->filetype = AFF_TYPE_NONE;
1906+ x = AFF_TYPE_NONE;
19051907 else
19061908 tcc_warning("unsupported language '%s'", optarg);
1909+ s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
19071910 break;
19081911 case TCC_OPTION_O:
19091912 last_o = atoi(optarg);
tcc.c+2−5
@@ -62,7 +62,7 @@ static const char help[] = · 61 unmodified lines
6262 " -bt N show N callers in stack traces\n"
6363 #endif
6464 "Misc. options:\n"
65− " -x[c|a|n] specify type of the next infile\n"
65+ " -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)\n"
6666 " -nostdinc do not use standard system include paths\n"
6767 " -nostdlib do not link with standard crt and libraries\n"
6868 " -Bdir set tcc's private include/library dir\n"
@@ -322,8 +322,7 @@ redo: · 253 unmodified lines
322322 for (first_file = NULL, ret = 0;;) {
323323 struct filespec *f = s->files[s->nb_files - n];
324324 s->filetype = f->type;
325− s->alacarte_link = f->alacarte;
326− if (f->type == AFF_TYPE_LIB) {
325+ if (f->type & AFF_TYPE_LIB) {
327326 if (tcc_add_library_err(s, f->name) < 0)
328327 ret = 1;
329328 } else {
@@ -334,8 +333,6 @@ redo: · 4 unmodified lines
334333 if (tcc_add_file(s, f->name) < 0)
335334 ret = 1;
336335 }
337− s->filetype = 0;
338− s->alacarte_link = 1;
339336 if (--n == 0 || ret
340337 || (s->output_type == TCC_OUTPUT_OBJ && !s->option_r))
341338 break;
tcc.h+6−7
@@ -638,7 +638,6 @@ struct sym_attr { · 637 unmodified lines
638638 };
639639
640640 struct TCCState {
641−
642641 int verbose; /* if true, display some information during compilation */
643642 int nostdinc; /* if true, no standard headers are added */
644643 int nostdlib; /* if true, no standard libraries are added */
@@ -646,7 +645,7 @@ struct TCCState { · 1 unmodified line
646645 int static_link; /* if true, static linking is performed */
647646 int rdynamic; /* if true, all symbols are exported */
648647 int symbolic; /* if true, resolve symbols in the current module first */
649− int alacarte_link; /* if true, only link in referenced objects from archive */
648+ int filetype; /* file type for compilation (NONE,C,ASM) */
650649
651650 char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
652651 char *soname; /* as specified on the command line (-soname) */
@@ -811,7 +810,6 @@ struct TCCState { · 158 unmodified lines
811810 struct filespec **files; /* files seen on command line */
812811 int nb_files; /* number thereof */
813812 int nb_libraries; /* number of libs thereof */
814− int filetype;
815813 char *outfile; /* output filename */
816814 int option_r; /* option -r */
817815 int do_bench; /* option -bench */
@@ -824,7 +822,6 @@ struct TCCState { · 6 unmodified lines
824822
825823 struct filespec {
826824 char type;
827− char alacarte;
828825 char name[1];
829826 };
830827
@@ -1150,12 +1147,14 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 319 unmodified lines
11501147 #define AFF_PRINT_ERROR 0x10 /* print error if file not found */
11511148 #define AFF_REFERENCED_DLL 0x20 /* load a referenced dll from another dll */
11521149 #define AFF_TYPE_BIN 0x40 /* file to add is binary */
1150+#define AFF_WHOLE_ARCHIVE 0x80 /* load all objects from archive */
11531151 /* s->filetype: */
11541152 #define AFF_TYPE_NONE 0
11551153 #define AFF_TYPE_C 1
11561154 #define AFF_TYPE_ASM 2
1157−#define AFF_TYPE_ASMPP 3
1158−#define AFF_TYPE_LIB 4
1155+#define AFF_TYPE_ASMPP 4
1156+#define AFF_TYPE_LIB 8
1157+#define AFF_TYPE_MASK (15 | AFF_TYPE_BIN)
11591158 /* values from tcc_object_type(...) */
11601159 #define AFF_BINTYPE_REL 1
11611160 #define AFF_BINTYPE_DYN 2
@@ -1423,7 +1422,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s); · 261 unmodified lines
14231422
14241423 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h);
14251424 ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
1426−ST_FUNC int tcc_load_archive(TCCState *s1, int fd);
1425+ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte);
14271426 ST_FUNC void tcc_add_bcheck(TCCState *s1);
14281427 ST_FUNC void tcc_add_runtime(TCCState *s1);
14291428
tccelf.c+7−4
@@ -1146,7 +1146,6 @@ static void add_init_array_defines(TCCState *s1, const char *section_name) · 1145 unmodified lines
11461146 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
11471147 s->sh_num, sym_end);
11481148 }
1149−#endif
11501149
11511150 static int tcc_add_support(TCCState *s1, const char *filename)
11521151 {
@@ -1154,6 +1153,7 @@ static int tcc_add_support(TCCState *s1, const char *filename) · 1 unmodified line
11541153 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename);
11551154 return tcc_add_file(s1, buf);
11561155 }
1156+#endif
11571157
11581158 ST_FUNC void tcc_add_bcheck(TCCState *s1)
11591159 {
@@ -1189,8 +1189,10 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1) · 29 unmodified lines
11891189 /* add tcc runtime libraries */
11901190 ST_FUNC void tcc_add_runtime(TCCState *s1)
11911191 {
1192+ s1->filetype = 0;
11921193 tcc_add_bcheck(s1);
11931194 tcc_add_pragma_libs(s1);
1195+#ifndef TCC_TARGET_PE
11941196 /* add libc */
11951197 if (!s1->nostdlib) {
11961198 tcc_add_library_err(s1, "c");
@@ -1207,6 +1209,7 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) · 10 unmodified lines
12071209 if (s1->output_type != TCC_OUTPUT_MEMORY)
12081210 tcc_add_crt(s1, "crtn.o");
12091211 }
1212+#endif
12101213 }
12111214
12121215 /* add various standard linker symbols (must be done after the
@@ -2628,7 +2631,7 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize) · 1415 unmodified lines
26282631 }
26292632
26302633 /* load a '.a' file */
2631−ST_FUNC int tcc_load_archive(TCCState *s1, int fd)
2634+ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte)
26322635 {
26332636 ArchiveHeader hdr;
26342637 char ar_size[11];
@@ -2662,10 +2665,10 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) · 27 unmodified lines
26622665 size = (size + 1) & ~1;
26632666 if (!strcmp(ar_name, "/")) {
26642667 /* coff symbol table : we handle it */
2665− if(s1->alacarte_link)
2668+ if (alacarte)
26662669 return tcc_load_alacarte(s1, fd, size, 4);
26672670 } else if (!strcmp(ar_name, "/SYM64/")) {
2668− if(s1->alacarte_link)
2671+ if (alacarte)
26692672 return tcc_load_alacarte(s1, fd, size, 8);
26702673 } else {
26712674 ElfW(Ehdr) ehdr;
tccpe.c+1−3
@@ -1866,8 +1866,6 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) · 1865 unmodified lines
18661866 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
18671867 SHN_UNDEF, start_symbol);
18681868
1869− tcc_add_pragma_libs(s1);
1870−
18711869 if (0 == s1->nostdlib) {
18721870 static const char *libs[] = {
18731871 TCC_LIBTCC1, "msvcrt", "kernel32", "", "user32", "gdi32", NULL
@@ -1948,7 +1946,7 @@ ST_FUNC int pe_output_file(TCCState *s1, const char *filename) · 74 unmodified lines
19481946 pe.filename = filename;
19491947 pe.s1 = s1;
19501948
1951− tcc_add_bcheck(s1);
1949+ tcc_add_runtime(s1);
19521950 pe_add_runtime(s1, &pe);
19531951 resolve_common_syms(s1);
19541952 pe_set_options(s1, &pe);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t