source: trunk/src/kmk/kmkbuiltin/kDepObj.c@ 2896

Last change on this file since 2896 was 2896, checked in by bird, 9 years ago

kDepObj: Added support for -bigobj output.

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