2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: exec_elf32.c,v 1.6 1999/09/20 04:12:16 christos Exp $");
39 __FBSDID("$FreeBSD$");
45 #include <sys/types.h>
46 #include <sys/endian.h>
59 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
60 (defined(NLIST_ELF64) && (ELFSIZE == 64))
62 #define __ELF_WORD_SIZE ELFSIZE
64 #include <sys/elf32.h>
65 #define xewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
66 #define htoxew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
67 #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
68 #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
70 #include <sys/elf64.h>
71 #define xewtoh(x) ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
72 #define htoxew(x) ((data == ELFDATA2MSB) ? htobe64(x) : htole64(x))
73 /* elf64 Elf64_Word are 32 bits */
74 #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
75 #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
77 #include <sys/elf_generic.h>
79 #define CONCAT(x,y) __CONCAT(x,y)
80 #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
81 #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
82 #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
83 #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
85 #define ELFCLASS CONCAT(ELFCLASS,ELFSIZE)
88 #define xe16toh(x) ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
89 #define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
90 #define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
98 xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
102 if (lseek(fd, off, SEEK_SET) != off) {
106 if ((size_t)(rv = read(fd, buf, size)) != size) {
107 fprintf(stderr, "%s: read error: %s\n", fn,
108 rv == -1 ? strerror(errno) : "short read");
115 xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
119 if (lseek(fd, off, SEEK_SET) != off) {
123 if ((size_t)(rv = write(fd, buf, size)) != size) {
124 fprintf(stderr, "%s: write error: %s\n", fn,
125 rv == -1 ? strerror(errno) : "short write");
132 xmalloc(size_t size, const char *fn, const char *use)
138 fprintf(stderr, "%s: out of memory (allocating for %s)\n",
144 xrealloc(void *ptr, size_t size, const char *fn, const char *use)
148 rv = realloc(ptr, size);
151 fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
158 ELFNAMEEND(check)(int fd, const char *fn)
165 * Check the header to maek sure it's an ELF file (of the
168 if (fstat(fd, &sb) == -1)
170 if (sb.st_size < (off_t)(sizeof eh))
172 if (read(fd, &eh, sizeof eh) != sizeof eh)
175 if (IS_ELF(eh) == 0 || eh.e_ident[EI_CLASS] != ELFCLASS)
178 data = eh.e_ident[EI_DATA];
180 switch (xe16toh(eh.e_machine)) {
182 case EM_ALPHA: break;
184 #define EM_AARCH64 183
186 case EM_AARCH64: break;
189 case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
191 case EM_PPC64: break;
195 case EM_RISCV: break;
197 case EM_SPARCV9: break;
198 case EM_X86_64: break;
199 /* ELFDEFNNAME(MACHDEP_ID_CASES) */
209 * This function 'hides' (some of) ELF executable file's symbols.
210 * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
211 * Symbols in the global keep list, or which are marked as being undefined,
214 * An old version of this code shuffled various tables around, turning
215 * global symbols to be hidden into local symbols. That lost on the
216 * mips, because CALL16 relocs must reference global symbols, and, if
217 * those symbols were being hidden, they were no longer global.
219 * The new renaming behaviour doesn't take global symbols out of the
220 * namespace. However, it's ... unlikely that there will ever be
221 * any collisions in practice because of the new method.
224 ELFNAMEEND(hide)(int fd, const char *fn)
227 struct shlayout *layoutp = NULL;
228 Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
230 Elf_Sym *symtabp = NULL;
231 char *shstrtabp = NULL, *strtabp = NULL;
235 int rv, i, weird, l, m, r, strtabidx;
236 size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
237 char *nstrtabp = NULL;
239 const char *weirdreason = NULL;
244 if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
247 data = ehdr.e_ident[EI_DATA];
248 shnum = xe16toh(ehdr.e_shnum);
250 shdrsize = shnum * xe16toh(ehdr.e_shentsize);
251 if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
253 if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
257 symtabshdr = strtabshdr = shstrtabshdr = NULL;
259 for (i = 0; i < shnum; i++) {
260 switch (xe32toh(shdrp[i].sh_type)) {
262 if (symtabshdr != NULL) {
264 weirdreason = "multiple symbol tables";
266 symtabshdr = &shdrp[i];
267 strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
270 if (i == xe16toh(ehdr.e_shstrndx))
271 shstrtabshdr = &shdrp[i];
275 if (symtabshdr == NULL)
277 if (strtabshdr == NULL) {
279 weirdreason = "string table does not exist";
281 if (shstrtabshdr == NULL) {
283 weirdreason = "section header string table does not exist";
285 if (strtabshdr == shstrtabshdr) {
287 weirdreason = "combined strtab and shstrtab not supported";
289 if (weirdreason == NULL)
290 weirdreason = "unsupported";
292 fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
297 * sort section layout table by offset
299 layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
304 /* add a pseudo entry to represent the section header table */
305 shdrshdr.sh_offset = ehdr.e_shoff;
306 shdrshdr.sh_size = htoxew(shdrsize);
307 shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
308 layoutp[shnum].shdr = &shdrshdr;
310 /* insert and sort normal section headers */
311 for (i = shnum; i-- != 0;) {
316 if (xewtoh(shdrp[i].sh_offset) >
317 xewtoh(layoutp[m].shdr->sh_offset))
324 memmove(&layoutp[i], &layoutp[i + 1],
325 sizeof(struct shlayout) * (r - i));
328 layoutp[r].shdr = &shdrp[i];
329 layoutp[r].bufp = NULL;
334 * load up everything we need
337 /* load section string table for debug use */
338 if ((size = xewtoh(shstrtabshdr->sh_size)) == 0)
340 if ((shstrtabp = xmalloc(size, fn, "section string table")) == NULL)
342 if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
345 if (shstrtabp[size - 1] != '\0')
348 /* we need symtab, strtab, and everything behind strtab */
350 for (i = 0; i < shnum; i++) {
351 if (layoutp[i].shdr == &shdrshdr) {
352 /* not load section header again */
353 layoutp[i].bufp = shdrp;
356 if (layoutp[i].shdr == shstrtabshdr) {
357 /* not load section string table again */
358 layoutp[i].bufp = shstrtabp;
362 if (layoutp[i].shdr == strtabshdr)
364 if (layoutp[i].shdr == symtabshdr || i >= strtabidx) {
365 off = xewtoh(layoutp[i].shdr->sh_offset);
366 if ((size = xewtoh(layoutp[i].shdr->sh_size)) == 0)
368 layoutp[i].bufp = xmalloc(size, fn,
369 shstrtabp + xewtoh(layoutp[i].shdr->sh_name));
370 if (layoutp[i].bufp == NULL)
372 if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
376 /* set symbol table and string table */
377 if (layoutp[i].shdr == symtabshdr) {
378 symtabp = layoutp[i].bufp;
379 } else if (layoutp[i].shdr == strtabshdr) {
380 strtabp = layoutp[i].bufp;
381 if (strtabp[size - 1] != '\0')
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 */