2 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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.
31 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: exec_elf32.c,v 1.6 1999/09/20 04:12:16 christos Exp $");
37 __FBSDID("$FreeBSD$");
43 #include <sys/types.h>
44 #include <sys/endian.h>
56 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
57 (defined(NLIST_ELF64) && (ELFSIZE == 64))
59 #define __ELF_WORD_SIZE ELFSIZE
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))
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))
74 #include <sys/elf_generic.h>
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)))
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))
92 xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
96 if (lseek(fd, off, SEEK_SET) != off) {
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");
109 xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
113 if (lseek(fd, off, SEEK_SET) != off) {
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");
126 xmalloc(size_t size, const char *fn, const char *use)
132 fprintf(stderr, "%s: out of memory (allocating for %s)\n",
138 xrealloc(void *ptr, size_t size, const char *fn, const char *use)
142 rv = realloc(ptr, size);
145 fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
152 ELFNAMEEND(check)(int fd, const char *fn)
159 * Check the header to maek sure it's an ELF file (of the
162 if (fstat(fd, &sb) == -1)
164 if (sb.st_size < (off_t)(sizeof eh))
166 if (read(fd, &eh, sizeof eh) != sizeof eh)
172 data = eh.e_ident[EI_DATA];
174 switch (xe16toh(eh.e_machine)) {
176 case EM_ALPHA: break;
184 #ifndef EM_MIPS_RS4_BE /* same as EM_MIPS_RS3_LE */
185 #define EM_MIPS_RS4_BE 10
188 case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
196 case EM_PPC64: break;
198 #define EM_SPARCV9 43
200 case EM_SPARCV9: break;
204 case EM_X86_64: break;
205 /* ELFDEFNNAME(MACHDEP_ID_CASES) */
215 * This function 'hides' (some of) ELF executable file's symbols.
216 * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
217 * Symbols in the global keep list, or which are marked as being undefined,
220 * An old version of this code shuffled various tables around, turning
221 * global symbols to be hidden into local symbols. That lost on the
222 * mips, because CALL16 relocs must reference global symbols, and, if
223 * those symbols were being hidden, they were no longer global.
225 * The new renaming behaviour doesn't take global symbols out of the
226 * namespace. However, it's ... unlikely that there will ever be
227 * any collisions in practice because of the new method.
230 ELFNAMEEND(hide)(int fd, const char *fn)
233 struct shlayout *layoutp = NULL;
234 Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
236 Elf_Sym *symtabp = NULL;
237 char *shstrtabp = NULL, *strtabp = NULL;
241 int rv, i, weird, l, m, r, strtabidx;
242 size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
243 char *nstrtabp = NULL;
245 const char *weirdreason = NULL;
250 if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
253 data = ehdr.e_ident[EI_DATA];
254 shnum = xe16toh(ehdr.e_shnum);
256 shdrsize = shnum * xe16toh(ehdr.e_shentsize);
257 if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
259 if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
263 symtabshdr = strtabshdr = shstrtabshdr = NULL;
265 for (i = 0; i < shnum; i++) {
266 switch (xe32toh(shdrp[i].sh_type)) {
268 if (symtabshdr != NULL) {
270 weirdreason = "multiple symbol tables";
272 symtabshdr = &shdrp[i];
273 strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
276 if (i == xe16toh(ehdr.e_shstrndx))
277 shstrtabshdr = &shdrp[i];
281 if (symtabshdr == NULL)
283 if (strtabshdr == NULL) {
285 weirdreason = "string table does not exist";
287 if (shstrtabshdr == NULL) {
289 weirdreason = "section header string table does not exist";
291 if (weirdreason == NULL)
292 weirdreason = "unsupported";
294 fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
299 * sort section layout table by offset
301 layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
306 /* add a pseudo entry to represent the section header table */
307 shdrshdr.sh_offset = ehdr.e_shoff;
308 shdrshdr.sh_size = htoxew(shdrsize);
309 shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
310 layoutp[shnum].shdr = &shdrshdr;
312 /* insert and sort normal section headers */
313 for (i = shnum; i-- != 0;) {
318 if (xewtoh(shdrp[i].sh_offset) >
319 xewtoh(layoutp[m].shdr->sh_offset))
326 memmove(&layoutp[i], &layoutp[i + 1],
327 sizeof(struct shlayout) * (r - i));
330 layoutp[r].shdr = &shdrp[i];
331 layoutp[r].bufp = NULL;
336 * load up everything we need
339 /* load section string table for debug use */
340 if ((shstrtabp = xmalloc(xewtoh(shstrtabshdr->sh_size), fn,
341 "section string table")) == NULL)
343 if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
344 xewtoh(shstrtabshdr->sh_size), fn) != xewtoh(shstrtabshdr->sh_size))
347 /* we need symtab, strtab, and everything behind strtab */
349 for (i = 0; i < shnum; i++) {
350 if (layoutp[i].shdr == &shdrshdr) {
351 /* not load section header again */
352 layoutp[i].bufp = shdrp;
355 if (layoutp[i].shdr == shstrtabshdr) {
356 /* not load section string table again */
357 layoutp[i].bufp = shstrtabp;
361 if (layoutp[i].shdr == strtabshdr)
363 if (layoutp[i].shdr == symtabshdr || i >= strtabidx) {
364 off = xewtoh(layoutp[i].shdr->sh_offset);
365 size = xewtoh(layoutp[i].shdr->sh_size);
366 layoutp[i].bufp = xmalloc(size, fn,
367 shstrtabp + xewtoh(layoutp[i].shdr->sh_name));
368 if (layoutp[i].bufp == NULL)
370 if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
374 /* set symbol table and string table */
375 if (layoutp[i].shdr == symtabshdr)
376 symtabp = layoutp[i].bufp;
377 else if (layoutp[i].shdr == strtabshdr)
378 strtabp = layoutp[i].bufp;
383 nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
384 if (nstrtabp == NULL)
388 fn_size = strlen(fn);
390 /* Prepare data structures for symbol movement. */
391 nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
393 /* move symbols, making them local */
394 for (ewi = 0; ewi < nsyms; ewi++) {
395 Elf_Sym *sp = &symtabp[ewi];
396 const char *symname = strtabp + xe32toh(sp->st_name);
399 * make sure there's size for the next entry, even if it's
400 * as large as it can be.
402 * "_$$hide$$ <filename> <symname><NUL>" ->
403 * 9 + 3 + sizes of fn and sym name
405 while ((nstrtab_size - nstrtab_nextoff) <
406 strlen(symname) + fn_size + 12) {
408 nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
410 if (nstrtabp == NULL)
414 sp->st_name = htowew(nstrtab_nextoff);
416 /* if it's a keeper or is undefined, don't rename it. */
417 if (in_keep_list(symname) ||
418 (xe16toh(sp->st_shndx) == SHN_UNDEF)) {
419 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
422 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
423 "_$$hide$$ %s %s", fn, symname) + 1;
425 nstrtab_nextoff += newent_len;
427 strtabshdr->sh_size = htoxew(nstrtab_nextoff);
430 * update section header table in ascending order of offset
432 for (i = strtabidx + 1; i < shnum; i++) {
434 off = xewtoh(layoutp[i - 1].shdr->sh_offset) +
435 xewtoh(layoutp[i - 1].shdr->sh_size);
436 align = xewtoh(layoutp[i].shdr->sh_addralign);
437 off = (off + (align - 1)) & ~(align - 1);
438 layoutp[i].shdr->sh_offset = htoxew(off);
442 * write data to the file in descending order of offset
444 for (i = shnum; i-- != 0;) {
445 if (layoutp[i].shdr == strtabshdr) {
446 /* new string table */
449 buf = layoutp[i].bufp;
451 if (layoutp[i].shdr == &shdrshdr ||
452 layoutp[i].shdr == symtabshdr || i >= strtabidx) {
457 * update the offset of section header table in elf
460 if (layoutp[i].shdr == &shdrshdr &&
461 ehdr.e_shoff != shdrshdr.sh_offset) {
462 ehdr.e_shoff = shdrshdr.sh_offset;
463 off = (ELFSIZE == 32) ? 32 : 44;
464 size = sizeof(Elf_Off);
465 if ((size_t)xwriteatoff(fd, &ehdr.e_shoff, off, size,
470 off = xewtoh(layoutp[i].shdr->sh_offset);
471 size = xewtoh(layoutp[i].shdr->sh_size);
472 if ((size_t)xwriteatoff(fd, buf, off, size, fn) != size)
478 if (layoutp != NULL) {
479 for (i = 0; i < shnum; i++) {
480 if (layoutp[i].bufp != NULL)
481 free(layoutp[i].bufp);
493 #endif /* include this size of ELF */