Changeset 3192 for trunk/src/kmk/kmkbuiltin/kDepObj.c
- Timestamp:
- Mar 26, 2018, 10:25:56 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/kDepObj.c
r3167 r3192 39 39 #else 40 40 # include <io.h> 41 typedef intptr_t ssize_t; 41 42 #endif 42 43 #include "k/kDefs.h" … … 44 45 #include "k/kLdrFmts/pe.h" 45 46 #include "kDep.h" 47 #include "err.h" 46 48 #include "kmkbuiltin.h" 47 49 … … 170 172 typedef struct KDEPOBJGLOBALS 171 173 { 174 /** The command execution context. */ 175 PKMKBUILTINCTX pCtx; 172 176 /** Core instance. */ 173 177 DEPGLOBALS Core; 174 /** the executable name. */175 const char *argv0;176 178 /** The file. */ 177 179 const char *pszFile; … … 194 196 static int kDepErr(PKDEPOBJGLOBALS pThis, int rc, const char *pszFormat, ...) 195 197 { 198 char szMsg[2048]; 196 199 va_list va; 197 const char *psz; 198 const char *pszName = pThis->argv0; 199 200 fflush(stdout); 201 202 /* The message prefix. */ 203 while ((psz = strpbrk(pszName, "/\\:")) != NULL) 204 pszName = psz + 1; 200 va_start(va, pszFormat); 201 vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, va); 202 va_end(va); 203 szMsg[sizeof(szMsg) - 1] = '\0'; 205 204 206 205 if (pThis->pszFile) 207 fprintf(stderr, "%s: %s: error: ", pszName, pThis->pszFile);206 warnx(pThis->pCtx, "%s: error: %s", pThis->pszFile, szMsg); 208 207 else 209 fprintf(stderr, "%s: error: ", pszName); 210 211 /* The message. */ 212 va_start(va, pszFormat); 213 vfprintf(stderr, pszFormat, va); 214 va_end(va); 215 208 errx(pThis->pCtx, rc, "%s", szMsg); 216 209 return rc; 217 210 } … … 294 287 PCKDEPOMFTHEADR pTHeadr = (PCKDEPOMFTHEADR)pHdr; 295 288 if (1 + pTHeadr->Name.cch + 1 != pHdr->cbRec) 296 return kDepErr(pThis, 1, "%#07x - Bad %cHEADR record, length mismatch. \n",289 return kDepErr(pThis, 1, "%#07x - Bad %cHEADR record, length mismatch.", 297 290 (const KU8*)pHdr - pbFile, pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L'); 298 291 if ( ( pTHeadr->Name.cch > 2 … … 325 318 326 319 if (pHdr->cbRec < 2 + 1) 327 return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, too small. \n", (const KU8*)pHdr - pbFile);320 return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, too small.", (const KU8*)pHdr - pbFile); 328 321 if (uData.pb[0] & 0x3f) 329 return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, reserved flags set. \n", (const KU8*)pHdr - pbFile);322 return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, reserved flags set.", (const KU8*)pHdr - pbFile); 330 323 uClass = uData.pb[1]; 331 324 uData.pb += 2; … … 344 337 if (pHdr->cbRec == 2 + 1) 345 338 return 0; 346 return kDepErr(pThis, 1, "%#07lx - Bad DEPENDENCY FILE record, length mismatch. (%u/%u) \n",339 return kDepErr(pThis, 1, "%#07lx - Bad DEPENDENCY FILE record, length mismatch. (%u/%u)", 347 340 (long)((const KU8 *)pHdr - pbFile), 348 341 K_OFFSETOF(KDEPOMFDEPFILE, Name.ach[pDep->Name.cch]) + 1, … … 398 391 KU16 uSeg = kDepObjOMFGetIndex(&uData, &cbRecLeft); 399 392 if (uSeg == KU16_MAX) 400 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record \n", (long)((const KU8 *)pHdr - pbFile));393 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record", (long)((const KU8 *)pHdr - pbFile)); 401 394 K_NOREF(uGrp); 402 395 … … 415 408 416 409 if (cbRecLeft < 2+1+1+2+2+4) 417 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too short \n", (long)((const KU8 *)pHdr - pbFile));410 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too short", (long)((const KU8 *)pHdr - pbFile)); 418 411 cbRecLeft -= 2+1+1+2+2+4; 419 412 uLine = *uData.pu16++; … … 431 424 432 425 if (uLine != 0) 433 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, line %#x (MBZ) \n", (long)((const KU8 *)pHdr - pbFile), uLine);426 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, line %#x (MBZ)", (long)((const KU8 *)pHdr - pbFile), uLine); 434 427 cLinFiles = iLinFile = KU32_MAX; 435 428 if ( uLinNumType == 3 /* file names table */ … … 437 430 cLinNums = 0; /* no line numbers */ 438 431 else if (uLinNumType > 4) 439 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, type %#x unknown \n", (long)((const KU8 *)pHdr - pbFile), uLinNumType);432 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, type %#x unknown", (long)((const KU8 *)pHdr - pbFile), uLinNumType); 440 433 } 441 434 else … … 452 445 { 453 446 if (cbRecLeft < cbEntry) 454 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete line entry \n", (long)((const KU8 *)pHdr - pbFile));447 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete line entry", (long)((const KU8 *)pHdr - pbFile)); 455 448 456 449 switch (uLinNumType) … … 492 485 493 486 if (cbRecLeft < 4+4+4) 494 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete file/path table header \n", (long)((const KU8 *)pHdr - pbFile));487 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete file/path table header", (long)((const KU8 *)pHdr - pbFile)); 495 488 cbRecLeft -= 4+4+4; 496 489 … … 501 494 uLinNumType == 3 ? "file names" : "path", cLinFiles, cLinFiles, iFirstCol, cCols)); 502 495 if (cLinFiles == KU32_MAX) 503 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too many file/path table entries. \n", (long)((const KU8 *)pHdr - pbFile));496 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too many file/path table entries.", (long)((const KU8 *)pHdr - pbFile)); 504 497 iLinFile = 0; 505 498 } … … 510 503 int cbName = *uData.pb++; 511 504 if (cbRecLeft < 1 + cbName) 512 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, file/path table entry too long. \n", (long)((const KU8 *)pHdr - pbFile));505 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, file/path table entry too long.", (long)((const KU8 *)pHdr - pbFile)); 513 506 iLinFile++; 514 507 dprintf(("#%" KU32_PRI": %.*s\n", iLinFile, cbName, uData.pch)); … … 542 535 543 536 if (cbLeft) 544 return kDepErr(pThis, 1, "%#07x - Unexpected EOF. cbLeft=%#x \n", (const KU8*)pHdr - pbFile, cbLeft);537 return kDepErr(pThis, 1, "%#07x - Unexpected EOF. cbLeft=%#x", (const KU8*)pHdr - pbFile, cbLeft); 545 538 546 539 if (iSrc == 0 && iMaybeSrc <= 1) … … 612 605 if (off + sizeof(*pHdr) >= cbSyms) 613 606 { 614 fprintf(stderr, "%s: CV symbol table entry at %08" KX32_PRI " is too long; cbSyms=%#" KX32_PRI "\n",615 pThis->argv0,off, cbSyms);607 kDepErr(pThis, 1, "CV symbol table entry at %08" KX32_PRI " is too long; cbSyms=%#" KX32_PRI "", 608 off, cbSyms); 616 609 return 1; /* FIXME */ 617 610 } … … 620 613 if (off + cbData + sizeof(*pHdr) > cbSyms) 621 614 { 622 fprintf(stderr, "%s: CV symbol table entry at %08" KX32_PRI " is too long; cbData=%#" KX32_PRI " cbSyms=%#" KX32_PRI "\n",623 pThis->argv0,off, cbData, cbSyms);615 kDepErr(pThis, 1, "CV symbol table entry at %08" KX32_PRI " is too long; cbData=%#" KX32_PRI " cbSyms=%#" KX32_PRI, 616 off, cbData, cbSyms); 624 617 return 1; /* FIXME */ 625 618 } … … 645 638 dprintf(("%06" KX32_PRI " %06" KX32_PRI ": String table\n", off, cbData)); 646 639 if (pchStrTab) 647 fprintf(stderr, "%s: warning: Found yet another string table!\n", pThis->argv0);640 warnx(pThis->pCtx, "%s: warning: Found yet another string table!", pThis->pszFile); 648 641 pchStrTab = uData.pch; 649 642 cbStrTab = cbData; … … 654 647 dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Source files\n", off, cbData)); 655 648 if (uSrcFiles.pb) 656 fprintf(stderr, "%s: warning: Found yet another source files table!\n", pThis->argv0);649 warnx(pThis->pCtx, "%s: warning: Found yet another source files table!", pThis->pszFile); 657 650 uSrcFiles = uData; 658 651 cbSrcFiles = cbData; … … 703 696 */ 704 697 if (off + 8 > cbSrcFiles) 705 { 706 fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is too long; cbSrcFiles=%#" KX32_PRI "\n", 707 pThis->argv0, off, cbSrcFiles); 708 return 1; 709 } 698 return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " is too long; cbSrcFiles=%#" KX32_PRI, 699 off, cbSrcFiles); 710 700 uSrc.pb = uSrcFiles.pb + off; 711 701 u16Type = uSrc.pu16[2]; 712 702 cbSrc = u16Type == 0x0110 ? 6 + 16 + 2 : 6 + 2; 713 703 if (off + cbSrc > cbSrcFiles) 714 { 715 fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is too long; cbSrc=%#" KX32_PRI " cbSrcFiles=%#" KX32_PRI "\n", 716 pThis->argv0, off, cbSrc, cbSrcFiles); 717 return 1; 718 } 704 return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " is too long; cbSrc=%#" KX32_PRI " cbSrcFiles=%#" KX32_PRI, 705 off, cbSrc, cbSrcFiles); 719 706 720 707 offFile = *uSrc.pu32; 721 708 if (offFile > cbStrTab) 722 { 723 fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is out side the string table; offFile=%#" KX32_PRI " cbStrTab=%#" KX32_PRI "\n", 724 pThis->argv0, off, offFile, cbStrTab); 725 return 1; 726 } 709 return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " is out side the string table; offFile=%#" KX32_PRI " cbStrTab=%#" KX32_PRI, 710 off, offFile, cbStrTab); 727 711 pszFile = pchStrTab + offFile; 728 712 cchFile = strlen(pszFile); 729 713 if (cchFile == 0) 730 { 731 fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " has an empty file name; offFile=%#x" KX32_PRI "\n", 732 pThis->argv0, off, offFile); 733 return 1; 734 } 714 return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " has an empty file name; offFile=%#x" KX32_PRI, 715 off, offFile); 735 716 736 717 /* … … 822 803 * @returns K_TRUE if it's COFF, K_FALSE otherwise. 823 804 * 805 * @param pThis The kDepObj instance data. 824 806 * @param pb The start of the file. 825 807 * @param cb The file size. 826 808 */ 827 KBOOL kDepObjCOFFTest( const KU8 *pbFile, KSIZE cbFile)809 KBOOL kDepObjCOFFTest(PKDEPOBJGLOBALS pThis, const KU8 *pbFile, KSIZE cbFile) 828 810 { 829 811 IMAGE_FILE_HEADER const *pFileHdr = (IMAGE_FILE_HEADER const *)pbFile; … … 864 846 && pBigObjHdr->Machine != IMAGE_FILE_MACHINE_EBC) 865 847 { 866 fprintf(stderr, "kDepObj: error: bigobj Machine not supported: %#x\n", pBigObjHdr->Machine);848 kDepErr(pThis, 1, "bigobj Machine not supported: %#x", pBigObjHdr->Machine); 867 849 return K_FALSE; 868 850 } 869 851 if (pBigObjHdr->Flags != 0) 870 852 { 871 fprintf(stderr, "kDepObj: error: bigobj Flags field is non-zero: %#x\n", pBigObjHdr->Flags);853 kDepErr(pThis, 1, "bigobj Flags field is non-zero: %#x", pBigObjHdr->Flags); 872 854 return K_FALSE; 873 855 } 874 856 if (pBigObjHdr->SizeOfData != 0) 875 857 { 876 fprintf(stderr, "kDepObj: error: bigobj SizeOfData field is non-zero: %#x\n", pBigObjHdr->SizeOfData);858 kDepErr(pThis, 1, "bigobj SizeOfData field is non-zero: %#x", pBigObjHdr->SizeOfData); 877 859 return K_FALSE; 878 860 } … … 977 959 if (kDepObjOMFTest(pbFile, cbFile)) 978 960 rc = kDepObjOMFParse(pThis, pbFile, cbFile); 979 else if (kDepObjCOFFTest(p bFile, cbFile))961 else if (kDepObjCOFFTest(pThis, pbFile, cbFile)) 980 962 rc = kDepObjCOFFParse(pThis, pbFile, cbFile); 981 963 else 982 { 983 fprintf(stderr, "%s: error: Doesn't recognize the header of the OMF/COFF file.\n", pThis->argv0); 984 rc = 1; 985 } 964 rc = kDepErr(pThis, 1, "Doesn't recognize the header of the OMF/COFF file."); 986 965 987 966 depFreeFileMemory(pbFile, pvOpaque); … … 990 969 991 970 992 static void kDebObjUsage(const char *a_argv0) 993 { 994 printf("usage: %s -o <output> -t <target> [-fqs] [-e <ignore-ext>] <OMF or COFF file>\n" 995 " or: %s --help\n" 996 " or: %s --version\n", 997 a_argv0, a_argv0, a_argv0); 971 static void kDebObjUsage(PKMKBUILTINCTX pCtx, int fIsErr) 972 { 973 kmk_builtin_ctx_printf(pCtx, fIsErr, 974 "usage: %s -o <output> -t <target> [-fqs] [-e <ignore-ext>] <OMF or COFF file>\n" 975 " or: %s --help\n" 976 " or: %s --version\n", 977 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName); 998 978 } 999 979 1000 980 1001 int kmk_builtin_kDepObj(int argc, char * argv[], char **envp)981 int kmk_builtin_kDepObj(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx) 1002 982 { 1003 983 int i; … … 1017 997 1018 998 /* Init instance data. */ 1019 This. argv0 = argv[0];999 This.pCtx = pCtx; 1020 1000 This.pszFile = NULL; 1021 1001 … … 1025 1005 if (argc <= 1) 1026 1006 { 1027 kDebObjUsage( argv[0]);1007 kDebObjUsage(pCtx, 0); 1028 1008 return 1; 1029 1009 } … … 1047 1027 else 1048 1028 { 1049 fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);1050 kDebObjUsage( argv[0]);1029 errx(pCtx, 2, "Invalid argument '%s'.", argv[i]); 1030 kDebObjUsage(pCtx, 1); 1051 1031 return 2; 1052 1032 } … … 1067 1047 pszValue = argv[i]; 1068 1048 else 1069 { 1070 fprintf(stderr, "%s: syntax error: The '-%c' option takes a value.\n", argv[0], chOpt); 1071 return 2; 1072 } 1049 return errx(pCtx, 2, "The '-%c' option takes a value.", chOpt); 1073 1050 break; 1074 1051 … … 1087 1064 { 1088 1065 if (pOutput) 1089 { 1090 fprintf(stderr, "%s: syntax error: only one output file!\n", argv[0]); 1091 return 2; 1092 } 1066 return errx(pCtx, 2, "only one output file!"); 1093 1067 pszOutput = pszValue; 1094 1068 if (pszOutput[0] == '-' && !pszOutput[1]) … … 1097 1071 pOutput = fopen(pszOutput, "w"); 1098 1072 if (!pOutput) 1099 { 1100 fprintf(stderr, "%s: error: Failed to create output file '%s'.\n", argv[0], pszOutput); 1101 return 1; 1102 } 1073 return err(pCtx, 1, "Failed to create output file '%s'", pszOutput); 1103 1074 break; 1104 1075 } … … 1110 1081 { 1111 1082 if (pszTarget) 1112 { 1113 fprintf(stderr, "%s: syntax error: only one target!\n", argv[0]); 1114 return 1; 1115 } 1083 return errx(pCtx, 2, "only one target!"); 1116 1084 pszTarget = pszValue; 1117 1085 break; … … 1151 1119 { 1152 1120 if (pszIgnoreExt) 1153 { 1154 fprintf(stderr, "%s: syntax error: The '-e' option can only be used once!\n", argv[0]); 1155 return 2; 1156 } 1121 return errx(pCtx, 2, "The '-e' option can only be used once!"); 1157 1122 pszIgnoreExt = pszValue; 1158 1123 break; … … 1163 1128 */ 1164 1129 case '?': 1165 kDebObjUsage( argv[0]);1130 kDebObjUsage(pCtx, 0); 1166 1131 return 0; 1167 1132 case 'V': … … 1173 1138 */ 1174 1139 default: 1175 fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);1176 kDebObjUsage( argv[0]);1140 errx(pCtx, 2, "Invalid argument '%s'.", argv[i]); 1141 kDebObjUsage(pCtx, 1); 1177 1142 return 2; 1178 1143 } … … 1182 1147 pInput = fopen(argv[i], "rb"); 1183 1148 if (!pInput) 1184 { 1185 fprintf(stderr, "%s: error: Failed to open input file '%s'.\n", argv[0], argv[i]); 1186 return 1; 1187 } 1149 return err(pCtx, 1, "Failed to open input file '%s'", argv[i]); 1188 1150 fInput = 1; 1189 1151 } … … 1195 1157 { 1196 1158 if (++i < argc) 1197 { 1198 fprintf(stderr, "%s: syntax error: No arguments shall follow the input spec.\n", argv[0]); 1199 return 1; 1200 } 1159 return errx(pCtx, 2, "No arguments shall follow the input spec."); 1201 1160 break; 1202 1161 } … … 1207 1166 */ 1208 1167 if (!pInput) 1209 { 1210 fprintf(stderr, "%s: syntax error: No input!\n", argv[0]); 1211 return 1; 1212 } 1168 return errx(pCtx, 2, "No input!"); 1213 1169 if (!pOutput) 1214 { 1215 fprintf(stderr, "%s: syntax error: No output!\n", argv[0]); 1216 return 1; 1217 } 1170 return errx(pCtx, 2, "No output!"); 1218 1171 if (!pszTarget) 1219 { 1220 fprintf(stderr, "%s: syntax error: No target!\n", argv[0]); 1221 return 1; 1222 } 1172 return errx(pCtx, 2, "No target!"); 1223 1173 1224 1174 /* … … 1245 1195 */ 1246 1196 if (!i && ferror(pOutput)) 1247 { 1248 i = 1; 1249 fprintf(stderr, "%s: error: Error writing to '%s'.\n", argv[0], pszOutput); 1250 } 1197 i = errx(pCtx, 1, "Error writing to '%s'", pszOutput); 1251 1198 fclose(pOutput); 1252 1199 if (i) 1253 1200 { 1254 1201 if (unlink(pszOutput)) 1255 fprintf(stderr, "%s: warning: failed to remove output file '%s' on failure.\n", argv[0], pszOutput);1202 warn(pCtx, "warning: failed to remove output file '%s' on failure.", pszOutput); 1256 1203 } 1257 1204 … … 1260 1207 } 1261 1208 1209 #ifdef KMK_BUILTIN_STANDALONE 1210 int main(int argc, char **argv, char **envp) 1211 { 1212 KMKBUILTINCTX Ctx = { "kDepObj", NULL }; 1213 return kmk_builtin_kDepObj(argc, argv, envp, &Ctx); 1214 } 1215 #endif 1216
Note:
See TracChangeset
for help on using the changeset viewer.