As suggested by Herman, move comment to the next line otherwise it incorrectly handle...
[tinycc.git] / tcctools.c
blob4567b81a8e766b25d28214cb5d1250bc2ff0cc37
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 const ArHdr arhdr_init = {
72 "/ ",
73 " ",
74 "0 ",
75 "0 ",
76 "0 ",
77 " ",
78 ARFMAG
81 ArHdr arhdr = arhdr_init;
82 ArHdr arhdro = arhdr_init;
84 FILE *fi, *fh = NULL, *fo = NULL;
85 ElfW(Ehdr) *ehdr;
86 ElfW(Shdr) *shdr;
87 ElfW(Sym) *sym;
88 int i, fsize, i_lib, i_obj;
89 char *buf, *shstr, *symtab = NULL, *strtab = NULL;
90 int symtabsize = 0;//, strtabsize = 0;
91 char *anames = NULL;
92 int *afpos = NULL;
93 int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs;
94 char tfile[260], stmp[20];
95 char *file, *name;
96 int ret = 2;
97 const char *ops_conflict = "habdioptxN"; // unsupported but destructive if ignored.
98 int verbose = 0;
100 i_lib = 0; i_obj = 0; // will hold the index of the lib and first obj
101 for (i = 1; i < argc; i++) {
102 const char *a = argv[i];
103 if (*a == '-' && strstr(a, "."))
104 ret = 1; // -x.y is always invalid (same as gnu ar)
105 if ((*a == '-') || (i == 1 && !strstr(a, "."))) { // options argument
106 if (contains_any(a, ops_conflict))
107 ret = 1;
108 if (strstr(a, "v"))
109 verbose = 1;
110 } else { // lib or obj files: don't abort - keep validating all args.
111 if (!i_lib) // first file is the lib
112 i_lib = i;
113 else if (!i_obj) // second file is the first obj
114 i_obj = i;
118 if (!i_obj) // i_obj implies also i_lib. we require both.
119 ret = 1;
121 if (ret == 1)
122 return ar_usage(ret);
124 if ((fh = fopen(argv[i_lib], "wb")) == NULL)
126 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_lib]);
127 goto the_end;
130 sprintf(tfile, "%s.tmp", argv[i_lib]);
131 if ((fo = fopen(tfile, "wb+")) == NULL)
133 fprintf(stderr, "tcc: ar: can't create temporary file %s\n", tfile);
134 goto the_end;
137 funcmax = 250;
138 afpos = tcc_realloc(NULL, funcmax * sizeof *afpos); // 250 func
139 memcpy(&arhdro.ar_mode, "100666", 6);
141 // i_obj = first input object file
142 while (i_obj < argc)
144 if (*argv[i_obj] == '-') { // by now, all options start with '-'
145 i_obj++;
146 continue;
148 if ((fi = fopen(argv[i_obj], "rb")) == NULL) {
149 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_obj]);
150 goto the_end;
152 if (verbose)
153 printf("a - %s\n", argv[i_obj]);
155 fseek(fi, 0, SEEK_END);
156 fsize = ftell(fi);
157 fseek(fi, 0, SEEK_SET);
158 buf = tcc_malloc(fsize + 1);
159 fread(buf, fsize, 1, fi);
160 fclose(fi);
162 // elf header
163 ehdr = (ElfW(Ehdr) *)buf;
164 if (ehdr->e_ident[4] != ELFCLASSW)
166 fprintf(stderr, "tcc: ar: Unsupported Elf Class: %s\n", argv[i_obj]);
167 goto the_end;
170 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
171 shstr = (char *)(buf + shdr->sh_offset);
172 for (i = 0; i < ehdr->e_shnum; i++)
174 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
175 if (!shdr->sh_offset)
176 continue;
177 if (shdr->sh_type == SHT_SYMTAB)
179 symtab = (char *)(buf + shdr->sh_offset);
180 symtabsize = shdr->sh_size;
182 if (shdr->sh_type == SHT_STRTAB)
184 if (!strcmp(shstr + shdr->sh_name, ".strtab"))
186 strtab = (char *)(buf + shdr->sh_offset);
187 //strtabsize = shdr->sh_size;
192 if (symtab && symtabsize)
194 int nsym = symtabsize / sizeof(ElfW(Sym));
195 //printf("symtab: info size shndx name\n");
196 for (i = 1; i < nsym; i++)
198 sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
199 if (sym->st_shndx &&
200 (sym->st_info == 0x10
201 || sym->st_info == 0x11
202 || sym->st_info == 0x12
203 || sym->st_info == 0x20
204 || sym->st_info == 0x21
205 || sym->st_info == 0x22
206 )) {
207 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
208 istrlen = strlen(strtab + sym->st_name)+1;
209 anames = tcc_realloc(anames, strpos+istrlen);
210 strcpy(anames + strpos, strtab + sym->st_name);
211 strpos += istrlen;
212 if (++funccnt >= funcmax) {
213 funcmax += 250;
214 afpos = tcc_realloc(afpos, funcmax * sizeof *afpos); // 250 func more
216 afpos[funccnt] = fpos;
221 file = argv[i_obj];
222 for (name = strchr(file, 0);
223 name > file && name[-1] != '/' && name[-1] != '\\';
224 --name);
225 istrlen = strlen(name);
226 if (istrlen >= sizeof(arhdro.ar_name))
227 istrlen = sizeof(arhdro.ar_name) - 1;
228 memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
229 memcpy(arhdro.ar_name, name, istrlen);
230 arhdro.ar_name[istrlen] = '/';
231 sprintf(stmp, "%-10d", fsize);
232 memcpy(&arhdro.ar_size, stmp, 10);
233 fwrite(&arhdro, sizeof(arhdro), 1, fo);
234 fwrite(buf, fsize, 1, fo);
235 tcc_free(buf);
236 i_obj++;
237 fpos += (fsize + sizeof(arhdro));
239 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
240 fpos = 0;
241 if ((hofs & 1)) // align
242 hofs++, fpos = 1;
243 // write header
244 fwrite("!<arch>\n", 8, 1, fh);
245 sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
246 memcpy(&arhdr.ar_size, stmp, 10);
247 fwrite(&arhdr, sizeof(arhdr), 1, fh);
248 afpos[0] = le2belong(funccnt);
249 for (i=1; i<=funccnt; i++)
250 afpos[i] = le2belong(afpos[i] + hofs);
251 fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
252 fwrite(anames, strpos, 1, fh);
253 if (fpos)
254 fwrite("", 1, 1, fh);
255 // write objects
256 fseek(fo, 0, SEEK_END);
257 fsize = ftell(fo);
258 fseek(fo, 0, SEEK_SET);
259 buf = tcc_malloc(fsize + 1);
260 fread(buf, fsize, 1, fo);
261 fwrite(buf, fsize, 1, fh);
262 tcc_free(buf);
263 ret = 0;
264 the_end:
265 if (anames)
266 tcc_free(anames);
267 if (afpos)
268 tcc_free(afpos);
269 if (fh)
270 fclose(fh);
271 if (fo)
272 fclose(fo), remove(tfile);
273 return ret;
276 /* -------------------------------------------------------------- */
278 * tiny_impdef creates an export definition file (.def) from a dll
279 * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
281 * Copyright (c) 2005,2007 grischka
283 * This program is free software; you can redistribute it and/or modify
284 * it under the terms of the GNU General Public License as published by
285 * the Free Software Foundation; either version 2 of the License, or
286 * (at your option) any later version.
288 * This program is distributed in the hope that it will be useful,
289 * but WITHOUT ANY WARRANTY; without even the implied warranty of
290 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
291 * GNU General Public License for more details.
293 * You should have received a copy of the GNU General Public License
294 * along with this program; if not, write to the Free Software
295 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
298 #ifdef TCC_TARGET_PE
300 ST_FUNC int tcc_tool_impdef(TCCState *s1, int argc, char **argv)
302 int ret, v, i;
303 char infile[260];
304 char outfile[260];
306 const char *file;
307 char *p, *q;
308 FILE *fp, *op;
310 #ifdef _WIN32
311 char path[260];
312 #endif
314 infile[0] = outfile[0] = 0;
315 fp = op = NULL;
316 ret = 1;
317 p = NULL;
318 v = 0;
320 for (i = 1; i < argc; ++i) {
321 const char *a = argv[i];
322 if ('-' == a[0]) {
323 if (0 == strcmp(a, "-v")) {
324 v = 1;
325 } else if (0 == strcmp(a, "-o")) {
326 if (++i == argc)
327 goto usage;
328 strcpy(outfile, argv[i]);
329 } else
330 goto usage;
331 } else if (0 == infile[0])
332 strcpy(infile, a);
333 else
334 goto usage;
337 if (0 == infile[0]) {
338 usage:
339 fprintf(stderr,
340 "usage: tcc -impdef library.dll [-v] [-o outputfile]\n"
341 "create export definition file (.def) from dll\n"
343 goto the_end;
346 if (0 == outfile[0]) {
347 strcpy(outfile, tcc_basename(infile));
348 q = strrchr(outfile, '.');
349 if (NULL == q)
350 q = strchr(outfile, 0);
351 strcpy(q, ".def");
354 file = infile;
355 #ifdef _WIN32
356 if (SearchPath(NULL, file, ".dll", sizeof path, path, NULL))
357 file = path;
358 #endif
359 ret = tcc_get_dllexports(file, &p);
360 if (ret || !p) {
361 fprintf(stderr, "tcc: impdef: %s '%s'\n",
362 ret == -1 ? "can't find file" :
363 ret == 1 ? "can't read symbols" :
364 ret == 0 ? "no symbols found in" :
365 "unknown file type", file);
366 ret = 1;
367 goto the_end;
370 if (v)
371 printf("-> %s\n", file);
373 op = fopen(outfile, "wb");
374 if (NULL == op) {
375 fprintf(stderr, "tcc: impdef: could not create output file: %s\n", outfile);
376 goto the_end;
379 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", tcc_basename(file));
380 for (q = p, i = 0; *q; ++i) {
381 fprintf(op, "%s\n", q);
382 q += strlen(q) + 1;
385 if (v)
386 printf("<- %s (%d symbol%s)\n", outfile, i, &"s"[i<2]);
388 ret = 0;
390 the_end:
391 if (p)
392 tcc_free(p);
393 if (fp)
394 fclose(fp);
395 if (op)
396 fclose(op);
397 return ret;
400 #endif /* TCC_TARGET_PE */
402 /* -------------------------------------------------------------- */
404 * TCC - Tiny C Compiler
406 * Copyright (c) 2001-2004 Fabrice Bellard
408 * This library is free software; you can redistribute it and/or
409 * modify it under the terms of the GNU Lesser General Public
410 * License as published by the Free Software Foundation; either
411 * version 2 of the License, or (at your option) any later version.
413 * This library is distributed in the hope that it will be useful,
414 * but WITHOUT ANY WARRANTY; without even the implied warranty of
415 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
416 * Lesser General Public License for more details.
418 * You should have received a copy of the GNU Lesser General Public
419 * License along with this library; if not, write to the Free Software
420 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
423 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
425 #if !defined TCC_TARGET_I386 && !defined TCC_TARGET_X86_64
427 ST_FUNC void tcc_tool_cross(TCCState *s1, char **argv, int option)
429 tcc_error("-m%d not implemented.", option);
432 #else
433 #ifdef _WIN32
434 #include <process.h>
436 static char *str_replace(const char *str, const char *p, const char *r)
438 const char *s, *s0;
439 char *d, *d0;
440 int sl, pl, rl;
442 sl = strlen(str);
443 pl = strlen(p);
444 rl = strlen(r);
445 for (d0 = NULL;; d0 = tcc_malloc(sl + 1)) {
446 for (d = d0, s = str; s0 = s, s = strstr(s, p), s; s += pl) {
447 if (d) {
448 memcpy(d, s0, sl = s - s0), d += sl;
449 memcpy(d, r, rl), d += rl;
450 } else
451 sl += rl - pl;
453 if (d) {
454 strcpy(d, s0);
455 return d0;
460 static int execvp_win32(const char *prog, char **argv)
462 int ret; char **p;
463 /* replace all " by \" */
464 for (p = argv; *p; ++p)
465 if (strchr(*p, '"'))
466 *p = str_replace(*p, "\"", "\\\"");
467 ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
468 if (-1 == ret)
469 return ret;
470 _cwait(&ret, ret, WAIT_CHILD);
471 exit(ret);
473 #define execvp execvp_win32
474 #endif /* _WIN32 */
476 ST_FUNC void tcc_tool_cross(TCCState *s1, char **argv, int target)
478 char program[4096];
479 char *a0 = argv[0];
480 int prefix = tcc_basename(a0) - a0;
482 snprintf(program, sizeof program,
483 "%.*s%s"
484 #ifdef TCC_TARGET_PE
485 "-win32"
486 #endif
487 "-tcc"
488 #ifdef _WIN32
489 ".exe"
490 #endif
491 , prefix, a0, target == 64 ? "x86_64" : "i386");
493 if (strcmp(a0, program))
494 execvp(argv[0] = program, argv);
495 tcc_error("could not run '%s'", program);
498 #endif /* TCC_TARGET_I386 && TCC_TARGET_X86_64 */
499 /* -------------------------------------------------------------- */
500 /* enable commandline wildcard expansion (tcc -o x.exe *.c) */
502 #ifdef _WIN32
503 const int _CRT_glob = 1;
504 #ifndef _CRT_glob
505 const int _dowildcard = 1;
506 #endif
507 #endif
509 /* -------------------------------------------------------------- */
510 /* generate xxx.d file */
512 static char *escape_target_dep(const char *s) {
513 char *res = tcc_malloc(strlen(s) * 2 + 1);
514 int j;
515 for (j = 0; *s; s++, j++) {
516 if (is_space(*s)) {
517 res[j++] = '\\';
519 res[j] = *s;
521 res[j] = '\0';
522 return res;
525 ST_FUNC void gen_makedeps(TCCState *s1, const char *target, const char *filename)
527 FILE *depout;
528 char buf[1024], *escaped_target;
529 int i, k;
531 if (!filename) {
532 /* compute filename automatically: dir/file.o -> dir/file.d */
533 snprintf(buf, sizeof buf, "%.*s.d",
534 (int)(tcc_fileextension(target) - target), target);
535 filename = buf;
538 if (s1->verbose)
539 printf("<- %s\n", filename);
541 if(!strcmp(filename, "-"))
542 depout = fdopen(1, "w");
543 else
544 /* XXX return err codes instead of error() ? */
545 depout = fopen(filename, "w");
546 if (!depout)
547 tcc_error("could not open '%s'", filename);
548 fprintf(depout, "%s:", target);
549 for (i = 0; i<s1->nb_target_deps; ++i) {
550 for (k = 0; k < i; ++k)
551 if (0 == strcmp(s1->target_deps[i], s1->target_deps[k]))
552 goto next;
553 escaped_target = escape_target_dep(s1->target_deps[i]);
554 fprintf(depout, " \\\n %s", escaped_target);
555 tcc_free(escaped_target);
556 next:;
558 fprintf(depout, "\n");
559 fclose(depout);
562 /* -------------------------------------------------------------- */