VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/kDepObj.c@ 2759

Last change on this file since 2759 was 2759, checked in by bird, 10 years ago

More generic ENOSPC workaround for windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 38.2 KB
Line 
1/* $Id: kDepObj.c 2759 2015-01-28 16:14:00Z bird $ */
2/** @file
3 * kDepObj - Extract dependency information from an object file.
4 */
5
6/*
7 * Copyright (c) 2007-2010 knut st. osmundsen <[email protected]>
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 "config.h"
31#include <stdio.h>
32#include <stdlib.h>
33#include <stddef.h>
34#include <string.h>
35#include <errno.h>
36#include <ctype.h>
37#include <stdarg.h>
38#ifdef HAVE_ALLOCA_H
39# include <alloca.h>
40#endif
41#if !defined(_MSC_VER)
42# include <unistd.h>
43#else
44# include <io.h>
45#endif
46#include "../../lib/k/kDefs.h"
47#include "../../lib/k/kTypes.h"
48#include "../../lib/k/kLdrFmts/pe.h"
49#include "../../lib/kDep.h"
50#include "kmkbuiltin.h"
51
52
53/*******************************************************************************
54* Defined Constants And Macros *
55*******************************************************************************/
56/*#define DEBUG*/
57#define DEBUG
58#ifdef DEBUG
59# define dprintf(a) printf a
60# define dump(pb, cb, offBase) depHexDump(pb,cb,offBase)
61#else
62# define dprintf(a) do {} while (0)
63# define dump(pb, cb, offBase) do {} while (0)
64#endif
65
66/** @name OMF defines
67 * @{ */
68#define KDEPOMF_THEADR 0x80
69#define KDEPOMF_LHEADR 0x82
70#define KDEPOMF_COMENT 0x88
71#define KDEPOMF_CMTCLS_DEPENDENCY 0xe9
72#define KDEPOMF_CMTCLS_DBGTYPE 0xa1
73#define KDEPOMF_LINNUM 0x94
74#define KDEPOMF_LINNUM32 0x95
75/** @} */
76
77
78/*******************************************************************************
79* Structures and Typedefs *
80*******************************************************************************/
81/** @name OMF Structures
82 * @{ */
83#pragma pack(1)
84/** OMF record header. */
85typedef struct KDEPOMFHDR
86{
87 /** The record type. */
88 KU8 bType;
89 /** The size of the record, excluding this header. */
90 KU16 cbRec;
91} KDEPOMFHDR;
92typedef KDEPOMFHDR *PKDEPOMFHDR;
93typedef const KDEPOMFHDR *PCKDEPOMFHDR;
94
95/** OMF string. */
96typedef struct KDEPOMFSTR
97{
98 KU8 cch;
99 char ach[1];
100} KDEPOMFSTR;
101typedef KDEPOMFSTR *PKDEPOMFSTR;
102typedef const KDEPOMFSTR *PCKDEPOMFSTR;
103
104/** THEADR/LHEADR. */
105typedef struct KDEPOMFTHEADR
106{
107 KDEPOMFHDR Hdr;
108 KDEPOMFSTR Name;
109} KDEPOMFTHEADR;
110typedef KDEPOMFTHEADR *PKDEPOMFTHEADR;
111typedef const KDEPOMFTHEADR *PCKDEPOMFTHEADR;
112
113/** Dependency File. */
114typedef struct KDEPOMFDEPFILE
115{
116 KDEPOMFHDR Hdr;
117 KU8 fType;
118 KU8 bClass;
119 KU16 wDosTime;
120 KU16 wDosDate;
121 KDEPOMFSTR Name;
122} KDEPOMFDEPFILE;
123typedef KDEPOMFDEPFILE *PKDEPOMFDEPFILE;
124typedef const KDEPOMFDEPFILE *PCKDEPOMFDEPFILE;
125
126#pragma pack()
127/** @} */
128
129
130/** @name COFF Structures
131 * @{ */
132#pragma pack(1)
133
134typedef struct KDEPCVSYMHDR
135{
136 /** The record size minus the size field. */
137 KU16 cb;
138 /** The record type. */
139 KU16 uType;
140} KDEPCVSYMHDR;
141typedef KDEPCVSYMHDR *PKDEPCVSYMHDR;
142typedef const KDEPCVSYMHDR *PCKDEPCVSYMHDR;
143
144/** @name Selection of KDEPCVSYMHDR::uType values.
145 * @{ */
146#define K_CV8_S_MSTOOL KU16_C(0x1116)
147/** @} */
148
149typedef struct KDEPCV8SYMHDR
150{
151 /** The record type. */
152 KU32 uType;
153 /** The record size minus the size field. */
154 KU32 cb;
155} KDEPCV8SYMHDR;
156typedef KDEPCV8SYMHDR *PKDEPCV8SYMHDR;
157typedef const KDEPCV8SYMHDR *PCKDEPCV8SYMHDR;
158
159/** @name Known KDEPCV8SYMHDR::uType Values.
160 * @{ */
161#define K_CV8_SYMBOL_INFO KU32_C(0x000000f1)
162#define K_CV8_LINE_NUMBERS KU32_C(0x000000f2)
163#define K_CV8_STRING_TABLE KU32_C(0x000000f3)
164#define K_CV8_SOURCE_FILES KU32_C(0x000000f4)
165#define K_CV8_COMDAT_XXXXX KU32_C(0x000000f5) /**< no idea about the format... */
166/** @} */
167
168#pragma pack()
169/** @} */
170
171
172/*******************************************************************************
173* Global Variables *
174*******************************************************************************/
175/** the executable name. */
176static const char *argv0 = "";
177static const char *g_pszFile = NULL;
178
179
180/**
181 * Prints an error message.
182 *
183 * @returns rc.
184 * @param rc The return code, for making one line return statements.
185 * @param pszFormat The message format string.
186 * @param ... Format arguments.
187 * @todo Promote this to kDep.c.
188 */
189static int kDepErr(int rc, const char *pszFormat, ...)
190{
191 va_list va;
192 const char *psz;
193 const char *pszName = argv0;
194
195 fflush(stdout);
196
197 /* The message prefix. */
198 while ((psz = strpbrk(pszName, "/\\:")) != NULL)
199 pszName = psz + 1;
200
201 if (g_pszFile)
202 fprintf(stderr, "%s: %s: error: ", pszName, g_pszFile);
203 else
204 fprintf(stderr, "%s: error: ", pszName);
205
206 /* The message. */
207 va_start(va, pszFormat);
208 vfprintf(stderr, pszFormat, va);
209 va_end(va);
210
211 return rc;
212}
213
214
215/**
216 * Gets an index from the data.
217 *
218 * @returns The index, KU16_MAX on buffer underflow.
219 * @param puData The current data stream position (in/out).
220 * @param pcbLeft Number of bytes left (in/out).
221 */
222static KU16 kDepObjOMFGetIndex(KPCUINT *puData, KU16 *pcbLeft)
223{
224 KU16 u16;
225
226 if (*pcbLeft >= 1 && *pcbLeft != KU16_MAX)
227 {
228 *pcbLeft -= 1;
229 u16 = *puData->pb++;
230 if (u16 & KU16_C(0x80))
231 {
232 if (*pcbLeft >= 1)
233 {
234 *pcbLeft -= 1;
235 u16 = ((u16 & KU16_C(0x7f)) << 8) | *puData->pb++;
236 }
237 else
238 u16 = KU16_MAX;
239 }
240 }
241 else
242 u16 = KU16_MAX;
243 return u16;
244}
245
246
247/**
248 * Parses the OMF file.
249 *
250 * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
251 * @param pbFile The start of the file.
252 * @param cbFile The file size.
253 */
254int kDepObjOMFParse(const KU8 *pbFile, KSIZE cbFile)
255{
256 PCKDEPOMFHDR pHdr = (PCKDEPOMFHDR)pbFile;
257 KSIZE cbLeft = cbFile;
258 char uDbgType = 0; /* H or C */
259 KU8 uDbgVer = KU8_MAX;
260 KU32 iSrc = 0;
261 KU32 iMaybeSrc = 0;
262 KU8 uLinNumType = KU8_MAX;
263 KU16 cLinNums = 0;
264 KU32 cLinFiles = 0;
265 KU32 iLinFile = 0;
266
267 /*
268 * Iterate thru the records until we hit the end or an invalid one.
269 */
270 while ( cbLeft >= sizeof(*pHdr)
271 && cbLeft >= pHdr->cbRec + sizeof(*pHdr))
272 {
273 KPCUINT uData;
274 uData.pv = pHdr + 1;
275
276 /* process selected record types. */
277 dprintf(("%#07" KUPTR_PRI ": %#04x %#05x\n", (const KU8*)pHdr - pbFile, pHdr->bType, pHdr->cbRec));
278 switch (pHdr->bType)
279 {
280 /*
281 * The T/L Header contains the source name. When emitting CodeView 4
282 * and earlier (like masm and watcom does), all includes used by the
283 * line number tables have their own THEADR record.
284 */
285 case KDEPOMF_THEADR:
286 case KDEPOMF_LHEADR:
287 {
288 PCKDEPOMFTHEADR pTHeadr = (PCKDEPOMFTHEADR)pHdr;
289 if (1 + pTHeadr->Name.cch + 1 != pHdr->cbRec)
290 return kDepErr(1, "%#07x - Bad %cHEADR record, length mismatch.\n",
291 (const KU8*)pHdr - pbFile, pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L');
292 if ( ( pTHeadr->Name.cch > 2
293 && pTHeadr->Name.ach[pTHeadr->Name.cch - 2] == '.'
294 && ( pTHeadr->Name.ach[pTHeadr->Name.cch - 1] == 'o'
295 || pTHeadr->Name.ach[pTHeadr->Name.cch - 1] == 'O'))
296 || ( pTHeadr->Name.cch > 4
297 && pTHeadr->Name.ach[pTHeadr->Name.cch - 4] == '.'
298 && ( pTHeadr->Name.ach[pTHeadr->Name.cch - 3] == 'o'
299 || pTHeadr->Name.ach[pTHeadr->Name.cch - 3] == 'O')
300 && ( pTHeadr->Name.ach[pTHeadr->Name.cch - 2] == 'b'
301 || pTHeadr->Name.ach[pTHeadr->Name.cch - 2] == 'B')
302 && ( pTHeadr->Name.ach[pTHeadr->Name.cch - 1] == 'j'
303 || pTHeadr->Name.ach[pTHeadr->Name.cch - 1] == 'J'))
304 )
305 dprintf(("%cHEADR: %.*s [ignored]\n", pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L', pTHeadr->Name.cch, pTHeadr->Name.ach));
306 else
307 {
308 dprintf(("%cHEADR: %.*s\n", pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L', pTHeadr->Name.cch, pTHeadr->Name.ach));
309 depAdd(pTHeadr->Name.ach, pTHeadr->Name.cch);
310 iMaybeSrc++;
311 }
312 uLinNumType = KU8_MAX;
313 break;
314 }
315
316 case KDEPOMF_COMENT:
317 {
318 KU8 uClass;
319
320 if (pHdr->cbRec < 2 + 1)
321 return kDepErr(1, "%#07x - Bad COMMENT record, too small.\n", (const KU8*)pHdr - pbFile);
322 if (uData.pb[0] & 0x3f)
323 return kDepErr(1, "%#07x - Bad COMMENT record, reserved flags set.\n", (const KU8*)pHdr - pbFile);
324 uClass = uData.pb[1];
325 uData.pb += 2;
326 switch (uClass)
327 {
328 /*
329 * Borland dependency file comment (famously used by wmake and Watcom).
330 */
331 case KDEPOMF_CMTCLS_DEPENDENCY:
332 {
333 PCKDEPOMFDEPFILE pDep = (PCKDEPOMFDEPFILE)pHdr;
334 if (K_OFFSETOF(KDEPOMFDEPFILE, Name.ach[pDep->Name.cch]) + 1 != pHdr->cbRec + sizeof(*pHdr))
335 {
336 /* Empty record indicates the end of the dependency files,
337 no need to go on. */
338 if (pHdr->cbRec == 2 + 1)
339 return 0;
340 return kDepErr(1, "%#07lx - Bad DEPENDENCY FILE record, length mismatch. (%u/%u)\n",
341 (long)((const KU8 *)pHdr - pbFile),
342 K_OFFSETOF(KDEPOMFDEPFILE, Name.ach[pDep->Name.cch]) + 1,
343 (unsigned)(pHdr->cbRec + sizeof(*pHdr)));
344 }
345 depAdd(pDep->Name.ach, pDep->Name.cch);
346 iSrc++;
347 break;
348 }
349
350 /*
351 * Pick up the debug type so we can parse the LINNUM records.
352 */
353 case KDEPOMF_CMTCLS_DBGTYPE:
354 if (pHdr->cbRec < 2 + 3 + 1)
355 break; /* ignore, Borland used this for something else apparently. */
356 if ( !(uData.pb[1] == 'C' && uData.pb[2] == 'V')
357 && !(uData.pb[1] == 'H' && uData.pb[2] == 'L'))
358 {
359 dprintf(("Unknown debug type: %c%c (%u)\n", uData.pb[1], uData.pb[2], uData.pb[0]));
360 break;
361 }
362 uDbgType = uData.pb[1];
363 uDbgVer = uData.pb[0];
364 dprintf(("Debug Type %s ver %u\n", uDbgType == 'H' ? "HLL" : "CodeView", uDbgVer));
365 break;
366
367 }
368 break; /* COMENT */
369 }
370
371 /*
372 * LINNUM + THEADR == sigar.
373 */
374 case KDEPOMF_LINNUM:
375 if (uDbgType == 'C')
376 iMaybeSrc |= KU32_C(0x80000000);
377 dprintf(("LINNUM:\n"));
378 break;
379
380 /*
381 * The HLL v4 and v6 file names table will include all files when present, which
382 * is perfect for generating dependencies.
383 */
384 case KDEPOMF_LINNUM32:
385 if ( uDbgType == 'H'
386 && uDbgVer >= 3
387 && uDbgVer <= 6)
388 {
389 /* skip two indexes (group & segment) */
390 KU16 cbRecLeft = pHdr->cbRec - 1;
391 KU16 uGrp = kDepObjOMFGetIndex(&uData, &cbRecLeft);
392 KU16 uSeg = kDepObjOMFGetIndex(&uData, &cbRecLeft);
393 if (uSeg == KU16_MAX)
394 return kDepErr(1, "%#07lx - Bad LINNUM32 record\n", (long)((const KU8 *)pHdr - pbFile));
395
396 if (uLinNumType == KU8_MAX)
397 {
398 static const char * const s_apsz[5] =
399 {
400 "source file", "listing file", "source & listing file", "file names table", "path table"
401 };
402 KU16 uLine;
403 KU8 uReserved;
404 KU16 uSeg2;
405 KU32 cbLinNames;
406
407 if (cbRecLeft < 2+1+1+2+2+4)
408 return kDepErr(1, "%#07lx - Bad LINNUM32 record, too short\n", (long)((const KU8 *)pHdr - pbFile));
409 cbRecLeft -= 2+1+1+2+2+4;
410 uLine = *uData.pu16++;
411 uLinNumType = *uData.pu8++;
412 uReserved = *uData.pu8++;
413 cLinNums = *uData.pu16++;
414 uSeg2 = *uData.pu16++;
415 cbLinNames = *uData.pu32++;
416
417 dprintf(("LINNUM32: uGrp=%#x uSeg=%#x uSeg2=%#x uLine=%#x (MBZ) uReserved=%#x\n",
418 uGrp, uSeg, uSeg2, uLine, uReserved));
419 dprintf(("LINNUM32: cLinNums=%#x (%u) cbLinNames=%#x (%u) uLinNumType=%#x (%s)\n",
420 cLinNums, cLinNums, cbLinNames, cbLinNames, uLinNumType,
421 uLinNumType < K_ELEMENTS(s_apsz) ? s_apsz[uLinNumType] : "??"));
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++;
488 cCols = *uData.pu32++;
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, KSIZE 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 IMAGE_SECTION_HEADER const *paSHdrs = (IMAGE_SECTION_HEADER const *)((KU8 const *)(pFileHdr + 1) + pFileHdr->SizeOfOptionalHeader);
762 unsigned cSHdrs = pFileHdr->NumberOfSections;
763 unsigned iSHdr;
764 KPCUINT u;
765 int rcRet = 2;
766 int rc;
767
768 printf("COFF file!\n");
769
770 for (iSHdr = 0; iSHdr < cSHdrs; iSHdr++)
771 {
772 if ( !memcmp(paSHdrs[iSHdr].Name, ".debug$S", sizeof(".debug$S") - 1)
773 && paSHdrs[iSHdr].SizeOfRawData > 4)
774 {
775 u.pb = pbFile + paSHdrs[iSHdr].PointerToRawData;
776 dprintf(("CV symbol table: version=%x\n", *u.pu32));
777 if (*u.pu32 == 0x000000004)
778 rc = kDepObjCOFFParseCV8SymbolSection(u.pb, paSHdrs[iSHdr].SizeOfRawData);
779 else
780 rc = 2;
781 dprintf(("rc=%d\n", rc));
782 if (rcRet == 2)
783 rcRet = rc;
784 if (rcRet != 2)
785 return rc;
786 }
787 printf("#%d: %.8s\n", iSHdr, paSHdrs[iSHdr].Name);
788 }
789 return rcRet;
790}
791
792
793/**
794 * Checks if this file is a COFF file or not.
795 *
796 * @returns K_TRUE if it's COFF, K_FALSE otherwise.
797 *
798 * @param pb The start of the file.
799 * @param cb The file size.
800 */
801KBOOL kDepObjCOFFTest(const KU8 *pbFile, KSIZE cbFile)
802{
803 IMAGE_FILE_HEADER const *pFileHdr = (IMAGE_FILE_HEADER const *)pbFile;
804 IMAGE_SECTION_HEADER const *paSHdrs = (IMAGE_SECTION_HEADER const *)((KU8 const *)(pFileHdr + 1) + pFileHdr->SizeOfOptionalHeader);
805 unsigned cSHdrs = pFileHdr->NumberOfSections;
806 unsigned iSHdr;
807 KSIZE cbHdrs = (const KU8 *)&paSHdrs[cSHdrs] - (const KU8 *)pbFile;
808
809 if (cbFile <= sizeof(*pFileHdr))
810 return K_FALSE;
811 if ( pFileHdr->Machine != IMAGE_FILE_MACHINE_I386
812 && pFileHdr->Machine != IMAGE_FILE_MACHINE_AMD64)
813 return K_FALSE;
814 if (pFileHdr->SizeOfOptionalHeader != 0)
815 return K_FALSE; /* COFF files doesn't have an optional header */
816
817 if ( pFileHdr->NumberOfSections <= 1
818 || pFileHdr->NumberOfSections > cbFile)
819 return K_FALSE;
820
821 if (cbHdrs >= cbFile)
822 return K_FALSE;
823
824 if ( pFileHdr->PointerToSymbolTable != 0
825 && ( pFileHdr->PointerToSymbolTable < cbHdrs
826 || pFileHdr->PointerToSymbolTable > cbFile))
827 return K_FALSE;
828 if ( pFileHdr->PointerToSymbolTable == 0
829 && pFileHdr->NumberOfSymbols != 0)
830 return K_FALSE;
831 if ( pFileHdr->Characteristics
832 & ( IMAGE_FILE_DLL
833 | IMAGE_FILE_SYSTEM
834 | IMAGE_FILE_UP_SYSTEM_ONLY
835 | IMAGE_FILE_NET_RUN_FROM_SWAP
836 | IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP
837 | IMAGE_FILE_EXECUTABLE_IMAGE
838 | IMAGE_FILE_RELOCS_STRIPPED))
839 return K_FALSE;
840
841 for (iSHdr = 0; iSHdr < cSHdrs; iSHdr++)
842 {
843 if ( paSHdrs[iSHdr].PointerToRawData != 0
844 && ( paSHdrs[iSHdr].PointerToRawData < cbHdrs
845 || paSHdrs[iSHdr].PointerToRawData >= cbFile
846 || paSHdrs[iSHdr].PointerToRawData + paSHdrs[iSHdr].SizeOfRawData > cbFile))
847 return K_FALSE;
848 if ( paSHdrs[iSHdr].PointerToRelocations != 0
849 && ( paSHdrs[iSHdr].PointerToRelocations < cbHdrs
850 || paSHdrs[iSHdr].PointerToRelocations >= cbFile
851 || paSHdrs[iSHdr].PointerToRelocations + paSHdrs[iSHdr].NumberOfRelocations * 10 > cbFile)) /* IMAGE_RELOCATION */
852 return K_FALSE;
853 if ( paSHdrs[iSHdr].PointerToLinenumbers != 0
854 && ( paSHdrs[iSHdr].PointerToLinenumbers < cbHdrs
855 || paSHdrs[iSHdr].PointerToLinenumbers >= cbFile
856 || paSHdrs[iSHdr].PointerToLinenumbers + paSHdrs[iSHdr].NumberOfLinenumbers * 6 > cbFile)) /* IMAGE_LINENUMBER */
857 return K_FALSE;
858 }
859
860 return K_TRUE;
861}
862
863
864/**
865 * Read the file into memory and parse it.
866 */
867static int kDepObjProcessFile(FILE *pInput)
868{
869 size_t cbFile;
870 KU8 *pbFile;
871 void *pvOpaque;
872 int rc = 0;
873
874 /*
875 * Read the file into memory.
876 */
877 pbFile = (KU8 *)depReadFileIntoMemory(pInput, &cbFile, &pvOpaque);
878 if (!pbFile)
879 return 1;
880
881 /*
882 * See if it's an OMF file, then process it.
883 */
884 if (kDepObjOMFTest(pbFile, cbFile))
885 rc = kDepObjOMFParse(pbFile, cbFile);
886 else if (kDepObjCOFFTest(pbFile, cbFile))
887 rc = kDepObjCOFFParse(pbFile, cbFile);
888 else
889 {
890 fprintf(stderr, "%s: error: Doesn't recognize the header of the OMF file.\n", argv0);
891 rc = 1;
892 }
893
894 depFreeFileMemory(pbFile, pvOpaque);
895 return rc;
896}
897
898
899static void usage(const char *a_argv0)
900{
901 printf("usage: %s -o <output> -t <target> [-fqs] <OMF-file>\n"
902 " or: %s --help\n"
903 " or: %s --version\n",
904 a_argv0, a_argv0, a_argv0);
905}
906
907
908int kmk_builtin_kDepObj(int argc, char *argv[], char **envp)
909{
910 int i;
911
912 /* Arguments. */
913 FILE *pOutput = NULL;
914 const char *pszOutput = NULL;
915 FILE *pInput = NULL;
916 const char *pszTarget = NULL;
917 int fStubs = 0;
918 int fFixCase = 0;
919 /* Argument parsing. */
920 int fInput = 0; /* set when we've found input argument. */
921 int fQuiet = 0;
922
923 argv0 = argv[0];
924
925 /*
926 * Parse arguments.
927 */
928 if (argc <= 1)
929 {
930 usage(argv[0]);
931 return 1;
932 }
933 for (i = 1; i < argc; i++)
934 {
935 if (argv[i][0] == '-')
936 {
937 const char *psz = &argv[i][1];
938 if (*psz == '-')
939 {
940 if (!strcmp(psz, "-quiet"))
941 psz = "q";
942 else if (!strcmp(psz, "-help"))
943 psz = "?";
944 else if (!strcmp(psz, "-version"))
945 psz = "V";
946 }
947
948 switch (*psz)
949 {
950 /*
951 * Output file.
952 */
953 case 'o':
954 {
955 pszOutput = &argv[i][2];
956 if (pOutput)
957 {
958 fprintf(stderr, "%s: syntax error: only one output file!\n", argv[0]);
959 return 1;
960 }
961 if (!*pszOutput)
962 {
963 if (++i >= argc)
964 {
965 fprintf(stderr, "%s: syntax error: The '-o' argument is missing the filename.\n", argv[0]);
966 return 1;
967 }
968 pszOutput = argv[i];
969 }
970 if (pszOutput[0] == '-' && !pszOutput[1])
971 pOutput = stdout;
972 else
973 pOutput = fopen(pszOutput, "w");
974 if (!pOutput)
975 {
976 fprintf(stderr, "%s: error: Failed to create output file '%s'.\n", argv[0], pszOutput);
977 return 1;
978 }
979 break;
980 }
981
982 /*
983 * Target name.
984 */
985 case 't':
986 {
987 if (pszTarget)
988 {
989 fprintf(stderr, "%s: syntax error: only one target!\n", argv[0]);
990 return 1;
991 }
992 pszTarget = &argv[i][2];
993 if (!*pszTarget)
994 {
995 if (++i >= argc)
996 {
997 fprintf(stderr, "%s: syntax error: The '-t' argument is missing the target name.\n", argv[0]);
998 return 1;
999 }
1000 pszTarget = argv[i];
1001 }
1002 break;
1003 }
1004
1005 /*
1006 * Fix case.
1007 */
1008 case 'f':
1009 {
1010 fFixCase = 1;
1011 break;
1012 }
1013
1014 /*
1015 * Quiet.
1016 */
1017 case 'q':
1018 {
1019 fQuiet = 1;
1020 break;
1021 }
1022
1023 /*
1024 * Generate stubs.
1025 */
1026 case 's':
1027 {
1028 fStubs = 1;
1029 break;
1030 }
1031
1032 /*
1033 * The mandatory version & help.
1034 */
1035 case '?':
1036 usage(argv[0]);
1037 return 0;
1038 case 'V':
1039 case 'v':
1040 return kbuild_version(argv[0]);
1041
1042 /*
1043 * Invalid argument.
1044 */
1045 default:
1046 fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);
1047 usage(argv[0]);
1048 return 1;
1049 }
1050 }
1051 else
1052 {
1053 pInput = fopen(argv[i], "rb");
1054 if (!pInput)
1055 {
1056 fprintf(stderr, "%s: error: Failed to open input file '%s'.\n", argv[0], argv[i]);
1057 return 1;
1058 }
1059 fInput = 1;
1060 }
1061
1062 /*
1063 * End of the line?
1064 */
1065 if (fInput)
1066 {
1067 if (++i < argc)
1068 {
1069 fprintf(stderr, "%s: syntax error: No arguments shall follow the input spec.\n", argv[0]);
1070 return 1;
1071 }
1072 break;
1073 }
1074 }
1075
1076 /*
1077 * Got all we require?
1078 */
1079 if (!pInput)
1080 {
1081 fprintf(stderr, "%s: syntax error: No input!\n", argv[0]);
1082 return 1;
1083 }
1084 if (!pOutput)
1085 {
1086 fprintf(stderr, "%s: syntax error: No output!\n", argv[0]);
1087 return 1;
1088 }
1089 if (!pszTarget)
1090 {
1091 fprintf(stderr, "%s: syntax error: No target!\n", argv[0]);
1092 return 1;
1093 }
1094
1095 /*
1096 * Do the parsing.
1097 */
1098 i = kDepObjProcessFile(pInput);
1099 fclose(pInput);
1100
1101 /*
1102 * Write the dependecy file.
1103 */
1104 if (!i)
1105 {
1106 depOptimize(fFixCase, fQuiet);
1107 fprintf(pOutput, "%s:", pszTarget);
1108 depPrint(pOutput);
1109 if (fStubs)
1110 depPrintStubs(pOutput);
1111 }
1112
1113 /*
1114 * Close the output, delete output on failure.
1115 */
1116 if (!i && ferror(pOutput))
1117 {
1118 i = 1;
1119 fprintf(stderr, "%s: error: Error writing to '%s'.\n", argv[0], pszOutput);
1120 }
1121 fclose(pOutput);
1122 if (i)
1123 {
1124 if (unlink(pszOutput))
1125 fprintf(stderr, "%s: warning: failed to remove output file '%s' on failure.\n", argv[0], pszOutput);
1126 }
1127
1128 depCleanup();
1129 return i;
1130}
1131
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette