fixup! riscv: Implement large addend for global address
[tinycc.git] / tcctools.c
blob5eb4349dc11c559195a714c774538e99f8010b10
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 const char *created_file = NULL; // must delete on error
86 ElfW(Ehdr) *ehdr;
87 ElfW(Shdr) *shdr;
88 ElfW(Sym) *sym;
89 int i, fsize, i_lib, i_obj;
90 char *buf, *shstr, *symtab = NULL, *strtab = NULL;
91 int symtabsize = 0;//, strtabsize = 0;
92 char *anames = NULL;
93 int *afpos = NULL;
94 int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs;
95 char tfile[260], stmp[20];
96 char *file, *name;
97 int ret = 2;
98 const char *ops_conflict = "habdiopN"; // unsupported but destructive if ignored.
99 int extract = 0;
100 int table = 0;
101 int verbose = 0;
103 i_lib = 0; i_obj = 0; // will hold the index of the lib and first obj
104 for (i = 1; i < argc; i++) {
105 const char *a = argv[i];
106 if (*a == '-' && strstr(a, "."))
107 ret = 1; // -x.y is always invalid (same as gnu ar)
108 if ((*a == '-') || (i == 1 && !strstr(a, "."))) { // options argument
109 if (contains_any(a, ops_conflict))
110 ret = 1;
111 if (strstr(a, "x"))
112 extract = 1;
113 if (strstr(a, "t"))
114 table = 1;
115 if (strstr(a, "v"))
116 verbose = 1;
117 } else { // lib or obj files: don't abort - keep validating all args.
118 if (!i_lib) // first file is the lib
119 i_lib = i;
120 else if (!i_obj) // second file is the first obj
121 i_obj = i;
125 if (!i_lib) // i_obj implies also i_lib.
126 ret = 1;
127 i_obj = i_obj ? i_obj : argc; // An empty archive will be generated if no input file is given
129 if (ret == 1)
130 return ar_usage(ret);
132 if (extract || table) {
133 if ((fh = fopen(argv[i_lib], "rb")) == NULL)
135 fprintf(stderr, "tcc: ar: can't open file %s\n", argv[i_lib]);
136 goto finish;
138 fread(stmp, 1, 8, fh);
139 if (memcmp(stmp,ARMAG,8))
141 no_ar:
142 fprintf(stderr, "tcc: ar: not an ar archive %s\n", argv[i_lib]);
143 goto finish;
145 while (fread(&arhdr, 1, sizeof(arhdr), fh) == sizeof(arhdr)) {
146 char *p, *e;
148 if (memcmp(arhdr.ar_fmag, ARFMAG, 2))
149 goto no_ar;
150 p = arhdr.ar_name;
151 for (e = p + sizeof arhdr.ar_name; e > p && e[-1] == ' ';)
152 e--;
153 *e = '\0';
154 arhdr.ar_size[sizeof arhdr.ar_size-1] = 0;
155 fsize = atoi(arhdr.ar_size);
156 buf = tcc_malloc(fsize + 1);
157 fread(buf, fsize, 1, fh);
158 if (strcmp(arhdr.ar_name,"/") && strcmp(arhdr.ar_name,"/SYM64/")) {
159 if (e > p && e[-1] == '/')
160 e[-1] = '\0';
161 /* tv not implemented */
162 if (table || verbose)
163 printf("%s%s\n", extract ? "x - " : "", arhdr.ar_name);
164 if (extract) {
165 if ((fo = fopen(arhdr.ar_name, "wb")) == NULL)
167 fprintf(stderr, "tcc: ar: can't create file %s\n",
168 arhdr.ar_name);
169 tcc_free(buf);
170 goto finish;
172 fwrite(buf, fsize, 1, fo);
173 fclose(fo);
174 /* ignore date/uid/gid/mode */
177 tcc_free(buf);
179 ret = 0;
180 finish:
181 if (fh)
182 fclose(fh);
183 return ret;
186 if ((fh = fopen(argv[i_lib], "wb")) == NULL)
188 fprintf(stderr, "tcc: ar: can't create file %s\n", argv[i_lib]);
189 goto the_end;
191 created_file = argv[i_lib];
193 sprintf(tfile, "%s.tmp", argv[i_lib]);
194 if ((fo = fopen(tfile, "wb+")) == NULL)
196 fprintf(stderr, "tcc: ar: can't create temporary file %s\n", tfile);
197 goto the_end;
200 funcmax = 250;
201 afpos = tcc_realloc(NULL, funcmax * sizeof *afpos); // 250 func
202 memcpy(&arhdro.ar_mode, "100644", 6);
204 // i_obj = first input object file
205 while (i_obj < argc)
207 if (*argv[i_obj] == '-') { // by now, all options start with '-'
208 i_obj++;
209 continue;
211 if ((fi = fopen(argv[i_obj], "rb")) == NULL) {
212 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_obj]);
213 goto the_end;
215 if (verbose)
216 printf("a - %s\n", argv[i_obj]);
218 fseek(fi, 0, SEEK_END);
219 fsize = ftell(fi);
220 fseek(fi, 0, SEEK_SET);
221 buf = tcc_malloc(fsize + 1);
222 fread(buf, fsize, 1, fi);
223 fclose(fi);
225 // elf header
226 ehdr = (ElfW(Ehdr) *)buf;
227 if (ehdr->e_ident[4] != ELFCLASSW)
229 fprintf(stderr, "tcc: ar: Unsupported Elf Class: %s\n", argv[i_obj]);
230 goto the_end;
233 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
234 shstr = (char *)(buf + shdr->sh_offset);
235 for (i = 0; i < ehdr->e_shnum; i++)
237 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
238 if (!shdr->sh_offset)
239 continue;
240 if (shdr->sh_type == SHT_SYMTAB)
242 symtab = (char *)(buf + shdr->sh_offset);
243 symtabsize = shdr->sh_size;
245 if (shdr->sh_type == SHT_STRTAB)
247 if (!strcmp(shstr + shdr->sh_name, ".strtab"))
249 strtab = (char *)(buf + shdr->sh_offset);
250 //strtabsize = shdr->sh_size;
255 if (symtab && symtabsize)
257 int nsym = symtabsize / sizeof(ElfW(Sym));
258 //printf("symtab: info size shndx name\n");
259 for (i = 1; i < nsym; i++)
261 sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
262 if (sym->st_shndx &&
263 (sym->st_info == 0x10
264 || sym->st_info == 0x11
265 || sym->st_info == 0x12
266 || sym->st_info == 0x20
267 || sym->st_info == 0x21
268 || sym->st_info == 0x22
269 )) {
270 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
271 istrlen = strlen(strtab + sym->st_name)+1;
272 anames = tcc_realloc(anames, strpos+istrlen);
273 strcpy(anames + strpos, strtab + sym->st_name);
274 strpos += istrlen;
275 if (++funccnt >= funcmax) {
276 funcmax += 250;
277 afpos = tcc_realloc(afpos, funcmax * sizeof *afpos); // 250 func more
279 afpos[funccnt] = fpos;
284 file = argv[i_obj];
285 for (name = strchr(file, 0);
286 name > file && name[-1] != '/' && name[-1] != '\\';
287 --name);
288 istrlen = strlen(name);
289 if (istrlen >= sizeof(arhdro.ar_name))
290 istrlen = sizeof(arhdro.ar_name) - 1;
291 memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
292 memcpy(arhdro.ar_name, name, istrlen);
293 arhdro.ar_name[istrlen] = '/';
294 sprintf(stmp, "%-10d", fsize);
295 memcpy(&arhdro.ar_size, stmp, 10);
296 fwrite(&arhdro, sizeof(arhdro), 1, fo);
297 fwrite(buf, fsize, 1, fo);
298 tcc_free(buf);
299 i_obj++;
300 fpos += (fsize + sizeof(arhdro));
302 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
303 fpos = 0;
304 if ((hofs & 1)) // align
305 hofs++, fpos = 1;
306 // write header
307 fwrite(ARMAG, 8, 1, fh);
308 // create an empty archive
309 if (!funccnt) {
310 ret = 0;
311 goto the_end;
313 sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)) + fpos);
314 memcpy(&arhdr.ar_size, stmp, 10);
315 fwrite(&arhdr, sizeof(arhdr), 1, fh);
316 afpos[0] = le2belong(funccnt);
317 for (i=1; i<=funccnt; i++)
318 afpos[i] = le2belong(afpos[i] + hofs);
319 fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
320 fwrite(anames, strpos, 1, fh);
321 if (fpos)
322 fwrite("", 1, 1, fh);
323 // write objects
324 fseek(fo, 0, SEEK_END);
325 fsize = ftell(fo);
326 fseek(fo, 0, SEEK_SET);
327 buf = tcc_malloc(fsize + 1);
328 fread(buf, fsize, 1, fo);
329 fwrite(buf, fsize, 1, fh);
330 tcc_free(buf);
331 ret = 0;
332 the_end:
333 if (anames)
334 tcc_free(anames);
335 if (afpos)
336 tcc_free(afpos);
337 if (fh)
338 fclose(fh);
339 if (created_file && ret != 0)
340 remove(created_file);
341 if (fo)
342 fclose(fo), remove(tfile);
343 return ret;
346 /* -------------------------------------------------------------- */
348 * tiny_impdef creates an export definition file (.def) from a dll
349 * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
351 * Copyright (c) 2005,2007 grischka
353 * This program is free software; you can redistribute it and/or modify
354 * it under the terms of the GNU General Public License as published by
355 * the Free Software Foundation; either version 2 of the License, or
356 * (at your option) any later version.
358 * This program is distributed in the hope that it will be useful,
359 * but WITHOUT ANY WARRANTY; without even the implied warranty of
360 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
361 * GNU General Public License for more details.
363 * You should have received a copy of the GNU General Public License
364 * along with this program; if not, write to the Free Software
365 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
368 #ifdef TCC_TARGET_PE
370 ST_FUNC int tcc_tool_impdef(TCCState *s1, int argc, char **argv)
372 int ret, v, i;
373 char infile[260];
374 char outfile[260];
376 const char *file;
377 char *p, *q;
378 FILE *fp, *op;
380 #ifdef _WIN32
381 char path[260];
382 #endif
384 infile[0] = outfile[0] = 0;
385 fp = op = NULL;
386 ret = 1;
387 p = NULL;
388 v = 0;
390 for (i = 1; i < argc; ++i) {
391 const char *a = argv[i];
392 if ('-' == a[0]) {
393 if (0 == strcmp(a, "-v")) {
394 v = 1;
395 } else if (0 == strcmp(a, "-o")) {
396 if (++i == argc)
397 goto usage;
398 strcpy(outfile, argv[i]);
399 } else
400 goto usage;
401 } else if (0 == infile[0])
402 strcpy(infile, a);
403 else
404 goto usage;
407 if (0 == infile[0]) {
408 usage:
409 fprintf(stderr,
410 "usage: tcc -impdef library.dll [-v] [-o outputfile]\n"
411 "create export definition file (.def) from dll\n"
413 goto the_end;
416 if (0 == outfile[0]) {
417 strcpy(outfile, tcc_basename(infile));
418 q = strrchr(outfile, '.');
419 if (NULL == q)
420 q = strchr(outfile, 0);
421 strcpy(q, ".def");
424 file = infile;
425 #ifdef _WIN32
426 if (SearchPath(NULL, file, ".dll", sizeof path, path, NULL))
427 file = path;
428 #endif
429 ret = tcc_get_dllexports(file, &p);
430 if (ret || !p) {
431 fprintf(stderr, "tcc: impdef: %s '%s'\n",
432 ret == -1 ? "can't find file" :
433 ret == 1 ? "can't read symbols" :
434 ret == 0 ? "no symbols found in" :
435 "unknown file type", file);
436 ret = 1;
437 goto the_end;
440 if (v)
441 printf("-> %s\n", file);
443 op = fopen(outfile, "wb");
444 if (NULL == op) {
445 fprintf(stderr, "tcc: impdef: could not create output file: %s\n", outfile);
446 goto the_end;
449 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", tcc_basename(file));
450 for (q = p, i = 0; *q; ++i) {
451 fprintf(op, "%s\n", q);
452 q += strlen(q) + 1;
455 if (v)
456 printf("<- %s (%d symbol%s)\n", outfile, i, &"s"[i<2]);
458 ret = 0;
460 the_end:
461 if (p)
462 tcc_free(p);
463 if (fp)
464 fclose(fp);
465 if (op)
466 fclose(op);
467 return ret;
470 #endif /* TCC_TARGET_PE */
472 /* -------------------------------------------------------------- */
474 * TCC - Tiny C Compiler
476 * Copyright (c) 2001-2004 Fabrice Bellard
478 * This library is free software; you can redistribute it and/or
479 * modify it under the terms of the GNU Lesser General Public
480 * License as published by the Free Software Foundation; either
481 * version 2 of the License, or (at your option) any later version.
483 * This library is distributed in the hope that it will be useful,
484 * but WITHOUT ANY WARRANTY; without even the implied warranty of
485 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
486 * Lesser General Public License for more details.
488 * You should have received a copy of the GNU Lesser General Public
489 * License along with this library; if not, write to the Free Software
490 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
493 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
495 #if !defined TCC_TARGET_I386 && !defined TCC_TARGET_X86_64
497 ST_FUNC int tcc_tool_cross(TCCState *s1, char **argv, int option)
499 tcc_error_noabort("-m%d not implemented.", option);
500 return 1;
503 #else
504 #ifdef _WIN32
505 #include <process.h>
507 static char *str_replace(const char *str, const char *p, const char *r)
509 const char *s, *s0;
510 char *d, *d0;
511 int sl, pl, rl;
513 sl = strlen(str);
514 pl = strlen(p);
515 rl = strlen(r);
516 for (d0 = NULL;; d0 = tcc_malloc(sl + 1)) {
517 for (d = d0, s = str; s0 = s, s = strstr(s, p), s; s += pl) {
518 if (d) {
519 memcpy(d, s0, sl = s - s0), d += sl;
520 memcpy(d, r, rl), d += rl;
521 } else
522 sl += rl - pl;
524 if (d) {
525 strcpy(d, s0);
526 return d0;
531 static int execvp_win32(const char *prog, char **argv)
533 int ret; char **p;
534 /* replace all " by \" */
535 for (p = argv; *p; ++p)
536 if (strchr(*p, '"'))
537 *p = str_replace(*p, "\"", "\\\"");
538 ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
539 if (-1 == ret)
540 return ret;
541 _cwait(&ret, ret, WAIT_CHILD);
542 exit(ret);
544 #define execvp execvp_win32
545 #endif /* _WIN32 */
547 ST_FUNC int tcc_tool_cross(TCCState *s1, char **argv, int target)
549 char program[4096];
550 char *a0 = argv[0];
551 int prefix = tcc_basename(a0) - a0;
553 snprintf(program, sizeof program,
554 "%.*s%s"
555 #ifdef TCC_TARGET_PE
556 "-win32"
557 #endif
558 "-tcc"
559 #ifdef _WIN32
560 ".exe"
561 #endif
562 , prefix, a0, target == 64 ? "x86_64" : "i386");
564 if (strcmp(a0, program))
565 execvp(argv[0] = program, argv);
566 tcc_error_noabort("could not run '%s'", program);
567 return 1;
570 #endif /* TCC_TARGET_I386 && TCC_TARGET_X86_64 */
571 /* -------------------------------------------------------------- */
572 /* enable commandline wildcard expansion (tcc -o x.exe *.c) */
574 #ifdef _WIN32
575 const int _CRT_glob = 1;
576 #ifndef _CRT_glob
577 const int _dowildcard = 1;
578 #endif
579 #endif
581 /* -------------------------------------------------------------- */
582 /* generate xxx.d file */
584 static char *escape_target_dep(const char *s) {
585 char *res = tcc_malloc(strlen(s) * 2 + 1);
586 int j;
587 for (j = 0; *s; s++, j++) {
588 if (is_space(*s)) {
589 res[j++] = '\\';
591 res[j] = *s;
593 res[j] = '\0';
594 return res;
597 ST_FUNC int gen_makedeps(TCCState *s1, const char *target, const char *filename)
599 FILE *depout;
600 char buf[1024];
601 char **escaped_targets;
602 int i, k, num_targets;
604 if (!filename) {
605 /* compute filename automatically: dir/file.o -> dir/file.d */
606 snprintf(buf, sizeof buf, "%.*s.d",
607 (int)(tcc_fileextension(target) - target), target);
608 filename = buf;
611 if(!strcmp(filename, "-"))
612 depout = fdopen(1, "w");
613 else
614 /* XXX return err codes instead of error() ? */
615 depout = fopen(filename, "w");
616 if (!depout)
617 return tcc_error_noabort("could not open '%s'", filename);
618 if (s1->verbose)
619 printf("<- %s\n", filename);
621 escaped_targets = tcc_malloc(s1->nb_target_deps * sizeof(*escaped_targets));
622 num_targets = 0;
623 for (i = 0; i<s1->nb_target_deps; ++i) {
624 for (k = 0; k < i; ++k)
625 if (0 == strcmp(s1->target_deps[i], s1->target_deps[k]))
626 goto next;
627 escaped_targets[num_targets++] = escape_target_dep(s1->target_deps[i]);
628 next:;
631 fprintf(depout, "%s:", target);
632 for (i = 0; i < num_targets; ++i)
633 fprintf(depout, " \\\n %s", escaped_targets[i]);
634 fprintf(depout, "\n");
635 if (s1->gen_phony_deps) {
636 /* Skip first file, which is the c file.
637 * Only works for single file give on command-line,
638 * but other compilers have the same limitation */
639 for (i = 1; i < num_targets; ++i)
640 fprintf(depout, "%s:\n", escaped_targets[i]);
642 for (i = 0; i < num_targets; ++i)
643 tcc_free(escaped_targets[i]);
644 tcc_free(escaped_targets);
645 fclose(depout);
646 return 0;
649 /* -------------------------------------------------------------- */