1 | /* $Id: kHll.cpp,v 1.6 2000-03-27 10:20:42 bird Exp $
|
---|
2 | *
|
---|
3 | * kHll - Implementation of the class kHll.
|
---|
4 | * That class is used to create HLL debuginfo.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
---|
7 | *
|
---|
8 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
9 | *
|
---|
10 | */
|
---|
11 |
|
---|
12 |
|
---|
13 | /*******************************************************************************
|
---|
14 | * Defined Constants And Macros *
|
---|
15 | *******************************************************************************/
|
---|
16 | #define INCL_TYPES
|
---|
17 | #define INCL_DOSERRORS
|
---|
18 | #define FOR_EXEHDR 1 /* exe386.h flag */
|
---|
19 | #define DWORD ULONG /* Used by exe386.h / newexe.h */
|
---|
20 | #define WORD USHORT /* Used by exe386.h / newexe.h */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Internal Functions *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <os2.h>
|
---|
27 | #include <newexe.h>
|
---|
28 | #include <exe386.h>
|
---|
29 |
|
---|
30 | #include <malloc.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <string.h>
|
---|
33 | #include <stddef.h>
|
---|
34 | #include <stdlib.h>
|
---|
35 | #include <assert.h>
|
---|
36 |
|
---|
37 | #include "hll.h"
|
---|
38 | #include "kList.h"
|
---|
39 | #include "kHll.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Internal Functions *
|
---|
44 | *******************************************************************************/
|
---|
45 | signed long fsize(FILE *phFile);
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * *
|
---|
52 | * kHllBaseEntry *
|
---|
53 | * *
|
---|
54 | * kHllBaseEntry *
|
---|
55 | * *
|
---|
56 | *******************************************************************************/
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Writes a list to disk.
|
---|
61 | * @returns Number of bytes written.
|
---|
62 | * @returns Count of bytes written on success. (includes 0)
|
---|
63 | * -3 Invalid offsets.
|
---|
64 | * -2 Seek error.
|
---|
65 | * -1 Write error.
|
---|
66 | * @param phFile Output filehandle.
|
---|
67 | * @param pEntry Pointer to the start of the list which is to be written.
|
---|
68 | */
|
---|
69 | int kHllBaseEntry::writeList(FILE *phFile, kHllBaseEntry *pEntry)
|
---|
70 | {
|
---|
71 | int cch;
|
---|
72 | int cchWritten = 0;
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * Loop thru the list and write all the entries to disk.
|
---|
76 | */
|
---|
77 | while (pEntry != NULL)
|
---|
78 | {
|
---|
79 | cchWritten += cch = pEntry->write(phFile);
|
---|
80 | if (cch < 0)
|
---|
81 | return cch;
|
---|
82 | if (cch == 0)
|
---|
83 | return -1;
|
---|
84 | pEntry = (kHllBaseEntry*)pEntry->getNext();
|
---|
85 | }
|
---|
86 |
|
---|
87 | return cchWritten;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 | /*******************************************************************************
|
---|
96 | * *
|
---|
97 | * kHllPubSymEntry *
|
---|
98 | * *
|
---|
99 | * kHllPubSymEntry *
|
---|
100 | * *
|
---|
101 | *******************************************************************************/
|
---|
102 |
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Creates an HLL public symbol entry.
|
---|
107 | * @param pachName Symbol name.
|
---|
108 | * @param cchName Length of symbol name.
|
---|
109 | * @param off Offset into the object.
|
---|
110 | * @param iObject LX Object index.
|
---|
111 | * @param iType Type index. (index into type table)
|
---|
112 | */
|
---|
113 | kHllPubSymEntry::kHllPubSymEntry(
|
---|
114 | const char * pachName,
|
---|
115 | int cchName,
|
---|
116 | unsigned long off,
|
---|
117 | unsigned short iObject,
|
---|
118 | unsigned short iType
|
---|
119 | )
|
---|
120 | {
|
---|
121 | pPubSym = (PHLLPUBLICSYM)malloc(cchName + sizeof(HLLPUBLICSYM));
|
---|
122 | assert(pPubSym != NULL);
|
---|
123 |
|
---|
124 | pPubSym->cchName = cchName;
|
---|
125 | pPubSym->achName[0] = '\0';
|
---|
126 | strncat((char*)&pPubSym->achName[0], pachName, cchName);
|
---|
127 | pPubSym->off = off;
|
---|
128 | pPubSym->iObject = iObject;
|
---|
129 | pPubSym->iType = iType;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Destructor.
|
---|
136 | */
|
---|
137 | kHllPubSymEntry::~kHllPubSymEntry()
|
---|
138 | {
|
---|
139 | if (pPubSym != NULL)
|
---|
140 | free(pPubSym);
|
---|
141 | pPubSym = NULL;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Write this entry to file.
|
---|
148 | * @returns Number of bytes written.
|
---|
149 | * @param phFile File handle.
|
---|
150 | */
|
---|
151 | int kHllPubSymEntry::write(FILE *phFile)
|
---|
152 | {
|
---|
153 | assert(pPubSym != NULL);
|
---|
154 | return fwrite(pPubSym,
|
---|
155 | 1,
|
---|
156 | offsetof(HLLPUBLICSYM, achName) + pPubSym->cchName,
|
---|
157 | phFile);
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 | /*******************************************************************************
|
---|
165 | * *
|
---|
166 | * kHllModuleEntry *
|
---|
167 | * *
|
---|
168 | * kHllModuleEntry *
|
---|
169 | * *
|
---|
170 | *******************************************************************************/
|
---|
171 |
|
---|
172 |
|
---|
173 |
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Creates an HLL module entry.
|
---|
180 | * @param pszName Module name. (NULL is not allowed!)
|
---|
181 | * @param iLib Library index.
|
---|
182 | * @param cSegInfo Number of objects in the array.
|
---|
183 | * @param paSegInfo Pointer to an array of objects.
|
---|
184 | */
|
---|
185 | kHllModuleEntry::kHllModuleEntry(
|
---|
186 | const char * pszName,
|
---|
187 | unsigned short iLib,
|
---|
188 | unsigned char cSegInfo/*= 0 */,
|
---|
189 | PHLLSEGINFO paSegInfo/*= NULL */
|
---|
190 | )
|
---|
191 | : fValidOffsetsAndSizes(FALSE)
|
---|
192 | {
|
---|
193 | int i;
|
---|
194 | int cchName;
|
---|
195 | PHLLSEGINFO pSegInfo;
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Debug parameter validations.
|
---|
199 | */
|
---|
200 | assert(pszName != NULL);
|
---|
201 | assert(cSegInfo == 0 || paSegInfo != NULL);
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * Allocate data storage and fill HLL structure.
|
---|
205 | */
|
---|
206 | cchName = strlen(pszName);
|
---|
207 | pModule = (PHLLMODULE)malloc(sizeof(HLLMODULE) + cchName +
|
---|
208 | sizeof(HLLSEGINFO) * max((cSegInfo - 1), 3));
|
---|
209 | assert(pModule != NULL);
|
---|
210 | memset(pModule, 0, sizeof(*pModule));
|
---|
211 | pModule->cchName = cchName;
|
---|
212 | strcpy((char*)&pModule->achName[0], pszName);
|
---|
213 | pModule->chVerMajor = 4;
|
---|
214 | pModule->chVerMinor = 0;
|
---|
215 | pModule->cSegInfo = cSegInfo;
|
---|
216 | pModule->iLib = iLib;
|
---|
217 | pModule->usDebugStyle = HLL_MOD_STYLE;
|
---|
218 | pModule->overlay = 0;
|
---|
219 | pModule->pad = 0;
|
---|
220 |
|
---|
221 | /* objects */
|
---|
222 | if (cSegInfo > 0)
|
---|
223 | {
|
---|
224 | pModule->SegInfo0.iObject = paSegInfo->iObject;
|
---|
225 | pModule->SegInfo0.cb = paSegInfo->cb;
|
---|
226 | pModule->SegInfo0.off = paSegInfo->off;
|
---|
227 |
|
---|
228 | for (i = 1, pSegInfo = (PHLLSEGINFO)&pModule->achName[cchName]; i < cSegInfo; i++, pSegInfo++)
|
---|
229 | {
|
---|
230 | pSegInfo->iObject = paSegInfo[i].iObject;
|
---|
231 | pSegInfo->cb = paSegInfo[i].cb;
|
---|
232 | pSegInfo->off = paSegInfo[i].off;
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Destructor - free storage.
|
---|
240 | */
|
---|
241 | kHllModuleEntry::~kHllModuleEntry()
|
---|
242 | {
|
---|
243 | if (pModule != NULL)
|
---|
244 | free(pModule);
|
---|
245 | pModule = NULL;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Adds an object to the module.
|
---|
252 | * @returns Success indicator.
|
---|
253 | * @param iObject LX Object index.
|
---|
254 | * @param off Offset into the object to the module data.
|
---|
255 | * @param cb Size of module data (in the object).
|
---|
256 | */
|
---|
257 | BOOL kHllModuleEntry::addSegInfo(
|
---|
258 | unsigned short int iObject,
|
---|
259 | unsigned long off,
|
---|
260 | unsigned long cb
|
---|
261 | )
|
---|
262 | {
|
---|
263 | assert(pModule != NULL);
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * Reallocate? (Note that we'll initially allocated space for 3 objects.)
|
---|
267 | */
|
---|
268 | if (pModule->cSegInfo >= 3)
|
---|
269 | {
|
---|
270 | void *pv = realloc(pModule, sizeof(HLLMODULE) + pModule->cchName
|
---|
271 | + (pModule->cSegInfo + 1) * sizeof(HLLSEGINFO));
|
---|
272 | assert(pv != NULL);
|
---|
273 | if (pv == NULL)
|
---|
274 | return FALSE;
|
---|
275 | pModule = (PHLLMODULE)pv;
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | /*
|
---|
280 | * Add module.
|
---|
281 | */
|
---|
282 | if (pModule->cSegInfo == 0)
|
---|
283 | {
|
---|
284 | pModule->SegInfo0.cb = cb;
|
---|
285 | pModule->SegInfo0.off = off;
|
---|
286 | pModule->SegInfo0.iObject = iObject;
|
---|
287 | }
|
---|
288 | else
|
---|
289 | {
|
---|
290 | PHLLSEGINFO pSegInfo = (PHLLSEGINFO)((pModule->cSegInfo - 1) * sizeof(HLLSEGINFO)
|
---|
291 | + &pModule->achName[pModule->cchName]);
|
---|
292 | pSegInfo->cb = cb;
|
---|
293 | pSegInfo->off = off;
|
---|
294 | pSegInfo->iObject = iObject;
|
---|
295 | }
|
---|
296 | pModule->cSegInfo++;
|
---|
297 |
|
---|
298 | return TRUE;
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Adds a public symbol.
|
---|
305 | * @returns Handle to the symbol. NULL on error.
|
---|
306 | * @param pszName Symbol name.
|
---|
307 | * @param off Offset into the LX Object of the symbol.
|
---|
308 | * @param iObject LX Object index.
|
---|
309 | * @param pvType Type handle. NULL if not type.
|
---|
310 | */
|
---|
311 | const void * kHllModuleEntry::addPublicSymbol(
|
---|
312 | const char * pszName,
|
---|
313 | unsigned long int off,
|
---|
314 | unsigned short int iObject,
|
---|
315 | const void * pvType
|
---|
316 | )
|
---|
317 | {
|
---|
318 | assert(pszName != NULL);
|
---|
319 | return addPublicSymbol(pszName, strlen(pszName), off, iObject, pvType);
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Adds a public symbol.
|
---|
326 | * @returns Handle to the symbol. NULL on error.
|
---|
327 | * @param pachName Symbol name.
|
---|
328 | * @param cchName Name length.
|
---|
329 | * @param off Offset into the LX Object of the symbol.
|
---|
330 | * @param iObject LX Object index.
|
---|
331 | * @param pvType Type handle. NULL if not type.
|
---|
332 | */
|
---|
333 | const void * kHllModuleEntry::addPublicSymbol(
|
---|
334 | const char * pachName,
|
---|
335 | int cchName,
|
---|
336 | unsigned long int off,
|
---|
337 | unsigned short int iObject,
|
---|
338 | const void * pvType
|
---|
339 | )
|
---|
340 | {
|
---|
341 | kHllPubSymEntry * pEntry;
|
---|
342 |
|
---|
343 | /* parameter assertion */
|
---|
344 | assert(pachName != NULL);
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Create a public symbol entry
|
---|
348 | * Insert into it's list.
|
---|
349 | * Invalidate offsets.
|
---|
350 | */
|
---|
351 | pEntry = new kHllPubSymEntry(
|
---|
352 | pachName,
|
---|
353 | cchName,
|
---|
354 | off,
|
---|
355 | iObject,
|
---|
356 | pvType == NULL ? 0 : -1 //FIXME/TODO: Types->getIndex(pvType); check if 0 or -1.
|
---|
357 | );
|
---|
358 |
|
---|
359 | PublicSymbols.insert(pEntry);
|
---|
360 |
|
---|
361 | fValidOffsetsAndSizes = FALSE;
|
---|
362 |
|
---|
363 | return pEntry;
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 |
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Write this HLL entry to file.
|
---|
371 | * @returns Count of bytes written. -1 on error.
|
---|
372 | * @param phFile Filehandle.
|
---|
373 | * @param off Current offset into the HLL data.
|
---|
374 | * This is stored and used when making the directory
|
---|
375 | * entries for this module.
|
---|
376 | */
|
---|
377 | int kHllModuleEntry::write(FILE *phFile, unsigned long off)
|
---|
378 | {
|
---|
379 | int cch;
|
---|
380 | int cchToWrite;
|
---|
381 | int cchWritten = 0;
|
---|
382 |
|
---|
383 | /* validate object state */
|
---|
384 | assert(pModule != NULL);
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * Write module HLL data.
|
---|
388 | */
|
---|
389 | offModule = off;
|
---|
390 | cchToWrite = offsetof(HLLMODULE, achName) + pModule->cchName + sizeof(HLLSEGINFO) * max(pModule->cSegInfo-1, 0);
|
---|
391 | cch = fwrite(pModule, 1, cchToWrite, phFile);
|
---|
392 | if (cch != cchToWrite)
|
---|
393 | return -1;
|
---|
394 | cchWritten += cch;
|
---|
395 | cbModule = cch;
|
---|
396 | off += cch;
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * Write the lists.
|
---|
400 | * Public Symbols
|
---|
401 | * Types
|
---|
402 | * Symbols
|
---|
403 | * Source
|
---|
404 | */
|
---|
405 | offPublicSymbols = off;
|
---|
406 | cbPublicSymbols = cch = kHllBaseEntry::writeList(phFile, PublicSymbols.getFirst());
|
---|
407 | if (cch < 0)
|
---|
408 | return cch;
|
---|
409 | cchWritten += cch;
|
---|
410 | off += cch;
|
---|
411 |
|
---|
412 | /*
|
---|
413 | offTypes = off;
|
---|
414 | cbTypes = cch = kHllBaseEntry::writeList(phFile, Types.getFirst());
|
---|
415 | if (cch < 0)
|
---|
416 | return cch;
|
---|
417 | cchWritten += cch;
|
---|
418 | off += cch;
|
---|
419 |
|
---|
420 |
|
---|
421 | offSymbols = off;
|
---|
422 | cbSymbols = cch = kHllBaseEntry::writeList(phFile, Symbols.getFirst());
|
---|
423 | if (cch < 0)
|
---|
424 | return cch;
|
---|
425 | cchWritten += cch;
|
---|
426 | off += cch;
|
---|
427 |
|
---|
428 | offSource = off;
|
---|
429 | cbSource = cch = kHllBaseEntry::writeList(phFile, Source.getFirst());
|
---|
430 | if (cch < 0)
|
---|
431 | return cch;
|
---|
432 | cchWritten += cch;
|
---|
433 | off += cch;
|
---|
434 | */
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * Marks offsets and sizes valid and returns succesfully.
|
---|
438 | */
|
---|
439 | fValidOffsetsAndSizes = TRUE;
|
---|
440 | return cchWritten;
|
---|
441 | }
|
---|
442 |
|
---|
443 |
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Writes the directory entries for this module to file.
|
---|
447 | * @returns Count of bytes written on success.
|
---|
448 | * -3 Invalid offsets.
|
---|
449 | * -2 Seek error.
|
---|
450 | * -1 Write error.
|
---|
451 | * 0 no data written (this is an error condition!)
|
---|
452 | * @param phFile Filehandle.
|
---|
453 | * @param iMod Index of this module.
|
---|
454 | */
|
---|
455 | int kHllModuleEntry::writeDirEntries(FILE *phFile, unsigned short iMod)
|
---|
456 | {
|
---|
457 | HLLDIRENTRY hllDirEntry;
|
---|
458 | int cch;
|
---|
459 | int cchWritten = 0;
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * Check that offsets are valid!
|
---|
463 | */
|
---|
464 | assert(fValidOffsetsAndSizes);
|
---|
465 | if (!fValidOffsetsAndSizes)
|
---|
466 | return -3;
|
---|
467 |
|
---|
468 | /*
|
---|
469 | * Write Directory Entries.
|
---|
470 | * Module.
|
---|
471 | * Public Symbols. (if any)
|
---|
472 | * Types. (if any)
|
---|
473 | * Symbols. (if any)
|
---|
474 | * Source. (if any)
|
---|
475 | */
|
---|
476 | hllDirEntry.usType = HLL_DE_MODULES;
|
---|
477 | hllDirEntry.cb = cbModule;
|
---|
478 | hllDirEntry.off = offModule;
|
---|
479 | hllDirEntry.iMod = iMod;
|
---|
480 | cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
|
---|
481 | if (cch != sizeof(hllDirEntry))
|
---|
482 | return -1;
|
---|
483 | cchWritten += cch;
|
---|
484 |
|
---|
485 | if (cbPublicSymbols > 0)
|
---|
486 | {
|
---|
487 | hllDirEntry.usType = HLL_DE_PUBLICS;
|
---|
488 | hllDirEntry.cb = cbPublicSymbols;
|
---|
489 | hllDirEntry.off = offPublicSymbols;
|
---|
490 | hllDirEntry.iMod = iMod;
|
---|
491 | cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
|
---|
492 | if (cch != sizeof(hllDirEntry))
|
---|
493 | return -1;
|
---|
494 | cchWritten += cch;
|
---|
495 | }
|
---|
496 |
|
---|
497 | /*
|
---|
498 | if (cbTypes > 0)
|
---|
499 | {
|
---|
500 | hllDirEntry.usType = HLL_DE_TYPES;
|
---|
501 | hllDirEntry.cb = cbTypes;
|
---|
502 | hllDirEntry.off = offTypes;
|
---|
503 | hllDirEntry.iMod = iMod;
|
---|
504 | cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
|
---|
505 | if (cch != sizeof(hllDirEntry))
|
---|
506 | return -1;
|
---|
507 | cchWritten += cch;
|
---|
508 | }
|
---|
509 |
|
---|
510 | if (cbSymbols > 0)
|
---|
511 | {
|
---|
512 | hllDirEntry.usType = HLL_DE_SYMBOLS;
|
---|
513 | hllDirEntry.cb = cbSymbols;
|
---|
514 | hllDirEntry.off = offSymbols;
|
---|
515 | hllDirEntry.iMod = iMod;
|
---|
516 | cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
|
---|
517 | if (cch != sizeof(hllDirEntry))
|
---|
518 | return -1;
|
---|
519 | cchWritten += cch;
|
---|
520 | }
|
---|
521 |
|
---|
522 | if (cbSource > 0)
|
---|
523 | {
|
---|
524 | hllDirEntry.usType = HLL_DE_IBMSRC;
|
---|
525 | hllDirEntry.cb = cbSource;
|
---|
526 | hllDirEntry.off = offSource;
|
---|
527 | hllDirEntry.iMod = iMod;
|
---|
528 | cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
|
---|
529 | if (cch != sizeof(hllDirEntry))
|
---|
530 | return -1;
|
---|
531 | cchWritten += cch;
|
---|
532 | }
|
---|
533 |
|
---|
534 | */
|
---|
535 |
|
---|
536 | return cchWritten;
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 |
|
---|
541 |
|
---|
542 |
|
---|
543 |
|
---|
544 |
|
---|
545 |
|
---|
546 |
|
---|
547 |
|
---|
548 |
|
---|
549 |
|
---|
550 | /*******************************************************************************
|
---|
551 | * *
|
---|
552 | * kHll *
|
---|
553 | * *
|
---|
554 | * kHll *
|
---|
555 | * *
|
---|
556 | *******************************************************************************/
|
---|
557 |
|
---|
558 |
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * Writes HLL debuginfo to the given file at the current position.
|
---|
562 | * The file should be opened in write mode.
|
---|
563 | * @returns Number of bytes written.
|
---|
564 | * @param phFile Filehandle to output file. Starts writing at current pos.
|
---|
565 | */
|
---|
566 | int kHll::write(FILE *phFile)
|
---|
567 | {
|
---|
568 | HLLHDR hllHdr;
|
---|
569 | HLLDIR hllDir;
|
---|
570 | kHllModuleEntry * pModule;
|
---|
571 | int cch; /* Number of bytes written to the file in an operation. */
|
---|
572 | int cchWritten = 0; /* Number of bytes written to the file. */
|
---|
573 | long int lPosStart; /* Starting position. */
|
---|
574 | long int lPosDir; /* Directory position. */
|
---|
575 | long int lPos; /* A file position. */
|
---|
576 | int iMod; /* Module index (passed in to writeDirEntries) */
|
---|
577 |
|
---|
578 | /* Get starting position. */
|
---|
579 | lPosStart = ftell(phFile);
|
---|
580 |
|
---|
581 | /* Make temporary header and write it */
|
---|
582 | memcpy(hllHdr.achSignature, "NB04", 4);
|
---|
583 | hllHdr.offDirectory = 0;
|
---|
584 | hllHdr.ulReserved = 1;
|
---|
585 | cch = fwrite(&hllHdr, 1, sizeof(hllHdr), phFile);
|
---|
586 | if (cch != sizeof(hllHdr))
|
---|
587 | return -1;
|
---|
588 | cchWritten += cch;
|
---|
589 |
|
---|
590 |
|
---|
591 | /*
|
---|
592 | * Start writing modules
|
---|
593 | */
|
---|
594 | pModule = (kHllModuleEntry*)Modules.getFirst();
|
---|
595 | while (pModule != NULL)
|
---|
596 | {
|
---|
597 | cch = pModule->write(phFile, cchWritten);
|
---|
598 | if (cch <= 0)
|
---|
599 | return cch;
|
---|
600 | cchWritten += cch;
|
---|
601 | pModule = (kHllModuleEntry *)pModule->getNext();
|
---|
602 | }
|
---|
603 |
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * Write libraries.
|
---|
607 | */
|
---|
608 | //Not implemented yet - TODO/FIXME!
|
---|
609 |
|
---|
610 |
|
---|
611 | /*
|
---|
612 | * Write directory.
|
---|
613 | * Make and write temporary directory header.
|
---|
614 | * Write directory entries per module.
|
---|
615 | * Write directory entry for libraries.
|
---|
616 | * Remake and rewrite directory header. (correct cEntries)
|
---|
617 | */
|
---|
618 | lPosDir = ftell(phFile);
|
---|
619 | hllDir.cEntries = 0;
|
---|
620 | hllDir.ulReserved = 0; //FIXME/TODO - not quite sure what this is or should be!
|
---|
621 | cch = fwrite(&hllDir, 1, offsetof(HLLDIR, aEntries), phFile);
|
---|
622 | if (cch != offsetof(HLLDIR, aEntries))
|
---|
623 | return -1;
|
---|
624 | cchWritten += cch;
|
---|
625 |
|
---|
626 | iMod = 1;
|
---|
627 | pModule = (kHllModuleEntry*)Modules.getFirst();
|
---|
628 | while (pModule != NULL)
|
---|
629 | {
|
---|
630 | cch = pModule->writeDirEntries(phFile, iMod);
|
---|
631 | if (cch == -1)
|
---|
632 | return -1;
|
---|
633 | cchWritten += cch;
|
---|
634 | pModule = (kHllModuleEntry *)pModule->getNext();
|
---|
635 | iMod++;
|
---|
636 | }
|
---|
637 |
|
---|
638 | //Library - TODO/FIXME
|
---|
639 |
|
---|
640 | lPos = ftell(phFile);
|
---|
641 | hllDir.cEntries = (lPos - lPosDir - offsetof(HLLDIR, aEntries)) / sizeof(HLLDIRENTRY);
|
---|
642 | if (fseek(phFile, lPosDir, SEEK_SET) != 0)
|
---|
643 | return -2;
|
---|
644 | cch = fwrite(&hllDir, 1, offsetof(HLLDIR, aEntries), phFile);
|
---|
645 | if (cch != offsetof(HLLDIR, aEntries))
|
---|
646 | return -1;
|
---|
647 |
|
---|
648 | /*
|
---|
649 | * Rewrite HLL header (with correct directory offset).
|
---|
650 | */
|
---|
651 | hllHdr.offDirectory = lPosDir - lPosStart;
|
---|
652 | if (fseek(phFile, lPosStart, SEEK_SET) != 0)
|
---|
653 | return -2;
|
---|
654 | cch = fwrite(&hllHdr, 1, sizeof(hllHdr), phFile);
|
---|
655 | if (cch != sizeof(hllHdr))
|
---|
656 | return -1;
|
---|
657 |
|
---|
658 | return cchWritten;
|
---|
659 | }
|
---|
660 |
|
---|
661 |
|
---|
662 |
|
---|
663 | /**
|
---|
664 | * Constructor - Creates an empty HLL object.
|
---|
665 | */
|
---|
666 | kHll::kHll()
|
---|
667 | {
|
---|
668 | }
|
---|
669 |
|
---|
670 |
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Destructor.
|
---|
674 | */
|
---|
675 | kHll::~kHll()
|
---|
676 | {
|
---|
677 | }
|
---|
678 |
|
---|
679 |
|
---|
680 |
|
---|
681 | /**
|
---|
682 | * Adds a module.
|
---|
683 | * @returns Pointer to the module object added. NULL on error.
|
---|
684 | * @param pszName Module name
|
---|
685 | * @param pvLib Library module handle.
|
---|
686 | * @param cSegInfo Number of objects in the array.
|
---|
687 | * @param paSegInfo Pointer to an array of objects.
|
---|
688 | */
|
---|
689 | kHllModuleEntry * kHll::addModule(
|
---|
690 | const char * pszName,
|
---|
691 | const void * pvLib,
|
---|
692 | unsigned cSegInfo,
|
---|
693 | PHLLSEGINFO paSegInfo)
|
---|
694 | {
|
---|
695 | kHllModuleEntry * pEntry;
|
---|
696 | assert(pszName != NULL);
|
---|
697 |
|
---|
698 | pEntry = new kHllModuleEntry(
|
---|
699 | pszName,
|
---|
700 | pvLib == NULL ? 0 : -1, //FIXME/TODO: Libs->getIndex(pvLib); check if 0 or -1;
|
---|
701 | cSegInfo,
|
---|
702 | paSegInfo);
|
---|
703 |
|
---|
704 | Modules.insert(pEntry);
|
---|
705 | return pEntry;
|
---|
706 | }
|
---|
707 |
|
---|
708 |
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * Adds a module.
|
---|
712 | * @returns Pointer to the module object added. NULL on error.
|
---|
713 | * @param pachName Module name
|
---|
714 | * @param cchName Length of modulename
|
---|
715 | * @param pvLib Library module handle.
|
---|
716 | * @param cSegInfo Number of objects in the array.
|
---|
717 | * @param paSegInfo Pointer to an array of objects.
|
---|
718 | */
|
---|
719 | kHllModuleEntry * kHll::addModule(
|
---|
720 | const char * pachName,
|
---|
721 | unsigned cchName,
|
---|
722 | const void * pvLib,
|
---|
723 | unsigned cSegInfo,
|
---|
724 | PHLLSEGINFO paSegInfo)
|
---|
725 | {
|
---|
726 | char szModName[256];
|
---|
727 | kHllModuleEntry * pEntry;
|
---|
728 | assert(pachName != NULL && cchName > 0);
|
---|
729 |
|
---|
730 | szModName[0] = '\0';
|
---|
731 | strncat(szModName, pachName, min(cchName, 255));
|
---|
732 | pEntry = new kHllModuleEntry(
|
---|
733 | szModName,
|
---|
734 | pvLib == NULL ? 0 : -1, //FIXME/TODO: Libs->getIndex(pvLib); check if 0 or -1;
|
---|
735 | cSegInfo,
|
---|
736 | paSegInfo);
|
---|
737 |
|
---|
738 | Modules.insert(pEntry);
|
---|
739 | return pEntry;
|
---|
740 | }
|
---|
741 |
|
---|
742 |
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Writes the HLL info to a file. (Not LX?)
|
---|
746 | * @returns Success indicator.
|
---|
747 | * @param pszFilename Name of the output file.
|
---|
748 | * @remark IMPORTANT! This is mostly for debugging!
|
---|
749 | * It completely overwrites the file if it exists!
|
---|
750 | */
|
---|
751 | BOOL kHll::write(
|
---|
752 | const char *pszFilename
|
---|
753 | )
|
---|
754 | {
|
---|
755 | FILE * phFile;
|
---|
756 |
|
---|
757 | phFile = fopen(pszFilename, "wb");
|
---|
758 | if (phFile != NULL)
|
---|
759 | {
|
---|
760 | int cch = write(phFile);
|
---|
761 | if (cch > 0)
|
---|
762 | {
|
---|
763 | fclose(phFile);
|
---|
764 | return TRUE;
|
---|
765 | }
|
---|
766 | else
|
---|
767 | fprintf(stderr, "write failed with cch=%d\n", cch);
|
---|
768 | fclose(phFile);
|
---|
769 | }
|
---|
770 |
|
---|
771 | return FALSE;
|
---|
772 | }
|
---|
773 |
|
---|
774 |
|
---|
775 |
|
---|
776 | /**
|
---|
777 | * Writes the HLL info to a file. (Not LX?)
|
---|
778 | * Failes if there is debuginfo in the file.
|
---|
779 | * No backup is made. (sorry)
|
---|
780 | * @returns OS2 return code.
|
---|
781 | * @param pszFilename Name of the output file.
|
---|
782 | */
|
---|
783 | APIRET kHll::writeToLX(
|
---|
784 | const char *pszFilename
|
---|
785 | )
|
---|
786 | {
|
---|
787 | APIRET rc;
|
---|
788 | FILE * phFile;
|
---|
789 |
|
---|
790 | phFile = fopen(pszFilename, "rb+");
|
---|
791 | if (phFile != NULL)
|
---|
792 | {
|
---|
793 | struct exe_hdr ehdr;
|
---|
794 | struct e32_exe e32;
|
---|
795 | int cch;
|
---|
796 | long lPosLXHdr;
|
---|
797 | long cbLXFile;
|
---|
798 |
|
---|
799 | /*
|
---|
800 | * Read exe header
|
---|
801 | */
|
---|
802 | cch = fread(&ehdr, 1, sizeof(ehdr), phFile);
|
---|
803 | if (cch == sizeof(ehdr))
|
---|
804 | {
|
---|
805 | cbLXFile = fsize(phFile);
|
---|
806 | if (ehdr.e_magic == EMAGIC)
|
---|
807 | lPosLXHdr = ehdr.e_lfanew;
|
---|
808 | else
|
---|
809 | lPosLXHdr = 0;
|
---|
810 | if (fseek(phFile, lPosLXHdr, SEEK_SET) == 0)
|
---|
811 | {
|
---|
812 | cch = fread(&e32, 1, sizeof(e32), phFile);
|
---|
813 | if (cch == sizeof(e32))
|
---|
814 | {
|
---|
815 | if (*(unsigned short*)&e32.e32_magic[0] == E32MAGIC)
|
---|
816 | {
|
---|
817 | /*
|
---|
818 | * Found exeheader.
|
---|
819 | * Check if there is any debug info.
|
---|
820 | */
|
---|
821 | if ((e32.e32_debuginfo == 0 && e32.e32_debuginfo == 0)
|
---|
822 | || (cbLXFile == e32.e32_debuglen + e32.e32_debuginfo)
|
---|
823 | )
|
---|
824 | {
|
---|
825 | long lPosDebug;
|
---|
826 |
|
---|
827 | if (e32.e32_debuginfo != 0 && e32.e32_debuglen != 0)
|
---|
828 | lPosDebug = e32.e32_debuginfo;
|
---|
829 | else
|
---|
830 | lPosDebug = cbLXFile;
|
---|
831 |
|
---|
832 | /*
|
---|
833 | * Go to debug info position in the file and write debug info.
|
---|
834 | */
|
---|
835 | if (fseek(phFile, lPosDebug, SEEK_SET) == 0)
|
---|
836 | {
|
---|
837 | /*
|
---|
838 | * Write the HLL data to disk.
|
---|
839 | */
|
---|
840 | cch = write(phFile);
|
---|
841 | if (cch > 0)
|
---|
842 | {
|
---|
843 | /*
|
---|
844 | * Update exeheader.
|
---|
845 | */
|
---|
846 | e32.e32_debuglen = cch;
|
---|
847 | e32.e32_debuginfo = lPosDebug;
|
---|
848 | if (fseek(phFile, lPosLXHdr, SEEK_SET) == 0)
|
---|
849 | {
|
---|
850 | /*
|
---|
851 | * Write the updated header to disk.
|
---|
852 | */
|
---|
853 | cch = fwrite(&e32, 1, sizeof(e32), phFile);
|
---|
854 | if (cch == sizeof(e32))
|
---|
855 | rc = NO_ERROR;
|
---|
856 | else
|
---|
857 | rc = ERROR_WRITE_FAULT;
|
---|
858 | }
|
---|
859 | else
|
---|
860 | rc = ERROR_SEEK;
|
---|
861 | }
|
---|
862 | else
|
---|
863 | {
|
---|
864 | fprintf(stderr, "error - write failed with cch=%d\n", cch);
|
---|
865 | rc = ERROR_WRITE_FAULT;
|
---|
866 | }
|
---|
867 | }
|
---|
868 | else
|
---|
869 | rc = ERROR_SEEK;
|
---|
870 | }
|
---|
871 | else
|
---|
872 | {
|
---|
873 | fprintf(stderr, "error - debuginfo exists\n");
|
---|
874 | rc = ERROR_BAD_EXE_FORMAT;
|
---|
875 | }
|
---|
876 |
|
---|
877 | }
|
---|
878 | else
|
---|
879 | rc = ERROR_INVALID_EXE_SIGNATURE;
|
---|
880 | }
|
---|
881 | else
|
---|
882 | rc = ERROR_BAD_EXE_FORMAT;
|
---|
883 | }
|
---|
884 | else
|
---|
885 | rc = ERROR_BAD_EXE_FORMAT;
|
---|
886 | }
|
---|
887 | else
|
---|
888 | rc = ERROR_READ_FAULT;
|
---|
889 |
|
---|
890 | fclose(phFile);
|
---|
891 | }
|
---|
892 | else
|
---|
893 | rc = ERROR_ACCESS_DENIED; //?
|
---|
894 |
|
---|
895 |
|
---|
896 | return rc;
|
---|
897 | }
|
---|
898 |
|
---|
899 |
|
---|
900 | /**
|
---|
901 | * Find the size of a file.
|
---|
902 | * @returns Size of file. -1 on error.
|
---|
903 | * @param phFile File handle.
|
---|
904 | */
|
---|
905 | signed long fsize(FILE *phFile)
|
---|
906 | {
|
---|
907 | int ipos;
|
---|
908 | signed long cb;
|
---|
909 |
|
---|
910 | if ((ipos = ftell(phFile)) < 0
|
---|
911 | ||
|
---|
912 | fseek(phFile, 0, SEEK_END) != 0
|
---|
913 | ||
|
---|
914 | (cb = ftell(phFile)) < 0
|
---|
915 | ||
|
---|
916 | fseek(phFile, ipos, SEEK_SET) != 0
|
---|
917 | )
|
---|
918 | cb = -1;
|
---|
919 | return cb;
|
---|
920 | }
|
---|
921 |
|
---|