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 #define COMPILER_STYLE(comp) (comp->style == SUN ? "cc" : "gcc")
 339 
 340 typedef struct {
 341         char *name;
 342         char *path;
 343         compiler_style_t style;
 344 } cw_compiler_t;
 345 
 346 typedef struct cw_ictx {
 347         cw_compiler_t   *i_compiler;
 348         struct aelist   *i_ae;
 349         uint32_t        i_flags;
 350         int             i_oldargc;
 351         char            **i_oldargv;
 352         pid_t           i_pid;
 353         char            i_discard[MAXPATHLEN];
 354         char            *i_stderr;
 355 } cw_ictx_t;
 356 
 357 /*
 358  * Status values to indicate which Studio compiler and associated
 359  * flags are being used.
 360  */
 361 #define M32             0x01    /* -m32 - only on Studio 12 */
 362 #define M64             0x02    /* -m64 - only on Studio 12 */
 363 #define SS11            0x100   /* Studio 11 */
 364 #define SS12            0x200   /* Studio 12 */
 365 
 366 #define TRANS_ENTRY     5
 367 /*
 368  * Translation table definition for the -xarch= flag. The "x_arg"
 369  * value is translated into the appropriate gcc flags according
 370  * to the values in x_trans[n]. The x_flags indicates what compiler
 371  * is being used and what flags have been set via the use of
 372  * "x_arg".
 373  */
 374 typedef struct xarch_table {
 375         char    *x_arg;
 376         int     x_flags;
 377         char    *x_trans[TRANS_ENTRY];
 378 } xarch_table_t;
 379 
 380 /*
 381  * The translation table for the -xarch= flag used in the Studio compilers.
 382  */
 383 static const xarch_table_t xtbl[] = {
 384 #if defined(__x86)
 385         { "generic",    SS11, {NULL} },
 386         { "generic64",  (SS11|M64), { "-m64", "-mtune=opteron" } },
 387         { "amd64",      (SS11|M64), { "-m64", "-mtune=opteron" } },
 388         { "386",        SS11,   { "-march=i386" } },
 389         { "pentium_pro", SS11,  { "-march=pentiumpro" } },
 390         { "sse",        SS11, { "-msse", "-mfpmath=sse" } },
 391         { "sse2",       SS11, { "-msse2", "-mfpmath=sse" } },
 392 #elif defined(__sparc)
 393         { "generic",    (SS11|M32), { "-m32", "-mcpu=v8" } },
 394         { "generic64",  (SS11|M64), { "-m64", "-mcpu=v9" } },
 395         { "v8",         (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
 396         { "v8plus",     (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
 397         { "v8plusa",    (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
 398                         "-mvis" } },
 399         { "v8plusb",    (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
 400                         "-mvis" } },
 401         { "v9",         (SS11|M64), { "-m64", "-mcpu=v9" } },
 402         { "v9a",        (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
 403         { "v9b",        (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
 404         { "sparc",      SS12, { "-mcpu=v9", "-mv8plus" } },
 405         { "sparcvis",   SS12, { "-mcpu=ultrasparc", "-mvis" } },
 406         { "sparcvis2",  SS12, { "-mcpu=ultrasparc3", "-mvis" } }
 407 #endif
 408 };
 409 
 410 static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
 411 
 412 static const char *xchip_tbl[] = {
 413 #if defined(__x86)
 414         "386",          "-mtune=i386", NULL,
 415         "486",          "-mtune=i486", NULL,
 416         "pentium",      "-mtune=pentium", NULL,
 417         "pentium_pro",  "-mtune=pentiumpro", NULL,
 418 #elif defined(__sparc)
 419         "super",        "-mtune=supersparc", NULL,
 420         "ultra",        "-mtune=ultrasparc", NULL,
 421         "ultra3",       "-mtune=ultrasparc3", NULL,
 422 #endif
 423         NULL,           NULL
 424 };
 425 
 426 static const char *xcode_tbl[] = {
 427 #if defined(__sparc)
 428         "abs32",        "-fno-pic", "-mcmodel=medlow", NULL,
 429         "abs44",        "-fno-pic", "-mcmodel=medmid", NULL,
 430         "abs64",        "-fno-pic", "-mcmodel=medany", NULL,
 431         "pic13",        "-fpic", NULL,
 432         "pic32",        "-fPIC", NULL,
 433 #endif
 434         NULL,           NULL
 435 };
 436 
 437 static const char *xtarget_tbl[] = {
 438 #if defined(__x86)
 439         "pentium_pro",  "-march=pentiumpro", NULL,
 440 #endif  /* __x86 */
 441         NULL,           NULL
 442 };
 443 
 444 static const char *xregs_tbl[] = {
 445 #if defined(__sparc)
 446         "appl",         "-mapp-regs", NULL,
 447         "no%appl",      "-mno-app-regs", NULL,
 448         "float",        "-mfpu", NULL,
 449         "no%float",     "-mno-fpu", NULL,
 450 #endif  /* __sparc */
 451         NULL,           NULL
 452 };
 453 
 454 static void
 455 nomem(void)
 456 {
 457         (void) errx(1, "out of memory");
 458 }
 459 
 460 static void
 461 newae(struct aelist *ael, const char *arg)
 462 {
 463         struct ae *ae;
 464 
 465         if ((ae = calloc(sizeof (*ae), 1)) == NULL)
 466                 nomem();
 467         ae->ae_arg = strdup(arg);
 468         if (ael->ael_tail == NULL)
 469                 ael->ael_head = ae;
 470         else
 471                 ael->ael_tail->ae_next = ae;
 472         ael->ael_tail = ae;
 473         ael->ael_argc++;
 474 }
 475 
 476 static cw_ictx_t *
 477 newictx(void)
 478 {
 479         cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1);
 480         if (ctx)
 481                 if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) {
 482                         free(ctx);
 483                         return (NULL);
 484                 }
 485 
 486         return (ctx);
 487 }
 488 
 489 static void
 490 error(const char *arg)
 491 {
 492          errx(2, "error: mapping failed at or near arg '%s'\n", arg);
 493 }
 494 
 495 /*
 496  * Add the current favourite set of warnings to the gcc invocation.
 497  */
 498 static void
 499 warnings(struct aelist *h)
 500 {
 501         static int warningsonce;
 502 
 503         if (warningsonce++)
 504                 return;
 505 
 506         /*
 507          * Enable as many warnings as exist, then disable those that we never
 508          * ever want.
 509          */
 510         newae(h, "-Wall");
 511         newae(h, "-Wextra");
 512 }
 513 
 514 static void
 515 optim_disable(struct aelist *h, int level)
 516 {
 517         if (level >= 2) {
 518                 newae(h, "-fno-strict-aliasing");
 519                 newae(h, "-fno-unit-at-a-time");
 520                 newae(h, "-fno-optimize-sibling-calls");
 521         }
 522 }
 523 
 524 /* ARGSUSED */
 525 static void
 526 Xamode(struct aelist *h __attribute__((__unused__)))
 527 {
 528 }
 529 
 530 static void
 531 Xcmode(struct aelist *h)
 532 {
 533         static int xconce;
 534 
 535         if (xconce++)
 536                 return;
 537 
 538         newae(h, "-ansi");
 539         newae(h, "-pedantic-errors");
 540 }
 541 
 542 static void
 543 Xsmode(struct aelist *h)
 544 {
 545         static int xsonce;
 546 
 547         if (xsonce++)
 548                 return;
 549 
 550         newae(h, "-traditional");
 551         newae(h, "-traditional-cpp");
 552 }
 553 
 554 static void
 555 usage()
 556 {
 557         extern char *__progname;
 558         (void) fprintf(stderr,
 559             "usage: %s [-C] [--versions] --primary <compiler> "
 560             "[--shadow <compiler>]... -- cflags...\n",
 561             __progname);
 562         (void) fprintf(stderr, "compilers take the form: name,path,style\n"
 563             " - name: a unique name usable in flag specifiers\n"
 564             " - path: path to the compiler binary\n"
 565             " - style: the style of flags expected: either sun or gnu\n");
 566         exit(2);
 567 }
 568 
 569 static int
 570 xlate_xtb(struct aelist *h, const char *xarg)
 571 {
 572         int     i, j;
 573 
 574         for (i = 0; i < xtbl_size; i++) {
 575                 if (strcmp(xtbl[i].x_arg, xarg) == 0)
 576                         break;
 577         }
 578 
 579         /*
 580          * At the end of the table and so no matching "arg" entry
 581          * found and so this must be a bad -xarch= flag.
 582          */
 583         if (i == xtbl_size)
 584                 error(xarg);
 585 
 586         for (j = 0; j < TRANS_ENTRY; j++) {
 587                 if (xtbl[i].x_trans[j] != NULL)
 588                         newae(h, xtbl[i].x_trans[j]);
 589                 else
 590                         break;
 591         }
 592         return (xtbl[i].x_flags);
 593 
 594 }
 595 
 596 static void
 597 xlate(struct aelist *h, const char *xarg, const char **table)
 598 {
 599         while (*table != NULL && strcmp(xarg, *table) != 0) {
 600                 while (*table != NULL)
 601                         table++;
 602                 table++;
 603         }
 604 
 605         if (*table == NULL)
 606                 error(xarg);
 607 
 608         table++;
 609 
 610         while (*table != NULL) {
 611                 newae(h, *table);
 612                 table++;
 613         }
 614 }
 615 
 616 static void
 617 do_gcc(cw_ictx_t *ctx)
 618 {
 619         int c;
 620         int pic = 0, nolibc = 0;
 621         int in_output = 0, seen_o = 0, c_files = 0;
 622         cw_op_t op = CW_O_LINK;
 623         char *model = NULL;
 624         char *nameflag;
 625         int     mflag = 0;
 626 
 627         if (ctx->i_flags & CW_F_PROG) {
 628                 newae(ctx->i_ae, "--version");
 629                 return;
 630         }
 631 
 632         newae(ctx->i_ae, "-fident");
 633         newae(ctx->i_ae, "-finline");
 634         newae(ctx->i_ae, "-fno-inline-functions");
 635         newae(ctx->i_ae, "-fno-builtin");
 636         newae(ctx->i_ae, "-fno-asm");
 637         newae(ctx->i_ae, "-fdiagnostics-show-option");
 638         newae(ctx->i_ae, "-nodefaultlibs");
 639 
 640 #if defined(__sparc)
 641         /*
 642          * The SPARC ldd and std instructions require 8-byte alignment of
 643          * their address operand.  gcc correctly uses them only when the
 644          * ABI requires 8-byte alignment; unfortunately we have a number of
 645          * pieces of buggy code that doesn't conform to the ABI.  This
 646          * flag makes gcc work more like Studio with -xmemalign=4.
 647          */
 648         newae(ctx->i_ae, "-mno-integer-ldd-std");
 649 #endif
 650 
 651         /*
 652          * This is needed because 'u' is defined
 653          * under a conditional on 'sun'.  Should
 654          * probably just remove the conditional,
 655          * or make it be dependent on '__sun'.
 656          *
 657          * -Dunix is also missing in enhanced ANSI mode
 658          */
 659         newae(ctx->i_ae, "-D__sun");
 660 
 661         if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->name) == -1)
 662                 nomem();
 663 
 664         /*
 665          * Walk the argument list, translating as we go ..
 666          */
 667         while (--ctx->i_oldargc > 0) {
 668                 char *arg = *++ctx->i_oldargv;
 669                 size_t arglen = strlen(arg);
 670 
 671                 if (*arg == '-') {
 672                         arglen--;
 673                 } else {
 674                         /*
 675                          * Discard inline files that gcc doesn't grok
 676                          */
 677                         if (!in_output && arglen > 3 &&
 678                             strcmp(arg + arglen - 3, ".il") == 0)
 679                                 continue;
 680 
 681                         if (!in_output && arglen > 2 &&
 682                             arg[arglen - 2] == '.' &&
 683                             (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
 684                             arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
 685                                 c_files++;
 686 
 687                         /*
 688                          * Otherwise, filenames and partial arguments
 689                          * are passed through for gcc to chew on.  However,
 690                          * output is always discarded for the secondary
 691                          * compiler.
 692                          */
 693                         if ((ctx->i_flags & CW_F_SHADOW) && in_output)
 694                                 newae(ctx->i_ae, ctx->i_discard);
 695                         else
 696                                 newae(ctx->i_ae, arg);
 697                         in_output = 0;
 698                         continue;
 699                 }
 700 
 701                 if (ctx->i_flags & CW_F_CXX) {
 702                         if (strncmp(arg, "-_g++=", 6) == 0) {
 703                                 newae(ctx->i_ae, strchr(arg, '=') + 1);
 704                                 continue;
 705                         }
 706                         if (strncmp(arg, "-compat=", 8) == 0) {
 707                                 /* discard -compat=4 and -compat=5 */
 708                                 continue;
 709                         }
 710                         if (strcmp(arg, "-Qoption") == 0) {
 711                                 /* discard -Qoption and its two arguments */
 712                                 if (ctx->i_oldargc < 3)
 713                                         error(arg);
 714                                 ctx->i_oldargc -= 2;
 715                                 ctx->i_oldargv += 2;
 716                                 continue;
 717                         }
 718                         if (strcmp(arg, "-xwe") == 0) {
 719                                 /* turn warnings into errors */
 720                                 newae(ctx->i_ae, "-Werror");
 721                                 continue;
 722                         }
 723                         if (strcmp(arg, "-noex") == 0) {
 724                                 /* no exceptions */
 725                                 newae(ctx->i_ae, "-fno-exceptions");
 726                                 /* no run time type descriptor information */
 727                                 newae(ctx->i_ae, "-fno-rtti");
 728                                 continue;
 729                         }
 730                         if (strcmp(arg, "-pic") == 0) {
 731                                 newae(ctx->i_ae, "-fpic");
 732                                 pic = 1;
 733                                 continue;
 734                         }
 735                         if (strcmp(arg, "-PIC") == 0) {
 736                                 newae(ctx->i_ae, "-fPIC");
 737                                 pic = 1;
 738                                 continue;
 739                         }
 740                         if (strcmp(arg, "-norunpath") == 0) {
 741                                 /* gcc has no corresponding option */
 742                                 continue;
 743                         }
 744                         if (strcmp(arg, "-nolib") == 0) {
 745                                 /* -nodefaultlibs is on by default */
 746                                 nolibc = 1;
 747                                 continue;
 748                         }
 749 #if defined(__sparc)
 750                         if (strcmp(arg, "-cg92") == 0) {
 751                                 mflag |= xlate_xtb(ctx->i_ae, "v8");
 752                                 xlate(ctx->i_ae, "super", xchip_tbl);
 753                                 continue;
 754                         }
 755 #endif  /* __sparc */
 756                 }
 757 
 758                 switch ((c = arg[1])) {
 759                 case '_':
 760                         if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
 761                             (strncmp(arg, "-_gcc=", 6) == 0) ||
 762                             (strncmp(arg, "-_gnu=", 6) == 0)) {
 763                                 newae(ctx->i_ae, strchr(arg, '=') + 1);
 764                         }
 765                         break;
 766                 case '#':
 767                         if (arglen == 1) {
 768                                 newae(ctx->i_ae, "-v");
 769                                 break;
 770                         }
 771                         error(arg);
 772                         break;
 773                 case 'g':
 774                         newae(ctx->i_ae, "-gdwarf-2");
 775                         break;
 776                 case 'E':
 777                         if (arglen == 1) {
 778                                 newae(ctx->i_ae, "-xc");
 779                                 newae(ctx->i_ae, arg);
 780                                 op = CW_O_PREPROCESS;
 781                                 nolibc = 1;
 782                                 break;
 783                         }
 784                         error(arg);
 785                         break;
 786                 case 'c':
 787                 case 'S':
 788                         if (arglen == 1) {
 789                                 op = CW_O_COMPILE;
 790                                 nolibc = 1;
 791                         }
 792                         /* FALLTHROUGH */
 793                 case 'C':
 794                 case 'H':
 795                 case 'p':
 796                         if (arglen == 1) {
 797                                 newae(ctx->i_ae, arg);
 798                                 break;
 799                         }
 800                         error(arg);
 801                         break;
 802                 case 'A':
 803                 case 'h':
 804                 case 'I':
 805                 case 'i':
 806                 case 'L':
 807                 case 'l':
 808                 case 'R':
 809                 case 'U':
 810                 case 'u':
 811                 case 'w':
 812                         newae(ctx->i_ae, arg);
 813                         break;
 814                 case 'o':
 815                         seen_o = 1;
 816                         if (arglen == 1) {
 817                                 in_output = 1;
 818                                 newae(ctx->i_ae, arg);
 819                         } else if (ctx->i_flags & CW_F_SHADOW) {
 820                                 newae(ctx->i_ae, "-o");
 821                                 newae(ctx->i_ae, ctx->i_discard);
 822                         } else {
 823                                 newae(ctx->i_ae, arg);
 824                         }
 825                         break;
 826                 case 'D':
 827                         newae(ctx->i_ae, arg);
 828                         /*
 829                          * XXX  Clearly a hack ... do we need _KADB too?
 830                          */
 831                         if (strcmp(arg, "-D_KERNEL") == 0 ||
 832                             strcmp(arg, "-D_BOOT") == 0)
 833                                 newae(ctx->i_ae, "-ffreestanding");
 834                         break;
 835                 case 'd':
 836                         if (arglen == 2) {
 837                                 if (strcmp(arg, "-dy") == 0) {
 838                                         newae(ctx->i_ae, "-Wl,-dy");
 839                                         break;
 840                                 }
 841                                 if (strcmp(arg, "-dn") == 0) {
 842                                         newae(ctx->i_ae, "-Wl,-dn");
 843                                         break;
 844                                 }
 845                         }
 846                         if (strcmp(arg, "-dalign") == 0) {
 847                                 /*
 848                                  * -dalign forces alignment in some cases;
 849                                  * gcc does not need any flag to do this.
 850                                  */
 851                                 break;
 852                         }
 853                         error(arg);
 854                         break;
 855                 case 'e':
 856                         if (strcmp(arg,
 857                             "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
 858                                 /*
 859                                  * Accept but ignore this -- gcc doesn't
 860                                  * seem to complain about empty translation
 861                                  * units
 862                                  */
 863                                 break;
 864                         }
 865                         /* XX64 -- ignore all -erroff= options, for now */
 866                         if (strncmp(arg, "-erroff=", 8) == 0)
 867                                 break;
 868                         if (strcmp(arg, "-errtags=yes") == 0) {
 869                                 warnings(ctx->i_ae);
 870                                 break;
 871                         }
 872                         if (strcmp(arg, "-errwarn=%all") == 0) {
 873                                 newae(ctx->i_ae, "-Werror");
 874                                 break;
 875                         }
 876                         error(arg);
 877                         break;
 878                 case 'f':
 879                         if (strcmp(arg, "-flags") == 0) {
 880                                 newae(ctx->i_ae, "--help");
 881                                 break;
 882                         }
 883                         if (strncmp(arg, "-features=zla", 13) == 0) {
 884                                 /*
 885                                  * Accept but ignore this -- gcc allows
 886                                  * zero length arrays.
 887                                  */
 888                                 break;
 889                         }
 890                         error(arg);
 891                         break;
 892                 case 'G':
 893                         newae(ctx->i_ae, "-shared");
 894                         nolibc = 1;
 895                         break;
 896                 case 'k':
 897                         if (strcmp(arg, "-keeptmp") == 0) {
 898                                 newae(ctx->i_ae, "-save-temps");
 899                                 break;
 900                         }
 901                         error(arg);
 902                         break;
 903                 case 'K':
 904                         if (arglen == 1) {
 905                                 if ((arg = *++ctx->i_oldargv) == NULL ||
 906                                     *arg == '\0')
 907                                         error("-K");
 908                                 ctx->i_oldargc--;
 909                         } else {
 910                                 arg += 2;
 911                         }
 912                         if (strcmp(arg, "pic") == 0) {
 913                                 newae(ctx->i_ae, "-fpic");
 914                                 pic = 1;
 915                                 break;
 916                         }
 917                         if (strcmp(arg, "PIC") == 0) {
 918                                 newae(ctx->i_ae, "-fPIC");
 919                                 pic = 1;
 920                                 break;
 921                         }
 922                         error("-K");
 923                         break;
 924                 case 'm':
 925                         if (strcmp(arg, "-mt") == 0) {
 926                                 newae(ctx->i_ae, "-D_REENTRANT");
 927                                 break;
 928                         }
 929                         if (strcmp(arg, "-m64") == 0) {
 930                                 newae(ctx->i_ae, "-m64");
 931 #if defined(__x86)
 932                                 newae(ctx->i_ae, "-mtune=opteron");
 933 #endif
 934                                 mflag |= M64;
 935                                 break;
 936                         }
 937                         if (strcmp(arg, "-m32") == 0) {
 938                                 newae(ctx->i_ae, "-m32");
 939                                 mflag |= M32;
 940                                 break;
 941                         }
 942                         error(arg);
 943                         break;
 944                 case 'B':       /* linker options */
 945                 case 'M':
 946                 case 'z':
 947                         {
 948                                 char *opt;
 949                                 size_t len;
 950                                 char *s;
 951 
 952                                 if (arglen == 1) {
 953                                         opt = *++ctx->i_oldargv;
 954                                         if (opt == NULL || *opt == '\0')
 955                                                 error(arg);
 956                                         ctx->i_oldargc--;
 957                                 } else {
 958                                         opt = arg + 2;
 959                                 }
 960                                 len = strlen(opt) + 7;
 961                                 if ((s = malloc(len)) == NULL)
 962                                         nomem();
 963                                 (void) snprintf(s, len, "-Wl,-%c%s", c, opt);
 964                                 newae(ctx->i_ae, s);
 965                                 free(s);
 966                         }
 967                         break;
 968                 case 'n':
 969                         if (strcmp(arg, "-noqueue") == 0) {
 970                                 /*
 971                                  * Horrid license server stuff - n/a
 972                                  */
 973                                 break;
 974                         }
 975                         error(arg);
 976                         break;
 977                 case 'O':
 978                         if (arglen == 1) {
 979                                 newae(ctx->i_ae, "-O");
 980                                 break;
 981                         }
 982                         error(arg);
 983                         break;
 984                 case 'P':
 985                         /*
 986                          * We could do '-E -o filename.i', but that's hard,
 987                          * and we don't need it for the case that's triggering
 988                          * this addition.  We'll require the user to specify
 989                          * -o in the Makefile.  If they don't they'll find out
 990                          * in a hurry.
 991                          */
 992                         newae(ctx->i_ae, "-E");
 993                         op = CW_O_PREPROCESS;
 994                         nolibc = 1;
 995                         break;
 996                 case 'q':
 997                         if (strcmp(arg, "-qp") == 0) {
 998                                 newae(ctx->i_ae, "-p");
 999                                 break;
1000                         }
1001                         error(arg);
1002                         break;
1003                 case 's':
1004                         if (arglen == 1) {
1005                                 newae(ctx->i_ae, "-Wl,-s");
1006                                 break;
1007                         }
1008                         error(arg);
1009                         break;
1010                 case 't':
1011                         if (arglen == 1) {
1012                                 newae(ctx->i_ae, "-Wl,-t");
1013                                 break;
1014                         }
1015                         error(arg);
1016                         break;
1017                 case 'V':
1018                         if (arglen == 1) {
1019                                 ctx->i_flags &= ~CW_F_ECHO;
1020                                 newae(ctx->i_ae, "--version");
1021                                 break;
1022                         }
1023                         error(arg);
1024                         break;
1025                 case 'v':
1026                         if (arglen == 1) {
1027                                 warnings(ctx->i_ae);
1028                                 break;
1029                         }
1030                         error(arg);
1031                         break;
1032                 case 'W':
1033                         if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
1034                                 /*
1035                                  * gcc's preprocessor will accept c99
1036                                  * regardless, so accept and ignore.
1037                                  */
1038                                 break;
1039                         }
1040                         if (strncmp(arg, "-Wa,", 4) == 0 ||
1041                             strncmp(arg, "-Wp,", 4) == 0 ||
1042                             strncmp(arg, "-Wl,", 4) == 0) {
1043                                 newae(ctx->i_ae, arg);
1044                                 break;
1045                         }
1046                         if (strcmp(arg, "-W0,-xc99=pragma") == 0) {
1047                                 /* (undocumented) enables _Pragma */
1048                                 break;
1049                         }
1050                         if (strcmp(arg, "-W0,-xc99=%none") == 0) {
1051                                 /*
1052                                  * This is a polite way of saying
1053                                  * "no c99 constructs allowed!"
1054                                  * For now, just accept and ignore this.
1055                                  */
1056                                 break;
1057                         }
1058                         if (strcmp(arg, "-W0,-noglobal") == 0 ||
1059                             strcmp(arg, "-W0,-xglobalstatic") == 0) {
1060                                 /*
1061                                  * gcc doesn't prefix local symbols
1062                                  * in debug mode, so this is not needed.
1063                                  */
1064                                 break;
1065                         }
1066                         if (strcmp(arg, "-W0,-Lt") == 0) {
1067                                 /*
1068                                  * Generate tests at the top of loops.
1069                                  * There is no direct gcc equivalent, ignore.
1070                                  */
1071                                 break;
1072                         }
1073                         if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) {
1074                                 newae(ctx->i_ae,
1075                                     "-fno-eliminate-unused-debug-symbols");
1076                                 newae(ctx->i_ae,
1077                                     "-fno-eliminate-unused-debug-types");
1078                                 break;
1079                         }
1080                         if (strcmp(arg, "-W2,-xwrap_int") == 0) {
1081                                 /*
1082                                  * Use the legacy behaviour (pre-SS11)
1083                                  * for integer wrapping.
1084                                  * gcc does not need this.
1085                                  */
1086                                 break;
1087                         }
1088                         if (strcmp(arg, "-W2,-Rcond_elim") == 0) {
1089                                 /*
1090                                  * Elimination and expansion of conditionals;
1091                                  * gcc has no direct equivalent.
1092                                  */
1093                                 break;
1094                         }
1095                         if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
1096                                 /*
1097                                  * Prevents optimizing away checks for
1098                                  * unbound weak symbol addresses.  gcc does
1099                                  * not do this, so it's not needed.
1100                                  */
1101                                 break;
1102                         }
1103                         if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
1104                                 xlate(ctx->i_ae, arg + 11, xcode_tbl);
1105                                 if (strncmp(arg + 11, "pic", 3) == 0)
1106                                         pic = 1;
1107                                 break;
1108                         }
1109                         if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
1110                                 /*
1111                                  * Prevents insertion of register symbols.
1112                                  * gcc doesn't do this, so ignore it.
1113                                  */
1114                                 break;
1115                         }
1116                         if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) {
1117                                 /*
1118                                  * Prevents optimizing away of static variables.
1119                                  * gcc does not do this, so it's not needed.
1120                                  */
1121                                 break;
1122                         }
1123 #if defined(__x86)
1124                         if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) {
1125                                 newae(ctx->i_ae, "-ffreestanding");
1126                                 newae(ctx->i_ae, "-mno-red-zone");
1127                                 model = "-mcmodel=kernel";
1128                                 nolibc = 1;
1129                                 break;
1130                         }
1131                         if (strcmp(arg, "-Wu,-save_args") == 0) {
1132                                 newae(ctx->i_ae, "-msave-args");
1133                                 break;
1134                         }
1135 #endif  /* __x86 */
1136                         error(arg);
1137                         break;
1138                 case 'X':
1139                         if (strcmp(arg, "-Xa") == 0 ||
1140                             strcmp(arg, "-Xt") == 0) {
1141                                 Xamode(ctx->i_ae);
1142                                 break;
1143                         }
1144                         if (strcmp(arg, "-Xc") == 0) {
1145                                 Xcmode(ctx->i_ae);
1146                                 break;
1147                         }
1148                         if (strcmp(arg, "-Xs") == 0) {
1149                                 Xsmode(ctx->i_ae);
1150                                 break;
1151                         }
1152                         error(arg);
1153                         break;
1154                 case 'x':
1155                         if (arglen == 1)
1156                                 error(arg);
1157                         switch (arg[2]) {
1158 #if defined(__x86)
1159                         case '3':
1160                                 if (strcmp(arg, "-x386") == 0) {
1161                                         newae(ctx->i_ae, "-march=i386");
1162                                         break;
1163                                 }
1164                                 error(arg);
1165                                 break;
1166                         case '4':
1167                                 if (strcmp(arg, "-x486") == 0) {
1168                                         newae(ctx->i_ae, "-march=i486");
1169                                         break;
1170                                 }
1171                                 error(arg);
1172                                 break;
1173 #endif  /* __x86 */
1174                         case 'a':
1175                                 if (strncmp(arg, "-xarch=", 7) == 0) {
1176                                         mflag |= xlate_xtb(ctx->i_ae, arg + 7);
1177                                         break;
1178                                 }
1179                                 error(arg);
1180                                 break;
1181                         case 'b':
1182                                 if (strncmp(arg, "-xbuiltin=", 10) == 0) {
1183                                         if (strcmp(arg + 10, "%all"))
1184                                                 newae(ctx->i_ae, "-fbuiltin");
1185                                         break;
1186                                 }
1187                                 error(arg);
1188                                 break;
1189                         case 'C':
1190                                 /* Accept C++ style comments -- ignore */
1191                                 if (strcmp(arg, "-xCC") == 0)
1192                                         break;
1193                                 error(arg);
1194                                 break;
1195                         case 'c':
1196                                 if (strncmp(arg, "-xc99=%all", 10) == 0) {
1197                                         newae(ctx->i_ae, "-std=gnu99");
1198                                         break;
1199                                 }
1200                                 if (strncmp(arg, "-xc99=%none", 11) == 0) {
1201                                         newae(ctx->i_ae, "-std=gnu89");
1202                                         break;
1203                                 }
1204                                 if (strncmp(arg, "-xchip=", 7) == 0) {
1205                                         xlate(ctx->i_ae, arg + 7, xchip_tbl);
1206                                         break;
1207                                 }
1208                                 if (strncmp(arg, "-xcode=", 7) == 0) {
1209                                         xlate(ctx->i_ae, arg + 7, xcode_tbl);
1210                                         if (strncmp(arg + 7, "pic", 3) == 0)
1211                                                 pic = 1;
1212                                         break;
1213                                 }
1214                                 if (strncmp(arg, "-xcache=", 8) == 0)
1215                                         break;
1216                                 if (strncmp(arg, "-xcrossfile", 11) == 0)
1217                                         break;
1218                                 error(arg);
1219                                 break;
1220                         case 'd':
1221                                 if (strcmp(arg, "-xdepend") == 0)
1222                                         break;
1223                                 if (strncmp(arg, "-xdebugformat=", 14) == 0)
1224                                         break;
1225                                 error(arg);
1226                                 break;
1227                         case 'F':
1228                                 /*
1229                                  * Compile for mapfile reordering, or unused
1230                                  * section elimination, syntax can be -xF or
1231                                  * more complex, like -xF=%all -- ignore.
1232                                  */
1233                                 if (strncmp(arg, "-xF", 3) == 0)
1234                                         break;
1235                                 error(arg);
1236                                 break;
1237                         case 'i':
1238                                 if (strncmp(arg, "-xinline", 8) == 0)
1239                                         /* No inlining; ignore */
1240                                         break;
1241                                 if (strcmp(arg, "-xildon") == 0 ||
1242                                     strcmp(arg, "-xildoff") == 0)
1243                                         /* No incremental linking; ignore */
1244                                         break;
1245                                 error(arg);
1246                                 break;
1247 #if defined(__x86)
1248                         case 'm':
1249                                 if (strcmp(arg, "-xmodel=kernel") == 0) {
1250                                         newae(ctx->i_ae, "-ffreestanding");
1251                                         newae(ctx->i_ae, "-mno-red-zone");
1252                                         model = "-mcmodel=kernel";
1253                                         nolibc = 1;
1254                                         break;
1255                                 }
1256                                 error(arg);
1257                                 break;
1258 #endif  /* __x86 */
1259                         case 'M':
1260                                 if (strcmp(arg, "-xM") == 0) {
1261                                         newae(ctx->i_ae, "-M");
1262                                         break;
1263                                 }
1264                                 if (strcmp(arg, "-xM1") == 0) {
1265                                         newae(ctx->i_ae, "-MM");
1266                                         break;
1267                                 }
1268                                 error(arg);
1269                                 break;
1270                         case 'n':
1271                                 if (strcmp(arg, "-xnolib") == 0) {
1272                                         nolibc = 1;
1273                                         break;
1274                                 }
1275                                 error(arg);
1276                                 break;
1277                         case 'O':
1278                                 if (strncmp(arg, "-xO", 3) == 0) {
1279                                         size_t len = strlen(arg);
1280                                         char *s;
1281                                         int c = *(arg + 3);
1282                                         int level;
1283 
1284                                         if (len != 4 || !isdigit(c))
1285                                                 error(arg);
1286 
1287                                         if ((s = malloc(len)) == NULL)
1288                                                 nomem();
1289 
1290                                         level = atoi(arg + 3);
1291                                         if (level > 5)
1292                                                 error(arg);
1293                                         if (level >= 2) {
1294                                                 /*
1295                                                  * For gcc-3.4.x at -O2 we
1296                                                  * need to disable optimizations
1297                                                  * that break ON.
1298                                                  */
1299                                                 optim_disable(ctx->i_ae, level);
1300                                                 /*
1301                                                  * limit -xO3 to -O2 as well.
1302                                                  */
1303                                                 level = 2;
1304                                         }
1305                                         (void) snprintf(s, len, "-O%d", level);
1306                                         newae(ctx->i_ae, s);
1307                                         free(s);
1308                                         break;
1309                                 }
1310                                 error(arg);
1311                                 break;
1312                         case 'p':
1313                                 if (strcmp(arg, "-xpentium") == 0) {
1314                                         newae(ctx->i_ae, "-march=pentium");
1315                                         break;
1316                                 }
1317                                 if (strcmp(arg, "-xpg") == 0) {
1318                                         newae(ctx->i_ae, "-pg");
1319                                         break;
1320                                 }
1321                                 error(arg);
1322                                 break;
1323                         case 'r':
1324                                 if (strncmp(arg, "-xregs=", 7) == 0) {
1325                                         xlate(ctx->i_ae, arg + 7, xregs_tbl);
1326                                         break;
1327                                 }
1328                                 error(arg);
1329                                 break;
1330                         case 's':
1331                                 if (strcmp(arg, "-xs") == 0 ||
1332                                     strcmp(arg, "-xspace") == 0 ||
1333                                     strcmp(arg, "-xstrconst") == 0)
1334                                         break;
1335                                 error(arg);
1336                                 break;
1337                         case 't':
1338                                 if (strcmp(arg, "-xtransition") == 0) {
1339                                         newae(ctx->i_ae, "-Wtransition");
1340                                         break;
1341                                 }
1342                                 if (strcmp(arg, "-xtrigraphs=yes") == 0) {
1343                                         newae(ctx->i_ae, "-trigraphs");
1344                                         break;
1345                                 }
1346                                 if (strcmp(arg, "-xtrigraphs=no") == 0) {
1347                                         newae(ctx->i_ae, "-notrigraphs");
1348                                         break;
1349                                 }
1350                                 if (strncmp(arg, "-xtarget=", 9) == 0) {
1351                                         xlate(ctx->i_ae, arg + 9, xtarget_tbl);
1352                                         break;
1353                                 }
1354                                 error(arg);
1355                                 break;
1356                         case 'e':
1357                         case 'h':
1358                         case 'l':
1359                         default:
1360                                 error(arg);
1361                                 break;
1362                         }
1363                         break;
1364                 case 'Y':
1365                         if (arglen == 1) {
1366                                 if ((arg = *++ctx->i_oldargv) == NULL ||
1367                                     *arg == '\0')
1368                                         error("-Y");
1369                                 ctx->i_oldargc--;
1370                                 arglen = strlen(arg + 1);
1371                         } else {
1372                                 arg += 2;
1373                         }
1374                         /* Just ignore -YS,... for now */
1375                         if (strncmp(arg, "S,", 2) == 0)
1376                                 break;
1377                         if (strncmp(arg, "l,", 2) == 0) {
1378                                 char *s = strdup(arg);
1379                                 s[0] = '-';
1380                                 s[1] = 'B';
1381                                 newae(ctx->i_ae, s);
1382                                 free(s);
1383                                 break;
1384                         }
1385                         if (strncmp(arg, "I,", 2) == 0) {
1386                                 char *s = strdup(arg);
1387                                 s[0] = '-';
1388                                 s[1] = 'I';
1389                                 newae(ctx->i_ae, "-nostdinc");
1390                                 newae(ctx->i_ae, s);
1391                                 free(s);
1392                                 break;
1393                         }
1394                         error(arg);
1395                         break;
1396                 case 'Q':
1397                         /*
1398                          * We could map -Qy into -Wl,-Qy etc.
1399                          */
1400                 default:
1401                         error(arg);
1402                         break;
1403                 }
1404         }
1405 
1406         free(nameflag);
1407 
1408         if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1409             op != CW_O_PREPROCESS) {
1410                 (void) errx(2, "multiple source files are "
1411                     "allowed only with -E or -P");
1412         }
1413 
1414         /*
1415          * Make sure that we do not have any unintended interactions between
1416          * the xarch options passed in and the version of the Studio compiler
1417          * used.
1418          */
1419         if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1420                 (void) errx(2,
1421                     "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1422         }
1423 
1424         switch (mflag) {
1425         case 0:
1426                 /* FALLTHROUGH */
1427         case M32:
1428 #if defined(__sparc)
1429                 /*
1430                  * Only -m32 is defined and so put in the missing xarch
1431                  * translation.
1432                  */
1433                 newae(ctx->i_ae, "-mcpu=v8");
1434                 newae(ctx->i_ae, "-mno-v8plus");
1435 #endif
1436                 break;
1437         case M64:
1438 #if defined(__sparc)
1439                 /*
1440                  * Only -m64 is defined and so put in the missing xarch
1441                  * translation.
1442                  */
1443                 newae(ctx->i_ae, "-mcpu=v9");
1444 #endif
1445                 break;
1446         case SS12:
1447 #if defined(__sparc)
1448                 /* no -m32/-m64 flag used - this is an error for sparc builds */
1449                 (void) fprintf(stderr, "No -m32/-m64 flag defined\n");
1450                 exit(2);
1451 #endif
1452                 break;
1453         case SS11:
1454                 /* FALLTHROUGH */
1455         case (SS11|M32):
1456         case (SS11|M64):
1457                 break;
1458         case (SS12|M32):
1459 #if defined(__sparc)
1460                 /*
1461                  * Need to add in further 32 bit options because with SS12
1462                  * the xarch=sparcvis option can be applied to 32 or 64
1463                  * bit, and so the translatation table (xtbl) cannot handle
1464                  * that.
1465                  */
1466                 newae(ctx->i_ae, "-mv8plus");
1467 #endif
1468                 break;
1469         case (SS12|M64):
1470                 break;
1471         default:
1472                 (void) fprintf(stderr,
1473                     "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1474                 exit(2);
1475         }
1476         if (op == CW_O_LINK && (ctx->i_flags & CW_F_SHADOW))
1477                 exit(0);
1478 
1479         if (model && !pic)
1480                 newae(ctx->i_ae, model);
1481         if (!nolibc)
1482                 newae(ctx->i_ae, "-lc");
1483         if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1484                 newae(ctx->i_ae, "-o");
1485                 newae(ctx->i_ae, ctx->i_discard);
1486         }
1487 }
1488 
1489 static void
1490 do_cc(cw_ictx_t *ctx)
1491 {
1492         int in_output = 0, seen_o = 0;
1493         cw_op_t op = CW_O_LINK;
1494         char *nameflag;
1495 
1496         if (ctx->i_flags & CW_F_PROG) {
1497                 newae(ctx->i_ae, "-V");
1498                 return;
1499         }
1500 
1501         if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->name) == -1)
1502                 nomem();
1503 
1504         while (--ctx->i_oldargc > 0) {
1505                 char *arg = *++ctx->i_oldargv;
1506 
1507                 if (strncmp(arg, "-_CC=", 5) == 0) {
1508                         newae(ctx->i_ae, strchr(arg, '=') + 1);
1509                         continue;
1510                 }
1511 
1512                 if (*arg != '-') {
1513                         if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1514                                 newae(ctx->i_ae, arg);
1515                         } else {
1516                                 in_output = 0;
1517                                 newae(ctx->i_ae, ctx->i_discard);
1518                         }
1519                         continue;
1520                 }
1521                 switch (*(arg + 1)) {
1522                 case '_':
1523                         if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
1524                             (strncmp(arg, "-_cc=", 5) == 0) ||
1525                             (strncmp(arg, "-_sun=", 6) == 0)) {
1526                                 newae(ctx->i_ae, strchr(arg, '=') + 1);
1527                         }
1528                         break;
1529 
1530                 case 'V':
1531                         ctx->i_flags &= ~CW_F_ECHO;
1532                         newae(ctx->i_ae, arg);
1533                         break;
1534                 case 'o':
1535                         seen_o = 1;
1536                         if (strlen(arg) == 2) {
1537                                 in_output = 1;
1538                                 newae(ctx->i_ae, arg);
1539                         } else if (ctx->i_flags & CW_F_SHADOW) {
1540                                 newae(ctx->i_ae, "-o");
1541                                 newae(ctx->i_ae, ctx->i_discard);
1542                         } else {
1543                                 newae(ctx->i_ae, arg);
1544                         }
1545                         break;
1546                 case 'c':
1547                 case 'S':
1548                         if (strlen(arg) == 2)
1549                                 op = CW_O_COMPILE;
1550                         newae(ctx->i_ae, arg);
1551                         break;
1552                 case 'E':
1553                 case 'P':
1554                         if (strlen(arg) == 2)
1555                                 op = CW_O_PREPROCESS;
1556                 /*FALLTHROUGH*/
1557                 default:
1558                         newae(ctx->i_ae, arg);
1559                 }
1560         }
1561 
1562         free(nameflag);
1563 
1564         if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1565             (ctx->i_flags & CW_F_SHADOW))
1566                 exit(0);
1567 
1568         if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1569                 newae(ctx->i_ae, "-o");
1570                 newae(ctx->i_ae, ctx->i_discard);
1571         }
1572 }
1573 
1574 static void
1575 prepctx(cw_ictx_t *ctx)
1576 {
1577         newae(ctx->i_ae, ctx->i_compiler->path);
1578 
1579         if (ctx->i_flags & CW_F_PROG) {
1580                 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1581                     "shadow" : "primary", ctx->i_compiler->path);
1582                 (void) fflush(stdout);
1583         }
1584 
1585         if (!(ctx->i_flags & CW_F_XLATE))
1586                 return;
1587 
1588         switch (ctx->i_compiler->style) {
1589         case SUN:
1590                 do_cc(ctx);
1591                 break;
1592         case GNU:
1593                 do_gcc(ctx);
1594                 break;
1595         }
1596 }
1597 
1598 static int
1599 invoke(cw_ictx_t *ctx)
1600 {
1601         char **newargv;
1602         int ac;
1603         struct ae *a;
1604 
1605         if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
1606             NULL)
1607                 nomem();
1608 
1609         if (ctx->i_flags & CW_F_ECHO)
1610                 (void) fprintf(stderr, "+ ");
1611 
1612         for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
1613                 newargv[ac] = a->ae_arg;
1614                 if (ctx->i_flags & CW_F_ECHO)
1615                         (void) fprintf(stderr, "%s ", a->ae_arg);
1616                 if (a == ctx->i_ae->ael_tail)
1617                         break;
1618         }
1619 
1620         if (ctx->i_flags & CW_F_ECHO) {
1621                 (void) fprintf(stderr, "\n");
1622                 (void) fflush(stderr);
1623         }
1624 
1625         if (!(ctx->i_flags & CW_F_EXEC))
1626                 return (0);
1627 
1628         /*
1629          * We must fix up the environment here so that the
1630          * dependency files are not trampled by the shadow compiler.
1631          */
1632         if ((ctx->i_flags & CW_F_SHADOW) &&
1633             (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1634             unsetenv("DEPENDENCIES_OUTPUT") != 0)) {
1635                 (void) fprintf(stderr, "error: environment setup failed: %s\n",
1636                     strerror(errno));
1637                 return (-1);
1638         }
1639 
1640         (void) execv(newargv[0], newargv);
1641         warn("couldn't run %s", newargv[0]);
1642 
1643         return (-1);
1644 }
1645 
1646 static int
1647 reap(cw_ictx_t *ctx)
1648 {
1649         int status, ret = 0;
1650         char buf[1024];
1651         struct stat s;
1652 
1653         /*
1654          * Only wait for one specific child.
1655          */
1656         if (ctx->i_pid <= 0)
1657                 return (-1);
1658 
1659         do {
1660                 if (waitpid(ctx->i_pid, &status, 0) < 0) {
1661                         warn("cannot reap child");
1662                         return (-1);
1663                 }
1664                 if (status != 0) {
1665                         if (WIFSIGNALED(status)) {
1666                                 ret = -WTERMSIG(status);
1667                                 break;
1668                         } else if (WIFEXITED(status)) {
1669                                 ret = WEXITSTATUS(status);
1670                                 break;
1671                         }
1672                 }
1673         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1674 
1675         (void) unlink(ctx->i_discard);
1676 
1677         if (stat(ctx->i_stderr, &s) < 0) {
1678                 warn("stat failed on child cleanup");
1679                 return (-1);
1680         }
1681         if (s.st_size != 0) {
1682                 FILE *f;
1683 
1684                 if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1685                         while (fgets(buf, sizeof (buf), f))
1686                                 (void) fprintf(stderr, "%s", buf);
1687                         (void) fflush(stderr);
1688                         (void) fclose(f);
1689                 }
1690         }
1691         (void) unlink(ctx->i_stderr);
1692         free(ctx->i_stderr);
1693 
1694         /*
1695          * cc returns an error code when given -V; we want that to succeed.
1696          */
1697         if (ctx->i_flags & CW_F_PROG)
1698                 return (0);
1699 
1700         return (ret);
1701 }
1702 
1703 static int
1704 exec_ctx(cw_ictx_t *ctx, int block)
1705 {
1706         char *file;
1707 
1708         /*
1709          * To avoid offending cc's sensibilities, the name of its output
1710          * file must end in '.o'.
1711          */
1712         if ((file = tempnam(NULL, ".cw")) == NULL) {
1713                 nomem();
1714                 return (-1);
1715         }
1716         (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1717         (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1718         free(file);
1719 
1720         if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1721                 nomem();
1722                 return (-1);
1723         }
1724 
1725         if ((ctx->i_pid = fork()) == 0) {
1726                 int fd;
1727 
1728                 (void) fclose(stderr);
1729                 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1730                     0666)) < 0) {
1731                         err(1, "open failed for standard error");
1732                 }
1733                 if (dup2(fd, 2) < 0) {
1734                         err(1, "dup2 failed for standard error");
1735                 }
1736                 if (fd != 2)
1737                         (void) close(fd);
1738                 if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1739                         err(1, "freopen failed for /dev/fd/2");
1740                 }
1741                 prepctx(ctx);
1742                 exit(invoke(ctx));
1743         }
1744 
1745         if (ctx->i_pid < 0) {
1746                 err(1, "fork failed");
1747         }
1748 
1749         if (block)
1750                 return (reap(ctx));
1751 
1752         return (0);
1753 }
1754 
1755 static int
1756 parse_compiler(const char *spec, cw_compiler_t *compiler)
1757 {
1758         char *tspec, *token;
1759 
1760         if ((tspec = strdup(spec)) == NULL)
1761                 err(1, "out of memory");
1762 
1763         if ((token = strsep(&tspec, ",")) == NULL)
1764                 errx(1, "Compiler is missing a name: %s", spec);
1765         compiler->name = token;
1766 
1767         if ((token = strsep(&tspec, ",")) == NULL)
1768                 errx(1, "Compiler is missing a path: %s", spec);
1769         compiler->path = token;
1770 
1771         if ((token = strsep(&tspec, ",")) == NULL)
1772                 errx(1, "Compiler is missing a style: %s", spec);
1773 
1774         if ((strcasecmp(token, "gnu") == 0) ||
1775             (strcasecmp(token, "gcc") == 0))
1776                 compiler->style = GNU;
1777         else if ((strcasecmp(token, "sun") == 0) ||
1778             (strcasecmp(token, "cc") == 0))
1779                 compiler->style = SUN;
1780         else
1781                 errx(1, "unknown compiler style: %s", token);
1782 
1783         if (tspec != NULL)
1784                 errx(1, "Excess tokens in compiler: %s", spec);
1785 
1786         return (0);
1787 }
1788 
1789 int
1790 main(int argc, char **argv)
1791 {
1792         int ch;
1793         cw_compiler_t primary = { NULL, NULL, 0 };
1794         cw_compiler_t shadows[10];
1795         int nshadows = 0;
1796         int ret = 0;
1797         boolean_t do_serial = B_FALSE;
1798         boolean_t do_exec = B_FALSE;
1799         boolean_t vflg = B_FALSE;
1800         boolean_t Cflg = B_FALSE;
1801         boolean_t cflg = B_FALSE;
1802         boolean_t nflg = B_FALSE;
1803 
1804         cw_ictx_t *main_ctx = newictx();
1805 
1806         static struct option longopts[] = {
1807                 { "compiler", no_argument, NULL, 'c' },
1808                 { "noecho", no_argument, NULL ,'n' },
1809                 { "primary", required_argument, NULL, 'p' },
1810                 { "shadow", required_argument, NULL, 's' },
1811                 { "versions", no_argument, NULL, 'v' },
1812                 { NULL, 0, NULL, 0 },
1813         };
1814 
1815         while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) {
1816                 switch (ch) {
1817                 case 'c':
1818                         cflg = B_TRUE;
1819                         break;
1820                 case 'C':
1821                         Cflg = B_TRUE;
1822                         break;
1823                 case 'n':
1824                         nflg = B_TRUE;
1825                         break;
1826                 case 'p':
1827                         if (primary.path != NULL) {
1828                                 warnx("Only one primary compiler may be specified");
1829                                 usage();
1830                         }
1831 
1832                         if (parse_compiler(optarg, &primary) != 0)
1833                                 errx(1, "Couldn't parse %s as a compiler spec", optarg);
1834                         break;
1835                 case 's':
1836                         if (nshadows >= 10)
1837                                 errx(1, "May only use 10 shadows at the moment");
1838                         if (parse_compiler(optarg, &shadows[nshadows]) != 0)
1839                                 errx(1, "Couldn't parse %s as a compiler spec", optarg);
1840                         nshadows++;
1841                         break;
1842                 case 'v':
1843                         vflg = B_TRUE;
1844                         break;
1845                 default:
1846                         fprintf(stderr, "Did you forget '--'?\n");
1847                         usage();
1848                 }
1849         }
1850 
1851         if (primary.path == NULL) {
1852                 warnx("A primary compiler must be specified");
1853                 usage();
1854         }
1855 
1856         do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE;
1857         do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE;
1858 
1859         /* Leave room for argv[0] */
1860         argc -= (optind - 1);
1861         argv += (optind - 1);
1862 
1863         if (main_ctx == NULL)
1864                 nomem();
1865 
1866         main_ctx->i_oldargc = argc;
1867         main_ctx->i_oldargv = argv;
1868         main_ctx->i_flags = CW_F_XLATE;
1869         if (nflg == 0)
1870                 main_ctx->i_flags |= CW_F_ECHO;
1871         if (do_exec)
1872                 main_ctx->i_flags |= CW_F_EXEC;
1873         if (Cflg)
1874                 main_ctx->i_flags |= CW_F_CXX;
1875         main_ctx->i_compiler = &primary;
1876 
1877         if (cflg) {
1878                 fputs(primary.path, stdout);
1879         }
1880 
1881         if (vflg) {
1882                 (void) printf("cw version %s\n", CW_VERSION);
1883                 (void) fflush(stdout);
1884                 main_ctx->i_flags &= ~CW_F_ECHO;
1885                 main_ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1886                 do_serial = 1;
1887         }
1888 
1889         ret |= exec_ctx(main_ctx, do_serial);
1890 
1891         for (int i = 0; i < nshadows; i++) {
1892                 cw_ictx_t *shadow_ctx = newictx();
1893 
1894                 if (shadow_ctx == NULL)
1895                         nomem();
1896 
1897                 memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));
1898 
1899                 shadow_ctx->i_flags |= CW_F_SHADOW;
1900                 shadow_ctx->i_compiler = &shadows[i];
1901 
1902                 /* XXX: Would be nice to run these parallel, too */
1903                 ret |= exec_ctx(shadow_ctx, 1);
1904         }
1905 
1906         if (!do_serial)
1907                 ret |= reap(main_ctx);
1908 
1909         return (ret);
1910 }