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