[2263] | 1 | /* $Id: kDepObj.c 3364 2020-06-08 19:29:42Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kDepObj - Extract dependency information from an object file.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[2413] | 7 | * Copyright (c) 2007-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
---|
[2263] | 8 | *
|
---|
| 9 | * This file is part of kBuild.
|
---|
| 10 | *
|
---|
| 11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 3 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * kBuild is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
---|
| 23 | *
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | /*******************************************************************************
|
---|
| 27 | * Header Files *
|
---|
| 28 | *******************************************************************************/
|
---|
[2759] | 29 | #define MSCFAKES_NO_WINDOWS_H
|
---|
[2263] | 30 | #include <stdio.h>
|
---|
| 31 | #include <stdlib.h>
|
---|
| 32 | #include <stddef.h>
|
---|
| 33 | #include <string.h>
|
---|
| 34 | #include <errno.h>
|
---|
| 35 | #include <ctype.h>
|
---|
[2278] | 36 | #include <stdarg.h>
|
---|
[2263] | 37 | #if !defined(_MSC_VER)
|
---|
| 38 | # include <unistd.h>
|
---|
| 39 | #else
|
---|
| 40 | # include <io.h>
|
---|
[3192] | 41 | typedef intptr_t ssize_t;
|
---|
[2263] | 42 | #endif
|
---|
[2856] | 43 | #include "k/kDefs.h"
|
---|
| 44 | #include "k/kTypes.h"
|
---|
| 45 | #include "k/kLdrFmts/pe.h"
|
---|
[3238] | 46 | #ifdef HAVE_CONFIG_H
|
---|
| 47 | # include "config.h"
|
---|
| 48 | #endif
|
---|
[2856] | 49 | #include "kDep.h"
|
---|
[3192] | 50 | #include "err.h"
|
---|
[2263] | 51 | #include "kmkbuiltin.h"
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | /*******************************************************************************
|
---|
| 55 | * Defined Constants And Macros *
|
---|
| 56 | *******************************************************************************/
|
---|
[2894] | 57 | #if 0
|
---|
[2263] | 58 | # define dprintf(a) printf a
|
---|
| 59 | # define dump(pb, cb, offBase) depHexDump(pb,cb,offBase)
|
---|
[3064] | 60 | # define WITH_DPRINTF
|
---|
[2263] | 61 | #else
|
---|
| 62 | # define dprintf(a) do {} while (0)
|
---|
| 63 | # define dump(pb, cb, offBase) do {} while (0)
|
---|
[3064] | 64 | # undef WITH_DPRINTF
|
---|
[2263] | 65 | #endif
|
---|
| 66 |
|
---|
| 67 | /** @name OMF defines
|
---|
| 68 | * @{ */
|
---|
| 69 | #define KDEPOMF_THEADR 0x80
|
---|
| 70 | #define KDEPOMF_LHEADR 0x82
|
---|
[2278] | 71 | #define KDEPOMF_COMENT 0x88
|
---|
| 72 | #define KDEPOMF_CMTCLS_DEPENDENCY 0xe9
|
---|
| 73 | #define KDEPOMF_CMTCLS_DBGTYPE 0xa1
|
---|
| 74 | #define KDEPOMF_LINNUM 0x94
|
---|
| 75 | #define KDEPOMF_LINNUM32 0x95
|
---|
[2263] | 76 | /** @} */
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | /*******************************************************************************
|
---|
| 80 | * Structures and Typedefs *
|
---|
| 81 | *******************************************************************************/
|
---|
[2267] | 82 | /** @name OMF Structures
|
---|
[2263] | 83 | * @{ */
|
---|
| 84 | #pragma pack(1)
|
---|
| 85 | /** OMF record header. */
|
---|
| 86 | typedef struct KDEPOMFHDR
|
---|
| 87 | {
|
---|
| 88 | /** The record type. */
|
---|
| 89 | KU8 bType;
|
---|
| 90 | /** The size of the record, excluding this header. */
|
---|
| 91 | KU16 cbRec;
|
---|
| 92 | } KDEPOMFHDR;
|
---|
| 93 | typedef KDEPOMFHDR *PKDEPOMFHDR;
|
---|
| 94 | typedef const KDEPOMFHDR *PCKDEPOMFHDR;
|
---|
| 95 |
|
---|
| 96 | /** OMF string. */
|
---|
| 97 | typedef struct KDEPOMFSTR
|
---|
| 98 | {
|
---|
| 99 | KU8 cch;
|
---|
| 100 | char ach[1];
|
---|
| 101 | } KDEPOMFSTR;
|
---|
| 102 | typedef KDEPOMFSTR *PKDEPOMFSTR;
|
---|
| 103 | typedef const KDEPOMFSTR *PCKDEPOMFSTR;
|
---|
| 104 |
|
---|
| 105 | /** THEADR/LHEADR. */
|
---|
| 106 | typedef struct KDEPOMFTHEADR
|
---|
| 107 | {
|
---|
| 108 | KDEPOMFHDR Hdr;
|
---|
| 109 | KDEPOMFSTR Name;
|
---|
| 110 | } KDEPOMFTHEADR;
|
---|
| 111 | typedef KDEPOMFTHEADR *PKDEPOMFTHEADR;
|
---|
| 112 | typedef const KDEPOMFTHEADR *PCKDEPOMFTHEADR;
|
---|
| 113 |
|
---|
| 114 | /** Dependency File. */
|
---|
| 115 | typedef struct KDEPOMFDEPFILE
|
---|
| 116 | {
|
---|
| 117 | KDEPOMFHDR Hdr;
|
---|
| 118 | KU8 fType;
|
---|
| 119 | KU8 bClass;
|
---|
| 120 | KU16 wDosTime;
|
---|
| 121 | KU16 wDosDate;
|
---|
| 122 | KDEPOMFSTR Name;
|
---|
| 123 | } KDEPOMFDEPFILE;
|
---|
| 124 | typedef KDEPOMFDEPFILE *PKDEPOMFDEPFILE;
|
---|
| 125 | typedef const KDEPOMFDEPFILE *PCKDEPOMFDEPFILE;
|
---|
| 126 |
|
---|
| 127 | #pragma pack()
|
---|
| 128 | /** @} */
|
---|
| 129 |
|
---|
| 130 |
|
---|
[2267] | 131 | /** @name COFF Structures
|
---|
| 132 | * @{ */
|
---|
| 133 | #pragma pack(1)
|
---|
| 134 |
|
---|
| 135 | typedef struct KDEPCVSYMHDR
|
---|
| 136 | {
|
---|
| 137 | /** The record size minus the size field. */
|
---|
| 138 | KU16 cb;
|
---|
| 139 | /** The record type. */
|
---|
| 140 | KU16 uType;
|
---|
| 141 | } KDEPCVSYMHDR;
|
---|
| 142 | typedef KDEPCVSYMHDR *PKDEPCVSYMHDR;
|
---|
| 143 | typedef const KDEPCVSYMHDR *PCKDEPCVSYMHDR;
|
---|
| 144 |
|
---|
| 145 | /** @name Selection of KDEPCVSYMHDR::uType values.
|
---|
| 146 | * @{ */
|
---|
| 147 | #define K_CV8_S_MSTOOL KU16_C(0x1116)
|
---|
| 148 | /** @} */
|
---|
| 149 |
|
---|
| 150 | typedef struct KDEPCV8SYMHDR
|
---|
| 151 | {
|
---|
| 152 | /** The record type. */
|
---|
| 153 | KU32 uType;
|
---|
| 154 | /** The record size minus the size field. */
|
---|
| 155 | KU32 cb;
|
---|
| 156 | } KDEPCV8SYMHDR;
|
---|
| 157 | typedef KDEPCV8SYMHDR *PKDEPCV8SYMHDR;
|
---|
| 158 | typedef const KDEPCV8SYMHDR *PCKDEPCV8SYMHDR;
|
---|
| 159 |
|
---|
| 160 | /** @name Known KDEPCV8SYMHDR::uType Values.
|
---|
| 161 | * @{ */
|
---|
| 162 | #define K_CV8_SYMBOL_INFO KU32_C(0x000000f1)
|
---|
| 163 | #define K_CV8_LINE_NUMBERS KU32_C(0x000000f2)
|
---|
| 164 | #define K_CV8_STRING_TABLE KU32_C(0x000000f3)
|
---|
| 165 | #define K_CV8_SOURCE_FILES KU32_C(0x000000f4)
|
---|
| 166 | #define K_CV8_COMDAT_XXXXX KU32_C(0x000000f5) /**< no idea about the format... */
|
---|
| 167 | /** @} */
|
---|
| 168 |
|
---|
| 169 | #pragma pack()
|
---|
| 170 | /** @} */
|
---|
| 171 |
|
---|
[3167] | 172 | /**
|
---|
| 173 | * Globals.
|
---|
| 174 | */
|
---|
| 175 | typedef struct KDEPOBJGLOBALS
|
---|
| 176 | {
|
---|
[3192] | 177 | /** The command execution context. */
|
---|
| 178 | PKMKBUILTINCTX pCtx;
|
---|
[3167] | 179 | /** Core instance. */
|
---|
| 180 | DEPGLOBALS Core;
|
---|
| 181 | /** The file. */
|
---|
| 182 | const char *pszFile;
|
---|
| 183 | } KDEPOBJGLOBALS;
|
---|
| 184 | /** Pointer to kDepObj globals. */
|
---|
| 185 | typedef KDEPOBJGLOBALS *PKDEPOBJGLOBALS;
|
---|
[2267] | 186 |
|
---|
[2263] | 187 |
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
[2278] | 190 | * Prints an error message.
|
---|
| 191 | *
|
---|
| 192 | * @returns rc.
|
---|
[3167] | 193 | * @param pThis kObjDep instance data.
|
---|
[2278] | 194 | * @param rc The return code, for making one line return statements.
|
---|
| 195 | * @param pszFormat The message format string.
|
---|
| 196 | * @param ... Format arguments.
|
---|
| 197 | * @todo Promote this to kDep.c.
|
---|
| 198 | */
|
---|
[3167] | 199 | static int kDepErr(PKDEPOBJGLOBALS pThis, int rc, const char *pszFormat, ...)
|
---|
[2278] | 200 | {
|
---|
[3192] | 201 | char szMsg[2048];
|
---|
[2278] | 202 | va_list va;
|
---|
[3192] | 203 | va_start(va, pszFormat);
|
---|
| 204 | vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, va);
|
---|
| 205 | va_end(va);
|
---|
| 206 | szMsg[sizeof(szMsg) - 1] = '\0';
|
---|
[2278] | 207 |
|
---|
[3167] | 208 | if (pThis->pszFile)
|
---|
[3192] | 209 | warnx(pThis->pCtx, "%s: error: %s", pThis->pszFile, szMsg);
|
---|
[2278] | 210 | else
|
---|
[3192] | 211 | errx(pThis->pCtx, rc, "%s", szMsg);
|
---|
[2278] | 212 | return rc;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | /**
|
---|
| 217 | * Gets an index from the data.
|
---|
| 218 | *
|
---|
| 219 | * @returns The index, KU16_MAX on buffer underflow.
|
---|
| 220 | * @param puData The current data stream position (in/out).
|
---|
| 221 | * @param pcbLeft Number of bytes left (in/out).
|
---|
| 222 | */
|
---|
| 223 | static KU16 kDepObjOMFGetIndex(KPCUINT *puData, KU16 *pcbLeft)
|
---|
| 224 | {
|
---|
| 225 | KU16 u16;
|
---|
| 226 |
|
---|
| 227 | if (*pcbLeft >= 1 && *pcbLeft != KU16_MAX)
|
---|
| 228 | {
|
---|
| 229 | *pcbLeft -= 1;
|
---|
| 230 | u16 = *puData->pb++;
|
---|
| 231 | if (u16 & KU16_C(0x80))
|
---|
| 232 | {
|
---|
| 233 | if (*pcbLeft >= 1)
|
---|
| 234 | {
|
---|
| 235 | *pcbLeft -= 1;
|
---|
| 236 | u16 = ((u16 & KU16_C(0x7f)) << 8) | *puData->pb++;
|
---|
| 237 | }
|
---|
| 238 | else
|
---|
| 239 | u16 = KU16_MAX;
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | else
|
---|
| 243 | u16 = KU16_MAX;
|
---|
| 244 | return u16;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | /**
|
---|
[2263] | 249 | * Parses the OMF file.
|
---|
| 250 | *
|
---|
[2278] | 251 | * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
|
---|
[3167] | 252 | * @param pThis The kDepObj instance data.
|
---|
[2263] | 253 | * @param pbFile The start of the file.
|
---|
| 254 | * @param cbFile The file size.
|
---|
| 255 | */
|
---|
[3167] | 256 | int kDepObjOMFParse(PKDEPOBJGLOBALS pThis, const KU8 *pbFile, KSIZE cbFile)
|
---|
[2263] | 257 | {
|
---|
[2278] | 258 | PCKDEPOMFHDR pHdr = (PCKDEPOMFHDR)pbFile;
|
---|
| 259 | KSIZE cbLeft = cbFile;
|
---|
| 260 | char uDbgType = 0; /* H or C */
|
---|
| 261 | KU8 uDbgVer = KU8_MAX;
|
---|
| 262 | KU32 iSrc = 0;
|
---|
| 263 | KU32 iMaybeSrc = 0;
|
---|
| 264 | KU8 uLinNumType = KU8_MAX;
|
---|
| 265 | KU16 cLinNums = 0;
|
---|
| 266 | KU32 cLinFiles = 0;
|
---|
| 267 | KU32 iLinFile = 0;
|
---|
[2263] | 268 |
|
---|
| 269 | /*
|
---|
| 270 | * Iterate thru the records until we hit the end or an invalid one.
|
---|
| 271 | */
|
---|
| 272 | while ( cbLeft >= sizeof(*pHdr)
|
---|
| 273 | && cbLeft >= pHdr->cbRec + sizeof(*pHdr))
|
---|
| 274 | {
|
---|
[2278] | 275 | KPCUINT uData;
|
---|
| 276 | uData.pv = pHdr + 1;
|
---|
| 277 |
|
---|
[2263] | 278 | /* process selected record types. */
|
---|
[2591] | 279 | dprintf(("%#07" KUPTR_PRI ": %#04x %#05x\n", (const KU8*)pHdr - pbFile, pHdr->bType, pHdr->cbRec));
|
---|
[2263] | 280 | switch (pHdr->bType)
|
---|
| 281 | {
|
---|
| 282 | /*
|
---|
| 283 | * The T/L Header contains the source name. When emitting CodeView 4
|
---|
| 284 | * and earlier (like masm and watcom does), all includes used by the
|
---|
| 285 | * line number tables have their own THEADR record.
|
---|
| 286 | */
|
---|
| 287 | case KDEPOMF_THEADR:
|
---|
| 288 | case KDEPOMF_LHEADR:
|
---|
| 289 | {
|
---|
| 290 | PCKDEPOMFTHEADR pTHeadr = (PCKDEPOMFTHEADR)pHdr;
|
---|
| 291 | if (1 + pTHeadr->Name.cch + 1 != pHdr->cbRec)
|
---|
[3192] | 292 | return kDepErr(pThis, 1, "%#07x - Bad %cHEADR record, length mismatch.",
|
---|
[2278] | 293 | (const KU8*)pHdr - pbFile, pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L');
|
---|
| 294 | if ( ( pTHeadr->Name.cch > 2
|
---|
| 295 | && pTHeadr->Name.ach[pTHeadr->Name.cch - 2] == '.'
|
---|
| 296 | && ( pTHeadr->Name.ach[pTHeadr->Name.cch - 1] == 'o'
|
---|
| 297 | || pTHeadr->Name.ach[pTHeadr->Name.cch - 1] == 'O'))
|
---|
| 298 | || ( pTHeadr->Name.cch > 4
|
---|
| 299 | && pTHeadr->Name.ach[pTHeadr->Name.cch - 4] == '.'
|
---|
| 300 | && ( pTHeadr->Name.ach[pTHeadr->Name.cch - 3] == 'o'
|
---|
| 301 | || pTHeadr->Name.ach[pTHeadr->Name.cch - 3] == 'O')
|
---|
| 302 | && ( pTHeadr->Name.ach[pTHeadr->Name.cch - 2] == 'b'
|
---|
| 303 | || pTHeadr->Name.ach[pTHeadr->Name.cch - 2] == 'B')
|
---|
| 304 | && ( pTHeadr->Name.ach[pTHeadr->Name.cch - 1] == 'j'
|
---|
| 305 | || pTHeadr->Name.ach[pTHeadr->Name.cch - 1] == 'J'))
|
---|
| 306 | )
|
---|
| 307 | dprintf(("%cHEADR: %.*s [ignored]\n", pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L', pTHeadr->Name.cch, pTHeadr->Name.ach));
|
---|
| 308 | else
|
---|
[2263] | 309 | {
|
---|
[2278] | 310 | dprintf(("%cHEADR: %.*s\n", pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L', pTHeadr->Name.cch, pTHeadr->Name.ach));
|
---|
[3167] | 311 | depAdd(&pThis->Core, pTHeadr->Name.ach, pTHeadr->Name.cch);
|
---|
[2278] | 312 | iMaybeSrc++;
|
---|
[2263] | 313 | }
|
---|
[2278] | 314 | uLinNumType = KU8_MAX;
|
---|
[2263] | 315 | break;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[2278] | 318 | case KDEPOMF_COMENT:
|
---|
| 319 | {
|
---|
| 320 | KU8 uClass;
|
---|
| 321 |
|
---|
| 322 | if (pHdr->cbRec < 2 + 1)
|
---|
[3192] | 323 | return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, too small.", (const KU8*)pHdr - pbFile);
|
---|
[2278] | 324 | if (uData.pb[0] & 0x3f)
|
---|
[3192] | 325 | return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, reserved flags set.", (const KU8*)pHdr - pbFile);
|
---|
[2278] | 326 | uClass = uData.pb[1];
|
---|
| 327 | uData.pb += 2;
|
---|
| 328 | switch (uClass)
|
---|
[2263] | 329 | {
|
---|
| 330 | /*
|
---|
| 331 | * Borland dependency file comment (famously used by wmake and Watcom).
|
---|
| 332 | */
|
---|
| 333 | case KDEPOMF_CMTCLS_DEPENDENCY:
|
---|
| 334 | {
|
---|
| 335 | PCKDEPOMFDEPFILE pDep = (PCKDEPOMFDEPFILE)pHdr;
|
---|
| 336 | if (K_OFFSETOF(KDEPOMFDEPFILE, Name.ach[pDep->Name.cch]) + 1 != pHdr->cbRec + sizeof(*pHdr))
|
---|
| 337 | {
|
---|
| 338 | /* Empty record indicates the end of the dependency files,
|
---|
| 339 | no need to go on. */
|
---|
| 340 | if (pHdr->cbRec == 2 + 1)
|
---|
| 341 | return 0;
|
---|
[3192] | 342 | return kDepErr(pThis, 1, "%#07lx - Bad DEPENDENCY FILE record, length mismatch. (%u/%u)",
|
---|
[2278] | 343 | (long)((const KU8 *)pHdr - pbFile),
|
---|
| 344 | K_OFFSETOF(KDEPOMFDEPFILE, Name.ach[pDep->Name.cch]) + 1,
|
---|
| 345 | (unsigned)(pHdr->cbRec + sizeof(*pHdr)));
|
---|
[2263] | 346 | }
|
---|
[3167] | 347 | depAdd(&pThis->Core, pDep->Name.ach, pDep->Name.cch);
|
---|
[2278] | 348 | iSrc++;
|
---|
[2263] | 349 | break;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
[2278] | 352 | /*
|
---|
| 353 | * Pick up the debug type so we can parse the LINNUM records.
|
---|
| 354 | */
|
---|
| 355 | case KDEPOMF_CMTCLS_DBGTYPE:
|
---|
| 356 | if (pHdr->cbRec < 2 + 3 + 1)
|
---|
| 357 | break; /* ignore, Borland used this for something else apparently. */
|
---|
| 358 | if ( !(uData.pb[1] == 'C' && uData.pb[2] == 'V')
|
---|
| 359 | && !(uData.pb[1] == 'H' && uData.pb[2] == 'L'))
|
---|
| 360 | {
|
---|
| 361 | dprintf(("Unknown debug type: %c%c (%u)\n", uData.pb[1], uData.pb[2], uData.pb[0]));
|
---|
| 362 | break;
|
---|
| 363 | }
|
---|
| 364 | uDbgType = uData.pb[1];
|
---|
| 365 | uDbgVer = uData.pb[0];
|
---|
| 366 | dprintf(("Debug Type %s ver %u\n", uDbgType == 'H' ? "HLL" : "CodeView", uDbgVer));
|
---|
| 367 | break;
|
---|
| 368 |
|
---|
[2263] | 369 | }
|
---|
[2278] | 370 | break; /* COMENT */
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | /*
|
---|
| 374 | * LINNUM + THEADR == sigar.
|
---|
| 375 | */
|
---|
| 376 | case KDEPOMF_LINNUM:
|
---|
| 377 | if (uDbgType == 'C')
|
---|
| 378 | iMaybeSrc |= KU32_C(0x80000000);
|
---|
| 379 | dprintf(("LINNUM:\n"));
|
---|
[2263] | 380 | break;
|
---|
[2278] | 381 |
|
---|
| 382 | /*
|
---|
| 383 | * The HLL v4 and v6 file names table will include all files when present, which
|
---|
| 384 | * is perfect for generating dependencies.
|
---|
| 385 | */
|
---|
| 386 | case KDEPOMF_LINNUM32:
|
---|
| 387 | if ( uDbgType == 'H'
|
---|
| 388 | && uDbgVer >= 3
|
---|
| 389 | && uDbgVer <= 6)
|
---|
| 390 | {
|
---|
| 391 | /* skip two indexes (group & segment) */
|
---|
| 392 | KU16 cbRecLeft = pHdr->cbRec - 1;
|
---|
| 393 | KU16 uGrp = kDepObjOMFGetIndex(&uData, &cbRecLeft);
|
---|
| 394 | KU16 uSeg = kDepObjOMFGetIndex(&uData, &cbRecLeft);
|
---|
| 395 | if (uSeg == KU16_MAX)
|
---|
[3192] | 396 | return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record", (long)((const KU8 *)pHdr - pbFile));
|
---|
[3064] | 397 | K_NOREF(uGrp);
|
---|
[2278] | 398 |
|
---|
| 399 | if (uLinNumType == KU8_MAX)
|
---|
| 400 | {
|
---|
[3064] | 401 | #ifdef WITH_DPRINTF
|
---|
[2278] | 402 | static const char * const s_apsz[5] =
|
---|
| 403 | {
|
---|
| 404 | "source file", "listing file", "source & listing file", "file names table", "path table"
|
---|
| 405 | };
|
---|
[3064] | 406 | #endif
|
---|
[2278] | 407 | KU16 uLine;
|
---|
| 408 | KU8 uReserved;
|
---|
| 409 | KU16 uSeg2;
|
---|
| 410 | KU32 cbLinNames;
|
---|
| 411 |
|
---|
| 412 | if (cbRecLeft < 2+1+1+2+2+4)
|
---|
[3192] | 413 | return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too short", (long)((const KU8 *)pHdr - pbFile));
|
---|
[2278] | 414 | cbRecLeft -= 2+1+1+2+2+4;
|
---|
| 415 | uLine = *uData.pu16++;
|
---|
| 416 | uLinNumType = *uData.pu8++;
|
---|
[3064] | 417 | uReserved = *uData.pu8++; K_NOREF(uReserved);
|
---|
| 418 | cLinNums = *uData.pu16++; K_NOREF(cLinNums);
|
---|
| 419 | uSeg2 = *uData.pu16++; K_NOREF(uSeg2);
|
---|
| 420 | cbLinNames = *uData.pu32++; K_NOREF(cbLinNames);
|
---|
[2278] | 421 |
|
---|
| 422 | dprintf(("LINNUM32: uGrp=%#x uSeg=%#x uSeg2=%#x uLine=%#x (MBZ) uReserved=%#x\n",
|
---|
| 423 | uGrp, uSeg, uSeg2, uLine, uReserved));
|
---|
| 424 | dprintf(("LINNUM32: cLinNums=%#x (%u) cbLinNames=%#x (%u) uLinNumType=%#x (%s)\n",
|
---|
| 425 | cLinNums, cLinNums, cbLinNames, cbLinNames, uLinNumType,
|
---|
| 426 | uLinNumType < K_ELEMENTS(s_apsz) ? s_apsz[uLinNumType] : "??"));
|
---|
[3064] | 427 |
|
---|
[2278] | 428 | if (uLine != 0)
|
---|
[3192] | 429 | return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, line %#x (MBZ)", (long)((const KU8 *)pHdr - pbFile), uLine);
|
---|
[2278] | 430 | cLinFiles = iLinFile = KU32_MAX;
|
---|
| 431 | if ( uLinNumType == 3 /* file names table */
|
---|
| 432 | || uLinNumType == 4 /* path table */)
|
---|
| 433 | cLinNums = 0; /* no line numbers */
|
---|
| 434 | else if (uLinNumType > 4)
|
---|
[3192] | 435 | return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, type %#x unknown", (long)((const KU8 *)pHdr - pbFile), uLinNumType);
|
---|
[2278] | 436 | }
|
---|
| 437 | else
|
---|
| 438 | dprintf(("LINNUM32: uGrp=%#x uSeg=%#x\n", uGrp, uSeg));
|
---|
| 439 |
|
---|
| 440 |
|
---|
| 441 | /* Skip file numbers (we parse them to follow the stream correctly). */
|
---|
| 442 | if (uLinNumType != 3 && uLinNumType != 4)
|
---|
| 443 | {
|
---|
[3364] | 444 | static const KU16 s_acbTypes[3] = { 2+2+4, 4+4+4, 2+2+4+4+4 };
|
---|
| 445 | KU16 cbEntry = s_acbTypes[uLinNumType];
|
---|
[2278] | 446 |
|
---|
| 447 | while (cLinNums && cbRecLeft)
|
---|
| 448 | {
|
---|
| 449 | if (cbRecLeft < cbEntry)
|
---|
[3192] | 450 | return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete line entry", (long)((const KU8 *)pHdr - pbFile));
|
---|
[2278] | 451 |
|
---|
| 452 | switch (uLinNumType)
|
---|
| 453 | {
|
---|
| 454 | case 0: /* source file */
|
---|
| 455 | dprintf((" Line %6" KU16_PRI " of file %2" KU16_PRI " at %#010" KX32_PRI "\n",
|
---|
| 456 | uData.pu16[0], uData.pu16[1], uData.pu32[1]));
|
---|
| 457 | break;
|
---|
| 458 | case 1: /* listing file */
|
---|
| 459 | dprintf((" Line %6" KU32_PRI ", statement %6" KU32_PRI " at %#010" KX32_PRI "\n",
|
---|
| 460 | uData.pu32[0], uData.pu32[1], uData.pu32[2]));
|
---|
| 461 | break;
|
---|
| 462 | case 2: /* source & listing file */
|
---|
| 463 | dprintf((" Line %6" KU16_PRI " of file %2" KU16_PRI ", listning line %6" KU32_PRI ", statement %6" KU32_PRI " at %#010" KX32_PRI "\n",
|
---|
| 464 | uData.pu16[0], uData.pu16[1], uData.pu32[1], uData.pu32[2], uData.pu32[3]));
|
---|
| 465 | break;
|
---|
| 466 | }
|
---|
| 467 | uData.pb += cbEntry;
|
---|
| 468 | cbRecLeft -= cbEntry;
|
---|
| 469 | cLinNums--;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | /* If at end of the announced line number entiries, we may find a file names table
|
---|
| 473 | here (who is actually emitting this?). */
|
---|
| 474 | if (!cLinNums)
|
---|
| 475 | {
|
---|
| 476 | uLinNumType = cbRecLeft > 0 ? 3 : KU8_MAX;
|
---|
| 477 | dprintf(("End-of-line-numbers; uLinNumType=%u cbRecLeft=%#x\n", uLinNumType, cbRecLeft));
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | if (uLinNumType == 3 || uLinNumType == 4)
|
---|
| 482 | {
|
---|
| 483 | /* Read the file/path table header (first time only). */
|
---|
| 484 | if (cLinFiles == KU32_MAX && iLinFile == KU32_MAX)
|
---|
| 485 | {
|
---|
| 486 | KU32 iFirstCol;
|
---|
| 487 | KU32 cCols;
|
---|
| 488 |
|
---|
| 489 | if (cbRecLeft < 4+4+4)
|
---|
[3192] | 490 | return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete file/path table header", (long)((const KU8 *)pHdr - pbFile));
|
---|
[2278] | 491 | cbRecLeft -= 4+4+4;
|
---|
| 492 |
|
---|
[3064] | 493 | iFirstCol = *uData.pu32++; K_NOREF(iFirstCol);
|
---|
| 494 | cCols = *uData.pu32++; K_NOREF(cCols);
|
---|
[2278] | 495 | cLinFiles = *uData.pu32++;
|
---|
| 496 | dprintf(("%s table header: cLinFiles=%#" KX32_PRI " (%" KU32_PRI ") iFirstCol=%" KU32_PRI " cCols=%" KU32_PRI"\n",
|
---|
| 497 | uLinNumType == 3 ? "file names" : "path", cLinFiles, cLinFiles, iFirstCol, cCols));
|
---|
| 498 | if (cLinFiles == KU32_MAX)
|
---|
[3192] | 499 | return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too many file/path table entries.", (long)((const KU8 *)pHdr - pbFile));
|
---|
[2278] | 500 | iLinFile = 0;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | /* Parse the file names / path table. */
|
---|
| 504 | while (iLinFile < cLinFiles && cbRecLeft)
|
---|
| 505 | {
|
---|
[3364] | 506 | KU16 cbName = *uData.pb++;
|
---|
[2278] | 507 | if (cbRecLeft < 1 + cbName)
|
---|
[3192] | 508 | return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, file/path table entry too long.", (long)((const KU8 *)pHdr - pbFile));
|
---|
[2278] | 509 | iLinFile++;
|
---|
| 510 | dprintf(("#%" KU32_PRI": %.*s\n", iLinFile, cbName, uData.pch));
|
---|
| 511 | if (uLinNumType == 3)
|
---|
| 512 | {
|
---|
[3167] | 513 | depAdd(&pThis->Core, uData.pch, cbName);
|
---|
[2278] | 514 | iSrc++;
|
---|
| 515 | }
|
---|
| 516 | cbRecLeft -= 1 + cbName;
|
---|
| 517 | uData.pb += cbName;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | /* The end? */
|
---|
| 521 | if (iLinFile == cLinFiles)
|
---|
| 522 | {
|
---|
| 523 | uLinNumType = KU8_MAX;
|
---|
| 524 | dprintf(("End-of-file/path-table; cbRecLeft=%#x\n", cbRecLeft));
|
---|
| 525 | }
|
---|
| 526 | }
|
---|
| 527 | }
|
---|
| 528 | else
|
---|
| 529 | dprintf(("LINNUM32: Unknown or unsupported format\n"));
|
---|
| 530 | break;
|
---|
| 531 |
|
---|
[2263] | 532 | }
|
---|
| 533 |
|
---|
| 534 | /* advance */
|
---|
| 535 | cbLeft -= pHdr->cbRec + sizeof(*pHdr);
|
---|
| 536 | pHdr = (PCKDEPOMFHDR)((const KU8 *)(pHdr + 1) + pHdr->cbRec);
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | if (cbLeft)
|
---|
[3192] | 540 | return kDepErr(pThis, 1, "%#07x - Unexpected EOF. cbLeft=%#x", (const KU8*)pHdr - pbFile, cbLeft);
|
---|
[2278] | 541 |
|
---|
| 542 | if (iSrc == 0 && iMaybeSrc <= 1)
|
---|
[2263] | 543 | {
|
---|
[2278] | 544 | dprintf(("kDepObjOMFParse: No cylindrical smoking thing: iSrc=0 iMaybeSrc=%#" KX32_PRI"\n", iMaybeSrc));
|
---|
| 545 | return 2;
|
---|
[2263] | 546 | }
|
---|
[2278] | 547 | dprintf(("kDepObjOMFParse: iSrc=%" KU32_PRI " iMaybeSrc=%#" KX32_PRI "\n", iSrc, iMaybeSrc));
|
---|
[2263] | 548 | return 0;
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 |
|
---|
| 552 | /**
|
---|
| 553 | * Checks if this file is an OMF file or not.
|
---|
| 554 | *
|
---|
| 555 | * @returns K_TRUE if it's OMF, K_FALSE otherwise.
|
---|
| 556 | *
|
---|
| 557 | * @param pb The start of the file.
|
---|
| 558 | * @param cb The file size.
|
---|
| 559 | */
|
---|
| 560 | KBOOL kDepObjOMFTest(const KU8 *pbFile, KSIZE cbFile)
|
---|
| 561 | {
|
---|
| 562 | PCKDEPOMFTHEADR pHdr = (PCKDEPOMFTHEADR)pbFile;
|
---|
| 563 |
|
---|
[2267] | 564 | if (cbFile <= sizeof(*pHdr))
|
---|
[2263] | 565 | return K_FALSE;
|
---|
| 566 | if ( pHdr->Hdr.bType != KDEPOMF_THEADR
|
---|
| 567 | && pHdr->Hdr.bType != KDEPOMF_LHEADR)
|
---|
| 568 | return K_FALSE;
|
---|
| 569 | if (pHdr->Hdr.cbRec + sizeof(pHdr->Hdr) >= cbFile)
|
---|
| 570 | return K_FALSE;
|
---|
| 571 | if (pHdr->Hdr.cbRec != 1 + pHdr->Name.cch + 1)
|
---|
| 572 | return K_FALSE;
|
---|
| 573 |
|
---|
| 574 | return K_TRUE;
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 |
|
---|
| 578 | /**
|
---|
[2267] | 579 | * Parses a CodeView 8 symbol section.
|
---|
| 580 | *
|
---|
[2278] | 581 | * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
|
---|
[3167] | 582 | * @param pThis The kDepObj instance data.
|
---|
[2267] | 583 | * @param pbSyms Pointer to the start of the symbol section.
|
---|
| 584 | * @param cbSyms Size of the symbol section.
|
---|
| 585 | */
|
---|
[3167] | 586 | int kDepObjCOFFParseCV8SymbolSection(PKDEPOBJGLOBALS pThis, const KU8 *pbSyms, KU32 cbSyms)
|
---|
[2267] | 587 | {
|
---|
| 588 | char const * pchStrTab = NULL;
|
---|
| 589 | KU32 cbStrTab = 0;
|
---|
| 590 | KPCUINT uSrcFiles = {0};
|
---|
| 591 | KU32 cbSrcFiles = 0;
|
---|
| 592 | KU32 off = 4;
|
---|
| 593 | KU32 iSrc = 0;
|
---|
| 594 |
|
---|
| 595 | if (cbSyms < 16)
|
---|
| 596 | return 1;
|
---|
| 597 |
|
---|
| 598 | /*
|
---|
| 599 | * The parsing loop.
|
---|
| 600 | */
|
---|
| 601 | while (off < cbSyms)
|
---|
| 602 | {
|
---|
| 603 | PCKDEPCV8SYMHDR pHdr = (PCKDEPCV8SYMHDR)(pbSyms + off);
|
---|
| 604 | KPCUINT uData;
|
---|
| 605 | KU32 cbData;
|
---|
| 606 | uData.pv = pHdr + 1;
|
---|
| 607 |
|
---|
| 608 | if (off + sizeof(*pHdr) >= cbSyms)
|
---|
| 609 | {
|
---|
[3192] | 610 | kDepErr(pThis, 1, "CV symbol table entry at %08" KX32_PRI " is too long; cbSyms=%#" KX32_PRI "",
|
---|
| 611 | off, cbSyms);
|
---|
[2267] | 612 | return 1; /* FIXME */
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | cbData = pHdr->cb;
|
---|
| 616 | if (off + cbData + sizeof(*pHdr) > cbSyms)
|
---|
| 617 | {
|
---|
[3192] | 618 | kDepErr(pThis, 1, "CV symbol table entry at %08" KX32_PRI " is too long; cbData=%#" KX32_PRI " cbSyms=%#" KX32_PRI,
|
---|
| 619 | off, cbData, cbSyms);
|
---|
[2267] | 620 | return 1; /* FIXME */
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | /* If the size is 0, assume it covers the rest of the section. VC++ 2003 has
|
---|
| 624 | been observed doing thing. */
|
---|
| 625 | if (!cbData)
|
---|
| 626 | cbData = cbSyms - off;
|
---|
| 627 |
|
---|
| 628 | switch (pHdr->uType)
|
---|
| 629 | {
|
---|
| 630 | case K_CV8_SYMBOL_INFO:
|
---|
| 631 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Symbol Info\n", off, cbData));
|
---|
| 632 | /*dump(uData.pb, cbData, 0);*/
|
---|
| 633 | break;
|
---|
| 634 |
|
---|
| 635 | case K_CV8_LINE_NUMBERS:
|
---|
| 636 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Line numbers\n", off, cbData));
|
---|
| 637 | /*dump(uData.pb, cbData, 0);*/
|
---|
| 638 | break;
|
---|
| 639 |
|
---|
| 640 | case K_CV8_STRING_TABLE:
|
---|
| 641 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": String table\n", off, cbData));
|
---|
| 642 | if (pchStrTab)
|
---|
[3192] | 643 | warnx(pThis->pCtx, "%s: warning: Found yet another string table!", pThis->pszFile);
|
---|
[2267] | 644 | pchStrTab = uData.pch;
|
---|
| 645 | cbStrTab = cbData;
|
---|
| 646 | /*dump(uData.pb, cbData, 0);*/
|
---|
| 647 | break;
|
---|
| 648 |
|
---|
| 649 | case K_CV8_SOURCE_FILES:
|
---|
| 650 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Source files\n", off, cbData));
|
---|
| 651 | if (uSrcFiles.pb)
|
---|
[3192] | 652 | warnx(pThis->pCtx, "%s: warning: Found yet another source files table!", pThis->pszFile);
|
---|
[2267] | 653 | uSrcFiles = uData;
|
---|
| 654 | cbSrcFiles = cbData;
|
---|
| 655 | /*dump(uData.pb, cbData, 0);*/
|
---|
| 656 | break;
|
---|
| 657 |
|
---|
| 658 | case K_CV8_COMDAT_XXXXX:
|
---|
| 659 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": 0xf5 Unknown COMDAT stuff\n", off, cbData));
|
---|
| 660 | /*dump(uData.pb, cbData, 0);*/
|
---|
| 661 | break;
|
---|
| 662 |
|
---|
| 663 | default:
|
---|
| 664 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Unknown type %#" KX32_PRI "\n",
|
---|
| 665 | off, cbData, pHdr->uType));
|
---|
| 666 | dump(uData.pb, cbData, 0);
|
---|
| 667 | break;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | /* next */
|
---|
| 671 | cbData = (cbData + 3) & ~KU32_C(3);
|
---|
| 672 | off += cbData + sizeof(*pHdr);
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | /*
|
---|
| 676 | * Did we find any source files and strings?
|
---|
| 677 | */
|
---|
| 678 | if (!pchStrTab || !uSrcFiles.pv)
|
---|
| 679 | {
|
---|
[2278] | 680 | dprintf(("kDepObjCOFFParseCV8SymbolSection: No cylindrical smoking thing: pchStrTab=%p uSrcFiles.pv=%p\n", pchStrTab, uSrcFiles.pv));
|
---|
[2267] | 681 | return 2;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | /*
|
---|
| 685 | * Iterate the source file table.
|
---|
| 686 | */
|
---|
| 687 | off = 0;
|
---|
| 688 | while (off < cbSrcFiles)
|
---|
| 689 | {
|
---|
| 690 | KU32 offFile;
|
---|
| 691 | const char *pszFile;
|
---|
| 692 | KSIZE cchFile;
|
---|
| 693 | KU16 u16Type;
|
---|
| 694 | KPCUINT uSrc;
|
---|
| 695 | KU32 cbSrc;
|
---|
| 696 |
|
---|
| 697 | /*
|
---|
| 698 | * Validate and parse the entry (variable length record are fun).
|
---|
| 699 | */
|
---|
| 700 | if (off + 8 > cbSrcFiles)
|
---|
[3192] | 701 | return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " is too long; cbSrcFiles=%#" KX32_PRI,
|
---|
| 702 | off, cbSrcFiles);
|
---|
[2267] | 703 | uSrc.pb = uSrcFiles.pb + off;
|
---|
| 704 | u16Type = uSrc.pu16[2];
|
---|
[3324] | 705 | switch (u16Type)
|
---|
| 706 | {
|
---|
| 707 | case 0x0110: cbSrc = 6 + 16 + 2; break; /* MD5 */
|
---|
| 708 | case 0x0214: cbSrc = 6 + 20 + 2; break; /* SHA1 */ /** @todo check this */
|
---|
| 709 | case 0x0320: cbSrc = 6 + 32 + 2; break; /* SHA-256 */
|
---|
| 710 | default: cbSrc = 6 + 0 + 2; break;
|
---|
| 711 | }
|
---|
[2267] | 712 | if (off + cbSrc > cbSrcFiles)
|
---|
[3192] | 713 | return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " is too long; cbSrc=%#" KX32_PRI " cbSrcFiles=%#" KX32_PRI,
|
---|
| 714 | off, cbSrc, cbSrcFiles);
|
---|
[2267] | 715 |
|
---|
| 716 | offFile = *uSrc.pu32;
|
---|
| 717 | if (offFile > cbStrTab)
|
---|
[3192] | 718 | return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " is out side the string table; offFile=%#" KX32_PRI " cbStrTab=%#" KX32_PRI,
|
---|
| 719 | off, offFile, cbStrTab);
|
---|
[2267] | 720 | pszFile = pchStrTab + offFile;
|
---|
| 721 | cchFile = strlen(pszFile);
|
---|
| 722 | if (cchFile == 0)
|
---|
[3192] | 723 | return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " has an empty file name; offFile=%#x" KX32_PRI,
|
---|
| 724 | off, offFile);
|
---|
[2267] | 725 |
|
---|
| 726 | /*
|
---|
| 727 | * Display the result and add it to the dependency database.
|
---|
| 728 | */
|
---|
[3167] | 729 | depAdd(&pThis->Core, pszFile, cchFile);
|
---|
[3324] | 730 | #ifdef WITH_DPRINTF
|
---|
| 731 | dprintf(("#%03" KU32_PRI ": ", iSrc));
|
---|
| 732 | {
|
---|
| 733 | KU32 off = 6;
|
---|
| 734 | for (;off < cbSrc - 2; off++)
|
---|
| 735 | dprintf(("%02" KX8_PRI, uSrc.pb[off]));
|
---|
| 736 | if (cbSrc == 6)
|
---|
| 737 | dprintf(("type=%#06" KX16_PRI, u16Type));
|
---|
| 738 | dprintf((" '%s'\n", pszFile));
|
---|
| 739 | }
|
---|
| 740 | #endif
|
---|
[2267] | 741 |
|
---|
| 742 |
|
---|
| 743 | /* next */
|
---|
| 744 | iSrc++;
|
---|
| 745 | off += cbSrc;
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 | if (iSrc == 0)
|
---|
| 749 | {
|
---|
[2278] | 750 | dprintf(("kDepObjCOFFParseCV8SymbolSection: No cylindrical smoking thing: iSrc=0\n"));
|
---|
[2267] | 751 | return 2;
|
---|
| 752 | }
|
---|
[2278] | 753 | dprintf(("kDepObjCOFFParseCV8SymbolSection: iSrc=%" KU32_PRI "\n", iSrc));
|
---|
[2267] | 754 | return 0;
|
---|
| 755 | }
|
---|
| 756 |
|
---|
| 757 |
|
---|
| 758 | /**
|
---|
| 759 | * Parses the OMF file.
|
---|
| 760 | *
|
---|
| 761 | * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
|
---|
[3167] | 762 | * @param pThis The kDepObj instance data.
|
---|
[2267] | 763 | * @param pbFile The start of the file.
|
---|
| 764 | * @param cbFile The file size.
|
---|
| 765 | */
|
---|
[3167] | 766 | int kDepObjCOFFParse(PKDEPOBJGLOBALS pThis, const KU8 *pbFile, KSIZE cbFile)
|
---|
[2267] | 767 | {
|
---|
[2896] | 768 | IMAGE_FILE_HEADER const *pFileHdr = (IMAGE_FILE_HEADER const *)pbFile;
|
---|
| 769 | ANON_OBJECT_HEADER_BIGOBJ const *pBigObjHdr = (ANON_OBJECT_HEADER_BIGOBJ const *)pbFile;
|
---|
| 770 | IMAGE_SECTION_HEADER const *paSHdrs;
|
---|
| 771 | KU32 cSHdrs;
|
---|
| 772 | unsigned iSHdr;
|
---|
| 773 | KPCUINT u;
|
---|
[3341] | 774 | KBOOL fDebugS = K_FALSE;
|
---|
[2896] | 775 | int rcRet = 2;
|
---|
| 776 | int rc;
|
---|
[2267] | 777 |
|
---|
[2896] | 778 | if ( pBigObjHdr->Sig1 == 0
|
---|
| 779 | && pBigObjHdr->Sig2 == KU16_MAX)
|
---|
| 780 | {
|
---|
| 781 | paSHdrs = (IMAGE_SECTION_HEADER const *)(pBigObjHdr + 1);
|
---|
| 782 | cSHdrs = pBigObjHdr->NumberOfSections;
|
---|
| 783 | }
|
---|
| 784 | else
|
---|
| 785 | {
|
---|
| 786 | paSHdrs = (IMAGE_SECTION_HEADER const *)((KU8 const *)(pFileHdr + 1) + pFileHdr->SizeOfOptionalHeader);
|
---|
| 787 | cSHdrs = pFileHdr->NumberOfSections;
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 |
|
---|
[2894] | 791 | dprintf(("COFF file!\n"));
|
---|
[2267] | 792 |
|
---|
| 793 | for (iSHdr = 0; iSHdr < cSHdrs; iSHdr++)
|
---|
| 794 | {
|
---|
| 795 | if ( !memcmp(paSHdrs[iSHdr].Name, ".debug$S", sizeof(".debug$S") - 1)
|
---|
| 796 | && paSHdrs[iSHdr].SizeOfRawData > 4)
|
---|
| 797 | {
|
---|
| 798 | u.pb = pbFile + paSHdrs[iSHdr].PointerToRawData;
|
---|
| 799 | dprintf(("CV symbol table: version=%x\n", *u.pu32));
|
---|
| 800 | if (*u.pu32 == 0x000000004)
|
---|
[3167] | 801 | rc = kDepObjCOFFParseCV8SymbolSection(pThis, u.pb, paSHdrs[iSHdr].SizeOfRawData);
|
---|
[2267] | 802 | else
|
---|
| 803 | rc = 2;
|
---|
| 804 | dprintf(("rc=%d\n", rc));
|
---|
| 805 | if (rcRet == 2)
|
---|
| 806 | rcRet = rc;
|
---|
| 807 | if (rcRet != 2)
|
---|
| 808 | return rc;
|
---|
[3341] | 809 | fDebugS = K_TRUE;
|
---|
[2267] | 810 | }
|
---|
[2894] | 811 | dprintf(("#%d: %.8s\n", iSHdr, paSHdrs[iSHdr].Name));
|
---|
[2267] | 812 | }
|
---|
[3341] | 813 |
|
---|
| 814 | /* If we found no dependencies but did find a .debug$S section, check if
|
---|
| 815 | this is a case where the compile didn't emit any because there is no
|
---|
| 816 | code in this compilation unit. */
|
---|
| 817 | if (rcRet == 2)
|
---|
| 818 | {
|
---|
| 819 | if (fDebugS)
|
---|
| 820 | {
|
---|
| 821 | for (iSHdr = 0; iSHdr < cSHdrs; iSHdr++)
|
---|
| 822 | if (!memcmp(paSHdrs[iSHdr].Name, ".text", sizeof(".text") - 1))
|
---|
| 823 | return kDepErr(pThis, 2, "%s: no dependencies (has text).", pThis->pszFile);
|
---|
| 824 | warnx(pThis->pCtx, "%s: no dependencies, but also no text, so probably (mostly) harmless.", pThis->pszFile);
|
---|
| 825 | return 0;
|
---|
| 826 | }
|
---|
| 827 | kDepErr(pThis, 2, "%s: no dependencies.", pThis->pszFile);
|
---|
| 828 | }
|
---|
| 829 |
|
---|
[2267] | 830 | return rcRet;
|
---|
| 831 | }
|
---|
| 832 |
|
---|
| 833 |
|
---|
| 834 | /**
|
---|
| 835 | * Checks if this file is a COFF file or not.
|
---|
| 836 | *
|
---|
| 837 | * @returns K_TRUE if it's COFF, K_FALSE otherwise.
|
---|
| 838 | *
|
---|
[3192] | 839 | * @param pThis The kDepObj instance data.
|
---|
[2267] | 840 | * @param pb The start of the file.
|
---|
| 841 | * @param cb The file size.
|
---|
| 842 | */
|
---|
[3192] | 843 | KBOOL kDepObjCOFFTest(PKDEPOBJGLOBALS pThis, const KU8 *pbFile, KSIZE cbFile)
|
---|
[2267] | 844 | {
|
---|
[2896] | 845 | IMAGE_FILE_HEADER const *pFileHdr = (IMAGE_FILE_HEADER const *)pbFile;
|
---|
| 846 | ANON_OBJECT_HEADER_BIGOBJ const *pBigObjHdr = (ANON_OBJECT_HEADER_BIGOBJ const *)pbFile;
|
---|
| 847 | IMAGE_SECTION_HEADER const *paSHdrs;
|
---|
| 848 | KU32 cSHdrs;
|
---|
| 849 | KU32 iSHdr;
|
---|
| 850 | KSIZE cbHdrs;
|
---|
[2267] | 851 |
|
---|
| 852 | if (cbFile <= sizeof(*pFileHdr))
|
---|
| 853 | return K_FALSE;
|
---|
| 854 |
|
---|
[2896] | 855 | /*
|
---|
| 856 | * Deal with -bigobj output first.
|
---|
| 857 | */
|
---|
| 858 | if ( pBigObjHdr->Sig1 == 0
|
---|
| 859 | && pBigObjHdr->Sig2 == KU16_MAX)
|
---|
| 860 | {
|
---|
| 861 | static const KU8 s_abClsId[16] = { ANON_OBJECT_HEADER_BIGOBJ_CLS_ID_BYTES };
|
---|
[2267] | 862 |
|
---|
[2896] | 863 | paSHdrs = (IMAGE_SECTION_HEADER const *)(pBigObjHdr + 1);
|
---|
| 864 | cSHdrs = pBigObjHdr->NumberOfSections;
|
---|
| 865 | cbHdrs = sizeof(IMAGE_SECTION_HEADER) * cSHdrs;
|
---|
[2267] | 866 |
|
---|
[2896] | 867 | if (cbFile <= sizeof(*pBigObjHdr))
|
---|
| 868 | return K_FALSE;
|
---|
| 869 |
|
---|
| 870 | if (pBigObjHdr->Version != 2)
|
---|
| 871 | return K_FALSE;
|
---|
| 872 | if (memcmp(&pBigObjHdr->ClassID[0], s_abClsId, sizeof(pBigObjHdr->ClassID)) != 0)
|
---|
| 873 | return K_FALSE;
|
---|
| 874 |
|
---|
| 875 | if ( pBigObjHdr->Machine != IMAGE_FILE_MACHINE_I386
|
---|
| 876 | && pBigObjHdr->Machine != IMAGE_FILE_MACHINE_AMD64
|
---|
| 877 | && pBigObjHdr->Machine != IMAGE_FILE_MACHINE_ARM
|
---|
| 878 | && pBigObjHdr->Machine != IMAGE_FILE_MACHINE_ARMNT
|
---|
| 879 | && pBigObjHdr->Machine != IMAGE_FILE_MACHINE_ARM64
|
---|
| 880 | && pBigObjHdr->Machine != IMAGE_FILE_MACHINE_EBC)
|
---|
| 881 | {
|
---|
[3192] | 882 | kDepErr(pThis, 1, "bigobj Machine not supported: %#x", pBigObjHdr->Machine);
|
---|
[2896] | 883 | return K_FALSE;
|
---|
| 884 | }
|
---|
| 885 | if (pBigObjHdr->Flags != 0)
|
---|
| 886 | {
|
---|
[3192] | 887 | kDepErr(pThis, 1, "bigobj Flags field is non-zero: %#x", pBigObjHdr->Flags);
|
---|
[2896] | 888 | return K_FALSE;
|
---|
| 889 | }
|
---|
| 890 | if (pBigObjHdr->SizeOfData != 0)
|
---|
| 891 | {
|
---|
[3192] | 892 | kDepErr(pThis, 1, "bigobj SizeOfData field is non-zero: %#x", pBigObjHdr->SizeOfData);
|
---|
[2896] | 893 | return K_FALSE;
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 | if ( pBigObjHdr->PointerToSymbolTable != 0
|
---|
| 897 | && ( pBigObjHdr->PointerToSymbolTable < cbHdrs
|
---|
| 898 | || pBigObjHdr->PointerToSymbolTable > cbFile))
|
---|
| 899 | return K_FALSE;
|
---|
| 900 | if ( pBigObjHdr->PointerToSymbolTable == 0
|
---|
| 901 | && pBigObjHdr->NumberOfSymbols != 0)
|
---|
| 902 | return K_FALSE;
|
---|
| 903 | }
|
---|
| 904 | /*
|
---|
| 905 | * Look for normal COFF object.
|
---|
| 906 | */
|
---|
| 907 | else
|
---|
| 908 | {
|
---|
| 909 | paSHdrs = (IMAGE_SECTION_HEADER const *)((KU8 const *)(pFileHdr + 1) + pFileHdr->SizeOfOptionalHeader);
|
---|
| 910 | cSHdrs = pFileHdr->NumberOfSections;
|
---|
| 911 | cbHdrs = (const KU8 *)&paSHdrs[cSHdrs] - (const KU8 *)pbFile;
|
---|
| 912 |
|
---|
| 913 | if ( pFileHdr->Machine != IMAGE_FILE_MACHINE_I386
|
---|
| 914 | && pFileHdr->Machine != IMAGE_FILE_MACHINE_AMD64
|
---|
| 915 | && pFileHdr->Machine != IMAGE_FILE_MACHINE_ARM
|
---|
| 916 | && pFileHdr->Machine != IMAGE_FILE_MACHINE_ARMNT
|
---|
| 917 | && pFileHdr->Machine != IMAGE_FILE_MACHINE_ARM64
|
---|
| 918 | && pFileHdr->Machine != IMAGE_FILE_MACHINE_EBC)
|
---|
| 919 | return K_FALSE;
|
---|
| 920 |
|
---|
| 921 | if (pFileHdr->SizeOfOptionalHeader != 0)
|
---|
| 922 | return K_FALSE; /* COFF files doesn't have an optional header */
|
---|
| 923 |
|
---|
| 924 | if ( pFileHdr->PointerToSymbolTable != 0
|
---|
| 925 | && ( pFileHdr->PointerToSymbolTable < cbHdrs
|
---|
| 926 | || pFileHdr->PointerToSymbolTable > cbFile))
|
---|
| 927 | return K_FALSE;
|
---|
| 928 | if ( pFileHdr->PointerToSymbolTable == 0
|
---|
| 929 | && pFileHdr->NumberOfSymbols != 0)
|
---|
| 930 | return K_FALSE;
|
---|
| 931 | if ( pFileHdr->Characteristics
|
---|
| 932 | & ( IMAGE_FILE_DLL
|
---|
| 933 | | IMAGE_FILE_SYSTEM
|
---|
| 934 | | IMAGE_FILE_UP_SYSTEM_ONLY
|
---|
| 935 | | IMAGE_FILE_NET_RUN_FROM_SWAP
|
---|
| 936 | | IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP
|
---|
| 937 | | IMAGE_FILE_EXECUTABLE_IMAGE
|
---|
| 938 | | IMAGE_FILE_RELOCS_STRIPPED))
|
---|
| 939 | return K_FALSE;
|
---|
| 940 | }
|
---|
| 941 | if ( cSHdrs <= 1
|
---|
| 942 | || cSHdrs > cbFile)
|
---|
[2267] | 943 | return K_FALSE;
|
---|
[2896] | 944 | if (cbHdrs >= cbFile)
|
---|
[2267] | 945 | return K_FALSE;
|
---|
| 946 |
|
---|
[2896] | 947 | /*
|
---|
| 948 | * Check the section headers.
|
---|
| 949 | */
|
---|
[2267] | 950 | for (iSHdr = 0; iSHdr < cSHdrs; iSHdr++)
|
---|
| 951 | {
|
---|
[2896] | 952 | if ( paSHdrs[iSHdr].PointerToRawData != 0
|
---|
| 953 | && ( paSHdrs[iSHdr].PointerToRawData < cbHdrs
|
---|
| 954 | || paSHdrs[iSHdr].PointerToRawData >= cbFile
|
---|
| 955 | || paSHdrs[iSHdr].PointerToRawData + paSHdrs[iSHdr].SizeOfRawData > cbFile))
|
---|
[2267] | 956 | return K_FALSE;
|
---|
[2896] | 957 | if ( paSHdrs[iSHdr].PointerToRelocations != 0
|
---|
| 958 | && ( paSHdrs[iSHdr].PointerToRelocations < cbHdrs
|
---|
| 959 | || paSHdrs[iSHdr].PointerToRelocations >= cbFile
|
---|
| 960 | || paSHdrs[iSHdr].PointerToRelocations + paSHdrs[iSHdr].NumberOfRelocations * 10 > cbFile)) /* IMAGE_RELOCATION */
|
---|
[2267] | 961 | return K_FALSE;
|
---|
[2896] | 962 | if ( paSHdrs[iSHdr].PointerToLinenumbers != 0
|
---|
| 963 | && ( paSHdrs[iSHdr].PointerToLinenumbers < cbHdrs
|
---|
| 964 | || paSHdrs[iSHdr].PointerToLinenumbers >= cbFile
|
---|
| 965 | || paSHdrs[iSHdr].PointerToLinenumbers + paSHdrs[iSHdr].NumberOfLinenumbers * 6 > cbFile)) /* IMAGE_LINENUMBER */
|
---|
[2267] | 966 | return K_FALSE;
|
---|
| 967 | }
|
---|
| 968 |
|
---|
| 969 | return K_TRUE;
|
---|
| 970 | }
|
---|
| 971 |
|
---|
| 972 |
|
---|
| 973 | /**
|
---|
[2263] | 974 | * Read the file into memory and parse it.
|
---|
| 975 | */
|
---|
[3167] | 976 | static int kDepObjProcessFile(PKDEPOBJGLOBALS pThis, FILE *pInput)
|
---|
[2263] | 977 | {
|
---|
| 978 | size_t cbFile;
|
---|
| 979 | KU8 *pbFile;
|
---|
| 980 | void *pvOpaque;
|
---|
| 981 | int rc = 0;
|
---|
| 982 |
|
---|
| 983 | /*
|
---|
| 984 | * Read the file into memory.
|
---|
| 985 | */
|
---|
| 986 | pbFile = (KU8 *)depReadFileIntoMemory(pInput, &cbFile, &pvOpaque);
|
---|
| 987 | if (!pbFile)
|
---|
| 988 | return 1;
|
---|
| 989 |
|
---|
| 990 | /*
|
---|
| 991 | * See if it's an OMF file, then process it.
|
---|
| 992 | */
|
---|
| 993 | if (kDepObjOMFTest(pbFile, cbFile))
|
---|
[3167] | 994 | rc = kDepObjOMFParse(pThis, pbFile, cbFile);
|
---|
[3192] | 995 | else if (kDepObjCOFFTest(pThis, pbFile, cbFile))
|
---|
[3167] | 996 | rc = kDepObjCOFFParse(pThis, pbFile, cbFile);
|
---|
[2263] | 997 | else
|
---|
[3192] | 998 | rc = kDepErr(pThis, 1, "Doesn't recognize the header of the OMF/COFF file.");
|
---|
[2263] | 999 |
|
---|
| 1000 | depFreeFileMemory(pbFile, pvOpaque);
|
---|
| 1001 | return rc;
|
---|
| 1002 | }
|
---|
| 1003 |
|
---|
| 1004 |
|
---|
[3192] | 1005 | static void kDebObjUsage(PKMKBUILTINCTX pCtx, int fIsErr)
|
---|
[2263] | 1006 | {
|
---|
[3192] | 1007 | kmk_builtin_ctx_printf(pCtx, fIsErr,
|
---|
| 1008 | "usage: %s -o <output> -t <target> [-fqs] [-e <ignore-ext>] <OMF or COFF file>\n"
|
---|
| 1009 | " or: %s --help\n"
|
---|
| 1010 | " or: %s --version\n",
|
---|
| 1011 | pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
|
---|
[2263] | 1012 | }
|
---|
| 1013 |
|
---|
| 1014 |
|
---|
[3192] | 1015 | int kmk_builtin_kDepObj(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
|
---|
[2263] | 1016 | {
|
---|
[3167] | 1017 | int i;
|
---|
| 1018 | KDEPOBJGLOBALS This;
|
---|
[2263] | 1019 |
|
---|
| 1020 | /* Arguments. */
|
---|
| 1021 | FILE *pOutput = NULL;
|
---|
| 1022 | const char *pszOutput = NULL;
|
---|
| 1023 | FILE *pInput = NULL;
|
---|
| 1024 | const char *pszTarget = NULL;
|
---|
| 1025 | int fStubs = 0;
|
---|
| 1026 | int fFixCase = 0;
|
---|
[2955] | 1027 | const char *pszIgnoreExt = NULL;
|
---|
[2263] | 1028 | /* Argument parsing. */
|
---|
| 1029 | int fInput = 0; /* set when we've found input argument. */
|
---|
| 1030 | int fQuiet = 0;
|
---|
| 1031 |
|
---|
[3167] | 1032 | /* Init instance data. */
|
---|
[3192] | 1033 | This.pCtx = pCtx;
|
---|
[3167] | 1034 | This.pszFile = NULL;
|
---|
[2263] | 1035 |
|
---|
| 1036 | /*
|
---|
| 1037 | * Parse arguments.
|
---|
| 1038 | */
|
---|
| 1039 | if (argc <= 1)
|
---|
| 1040 | {
|
---|
[3192] | 1041 | kDebObjUsage(pCtx, 0);
|
---|
[2263] | 1042 | return 1;
|
---|
| 1043 | }
|
---|
| 1044 | for (i = 1; i < argc; i++)
|
---|
| 1045 | {
|
---|
| 1046 | if (argv[i][0] == '-')
|
---|
| 1047 | {
|
---|
[2955] | 1048 | const char *pszValue;
|
---|
[2263] | 1049 | const char *psz = &argv[i][1];
|
---|
[2955] | 1050 | char chOpt;
|
---|
| 1051 | chOpt = *psz++;
|
---|
| 1052 | if (chOpt == '-')
|
---|
[2263] | 1053 | {
|
---|
[2955] | 1054 | /* Convert long to short option. */
|
---|
| 1055 | if (!strcmp(psz, "quiet"))
|
---|
| 1056 | chOpt = 'q';
|
---|
| 1057 | else if (!strcmp(psz, "help"))
|
---|
| 1058 | chOpt = '?';
|
---|
| 1059 | else if (!strcmp(psz, "version"))
|
---|
| 1060 | chOpt = 'V';
|
---|
| 1061 | else
|
---|
| 1062 | {
|
---|
[3192] | 1063 | errx(pCtx, 2, "Invalid argument '%s'.", argv[i]);
|
---|
| 1064 | kDebObjUsage(pCtx, 1);
|
---|
[2955] | 1065 | return 2;
|
---|
| 1066 | }
|
---|
| 1067 | psz = "";
|
---|
[2263] | 1068 | }
|
---|
| 1069 |
|
---|
[2955] | 1070 | /*
|
---|
| 1071 | * Requires value?
|
---|
| 1072 | */
|
---|
| 1073 | switch (chOpt)
|
---|
[2263] | 1074 | {
|
---|
[2955] | 1075 | case 'o':
|
---|
| 1076 | case 't':
|
---|
| 1077 | case 'e':
|
---|
| 1078 | if (*psz)
|
---|
| 1079 | pszValue = psz;
|
---|
| 1080 | else if (++i < argc)
|
---|
| 1081 | pszValue = argv[i];
|
---|
| 1082 | else
|
---|
[3192] | 1083 | return errx(pCtx, 2, "The '-%c' option takes a value.", chOpt);
|
---|
[2955] | 1084 | break;
|
---|
| 1085 |
|
---|
| 1086 | default:
|
---|
| 1087 | pszValue = NULL;
|
---|
| 1088 | break;
|
---|
| 1089 | }
|
---|
| 1090 |
|
---|
| 1091 |
|
---|
| 1092 | switch (chOpt)
|
---|
| 1093 | {
|
---|
[2263] | 1094 | /*
|
---|
| 1095 | * Output file.
|
---|
| 1096 | */
|
---|
| 1097 | case 'o':
|
---|
| 1098 | {
|
---|
| 1099 | if (pOutput)
|
---|
[3192] | 1100 | return errx(pCtx, 2, "only one output file!");
|
---|
[2955] | 1101 | pszOutput = pszValue;
|
---|
[2263] | 1102 | if (pszOutput[0] == '-' && !pszOutput[1])
|
---|
| 1103 | pOutput = stdout;
|
---|
| 1104 | else
|
---|
[3219] | 1105 | pOutput = fopen(pszOutput, "w" KMK_FOPEN_NO_INHERIT_MODE);
|
---|
[2263] | 1106 | if (!pOutput)
|
---|
[3192] | 1107 | return err(pCtx, 1, "Failed to create output file '%s'", pszOutput);
|
---|
[2263] | 1108 | break;
|
---|
| 1109 | }
|
---|
| 1110 |
|
---|
| 1111 | /*
|
---|
| 1112 | * Target name.
|
---|
| 1113 | */
|
---|
| 1114 | case 't':
|
---|
| 1115 | {
|
---|
| 1116 | if (pszTarget)
|
---|
[3192] | 1117 | return errx(pCtx, 2, "only one target!");
|
---|
[2955] | 1118 | pszTarget = pszValue;
|
---|
[2263] | 1119 | break;
|
---|
| 1120 | }
|
---|
| 1121 |
|
---|
| 1122 | /*
|
---|
| 1123 | * Fix case.
|
---|
| 1124 | */
|
---|
| 1125 | case 'f':
|
---|
| 1126 | {
|
---|
| 1127 | fFixCase = 1;
|
---|
| 1128 | break;
|
---|
| 1129 | }
|
---|
| 1130 |
|
---|
| 1131 | /*
|
---|
| 1132 | * Quiet.
|
---|
| 1133 | */
|
---|
| 1134 | case 'q':
|
---|
| 1135 | {
|
---|
| 1136 | fQuiet = 1;
|
---|
| 1137 | break;
|
---|
| 1138 | }
|
---|
| 1139 |
|
---|
| 1140 | /*
|
---|
| 1141 | * Generate stubs.
|
---|
| 1142 | */
|
---|
| 1143 | case 's':
|
---|
| 1144 | {
|
---|
| 1145 | fStubs = 1;
|
---|
| 1146 | break;
|
---|
| 1147 | }
|
---|
| 1148 |
|
---|
| 1149 | /*
|
---|
[2955] | 1150 | * Extension to ignore.
|
---|
| 1151 | */
|
---|
| 1152 | case 'e':
|
---|
| 1153 | {
|
---|
| 1154 | if (pszIgnoreExt)
|
---|
[3192] | 1155 | return errx(pCtx, 2, "The '-e' option can only be used once!");
|
---|
[2955] | 1156 | pszIgnoreExt = pszValue;
|
---|
| 1157 | break;
|
---|
| 1158 | }
|
---|
| 1159 |
|
---|
| 1160 | /*
|
---|
[2263] | 1161 | * The mandatory version & help.
|
---|
| 1162 | */
|
---|
| 1163 | case '?':
|
---|
[3192] | 1164 | kDebObjUsage(pCtx, 0);
|
---|
[2263] | 1165 | return 0;
|
---|
| 1166 | case 'V':
|
---|
| 1167 | case 'v':
|
---|
| 1168 | return kbuild_version(argv[0]);
|
---|
| 1169 |
|
---|
| 1170 | /*
|
---|
| 1171 | * Invalid argument.
|
---|
| 1172 | */
|
---|
| 1173 | default:
|
---|
[3192] | 1174 | errx(pCtx, 2, "Invalid argument '%s'.", argv[i]);
|
---|
| 1175 | kDebObjUsage(pCtx, 1);
|
---|
[2955] | 1176 | return 2;
|
---|
[2263] | 1177 | }
|
---|
| 1178 | }
|
---|
| 1179 | else
|
---|
| 1180 | {
|
---|
[3219] | 1181 | pInput = fopen(argv[i], "rb" KMK_FOPEN_NO_INHERIT_MODE);
|
---|
[2263] | 1182 | if (!pInput)
|
---|
[3192] | 1183 | return err(pCtx, 1, "Failed to open input file '%s'", argv[i]);
|
---|
[3341] | 1184 | This.pszFile = argv[i];
|
---|
[2263] | 1185 | fInput = 1;
|
---|
| 1186 | }
|
---|
| 1187 |
|
---|
| 1188 | /*
|
---|
| 1189 | * End of the line?
|
---|
| 1190 | */
|
---|
| 1191 | if (fInput)
|
---|
| 1192 | {
|
---|
| 1193 | if (++i < argc)
|
---|
[3192] | 1194 | return errx(pCtx, 2, "No arguments shall follow the input spec.");
|
---|
[2263] | 1195 | break;
|
---|
| 1196 | }
|
---|
| 1197 | }
|
---|
| 1198 |
|
---|
| 1199 | /*
|
---|
| 1200 | * Got all we require?
|
---|
| 1201 | */
|
---|
| 1202 | if (!pInput)
|
---|
[3192] | 1203 | return errx(pCtx, 2, "No input!");
|
---|
[2263] | 1204 | if (!pOutput)
|
---|
[3192] | 1205 | return errx(pCtx, 2, "No output!");
|
---|
[2263] | 1206 | if (!pszTarget)
|
---|
[3192] | 1207 | return errx(pCtx, 2, "No target!");
|
---|
[2263] | 1208 |
|
---|
| 1209 | /*
|
---|
| 1210 | * Do the parsing.
|
---|
| 1211 | */
|
---|
[3167] | 1212 | depInit(&This.Core);
|
---|
| 1213 | i = kDepObjProcessFile(&This, pInput);
|
---|
[2263] | 1214 | fclose(pInput);
|
---|
| 1215 |
|
---|
| 1216 | /*
|
---|
| 1217 | * Write the dependecy file.
|
---|
| 1218 | */
|
---|
| 1219 | if (!i)
|
---|
| 1220 | {
|
---|
[3167] | 1221 | depOptimize(&This.Core, fFixCase, fQuiet, pszIgnoreExt);
|
---|
[3315] | 1222 | depPrintTargetWithDeps(&This.Core, pOutput, pszTarget, 1 /*fEscapeTarget*/);
|
---|
[2263] | 1223 | if (fStubs)
|
---|
[3167] | 1224 | depPrintStubs(&This.Core, pOutput);
|
---|
[2263] | 1225 | }
|
---|
| 1226 |
|
---|
| 1227 | /*
|
---|
| 1228 | * Close the output, delete output on failure.
|
---|
| 1229 | */
|
---|
| 1230 | if (!i && ferror(pOutput))
|
---|
[3192] | 1231 | i = errx(pCtx, 1, "Error writing to '%s'", pszOutput);
|
---|
[2263] | 1232 | fclose(pOutput);
|
---|
| 1233 | if (i)
|
---|
| 1234 | {
|
---|
| 1235 | if (unlink(pszOutput))
|
---|
[3192] | 1236 | warn(pCtx, "warning: failed to remove output file '%s' on failure.", pszOutput);
|
---|
[2263] | 1237 | }
|
---|
| 1238 |
|
---|
[3167] | 1239 | depCleanup(&This.Core);
|
---|
[2263] | 1240 | return i;
|
---|
| 1241 | }
|
---|
| 1242 |
|
---|
[3192] | 1243 | #ifdef KMK_BUILTIN_STANDALONE
|
---|
| 1244 | int main(int argc, char **argv, char **envp)
|
---|
| 1245 | {
|
---|
| 1246 | KMKBUILTINCTX Ctx = { "kDepObj", NULL };
|
---|
| 1247 | return kmk_builtin_kDepObj(argc, argv, envp, &Ctx);
|
---|
| 1248 | }
|
---|
| 1249 | #endif
|
---|
| 1250 |
|
---|