Honor CC when testing for -Wno-unused-result
[tinycc.git] / win32 / tools / tiny_libmaker.c
blob42373d837aeb7e05b7bae955d41cfe622e6946da
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
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #ifdef _WIN32
25 #include <io.h> /* for mktemp */
26 #endif
28 #include "../../elf.h"
30 #ifdef TCC_TARGET_X86_64
31 # define ELFCLASSW ELFCLASS64
32 # define ElfW(type) Elf##64##_##type
33 # define ELFW(type) ELF##64##_##type
34 #else
35 # define ELFCLASSW ELFCLASS32
36 # define ElfW(type) Elf##32##_##type
37 # define ELFW(type) ELF##32##_##type
38 #endif
40 #define ARMAG "!<arch>\n"
41 #define ARFMAG "`\n"
43 typedef struct ArHdr {
44 char ar_name[16];
45 char ar_date[12];
46 char ar_uid[6];
47 char ar_gid[6];
48 char ar_mode[8];
49 char ar_size[10];
50 char ar_fmag[2];
51 } ArHdr;
53 unsigned long le2belong(unsigned long ul) {
54 return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) +
55 ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8);
58 ArHdr arhdr = {
59 "/ ",
60 " ",
61 "0 ",
62 "0 ",
63 "0 ",
64 " ",
65 ARFMAG
68 ArHdr arhdro = {
69 " ",
70 " ",
71 "0 ",
72 "0 ",
73 "0 ",
74 " ",
75 ARFMAG
78 int main(int argc, char **argv)
80 FILE *fi, *fh, *fo;
81 ElfW(Ehdr) *ehdr;
82 ElfW(Shdr) *shdr;
83 ElfW(Sym) *sym;
84 int i, fsize, iarg;
85 char *buf, *shstr, *symtab = NULL, *strtab = NULL;
86 int symtabsize = 0, strtabsize = 0;
87 char *anames = NULL;
88 int *afpos = NULL;
89 int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs;
90 char afile[260], tfile[260], stmp[20];
91 char *file, *name;
94 strcpy(afile, "ar_test.a");
95 iarg = 1;
97 if (argc < 2)
99 printf("usage: tiny_libmaker [lib] file...\n");
100 return 1;
102 for (i=1; i<argc; i++) {
103 istrlen = strlen(argv[i]);
104 if (argv[i][istrlen-2] == '.') {
105 if(argv[i][istrlen-1] == 'a')
106 strcpy(afile, argv[i]);
107 else if(argv[i][istrlen-1] == 'o') {
108 iarg = i;
109 break;
114 strcpy(tfile, "./XXXXXX");
115 if (!mktemp(tfile) || (fo = fopen(tfile, "wb+")) == NULL)
117 fprintf(stderr, "Can't open temporary file %s\n", tfile);
118 return 2;
121 if ((fh = fopen(afile, "wb")) == NULL)
123 fprintf(stderr, "Can't open file %s \n", afile);
124 fclose(fo);
125 remove(tfile);
126 return 2;
129 funcmax = 250;
130 afpos = realloc(NULL, funcmax * sizeof *afpos); // 250 func
131 memcpy(&arhdro.ar_mode, "100666", 6);
133 //iarg = 1;
134 while (iarg < argc)
136 if (!strcmp(argv[iarg], "rcs")) {
137 iarg++;
138 continue;
140 if ((fi = fopen(argv[iarg], "rb")) == NULL)
142 fprintf(stderr, "Can't open file %s \n", argv[iarg]);
143 fclose(fo);
144 remove(tfile);
145 return 2;
147 fseek(fi, 0, SEEK_END);
148 fsize = ftell(fi);
149 fseek(fi, 0, SEEK_SET);
150 buf = malloc(fsize + 1);
151 fread(buf, fsize, 1, fi);
152 fclose(fi);
154 //printf("%s:\n", argv[iarg]);
155 // elf header
156 ehdr = (ElfW(Ehdr) *)buf;
157 if (ehdr->e_ident[4] != ELFCLASSW)
159 fprintf(stderr, "Unsupported Elf Class: %s\n", argv[iarg]);
160 fclose(fo);
161 remove(tfile);
162 return 2;
165 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
166 shstr = (char *)(buf + shdr->sh_offset);
167 for (i = 0; i < ehdr->e_shnum; i++)
169 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
170 if (!shdr->sh_offset) continue;
171 if (shdr->sh_type == SHT_SYMTAB)
173 symtab = (char *)(buf + shdr->sh_offset);
174 symtabsize = shdr->sh_size;
176 if (shdr->sh_type == SHT_STRTAB)
178 if (!strcmp(shstr + shdr->sh_name, ".strtab"))
180 strtab = (char *)(buf + shdr->sh_offset);
181 strtabsize = shdr->sh_size;
186 if (symtab && symtabsize)
188 int nsym = symtabsize / sizeof(ElfW(Sym));
189 //printf("symtab: info size shndx name\n");
190 for (i = 1; i < nsym; i++)
192 sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
193 if (sym->st_shndx &&
194 (sym->st_info == 0x10
195 || sym->st_info == 0x11
196 || sym->st_info == 0x12
197 )) {
198 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
199 istrlen = strlen(strtab + sym->st_name)+1;
200 anames = realloc(anames, strpos+istrlen);
201 strcpy(anames + strpos, strtab + sym->st_name);
202 strpos += istrlen;
203 if (++funccnt >= funcmax) {
204 funcmax += 250;
205 afpos = realloc(afpos, funcmax * sizeof *afpos); // 250 func more
207 afpos[funccnt] = fpos;
212 file = argv[iarg];
213 for (name = strchr(file, 0);
214 name > file && name[-1] != '/' && name[-1] != '\\';
215 --name);
216 istrlen = strlen(name);
217 if (istrlen >= sizeof(arhdro.ar_name))
218 istrlen = sizeof(arhdro.ar_name) - 1;
219 memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
220 memcpy(arhdro.ar_name, name, istrlen);
221 arhdro.ar_name[istrlen] = '/';
223 sprintf(stmp, "%-10d", fsize);
224 memcpy(&arhdro.ar_size, stmp, 10);
225 fwrite(&arhdro, sizeof(arhdro), 1, fo);
226 fwrite(buf, fsize, 1, fo);
227 free(buf);
228 iarg++;
229 fpos += (fsize + sizeof(arhdro));
231 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
232 if ((hofs & 1)) { // align
233 hofs++;
234 fpos = 1;
235 } else fpos = 0;
236 // write header
237 fwrite("!<arch>\n", 8, 1, fh);
238 sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
239 memcpy(&arhdr.ar_size, stmp, 10);
240 fwrite(&arhdr, sizeof(arhdr), 1, fh);
241 afpos[0] = le2belong(funccnt);
242 for (i=1; i<=funccnt; i++) {
243 afpos[i] = le2belong(afpos[i] + hofs);
245 fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
246 fwrite(anames, strpos, 1, fh);
247 if (fpos) fwrite("", 1, 1, fh);
248 // write objects
249 fseek(fo, 0, SEEK_END);
250 fsize = ftell(fo);
251 fseek(fo, 0, SEEK_SET);
252 buf = malloc(fsize + 1);
253 fread(buf, fsize, 1, fo);
254 fclose(fo);
255 fwrite(buf, fsize, 1, fh);
256 fclose(fh);
257 free(buf);
258 if (anames)
259 free(anames);
260 if (afpos)
261 free(afpos);
262 remove(tfile);
263 return 0;