- tccgen.c: cleanup switch data etc. after errors (*) - tccpe.c: faster get_dllexports (*) - tccpe.c: support -Wl,-e[ntry]=... (*) - libtcc.c: win32: use ANSI functions (GetModuleFileNameA etc.) - tccrun.c: be nice to tcc-0.9.26 ("struct/enum already defined") - tccpp.c: be nice to tcc-0.9.27's va_start/end macros (*) suggested by Robert Schlicht https://lists.gnu.org/archive/html/tinycc-devel/2024-03/msg00012.html
Unified · Split
| @@ -101,7 +101,7 @@ BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) · 100 unmodified lines | ||
| 101 | 101 | static inline char *config_tccdir_w32(char *path) |
| 102 | 102 | { |
| 103 | 103 | char *p; |
| 104 | − GetModuleFileName(tcc_module, path, MAX_PATH); | |
| 104 | + GetModuleFileNameA(tcc_module, path, MAX_PATH); | |
| 105 | 105 | p = tcc_basename(normalize_slashes(strlwr(path))); |
| 106 | 106 | if (p > path) |
| 107 | 107 | --p; |
| @@ -115,7 +115,7 @@ static inline char *config_tccdir_w32(char *path) · 7 unmodified lines | ||
| 115 | 115 | static void tcc_add_systemdir(TCCState *s) |
| 116 | 116 | { |
| 117 | 117 | char buf[1000]; |
| 118 | − GetSystemDirectory(buf, sizeof buf); | |
| 118 | + GetSystemDirectoryA(buf, sizeof buf); | |
| 119 | 119 | tcc_add_library_path(s, normalize_slashes(buf)); |
| 120 | 120 | } |
| 121 | 121 | #endif |
| @@ -801,14 +801,11 @@ LIBTCCAPI TCCState *tcc_new(void) · 679 unmodified lines | ||
| 801 | 801 | TCCState *s; |
| 802 | 802 | |
| 803 | 803 | s = tcc_mallocz(sizeof(TCCState)); |
| 804 | − if (!s) | |
| 805 | − return NULL; | |
| 806 | 804 | #ifdef MEM_DEBUG |
| 807 | 805 | tcc_memcheck(1); |
| 808 | 806 | #endif |
| 809 | 807 | |
| 810 | 808 | #undef gnu_ext |
| 811 | − | |
| 812 | 809 | s->gnu_ext = 1; |
| 813 | 810 | s->tcc_ext = 1; |
| 814 | 811 | s->nocommon = 1; |
| @@ -874,11 +871,13 @@ LIBTCCAPI void tcc_delete(TCCState *s1) · 59 unmodified lines | ||
| 874 | 871 | cstr_free(&s1->cmdline_defs); |
| 875 | 872 | cstr_free(&s1->cmdline_incl); |
| 876 | 873 | cstr_free(&s1->linker_arg); |
| 874 | + tcc_free(s1->dState); | |
| 877 | 875 | #ifdef TCC_IS_NATIVE |
| 878 | 876 | /* free runtime memory */ |
| 879 | 877 | tcc_run_free(s1); |
| 880 | 878 | #endif |
| 881 | − tcc_free(s1->dState); | |
| 879 | + /* free loaded dlls array */ | |
| 880 | + dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls); | |
| 882 | 881 | tcc_free(s1); |
| 883 | 882 | #ifdef MEM_DEBUG |
| 884 | 883 | tcc_memcheck(-1); |
| @@ -157,6 +157,7 @@ static void gv_dup(void); · 156 unmodified lines | ||
| 157 | 157 | static int get_temp_local_var(int size,int align); |
| 158 | 158 | static void clear_temp_local_var_list(); |
| 159 | 159 | static void cast_error(CType *st, CType *dt); |
| 160 | +static void end_switch(void); | |
| 160 | 161 | |
| 161 | 162 | /* ------------------------------------------------------------------------- */ |
| 162 | 163 | /* Automagical code suppression */ |
| @@ -381,12 +382,10 @@ ST_FUNC void tccgen_init(TCCState *s1) · 218 unmodified lines | ||
| 381 | 382 | |
| 382 | 383 | ST_FUNC int tccgen_compile(TCCState *s1) |
| 383 | 384 | { |
| 384 | − cur_text_section = NULL; | |
| 385 | 385 | funcname = ""; |
| 386 | 386 | func_ind = -1; |
| 387 | 387 | anon_sym = SYM_FIRST_ANOM; |
| 388 | 388 | nocode_wanted = DATA_ONLY_WANTED; /* no code outside of functions */ |
| 389 | − local_scope = 0; | |
| 390 | 389 | debug_modes = (s1->do_debug ? 1 : 0) | s1->test_coverage << 1; |
| 391 | 390 | |
| 392 | 391 | tcc_debug_start(s1); |
| @@ -418,10 +417,19 @@ ST_FUNC void tccgen_finish(TCCState *s1) · 25 unmodified lines | ||
| 418 | 417 | free_defines(NULL); |
| 419 | 418 | /* free sym_pools */ |
| 420 | 419 | dynarray_reset(&sym_pools, &nb_sym_pools); |
| 421 | − sym_free_first = NULL; | |
| 422 | − global_label_stack = local_label_stack = NULL; | |
| 423 | 420 | cstr_free(&initstr); |
| 424 | 421 | dynarray_reset(&stk_data, &nb_stk_data); |
| 422 | + while (cur_switch) | |
| 423 | + end_switch(); | |
| 424 | + local_scope = 0; | |
| 425 | + loop_scope = NULL; | |
| 426 | + all_cleanups = NULL; | |
| 427 | + pending_gotos = NULL; | |
| 428 | + nb_temp_local_vars = 0; | |
| 429 | + global_label_stack = NULL; | |
| 430 | + local_label_stack = NULL; | |
| 431 | + cur_text_section = NULL; | |
| 432 | + sym_free_first = NULL; | |
| 425 | 433 | } |
| 426 | 434 | |
| 427 | 435 | /* ------------------------------------------------------------------------- */ |
| @@ -6754,6 +6762,14 @@ static void gcase(struct case_t **base, int len, int *bsym) · 6326 unmodified lines | ||
| 6754 | 6762 | *bsym = gjmp(*bsym); |
| 6755 | 6763 | } |
| 6756 | 6764 | |
| 6765 | +static void end_switch(void) | |
| 6766 | +{ | |
| 6767 | + struct switch_t *sw = cur_switch; | |
| 6768 | + dynarray_reset(&sw->p, &sw->n); | |
| 6769 | + cur_switch = sw->prev; | |
| 6770 | + tcc_free(sw); | |
| 6771 | +} | |
| 6772 | + | |
| 6757 | 6773 | /* ------------------------------------------------------------------------- */ |
| 6758 | 6774 | /* __attribute__((cleanup(fn))) */ |
| 6759 | 6775 | |
| @@ -7154,15 +7170,14 @@ again: · 394 unmodified lines | ||
| 7154 | 7170 | skip_switch: |
| 7155 | 7171 | /* break label */ |
| 7156 | 7172 | gsym(a); |
| 7157 | − | |
| 7158 | − dynarray_reset(&sw->p, &sw->n); | |
| 7159 | − cur_switch = sw->prev; | |
| 7160 | − tcc_free(sw); | |
| 7173 | + end_switch(); | |
| 7161 | 7174 | |
| 7162 | 7175 | } else if (t == TOK_CASE) { |
| 7163 | − struct case_t *cr = tcc_malloc(sizeof(struct case_t)); | |
| 7176 | + struct case_t *cr; | |
| 7164 | 7177 | if (!cur_switch) |
| 7165 | 7178 | expect("switch"); |
| 7179 | + cr = tcc_malloc(sizeof(struct case_t)); | |
| 7180 | + dynarray_add(&cur_switch->p, &cur_switch->n, cr); | |
| 7166 | 7181 | cr->v1 = cr->v2 = expr_const64(); |
| 7167 | 7182 | if (gnu_ext && tok == TOK_DOTS) { |
| 7168 | 7183 | next(); |
| @@ -7174,7 +7189,6 @@ again: · 5 unmodified lines | ||
| 7174 | 7189 | /* case and default are unreachable from a switch under nocode_wanted */ |
| 7175 | 7190 | if (!cur_switch->nocode_wanted) |
| 7176 | 7191 | cr->sym = gind(); |
| 7177 | − dynarray_add(&cur_switch->p, &cur_switch->n, cr); | |
| 7178 | 7192 | skip(':'); |
| 7179 | 7193 | goto block_after_label; |
| 7180 | 7194 | |
| @@ -8574,9 +8588,9 @@ static int decl(int l) · 1393 unmodified lines | ||
| 8574 | 8588 | fn = tcc_malloc(sizeof *fn + strlen(file->filename)); |
| 8575 | 8589 | strcpy(fn->filename, file->filename); |
| 8576 | 8590 | fn->sym = sym; |
| 8577 | − skip_or_save_block(&fn->func_str); | |
| 8578 | 8591 | dynarray_add(&tcc_state->inline_fns, |
| 8579 | 8592 | &tcc_state->nb_inline_fns, fn); |
| 8593 | + skip_or_save_block(&fn->func_str); | |
| 8580 | 8594 | } else { |
| 8581 | 8595 | /* compute text section */ |
| 8582 | 8596 | cur_text_section = ad.section; |
| @@ -921,7 +921,7 @@ static void pe_build_imports(struct pe_info *pe) · 920 unmodified lines | ||
| 921 | 921 | if (pe->type == PE_RUN) { |
| 922 | 922 | if (dllref) { |
| 923 | 923 | if ( !dllref->handle ) |
| 924 | − dllref->handle = LoadLibrary(dllref->name); | |
| 924 | + dllref->handle = LoadLibraryA(dllref->name); | |
| 925 | 925 | v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name); |
| 926 | 926 | } |
| 927 | 927 | if (!v) |
| @@ -1600,14 +1600,15 @@ static int read_mem(int fd, unsigned offset, void *buffer, unsigned len) · 672 unmodified lines | ||
| 1600 | 1600 | |
| 1601 | 1601 | static int get_dllexports(int fd, char **pp) |
| 1602 | 1602 | { |
| 1603 | − int l, i, n, n0, ret; | |
| 1603 | + int i, k, l, n, n0, ret; | |
| 1604 | 1604 | char *p; |
| 1605 | 1605 | |
| 1606 | 1606 | IMAGE_SECTION_HEADER ish; |
| 1607 | 1607 | IMAGE_EXPORT_DIRECTORY ied; |
| 1608 | 1608 | IMAGE_DOS_HEADER dh; |
| 1609 | 1609 | IMAGE_FILE_HEADER ih; |
| 1610 | − DWORD sig, ref, addr, ptr, namep; | |
| 1610 | + DWORD sig, ref, addr; | |
| 1611 | + DWORD *namep = NULL, p0 = 0, p1; | |
| 1611 | 1612 | |
| 1612 | 1613 | int pef_hdroffset, opt_hdroffset, sec_hdroffset; |
| 1613 | 1614 | |
| @@ -1652,33 +1653,37 @@ static int get_dllexports(int fd, char **pp) · 38 unmodified lines | ||
| 1652 | 1653 | goto found; |
| 1653 | 1654 | } |
| 1654 | 1655 | goto the_end_0; |
| 1655 | − | |
| 1656 | 1656 | found: |
| 1657 | 1657 | ref = ish.VirtualAddress - ish.PointerToRawData; |
| 1658 | 1658 | if (!read_mem(fd, addr - ref, &ied, sizeof ied)) |
| 1659 | 1659 | goto the_end; |
| 1660 | − | |
| 1661 | − namep = ied.AddressOfNames - ref; | |
| 1662 | − for (i = 0; i < ied.NumberOfNames; ++i) { | |
| 1663 | − if (!read_mem(fd, namep, &ptr, sizeof ptr)) | |
| 1660 | + k = ied.NumberOfNames; | |
| 1661 | + if (k) { | |
| 1662 | + namep = tcc_malloc(l = k * sizeof *namep); | |
| 1663 | + if (!read_mem(fd, ied.AddressOfNames - ref, namep, l)) | |
| 1664 | 1664 | goto the_end; |
| 1665 | − namep += sizeof ptr; | |
| 1666 | − for (l = 0;;) { | |
| 1667 | − if (n+1 >= n0) | |
| 1668 | − p = tcc_realloc(p, n0 = n0 ? n0 * 2 : 256); | |
| 1669 | − if (!read_mem(fd, ptr - ref + l++, p + n, 1)) { | |
| 1670 | − tcc_free(p), p = NULL; | |
| 1671 | − goto the_end; | |
| 1672 | − } | |
| 1673 | − if (p[n++] == 0) | |
| 1674 | − break; | |
| 1665 | + for (i = l = 0; i < k; ++i) { | |
| 1666 | + p1 = namep[i] - ref; | |
| 1667 | + if (p1 != p0) | |
| 1668 | + lseek(fd, p0 = p1, SEEK_SET), l = 0; | |
| 1669 | + do { | |
| 1670 | + if (0 == l) { | |
| 1671 | + if (n + 1000 >= n0) | |
| 1672 | + p = tcc_realloc(p, n0 += 1000); | |
| 1673 | + if ((l = read(fd, p + n, 1000 - 1)) <= 0) | |
| 1674 | + goto the_end; | |
| 1675 | + } | |
| 1676 | + --l, ++p0; | |
| 1677 | + } while (p[n++]); | |
| 1675 | 1678 | } |
| 1676 | − } | |
| 1677 | − if (p) | |
| 1678 | 1679 | p[n] = 0; |
| 1680 | + } | |
| 1679 | 1681 | the_end_0: |
| 1680 | 1682 | ret = 0; |
| 1681 | 1683 | the_end: |
| 1684 | + tcc_free(namep); | |
| 1685 | + if (ret && p) | |
| 1686 | + tcc_free(p), p = NULL; | |
| 1682 | 1687 | *pp = p; |
| 1683 | 1688 | return ret; |
| 1684 | 1689 | } |
| @@ -1810,15 +1815,14 @@ quit: · 125 unmodified lines | ||
| 1810 | 1815 | static int pe_load_dll(TCCState *s1, int fd, const char *filename) |
| 1811 | 1816 | { |
| 1812 | 1817 | char *p, *q; |
| 1813 | − int index, ret; | |
| 1814 | − | |
| 1815 | − ret = get_dllexports(fd, &p); | |
| 1816 | − if (ret) { | |
| 1818 | + DLLReference *ref = tcc_add_dllref(s1, filename, 0); | |
| 1819 | + if (ref->found) | |
| 1820 | + return 0; | |
| 1821 | + if (get_dllexports(fd, &p)) | |
| 1817 | 1822 | return -1; |
| 1818 | − } else if (p) { | |
| 1819 | − index = tcc_add_dllref(s1, filename, 0)->index; | |
| 1823 | + if (p) { | |
| 1820 | 1824 | for (q = p; *q; q += 1 + strlen(q)) |
| 1821 | − pe_putimport(s1, index, q, 0); | |
| 1825 | + pe_putimport(s1, ref->index, q, 0); | |
| 1822 | 1826 | tcc_free(p); |
| 1823 | 1827 | } |
| 1824 | 1828 | return 0; |
| @@ -1941,15 +1945,20 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) · 116 unmodified lines | ||
| 1941 | 1945 | start_symbol = "__start"; |
| 1942 | 1946 | run_symbol = "__runmain"; |
| 1943 | 1947 | pe_type = PE_EXE; |
| 1944 | − | |
| 1948 | + if (s1->pe_subsystem == 2) | |
| 1949 | + pe_type = PE_GUI; | |
| 1945 | 1950 | } |
| 1951 | + | |
| 1946 | 1952 | if (TCC_OUTPUT_MEMORY == s1->output_type && !s1->nostdlib) |
| 1947 | 1953 | start_symbol = run_symbol; |
| 1948 | 1954 | } |
| 1949 | − | |
| 1950 | − pe->start_symbol = start_symbol + 1; | |
| 1951 | − if (!s1->leading_underscore || strchr(start_symbol, '@')) | |
| 1952 | − ++start_symbol; | |
| 1955 | + if (s1->elf_entryname) { | |
| 1956 | + pe->start_symbol = start_symbol = s1->elf_entryname; | |
| 1957 | + } else { | |
| 1958 | + pe->start_symbol = start_symbol + 1; | |
| 1959 | + if (!s1->leading_underscore || strchr(start_symbol, '@')) | |
| 1960 | + ++start_symbol; | |
| 1961 | + } | |
| 1953 | 1962 | |
| 1954 | 1963 | #ifdef CONFIG_TCC_BACKTRACE |
| 1955 | 1964 | if (s1->do_backtrace) { |
| @@ -2055,8 +2064,8 @@ ST_FUNC int pe_output_file(TCCState *s1, const char *filename) · 99 unmodified lines | ||
| 2055 | 2064 | #ifdef CONFIG_TCC_BCHECK |
| 2056 | 2065 | tcc_add_bcheck(s1); |
| 2057 | 2066 | #endif |
| 2058 | − tcc_add_pragma_libs(s1); | |
| 2059 | 2067 | pe_add_runtime(s1, &pe); |
| 2068 | + tcc_add_pragma_libs(s1); | |
| 2060 | 2069 | resolve_common_syms(s1); |
| 2061 | 2070 | pe_set_options(s1, &pe); |
| 2062 | 2071 | pe_check_symbols(&pe); |
| @@ -3547,11 +3547,7 @@ static void putdefs(CString *cs, const char *p) · 3546 unmodified lines | ||
| 3547 | 3547 | |
| 3548 | 3548 | static void tcc_predefs(TCCState *s1, CString *cs, int is_asm) |
| 3549 | 3549 | { |
| 3550 | − int a, b, c; | |
| 3551 | − | |
| 3552 | − sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c); | |
| 3553 | − cstr_printf(cs, "#define __TINYC__ %d\n", a*10000 + b*100 + c); | |
| 3554 | − | |
| 3550 | + cstr_printf(cs, "#define __TINYC__ 9%.2s\n", TCC_VERSION + 4); | |
| 3555 | 3551 | putdefs(cs, target_machine_defs); |
| 3556 | 3552 | putdefs(cs, target_os_defs); |
| 3557 | 3553 | |
| @@ -3742,7 +3738,10 @@ static void tok_print(const int *str, const char *msg, ...) · 184 unmodified lines | ||
| 3742 | 3738 | va_list ap; |
| 3743 | 3739 | int t, t0, s; |
| 3744 | 3740 | CValue cval; |
| 3745 | − va_start(ap, msg), vfprintf(fp, msg, ap), va_end(ap); | |
| 3741 | + | |
| 3742 | + va_start(ap, msg); | |
| 3743 | + vfprintf(fp, msg, ap); | |
| 3744 | + va_end(ap); | |
| 3746 | 3745 | |
| 3747 | 3746 | s = t0 = 0; |
| 3748 | 3747 | while (str) { |
| @@ -179,8 +179,6 @@ ST_FUNC void tcc_run_free(TCCState *s1) · 178 unmodified lines | ||
| 179 | 179 | dlclose(ref->handle); |
| 180 | 180 | #endif |
| 181 | 181 | } |
| 182 | − /* free loaded dlls array */ | |
| 183 | − dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls); | |
| 184 | 182 | /* unmap or unprotect and free memory */ |
| 185 | 183 | ptr = s1->run_ptr; |
| 186 | 184 | if (NULL == ptr) |
| @@ -854,7 +852,7 @@ static addr_t rt_printline_dwarf (rt_context *rc, addr_t wanted_pc, bt_info *bi) · 667 unmodified lines | ||
| 854 | 852 | char *dirs[DIR_TABLE_SIZE]; |
| 855 | 853 | #endif |
| 856 | 854 | unsigned int filename_size; |
| 857 | − struct dwarf_filename_struct { | |
| 855 | + struct /*dwarf_filename_struct*/ { | |
| 858 | 856 | unsigned int dir_entry; |
| 859 | 857 | char *name; |
| 860 | 858 | } filename_table[FILE_TABLE_SIZE]; |
| @@ -3760,14 +3760,13 @@ void asm_dot_test(void) · 3759 unmodified lines | ||
| 3760 | 3760 | |
| 3761 | 3761 | void asm_pcrel_test(void) |
| 3762 | 3762 | { |
| 3763 | −#if defined(__i386__) | |
| 3764 | 3763 | unsigned o1, o2; |
| 3765 | 3764 | /* subtract text-section label from forward or other-section label */ |
| 3766 | 3765 | asm("1: mov $2f-1b,%%eax; mov %%eax,%0" : "=m"(o1)); |
| 3767 | 3766 | /* verify ... */ |
| 3768 | − asm("2: mov $2b,%%eax; sub $1b,%%eax; mov %%eax,%0" : "=m"(o2)); | |
| 3767 | + asm("2: lea 2b"RX",%eax; lea 1b"RX",%ecx; sub %ecx,%eax"); | |
| 3768 | + asm("mov %%eax,%0" : "=m"(o2)); | |
| 3769 | 3769 | printf("%s : %x\n", __FUNCTION__, o1 - o2); /* should be zero */ |
| 3770 | −#endif | |
| 3771 | 3770 | } |
| 3772 | 3771 | |
| 3773 | 3772 | void asm_test(void) |
| @@ -137,6 +137,7 @@ for %%f in (*tcc.exe *tcc.dll) do @del %%f · 136 unmodified lines | ||
| 137 | 137 | @if _%TCC_C%_==__ goto compiler_2parts |
| 138 | 138 | @rem if TCC_C was defined then build only tcc.exe |
| 139 | 139 | %CC% -o tcc.exe %TCC_C% %D% |
| 140 | +@if errorlevel 1 goto :the_end | |
| 140 | 141 | @goto :compiler_done |
| 141 | 142 | |
| 142 | 143 | :compiler_2parts |
| @@ -144,8 +145,10 @@ for %%f in (*tcc.exe *tcc.dll) do @del %%f · 1 unmodified line | ||
| 144 | 145 | %CC% -o libtcc.dll -shared %LIBTCC_C% %D% -DLIBTCC_AS_DLL |
| 145 | 146 | @if errorlevel 1 goto :the_end |
| 146 | 147 | %CC% -o tcc.exe ..\tcc.c libtcc.dll %D% -DONE_SOURCE"=0" |
| 148 | +@if errorlevel 1 goto :the_end | |
| 147 | 149 | if not _%XCC%_==_yes_ goto :compiler_done |
| 148 | 150 | %CC% -o %PX%-tcc.exe ..\tcc.c %DX% |
| 151 | +@if errorlevel 1 goto :the_end | |
| 149 | 152 | :compiler_done |
| 150 | 153 | @if (%EXES_ONLY%)==(yes) goto :files_done |
| 151 | 154 | |