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>
57 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
58 (defined(NLIST_ELF64) && (ELFSIZE == 64))
60 #define __ELF_WORD_SIZE ELFSIZE
62 #include <sys/elf32.h>
63 #define xewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
64 #define htoxew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
65 #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
66 #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
68 #include <sys/elf64.h>
69 #define xewtoh(x) ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
70 #define htoxew(x) ((data == ELFDATA2MSB) ? htobe64(x) : htole64(x))
71 /* elf64 Elf64_Word are 32 bits */
72 #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
73 #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
75 #include <sys/elf_generic.h>
77 #define CONCAT(x,y) __CONCAT(x,y)
78 #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
79 #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
80 #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
81 #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
83 #define xe16toh(x) ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
84 #define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
85 #define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
93 xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
97 if (lseek(fd, off, SEEK_SET) != off) {
101 if ((size_t)(rv = read(fd, buf, size)) != size) {
102 fprintf(stderr, "%s: read error: %s\n", fn,
103 rv == -1 ? strerror(errno) : "short read");
110 xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
114 if (lseek(fd, off, SEEK_SET) != off) {
118 if ((size_t)(rv = write(fd, buf, size)) != size) {
119 fprintf(stderr, "%s: write error: %s\n", fn,
120 rv == -1 ? strerror(errno) : "short write");
127 xmalloc(size_t size, const char *fn, const char *use)
133 fprintf(stderr, "%s: out of memory (allocating for %s)\n",
139 xrealloc(void *ptr, size_t size, const char *fn, const char *use)
143 rv = realloc(ptr, size);
146 fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
153 ELFNAMEEND(check)(int fd, const char *fn)
160 * Check the header to maek sure it's an ELF file (of the
163 if (fstat(fd, &sb) == -1)
165 if (sb.st_size < (off_t)(sizeof eh))
167 if (read(fd, &eh, sizeof eh) != sizeof eh)
173 data = eh.e_ident[EI_DATA];
175 switch (xe16toh(eh.e_machine)) {
177 case EM_ALPHA: break;
179 #define EM_AARCH64 183
181 case EM_AARCH64: break;
189 #ifndef EM_MIPS_RS4_BE /* same as EM_MIPS_RS3_LE */
190 #define EM_MIPS_RS4_BE 10
193 case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
201 case EM_PPC64: break;
203 #define EM_SPARCV9 43
205 case EM_SPARCV9: break;
209 case EM_X86_64: break;
210 /* ELFDEFNNAME(MACHDEP_ID_CASES) */
220 * This function 'hides' (some of) ELF executable file's symbols.
221 * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
222 * Symbols in the global keep list, or which are marked as being undefined,
225 * An old version of this code shuffled various tables around, turning
226 * global symbols to be hidden into local symbols. That lost on the
227 * mips, because CALL16 relocs must reference global symbols, and, if
228 * those symbols were being hidden, they were no longer global.
230 * The new renaming behaviour doesn't take global symbols out of the
231 * namespace. However, it's ... unlikely that there will ever be
232 * any collisions in practice because of the new method.
235 ELFNAMEEND(hide)(int fd, const char *fn)
238 struct shlayout *layoutp = NULL;
239 Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
241 Elf_Sym *symtabp = NULL;
242 char *shstrtabp = NULL, *strtabp = NULL;
246 int rv, i, weird, l, m, r, strtabidx;
247 size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
248 char *nstrtabp = NULL;
250 const char *weirdreason = NULL;
255 if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
258 data = ehdr.e_ident[EI_DATA];
259 shnum = xe16toh(ehdr.e_shnum);
261 shdrsize = shnum * xe16toh(ehdr.e_shentsize);
262 if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
264 if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
268 symtabshdr = strtabshdr = shstrtabshdr = NULL;
270 for (i = 0; i < shnum; i++) {
271 switch (xe32toh(shdrp[i].sh_type)) {
273 if (symtabshdr != NULL) {
275 weirdreason = "multiple symbol tables";
277 symtabshdr = &shdrp[i];
278 strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
281 if (i == xe16toh(ehdr.e_shstrndx))
282 shstrtabshdr = &shdrp[i];
286 if (symtabshdr == NULL)
288 if (strtabshdr == NULL) {
290 weirdreason = "string table does not exist";
292 if (shstrtabshdr == NULL) {
294 weirdreason = "section header string table does not exist";
296 if (weirdreason == NULL)
297 weirdreason = "unsupported";
299 fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
304 * sort section layout table by offset
306 layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
311 /* add a pseudo entry to represent the section header table */
312 shdrshdr.sh_offset = ehdr.e_shoff;
313 shdrshdr.sh_size = htoxew(shdrsize);
314 shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
315 layoutp[shnum].shdr = &shdrshdr;
317 /* insert and sort normal section headers */
318 for (i = shnum; i-- != 0;) {
323 if (xewtoh(shdrp[i].sh_offset) >
324 xewtoh(layoutp[m].shdr->sh_offset))
331 memmove(&layoutp[i], &layoutp[i + 1],
332 sizeof(struct shlayout) * (r - i));
335 layoutp[r].shdr = &shdrp[i];
336 layoutp[r].bufp = NULL;
341 * load up everything we need
344 /* load section string table for debug use */
345 if ((shstrtabp = xmalloc(xewtoh(shstrtabshdr->sh_size), fn,
346 "section string table")) == NULL)
348 if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
349 xewtoh(shstrtabshdr->sh_size), fn) != xewtoh(shstrtabshdr->sh_size))
352 /* we need symtab, strtab, and everything behind strtab */
354 for (i = 0; i < shnum; i++) {
355 if (layoutp[i].shdr == &shdrshdr) {
356 /* not load section header again */
357 layoutp[i].bufp = shdrp;
360 if (layoutp[i].shdr == shstrtabshdr) {
361 /* not load section string table again */
362 layoutp[i].bufp = shstrtabp;
366 if (layoutp[i].shdr == strtabshdr)
368 if (layoutp[i].shdr == symtabshdr || i >= strtabidx) {
369 off = xewtoh(layoutp[i].shdr->sh_offset);
370 size = xewtoh(layoutp[i].shdr->sh_size);
371 layoutp[i].bufp = xmalloc(size, fn,
372 shstrtabp + xewtoh(layoutp[i].shdr->sh_name));
373 if (layoutp[i].bufp == NULL)
375 if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
379 /* set symbol table and string table */
380 if (layoutp[i].shdr == symtabshdr)
381 symtabp = layoutp[i].bufp;
382 else if (layoutp[i].shdr == strtabshdr)
383 strtabp = layoutp[i].bufp;
388 nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
389 if (nstrtabp == NULL)
393 fn_size = strlen(fn);
395 /* Prepare data structures for symbol movement. */
396 nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
398 /* move symbols, making them local */
399 for (ewi = 0; ewi < nsyms; ewi++) {
400 Elf_Sym *sp = &symtabp[ewi];
401 const char *symname = strtabp + xe32toh(sp->st_name);
404 * make sure there's size for the next entry, even if it's
405 * as large as it can be.
407 * "_$$hide$$ <filename> <symname><NUL>" ->
408 * 9 + 3 + sizes of fn and sym name
410 while ((nstrtab_size - nstrtab_nextoff) <
411 strlen(symname) + fn_size + 12) {
413 nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
415 if (nstrtabp == NULL)
419 sp->st_name = htowew(nstrtab_nextoff);
421 /* if it's a keeper or is undefined, don't rename it. */
422 if (in_keep_list(symname) ||
423 (xe16toh(sp->st_shndx) == SHN_UNDEF)) {
424 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
427 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
428 "_$$hide$$ %s %s", fn, symname) + 1;
430 nstrtab_nextoff += newent_len;
432 strtabshdr->sh_size = htoxew(nstrtab_nextoff);
435 * update section header table in ascending order of offset
437 for (i = strtabidx + 1; i < shnum; i++) {
439 off = xewtoh(layoutp[i - 1].shdr->sh_offset) +
440 xewtoh(layoutp[i - 1].shdr->sh_size);
441 align = xewtoh(layoutp[i].shdr->sh_addralign);
442 off = (off + (align - 1)) & ~(align - 1);
443 layoutp[i].shdr->sh_offset = htoxew(off);
447 * write data to the file in descending order of offset
449 for (i = shnum; i-- != 0;) {
450 if (layoutp[i].shdr == strtabshdr) {
451 /* new string table */
454 buf = layoutp[i].bufp;
456 if (layoutp[i].shdr == &shdrshdr ||
457 layoutp[i].shdr == symtabshdr || i >= strtabidx) {
462 * update the offset of section header table in elf
465 if (layoutp[i].shdr == &shdrshdr &&
466 ehdr.e_shoff != shdrshdr.sh_offset) {
467 ehdr.e_shoff = shdrshdr.sh_offset;
468 off = offsetof(Elf_Ehdr, e_shoff);
469 size = sizeof(Elf_Off);
470 if ((size_t)xwriteatoff(fd, &ehdr.e_shoff, off, size,
475 off = xewtoh(layoutp[i].shdr->sh_offset);
476 size = xewtoh(layoutp[i].shdr->sh_size);
477 if ((size_t)xwriteatoff(fd, buf, off, size, fn) != size)
483 if (layoutp != NULL) {
484 for (i = 0; i < shnum; i++) {
485 if (layoutp[i].bufp != NULL)
486 free(layoutp[i].bufp);
498 #endif /* include this size of ELF */