Remove some unused-parameter lint
[tinycc.git] / tcctools.c
blob55050adae3eaf00b7da97e4bf14440ba9c24a1a7
1 /* -------------------------------------------------------------- */
2 /*
3 * TCC - Tiny C Compiler
5 * tcctools.c - extra tools and and -m32/64 support
7 */
9 /* -------------------------------------------------------------- */
11 * This program is for making libtcc1.a without ar
12 * tiny_libmaker - tiny elf lib maker
13 * usage: tiny_libmaker [lib] files...
14 * Copyright (c) 2007 Timppa
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include "tcc.h"
33 //#define ARMAG "!<arch>\n"
34 #define ARFMAG "`\n"
36 typedef struct {
37 char ar_name[16];
38 char ar_date[12];
39 char ar_uid[6];
40 char ar_gid[6];
41 char ar_mode[8];
42 char ar_size[10];
43 char ar_fmag[2];
44 } ArHdr;
46 static unsigned long le2belong(unsigned long ul) {
47 return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) +
48 ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8);
51 /* Returns 1 if s contains any of the chars of list, else 0 */
52 static int contains_any(const char *s, const char *list) {
53 const char *l;
54 for (; *s; s++) {
55 for (l = list; *l; l++) {
56 if (*s == *l)
57 return 1;
60 return 0;
63 static int ar_usage(int ret) {
64 fprintf(stderr, "usage: tcc -ar [rcsv] lib file...\n");
65 fprintf(stderr, "create library ([abdioptxN] not supported).\n");
66 return ret;
69 ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
71 static ArHdr arhdr = {
72 "/ ",
73 " ",
74 "0 ",
75 "0 ",
76 "0 ",
77 " ",
78 ARFMAG
81 static ArHdr arhdro = {
82 " ",
83 " ",
84 "0 ",
85 "0 ",
86 "0 ",
87 " ",
88 ARFMAG
91 FILE *fi, *fh = NULL, *fo = NULL;
92 ElfW(Ehdr) *ehdr;
93 ElfW(Shdr) *shdr;
94 ElfW(Sym) *sym;
95 int i, fsize, i_lib, i_obj;
96 char *buf, *shstr, *symtab = NULL, *strtab = NULL;
97 int symtabsize = 0;//, strtabsize = 0;
98 char *anames = NULL;
99 int *afpos = NULL;
100 int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs;
101 char tfile[260], stmp[20];
102 char *file, *name;
103 int ret = 2;
104 const char *ops_conflict = "habdioptxN"; // unsupported but destructive if ignored.
105 int verbose = 0;
107 (void) s1; /* not used */
108 i_lib = 0; i_obj = 0; // will hold the index of the lib and first obj
109 for (i = 1; i < argc; i++) {
110 const char *a = argv[i];
111 if (*a == '-' && strstr(a, "."))
112 ret = 1; // -x.y is always invalid (same as gnu ar)
113 if ((*a == '-') || (i == 1 && !strstr(a, "."))) { // options argument
114 if (contains_any(a, ops_conflict))
115 ret = 1;
116 if (strstr(a, "v"))
117 verbose = 1;
118 } else { // lib or obj files: don't abort - keep validating all args.
119 if (!i_lib) // first file is the lib
120 i_lib = i;
121 else if (!i_obj) // second file is the first obj
122 i_obj = i;
126 if (!i_obj) // i_obj implies also i_lib. we require both.
127 ret = 1;
129 if (ret == 1)
130 return ar_usage(ret);
132 if ((fh = fopen(argv[i_lib], "wb")) == NULL)
134 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_lib]);
135 goto the_end;
138 sprintf(tfile, "%s.tmp", argv[i_lib]);
139 if ((fo = fopen(tfile, "wb+")) == NULL)
141 fprintf(stderr, "tcc: ar: can't create temporary file %s\n", tfile);
142 goto the_end;
145 funcmax = 250;
146 afpos = tcc_realloc(NULL, funcmax * sizeof *afpos); // 250 func
147 memcpy(&arhdro.ar_mode, "100666", 6);
149 // i_obj = first input object file
150 while (i_obj < argc)
152 if (*argv[i_obj] == '-') { // by now, all options start with '-'
153 i_obj++;
154 continue;
156 if ((fi = fopen(argv[i_obj], "rb")) == NULL) {
157 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_obj]);
158 goto the_end;
160 if (verbose)
161 printf("a - %s\n", argv[i_obj]);
163 fseek(fi, 0, SEEK_END);
164 fsize = ftell(fi);
165 fseek(fi, 0, SEEK_SET);
166 buf = tcc_malloc(fsize + 1);
167 fread(buf, fsize, 1, fi);
168 fclose(fi);
170 // elf header
171 ehdr = (ElfW(Ehdr) *)buf;
172 if (ehdr->e_ident[4] != ELFCLASSW)
174 fprintf(stderr, "tcc: ar: Unsupported Elf Class: %s\n", argv[i_obj]);
175 goto the_end;
178 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
179 shstr = (char *)(buf + shdr->sh_offset);
180 for (i = 0; i < ehdr->e_shnum; i++)
182 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
183 if (!shdr->sh_offset)
184 continue;
185 if (shdr->sh_type == SHT_SYMTAB)
187 symtab = (char *)(buf + shdr->sh_offset);
188 symtabsize = shdr->sh_size;
190 if (shdr->sh_type == SHT_STRTAB)
192 if (!strcmp(shstr + shdr->sh_name, ".strtab"))
194 strtab = (char *)(buf + shdr->sh_offset);
195 //strtabsize = shdr->sh_size;
200 if (symtab && symtabsize)
202 int nsym = symtabsize / sizeof(ElfW(Sym));
203 //printf("symtab: info size shndx name\n");
204 for (i = 1; i < nsym; i++)
206 sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
207 if (sym->st_shndx &&
208 (sym->st_info == 0x10
209 || sym->st_info == 0x11
210 || sym->st_info == 0x12
211 )) {
212 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
213 istrlen = strlen(strtab + sym->st_name)+1;
214 anames = tcc_realloc(anames, strpos+istrlen);
215 strcpy(anames + strpos, strtab + sym->st_name);
216 strpos += istrlen;
217 if (++funccnt >= funcmax) {
218 funcmax += 250;
219 afpos = tcc_realloc(afpos, funcmax * sizeof *afpos); // 250 func more
221 afpos[funccnt] = fpos;
226 file = argv[i_obj];
227 for (name = strchr(file, 0);
228 name > file && name[-1] != '/' && name[-1] != '\\';
229 --name);
230 istrlen = strlen(name);
231 if (istrlen >= sizeof(arhdro.ar_name))
232 istrlen = sizeof(arhdro.ar_name) - 1;
233 memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
234 memcpy(arhdro.ar_name, name, istrlen);
235 arhdro.ar_name[istrlen] = '/';
236 sprintf(stmp, "%-10d", fsize);
237 memcpy(&arhdro.ar_size, stmp, 10);
238 fwrite(&arhdro, sizeof(arhdro), 1, fo);
239 fwrite(buf, fsize, 1, fo);
240 tcc_free(buf);
241 i_obj++;
242 fpos += (fsize + sizeof(arhdro));
244 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
245 fpos = 0;
246 if ((hofs & 1)) // align
247 hofs++, fpos = 1;
248 // write header
249 fwrite("!<arch>\n", 8, 1, fh);
250 sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
251 memcpy(&arhdr.ar_size, stmp, 10);
252 fwrite(&arhdr, sizeof(arhdr), 1, fh);
253 afpos[0] = le2belong(funccnt);
254 for (i=1; i<=funccnt; i++)
255 afpos[i] = le2belong(afpos[i] + hofs);
256 fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
257 fwrite(anames, strpos, 1, fh);
258 if (fpos)
259 fwrite("", 1, 1, fh);
260 // write objects
261 fseek(fo, 0, SEEK_END);
262 fsize = ftell(fo);
263 fseek(fo, 0, SEEK_SET);
264 buf = tcc_malloc(fsize + 1);
265 fread(buf, fsize, 1, fo);
266 fwrite(buf, fsize, 1, fh);
267 tcc_free(buf);
268 ret = 0;
269 the_end:
270 if (anames)
271 tcc_free(anames);
272 if (afpos)
273 tcc_free(afpos);
274 if (fh)
275 fclose(fh);
276 if (fo)
277 fclose(fo), remove(tfile);
278 return ret;
281 /* -------------------------------------------------------------- */
283 * tiny_impdef creates an export definition file (.def) from a dll
284 * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
286 * Copyright (c) 2005,2007 grischka
288 * This program is free software; you can redistribute it and/or modify
289 * it under the terms of the GNU General Public License as published by
290 * the Free Software Foundation; either version 2 of the License, or
291 * (at your option) any later version.
293 * This program is distributed in the hope that it will be useful,
294 * but WITHOUT ANY WARRANTY; without even the implied warranty of
295 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
296 * GNU General Public License for more details.
298 * You should have received a copy of the GNU General Public License
299 * along with this program; if not, write to the Free Software
300 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
303 #ifdef TCC_TARGET_PE
305 ST_FUNC int tcc_tool_impdef(TCCState *s1, int argc, char **argv)
307 int ret, v, i;
308 char infile[260];
309 char outfile[260];
311 const char *file;
312 char *p, *q;
313 FILE *fp, *op;
315 #ifdef _WIN32
316 char path[260];
317 #endif
319 infile[0] = outfile[0] = 0;
320 fp = op = NULL;
321 ret = 1;
322 p = NULL;
323 v = 0;
325 (void) s1; /* not used */
326 for (i = 1; i < argc; ++i) {
327 const char *a = argv[i];
328 if ('-' == a[0]) {
329 if (0 == strcmp(a, "-v")) {
330 v = 1;
331 } else if (0 == strcmp(a, "-o")) {
332 if (++i == argc)
333 goto usage;
334 strcpy(outfile, argv[i]);
335 } else
336 goto usage;
337 } else if (0 == infile[0])
338 strcpy(infile, a);
339 else
340 goto usage;
343 if (0 == infile[0]) {
344 usage:
345 fprintf(stderr,
346 "usage: tcc -impdef library.dll [-v] [-o outputfile]\n"
347 "create export definition file (.def) from dll\n"
349 goto the_end;
352 if (0 == outfile[0]) {
353 strcpy(outfile, tcc_basename(infile));
354 q = strrchr(outfile, '.');
355 if (NULL == q)
356 q = strchr(outfile, 0);
357 strcpy(q, ".def");
360 file = infile;
361 #ifdef _WIN32
362 if (SearchPath(NULL, file, ".dll", sizeof path, path, NULL))
363 file = path;
364 #endif
365 ret = tcc_get_dllexports(file, &p);
366 if (ret || !p) {
367 fprintf(stderr, "tcc: impdef: %s '%s'\n",
368 ret == 32 ? "can't read symbols from 32bit" :
369 ret == 64 ? "can't read symbols from 64bit" :
370 ret == -1 ? "can't find file" :
371 ret == 0 ? "no symbols found in" :
372 "unknown file type", file);
373 ret = 1;
374 goto the_end;
377 if (v)
378 printf("-> %s\n", file);
380 op = fopen(outfile, "w");
381 if (NULL == op) {
382 fprintf(stderr, "tcc: impdef: could not create output file: %s\n", outfile);
383 goto the_end;
386 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", tcc_basename(file));
387 for (q = p, i = 0; *q; ++i) {
388 fprintf(op, "%s\n", q);
389 q += strlen(q) + 1;
392 if (v)
393 printf("<- %s (%d symbol%s)\n", outfile, i, "s" + (i<2));
395 ret = 0;
397 the_end:
398 /* cannot free memory received from tcc_get_dllexports
399 if it came from a dll */
400 /* if (p)
401 tcc_free(p); */
402 if (fp)
403 fclose(fp);
404 if (op)
405 fclose(op);
406 return ret;
409 #endif /* TCC_TARGET_PE */
411 /* -------------------------------------------------------------- */
413 * TCC - Tiny C Compiler
415 * Copyright (c) 2001-2004 Fabrice Bellard
417 * This library is free software; you can redistribute it and/or
418 * modify it under the terms of the GNU Lesser General Public
419 * License as published by the Free Software Foundation; either
420 * version 2 of the License, or (at your option) any later version.
422 * This library is distributed in the hope that it will be useful,
423 * but WITHOUT ANY WARRANTY; without even the implied warranty of
424 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
425 * Lesser General Public License for more details.
427 * You should have received a copy of the GNU Lesser General Public
428 * License along with this library; if not, write to the Free Software
429 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
432 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
434 #if !defined TCC_TARGET_I386 && !defined TCC_TARGET_X86_64
436 ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option)
438 (void) s; (void) argv; /* not used */
439 tcc_error("-m%d not implemented.", option);
442 #else
443 #ifdef _WIN32
444 #include <process.h>
446 static char *str_replace(const char *str, const char *p, const char *r)
448 const char *s, *s0;
449 char *d, *d0;
450 int sl, pl, rl;
452 sl = strlen(str);
453 pl = strlen(p);
454 rl = strlen(r);
455 for (d0 = NULL;; d0 = tcc_malloc(sl + 1)) {
456 for (d = d0, s = str; s0 = s, s = strstr(s, p), s; s += pl) {
457 if (d) {
458 memcpy(d, s0, sl = s - s0), d += sl;
459 memcpy(d, r, rl), d += rl;
460 } else
461 sl += rl - pl;
463 if (d) {
464 strcpy(d, s0);
465 return d0;
470 static int execvp_win32(const char *prog, char **argv)
472 int ret; char **p;
473 /* replace all " by \" */
474 for (p = argv; *p; ++p)
475 if (strchr(*p, '"'))
476 *p = str_replace(*p, "\"", "\\\"");
477 ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
478 if (-1 == ret)
479 return ret;
480 _cwait(&ret, ret, WAIT_CHILD);
481 exit(ret);
483 #define execvp execvp_win32
484 #endif /* _WIN32 */
486 ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int target)
488 char program[4096];
489 char *a0 = argv[0];
490 int prefix = tcc_basename(a0) - a0;
492 (void) s; /* not used */
493 snprintf(program, sizeof program,
494 "%.*s%s"
495 #ifdef TCC_TARGET_PE
496 "-win32"
497 #endif
498 "-tcc"
499 #ifdef _WIN32
500 ".exe"
501 #endif
502 , prefix, a0, target == 64 ? "x86_64" : "i386");
504 if (strcmp(a0, program))
505 execvp(argv[0] = program, argv);
506 tcc_error("could not run '%s'", program);
509 #endif /* TCC_TARGET_I386 && TCC_TARGET_X86_64 */
510 /* -------------------------------------------------------------- */
511 /* enable commandline wildcard expansion (tcc -o x.exe *.c) */
513 #ifdef _WIN32
514 int _CRT_glob = 1;
515 #ifndef _CRT_glob
516 int _dowildcard = 1;
517 #endif
518 #endif
520 /* -------------------------------------------------------------- */
521 /* generate xxx.d file */
523 ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename)
525 FILE *depout;
526 char buf[1024];
527 int i;
529 if (!filename) {
530 /* compute filename automatically: dir/file.o -> dir/file.d */
531 snprintf(buf, sizeof buf, "%.*s.d",
532 (int)(tcc_fileextension(target) - target), target);
533 filename = buf;
536 if (s->verbose)
537 printf("<- %s\n", filename);
539 /* XXX return err codes instead of error() ? */
540 depout = fopen(filename, "w");
541 if (!depout)
542 tcc_error("could not open '%s'", filename);
544 fprintf(depout, "%s: \\\n", target);
545 for (i=0; i<s->nb_target_deps; ++i)
546 fprintf(depout, " %s \\\n", s->target_deps[i]);
547 fprintf(depout, "\n");
548 fclose(depout);
551 /* -------------------------------------------------------------- */