source: EcnlProtoTool/trunk/tcc-0.9.27/tcctools.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 15.6 KB
Line 
1/* -------------------------------------------------------------- */
2/*
3 * TCC - Tiny C Compiler
4 *
5 * tcctools.c - extra tools and and -m32/64 support
6 *
7 */
8
9/* -------------------------------------------------------------- */
10/*
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
15 *
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.
20 *
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.
25 *
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.
29 */
30
31#include "tcc.h"
32
33//#define ARMAG "!<arch>\n"
34#define ARFMAG "`\n"
35
36typedef 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;
45
46static unsigned long le2belong(unsigned long ul) {
47 return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) +
48 ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8);
49}
50
51/* Returns 1 if s contains any of the chars of list, else 0 */
52static 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;
58 }
59 }
60 return 0;
61}
62
63static 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;
67}
68
69ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
70{
71 static ArHdr arhdr = {
72 "/ ",
73 " ",
74 "0 ",
75 "0 ",
76 "0 ",
77 " ",
78 ARFMAG
79 };
80
81 static ArHdr arhdro = {
82 " ",
83 " ",
84 "0 ",
85 "0 ",
86 "0 ",
87 " ",
88 ARFMAG
89 };
90
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;
106
107 i_lib = 0; i_obj = 0; // will hold the index of the lib and first obj
108 for (i = 1; i < argc; i++) {
109 const char *a = argv[i];
110 if (*a == '-' && strstr(a, "."))
111 ret = 1; // -x.y is always invalid (same as gnu ar)
112 if ((*a == '-') || (i == 1 && !strstr(a, "."))) { // options argument
113 if (contains_any(a, ops_conflict))
114 ret = 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;
122 }
123 }
124
125 if (!i_obj) // i_obj implies also i_lib. we require both.
126 ret = 1;
127
128 if (ret == 1)
129 return ar_usage(ret);
130
131 if ((fh = fopen(argv[i_lib], "wb")) == NULL)
132 {
133 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_lib]);
134 goto the_end;
135 }
136
137 sprintf(tfile, "%s.tmp", argv[i_lib]);
138 if ((fo = fopen(tfile, "wb+")) == NULL)
139 {
140 fprintf(stderr, "tcc: ar: can't create temporary file %s\n", tfile);
141 goto the_end;
142 }
143
144 funcmax = 250;
145 afpos = tcc_realloc(NULL, funcmax * sizeof *afpos); // 250 func
146 memcpy(&arhdro.ar_mode, "100666", 6);
147
148 // i_obj = first input object file
149 while (i_obj < argc)
150 {
151 if (*argv[i_obj] == '-') { // by now, all options start with '-'
152 i_obj++;
153 continue;
154 }
155 if ((fi = fopen(argv[i_obj], "rb")) == NULL) {
156 fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_obj]);
157 goto the_end;
158 }
159 if (verbose)
160 printf("a - %s\n", argv[i_obj]);
161
162 fseek(fi, 0, SEEK_END);
163 fsize = ftell(fi);
164 fseek(fi, 0, SEEK_SET);
165 buf = tcc_malloc(fsize + 1);
166 fread(buf, fsize, 1, fi);
167 fclose(fi);
168
169 // elf header
170 ehdr = (ElfW(Ehdr) *)buf;
171 if (ehdr->e_ident[4] != ELFCLASSW)
172 {
173 fprintf(stderr, "tcc: ar: Unsupported Elf Class: %s\n", argv[i_obj]);
174 goto the_end;
175 }
176
177 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
178 shstr = (char *)(buf + shdr->sh_offset);
179 for (i = 0; i < ehdr->e_shnum; i++)
180 {
181 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
182 if (!shdr->sh_offset)
183 continue;
184 if (shdr->sh_type == SHT_SYMTAB)
185 {
186 symtab = (char *)(buf + shdr->sh_offset);
187 symtabsize = shdr->sh_size;
188 }
189 if (shdr->sh_type == SHT_STRTAB)
190 {
191 if (!strcmp(shstr + shdr->sh_name, ".strtab"))
192 {
193 strtab = (char *)(buf + shdr->sh_offset);
194 //strtabsize = shdr->sh_size;
195 }
196 }
197 }
198
199 if (symtab && symtabsize)
200 {
201 int nsym = symtabsize / sizeof(ElfW(Sym));
202 //printf("symtab: info size shndx name\n");
203 for (i = 1; i < nsym; i++)
204 {
205 sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
206 if (sym->st_shndx &&
207 (sym->st_info == 0x10
208 || sym->st_info == 0x11
209 || sym->st_info == 0x12
210 )) {
211 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
212 istrlen = strlen(strtab + sym->st_name)+1;
213 anames = tcc_realloc(anames, strpos+istrlen);
214 strcpy(anames + strpos, strtab + sym->st_name);
215 strpos += istrlen;
216 if (++funccnt >= funcmax) {
217 funcmax += 250;
218 afpos = tcc_realloc(afpos, funcmax * sizeof *afpos); // 250 func more
219 }
220 afpos[funccnt] = fpos;
221 }
222 }
223 }
224
225 file = argv[i_obj];
226 for (name = strchr(file, 0);
227 name > file && name[-1] != '/' && name[-1] != '\\';
228 --name);
229 istrlen = strlen(name);
230 if (istrlen >= sizeof(arhdro.ar_name))
231 istrlen = sizeof(arhdro.ar_name) - 1;
232 memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
233 memcpy(arhdro.ar_name, name, istrlen);
234 arhdro.ar_name[istrlen] = '/';
235 sprintf(stmp, "%-10d", fsize);
236 memcpy(&arhdro.ar_size, stmp, 10);
237 fwrite(&arhdro, sizeof(arhdro), 1, fo);
238 fwrite(buf, fsize, 1, fo);
239 tcc_free(buf);
240 i_obj++;
241 fpos += (fsize + sizeof(arhdro));
242 }
243 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
244 fpos = 0;
245 if ((hofs & 1)) // align
246 hofs++, fpos = 1;
247 // write header
248 fwrite("!<arch>\n", 8, 1, fh);
249 sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
250 memcpy(&arhdr.ar_size, stmp, 10);
251 fwrite(&arhdr, sizeof(arhdr), 1, fh);
252 afpos[0] = le2belong(funccnt);
253 for (i=1; i<=funccnt; i++)
254 afpos[i] = le2belong(afpos[i] + hofs);
255 fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
256 fwrite(anames, strpos, 1, fh);
257 if (fpos)
258 fwrite("", 1, 1, fh);
259 // write objects
260 fseek(fo, 0, SEEK_END);
261 fsize = ftell(fo);
262 fseek(fo, 0, SEEK_SET);
263 buf = tcc_malloc(fsize + 1);
264 fread(buf, fsize, 1, fo);
265 fwrite(buf, fsize, 1, fh);
266 tcc_free(buf);
267 ret = 0;
268the_end:
269 if (anames)
270 tcc_free(anames);
271 if (afpos)
272 tcc_free(afpos);
273 if (fh)
274 fclose(fh);
275 if (fo)
276 fclose(fo), remove(tfile);
277 return ret;
278}
279
280/* -------------------------------------------------------------- */
281/*
282 * tiny_impdef creates an export definition file (.def) from a dll
283 * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
284 *
285 * Copyright (c) 2005,2007 grischka
286 *
287 * This program is free software; you can redistribute it and/or modify
288 * it under the terms of the GNU General Public License as published by
289 * the Free Software Foundation; either version 2 of the License, or
290 * (at your option) any later version.
291 *
292 * This program is distributed in the hope that it will be useful,
293 * but WITHOUT ANY WARRANTY; without even the implied warranty of
294 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
295 * GNU General Public License for more details.
296 *
297 * You should have received a copy of the GNU General Public License
298 * along with this program; if not, write to the Free Software
299 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
300 */
301
302#ifdef TCC_TARGET_PE
303
304ST_FUNC int tcc_tool_impdef(TCCState *s1, int argc, char **argv)
305{
306 int ret, v, i;
307 char infile[260];
308 char outfile[260];
309
310 const char *file;
311 char *p, *q;
312 FILE *fp, *op;
313
314#ifdef _WIN32
315 char path[260];
316#endif
317
318 infile[0] = outfile[0] = 0;
319 fp = op = NULL;
320 ret = 1;
321 p = NULL;
322 v = 0;
323
324 for (i = 1; i < argc; ++i) {
325 const char *a = argv[i];
326 if ('-' == a[0]) {
327 if (0 == strcmp(a, "-v")) {
328 v = 1;
329 } else if (0 == strcmp(a, "-o")) {
330 if (++i == argc)
331 goto usage;
332 strcpy(outfile, argv[i]);
333 } else
334 goto usage;
335 } else if (0 == infile[0])
336 strcpy(infile, a);
337 else
338 goto usage;
339 }
340
341 if (0 == infile[0]) {
342usage:
343 fprintf(stderr,
344 "usage: tcc -impdef library.dll [-v] [-o outputfile]\n"
345 "create export definition file (.def) from dll\n"
346 );
347 goto the_end;
348 }
349
350 if (0 == outfile[0]) {
351 strcpy(outfile, tcc_basename(infile));
352 q = strrchr(outfile, '.');
353 if (NULL == q)
354 q = strchr(outfile, 0);
355 strcpy(q, ".def");
356 }
357
358 file = infile;
359#ifdef _WIN32
360 if (SearchPath(NULL, file, ".dll", sizeof path, path, NULL))
361 file = path;
362#endif
363 ret = tcc_get_dllexports(file, &p);
364 if (ret || !p) {
365 fprintf(stderr, "tcc: impdef: %s '%s'\n",
366 ret == -1 ? "can't find file" :
367 ret == 1 ? "can't read symbols" :
368 ret == 0 ? "no symbols found in" :
369 "unknown file type", file);
370 ret = 1;
371 goto the_end;
372 }
373
374 if (v)
375 printf("-> %s\n", file);
376
377 op = fopen(outfile, "wb");
378 if (NULL == op) {
379 fprintf(stderr, "tcc: impdef: could not create output file: %s\n", outfile);
380 goto the_end;
381 }
382
383 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", tcc_basename(file));
384 for (q = p, i = 0; *q; ++i) {
385 fprintf(op, "%s\n", q);
386 q += strlen(q) + 1;
387 }
388
389 if (v)
390 printf("<- %s (%d symbol%s)\n", outfile, i, &"s"[i<2]);
391
392 ret = 0;
393
394the_end:
395 /* cannot free memory received from tcc_get_dllexports
396 if it came from a dll */
397 /* if (p)
398 tcc_free(p); */
399 if (fp)
400 fclose(fp);
401 if (op)
402 fclose(op);
403 return ret;
404}
405
406#endif /* TCC_TARGET_PE */
407
408/* -------------------------------------------------------------- */
409/*
410 * TCC - Tiny C Compiler
411 *
412 * Copyright (c) 2001-2004 Fabrice Bellard
413 *
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.
418 *
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.
423 *
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
427 */
428
429/* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
430
431#if !defined TCC_TARGET_I386 && !defined TCC_TARGET_X86_64
432
433ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option)
434{
435 tcc_error("-m%d not implemented.", option);
436}
437
438#else
439#ifdef _WIN32
440#include <process.h>
441
442static char *str_replace(const char *str, const char *p, const char *r)
443{
444 const char *s, *s0;
445 char *d, *d0;
446 int sl, pl, rl;
447
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;
458 }
459 if (d) {
460 strcpy(d, s0);
461 return d0;
462 }
463 }
464}
465
466static int execvp_win32(const char *prog, char **argv)
467{
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);
478}
479#define execvp execvp_win32
480#endif /* _WIN32 */
481
482ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int target)
483{
484 char program[4096];
485 char *a0 = argv[0];
486 int prefix = tcc_basename(a0) - a0;
487
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");
498
499 if (strcmp(a0, program))
500 execvp(argv[0] = program, argv);
501 tcc_error("could not run '%s'", program);
502}
503
504#endif /* TCC_TARGET_I386 && TCC_TARGET_X86_64 */
505/* -------------------------------------------------------------- */
506/* enable commandline wildcard expansion (tcc -o x.exe *.c) */
507
508#ifdef _WIN32
509int _CRT_glob = 1;
510#ifndef _CRT_glob
511int _dowildcard = 1;
512#endif
513#endif
514
515/* -------------------------------------------------------------- */
516/* generate xxx.d file */
517
518ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename)
519{
520 FILE *depout;
521 char buf[1024];
522 int i;
523
524 if (!filename) {
525 /* compute filename automatically: dir/file.o -> dir/file.d */
526 snprintf(buf, sizeof buf, "%.*s.d",
527 (int)(tcc_fileextension(target) - target), target);
528 filename = buf;
529 }
530
531 if (s->verbose)
532 printf("<- %s\n", filename);
533
534 /* XXX return err codes instead of error() ? */
535 depout = fopen(filename, "w");
536 if (!depout)
537 tcc_error("could not open '%s'", filename);
538
539 fprintf(depout, "%s: \\\n", target);
540 for (i=0; i<s->nb_target_deps; ++i)
541 fprintf(depout, " %s \\\n", s->target_deps[i]);
542 fprintf(depout, "\n");
543 fclose(depout);
544}
545
546/* -------------------------------------------------------------- */
Note: See TracBrowser for help on using the repository browser.