1 /*
2 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Christopher G. Demetriou
15 * for the NetBSD Project.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #ifndef lint
33 #if 0
34 __RCSID("$NetBSD: exec_elf32.c,v 1.6 1999/09/20 04:12:16 christos Exp $");
35 #endif
36 #endif
37 __FBSDID("$FreeBSD: stable/9/usr.sbin/crunch/crunchide/exec_elf32.c 246895 2013-02-17 03:33:13Z pfg $");
38
39 #ifndef ELFSIZE
40 #define ELFSIZE 32
41 #endif
42
43 #include <sys/types.h>
44 #include <sys/endian.h>
45 #include <sys/stat.h>
46
47 #include <errno.h>
48 #include <limits.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include "extern.h"
55
56 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
57 (defined(NLIST_ELF64) && (ELFSIZE == 64))
58
59 #define __ELF_WORD_SIZE ELFSIZE
60 #if (ELFSIZE == 32)
61 #include <sys/elf32.h>
62 #define xewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
63 #define htoxew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
64 #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
65 #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
66 #elif (ELFSIZE == 64)
67 #include <sys/elf64.h>
68 #define xewtoh(x) ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
69 #define htoxew(x) ((data == ELFDATA2MSB) ? htobe64(x) : htole64(x))
70 /* elf64 Elf64_Word are 32 bits */
71 #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
72 #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
73 #endif
74 #include <sys/elf_generic.h>
75
76 #define CONCAT(x,y) __CONCAT(x,y)
77 #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
78 #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
79 #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
80 #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
81
82 #define xe16toh(x) ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
83 #define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
84 #define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
85
86 struct shlayout {
87 Elf_Shdr *shdr;
88 void *bufp;
89 };
90
91 static ssize_t
xreadatoff(int fd,void * buf,off_t off,size_t size,const char * fn)92 xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
93 {
94 ssize_t rv;
95
96 if (lseek(fd, off, SEEK_SET) != off) {
97 perror(fn);
98 return -1;
99 }
100 if ((size_t)(rv = read(fd, buf, size)) != size) {
101 fprintf(stderr, "%s: read error: %s\n", fn,
102 rv == -1 ? strerror(errno) : "short read");
103 return -1;
104 }
105 return size;
106 }
107
108 static ssize_t
xwriteatoff(int fd,void * buf,off_t off,size_t size,const char * fn)109 xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
110 {
111 ssize_t rv;
112
113 if (lseek(fd, off, SEEK_SET) != off) {
114 perror(fn);
115 return -1;
116 }
117 if ((size_t)(rv = write(fd, buf, size)) != size) {
118 fprintf(stderr, "%s: write error: %s\n", fn,
119 rv == -1 ? strerror(errno) : "short write");
120 return -1;
121 }
122 return size;
123 }
124
125 static void *
xmalloc(size_t size,const char * fn,const char * use)126 xmalloc(size_t size, const char *fn, const char *use)
127 {
128 void *rv;
129
130 rv = malloc(size);
131 if (rv == NULL)
132 fprintf(stderr, "%s: out of memory (allocating for %s)\n",
133 fn, use);
134 return (rv);
135 }
136
137 static void *
xrealloc(void * ptr,size_t size,const char * fn,const char * use)138 xrealloc(void *ptr, size_t size, const char *fn, const char *use)
139 {
140 void *rv;
141
142 rv = realloc(ptr, size);
143 if (rv == NULL) {
144 free(ptr);
145 fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
146 fn, use);
147 }
148 return (rv);
149 }
150
151 int
ELFNAMEEND(check)152 ELFNAMEEND(check)(int fd, const char *fn)
153 {
154 Elf_Ehdr eh;
155 struct stat sb;
156 unsigned char data;
157
158 /*
159 * Check the header to maek sure it's an ELF file (of the
160 * appropriate size).
161 */
162 if (fstat(fd, &sb) == -1)
163 return 0;
164 if (sb.st_size < (off_t)(sizeof eh))
165 return 0;
166 if (read(fd, &eh, sizeof eh) != sizeof eh)
167 return 0;
168
169 if (IS_ELF(eh) == 0)
170 return 0;
171
172 data = eh.e_ident[EI_DATA];
173
174 switch (xe16toh(eh.e_machine)) {
175 case EM_386: break;
176 case EM_ALPHA: break;
177 #ifndef EM_ARM
178 #define EM_ARM 40
179 #endif
180 case EM_ARM: break;
181 #ifndef EM_MIPS
182 #define EM_MIPS 8
183 #endif
184 #ifndef EM_MIPS_RS4_BE /* same as EM_MIPS_RS3_LE */
185 #define EM_MIPS_RS4_BE 10
186 #endif
187 case EM_MIPS: break;
188 case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
189 #ifndef EM_IA_64
190 #define EM_IA_64 50
191 #endif
192 case EM_IA_64: break;
193 #ifndef EM_PPC
194 #define EM_PPC 20
195 #endif
196 case EM_PPC: break;
197 #ifndef EM_PPC64
198 #define EM_PPC64 21
199 #endif
200 case EM_PPC64: break;
201 #ifndef EM_SPARCV9
202 #define EM_SPARCV9 43
203 #endif
204 case EM_SPARCV9: break;
205 #ifndef EM_X86_64
206 #define EM_X86_64 62
207 #endif
208 case EM_X86_64: break;
209 /* ELFDEFNNAME(MACHDEP_ID_CASES) */
210
211 default:
212 return 0;
213 }
214
215 return 1;
216 }
217
218 /*
219 * This function 'hides' (some of) ELF executable file's symbols.
220 * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
221 * Symbols in the global keep list, or which are marked as being undefined,
222 * are left alone.
223 *
224 * An old version of this code shuffled various tables around, turning
225 * global symbols to be hidden into local symbols. That lost on the
226 * mips, because CALL16 relocs must reference global symbols, and, if
227 * those symbols were being hidden, they were no longer global.
228 *
229 * The new renaming behaviour doesn't take global symbols out of the
230 * namespace. However, it's ... unlikely that there will ever be
231 * any collisions in practice because of the new method.
232 */
233 int
ELFNAMEEND(hide)234 ELFNAMEEND(hide)(int fd, const char *fn)
235 {
236 Elf_Ehdr ehdr;
237 struct shlayout *layoutp = NULL;
238 Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
239 Elf_Shdr shdrshdr;
240 Elf_Sym *symtabp = NULL;
241 char *shstrtabp = NULL, *strtabp = NULL;
242 Elf_Size nsyms, ewi;
243 Elf_Off off;
244 ssize_t shdrsize;
245 int rv, i, weird, l, m, r, strtabidx;
246 size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
247 char *nstrtabp = NULL;
248 unsigned char data;
249 const char *weirdreason = NULL;
250 void *buf;
251 Elf_Half shnum;
252
253 rv = 0;
254 if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
255 goto bad;
256
257 data = ehdr.e_ident[EI_DATA];
258 shnum = xe16toh(ehdr.e_shnum);
259
260 shdrsize = shnum * xe16toh(ehdr.e_shentsize);
261 if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
262 goto bad;
263 if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
264 shdrsize)
265 goto bad;
266
267 symtabshdr = strtabshdr = shstrtabshdr = NULL;
268 weird = 0;
269 for (i = 0; i < shnum; i++) {
270 switch (xe32toh(shdrp[i].sh_type)) {
271 case SHT_SYMTAB:
272 if (symtabshdr != NULL) {
273 weird = 1;
274 weirdreason = "multiple symbol tables";
275 }
276 symtabshdr = &shdrp[i];
277 strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
278 break;
279 case SHT_STRTAB:
280 if (i == xe16toh(ehdr.e_shstrndx))
281 shstrtabshdr = &shdrp[i];
282 break;
283 }
284 }
285 if (symtabshdr == NULL)
286 goto out;
287 if (strtabshdr == NULL) {
288 weird = 1;
289 weirdreason = "string table does not exist";
290 }
291 if (shstrtabshdr == NULL) {
292 weird = 1;
293 weirdreason = "section header string table does not exist";
294 }
295 if (weirdreason == NULL)
296 weirdreason = "unsupported";
297 if (weird) {
298 fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
299 goto bad;
300 }
301
302 /*
303 * sort section layout table by offset
304 */
305 layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
306 fn, "layout table");
307 if (layoutp == NULL)
308 goto bad;
309
310 /* add a pseudo entry to represent the section header table */
311 shdrshdr.sh_offset = ehdr.e_shoff;
312 shdrshdr.sh_size = htoxew(shdrsize);
313 shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
314 layoutp[shnum].shdr = &shdrshdr;
315
316 /* insert and sort normal section headers */
317 for (i = shnum; i-- != 0;) {
318 l = i + 1;
319 r = shnum;
320 while (l <= r) {
321 m = ( l + r) / 2;
322 if (xewtoh(shdrp[i].sh_offset) >
323 xewtoh(layoutp[m].shdr->sh_offset))
324 l = m + 1;
325 else
326 r = m - 1;
327 }
328
329 if (r != i) {
330 memmove(&layoutp[i], &layoutp[i + 1],
331 sizeof(struct shlayout) * (r - i));
332 }
333
334 layoutp[r].shdr = &shdrp[i];
335 layoutp[r].bufp = NULL;
336 }
337 ++shnum;
338
339 /*
340 * load up everything we need
341 */
342
343 /* load section string table for debug use */
344 if ((shstrtabp = xmalloc(xewtoh(shstrtabshdr->sh_size), fn,
345 "section string table")) == NULL)
346 goto bad;
347 if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
348 xewtoh(shstrtabshdr->sh_size), fn) != xewtoh(shstrtabshdr->sh_size))
349 goto bad;
350
351 /* we need symtab, strtab, and everything behind strtab */
352 strtabidx = INT_MAX;
353 for (i = 0; i < shnum; i++) {
354 if (layoutp[i].shdr == &shdrshdr) {
355 /* not load section header again */
356 layoutp[i].bufp = shdrp;
357 continue;
358 }
359 if (layoutp[i].shdr == shstrtabshdr) {
360 /* not load section string table again */
361 layoutp[i].bufp = shstrtabp;
362 continue;
363 }
364
365 if (layoutp[i].shdr == strtabshdr)
366 strtabidx = i;
367 if (layoutp[i].shdr == symtabshdr || i >= strtabidx) {
368 off = xewtoh(layoutp[i].shdr->sh_offset);
369 size = xewtoh(layoutp[i].shdr->sh_size);
370 layoutp[i].bufp = xmalloc(size, fn,
371 shstrtabp + xewtoh(layoutp[i].shdr->sh_name));
372 if (layoutp[i].bufp == NULL)
373 goto bad;
374 if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
375 size)
376 goto bad;
377
378 /* set symbol table and string table */
379 if (layoutp[i].shdr == symtabshdr)
380 symtabp = layoutp[i].bufp;
381 else if (layoutp[i].shdr == strtabshdr)
382 strtabp = layoutp[i].bufp;
383 }
384 }
385
386 nstrtab_size = 256;
387 nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
388 if (nstrtabp == NULL)
389 goto bad;
390 nstrtab_nextoff = 0;
391
392 fn_size = strlen(fn);
393
394 /* Prepare data structures for symbol movement. */
395 nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
396
397 /* move symbols, making them local */
398 for (ewi = 0; ewi < nsyms; ewi++) {
399 Elf_Sym *sp = &symtabp[ewi];
400 const char *symname = strtabp + xe32toh(sp->st_name);
401 size_t newent_len;
402 /*
403 * make sure there's size for the next entry, even if it's
404 * as large as it can be.
405 *
406 * "_$$hide$$ <filename> <symname><NUL>" ->
407 * 9 + 3 + sizes of fn and sym name
408 */
409 while ((nstrtab_size - nstrtab_nextoff) <
410 strlen(symname) + fn_size + 12) {
411 nstrtab_size *= 2;
412 nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
413 "new string table");
414 if (nstrtabp == NULL)
415 goto bad;
416 }
417
418 sp->st_name = htowew(nstrtab_nextoff);
419
420 /* if it's a keeper or is undefined, don't rename it. */
421 if (in_keep_list(symname) ||
422 (xe16toh(sp->st_shndx) == SHN_UNDEF)) {
423 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
424 "%s", symname) + 1;
425 } else {
426 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
427 "_$$hide$$ %s %s", fn, symname) + 1;
428 }
429 nstrtab_nextoff += newent_len;
430 }
431 strtabshdr->sh_size = htoxew(nstrtab_nextoff);
432
433 /*
434 * update section header table in ascending order of offset
435 */
436 for (i = strtabidx + 1; i < shnum; i++) {
437 Elf_Off off, align;
438 off = xewtoh(layoutp[i - 1].shdr->sh_offset) +
439 xewtoh(layoutp[i - 1].shdr->sh_size);
440 align = xewtoh(layoutp[i].shdr->sh_addralign);
441 off = (off + (align - 1)) & ~(align - 1);
442 layoutp[i].shdr->sh_offset = htoxew(off);
443 }
444
445 /*
446 * write data to the file in descending order of offset
447 */
448 for (i = shnum; i-- != 0;) {
449 if (layoutp[i].shdr == strtabshdr) {
450 /* new string table */
451 buf = nstrtabp;
452 } else
453 buf = layoutp[i].bufp;
454
455 if (layoutp[i].shdr == &shdrshdr ||
456 layoutp[i].shdr == symtabshdr || i >= strtabidx) {
457 if (buf == NULL)
458 goto bad;
459
460 /*
461 * update the offset of section header table in elf
462 * header if needed.
463 */
464 if (layoutp[i].shdr == &shdrshdr &&
465 ehdr.e_shoff != shdrshdr.sh_offset) {
466 ehdr.e_shoff = shdrshdr.sh_offset;
467 off = (ELFSIZE == 32) ? 32 : 44;
468 size = sizeof(Elf_Off);
469 if ((size_t)xwriteatoff(fd, &ehdr.e_shoff, off, size,
470 fn) != size)
471 goto bad;
472 }
473
474 off = xewtoh(layoutp[i].shdr->sh_offset);
475 size = xewtoh(layoutp[i].shdr->sh_size);
476 if ((size_t)xwriteatoff(fd, buf, off, size, fn) != size)
477 goto bad;
478 }
479 }
480
481 out:
482 if (layoutp != NULL) {
483 for (i = 0; i < shnum; i++) {
484 if (layoutp[i].bufp != NULL)
485 free(layoutp[i].bufp);
486 }
487 free(layoutp);
488 }
489 free(nstrtabp);
490 return (rv);
491
492 bad:
493 rv = 1;
494 goto out;
495 }
496
497 #endif /* include this size of ELF */
498