VirtualBox

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

Last change on this file since 3238 was 3238, checked in by bird, 6 years ago

kDepObj: build fix (debian)

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