1 | /* $Id: kDepObj.c 2278 2009-02-24 02:19:35Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kDepObj - Extract dependency information from an object file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007-2009 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 | #include "config.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 | #ifdef HAVE_ALLOCA_H
|
---|
38 | # include <alloca.h>
|
---|
39 | #endif
|
---|
40 | #if !defined(_MSC_VER)
|
---|
41 | # include <unistd.h>
|
---|
42 | #else
|
---|
43 | # include <io.h>
|
---|
44 | #endif
|
---|
45 | #include "../../lib/k/kDefs.h"
|
---|
46 | #include "../../lib/k/kTypes.h"
|
---|
47 | #include "../../lib/k/kLdrFmts/pe.h"
|
---|
48 | #include "../../lib/kDep.h"
|
---|
49 | #include "kmkbuiltin.h"
|
---|
50 |
|
---|
51 |
|
---|
52 | /*******************************************************************************
|
---|
53 | * Defined Constants And Macros *
|
---|
54 | *******************************************************************************/
|
---|
55 | /*#define DEBUG*/
|
---|
56 | #define DEBUG
|
---|
57 | #ifdef DEBUG
|
---|
58 | # define dprintf(a) printf a
|
---|
59 | # define dump(pb, cb, offBase) depHexDump(pb,cb,offBase)
|
---|
60 | #else
|
---|
61 | # define dprintf(a) do {} while (0)
|
---|
62 | # define dump(pb, cb, offBase) do {} while (0)
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | /** @name OMF defines
|
---|
66 | * @{ */
|
---|
67 | #define KDEPOMF_THEADR 0x80
|
---|
68 | #define KDEPOMF_LHEADR 0x82
|
---|
69 | #define KDEPOMF_COMENT 0x88
|
---|
70 | #define KDEPOMF_CMTCLS_DEPENDENCY 0xe9
|
---|
71 | #define KDEPOMF_CMTCLS_DBGTYPE 0xa1
|
---|
72 | #define KDEPOMF_LINNUM 0x94
|
---|
73 | #define KDEPOMF_LINNUM32 0x95
|
---|
74 | /** @} */
|
---|
75 |
|
---|
76 |
|
---|
77 | /*******************************************************************************
|
---|
78 | * Structures and Typedefs *
|
---|
79 | *******************************************************************************/
|
---|
80 | /** @name OMF Structures
|
---|
81 | * @{ */
|
---|
82 | #pragma pack(1)
|
---|
83 | /** OMF record header. */
|
---|
84 | typedef struct KDEPOMFHDR
|
---|
85 | {
|
---|
86 | /** The record type. */
|
---|
87 | KU8 bType;
|
---|
88 | /** The size of the record, excluding this header. */
|
---|
89 | KU16 cbRec;
|
---|
90 | } KDEPOMFHDR;
|
---|
91 | typedef KDEPOMFHDR *PKDEPOMFHDR;
|
---|
92 | typedef const KDEPOMFHDR *PCKDEPOMFHDR;
|
---|
93 |
|
---|
94 | /** OMF string. */
|
---|
95 | typedef struct KDEPOMFSTR
|
---|
96 | {
|
---|
97 | KU8 cch;
|
---|
98 | char ach[1];
|
---|
99 | } KDEPOMFSTR;
|
---|
100 | typedef KDEPOMFSTR *PKDEPOMFSTR;
|
---|
101 | typedef const KDEPOMFSTR *PCKDEPOMFSTR;
|
---|
102 |
|
---|
103 | /** THEADR/LHEADR. */
|
---|
104 | typedef struct KDEPOMFTHEADR
|
---|
105 | {
|
---|
106 | KDEPOMFHDR Hdr;
|
---|
107 | KDEPOMFSTR Name;
|
---|
108 | } KDEPOMFTHEADR;
|
---|
109 | typedef KDEPOMFTHEADR *PKDEPOMFTHEADR;
|
---|
110 | typedef const KDEPOMFTHEADR *PCKDEPOMFTHEADR;
|
---|
111 |
|
---|
112 | /** Dependency File. */
|
---|
113 | typedef struct KDEPOMFDEPFILE
|
---|
114 | {
|
---|
115 | KDEPOMFHDR Hdr;
|
---|
116 | KU8 fType;
|
---|
117 | KU8 bClass;
|
---|
118 | KU16 wDosTime;
|
---|
119 | KU16 wDosDate;
|
---|
120 | KDEPOMFSTR Name;
|
---|
121 | } KDEPOMFDEPFILE;
|
---|
122 | typedef KDEPOMFDEPFILE *PKDEPOMFDEPFILE;
|
---|
123 | typedef const KDEPOMFDEPFILE *PCKDEPOMFDEPFILE;
|
---|
124 |
|
---|
125 | #pragma pack()
|
---|
126 | /** @} */
|
---|
127 |
|
---|
128 |
|
---|
129 | /** @name COFF Structures
|
---|
130 | * @{ */
|
---|
131 | #pragma pack(1)
|
---|
132 |
|
---|
133 | typedef struct KDEPCVSYMHDR
|
---|
134 | {
|
---|
135 | /** The record size minus the size field. */
|
---|
136 | KU16 cb;
|
---|
137 | /** The record type. */
|
---|
138 | KU16 uType;
|
---|
139 | } KDEPCVSYMHDR;
|
---|
140 | typedef KDEPCVSYMHDR *PKDEPCVSYMHDR;
|
---|
141 | typedef const KDEPCVSYMHDR *PCKDEPCVSYMHDR;
|
---|
142 |
|
---|
143 | /** @name Selection of KDEPCVSYMHDR::uType values.
|
---|
144 | * @{ */
|
---|
145 | #define K_CV8_S_MSTOOL KU16_C(0x1116)
|
---|
146 | /** @} */
|
---|
147 |
|
---|
148 | typedef struct KDEPCV8SYMHDR
|
---|
149 | {
|
---|
150 | /** The record type. */
|
---|
151 | KU32 uType;
|
---|
152 | /** The record size minus the size field. */
|
---|
153 | KU32 cb;
|
---|
154 | } KDEPCV8SYMHDR;
|
---|
155 | typedef KDEPCV8SYMHDR *PKDEPCV8SYMHDR;
|
---|
156 | typedef const KDEPCV8SYMHDR *PCKDEPCV8SYMHDR;
|
---|
157 |
|
---|
158 | /** @name Known KDEPCV8SYMHDR::uType Values.
|
---|
159 | * @{ */
|
---|
160 | #define K_CV8_SYMBOL_INFO KU32_C(0x000000f1)
|
---|
161 | #define K_CV8_LINE_NUMBERS KU32_C(0x000000f2)
|
---|
162 | #define K_CV8_STRING_TABLE KU32_C(0x000000f3)
|
---|
163 | #define K_CV8_SOURCE_FILES KU32_C(0x000000f4)
|
---|
164 | #define K_CV8_COMDAT_XXXXX KU32_C(0x000000f5) /**< no idea about the format... */
|
---|
165 | /** @} */
|
---|
166 |
|
---|
167 | #pragma pack()
|
---|
168 | /** @} */
|
---|
169 |
|
---|
170 |
|
---|
171 | /*******************************************************************************
|
---|
172 | * Global Variables *
|
---|
173 | *******************************************************************************/
|
---|
174 | /** the executable name. */
|
---|
175 | static const char *argv0 = "";
|
---|
176 | static const char *g_pszFile = NULL;
|
---|
177 |
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Prints an error message.
|
---|
181 | *
|
---|
182 | * @returns rc.
|
---|
183 | * @param rc The return code, for making one line return statements.
|
---|
184 | * @param pszFormat The message format string.
|
---|
185 | * @param ... Format arguments.
|
---|
186 | * @todo Promote this to kDep.c.
|
---|
187 | */
|
---|
188 | static int kDepErr(int rc, const char *pszFormat, ...)
|
---|
189 | {
|
---|
190 | va_list va;
|
---|
191 | const char *psz;
|
---|
192 | const char *pszName = argv0;
|
---|
193 |
|
---|
194 | fflush(stdout);
|
---|
195 |
|
---|
196 | /* The message prefix. */
|
---|
197 | while ((psz = strpbrk(pszName, "/\\:")) != NULL)
|
---|
198 | pszName = psz + 1;
|
---|
199 |
|
---|
200 | if (g_pszFile)
|
---|
201 | fprintf(stderr, "%s: %s: error: ", pszName, g_pszFile);
|
---|
202 | else
|
---|
203 | fprintf(stderr, "%s: error: ", pszName);
|
---|
204 |
|
---|
205 | /* The message. */
|
---|
206 | va_start(va, pszFormat);
|
---|
207 | vfprintf(stderr, pszFormat, va);
|
---|
208 | va_end(va);
|
---|
209 |
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Gets an index from the data.
|
---|
216 | *
|
---|
217 | * @returns The index, KU16_MAX on buffer underflow.
|
---|
218 | * @param puData The current data stream position (in/out).
|
---|
219 | * @param pcbLeft Number of bytes left (in/out).
|
---|
220 | */
|
---|
221 | static KU16 kDepObjOMFGetIndex(KPCUINT *puData, KU16 *pcbLeft)
|
---|
222 | {
|
---|
223 | KU16 u16;
|
---|
224 |
|
---|
225 | if (*pcbLeft >= 1 && *pcbLeft != KU16_MAX)
|
---|
226 | {
|
---|
227 | *pcbLeft -= 1;
|
---|
228 | u16 = *puData->pb++;
|
---|
229 | if (u16 & KU16_C(0x80))
|
---|
230 | {
|
---|
231 | if (*pcbLeft >= 1)
|
---|
232 | {
|
---|
233 | *pcbLeft -= 1;
|
---|
234 | u16 = ((u16 & KU16_C(0x7f)) << 8) | *puData->pb++;
|
---|
235 | }
|
---|
236 | else
|
---|
237 | u16 = KU16_MAX;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | else
|
---|
241 | u16 = KU16_MAX;
|
---|
242 | return u16;
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Parses the OMF file.
|
---|
248 | *
|
---|
249 | * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
|
---|
250 | * @param pbFile The start of the file.
|
---|
251 | * @param cbFile The file size.
|
---|
252 | */
|
---|
253 | int kDepObjOMFParse(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(("%#07x: %#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(1, "%#07x - Bad %cHEADR record, length mismatch.\n",
|
---|
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(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(1, "%#07x - Bad COMMENT record, too small.\n", (const KU8*)pHdr - pbFile);
|
---|
321 | if (uData.pb[0] & 0x3f)
|
---|
322 | return kDepErr(1, "%#07x - Bad COMMENT record, reserved flags set.\n", (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(1, "%#07lx - Bad DEPENDENCY FILE record, length mismatch. (%u/%u)\n",
|
---|
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(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(1, "%#07lx - Bad LINNUM32 record\n", (long)((const KU8 *)pHdr - pbFile));
|
---|
394 |
|
---|
395 | if (uLinNumType == KU8_MAX)
|
---|
396 | {
|
---|
397 | static const char * const s_apsz[5] =
|
---|
398 | {
|
---|
399 | "source file", "listing file", "source & listing file", "file names table", "path table"
|
---|
400 | };
|
---|
401 | KU16 uLine;
|
---|
402 | KU8 uReserved;
|
---|
403 | KU16 uSeg2;
|
---|
404 | KU32 cbLinNames;
|
---|
405 |
|
---|
406 | if (cbRecLeft < 2+1+1+2+2+4)
|
---|
407 | return kDepErr(1, "%#07lx - Bad LINNUM32 record, too short\n", (long)((const KU8 *)pHdr - pbFile));
|
---|
408 | cbRecLeft -= 2+1+1+2+2+4;
|
---|
409 | uLine = *uData.pu16++;
|
---|
410 | uLinNumType = *uData.pu8++;
|
---|
411 | uReserved = *uData.pu8++;
|
---|
412 | cLinNums = *uData.pu16++;
|
---|
413 | uSeg2 = *uData.pu16++;
|
---|
414 | cbLinNames = *uData.pu32++;
|
---|
415 |
|
---|
416 | dprintf(("LINNUM32: uGrp=%#x uSeg=%#x uSeg2=%#x uLine=%#x (MBZ) uReserved=%#x\n",
|
---|
417 | uGrp, uSeg, uSeg2, uLine, uReserved));
|
---|
418 | dprintf(("LINNUM32: cLinNums=%#x (%u) cbLinNames=%#x (%u) uLinNumType=%#x (%s)\n",
|
---|
419 | cLinNums, cLinNums, cbLinNames, cbLinNames, uLinNumType,
|
---|
420 | uLinNumType < K_ELEMENTS(s_apsz) ? s_apsz[uLinNumType] : "??"));
|
---|
421 | if (uLine != 0)
|
---|
422 | return kDepErr(1, "%#07lx - Bad LINNUM32 record, line %#x (MBZ)\n", (long)((const KU8 *)pHdr - pbFile), uLine);
|
---|
423 | cLinFiles = iLinFile = KU32_MAX;
|
---|
424 | if ( uLinNumType == 3 /* file names table */
|
---|
425 | || uLinNumType == 4 /* path table */)
|
---|
426 | cLinNums = 0; /* no line numbers */
|
---|
427 | else if (uLinNumType > 4)
|
---|
428 | return kDepErr(1, "%#07lx - Bad LINNUM32 record, type %#x unknown\n", (long)((const KU8 *)pHdr - pbFile), uLinNumType);
|
---|
429 | }
|
---|
430 | else
|
---|
431 | dprintf(("LINNUM32: uGrp=%#x uSeg=%#x\n", uGrp, uSeg));
|
---|
432 |
|
---|
433 |
|
---|
434 | /* Skip file numbers (we parse them to follow the stream correctly). */
|
---|
435 | if (uLinNumType != 3 && uLinNumType != 4)
|
---|
436 | {
|
---|
437 | static const unsigned s_acbTypes[3] = { 2+2+4, 4+4+4, 2+2+4+4+4 };
|
---|
438 | unsigned cbEntry = s_acbTypes[uLinNumType];
|
---|
439 |
|
---|
440 | while (cLinNums && cbRecLeft)
|
---|
441 | {
|
---|
442 | if (cbRecLeft < cbEntry)
|
---|
443 | return kDepErr(1, "%#07lx - Bad LINNUM32 record, incomplete line entry\n", (long)((const KU8 *)pHdr - pbFile));
|
---|
444 |
|
---|
445 | switch (uLinNumType)
|
---|
446 | {
|
---|
447 | case 0: /* source file */
|
---|
448 | dprintf((" Line %6" KU16_PRI " of file %2" KU16_PRI " at %#010" KX32_PRI "\n",
|
---|
449 | uData.pu16[0], uData.pu16[1], uData.pu32[1]));
|
---|
450 | break;
|
---|
451 | case 1: /* listing file */
|
---|
452 | dprintf((" Line %6" KU32_PRI ", statement %6" KU32_PRI " at %#010" KX32_PRI "\n",
|
---|
453 | uData.pu32[0], uData.pu32[1], uData.pu32[2]));
|
---|
454 | break;
|
---|
455 | case 2: /* source & listing file */
|
---|
456 | dprintf((" Line %6" KU16_PRI " of file %2" KU16_PRI ", listning line %6" KU32_PRI ", statement %6" KU32_PRI " at %#010" KX32_PRI "\n",
|
---|
457 | uData.pu16[0], uData.pu16[1], uData.pu32[1], uData.pu32[2], uData.pu32[3]));
|
---|
458 | break;
|
---|
459 | }
|
---|
460 | uData.pb += cbEntry;
|
---|
461 | cbRecLeft -= cbEntry;
|
---|
462 | cLinNums--;
|
---|
463 | }
|
---|
464 |
|
---|
465 | /* If at end of the announced line number entiries, we may find a file names table
|
---|
466 | here (who is actually emitting this?). */
|
---|
467 | if (!cLinNums)
|
---|
468 | {
|
---|
469 | uLinNumType = cbRecLeft > 0 ? 3 : KU8_MAX;
|
---|
470 | dprintf(("End-of-line-numbers; uLinNumType=%u cbRecLeft=%#x\n", uLinNumType, cbRecLeft));
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | if (uLinNumType == 3 || uLinNumType == 4)
|
---|
475 | {
|
---|
476 | /* Read the file/path table header (first time only). */
|
---|
477 | if (cLinFiles == KU32_MAX && iLinFile == KU32_MAX)
|
---|
478 | {
|
---|
479 | KU32 iFirstCol;
|
---|
480 | KU32 cCols;
|
---|
481 |
|
---|
482 | if (cbRecLeft < 4+4+4)
|
---|
483 | return kDepErr(1, "%#07lx - Bad LINNUM32 record, incomplete file/path table header\n", (long)((const KU8 *)pHdr - pbFile));
|
---|
484 | cbRecLeft -= 4+4+4;
|
---|
485 |
|
---|
486 | iFirstCol = *uData.pu32++;
|
---|
487 | cCols = *uData.pu32++;
|
---|
488 | cLinFiles = *uData.pu32++;
|
---|
489 | dprintf(("%s table header: cLinFiles=%#" KX32_PRI " (%" KU32_PRI ") iFirstCol=%" KU32_PRI " cCols=%" KU32_PRI"\n",
|
---|
490 | uLinNumType == 3 ? "file names" : "path", cLinFiles, cLinFiles, iFirstCol, cCols));
|
---|
491 | if (cLinFiles == KU32_MAX)
|
---|
492 | return kDepErr(1, "%#07lx - Bad LINNUM32 record, too many file/path table entries.\n", (long)((const KU8 *)pHdr - pbFile));
|
---|
493 | iLinFile = 0;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /* Parse the file names / path table. */
|
---|
497 | while (iLinFile < cLinFiles && cbRecLeft)
|
---|
498 | {
|
---|
499 | int cbName = *uData.pb++;
|
---|
500 | if (cbRecLeft < 1 + cbName)
|
---|
501 | return kDepErr(1, "%#07lx - Bad LINNUM32 record, file/path table entry too long.\n", (long)((const KU8 *)pHdr - pbFile));
|
---|
502 | iLinFile++;
|
---|
503 | dprintf(("#%" KU32_PRI": %.*s\n", iLinFile, cbName, uData.pch));
|
---|
504 | if (uLinNumType == 3)
|
---|
505 | {
|
---|
506 | depAdd(uData.pch, cbName);
|
---|
507 | iSrc++;
|
---|
508 | }
|
---|
509 | cbRecLeft -= 1 + cbName;
|
---|
510 | uData.pb += cbName;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /* The end? */
|
---|
514 | if (iLinFile == cLinFiles)
|
---|
515 | {
|
---|
516 | uLinNumType = KU8_MAX;
|
---|
517 | dprintf(("End-of-file/path-table; cbRecLeft=%#x\n", cbRecLeft));
|
---|
518 | }
|
---|
519 | }
|
---|
520 | }
|
---|
521 | else
|
---|
522 | dprintf(("LINNUM32: Unknown or unsupported format\n"));
|
---|
523 | break;
|
---|
524 |
|
---|
525 | }
|
---|
526 |
|
---|
527 | /* advance */
|
---|
528 | cbLeft -= pHdr->cbRec + sizeof(*pHdr);
|
---|
529 | pHdr = (PCKDEPOMFHDR)((const KU8 *)(pHdr + 1) + pHdr->cbRec);
|
---|
530 | }
|
---|
531 |
|
---|
532 | if (cbLeft)
|
---|
533 | return kDepErr(1, "%#07x - Unexpected EOF. cbLeft=%#x\n", (const KU8*)pHdr - pbFile, cbLeft);
|
---|
534 |
|
---|
535 | if (iSrc == 0 && iMaybeSrc <= 1)
|
---|
536 | {
|
---|
537 | dprintf(("kDepObjOMFParse: No cylindrical smoking thing: iSrc=0 iMaybeSrc=%#" KX32_PRI"\n", iMaybeSrc));
|
---|
538 | return 2;
|
---|
539 | }
|
---|
540 | dprintf(("kDepObjOMFParse: iSrc=%" KU32_PRI " iMaybeSrc=%#" KX32_PRI "\n", iSrc, iMaybeSrc));
|
---|
541 | return 0;
|
---|
542 | }
|
---|
543 |
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Checks if this file is an OMF file or not.
|
---|
547 | *
|
---|
548 | * @returns K_TRUE if it's OMF, K_FALSE otherwise.
|
---|
549 | *
|
---|
550 | * @param pb The start of the file.
|
---|
551 | * @param cb The file size.
|
---|
552 | */
|
---|
553 | KBOOL kDepObjOMFTest(const KU8 *pbFile, KSIZE cbFile)
|
---|
554 | {
|
---|
555 | PCKDEPOMFTHEADR pHdr = (PCKDEPOMFTHEADR)pbFile;
|
---|
556 |
|
---|
557 | if (cbFile <= sizeof(*pHdr))
|
---|
558 | return K_FALSE;
|
---|
559 | if ( pHdr->Hdr.bType != KDEPOMF_THEADR
|
---|
560 | && pHdr->Hdr.bType != KDEPOMF_LHEADR)
|
---|
561 | return K_FALSE;
|
---|
562 | if (pHdr->Hdr.cbRec + sizeof(pHdr->Hdr) >= cbFile)
|
---|
563 | return K_FALSE;
|
---|
564 | if (pHdr->Hdr.cbRec != 1 + pHdr->Name.cch + 1)
|
---|
565 | return K_FALSE;
|
---|
566 |
|
---|
567 | return K_TRUE;
|
---|
568 | }
|
---|
569 |
|
---|
570 |
|
---|
571 | /**
|
---|
572 | * Parses a CodeView 8 symbol section.
|
---|
573 | *
|
---|
574 | * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
|
---|
575 | * @param pbSyms Pointer to the start of the symbol section.
|
---|
576 | * @param cbSyms Size of the symbol section.
|
---|
577 | */
|
---|
578 | int kDepObjCOFFParseCV8SymbolSection(const KU8 *pbSyms, KSIZE cbSyms)
|
---|
579 | {
|
---|
580 | char const * pchStrTab = NULL;
|
---|
581 | KU32 cbStrTab = 0;
|
---|
582 | KPCUINT uSrcFiles = {0};
|
---|
583 | KU32 cbSrcFiles = 0;
|
---|
584 | KU32 off = 4;
|
---|
585 | KU32 iSrc = 0;
|
---|
586 |
|
---|
587 | if (cbSyms < 16)
|
---|
588 | return 1;
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * The parsing loop.
|
---|
592 | */
|
---|
593 | while (off < cbSyms)
|
---|
594 | {
|
---|
595 | PCKDEPCV8SYMHDR pHdr = (PCKDEPCV8SYMHDR)(pbSyms + off);
|
---|
596 | KPCUINT uData;
|
---|
597 | KU32 cbData;
|
---|
598 | uData.pv = pHdr + 1;
|
---|
599 |
|
---|
600 | if (off + sizeof(*pHdr) >= cbSyms)
|
---|
601 | {
|
---|
602 | fprintf(stderr, "%s: CV symbol table entry at %08" KX32_PRI " is too long; cbSyms=%#" KSIZE_PRI "\n",
|
---|
603 | argv0, off, cbSyms);
|
---|
604 | return 1; /* FIXME */
|
---|
605 | }
|
---|
606 |
|
---|
607 | cbData = pHdr->cb;
|
---|
608 | if (off + cbData + sizeof(*pHdr) > cbSyms)
|
---|
609 | {
|
---|
610 | fprintf(stderr, "%s: CV symbol table entry at %08" KX32_PRI " is too long; cbData=%#" KX32_PRI " cbSyms=%#" KSIZE_PRI "\n",
|
---|
611 | argv0, off, cbData, cbSyms);
|
---|
612 | return 1; /* FIXME */
|
---|
613 | }
|
---|
614 |
|
---|
615 | /* If the size is 0, assume it covers the rest of the section. VC++ 2003 has
|
---|
616 | been observed doing thing. */
|
---|
617 | if (!cbData)
|
---|
618 | cbData = cbSyms - off;
|
---|
619 |
|
---|
620 | switch (pHdr->uType)
|
---|
621 | {
|
---|
622 | case K_CV8_SYMBOL_INFO:
|
---|
623 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Symbol Info\n", off, cbData));
|
---|
624 | /*dump(uData.pb, cbData, 0);*/
|
---|
625 | break;
|
---|
626 |
|
---|
627 | case K_CV8_LINE_NUMBERS:
|
---|
628 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Line numbers\n", off, cbData));
|
---|
629 | /*dump(uData.pb, cbData, 0);*/
|
---|
630 | break;
|
---|
631 |
|
---|
632 | case K_CV8_STRING_TABLE:
|
---|
633 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": String table\n", off, cbData));
|
---|
634 | if (pchStrTab)
|
---|
635 | fprintf(stderr, "%s: warning: Found yet another string table!\n", argv0);
|
---|
636 | pchStrTab = uData.pch;
|
---|
637 | cbStrTab = cbData;
|
---|
638 | /*dump(uData.pb, cbData, 0);*/
|
---|
639 | break;
|
---|
640 |
|
---|
641 | case K_CV8_SOURCE_FILES:
|
---|
642 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Source files\n", off, cbData));
|
---|
643 | if (uSrcFiles.pb)
|
---|
644 | fprintf(stderr, "%s: warning: Found yet another source files table!\n", argv0);
|
---|
645 | uSrcFiles = uData;
|
---|
646 | cbSrcFiles = cbData;
|
---|
647 | /*dump(uData.pb, cbData, 0);*/
|
---|
648 | break;
|
---|
649 |
|
---|
650 | case K_CV8_COMDAT_XXXXX:
|
---|
651 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": 0xf5 Unknown COMDAT stuff\n", off, cbData));
|
---|
652 | /*dump(uData.pb, cbData, 0);*/
|
---|
653 | break;
|
---|
654 |
|
---|
655 | default:
|
---|
656 | dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Unknown type %#" KX32_PRI "\n",
|
---|
657 | off, cbData, pHdr->uType));
|
---|
658 | dump(uData.pb, cbData, 0);
|
---|
659 | break;
|
---|
660 | }
|
---|
661 |
|
---|
662 | /* next */
|
---|
663 | cbData = (cbData + 3) & ~KU32_C(3);
|
---|
664 | off += cbData + sizeof(*pHdr);
|
---|
665 | }
|
---|
666 |
|
---|
667 | /*
|
---|
668 | * Did we find any source files and strings?
|
---|
669 | */
|
---|
670 | if (!pchStrTab || !uSrcFiles.pv)
|
---|
671 | {
|
---|
672 | dprintf(("kDepObjCOFFParseCV8SymbolSection: No cylindrical smoking thing: pchStrTab=%p uSrcFiles.pv=%p\n", pchStrTab, uSrcFiles.pv));
|
---|
673 | return 2;
|
---|
674 | }
|
---|
675 |
|
---|
676 | /*
|
---|
677 | * Iterate the source file table.
|
---|
678 | */
|
---|
679 | off = 0;
|
---|
680 | while (off < cbSrcFiles)
|
---|
681 | {
|
---|
682 | KU32 offFile;
|
---|
683 | const char *pszFile;
|
---|
684 | KSIZE cchFile;
|
---|
685 | KU16 u16Type;
|
---|
686 | KPCUINT uSrc;
|
---|
687 | KU32 cbSrc;
|
---|
688 |
|
---|
689 | /*
|
---|
690 | * Validate and parse the entry (variable length record are fun).
|
---|
691 | */
|
---|
692 | if (off + 8 > cbSrcFiles)
|
---|
693 | {
|
---|
694 | fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is too long; cbSrcFiles=%#" KSIZE_PRI "\n",
|
---|
695 | argv0, off, cbSrcFiles);
|
---|
696 | return 1;
|
---|
697 | }
|
---|
698 | uSrc.pb = uSrcFiles.pb + off;
|
---|
699 | u16Type = uSrc.pu16[2];
|
---|
700 | cbSrc = u16Type == 0x0110 ? 6 + 16 + 2 : 6 + 2;
|
---|
701 | if (off + cbSrc > cbSrcFiles)
|
---|
702 | {
|
---|
703 | fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is too long; cbSrc=%#" KX32_PRI " cbSrcFiles=%#" KSIZE_PRI "\n",
|
---|
704 | argv0, off, cbSrc, cbSrcFiles);
|
---|
705 | return 1;
|
---|
706 | }
|
---|
707 |
|
---|
708 | offFile = *uSrc.pu32;
|
---|
709 | if (offFile > cbStrTab)
|
---|
710 | {
|
---|
711 | fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is out side the string table; offFile=%#" KX32_PRI " cbStrTab=%#" KX32_PRI "\n",
|
---|
712 | argv0, off, offFile, cbStrTab);
|
---|
713 | return 1;
|
---|
714 | }
|
---|
715 | pszFile = pchStrTab + offFile;
|
---|
716 | cchFile = strlen(pszFile);
|
---|
717 | if (cchFile == 0)
|
---|
718 | {
|
---|
719 | fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " has an empty file name; offFile=%#x" KX32_PRI "\n",
|
---|
720 | argv0, off, offFile);
|
---|
721 | return 1;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /*
|
---|
725 | * Display the result and add it to the dependency database.
|
---|
726 | */
|
---|
727 | depAdd(pszFile, cchFile);
|
---|
728 | if (u16Type == 0x0110)
|
---|
729 | dprintf(("#%03" KU32_PRI ": {todo-md5-todo} '%s'\n",
|
---|
730 | iSrc, pszFile));
|
---|
731 | else
|
---|
732 | dprintf(("#%03" KU32_PRI ": type=%#06" KX16_PRI " '%s'\n", iSrc, u16Type, pszFile));
|
---|
733 |
|
---|
734 |
|
---|
735 | /* next */
|
---|
736 | iSrc++;
|
---|
737 | off += cbSrc;
|
---|
738 | }
|
---|
739 |
|
---|
740 | if (iSrc == 0)
|
---|
741 | {
|
---|
742 | dprintf(("kDepObjCOFFParseCV8SymbolSection: No cylindrical smoking thing: iSrc=0\n"));
|
---|
743 | return 2;
|
---|
744 | }
|
---|
745 | dprintf(("kDepObjCOFFParseCV8SymbolSection: iSrc=%" KU32_PRI "\n", iSrc));
|
---|
746 | return 0;
|
---|
747 | }
|
---|
748 |
|
---|
749 |
|
---|
750 | /**
|
---|
751 | * Parses the OMF file.
|
---|
752 | *
|
---|
753 | * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
|
---|
754 | * @param pbFile The start of the file.
|
---|
755 | * @param cbFile The file size.
|
---|
756 | */
|
---|
757 | int kDepObjCOFFParse(const KU8 *pbFile, KSIZE cbFile)
|
---|
758 | {
|
---|
759 | IMAGE_FILE_HEADER const *pFileHdr = (IMAGE_FILE_HEADER const *)pbFile;
|
---|
760 | IMAGE_SECTION_HEADER const *paSHdrs = (IMAGE_SECTION_HEADER const *)((KU8 const *)(pFileHdr + 1) + pFileHdr->SizeOfOptionalHeader);
|
---|
761 | unsigned cSHdrs = pFileHdr->NumberOfSections;
|
---|
762 | unsigned iSHdr;
|
---|
763 | KPCUINT u;
|
---|
764 | int rcRet = 2;
|
---|
765 | int rc;
|
---|
766 |
|
---|
767 | printf("COFF file!\n");
|
---|
768 |
|
---|
769 | for (iSHdr = 0; iSHdr < cSHdrs; iSHdr++)
|
---|
770 | {
|
---|
771 | if ( !memcmp(paSHdrs[iSHdr].Name, ".debug$S", sizeof(".debug$S") - 1)
|
---|
772 | && paSHdrs[iSHdr].SizeOfRawData > 4)
|
---|
773 | {
|
---|
774 | u.pb = pbFile + paSHdrs[iSHdr].PointerToRawData;
|
---|
775 | dprintf(("CV symbol table: version=%x\n", *u.pu32));
|
---|
776 | if (*u.pu32 == 0x000000004)
|
---|
777 | rc = kDepObjCOFFParseCV8SymbolSection(u.pb, paSHdrs[iSHdr].SizeOfRawData);
|
---|
778 | else
|
---|
779 | rc = 2;
|
---|
780 | dprintf(("rc=%d\n", rc));
|
---|
781 | if (rcRet == 2)
|
---|
782 | rcRet = rc;
|
---|
783 | if (rcRet != 2)
|
---|
784 | return rc;
|
---|
785 | }
|
---|
786 | printf("#%d: %.8s\n", iSHdr, paSHdrs[iSHdr].Name);
|
---|
787 | }
|
---|
788 | return rcRet;
|
---|
789 | }
|
---|
790 |
|
---|
791 |
|
---|
792 | /**
|
---|
793 | * Checks if this file is a COFF file or not.
|
---|
794 | *
|
---|
795 | * @returns K_TRUE if it's COFF, K_FALSE otherwise.
|
---|
796 | *
|
---|
797 | * @param pb The start of the file.
|
---|
798 | * @param cb The file size.
|
---|
799 | */
|
---|
800 | KBOOL kDepObjCOFFTest(const KU8 *pbFile, KSIZE cbFile)
|
---|
801 | {
|
---|
802 | IMAGE_FILE_HEADER const *pFileHdr = (IMAGE_FILE_HEADER const *)pbFile;
|
---|
803 | IMAGE_SECTION_HEADER const *paSHdrs = (IMAGE_SECTION_HEADER const *)((KU8 const *)(pFileHdr + 1) + pFileHdr->SizeOfOptionalHeader);
|
---|
804 | unsigned cSHdrs = pFileHdr->NumberOfSections;
|
---|
805 | unsigned iSHdr;
|
---|
806 | KSIZE cbHdrs = (const KU8 *)&paSHdrs[cSHdrs] - (const KU8 *)pbFile;
|
---|
807 |
|
---|
808 | if (cbFile <= sizeof(*pFileHdr))
|
---|
809 | return K_FALSE;
|
---|
810 | if ( pFileHdr->Machine != IMAGE_FILE_MACHINE_I386
|
---|
811 | && pFileHdr->Machine != IMAGE_FILE_MACHINE_AMD64)
|
---|
812 | return K_FALSE;
|
---|
813 | if (pFileHdr->SizeOfOptionalHeader != 0)
|
---|
814 | return K_FALSE; /* COFF files doesn't have an optional header */
|
---|
815 |
|
---|
816 | if ( pFileHdr->NumberOfSections <= 1
|
---|
817 | || pFileHdr->NumberOfSections > cbFile)
|
---|
818 | return K_FALSE;
|
---|
819 |
|
---|
820 | if (cbHdrs >= cbFile)
|
---|
821 | return K_FALSE;
|
---|
822 |
|
---|
823 | if ( pFileHdr->PointerToSymbolTable != 0
|
---|
824 | && ( pFileHdr->PointerToSymbolTable < cbHdrs
|
---|
825 | || pFileHdr->PointerToSymbolTable > cbFile))
|
---|
826 | return K_FALSE;
|
---|
827 | if ( pFileHdr->PointerToSymbolTable == 0
|
---|
828 | && pFileHdr->NumberOfSymbols != 0)
|
---|
829 | return K_FALSE;
|
---|
830 | if ( pFileHdr->Characteristics
|
---|
831 | & ( IMAGE_FILE_DLL
|
---|
832 | | IMAGE_FILE_SYSTEM
|
---|
833 | | IMAGE_FILE_UP_SYSTEM_ONLY
|
---|
834 | | IMAGE_FILE_NET_RUN_FROM_SWAP
|
---|
835 | | IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP
|
---|
836 | | IMAGE_FILE_EXECUTABLE_IMAGE
|
---|
837 | | IMAGE_FILE_RELOCS_STRIPPED))
|
---|
838 | return K_FALSE;
|
---|
839 |
|
---|
840 | for (iSHdr = 0; iSHdr < cSHdrs; iSHdr++)
|
---|
841 | {
|
---|
842 | if ( paSHdrs[iSHdr].PointerToRawData != 0
|
---|
843 | && ( paSHdrs[iSHdr].PointerToRawData < cbHdrs
|
---|
844 | || paSHdrs[iSHdr].PointerToRawData >= cbFile
|
---|
845 | || paSHdrs[iSHdr].PointerToRawData + paSHdrs[iSHdr].SizeOfRawData > cbFile))
|
---|
846 | return K_FALSE;
|
---|
847 | if ( paSHdrs[iSHdr].PointerToRelocations != 0
|
---|
848 | && ( paSHdrs[iSHdr].PointerToRelocations < cbHdrs
|
---|
849 | || paSHdrs[iSHdr].PointerToRelocations >= cbFile
|
---|
850 | || paSHdrs[iSHdr].PointerToRelocations + paSHdrs[iSHdr].NumberOfRelocations * 10 > cbFile)) /* IMAGE_RELOCATION */
|
---|
851 | return K_FALSE;
|
---|
852 | if ( paSHdrs[iSHdr].PointerToLinenumbers != 0
|
---|
853 | && ( paSHdrs[iSHdr].PointerToLinenumbers < cbHdrs
|
---|
854 | || paSHdrs[iSHdr].PointerToLinenumbers >= cbFile
|
---|
855 | || paSHdrs[iSHdr].PointerToLinenumbers + paSHdrs[iSHdr].NumberOfLinenumbers * 6 > cbFile)) /* IMAGE_LINENUMBER */
|
---|
856 | return K_FALSE;
|
---|
857 | }
|
---|
858 |
|
---|
859 | return K_TRUE;
|
---|
860 | }
|
---|
861 |
|
---|
862 |
|
---|
863 | /**
|
---|
864 | * Read the file into memory and parse it.
|
---|
865 | */
|
---|
866 | static int kDepObjProcessFile(FILE *pInput)
|
---|
867 | {
|
---|
868 | size_t cbFile;
|
---|
869 | KU8 *pbFile;
|
---|
870 | void *pvOpaque;
|
---|
871 | int rc = 0;
|
---|
872 |
|
---|
873 | /*
|
---|
874 | * Read the file into memory.
|
---|
875 | */
|
---|
876 | pbFile = (KU8 *)depReadFileIntoMemory(pInput, &cbFile, &pvOpaque);
|
---|
877 | if (!pbFile)
|
---|
878 | return 1;
|
---|
879 |
|
---|
880 | /*
|
---|
881 | * See if it's an OMF file, then process it.
|
---|
882 | */
|
---|
883 | if (kDepObjOMFTest(pbFile, cbFile))
|
---|
884 | rc = kDepObjOMFParse(pbFile, cbFile);
|
---|
885 | else if (kDepObjCOFFTest(pbFile, cbFile))
|
---|
886 | rc = kDepObjCOFFParse(pbFile, cbFile);
|
---|
887 | else
|
---|
888 | {
|
---|
889 | fprintf(stderr, "%s: error: Doesn't recognize the header of the OMF file.\n", argv0);
|
---|
890 | rc = 1;
|
---|
891 | }
|
---|
892 |
|
---|
893 | depFreeFileMemory(pbFile, pvOpaque);
|
---|
894 | return rc;
|
---|
895 | }
|
---|
896 |
|
---|
897 |
|
---|
898 | static void usage(const char *a_argv0)
|
---|
899 | {
|
---|
900 | printf("usage: %s -o <output> -t <target> [-fqs] <OMF-file>\n"
|
---|
901 | " or: %s --help\n"
|
---|
902 | " or: %s --version\n",
|
---|
903 | a_argv0, a_argv0, a_argv0);
|
---|
904 | }
|
---|
905 |
|
---|
906 |
|
---|
907 | int kmk_builtin_kDepObj(int argc, char *argv[], char **envp)
|
---|
908 | {
|
---|
909 | int i;
|
---|
910 |
|
---|
911 | /* Arguments. */
|
---|
912 | FILE *pOutput = NULL;
|
---|
913 | const char *pszOutput = NULL;
|
---|
914 | FILE *pInput = NULL;
|
---|
915 | const char *pszTarget = NULL;
|
---|
916 | int fStubs = 0;
|
---|
917 | int fFixCase = 0;
|
---|
918 | /* Argument parsing. */
|
---|
919 | int fInput = 0; /* set when we've found input argument. */
|
---|
920 | int fQuiet = 0;
|
---|
921 |
|
---|
922 | argv0 = argv[0];
|
---|
923 |
|
---|
924 | /*
|
---|
925 | * Parse arguments.
|
---|
926 | */
|
---|
927 | if (argc <= 1)
|
---|
928 | {
|
---|
929 | usage(argv[0]);
|
---|
930 | return 1;
|
---|
931 | }
|
---|
932 | for (i = 1; i < argc; i++)
|
---|
933 | {
|
---|
934 | if (argv[i][0] == '-')
|
---|
935 | {
|
---|
936 | const char *psz = &argv[i][1];
|
---|
937 | if (*psz == '-')
|
---|
938 | {
|
---|
939 | if (!strcmp(psz, "-quiet"))
|
---|
940 | psz = "q";
|
---|
941 | else if (!strcmp(psz, "-help"))
|
---|
942 | psz = "?";
|
---|
943 | else if (!strcmp(psz, "-version"))
|
---|
944 | psz = "V";
|
---|
945 | }
|
---|
946 |
|
---|
947 | switch (*psz)
|
---|
948 | {
|
---|
949 | /*
|
---|
950 | * Output file.
|
---|
951 | */
|
---|
952 | case 'o':
|
---|
953 | {
|
---|
954 | pszOutput = &argv[i][2];
|
---|
955 | if (pOutput)
|
---|
956 | {
|
---|
957 | fprintf(stderr, "%s: syntax error: only one output file!\n", argv[0]);
|
---|
958 | return 1;
|
---|
959 | }
|
---|
960 | if (!*pszOutput)
|
---|
961 | {
|
---|
962 | if (++i >= argc)
|
---|
963 | {
|
---|
964 | fprintf(stderr, "%s: syntax error: The '-o' argument is missing the filename.\n", argv[0]);
|
---|
965 | return 1;
|
---|
966 | }
|
---|
967 | pszOutput = argv[i];
|
---|
968 | }
|
---|
969 | if (pszOutput[0] == '-' && !pszOutput[1])
|
---|
970 | pOutput = stdout;
|
---|
971 | else
|
---|
972 | pOutput = fopen(pszOutput, "w");
|
---|
973 | if (!pOutput)
|
---|
974 | {
|
---|
975 | fprintf(stderr, "%s: error: Failed to create output file '%s'.\n", argv[0], pszOutput);
|
---|
976 | return 1;
|
---|
977 | }
|
---|
978 | break;
|
---|
979 | }
|
---|
980 |
|
---|
981 | /*
|
---|
982 | * Target name.
|
---|
983 | */
|
---|
984 | case 't':
|
---|
985 | {
|
---|
986 | if (pszTarget)
|
---|
987 | {
|
---|
988 | fprintf(stderr, "%s: syntax error: only one target!\n", argv[0]);
|
---|
989 | return 1;
|
---|
990 | }
|
---|
991 | pszTarget = &argv[i][2];
|
---|
992 | if (!*pszTarget)
|
---|
993 | {
|
---|
994 | if (++i >= argc)
|
---|
995 | {
|
---|
996 | fprintf(stderr, "%s: syntax error: The '-t' argument is missing the target name.\n", argv[0]);
|
---|
997 | return 1;
|
---|
998 | }
|
---|
999 | pszTarget = argv[i];
|
---|
1000 | }
|
---|
1001 | break;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /*
|
---|
1005 | * Fix case.
|
---|
1006 | */
|
---|
1007 | case 'f':
|
---|
1008 | {
|
---|
1009 | fFixCase = 1;
|
---|
1010 | break;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | /*
|
---|
1014 | * Quiet.
|
---|
1015 | */
|
---|
1016 | case 'q':
|
---|
1017 | {
|
---|
1018 | fQuiet = 1;
|
---|
1019 | break;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | /*
|
---|
1023 | * Generate stubs.
|
---|
1024 | */
|
---|
1025 | case 's':
|
---|
1026 | {
|
---|
1027 | fStubs = 1;
|
---|
1028 | break;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | /*
|
---|
1032 | * The mandatory version & help.
|
---|
1033 | */
|
---|
1034 | case '?':
|
---|
1035 | usage(argv[0]);
|
---|
1036 | return 0;
|
---|
1037 | case 'V':
|
---|
1038 | case 'v':
|
---|
1039 | return kbuild_version(argv[0]);
|
---|
1040 |
|
---|
1041 | /*
|
---|
1042 | * Invalid argument.
|
---|
1043 | */
|
---|
1044 | default:
|
---|
1045 | fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);
|
---|
1046 | usage(argv[0]);
|
---|
1047 | return 1;
|
---|
1048 | }
|
---|
1049 | }
|
---|
1050 | else
|
---|
1051 | {
|
---|
1052 | pInput = fopen(argv[i], "rb");
|
---|
1053 | if (!pInput)
|
---|
1054 | {
|
---|
1055 | fprintf(stderr, "%s: error: Failed to open input file '%s'.\n", argv[0], argv[i]);
|
---|
1056 | return 1;
|
---|
1057 | }
|
---|
1058 | fInput = 1;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | /*
|
---|
1062 | * End of the line?
|
---|
1063 | */
|
---|
1064 | if (fInput)
|
---|
1065 | {
|
---|
1066 | if (++i < argc)
|
---|
1067 | {
|
---|
1068 | fprintf(stderr, "%s: syntax error: No arguments shall follow the input spec.\n", argv[0]);
|
---|
1069 | return 1;
|
---|
1070 | }
|
---|
1071 | break;
|
---|
1072 | }
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | /*
|
---|
1076 | * Got all we require?
|
---|
1077 | */
|
---|
1078 | if (!pInput)
|
---|
1079 | {
|
---|
1080 | fprintf(stderr, "%s: syntax error: No input!\n", argv[0]);
|
---|
1081 | return 1;
|
---|
1082 | }
|
---|
1083 | if (!pOutput)
|
---|
1084 | {
|
---|
1085 | fprintf(stderr, "%s: syntax error: No output!\n", argv[0]);
|
---|
1086 | return 1;
|
---|
1087 | }
|
---|
1088 | if (!pszTarget)
|
---|
1089 | {
|
---|
1090 | fprintf(stderr, "%s: syntax error: No target!\n", argv[0]);
|
---|
1091 | return 1;
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | /*
|
---|
1095 | * Do the parsing.
|
---|
1096 | */
|
---|
1097 | i = kDepObjProcessFile(pInput);
|
---|
1098 | fclose(pInput);
|
---|
1099 |
|
---|
1100 | /*
|
---|
1101 | * Write the dependecy file.
|
---|
1102 | */
|
---|
1103 | if (!i)
|
---|
1104 | {
|
---|
1105 | depOptimize(fFixCase, fQuiet);
|
---|
1106 | fprintf(pOutput, "%s:", pszTarget);
|
---|
1107 | depPrint(pOutput);
|
---|
1108 | if (fStubs)
|
---|
1109 | depPrintStubs(pOutput);
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | /*
|
---|
1113 | * Close the output, delete output on failure.
|
---|
1114 | */
|
---|
1115 | if (!i && ferror(pOutput))
|
---|
1116 | {
|
---|
1117 | i = 1;
|
---|
1118 | fprintf(stderr, "%s: error: Error writing to '%s'.\n", argv[0], pszOutput);
|
---|
1119 | }
|
---|
1120 | fclose(pOutput);
|
---|
1121 | if (i)
|
---|
1122 | {
|
---|
1123 | if (unlink(pszOutput))
|
---|
1124 | fprintf(stderr, "%s: warning: failed to remove output file '%s' on failure.\n", argv[0], pszOutput);
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | depCleanup();
|
---|
1128 | return i;
|
---|
1129 | }
|
---|
1130 |
|
---|