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;
178 #define EM_AARCH64 183
180 case EM_AARCH64: break;
188 #ifndef EM_MIPS_RS4_BE /* same as EM_MIPS_RS3_LE */
189 #define EM_MIPS_RS4_BE 10
192 case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
200 case EM_PPC64: break;
202 #define EM_SPARCV9 43
204 case EM_SPARCV9: break;
208 case EM_X86_64: break;
209 /* ELFDEFNNAME(MACHDEP_ID_CASES) */
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,
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.
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.
234 ELFNAMEEND(hide)(int fd, const char *fn)
237 struct shlayout *layoutp = NULL;
238 Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
240 Elf_Sym *symtabp = NULL;
241 char *shstrtabp = NULL, *strtabp = NULL;
245 int rv, i, weird, l, m, r, strtabidx;
246 size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
247 char *nstrtabp = NULL;
249 const char *weirdreason = NULL;
254 if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
257 data = ehdr.e_ident[EI_DATA];
258 shnum = xe16toh(ehdr.e_shnum);
260 shdrsize = shnum * xe16toh(ehdr.e_shentsize);
261 if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
263 if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
267 symtabshdr = strtabshdr = shstrtabshdr = NULL;
269 for (i = 0; i < shnum; i++) {
270 switch (xe32toh(shdrp[i].sh_type)) {
272 if (symtabshdr != NULL) {
274 weirdreason = "multiple symbol tables";
276 symtabshdr = &shdrp[i];
277 strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
280 if (i == xe16toh(ehdr.e_shstrndx))
281 shstrtabshdr = &shdrp[i];
285 if (symtabshdr == NULL)
287 if (strtabshdr == NULL) {
289 weirdreason = "string table does not exist";
291 if (shstrtabshdr == NULL) {
293 weirdreason = "section header string table does not exist";
295 if (weirdreason == NULL)
296 weirdreason = "unsupported";
298 fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
303 * sort section layout table by offset
305 layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
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;
316 /* insert and sort normal section headers */
317 for (i = shnum; i-- != 0;) {
322 if (xewtoh(shdrp[i].sh_offset) >
323 xewtoh(layoutp[m].shdr->sh_offset))
330 memmove(&layoutp[i], &layoutp[i + 1],
331 sizeof(struct shlayout) * (r - i));
334 layoutp[r].shdr = &shdrp[i];
335 layoutp[r].bufp = NULL;
340 * load up everything we need
343 /* load section string table for debug use */
344 if ((shstrtabp = xmalloc(xewtoh(shstrtabshdr->sh_size), fn,
345 "section string table")) == NULL)
347 if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
348 xewtoh(shstrtabshdr->sh_size), fn) != xewtoh(shstrtabshdr->sh_size))
351 /* we need symtab, strtab, and everything behind strtab */
353 for (i = 0; i < shnum; i++) {
354 if (layoutp[i].shdr == &shdrshdr) {
355 /* not load section header again */
356 layoutp[i].bufp = shdrp;
359 if (layoutp[i].shdr == shstrtabshdr) {
360 /* not load section string table again */
361 layoutp[i].bufp = shstrtabp;
365 if (layoutp[i].shdr == strtabshdr)
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)
374 if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
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;
387 nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
388 if (nstrtabp == NULL)
392 fn_size = strlen(fn);
394 /* Prepare data structures for symbol movement. */
395 nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
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);
403 * make sure there's size for the next entry, even if it's
404 * as large as it can be.
406 * "_$$hide$$ <filename> <symname><NUL>" ->
407 * 9 + 3 + sizes of fn and sym name
409 while ((nstrtab_size - nstrtab_nextoff) <
410 strlen(symname) + fn_size + 12) {
412 nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
414 if (nstrtabp == NULL)
418 sp->st_name = htowew(nstrtab_nextoff);
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,
426 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
427 "_$$hide$$ %s %s", fn, symname) + 1;
429 nstrtab_nextoff += newent_len;
431 strtabshdr->sh_size = htoxew(nstrtab_nextoff);
434 * update section header table in ascending order of offset
436 for (i = strtabidx + 1; i < shnum; i++) {
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);
446 * write data to the file in descending order of offset
448 for (i = shnum; i-- != 0;) {
449 if (layoutp[i].shdr == strtabshdr) {
450 /* new string table */
453 buf = layoutp[i].bufp;
455 if (layoutp[i].shdr == &shdrshdr ||
456 layoutp[i].shdr == symtabshdr || i >= strtabidx) {
461 * update the offset of section header table in elf
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,
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)
482 if (layoutp != NULL) {
483 for (i = 0; i < shnum; i++) {
484 if (layoutp[i].bufp != NULL)
485 free(layoutp[i].bufp);
497 #endif /* include this size of ELF */