8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / aw / aw.c
blob32c7b72145e959060774bd48eeb37291b5658322
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Wrapper for the GNU assembler to make it accept the Sun assembler
29 * arguments where possible.
31 * There are several limitations; the Sun assembler takes multiple
32 * source files, we only take one.
34 * -b, -s, -xF, -T plain not supported.
35 * -S isn't supported either, because while GNU as does generate
36 * listings with -a, there's no obvious mapping between sub-options.
37 * -K pic, -K PIC not supported either, though it's not clear what
38 * these actually do ..
39 * -Qy (not supported) adds a string to the .comment section
40 * describing the assembler version, while
41 * -Qn (supported) suppresses the string (also the default).
43 * We also add '-#' support to see invocation lines..
44 * We also add '-xarch=amd64' in case we need to feed the assembler
45 * something different (or in case we need to invoke a different binary
46 * altogether!)
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 #include <stdio.h>
52 #include <unistd.h>
53 #include <string.h>
54 #include <stdlib.h>
55 #include <sys/param.h>
57 static const char *progname;
58 static int verbose;
60 struct aelist {
61 int ael_argc;
62 struct ae {
63 struct ae *ae_next;
64 char *ae_arg;
65 } *ael_head, *ael_tail;
68 static struct aelist *
69 newael(void)
71 return (calloc(sizeof (struct aelist), 1));
74 static void
75 newae(struct aelist *ael, const char *arg)
77 struct ae *ae;
79 ae = calloc(sizeof (*ae), 1);
80 ae->ae_arg = strdup(arg);
81 if (ael->ael_tail == NULL)
82 ael->ael_head = ae;
83 else
84 ael->ael_tail->ae_next = ae;
85 ael->ael_tail = ae;
86 ael->ael_argc++;
89 static void
90 fixae_arg(struct ae *ae, const char *newarg)
92 free(ae->ae_arg);
93 ae->ae_arg = strdup(newarg);
96 static char **
97 aeltoargv(struct aelist *ael)
99 struct ae *ae;
100 char **argv;
101 int argc;
103 argv = calloc(sizeof (*argv), ael->ael_argc + 1);
105 for (argc = 0, ae = ael->ael_head; ae; ae = ae->ae_next, argc++) {
106 argv[argc] = ae->ae_arg;
107 if (ae == ael->ael_tail)
108 break;
111 return (argv);
114 static int
115 error(const char *arg)
117 (void) fprintf(stderr,
118 "%s: as->gas mapping failed at or near arg '%s'\n", progname, arg);
119 return (2);
122 static int
123 usage(const char *arg)
125 if (arg != NULL)
126 (void) fprintf(stderr, "error: %s\n", arg);
127 (void) fprintf(stderr, "Usage: %s [-V] [-#]\n"
128 "\t[-xarch=architecture]\n"
129 "\t[-o objfile] [-L]\n"
130 "\t[-P [[-Ipath] [-Dname] [-Dname=def] [-Uname]]...]\n"
131 "\t[-m] [-n] file.s ...\n", progname);
132 return (3);
135 static void
136 copyuntil(FILE *in, FILE *out, int termchar)
138 int c;
140 while ((c = fgetc(in)) != EOF) {
141 if (out && fputc(c, out) == EOF)
142 exit(1);
143 if (c == termchar)
144 break;
149 * Variant of copyuntil(), used for copying the path used
150 * for .file directives. This version removes the workspace
151 * from the head of the path, or failing that, attempts to remove
152 * /usr/include. This is a workaround for the way gas handles
153 * these directives. The objects produced by gas contain STT_FILE
154 * symbols for every .file directive. These FILE symbols contain our
155 * workspace paths, leading to wsdiff incorrectly flagging them as
156 * having changed. By clipping off the workspace from these paths,
157 * we eliminate these false positives.
159 static void
160 copyuntil_path(FILE *in, FILE *out, int termchar,
161 const char *wspace, size_t wspace_len)
163 #define PROTO_INC "/proto/root_i386/usr/include/"
164 #define SYS_INC "/usr/include/"
166 static const size_t proto_inc_len = sizeof (PROTO_INC) - 1;
167 static const size_t sys_inc_len = sizeof (SYS_INC) - 1;
170 * Dynamically sized buffer for reading paths. Retained
171 * and reused between calls.
173 static char *buf = NULL;
174 static size_t bufsize = 0;
176 size_t bufcnt = 0;
177 char *bufptr;
178 int c;
180 /* Read the path into the buffer */
181 while ((c = fgetc(in)) != EOF) {
183 * If we need a buffer, or need a larger buffer,
184 * fix that here.
186 if (bufcnt >= bufsize) {
187 bufsize = (bufsize == 0) ? MAXPATHLEN : (bufsize * 2);
188 buf = realloc(buf, bufsize + 1); /* + room for NULL */
189 if (buf == NULL) {
190 perror("realloc");
191 exit(1);
195 buf[bufcnt++] = c;
196 if (c == termchar)
197 break;
199 if (bufcnt == 0)
200 return;
203 * We have a non-empty buffer, and thus the opportunity
204 * to do some surgery on it before passing it to the output.
206 buf[bufcnt] = '\0';
207 bufptr = buf;
210 * If our workspace is at the start, remove it.
211 * If not, then look for the system /usr/include instead.
213 if ((wspace_len > 0) && (wspace_len < bufcnt) &&
214 (strncmp(bufptr, wspace, wspace_len) == 0)) {
215 bufptr += wspace_len;
216 bufcnt -= wspace_len;
219 * Further opportunity: Also clip the prefix
220 * that leads to /usr/include in the proto.
222 if ((proto_inc_len < bufcnt) &&
223 (strncmp(bufptr, PROTO_INC, proto_inc_len) == 0)) {
224 bufptr += proto_inc_len;
225 bufcnt -= proto_inc_len;
227 } else if ((sys_inc_len < bufcnt) &&
228 (strncmp(bufptr, SYS_INC, sys_inc_len) == 0)) {
229 bufptr += sys_inc_len;
230 bufcnt -= sys_inc_len;
233 /* Output whatever is left */
234 if (out && (fwrite(bufptr, 1, bufcnt, out) != bufcnt)) {
235 perror("fwrite");
236 exit(1);
239 #undef PROTO_INC
240 #undef SYS_INC
244 * The idea here is to take directives like this emitted
245 * by cpp:
247 * # num
249 * and convert them to directives like this that are
250 * understood by the GNU assembler:
252 * .line num
254 * and similarly:
256 * # num "string" optional stuff
258 * is converted to
260 * .line num
261 * .file "string"
263 * While this could be done with a sequence of sed
264 * commands, this is simpler and faster..
266 static pid_t
267 filter(int pipein, int pipeout)
269 pid_t pid;
270 FILE *in, *out;
271 char *wspace;
272 size_t wspace_len;
274 if (verbose)
275 (void) fprintf(stderr, "{#line filter} ");
277 switch (pid = fork()) {
278 case 0:
279 if (dup2(pipein, 0) == -1 ||
280 dup2(pipeout, 1) == -1) {
281 perror("dup2");
282 exit(1);
284 closefrom(3);
285 break;
286 case -1:
287 perror("fork");
288 exit(1);
289 default:
290 return (pid);
293 in = fdopen(0, "r");
294 out = fdopen(1, "w");
297 * Key off the CODEMGR_WS environment variable to detect
298 * if we're in an activated workspace, and to get the
299 * path to the workspace.
301 wspace = getenv("CODEMGR_WS");
302 if (wspace != NULL)
303 wspace_len = strlen(wspace);
305 while (!feof(in)) {
306 int c, num;
308 switch (c = fgetc(in)) {
309 case '#':
310 switch (fscanf(in, " %d", &num)) {
311 case 0:
313 * discard comment lines completely
314 * discard ident strings completely too.
315 * (GNU as politely ignores them..)
317 copyuntil(in, NULL, '\n');
318 break;
319 default:
320 (void) fprintf(stderr, "fscanf botch?");
321 /*FALLTHROUGH*/
322 case EOF:
323 exit(1);
324 /*NOTREACHED*/
325 case 1:
327 * This line has a number at the beginning;
328 * if it has a string after the number, then
329 * it's a filename.
331 * If this is an activated workspace, use
332 * copyuntil_path() to do path rewriting
333 * that will prevent workspace paths from
334 * being burned into the resulting object.
335 * If not in an activated workspace, then
336 * copy the existing path straight through
337 * without interpretation.
339 if (fgetc(in) == ' ' && fgetc(in) == '"') {
340 (void) fprintf(out, "\t.file \"");
341 if (wspace != NULL)
342 copyuntil_path(in, out, '"',
343 wspace, wspace_len);
344 else
345 copyuntil(in, out, '"');
346 (void) fputc('\n', out);
348 (void) fprintf(out, "\t.line %d\n", num - 1);
350 * discard the rest of the line
352 copyuntil(in, NULL, '\n');
353 break;
355 break;
356 case '\n':
358 * preserve newlines
360 (void) fputc(c, out);
361 break;
362 case EOF:
364 * don't write EOF!
366 break;
367 default:
369 * lines that don't begin with '#' are copied
371 (void) fputc(c, out);
372 copyuntil(in, out, '\n');
373 break;
376 if (ferror(out))
377 exit(1);
380 exit(0);
381 /*NOTREACHED*/
384 static pid_t
385 invoke(char **argv, int pipein, int pipeout)
387 pid_t pid;
389 if (verbose) {
390 char **dargv = argv;
392 while (*dargv)
393 (void) fprintf(stderr, "%s ", *dargv++);
396 switch (pid = fork()) {
397 case 0:
398 if (pipein >= 0 && dup2(pipein, 0) == -1) {
399 perror("dup2");
400 exit(1);
402 if (pipeout >= 0 && dup2(pipeout, 1) == -1) {
403 perror("dup2");
404 exit(1);
406 closefrom(3);
407 (void) execvp(argv[0], argv);
408 perror("execvp");
409 (void) fprintf(stderr, "%s: couldn't run %s\n",
410 progname, argv[0]);
411 break;
412 case -1:
413 perror("fork");
414 exit(1);
415 default:
416 return (pid);
418 exit(2);
419 /*NOTREACHED*/
422 static int
423 pipeline(char **ppargv, char **asargv)
425 int pipedes[4];
426 int active = 0;
427 int rval = 0;
428 pid_t pid_pp, pid_f, pid_as;
430 if (pipe(pipedes) == -1 || pipe(pipedes + 2) == -1) {
431 perror("pipe");
432 return (4);
435 if ((pid_pp = invoke(ppargv, -1, pipedes[0])) > 0)
436 active++;
438 if (verbose)
439 (void) fprintf(stderr, "| ");
441 if ((pid_f = filter(pipedes[1], pipedes[2])) > 0)
442 active++;
444 if (verbose)
445 (void) fprintf(stderr, "| ");
447 if ((pid_as = invoke(asargv, pipedes[3], -1)) > 0)
448 active++;
450 if (verbose) {
451 (void) fprintf(stderr, "\n");
452 (void) fflush(stderr);
455 closefrom(3);
457 if (active != 3)
458 return (5);
460 while (active != 0) {
461 pid_t pid;
462 int stat;
464 if ((pid = wait(&stat)) == -1) {
465 rval++;
466 break;
469 if (!WIFEXITED(stat))
470 continue;
472 if (pid == pid_pp || pid == pid_f || pid == pid_as) {
473 active--;
474 if (WEXITSTATUS(stat) != 0)
475 rval++;
479 return (rval);
483 main(int argc, char *argv[])
485 struct aelist *cpp = NULL;
486 struct aelist *m4 = NULL;
487 struct aelist *as = newael();
488 char **asargv;
489 char *outfile = NULL;
490 char *srcfile = NULL;
491 const char *dir, *cmd;
492 static char as_pgm[MAXPATHLEN];
493 static char as64_pgm[MAXPATHLEN];
494 static char m4_pgm[MAXPATHLEN];
495 static char m4_cmdefs[MAXPATHLEN];
496 static char cpp_pgm[MAXPATHLEN];
497 int as64 = 0;
498 int code;
500 if ((progname = strrchr(argv[0], '/')) == NULL)
501 progname = argv[0];
502 else
503 progname++;
506 * Helpful when debugging, or when changing tool versions..
508 if ((cmd = getenv("AW_AS")) != NULL)
509 strlcpy(as_pgm, cmd, sizeof (as_pgm));
510 else {
511 if ((dir = getenv("AW_AS_DIR")) == NULL)
512 dir = DEFAULT_AS_DIR; /* /usr/sfw/bin */
513 (void) snprintf(as_pgm, sizeof (as_pgm), "%s/gas", dir);
516 if ((cmd = getenv("AW_AS64")) != NULL)
517 strlcpy(as64_pgm, cmd, sizeof (as64_pgm));
518 else {
519 if ((dir = getenv("AW_AS64_DIR")) == NULL)
520 dir = DEFAULT_AS64_DIR; /* /usr/sfw/bin */
521 (void) snprintf(as64_pgm, sizeof (as_pgm), "%s/gas", dir);
524 if ((cmd = getenv("AW_M4")) != NULL)
525 strlcpy(m4_pgm, cmd, sizeof (m4_pgm));
526 else {
527 if ((dir = getenv("AW_M4_DIR")) == NULL)
528 dir = DEFAULT_M4_DIR; /* /usr/ccs/bin */
529 (void) snprintf(m4_pgm, sizeof (m4_pgm), "%s/m4", dir);
532 if ((cmd = getenv("AW_M4LIB")) != NULL)
533 strlcpy(m4_cmdefs, cmd, sizeof (m4_cmdefs));
534 else {
535 if ((dir = getenv("AW_M4LIB_DIR")) == NULL)
536 dir = DEFAULT_M4LIB_DIR; /* /usr/ccs/lib */
537 (void) snprintf(m4_cmdefs, sizeof (m4_cmdefs),
538 "%s/cm4defs", dir);
541 if ((cmd = getenv("AW_CPP")) != NULL)
542 strlcpy(cpp_pgm, cmd, sizeof (cpp_pgm));
543 else {
544 if ((dir = getenv("AW_CPP_DIR")) == NULL)
545 dir = DEFAULT_CPP_DIR; /* /usr/ccs/lib */
546 (void) snprintf(cpp_pgm, sizeof (cpp_pgm), "%s/cpp", dir);
549 newae(as, as_pgm);
550 newae(as, "--warn");
551 newae(as, "--fatal-warnings");
552 newae(as, "--traditional-format");
555 * Walk the argument list, translating as we go ..
557 while (--argc > 0) {
558 char *arg;
559 int arglen;
561 arg = *++argv;
562 arglen = strlen(arg);
564 if (*arg != '-') {
565 char *filename;
568 * filenames ending in '.s' are taken to be
569 * assembler files, and provide the default
570 * basename of the output file.
572 * other files are passed through to the
573 * preprocessor, if present, or to gas if not.
575 filename = arg;
576 if ((arglen > 2) &&
577 ((strcmp(arg + arglen - 2, ".s") == 0) ||
578 (strcmp(arg + arglen - 2, ".S") == 0))) {
580 * Though 'as' allows multiple assembler
581 * files to be processed in one invocation
582 * of the assembler, ON only processes one
583 * file at a time, which makes things a lot
584 * simpler!
586 if (srcfile == NULL)
587 srcfile = arg;
588 else
589 return (usage(
590 "one assembler file at a time"));
593 * If we haven't seen a -o option yet,
594 * default the output to the basename
595 * of the input, substituting a .o on the end
597 if (outfile == NULL) {
598 char *argcopy;
600 argcopy = strdup(arg);
601 argcopy[arglen - 1] = 'o';
603 if ((outfile = strrchr(
604 argcopy, '/')) == NULL)
605 outfile = argcopy;
606 else
607 outfile++;
610 if (cpp)
611 newae(cpp, filename);
612 else if (m4)
613 newae(m4, filename);
614 else
615 newae(as, filename);
616 continue;
617 } else
618 arglen--;
620 switch (arg[1]) {
621 case 'K':
623 * -K pic
624 * -K PIC
626 if (arglen == 1) {
627 if ((arg = *++argv) == NULL || *arg == '\0')
628 return (usage("malformed -K"));
629 argc--;
630 } else {
631 arg += 2;
633 if (strcmp(arg, "PIC") != 0 && strcmp(arg, "pic") != 0)
634 return (usage("malformed -K"));
635 break; /* just ignore -Kpic for gcc */
636 case 'Q':
637 if (strcmp(arg, "-Qn") == 0)
638 break;
639 /*FALLTHROUGH*/
640 case 'b':
641 case 's':
642 case 'T':
644 * -b Extra symbol table for source browser ..
645 * not relevant to gas, thus should error.
646 * -s Put stabs in .stabs section not stabs.excl
647 * not clear if there's an equivalent
648 * -T 4.x migration option
650 default:
651 return (error(arg));
652 case 'x':
654 * Accept -xarch special case to invoke alternate
655 * assemblers or assembler flags for different
656 * architectures.
658 if (strcmp(arg, "-xarch=amd64") == 0 ||
659 strcmp(arg, "-xarch=generic64") == 0) {
660 as64++;
661 fixae_arg(as->ael_head, as64_pgm);
662 break;
665 * XX64: Is this useful to gas?
667 if (strcmp(arg, "-xmodel=kernel") == 0)
668 break;
671 * -xF Generates performance analysis data
672 * no equivalent
674 return (error(arg));
675 case 'V':
676 newae(as, arg);
677 break;
678 case '#':
679 verbose++;
680 break;
681 case 'L':
682 newae(as, "--keep-locals");
683 break;
684 case 'n':
685 newae(as, "--no-warn");
686 break;
687 case 'o':
688 if (arglen != 1)
689 return (usage("bad -o flag"));
690 if ((arg = *++argv) == NULL || *arg == '\0')
691 return (usage("bad -o flag"));
692 outfile = arg;
693 argc--;
694 arglen = strlen(arg + 1);
695 break;
696 case 'm':
697 if (cpp)
698 return (usage("-m conflicts with -P"));
699 if (m4 == NULL) {
700 m4 = newael();
701 newae(m4, m4_pgm);
702 newae(m4, m4_cmdefs);
704 break;
705 case 'P':
706 if (m4)
707 return (usage("-P conflicts with -m"));
708 if (cpp == NULL) {
709 cpp = newael();
710 newae(cpp, cpp_pgm);
711 newae(cpp, "-D__GNUC_AS__");
713 break;
714 case 'D':
715 case 'U':
716 if (cpp)
717 newae(cpp, arg);
718 else if (m4)
719 newae(m4, arg);
720 else
721 newae(as, arg);
722 break;
723 case 'I':
724 if (cpp)
725 newae(cpp, arg);
726 else
727 newae(as, arg);
728 break;
729 case '-': /* a gas-specific option */
730 newae(as, arg);
731 break;
735 #if defined(__i386)
736 if (as64)
737 newae(as, "--64");
738 else
739 newae(as, "--32");
740 #endif
742 if (srcfile == NULL)
743 return (usage("no source file(s) specified"));
744 if (outfile == NULL)
745 outfile = "a.out";
746 newae(as, "-o");
747 newae(as, outfile);
749 asargv = aeltoargv(as);
750 if (cpp) {
751 #if defined(__sparc)
752 newae(cpp, "-Dsparc");
753 newae(cpp, "-D__sparc");
754 if (as64)
755 newae(cpp, "-D__sparcv9");
756 else
757 newae(cpp, "-D__sparcv8");
758 #elif defined(__i386) || defined(__x86)
759 if (as64) {
760 newae(cpp, "-D__x86_64");
761 newae(cpp, "-D__amd64");
762 } else {
763 newae(cpp, "-Di386");
764 newae(cpp, "-D__i386");
766 #else
767 #error "need isa-dependent defines"
768 #endif
769 code = pipeline(aeltoargv(cpp), asargv);
770 } else if (m4)
771 code = pipeline(aeltoargv(m4), asargv);
772 else {
774 * XXX should arrange to fork/exec so that we
775 * can unlink the output file if errors are
776 * detected..
778 (void) execvp(asargv[0], asargv);
779 perror("execvp");
780 (void) fprintf(stderr, "%s: couldn't run %s\n",
781 progname, asargv[0]);
782 code = 7;
784 if (code != 0)
785 (void) unlink(outfile);
786 return (code);