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
|
/* $OpenBSD: elf2olf.c,v 1.3 1996/12/09 07:10:14 deraadt Exp $ */
/*
* Copyright (c) 1996 Erik Theisen. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1996 Erik Theisen. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char rcsid[] = "@(#) $Id: elf2olf.c,v 1.3 1996/12/09 07:10:14 deraadt Exp $";
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <olf_abi.h>
int retval = 0;
int olf2elf;
char *progname;
int verbose;
int opsys = OS_ID;
char *os_namev[] = ONAMEV;
/* Handle endianess */
#define word(x,y)((y == ELFDATA2LSB) ? ntohl(htonl(x)) : ntohl(x))
#define half(x,y)((y == ELFDATA2LSB) ? ntohs(htons(x)) : ntohs(x))
int
main(int argc, char*argv[])
{
extern char *optarg;
extern int optind;
register int ch, i, okay;
char *opstring;
register int fd;
struct stat st;
Elf32_Ehdr ehdr;
Elf32_Shdr shdr;
int e;
if (progname = strrchr(*argv, '/'))
++progname;
else
progname = *argv;
if (strstr(progname, "olf2elf"))
olf2elf = 1;
/*
* Process cmdline
*/
opstring = olf2elf ? "v" : "vo:";
while((ch = getopt(argc, argv, opstring)) != EOF)
switch(ch) {
case 'v':
verbose = 1;
break;
case 'o':
for (i = 1; i <= OOS_NUM; i++) {
if (os_namev[i] == NULL) {
fprintf(stderr,
"%s: illegal -o argument -- %s\n",
progname, optarg);
usage();
}
else if (strcmp(optarg, os_namev[i]) == 0) {
opsys = i;
break;
}
}
break;
default:
usage();
}
argc -= optind;
argv += optind;
if (argc == 0)
usage();
/*
* Process file(s)
*/
do {
okay = 0;
if ((fd = open(*argv, O_RDWR | O_EXLOCK, 0)) > 0 &&
lseek(fd, (off_t)0, SEEK_SET) == 0 &&
fstat(fd, &st) == 0) {
/* Make sure this is a 32bit ELF or OLF version 1 file */
if (read(fd, &ehdr, sizeof(Elf32_Ehdr)) == sizeof(Elf32_Ehdr)&&
(IS_ELF(ehdr) || IS_OLF(ehdr)) &&
ehdr.e_ident[EI_CLASS] == ELFCLASS32 &&
ehdr.e_ident[EI_VERSION] == 1) {
/* Is this elf2olf? */
if(!olf2elf) {
/* Tag, your it... */
ehdr.e_ident[OI_MAG0] = OLFMAG0;
ehdr.e_ident[OI_MAG1] = OLFMAG1;
ehdr.e_ident[OI_MAG2] = OLFMAG2;
ehdr.e_ident[OI_MAG3] = OLFMAG3;
ehdr.e_ident[OI_OS] = opsys;
ehdr.e_ident[OI_DYNAMIC] = ODYNAMIC_N;
ehdr.e_ident[OI_STRIP] = OSTRIP;
/* We'll need this endian */
e = ehdr.e_ident[EI_DATA];
/* Now we need to figure out wether or */
/* not we're really stripped. */
if (lseek(fd, (off_t)word(ehdr.e_shoff, e),
SEEK_SET) == word(ehdr.e_shoff, e)) {
/*
* search through section header table
* looking for a section header of type
* SHT_SYMTAB and SHT_DYNAMIC. If there is
* one present we're NOT stripped and/or
* dynamic.
*/
for (i = 0; i < half(ehdr.e_shnum, e); i++) {
if (read(fd, &shdr, sizeof(Elf32_Shdr)) == sizeof(Elf32_Shdr)){
if (word(shdr.sh_type, e) == SHT_SYMTAB)
ehdr.e_ident[OI_STRIP] = OSTRIP_N;
else if (word(shdr.sh_type, e) == SHT_DYNAMIC)
ehdr.e_ident[OI_DYNAMIC] = ODYNAMIC;
} else
warn(progname, *argv, errno);
} /* while less than number of section headers */
/* We're ready to modify */
okay = 1;
} else /* Bogus section header table seek */
warn(progname, *argv, errno);
} else { /* olf2elf */
ehdr.e_ident[EI_MAG0] = ELFMAG0;
ehdr.e_ident[EI_MAG1] = ELFMAG1;
ehdr.e_ident[EI_MAG2] = ELFMAG2;
ehdr.e_ident[EI_MAG3] = ELFMAG3;
ehdr.e_ident[OI_OS] = 0;
ehdr.e_ident[OI_DYNAMIC] = 0;
ehdr.e_ident[OI_STRIP] = 0;
okay = 1;
} /* olf2elf */
} else /* Bogus non-ELF file encountered */
warn(progname, *argv, ENOEXEC);
/*
* Do It.
*/
if (okay) {
if (lseek(fd, (off_t)0, SEEK_SET) == 0) {
if (write(fd, &ehdr, sizeof(Elf32_Ehdr)) == sizeof(Elf32_Ehdr)) {
if (verbose) {
if (!olf2elf) {
printf("ELF %s => OLF %d-bit %s %s linked %s OLF.\n",
*argv,
(ehdr.e_ident[OI_CLASS] == OLFCLASS32)?\
32 : 64,
os_namev[ehdr.e_ident[OI_OS]],
ehdr.e_ident[OI_DYNAMIC] ? \
"dynamically" : "statically",
!ehdr.e_ident[OI_STRIP] ?
"stripped" : "unstripped");
} else
printf("OLF %s => ELF.\n", *argv);
}
} else /* bad write */
warn(progname, *argv, errno);
} else /* bad seek */
warn(progname, *argv, errno);
} /* okay? */
fsync(fd);
close(fd);
} else /* couldn't handle file */
warn(progname, *argv, errno);
} while (*(++argv) != NULL);
return (retval);
}
warn(name, fname, errval)
char *name;
char *fname;
int errval;
{
fprintf(stderr, "%s: %s: %s.\n", name, fname, strerror(errval));
retval = 1;
}
usage()
{
register int i;
int col = 8;
if (olf2elf) {
fprintf(stderr, "usage: %s [-v] file ...\n", progname);
} else {
fprintf(stderr, "usage: %s [-v] [-o opsys] elffile ...\n", progname);
fprintf(stderr, "where opsys is:\n\t");
for (i = 1; os_namev[i] != NULL; i++) {
col = col + strlen(os_namev[i]) + 2;
if (col > 78) {
fprintf(stderr, "\n\t");
col = 8;
}
fprintf(stderr, "%s%s", os_namev[i],
os_namev[i+1] ? ", " : "\n");
}
}
exit(1);
}
|