VirtualBox

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

Last change on this file since 3192 was 3192, checked in by bird, 7 years ago

kmkbuiltin: funnel output thru output.c (usually via err.c).

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

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