File: diet.c

package info (click to toggle)
dietlibc 0.34~cvs20160606-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 11,336 kB
  • sloc: ansic: 71,631; asm: 13,006; cpp: 1,860; makefile: 799; sh: 292; perl: 62
file content (577 lines) | stat: -rw-r--r-- 14,384 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <write12.h>

#include "dietfeatures.h"

/* goal:
 *   when invoked as
 * "diet gcc -c t.c"
 *   exec
 * "gcc -I/path/to/dietlibc/include -c t.c"
 *
 *   when invoked as
 * "diet sparc-linux-gcc -o t t.o"
 *   exec
 * "sparc-linux-gcc -nostdlib -static -o t t.o /path/to/dietlibc/bin-sparc/start.o /path/to/dietlibc/bin-sparc/dietlibc.a"
*/

static void error(const char *message) {
  __write2(message);
  exit(1);
}

static const char* Os[] = {
  "i386","-Os","-mpreferred-stack-boundary=4",
	 "-falign-functions=1","-falign-jumps=1",
	 "-falign-loops=1","-fomit-frame-pointer",0,
  "x86_64","-Os",0,
  "x32","-Os",0,
  "sparc","-Os","-mcpu=v9",0,
  "sparc64","-Os","-m64","-mhard-quad-float",0,
  "alpha","-Os","-fomit-frame-pointer",0,
  "arm","-Os","-fomit-frame-pointer",0,
  "aarch64","-Os","-fomit-frame-pointer",0,
  "mips","-Os","-fomit-frame-pointer","-march=mips2",0,
  "mipsel","-Os","-fomit-frame-pointer","-march=mips2",0,
  "mips64","-Os","-fomit-frame-pointer",0,
  "ppc","-Os","-fomit-frame-pointer","-mpowerpc-gpopt","-mpowerpc-gfxopt",0,
  "ppc64","-Os","-fomit-frame-pointer","-mpowerpc-gpopt","-mpowerpc-gfxopt",0,
  "ppc64le","-Os","-fomit-frame-pointer","-mpowerpc-gpopt","-mpowerpc-gfxopt",0,
  "s390","-Os","-fomit-frame-pointer",0,
  "s390x","-Os","-fomit-frame-pointer",0,
  "sh","-Os","-fomit-frame-pointer",0,
  "ia64","-Os","-fno-omit-frame-pointer",0,
  0};

#ifdef INSTALLVERSION
static char *debian_diethome(int argc, char *argv[]) {
  int cc_arg = 1;
  /* All architectures that are currently supported in Debian's
   * dietlibc package. */
  static const char *debian_arch_table[] = {
    "alpha-linux-gnu",
    "x86_64-linux-gnu",
    "aarch64-linux-gnu",
    "arm-linux-gnu",
    "armeb-linux-gnu",
    "arm-linux-gnueabi",
    "arm-linux-gnueabihf",
    "hppa-linux-gnu",
    "i386-linux-gnu",
    "ia64-linux-gnu",
    "mips-linux-gnu",
    "mipsel-linux-gnu",
    "mips64el-linux-gnuabi64",
    "powerpc-linux-gnu",
    "powerpc-linux-gnuspe",
    "powerpc64-linux-gnu",
    "powerpc64le-linux-gnu",
    "s390-linux-gnu",
    "s390x-linux-gnu",
    "sparc-linux-gnu",
    "sparc64-linux-gnu",
    "x86_64-linux-gnux32",
    0
  };
  /* Increase this if there's ever an arch with more than 64 characeters. */
  static char result[64 + sizeof("/usr/lib/") - 1 + sizeof("/diet")];
  const char **debian_arch;
  int m, i;

  /* The following code repeats some stuff that is done in main(), but
   * but we want the dietlibc change to be as uninvasive as possible. */
  m=0;
  for (i=1; i<argc; ++i) {
    if (!strcmp(argv[i],"-m32")) m=32; else
    if (!strcmp(argv[i],"-mx32")) m=33; else
    if (!strcmp(argv[i],"-m64")) m=64;
  }

  while (argv[cc_arg] && (
    strcmp(argv[cc_arg], "-v") == 0 ||
    strcmp(argv[cc_arg], "-L") == 0 ||
    strcmp(argv[cc_arg], "-Os") == 0
  )) cc_arg++;
  if (!argv[cc_arg]) {
    strcpy(result, DIETHOME);
    goto check_m;
  }
  for (debian_arch = debian_arch_table; *debian_arch; debian_arch++) {
    if (strncmp(*debian_arch, argv[cc_arg], strlen(*debian_arch)) == 0 &&
        argv[cc_arg][strlen(*debian_arch)] == '-') {
      strcpy(result, "/usr/lib/");
      strcat(result, *debian_arch);
      strcat(result, "/diet");
      goto check_m;
    }
  }
  /* Unknown cross-compiler, default to /usr/lib/diet */
  if (strstr(argv[cc_arg], "-linux-")) {
    strcpy(result, "/usr/lib/diet");
    return result;
  } else {
    strcpy(result, DIETHOME);
    goto check_m;
  }

check_m:
  /* If x86 is selected, check if multilib -m{32,x32,64} is specified,
   * and use alternative diethome instead. This is required for
   * diet gcc -m32 to work on amd64. */
  if (!strcmp(result, "/usr/lib/x86_64-linux-gnu/diet")
   || !strcmp(result, "/usr/lib/i386-linux-gnu/diet")
   || !strcmp(result, "/usr/lib/x86_64-linux-gnux32/diet")
  ) {
    switch (m) {
      case 32: strcpy(result, "/usr/lib/i386-linux-gnu/diet"); break;
      case 33: strcpy(result, "/usr/lib/x86_64-linux-gnux32/diet"); break;
      case 64: strcpy(result, "/usr/lib/x86_64-linux-gnu/diet"); break;
      default: break;
    }
  }
  return result;
}
#endif

static void usage(void) {
  __write2(
#ifdef __DYN_LIB
	   "dyn-"
#endif
	   "diet version " VERSION
#ifndef INSTALLVERSION
	   " (non-install version in source tree)"
#endif
	   "\n\n");
  error("usage: diet [-v] [-Os] gcc command line\n"
	"e.g.   diet -Os gcc -c t.c\n"
	"or     diet sparc-linux-gcc -o foo foo.c bar.o\n");
}

int main(int argc,char *argv[]) {
  int _link=0;
  int compile=0;
  int preprocess=0;
  int verbose=0;
  int profile=0;
  int cross=0;
  char* diethome;
  char* platform;
#ifdef __DYN_LIB
  int shared=0;
#endif
  char* shortplatform=0;
#ifdef WANT_SAFEGUARD
  char safeguard1[]="-include";
  char* safeguard2;
#endif
  const char *nostdlib="-nostdlib";
  const char *libgcc="-lgcc";
  char *libpthread="-lpthread";
  char dashL[1000];
  /* Older, MultiArch-unaware dietlibc versions would install the libs
   * into /usr/lib/diet/lib, and some packages might still put other
   * static libraries there, so we need to support that for at least
   * one Debian release. */
  char dashL_debian_old[] = "-L/usr/lib/diet/lib";
  char dashstatic[]="-static";
  char dashshared[]="-shared";	// for -fpie
  int i;
  int mangleopts=0;
  int printpath=0;
  char manglebuf[1024];
  int m,pie;

  if (!(diethome = getenv("DIETHOME")))
#ifdef INSTALLVERSION
    diethome=debian_diethome(argc, argv);
#else
    diethome=DIETHOME;
#endif
#ifdef WANT_SAFEGUARD
  safeguard2=alloca(strlen(diethome)+30);
  strcpy(safeguard2, diethome);
  strcat(safeguard2, "/include/dietref.h");
#endif
  platform=alloca(strlen(diethome)+100);
  strcpy(platform,diethome);
#ifdef INSTALLVERSION
  strcat(platform,"/lib-");
#else
#ifndef __DYN_LIB
  strcat(platform,"/bin-");
#else
  strcat(platform,"/pic-");
#endif
#endif
  strcpy(dashL,"-L");

  do {
    if (!argv[1]) usage();
    if (!strcmp(argv[1],"-v")) {
      ++argv; --argc;
      verbose=1;
    } else if (!strcmp(argv[1],"-Os")) {
      ++argv; --argc;
      mangleopts=1;
    } else if (!strcmp(argv[1],"-L")) {
      ++argv; --argc;
      printpath=1;
    } else break;
  } while (1);
  {
    m=0;
    pie=0;
    for (i=1; i<argc; ++i) {
      if (!strcmp(argv[i],"-m32")) m=32; else
      if (!strcmp(argv[i],"-mx32")) m=33; else
      if (!strcmp(argv[i],"-m64")) m=64; else
      if (!strcmp(argv[i],"-fpie")) pie=1; else
      if (!strcmp(argv[i],"-fno-pie")) pie=0;
    }
  }
  {
    char *cc=argv[1];
    char *tmp=strchr(cc,0)-2;
    char *tmp2,*tmp3;
    if (tmp<cc) goto donttouch;
    if (!strstr(cc,"cc") && !strstr(cc,"clang")) goto donttouch;
    if ((tmp2=strstr(cc,"linux-"))) {	/* cross compiling? */
      int len=strlen(platform);
      --tmp2;
      tmp3=strchr(cc,'-');
      if (tmp3<tmp2) tmp2=tmp3;
      if (tmp2-cc>90) error("platform name too long!\n");
      shortplatform=platform+len;
      memmove(shortplatform,argv[1],(size_t)(tmp2-cc));
      platform[tmp2-cc+len]=0;
      if (shortplatform[0]=='i' && shortplatform[2]=='8' && shortplatform[3]=='6') shortplatform[1]='3';
      if (!strncmp(shortplatform, "powerpc", 7)) {
        shortplatform[0] = shortplatform[1] = 'p'; shortplatform[2] = 'c';
        memmove(&shortplatform[3], &shortplatform[7], strlen(&shortplatform[7]) + 1);
      }
      if (!strcmp(shortplatform, "hppa"))
        strcpy(shortplatform, "parisc");
      if (!strcmp(shortplatform, "mips64el"))
        strcpy(shortplatform, "mips64");
      if (!strncmp(cc, "x86_64-linux-gnux32-", 20) || !strncmp(cc, "x86_64-pc-linux-gnux32-", 23))
        strcpy(shortplatform, "x32");
      cross=1;
    } else {
#ifdef __sparc__
#ifdef __arch64__
      shortplatform="sparc64";
#else
      shortplatform="sparc";
#endif
#endif
#ifdef __powerpc__
      shortplatform="ppc";
#endif
#ifdef __powerpc64__
#ifdef __LITTLE_ENDIAN__
      shortplatform="ppc64le";
#else
      shortplatform="ppc64";
#endif
#endif
#ifdef __i386__
      shortplatform="i386";
#endif
#ifdef __alpha__
      shortplatform="alpha";
#endif
#ifdef __arm__
      shortplatform="arm";
#endif
#ifdef __aarch64__
      shortplatform="aarch64";
#endif
#ifdef __mips64__
      shortplatform="mips64";
#else
#ifdef __MIPSEL__
      shortplatform="mipsel";
#endif
#ifdef __MIPSEB__
      shortplatform="mips";
#endif
#endif
#ifdef __s390x__
      shortplatform="s390x";
#else
#ifdef __s390__
      shortplatform="s390";
#endif
#endif
#ifdef __sh__
      shortplatform="sh";
#endif
#ifdef __hppa__
      shortplatform="parisc";
#endif
#ifdef __x86_64__
#ifdef __ILP32__
      shortplatform=(m==32?"i386":(m==64?"x86_64":"x32"));
#else
      shortplatform=(m==32?"i386":(m==33?"x32":"x86_64"));
#endif
#endif
#ifdef __ia64__
      shortplatform="ia64";
#endif
      {
	char *tmp4=platform+strlen(platform);
	strcpy(tmp4,shortplatform);
	shortplatform=tmp4;
      }
    }
    /* MIPS needs special handling.  If argv contains -EL, change
     * platform name to mipsel */
    if (!strcmp(shortplatform,"mips")) {
      for (i=1; i<argc; ++i)
	if (!strcmp(argv[i],"-EL"))
	  strcpy(shortplatform,"mipsel");
    }
    if (printpath) {
      write(1,platform,strlen(platform));
      return 0;
    }
    strcat(dashL,platform);
    if (strcmp(tmp,"ld")) {
      char **newargv;
      char **dest;
      char *a,*b,*c;
#ifdef WANT_DYNAMIC
      char *d,*e,*f;
#endif
/* we need to add -I... if the command line contains -c, -S or -E */
      for (i=2; i<argc; ++i) {
	if (argv[i][0]=='-' && argv[i][1]=='M')
	  goto pp;
	if (!strcmp(argv[i],"-pg"))
	  profile=1;
	if (!strcmp(argv[i],"-c") || !strcmp(argv[i],"-S"))
	  compile=1;
	if (!strcmp(argv[i],"-E"))
pp:
	  preprocess=compile=1;
      }
/* we need to add -nostdlib if we are not compiling */
      _link=!compile;
#ifdef __DYN_LIB
      if (_link) {
	for (i=2; i<argc; ++i)
	  if (!strcmp(argv[i],"-shared")) {
	    shared=1;
	    _link=0;
	  }
      }
#endif
#if 0
      for (i=2; i<argc; ++i)
	if (!strcmp(argv[i],"-o"))
	  if (!compile) _link=1;
#endif

      newargv=alloca(sizeof(char*)*(argc+100));
      a=alloca(strlen(diethome)+20);
      b=alloca(strlen(platform)+20);
      c=alloca(strlen(platform)+20);

      strcpy(a,diethome); strcat(a,"/include");
#ifndef __DYN_LIB
      strcpy(b,platform);
      if (profile)
	strcat(b,"/pstart.o");
      else
	strcat(b,pie ? "/start-pie.o" : "/start.o");
#ifdef INSTALLVERSION
      strcpy(c,platform); strcat(c,"/libc.a");
#else
      strcpy(c,platform); strcat(c,"/dietlibc.a");
#endif
#else
      strcpy(b,platform); strcat(b,"/dstart.o");
      strcpy(c,"-lc");
#endif

#ifdef WANT_DYNAMIC
      d=alloca(strlen(platform)+20);
      e=alloca(strlen(platform)+20);
#ifdef __DYN_LIB
      strcpy(d,platform);
      strcpy(e,platform);
      if (shared)
	strcat(d,"/dyn_so_start.o");
#ifdef INSTALLVERSION
      else
	strcat(d,"/dyn_dstart.o");
      strcat(e,"/dyn_dstop.o");
#else
      else
	strcat(d,"/dyn_start.o");
      strcat(e,"/dyn_stop.o");
#endif
#else
      strcpy(d,platform); strcat(d,"/dyn_start.o");
      strcpy(e,platform); strcat(e,"/dyn_stop.o");
#endif
#endif

      dest=newargv;
      *dest++=argv[1];
      if (argv[2]) {
	if (!strcmp(argv[2],"-V")) {
	  *dest++=argv[2];
	  *dest++=argv[3];
	  argv+=2;
	  argc-=2;
	} else if (!memcmp(argv[2],"-V",2)) {
	  *dest++=argv[2];
	  ++argv;
	  --argc;
	}
      }
#ifndef __DYN_LIB
      if (_link) {
	*dest++=(char*)nostdlib;
	*dest++=pie ? dashshared : dashstatic;
	*dest++=dashL;
	if (!cross)
	  *dest++=dashL_debian_old;
      }
#else
      /* avoid R_*_COPY relocations */
      *dest++="-fPIC";
      if (_link || shared) { *dest++=(char*)nostdlib; *dest++=dashL; if (!cross) *dest++=dashL_debian_old; }
#endif
#ifdef WANT_SAFEGUARD
      if (compile && !preprocess) {
	*dest++=safeguard1;
	*dest++=safeguard2;
      }
#endif
      if (_link) { *dest++=b; }
#ifdef WANT_DYNAMIC
      if (_link) { *dest++=d; }
#endif
      for (i=2; i<argc; ++i) {
	if (!strcmp(argv[i],"-pthread")) {
	  *dest++="-D_REENTRANT";
	  if (_link) *dest++="-lpthread";
	  continue;
	}
#if 0
	if (mangleopts)
	  if (argv[i][0]=='-' && (argv[i][1]=='O' || argv[i][1]=='f' ||
				  (argv[i][1]=='m' && argv[i][2]!='3' && argv[i][2]!='6' && argv[i][2]!='x'))) {
	    if (strcmp(argv[i],"-fpic") && strcmp(argv[i],"-fno-pic") &&
		strcmp(argv[i],"-fpie") && strcmp(argv[i],"-fno-pie") &&
		strncmp(argv[i],"-fvisibility=",13))
	      continue;
	  }
#endif
	*dest++=argv[i];
      }
#ifndef __DYN_LIB
      if (compile || _link) {
	*dest++="-isystem";
	*dest++=a;
      }
#else
      if (compile || _link || shared) {
	*dest++="-isystem";
	*dest++=a;
      }
#endif
      *dest++="-D__dietlibc__";
      if (mangleopts) {
	const char **o=Os;

	{
	  int fd;
	  tmp=getenv("HOME");
	  if (tmp) {
	    if (strlen(tmp)+strlen(cc)<900) {
	      strcpy(manglebuf,tmp);
	      strcat(manglebuf,"/.diet/");
	      tmp=strrchr(cc,'/');
	      if (tmp) ++tmp; else tmp=cc;
	      strcat(manglebuf,tmp);
	      if ((fd=open(manglebuf,O_RDONLY))>=0) {
		int len=read(fd,manglebuf,1023);
		if (len>0) {
		  manglebuf[len]=0;
		  *dest++=manglebuf;
		  for (i=1; i<len; ++i) {
		    if (manglebuf[i]==' ' || manglebuf[i]=='\n') {
		      manglebuf[i]=0;
		      if (i+1<len)
			*dest++=manglebuf+i+1;
		    }
		  }
		  goto incorporated;
		}
	      }
	    }
	  }
	}
	for (o=Os;*o;++o) {
	  if (!strcmp(*o,shortplatform)) {
	    ++o;
	    while (*o) {
	      *dest++=(char*)*o;
	      ++o;
	    }
	    break;
	  } else
	    while (*o) ++o;
	}
      }
incorporated:
      if (_link) {
	if (profile) *dest++="-lgmon";
	*dest++=c; *dest++=(char*)libgcc;
	if (!strcmp(shortplatform,"sparc") ||
	    !strcmp(shortplatform,"sparc64") ||
	    !strncmp(shortplatform,"arm",3)) {
	  *dest++=c;
	}
      }
#ifdef WANT_DYNAMIC
      if (_link) { *dest++=e; }
#endif
#ifdef __DYN_LIB
      if (shared){ *dest++=c; }
      f=alloca(strlen(platform)+100);
      if (_link) {
	strcpy(f,"-Wl,-dynamic-linker=");
	strcat(f,platform);
//	strcat(f,"/diet-linux.so");
	strcat(f,"/libdl.so");
	*dest++=f;
      }
#endif
      *dest=0;
      if (verbose) {
	for (i=0; newargv[i]; i++) {
	  __write2(newargv[i]);
	  __write2(" ");
	}
	__write2("\n");
      }
      execvp(newargv[0],newargv);
      goto error;
    }
  }
donttouch:
  execvp(argv[1],argv+1);
error:
  error("execvp() failed!\n");
  return 1;
}