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.4 1997/08/12 06:07:24 mikel Exp $");
37 __FBSDID("$FreeBSD$");
43 #include <sys/types.h>
44 #include <sys/endian.h>
55 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
56 (defined(NLIST_ELF64) && (ELFSIZE == 64))
58 #define __ELF_WORD_SIZE ELFSIZE
60 #include <sys/elf32.h>
61 #define xewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
62 #define htoxew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
63 #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
64 #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
66 #include <sys/elf64.h>
67 #define xewtoh(x) ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
68 #define htoxew(x) ((data == ELFDATA2MSB) ? htobe64(x) : htole64(x))
69 /* elf64 Elf64_Word are 32 bits */
70 #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
71 #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
73 #include <sys/elf_generic.h>
75 #define CONCAT(x,y) __CONCAT(x,y)
76 #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
77 #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
78 #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
79 #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
81 #define xe16toh(x) ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
82 #define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
83 #define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
86 struct listelem *next;
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 ((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 ((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 < 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;
185 #ifndef EM_MIPS_RS4_BE /* same as EM_MIPS_RS3_LE */
186 #define EM_MIPS_RS4_BE 10
189 case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
193 case EM_IA_64: 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 Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr;
239 Elf_Sym *symtabp = NULL;
240 char *strtabp = NULL;
244 size_t nstrtab_size, nstrtab_nextoff, fn_size;
245 char *nstrtabp = NULL;
247 Elf_Off maxoff, stroff;
248 const char *weirdreason = NULL;
251 if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
254 data = ehdr.e_ident[EI_DATA];
256 shdrsize = xe16toh(ehdr.e_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 = NULL;
266 for (i = 0; i < xe16toh(ehdr.e_shnum); i++) {
267 if (xewtoh(shdrp[i].sh_offset) > maxoff)
268 maxoff = xewtoh(shdrp[i].sh_offset);
269 switch (xe32toh(shdrp[i].sh_type)) {
271 if (symtabshdr != NULL)
273 symtabshdr = &shdrp[i];
274 strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
276 /* Check whether the string table is the last section */
277 stroff = xewtoh(shdrp[xe32toh(shdrp[i].sh_link)].sh_offset);
278 if (!weird && xe32toh(shdrp[i].sh_link) != (xe16toh(ehdr.e_shnum) - 1)) {
280 weirdreason = "string table not last section";
286 weirdreason = "unsupported";
287 if (symtabshdr == NULL)
289 if (strtabshdr == NULL)
291 if (!weird && stroff != maxoff) {
293 weirdreason = "string table section not last in file";
296 fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
301 * load up everything we need
305 if ((symtabp = xmalloc(xewtoh(symtabshdr->sh_size), fn, "symbol table"))
308 if (xreadatoff(fd, symtabp, xewtoh(symtabshdr->sh_offset),
309 xewtoh(symtabshdr->sh_size), fn) != xewtoh(symtabshdr->sh_size))
313 if ((strtabp = xmalloc(xewtoh(strtabshdr->sh_size), fn, "string table"))
316 if (xreadatoff(fd, strtabp, xewtoh(strtabshdr->sh_offset),
317 xewtoh(strtabshdr->sh_size), fn) != xewtoh(strtabshdr->sh_size))
321 nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
322 if (nstrtabp == NULL)
326 fn_size = strlen(fn);
328 /* Prepare data structures for symbol movement. */
329 nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
331 /* move symbols, making them local */
332 for (ewi = 0; ewi < nsyms; ewi++) {
333 Elf_Sym *sp = &symtabp[ewi];
334 const char *symname = strtabp + xe32toh(sp->st_name);
337 * make sure there's size for the next entry, even if it's
338 * as large as it can be.
340 * "_$$hide$$ <filename> <symname><NUL>" ->
341 * 9 + 3 + sizes of fn and sym name
343 while ((nstrtab_size - nstrtab_nextoff) <
344 strlen(symname) + fn_size + 12) {
346 nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
348 if (nstrtabp == NULL)
352 sp->st_name = htowew(nstrtab_nextoff);
354 /* if it's a keeper or is undefined, don't rename it. */
355 if (in_keep_list(symname) ||
356 (xe16toh(sp->st_shndx) == SHN_UNDEF)) {
357 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
360 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
361 "_$$hide$$ %s %s", fn, symname) + 1;
363 nstrtab_nextoff += newent_len;
365 strtabshdr->sh_size = htoxew(nstrtab_nextoff);
368 * write new tables to the file
370 if (xwriteatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
373 if (xwriteatoff(fd, symtabp, xewtoh(symtabshdr->sh_offset),
374 xewtoh(symtabshdr->sh_size), fn) != xewtoh(symtabshdr->sh_size))
376 /* write new symbol table strings */
377 if ((size_t)xwriteatoff(fd, nstrtabp, xewtoh(strtabshdr->sh_offset),
378 xewtoh(strtabshdr->sh_size), fn) != xewtoh(strtabshdr->sh_size))
395 #endif /* include this size of ELF */