riscv64-tok.h: update with more instructions from the spec
[tinycc.git] / tcctools.c
blobbef21ef6b1d27bbe634cc393759051c8fbc73795
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 [crstvx] lib [files]\n");
65 fprintf(stderr, "create library ([abdiopN] 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 "0 ",
74 "0 ",
75 "0 ",
76 "0 ",
77 "0 ",
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 = "habdiopN"; // unsupported but destructive if ignored.
98 int extract = 0;
99 int table = 0;
100 int verbose = 0;
102 i_lib = 0; i_obj = 0; // will hold the index of the lib and first obj
103 for (i = 1; i < argc; i++) {
104 const char *a = argv[i];
105 if (*a == '-' && strstr(a, "."))
106 ret = 1; // -x.y is always invalid (same as gnu ar)
107 if ((*a == '-') || (i == 1 && !strstr(a, "."))) { // options argument
108 if (contains_any(a, ops_conflict))
109 ret = 1;
110 if (strstr(a, "x"))
111 extract = 1;
112 if (strstr(a, "t"))
113 table = 1;
114 if (strstr(a, "v"))
115 verbose = 1;
116 } else { // lib or obj files: don't abort - keep validating all args.
117 if (!i_lib) // first file is the lib
118 i_lib = i;
119 else if (!i_obj) // second file is the first obj
120 i_obj = i;
124 if (!i_lib) // i_obj implies also i_lib.
125 ret = 1;
126 i_obj = i_obj ? i_obj : argc; // An empty archive will be generated if no input file is given
128 if (ret == 1)
129 return ar_usage(ret);
131 if (extract || table) {
132 if ((fh = fopen(argv[i_lib], "rb")) == NULL)
134 fprintf(stderr, "tcc: ar: can't open file %s\n", argv[i_lib]);
135 goto finish;
137 fread(stmp, 1, 8, fh);
138 if (memcmp(stmp,ARMAG,8))
140 no_ar:
141 fprintf(stderr, "tcc: ar: not an ar archive %s\n", argv[i_lib]);
142 goto finish;
144 while (fread(&arhdr, 1, sizeof(arhdr), fh) == sizeof(arhdr)) {
145 char *p, *e;
147 if (memcmp(arhdr.ar_fmag, ARFMAG, 2))
148 goto no_ar;
149 p = arhdr.ar_name;
150 for (e = p + sizeof arhdr.ar_name; e > p && e[-1] == ' ';)
151 e--;
152 *e = '\0';
153 arhdr.ar_size[sizeof arhdr.ar_size-1] = 0;
154 fsize = atoi(arhdr.ar_size);
155 buf = tcc_malloc(fsize + 1);
156 fread(buf, fsize, 1, fh);
157 if (strcmp(arhdr.ar_name,"/") && strcmp(arhdr.ar_name,"/SYM64/")) {
158 if (e > p && e[-1] == '/')
159 e[-1] = '\0';
160 /* tv not implemented */
161 if (table || verbose)
162 printf("%s%s\n", extract ? "x - " : "", arhdr.ar_name);
163 if (extract) {
164 if ((fo = fopen(arhdr.ar_name, "wb")) == NULL)
166 fprintf(stderr, "tcc: ar: can't create file %s\n",
167 arhdr.ar_name);
168 tcc_free(buf);
169 goto finish;
171 fwrite(buf, fsize, 1, fo);
172 fclose(fo);
173 /* ignore date/uid/gid/mode */
176 tcc_free(buf);
178 ret = 0;
179 finish:
180 if (fh)
181 fclose(fh);
182 return ret;
185 if ((fh = fopen(argv[i_lib], "wb")) == NULL)
187 fprintf(stderr, "tcc: ar: can't create file %s\n", argv[i_lib]);
188 goto the_end;
191 sprintf(tfile, "%s.tmp", argv[i_lib]);
192 if ((fo = fopen(tfile, "wb+")) == NULL)
194 fprintf(stderr, "tcc: ar: can't create temporary file %s\n", tfile);
195 goto the_end;
198 funcmax = 250;
199 afpos = tcc_realloc(NULL, funcmax * sizeof *afpos); // 250 func
200 memcpy(&arhdro.ar_mode, "100644", 6);
202 // i_obj = first input object file
203 while (i_obj < argc)
205 if (*argv[i_obj] == '-') { // by now, all options start with '-'
206 i_obj++;
207 continue;
209 if ((fi = fopen(argv[i_obj], "rb")) == NULL) {
210 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_obj]);
211 goto the_end;
213 if (verbose)
214 printf("a - %s\n", argv[i_obj]);
216 fseek(fi, 0, SEEK_END);
217 fsize = ftell(fi);
218 fseek(fi, 0, SEEK_SET);
219 buf = tcc_malloc(fsize + 1);
220 fread(buf, fsize, 1, fi);
221 fclose(fi);
223 // elf header
224 ehdr = (ElfW(Ehdr) *)buf;
225 if (ehdr->e_ident[4] != ELFCLASSW)
227 fprintf(stderr, "tcc: ar: Unsupported Elf Class: %s\n", argv[i_obj]);
228 goto the_end;
231 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
232 shstr = (char *)(buf + shdr->sh_offset);
233 for (i = 0; i < ehdr->e_shnum; i++)
235 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
236 if (!shdr->sh_offset)
237 continue;
238 if (shdr->sh_type == SHT_SYMTAB)
240 symtab = (char *)(buf + shdr->sh_offset);
241 symtabsize = shdr->sh_size;
243 if (shdr->sh_type == SHT_STRTAB)
245 if (!strcmp(shstr + shdr->sh_name, ".strtab"))
247 strtab = (char *)(buf + shdr->sh_offset);
248 //strtabsize = shdr->sh_size;
253 if (symtab && symtabsize)
255 int nsym = symtabsize / sizeof(ElfW(Sym));
256 //printf("symtab: info size shndx name\n");
257 for (i = 1; i < nsym; i++)
259 sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
260 if (sym->st_shndx &&
261 (sym->st_info == 0x10
262 || sym->st_info == 0x11
263 || sym->st_info == 0x12
264 || sym->st_info == 0x20
265 || sym->st_info == 0x21
266 || sym->st_info == 0x22
267 )) {
268 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
269 istrlen = strlen(strtab + sym->st_name)+1;
270 anames = tcc_realloc(anames, strpos+istrlen);
271 strcpy(anames + strpos, strtab + sym->st_name);
272 strpos += istrlen;
273 if (++funccnt >= funcmax) {
274 funcmax += 250;
275 afpos = tcc_realloc(afpos, funcmax * sizeof *afpos); // 250 func more
277 afpos[funccnt] = fpos;
282 file = argv[i_obj];
283 for (name = strchr(file, 0);
284 name > file && name[-1] != '/' && name[-1] != '\\';
285 --name);
286 istrlen = strlen(name);
287 if (istrlen >= sizeof(arhdro.ar_name))
288 istrlen = sizeof(arhdro.ar_name) - 1;
289 memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
290 memcpy(arhdro.ar_name, name, istrlen);
291 arhdro.ar_name[istrlen] = '/';
292 sprintf(stmp, "%-10d", fsize);
293 memcpy(&arhdro.ar_size, stmp, 10);
294 fwrite(&arhdro, sizeof(arhdro), 1, fo);
295 fwrite(buf, fsize, 1, fo);
296 tcc_free(buf);
297 i_obj++;
298 fpos += (fsize + sizeof(arhdro));
300 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
301 fpos = 0;
302 if ((hofs & 1)) // align
303 hofs++, fpos = 1;
304 // write header
305 fwrite(ARMAG, 8, 1, fh);
306 // create an empty archive
307 if (!funccnt) {
308 ret = 0;
309 goto the_end;
311 sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)) + fpos);
312 memcpy(&arhdr.ar_size, stmp, 10);
313 fwrite(&arhdr, sizeof(arhdr), 1, fh);
314 afpos[0] = le2belong(funccnt);
315 for (i=1; i<=funccnt; i++)
316 afpos[i] = le2belong(afpos[i] + hofs);
317 fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
318 fwrite(anames, strpos, 1, fh);
319 if (fpos)
320 fwrite("", 1, 1, fh);
321 // write objects
322 fseek(fo, 0, SEEK_END);
323 fsize = ftell(fo);
324 fseek(fo, 0, SEEK_SET);
325 buf = tcc_malloc(fsize + 1);
326 fread(buf, fsize, 1, fo);
327 fwrite(buf, fsize, 1, fh);
328 tcc_free(buf);
329 ret = 0;
330 the_end:
331 if (anames)
332 tcc_free(anames);
333 if (afpos)
334 tcc_free(afpos);
335 if (fh)
336 fclose(fh);
337 if (fo)
338 fclose(fo), remove(tfile);
339 return ret;
342 /* -------------------------------------------------------------- */
344 * tiny_impdef creates an export definition file (.def) from a dll
345 * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
347 * Copyright (c) 2005,2007 grischka
349 * This program is free software; you can redistribute it and/or modify
350 * it under the terms of the GNU General Public License as published by
351 * the Free Software Foundation; either version 2 of the License, or
352 * (at your option) any later version.
354 * This program is distributed in the hope that it will be useful,
355 * but WITHOUT ANY WARRANTY; without even the implied warranty of
356 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
357 * GNU General Public License for more details.
359 * You should have received a copy of the GNU General Public License
360 * along with this program; if not, write to the Free Software
361 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
364 #ifdef TCC_TARGET_PE
366 ST_FUNC int tcc_tool_impdef(TCCState *s1, int argc, char **argv)
368 int ret, v, i;
369 char infile[260];
370 char outfile[260];
372 const char *file;
373 char *p, *q;
374 FILE *fp, *op;
376 #ifdef _WIN32
377 char path[260];
378 #endif
380 infile[0] = outfile[0] = 0;
381 fp = op = NULL;
382 ret = 1;
383 p = NULL;
384 v = 0;
386 for (i = 1; i < argc; ++i) {
387 const char *a = argv[i];
388 if ('-' == a[0]) {
389 if (0 == strcmp(a, "-v")) {
390 v = 1;
391 } else if (0 == strcmp(a, "-o")) {
392 if (++i == argc)
393 goto usage;
394 strcpy(outfile, argv[i]);
395 } else
396 goto usage;
397 } else if (0 == infile[0])
398 strcpy(infile, a);
399 else
400 goto usage;
403 if (0 == infile[0]) {
404 usage:
405 fprintf(stderr,
406 "usage: tcc -impdef library.dll [-v] [-o outputfile]\n"
407 "create export definition file (.def) from dll\n"
409 goto the_end;
412 if (0 == outfile[0]) {
413 strcpy(outfile, tcc_basename(infile));
414 q = strrchr(outfile, '.');
415 if (NULL == q)
416 q = strchr(outfile, 0);
417 strcpy(q, ".def");
420 file = infile;
421 #ifdef _WIN32
422 if (SearchPath(NULL, file, ".dll", sizeof path, path, NULL))
423 file = path;
424 #endif
425 ret = tcc_get_dllexports(file, &p);
426 if (ret || !p) {
427 fprintf(stderr, "tcc: impdef: %s '%s'\n",
428 ret == -1 ? "can't find file" :
429 ret == 1 ? "can't read symbols" :
430 ret == 0 ? "no symbols found in" :
431 "unknown file type", file);
432 ret = 1;
433 goto the_end;
436 if (v)
437 printf("-> %s\n", file);
439 op = fopen(outfile, "wb");
440 if (NULL == op) {
441 fprintf(stderr, "tcc: impdef: could not create output file: %s\n", outfile);
442 goto the_end;
445 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", tcc_basename(file));
446 for (q = p, i = 0; *q; ++i) {
447 fprintf(op, "%s\n", q);
448 q += strlen(q) + 1;
451 if (v)
452 printf("<- %s (%d symbol%s)\n", outfile, i, &"s"[i<2]);
454 ret = 0;
456 the_end:
457 if (p)
458 tcc_free(p);
459 if (fp)
460 fclose(fp);
461 if (op)
462 fclose(op);
463 return ret;
466 #endif /* TCC_TARGET_PE */
468 /* -------------------------------------------------------------- */
470 * TCC - Tiny C Compiler
472 * Copyright (c) 2001-2004 Fabrice Bellard
474 * This library is free software; you can redistribute it and/or
475 * modify it under the terms of the GNU Lesser General Public
476 * License as published by the Free Software Foundation; either
477 * version 2 of the License, or (at your option) any later version.
479 * This library is distributed in the hope that it will be useful,
480 * but WITHOUT ANY WARRANTY; without even the implied warranty of
481 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
482 * Lesser General Public License for more details.
484 * You should have received a copy of the GNU Lesser General Public
485 * License along with this library; if not, write to the Free Software
486 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
489 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
491 #if !defined TCC_TARGET_I386 && !defined TCC_TARGET_X86_64
493 ST_FUNC int tcc_tool_cross(TCCState *s1, char **argv, int option)
495 tcc_error_noabort("-m%d not implemented.", option);
496 return 1;
499 #else
500 #ifdef _WIN32
501 #include <process.h>
503 static char *str_replace(const char *str, const char *p, const char *r)
505 const char *s, *s0;
506 char *d, *d0;
507 int sl, pl, rl;
509 sl = strlen(str);
510 pl = strlen(p);
511 rl = strlen(r);
512 for (d0 = NULL;; d0 = tcc_malloc(sl + 1)) {
513 for (d = d0, s = str; s0 = s, s = strstr(s, p), s; s += pl) {
514 if (d) {
515 memcpy(d, s0, sl = s - s0), d += sl;
516 memcpy(d, r, rl), d += rl;
517 } else
518 sl += rl - pl;
520 if (d) {
521 strcpy(d, s0);
522 return d0;
527 static int execvp_win32(const char *prog, char **argv)
529 int ret; char **p;
530 /* replace all " by \" */
531 for (p = argv; *p; ++p)
532 if (strchr(*p, '"'))
533 *p = str_replace(*p, "\"", "\\\"");
534 ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
535 if (-1 == ret)
536 return ret;
537 _cwait(&ret, ret, WAIT_CHILD);
538 exit(ret);
540 #define execvp execvp_win32
541 #endif /* _WIN32 */
543 ST_FUNC int tcc_tool_cross(TCCState *s1, char **argv, int target)
545 char program[4096];
546 char *a0 = argv[0];
547 int prefix = tcc_basename(a0) - a0;
549 snprintf(program, sizeof program,
550 "%.*s%s"
551 #ifdef TCC_TARGET_PE
552 "-win32"
553 #endif
554 "-tcc"
555 #ifdef _WIN32
556 ".exe"
557 #endif
558 , prefix, a0, target == 64 ? "x86_64" : "i386");
560 if (strcmp(a0, program))
561 execvp(argv[0] = program, argv);
562 tcc_error_noabort("could not run '%s'", program);
563 return 1;
566 #endif /* TCC_TARGET_I386 && TCC_TARGET_X86_64 */
567 /* -------------------------------------------------------------- */
568 /* enable commandline wildcard expansion (tcc -o x.exe *.c) */
570 #ifdef _WIN32
571 const int _CRT_glob = 1;
572 #ifndef _CRT_glob
573 const int _dowildcard = 1;
574 #endif
575 #endif
577 /* -------------------------------------------------------------- */
578 /* generate xxx.d file */
580 static char *escape_target_dep(const char *s) {
581 char *res = tcc_malloc(strlen(s) * 2 + 1);
582 int j;
583 for (j = 0; *s; s++, j++) {
584 if (is_space(*s)) {
585 res[j++] = '\\';
587 res[j] = *s;
589 res[j] = '\0';
590 return res;
593 ST_FUNC int gen_makedeps(TCCState *s1, const char *target, const char *filename)
595 FILE *depout;
596 char buf[1024], *escaped_target;
597 int i, k;
599 if (!filename) {
600 /* compute filename automatically: dir/file.o -> dir/file.d */
601 snprintf(buf, sizeof buf, "%.*s.d",
602 (int)(tcc_fileextension(target) - target), target);
603 filename = buf;
606 if(!strcmp(filename, "-"))
607 depout = fdopen(1, "w");
608 else
609 /* XXX return err codes instead of error() ? */
610 depout = fopen(filename, "w");
611 if (!depout)
612 return tcc_error_noabort("could not open '%s'", filename);
613 if (s1->verbose)
614 printf("<- %s\n", filename);
616 fprintf(depout, "%s:", target);
617 for (i = 0; i<s1->nb_target_deps; ++i) {
618 for (k = 0; k < i; ++k)
619 if (0 == strcmp(s1->target_deps[i], s1->target_deps[k]))
620 goto next;
621 escaped_target = escape_target_dep(s1->target_deps[i]);
622 fprintf(depout, " \\\n %s", escaped_target);
623 tcc_free(escaped_target);
624 next:;
626 fprintf(depout, "\n");
627 fclose(depout);
628 return 0;
631 /* -------------------------------------------------------------- */