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