History

win32 merge (grischka)

bellard committed 21 years ago

Commit fe9b1f61 parent 09f4ce94 files · +186 −145Browse files

Unified · Split

4 files
  1. tcc.c +21 −10
  2. tccelf.c +2 −9
  3. tccpe.c +61 −24
  4. win32/readme.txt +102 −102
tcc.c+21−10
@@ -711,9 +711,12 @@ int __stdcall FreeConsole(void); · 710 unmodified lines
711711
712712 #define snprintf _snprintf
713713 #define vsnprintf _vsnprintf
714+#ifndef __GNUC__
715+ #define strtold (long double)strtod
716+ #define strtof (float)strtod
717+ #define strtoll (long long)strtol
714718 #endif
715−
716−#if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
719+#elif defined(TCC_UCLIBC) || defined(__FreeBSD__)
717720 /* currently incorrect */
718721 long double strtold(const char *nptr, char **endptr)
719722 {
@@ -2730,6 +2733,7 @@ static void pragma_parse(TCCState *s1) · 2010 unmodified lines
27302733 #pragma pack(push,1) // push & set
27312734 #pragma pack(pop) // restore previous
27322735 */
2736+ next();
27332737 skip('(');
27342738 if (tok == TOK_ASM_pop) {
27352739 next();
@@ -10514,8 +10518,10 @@ int main(int argc, char **argv) · 7778 unmodified lines
1051410518 GetModuleFileNameA(NULL, path, sizeof path);
1051510519 p = d = strlwr(path);
1051610520 while (*d)
10517− if (*d++ == '\\')
10518− (p = d)[-1] = '/';
10521+ {
10522+ if (*d == '\\') *d = '/', p = d;
10523+ ++d;
10524+ }
1051910525 *p = '\0';
1052010526 tcc_lib_path = path;
1052110527 }
@@ -10555,23 +10561,28 @@ int main(int argc, char **argv) · 33 unmodified lines
1055510561 error("cannot specify libraries with -c");
1055610562 }
1055710563
10564+ if (output_type != TCC_OUTPUT_MEMORY) {
10565+ if (!outfile) {
1055810566 /* 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 */
1056310567 pstrcpy(objfilename, sizeof(objfilename) - 1,
10568+ /* strip path */
1056410569 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, '.');
1056710575 if (!ext)
1056810576 goto default_outfile;
10577+ /* add .o extension */
1056910578 strcpy(ext + 1, "o");
1057010579 } else {
1057110580 default_outfile:
1057210581 pstrcpy(objfilename, sizeof(objfilename), "a.out");
1057310582 }
10583+#endif
1057410584 outfile = objfilename;
10585+ }
1057510586 }
1057610587
1057710588 if (do_bench) {
tccelf.c+2−9
@@ -2095,7 +2095,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 2094 unmodified lines
20952095 {
20962096 Elf32_Ehdr ehdr;
20972097 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;
20992099 Elf32_Sym *sym, *dynsym;
21002100 Elf32_Dyn *dt, *dynamic;
21012101 unsigned char *dynstr;
@@ -2175,15 +2175,8 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 73 unmodified lines
21752175 if (sym_bind == STB_LOCAL)
21762176 continue;
21772177 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
21852178 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);
21872180 }
21882181
21892182 /* load all referenced DLLs */
tccpe.c+61−24
@@ -742,24 +742,38 @@ ST void pe_build_imports(struct pe_info *pe) · 741 unmodified lines
742742 }
743743
744744 /* ------------------------------------------------------------- */
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+
745754 ST void pe_build_exports(struct pe_info *pe)
746755 {
747756 Elf32_Sym *sym;
748− DWORD func_offset;
749− DWORD voffset = pe->thunk->sh_addr - pe->imagebase;
757+ DWORD func_offset, voffset;
750758 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;
752763
753764 // for simplicity only functions are exported
754765 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+ }
759772
760773 if (0 == sym_count)
761774 return;
762775
776+ qsort (sorted, sym_count, sizeof sorted[0], sym_cmp);
763777 pe_align_section(pe->thunk, 16);
764778
765779 pe->exp_offs = pe->thunk->data_offset;
@@ -774,30 +788,28 @@ ST void pe_build_exports(struct pe_info *pe) · 8 unmodified lines
774788 hdr->NumberOfFunctions = sym_count;
775789 hdr->NumberOfNames = sym_count;
776790 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);
781793 hdr->Name = pe->thunk->data_offset + voffset;
782794 put_elf_str(pe->thunk, tcc_basename(pe->filename));
783795
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;
794806 *pname = pe->thunk->data_offset + voffset;
795− *pord = ordinal;
807+ *pord = ord;
796808 put_elf_str(pe->thunk, name);
797809 /* printf("export: %s\n", name); */
798− ++ordinal;
799810 }
800811 pe->exp_size = pe->thunk->data_offset - pe->exp_offs;
812+ tcc_free(sorted);
801813 }
802814
803815 /* ------------------------------------------------------------- */
@@ -1119,6 +1131,24 @@ int pe_load_def_file(TCCState * s1, FILE * fp) · 315 unmodified lines
11191131 return 0;
11201132 }
11211133
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+
11221152 /* ------------------------------------------------------------- */
11231153 unsigned long pe_add_runtime(TCCState * s1)
11241154 {
@@ -1127,6 +1157,13 @@ unsigned long pe_add_runtime(TCCState * s1) · 2 unmodified lines
11271157
11281158 if (find_elf_sym(symtab_section, "WinMain"))
11291159 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+ }
11301167
11311168 start_symbol =
11321169 TCC_OUTPUT_MEMORY == s1->output_type
@@ -1192,7 +1229,7 @@ int tcc_output_pe(TCCState * s1, const char *filename) · 59 unmodified lines
11921229 {
11931230 Section *s;
11941231 FILE *f;
1195− f = fopen("tccpe.log", "wb");
1232+ f = fopen("tccpe.log", "wt");
11961233 for (i = 1; i < s1->nb_sections; ++i) {
11971234 s = s1->sections[i];
11981235 pe_print_section(f, s);
win32/readme.txt+102−102
@@ -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+

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