tiny_libmaker: fix a comment
[tinycc.git] / win32 / tools / tiny_libmaker.c
bloba6949159ef82279dc73d9e5d91477fcc523b873b
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 #include "../../elf.h"
26 #ifdef TCC_TARGET_X86_64
27 # define ELFCLASSW ELFCLASS64
28 # define ElfW(type) Elf##64##_##type
29 # define ELFW(type) ELF##64##_##type
30 #else
31 # define ELFCLASSW ELFCLASS32
32 # define ElfW(type) Elf##32##_##type
33 # define ELFW(type) ELF##32##_##type
34 #endif
36 #define ARMAG "!<arch>\n"
37 #define ARFMAG "`\n"
39 typedef struct ArHdr {
40 char ar_name[16];
41 char ar_date[12];
42 char ar_uid[6];
43 char ar_gid[6];
44 char ar_mode[8];
45 char ar_size[10];
46 char ar_fmag[2];
47 } ArHdr;
49 unsigned long le2belong(unsigned long ul) {
50 return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) +
51 ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8);
54 ArHdr arhdr = {
55 "/ ",
56 " ",
57 "0 ",
58 "0 ",
59 "0 ",
60 " ",
61 ARFMAG
64 ArHdr arhdro = {
65 " ",
66 " ",
67 "0 ",
68 "0 ",
69 "0 ",
70 " ",
71 ARFMAG
74 /* Returns 1 if s contains any of the chars of list, else 0 */
75 int contains_any(const char *s, const char *list) {
76 const char *l;
77 for (; *s; s++) {
78 for (l = list; *l; l++) {
79 if (*s == *l)
80 return 1;
83 return 0;
86 int usage(int ret) {
87 fprintf(stderr, "usage: tiny_libmaker [rcsv] lib file...\n");
88 fprintf(stderr, "Always creates a new lib. [abdioptxN] are explicitly rejected.\n");
89 return ret;
92 int main(int argc, char **argv)
94 FILE *fi, *fh = NULL, *fo = NULL;
95 ElfW(Ehdr) *ehdr;
96 ElfW(Shdr) *shdr;
97 ElfW(Sym) *sym;
98 int i, fsize, i_lib, i_obj;
99 char *buf, *shstr, *symtab = NULL, *strtab = NULL;
100 int symtabsize = 0;//, strtabsize = 0;
101 char *anames = NULL;
102 int *afpos = NULL;
103 int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs;
104 char tfile[260], stmp[20];
105 char *file, *name;
106 int ret = 2;
107 char *ops_conflict = "habdioptxN"; // unsupported but destructive if ignored.
108 int verbose = 0;
110 i_lib = 0; i_obj = 0; // will hold the index of the lib and first obj
111 for (i = 1; i < argc; i++) {
112 const char *a = argv[i];
113 if (*a == '-' && strstr(a, "."))
114 return usage(1); // -x.y is always invalid (same as gnu ar)
115 if ((*a == '-') || (i == 1 && !strstr(a, "."))) { // options argument
116 if (contains_any(a, ops_conflict))
117 return usage(1);
118 if (strstr(a, "v"))
119 verbose = 1;
120 } else { // lib or obj files: don't abort - keep validating all args.
121 if (!i_lib) // first file is the lib
122 i_lib = i;
123 else if (!i_obj) // second file is the first obj
124 i_obj = i;
127 if (!i_obj) // i_obj implies also i_lib. we require both.
128 return usage(1);
130 if ((fh = fopen(argv[i_lib], "wb")) == NULL)
132 fprintf(stderr, "Can't open file %s \n", argv[i_lib]);
133 goto the_end;
136 sprintf(tfile, "%s.tmp", argv[i_lib]);
137 if ((fo = fopen(tfile, "wb+")) == NULL)
139 fprintf(stderr, "Can't create temporary file %s\n", tfile);
140 goto the_end;
143 funcmax = 250;
144 afpos = realloc(NULL, funcmax * sizeof *afpos); // 250 func
145 memcpy(&arhdro.ar_mode, "100666", 6);
147 // i_obj = first input object file
148 while (i_obj < argc)
150 if (*argv[i_obj] == '-') { // by now, all options start with '-'
151 i_obj++;
152 continue;
154 if (verbose)
155 printf("a - %s\n", argv[i_obj]);
157 if ((fi = fopen(argv[i_obj], "rb")) == NULL)
159 fprintf(stderr, "Can't open file %s \n", argv[i_obj]);
160 goto the_end;
162 fseek(fi, 0, SEEK_END);
163 fsize = ftell(fi);
164 fseek(fi, 0, SEEK_SET);
165 buf = malloc(fsize + 1);
166 fread(buf, fsize, 1, fi);
167 fclose(fi);
169 // elf header
170 ehdr = (ElfW(Ehdr) *)buf;
171 if (ehdr->e_ident[4] != ELFCLASSW)
173 fprintf(stderr, "Unsupported Elf Class: %s\n", argv[i_obj]);
174 goto the_end;
177 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
178 shstr = (char *)(buf + shdr->sh_offset);
179 for (i = 0; i < ehdr->e_shnum; i++)
181 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
182 if (!shdr->sh_offset)
183 continue;
184 if (shdr->sh_type == SHT_SYMTAB)
186 symtab = (char *)(buf + shdr->sh_offset);
187 symtabsize = shdr->sh_size;
189 if (shdr->sh_type == SHT_STRTAB)
191 if (!strcmp(shstr + shdr->sh_name, ".strtab"))
193 strtab = (char *)(buf + shdr->sh_offset);
194 //strtabsize = shdr->sh_size;
199 if (symtab && symtabsize)
201 int nsym = symtabsize / sizeof(ElfW(Sym));
202 //printf("symtab: info size shndx name\n");
203 for (i = 1; i < nsym; i++)
205 sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
206 if (sym->st_shndx &&
207 (sym->st_info == 0x10
208 || sym->st_info == 0x11
209 || sym->st_info == 0x12
210 )) {
211 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
212 istrlen = strlen(strtab + sym->st_name)+1;
213 anames = realloc(anames, strpos+istrlen);
214 strcpy(anames + strpos, strtab + sym->st_name);
215 strpos += istrlen;
216 if (++funccnt >= funcmax) {
217 funcmax += 250;
218 afpos = realloc(afpos, funcmax * sizeof *afpos); // 250 func more
220 afpos[funccnt] = fpos;
225 file = argv[i_obj];
226 for (name = strchr(file, 0);
227 name > file && name[-1] != '/' && name[-1] != '\\';
228 --name);
229 istrlen = strlen(name);
230 if (istrlen >= sizeof(arhdro.ar_name))
231 istrlen = sizeof(arhdro.ar_name) - 1;
232 memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
233 memcpy(arhdro.ar_name, name, istrlen);
234 arhdro.ar_name[istrlen] = '/';
235 sprintf(stmp, "%-10d", fsize);
236 memcpy(&arhdro.ar_size, stmp, 10);
237 fwrite(&arhdro, sizeof(arhdro), 1, fo);
238 fwrite(buf, fsize, 1, fo);
239 free(buf);
240 i_obj++;
241 fpos += (fsize + sizeof(arhdro));
243 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
244 fpos = 0;
245 if ((hofs & 1)) // align
246 hofs++, fpos = 1;
247 // write header
248 fwrite("!<arch>\n", 8, 1, fh);
249 sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
250 memcpy(&arhdr.ar_size, stmp, 10);
251 fwrite(&arhdr, sizeof(arhdr), 1, fh);
252 afpos[0] = le2belong(funccnt);
253 for (i=1; i<=funccnt; i++)
254 afpos[i] = le2belong(afpos[i] + hofs);
255 fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
256 fwrite(anames, strpos, 1, fh);
257 if (fpos)
258 fwrite("", 1, 1, fh);
259 // write objects
260 fseek(fo, 0, SEEK_END);
261 fsize = ftell(fo);
262 fseek(fo, 0, SEEK_SET);
263 buf = malloc(fsize + 1);
264 fread(buf, fsize, 1, fo);
265 fwrite(buf, fsize, 1, fh);
266 free(buf);
267 ret = 0;
268 the_end:
269 if (anames)
270 free(anames);
271 if (afpos)
272 free(afpos);
273 if (fh)
274 fclose(fh);
275 if (fo)
276 fclose(fo), remove(tfile);
277 return ret;