| 1 | /* $Id: kHll.cpp,v 1.9 2000-03-31 15:35:09 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 | cch = fwrite(&hllHdr, 1, sizeof(hllHdr), phFile);
|
|---|
| 585 | if (cch != sizeof(hllHdr))
|
|---|
| 586 | return -1;
|
|---|
| 587 | cchWritten += cch;
|
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 | /*
|
|---|
| 591 | * Start writing modules
|
|---|
| 592 | */
|
|---|
| 593 | pModule = (kHllModuleEntry*)Modules.getFirst();
|
|---|
| 594 | while (pModule != NULL)
|
|---|
| 595 | {
|
|---|
| 596 | cch = pModule->write(phFile, cchWritten);
|
|---|
| 597 | if (cch <= 0)
|
|---|
| 598 | return cch;
|
|---|
| 599 | cchWritten += cch;
|
|---|
| 600 | pModule = (kHllModuleEntry *)pModule->getNext();
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 | /*
|
|---|
| 605 | * Write libraries.
|
|---|
| 606 | */
|
|---|
| 607 | //Not implemented yet - TODO/FIXME!
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 | /*
|
|---|
| 611 | * Write directory.
|
|---|
| 612 | * Make and write temporary directory header.
|
|---|
| 613 | * Write directory entries per module.
|
|---|
| 614 | * Write directory entry for libraries.
|
|---|
| 615 | * Remake and rewrite directory header. (correct cEntries)
|
|---|
| 616 | */
|
|---|
| 617 | lPosDir = ftell(phFile);
|
|---|
| 618 | hllDir.cEntries = 0;
|
|---|
| 619 | hllDir.cb = offsetof(HLLDIR, aEntries);
|
|---|
| 620 | hllDir.cbEntry = sizeof(HLLDIRENTRY);
|
|---|
| 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 | /*
|
|---|
| 659 | * Hacking:
|
|---|
| 660 | * Writing an extra HLL header pointing to an non-existing directory
|
|---|
| 661 | * staring at the last byte of this header. This is present when linking
|
|---|
| 662 | * with ilink...
|
|---|
| 663 | */
|
|---|
| 664 | if (fseek(phFile, lPosStart + cchWritten, SEEK_SET) != 0)
|
|---|
| 665 | return -2;
|
|---|
| 666 | hllHdr.offDirectory = cchWritten + sizeof(hllHdr) - 1;
|
|---|
| 667 | cch = fwrite(&hllHdr, 1, sizeof(hllHdr), phFile);
|
|---|
| 668 | if (cch != sizeof(hllHdr))
|
|---|
| 669 | return -1;
|
|---|
| 670 | cchWritten += cch;
|
|---|
| 671 |
|
|---|
| 672 | return cchWritten;
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 | /**
|
|---|
| 678 | * Constructor - Creates an empty HLL object.
|
|---|
| 679 | */
|
|---|
| 680 | kHll::kHll()
|
|---|
| 681 | {
|
|---|
| 682 | }
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 | /**
|
|---|
| 687 | * Destructor.
|
|---|
| 688 | */
|
|---|
| 689 | kHll::~kHll()
|
|---|
| 690 | {
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
|
|---|
| 695 | /**
|
|---|
| 696 | * Adds a module.
|
|---|
| 697 | * @returns Pointer to the module object added. NULL on error.
|
|---|
| 698 | * @param pszName Module name
|
|---|
| 699 | * @param pvLib Library module handle.
|
|---|
| 700 | * @param cSegInfo Number of objects in the array.
|
|---|
| 701 | * @param paSegInfo Pointer to an array of objects.
|
|---|
| 702 | */
|
|---|
| 703 | kHllModuleEntry * kHll::addModule(
|
|---|
| 704 | const char * pszName,
|
|---|
| 705 | const void * pvLib,
|
|---|
| 706 | unsigned cSegInfo,
|
|---|
| 707 | PHLLSEGINFO paSegInfo)
|
|---|
| 708 | {
|
|---|
| 709 | kHllModuleEntry * pEntry;
|
|---|
| 710 | assert(pszName != NULL);
|
|---|
| 711 |
|
|---|
| 712 | pEntry = new kHllModuleEntry(
|
|---|
| 713 | pszName,
|
|---|
| 714 | pvLib == NULL ? 0 : -1, //FIXME/TODO: Libs->getIndex(pvLib); check if 0 or -1;
|
|---|
| 715 | cSegInfo,
|
|---|
| 716 | paSegInfo);
|
|---|
| 717 |
|
|---|
| 718 | Modules.insert(pEntry);
|
|---|
| 719 | return pEntry;
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 | /**
|
|---|
| 725 | * Adds a module.
|
|---|
| 726 | * @returns Pointer to the module object added. NULL on error.
|
|---|
| 727 | * @param pachName Module name
|
|---|
| 728 | * @param cchName Length of modulename
|
|---|
| 729 | * @param pvLib Library module handle.
|
|---|
| 730 | * @param cSegInfo Number of objects in the array.
|
|---|
| 731 | * @param paSegInfo Pointer to an array of objects.
|
|---|
| 732 | */
|
|---|
| 733 | kHllModuleEntry * kHll::addModule(
|
|---|
| 734 | const char * pachName,
|
|---|
| 735 | unsigned cchName,
|
|---|
| 736 | const void * pvLib,
|
|---|
| 737 | unsigned cSegInfo,
|
|---|
| 738 | PHLLSEGINFO paSegInfo)
|
|---|
| 739 | {
|
|---|
| 740 | char szModName[256];
|
|---|
| 741 | kHllModuleEntry * pEntry;
|
|---|
| 742 | assert(pachName != NULL && cchName > 0);
|
|---|
| 743 |
|
|---|
| 744 | szModName[0] = '\0';
|
|---|
| 745 | strncat(szModName, pachName, min(cchName, 255));
|
|---|
| 746 | pEntry = new kHllModuleEntry(
|
|---|
| 747 | szModName,
|
|---|
| 748 | pvLib == NULL ? 0 : -1, //FIXME/TODO: Libs->getIndex(pvLib); check if 0 or -1;
|
|---|
| 749 | cSegInfo,
|
|---|
| 750 | paSegInfo);
|
|---|
| 751 |
|
|---|
| 752 | Modules.insert(pEntry);
|
|---|
| 753 | return pEntry;
|
|---|
| 754 | }
|
|---|
| 755 |
|
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 | /**
|
|---|
| 759 | * Writes the HLL info to a file. (Not LX?)
|
|---|
| 760 | * @returns Success indicator.
|
|---|
| 761 | * @param pszFilename Name of the output file.
|
|---|
| 762 | * @remark IMPORTANT! This is mostly for debugging!
|
|---|
| 763 | * It completely overwrites the file if it exists!
|
|---|
| 764 | */
|
|---|
| 765 | BOOL kHll::write(
|
|---|
| 766 | const char *pszFilename
|
|---|
| 767 | )
|
|---|
| 768 | {
|
|---|
| 769 | FILE * phFile;
|
|---|
| 770 |
|
|---|
| 771 | phFile = fopen(pszFilename, "wb");
|
|---|
| 772 | if (phFile != NULL)
|
|---|
| 773 | {
|
|---|
| 774 | int cch = write(phFile);
|
|---|
| 775 | if (cch > 0)
|
|---|
| 776 | {
|
|---|
| 777 | fclose(phFile);
|
|---|
| 778 | return TRUE;
|
|---|
| 779 | }
|
|---|
| 780 | else
|
|---|
| 781 | fprintf(stderr, "write failed with cch=%d\n", cch);
|
|---|
| 782 | fclose(phFile);
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | return FALSE;
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 | /**
|
|---|
| 791 | * Writes the HLL info to a file. (Not LX?)
|
|---|
| 792 | * Failes if there is debuginfo in the file.
|
|---|
| 793 | * No backup is made. (sorry)
|
|---|
| 794 | * @returns OS2 return code.
|
|---|
| 795 | * @param pszFilename Name of the output file.
|
|---|
| 796 | */
|
|---|
| 797 | APIRET kHll::writeToLX(
|
|---|
| 798 | const char *pszFilename
|
|---|
| 799 | )
|
|---|
| 800 | {
|
|---|
| 801 | APIRET rc;
|
|---|
| 802 | FILE * phFile;
|
|---|
| 803 |
|
|---|
| 804 | phFile = fopen(pszFilename, "rb+");
|
|---|
| 805 | if (phFile != NULL)
|
|---|
| 806 | {
|
|---|
| 807 | struct exe_hdr ehdr;
|
|---|
| 808 | struct e32_exe e32;
|
|---|
| 809 | int cch;
|
|---|
| 810 | long lPosLXHdr;
|
|---|
| 811 | long cbLXFile;
|
|---|
| 812 |
|
|---|
| 813 | /*
|
|---|
| 814 | * Read exe header
|
|---|
| 815 | */
|
|---|
| 816 | cch = fread(&ehdr, 1, sizeof(ehdr), phFile);
|
|---|
| 817 | if (cch == sizeof(ehdr))
|
|---|
| 818 | {
|
|---|
| 819 | cbLXFile = fsize(phFile);
|
|---|
| 820 | if (ehdr.e_magic == EMAGIC)
|
|---|
| 821 | lPosLXHdr = ehdr.e_lfanew;
|
|---|
| 822 | else
|
|---|
| 823 | lPosLXHdr = 0;
|
|---|
| 824 | if (fseek(phFile, lPosLXHdr, SEEK_SET) == 0)
|
|---|
| 825 | {
|
|---|
| 826 | cch = fread(&e32, 1, sizeof(e32), phFile);
|
|---|
| 827 | if (cch == sizeof(e32))
|
|---|
| 828 | {
|
|---|
| 829 | if (*(unsigned short*)&e32.e32_magic[0] == E32MAGIC)
|
|---|
| 830 | {
|
|---|
| 831 | /*
|
|---|
| 832 | * Found exeheader.
|
|---|
| 833 | * Check if there is any debug info.
|
|---|
| 834 | */
|
|---|
| 835 | if ((e32.e32_debuginfo == 0 && e32.e32_debuginfo == 0)
|
|---|
| 836 | || (cbLXFile == e32.e32_debuglen + e32.e32_debuginfo)
|
|---|
| 837 | )
|
|---|
| 838 | {
|
|---|
| 839 | long lPosDebug;
|
|---|
| 840 |
|
|---|
| 841 | if (e32.e32_debuginfo != 0 && e32.e32_debuglen != 0)
|
|---|
| 842 | lPosDebug = e32.e32_debuginfo;
|
|---|
| 843 | else
|
|---|
| 844 | lPosDebug = cbLXFile;
|
|---|
| 845 |
|
|---|
| 846 | /*
|
|---|
| 847 | * Go to debug info position in the file and write debug info.
|
|---|
| 848 | */
|
|---|
| 849 | if (fseek(phFile, lPosDebug, SEEK_SET) == 0)
|
|---|
| 850 | {
|
|---|
| 851 | /*
|
|---|
| 852 | * Write the HLL data to disk.
|
|---|
| 853 | */
|
|---|
| 854 | cch = write(phFile);
|
|---|
| 855 | if (cch > 0)
|
|---|
| 856 | {
|
|---|
| 857 | /*
|
|---|
| 858 | * Update exeheader.
|
|---|
| 859 | */
|
|---|
| 860 | e32.e32_debuglen = cch;
|
|---|
| 861 | e32.e32_debuginfo = lPosDebug;
|
|---|
| 862 | if (fseek(phFile, lPosLXHdr, SEEK_SET) == 0)
|
|---|
| 863 | {
|
|---|
| 864 | /*
|
|---|
| 865 | * Write the updated header to disk.
|
|---|
| 866 | */
|
|---|
| 867 | cch = fwrite(&e32, 1, sizeof(e32), phFile);
|
|---|
| 868 | if (cch == sizeof(e32))
|
|---|
| 869 | rc = NO_ERROR;
|
|---|
| 870 | else
|
|---|
| 871 | rc = ERROR_WRITE_FAULT;
|
|---|
| 872 | }
|
|---|
| 873 | else
|
|---|
| 874 | rc = ERROR_SEEK;
|
|---|
| 875 | }
|
|---|
| 876 | else
|
|---|
| 877 | {
|
|---|
| 878 | fprintf(stderr, "error - write failed with cch=%d\n", cch);
|
|---|
| 879 | rc = ERROR_WRITE_FAULT;
|
|---|
| 880 | }
|
|---|
| 881 | }
|
|---|
| 882 | else
|
|---|
| 883 | rc = ERROR_SEEK;
|
|---|
| 884 | }
|
|---|
| 885 | else
|
|---|
| 886 | {
|
|---|
| 887 | fprintf(stderr, "error - debuginfo exists\n");
|
|---|
| 888 | rc = ERROR_BAD_EXE_FORMAT;
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | }
|
|---|
| 892 | else
|
|---|
| 893 | rc = ERROR_INVALID_EXE_SIGNATURE;
|
|---|
| 894 | }
|
|---|
| 895 | else
|
|---|
| 896 | rc = ERROR_BAD_EXE_FORMAT;
|
|---|
| 897 | }
|
|---|
| 898 | else
|
|---|
| 899 | rc = ERROR_BAD_EXE_FORMAT;
|
|---|
| 900 | }
|
|---|
| 901 | else
|
|---|
| 902 | rc = ERROR_READ_FAULT;
|
|---|
| 903 |
|
|---|
| 904 | fclose(phFile);
|
|---|
| 905 | }
|
|---|
| 906 | else
|
|---|
| 907 | rc = ERROR_ACCESS_DENIED; //?
|
|---|
| 908 |
|
|---|
| 909 |
|
|---|
| 910 | return rc;
|
|---|
| 911 | }
|
|---|
| 912 |
|
|---|
| 913 |
|
|---|
| 914 | /**
|
|---|
| 915 | * Find the size of a file.
|
|---|
| 916 | * @returns Size of file. -1 on error.
|
|---|
| 917 | * @param phFile File handle.
|
|---|
| 918 | */
|
|---|
| 919 | signed long fsize(FILE *phFile)
|
|---|
| 920 | {
|
|---|
| 921 | int ipos;
|
|---|
| 922 | signed long cb;
|
|---|
| 923 |
|
|---|
| 924 | if ((ipos = ftell(phFile)) < 0
|
|---|
| 925 | ||
|
|---|
| 926 | fseek(phFile, 0, SEEK_END) != 0
|
|---|
| 927 | ||
|
|---|
| 928 | (cb = ftell(phFile)) < 0
|
|---|
| 929 | ||
|
|---|
| 930 | fseek(phFile, ipos, SEEK_SET) != 0
|
|---|
| 931 | )
|
|---|
| 932 | cb = -1;
|
|---|
| 933 | return cb;
|
|---|
| 934 | }
|
|---|
| 935 |
|
|---|