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

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

Adds a source name entry with the same name as the module. This way the
PM Debugger will accept the debugdata. It seems to work quit good actually.

File size: 11.0 KB
Line 
1/* $Id: Sym2Hll.cpp,v 1.4 2000-04-07 02:49: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* 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 * readfile(const char *pszFilename);
41signed long fsize(FILE *phFile);
42
43
44/*******************************************************************************
45* External Functions *
46*******************************************************************************/
47APIRET APIENTRY DosReplaceModule (PSZ pszOldModule, PSZ pszNewModule, PSZ pszBackupModule);
48
49
50
51int 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 pModule->getSourceEntry()->addFile(pMapDef->achModName, pMapDef->cbModName);
135
136
137 /*
138 * Read and convert segments with info.
139 */
140 pSegDef = SEGDEFPTR(pbSym, *pMapDef);
141 iSegment = 1;
142 while (pSegDef != NULL)
143 {
144 struct o32_obj *pLXObject;
145 PSYMDEF32 pSymDef32; /* Symbol definition 32-bit */
146 PSYMDEF16 pSymDef16; /* Symbol definition 16-bit */
147 int iSym;
148
149
150 /*
151 * Dump Segment definition.
152 */
153 printf(" - Segment Definition -\n"
154 " ppNextSeg 0x%04x paragraph pointer to next segment\n"
155 " cSymbols 0x%04x count of symbols in list\n"
156 " pSymDef 0x%04x offset of symbol chain\n"
157 " wReserved1 0x%04x reserved\n"
158 " wReserved2 0x%04x reserved\n"
159 " wReserved3 0x%04x reserved\n"
160 " wReserved4 0x%04x reserved\n"
161 " bFlags 0x%04x symbol types\n"
162 " bReserved1 0x%04x reserved\n"
163 " ppLineDef 0x%04x offset of line number record\n"
164 " bReserved2 0x%04x reserved\n"
165 " bReserved3 0x%04x reserved\n"
166 " cbSegName 0x%04x length of segment name\n"
167 " achSegName %.*s\n",
168 pSegDef->ppNextSeg,
169 pSegDef->cSymbols,
170 pSegDef->pSymDef,
171 pSegDef->wReserved1,
172 pSegDef->wReserved2,
173 pSegDef->wReserved3,
174 pSegDef->wReserved4,
175 pSegDef->bFlags,
176 pSegDef->bReserved1,
177 pSegDef->ppLineDef ,
178 pSegDef->bReserved2,
179 pSegDef->bReserved3,
180 pSegDef->cbSegName,
181 pSegDef->cbSegName,
182 pSegDef->achSegName
183 );
184
185 /*
186 * Add segment to the module - FIXME - need info from the LX Object table...
187 */
188 pLXObject = pFileLX->getObject(iSegment-1);
189 if (pLXObject)
190 {
191 if (!pModule->addSegInfo(iSegment, 0, pLXObject->o32_size))
192 fprintf(stderr, "warning: addseginfo failed!\n");
193 }
194 else
195 fprintf(stderr, "warning: pFileLX->getObject failed for iSegment=%d\n",
196 iSegment);
197
198 /*
199 * Read and convert symbols
200 */
201 for (iSym = 0; iSym < pSegDef->cSymbols; iSym++)
202 {
203 unsigned long offset;
204 int cchName;
205 const char * pachName;
206 pSymDef32 = SYMDEFPTR32(pbSym, pSegDef, iSym);
207 pSymDef16 = (PSYMDEF16)pSymDef32;
208
209 if (SEG32BitSegment(*pSegDef))
210 { /* pSymDef32 */
211 offset = pSymDef32->wSymVal;
212 cchName = pSymDef32->cbSymName;
213 pachName = pSymDef32->achSymName;
214 }
215 else
216 { /* pSymDef16 */
217 offset = pSymDef16->wSymVal;
218 cchName = pSymDef16->cbSymName;
219 pachName = pSymDef16->achSymName;
220 }
221
222 printf(" 0x%08x %.*s\n",
223 offset,
224 cchName,
225 pachName);
226
227 /*
228 * Add symbol - currently we define it as public - it's a symbol local to this module really.
229 */
230 pModule->addPublicSymbol(pachName, cchName, offset, iSegment, 0);
231 }
232
233
234 /*
235 * Next segment
236 */
237 printf("\n");
238 pSegDef = NEXTSEGDEFPTR(pbSym, *pSegDef);
239 iSegment++;
240 }
241
242
243 /*
244 * Next map
245 */
246 pMapDef = NEXTMAPDEFPTR(pbSym, *pMapDef);
247 if (pMapDef != NULL)
248 {
249 if (pMapDef->ppNextMap == 0)
250 { /* last map */
251 PLAST_MAPDEF pLastMapDef = (PLAST_MAPDEF)pMapDef;
252 printf("- Last Map definition -\n"
253 " ppNextMap 0x%04x always zero\n"
254 " version 0x%02x release number (minor version number)\n"
255 " release 0x%02x major version number\n",
256 pLastMapDef->ppNextMap,
257 pLastMapDef->release,
258 pLastMapDef->version
259 );
260 break;
261 }
262 }
263 } /* Map loop */
264
265 /*
266 * debug
267 */
268 pHll->write("debug.hll");
269 rc = pHll->writeToLX(argv[2]);
270 if (rc == ERROR_ACCESS_DENIED)
271 {
272
273 rc = DosReplaceModule(argv[2], NULL, NULL);
274 if (rc == NO_ERROR)
275 {
276 rc = pHll->writeToLX(argv[2]);
277 }
278 }
279 }
280 else
281 {
282 fprintf(stderr, "failed to open/read .sym file.\n");
283 return -2;
284 }
285
286
287
288
289
290
291 return -1;
292}
293
294
295
296
297
298/**
299 * Creates a memory buffer for a binary file.
300 * @returns Pointer to file memoryblock. NULL on error.
301 * @param pszFilename Pointer to filename string.
302 * @remark This function is the one using most of the execution
303 * time (DosRead + DosOpen) - about 70% of the execution time!
304 */
305void *readfile(const char *pszFilename)
306{
307 void *pvFile = NULL;
308 FILE *phFile;
309
310 phFile = fopen(pszFilename, "rb");
311 if (phFile != NULL)
312 {
313 signed long cbFile = fsize(phFile);
314 if (cbFile > 0)
315 {
316 pvFile = malloc(cbFile + 1);
317 if (pvFile != NULL)
318 {
319 memset(pvFile, 0, cbFile + 1);
320 if (fread(pvFile, 1, cbFile, phFile) == 0)
321 { /* failed! */
322 free(pvFile);
323 pvFile = NULL;
324 }
325 }
326 else
327 fprintf(stderr, "warning/error: failed to open file %s\n", pszFilename);
328 }
329 fclose(phFile);
330 }
331 return pvFile;
332}
333
334
335
336
337#ifdef __IBMCPP__
338#include "klist.cpp"
339#endif
Note: See TracBrowser for help on using the repository browser.