Print this page
But keep the Xamode() removal
Revert "9899 cw(1onbld) should shadow more compilation" (leaks object files)
This reverts commit d0843a33a8ba49c316537132aa23e7df6d6fc64b.

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;
      311 +        char            i_discard[MAXPATHLEN];
 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 ↓ 224 lines elided ↑ open up ↑
 546  546                  error(xarg);
 547  547  
 548  548          table++;
 549  549  
 550  550          while (*table != NULL) {
 551  551                  newae(h, *table);
 552  552                  table++;
 553  553          }
 554  554  }
 555  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      -
 585  556  static void
 586  557  do_gcc(cw_ictx_t *ctx)
 587  558  {
 588  559          int c;
 589  560          int nolibc = 0;
 590  561          int in_output = 0, seen_o = 0, c_files = 0;
 591  562          cw_op_t op = CW_O_LINK;
 592  563          char *model = NULL;
 593  564          char *nameflag;
 594      -        int mflag = 0;
      565 +        int     mflag = 0;
 595  566  
 596  567          if (ctx->i_flags & CW_F_PROG) {
 597  568                  newae(ctx->i_ae, "--version");
 598  569                  return;
 599  570          }
 600  571  
 601  572          newae(ctx->i_ae, "-fident");
 602  573          newae(ctx->i_ae, "-finline");
 603  574          newae(ctx->i_ae, "-fno-inline-functions");
 604  575          newae(ctx->i_ae, "-fno-builtin");
↓ open down ↓ 47 lines elided ↑ open up ↑
 652  623                              (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
 653  624                              arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
 654  625                                  c_files++;
 655  626  
 656  627                          /*
 657  628                           * Otherwise, filenames and partial arguments
 658  629                           * are passed through for gcc to chew on.  However,
 659  630                           * output is always discarded for the secondary
 660  631                           * compiler.
 661  632                           */
 662      -                        if ((ctx->i_flags & CW_F_SHADOW) && in_output) {
 663      -                                ctx->i_discard = discard_file_name(arg);
      633 +                        if ((ctx->i_flags & CW_F_SHADOW) && in_output)
 664  634                                  newae(ctx->i_ae, ctx->i_discard);
 665      -                        } else {
      635 +                        else
 666  636                                  newae(ctx->i_ae, arg);
 667      -                        }
 668  637                          in_output = 0;
 669  638                          continue;
 670  639                  }
 671  640  
 672  641                  if (ctx->i_flags & CW_F_CXX) {
 673  642                          if (strncmp(arg, "-_g++=", 6) == 0) {
 674  643                                  newae(ctx->i_ae, strchr(arg, '=') + 1);
 675  644                                  continue;
 676  645                          }
 677  646                          if (strncmp(arg, "-compat=", 8) == 0) {
↓ open down ↓ 95 lines elided ↑ open up ↑
 773  742                  case 'w':
 774  743                          newae(ctx->i_ae, arg);
 775  744                          break;
 776  745                  case 'o':
 777  746                          seen_o = 1;
 778  747                          if (arglen == 1) {
 779  748                                  in_output = 1;
 780  749                                  newae(ctx->i_ae, arg);
 781  750                          } else if (ctx->i_flags & CW_F_SHADOW) {
 782  751                                  newae(ctx->i_ae, "-o");
 783      -                                ctx->i_discard = discard_file_name(arg);
 784  752                                  newae(ctx->i_ae, ctx->i_discard);
 785  753                          } else {
 786  754                                  newae(ctx->i_ae, arg);
 787  755                          }
 788  756                          break;
 789  757                  case 'D':
 790  758                          newae(ctx->i_ae, arg);
 791  759                          /*
 792  760                           * XXX  Clearly a hack ... do we need _KADB too?
 793  761                           */
↓ open down ↓ 418 lines elided ↑ open up ↑
1212 1180                           * We could map -Qy into -Wl,-Qy etc.
1213 1181                           */
1214 1182                  default:
1215 1183                          error(arg);
1216 1184                          break;
1217 1185                  }
1218 1186          }
1219 1187  
1220 1188          free(nameflag);
1221 1189  
1222      -        /*
1223      -         * When compiling multiple source files in a single invocation some
1224      -         * compilers output objects into the current directory with
1225      -         * predictable and conventional names.
1226      -         *
1227      -         * We prevent any attempt to compile multiple files at once so that
1228      -         * any such objects created by a shadow can't escape into a later
1229      -         * link-edit.
1230      -         */
1231      -        if (c_files > 1 && op != CW_O_PREPROCESS) {
     1190 +        if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
     1191 +            op != CW_O_PREPROCESS) {
1232 1192                  errx(2, "multiple source files are "
1233 1193                      "allowed only with -E or -P");
1234 1194          }
1235 1195  
1236 1196          /*
1237 1197           * Make sure that we do not have any unintended interactions between
1238 1198           * the xarch options passed in and the version of the Studio compiler
1239 1199           * used.
1240 1200           */
1241 1201          if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
↓ open down ↓ 47 lines elided ↑ open up ↑
1289 1249  #endif
1290 1250                  break;
1291 1251          case (SS12|M64):
1292 1252                  break;
1293 1253          default:
1294 1254                  (void) fprintf(stderr,
1295 1255                      "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1296 1256                  exit(2);
1297 1257          }
1298 1258  
1299      -        if (ctx->i_flags & CW_F_SHADOW) {
1300      -                if (op == CW_O_PREPROCESS)
1301      -                        exit(0);
1302      -                else if (op == CW_O_LINK && c_files == 0)
1303      -                        exit(0);
1304      -        }
     1259 +        if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
     1260 +            (ctx->i_flags & CW_F_SHADOW))
     1261 +                exit(0);
1305 1262  
1306 1263          if (model != NULL)
1307 1264                  newae(ctx->i_ae, model);
1308 1265          if (!nolibc)
1309 1266                  newae(ctx->i_ae, "-lc");
1310 1267          if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1311      -                ctx->i_discard = discard_file_name(NULL);
1312 1268                  newae(ctx->i_ae, "-o");
1313 1269                  newae(ctx->i_ae, ctx->i_discard);
1314 1270          }
1315 1271  }
1316 1272  
1317 1273  static void
1318 1274  do_cc(cw_ictx_t *ctx)
1319 1275  {
1320      -        int in_output = 0, seen_o = 0, c_files = 0;
     1276 +        int in_output = 0, seen_o = 0;
1321 1277          cw_op_t op = CW_O_LINK;
1322 1278          char *nameflag;
1323 1279  
1324 1280          if (ctx->i_flags & CW_F_PROG) {
1325 1281                  newae(ctx->i_ae, "-V");
1326 1282                  return;
1327 1283          }
1328 1284  
1329 1285          if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
1330 1286                  nomem();
1331 1287  
1332 1288          while (--ctx->i_oldargc > 0) {
1333 1289                  char *arg = *++ctx->i_oldargv;
1334      -                size_t arglen = strlen(arg);
1335 1290  
1336 1291                  if (strncmp(arg, "-_CC=", 5) == 0) {
1337 1292                          newae(ctx->i_ae, strchr(arg, '=') + 1);
1338 1293                          continue;
1339 1294                  }
1340 1295  
1341 1296                  if (*arg != '-') {
1342      -                        if (!in_output && arglen > 2 &&
1343      -                            arg[arglen - 2] == '.' &&
1344      -                            (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
1345      -                            arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
1346      -                                c_files++;
1347      -
1348 1297                          if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1349 1298                                  newae(ctx->i_ae, arg);
1350 1299                          } else {
1351 1300                                  in_output = 0;
1352      -                                ctx->i_discard = discard_file_name(arg);
1353 1301                                  newae(ctx->i_ae, ctx->i_discard);
1354 1302                          }
1355 1303                          continue;
1356 1304                  }
1357 1305                  switch (*(arg + 1)) {
1358 1306                  case '_':
1359 1307                          if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
1360 1308                              (strncmp(arg, "-_cc=", 5) == 0) ||
1361 1309                              (strncmp(arg, "-_sun=", 6) == 0)) {
1362 1310                                  newae(ctx->i_ae, strchr(arg, '=') + 1);
↓ open down ↓ 4 lines elided ↑ open up ↑
1367 1315                          ctx->i_flags &= ~CW_F_ECHO;
1368 1316                          newae(ctx->i_ae, arg);
1369 1317                          break;
1370 1318                  case 'o':
1371 1319                          seen_o = 1;
1372 1320                          if (strlen(arg) == 2) {
1373 1321                                  in_output = 1;
1374 1322                                  newae(ctx->i_ae, arg);
1375 1323                          } else if (ctx->i_flags & CW_F_SHADOW) {
1376 1324                                  newae(ctx->i_ae, "-o");
1377      -                                ctx->i_discard = discard_file_name(arg);
1378 1325                                  newae(ctx->i_ae, ctx->i_discard);
1379 1326                          } else {
1380 1327                                  newae(ctx->i_ae, arg);
1381 1328                          }
1382 1329                          break;
1383 1330                  case 'c':
1384 1331                  case 'S':
1385 1332                          if (strlen(arg) == 2)
1386 1333                                  op = CW_O_COMPILE;
1387 1334                          newae(ctx->i_ae, arg);
↓ open down ↓ 3 lines elided ↑ open up ↑
1391 1338                          if (strlen(arg) == 2)
1392 1339                                  op = CW_O_PREPROCESS;
1393 1340                  /*FALLTHROUGH*/
1394 1341                  default:
1395 1342                          newae(ctx->i_ae, arg);
1396 1343                  }
1397 1344          }
1398 1345  
1399 1346          free(nameflag);
1400 1347  
1401      -        /* See the comment on this same code in do_gcc() */
1402      -        if (c_files > 1 && op != CW_O_PREPROCESS) {
1403      -                errx(2, "multiple source files are "
1404      -                    "allowed only with -E or -P");
1405      -        }
     1348 +        if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
     1349 +            (ctx->i_flags & CW_F_SHADOW))
     1350 +                exit(0);
1406 1351  
1407      -        if (ctx->i_flags & CW_F_SHADOW) {
1408      -                if (op == CW_O_PREPROCESS)
1409      -                        exit(0);
1410      -                else if (op == CW_O_LINK && c_files == 0)
1411      -                        exit(0);
1412      -        }
1413      -
1414 1352          if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1415 1353                  newae(ctx->i_ae, "-o");
1416      -                ctx->i_discard = discard_file_name(NULL);
1417 1354                  newae(ctx->i_ae, ctx->i_discard);
1418 1355          }
1419 1356  }
1420 1357  
1421 1358  static void
1422 1359  prepctx(cw_ictx_t *ctx)
1423 1360  {
1424 1361          newae(ctx->i_ae, ctx->i_compiler->c_path);
1425 1362  
1426 1363          if (ctx->i_flags & CW_F_PROG) {
↓ open down ↓ 89 lines elided ↑ open up ↑
1516 1453                                  ret = -WTERMSIG(status);
1517 1454                                  break;
1518 1455                          } else if (WIFEXITED(status)) {
1519 1456                                  ret = WEXITSTATUS(status);
1520 1457                                  break;
1521 1458                          }
1522 1459                  }
1523 1460          } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1524 1461  
1525 1462          (void) unlink(ctx->i_discard);
1526      -        free(ctx->i_discard);
1527 1463  
1528 1464          if (stat(ctx->i_stderr, &s) < 0) {
1529 1465                  warn("stat failed on child cleanup");
1530 1466                  return (-1);
1531 1467          }
1532 1468          if (s.st_size != 0) {
1533 1469                  FILE *f;
1534 1470  
1535 1471                  if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1536 1472                          while (fgets(buf, sizeof (buf), f))
↓ open down ↓ 10 lines elided ↑ open up ↑
1547 1483           */
1548 1484          if (ctx->i_flags & CW_F_PROG)
1549 1485                  return (0);
1550 1486  
1551 1487          return (ret);
1552 1488  }
1553 1489  
1554 1490  static int
1555 1491  exec_ctx(cw_ictx_t *ctx, int block)
1556 1492  {
     1493 +        char *file;
     1494 +
     1495 +        /*
     1496 +         * To avoid offending cc's sensibilities, the name of its output
     1497 +         * file must end in '.o'.
     1498 +         */
     1499 +        if ((file = tempnam(NULL, ".cw")) == NULL) {
     1500 +                nomem();
     1501 +                return (-1);
     1502 +        }
     1503 +        (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
     1504 +        (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
     1505 +        free(file);
     1506 +
1557 1507          if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1558 1508                  nomem();
1559 1509                  return (-1);
1560 1510          }
1561 1511  
1562 1512          if ((ctx->i_pid = fork()) == 0) {
1563 1513                  int fd;
1564 1514  
1565 1515                  (void) fclose(stderr);
1566 1516                  if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
↓ open down ↓ 192 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX