12ec21a68SJordan K. Hubbard /* 22ec21a68SJordan K. Hubbard * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved. 32ec21a68SJordan K. Hubbard * 42ec21a68SJordan K. Hubbard * Redistribution and use in source and binary forms, with or without 52ec21a68SJordan K. Hubbard * modification, are permitted provided that the following conditions 62ec21a68SJordan K. Hubbard * are met: 72ec21a68SJordan K. Hubbard * 1. Redistributions of source code must retain the above copyright 82ec21a68SJordan K. Hubbard * notice, this list of conditions and the following disclaimer. 92ec21a68SJordan K. Hubbard * 2. Redistributions in binary form must reproduce the above copyright 102ec21a68SJordan K. Hubbard * notice, this list of conditions and the following disclaimer in the 112ec21a68SJordan K. Hubbard * documentation and/or other materials provided with the distribution. 122ec21a68SJordan K. Hubbard * 3. All advertising materials mentioning features or use of this software 132ec21a68SJordan K. Hubbard * must display the following acknowledgement: 142ec21a68SJordan K. Hubbard * This product includes software developed by Christopher G. Demetriou 152ec21a68SJordan K. Hubbard * for the NetBSD Project. 162ec21a68SJordan K. Hubbard * 4. The name of the author may not be used to endorse or promote products 172ec21a68SJordan K. Hubbard * derived from this software without specific prior written permission 182ec21a68SJordan K. Hubbard * 192ec21a68SJordan K. Hubbard * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 202ec21a68SJordan K. Hubbard * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 212ec21a68SJordan K. Hubbard * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 222ec21a68SJordan K. Hubbard * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 232ec21a68SJordan K. Hubbard * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 242ec21a68SJordan K. Hubbard * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 252ec21a68SJordan K. Hubbard * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 262ec21a68SJordan K. Hubbard * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 272ec21a68SJordan K. Hubbard * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 282ec21a68SJordan K. Hubbard * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 292ec21a68SJordan K. Hubbard */ 302ec21a68SJordan K. Hubbard 312ec21a68SJordan K. Hubbard #include <sys/cdefs.h> 322ec21a68SJordan K. Hubbard #ifndef lint 335e6220d9SDavid E. O'Brien #if 0 3496acb2c4SPedro F. Giffuni __RCSID("$NetBSD: exec_elf32.c,v 1.6 1999/09/20 04:12:16 christos Exp $"); 352ec21a68SJordan K. Hubbard #endif 365e6220d9SDavid E. O'Brien #endif 3790b77cf3SDavid E. O'Brien __FBSDID("$FreeBSD$"); 382ec21a68SJordan K. Hubbard 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 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 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 * 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 * 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 1562ec21a68SJordan K. Hubbard ELFNAMEEND(check)(int fd, const char *fn) 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 /* 1632ec21a68SJordan K. Hubbard * Check the header to maek 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; 181*66137b66SEd Maste #ifndef EM_AARCH64 182*66137b66SEd Maste #define EM_AARCH64 183 183*66137b66SEd 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; 19066c795d4SRuslan Ermilov case EM_SPARCV9: break; 1918e81109eSPeter Wemm case EM_X86_64: break; 1922ec21a68SJordan K. Hubbard /* ELFDEFNNAME(MACHDEP_ID_CASES) */ 1932ec21a68SJordan K. Hubbard 1942ec21a68SJordan K. Hubbard default: 1952ec21a68SJordan K. Hubbard return 0; 1962ec21a68SJordan K. Hubbard } 1972ec21a68SJordan K. Hubbard 1982ec21a68SJordan K. Hubbard return 1; 1992ec21a68SJordan K. Hubbard } 2002ec21a68SJordan K. Hubbard 2017420b323SAdrian Chadd /* 2027420b323SAdrian Chadd * This function 'hides' (some of) ELF executable file's symbols. 2037420b323SAdrian Chadd * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>". 2047420b323SAdrian Chadd * Symbols in the global keep list, or which are marked as being undefined, 2057420b323SAdrian Chadd * are left alone. 2067420b323SAdrian Chadd * 2077420b323SAdrian Chadd * An old version of this code shuffled various tables around, turning 2087420b323SAdrian Chadd * global symbols to be hidden into local symbols. That lost on the 2097420b323SAdrian Chadd * mips, because CALL16 relocs must reference global symbols, and, if 2107420b323SAdrian Chadd * those symbols were being hidden, they were no longer global. 2117420b323SAdrian Chadd * 2127420b323SAdrian Chadd * The new renaming behaviour doesn't take global symbols out of the 2137420b323SAdrian Chadd * namespace. However, it's ... unlikely that there will ever be 2147420b323SAdrian Chadd * any collisions in practice because of the new method. 2157420b323SAdrian Chadd */ 2162ec21a68SJordan K. Hubbard int 2172ec21a68SJordan K. Hubbard ELFNAMEEND(hide)(int fd, const char *fn) 2182ec21a68SJordan K. Hubbard { 2192ec21a68SJordan K. Hubbard Elf_Ehdr ehdr; 220b5782a47SPedro F. Giffuni struct shlayout *layoutp = NULL; 221b5782a47SPedro F. Giffuni Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr; 222b5782a47SPedro F. Giffuni Elf_Shdr shdrshdr; 2232ec21a68SJordan K. Hubbard Elf_Sym *symtabp = NULL; 224b5782a47SPedro F. Giffuni char *shstrtabp = NULL, *strtabp = NULL; 2251d76342eSEitan Adler Elf_Size nsyms, ewi; 226b5782a47SPedro F. Giffuni Elf_Off off; 2272ec21a68SJordan K. Hubbard ssize_t shdrsize; 228b5782a47SPedro F. Giffuni int rv, i, weird, l, m, r, strtabidx; 229b5782a47SPedro F. Giffuni size_t nstrtab_size, nstrtab_nextoff, fn_size, size; 2307420b323SAdrian Chadd char *nstrtabp = NULL; 231d8d97708SRuslan Ermilov unsigned char data; 2327420b323SAdrian Chadd const char *weirdreason = NULL; 233b5782a47SPedro F. Giffuni void *buf; 234e8ee1a1eSPedro F. Giffuni Elf_Half shnum; 2352ec21a68SJordan K. Hubbard 2362ec21a68SJordan K. Hubbard rv = 0; 2372ec21a68SJordan K. Hubbard if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr) 2382ec21a68SJordan K. Hubbard goto bad; 2392ec21a68SJordan K. Hubbard 240d8d97708SRuslan Ermilov data = ehdr.e_ident[EI_DATA]; 241e8ee1a1eSPedro F. Giffuni shnum = xe16toh(ehdr.e_shnum); 242d8d97708SRuslan Ermilov 243e8ee1a1eSPedro F. Giffuni shdrsize = shnum * xe16toh(ehdr.e_shentsize); 2442ec21a68SJordan K. Hubbard if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL) 2452ec21a68SJordan K. Hubbard goto bad; 246d8d97708SRuslan Ermilov if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) != 247d8d97708SRuslan Ermilov shdrsize) 2482ec21a68SJordan K. Hubbard goto bad; 2492ec21a68SJordan K. Hubbard 250b5782a47SPedro F. Giffuni symtabshdr = strtabshdr = shstrtabshdr = NULL; 2512ec21a68SJordan K. Hubbard weird = 0; 252e8ee1a1eSPedro F. Giffuni for (i = 0; i < shnum; i++) { 253d8d97708SRuslan Ermilov switch (xe32toh(shdrp[i].sh_type)) { 2542ec21a68SJordan K. Hubbard case SHT_SYMTAB: 255b5782a47SPedro F. Giffuni if (symtabshdr != NULL) { 2562ec21a68SJordan K. Hubbard weird = 1; 257b5782a47SPedro F. Giffuni weirdreason = "multiple symbol tables"; 258b5782a47SPedro F. Giffuni } 2592ec21a68SJordan K. Hubbard symtabshdr = &shdrp[i]; 260d8d97708SRuslan Ermilov strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)]; 261b5782a47SPedro F. Giffuni break; 262b5782a47SPedro F. Giffuni case SHT_STRTAB: 263b5782a47SPedro F. Giffuni if (i == xe16toh(ehdr.e_shstrndx)) 264b5782a47SPedro F. Giffuni shstrtabshdr = &shdrp[i]; 2652ec21a68SJordan K. Hubbard break; 2662ec21a68SJordan K. Hubbard } 2672ec21a68SJordan K. Hubbard } 2682ec21a68SJordan K. Hubbard if (symtabshdr == NULL) 2692ec21a68SJordan K. Hubbard goto out; 270b5782a47SPedro F. Giffuni if (strtabshdr == NULL) { 2712ec21a68SJordan K. Hubbard weird = 1; 272b5782a47SPedro F. Giffuni weirdreason = "string table does not exist"; 2737420b323SAdrian Chadd } 274b5782a47SPedro F. Giffuni if (shstrtabshdr == NULL) { 275b5782a47SPedro F. Giffuni weird = 1; 276b5782a47SPedro F. Giffuni weirdreason = "section header string table does not exist"; 277b5782a47SPedro F. Giffuni } 278b5782a47SPedro F. Giffuni if (weirdreason == NULL) 279b5782a47SPedro F. Giffuni weirdreason = "unsupported"; 2802ec21a68SJordan K. Hubbard if (weird) { 2817420b323SAdrian Chadd fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason); 2822ec21a68SJordan K. Hubbard goto bad; 2832ec21a68SJordan K. Hubbard } 2842ec21a68SJordan K. Hubbard 2852ec21a68SJordan K. Hubbard /* 286b5782a47SPedro F. Giffuni * sort section layout table by offset 287b5782a47SPedro F. Giffuni */ 288e8ee1a1eSPedro F. Giffuni layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout), 289b5782a47SPedro F. Giffuni fn, "layout table"); 290b5782a47SPedro F. Giffuni if (layoutp == NULL) 291b5782a47SPedro F. Giffuni goto bad; 292b5782a47SPedro F. Giffuni 293b5782a47SPedro F. Giffuni /* add a pseudo entry to represent the section header table */ 294b5782a47SPedro F. Giffuni shdrshdr.sh_offset = ehdr.e_shoff; 295b5782a47SPedro F. Giffuni shdrshdr.sh_size = htoxew(shdrsize); 296b5782a47SPedro F. Giffuni shdrshdr.sh_addralign = htoxew(ELFSIZE / 8); 297e8ee1a1eSPedro F. Giffuni layoutp[shnum].shdr = &shdrshdr; 298b5782a47SPedro F. Giffuni 299b5782a47SPedro F. Giffuni /* insert and sort normal section headers */ 300e8ee1a1eSPedro F. Giffuni for (i = shnum; i-- != 0;) { 301b5782a47SPedro F. Giffuni l = i + 1; 302e8ee1a1eSPedro F. Giffuni r = shnum; 303b5782a47SPedro F. Giffuni while (l <= r) { 304b5782a47SPedro F. Giffuni m = ( l + r) / 2; 305b5782a47SPedro F. Giffuni if (xewtoh(shdrp[i].sh_offset) > 306b5782a47SPedro F. Giffuni xewtoh(layoutp[m].shdr->sh_offset)) 307b5782a47SPedro F. Giffuni l = m + 1; 308b5782a47SPedro F. Giffuni else 309b5782a47SPedro F. Giffuni r = m - 1; 310b5782a47SPedro F. Giffuni } 311b5782a47SPedro F. Giffuni 312b5782a47SPedro F. Giffuni if (r != i) { 313b5782a47SPedro F. Giffuni memmove(&layoutp[i], &layoutp[i + 1], 314b5782a47SPedro F. Giffuni sizeof(struct shlayout) * (r - i)); 315b5782a47SPedro F. Giffuni } 316b5782a47SPedro F. Giffuni 317b5782a47SPedro F. Giffuni layoutp[r].shdr = &shdrp[i]; 318b5782a47SPedro F. Giffuni layoutp[r].bufp = NULL; 319b5782a47SPedro F. Giffuni } 320e8ee1a1eSPedro F. Giffuni ++shnum; 321b5782a47SPedro F. Giffuni 322b5782a47SPedro F. Giffuni /* 3232ec21a68SJordan K. Hubbard * load up everything we need 3242ec21a68SJordan K. Hubbard */ 3252ec21a68SJordan K. Hubbard 326b5782a47SPedro F. Giffuni /* load section string table for debug use */ 327e52f9715SEd Maste if ((size = xewtoh(shstrtabshdr->sh_size)) == 0) 328e52f9715SEd Maste goto bad; 329e52f9715SEd Maste if ((shstrtabp = xmalloc(size, fn, "section string table")) == NULL) 3302ec21a68SJordan K. Hubbard goto bad; 331b5782a47SPedro F. Giffuni if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset), 332e52f9715SEd Maste size, fn) != size) 333e52f9715SEd Maste goto bad; 334e52f9715SEd Maste if (shstrtabp[size - 1] != '\0') 3352ec21a68SJordan K. Hubbard goto bad; 3362ec21a68SJordan K. Hubbard 337b5782a47SPedro F. Giffuni /* we need symtab, strtab, and everything behind strtab */ 338b5782a47SPedro F. Giffuni strtabidx = INT_MAX; 339e8ee1a1eSPedro F. Giffuni for (i = 0; i < shnum; i++) { 340b5782a47SPedro F. Giffuni if (layoutp[i].shdr == &shdrshdr) { 341b5782a47SPedro F. Giffuni /* not load section header again */ 342b5782a47SPedro F. Giffuni layoutp[i].bufp = shdrp; 343b5782a47SPedro F. Giffuni continue; 344b5782a47SPedro F. Giffuni } 345b5782a47SPedro F. Giffuni if (layoutp[i].shdr == shstrtabshdr) { 346b5782a47SPedro F. Giffuni /* not load section string table again */ 347b5782a47SPedro F. Giffuni layoutp[i].bufp = shstrtabp; 348b5782a47SPedro F. Giffuni continue; 349b5782a47SPedro F. Giffuni } 350b5782a47SPedro F. Giffuni 351b5782a47SPedro F. Giffuni if (layoutp[i].shdr == strtabshdr) 352b5782a47SPedro F. Giffuni strtabidx = i; 353b5782a47SPedro F. Giffuni if (layoutp[i].shdr == symtabshdr || i >= strtabidx) { 354b5782a47SPedro F. Giffuni off = xewtoh(layoutp[i].shdr->sh_offset); 355e52f9715SEd Maste if ((size = xewtoh(layoutp[i].shdr->sh_size)) == 0) 356e52f9715SEd Maste goto bad; 357b5782a47SPedro F. Giffuni layoutp[i].bufp = xmalloc(size, fn, 358b5782a47SPedro F. Giffuni shstrtabp + xewtoh(layoutp[i].shdr->sh_name)); 359b5782a47SPedro F. Giffuni if (layoutp[i].bufp == NULL) 3602ec21a68SJordan K. Hubbard goto bad; 361b5782a47SPedro F. Giffuni if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) != 362b5782a47SPedro F. Giffuni size) 3632ec21a68SJordan K. Hubbard goto bad; 3642ec21a68SJordan K. Hubbard 365b5782a47SPedro F. Giffuni /* set symbol table and string table */ 366e52f9715SEd Maste if (layoutp[i].shdr == symtabshdr) { 367b5782a47SPedro F. Giffuni symtabp = layoutp[i].bufp; 368e52f9715SEd Maste } else if (layoutp[i].shdr == strtabshdr) { 369b5782a47SPedro F. Giffuni strtabp = layoutp[i].bufp; 370e52f9715SEd Maste if (strtabp[size - 1] != '\0') 371e52f9715SEd Maste goto bad; 372e52f9715SEd Maste } 373b5782a47SPedro F. Giffuni } 374b5782a47SPedro F. Giffuni } 375b5782a47SPedro F. Giffuni 3767420b323SAdrian Chadd nstrtab_size = 256; 3777420b323SAdrian Chadd nstrtabp = xmalloc(nstrtab_size, fn, "new string table"); 3787420b323SAdrian Chadd if (nstrtabp == NULL) 3792ec21a68SJordan K. Hubbard goto bad; 3807420b323SAdrian Chadd nstrtab_nextoff = 0; 3812ec21a68SJordan K. Hubbard 3827420b323SAdrian Chadd fn_size = strlen(fn); 3832ec21a68SJordan K. Hubbard 3842ec21a68SJordan K. Hubbard /* Prepare data structures for symbol movement. */ 385d8d97708SRuslan Ermilov nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize); 3862ec21a68SJordan K. Hubbard 3872ec21a68SJordan K. Hubbard /* move symbols, making them local */ 3887420b323SAdrian Chadd for (ewi = 0; ewi < nsyms; ewi++) { 3897420b323SAdrian Chadd Elf_Sym *sp = &symtabp[ewi]; 3907420b323SAdrian Chadd const char *symname = strtabp + xe32toh(sp->st_name); 3917420b323SAdrian Chadd size_t newent_len; 3922ec21a68SJordan K. Hubbard /* 3937420b323SAdrian Chadd * make sure there's size for the next entry, even if it's 3947420b323SAdrian Chadd * as large as it can be. 3957420b323SAdrian Chadd * 3967420b323SAdrian Chadd * "_$$hide$$ <filename> <symname><NUL>" -> 3977420b323SAdrian Chadd * 9 + 3 + sizes of fn and sym name 3982ec21a68SJordan K. Hubbard */ 3997420b323SAdrian Chadd while ((nstrtab_size - nstrtab_nextoff) < 4007420b323SAdrian Chadd strlen(symname) + fn_size + 12) { 4017420b323SAdrian Chadd nstrtab_size *= 2; 4027420b323SAdrian Chadd nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn, 4037420b323SAdrian Chadd "new string table"); 4047420b323SAdrian Chadd if (nstrtabp == NULL) 4057420b323SAdrian Chadd goto bad; 4062ec21a68SJordan K. Hubbard } 4072ec21a68SJordan K. Hubbard 4082c457547SAdrian Chadd sp->st_name = htowew(nstrtab_nextoff); 4092ec21a68SJordan K. Hubbard 4107420b323SAdrian Chadd /* if it's a keeper or is undefined, don't rename it. */ 4117420b323SAdrian Chadd if (in_keep_list(symname) || 4127420b323SAdrian Chadd (xe16toh(sp->st_shndx) == SHN_UNDEF)) { 4137420b323SAdrian Chadd newent_len = sprintf(nstrtabp + nstrtab_nextoff, 4147420b323SAdrian Chadd "%s", symname) + 1; 4157420b323SAdrian Chadd } else { 4167420b323SAdrian Chadd newent_len = sprintf(nstrtabp + nstrtab_nextoff, 4177420b323SAdrian Chadd "_$$hide$$ %s %s", fn, symname) + 1; 4182ec21a68SJordan K. Hubbard } 4197420b323SAdrian Chadd nstrtab_nextoff += newent_len; 4202ec21a68SJordan K. Hubbard } 4217420b323SAdrian Chadd strtabshdr->sh_size = htoxew(nstrtab_nextoff); 4222ec21a68SJordan K. Hubbard 4232ec21a68SJordan K. Hubbard /* 424b5782a47SPedro F. Giffuni * update section header table in ascending order of offset 4252ec21a68SJordan K. Hubbard */ 426e8ee1a1eSPedro F. Giffuni for (i = strtabidx + 1; i < shnum; i++) { 427b5782a47SPedro F. Giffuni Elf_Off off, align; 428b5782a47SPedro F. Giffuni off = xewtoh(layoutp[i - 1].shdr->sh_offset) + 429b5782a47SPedro F. Giffuni xewtoh(layoutp[i - 1].shdr->sh_size); 430b5782a47SPedro F. Giffuni align = xewtoh(layoutp[i].shdr->sh_addralign); 431b5782a47SPedro F. Giffuni off = (off + (align - 1)) & ~(align - 1); 432b5782a47SPedro F. Giffuni layoutp[i].shdr->sh_offset = htoxew(off); 433b5782a47SPedro F. Giffuni } 434b5782a47SPedro F. Giffuni 435b5782a47SPedro F. Giffuni /* 436b5782a47SPedro F. Giffuni * write data to the file in descending order of offset 437b5782a47SPedro F. Giffuni */ 438e8ee1a1eSPedro F. Giffuni for (i = shnum; i-- != 0;) { 439b5782a47SPedro F. Giffuni if (layoutp[i].shdr == strtabshdr) { 440b5782a47SPedro F. Giffuni /* new string table */ 441b5782a47SPedro F. Giffuni buf = nstrtabp; 442b5782a47SPedro F. Giffuni } else 443b5782a47SPedro F. Giffuni buf = layoutp[i].bufp; 444b5782a47SPedro F. Giffuni 445b5782a47SPedro F. Giffuni if (layoutp[i].shdr == &shdrshdr || 446b5782a47SPedro F. Giffuni layoutp[i].shdr == symtabshdr || i >= strtabidx) { 447b5782a47SPedro F. Giffuni if (buf == NULL) 4482ec21a68SJordan K. Hubbard goto bad; 4492ec21a68SJordan K. Hubbard 450b5782a47SPedro F. Giffuni /* 451b5782a47SPedro F. Giffuni * update the offset of section header table in elf 452b5782a47SPedro F. Giffuni * header if needed. 453b5782a47SPedro F. Giffuni */ 454b5782a47SPedro F. Giffuni if (layoutp[i].shdr == &shdrshdr && 455b5782a47SPedro F. Giffuni ehdr.e_shoff != shdrshdr.sh_offset) { 456b5782a47SPedro F. Giffuni ehdr.e_shoff = shdrshdr.sh_offset; 457b15dc003SEd Maste off = offsetof(Elf_Ehdr, e_shoff); 458b5782a47SPedro F. Giffuni size = sizeof(Elf_Off); 459b5782a47SPedro F. Giffuni if ((size_t)xwriteatoff(fd, &ehdr.e_shoff, off, size, 460b5782a47SPedro F. Giffuni fn) != size) 461b5782a47SPedro F. Giffuni goto bad; 462b5782a47SPedro F. Giffuni } 463b5782a47SPedro F. Giffuni 464b5782a47SPedro F. Giffuni off = xewtoh(layoutp[i].shdr->sh_offset); 465b5782a47SPedro F. Giffuni size = xewtoh(layoutp[i].shdr->sh_size); 466b5782a47SPedro F. Giffuni if ((size_t)xwriteatoff(fd, buf, off, size, fn) != size) 467b5782a47SPedro F. Giffuni goto bad; 468b5782a47SPedro F. Giffuni } 469b5782a47SPedro F. Giffuni } 470b5782a47SPedro F. Giffuni 4712ec21a68SJordan K. Hubbard out: 472b5782a47SPedro F. Giffuni if (layoutp != NULL) { 473e8ee1a1eSPedro F. Giffuni for (i = 0; i < shnum; i++) { 474b5782a47SPedro F. Giffuni if (layoutp[i].bufp != NULL) 475b5782a47SPedro F. Giffuni free(layoutp[i].bufp); 476b5782a47SPedro F. Giffuni } 477b5782a47SPedro F. Giffuni free(layoutp); 478b5782a47SPedro F. Giffuni } 47996acb2c4SPedro F. Giffuni free(nstrtabp); 4802ec21a68SJordan K. Hubbard return (rv); 4812ec21a68SJordan K. Hubbard 4822ec21a68SJordan K. Hubbard bad: 4832ec21a68SJordan K. Hubbard rv = 1; 4842ec21a68SJordan K. Hubbard goto out; 4852ec21a68SJordan K. Hubbard } 4862ec21a68SJordan K. Hubbard 4872ec21a68SJordan K. Hubbard #endif /* include this size of ELF */ 488