Unified · Split
| @@ -711,9 +711,12 @@ int __stdcall FreeConsole(void); · 710 unmodified lines | ||
| 711 | 711 | |
| 712 | 712 | #define snprintf _snprintf |
| 713 | 713 | #define vsnprintf _vsnprintf |
| 714 | +#ifndef __GNUC__ | |
| 715 | + #define strtold (long double)strtod | |
| 716 | + #define strtof (float)strtod | |
| 717 | + #define strtoll (long long)strtol | |
| 714 | 718 | #endif |
| 715 | − | |
| 716 | −#if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__) | |
| 719 | +#elif defined(TCC_UCLIBC) || defined(__FreeBSD__) | |
| 717 | 720 | /* currently incorrect */ |
| 718 | 721 | long double strtold(const char *nptr, char **endptr) |
| 719 | 722 | { |
| @@ -2730,6 +2733,7 @@ static void pragma_parse(TCCState *s1) · 2010 unmodified lines | ||
| 2730 | 2733 | #pragma pack(push,1) // push & set |
| 2731 | 2734 | #pragma pack(pop) // restore previous |
| 2732 | 2735 | */ |
| 2736 | + next(); | |
| 2733 | 2737 | skip('('); |
| 2734 | 2738 | if (tok == TOK_ASM_pop) { |
| 2735 | 2739 | next(); |
| @@ -10514,8 +10518,10 @@ int main(int argc, char **argv) · 7778 unmodified lines | ||
| 10514 | 10518 | GetModuleFileNameA(NULL, path, sizeof path); |
| 10515 | 10519 | p = d = strlwr(path); |
| 10516 | 10520 | while (*d) |
| 10517 | − if (*d++ == '\\') | |
| 10518 | − (p = d)[-1] = '/'; | |
| 10521 | + { | |
| 10522 | + if (*d == '\\') *d = '/', p = d; | |
| 10523 | + ++d; | |
| 10524 | + } | |
| 10519 | 10525 | *p = '\0'; |
| 10520 | 10526 | tcc_lib_path = path; |
| 10521 | 10527 | } |
| @@ -10555,23 +10561,28 @@ int main(int argc, char **argv) · 33 unmodified lines | ||
| 10555 | 10561 | error("cannot specify libraries with -c"); |
| 10556 | 10562 | } |
| 10557 | 10563 | |
| 10564 | + if (output_type != TCC_OUTPUT_MEMORY) { | |
| 10565 | + if (!outfile) { | |
| 10558 | 10566 | /* compute default outfile name */ |
| 10559 | − if (output_type != TCC_OUTPUT_MEMORY && !outfile) { | |
| 10560 | − if (output_type == TCC_OUTPUT_OBJ && !reloc_output) { | |
| 10561 | − char *ext; | |
| 10562 | − /* strip path */ | |
| 10563 | 10567 | pstrcpy(objfilename, sizeof(objfilename) - 1, |
| 10568 | + /* strip path */ | |
| 10564 | 10569 | tcc_basename(files[0])); |
| 10565 | − /* add .o extension */ | |
| 10566 | − ext = strrchr(objfilename, '.'); | |
| 10570 | +#ifdef TCC_TARGET_PE | |
| 10571 | + pe_guess_outfile(objfilename, output_type); | |
| 10572 | +#else | |
| 10573 | + if (output_type == TCC_OUTPUT_OBJ && !reloc_output) { | |
| 10574 | + char *ext = strrchr(objfilename, '.'); | |
| 10567 | 10575 | if (!ext) |
| 10568 | 10576 | goto default_outfile; |
| 10577 | + /* add .o extension */ | |
| 10569 | 10578 | strcpy(ext + 1, "o"); |
| 10570 | 10579 | } else { |
| 10571 | 10580 | default_outfile: |
| 10572 | 10581 | pstrcpy(objfilename, sizeof(objfilename), "a.out"); |
| 10573 | 10582 | } |
| 10583 | +#endif | |
| 10574 | 10584 | outfile = objfilename; |
| 10585 | + } | |
| 10575 | 10586 | } |
| 10576 | 10587 | |
| 10577 | 10588 | if (do_bench) { |
| @@ -2095,7 +2095,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 2094 unmodified lines | ||
| 2095 | 2095 | { |
| 2096 | 2096 | Elf32_Ehdr ehdr; |
| 2097 | 2097 | Elf32_Shdr *shdr, *sh, *sh1; |
| 2098 | − int i, nb_syms, nb_dts, sym_bind, ret, other; | |
| 2098 | + int i, nb_syms, nb_dts, sym_bind, ret; | |
| 2099 | 2099 | Elf32_Sym *sym, *dynsym; |
| 2100 | 2100 | Elf32_Dyn *dt, *dynamic; |
| 2101 | 2101 | unsigned char *dynstr; |
| @@ -2175,15 +2175,8 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 73 unmodified lines | ||
| 2175 | 2175 | if (sym_bind == STB_LOCAL) |
| 2176 | 2176 | continue; |
| 2177 | 2177 | name = dynstr + sym->st_name; |
| 2178 | −#ifdef TCC_TARGET_PE | |
| 2179 | − /* in the PE format we need to know the DLL from which the | |
| 2180 | − symbol comes. XXX: add a new array for that ? */ | |
| 2181 | − other = s1->nb_loaded_dlls - 1; | |
| 2182 | −#else | |
| 2183 | − other = sym->st_other; | |
| 2184 | −#endif | |
| 2185 | 2178 | add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size, |
| 2186 | − sym->st_info, other, sym->st_shndx, name); | |
| 2179 | + sym->st_info, sym->st_other, sym->st_shndx, name); | |
| 2187 | 2180 | } |
| 2188 | 2181 | |
| 2189 | 2182 | /* load all referenced DLLs */ |
| @@ -742,24 +742,38 @@ ST void pe_build_imports(struct pe_info *pe) · 741 unmodified lines | ||
| 742 | 742 | } |
| 743 | 743 | |
| 744 | 744 | /* ------------------------------------------------------------- */ |
| 745 | +ST int sym_cmp(const void *va, const void *vb) | |
| 746 | +{ | |
| 747 | + Elf32_Sym *sa = (Elf32_Sym *)symtab_section->data + *(int*)va; | |
| 748 | + Elf32_Sym *sb = (Elf32_Sym *)symtab_section->data + *(int*)vb; | |
| 749 | + const char *ca = symtab_section->link->data + sa->st_name; | |
| 750 | + const char *cb = symtab_section->link->data + sb->st_name; | |
| 751 | + return strcmp(ca, cb); | |
| 752 | +} | |
| 753 | + | |
| 745 | 754 | ST void pe_build_exports(struct pe_info *pe) |
| 746 | 755 | { |
| 747 | 756 | Elf32_Sym *sym; |
| 748 | − DWORD func_offset; | |
| 749 | − DWORD voffset = pe->thunk->sh_addr - pe->imagebase; | |
| 757 | + DWORD func_offset, voffset; | |
| 750 | 758 | struct pe_export_header *hdr; |
| 751 | − int sym_count = 0, ordinal = 0; | |
| 759 | + int sym_count, n, ord, *sorted; | |
| 760 | + | |
| 761 | + voffset = pe->thunk->sh_addr - pe->imagebase; | |
| 762 | + sym_count = 0, n = 1, sorted = NULL; | |
| 752 | 763 | |
| 753 | 764 | // for simplicity only functions are exported |
| 754 | 765 | for_sym_in_symtab(sym) |
| 755 | − if (sym->st_shndx != text_section->sh_num) | |
| 756 | − sym->st_other &= ~1; | |
| 757 | − else if (sym->st_other & 1) | |
| 758 | − ++sym_count; | |
| 766 | + { | |
| 767 | + if ((sym->st_other & 1) | |
| 768 | + && sym->st_shndx == text_section->sh_num) | |
| 769 | + dynarray_add((void***)&sorted, &sym_count, (void*)n); | |
| 770 | + ++n; | |
| 771 | + } | |
| 759 | 772 | |
| 760 | 773 | if (0 == sym_count) |
| 761 | 774 | return; |
| 762 | 775 | |
| 776 | + qsort (sorted, sym_count, sizeof sorted[0], sym_cmp); | |
| 763 | 777 | pe_align_section(pe->thunk, 16); |
| 764 | 778 | |
| 765 | 779 | pe->exp_offs = pe->thunk->data_offset; |
| @@ -774,30 +788,28 @@ ST void pe_build_exports(struct pe_info *pe) · 8 unmodified lines | ||
| 774 | 788 | hdr->NumberOfFunctions = sym_count; |
| 775 | 789 | hdr->NumberOfNames = sym_count; |
| 776 | 790 | hdr->AddressOfFunctions = func_offset + voffset; |
| 777 | − hdr->AddressOfNames = | |
| 778 | − hdr->AddressOfFunctions + sym_count * sizeof(DWORD); | |
| 779 | − hdr->AddressOfNameOrdinals = | |
| 780 | − hdr->AddressOfNames + sym_count * sizeof(DWORD); | |
| 791 | + hdr->AddressOfNames = hdr->AddressOfFunctions + sym_count * sizeof(DWORD); | |
| 792 | + hdr->AddressOfNameOrdinals = hdr->AddressOfNames + sym_count * sizeof(DWORD); | |
| 781 | 793 | hdr->Name = pe->thunk->data_offset + voffset; |
| 782 | 794 | put_elf_str(pe->thunk, tcc_basename(pe->filename)); |
| 783 | 795 | |
| 784 | − for_sym_in_symtab(sym) | |
| 785 | − if (sym->st_other & 1) { | |
| 786 | − char *name = symtab_section->link->data + sym->st_name; | |
| 787 | − DWORD *p = (DWORD *) (pe->thunk->data + func_offset); | |
| 788 | − DWORD *pfunc = p + ordinal; | |
| 789 | − DWORD *pname = p + sym_count + ordinal; | |
| 790 | − WORD *pord = (WORD *) (p + 2 * sym_count) + ordinal; | |
| 791 | − *pfunc = | |
| 792 | − sym->st_value + pe->s1->sections[sym->st_shndx]->sh_addr - | |
| 793 | − pe->imagebase; | |
| 796 | + for (ord = 0; ord < sym_count; ++ord) | |
| 797 | + { | |
| 798 | + char *name; DWORD *p, *pfunc, *pname; WORD *pord; | |
| 799 | + sym = (Elf32_Sym *)symtab_section->data + sorted[ord]; | |
| 800 | + name = symtab_section->link->data + sym->st_name; | |
| 801 | + p = (DWORD*)(pe->thunk->data + func_offset); | |
| 802 | + pfunc = p + ord; | |
| 803 | + pname = p + sym_count + ord; | |
| 804 | + pord = (WORD *)(p + 2*sym_count) + ord; | |
| 805 | + *pfunc = sym->st_value + pe->s1->sections[sym->st_shndx]->sh_addr - pe->imagebase; | |
| 794 | 806 | *pname = pe->thunk->data_offset + voffset; |
| 795 | − *pord = ordinal; | |
| 807 | + *pord = ord; | |
| 796 | 808 | put_elf_str(pe->thunk, name); |
| 797 | 809 | /* printf("export: %s\n", name); */ |
| 798 | − ++ordinal; | |
| 799 | 810 | } |
| 800 | 811 | pe->exp_size = pe->thunk->data_offset - pe->exp_offs; |
| 812 | + tcc_free(sorted); | |
| 801 | 813 | } |
| 802 | 814 | |
| 803 | 815 | /* ------------------------------------------------------------- */ |
| @@ -1119,6 +1131,24 @@ int pe_load_def_file(TCCState * s1, FILE * fp) · 315 unmodified lines | ||
| 1119 | 1131 | return 0; |
| 1120 | 1132 | } |
| 1121 | 1133 | |
| 1134 | +/* ------------------------------------------------------------- */ | |
| 1135 | +void pe_guess_outfile(char *objfilename, int output_type) | |
| 1136 | +{ | |
| 1137 | + char *ext = strrchr(objfilename, '.'); | |
| 1138 | + if (NULL == ext) | |
| 1139 | + ext = strchr(objfilename, 0); | |
| 1140 | + if (output_type == TCC_OUTPUT_DLL) | |
| 1141 | + strcpy(ext, ".dll"); | |
| 1142 | + else | |
| 1143 | + if (output_type == TCC_OUTPUT_EXE) | |
| 1144 | + strcpy(ext, ".exe"); | |
| 1145 | + else | |
| 1146 | + if (output_type == TCC_OUTPUT_OBJ && strcmp(ext, ".o")) | |
| 1147 | + strcpy(ext, ".o"); | |
| 1148 | + else | |
| 1149 | + error("no outputfile given"); | |
| 1150 | +} | |
| 1151 | + | |
| 1122 | 1152 | /* ------------------------------------------------------------- */ |
| 1123 | 1153 | unsigned long pe_add_runtime(TCCState * s1) |
| 1124 | 1154 | { |
| @@ -1127,6 +1157,13 @@ unsigned long pe_add_runtime(TCCState * s1) · 2 unmodified lines | ||
| 1127 | 1157 | |
| 1128 | 1158 | if (find_elf_sym(symtab_section, "WinMain")) |
| 1129 | 1159 | pe_type = PE_GUI; |
| 1160 | + else | |
| 1161 | + if (TCC_OUTPUT_DLL == s1->output_type) | |
| 1162 | + { | |
| 1163 | + pe_type = PE_DLL; | |
| 1164 | + // need this for 'tccelf.c:relocate_section()' | |
| 1165 | + s1->output_type = TCC_OUTPUT_EXE; | |
| 1166 | + } | |
| 1130 | 1167 | |
| 1131 | 1168 | start_symbol = |
| 1132 | 1169 | TCC_OUTPUT_MEMORY == s1->output_type |
| @@ -1192,7 +1229,7 @@ int tcc_output_pe(TCCState * s1, const char *filename) · 59 unmodified lines | ||
| 1192 | 1229 | { |
| 1193 | 1230 | Section *s; |
| 1194 | 1231 | FILE *f; |
| 1195 | − f = fopen("tccpe.log", "wb"); | |
| 1232 | + f = fopen("tccpe.log", "wt"); | |
| 1196 | 1233 | for (i = 1; i < s1->nb_sections; ++i) { |
| 1197 | 1234 | s = s1->sections[i]; |
| 1198 | 1235 | pe_print_section(f, s); |
| @@ -1,102 +1,102 @@ | ||
| 1 | − | |
| 2 | − TinyCC-PE | |
| 3 | − --------- | |
| 4 | − | |
| 5 | − TinyCC (aka TCC) is a small but hyperfast C compiler, | |
| 6 | − written by Fabrice Bellard, | |
| 7 | − | |
| 8 | − | |
| 9 | − TinyCC-PE is the TinyCC compiler with an extension to | |
| 10 | − write PE executables for MS-Windows. | |
| 11 | − | |
| 12 | − | |
| 13 | − Features: | |
| 14 | − --------- | |
| 15 | − | |
| 16 | − TinyCC-PE can produce console applications, native windows | |
| 17 | − GUI programs and DLL's. | |
| 18 | − | |
| 19 | − Most of the features pointed out by Fabrice Bellard for the | |
| 20 | − original version are still valid, i.e: | |
| 21 | − | |
| 22 | − - SMALL! The package with ~400kb includes a complete C-compiler | |
| 23 | − with header files for console and GUI applications. | |
| 24 | − | |
| 25 | − - With the -run switch you can run C-sources without any | |
| 26 | − linking directly from the command line. | |
| 27 | − | |
| 28 | − - TCC can of course compile itself. | |
| 29 | − | |
| 30 | − | |
| 31 | − Installation: | |
| 32 | − ------------- | |
| 33 | − | |
| 34 | − Just unzip the package to a directory anywhere on your computer. | |
| 35 | − | |
| 36 | − | |
| 37 | − Examples: | |
| 38 | − --------- | |
| 39 | − | |
| 40 | − For the 'Fibonacci' console example type from the command line: | |
| 41 | − | |
| 42 | − tcc examples\fib.c | |
| 43 | − | |
| 44 | − | |
| 45 | − For the 'Hello Windows' GUI example: | |
| 46 | − | |
| 47 | − tcc examples\hello_win.c | |
| 48 | − | |
| 49 | − | |
| 50 | − For the 'Hello DLL' example: | |
| 51 | − | |
| 52 | − tcc -shared examples\dll.c | |
| 53 | − tcc examples\hello_dll.c examples\dll.def | |
| 54 | − | |
| 55 | − | |
| 56 | − Import Definitions: | |
| 57 | − ------------------- | |
| 58 | − | |
| 59 | − TinyCC-PE searches and reads import definition files similar | |
| 60 | − to libraries. | |
| 61 | − | |
| 62 | − The included 'tiny_impdef' program may be used to make .def files | |
| 63 | − for any DLL, e.g for an 'opengl32.def': | |
| 64 | − | |
| 65 | − tiny_impdef.exe opengl32.dll | |
| 66 | − | |
| 67 | − or to the same effect: | |
| 68 | − | |
| 69 | − tcc -lkernel32 -run tiny_impdef.c opengl32.dll | |
| 70 | − | |
| 71 | − | |
| 72 | − Header Files: | |
| 73 | − ------------- | |
| 74 | − | |
| 75 | − The system header files, except '_mingw.h', are from the | |
| 76 | − 2.0 mingw distribution. See also: http://www.mingw.org/ | |
| 77 | − | |
| 78 | − | |
| 79 | − Compile TCC: | |
| 80 | − ------------ | |
| 81 | − | |
| 82 | − With TCC itself just say: | |
| 83 | − | |
| 84 | − tcc src\tcc.c -lkernel32 -o tcc.new.exe | |
| 85 | − | |
| 86 | − Other compilers like mingw-gcc or msvc work as well. | |
| 87 | − To make libtcc1.a, you need 'ar' from the mingw binutils. | |
| 88 | − | |
| 89 | − | |
| 90 | − Documentation and License: | |
| 91 | − -------------------------- | |
| 92 | − | |
| 93 | − TCC is distributed under the GNU Lesser General Public License | |
| 94 | − (see COPYING file). | |
| 95 | − | |
| 96 | − Please read the original tcc-doc.html to have all the features | |
| 97 | − of TCC. Also visit: http://fabrice.bellard.free.fr/tcc/ | |
| 98 | − | |
| 99 | − | |
| 100 | − -------------------------------------------- | |
| 101 | − 09.Apr.2005 - grischka@users.sourceforge.net | |
| 102 | − | |
| 1 | + | |
| 2 | + TinyCC-PE | |
| 3 | + --------- | |
| 4 | + | |
| 5 | + TinyCC (aka TCC) is a small but hyperfast C compiler, | |
| 6 | + written by Fabrice Bellard, | |
| 7 | + | |
| 8 | + | |
| 9 | + TinyCC-PE is the TinyCC compiler with an extension to | |
| 10 | + write PE executables for MS-Windows. | |
| 11 | + | |
| 12 | + | |
| 13 | + Features: | |
| 14 | + --------- | |
| 15 | + | |
| 16 | + TinyCC-PE can produce console applications, native windows | |
| 17 | + GUI programs and DLL's. | |
| 18 | + | |
| 19 | + Most of the features pointed out by Fabrice Bellard for the | |
| 20 | + original version are still valid, i.e: | |
| 21 | + | |
| 22 | + - SMALL! The package with ~400kb includes a complete C-compiler | |
| 23 | + with header files for console and GUI applications. | |
| 24 | + | |
| 25 | + - With the -run switch you can run C-sources without any | |
| 26 | + linking directly from the command line. | |
| 27 | + | |
| 28 | + - TCC can of course compile itself. | |
| 29 | + | |
| 30 | + | |
| 31 | + Installation: | |
| 32 | + ------------- | |
| 33 | + | |
| 34 | + Just unzip the package to a directory anywhere on your computer. | |
| 35 | + | |
| 36 | + | |
| 37 | + Examples: | |
| 38 | + --------- | |
| 39 | + | |
| 40 | + For the 'Fibonacci' console example type from the command line: | |
| 41 | + | |
| 42 | + tcc examples\fib.c | |
| 43 | + | |
| 44 | + | |
| 45 | + For the 'Hello Windows' GUI example: | |
| 46 | + | |
| 47 | + tcc examples\hello_win.c | |
| 48 | + | |
| 49 | + | |
| 50 | + For the 'Hello DLL' example: | |
| 51 | + | |
| 52 | + tcc -shared examples\dll.c | |
| 53 | + tcc examples\hello_dll.c examples\dll.def | |
| 54 | + | |
| 55 | + | |
| 56 | + Import Definitions: | |
| 57 | + ------------------- | |
| 58 | + | |
| 59 | + TinyCC-PE searches and reads import definition files similar | |
| 60 | + to libraries. | |
| 61 | + | |
| 62 | + The included 'tiny_impdef' program may be used to make .def files | |
| 63 | + for any DLL, e.g for an 'opengl32.def': | |
| 64 | + | |
| 65 | + tiny_impdef.exe opengl32.dll | |
| 66 | + | |
| 67 | + or to the same effect: | |
| 68 | + | |
| 69 | + tcc -lkernel32 -run tiny_impdef.c opengl32.dll | |
| 70 | + | |
| 71 | + | |
| 72 | + Header Files: | |
| 73 | + ------------- | |
| 74 | + | |
| 75 | + The system header files, except '_mingw.h', are from the | |
| 76 | + 2.0 mingw distribution. See also: http://www.mingw.org/ | |
| 77 | + | |
| 78 | + | |
| 79 | + Compile TCC: | |
| 80 | + ------------ | |
| 81 | + | |
| 82 | + With TCC itself just say: | |
| 83 | + | |
| 84 | + tcc src\tcc.c -lkernel32 -o tcc.new.exe | |
| 85 | + | |
| 86 | + Other compilers like mingw-gcc or msvc work as well. | |
| 87 | + To make libtcc1.a, you need 'ar' from the mingw binutils. | |
| 88 | + | |
| 89 | + | |
| 90 | + Documentation and License: | |
| 91 | + -------------------------- | |
| 92 | + | |
| 93 | + TCC is distributed under the GNU Lesser General Public License | |
| 94 | + (see COPYING file). | |
| 95 | + | |
| 96 | + Please read the original tcc-doc.html to have all the features | |
| 97 | + of TCC. Also visit: http://fabrice.bellard.free.fr/tcc/ | |
| 98 | + | |
| 99 | + | |
| 100 | + -------------------------------------------- | |
| 101 | + 09.Apr.2005 - grischka@users.sourceforge.net | |
| 102 | + | |