1 
   2 /*
   3  * CDDL HEADER START
   4  *
   5  * The contents of this file are subject to the terms of the
   6  * Common Development and Distribution License (the "License").
   7  * You may not use this file except in compliance with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 
  23 /*
  24  * Copyright 2018, Richard Lowe.
  25  */
  26 /*
  27  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 /*
  32  * Wrapper for the GNU C compiler to make it accept the Sun C compiler
  33  * arguments where possible.
  34  *
  35  * Since the translation is inexact, this is something of a work-in-progress.
  36  *
  37  */
  38 
  39 /* If you modify this file, you must increment CW_VERSION */
  40 #define CW_VERSION      "2.0"
  41 
  42 /*
  43  * -#           Verbose mode
  44  * -###         Show compiler commands built by driver, no compilation
  45  * -A<name[(tokens)]>     Preprocessor predicate assertion
  46  * -B<[static|dynamic]>   Specify dynamic or static binding
  47  * -C           Prevent preprocessor from removing comments
  48  * -c           Compile only - produce .o files, suppress linking
  49  * -cg92        Alias for -xtarget=ss1000
  50  * -D<name[=token]>       Associate name with token as if by #define
  51  * -d[y|n]      dynamic [-dy] or static [-dn] option to linker
  52  * -E           Compile source through preprocessor only, output to stdout
  53  * -erroff=<t>    Suppress warnings specified by tags t(%none, %all, <tag list>)
  54  * -errtags=<a>   Display messages with tags a(no, yes)
  55  * -errwarn=<t>   Treats warnings specified by tags t(%none, %all, <tag list>)
  56  *              as errors
  57  * -fast        Optimize using a selection of options
  58  * -fd          Report old-style function definitions and declarations
  59  * -features=zla        Allow zero-length arrays
  60  * -flags       Show this summary of compiler options
  61  * -fnonstd     Initialize floating-point hardware to non-standard preferences
  62  * -fns[=<yes|no>] Select non-standard floating point mode
  63  * -fprecision=<p> Set FP rounding precision mode p(single, double, extended)
  64  * -fround=<r>    Select the IEEE rounding mode in effect at startup
  65  * -fsimple[=<n>] Select floating-point optimization preferences <n>
  66  * -fsingle     Use single-precision arithmetic (-Xt and -Xs modes only)
  67  * -ftrap=<t>     Select floating-point trapping mode in effect at startup
  68  * -fstore      force floating pt. values to target precision on assignment
  69  * -G           Build a dynamic shared library
  70  * -g           Compile for debugging
  71  * -H           Print path name of each file included during compilation
  72  * -h <name>      Assign <name> to generated dynamic shared library
  73  * -I<dir>        Add <dir> to preprocessor #include file search path
  74  * -i           Passed to linker to ignore any LD_LIBRARY_PATH setting
  75  * -keeptmp     Keep temporary files created during compilation
  76  * -KPIC        Compile position independent code with 32-bit addresses
  77  * -Kpic        Compile position independent code
  78  * -L<dir>        Pass to linker to add <dir> to the library search path
  79  * -l<name>       Link with library lib<name>.a or lib<name>.so
  80  * -mc          Remove duplicate strings from .comment section of output files
  81  * -mr          Remove all strings from .comment section of output files
  82  * -mr,"string" Remove all strings and append "string" to .comment section
  83  * -mt          Specify options needed when compiling multi-threaded code
  84  * -native      Find available processor, generate code accordingly
  85  * -nofstore    Do not force floating pt. values to target precision
  86  *              on assignment
  87  * -nolib       Same as -xnolib
  88  * -noqueue     Disable queuing of compiler license requests
  89  * -norunpath   Do not build in a runtime path for shared libraries
  90  * -O           Use default optimization level (-xO2 or -xO3. Check man page.)
  91  * -o <outputfile> Set name of output file to <outputfile>
  92  * -P           Compile source through preprocessor only, output to .i  file
  93  * -PIC         Alias for -KPIC or -xcode=pic32
  94  * -p           Compile for profiling with prof
  95  * -pic         Alias for -Kpic or -xcode=pic13
  96  * -Q[y|n]      Emit/don't emit identification info to output file
  97  * -qp          Compile for profiling with prof
  98  * -R<dir[:dir]> Build runtime search path list into executable
  99  * -S           Compile and only generate assembly code (.s)
 100  * -s           Strip symbol table from the executable file
 101  * -t           Turn off duplicate symbol warnings when linking
 102  * -U<name>       Delete initial definition of preprocessor symbol <name>
 103  * -V           Report version number of each compilation phase
 104  * -v           Do stricter semantic checking
 105  * -W<c>,<arg>      Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u)
 106  * -w           Suppress compiler warning messages
 107  * -Xa          Compile assuming ANSI C conformance, allow K & R extensions
 108  *              (default mode)
 109  * -Xc          Compile assuming strict ANSI C conformance
 110  * -Xs          Compile assuming (pre-ANSI) K & R C style code
 111  * -Xt          Compile assuming K & R conformance, allow ANSI C
 112  * -x386        Generate code for the 80386 processor
 113  * -x486        Generate code for the 80486 processor
 114  * -xarch=<a>     Specify target architecture instruction set
 115  * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions
 116  *              for system functions, b={%all,%none}
 117  * -xCC         Accept C++ style comments
 118  * -xchar_byte_order=<o> Specify multi-char byte order <o> (default, high, low)
 119  * -xchip=<c>     Specify the target processor for use by the optimizer
 120  * -xcode=<c>     Generate different code for forming addresses
 121  * -xcrossfile[=<n>] Enable optimization and inlining across source files,
 122  *              n={0|1}
 123  * -xe          Perform only syntax/semantic checking, no code generation
 124  * -xF          Compile for later mapfile reordering or unused section
 125  *              elimination
 126  * -xhelp=<f>     Display on-line help information f(flags, readme, errors)
 127  * -xildoff     Cancel -xildon
 128  * -xildon      Enable use of the incremental linker, ild
 129  * -xinline=[<a>,...,<a>]  Attempt inlining of specified user routines,
 130  *              <a>={%auto,func,no%func}
 131  * -xlibmieee   Force IEEE 754 return values for math routines in
 132  *              exceptional cases
 133  * -xlibmil     Inline selected libm math routines for optimization
 134  * -xlic_lib=sunperf    Link in the Sun supplied performance libraries
 135  * -xlicinfo    Show license server information
 136  * -xM          Generate makefile dependencies
 137  * -xM1         Generate makefile dependencies, but exclude /usr/include
 138  * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt
 139  * -xnolib      Do not link with default system libraries
 140  * -xnolibmil   Cancel -xlibmil on command line
 141  * -xO<n> Generate optimized code (n={1|2|3|4|5})
 142  * -xP          Print prototypes for function definitions
 143  * -xpentium    Generate code for the pentium processor
 144  * -xpg         Compile for profiling with gprof
 145  * -xprofile=<p> Collect data for a profile or use a profile to optimize
 146  *              <p>={{collect,use}[:<path>],tcov}
 147  * -xregs=<r>     Control register allocation
 148  * -xs          Allow debugging without object (.o) files
 149  * -xsb         Compile for use with the WorkShop source browser
 150  * -xsbfast     Generate only WorkShop source browser info, no compilation
 151  * -xsfpconst   Represent unsuffixed floating point constants as single
 152  *              precision
 153  * -xspace      Do not do optimizations that increase code size
 154  * -xstrconst   Place string literals into read-only data segment
 155  * -xtarget=<t>   Specify target system for optimization
 156  * -xtemp=<dir>   Set directory for temporary files to <dir>
 157  * -xtime       Report the execution time for each compilation phase
 158  * -xtransition Emit warnings for differences between K&R C and ANSI C
 159  * -xtrigraphs[=<yes|no>] Enable|disable trigraph translation
 160  * -xunroll=n   Enable unrolling loops n times where possible
 161  * -Y<c>,<dir>      Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u)
 162  * -YA,<dir>      Change default directory searched for components
 163  * -YI,<dir>      Change default directory searched for include files
 164  * -YP,<dir>      Change default directory for finding libraries files
 165  * -YS,<dir>      Change default directory for startup object files
 166  */
 167 
 168 /*
 169  * Translation table:
 170  */
 171 /*
 172  * -#                           -v
 173  * -###                         error
 174  * -A<name[(tokens)]>             pass-thru
 175  * -B<[static|dynamic]>           pass-thru (syntax error for anything else)
 176  * -C                           pass-thru
 177  * -c                           pass-thru
 178  * -cg92                        -m32 -mcpu=v8 -mtune=supersparc (SPARC only)
 179  * -D<name[=token]>               pass-thru
 180  * -dy or -dn                   -Wl,-dy or -Wl,-dn
 181  * -E                           pass-thru
 182  * -erroff=E_EMPTY_TRANSLATION_UNIT ignore
 183  * -errtags=%all                -Wall
 184  * -errwarn=%all                -Werror else -Wno-error
 185  * -fast                        error
 186  * -fd                          error
 187  * -features=zla                ignore
 188  * -flags                       --help
 189  * -fnonstd                     error
 190  * -fns[=<yes|no>]                error
 191  * -fprecision=<p>                error
 192  * -fround=<r>                    error
 193  * -fsimple[=<n>]         error
 194  * -fsingle[=<n>]         error
 195  * -ftrap=<t>                     error
 196  * -fstore                      error
 197  * -G                           pass-thru
 198  * -g                           pass-thru
 199  * -H                           pass-thru
 200  * -h <name>                      pass-thru
 201  * -I<dir>                        pass-thru
 202  * -i                           pass-thru
 203  * -keeptmp                     -save-temps
 204  * -KPIC                        -fPIC
 205  * -Kpic                        -fpic
 206  * -L<dir>                        pass-thru
 207  * -l<name>                       pass-thru
 208  * -mc                          error
 209  * -mr                          error
 210  * -mr,"string"                 error
 211  * -mt                          -D_REENTRANT
 212  * -native                      error
 213  * -nofstore                    error
 214  * -nolib                       -nodefaultlibs
 215  * -noqueue                     ignore
 216  * -norunpath                   ignore
 217  * -O                           -O1 (Check the man page to be certain)
 218  * -o <outputfile>                pass-thru
 219  * -P                           -E -o filename.i (or error)
 220  * -PIC                         -fPIC (C++ only)
 221  * -p                           pass-thru
 222  * -pic                         -fpic (C++ only)
 223  * -Q[y|n]                      error
 224  * -qp                          -p
 225  * -R<dir[:dir]>          pass-thru
 226  * -S                           pass-thru
 227  * -s                           -Wl,-s
 228  * -t                           -Wl,-t
 229  * -U<name>                       pass-thru
 230  * -V                           --version
 231  * -v                           -Wall
 232  * -Wa,<arg>                      pass-thru
 233  * -Wp,<arg>                      pass-thru except -xc99=<a>
 234  * -Wl,<arg>                      pass-thru
 235  * -W{m,0,2,h,i,u>           error/ignore
 236  * -Wu,-xmodel=kernel           -ffreestanding -mcmodel=kernel -mno-red-zone
 237  * -xmodel=kernel               -ffreestanding -mcmodel=kernel -mno-red-zone
 238  * -Wu,-save_args               -msave-args
 239  * -w                           pass-thru
 240  * -Xa                          -std=iso9899:199409 or -ansi
 241  * -Xc                          -ansi -pedantic
 242  * -Xt                          error
 243  * -Xs                          -traditional -std=c89
 244  * -x386                        -march=i386 (x86 only)
 245  * -x486                        -march=i486 (x86 only)
 246  * -xarch=<a>                     table
 247  * -xbuiltin[=<b>]                -fbuiltin (-fno-builtin otherwise)
 248  * -xCC                         ignore
 249  * -xchar_byte_order=<o>  error
 250  * -xchip=<c>                     table
 251  * -xcode=<c>                     table
 252  * -xdebugformat=<format> ignore (always use dwarf-2 for gcc)
 253  * -xcrossfile[=<n>]              ignore
 254  * -xe                          error
 255  * -xF                          error
 256  * -xhelp=<f>                     error
 257  * -xildoff                     ignore
 258  * -xildon                      ignore
 259  * -xinline                     ignore
 260  * -xlibmieee                   error
 261  * -xlibmil                     error
 262  * -xlic_lib=sunperf            error
 263  * -xM                          -M
 264  * -xM1                         -MM
 265  * -xmaxopt=[...]               error
 266  * -xnolib                      -nodefaultlibs
 267  * -xnolibmil                   error
 268  * -xO<n>                 -O<n>
 269  * -xP                          error
 270  * -xpentium                    -march=pentium (x86 only)
 271  * -xpg                         error
 272  * -xprofile=<p>          error
 273  * -xregs=<r>                     table
 274  * -xs                          error
 275  * -xsb                         error
 276  * -xsbfast                     error
 277  * -xsfpconst                   error
 278  * -xspace                      ignore (-not -Os)
 279  * -xstrconst                   ignore
 280  * -xtarget=<t>                   table
 281  * -xtemp=<dir>                   error
 282  * -xtime                       error
 283  * -xtransition                 -Wtransition
 284  * -xtrigraphs=<yes|no>           -trigraphs -notrigraphs
 285  * -xunroll=n                   error
 286  * -W0,-xdbggen=no%usedonly     -fno-eliminate-unused-debug-symbols
 287  *                              -fno-eliminate-unused-debug-types
 288  * -Y<c>,<dir>                      error
 289  * -YA,<dir>                      error
 290  * -YI,<dir>                      -nostdinc -I<dir>
 291  * -YP,<dir>                      error
 292  * -YS,<dir>                      error
 293  */
 294 
 295 #include <ctype.h>
 296 #include <err.h>
 297 #include <errno.h>
 298 #include <fcntl.h>
 299 #include <getopt.h>
 300 #include <stdio.h>
 301 #include <stdlib.h>
 302 #include <string.h>
 303 #include <unistd.h>
 304 
 305 #include <sys/param.h>
 306 #include <sys/stat.h>
 307 #include <sys/types.h>
 308 #include <sys/utsname.h>
 309 #include <sys/wait.h>
 310 
 311 #define CW_F_CXX        0x01
 312 #define CW_F_SHADOW     0x02
 313 #define CW_F_EXEC       0x04
 314 #define CW_F_ECHO       0x08
 315 #define CW_F_XLATE      0x10
 316 #define CW_F_PROG       0x20
 317 
 318 typedef enum cw_op {
 319         CW_O_NONE = 0,
 320         CW_O_PREPROCESS,
 321         CW_O_COMPILE,
 322         CW_O_LINK
 323 } cw_op_t;
 324 
 325 struct aelist {
 326         struct ae {
 327                 struct ae *ae_next;
 328                 char *ae_arg;
 329         } *ael_head, *ael_tail;
 330         int ael_argc;
 331 };
 332 
 333 typedef enum {
 334         GNU,
 335         SUN
 336 } compiler_style_t;
 337 
 338 typedef struct {
 339         char *c_name;
 340         char *c_path;
 341         compiler_style_t c_style;
 342 } cw_compiler_t;
 343 
 344 typedef struct cw_ictx {
 345         struct cw_ictx  *i_next;
 346         cw_compiler_t   *i_compiler;
 347         struct aelist   *i_ae;
 348         uint32_t        i_flags;
 349         int             i_oldargc;
 350         char            **i_oldargv;
 351         pid_t           i_pid;
 352         char            i_discard[MAXPATHLEN];
 353         char            *i_stderr;
 354 } cw_ictx_t;
 355 
 356 /*
 357  * Status values to indicate which Studio compiler and associated
 358  * flags are being used.
 359  */
 360 #define M32             0x01    /* -m32 - only on Studio 12 */
 361 #define M64             0x02    /* -m64 - only on Studio 12 */
 362 #define SS11            0x100   /* Studio 11 */
 363 #define SS12            0x200   /* Studio 12 */
 364 
 365 #define TRANS_ENTRY     5
 366 /*
 367  * Translation table definition for the -xarch= flag. The "x_arg"
 368  * value is translated into the appropriate gcc flags according
 369  * to the values in x_trans[n]. The x_flags indicates what compiler
 370  * is being used and what flags have been set via the use of
 371  * "x_arg".
 372  */
 373 typedef struct xarch_table {
 374         char    *x_arg;
 375         int     x_flags;
 376         char    *x_trans[TRANS_ENTRY];
 377 } xarch_table_t;
 378 
 379 /*
 380  * The translation table for the -xarch= flag used in the Studio compilers.
 381  */
 382 static const xarch_table_t xtbl[] = {
 383 #if defined(__x86)
 384         { "generic",    SS11, {NULL} },
 385         { "generic64",  (SS11|M64), { "-m64", "-mtune=opteron" } },
 386         { "amd64",      (SS11|M64), { "-m64", "-mtune=opteron" } },
 387         { "386",        SS11,   { "-march=i386" } },
 388         { "pentium_pro", SS11,  { "-march=pentiumpro" } },
 389         { "sse",        SS11, { "-msse", "-mfpmath=sse" } },
 390         { "sse2",       SS11, { "-msse2", "-mfpmath=sse" } },
 391 #elif defined(__sparc)
 392         { "generic",    (SS11|M32), { "-m32", "-mcpu=v8" } },
 393         { "generic64",  (SS11|M64), { "-m64", "-mcpu=v9" } },
 394         { "v8",         (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
 395         { "v8plus",     (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
 396         { "v8plusa",    (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
 397                         "-mvis" } },
 398         { "v8plusb",    (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
 399                         "-mvis" } },
 400         { "v9",         (SS11|M64), { "-m64", "-mcpu=v9" } },
 401         { "v9a",        (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
 402         { "v9b",        (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
 403         { "sparc",      SS12, { "-mcpu=v9", "-mv8plus" } },
 404         { "sparcvis",   SS12, { "-mcpu=ultrasparc", "-mvis" } },
 405         { "sparcvis2",  SS12, { "-mcpu=ultrasparc3", "-mvis" } }
 406 #endif
 407 };
 408 
 409 static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
 410 
 411 static const char *xchip_tbl[] = {
 412 #if defined(__x86)
 413         "386",          "-mtune=i386", NULL,
 414         "486",          "-mtune=i486", NULL,
 415         "pentium",      "-mtune=pentium", NULL,
 416         "pentium_pro",  "-mtune=pentiumpro", NULL,
 417 #elif defined(__sparc)
 418         "super",        "-mtune=supersparc", NULL,
 419         "ultra",        "-mtune=ultrasparc", NULL,
 420         "ultra3",       "-mtune=ultrasparc3", NULL,
 421 #endif
 422         NULL,           NULL
 423 };
 424 
 425 static const char *xcode_tbl[] = {
 426 #if defined(__sparc)
 427         "abs32",        "-fno-pic", "-mcmodel=medlow", NULL,
 428         "abs44",        "-fno-pic", "-mcmodel=medmid", NULL,
 429         "abs64",        "-fno-pic", "-mcmodel=medany", NULL,
 430         "pic13",        "-fpic", NULL,
 431         "pic32",        "-fPIC", NULL,
 432 #endif
 433         NULL,           NULL
 434 };
 435 
 436 static const char *xtarget_tbl[] = {
 437 #if defined(__x86)
 438         "pentium_pro",  "-march=pentiumpro", NULL,
 439 #endif  /* __x86 */
 440         NULL,           NULL
 441 };
 442 
 443 static const char *xregs_tbl[] = {
 444 #if defined(__sparc)
 445         "appl",         "-mapp-regs", NULL,
 446         "no%appl",      "-mno-app-regs", NULL,
 447         "float",        "-mfpu", NULL,
 448         "no%float",     "-mno-fpu", NULL,
 449 #endif  /* __sparc */
 450         NULL,           NULL
 451 };
 452 
 453 static void
 454 nomem(void)
 455 {
 456         errx(1, "out of memory");
 457 }
 458 
 459 static void
 460 newae(struct aelist *ael, const char *arg)
 461 {
 462         struct ae *ae;
 463 
 464         if ((ae = calloc(sizeof (*ae), 1)) == NULL)
 465                 nomem();
 466         ae->ae_arg = strdup(arg);
 467         if (ael->ael_tail == NULL)
 468                 ael->ael_head = ae;
 469         else
 470                 ael->ael_tail->ae_next = ae;
 471         ael->ael_tail = ae;
 472         ael->ael_argc++;
 473 }
 474 
 475 static cw_ictx_t *
 476 newictx(void)
 477 {
 478         cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1);
 479         if (ctx)
 480                 if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) {
 481                         free(ctx);
 482                         return (NULL);
 483                 }
 484 
 485         return (ctx);
 486 }
 487 
 488 static void
 489 error(const char *arg)
 490 {
 491         errx(2, "error: mapping failed at or near arg '%s'", arg);
 492 }
 493 
 494 /*
 495  * Add the current favourite set of warnings to the gcc invocation.
 496  */
 497 static void
 498 warnings(struct aelist *h)
 499 {
 500         static int warningsonce;
 501 
 502         if (warningsonce++)
 503                 return;
 504 
 505         /*
 506          * Enable as many warnings as exist, then disable those that we never
 507          * ever want.
 508          */
 509         newae(h, "-Wall");
 510         newae(h, "-Wextra");
 511 }
 512 
 513 static void
 514 optim_disable(struct aelist *h, int level)
 515 {
 516         if (level >= 2) {
 517                 newae(h, "-fno-strict-aliasing");
 518                 newae(h, "-fno-unit-at-a-time");
 519                 newae(h, "-fno-optimize-sibling-calls");
 520         }
 521 }
 522 
 523 /* ARGSUSED */
 524 static void
 525 Xamode(struct aelist __unused *h)
 526 {
 527 }
 528 
 529 static void
 530 Xcmode(struct aelist *h)
 531 {
 532         static int xconce;
 533 
 534         if (xconce++)
 535                 return;
 536 
 537         newae(h, "-ansi");
 538         newae(h, "-pedantic-errors");
 539 }
 540 
 541 static void
 542 Xsmode(struct aelist *h)
 543 {
 544         static int xsonce;
 545 
 546         if (xsonce++)
 547                 return;
 548 
 549         newae(h, "-traditional");
 550         newae(h, "-traditional-cpp");
 551 }
 552 
 553 static void
 554 usage()
 555 {
 556         extern char *__progname;
 557         (void) fprintf(stderr,
 558             "usage: %s [-C] [--versions] --primary <compiler> "
 559             "[--shadow <compiler>]... -- cflags...\n",
 560             __progname);
 561         (void) fprintf(stderr, "compilers take the form: name,path,style\n"
 562             " - name: a unique name usable in flag specifiers\n"
 563             " - path: path to the compiler binary\n"
 564             " - style: the style of flags expected: either sun or gnu\n");
 565         exit(2);
 566 }
 567 
 568 static int
 569 xlate_xtb(struct aelist *h, const char *xarg)
 570 {
 571         int     i, j;
 572 
 573         for (i = 0; i < xtbl_size; i++) {
 574                 if (strcmp(xtbl[i].x_arg, xarg) == 0)
 575                         break;
 576         }
 577 
 578         /*
 579          * At the end of the table and so no matching "arg" entry
 580          * found and so this must be a bad -xarch= flag.
 581          */
 582         if (i == xtbl_size)
 583                 error(xarg);
 584 
 585         for (j = 0; j < TRANS_ENTRY; j++) {
 586                 if (xtbl[i].x_trans[j] != NULL)
 587                         newae(h, xtbl[i].x_trans[j]);
 588                 else
 589                         break;
 590         }
 591         return (xtbl[i].x_flags);
 592 
 593 }
 594 
 595 static void
 596 xlate(struct aelist *h, const char *xarg, const char **table)
 597 {
 598         while (*table != NULL && strcmp(xarg, *table) != 0) {
 599                 while (*table != NULL)
 600                         table++;
 601                 table++;
 602         }
 603 
 604         if (*table == NULL)
 605                 error(xarg);
 606 
 607         table++;
 608 
 609         while (*table != NULL) {
 610                 newae(h, *table);
 611                 table++;
 612         }
 613 }
 614 
 615 static void
 616 do_gcc(cw_ictx_t *ctx)
 617 {
 618         int c;
 619         int pic = 0, nolibc = 0;
 620         int in_output = 0, seen_o = 0, c_files = 0;
 621         cw_op_t op = CW_O_LINK;
 622         char *model = NULL;
 623         char *nameflag;
 624         int     mflag = 0;
 625 
 626         if (ctx->i_flags & CW_F_PROG) {
 627                 newae(ctx->i_ae, "--version");
 628                 return;
 629         }
 630 
 631         newae(ctx->i_ae, "-fident");
 632         newae(ctx->i_ae, "-finline");
 633         newae(ctx->i_ae, "-fno-inline-functions");
 634         newae(ctx->i_ae, "-fno-builtin");
 635         newae(ctx->i_ae, "-fno-asm");
 636         newae(ctx->i_ae, "-fdiagnostics-show-option");
 637         newae(ctx->i_ae, "-nodefaultlibs");
 638 
 639 #if defined(__sparc)
 640         /*
 641          * The SPARC ldd and std instructions require 8-byte alignment of
 642          * their address operand.  gcc correctly uses them only when the
 643          * ABI requires 8-byte alignment; unfortunately we have a number of
 644          * pieces of buggy code that doesn't conform to the ABI.  This
 645          * flag makes gcc work more like Studio with -xmemalign=4.
 646          */
 647         newae(ctx->i_ae, "-mno-integer-ldd-std");
 648 #endif
 649 
 650         /*
 651          * This is needed because 'u' is defined
 652          * under a conditional on 'sun'.  Should
 653          * probably just remove the conditional,
 654          * or make it be dependent on '__sun'.
 655          *
 656          * -Dunix is also missing in enhanced ANSI mode
 657          */
 658         newae(ctx->i_ae, "-D__sun");
 659 
 660         if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
 661                 nomem();
 662 
 663         /*
 664          * Walk the argument list, translating as we go ..
 665          */
 666         while (--ctx->i_oldargc > 0) {
 667                 char *arg = *++ctx->i_oldargv;
 668                 size_t arglen = strlen(arg);
 669 
 670                 if (*arg == '-') {
 671                         arglen--;
 672                 } else {
 673                         /*
 674                          * Discard inline files that gcc doesn't grok
 675                          */
 676                         if (!in_output && arglen > 3 &&
 677                             strcmp(arg + arglen - 3, ".il") == 0)
 678                                 continue;
 679 
 680                         if (!in_output && arglen > 2 &&
 681                             arg[arglen - 2] == '.' &&
 682                             (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
 683                             arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
 684                                 c_files++;
 685 
 686                         /*
 687                          * Otherwise, filenames and partial arguments
 688                          * are passed through for gcc to chew on.  However,
 689                          * output is always discarded for the secondary
 690                          * compiler.
 691                          */
 692                         if ((ctx->i_flags & CW_F_SHADOW) && in_output)
 693                                 newae(ctx->i_ae, ctx->i_discard);
 694                         else
 695                                 newae(ctx->i_ae, arg);
 696                         in_output = 0;
 697                         continue;
 698                 }
 699 
 700                 if (ctx->i_flags & CW_F_CXX) {
 701                         if (strncmp(arg, "-_g++=", 6) == 0) {
 702                                 newae(ctx->i_ae, strchr(arg, '=') + 1);
 703                                 continue;
 704                         }
 705                         if (strncmp(arg, "-compat=", 8) == 0) {
 706                                 /* discard -compat=4 and -compat=5 */
 707                                 continue;
 708                         }
 709                         if (strcmp(arg, "-Qoption") == 0) {
 710                                 /* discard -Qoption and its two arguments */
 711                                 if (ctx->i_oldargc < 3)
 712                                         error(arg);
 713                                 ctx->i_oldargc -= 2;
 714                                 ctx->i_oldargv += 2;
 715                                 continue;
 716                         }
 717                         if (strcmp(arg, "-xwe") == 0) {
 718                                 /* turn warnings into errors */
 719                                 newae(ctx->i_ae, "-Werror");
 720                                 continue;
 721                         }
 722                         if (strcmp(arg, "-noex") == 0) {
 723                                 /* no exceptions */
 724                                 newae(ctx->i_ae, "-fno-exceptions");
 725                                 /* no run time type descriptor information */
 726                                 newae(ctx->i_ae, "-fno-rtti");
 727                                 continue;
 728                         }
 729                         if (strcmp(arg, "-pic") == 0) {
 730                                 newae(ctx->i_ae, "-fpic");
 731                                 pic = 1;
 732                                 continue;
 733                         }
 734                         if (strcmp(arg, "-PIC") == 0) {
 735                                 newae(ctx->i_ae, "-fPIC");
 736                                 pic = 1;
 737                                 continue;
 738                         }
 739                         if (strcmp(arg, "-norunpath") == 0) {
 740                                 /* gcc has no corresponding option */
 741                                 continue;
 742                         }
 743                         if (strcmp(arg, "-nolib") == 0) {
 744                                 /* -nodefaultlibs is on by default */
 745                                 nolibc = 1;
 746                                 continue;
 747                         }
 748 #if defined(__sparc)
 749                         if (strcmp(arg, "-cg92") == 0) {
 750                                 mflag |= xlate_xtb(ctx->i_ae, "v8");
 751                                 xlate(ctx->i_ae, "super", xchip_tbl);
 752                                 continue;
 753                         }
 754 #endif  /* __sparc */
 755                 }
 756 
 757                 switch ((c = arg[1])) {
 758                 case '_':
 759                         if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
 760                             (strncmp(arg, "-_gcc=", 6) == 0) ||
 761                             (strncmp(arg, "-_gnu=", 6) == 0)) {
 762                                 newae(ctx->i_ae, strchr(arg, '=') + 1);
 763                         }
 764                         break;
 765                 case '#':
 766                         if (arglen == 1) {
 767                                 newae(ctx->i_ae, "-v");
 768                                 break;
 769                         }
 770                         error(arg);
 771                         break;
 772                 case 'g':
 773                         newae(ctx->i_ae, "-gdwarf-2");
 774                         break;
 775                 case 'E':
 776                         if (arglen == 1) {
 777                                 newae(ctx->i_ae, "-xc");
 778                                 newae(ctx->i_ae, arg);
 779                                 op = CW_O_PREPROCESS;
 780                                 nolibc = 1;
 781                                 break;
 782                         }
 783                         error(arg);
 784                         break;
 785                 case 'c':
 786                 case 'S':
 787                         if (arglen == 1) {
 788                                 op = CW_O_COMPILE;
 789                                 nolibc = 1;
 790                         }
 791                         /* FALLTHROUGH */
 792                 case 'C':
 793                 case 'H':
 794                 case 'p':
 795                         if (arglen == 1) {
 796                                 newae(ctx->i_ae, arg);
 797                                 break;
 798                         }
 799                         error(arg);
 800                         break;
 801                 case 'A':
 802                 case 'h':
 803                 case 'I':
 804                 case 'i':
 805                 case 'L':
 806                 case 'l':
 807                 case 'R':
 808                 case 'U':
 809                 case 'u':
 810                 case 'w':
 811                         newae(ctx->i_ae, arg);
 812                         break;
 813                 case 'o':
 814                         seen_o = 1;
 815                         if (arglen == 1) {
 816                                 in_output = 1;
 817                                 newae(ctx->i_ae, arg);
 818                         } else if (ctx->i_flags & CW_F_SHADOW) {
 819                                 newae(ctx->i_ae, "-o");
 820                                 newae(ctx->i_ae, ctx->i_discard);
 821                         } else {
 822                                 newae(ctx->i_ae, arg);
 823                         }
 824                         break;
 825                 case 'D':
 826                         newae(ctx->i_ae, arg);
 827                         /*
 828                          * XXX  Clearly a hack ... do we need _KADB too?
 829                          */
 830                         if (strcmp(arg, "-D_KERNEL") == 0 ||
 831                             strcmp(arg, "-D_BOOT") == 0)
 832                                 newae(ctx->i_ae, "-ffreestanding");
 833                         break;
 834                 case 'd':
 835                         if (arglen == 2) {
 836                                 if (strcmp(arg, "-dy") == 0) {
 837                                         newae(ctx->i_ae, "-Wl,-dy");
 838                                         break;
 839                                 }
 840                                 if (strcmp(arg, "-dn") == 0) {
 841                                         newae(ctx->i_ae, "-Wl,-dn");
 842                                         break;
 843                                 }
 844                         }
 845                         if (strcmp(arg, "-dalign") == 0) {
 846                                 /*
 847                                  * -dalign forces alignment in some cases;
 848                                  * gcc does not need any flag to do this.
 849                                  */
 850                                 break;
 851                         }
 852                         error(arg);
 853                         break;
 854                 case 'e':
 855                         if (strcmp(arg,
 856                             "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
 857                                 /*
 858                                  * Accept but ignore this -- gcc doesn't
 859                                  * seem to complain about empty translation
 860                                  * units
 861                                  */
 862                                 break;
 863                         }
 864                         /* XX64 -- ignore all -erroff= options, for now */
 865                         if (strncmp(arg, "-erroff=", 8) == 0)
 866                                 break;
 867                         if (strcmp(arg, "-errtags=yes") == 0) {
 868                                 warnings(ctx->i_ae);
 869                                 break;
 870                         }
 871                         if (strcmp(arg, "-errwarn=%all") == 0) {
 872                                 newae(ctx->i_ae, "-Werror");
 873                                 break;
 874                         }
 875                         error(arg);
 876                         break;
 877                 case 'f':
 878                         if (strcmp(arg, "-flags") == 0) {
 879                                 newae(ctx->i_ae, "--help");
 880                                 break;
 881                         }
 882                         if (strncmp(arg, "-features=zla", 13) == 0) {
 883                                 /*
 884                                  * Accept but ignore this -- gcc allows
 885                                  * zero length arrays.
 886                                  */
 887                                 break;
 888                         }
 889                         error(arg);
 890                         break;
 891                 case 'G':
 892                         newae(ctx->i_ae, "-shared");
 893                         nolibc = 1;
 894                         break;
 895                 case 'k':
 896                         if (strcmp(arg, "-keeptmp") == 0) {
 897                                 newae(ctx->i_ae, "-save-temps");
 898                                 break;
 899                         }
 900                         error(arg);
 901                         break;
 902                 case 'K':
 903                         if (arglen == 1) {
 904                                 if ((arg = *++ctx->i_oldargv) == NULL ||
 905                                     *arg == '\0')
 906                                         error("-K");
 907                                 ctx->i_oldargc--;
 908                         } else {
 909                                 arg += 2;
 910                         }
 911                         if (strcmp(arg, "pic") == 0) {
 912                                 newae(ctx->i_ae, "-fpic");
 913                                 pic = 1;
 914                                 break;
 915                         }
 916                         if (strcmp(arg, "PIC") == 0) {
 917                                 newae(ctx->i_ae, "-fPIC");
 918                                 pic = 1;
 919                                 break;
 920                         }
 921                         error("-K");
 922                         break;
 923                 case 'm':
 924                         if (strcmp(arg, "-mt") == 0) {
 925                                 newae(ctx->i_ae, "-D_REENTRANT");
 926                                 break;
 927                         }
 928                         if (strcmp(arg, "-m64") == 0) {
 929                                 newae(ctx->i_ae, "-m64");
 930 #if defined(__x86)
 931                                 newae(ctx->i_ae, "-mtune=opteron");
 932 #endif
 933                                 mflag |= M64;
 934                                 break;
 935                         }
 936                         if (strcmp(arg, "-m32") == 0) {
 937                                 newae(ctx->i_ae, "-m32");
 938                                 mflag |= M32;
 939                                 break;
 940                         }
 941                         error(arg);
 942                         break;
 943                 case 'B':       /* linker options */
 944                 case 'M':
 945                 case 'z':
 946                         {
 947                                 char *opt;
 948                                 size_t len;
 949                                 char *s;
 950 
 951                                 if (arglen == 1) {
 952                                         opt = *++ctx->i_oldargv;
 953                                         if (opt == NULL || *opt == '\0')
 954                                                 error(arg);
 955                                         ctx->i_oldargc--;
 956                                 } else {
 957                                         opt = arg + 2;
 958                                 }
 959                                 len = strlen(opt) + 7;
 960                                 if ((s = malloc(len)) == NULL)
 961                                         nomem();
 962                                 (void) snprintf(s, len, "-Wl,-%c%s", c, opt);
 963                                 newae(ctx->i_ae, s);
 964                                 free(s);
 965                         }
 966                         break;
 967                 case 'n':
 968                         if (strcmp(arg, "-noqueue") == 0) {
 969                                 /*
 970                                  * Horrid license server stuff - n/a
 971                                  */
 972                                 break;
 973                         }
 974                         error(arg);
 975                         break;
 976                 case 'O':
 977                         if (arglen == 1) {
 978                                 newae(ctx->i_ae, "-O");
 979                                 break;
 980                         }
 981                         error(arg);
 982                         break;
 983                 case 'P':
 984                         /*
 985                          * We could do '-E -o filename.i', but that's hard,
 986                          * and we don't need it for the case that's triggering
 987                          * this addition.  We'll require the user to specify
 988                          * -o in the Makefile.  If they don't they'll find out
 989                          * in a hurry.
 990                          */
 991                         newae(ctx->i_ae, "-E");
 992                         op = CW_O_PREPROCESS;
 993                         nolibc = 1;
 994                         break;
 995                 case 'q':
 996                         if (strcmp(arg, "-qp") == 0) {
 997                                 newae(ctx->i_ae, "-p");
 998                                 break;
 999                         }
1000                         error(arg);
1001                         break;
1002                 case 's':
1003                         if (arglen == 1) {
1004                                 newae(ctx->i_ae, "-Wl,-s");
1005                                 break;
1006                         }
1007                         error(arg);
1008                         break;
1009                 case 't':
1010                         if (arglen == 1) {
1011                                 newae(ctx->i_ae, "-Wl,-t");
1012                                 break;
1013                         }
1014                         error(arg);
1015                         break;
1016                 case 'V':
1017                         if (arglen == 1) {
1018                                 ctx->i_flags &= ~CW_F_ECHO;
1019                                 newae(ctx->i_ae, "--version");
1020                                 break;
1021                         }
1022                         error(arg);
1023                         break;
1024                 case 'v':
1025                         if (arglen == 1) {
1026                                 warnings(ctx->i_ae);
1027                                 break;
1028                         }
1029                         error(arg);
1030                         break;
1031                 case 'W':
1032                         if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
1033                                 /*
1034                                  * gcc's preprocessor will accept c99
1035                                  * regardless, so accept and ignore.
1036                                  */
1037                                 break;
1038                         }
1039                         if (strncmp(arg, "-Wa,", 4) == 0 ||
1040                             strncmp(arg, "-Wp,", 4) == 0 ||
1041                             strncmp(arg, "-Wl,", 4) == 0) {
1042                                 newae(ctx->i_ae, arg);
1043                                 break;
1044                         }
1045                         if (strcmp(arg, "-W0,-xc99=pragma") == 0) {
1046                                 /* (undocumented) enables _Pragma */
1047                                 break;
1048                         }
1049                         if (strcmp(arg, "-W0,-xc99=%none") == 0) {
1050                                 /*
1051                                  * This is a polite way of saying
1052                                  * "no c99 constructs allowed!"
1053                                  * For now, just accept and ignore this.
1054                                  */
1055                                 break;
1056                         }
1057                         if (strcmp(arg, "-W0,-noglobal") == 0 ||
1058                             strcmp(arg, "-W0,-xglobalstatic") == 0) {
1059                                 /*
1060                                  * gcc doesn't prefix local symbols
1061                                  * in debug mode, so this is not needed.
1062                                  */
1063                                 break;
1064                         }
1065                         if (strcmp(arg, "-W0,-Lt") == 0) {
1066                                 /*
1067                                  * Generate tests at the top of loops.
1068                                  * There is no direct gcc equivalent, ignore.
1069                                  */
1070                                 break;
1071                         }
1072                         if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) {
1073                                 newae(ctx->i_ae,
1074                                     "-fno-eliminate-unused-debug-symbols");
1075                                 newae(ctx->i_ae,
1076                                     "-fno-eliminate-unused-debug-types");
1077                                 break;
1078                         }
1079                         if (strcmp(arg, "-W2,-xwrap_int") == 0) {
1080                                 /*
1081                                  * Use the legacy behaviour (pre-SS11)
1082                                  * for integer wrapping.
1083                                  * gcc does not need this.
1084                                  */
1085                                 break;
1086                         }
1087                         if (strcmp(arg, "-W2,-Rcond_elim") == 0) {
1088                                 /*
1089                                  * Elimination and expansion of conditionals;
1090                                  * gcc has no direct equivalent.
1091                                  */
1092                                 break;
1093                         }
1094                         if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
1095                                 /*
1096                                  * Prevents optimizing away checks for
1097                                  * unbound weak symbol addresses.  gcc does
1098                                  * not do this, so it's not needed.
1099                                  */
1100                                 break;
1101                         }
1102                         if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
1103                                 xlate(ctx->i_ae, arg + 11, xcode_tbl);
1104                                 if (strncmp(arg + 11, "pic", 3) == 0)
1105                                         pic = 1;
1106                                 break;
1107                         }
1108                         if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
1109                                 /*
1110                                  * Prevents insertion of register symbols.
1111                                  * gcc doesn't do this, so ignore it.
1112                                  */
1113                                 break;
1114                         }
1115                         if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) {
1116                                 /*
1117                                  * Prevents optimizing away of static variables.
1118                                  * gcc does not do this, so it's not needed.
1119                                  */
1120                                 break;
1121                         }
1122 #if defined(__x86)
1123                         if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) {
1124                                 newae(ctx->i_ae, "-ffreestanding");
1125                                 newae(ctx->i_ae, "-mno-red-zone");
1126                                 model = "-mcmodel=kernel";
1127                                 nolibc = 1;
1128                                 break;
1129                         }
1130                         if (strcmp(arg, "-Wu,-save_args") == 0) {
1131                                 newae(ctx->i_ae, "-msave-args");
1132                                 break;
1133                         }
1134 #endif  /* __x86 */
1135                         error(arg);
1136                         break;
1137                 case 'X':
1138                         if (strcmp(arg, "-Xa") == 0 ||
1139                             strcmp(arg, "-Xt") == 0) {
1140                                 Xamode(ctx->i_ae);
1141                                 break;
1142                         }
1143                         if (strcmp(arg, "-Xc") == 0) {
1144                                 Xcmode(ctx->i_ae);
1145                                 break;
1146                         }
1147                         if (strcmp(arg, "-Xs") == 0) {
1148                                 Xsmode(ctx->i_ae);
1149                                 break;
1150                         }
1151                         error(arg);
1152                         break;
1153                 case 'x':
1154                         if (arglen == 1)
1155                                 error(arg);
1156                         switch (arg[2]) {
1157 #if defined(__x86)
1158                         case '3':
1159                                 if (strcmp(arg, "-x386") == 0) {
1160                                         newae(ctx->i_ae, "-march=i386");
1161                                         break;
1162                                 }
1163                                 error(arg);
1164                                 break;
1165                         case '4':
1166                                 if (strcmp(arg, "-x486") == 0) {
1167                                         newae(ctx->i_ae, "-march=i486");
1168                                         break;
1169                                 }
1170                                 error(arg);
1171                                 break;
1172 #endif  /* __x86 */
1173                         case 'a':
1174                                 if (strncmp(arg, "-xarch=", 7) == 0) {
1175                                         mflag |= xlate_xtb(ctx->i_ae, arg + 7);
1176                                         break;
1177                                 }
1178                                 error(arg);
1179                                 break;
1180                         case 'b':
1181                                 if (strncmp(arg, "-xbuiltin=", 10) == 0) {
1182                                         if (strcmp(arg + 10, "%all"))
1183                                                 newae(ctx->i_ae, "-fbuiltin");
1184                                         break;
1185                                 }
1186                                 error(arg);
1187                                 break;
1188                         case 'C':
1189                                 /* Accept C++ style comments -- ignore */
1190                                 if (strcmp(arg, "-xCC") == 0)
1191                                         break;
1192                                 error(arg);
1193                                 break;
1194                         case 'c':
1195                                 if (strncmp(arg, "-xc99=%all", 10) == 0) {
1196                                         newae(ctx->i_ae, "-std=gnu99");
1197                                         break;
1198                                 }
1199                                 if (strncmp(arg, "-xc99=%none", 11) == 0) {
1200                                         newae(ctx->i_ae, "-std=gnu89");
1201                                         break;
1202                                 }
1203                                 if (strncmp(arg, "-xchip=", 7) == 0) {
1204                                         xlate(ctx->i_ae, arg + 7, xchip_tbl);
1205                                         break;
1206                                 }
1207                                 if (strncmp(arg, "-xcode=", 7) == 0) {
1208                                         xlate(ctx->i_ae, arg + 7, xcode_tbl);
1209                                         if (strncmp(arg + 7, "pic", 3) == 0)
1210                                                 pic = 1;
1211                                         break;
1212                                 }
1213                                 if (strncmp(arg, "-xcache=", 8) == 0)
1214                                         break;
1215                                 if (strncmp(arg, "-xcrossfile", 11) == 0)
1216                                         break;
1217                                 error(arg);
1218                                 break;
1219                         case 'd':
1220                                 if (strcmp(arg, "-xdepend") == 0)
1221                                         break;
1222                                 if (strncmp(arg, "-xdebugformat=", 14) == 0)
1223                                         break;
1224                                 error(arg);
1225                                 break;
1226                         case 'F':
1227                                 /*
1228                                  * Compile for mapfile reordering, or unused
1229                                  * section elimination, syntax can be -xF or
1230                                  * more complex, like -xF=%all -- ignore.
1231                                  */
1232                                 if (strncmp(arg, "-xF", 3) == 0)
1233                                         break;
1234                                 error(arg);
1235                                 break;
1236                         case 'i':
1237                                 if (strncmp(arg, "-xinline", 8) == 0)
1238                                         /* No inlining; ignore */
1239                                         break;
1240                                 if (strcmp(arg, "-xildon") == 0 ||
1241                                     strcmp(arg, "-xildoff") == 0)
1242                                         /* No incremental linking; ignore */
1243                                         break;
1244                                 error(arg);
1245                                 break;
1246 #if defined(__x86)
1247                         case 'm':
1248                                 if (strcmp(arg, "-xmodel=kernel") == 0) {
1249                                         newae(ctx->i_ae, "-ffreestanding");
1250                                         newae(ctx->i_ae, "-mno-red-zone");
1251                                         model = "-mcmodel=kernel";
1252                                         nolibc = 1;
1253                                         break;
1254                                 }
1255                                 error(arg);
1256                                 break;
1257 #endif  /* __x86 */
1258                         case 'M':
1259                                 if (strcmp(arg, "-xM") == 0) {
1260                                         newae(ctx->i_ae, "-M");
1261                                         break;
1262                                 }
1263                                 if (strcmp(arg, "-xM1") == 0) {
1264                                         newae(ctx->i_ae, "-MM");
1265                                         break;
1266                                 }
1267                                 error(arg);
1268                                 break;
1269                         case 'n':
1270                                 if (strcmp(arg, "-xnolib") == 0) {
1271                                         nolibc = 1;
1272                                         break;
1273                                 }
1274                                 error(arg);
1275                                 break;
1276                         case 'O':
1277                                 if (strncmp(arg, "-xO", 3) == 0) {
1278                                         size_t len = strlen(arg);
1279                                         char *s = NULL;
1280                                         int c = *(arg + 3);
1281                                         int level;
1282 
1283                                         if (len != 4 || !isdigit(c))
1284                                                 error(arg);
1285 
1286                                         level = atoi(arg + 3);
1287                                         if (level > 5)
1288                                                 error(arg);
1289                                         if (level >= 2) {
1290                                                 /*
1291                                                  * For gcc-3.4.x at -O2 we
1292                                                  * need to disable optimizations
1293                                                  * that break ON.
1294                                                  */
1295                                                 optim_disable(ctx->i_ae, level);
1296                                                 /*
1297                                                  * limit -xO3 to -O2 as well.
1298                                                  */
1299                                                 level = 2;
1300                                         }
1301                                         if (asprintf(&s, "-O%d", level) == -1)
1302                                                 nomem();
1303                                         newae(ctx->i_ae, s);
1304                                         free(s);
1305                                         break;
1306                                 }
1307                                 error(arg);
1308                                 break;
1309                         case 'p':
1310                                 if (strcmp(arg, "-xpentium") == 0) {
1311                                         newae(ctx->i_ae, "-march=pentium");
1312                                         break;
1313                                 }
1314                                 if (strcmp(arg, "-xpg") == 0) {
1315                                         newae(ctx->i_ae, "-pg");
1316                                         break;
1317                                 }
1318                                 error(arg);
1319                                 break;
1320                         case 'r':
1321                                 if (strncmp(arg, "-xregs=", 7) == 0) {
1322                                         xlate(ctx->i_ae, arg + 7, xregs_tbl);
1323                                         break;
1324                                 }
1325                                 error(arg);
1326                                 break;
1327                         case 's':
1328                                 if (strcmp(arg, "-xs") == 0 ||
1329                                     strcmp(arg, "-xspace") == 0 ||
1330                                     strcmp(arg, "-xstrconst") == 0)
1331                                         break;
1332                                 error(arg);
1333                                 break;
1334                         case 't':
1335                                 if (strcmp(arg, "-xtransition") == 0) {
1336                                         newae(ctx->i_ae, "-Wtransition");
1337                                         break;
1338                                 }
1339                                 if (strcmp(arg, "-xtrigraphs=yes") == 0) {
1340                                         newae(ctx->i_ae, "-trigraphs");
1341                                         break;
1342                                 }
1343                                 if (strcmp(arg, "-xtrigraphs=no") == 0) {
1344                                         newae(ctx->i_ae, "-notrigraphs");
1345                                         break;
1346                                 }
1347                                 if (strncmp(arg, "-xtarget=", 9) == 0) {
1348                                         xlate(ctx->i_ae, arg + 9, xtarget_tbl);
1349                                         break;
1350                                 }
1351                                 error(arg);
1352                                 break;
1353                         case 'e':
1354                         case 'h':
1355                         case 'l':
1356                         default:
1357                                 error(arg);
1358                                 break;
1359                         }
1360                         break;
1361                 case 'Y':
1362                         if (arglen == 1) {
1363                                 if ((arg = *++ctx->i_oldargv) == NULL ||
1364                                     *arg == '\0')
1365                                         error("-Y");
1366                                 ctx->i_oldargc--;
1367                                 arglen = strlen(arg + 1);
1368                         } else {
1369                                 arg += 2;
1370                         }
1371                         /* Just ignore -YS,... for now */
1372                         if (strncmp(arg, "S,", 2) == 0)
1373                                 break;
1374                         if (strncmp(arg, "l,", 2) == 0) {
1375                                 char *s = strdup(arg);
1376                                 s[0] = '-';
1377                                 s[1] = 'B';
1378                                 newae(ctx->i_ae, s);
1379                                 free(s);
1380                                 break;
1381                         }
1382                         if (strncmp(arg, "I,", 2) == 0) {
1383                                 char *s = strdup(arg);
1384                                 s[0] = '-';
1385                                 s[1] = 'I';
1386                                 newae(ctx->i_ae, "-nostdinc");
1387                                 newae(ctx->i_ae, s);
1388                                 free(s);
1389                                 break;
1390                         }
1391                         error(arg);
1392                         break;
1393                 case 'Q':
1394                         /*
1395                          * We could map -Qy into -Wl,-Qy etc.
1396                          */
1397                 default:
1398                         error(arg);
1399                         break;
1400                 }
1401         }
1402 
1403         free(nameflag);
1404 
1405         if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1406             op != CW_O_PREPROCESS) {
1407                 errx(2, "multiple source files are "
1408                     "allowed only with -E or -P");
1409         }
1410 
1411         /*
1412          * Make sure that we do not have any unintended interactions between
1413          * the xarch options passed in and the version of the Studio compiler
1414          * used.
1415          */
1416         if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1417                 errx(2,
1418                     "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1419         }
1420 
1421         switch (mflag) {
1422         case 0:
1423                 /* FALLTHROUGH */
1424         case M32:
1425 #if defined(__sparc)
1426                 /*
1427                  * Only -m32 is defined and so put in the missing xarch
1428                  * translation.
1429                  */
1430                 newae(ctx->i_ae, "-mcpu=v8");
1431                 newae(ctx->i_ae, "-mno-v8plus");
1432 #endif
1433                 break;
1434         case M64:
1435 #if defined(__sparc)
1436                 /*
1437                  * Only -m64 is defined and so put in the missing xarch
1438                  * translation.
1439                  */
1440                 newae(ctx->i_ae, "-mcpu=v9");
1441 #endif
1442                 break;
1443         case SS12:
1444 #if defined(__sparc)
1445                 /* no -m32/-m64 flag used - this is an error for sparc builds */
1446                 (void) fprintf(stderr, "No -m32/-m64 flag defined\n");
1447                 exit(2);
1448 #endif
1449                 break;
1450         case SS11:
1451                 /* FALLTHROUGH */
1452         case (SS11|M32):
1453         case (SS11|M64):
1454                 break;
1455         case (SS12|M32):
1456 #if defined(__sparc)
1457                 /*
1458                  * Need to add in further 32 bit options because with SS12
1459                  * the xarch=sparcvis option can be applied to 32 or 64
1460                  * bit, and so the translatation table (xtbl) cannot handle
1461                  * that.
1462                  */
1463                 newae(ctx->i_ae, "-mv8plus");
1464 #endif
1465                 break;
1466         case (SS12|M64):
1467                 break;
1468         default:
1469                 (void) fprintf(stderr,
1470                     "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1471                 exit(2);
1472         }
1473 
1474         if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1475             (ctx->i_flags & CW_F_SHADOW))
1476                 exit(0);
1477 
1478         if (model && !pic)
1479                 newae(ctx->i_ae, model);
1480         if (!nolibc)
1481                 newae(ctx->i_ae, "-lc");
1482         if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1483                 newae(ctx->i_ae, "-o");
1484                 newae(ctx->i_ae, ctx->i_discard);
1485         }
1486 }
1487 
1488 static void
1489 do_cc(cw_ictx_t *ctx)
1490 {
1491         int in_output = 0, seen_o = 0;
1492         cw_op_t op = CW_O_LINK;
1493         char *nameflag;
1494 
1495         if (ctx->i_flags & CW_F_PROG) {
1496                 newae(ctx->i_ae, "-V");
1497                 return;
1498         }
1499 
1500         if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
1501                 nomem();
1502 
1503         while (--ctx->i_oldargc > 0) {
1504                 char *arg = *++ctx->i_oldargv;
1505 
1506                 if (strncmp(arg, "-_CC=", 5) == 0) {
1507                         newae(ctx->i_ae, strchr(arg, '=') + 1);
1508                         continue;
1509                 }
1510 
1511                 if (*arg != '-') {
1512                         if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1513                                 newae(ctx->i_ae, arg);
1514                         } else {
1515                                 in_output = 0;
1516                                 newae(ctx->i_ae, ctx->i_discard);
1517                         }
1518                         continue;
1519                 }
1520                 switch (*(arg + 1)) {
1521                 case '_':
1522                         if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
1523                             (strncmp(arg, "-_cc=", 5) == 0) ||
1524                             (strncmp(arg, "-_sun=", 6) == 0)) {
1525                                 newae(ctx->i_ae, strchr(arg, '=') + 1);
1526                         }
1527                         break;
1528 
1529                 case 'V':
1530                         ctx->i_flags &= ~CW_F_ECHO;
1531                         newae(ctx->i_ae, arg);
1532                         break;
1533                 case 'o':
1534                         seen_o = 1;
1535                         if (strlen(arg) == 2) {
1536                                 in_output = 1;
1537                                 newae(ctx->i_ae, arg);
1538                         } else if (ctx->i_flags & CW_F_SHADOW) {
1539                                 newae(ctx->i_ae, "-o");
1540                                 newae(ctx->i_ae, ctx->i_discard);
1541                         } else {
1542                                 newae(ctx->i_ae, arg);
1543                         }
1544                         break;
1545                 case 'c':
1546                 case 'S':
1547                         if (strlen(arg) == 2)
1548                                 op = CW_O_COMPILE;
1549                         newae(ctx->i_ae, arg);
1550                         break;
1551                 case 'E':
1552                 case 'P':
1553                         if (strlen(arg) == 2)
1554                                 op = CW_O_PREPROCESS;
1555                 /*FALLTHROUGH*/
1556                 default:
1557                         newae(ctx->i_ae, arg);
1558                 }
1559         }
1560 
1561         free(nameflag);
1562 
1563         if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1564             (ctx->i_flags & CW_F_SHADOW))
1565                 exit(0);
1566 
1567         if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1568                 newae(ctx->i_ae, "-o");
1569                 newae(ctx->i_ae, ctx->i_discard);
1570         }
1571 }
1572 
1573 static void
1574 prepctx(cw_ictx_t *ctx)
1575 {
1576         newae(ctx->i_ae, ctx->i_compiler->c_path);
1577 
1578         if (ctx->i_flags & CW_F_PROG) {
1579                 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1580                     "shadow" : "primary", ctx->i_compiler->c_path);
1581                 (void) fflush(stdout);
1582         }
1583 
1584         if (!(ctx->i_flags & CW_F_XLATE))
1585                 return;
1586 
1587         switch (ctx->i_compiler->c_style) {
1588         case SUN:
1589                 do_cc(ctx);
1590                 break;
1591         case GNU:
1592                 do_gcc(ctx);
1593                 break;
1594         }
1595 }
1596 
1597 static int
1598 invoke(cw_ictx_t *ctx)
1599 {
1600         char **newargv;
1601         int ac;
1602         struct ae *a;
1603 
1604         if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
1605             NULL)
1606                 nomem();
1607 
1608         if (ctx->i_flags & CW_F_ECHO)
1609                 (void) fprintf(stderr, "+ ");
1610 
1611         for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
1612                 newargv[ac] = a->ae_arg;
1613                 if (ctx->i_flags & CW_F_ECHO)
1614                         (void) fprintf(stderr, "%s ", a->ae_arg);
1615                 if (a == ctx->i_ae->ael_tail)
1616                         break;
1617         }
1618 
1619         if (ctx->i_flags & CW_F_ECHO) {
1620                 (void) fprintf(stderr, "\n");
1621                 (void) fflush(stderr);
1622         }
1623 
1624         if (!(ctx->i_flags & CW_F_EXEC))
1625                 return (0);
1626 
1627         /*
1628          * We must fix up the environment here so that the dependency files are
1629          * not trampled by the shadow compiler. Also take care of GCC
1630          * environment variables that will throw off gcc. This assumes a primary
1631          * gcc.
1632          */
1633         if ((ctx->i_flags & CW_F_SHADOW) &&
1634             (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1635             unsetenv("DEPENDENCIES_OUTPUT") != 0 ||
1636             unsetenv("GCC_ROOT") != 0)) {
1637                 (void) fprintf(stderr, "error: environment setup failed: %s\n",
1638                     strerror(errno));
1639                 return (-1);
1640         }
1641 
1642         (void) execv(newargv[0], newargv);
1643         warn("couldn't run %s", newargv[0]);
1644 
1645         return (-1);
1646 }
1647 
1648 static int
1649 reap(cw_ictx_t *ctx)
1650 {
1651         int status, ret = 0;
1652         char buf[1024];
1653         struct stat s;
1654 
1655         /*
1656          * Only wait for one specific child.
1657          */
1658         if (ctx->i_pid <= 0)
1659                 return (-1);
1660 
1661         do {
1662                 if (waitpid(ctx->i_pid, &status, 0) < 0) {
1663                         warn("cannot reap child");
1664                         return (-1);
1665                 }
1666                 if (status != 0) {
1667                         if (WIFSIGNALED(status)) {
1668                                 ret = -WTERMSIG(status);
1669                                 break;
1670                         } else if (WIFEXITED(status)) {
1671                                 ret = WEXITSTATUS(status);
1672                                 break;
1673                         }
1674                 }
1675         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1676 
1677         (void) unlink(ctx->i_discard);
1678 
1679         if (stat(ctx->i_stderr, &s) < 0) {
1680                 warn("stat failed on child cleanup");
1681                 return (-1);
1682         }
1683         if (s.st_size != 0) {
1684                 FILE *f;
1685 
1686                 if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1687                         while (fgets(buf, sizeof (buf), f))
1688                                 (void) fprintf(stderr, "%s", buf);
1689                         (void) fflush(stderr);
1690                         (void) fclose(f);
1691                 }
1692         }
1693         (void) unlink(ctx->i_stderr);
1694         free(ctx->i_stderr);
1695 
1696         /*
1697          * cc returns an error code when given -V; we want that to succeed.
1698          */
1699         if (ctx->i_flags & CW_F_PROG)
1700                 return (0);
1701 
1702         return (ret);
1703 }
1704 
1705 static int
1706 exec_ctx(cw_ictx_t *ctx, int block)
1707 {
1708         char *file;
1709 
1710         /*
1711          * To avoid offending cc's sensibilities, the name of its output
1712          * file must end in '.o'.
1713          */
1714         if ((file = tempnam(NULL, ".cw")) == NULL) {
1715                 nomem();
1716                 return (-1);
1717         }
1718         (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1719         (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1720         free(file);
1721 
1722         if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1723                 nomem();
1724                 return (-1);
1725         }
1726 
1727         if ((ctx->i_pid = fork()) == 0) {
1728                 int fd;
1729 
1730                 (void) fclose(stderr);
1731                 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1732                     0666)) < 0) {
1733                         err(1, "open failed for standard error");
1734                 }
1735                 if (dup2(fd, 2) < 0) {
1736                         err(1, "dup2 failed for standard error");
1737                 }
1738                 if (fd != 2)
1739                         (void) close(fd);
1740                 if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1741                         err(1, "freopen failed for /dev/fd/2");
1742                 }
1743 
1744                 prepctx(ctx);
1745                 exit(invoke(ctx));
1746         }
1747 
1748         if (ctx->i_pid < 0) {
1749                 err(1, "fork failed");
1750         }
1751 
1752         if (block)
1753                 return (reap(ctx));
1754 
1755         return (0);
1756 }
1757 
1758 static void
1759 parse_compiler(const char *spec, cw_compiler_t *compiler)
1760 {
1761         char *tspec, *token;
1762 
1763         if ((tspec = strdup(spec)) == NULL)
1764                 nomem();
1765 
1766         if ((token = strsep(&tspec, ",")) == NULL)
1767                 errx(1, "Compiler is missing a name: %s", spec);
1768         compiler->c_name = token;
1769 
1770         if ((token = strsep(&tspec, ",")) == NULL)
1771                 errx(1, "Compiler is missing a path: %s", spec);
1772         compiler->c_path = token;
1773 
1774         if ((token = strsep(&tspec, ",")) == NULL)
1775                 errx(1, "Compiler is missing a style: %s", spec);
1776 
1777         if ((strcasecmp(token, "gnu") == 0) ||
1778             (strcasecmp(token, "gcc") == 0))
1779                 compiler->c_style = GNU;
1780         else if ((strcasecmp(token, "sun") == 0) ||
1781             (strcasecmp(token, "cc") == 0))
1782                 compiler->c_style = SUN;
1783         else
1784                 errx(1, "unknown compiler style: %s", token);
1785 
1786         if (tspec != NULL)
1787                 errx(1, "Excess tokens in compiler: %s", spec);
1788 }
1789 
1790 int
1791 main(int argc, char **argv)
1792 {
1793         int ch;
1794         cw_compiler_t primary = { NULL, NULL, 0 };
1795         cw_compiler_t shadows[10];
1796         int nshadows = 0;
1797         int ret = 0;
1798         boolean_t do_serial = B_FALSE;
1799         boolean_t do_exec = B_FALSE;
1800         boolean_t vflg = B_FALSE;
1801         boolean_t Cflg = B_FALSE;
1802         boolean_t cflg = B_FALSE;
1803         boolean_t nflg = B_FALSE;
1804 
1805         cw_ictx_t *main_ctx;
1806 
1807         static struct option longopts[] = {
1808                 { "compiler", no_argument, NULL, 'c' },
1809                 { "noecho", no_argument, NULL, 'n' },
1810                 { "primary", required_argument, NULL, 'p' },
1811                 { "shadow", required_argument, NULL, 's' },
1812                 { "versions", no_argument, NULL, 'v' },
1813                 { NULL, 0, NULL, 0 },
1814         };
1815 
1816 
1817         if ((main_ctx = newictx()) == NULL)
1818                 nomem();
1819 
1820         while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) {
1821                 switch (ch) {
1822                 case 'c':
1823                         cflg = B_TRUE;
1824                         break;
1825                 case 'C':
1826                         Cflg = B_TRUE;
1827                         break;
1828                 case 'n':
1829                         nflg = B_TRUE;
1830                         break;
1831                 case 'p':
1832                         if (primary.c_path != NULL) {
1833                                 warnx("Only one primary compiler may "
1834                                     "be specified");
1835                                 usage();
1836                         }
1837 
1838                         parse_compiler(optarg, &primary);
1839                         break;
1840                 case 's':
1841                         if (nshadows >= 10)
1842                                 errx(1, "May only use 10 shadows at "
1843                                     "the moment");
1844                         parse_compiler(optarg, &shadows[nshadows]);
1845                         nshadows++;
1846                         break;
1847                 case 'v':
1848                         vflg = B_TRUE;
1849                         break;
1850                 default:
1851                         (void) fprintf(stderr, "Did you forget '--'?\n");
1852                         usage();
1853                 }
1854         }
1855 
1856         if (primary.c_path == NULL) {
1857                 warnx("A primary compiler must be specified");
1858                 usage();
1859         }
1860 
1861         do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE;
1862         do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE;
1863 
1864         /* Leave room for argv[0] */
1865         argc -= (optind - 1);
1866         argv += (optind - 1);
1867 
1868         main_ctx->i_oldargc = argc;
1869         main_ctx->i_oldargv = argv;
1870         main_ctx->i_flags = CW_F_XLATE;
1871         if (nflg == 0)
1872                 main_ctx->i_flags |= CW_F_ECHO;
1873         if (do_exec)
1874                 main_ctx->i_flags |= CW_F_EXEC;
1875         if (Cflg)
1876                 main_ctx->i_flags |= CW_F_CXX;
1877         main_ctx->i_compiler = &primary;
1878 
1879         if (cflg) {
1880                 (void) fputs(primary.c_path, stdout);
1881         }
1882 
1883         if (vflg) {
1884                 (void) printf("cw version %s\n", CW_VERSION);
1885                 (void) fflush(stdout);
1886                 main_ctx->i_flags &= ~CW_F_ECHO;
1887                 main_ctx->i_flags |= CW_F_PROG | CW_F_EXEC;
1888                 do_serial = 1;
1889         }
1890 
1891         ret |= exec_ctx(main_ctx, do_serial);
1892 
1893         for (int i = 0; i < nshadows; i++) {
1894                 int r;
1895                 cw_ictx_t *shadow_ctx;
1896 
1897                 if ((shadow_ctx = newictx()) == NULL)
1898                         nomem();
1899 
1900                 memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));
1901 
1902                 shadow_ctx->i_flags |= CW_F_SHADOW;
1903                 shadow_ctx->i_compiler = &shadows[i];
1904 
1905                 r = exec_ctx(shadow_ctx, do_serial);
1906                 if (r == 0) {
1907                         shadow_ctx->i_next = main_ctx->i_next;
1908                         main_ctx->i_next = shadow_ctx;
1909                 }
1910                 ret |= r;
1911         }
1912 
1913         if (!do_serial) {
1914                 cw_ictx_t *next = main_ctx;
1915                 while (next != NULL) {
1916                         cw_ictx_t *toreap = next;
1917                         next = next->i_next;
1918                         ret |= reap(toreap);
1919                 }
1920         }
1921 
1922         return (ret);
1923 }