Print this page
9899 cw(1onbld) should shadow more compilation
9888 cw shouldn't use __unused

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/cw/cw.c
          +++ new/usr/src/tools/cw/cw.c
↓ open down ↓ 300 lines elided ↑ open up ↑
 301  301  } cw_compiler_t;
 302  302  
 303  303  typedef struct cw_ictx {
 304  304          struct cw_ictx  *i_next;
 305  305          cw_compiler_t   *i_compiler;
 306  306          struct aelist   *i_ae;
 307  307          uint32_t        i_flags;
 308  308          int             i_oldargc;
 309  309          char            **i_oldargv;
 310  310          pid_t           i_pid;
 311      -        char            i_discard[MAXPATHLEN];
      311 +        char            *i_discard;
 312  312          char            *i_stderr;
 313  313  } cw_ictx_t;
 314  314  
 315  315  /*
 316  316   * Status values to indicate which Studio compiler and associated
 317  317   * flags are being used.
 318  318   */
 319  319  #define M32             0x01    /* -m32 - only on Studio 12 */
 320  320  #define M64             0x02    /* -m64 - only on Studio 12 */
 321  321  #define SS11            0x100   /* Studio 11 */
↓ open down ↓ 150 lines elided ↑ open up ↑
 472  472  static void
 473  473  optim_disable(struct aelist *h, int level)
 474  474  {
 475  475          if (level >= 2) {
 476  476                  newae(h, "-fno-strict-aliasing");
 477  477                  newae(h, "-fno-unit-at-a-time");
 478  478                  newae(h, "-fno-optimize-sibling-calls");
 479  479          }
 480  480  }
 481  481  
 482      -/* ARGSUSED */
 483      -static void
 484      -Xamode(struct aelist __unused *h)
 485      -{
 486      -}
 487      -
 488  482  static void
 489  483  Xsmode(struct aelist *h)
 490  484  {
 491  485          static int xsonce;
 492  486  
 493  487          if (xsonce++)
 494  488                  return;
 495  489  
 496  490          newae(h, "-traditional");
 497  491          newae(h, "-traditional-cpp");
↓ open down ↓ 54 lines elided ↑ open up ↑
 552  546                  error(xarg);
 553  547  
 554  548          table++;
 555  549  
 556  550          while (*table != NULL) {
 557  551                  newae(h, *table);
 558  552                  table++;
 559  553          }
 560  554  }
 561  555  
      556 +/*
      557 + * The compiler wants the output file to end in appropriate extension.  If
      558 + * we're generating a name from whole cloth (path == NULL), we assume that
      559 + * extension to be .o, otherwise we match the extension of the caller.
      560 + */
      561 +static char *
      562 +discard_file_name(const char *path)
      563 +{
      564 +        char *ret, *ext, *file;
      565 +
      566 +        if (path == NULL) {
      567 +                ext = ".o";
      568 +        } else {
      569 +                ext = strrchr(path, '.');
      570 +        }
      571 +
      572 +        if ((ret = calloc(MAXPATHLEN, sizeof (char))) == NULL)
      573 +                nomem();
      574 +
      575 +        if ((file = tempnam(NULL, ".cw")) == NULL)
      576 +                nomem();
      577 +
      578 +        (void) strlcpy(ret, file, MAXPATHLEN);
      579 +        if (ext != NULL)
      580 +                (void) strlcat(ret, ext, MAXPATHLEN);
      581 +        free(file);
      582 +        return (ret);
      583 +}
      584 +
 562  585  static void
 563  586  do_gcc(cw_ictx_t *ctx)
 564  587  {
 565  588          int c;
 566  589          int nolibc = 0;
 567  590          int in_output = 0, seen_o = 0, c_files = 0;
 568  591          cw_op_t op = CW_O_LINK;
 569  592          char *model = NULL;
 570  593          char *nameflag;
 571      -        int     mflag = 0;
      594 +        int mflag = 0;
 572  595  
 573  596          if (ctx->i_flags & CW_F_PROG) {
 574  597                  newae(ctx->i_ae, "--version");
 575  598                  return;
 576  599          }
 577  600  
 578  601          newae(ctx->i_ae, "-fident");
 579  602          newae(ctx->i_ae, "-finline");
 580  603          newae(ctx->i_ae, "-fno-inline-functions");
 581  604          newae(ctx->i_ae, "-fno-builtin");
↓ open down ↓ 47 lines elided ↑ open up ↑
 629  652                              (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
 630  653                              arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
 631  654                                  c_files++;
 632  655  
 633  656                          /*
 634  657                           * Otherwise, filenames and partial arguments
 635  658                           * are passed through for gcc to chew on.  However,
 636  659                           * output is always discarded for the secondary
 637  660                           * compiler.
 638  661                           */
 639      -                        if ((ctx->i_flags & CW_F_SHADOW) && in_output)
      662 +                        if ((ctx->i_flags & CW_F_SHADOW) && in_output) {
      663 +                                ctx->i_discard = discard_file_name(arg);
      664 +
      665 +                                if (ctx->i_discard == NULL)
      666 +                                        nomem();
 640  667                                  newae(ctx->i_ae, ctx->i_discard);
 641      -                        else
      668 +                        } else {
 642  669                                  newae(ctx->i_ae, arg);
      670 +                        }
 643  671                          in_output = 0;
 644  672                          continue;
 645  673                  }
 646  674  
 647  675                  if (ctx->i_flags & CW_F_CXX) {
 648  676                          if (strncmp(arg, "-_g++=", 6) == 0) {
 649  677                                  newae(ctx->i_ae, strchr(arg, '=') + 1);
 650  678                                  continue;
 651  679                          }
 652  680                          if (strncmp(arg, "-compat=", 8) == 0) {
↓ open down ↓ 95 lines elided ↑ open up ↑
 748  776                  case 'w':
 749  777                          newae(ctx->i_ae, arg);
 750  778                          break;
 751  779                  case 'o':
 752  780                          seen_o = 1;
 753  781                          if (arglen == 1) {
 754  782                                  in_output = 1;
 755  783                                  newae(ctx->i_ae, arg);
 756  784                          } else if (ctx->i_flags & CW_F_SHADOW) {
 757  785                                  newae(ctx->i_ae, "-o");
      786 +                                ctx->i_discard = discard_file_name(arg);
      787 +                                if (ctx->i_discard == NULL)
      788 +                                        nomem();
      789 +
 758  790                                  newae(ctx->i_ae, ctx->i_discard);
 759  791                          } else {
 760  792                                  newae(ctx->i_ae, arg);
 761  793                          }
 762  794                          break;
 763  795                  case 'D':
 764  796                          newae(ctx->i_ae, arg);
 765  797                          /*
 766  798                           * XXX  Clearly a hack ... do we need _KADB too?
 767  799                           */
↓ open down ↓ 221 lines elided ↑ open up ↑
 989 1021                          if (strcmp(arg, "-Wu,-save_args") == 0) {
 990 1022                                  newae(ctx->i_ae, "-msave-args");
 991 1023                                  break;
 992 1024                          }
 993 1025  #endif  /* __x86 */
 994 1026                          error(arg);
 995 1027                          break;
 996 1028                  case 'X':
 997 1029                          if (strcmp(arg, "-Xa") == 0 ||
 998 1030                              strcmp(arg, "-Xt") == 0) {
 999      -                                Xamode(ctx->i_ae);
1000 1031                                  break;
1001 1032                          }
1002 1033                          if (strcmp(arg, "-Xs") == 0) {
1003 1034                                  Xsmode(ctx->i_ae);
1004 1035                                  break;
1005 1036                          }
1006 1037                          error(arg);
1007 1038                          break;
1008 1039                  case 'x':
1009 1040                          if (arglen == 1)
↓ open down ↓ 177 lines elided ↑ open up ↑
1187 1218                           * We could map -Qy into -Wl,-Qy etc.
1188 1219                           */
1189 1220                  default:
1190 1221                          error(arg);
1191 1222                          break;
1192 1223                  }
1193 1224          }
1194 1225  
1195 1226          free(nameflag);
1196 1227  
1197      -        if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1198      -            op != CW_O_PREPROCESS) {
     1228 +        /*
     1229 +         * When compiling multiple source files in a single invocation some
     1230 +         * compilers output objects into the current directory with
     1231 +         * predictable and conventional names.
     1232 +         *
     1233 +         * We prevent any attempt to compile multiple files at once so that
     1234 +         * any such objects created by a shadow can't escape into a later
     1235 +         * link-edit.
     1236 +         */
     1237 +        if (c_files > 1 && op != CW_O_PREPROCESS) {
1199 1238                  errx(2, "multiple source files are "
1200 1239                      "allowed only with -E or -P");
1201 1240          }
1202 1241  
1203 1242          /*
1204 1243           * Make sure that we do not have any unintended interactions between
1205 1244           * the xarch options passed in and the version of the Studio compiler
1206 1245           * used.
1207 1246           */
1208 1247          if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
↓ open down ↓ 47 lines elided ↑ open up ↑
1256 1295  #endif
1257 1296                  break;
1258 1297          case (SS12|M64):
1259 1298                  break;
1260 1299          default:
1261 1300                  (void) fprintf(stderr,
1262 1301                      "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1263 1302                  exit(2);
1264 1303          }
1265 1304  
1266      -        if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1267      -            (ctx->i_flags & CW_F_SHADOW))
1268      -                exit(0);
     1305 +        if (ctx->i_flags & CW_F_SHADOW) {
     1306 +                if (op == CW_O_PREPROCESS)
     1307 +                        exit(0);
     1308 +                else if (op == CW_O_LINK && c_files == 0)
     1309 +                        exit(0);
     1310 +        }
1269 1311  
1270 1312          if (model != NULL)
1271 1313                  newae(ctx->i_ae, model);
1272 1314          if (!nolibc)
1273 1315                  newae(ctx->i_ae, "-lc");
1274 1316          if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
     1317 +                ctx->i_discard = discard_file_name(NULL);
     1318 +
     1319 +                if (ctx->i_discard == NULL)
     1320 +                        nomem();
1275 1321                  newae(ctx->i_ae, "-o");
1276 1322                  newae(ctx->i_ae, ctx->i_discard);
1277 1323          }
1278 1324  }
1279 1325  
1280 1326  static void
1281 1327  do_cc(cw_ictx_t *ctx)
1282 1328  {
1283      -        int in_output = 0, seen_o = 0;
     1329 +        int in_output = 0, seen_o = 0, c_files = 0;
1284 1330          cw_op_t op = CW_O_LINK;
1285 1331          char *nameflag;
1286 1332  
1287 1333          if (ctx->i_flags & CW_F_PROG) {
1288 1334                  newae(ctx->i_ae, "-V");
1289 1335                  return;
1290 1336          }
1291 1337  
1292 1338          if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
1293 1339                  nomem();
1294 1340  
1295 1341          while (--ctx->i_oldargc > 0) {
1296 1342                  char *arg = *++ctx->i_oldargv;
     1343 +                size_t arglen = strlen(arg);
1297 1344  
1298 1345                  if (strncmp(arg, "-_CC=", 5) == 0) {
1299 1346                          newae(ctx->i_ae, strchr(arg, '=') + 1);
1300 1347                          continue;
1301 1348                  }
1302 1349  
1303 1350                  if (*arg != '-') {
     1351 +                        if (!in_output && arglen > 2 &&
     1352 +                            arg[arglen - 2] == '.' &&
     1353 +                            (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
     1354 +                            arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
     1355 +                                c_files++;
     1356 +
1304 1357                          if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1305 1358                                  newae(ctx->i_ae, arg);
1306 1359                          } else {
1307 1360                                  in_output = 0;
     1361 +                                ctx->i_discard = discard_file_name(arg);
     1362 +                                if (ctx->i_discard == NULL)
     1363 +                                        nomem();
1308 1364                                  newae(ctx->i_ae, ctx->i_discard);
1309 1365                          }
1310 1366                          continue;
1311 1367                  }
1312 1368                  switch (*(arg + 1)) {
1313 1369                  case '_':
1314 1370                          if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
1315 1371                              (strncmp(arg, "-_cc=", 5) == 0) ||
1316 1372                              (strncmp(arg, "-_sun=", 6) == 0)) {
1317 1373                                  newae(ctx->i_ae, strchr(arg, '=') + 1);
↓ open down ↓ 4 lines elided ↑ open up ↑
1322 1378                          ctx->i_flags &= ~CW_F_ECHO;
1323 1379                          newae(ctx->i_ae, arg);
1324 1380                          break;
1325 1381                  case 'o':
1326 1382                          seen_o = 1;
1327 1383                          if (strlen(arg) == 2) {
1328 1384                                  in_output = 1;
1329 1385                                  newae(ctx->i_ae, arg);
1330 1386                          } else if (ctx->i_flags & CW_F_SHADOW) {
1331 1387                                  newae(ctx->i_ae, "-o");
     1388 +                                ctx->i_discard = discard_file_name(arg);
     1389 +                                if (ctx->i_discard == NULL)
     1390 +                                        nomem();
1332 1391                                  newae(ctx->i_ae, ctx->i_discard);
1333 1392                          } else {
1334 1393                                  newae(ctx->i_ae, arg);
1335 1394                          }
1336 1395                          break;
1337 1396                  case 'c':
1338 1397                  case 'S':
1339 1398                          if (strlen(arg) == 2)
1340 1399                                  op = CW_O_COMPILE;
1341 1400                          newae(ctx->i_ae, arg);
↓ open down ↓ 3 lines elided ↑ open up ↑
1345 1404                          if (strlen(arg) == 2)
1346 1405                                  op = CW_O_PREPROCESS;
1347 1406                  /*FALLTHROUGH*/
1348 1407                  default:
1349 1408                          newae(ctx->i_ae, arg);
1350 1409                  }
1351 1410          }
1352 1411  
1353 1412          free(nameflag);
1354 1413  
1355      -        if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1356      -            (ctx->i_flags & CW_F_SHADOW))
1357      -                exit(0);
     1414 +        /* See the comment on this same code in do_gcc() */
     1415 +        if (c_files > 1 && op != CW_O_PREPROCESS) {
     1416 +                errx(2, "multiple source files are "
     1417 +                    "allowed only with -E or -P");
     1418 +        }
     1419 +
     1420 +        if (ctx->i_flags & CW_F_SHADOW) {
     1421 +                if (op == CW_O_PREPROCESS)
     1422 +                        exit(0);
     1423 +                else if (op == CW_O_LINK && c_files == 0)
     1424 +                        exit(0);
     1425 +        }
1358 1426  
1359 1427          if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1360 1428                  newae(ctx->i_ae, "-o");
     1429 +                ctx->i_discard = discard_file_name(NULL);
     1430 +
     1431 +                if (ctx->i_discard == NULL)
     1432 +                        nomem();
1361 1433                  newae(ctx->i_ae, ctx->i_discard);
1362 1434          }
1363 1435  }
1364 1436  
1365 1437  static void
1366 1438  prepctx(cw_ictx_t *ctx)
1367 1439  {
1368 1440          newae(ctx->i_ae, ctx->i_compiler->c_path);
1369 1441  
1370 1442          if (ctx->i_flags & CW_F_PROG) {
↓ open down ↓ 89 lines elided ↑ open up ↑
1460 1532                                  ret = -WTERMSIG(status);
1461 1533                                  break;
1462 1534                          } else if (WIFEXITED(status)) {
1463 1535                                  ret = WEXITSTATUS(status);
1464 1536                                  break;
1465 1537                          }
1466 1538                  }
1467 1539          } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1468 1540  
1469 1541          (void) unlink(ctx->i_discard);
     1542 +        free(ctx->i_discard);
1470 1543  
1471 1544          if (stat(ctx->i_stderr, &s) < 0) {
1472 1545                  warn("stat failed on child cleanup");
1473 1546                  return (-1);
1474 1547          }
1475 1548          if (s.st_size != 0) {
1476 1549                  FILE *f;
1477 1550  
1478 1551                  if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1479 1552                          while (fgets(buf, sizeof (buf), f))
↓ open down ↓ 10 lines elided ↑ open up ↑
1490 1563           */
1491 1564          if (ctx->i_flags & CW_F_PROG)
1492 1565                  return (0);
1493 1566  
1494 1567          return (ret);
1495 1568  }
1496 1569  
1497 1570  static int
1498 1571  exec_ctx(cw_ictx_t *ctx, int block)
1499 1572  {
1500      -        char *file;
1501      -
1502      -        /*
1503      -         * To avoid offending cc's sensibilities, the name of its output
1504      -         * file must end in '.o'.
1505      -         */
1506      -        if ((file = tempnam(NULL, ".cw")) == NULL) {
1507      -                nomem();
1508      -                return (-1);
1509      -        }
1510      -        (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1511      -        (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1512      -        free(file);
1513      -
1514 1573          if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1515 1574                  nomem();
1516 1575                  return (-1);
1517 1576          }
1518 1577  
1519 1578          if ((ctx->i_pid = fork()) == 0) {
1520 1579                  int fd;
1521 1580  
1522 1581                  (void) fclose(stderr);
1523 1582                  if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
↓ open down ↓ 192 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX