source: trunk/tools/dbginfo/Sym2Hll.cpp@ 3441

Last change on this file since 3441 was 3441, checked in by bird, 25 years ago

Fixed makefile, added missing sym.h and removed warnings in kHll.cpp and Sym2Hll.cpp.

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