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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*
* Copyright (c) 1997 Christopher G. Demetriou. 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christopher G. Demetriou
* for the NetBSD Project.
* 4. 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.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
__RCSID("$NetBSD: exec_elf32.c,v 1.4 1997/08/12 06:07:24 mikel Exp $");
#endif
#endif
__FBSDID("$FreeBSD$");
#ifndef ELFSIZE
#define ELFSIZE 32
#endif
#include <sys/types.h>
#include <sys/endian.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
(defined(NLIST_ELF64) && (ELFSIZE == 64))
#define __ELF_WORD_SIZE ELFSIZE
#if (ELFSIZE == 32)
#include <sys/elf32.h>
#define xewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
#define htoxew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
#elif (ELFSIZE == 64)
#include <sys/elf64.h>
#define xewtoh(x) ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
#define htoxew(x) ((data == ELFDATA2MSB) ? htobe64(x) : htole64(x))
#endif
#include <sys/elf_generic.h>
#define CONCAT(x,y) __CONCAT(x,y)
#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
#define xe16toh(x) ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
#define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
#define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
struct listelem {
struct listelem *next;
void *mem;
off_t file;
size_t size;
};
static ssize_t
xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
{
ssize_t rv;
if (lseek(fd, off, SEEK_SET) != off) {
perror(fn);
return -1;
}
if ((rv = read(fd, buf, size)) != size) {
fprintf(stderr, "%s: read error: %s\n", fn,
rv == -1 ? strerror(errno) : "short read");
return -1;
}
return size;
}
static ssize_t
xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
{
ssize_t rv;
if (lseek(fd, off, SEEK_SET) != off) {
perror(fn);
return -1;
}
if ((rv = write(fd, buf, size)) != size) {
fprintf(stderr, "%s: write error: %s\n", fn,
rv == -1 ? strerror(errno) : "short write");
return -1;
}
return size;
}
static void *
xmalloc(size_t size, const char *fn, const char *use)
{
void *rv;
rv = malloc(size);
if (rv == NULL)
fprintf(stderr, "%s: out of memory (allocating for %s)\n",
fn, use);
return (rv);
}
int
ELFNAMEEND(check)(int fd, const char *fn)
{
Elf_Ehdr eh;
struct stat sb;
unsigned char data;
/*
* Check the header to maek sure it's an ELF file (of the
* appropriate size).
*/
if (fstat(fd, &sb) == -1)
return 0;
if (sb.st_size < sizeof eh)
return 0;
if (read(fd, &eh, sizeof eh) != sizeof eh)
return 0;
if (IS_ELF(eh) == 0)
return 0;
data = eh.e_ident[EI_DATA];
switch (xe16toh(eh.e_machine)) {
case EM_386: break;
case EM_ALPHA: break;
#ifndef EM_ARM
#define EM_ARM 40
#endif
case EM_ARM: break;
#ifndef EM_IA_64
#define EM_IA_64 50
#endif
case EM_IA_64: break;
#ifndef EM_PPC
#define EM_PPC 20
#endif
case EM_PPC: break;
#ifndef EM_SPARCV9
#define EM_SPARCV9 43
#endif
case EM_SPARCV9: break;
#ifndef EM_X86_64
#define EM_X86_64 62
#endif
case EM_X86_64: break;
/* ELFDEFNNAME(MACHDEP_ID_CASES) */
default:
return 0;
}
return 1;
}
int
ELFNAMEEND(hide)(int fd, const char *fn)
{
Elf_Ehdr ehdr;
Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr;
Elf_Sym *symtabp = NULL;
char *strtabp = NULL;
Elf_Size *symfwmap = NULL, *symrvmap = NULL, nsyms, nlocalsyms, ewi;
struct listelem *relalist = NULL, *rellist = NULL, *tmpl;
ssize_t shdrsize;
int rv, i, weird;
unsigned char data;
rv = 0;
if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
goto bad;
data = ehdr.e_ident[EI_DATA];
shdrsize = xe16toh(ehdr.e_shnum) * xe16toh(ehdr.e_shentsize);
if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
goto bad;
if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
shdrsize)
goto bad;
symtabshdr = strtabshdr = NULL;
weird = 0;
for (i = 0; i < xe16toh(ehdr.e_shnum); i++) {
switch (xe32toh(shdrp[i].sh_type)) {
case SHT_SYMTAB:
if (symtabshdr != NULL)
weird = 1;
symtabshdr = &shdrp[i];
strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
break;
case SHT_RELA:
tmpl = xmalloc(sizeof *tmpl, fn, "rela list element");
if (tmpl == NULL)
goto bad;
tmpl->mem = NULL;
tmpl->file = xewtoh(shdrp[i].sh_offset);
tmpl->size = xewtoh(shdrp[i].sh_size);
tmpl->next = relalist;
relalist = tmpl;
break;
case SHT_REL:
tmpl = xmalloc(sizeof *tmpl, fn, "rel list element");
if (tmpl == NULL)
goto bad;
tmpl->mem = NULL;
tmpl->file = xewtoh(shdrp[i].sh_offset);
tmpl->size = xewtoh(shdrp[i].sh_size);
tmpl->next = rellist;
rellist = tmpl;
break;
}
}
if (symtabshdr == NULL)
goto out;
if (strtabshdr == NULL)
weird = 1;
if (weird) {
fprintf(stderr, "%s: weird executable (unsupported)\n", fn);
goto bad;
}
/*
* load up everything we need
*/
/* symbol table */
if ((symtabp = xmalloc(xewtoh(symtabshdr->sh_size), fn, "symbol table"))
== NULL)
goto bad;
if (xreadatoff(fd, symtabp, xewtoh(symtabshdr->sh_offset),
xewtoh(symtabshdr->sh_size), fn) != xewtoh(symtabshdr->sh_size))
goto bad;
/* string table */
if ((strtabp = xmalloc(xewtoh(strtabshdr->sh_size), fn, "string table"))
== NULL)
goto bad;
if (xreadatoff(fd, strtabp, xewtoh(strtabshdr->sh_offset),
xewtoh(strtabshdr->sh_size), fn) != xewtoh(strtabshdr->sh_size))
goto bad;
/* any rela tables */
for (tmpl = relalist; tmpl != NULL; tmpl = tmpl->next) {
if ((tmpl->mem = xmalloc(tmpl->size, fn, "rela table"))
== NULL)
goto bad;
if (xreadatoff(fd, tmpl->mem, tmpl->file,
tmpl->size, fn) != tmpl->size)
goto bad;
}
/* any rel tables */
for (tmpl = rellist; tmpl != NULL; tmpl = tmpl->next) {
if ((tmpl->mem = xmalloc(tmpl->size, fn, "rel table"))
== NULL)
goto bad;
if (xreadatoff(fd, tmpl->mem, tmpl->file,
tmpl->size, fn) != tmpl->size)
goto bad;
}
/* Prepare data structures for symbol movement. */
nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
nlocalsyms = xe32toh(symtabshdr->sh_info);
if ((symfwmap = xmalloc(nsyms * sizeof (Elf_Size), fn,
"symbol forward mapping table")) == NULL)
goto bad;
if ((symrvmap = xmalloc(nsyms * sizeof (Elf_Size), fn,
"symbol reverse mapping table")) == NULL)
goto bad;
/* init location -> symbol # table */
for (ewi = 0; ewi < nsyms; ewi++)
symrvmap[ewi] = ewi;
/* move symbols, making them local */
for (ewi = nlocalsyms; ewi < nsyms; ewi++) {
Elf_Sym *sp, symswap;
Elf_Size mapswap;
sp = &symtabp[ewi];
/* if it's on our keep list, don't move it */
if (in_keep_list(strtabp + xe32toh(sp->st_name)))
continue;
/* if it's an undefined symbol, keep it */
if (xe16toh(sp->st_shndx) == SHN_UNDEF)
continue;
/* adjust the symbol so that it's local */
sp->st_info =
ELF_ST_INFO(STB_LOCAL, sp->st_info);
/* (STB_LOCAL << 4) | ELF_SYM_TYPE(sp->st_info); *//* XXX */
/*
* move the symbol to its new location
*/
/* note that symbols in those locations have been swapped */
mapswap = symrvmap[ewi];
symrvmap[ewi] = symrvmap[nlocalsyms];
symrvmap[nlocalsyms] = mapswap;
/* and swap the symbols */
symswap = *sp;
*sp = symtabp[nlocalsyms];
symtabp[nlocalsyms] = symswap;
nlocalsyms++; /* note new local sym */
}
symtabshdr->sh_info = htoxe32(nlocalsyms);
/* set up symbol # -> location mapping table */
for (ewi = 0; ewi < nsyms; ewi++)
symfwmap[symrvmap[ewi]] = ewi;
/* any rela tables */
for (tmpl = relalist; tmpl != NULL; tmpl = tmpl->next) {
Elf_Rela *relap = tmpl->mem;
for (ewi = 0; ewi < tmpl->size / sizeof(*relap); ewi++) {
relap[ewi].r_info = htoxew(ELF_R_INFO(
symfwmap[ELF_R_SYM(xewtoh(relap[ewi].r_info))],
ELF_R_TYPE(xewtoh(relap[ewi].r_info))
));
}
}
/* any rel tables */
for (tmpl = rellist; tmpl != NULL; tmpl = tmpl->next) {
Elf_Rel *relp = tmpl->mem;
for (ewi = 0; ewi < tmpl->size / sizeof *relp; ewi++) {
relp[ewi].r_info = htoxew(ELF_R_INFO(
symfwmap[ELF_R_SYM(xewtoh(relp[ewi].r_info))],
ELF_R_TYPE(xewtoh(relp[ewi].r_info))
));
}
}
/*
* write new tables to the file
*/
if (xwriteatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
shdrsize)
goto bad;
if (xwriteatoff(fd, symtabp, xewtoh(symtabshdr->sh_offset),
xewtoh(symtabshdr->sh_size), fn) != xewtoh(symtabshdr->sh_size))
goto bad;
for (tmpl = relalist; tmpl != NULL; tmpl = tmpl->next) {
if (xwriteatoff(fd, tmpl->mem, tmpl->file,
tmpl->size, fn) != tmpl->size)
goto bad;
}
for (tmpl = rellist; tmpl != NULL; tmpl = tmpl->next) {
if (xwriteatoff(fd, tmpl->mem, tmpl->file,
tmpl->size, fn) != tmpl->size)
goto bad;
}
out:
if (shdrp != NULL)
free(shdrp);
if (symtabp != NULL)
free(symtabp);
if (strtabp != NULL)
free(strtabp);
if (symfwmap != NULL)
free(symfwmap);
if (symrvmap != NULL)
free(symrvmap);
while ((tmpl = relalist) != NULL) {
relalist = tmpl->next;
if (tmpl->mem != NULL)
free(tmpl->mem);
free(tmpl);
}
while ((tmpl = rellist) != NULL) {
rellist = tmpl->next;
if (tmpl->mem != NULL)
free(tmpl->mem);
free(tmpl);
}
return (rv);
bad:
rv = 1;
goto out;
}
#endif /* include this size of ELF */
|