libtcc: Activate DWARF debug infos with tcc -gdwarf ...
[tinycc.git] / tcctools.c
blob2bcc990d423f7b4168d5ffc73c9e8e0315e20aa9
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_lib) // i_obj implies also i_lib.
119 ret = 1;
120 i_obj = i_obj ? i_obj : argc; // An empty archive will be generated if no input file is given
122 if (ret == 1)
123 return ar_usage(ret);
125 if ((fh = fopen(argv[i_lib], "wb")) == NULL)
127 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_lib]);
128 goto the_end;
131 sprintf(tfile, "%s.tmp", argv[i_lib]);
132 if ((fo = fopen(tfile, "wb+")) == NULL)
134 fprintf(stderr, "tcc: ar: can't create temporary file %s\n", tfile);
135 goto the_end;
138 funcmax = 250;
139 afpos = tcc_realloc(NULL, funcmax * sizeof *afpos); // 250 func
140 memcpy(&arhdro.ar_mode, "100666", 6);
142 // i_obj = first input object file
143 while (i_obj < argc)
145 if (*argv[i_obj] == '-') { // by now, all options start with '-'
146 i_obj++;
147 continue;
149 if ((fi = fopen(argv[i_obj], "rb")) == NULL) {
150 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_obj]);
151 goto the_end;
153 if (verbose)
154 printf("a - %s\n", argv[i_obj]);
156 fseek(fi, 0, SEEK_END);
157 fsize = ftell(fi);
158 fseek(fi, 0, SEEK_SET);
159 buf = tcc_malloc(fsize + 1);
160 fread(buf, fsize, 1, fi);
161 fclose(fi);
163 // elf header
164 ehdr = (ElfW(Ehdr) *)buf;
165 if (ehdr->e_ident[4] != ELFCLASSW)
167 fprintf(stderr, "tcc: ar: Unsupported Elf Class: %s\n", argv[i_obj]);
168 goto the_end;
171 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
172 shstr = (char *)(buf + shdr->sh_offset);
173 for (i = 0; i < ehdr->e_shnum; i++)
175 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
176 if (!shdr->sh_offset)
177 continue;
178 if (shdr->sh_type == SHT_SYMTAB)
180 symtab = (char *)(buf + shdr->sh_offset);
181 symtabsize = shdr->sh_size;
183 if (shdr->sh_type == SHT_STRTAB)
185 if (!strcmp(shstr + shdr->sh_name, ".strtab"))
187 strtab = (char *)(buf + shdr->sh_offset);
188 //strtabsize = shdr->sh_size;
193 if (symtab && symtabsize)
195 int nsym = symtabsize / sizeof(ElfW(Sym));
196 //printf("symtab: info size shndx name\n");
197 for (i = 1; i < nsym; i++)
199 sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
200 if (sym->st_shndx &&
201 (sym->st_info == 0x10
202 || sym->st_info == 0x11
203 || sym->st_info == 0x12
204 || sym->st_info == 0x20
205 || sym->st_info == 0x21
206 || sym->st_info == 0x22
207 )) {
208 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
209 istrlen = strlen(strtab + sym->st_name)+1;
210 anames = tcc_realloc(anames, strpos+istrlen);
211 strcpy(anames + strpos, strtab + sym->st_name);
212 strpos += istrlen;
213 if (++funccnt >= funcmax) {
214 funcmax += 250;
215 afpos = tcc_realloc(afpos, funcmax * sizeof *afpos); // 250 func more
217 afpos[funccnt] = fpos;
222 file = argv[i_obj];
223 for (name = strchr(file, 0);
224 name > file && name[-1] != '/' && name[-1] != '\\';
225 --name);
226 istrlen = strlen(name);
227 if (istrlen >= sizeof(arhdro.ar_name))
228 istrlen = sizeof(arhdro.ar_name) - 1;
229 memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
230 memcpy(arhdro.ar_name, name, istrlen);
231 arhdro.ar_name[istrlen] = '/';
232 sprintf(stmp, "%-10d", fsize);
233 memcpy(&arhdro.ar_size, stmp, 10);
234 fwrite(&arhdro, sizeof(arhdro), 1, fo);
235 fwrite(buf, fsize, 1, fo);
236 tcc_free(buf);
237 i_obj++;
238 fpos += (fsize + sizeof(arhdro));
240 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
241 fpos = 0;
242 if ((hofs & 1)) // align
243 hofs++, fpos = 1;
244 // write header
245 fwrite("!<arch>\n", 8, 1, fh);
246 // create an empty archive
247 if (!funccnt) {
248 ret = 0;
249 goto the_end;
251 sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
252 memcpy(&arhdr.ar_size, stmp, 10);
253 fwrite(&arhdr, sizeof(arhdr), 1, fh);
254 afpos[0] = le2belong(funccnt);
255 for (i=1; i<=funccnt; i++)
256 afpos[i] = le2belong(afpos[i] + hofs);
257 fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
258 fwrite(anames, strpos, 1, fh);
259 if (fpos)
260 fwrite("", 1, 1, fh);
261 // write objects
262 fseek(fo, 0, SEEK_END);
263 fsize = ftell(fo);
264 fseek(fo, 0, SEEK_SET);
265 buf = tcc_malloc(fsize + 1);
266 fread(buf, fsize, 1, fo);
267 fwrite(buf, fsize, 1, fh);
268 tcc_free(buf);
269 ret = 0;
270 the_end:
271 if (anames)
272 tcc_free(anames);
273 if (afpos)
274 tcc_free(afpos);
275 if (fh)
276 fclose(fh);
277 if (fo)
278 fclose(fo), remove(tfile);
279 return ret;
282 /* -------------------------------------------------------------- */
284 * tiny_impdef creates an export definition file (.def) from a dll
285 * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
287 * Copyright (c) 2005,2007 grischka
289 * This program is free software; you can redistribute it and/or modify
290 * it under the terms of the GNU General Public License as published by
291 * the Free Software Foundation; either version 2 of the License, or
292 * (at your option) any later version.
294 * This program is distributed in the hope that it will be useful,
295 * but WITHOUT ANY WARRANTY; without even the implied warranty of
296 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
297 * GNU General Public License for more details.
299 * You should have received a copy of the GNU General Public License
300 * along with this program; if not, write to the Free Software
301 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
304 #ifdef TCC_TARGET_PE
306 ST_FUNC int tcc_tool_impdef(TCCState *s1, int argc, char **argv)
308 int ret, v, i;
309 char infile[260];
310 char outfile[260];
312 const char *file;
313 char *p, *q;
314 FILE *fp, *op;
316 #ifdef _WIN32
317 char path[260];
318 #endif
320 infile[0] = outfile[0] = 0;
321 fp = op = NULL;
322 ret = 1;
323 p = NULL;
324 v = 0;
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 == -1 ? "can't find file" :
369 ret == 1 ? "can't read symbols" :
370 ret == 0 ? "no symbols found in" :
371 "unknown file type", file);
372 ret = 1;
373 goto the_end;
376 if (v)
377 printf("-> %s\n", file);
379 op = fopen(outfile, "wb");
380 if (NULL == op) {
381 fprintf(stderr, "tcc: impdef: could not create output file: %s\n", outfile);
382 goto the_end;
385 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", tcc_basename(file));
386 for (q = p, i = 0; *q; ++i) {
387 fprintf(op, "%s\n", q);
388 q += strlen(q) + 1;
391 if (v)
392 printf("<- %s (%d symbol%s)\n", outfile, i, &"s"[i<2]);
394 ret = 0;
396 the_end:
397 if (p)
398 tcc_free(p);
399 if (fp)
400 fclose(fp);
401 if (op)
402 fclose(op);
403 return ret;
406 #endif /* TCC_TARGET_PE */
408 /* -------------------------------------------------------------- */
410 * TCC - Tiny C Compiler
412 * Copyright (c) 2001-2004 Fabrice Bellard
414 * This library is free software; you can redistribute it and/or
415 * modify it under the terms of the GNU Lesser General Public
416 * License as published by the Free Software Foundation; either
417 * version 2 of the License, or (at your option) any later version.
419 * This library is distributed in the hope that it will be useful,
420 * but WITHOUT ANY WARRANTY; without even the implied warranty of
421 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
422 * Lesser General Public License for more details.
424 * You should have received a copy of the GNU Lesser General Public
425 * License along with this library; if not, write to the Free Software
426 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
429 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
431 #if !defined TCC_TARGET_I386 && !defined TCC_TARGET_X86_64
433 ST_FUNC void tcc_tool_cross(TCCState *s1, char **argv, int option)
435 tcc_error("-m%d not implemented.", option);
438 #else
439 #ifdef _WIN32
440 #include <process.h>
442 static char *str_replace(const char *str, const char *p, const char *r)
444 const char *s, *s0;
445 char *d, *d0;
446 int sl, pl, rl;
448 sl = strlen(str);
449 pl = strlen(p);
450 rl = strlen(r);
451 for (d0 = NULL;; d0 = tcc_malloc(sl + 1)) {
452 for (d = d0, s = str; s0 = s, s = strstr(s, p), s; s += pl) {
453 if (d) {
454 memcpy(d, s0, sl = s - s0), d += sl;
455 memcpy(d, r, rl), d += rl;
456 } else
457 sl += rl - pl;
459 if (d) {
460 strcpy(d, s0);
461 return d0;
466 static int execvp_win32(const char *prog, char **argv)
468 int ret; char **p;
469 /* replace all " by \" */
470 for (p = argv; *p; ++p)
471 if (strchr(*p, '"'))
472 *p = str_replace(*p, "\"", "\\\"");
473 ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
474 if (-1 == ret)
475 return ret;
476 _cwait(&ret, ret, WAIT_CHILD);
477 exit(ret);
479 #define execvp execvp_win32
480 #endif /* _WIN32 */
482 ST_FUNC void tcc_tool_cross(TCCState *s1, char **argv, int target)
484 char program[4096];
485 char *a0 = argv[0];
486 int prefix = tcc_basename(a0) - a0;
488 snprintf(program, sizeof program,
489 "%.*s%s"
490 #ifdef TCC_TARGET_PE
491 "-win32"
492 #endif
493 "-tcc"
494 #ifdef _WIN32
495 ".exe"
496 #endif
497 , prefix, a0, target == 64 ? "x86_64" : "i386");
499 if (strcmp(a0, program))
500 execvp(argv[0] = program, argv);
501 tcc_error("could not run '%s'", program);
504 #endif /* TCC_TARGET_I386 && TCC_TARGET_X86_64 */
505 /* -------------------------------------------------------------- */
506 /* enable commandline wildcard expansion (tcc -o x.exe *.c) */
508 #ifdef _WIN32
509 const int _CRT_glob = 1;
510 #ifndef _CRT_glob
511 const int _dowildcard = 1;
512 #endif
513 #endif
515 /* -------------------------------------------------------------- */
516 /* generate xxx.d file */
518 static char *escape_target_dep(const char *s) {
519 char *res = tcc_malloc(strlen(s) * 2 + 1);
520 int j;
521 for (j = 0; *s; s++, j++) {
522 if (is_space(*s)) {
523 res[j++] = '\\';
525 res[j] = *s;
527 res[j] = '\0';
528 return res;
531 ST_FUNC void gen_makedeps(TCCState *s1, const char *target, const char *filename)
533 FILE *depout;
534 char buf[1024], *escaped_target;
535 int i, k;
537 if (!filename) {
538 /* compute filename automatically: dir/file.o -> dir/file.d */
539 snprintf(buf, sizeof buf, "%.*s.d",
540 (int)(tcc_fileextension(target) - target), target);
541 filename = buf;
544 if (s1->verbose)
545 printf("<- %s\n", filename);
547 if(!strcmp(filename, "-"))
548 depout = fdopen(1, "w");
549 else
550 /* XXX return err codes instead of error() ? */
551 depout = fopen(filename, "w");
552 if (!depout)
553 tcc_error("could not open '%s'", filename);
554 fprintf(depout, "%s:", target);
555 for (i = 0; i<s1->nb_target_deps; ++i) {
556 for (k = 0; k < i; ++k)
557 if (0 == strcmp(s1->target_deps[i], s1->target_deps[k]))
558 goto next;
559 escaped_target = escape_target_dep(s1->target_deps[i]);
560 fprintf(depout, " \\\n %s", escaped_target);
561 tcc_free(escaped_target);
562 next:;
564 fprintf(depout, "\n");
565 fclose(depout);
568 /* -------------------------------------------------------------- */