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

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

Included kDepObj in kWorker as a post execution option.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 38.1 KB
Line 
1/* $Id: kDepObj.c 2894 2016-09-08 13:27:56Z 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 IMAGE_SECTION_HEADER const *paSHdrs = (IMAGE_SECTION_HEADER const *)((KU8 const *)(pFileHdr + 1) + pFileHdr->SizeOfOptionalHeader);
756 unsigned cSHdrs = pFileHdr->NumberOfSections;
757 unsigned iSHdr;
758 KPCUINT u;
759 int rcRet = 2;
760 int rc;
761
762 dprintf(("COFF file!\n"));
763
764 for (iSHdr = 0; iSHdr < cSHdrs; iSHdr++)
765 {
766 if ( !memcmp(paSHdrs[iSHdr].Name, ".debug$S", sizeof(".debug$S") - 1)
767 && paSHdrs[iSHdr].SizeOfRawData > 4)
768 {
769 u.pb = pbFile + paSHdrs[iSHdr].PointerToRawData;
770 dprintf(("CV symbol table: version=%x\n", *u.pu32));
771 if (*u.pu32 == 0x000000004)
772 rc = kDepObjCOFFParseCV8SymbolSection(u.pb, paSHdrs[iSHdr].SizeOfRawData);
773 else
774 rc = 2;
775 dprintf(("rc=%d\n", rc));
776 if (rcRet == 2)
777 rcRet = rc;
778 if (rcRet != 2)
779 return rc;
780 }
781 dprintf(("#%d: %.8s\n", iSHdr, paSHdrs[iSHdr].Name));
782 }
783 return rcRet;
784}
785
786
787/**
788 * Checks if this file is a COFF file or not.
789 *
790 * @returns K_TRUE if it's COFF, K_FALSE otherwise.
791 *
792 * @param pb The start of the file.
793 * @param cb The file size.
794 */
795KBOOL kDepObjCOFFTest(const KU8 *pbFile, KSIZE cbFile)
796{
797 IMAGE_FILE_HEADER const *pFileHdr = (IMAGE_FILE_HEADER const *)pbFile;
798 IMAGE_SECTION_HEADER const *paSHdrs = (IMAGE_SECTION_HEADER const *)((KU8 const *)(pFileHdr + 1) + pFileHdr->SizeOfOptionalHeader);
799 unsigned cSHdrs = pFileHdr->NumberOfSections;
800 unsigned iSHdr;
801 KSIZE cbHdrs = (const KU8 *)&paSHdrs[cSHdrs] - (const KU8 *)pbFile;
802
803 if (cbFile <= sizeof(*pFileHdr))
804 return K_FALSE;
805 if ( pFileHdr->Machine != IMAGE_FILE_MACHINE_I386
806 && pFileHdr->Machine != IMAGE_FILE_MACHINE_AMD64)
807 return K_FALSE;
808 if (pFileHdr->SizeOfOptionalHeader != 0)
809 return K_FALSE; /* COFF files doesn't have an optional header */
810
811 if ( pFileHdr->NumberOfSections <= 1
812 || pFileHdr->NumberOfSections > cbFile)
813 return K_FALSE;
814
815 if (cbHdrs >= cbFile)
816 return K_FALSE;
817
818 if ( pFileHdr->PointerToSymbolTable != 0
819 && ( pFileHdr->PointerToSymbolTable < cbHdrs
820 || pFileHdr->PointerToSymbolTable > cbFile))
821 return K_FALSE;
822 if ( pFileHdr->PointerToSymbolTable == 0
823 && pFileHdr->NumberOfSymbols != 0)
824 return K_FALSE;
825 if ( pFileHdr->Characteristics
826 & ( IMAGE_FILE_DLL
827 | IMAGE_FILE_SYSTEM
828 | IMAGE_FILE_UP_SYSTEM_ONLY
829 | IMAGE_FILE_NET_RUN_FROM_SWAP
830 | IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP
831 | IMAGE_FILE_EXECUTABLE_IMAGE
832 | IMAGE_FILE_RELOCS_STRIPPED))
833 return K_FALSE;
834
835 for (iSHdr = 0; iSHdr < cSHdrs; iSHdr++)
836 {
837 if ( paSHdrs[iSHdr].PointerToRawData != 0
838 && ( paSHdrs[iSHdr].PointerToRawData < cbHdrs
839 || paSHdrs[iSHdr].PointerToRawData >= cbFile
840 || paSHdrs[iSHdr].PointerToRawData + paSHdrs[iSHdr].SizeOfRawData > cbFile))
841 return K_FALSE;
842 if ( paSHdrs[iSHdr].PointerToRelocations != 0
843 && ( paSHdrs[iSHdr].PointerToRelocations < cbHdrs
844 || paSHdrs[iSHdr].PointerToRelocations >= cbFile
845 || paSHdrs[iSHdr].PointerToRelocations + paSHdrs[iSHdr].NumberOfRelocations * 10 > cbFile)) /* IMAGE_RELOCATION */
846 return K_FALSE;
847 if ( paSHdrs[iSHdr].PointerToLinenumbers != 0
848 && ( paSHdrs[iSHdr].PointerToLinenumbers < cbHdrs
849 || paSHdrs[iSHdr].PointerToLinenumbers >= cbFile
850 || paSHdrs[iSHdr].PointerToLinenumbers + paSHdrs[iSHdr].NumberOfLinenumbers * 6 > cbFile)) /* IMAGE_LINENUMBER */
851 return K_FALSE;
852 }
853
854 return K_TRUE;
855}
856
857
858/**
859 * Read the file into memory and parse it.
860 */
861static int kDepObjProcessFile(FILE *pInput)
862{
863 size_t cbFile;
864 KU8 *pbFile;
865 void *pvOpaque;
866 int rc = 0;
867
868 /*
869 * Read the file into memory.
870 */
871 pbFile = (KU8 *)depReadFileIntoMemory(pInput, &cbFile, &pvOpaque);
872 if (!pbFile)
873 return 1;
874
875 /*
876 * See if it's an OMF file, then process it.
877 */
878 if (kDepObjOMFTest(pbFile, cbFile))
879 rc = kDepObjOMFParse(pbFile, cbFile);
880 else if (kDepObjCOFFTest(pbFile, cbFile))
881 rc = kDepObjCOFFParse(pbFile, cbFile);
882 else
883 {
884 fprintf(stderr, "%s: error: Doesn't recognize the header of the OMF file.\n", argv0);
885 rc = 1;
886 }
887
888 depFreeFileMemory(pbFile, pvOpaque);
889 return rc;
890}
891
892
893static void usage(const char *a_argv0)
894{
895 printf("usage: %s -o <output> -t <target> [-fqs] <OMF-file>\n"
896 " or: %s --help\n"
897 " or: %s --version\n",
898 a_argv0, a_argv0, a_argv0);
899}
900
901
902int kmk_builtin_kDepObj(int argc, char *argv[], char **envp)
903{
904 int i;
905
906 /* Arguments. */
907 FILE *pOutput = NULL;
908 const char *pszOutput = NULL;
909 FILE *pInput = NULL;
910 const char *pszTarget = NULL;
911 int fStubs = 0;
912 int fFixCase = 0;
913 /* Argument parsing. */
914 int fInput = 0; /* set when we've found input argument. */
915 int fQuiet = 0;
916
917 argv0 = argv[0];
918
919 /*
920 * Parse arguments.
921 */
922 if (argc <= 1)
923 {
924 usage(argv[0]);
925 return 1;
926 }
927 for (i = 1; i < argc; i++)
928 {
929 if (argv[i][0] == '-')
930 {
931 const char *psz = &argv[i][1];
932 if (*psz == '-')
933 {
934 if (!strcmp(psz, "-quiet"))
935 psz = "q";
936 else if (!strcmp(psz, "-help"))
937 psz = "?";
938 else if (!strcmp(psz, "-version"))
939 psz = "V";
940 }
941
942 switch (*psz)
943 {
944 /*
945 * Output file.
946 */
947 case 'o':
948 {
949 pszOutput = &argv[i][2];
950 if (pOutput)
951 {
952 fprintf(stderr, "%s: syntax error: only one output file!\n", argv[0]);
953 return 1;
954 }
955 if (!*pszOutput)
956 {
957 if (++i >= argc)
958 {
959 fprintf(stderr, "%s: syntax error: The '-o' argument is missing the filename.\n", argv[0]);
960 return 1;
961 }
962 pszOutput = argv[i];
963 }
964 if (pszOutput[0] == '-' && !pszOutput[1])
965 pOutput = stdout;
966 else
967 pOutput = fopen(pszOutput, "w");
968 if (!pOutput)
969 {
970 fprintf(stderr, "%s: error: Failed to create output file '%s'.\n", argv[0], pszOutput);
971 return 1;
972 }
973 break;
974 }
975
976 /*
977 * Target name.
978 */
979 case 't':
980 {
981 if (pszTarget)
982 {
983 fprintf(stderr, "%s: syntax error: only one target!\n", argv[0]);
984 return 1;
985 }
986 pszTarget = &argv[i][2];
987 if (!*pszTarget)
988 {
989 if (++i >= argc)
990 {
991 fprintf(stderr, "%s: syntax error: The '-t' argument is missing the target name.\n", argv[0]);
992 return 1;
993 }
994 pszTarget = argv[i];
995 }
996 break;
997 }
998
999 /*
1000 * Fix case.
1001 */
1002 case 'f':
1003 {
1004 fFixCase = 1;
1005 break;
1006 }
1007
1008 /*
1009 * Quiet.
1010 */
1011 case 'q':
1012 {
1013 fQuiet = 1;
1014 break;
1015 }
1016
1017 /*
1018 * Generate stubs.
1019 */
1020 case 's':
1021 {
1022 fStubs = 1;
1023 break;
1024 }
1025
1026 /*
1027 * The mandatory version & help.
1028 */
1029 case '?':
1030 usage(argv[0]);
1031 return 0;
1032 case 'V':
1033 case 'v':
1034 return kbuild_version(argv[0]);
1035
1036 /*
1037 * Invalid argument.
1038 */
1039 default:
1040 fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);
1041 usage(argv[0]);
1042 return 1;
1043 }
1044 }
1045 else
1046 {
1047 pInput = fopen(argv[i], "rb");
1048 if (!pInput)
1049 {
1050 fprintf(stderr, "%s: error: Failed to open input file '%s'.\n", argv[0], argv[i]);
1051 return 1;
1052 }
1053 fInput = 1;
1054 }
1055
1056 /*
1057 * End of the line?
1058 */
1059 if (fInput)
1060 {
1061 if (++i < argc)
1062 {
1063 fprintf(stderr, "%s: syntax error: No arguments shall follow the input spec.\n", argv[0]);
1064 return 1;
1065 }
1066 break;
1067 }
1068 }
1069
1070 /*
1071 * Got all we require?
1072 */
1073 if (!pInput)
1074 {
1075 fprintf(stderr, "%s: syntax error: No input!\n", argv[0]);
1076 return 1;
1077 }
1078 if (!pOutput)
1079 {
1080 fprintf(stderr, "%s: syntax error: No output!\n", argv[0]);
1081 return 1;
1082 }
1083 if (!pszTarget)
1084 {
1085 fprintf(stderr, "%s: syntax error: No target!\n", argv[0]);
1086 return 1;
1087 }
1088
1089 /*
1090 * Do the parsing.
1091 */
1092 i = kDepObjProcessFile(pInput);
1093 fclose(pInput);
1094
1095 /*
1096 * Write the dependecy file.
1097 */
1098 if (!i)
1099 {
1100 depOptimize(fFixCase, fQuiet);
1101 fprintf(pOutput, "%s:", pszTarget);
1102 depPrint(pOutput);
1103 if (fStubs)
1104 depPrintStubs(pOutput);
1105 }
1106
1107 /*
1108 * Close the output, delete output on failure.
1109 */
1110 if (!i && ferror(pOutput))
1111 {
1112 i = 1;
1113 fprintf(stderr, "%s: error: Error writing to '%s'.\n", argv[0], pszOutput);
1114 }
1115 fclose(pOutput);
1116 if (i)
1117 {
1118 if (unlink(pszOutput))
1119 fprintf(stderr, "%s: warning: failed to remove output file '%s' on failure.\n", argv[0], pszOutput);
1120 }
1121
1122 depCleanup();
1123 return i;
1124}
1125
Note: See TracBrowser for help on using the repository browser.