| 1 | /* $Id: Sym2Hll.cpp,v 1.3 2000-03-27 10:20:41 bird Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * Sym2Hll - Symbol file to HLL debuginfo converter.
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
|---|
| 6 | *
|
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 | /*******************************************************************************
|
|---|
| 11 | * Defined Constants And Macros *
|
|---|
| 12 | *******************************************************************************/
|
|---|
| 13 | #define INCL_DOSERRORS
|
|---|
| 14 | #define WORD USHORT
|
|---|
| 15 | #define DWORD ULONG
|
|---|
| 16 |
|
|---|
| 17 | /*******************************************************************************
|
|---|
| 18 | * Internal Functions *
|
|---|
| 19 | *******************************************************************************/
|
|---|
| 20 | #include <os2.h>
|
|---|
| 21 | #include <exe386.h>
|
|---|
| 22 |
|
|---|
| 23 | #include <stdio.h>
|
|---|
| 24 | #include <malloc.h>
|
|---|
| 25 | #include <stdlib.h>
|
|---|
| 26 | #include <stddef.h>
|
|---|
| 27 | #include <string.h>
|
|---|
| 28 | #include <assert.h>
|
|---|
| 29 |
|
|---|
| 30 | #include "hll.h"
|
|---|
| 31 | #include "kList.h"
|
|---|
| 32 | #include "kHll.h"
|
|---|
| 33 | #include "sym.h"
|
|---|
| 34 | #include "kFileFormatBase.h"
|
|---|
| 35 | #include "kFileLX.h"
|
|---|
| 36 |
|
|---|
| 37 | /*******************************************************************************
|
|---|
| 38 | * Internal Functions *
|
|---|
| 39 | *******************************************************************************/
|
|---|
| 40 | void * readfile(const char *pszFilename);
|
|---|
| 41 | signed long fsize(FILE *phFile);
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | /*******************************************************************************
|
|---|
| 45 | * External Functions *
|
|---|
| 46 | *******************************************************************************/
|
|---|
| 47 | APIRET APIENTRY DosReplaceModule (PSZ pszOldModule, PSZ pszNewModule, PSZ pszBackupModule);
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | int main(int argc, char **argv)
|
|---|
| 52 | {
|
|---|
| 53 | kHll * pHll;
|
|---|
| 54 | kFileLX * pFileLX;
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | * Quite simple input currently:
|
|---|
| 58 | * <filename.sym> <filename.exe>
|
|---|
| 59 | */
|
|---|
| 60 | if (argc != 3)
|
|---|
| 61 | {
|
|---|
| 62 | fprintf(stderr, "syntax error\n");
|
|---|
| 63 | return -87;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | pHll = new kHll();
|
|---|
| 67 | assert(pHll != NULL);
|
|---|
| 68 |
|
|---|
| 69 | try {
|
|---|
| 70 | pFileLX = new kFileLX(argv[2]);
|
|---|
| 71 | } catch (int errcd)
|
|---|
| 72 | {
|
|---|
| 73 | fprintf(stderr, "failed to open/read LX file (%s). errcd=%d\n", argv[2], errcd);
|
|---|
| 74 | return -3;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | /*
|
|---|
| 80 | * Start conversion.
|
|---|
| 81 | */
|
|---|
| 82 | PBYTE pbSym = (PBYTE)readfile(argv[1]);
|
|---|
| 83 | if (pbSym != NULL)
|
|---|
| 84 | {
|
|---|
| 85 | int rc;
|
|---|
| 86 | kHllModuleEntry*pModule;
|
|---|
| 87 | PMAPDEF pMapDef; /* Mapfile header */
|
|---|
| 88 |
|
|---|
| 89 | pMapDef = (PMAPDEF)pbSym;
|
|---|
| 90 | while (pMapDef != NULL)
|
|---|
| 91 | {
|
|---|
| 92 | int iSegment;
|
|---|
| 93 | PSEGDEF pSegDef; /* Segment header */
|
|---|
| 94 |
|
|---|
| 95 | /*
|
|---|
| 96 | * Map definition.
|
|---|
| 97 | */
|
|---|
| 98 | printf("- Map definition -\n"
|
|---|
| 99 | " ppNextMap 0x%04x paragraph pointer to next map\n"
|
|---|
| 100 | " bFlags 0x%02x symbol types\n"
|
|---|
| 101 | " bReserved1 0x%02x reserved\n"
|
|---|
| 102 | " pSegEntry 0x%04x segment entry point value\n"
|
|---|
| 103 | " cConsts 0x%04x count of constants in map\n"
|
|---|
| 104 | " pConstDef 0x%04x pointer to constant chain\n"
|
|---|
| 105 | " cSegs 0x%04x count of segments in map\n"
|
|---|
| 106 | " ppSegDef 0x%04x paragraph pointer to first segment\n"
|
|---|
| 107 | " cbMaxSym 0x%02x maximum symbol-name length\n"
|
|---|
| 108 | " cbModName 0x%02x length of module name\n"
|
|---|
| 109 | " achModName %.*s\n"
|
|---|
| 110 | "\n",
|
|---|
| 111 | pMapDef->ppNextMap,
|
|---|
| 112 | pMapDef->bFlags,
|
|---|
| 113 | pMapDef->bReserved1,
|
|---|
| 114 | pMapDef->pSegEntry,
|
|---|
| 115 | pMapDef->cConsts,
|
|---|
| 116 | pMapDef->pConstDef,
|
|---|
| 117 | pMapDef->cSegs,
|
|---|
| 118 | pMapDef->ppSegDef,
|
|---|
| 119 | pMapDef->cbMaxSym,
|
|---|
| 120 | pMapDef->cbModName,
|
|---|
| 121 | pMapDef->cbModName,
|
|---|
| 122 | pMapDef->achModName
|
|---|
| 123 | );
|
|---|
| 124 |
|
|---|
| 125 | /*
|
|---|
| 126 | * Add Modulename.
|
|---|
| 127 | */
|
|---|
| 128 | pModule = pHll->addModule(pMapDef->achModName, pMapDef->cbModName, NULL);
|
|---|
| 129 | if (pModule == NULL)
|
|---|
| 130 | {
|
|---|
| 131 | fprintf(stderr, "addModule failed\n");
|
|---|
| 132 | return -3;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | /*
|
|---|
| 137 | * Read and convert segments with info.
|
|---|
| 138 | */
|
|---|
| 139 | pSegDef = SEGDEFPTR(pbSym, *pMapDef);
|
|---|
| 140 | iSegment = 1;
|
|---|
| 141 | while (pSegDef != NULL)
|
|---|
| 142 | {
|
|---|
| 143 | struct o32_obj *pLXObject;
|
|---|
| 144 | PSYMDEF32 pSymDef32; /* Symbol definition 32-bit */
|
|---|
| 145 | PSYMDEF16 pSymDef16; /* Symbol definition 16-bit */
|
|---|
| 146 | int iSym;
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | /*
|
|---|
| 150 | * Dump Segment definition.
|
|---|
| 151 | */
|
|---|
| 152 | printf(" - Segment Definition -\n"
|
|---|
| 153 | " ppNextSeg 0x%04x paragraph pointer to next segment\n"
|
|---|
| 154 | " cSymbols 0x%04x count of symbols in list\n"
|
|---|
| 155 | " pSymDef 0x%04x offset of symbol chain\n"
|
|---|
| 156 | " wReserved1 0x%04x reserved\n"
|
|---|
| 157 | " wReserved2 0x%04x reserved\n"
|
|---|
| 158 | " wReserved3 0x%04x reserved\n"
|
|---|
| 159 | " wReserved4 0x%04x reserved\n"
|
|---|
| 160 | " bFlags 0x%04x symbol types\n"
|
|---|
| 161 | " bReserved1 0x%04x reserved\n"
|
|---|
| 162 | " ppLineDef 0x%04x offset of line number record\n"
|
|---|
| 163 | " bReserved2 0x%04x reserved\n"
|
|---|
| 164 | " bReserved3 0x%04x reserved\n"
|
|---|
| 165 | " cbSegName 0x%04x length of segment name\n"
|
|---|
| 166 | " achSegName %.*s\n",
|
|---|
| 167 | pSegDef->ppNextSeg,
|
|---|
| 168 | pSegDef->cSymbols,
|
|---|
| 169 | pSegDef->pSymDef,
|
|---|
| 170 | pSegDef->wReserved1,
|
|---|
| 171 | pSegDef->wReserved2,
|
|---|
| 172 | pSegDef->wReserved3,
|
|---|
| 173 | pSegDef->wReserved4,
|
|---|
| 174 | pSegDef->bFlags,
|
|---|
| 175 | pSegDef->bReserved1,
|
|---|
| 176 | pSegDef->ppLineDef ,
|
|---|
| 177 | pSegDef->bReserved2,
|
|---|
| 178 | pSegDef->bReserved3,
|
|---|
| 179 | pSegDef->cbSegName,
|
|---|
| 180 | pSegDef->cbSegName,
|
|---|
| 181 | pSegDef->achSegName
|
|---|
| 182 | );
|
|---|
| 183 |
|
|---|
| 184 | /*
|
|---|
| 185 | * Add segment to the module - FIXME - need info from the LX Object table...
|
|---|
| 186 | */
|
|---|
| 187 | pLXObject = pFileLX->getObject(iSegment-1);
|
|---|
| 188 | if (pLXObject)
|
|---|
| 189 | {
|
|---|
| 190 | if (!pModule->addSegInfo(iSegment, 0, pLXObject->o32_size))
|
|---|
| 191 | fprintf(stderr, "warning: addseginfo failed!\n");
|
|---|
| 192 | }
|
|---|
| 193 | else
|
|---|
| 194 | fprintf(stderr, "warning: pFileLX->getObject failed for iSegment=%d\n",
|
|---|
| 195 | iSegment);
|
|---|
| 196 |
|
|---|
| 197 | /*
|
|---|
| 198 | * Read and convert symbols
|
|---|
| 199 | */
|
|---|
| 200 | for (iSym = 0; iSym < pSegDef->cSymbols; iSym++)
|
|---|
| 201 | {
|
|---|
| 202 | unsigned long offset;
|
|---|
| 203 | int cchName;
|
|---|
| 204 | const char * pachName;
|
|---|
| 205 | pSymDef32 = SYMDEFPTR32(pbSym, pSegDef, iSym);
|
|---|
| 206 | pSymDef16 = (PSYMDEF16)pSymDef32;
|
|---|
| 207 |
|
|---|
| 208 | if (SEG32BitSegment(*pSegDef))
|
|---|
| 209 | { /* pSymDef32 */
|
|---|
| 210 | offset = pSymDef32->wSymVal;
|
|---|
| 211 | cchName = pSymDef32->cbSymName;
|
|---|
| 212 | pachName = pSymDef32->achSymName;
|
|---|
| 213 | }
|
|---|
| 214 | else
|
|---|
| 215 | { /* pSymDef16 */
|
|---|
| 216 | offset = pSymDef16->wSymVal;
|
|---|
| 217 | cchName = pSymDef16->cbSymName;
|
|---|
| 218 | pachName = pSymDef16->achSymName;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | printf(" 0x%08x %.*s\n",
|
|---|
| 222 | offset,
|
|---|
| 223 | cchName,
|
|---|
| 224 | pachName);
|
|---|
| 225 |
|
|---|
| 226 | /*
|
|---|
| 227 | * Add symbol - currently we define it as public - it's a symbol local to this module really.
|
|---|
| 228 | */
|
|---|
| 229 | pModule->addPublicSymbol(pachName, cchName, offset, iSegment, 0);
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 | /*
|
|---|
| 234 | * Next segment
|
|---|
| 235 | */
|
|---|
| 236 | printf("\n");
|
|---|
| 237 | pSegDef = NEXTSEGDEFPTR(pbSym, *pSegDef);
|
|---|
| 238 | iSegment++;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 | /*
|
|---|
| 243 | * Next map
|
|---|
| 244 | */
|
|---|
| 245 | pMapDef = NEXTMAPDEFPTR(pbSym, *pMapDef);
|
|---|
| 246 | if (pMapDef != NULL)
|
|---|
| 247 | {
|
|---|
| 248 | if (pMapDef->ppNextMap == 0)
|
|---|
| 249 | { /* last map */
|
|---|
| 250 | PLAST_MAPDEF pLastMapDef = (PLAST_MAPDEF)pMapDef;
|
|---|
| 251 | printf("- Last Map definition -\n"
|
|---|
| 252 | " ppNextMap 0x%04x always zero\n"
|
|---|
| 253 | " version 0x%02x release number (minor version number)\n"
|
|---|
| 254 | " release 0x%02x major version number\n",
|
|---|
| 255 | pLastMapDef->ppNextMap,
|
|---|
| 256 | pLastMapDef->release,
|
|---|
| 257 | pLastMapDef->version
|
|---|
| 258 | );
|
|---|
| 259 | break;
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 | } /* Map loop */
|
|---|
| 263 |
|
|---|
| 264 | /*
|
|---|
| 265 | * debug
|
|---|
| 266 | */
|
|---|
| 267 | pHll->write("debug.hll");
|
|---|
| 268 | rc = pHll->writeToLX(argv[2]);
|
|---|
| 269 | if (rc == ERROR_ACCESS_DENIED)
|
|---|
| 270 | {
|
|---|
| 271 |
|
|---|
| 272 | rc = DosReplaceModule(argv[2], NULL, NULL);
|
|---|
| 273 | if (rc == NO_ERROR)
|
|---|
| 274 | {
|
|---|
| 275 | rc = pHll->writeToLX(argv[2]);
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 | }
|
|---|
| 279 | else
|
|---|
| 280 | {
|
|---|
| 281 | fprintf(stderr, "failed to open/read .sym file.\n");
|
|---|
| 282 | return -2;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 | return -1;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 | /**
|
|---|
| 298 | * Creates a memory buffer for a binary file.
|
|---|
| 299 | * @returns Pointer to file memoryblock. NULL on error.
|
|---|
| 300 | * @param pszFilename Pointer to filename string.
|
|---|
| 301 | * @remark This function is the one using most of the execution
|
|---|
| 302 | * time (DosRead + DosOpen) - about 70% of the execution time!
|
|---|
| 303 | */
|
|---|
| 304 | void *readfile(const char *pszFilename)
|
|---|
| 305 | {
|
|---|
| 306 | void *pvFile = NULL;
|
|---|
| 307 | FILE *phFile;
|
|---|
| 308 |
|
|---|
| 309 | phFile = fopen(pszFilename, "rb");
|
|---|
| 310 | if (phFile != NULL)
|
|---|
| 311 | {
|
|---|
| 312 | signed long cbFile = fsize(phFile);
|
|---|
| 313 | if (cbFile > 0)
|
|---|
| 314 | {
|
|---|
| 315 | pvFile = malloc(cbFile + 1);
|
|---|
| 316 | if (pvFile != NULL)
|
|---|
| 317 | {
|
|---|
| 318 | memset(pvFile, 0, cbFile + 1);
|
|---|
| 319 | if (fread(pvFile, 1, cbFile, phFile) == 0)
|
|---|
| 320 | { /* failed! */
|
|---|
| 321 | free(pvFile);
|
|---|
| 322 | pvFile = NULL;
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 | else
|
|---|
| 326 | fprintf(stderr, "warning/error: failed to open file %s\n", pszFilename);
|
|---|
| 327 | }
|
|---|
| 328 | fclose(phFile);
|
|---|
| 329 | }
|
|---|
| 330 | return pvFile;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 | #ifdef __IBMCPP__
|
|---|
| 337 | #include "klist.cpp"
|
|---|
| 338 | #endif
|
|---|