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

Last change on this file since 3115 was 3064, checked in by bird, 8 years ago

kDepObj: warnings

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