-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripomf.c
358 lines (316 loc) · 10.1 KB
/
stripomf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* $Id: stripomf.c 828 2003-10-10 23:38:11Z bird $
*
* OMF Debug Info stripper.
*
* Copyright (c) 2003 InnoTek Systemberatung GmbH
* Author: knut st. osmundsen <bird@anduin.net>
*
*
* This file is part of GCC.
*
* GCC is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GCC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GCC; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __EMX__
#include <io.h>
#endif
#include <sys/omflib.h>
#include "defs.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#ifndef min
#define min(a,b) ((a) <= (b) ? (a) : (b))
#endif
/*******************************************************************************
* Global Variables *
*******************************************************************************/
const char *pszPgm;
/**
* Finds the base name in the filename.
* @returns Pointer within pszFile.
* @param pszFile Filename.
*/
static const char * mybasename(const char *pszFile)
{
const char *psz;
psz = strrchr(pszFile, '\\');
if (!psz || strchr(psz, '/'))
psz = strrchr(pszFile, '/');
if (!psz || strchr(psz, ':'))
psz = strrchr(pszFile, ':');
if (psz)
psz++;
else
psz = pszFile;
return psz;
}
/**
* Main function.
* @returns 0 on success.
* @returns 8 on error.
* @param pszFile Name of file to strip.
*/
static int stripomf(const char *pszFile)
{
FILE * pIn;
FILE * pTmp;
unsigned cb;
int rc = 8;
/*
* Open input file and output file.
*/
pIn = fopen(pszFile, "rb+");
if (!pIn)
{
fprintf(stderr, "%s: failed to open file '%s'\n", pszPgm, pszFile);
return 8;
}
if ( fseek(pIn, 0, SEEK_END)
|| (cb = ftell(pIn)) < 0
|| fseek(pIn, 0, SEEK_SET)
)
{
fclose(pIn);
fprintf(stderr, "%s: failed to determin size of file '%s'\n", pszPgm, pszFile);
return 8;
}
pTmp = tmpfile();
if (pTmp)
{
int off = 0;
int iSegDef = 0;
int iName = 0;
int fPassThruPrev = 1;
int iNameTypes = -1;
int iNameSymbols = -1;
int iSegTypes = -1;
int iSegSymbols = -1;
while (off < cb)
{
#pragma pack(1)
static struct
{
struct omf_rec hdr;
char ach[2048];
} Rec;
#pragma pack()
int fPassThru = 1;
/* read header */
if (fread(&Rec.hdr, sizeof(Rec.hdr), 1, pIn) != 1)
{
fprintf(stderr, "%s: Failed reading from file '%s'\n", pszPgm, pszFile);
break;
}
if (Rec.hdr.rec_len > sizeof(Rec.ach))
{
fprintf(stderr, "%s: Record at offset %d in '%s' is too long\n", pszPgm, off, pszFile);
break;
}
if (Rec.hdr.rec_type == LIBHDR)
{
fprintf(stderr, "%s: OMF libraries are not yet supported ('%s').\n", pszPgm, pszFile);
break;
}
/* read the rest of the record. */
if (fread(Rec.ach, Rec.hdr.rec_len, 1, pIn) != 1)
{
fprintf(stderr, "%s: Failed reading from file '%s'\n", pszPgm, pszFile);
break;
}
fPassThru = 1;
switch (Rec.hdr.rec_type)
{
/*
* Need to clear the state.
*/
case THEADR:
iNameTypes = iNameSymbols = iSegTypes = iSegSymbols = -1;
iName = iSegDef = 0;
break;
/*
* Need to find the name table indexs of $$SYMBOLS and $$TYPES.
*/
case LNAMES:
{
int offRec = 0;
while (offRec < Rec.hdr.rec_len - 1)
{
int cchStr = Rec.ach[offRec++];
iName++;
if (cchStr == 9 && !strncmp(&Rec.ach[offRec], "$$SYMBOLS", 9))
iNameSymbols = iName;
else if (cchStr == 7 && !strncmp(&Rec.ach[offRec], "$$TYPES", 7))
iNameTypes = iName;
offRec += cchStr;
}
break;
}
/*
* Need to find the name table indexs of $$SYMBOLS and $$TYPES.
*/
case SEGDEF:
case SEGDEF|REC32:
{
int iName;
int offRec = (Rec.ach[0] >> 5) ? 1 : 4;
offRec += (Rec.hdr.rec_type & REC32) ? 4 : 2;
/** @todo support name indexes higher than 127 */
iName = Rec.ach[offRec];
iSegDef++;
if (iName == iNameSymbols)
{
iSegSymbols = iSegDef;
}
else if (iName == iNameTypes)
{
iSegTypes = iSegDef;
}
break;
}
/*
* Linenumbers - no question.
*/
case LINNUM:
case LINNUM|REC32:
fPassThru = 0;
break;
/*
* LEDATA if it's $$SYMBOLS or $$TYPES.
*/
case LEDATA:
case LEDATA|REC32:
{ /* @todo indexes higher than 127. */
int iSeg = Rec.ach[0];
if (iSeg == iSegTypes || iSeg == iSegSymbols)
fPassThru = 0;
break;
}
/*
* If previous record was debug info this one is fixups to it
* and must be removed.
*/
case FIXUPP:
case FIXUPP|REC32:
fPassThru = fPassThruPrev;
break;
}
if ( fPassThru
&& fwrite(&Rec, Rec.hdr.rec_len + sizeof(Rec.hdr), 1, pTmp) != 1)
{
fprintf(stderr, "%s: Failed to write to tempfile.\n", pszPgm);
break;
}
off += Rec.hdr.rec_len + sizeof(Rec.hdr);
fPassThruPrev = fPassThru;
} /* read loop */
/*
* Did we succeed reading?
*/
if (off >= cb)
{
/*
* Now we must copy the result from the temp file to pszFile.
*/
int cbToCopy;
if ( (cbToCopy = ftell(pTmp)) >= 0
&& !fseek(pIn, 0, SEEK_SET)
&& !fseek(pTmp, 0, SEEK_SET))
{
int cbNewSize = cbToCopy;
while (cbToCopy > 0)
{
static char achBuffer[0x10000];
int cbRead = min(sizeof(achBuffer), cbToCopy);
cbRead = fread(achBuffer, 1, cbRead, pTmp);
if (cbRead <= 0)
{
fprintf(stderr, "%s: Failed reading from tempfile\n", pszPgm);
break;
}
if (fwrite(achBuffer, cbRead, 1, pIn) != 1)
{
fprintf(stderr, "%s: Failed to write to '%s'\n", pszPgm, pszFile);
break;
}
cbToCopy -= cbRead;
} /* tmp -> file loop */
/* success? */
if (cbToCopy == 0)
{
fflush(pIn);
_chsize(fileno(pIn), cbNewSize);
rc = 0;
}
}
else
fprintf(stderr, "%s: Failed to seek to start.\n", pszPgm);
}
fclose(pTmp);
}
else
fprintf(stderr, "%s: failed to open temporary file\n", pszPgm);
fclose(pIn);
return rc;
}
/**
* Show program usage.
* @returns 8 (prefered program exist code).
*/
static int usage(void)
{
fputs("stripomf " VERSION INNOTEK_VERSION " -- "
"Copyright (c) 2003 by InnoTek Systemberatung GmbH\n\n"
"Usage: stripomf <input_file>\n\n",
stderr);
return 8;
}
int main(int argc, char **argv)
{
int argi;
pszPgm = mybasename(argv[0]);
_response (&argc, &argv);
_wildcard (&argc, &argv);
#ifdef __EMX__
if (_isterm (1))
setvbuf (stdout, NULL, _IOLBF, BUFSIZ);
else
setvbuf (stdout, NULL, _IOFBF, BUFSIZ);
#endif /* __EMX__ */
/*
* Arguments
*/
for (argi = 1; argi < argc; argi++)
{
if (argv[argi][0] == '-')
{
/* option!*/
return usage();
}
else
{
int rc = stripomf(argv[argi]);
if (rc != 0)
return rc;
}
}
return 0;
}