History

stuff & fixes

libtcc.c: - revert "Small patch to allow..." (someone's personal easteregg) (see da3a763e977a84edf43c5ca101deae768ae8480d) - check return value from macho_load_tbd/dylib tcc.c: - remove help for "not yet implemented" option tccelf.c: - check PIE's for "unresolved symbols" tccgen.c: - avoid int->double->int cast (see a46372e910710b9b00d4f4122ef6c1b00b2b909c) - fix constant propagation with pseudo long doubles (must mask out VT_LONG from type) - cleanup find_field() (again) tccpp.c: - disallow strings and double constants in #if expressions win32/include/uchar.h: - change file mode

grischka committed 3 years ago

Commit cd75ca61 parent 3f3cbb58 files · +48 −94Browse files

Unified · Split

8 files
  1. libtcc.c +1 −39
  2. tcc.c +1 −2
  3. tcc.h +1 −2
  4. tccelf.c +8 −6
  5. tccgen.c +30 −40
  6. tccpp.c +5 −2
  7. tests/libtcc_test_mt.c +2 −3
  8. win32/include/uchar.h +0 −0
libtcc.c+1−39
@@ -99,50 +99,12 @@ BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) · 98 unmodified lines
9999 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
100100 static inline char *config_tccdir_w32(char *path)
101101 {
102− char temp[1024];
103− char try[1024];
104102 char *p;
105− int c;
106103 GetModuleFileName(tcc_module, path, MAX_PATH);
107104 p = tcc_basename(normalize_slashes(strlwr(path)));
108105 if (p > path)
109106 --p;
110−
111107 *p = 0;
112−
113− /*
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.
118− */
119− strncpy(temp, path, sizeof(temp)-1);
120− strcat(temp, "/include");
121−
122− if (_access(temp, 0) != 0) {
123− /* No 'include' folder found, so go up one level. */
124− strncpy(temp, path, sizeof(temp)-1);
125−
126− /* Try this for several "levels" up. */
127− for (c = 0; c < 4; c++) {
128− p = tcc_basename(temp);
129− if (p > temp) {
130− --p;
131− *p = '\0';
132− }
133−
134− strncpy(try, temp, sizeof(try)-1);
135− strcat(try, "/include");
136−
137− if (_access(try, 0) == 0) {
138− if (p != NULL)
139− p = '\0';
140− strcpy(path, temp);
141− break;
142− }
143− }
144− }
145−
146108 return path;
147109 }
148110 #define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))
@@ -1174,7 +1136,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 1025 unmodified lines
11741136 } else {
11751137 ret = macho_load_tbd(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0);
11761138 }
1177− break;
1139+ goto check_success;
11781140 default:
11791141 {
11801142 const char *ext = tcc_fileextension(filename);
tcc.c+1−2
@@ -48,7 +48,6 @@ static const char help[] = · 47 unmodified lines
4848 " -Dsym[=val] define 'sym' with value 'val'\n"
4949 " -Usym undefine 'sym'\n"
5050 " -E preprocess only\n"
51− " -C keep comments (not yet implemented)\n"
5251 "Linker options:\n"
5352 " -Ldir add library path 'dir'\n"
5453 " -llib link with dynamic or static library 'lib'\n"
@@ -303,7 +302,7 @@ redo: · 248 unmodified lines
303302 if (opt == OPT_M32 || opt == OPT_M64)
304303 return tcc_tool_cross(s, argv, opt);
305304 if (s->verbose)
306− printf(version);
305+ printf("%s", version);
307306 if (opt == OPT_AR)
308307 return tcc_tool_ar(s, argc, argv);
309308 #ifdef TCC_TARGET_PE
tcc.h+1−2
@@ -284,7 +284,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 283 unmodified lines
284284
285285 /* library search paths */
286286 #ifndef CONFIG_TCC_LIBPATHS
287−# ifdef TCC_TARGET_PE
287+# if defined TCC_TARGET_PE || defined _WIN32
288288 # define CONFIG_TCC_LIBPATHS "{B}/lib"
289289 # else
290290 # define CONFIG_TCC_LIBPATHS \
@@ -496,7 +496,6 @@ typedef struct CString { · 205 unmodified lines
496496 int size; /* size in bytes */
497497 int size_allocated;
498498 void *data; /* either 'char *' or 'nwchar_t *' */
499− struct CString *prev;
500499 } CString;
501500
502501 /* type definition */
tccelf.c+8−6
@@ -1880,7 +1880,7 @@ static void fill_local_got_entries(TCCState *s1) · 1879 unmodified lines
18801880
18811881 /* Bind symbols of executable: resolve undefined symbols from exported symbols
18821882 in shared libraries */
1883−static void bind_exe_dynsyms(TCCState *s1)
1883+static void bind_exe_dynsyms(TCCState *s1, int is_PIE)
18841884 {
18851885 const char *name;
18861886 int sym_index, index;
@@ -1895,6 +1895,8 @@ static void bind_exe_dynsyms(TCCState *s1) · 8 unmodified lines
18951895 name = (char *) symtab_section->link->data + sym->st_name;
18961896 sym_index = find_elf_sym(s1->dynsymtab_section, name);
18971897 if (sym_index) {
1898+ if (is_PIE)
1899+ continue;
18981900 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
18991901 type = ELFW(ST_TYPE)(esym->st_info);
19001902 if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
@@ -1970,9 +1972,9 @@ static void bind_libs_dynsyms(TCCState *s1) · 69 unmodified lines
19701972 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
19711973 name = (char *)symtab_section->link->data + sym->st_name;
19721974 dynsym_index = find_elf_sym(s1->dynsymtab_section, name);
1973− if (sym->st_shndx != SHN_UNDEF
1974− && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1975− if (dynsym_index || s1->rdynamic)
1975+ if (sym->st_shndx != SHN_UNDEF) {
1976+ if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL
1977+ && (dynsym_index || s1->rdynamic))
19761978 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
19771979 sym->st_info, 0, sym->st_shndx, name);
19781980 } else if (dynsym_index) {
@@ -2797,8 +2799,8 @@ static int elf_output_file(TCCState *s1, const char *filename) · 818 unmodified lines
27972799 dynamic->sh_entsize = sizeof(ElfW(Dyn));
27982800
27992801 got_sym = build_got(s1);
2800− if (file_type == TCC_OUTPUT_EXE) {
2801− bind_exe_dynsyms(s1);
2802+ if (file_type & TCC_OUTPUT_EXE) {
2803+ bind_exe_dynsyms(s1, file_type & TCC_OUTPUT_DYN);
28022804 if (s1->nb_errors)
28032805 goto the_end;
28042806 }
tccgen.c+30−40
@@ -2474,7 +2474,7 @@ void gen_negf(int op) · 2473 unmodified lines
24742474 /* generate a floating point operation with constant propagation */
24752475 static void gen_opif(int op)
24762476 {
2477− int c1, c2, cast_int = 0;
2477+ int c1, c2, i, bt;
24782478 SValue *v1, *v2;
24792479 #if defined _MSC_VER && defined __x86_64__
24802480 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
@@ -2486,15 +2486,16 @@ static void gen_opif(int op) · 5 unmodified lines
24862486 v2 = vtop;
24872487 if (op == TOK_NEG)
24882488 v1 = v2;
2489+ bt = v1->type.t & VT_BTYPE;
24892490
24902491 /* currently, we cannot do computations with forward symbols */
24912492 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
24922493 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
24932494 if (c1 && c2) {
2494− if (v1->type.t == VT_FLOAT) {
2495+ if (bt == VT_FLOAT) {
24952496 f1 = v1->c.f;
24962497 f2 = v2->c.f;
2497− } else if (v1->type.t == VT_DOUBLE) {
2498+ } else if (bt == VT_DOUBLE) {
24982499 f1 = v1->c.d;
24992500 f2 = v2->c.d;
25002501 } else {
@@ -2533,41 +2534,39 @@ static void gen_opif(int op) · 32 unmodified lines
25332534 f1 = -f1;
25342535 goto unary_result;
25352536 case TOK_EQ:
2536− f1 = f1 == f2;
2537+ i = f1 == f2;
25372538 make_int:
2538− cast_int = 1;
2539− break;
2539+ vtop -= 2;
2540+ vpushi(i);
2541+ return;
25402542 case TOK_NE:
2541− f1 = f1 != f2;
2543+ i = f1 != f2;
25422544 goto make_int;
25432545 case TOK_LT:
2544− f1 = f1 < f2;
2546+ i = f1 < f2;
25452547 goto make_int;
25462548 case TOK_GE:
2547− f1 = f1 >= f2;
2549+ i = f1 >= f2;
25482550 goto make_int;
25492551 case TOK_LE:
2550− f1 = f1 <= f2;
2552+ i = f1 <= f2;
25512553 goto make_int;
25522554 case TOK_GT:
2553− f1 = f1 > f2;
2555+ i = f1 > f2;
25542556 goto make_int;
2555− /* XXX: also handles tests ? */
25562557 default:
25572558 goto general_case;
25582559 }
25592560 vtop--;
25602561 unary_result:
25612562 /* XXX: overflow test ? */
2562− if (v1->type.t == VT_FLOAT) {
2563+ if (bt == VT_FLOAT) {
25632564 v1->c.f = f1;
2564− } else if (v1->type.t == VT_DOUBLE) {
2565+ } else if (bt == VT_DOUBLE) {
25652566 v1->c.d = f1;
25662567 } else {
25672568 v1->c.ld = f1;
25682569 }
2569− if (cast_int)
2570− gen_cast_s(VT_INT);
25712570 } else {
25722571 general_case:
25732572 if (op == TOK_NEG) {
@@ -4029,10 +4028,18 @@ static Sym * find_field (CType *type, int v, int *cumofs) · 1455 unmodified lines
40294028 {
40304029 Sym *s = type->ref;
40314030 int v1 = v | SYM_FIELD;
4032−
4031+ if (!(v & SYM_FIELD)) { /* top-level call */
4032+ if ((type->t & VT_BTYPE) != VT_STRUCT)
4033+ expect("struct or union");
4034+ if (v < TOK_UIDENT)
4035+ expect("field name");
4036+ if (s->c < 0)
4037+ tcc_error("dereferencing incomplete type '%s'",
4038+ get_tok_str(s->v & ~SYM_STRUCT, 0));
4039+ }
40334040 while ((s = s->next) != NULL) {
40344041 if (s->v == v1) {
4035− *cumofs += s->c;
4042+ *cumofs = s->c;
40364043 return s;
40374044 }
40384045 if ((s->type.t & VT_BTYPE) == VT_STRUCT
@@ -4045,17 +4052,9 @@ static Sym * find_field (CType *type, int v, int *cumofs) · 6 unmodified lines
40454052 }
40464053 }
40474054 }
4048−
4049− if (!(v & SYM_FIELD)) { /* top-level call */
4050− s = type->ref;
4051− if (s->c < 0)
4052− tcc_error("dereferencing incomplete type '%s'",
4053− get_tok_str(s->v & ~SYM_STRUCT, 0));
4054− else
4055− tcc_error("field not found: %s",
4056− get_tok_str(v, &tokc));
4057− }
4058− return NULL;
4055+ if (!(v & SYM_FIELD))
4056+ tcc_error("field not found: %s", get_tok_str(v, NULL));
4057+ return s;
40594058 }
40604059
40614060 static void check_fields (CType *type, int check)
@@ -6003,21 +6002,15 @@ special_math_val: · 1941 unmodified lines
60036002 if (tok == TOK_INC || tok == TOK_DEC) {
60046003 inc(1, tok);
60056004 next();
6006− } else if (tok == '.' || tok == TOK_ARROW || tok == TOK_CDOUBLE) {
6007− int qualifiers, cumofs = 0;
6005+ } else if (tok == '.' || tok == TOK_ARROW) {
6006+ int qualifiers, cumofs;
60086007 /* field */
60096008 if (tok == TOK_ARROW)
60106009 indir();
60116010 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
60126011 test_lvalue();
60136012 /* expect pointer on structure */
6014− if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
6015− expect("struct or union");
6016− if (tok == TOK_CDOUBLE)
6017− expect("field name");
60186013 next();
6019− if (tok == TOK_CINT || tok == TOK_CUINT)
6020− expect("field name");
60216014 s = find_field(&vtop->type, tok, &cumofs);
60226015 /* add field offset to pointer */
60236016 gaddrof();
@@ -7494,9 +7487,6 @@ static int decl_designator(init_params *p, CType *type, unsigned long c, · 1470 unmodified lines
74947487 l = tok;
74957488 struct_field:
74967489 next();
7497− if ((type->t & VT_BTYPE) != VT_STRUCT)
7498− expect("struct/union type");
7499− cumofs = 0;
75007490 f = find_field(type, l, &cumofs);
75017491 if (cur_field)
75027492 *cur_field = f;
tccpp.c+5−2
@@ -1457,7 +1457,10 @@ static int expr_preprocess(TCCState *s1) · 1456 unmodified lines
14571457 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
14581458 next(); /* do macro subst */
14591459 redo:
1460− if (tok == TOK_DEFINED) {
1460+ if (tok < TOK_IDENT) {
1461+ if (tok >= TOK_STR && tok <= TOK_CLDOUBLE)
1462+ tcc_error("invalid constant in preprocessor expression");
1463+ } else if (tok == TOK_DEFINED) {
14611464 next_nomacro();
14621465 t = tok;
14631466 if (t == '(')
@@ -1489,7 +1492,7 @@ static int expr_preprocess(TCCState *s1) · 25 unmodified lines
14891492 expect("')'");
14901493 tok = TOK_CINT;
14911494 tokc.i = c;
1492− } else if (tok >= TOK_IDENT) {
1495+ } else {
14931496 /* if undefined macro, replace with zero, check for func-like */
14941497 t = tok;
14951498 tok = TOK_CINT;
tests/libtcc_test_mt.c+2−3
@@ -209,9 +209,8 @@ TF_TYPE(thread_test_complex, vn) · 208 unmodified lines
209209 sleep_ms(2);
210210 ret = tcc_add_file(s, argv[0]);
211211 sleep_ms(3);
212− if (ret < 0)
213− exit(1);
214− tcc_run(s, argc, argv);
212+ if (ret == 0)
213+ tcc_run(s, argc, argv);
215214 tcc_delete(s);
216215 fflush(stdout);
217216 return 0;
win32/include/uchar.h+0−0

File mode changed from 100755 to 100644.

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