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]
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
49 #include <sys/types.h>
55 #include <sys/param.h>
57 static const char *progname
;
65 } *ael_head
, *ael_tail
;
68 static struct aelist
*
71 return (calloc(sizeof (struct aelist
), 1));
75 newae(struct aelist
*ael
, const char *arg
)
79 ae
= calloc(sizeof (*ae
), 1);
80 ae
->ae_arg
= strdup(arg
);
81 if (ael
->ael_tail
== NULL
)
84 ael
->ael_tail
->ae_next
= ae
;
90 fixae_arg(struct ae
*ae
, const char *newarg
)
93 ae
->ae_arg
= strdup(newarg
);
97 aeltoargv(struct aelist
*ael
)
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
)
115 error(const char *arg
)
117 (void) fprintf(stderr
,
118 "%s: as->gas mapping failed at or near arg '%s'\n", progname
, arg
);
123 usage(const char *arg
)
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
);
136 copyuntil(FILE *in
, FILE *out
, int termchar
)
140 while ((c
= fgetc(in
)) != EOF
) {
141 if (out
&& fputc(c
, out
) == EOF
)
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.
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;
180 /* Read the path into the buffer */
181 while ((c
= fgetc(in
)) != EOF
) {
183 * If we need a buffer, or need a larger buffer,
186 if (bufcnt
>= bufsize
) {
187 bufsize
= (bufsize
== 0) ? MAXPATHLEN
: (bufsize
* 2);
188 buf
= realloc(buf
, bufsize
+ 1); /* + room for NULL */
203 * We have a non-empty buffer, and thus the opportunity
204 * to do some surgery on it before passing it to the output.
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
)) {
244 * The idea here is to take directives like this emitted
249 * and convert them to directives like this that are
250 * understood by the GNU assembler:
256 * # num "string" optional stuff
263 * While this could be done with a sequence of sed
264 * commands, this is simpler and faster..
267 filter(int pipein
, int pipeout
)
275 (void) fprintf(stderr
, "{#line filter} ");
277 switch (pid
= fork()) {
279 if (dup2(pipein
, 0) == -1 ||
280 dup2(pipeout
, 1) == -1) {
293 out
= fdopen(1, "w");
296 * Key off the CODEMGR_WS environment variable to detect
297 * if we're in an activated workspace, and to get the
298 * path to the workspace.
300 wspace
= getenv("CODEMGR_WS");
302 wspace_len
= strlen(wspace
);
307 switch (c
= fgetc(in
)) {
309 switch (fscanf(in
, " %d", &num
)) {
312 * discard comment lines completely
313 * discard ident strings completely too.
314 * (GNU as politely ignores them..)
316 copyuntil(in
, NULL
, '\n');
319 (void) fprintf(stderr
, "fscanf botch?");
326 * This line has a number at the beginning;
327 * if it has a string after the number, then
330 * If this is an activated workspace, use
331 * copyuntil_path() to do path rewriting
332 * that will prevent workspace paths from
333 * being burned into the resulting object.
334 * If not in an activated workspace, then
335 * copy the existing path straight through
336 * without interpretation.
338 if (fgetc(in
) == ' ' && fgetc(in
) == '"') {
339 (void) fprintf(out
, "\t.file \"");
341 copyuntil_path(in
, out
, '"',
344 copyuntil(in
, out
, '"');
345 (void) fputc('\n', out
);
347 (void) fprintf(out
, "\t.line %d\n", num
- 1);
349 * discard the rest of the line
351 copyuntil(in
, NULL
, '\n');
359 (void) fputc(c
, out
);
368 * lines that don't begin with '#' are copied
370 (void) fputc(c
, out
);
371 copyuntil(in
, out
, '\n');
384 invoke(char **argv
, int pipein
, int pipeout
)
392 (void) fprintf(stderr
, "%s ", *dargv
++);
395 switch (pid
= fork()) {
397 if (pipein
>= 0 && dup2(pipein
, 0) == -1) {
401 if (pipeout
>= 0 && dup2(pipeout
, 1) == -1) {
406 (void) execvp(argv
[0], argv
);
408 (void) fprintf(stderr
, "%s: couldn't run %s\n",
421 pipeline(char **ppargv
, char **asargv
)
426 pid_t pid_pp
, pid_f
, pid_as
;
428 if (pipe(pipedes
) == -1 || pipe(pipedes
+ 2) == -1) {
433 if ((pid_pp
= invoke(ppargv
, -1, pipedes
[0])) > 0)
437 (void) fprintf(stderr
, "| ");
439 if ((pid_f
= filter(pipedes
[1], pipedes
[2])) > 0)
443 (void) fprintf(stderr
, "| ");
445 if ((pid_as
= invoke(asargv
, pipedes
[3], -1)) > 0)
449 (void) fprintf(stderr
, "\n");
450 (void) fflush(stderr
);
458 while (active
!= 0) {
462 if ((pid
= wait(&stat
)) == -1) {
467 if (!WIFEXITED(stat
))
470 if (pid
== pid_pp
|| pid
== pid_f
|| pid
== pid_as
) {
472 if (WEXITSTATUS(stat
) != 0)
481 main(int argc
, char *argv
[])
483 struct aelist
*cpp
= NULL
;
484 struct aelist
*m4
= NULL
;
485 struct aelist
*as
= newael();
487 char *outfile
= NULL
;
488 char *srcfile
= NULL
;
489 const char *dir
, *cmd
;
490 static char as_pgm
[MAXPATHLEN
];
491 static char as64_pgm
[MAXPATHLEN
];
492 static char m4_pgm
[MAXPATHLEN
];
493 static char m4_cmdefs
[MAXPATHLEN
];
494 static char cpp_pgm
[MAXPATHLEN
];
498 if ((progname
= strrchr(argv
[0], '/')) == NULL
)
504 * Helpful when debugging, or when changing tool versions..
506 if ((cmd
= getenv("AW_AS")) != NULL
)
507 strlcpy(as_pgm
, cmd
, sizeof (as_pgm
));
509 if ((dir
= getenv("AW_AS_DIR")) == NULL
)
510 dir
= DEFAULT_AS_DIR
; /* /usr/sfw/bin */
511 (void) snprintf(as_pgm
, sizeof (as_pgm
), "%s/gas", dir
);
514 if ((cmd
= getenv("AW_AS64")) != NULL
)
515 strlcpy(as64_pgm
, cmd
, sizeof (as64_pgm
));
517 if ((dir
= getenv("AW_AS64_DIR")) == NULL
)
518 dir
= DEFAULT_AS64_DIR
; /* /usr/sfw/bin */
519 (void) snprintf(as64_pgm
, sizeof (as_pgm
), "%s/gas", dir
);
522 if ((cmd
= getenv("AW_M4")) != NULL
)
523 strlcpy(m4_pgm
, cmd
, sizeof (m4_pgm
));
525 if ((dir
= getenv("AW_M4_DIR")) == NULL
)
526 dir
= DEFAULT_M4_DIR
; /* /usr/ccs/bin */
527 (void) snprintf(m4_pgm
, sizeof (m4_pgm
), "%s/m4", dir
);
530 if ((cmd
= getenv("AW_M4LIB")) != NULL
)
531 strlcpy(m4_cmdefs
, cmd
, sizeof (m4_cmdefs
));
533 if ((dir
= getenv("AW_M4LIB_DIR")) == NULL
)
534 dir
= DEFAULT_M4LIB_DIR
; /* /usr/ccs/lib */
535 (void) snprintf(m4_cmdefs
, sizeof (m4_cmdefs
),
539 if ((cmd
= getenv("AW_CPP")) != NULL
)
540 strlcpy(cpp_pgm
, cmd
, sizeof (cpp_pgm
));
542 if ((dir
= getenv("AW_CPP_DIR")) == NULL
)
543 dir
= DEFAULT_CPP_DIR
; /* /usr/ccs/lib */
544 (void) snprintf(cpp_pgm
, sizeof (cpp_pgm
), "%s/cpp", dir
);
549 newae(as
, "--fatal-warnings");
550 newae(as
, "--traditional-format");
553 * Walk the argument list, translating as we go ..
560 arglen
= strlen(arg
);
566 * filenames ending in '.s' are taken to be
567 * assembler files, and provide the default
568 * basename of the output file.
570 * other files are passed through to the
571 * preprocessor, if present, or to gas if not.
575 ((strcmp(arg
+ arglen
- 2, ".s") == 0) ||
576 (strcmp(arg
+ arglen
- 2, ".S") == 0))) {
578 * Though 'as' allows multiple assembler
579 * files to be processed in one invocation
580 * of the assembler, ON only processes one
581 * file at a time, which makes things a lot
588 "one assembler file at a time"));
591 * If we haven't seen a -o option yet,
592 * default the output to the basename
593 * of the input, substituting a .o on the end
595 if (outfile
== NULL
) {
598 argcopy
= strdup(arg
);
599 argcopy
[arglen
- 1] = 'o';
601 if ((outfile
= strrchr(
602 argcopy
, '/')) == NULL
)
609 newae(cpp
, filename
);
625 if ((arg
= *++argv
) == NULL
|| *arg
== '\0')
626 return (usage("malformed -K"));
631 if (strcmp(arg
, "PIC") != 0 && strcmp(arg
, "pic") != 0)
632 return (usage("malformed -K"));
633 break; /* just ignore -Kpic for gcc */
635 if (strcmp(arg
, "-Qn") == 0)
642 * -b Extra symbol table for source browser ..
643 * not relevant to gas, thus should error.
644 * -s Put stabs in .stabs section not stabs.excl
645 * not clear if there's an equivalent
646 * -T 4.x migration option
652 * Accept -xarch special case to invoke alternate
653 * assemblers or assembler flags for different
656 if (strcmp(arg
, "-xarch=amd64") == 0 ||
657 strcmp(arg
, "-xarch=generic64") == 0) {
659 fixae_arg(as
->ael_head
, as64_pgm
);
663 * XX64: Is this useful to gas?
665 if (strcmp(arg
, "-xmodel=kernel") == 0)
669 * -xF Generates performance analysis data
680 newae(as
, "--keep-locals");
683 newae(as
, "--no-warn");
687 return (usage("bad -o flag"));
688 if ((arg
= *++argv
) == NULL
|| *arg
== '\0')
689 return (usage("bad -o flag"));
692 arglen
= strlen(arg
+ 1);
696 return (usage("-m conflicts with -P"));
700 newae(m4
, m4_cmdefs
);
705 return (usage("-P conflicts with -m"));
709 newae(cpp
, "-D__GNUC_AS__");
727 case '-': /* a gas-specific option */
741 return (usage("no source file(s) specified"));
747 asargv
= aeltoargv(as
);
750 newae(cpp
, "-Dsparc");
751 newae(cpp
, "-D__sparc");
753 newae(cpp
, "-D__sparcv9");
755 newae(cpp
, "-D__sparcv8");
756 #elif defined(__i386) || defined(__x86)
758 newae(cpp
, "-D__x86_64");
759 newae(cpp
, "-D__amd64");
761 newae(cpp
, "-Di386");
762 newae(cpp
, "-D__i386");
765 #error "need isa-dependent defines"
767 code
= pipeline(aeltoargv(cpp
), asargv
);
769 code
= pipeline(aeltoargv(m4
), asargv
);
772 * XXX should arrange to fork/exec so that we
773 * can unlink the output file if errors are
776 (void) execvp(asargv
[0], asargv
);
778 (void) fprintf(stderr
, "%s: couldn't run %s\n",
779 progname
, asargv
[0]);
783 (void) unlink(outfile
);