crunchgen.c revision 1.93 1 /* $NetBSD: crunchgen.c,v 1.93 2019/12/18 02:16:04 christos Exp $ */
2 /*
3 * Copyright (c) 1994 University of Maryland
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
15 *
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 *
23 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
26 */
27 /*
28 * ========================================================================
29 * crunchgen.c
30 *
31 * Generates a Makefile and main C file for a crunched executable,
32 * from specs given in a .conf file.
33 */
34
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38
39 #include <sys/cdefs.h>
40 #if !defined(lint)
41 __RCSID("$NetBSD: crunchgen.c,v 1.93 2019/12/18 02:16:04 christos Exp $");
42 #endif
43
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <stdio.h>
47 #include <ctype.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <err.h>
51 #include <util.h>
52
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <sys/param.h>
56 #include <sys/utsname.h>
57
58 #define CRUNCH_VERSION "20180508"
59
60 #define MAXLINELEN 16384
61 #define MAXFIELDS 2048
62
63 /* internal representation of conf file: */
64
65 /* simple lists of strings suffice for most parms */
66
67 typedef struct strlst {
68 struct strlst *next;
69 char *str;
70 } strlst_t;
71
72 /* progs have structure, each field can be set with "special" or calculated */
73
74 typedef struct prog {
75 struct prog *next;
76 char *name, *ident;
77 char *srcdir, *objdir;
78 strlst_t *objs, *objpaths;
79 strlst_t *links, *keepsymbols;
80 int goterror;
81 } prog_t;
82
83
84 /* global state */
85
86 static strlst_t *srcdirs = NULL;
87 static strlst_t *libs = NULL;
88 static strlst_t *vars = NULL;
89 static prog_t *progs = NULL;
90
91 static char line[MAXLINELEN];
92
93 static char confname[MAXPATHLEN], infilename[MAXPATHLEN];
94 static char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
95 static char cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
96 static char curdir[MAXPATHLEN];
97 static char topdir[MAXPATHLEN];
98 static char libdir[MAXPATHLEN] = "/usr/lib";
99 static char dbg[MAXPATHLEN] = "-Os";
100 static int linenum = -1;
101 static int goterror = 0;
102
103 static const char *pname = "crunchgen";
104
105 /* options */
106 static int verbose, readcache, useobjs, oneobj, pie, libcsanitizer, sanitizer;
107 static int ssp, fortify;
108
109 static int reading_cache;
110 static char *machine;
111 static char *makeobjdirprefix;
112 static char *makebin;
113 static char *makeflags;
114
115 /* general library routines */
116
117 static void status(const char *str);
118 __dead static void out_of_memory(void);
119 static void add_string(strlst_t **listp, char *str);
120 static int is_dir(const char *pathname);
121 static int is_nonempty_file(const char *pathname);
122
123 /* helper routines for main() */
124
125 __dead static void usage(void);
126 static void parse_conf_file(void);
127 static void gen_outputs(void);
128
129 extern char *crunched_skel[];
130
131 int
132 main(int argc, char **argv)
133 {
134 char *p;
135 int optc;
136
137 if ((makebin = getenv("MAKE")) == NULL)
138 makebin = strdup("make");
139
140 if ((makeflags = getenv("MAKEFLAGS")) == NULL)
141 makeflags = strdup("");
142
143 if ((machine = getenv("MACHINE")) == NULL) {
144 static struct utsname utsname;
145
146 if (uname(&utsname) == -1) {
147 perror("uname");
148 exit(1);
149 }
150 machine = utsname.machine;
151 }
152 makeobjdirprefix = getenv("MAKEOBJDIRPREFIX");
153 verbose = 1;
154 readcache = 1;
155 useobjs = 0;
156 oneobj = 1;
157 pie = 0;
158 *outmkname = *outcfname = *execfname = '\0';
159
160 if (argc > 0)
161 pname = argv[0];
162
163 while ((optc = getopt(argc, argv, "m:c:d:e:FfopPqsD:L:Ov:")) != -1) {
164 switch(optc) {
165 case 'f': readcache = 0; break;
166 case 'F': fortify = 1; break;
167 case 'p': pie = 1; break;
168 case 'P': ssp = 1; break;
169 case 'q': verbose = 0; break;
170 case 'O': oneobj = 0; break;
171 case 'o': useobjs = 1, oneobj = 0; break;
172 case 's': sanitizer = 1; break;
173 case 'S': libcsanitizer = 1; break;
174
175 case 'm': (void)estrlcpy(outmkname, optarg, sizeof(outmkname)); break;
176 case 'c': (void)estrlcpy(outcfname, optarg, sizeof(outcfname)); break;
177 case 'e': (void)estrlcpy(execfname, optarg, sizeof(execfname)); break;
178 case 'd': (void)estrlcpy(dbg, optarg, sizeof(dbg)); break;
179
180 case 'D': (void)estrlcpy(topdir, optarg, sizeof(topdir)); break;
181 case 'L': (void)estrlcpy(libdir, optarg, sizeof(libdir)); break;
182 case 'v': add_string(&vars, optarg); break;
183
184 case '?':
185 default: usage();
186 }
187 }
188
189 argc -= optind;
190 argv += optind;
191
192 if (argc != 1)
193 usage();
194
195 /*
196 * generate filenames
197 */
198
199 (void)estrlcpy(infilename, argv[0], sizeof(infilename));
200 getcwd(curdir, MAXPATHLEN);
201
202 /* confname = `basename infilename .conf` */
203
204 if ((p = strrchr(infilename, '/')) != NULL)
205 (void)estrlcpy(confname, p + 1, sizeof(confname));
206 else
207 (void)estrlcpy(confname, infilename, sizeof(confname));
208 if ((p = strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
209 *p = '\0';
210
211 if (!*outmkname)
212 (void)snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
213 if (!*outcfname)
214 (void)snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
215 if (!*execfname)
216 (void)snprintf(execfname, sizeof(execfname), "%s", confname);
217
218 (void)snprintf(cachename, sizeof(cachename), "%s.cache", confname);
219
220 parse_conf_file();
221 gen_outputs();
222
223 exit(goterror);
224 }
225
226
227 void
228 usage(void)
229 {
230 fprintf(stderr,
231 "%s [-FfOoPpqSs] [-c c-file-name] [-D src-root] [-d build-options]\n"
232 "\t [-e exec-file-name] [-L lib-dir] [-m makefile-name]\n"
233 "\t [-v var-spec] conf-file\n", pname);
234 exit(1);
235 }
236
237
238 /*
239 * ========================================================================
240 * parse_conf_file subsystem
241 *
242 */
243
244 /* helper routines for parse_conf_file */
245
246 static void parse_one_file(char *filename);
247 static void parse_line(char *line, int *fc, char **fv, int nf);
248 static void add_srcdirs(int argc, char **argv);
249 static void add_progs(int argc, char **argv);
250 static void add_link(int argc, char **argv);
251 static void add_libs(int argc, char **argv);
252 static void add_special(int argc, char **argv);
253
254 static prog_t *find_prog(char *str);
255 static void add_prog(char *progname);
256
257
258 static void
259 parse_conf_file(void)
260 {
261 if (!is_nonempty_file(infilename)) {
262 fprintf(stderr, "%s: fatal: input file \"%s\" not found.\n",
263 pname, infilename);
264 exit(1);
265 }
266 parse_one_file(infilename);
267 if (readcache && is_nonempty_file(cachename)) {
268 reading_cache = 1;
269 parse_one_file(cachename);
270 }
271 }
272
273
274 static void
275 parse_one_file(char *filename)
276 {
277 char *fieldv[MAXFIELDS];
278 int fieldc;
279 void (*f)(int c, char **v);
280 FILE *cf;
281
282 (void)snprintf(line, sizeof(line), "reading %s", filename);
283 status(line);
284 (void)estrlcpy(curfilename, filename, sizeof(curfilename));
285
286 if ((cf = fopen(curfilename, "r")) == NULL) {
287 perror(curfilename);
288 goterror = 1;
289 return;
290 }
291
292 linenum = 0;
293 while (fgets(line, MAXLINELEN, cf) != NULL) {
294 linenum++;
295 parse_line(line, &fieldc, fieldv, MAXFIELDS);
296 if (fieldc < 1)
297 continue;
298 if (!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs;
299 else if (!strcmp(fieldv[0], "progs")) f = add_progs;
300 else if (!strcmp(fieldv[0], "ln")) f = add_link;
301 else if (!strcmp(fieldv[0], "libs")) f = add_libs;
302 else if (!strcmp(fieldv[0], "special")) f = add_special;
303 else {
304 fprintf(stderr, "%s:%d: skipping unknown command `%s'.\n",
305 curfilename, linenum, fieldv[0]);
306 goterror = 1;
307 continue;
308 }
309 if (fieldc < 2) {
310 fprintf(stderr,
311 "%s:%d: %s command needs at least 1 argument, skipping.\n",
312 curfilename, linenum, fieldv[0]);
313 goterror = 1;
314 continue;
315 }
316 f(fieldc, fieldv);
317 }
318
319 if (ferror(cf)) {
320 perror(curfilename);
321 goterror = 1;
322 }
323 fclose(cf);
324 }
325
326
327 static void
328 parse_line(char *pline, int *fc, char **fv, int nf)
329 {
330 char *p;
331
332 p = pline;
333 *fc = 0;
334 for (;;) {
335 while (isspace((unsigned char)*p))
336 p++;
337 if (*p == '\0' || *p == '#')
338 break;
339
340 if (*fc < nf)
341 fv[(*fc)++] = p;
342 while (*p && !isspace((unsigned char)*p) && *p != '#')
343 p++;
344 if (*p == '\0' || *p == '#')
345 break;
346 *p++ = '\0';
347 }
348 if (*p)
349 *p = '\0'; /* needed for '#' case */
350 }
351
352
353 static void
354 add_srcdirs(int argc, char **argv)
355 {
356 int i;
357 char tmppath[MAXPATHLEN];
358
359 for (i = 1; i < argc; i++) {
360 if (argv[i][0] == '/')
361 (void)estrlcpy(tmppath, argv[i], sizeof(tmppath));
362 else {
363 if (topdir[0] == '\0')
364 (void)estrlcpy(tmppath, curdir, sizeof(tmppath));
365 else
366 (void)estrlcpy(tmppath, topdir, sizeof(tmppath));
367 (void)estrlcat(tmppath, "/", sizeof(tmppath));
368 (void)estrlcat(tmppath, argv[i], sizeof(tmppath));
369 }
370 if (is_dir(tmppath))
371 add_string(&srcdirs, tmppath);
372 else {
373 fprintf(stderr, "%s:%d: `%s' is not a directory, skipping it.\n",
374 curfilename, linenum, tmppath);
375 goterror = 1;
376 }
377 }
378 }
379
380
381 static void
382 add_progs(int argc, char **argv)
383 {
384 int i;
385
386 for (i = 1; i < argc; i++)
387 add_prog(argv[i]);
388 }
389
390
391 static void
392 add_prog(char *progname)
393 {
394 prog_t *p1, *p2;
395
396 /* add to end, but be smart about dups */
397
398 for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
399 if (!strcmp(p2->name, progname))
400 return;
401
402 p2 = malloc(sizeof(prog_t));
403 if (p2)
404 p2->name = strdup(progname);
405 if (!p2 || !p2->name)
406 out_of_memory();
407
408 p2->next = NULL;
409 if (p1 == NULL)
410 progs = p2;
411 else
412 p1->next = p2;
413
414 p2->ident = p2->srcdir = p2->objdir = NULL;
415 p2->objs = p2->objpaths = p2->links = p2->keepsymbols = NULL;
416 p2->goterror = 0;
417 }
418
419
420 static void
421 add_link(int argc, char **argv)
422 {
423 int i;
424 prog_t *p = find_prog(argv[1]);
425
426 if (p == NULL) {
427 fprintf(stderr,
428 "%s:%d: no prog %s previously declared, skipping link.\n",
429 curfilename, linenum, argv[1]);
430 goterror = 1;
431 return;
432 }
433 for (i = 2; i < argc; i++)
434 add_string(&p->links, argv[i]);
435 }
436
437
438 static void
439 add_libs(int argc, char **argv)
440 {
441 int i;
442
443 for (i = 1; i < argc; i++)
444 add_string(&libs, argv[i]);
445 }
446
447
448 static void
449 add_special(int argc, char **argv)
450 {
451 int i;
452 prog_t *p = find_prog(argv[1]);
453
454 if (p == NULL) {
455 if (reading_cache)
456 return;
457 fprintf(stderr,
458 "%s:%d: no prog %s previously declared, skipping special.\n",
459 curfilename, linenum, argv[1]);
460 goterror = 1;
461 return;
462 }
463
464 if (!strcmp(argv[2], "ident")) {
465 if (argc != 4)
466 goto argcount;
467 if ((p->ident = strdup(argv[3])) == NULL)
468 out_of_memory();
469 return;
470 }
471
472 if (!strcmp(argv[2], "srcdir")) {
473 if (argc != 4)
474 goto argcount;
475 if (argv[3][0] == '/') {
476 if ((p->srcdir = strdup(argv[3])) == NULL)
477 out_of_memory();
478 } else {
479 char tmppath[MAXPATHLEN];
480 if (topdir[0] == '\0')
481 (void)estrlcpy(tmppath, curdir, sizeof(tmppath));
482 else
483 (void)estrlcpy(tmppath, topdir, sizeof(tmppath));
484 (void)estrlcat(tmppath, "/", sizeof(tmppath));
485 (void)estrlcat(tmppath, argv[3], sizeof(tmppath));
486 if ((p->srcdir = strdup(tmppath)) == NULL)
487 out_of_memory();
488 }
489 return;
490 }
491
492 if (!strcmp(argv[2], "objdir")) {
493 if (argc != 4)
494 goto argcount;
495 if ((p->objdir = strdup(argv[3])) == NULL)
496 out_of_memory();
497 return;
498 }
499
500 if (!strcmp(argv[2], "objs")) {
501 oneobj = 0;
502 for (i = 3; i < argc; i++)
503 add_string(&p->objs, argv[i]);
504 return;
505 }
506
507 if (!strcmp(argv[2], "objpaths")) {
508 oneobj = 0;
509 for (i = 3; i < argc; i++)
510 add_string(&p->objpaths, argv[i]);
511 return;
512 }
513
514 if (!strcmp(argv[2], "keepsymbols")) {
515 for (i = 3; i < argc; i++)
516 add_string(&p->keepsymbols, argv[i]);
517 return;
518 }
519
520 fprintf(stderr, "%s:%d: bad parameter name `%s', skipping line.\n",
521 curfilename, linenum, argv[2]);
522 goterror = 1;
523 return;
524
525 argcount:
526 fprintf(stderr,
527 "%s:%d: too %s arguments, expected \"special %s %s <string>\".\n",
528 curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
529 goterror = 1;
530 }
531
532
533 static prog_t *
534 find_prog(char *str)
535 {
536 prog_t *p;
537
538 for (p = progs; p != NULL; p = p->next)
539 if (!strcmp(p->name, str))
540 return p;
541
542 return NULL;
543 }
544
545
546 /*
547 * ========================================================================
548 * gen_outputs subsystem
549 *
550 */
551
552 /* helper subroutines */
553
554 static void remove_error_progs(void);
555 static void fillin_program(prog_t *p);
556 static void gen_specials_cache(void);
557 static void gen_output_makefile(void);
558 static void gen_output_cfile(void);
559
560 static void fillin_program_objs(prog_t *p, char *path);
561 static void top_makefile_rules(FILE *outmk);
562 static void bottom_makefile_rules(FILE *outmk);
563 static void prog_makefile_rules(FILE *outmk, prog_t *p, const char *);
564 static void output_strlst(FILE *outf, strlst_t *lst);
565 static char *genident(char *str);
566 static char *dir_search(char *progname);
567
568
569 static void
570 gen_outputs(void)
571 {
572 prog_t *p;
573
574 for (p = progs; p != NULL; p = p->next)
575 fillin_program(p);
576
577 remove_error_progs();
578 gen_specials_cache();
579 gen_output_cfile();
580 gen_output_makefile();
581 status("");
582 fprintf(stderr,
583 "Run \"make -f %s objs exe\" to build crunched binary.\n",
584 outmkname);
585 }
586
587
588 static void
589 fillin_program(prog_t *p)
590 {
591 char path[MAXPATHLEN];
592 char *srcparent;
593 strlst_t *s;
594
595 (void)snprintf(line, sizeof(line), "filling in parms for %s", p->name);
596 status(line);
597
598 if (!p->ident)
599 p->ident = genident(p->name);
600 if (!p->srcdir) {
601 srcparent = dir_search(p->name);
602 if (srcparent) {
603 (void)snprintf(path, sizeof(path), "%s/%s", srcparent, p->name);
604 if (is_dir(path)) {
605 if (path[0] == '/') {
606 if ((p->srcdir = strdup(path)) == NULL)
607 out_of_memory();
608 } else {
609 char tmppath[MAXPATHLEN];
610 if (topdir[0] == '\0')
611 (void)estrlcpy(tmppath, curdir, sizeof(tmppath));
612 else
613 (void)estrlcpy(tmppath, topdir, sizeof(tmppath));
614 (void)estrlcat(tmppath, "/", sizeof(tmppath));
615 (void)estrlcat(tmppath, path, sizeof(tmppath));
616 if ((p->srcdir = strdup(tmppath)) == NULL)
617 out_of_memory();
618 }
619 }
620 }
621 }
622
623 if (!p->srcdir && verbose)
624 fprintf(stderr, "%s: %s: warning: could not find source directory.\n",
625 infilename, p->name);
626
627 if (!p->objdir && p->srcdir && useobjs) {
628 if (makeobjdirprefix) {
629 (void)snprintf(path, sizeof(path), "%s/%s", makeobjdirprefix, p->srcdir);
630 if (is_dir(path))
631 p->objdir = strdup(path);
632 }
633 if (!p->objdir) {
634 (void)snprintf(path, sizeof(path), "%s/obj.%s", p->srcdir, machine);
635 if (is_dir(path))
636 p->objdir = strdup(path);
637 }
638 if (!p->objdir) {
639 (void)snprintf(path, sizeof(path), "%s/obj", p->srcdir);
640 if (is_dir(path))
641 p->objdir = strdup(path);
642 }
643 if (!p->objdir) {
644 p->objdir = p->srcdir;
645 }
646 }
647
648 if (oneobj)
649 return;
650
651 if (p->srcdir)
652 (void)snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
653 if (!p->objs && p->srcdir && is_nonempty_file(path))
654 fillin_program_objs(p, p->srcdir);
655
656 if (!p->objpaths && p->objs) {
657 char *objdir;
658 if (p->objdir && useobjs)
659 objdir = p->objdir;
660 else
661 objdir = p->ident;
662 for (s = p->objs; s != NULL; s = s->next) {
663 (void)snprintf(line, sizeof(line), "%s/%s", objdir, s->str);
664 add_string(&p->objpaths, line);
665 }
666 }
667
668 if (!p->objs && verbose)
669 fprintf(stderr, "%s: %s: warning: could not find any .o files.\n",
670 infilename, p->name);
671
672 if (!p->objpaths) {
673 fprintf(stderr,
674 "%s: %s: error: no objpaths specified or calculated.\n",
675 infilename, p->name);
676 p->goterror = goterror = 1;
677 }
678 }
679
680 static void
681 fillin_program_objs(prog_t *p, char *dirpath)
682 {
683 char *obj, *cp;
684 int rc;
685 int fd;
686 FILE *f;
687 char tempfname[MAXPATHLEN];
688
689 /* discover the objs from the srcdir Makefile */
690
691 (void)snprintf(tempfname, sizeof(tempfname), "/tmp/%sXXXXXX", confname);
692 if ((fd = mkstemp(tempfname)) < 0) {
693 perror(tempfname);
694 exit(1);
695 }
696
697 if ((f = fdopen(fd, "w")) == NULL) {
698 perror(tempfname);
699 goterror = 1;
700 return;
701 }
702
703 fprintf(f, ".include \"${.CURDIR}/Makefile\"\n");
704 fprintf(f, ".if defined(PROG)\n");
705 fprintf(f, "OBJS?= ${PROG}.o\n");
706 fprintf(f, ".endif\n");
707 fprintf(f, "crunchgen_objs:\n\t@echo 'OBJS= '${OBJS}\n");
708 fclose(f);
709
710 (void)snprintf(line, sizeof(line),
711 "cd %s && %s -B -f %s %s CRUNCHEDPROG=1 crunchgen_objs 2>&1", dirpath,
712 makebin, tempfname, makeflags);
713 if ((f = popen(line, "r")) == NULL) {
714 perror("submake pipe");
715 goterror = 1;
716 unlink(tempfname);
717 return;
718 }
719
720 while (fgets(line, MAXLINELEN, f)) {
721 if (strncmp(line, "OBJS= ", 6)) {
722 if (strcmp(line,
723 "sh: warning: running as root with dot in PATH\n") == 0)
724 continue;
725 fprintf(stderr, "make error: %s", line);
726 goterror = 1;
727 continue;
728 }
729 cp = line + 6;
730 while (isspace((unsigned char)*cp))
731 cp++;
732 while (*cp) {
733 obj = cp;
734 while (*cp && !isspace((unsigned char)*cp))
735 cp++;
736 if (*cp)
737 *cp++ = '\0';
738 add_string(&p->objs, obj);
739 while (isspace((unsigned char)*cp))
740 cp++;
741 }
742 }
743 if ((rc=pclose(f)) != 0) {
744 fprintf(stderr, "make error: make returned %d\n", rc);
745 goterror = 1;
746 }
747 unlink(tempfname);
748 }
749
750 static void
751 remove_error_progs(void)
752 {
753 prog_t *p1, *p2;
754
755 p1 = NULL; p2 = progs;
756 while (p2 != NULL) {
757 if (!p2->goterror)
758 p1 = p2, p2 = p2->next;
759 else {
760 /* delete it from linked list */
761 fprintf(stderr, "%s: %s: ignoring program because of errors.\n",
762 infilename, p2->name);
763 if (p1)
764 p1->next = p2->next;
765 else
766 progs = p2->next;
767 p2 = p2->next;
768 }
769 }
770 }
771
772 static void
773 gen_specials_cache(void)
774 {
775 FILE *cachef;
776 prog_t *p;
777
778 (void)snprintf(line, sizeof(line), "generating %s", cachename);
779 status(line);
780
781 if ((cachef = fopen(cachename, "w")) == NULL) {
782 perror(cachename);
783 goterror = 1;
784 return;
785 }
786
787 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen %s\n\n",
788 cachename, infilename, CRUNCH_VERSION);
789
790 for (p = progs; p != NULL; p = p->next) {
791 fprintf(cachef, "\n");
792 if (p->srcdir)
793 fprintf(cachef, "special %s srcdir %s\n", p->name, p->srcdir);
794 if (p->objdir && useobjs)
795 fprintf(cachef, "special %s objdir %s\n", p->name, p->objdir);
796 if (p->objs) {
797 fprintf(cachef, "special %s objs", p->name);
798 output_strlst(cachef, p->objs);
799 }
800 if (p->objpaths) {
801 fprintf(cachef, "special %s objpaths", p->name);
802 output_strlst(cachef, p->objpaths);
803 }
804 }
805 fclose(cachef);
806 }
807
808
809 static void
810 addno(char *str, size_t len, const char *stem)
811 {
812 char buf[128];
813 snprintf(buf, sizeof(buf), "NO%s=\n", stem);
814 strlcat(str, buf, len);
815 }
816
817
818 static void
819 gen_output_makefile(void)
820 {
821 prog_t *p;
822 FILE *outmk;
823 char noes[1024], *ptr;
824
825 noes[0] = '\0';
826
827 if (!pie)
828 addno(noes, sizeof(noes), "PIE");
829 if (!ssp)
830 addno(noes, sizeof(noes), "SSP");
831 if (!fortify)
832 addno(noes, sizeof(noes), "FORT");
833 if (!libcsanitizer)
834 addno(noes, sizeof(noes), "LIBCSANITIZER");
835 if (!sanitizer)
836 addno(noes, sizeof(noes), "SANITIZER");
837
838 addno(noes, sizeof(noes), "MAN");
839
840
841 (void)snprintf(line, sizeof(line), "generating %s", outmkname);
842 status(line);
843
844 if ((outmk = fopen(outmkname, "w")) == NULL) {
845 perror(outmkname);
846 goterror = 1;
847 return;
848 }
849
850 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
851 outmkname, infilename, CRUNCH_VERSION);
852
853 fprintf(outmk, "%s\n", noes);
854 while ((ptr = strchr(noes, '\n')) != NULL)
855 *ptr = ' ';
856
857 top_makefile_rules(outmk);
858
859 for (p = progs; p != NULL; p = p->next)
860 prog_makefile_rules(outmk, p, noes);
861
862 fprintf(outmk, "\n.include <bsd.prog.mk>\n");
863 fprintf(outmk, "\n# ========\n");
864
865 bottom_makefile_rules(outmk);
866
867 fclose(outmk);
868 }
869
870
871 static void
872 gen_output_cfile(void)
873 {
874 char **cp;
875 FILE *outcf;
876 prog_t *p;
877 strlst_t *s;
878
879 (void)snprintf(line, sizeof(line), "generating %s", outcfname);
880 status(line);
881
882 if ((outcf = fopen(outcfname, "w")) == NULL) {
883 perror(outcfname);
884 goterror = 1;
885 return;
886 }
887
888 fprintf(outcf,
889 "/* %s - generated from %s by crunchgen %s */\n",
890 outcfname, infilename, CRUNCH_VERSION);
891
892 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
893 for (cp = crunched_skel; *cp != NULL; cp++)
894 fprintf(outcf, "%s\n", *cp);
895
896 for (p = progs; p != NULL; p = p->next)
897 fprintf(outcf, "extern int _crunched_%s_stub(int, char **, char **);\n",
898 p->ident);
899
900 fprintf(outcf, "\nstatic const struct stub entry_points[] = {\n");
901 for (p = progs; p != NULL; p = p->next) {
902 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
903 p->name, p->ident);
904 for (s = p->links; s != NULL; s = s->next)
905 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
906 s->str, p->ident);
907 }
908
909 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
910 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
911 fclose(outcf);
912 }
913
914
915 static char *
916 genident(char *str)
917 {
918 char *n,*s,*d;
919
920 /*
921 * generates a Makefile/C identifier from a program name, mapping '-' to
922 * '_' and ignoring all other non-identifier characters. This leads to
923 * programs named "foo.bar" and "foobar" to map to the same identifier.
924 */
925
926 if ((n = strdup(str)) == NULL)
927 return NULL;
928 for (d = s = n; *s != '\0'; s++) {
929 if (*s == '-')
930 *d++ = '_';
931 else
932 if (*s == '_' || isalnum((unsigned char)*s))
933 *d++ = *s;
934 }
935 *d = '\0';
936 return n;
937 }
938
939
940 static char *
941 dir_search(char *progname)
942 {
943 char path[MAXPATHLEN];
944 strlst_t *dir;
945
946 for (dir=srcdirs; dir != NULL; dir=dir->next) {
947 snprintf(path, sizeof(path), "%s/%s/Makefile", dir->str, progname);
948 if (is_nonempty_file(path))
949 return dir->str;
950 }
951 return NULL;
952 }
953
954
955 static void
956 top_makefile_rules(FILE *outmk)
957 {
958 prog_t *p;
959
960 fprintf(outmk, "DBG=%s\n", dbg);
961 fprintf(outmk, "MAKE?=make\n");
962 #ifdef NEW_TOOLCHAIN
963 fprintf(outmk, "OBJCOPY?=objcopy\n");
964 fprintf(outmk, "NM?=nm\n");
965 fprintf(outmk, "AWK?=awk\n");
966 #else
967 fprintf(outmk, "CRUNCHIDE?=crunchide\n");
968 #endif
969
970 fprintf(outmk, "CRUNCHED_OBJS=");
971 for (p = progs; p != NULL; p = p->next)
972 fprintf(outmk, " %s.cro", p->name);
973 fprintf(outmk, "\n");
974 fprintf(outmk, "DPADD+= ${CRUNCHED_OBJS}\n");
975 fprintf(outmk, "LDADD+= ${CRUNCHED_OBJS} ");
976 output_strlst(outmk, libs);
977 fprintf(outmk, "CRUNCHEDOBJSDIRS=");
978 for (p = progs; p != NULL; p = p->next)
979 fprintf(outmk, " %s", p->ident);
980 fprintf(outmk, "\n\n");
981
982 fprintf(outmk, "SUBMAKE_TARGETS=");
983 for (p = progs; p != NULL; p = p->next)
984 fprintf(outmk, " %s_make", p->ident);
985 fprintf(outmk, "\n\n");
986
987 fprintf(outmk, "LDSTATIC=-static%s\n\n", pie ? " -pie" : "");
988 fprintf(outmk, "PROG=%s\n\n", execfname);
989
990 fprintf(outmk, "OBJCOPY_REMOVE_FLAGS=-R .eh_frame_hdr -R .note -R .note.netbsd.pax -R .ident -R .comment -R .copyright\n\n");
991
992 fprintf(outmk, "OBJCOPY_REMOVE_FLAGS+=-R .eh_frame\n");
993 fprintf(outmk, ".if ${MACHINE} != \"sparc64\"\n");
994 fprintf(outmk, "OBJCOPY_REMOVE_FLAGS+=-R .note.netbsd.mcmodel\n");
995 fprintf(outmk, ".endif\n\n");
996
997 fprintf(outmk, "all: ${PROG}.crunched\n");
998 fprintf(outmk, "${PROG}.crunched: ${SUBMAKE_TARGETS} .WAIT ${PROG}.strip\n");
999 fprintf(outmk, "${PROG}.strip:\n");
1000 fprintf(outmk, "\t${MAKE} -f ${PROG}.mk ${PROG}\n");
1001 fprintf(outmk, "\t@[ -f ${PROG}.unstripped -a ! ${PROG} -nt ${PROG}.unstripped ] || { \\\n");
1002 fprintf(outmk, "\t\t${_MKSHMSG:Uecho} \" strip \" ${PROG}; \\\n");
1003 fprintf(outmk, "\t\tcp ${PROG} ${PROG}.unstripped && \\\n");
1004 fprintf(outmk, "\t\t${OBJCOPY} -S ${OBJCOPY_REMOVE_FLAGS} ${PROG} && \\\n");
1005 fprintf(outmk, "\t\ttouch ${PROG}.unstripped; \\\n");
1006 fprintf(outmk, "\t}\n");
1007 fprintf(outmk, "objs: $(SUBMAKE_TARGETS)\n");
1008 fprintf(outmk, "exe: %s\n", execfname);
1009 fprintf(outmk, "clean:\n\trm -rf %s *.cro *.cro.syms *.o *_stub.c ${CRUNCHEDOBJSDIRS} ${PROG}.unstripped\n",
1010 execfname);
1011 }
1012
1013
1014 static void
1015 bottom_makefile_rules(FILE *outmk)
1016 {
1017 }
1018
1019
1020 static void
1021 prog_makefile_rules(FILE *outmk, prog_t *p, const char *noes)
1022 {
1023 strlst_t *lst;
1024
1025 fprintf(outmk, "\n# -------- %s\n\n", p->name);
1026
1027 fprintf(outmk, "%s_OBJPATHS=", p->ident);
1028 #ifndef NEW_TOOLCHAIN
1029 fprintf(outmk, " %s_stub.o", p->name);
1030 #endif
1031 if (p->objs)
1032 output_strlst(outmk, p->objpaths);
1033 else
1034 fprintf(outmk, " %s/%s.ro\n", p->ident, p->name);
1035
1036 if (p->srcdir && !useobjs) {
1037 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1038 if (p->objs) {
1039 fprintf(outmk, "%s_OBJS=", p->ident);
1040 output_strlst(outmk, p->objs);
1041 }
1042 fprintf(outmk, "%s_make: %s .PHONY\n", p->ident, p->ident);
1043 fprintf(outmk, "\t( cd %s; printf '.PATH: ${%s_SRCDIR}\\n"
1044 ".CURDIR:= ${%s_SRCDIR}\\n"
1045 ".include \"$${.CURDIR}/Makefile\"\\n",
1046 p->ident, p->ident, p->ident);
1047 for (lst = vars; lst != NULL; lst = lst->next)
1048 fprintf(outmk, "%s\\n", lst->str);
1049 fprintf(outmk, "'\\\n");
1050 #define MAKECMD \
1051 "\t| ${MAKE} -f- CRUNCHEDPROG=1 %sDBG=${DBG:Q} LDSTATIC=${LDSTATIC:Q} "
1052 fprintf(outmk, MAKECMD "depend", noes);
1053 fprintf(outmk, " )\n");
1054 fprintf(outmk, "\t( cd %s; printf '.PATH: ${%s_SRCDIR}\\n"
1055 ".CURDIR:= ${%s_SRCDIR}\\n"
1056 ".include \"$${.CURDIR}/Makefile\"\\n",
1057 p->ident, p->ident, p->ident);
1058 for (lst = vars; lst != NULL; lst = lst->next)
1059 fprintf(outmk, "%s\\n", lst->str);
1060 fprintf(outmk, "'\\\n");
1061 fprintf(outmk, MAKECMD, noes);
1062 if (p->objs)
1063 fprintf(outmk, "${%s_OBJS} ) \n\n", p->ident);
1064 else
1065 fprintf(outmk, "%s.ro ) \n\n", p->name);
1066 } else
1067 fprintf(outmk, "%s_make:\n\t@echo \"** Using existing objs for %s\"\n\n",
1068 p->ident, p->name);
1069
1070 #ifdef NEW_TOOLCHAIN
1071 fprintf(outmk, "%s:\n\t mkdir %s\n", p->ident, p->ident);
1072 #endif
1073 fprintf(outmk, "%s.cro: %s .WAIT ${%s_OBJPATHS}\n",
1074 p->name, p->ident, p->ident);
1075
1076 #ifdef NEW_TOOLCHAIN
1077 if (p->objs)
1078 fprintf(outmk, "\t${LD} -r -o %s/%s.ro $(%s_OBJPATHS)\n",
1079 p->ident, p->name, p->ident);
1080 /* Use one awk command.... */
1081 fprintf(outmk, "\t${NM} -ng %s/%s.ro | ${AWK} '/^ *U / { next };",
1082 p->ident, p->name);
1083 fprintf(outmk, " /^[0-9a-fA-F]+ C/ { next };");
1084 for (lst = p->keepsymbols; lst != NULL; lst = lst->next)
1085 fprintf(outmk, " / %s$$/ { next };", lst->str);
1086 fprintf(outmk, " / main$$/ { print \"main _crunched_%s_stub\"; next };",
1087 p->ident);
1088 /* gdb thinks these are C++ and ignores everthing after the first $$. */
1089 fprintf(outmk, " { print $$3 \" \" $$3 \"$$$$from$$$$%s\" }' "
1090 "> %s.cro.syms\n", p->name, p->name);
1091 fprintf(outmk, "\t${OBJCOPY} --redefine-syms %s.cro.syms ", p->name);
1092 fprintf(outmk, "%s/%s.ro %s.cro\n", p->ident, p->name, p->name);
1093 #else
1094 fprintf(outmk, "\t${LD} -dc -r -o %s.cro $(%s_OBJPATHS)\n",
1095 p->name, p->ident);
1096 fprintf(outmk, "\t${CRUNCHIDE} -k _crunched_%s_stub ", p->ident);
1097 for (lst = p->keepsymbols; lst != NULL; lst = lst->next)
1098 fprintf(outmk, "-k %s ", lst->str);
1099 fprintf(outmk, "%s.cro\n", p->name);
1100 fprintf(outmk, "%s_stub.c:\n", p->name);
1101 fprintf(outmk, "\techo \""
1102 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1103 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1104 p->ident, p->name);
1105 #endif
1106 }
1107
1108 static void
1109 output_strlst(FILE *outf, strlst_t *lst)
1110 {
1111 for (; lst != NULL; lst = lst->next)
1112 fprintf(outf, " %s", lst->str);
1113 fprintf(outf, "\n");
1114 }
1115
1116
1117 /*
1118 * ========================================================================
1119 * general library routines
1120 *
1121 */
1122
1123 static void
1124 status(const char *str)
1125 {
1126 static int lastlen = 0;
1127 int len, spaces;
1128
1129 if (!verbose)
1130 return;
1131
1132 len = strlen(str);
1133 spaces = lastlen - len;
1134 if (spaces < 1)
1135 spaces = 1;
1136
1137 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1138 fflush(stderr);
1139 lastlen = len;
1140 }
1141
1142
1143 static void
1144 out_of_memory(void)
1145 {
1146 fprintf(stderr, "%s: %d: out of memory, stopping.\n", infilename, linenum);
1147 exit(1);
1148 }
1149
1150
1151 static void
1152 add_string(strlst_t **listp, char *str)
1153 {
1154 strlst_t *p1, *p2;
1155
1156 /* add to end, but be smart about dups */
1157
1158 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1159 if (!strcmp(p2->str, str))
1160 return;
1161
1162 p2 = malloc(sizeof(strlst_t));
1163 if (p2)
1164 p2->str = strdup(str);
1165 if (!p2 || !p2->str)
1166 out_of_memory();
1167
1168 p2->next = NULL;
1169 if (p1 == NULL)
1170 *listp = p2;
1171 else
1172 p1->next = p2;
1173 }
1174
1175
1176 static int
1177 is_dir(const char *pathname)
1178 {
1179 struct stat buf;
1180
1181 if (stat(pathname, &buf) == -1)
1182 return 0;
1183 return S_ISDIR(buf.st_mode);
1184 }
1185
1186 static int
1187 is_nonempty_file(const char *pathname)
1188 {
1189 struct stat buf;
1190
1191 if (stat(pathname, &buf) == -1)
1192 return 0;
1193
1194 return S_ISREG(buf.st_mode) && buf.st_size > 0;
1195 }
1196