11de7b4b8SPedro F. Giffuni /*-
21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
31de7b4b8SPedro F. Giffuni *
42ec21a68SJordan K. Hubbard * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
52ec21a68SJordan K. Hubbard *
62ec21a68SJordan K. Hubbard * Redistribution and use in source and binary forms, with or without
72ec21a68SJordan K. Hubbard * modification, are permitted provided that the following conditions
82ec21a68SJordan K. Hubbard * are met:
92ec21a68SJordan K. Hubbard * 1. Redistributions of source code must retain the above copyright
102ec21a68SJordan K. Hubbard * notice, this list of conditions and the following disclaimer.
112ec21a68SJordan K. Hubbard * 2. Redistributions in binary form must reproduce the above copyright
122ec21a68SJordan K. Hubbard * notice, this list of conditions and the following disclaimer in the
132ec21a68SJordan K. Hubbard * documentation and/or other materials provided with the distribution.
142ec21a68SJordan K. Hubbard * 3. All advertising materials mentioning features or use of this software
152ec21a68SJordan K. Hubbard * must display the following acknowledgement:
162ec21a68SJordan K. Hubbard * This product includes software developed by Christopher G. Demetriou
172ec21a68SJordan K. Hubbard * for the NetBSD Project.
182ec21a68SJordan K. Hubbard * 4. The name of the author may not be used to endorse or promote products
192ec21a68SJordan K. Hubbard * derived from this software without specific prior written permission
202ec21a68SJordan K. Hubbard *
212ec21a68SJordan K. Hubbard * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
222ec21a68SJordan K. Hubbard * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
232ec21a68SJordan K. Hubbard * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
242ec21a68SJordan K. Hubbard * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
252ec21a68SJordan K. Hubbard * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
262ec21a68SJordan K. Hubbard * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
272ec21a68SJordan K. Hubbard * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
282ec21a68SJordan K. Hubbard * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
292ec21a68SJordan K. Hubbard * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
302ec21a68SJordan K. Hubbard * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
312ec21a68SJordan K. Hubbard */
322ec21a68SJordan K. Hubbard
332ec21a68SJordan K. Hubbard #include <sys/cdefs.h>
342ec21a68SJordan K. Hubbard #ifndef lint
355e6220d9SDavid E. O'Brien #if 0
3696acb2c4SPedro F. Giffuni __RCSID("$NetBSD: exec_elf32.c,v 1.6 1999/09/20 04:12:16 christos Exp $");
372ec21a68SJordan K. Hubbard #endif
385e6220d9SDavid E. O'Brien #endif
392ec21a68SJordan K. Hubbard #ifndef ELFSIZE
402ec21a68SJordan K. Hubbard #define ELFSIZE 32
412ec21a68SJordan K. Hubbard #endif
422ec21a68SJordan K. Hubbard
432ec21a68SJordan K. Hubbard #include <sys/types.h>
442d57da02SDavid E. O'Brien #include <sys/endian.h>
452ec21a68SJordan K. Hubbard #include <sys/stat.h>
462ec21a68SJordan K. Hubbard
472ec21a68SJordan K. Hubbard #include <errno.h>
48b5782a47SPedro F. Giffuni #include <limits.h>
49b15dc003SEd Maste #include <stddef.h>
502ec21a68SJordan K. Hubbard #include <stdio.h>
512ec21a68SJordan K. Hubbard #include <stdlib.h>
522ec21a68SJordan K. Hubbard #include <string.h>
532ec21a68SJordan K. Hubbard #include <unistd.h>
542ec21a68SJordan K. Hubbard
552ec21a68SJordan K. Hubbard #include "extern.h"
562ec21a68SJordan K. Hubbard
572ec21a68SJordan K. Hubbard #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
582ec21a68SJordan K. Hubbard (defined(NLIST_ELF64) && (ELFSIZE == 64))
592ec21a68SJordan K. Hubbard
6064b06e78SRuslan Ermilov #define __ELF_WORD_SIZE ELFSIZE
6164b06e78SRuslan Ermilov #if (ELFSIZE == 32)
6264b06e78SRuslan Ermilov #include <sys/elf32.h>
63d8d97708SRuslan Ermilov #define xewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
6482c7cf31SRuslan Ermilov #define htoxew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
652c457547SAdrian Chadd #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
662c457547SAdrian Chadd #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
6764b06e78SRuslan Ermilov #elif (ELFSIZE == 64)
6864b06e78SRuslan Ermilov #include <sys/elf64.h>
69d8d97708SRuslan Ermilov #define xewtoh(x) ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
7082c7cf31SRuslan Ermilov #define htoxew(x) ((data == ELFDATA2MSB) ? htobe64(x) : htole64(x))
712c457547SAdrian Chadd /* elf64 Elf64_Word are 32 bits */
722c457547SAdrian Chadd #define wewtoh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
732c457547SAdrian Chadd #define htowew(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
7464b06e78SRuslan Ermilov #endif
7564b06e78SRuslan Ermilov #include <sys/elf_generic.h>
762ec21a68SJordan K. Hubbard
772ec21a68SJordan K. Hubbard #define CONCAT(x,y) __CONCAT(x,y)
782ec21a68SJordan K. Hubbard #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
792ec21a68SJordan K. Hubbard #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
802ec21a68SJordan K. Hubbard #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
812ec21a68SJordan K. Hubbard #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
82070cadfcSEd Maste #ifndef ELFCLASS
83070cadfcSEd Maste #define ELFCLASS CONCAT(ELFCLASS,ELFSIZE)
84070cadfcSEd Maste #endif
852ec21a68SJordan K. Hubbard
86d8d97708SRuslan Ermilov #define xe16toh(x) ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
87d8d97708SRuslan Ermilov #define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
88d8d97708SRuslan Ermilov #define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
89d8d97708SRuslan Ermilov
90b5782a47SPedro F. Giffuni struct shlayout {
91b5782a47SPedro F. Giffuni Elf_Shdr *shdr;
92b5782a47SPedro F. Giffuni void *bufp;
932ec21a68SJordan K. Hubbard };
942ec21a68SJordan K. Hubbard
952ec21a68SJordan K. Hubbard static ssize_t
xreadatoff(int fd,void * buf,off_t off,size_t size,const char * fn)962ec21a68SJordan K. Hubbard xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
972ec21a68SJordan K. Hubbard {
982ec21a68SJordan K. Hubbard ssize_t rv;
992ec21a68SJordan K. Hubbard
1002ec21a68SJordan K. Hubbard if (lseek(fd, off, SEEK_SET) != off) {
1012ec21a68SJordan K. Hubbard perror(fn);
1022ec21a68SJordan K. Hubbard return -1;
1032ec21a68SJordan K. Hubbard }
10496acb2c4SPedro F. Giffuni if ((size_t)(rv = read(fd, buf, size)) != size) {
1052ec21a68SJordan K. Hubbard fprintf(stderr, "%s: read error: %s\n", fn,
1062ec21a68SJordan K. Hubbard rv == -1 ? strerror(errno) : "short read");
1072ec21a68SJordan K. Hubbard return -1;
1082ec21a68SJordan K. Hubbard }
1092ec21a68SJordan K. Hubbard return size;
1102ec21a68SJordan K. Hubbard }
1112ec21a68SJordan K. Hubbard
1122ec21a68SJordan K. Hubbard static ssize_t
xwriteatoff(int fd,void * buf,off_t off,size_t size,const char * fn)1132ec21a68SJordan K. Hubbard xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
1142ec21a68SJordan K. Hubbard {
1152ec21a68SJordan K. Hubbard ssize_t rv;
1162ec21a68SJordan K. Hubbard
1172ec21a68SJordan K. Hubbard if (lseek(fd, off, SEEK_SET) != off) {
1182ec21a68SJordan K. Hubbard perror(fn);
1192ec21a68SJordan K. Hubbard return -1;
1202ec21a68SJordan K. Hubbard }
12196acb2c4SPedro F. Giffuni if ((size_t)(rv = write(fd, buf, size)) != size) {
1222ec21a68SJordan K. Hubbard fprintf(stderr, "%s: write error: %s\n", fn,
1232ec21a68SJordan K. Hubbard rv == -1 ? strerror(errno) : "short write");
1242ec21a68SJordan K. Hubbard return -1;
1252ec21a68SJordan K. Hubbard }
1262ec21a68SJordan K. Hubbard return size;
1272ec21a68SJordan K. Hubbard }
1282ec21a68SJordan K. Hubbard
1292ec21a68SJordan K. Hubbard static void *
xmalloc(size_t size,const char * fn,const char * use)1302ec21a68SJordan K. Hubbard xmalloc(size_t size, const char *fn, const char *use)
1312ec21a68SJordan K. Hubbard {
1322ec21a68SJordan K. Hubbard void *rv;
1332ec21a68SJordan K. Hubbard
1342ec21a68SJordan K. Hubbard rv = malloc(size);
1352ec21a68SJordan K. Hubbard if (rv == NULL)
1362ec21a68SJordan K. Hubbard fprintf(stderr, "%s: out of memory (allocating for %s)\n",
1372ec21a68SJordan K. Hubbard fn, use);
1382ec21a68SJordan K. Hubbard return (rv);
1392ec21a68SJordan K. Hubbard }
1402ec21a68SJordan K. Hubbard
1417420b323SAdrian Chadd static void *
xrealloc(void * ptr,size_t size,const char * fn,const char * use)1427420b323SAdrian Chadd xrealloc(void *ptr, size_t size, const char *fn, const char *use)
1437420b323SAdrian Chadd {
1447420b323SAdrian Chadd void *rv;
1457420b323SAdrian Chadd
1467420b323SAdrian Chadd rv = realloc(ptr, size);
1477420b323SAdrian Chadd if (rv == NULL) {
1487420b323SAdrian Chadd free(ptr);
1497420b323SAdrian Chadd fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
1507420b323SAdrian Chadd fn, use);
1517420b323SAdrian Chadd }
1527420b323SAdrian Chadd return (rv);
1537420b323SAdrian Chadd }
1547420b323SAdrian Chadd
1552ec21a68SJordan K. Hubbard int
ELFNAMEEND(check)156dd7c7ff1SKyle Evans ELFNAMEEND(check)(int fd, const char *fn __unused)
1572ec21a68SJordan K. Hubbard {
1582ec21a68SJordan K. Hubbard Elf_Ehdr eh;
1592ec21a68SJordan K. Hubbard struct stat sb;
160d8d97708SRuslan Ermilov unsigned char data;
1612ec21a68SJordan K. Hubbard
1622ec21a68SJordan K. Hubbard /*
163*d34de8d3SGordon Bergling * Check the header to make sure it's an ELF file (of the
1642ec21a68SJordan K. Hubbard * appropriate size).
1652ec21a68SJordan K. Hubbard */
1662ec21a68SJordan K. Hubbard if (fstat(fd, &sb) == -1)
1672ec21a68SJordan K. Hubbard return 0;
16896acb2c4SPedro F. Giffuni if (sb.st_size < (off_t)(sizeof eh))
1692ec21a68SJordan K. Hubbard return 0;
1702ec21a68SJordan K. Hubbard if (read(fd, &eh, sizeof eh) != sizeof eh)
1712ec21a68SJordan K. Hubbard return 0;
1722ec21a68SJordan K. Hubbard
173070cadfcSEd Maste if (IS_ELF(eh) == 0 || eh.e_ident[EI_CLASS] != ELFCLASS)
1742ec21a68SJordan K. Hubbard return 0;
1752ec21a68SJordan K. Hubbard
176d8d97708SRuslan Ermilov data = eh.e_ident[EI_DATA];
177d8d97708SRuslan Ermilov
178d8d97708SRuslan Ermilov switch (xe16toh(eh.e_machine)) {
1792ec21a68SJordan K. Hubbard case EM_386: break;
1802ec21a68SJordan K. Hubbard case EM_ALPHA: break;
18166137b66SEd Maste #ifndef EM_AARCH64
18266137b66SEd Maste #define EM_AARCH64 183
18366137b66SEd Maste #endif
1845667872eSAndrew Turner case EM_AARCH64: break;
185ed575be4SOlivier Houchard case EM_ARM: break;
18622407452SDavid E. O'Brien case EM_MIPS: break;
18722407452SDavid E. O'Brien case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
188930d8c70SDavid E. O'Brien case EM_PPC: break;
1898a10bad2SNathan Whitehorn case EM_PPC64: break;
190c8daf7a0SEd Maste #ifndef EM_RISCV
191c8daf7a0SEd Maste #define EM_RISCV 243
192c8daf7a0SEd Maste #endif
193c8daf7a0SEd Maste case EM_RISCV: break;
1944a6768e6SBjoern A. Zeeb case EM_S390: break;
19566c795d4SRuslan Ermilov case EM_SPARCV9: break;
1968e81109eSPeter Wemm case EM_X86_64: break;
1972ec21a68SJordan K. Hubbard /* ELFDEFNNAME(MACHDEP_ID_CASES) */
1982ec21a68SJordan K. Hubbard
1992ec21a68SJordan K. Hubbard default:
2002ec21a68SJordan K. Hubbard return 0;
2012ec21a68SJordan K. Hubbard }
2022ec21a68SJordan K. Hubbard
2032ec21a68SJordan K. Hubbard return 1;
2042ec21a68SJordan K. Hubbard }
2052ec21a68SJordan K. Hubbard
2067420b323SAdrian Chadd /*
2077420b323SAdrian Chadd * This function 'hides' (some of) ELF executable file's symbols.
2087420b323SAdrian Chadd * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
2097420b323SAdrian Chadd * Symbols in the global keep list, or which are marked as being undefined,
2107420b323SAdrian Chadd * are left alone.
2117420b323SAdrian Chadd *
2127420b323SAdrian Chadd * An old version of this code shuffled various tables around, turning
2137420b323SAdrian Chadd * global symbols to be hidden into local symbols. That lost on the
2147420b323SAdrian Chadd * mips, because CALL16 relocs must reference global symbols, and, if
2157420b323SAdrian Chadd * those symbols were being hidden, they were no longer global.
2167420b323SAdrian Chadd *
2177420b323SAdrian Chadd * The new renaming behaviour doesn't take global symbols out of the
2187420b323SAdrian Chadd * namespace. However, it's ... unlikely that there will ever be
2197420b323SAdrian Chadd * any collisions in practice because of the new method.
2207420b323SAdrian Chadd */
2212ec21a68SJordan K. Hubbard int
ELFNAMEEND(hide)2222ec21a68SJordan K. Hubbard ELFNAMEEND(hide)(int fd, const char *fn)
2232ec21a68SJordan K. Hubbard {
2242ec21a68SJordan K. Hubbard Elf_Ehdr ehdr;
225b5782a47SPedro F. Giffuni struct shlayout *layoutp = NULL;
226b5782a47SPedro F. Giffuni Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
227b5782a47SPedro F. Giffuni Elf_Shdr shdrshdr;
2282ec21a68SJordan K. Hubbard Elf_Sym *symtabp = NULL;
229b5782a47SPedro F. Giffuni char *shstrtabp = NULL, *strtabp = NULL;
2301d76342eSEitan Adler Elf_Size nsyms, ewi;
231b5782a47SPedro F. Giffuni Elf_Off off;
2322ec21a68SJordan K. Hubbard ssize_t shdrsize;
233b5782a47SPedro F. Giffuni int rv, i, weird, l, m, r, strtabidx;
234b5782a47SPedro F. Giffuni size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
2357420b323SAdrian Chadd char *nstrtabp = NULL;
236d8d97708SRuslan Ermilov unsigned char data;
2377420b323SAdrian Chadd const char *weirdreason = NULL;
238b5782a47SPedro F. Giffuni void *buf;
239e8ee1a1eSPedro F. Giffuni Elf_Half shnum;
2402ec21a68SJordan K. Hubbard
2412ec21a68SJordan K. Hubbard rv = 0;
2422ec21a68SJordan K. Hubbard if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
2432ec21a68SJordan K. Hubbard goto bad;
2442ec21a68SJordan K. Hubbard
245d8d97708SRuslan Ermilov data = ehdr.e_ident[EI_DATA];
246e8ee1a1eSPedro F. Giffuni shnum = xe16toh(ehdr.e_shnum);
247d8d97708SRuslan Ermilov
248e8ee1a1eSPedro F. Giffuni shdrsize = shnum * xe16toh(ehdr.e_shentsize);
2492ec21a68SJordan K. Hubbard if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
2502ec21a68SJordan K. Hubbard goto bad;
251d8d97708SRuslan Ermilov if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
252d8d97708SRuslan Ermilov shdrsize)
2532ec21a68SJordan K. Hubbard goto bad;
2542ec21a68SJordan K. Hubbard
255b5782a47SPedro F. Giffuni symtabshdr = strtabshdr = shstrtabshdr = NULL;
2562ec21a68SJordan K. Hubbard weird = 0;
257e8ee1a1eSPedro F. Giffuni for (i = 0; i < shnum; i++) {
258d8d97708SRuslan Ermilov switch (xe32toh(shdrp[i].sh_type)) {
2592ec21a68SJordan K. Hubbard case SHT_SYMTAB:
260b5782a47SPedro F. Giffuni if (symtabshdr != NULL) {
2612ec21a68SJordan K. Hubbard weird = 1;
262b5782a47SPedro F. Giffuni weirdreason = "multiple symbol tables";
263b5782a47SPedro F. Giffuni }
2642ec21a68SJordan K. Hubbard symtabshdr = &shdrp[i];
265d8d97708SRuslan Ermilov strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
266b5782a47SPedro F. Giffuni break;
267b5782a47SPedro F. Giffuni case SHT_STRTAB:
268b5782a47SPedro F. Giffuni if (i == xe16toh(ehdr.e_shstrndx))
269b5782a47SPedro F. Giffuni shstrtabshdr = &shdrp[i];
2702ec21a68SJordan K. Hubbard break;
2712ec21a68SJordan K. Hubbard }
2722ec21a68SJordan K. Hubbard }
2732ec21a68SJordan K. Hubbard if (symtabshdr == NULL)
2742ec21a68SJordan K. Hubbard goto out;
275b5782a47SPedro F. Giffuni if (strtabshdr == NULL) {
2762ec21a68SJordan K. Hubbard weird = 1;
277b5782a47SPedro F. Giffuni weirdreason = "string table does not exist";
2787420b323SAdrian Chadd }
279b5782a47SPedro F. Giffuni if (shstrtabshdr == NULL) {
280b5782a47SPedro F. Giffuni weird = 1;
281b5782a47SPedro F. Giffuni weirdreason = "section header string table does not exist";
282b5782a47SPedro F. Giffuni }
283f513dfd4SEd Maste if (strtabshdr == shstrtabshdr) {
284f513dfd4SEd Maste weird = 1;
285f513dfd4SEd Maste weirdreason = "combined strtab and shstrtab not supported";
286f513dfd4SEd Maste }
287b5782a47SPedro F. Giffuni if (weirdreason == NULL)
288b5782a47SPedro F. Giffuni weirdreason = "unsupported";
2892ec21a68SJordan K. Hubbard if (weird) {
2907420b323SAdrian Chadd fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
2912ec21a68SJordan K. Hubbard goto bad;
2922ec21a68SJordan K. Hubbard }
2932ec21a68SJordan K. Hubbard
2942ec21a68SJordan K. Hubbard /*
295b5782a47SPedro F. Giffuni * sort section layout table by offset
296b5782a47SPedro F. Giffuni */
297e8ee1a1eSPedro F. Giffuni layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
298b5782a47SPedro F. Giffuni fn, "layout table");
299b5782a47SPedro F. Giffuni if (layoutp == NULL)
300b5782a47SPedro F. Giffuni goto bad;
301b5782a47SPedro F. Giffuni
302b5782a47SPedro F. Giffuni /* add a pseudo entry to represent the section header table */
303b5782a47SPedro F. Giffuni shdrshdr.sh_offset = ehdr.e_shoff;
304b5782a47SPedro F. Giffuni shdrshdr.sh_size = htoxew(shdrsize);
305b5782a47SPedro F. Giffuni shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
306e8ee1a1eSPedro F. Giffuni layoutp[shnum].shdr = &shdrshdr;
307b5782a47SPedro F. Giffuni
308b5782a47SPedro F. Giffuni /* insert and sort normal section headers */
309e8ee1a1eSPedro F. Giffuni for (i = shnum; i-- != 0;) {
310b5782a47SPedro F. Giffuni l = i + 1;
311e8ee1a1eSPedro F. Giffuni r = shnum;
312b5782a47SPedro F. Giffuni while (l <= r) {
313b5782a47SPedro F. Giffuni m = ( l + r) / 2;
314b5782a47SPedro F. Giffuni if (xewtoh(shdrp[i].sh_offset) >
315b5782a47SPedro F. Giffuni xewtoh(layoutp[m].shdr->sh_offset))
316b5782a47SPedro F. Giffuni l = m + 1;
317b5782a47SPedro F. Giffuni else
318b5782a47SPedro F. Giffuni r = m - 1;
319b5782a47SPedro F. Giffuni }
320b5782a47SPedro F. Giffuni
321b5782a47SPedro F. Giffuni if (r != i) {
322b5782a47SPedro F. Giffuni memmove(&layoutp[i], &layoutp[i + 1],
323b5782a47SPedro F. Giffuni sizeof(struct shlayout) * (r - i));
324b5782a47SPedro F. Giffuni }
325b5782a47SPedro F. Giffuni
326b5782a47SPedro F. Giffuni layoutp[r].shdr = &shdrp[i];
327b5782a47SPedro F. Giffuni layoutp[r].bufp = NULL;
328b5782a47SPedro F. Giffuni }
329e8ee1a1eSPedro F. Giffuni ++shnum;
330b5782a47SPedro F. Giffuni
331b5782a47SPedro F. Giffuni /*
3322ec21a68SJordan K. Hubbard * load up everything we need
3332ec21a68SJordan K. Hubbard */
3342ec21a68SJordan K. Hubbard
335b5782a47SPedro F. Giffuni /* load section string table for debug use */
336e52f9715SEd Maste if ((size = xewtoh(shstrtabshdr->sh_size)) == 0)
337e52f9715SEd Maste goto bad;
338e52f9715SEd Maste if ((shstrtabp = xmalloc(size, fn, "section string table")) == NULL)
3392ec21a68SJordan K. Hubbard goto bad;
340b5782a47SPedro F. Giffuni if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
341e52f9715SEd Maste size, fn) != size)
342e52f9715SEd Maste goto bad;
343e52f9715SEd Maste if (shstrtabp[size - 1] != '\0')
3442ec21a68SJordan K. Hubbard goto bad;
3452ec21a68SJordan K. Hubbard
346b5782a47SPedro F. Giffuni /* we need symtab, strtab, and everything behind strtab */
347b5782a47SPedro F. Giffuni strtabidx = INT_MAX;
348e8ee1a1eSPedro F. Giffuni for (i = 0; i < shnum; i++) {
349b5782a47SPedro F. Giffuni if (layoutp[i].shdr == &shdrshdr) {
350b5782a47SPedro F. Giffuni /* not load section header again */
351b5782a47SPedro F. Giffuni layoutp[i].bufp = shdrp;
352b5782a47SPedro F. Giffuni continue;
353b5782a47SPedro F. Giffuni }
354b5782a47SPedro F. Giffuni if (layoutp[i].shdr == shstrtabshdr) {
355b5782a47SPedro F. Giffuni /* not load section string table again */
356b5782a47SPedro F. Giffuni layoutp[i].bufp = shstrtabp;
357b5782a47SPedro F. Giffuni continue;
358b5782a47SPedro F. Giffuni }
359b5782a47SPedro F. Giffuni
360b5782a47SPedro F. Giffuni if (layoutp[i].shdr == strtabshdr)
361b5782a47SPedro F. Giffuni strtabidx = i;
362b5782a47SPedro F. Giffuni if (layoutp[i].shdr == symtabshdr || i >= strtabidx) {
363b5782a47SPedro F. Giffuni off = xewtoh(layoutp[i].shdr->sh_offset);
364e52f9715SEd Maste if ((size = xewtoh(layoutp[i].shdr->sh_size)) == 0)
365e52f9715SEd Maste goto bad;
366b5782a47SPedro F. Giffuni layoutp[i].bufp = xmalloc(size, fn,
367b5782a47SPedro F. Giffuni shstrtabp + xewtoh(layoutp[i].shdr->sh_name));
368b5782a47SPedro F. Giffuni if (layoutp[i].bufp == NULL)
3692ec21a68SJordan K. Hubbard goto bad;
370b5782a47SPedro F. Giffuni if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
371b5782a47SPedro F. Giffuni size)
3722ec21a68SJordan K. Hubbard goto bad;
3732ec21a68SJordan K. Hubbard
374b5782a47SPedro F. Giffuni /* set symbol table and string table */
375e52f9715SEd Maste if (layoutp[i].shdr == symtabshdr) {
376b5782a47SPedro F. Giffuni symtabp = layoutp[i].bufp;
377e52f9715SEd Maste } else if (layoutp[i].shdr == strtabshdr) {
378b5782a47SPedro F. Giffuni strtabp = layoutp[i].bufp;
379e52f9715SEd Maste if (strtabp[size - 1] != '\0')
380e52f9715SEd Maste goto bad;
381e52f9715SEd Maste }
382b5782a47SPedro F. Giffuni }
383b5782a47SPedro F. Giffuni }
384b5782a47SPedro F. Giffuni
3857420b323SAdrian Chadd nstrtab_size = 256;
3867420b323SAdrian Chadd nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
3877420b323SAdrian Chadd if (nstrtabp == NULL)
3882ec21a68SJordan K. Hubbard goto bad;
3897420b323SAdrian Chadd nstrtab_nextoff = 0;
3902ec21a68SJordan K. Hubbard
3917420b323SAdrian Chadd fn_size = strlen(fn);
3922ec21a68SJordan K. Hubbard
3932ec21a68SJordan K. Hubbard /* Prepare data structures for symbol movement. */
394d8d97708SRuslan Ermilov nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
3952ec21a68SJordan K. Hubbard
3962ec21a68SJordan K. Hubbard /* move symbols, making them local */
3977420b323SAdrian Chadd for (ewi = 0; ewi < nsyms; ewi++) {
3987420b323SAdrian Chadd Elf_Sym *sp = &symtabp[ewi];
3997420b323SAdrian Chadd const char *symname = strtabp + xe32toh(sp->st_name);
4007420b323SAdrian Chadd size_t newent_len;
4012ec21a68SJordan K. Hubbard /*
4027420b323SAdrian Chadd * make sure there's size for the next entry, even if it's
4037420b323SAdrian Chadd * as large as it can be.
4047420b323SAdrian Chadd *
4057420b323SAdrian Chadd * "_$$hide$$ <filename> <symname><NUL>" ->
4067420b323SAdrian Chadd * 9 + 3 + sizes of fn and sym name
4072ec21a68SJordan K. Hubbard */
4087420b323SAdrian Chadd while ((nstrtab_size - nstrtab_nextoff) <
4097420b323SAdrian Chadd strlen(symname) + fn_size + 12) {
4107420b323SAdrian Chadd nstrtab_size *= 2;
4117420b323SAdrian Chadd nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
4127420b323SAdrian Chadd "new string table");
4137420b323SAdrian Chadd if (nstrtabp == NULL)
4147420b323SAdrian Chadd goto bad;
4152ec21a68SJordan K. Hubbard }
4162ec21a68SJordan K. Hubbard
4172c457547SAdrian Chadd sp->st_name = htowew(nstrtab_nextoff);
4182ec21a68SJordan K. Hubbard
4197420b323SAdrian Chadd /* if it's a keeper or is undefined, don't rename it. */
4207420b323SAdrian Chadd if (in_keep_list(symname) ||
4217420b323SAdrian Chadd (xe16toh(sp->st_shndx) == SHN_UNDEF)) {
4227420b323SAdrian Chadd newent_len = sprintf(nstrtabp + nstrtab_nextoff,
4237420b323SAdrian Chadd "%s", symname) + 1;
4247420b323SAdrian Chadd } else {
4257420b323SAdrian Chadd newent_len = sprintf(nstrtabp + nstrtab_nextoff,
4267420b323SAdrian Chadd "_$$hide$$ %s %s", fn, symname) + 1;
4272ec21a68SJordan K. Hubbard }
4287420b323SAdrian Chadd nstrtab_nextoff += newent_len;
4292ec21a68SJordan K. Hubbard }
4307420b323SAdrian Chadd strtabshdr->sh_size = htoxew(nstrtab_nextoff);
4312ec21a68SJordan K. Hubbard
4322ec21a68SJordan K. Hubbard /*
433b5782a47SPedro F. Giffuni * update section header table in ascending order of offset
4342ec21a68SJordan K. Hubbard */
435e8ee1a1eSPedro F. Giffuni for (i = strtabidx + 1; i < shnum; i++) {
436dd7c7ff1SKyle Evans Elf_Off soff, align;
437dd7c7ff1SKyle Evans soff = xewtoh(layoutp[i - 1].shdr->sh_offset) +
438b5782a47SPedro F. Giffuni xewtoh(layoutp[i - 1].shdr->sh_size);
439b5782a47SPedro F. Giffuni align = xewtoh(layoutp[i].shdr->sh_addralign);
440dd7c7ff1SKyle Evans soff = (soff + (align - 1)) & ~(align - 1);
441dd7c7ff1SKyle Evans layoutp[i].shdr->sh_offset = htoxew(soff);
442b5782a47SPedro F. Giffuni }
443b5782a47SPedro F. Giffuni
444b5782a47SPedro F. Giffuni /*
445b5782a47SPedro F. Giffuni * write data to the file in descending order of offset
446b5782a47SPedro F. Giffuni */
447e8ee1a1eSPedro F. Giffuni for (i = shnum; i-- != 0;) {
448b5782a47SPedro F. Giffuni if (layoutp[i].shdr == strtabshdr) {
449b5782a47SPedro F. Giffuni /* new string table */
450b5782a47SPedro F. Giffuni buf = nstrtabp;
451b5782a47SPedro F. Giffuni } else
452b5782a47SPedro F. Giffuni buf = layoutp[i].bufp;
453b5782a47SPedro F. Giffuni
454b5782a47SPedro F. Giffuni if (layoutp[i].shdr == &shdrshdr ||
455b5782a47SPedro F. Giffuni layoutp[i].shdr == symtabshdr || i >= strtabidx) {
456b5782a47SPedro F. Giffuni if (buf == NULL)
4572ec21a68SJordan K. Hubbard goto bad;
4582ec21a68SJordan K. Hubbard
459b5782a47SPedro F. Giffuni /*
460b5782a47SPedro F. Giffuni * update the offset of section header table in elf
461b5782a47SPedro F. Giffuni * header if needed.
462b5782a47SPedro F. Giffuni */
463b5782a47SPedro F. Giffuni if (layoutp[i].shdr == &shdrshdr &&
464b5782a47SPedro F. Giffuni ehdr.e_shoff != shdrshdr.sh_offset) {
465b5782a47SPedro F. Giffuni ehdr.e_shoff = shdrshdr.sh_offset;
466b15dc003SEd Maste off = offsetof(Elf_Ehdr, e_shoff);
467b5782a47SPedro F. Giffuni size = sizeof(Elf_Off);
468b5782a47SPedro F. Giffuni if ((size_t)xwriteatoff(fd, &ehdr.e_shoff, off, size,
469b5782a47SPedro F. Giffuni fn) != size)
470b5782a47SPedro F. Giffuni goto bad;
471b5782a47SPedro F. Giffuni }
472b5782a47SPedro F. Giffuni
473b5782a47SPedro F. Giffuni off = xewtoh(layoutp[i].shdr->sh_offset);
474b5782a47SPedro F. Giffuni size = xewtoh(layoutp[i].shdr->sh_size);
475b5782a47SPedro F. Giffuni if ((size_t)xwriteatoff(fd, buf, off, size, fn) != size)
476b5782a47SPedro F. Giffuni goto bad;
477b5782a47SPedro F. Giffuni }
478b5782a47SPedro F. Giffuni }
479b5782a47SPedro F. Giffuni
4802ec21a68SJordan K. Hubbard out:
481b5782a47SPedro F. Giffuni if (layoutp != NULL) {
482e8ee1a1eSPedro F. Giffuni for (i = 0; i < shnum; i++) {
483b5782a47SPedro F. Giffuni if (layoutp[i].bufp != NULL)
484b5782a47SPedro F. Giffuni free(layoutp[i].bufp);
485b5782a47SPedro F. Giffuni }
486b5782a47SPedro F. Giffuni free(layoutp);
487b5782a47SPedro F. Giffuni }
48896acb2c4SPedro F. Giffuni free(nstrtabp);
4892ec21a68SJordan K. Hubbard return (rv);
4902ec21a68SJordan K. Hubbard
4912ec21a68SJordan K. Hubbard bad:
4922ec21a68SJordan K. Hubbard rv = 1;
4932ec21a68SJordan K. Hubbard goto out;
4942ec21a68SJordan K. Hubbard }
4952ec21a68SJordan K. Hubbard
4962ec21a68SJordan K. Hubbard #endif /* include this size of ELF */
497