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