History

Checkin tiny_libmaker (ar replacement) by Timovj Lahde

grischka committed 18 years ago

Commit 2eaa1101 parent 265dddb7 files · +393 −72Browse files

Unified · Split

7 files
  1. Makefile +6 −2
  2. configure +14 −1
  3. win32/build-tcc.bat +3 −6
  4. win32/include/_mingw.h +10 −10
  5. win32/readme.txt +41 −53
  6. win32/tools/tiny_impdef.c +0 −0
  7. win32/tools/tiny_libmaker.c +319 −0
Makefile+6−2
@@ -19,8 +19,10 @@ ifeq ($(GCC_MAJOR),2) · 18 unmodified lines
1919 CFLAGS+=-m386 -malign-functions=0
2020 else
2121 CFLAGS+=-march=i386 -falign-functions=0 -fno-strict-aliasing
22+ifneq ($(GCC_MAJOR),3)
2223 CFLAGS+=-Wno-pointer-sign -Wno-sign-compare
2324 endif
25+endif
2426
2527 DISAS=objdump -d
2628 INSTALL=install
@@ -30,7 +32,7 @@ PROGS=tcc$(EXESUF) · 3 unmodified lines
3032 ifdef CONFIG_CROSS
3133 PROGS+=c67-tcc$(EXESUF) arm-tcc$(EXESUF)
3234 endif
33−PROGS+=tiny_impdef$(EXESUF)
35+PROGS+=tiny_impdef$(EXESUF) tiny_libmaker$(EXESUF)
3436 else
3537 ifeq ($(ARCH),i386)
3638 PROGS=tcc$(EXESUF)
@@ -168,7 +170,9 @@ i386-win32-tcc$(EXESUF): tcc.c i386-gen.c tccelf.c tccasm.c i386-asm.c tcctok.h · 131 unmodified lines
168170 $(CC) $(CFLAGS) -DTCC_TARGET_PE -o $@ $< $(LIBS)
169171
170172 # windows utilities
171−tiny_impdef$(EXESUF): tiny_impdef.c
173+tiny_impdef$(EXESUF): win32/tools/tiny_impdef.c
174+ $(CC) $(CFLAGS) -o $@ $< -lkernel32
175+tiny_libmaker$(EXESUF): win32/tools/tiny_libmaker.c
172176 $(CC) $(CFLAGS) -o $@ $< -lkernel32
173177
174178 # TinyCC runtime libraries
configure+14−1
@@ -181,7 +181,20 @@ EOF · 180 unmodified lines
181181
182182 gcc_major="2"
183183 if $cc -o $TMPO $TMPC 2> /dev/null ; then
184− gcc_major="3"
184+ gcc_major="3"
185+fi
186+cat > $TMPC <<EOF
187+int main(void) {
188+#if __GNUC__ >= 4
189+return 0;
190+#else
191+#error gcc < 4
192+#endif
193+}
194+EOF
195+
196+if $cc -o $TMPO $TMPC 2> /dev/null ; then
197+ gcc_major="4"
185198 fi
186199
187200 if test x"$1" = x"-h" -o x"$1" = x"--help" ; then
win32/build-tcc.bat+3−6
@@ -2,17 +2,14 @@ · 1 unmodified line
22 @rem batch file to build tcc using gcc and ar from mingw
33 @rem ----------------------------------------------------
44 :
5−@if exist ..\config.h goto configready
6−:
7−@echo>..\config.h #define TCC_VERSION "0.9.24pre"
5+@echo>..\config.h #define TCC_VERSION "0.9.24"
86 @echo>>..\config.h #define TCC_TARGET_PE 1
97 @echo>>..\config.h #define CONFIG_TCCDIR NULL
108 :
11−:configready
12−:
139 gcc -Os -fno-strict-aliasing ../tcc.c -o tcc.exe -s
1410 gcc -Os -fno-strict-aliasing ../tcc.c -D LIBTCC -c -o libtcc.o
15−gcc -Os -fno-strict-aliasing ../tiny_impdef.c -o tiny_impdef.exe -s
11+gcc -Os tools/tiny_impdef.c -o tiny_impdef.exe -s
12+gcc -Os tools/tiny_libmaker.c -o tiny_libmaker.exe -s
1613 mkdir libtcc
1714 ar rcs libtcc/libtcc.a libtcc.o
1815 del libtcc.o
win32/include/_mingw.h+10−10
@@ -23,22 +23,21 @@ · 22 unmodified lines
2323 #define __int64 long long
2424 #define __int32 long
2525 #define __int16 short
26−#define __int8 char
27−#define __cdecl __attribute__((__cdecl__))
28−#define __stdcall __attribute__((__stdcall__))
29−#define __declspec(x) __attribute__((x))
26+#define __int8 char
27+#define __cdecl __attribute__((__cdecl__))
28+#define __stdcall __attribute__((__stdcall__))
29+#define __declspec(x) __attribute__((x))
3030
3131 #define __MINGW32_VERSION 2.0
3232 #define __MINGW32_MAJOR_VERSION 2
3333 #define __MINGW32_MINOR_VERSION 0
3434
35−#define __MSVCRT__
36−#define __MINGW_IMPORT extern
35+#define __MSVCRT__ 1
36+#define __MINGW_IMPORT extern
3737 #define _CRTIMP
38−#define __CRT_INLINE extern __inline__
38+#define __CRT_INLINE extern __inline__
3939
40−#define _WIN32
41−#define WIN32
40+#define WIN32 1
4241
4342 #ifndef _WINT_T
4443 #define _WINT_T
@@ -46,7 +45,8 @@ typedef unsigned int wint_t; · 1 unmodified line
4645 #endif
4746
4847 /* for winapi */
49−#define NONAMELESSUNION
48+#define _ANONYMOUS_UNION
49+#define _ANONYMOUS_STRUCT
5050 #define DECLSPEC_NORETURN
5151 #define WIN32_LEAN_AND_MEAN
5252 #define DECLARE_STDCALL_P(type) __stdcall type
win32/readme.txt+41−53
@@ -1,37 +1,34 @@
11
2− TinyCC-PE
3− ---------
4−
5− TinyCC (aka TCC) is a small but hyperfast C compiler,
6− written by Fabrice Bellard,
7−
2+ TinyCC
3+ ======
84
9− TinyCC-PE is the TinyCC compiler with an extension to
10− write PE executables for MS-Windows.
5+ This file contains some additional information for usage of TinyCC
6+ under MS-Windows:
117
128
13− Features:
14− ---------
9+ Overview:
10+ ---------
11+ TinyCC (aka TCC) is a small but hyperfast C compiler, written by
12+ Fabrice Bellard.
1513
16− TinyCC-PE can produce console applications, native windows
17− GUI programs and DLL's.
14+ TinyCC for MS-Windows can produce console applications, native
15+ windows GUI programs and DLL's.
1816
19− Most of the features pointed out by Fabrice Bellard for the
20− original version are still valid, i.e:
17+ The package with under 300kb includes a complete C-compiler with
18+ header files and basic system library support.
2119
22− - SMALL! The package with ~400kb includes a complete C-compiler
23− with header files for console and GUI applications.
20+ With the -run switch you can run C-sources without any linking
21+ directly from the command line.
2422
25− - With the -run switch you can run C-sources without any
26− linking directly from the command line.
23+ TinyCC can be used as dynamic code generator library in your own
24+ program.
2725
28− - TCC can of course compile itself.
26+ TinyCC can of course compile itself.
2927
3028
3129 Compilation: (omit that if you use the binary ZIP package)
3230 ------------
33−
34− You must use the MinGW and MSYS tools available at
31+ You can use the MinGW and MSYS tools available at
3532 http://www.mingw.org to compile TCC for Windows. Untar the TCC
3633 archive and type in the MSYS shell:
3734
@@ -48,13 +45,14 @@ · 10 unmodified lines
4845
4946 Installation: (from the binary ZIP package)
5047 -------------
51−
5248 Just unzip the package to a directory anywhere on your computer.
49+
50+ The binary package does not include libtcc. If you want tcc as
51+ dynamic code generator, please use the source code distribution.
5352
5453
5554 Examples:
5655 ---------
57−
5856 For the 'Fibonacci' console example type from the command line:
5957
6058 tcc examples\fib.c
@@ -69,25 +67,22 @@ · 8 unmodified lines
6967 tcc examples\hello_dll.c examples\dll.def
7068
7169
72− Import Definitions:
73− -------------------
74−
75− TinyCC-PE searches and reads import definition files similar
76− to libraries.
70+ Import Definition Files:
71+ ------------------------
72+ To link with Windows system DLLs, TinyCC uses import definition
73+ files (.def) instead of libraries.
7774
78− The included 'tiny_impdef' program may be used to make .def files
79− for any DLL, e.g for an 'opengl32.def':
75+ The included 'tiny_impdef' program may be used to make additional
76+ .def files for any DLL. For example:
8077
8178 tiny_impdef.exe opengl32.dll
8279
83− or to the same effect:
84−
85− tcc -run tiny_impdef.c opengl32.dll
80+ To use it, put the opengl32.def file into the tcc/lib directory,
81+ and specify -lopengl32 at the tcc commandline.
8682
8783
8884 Resource Files:
8985 ---------------
90−
9186 TinyCC-PE can now link windows resources in coff format as generated
9287 by MINGW's windres.exe. For example:
9388
@@ -95,32 +90,25 @@ · 1 unmodified line
9590 tcc app.c appres.o -o app.exe
9691
9792
98− Header Files:
99− -------------
100−
101− The system header files, except '_mingw.h', are from the
102− 3.7 mingw distribution. See also: http://www.mingw.org/
103−
104−
105− Compile TCC:
106− ------------
93+ Tiny Libmaker:
94+ --------------
95+ The included tiny_libmaker tool by Timovj Lahde can be used as
96+ 'ar' replacement to make a library from several object files.
10797
108− With TCC itself just say:
10998
110− tcc src\tcc.c -o tcc.new.exe
111−
112− Other compilers like mingw-gcc or msvc work as well.
113− To make libtcc1.a, you need 'ar' from the mingw binutils.
99+ Header Files:
100+ -------------
101+ The system header files (except _mingw.h) are from the mingw
102+ distribution (http://www.mingw.org/).
114103
115104
116105 Documentation and License:
117106 --------------------------
118−
119107 TCC is distributed under the GNU Lesser General Public License
120108 (see COPYING file).
121109
122− Please read the original tcc-doc.html to have all the features
123− of TCC. Also visit: http://fabrice.bellard.free.fr/tcc/
110+ Please read tcc-doc.html to have all the features of TCC. Also
111+ visit: http://fabrice.bellard.free.fr/tcc/
124112
125− --
126− grischka@users.sourceforge.net
113+
114+ -- grischka@users.sourceforge.net
win32/tools/tiny_impdef.crenamed from tiny_impdef.c+0−0

File renamed without changes.

win32/tools/tiny_libmaker.c+319−0
@@ -0,0 +1,319 @@
1+/*
2+ * This program is for making libtcc1.a without ar
3+ * tiny_libmaker - tiny elf lib maker
4+ * usage: tiny_libmaker [lib] files...
5+ * Copyright (c) 2007 Timppa
6+ *
7+ * This program is free software but WITHOUT ANY WARRANTY
8+ */
9+#include <stdio.h>
10+#include <stdlib.h>
11+#include <string.h>
12+
13+/* #include "ar-elf.h" */
14+/* "ar-elf.h" */
15+/* ELF_v1.2.pdf */
16+typedef unsigned short int Elf32_Half;
17+typedef int Elf32_Sword;
18+typedef unsigned int Elf32_Word;
19+typedef unsigned int Elf32_Addr;
20+typedef unsigned int Elf32_Off;
21+typedef unsigned short int Elf32_Section;
22+
23+#define EI_NIDENT 16
24+typedef struct {
25+ unsigned char e_ident[EI_NIDENT];
26+ Elf32_Half e_type;
27+ Elf32_Half e_machine;
28+ Elf32_Word e_version;
29+ Elf32_Addr e_entry;
30+ Elf32_Off e_phoff;
31+ Elf32_Off e_shoff;
32+ Elf32_Word e_flags;
33+ Elf32_Half e_ehsize;
34+ Elf32_Half e_phentsize;
35+ Elf32_Half e_phnum;
36+ Elf32_Half e_shentsize;
37+ Elf32_Half e_shnum;
38+ Elf32_Half e_shstrndx;
39+} Elf32_Ehdr;
40+
41+typedef struct {
42+ Elf32_Word sh_name;
43+ Elf32_Word sh_type;
44+ Elf32_Word sh_flags;
45+ Elf32_Addr sh_addr;
46+ Elf32_Off sh_offset;
47+ Elf32_Word sh_size;
48+ Elf32_Word sh_link;
49+ Elf32_Word sh_info;
50+ Elf32_Word sh_addralign;
51+ Elf32_Word sh_entsize;
52+} Elf32_Shdr;
53+
54+#define SHT_NULL 0
55+#define SHT_PROGBITS 1
56+#define SHT_SYMTAB 2
57+#define SHT_STRTAB 3
58+#define SHT_RELA 4
59+#define SHT_HASH 5
60+#define SHT_DYNAMIC 6
61+#define SHT_NOTE 7
62+#define SHT_NOBITS 8
63+#define SHT_REL 9
64+#define SHT_SHLIB 10
65+#define SHT_DYNSYM 11
66+
67+typedef struct {
68+ Elf32_Word st_name;
69+ Elf32_Addr st_value;
70+ Elf32_Word st_size;
71+ unsigned char st_info;
72+ unsigned char st_other;
73+ Elf32_Half st_shndx;
74+} Elf32_Sym;
75+
76+#define ELF32_ST_BIND(i) ((i)>>4)
77+#define ELF32_ST_TYPE(i) ((i)&0xf)
78+#define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
79+
80+#define STT_NOTYPE 0
81+#define STT_OBJECT 1
82+#define STT_FUNC 2
83+#define STT_SECTION 3
84+#define STT_FILE 4
85+#define STT_LOPROC 13
86+#define STT_HIPROC 15
87+
88+#define STB_LOCAL 0
89+#define STB_GLOBAL 1
90+#define STB_WEAK 2
91+#define STB_LOPROC 13
92+#define STB_HIPROC 15
93+
94+typedef struct {
95+ Elf32_Word p_type;
96+ Elf32_Off p_offset;
97+ Elf32_Addr p_vaddr;
98+ Elf32_Addr p_paddr;
99+ Elf32_Word p_filesz;
100+ Elf32_Word p_memsz;
101+ Elf32_Word p_flags;
102+ Elf32_Word p_align;
103+} Elf32_Phdr;
104+/* "ar-elf.h" ends */
105+
106+#define ARMAG "!<arch>\n"
107+#define ARFMAG "`\n"
108+
109+typedef struct ArHdr {
110+ char ar_name[16];
111+ char ar_date[12];
112+ char ar_uid[6];
113+ char ar_gid[6];
114+ char ar_mode[8];
115+ char ar_size[10];
116+ char ar_fmag[2];
117+} ArHdr;
118+
119+
120+unsigned long le2belong(unsigned long ul) {
121+ return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) +
122+ ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8);
123+}
124+
125+ArHdr arhdr = {
126+ "/ ",
127+ " ",
128+ "0 ",
129+ "0 ",
130+ "0 ",
131+ " ",
132+ ARFMAG
133+ };
134+
135+ArHdr arhdro = {
136+ " ",
137+ " ",
138+ "0 ",
139+ "0 ",
140+ "0 ",
141+ " ",
142+ ARFMAG
143+ };
144+
145+int main(int argc, char **argv)
146+{
147+ FILE *fi, *fh, *fo;
148+ Elf32_Ehdr *ehdr;
149+ Elf32_Shdr *shdr;
150+ Elf32_Sym *sym;
151+ int i, fsize, iarg;
152+ char *buf, *shstr, *symtab = NULL, *strtab = NULL;
153+ int symtabsize = 0, strtabsize = 0;
154+ char *anames = NULL;
155+ int *afpos = NULL;
156+ int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs;
157+ char afile[260], tfile[260], stmp[20];
158+
159+
160+ strcpy(afile, "ar_test.a");
161+ iarg = 1;
162+
163+ if (argc < 2)
164+ {
165+ printf("usage: tiny_libmaker [lib] file...\n");
166+ return 1;
167+ }
168+ for (i=1; i<argc; i++) {
169+ istrlen = strlen(argv[i]);
170+ if (argv[i][istrlen-2] == '.') {
171+ if(argv[i][istrlen-1] == 'a')
172+ strcpy(afile, argv[i]);
173+ else if(argv[i][istrlen-1] == 'o') {
174+ iarg = i;
175+ break;
176+ }
177+ }
178+ }
179+
180+ //tfile[0] = '.'; tfile[1] = '/';
181+ //if (tmpnam(&tfile[2])) {
182+ strcpy(tfile, "./XXXXXX");
183+ if (mktemp(tfile))
184+ {
185+ if ((fo = fopen(tfile, "wb+")) == NULL)
186+ {
187+ fprintf(stderr, "Can't open file %s \n", tfile);
188+ return 2;
189+ }
190+ }
191+
192+/*
193+ if ((fo = tmpfile()) == NULL)
194+ {
195+ fprintf(stderr, "Can't open temporary file \n");
196+ return 2;
197+ }
198+*/
199+ if ((fh = fopen(afile, "wb")) == NULL)
200+ {
201+ fprintf(stderr, "Can't open file %s \n", afile);
202+ remove(tfile);
203+ return 2;
204+ }
205+
206+ funcmax = 1000;
207+ afpos = realloc(NULL, funcmax); // 250 func
208+ memcpy(&arhdro.ar_mode, "100666", 6);
209+
210+ //iarg = 1;
211+ while (iarg < argc)
212+ {
213+ if (!strcmp(argv[iarg], "rcs")) {
214+ iarg++;
215+ continue;
216+ }
217+ if ((fi = fopen(argv[iarg], "rb")) == NULL)
218+ {
219+ fprintf(stderr, "Can't open file %s \n", argv[iarg]);
220+ remove(tfile);
221+ return 2;
222+ }
223+ fseek(fi, 0, SEEK_END);
224+ fsize = ftell(fi);
225+ fseek(fi, 0, SEEK_SET);
226+ buf = malloc(fsize + 1);
227+ fread(buf, fsize, 1, fi);
228+ fclose(fi);
229+
230+ printf("%s:\n", argv[iarg]);
231+ // elf header
232+ ehdr = (Elf32_Ehdr *)buf;
233+ shdr = (Elf32_Shdr *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
234+ shstr = (char *)(buf + shdr->sh_offset);
235+ for (i = 0; i < ehdr->e_shnum; i++)
236+ {
237+ shdr = (Elf32_Shdr *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
238+ if (!shdr->sh_offset) continue;
239+ if (shdr->sh_type == SHT_SYMTAB)
240+ {
241+ symtab = (char *)(buf + shdr->sh_offset);
242+ symtabsize = shdr->sh_size;
243+ }
244+ if (shdr->sh_type == SHT_STRTAB)
245+ {
246+ if (!strcmp(shstr + shdr->sh_name, ".strtab"))
247+ {
248+ strtab = (char *)(buf + shdr->sh_offset);
249+ strtabsize = shdr->sh_size;
250+ }
251+ }
252+ }
253+
254+ if (symtab && symtabsize)
255+ {
256+ int nsym = symtabsize / sizeof(Elf32_Sym);
257+ //printf("symtab: info size shndx name\n");
258+ for (i = 1; i < nsym; i++)
259+ {
260+ sym = (Elf32_Sym *) (symtab + i * sizeof(Elf32_Sym));
261+ if (sym->st_shndx && (sym->st_info == 0x11 || sym->st_info == 0x12)) {
262+ //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
263+ istrlen = strlen(strtab + sym->st_name)+1;
264+ anames = realloc(anames, strpos+istrlen);
265+ strcpy(anames + strpos, strtab + sym->st_name);
266+ strpos += istrlen;
267+ if (funccnt >= funcmax) {
268+ afpos = realloc(NULL, funcmax+1000); // 250 func more
269+ funcmax += 1000;
270+ }
271+ afpos[++funccnt] = fpos;
272+ }
273+ }
274+ }
275+ memset(&arhdro.ar_name, ' ', sizeof(arhdr.ar_name));
276+ strcpy(arhdro.ar_name, argv[iarg]);
277+ arhdro.ar_name[strlen(argv[iarg])] = '/';
278+ sprintf(stmp, "%-10d", fsize);
279+ memcpy(&arhdro.ar_size, stmp, 10);
280+ fwrite(&arhdro, sizeof(arhdro), 1, fo);
281+ fwrite(buf, fsize, 1, fo);
282+ free(buf);
283+ iarg++;
284+ fpos += (fsize + sizeof(arhdro));
285+ }
286+ hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
287+ if ((hofs & 1)) { // align
288+ hofs++;
289+ fpos = 1;
290+ } else fpos = 0;
291+ // write header
292+ fwrite("!<arch>\n", 8, 1, fh);
293+ sprintf(stmp, "%-10d", strpos + (funccnt+1) * sizeof(int));
294+ memcpy(&arhdr.ar_size, stmp, 10);
295+ fwrite(&arhdr, sizeof(arhdr), 1, fh);
296+ afpos[0] = le2belong(funccnt);
297+ for (i=1; i<=funccnt; i++) {
298+ afpos[i] = le2belong(afpos[i] + hofs);
299+ }
300+ fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
301+ fwrite(anames, strpos, 1, fh);
302+ if (fpos) fwrite("", 1, 1, fh);
303+ // write objects
304+ fseek(fo, 0, SEEK_END);
305+ fsize = ftell(fo);
306+ fseek(fo, 0, SEEK_SET);
307+ buf = malloc(fsize + 1);
308+ fread(buf, fsize, 1, fo);
309+ fclose(fo);
310+ fwrite(buf, fsize, 1, fh);
311+ fclose(fh);
312+ free(buf);
313+ if (anames)
314+ free(anames);
315+ if (afpos)
316+ free(afpos);
317+ remove(tfile);
318+ return 0;
319+}

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