Home | History | Annotate | Line # | Download | only in crunchide
exec_elf32.c revision 1.8
      1  1.8   hannken /*	$NetBSD: exec_elf32.c,v 1.8 1999/10/22 10:47:37 hannken Exp $	*/
      2  1.1       cgd 
      3  1.1       cgd /*
      4  1.5       cgd  * Copyright (c) 1997, 1998 Christopher G. Demetriou.  All rights reserved.
      5  1.1       cgd  *
      6  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      7  1.1       cgd  * modification, are permitted provided that the following conditions
      8  1.1       cgd  * are met:
      9  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     10  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     11  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     13  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     14  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     15  1.1       cgd  *    must display the following acknowledgement:
     16  1.1       cgd  *      This product includes software developed by Christopher G. Demetriou
     17  1.1       cgd  *	for the NetBSD Project.
     18  1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     19  1.1       cgd  *    derived from this software without specific prior written permission
     20  1.1       cgd  *
     21  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1       cgd  */
     32  1.1       cgd 
     33  1.3     perry #include <sys/cdefs.h>
     34  1.1       cgd #ifndef lint
     35  1.8   hannken __RCSID("$NetBSD: exec_elf32.c,v 1.8 1999/10/22 10:47:37 hannken Exp $");
     36  1.3     perry #endif
     37  1.3     perry 
     38  1.1       cgd #ifndef ELFSIZE
     39  1.1       cgd #define ELFSIZE         32
     40  1.1       cgd #endif
     41  1.1       cgd 
     42  1.1       cgd #include <sys/types.h>
     43  1.1       cgd #include <sys/stat.h>
     44  1.4     mikel 
     45  1.4     mikel #include <errno.h>
     46  1.1       cgd #include <stdio.h>
     47  1.2       cgd #include <stdlib.h>
     48  1.1       cgd #include <string.h>
     49  1.4     mikel #include <unistd.h>
     50  1.4     mikel 
     51  1.1       cgd #include "extern.h"
     52  1.1       cgd 
     53  1.1       cgd #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
     54  1.1       cgd     (defined(NLIST_ELF64) && (ELFSIZE == 64))
     55  1.1       cgd 
     56  1.1       cgd #include <sys/exec_elf.h>
     57  1.8   hannken 
     58  1.8   hannken #define CONCAT(x,y)     __CONCAT(x,y)
     59  1.8   hannken #define ELFNAME(x)      CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
     60  1.8   hannken #define ELFNAME2(x,y)   CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
     61  1.8   hannken #define ELFNAMEEND(x)   CONCAT(x,CONCAT(_elf,ELFSIZE))
     62  1.8   hannken #define ELFDEFNNAME(x)  CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
     63  1.1       cgd 
     64  1.2       cgd struct listelem {
     65  1.2       cgd 	struct listelem *next;
     66  1.2       cgd 	void *mem;
     67  1.2       cgd 	off_t file;
     68  1.2       cgd 	size_t size;
     69  1.2       cgd };
     70  1.2       cgd 
     71  1.2       cgd static ssize_t
     72  1.2       cgd xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
     73  1.2       cgd {
     74  1.2       cgd 	ssize_t rv;
     75  1.2       cgd 
     76  1.2       cgd 	if (lseek(fd, off, SEEK_SET) != off) {
     77  1.2       cgd 		perror(fn);
     78  1.2       cgd 		return -1;
     79  1.2       cgd 	}
     80  1.2       cgd 	if ((rv = read(fd, buf, size)) != size) {
     81  1.2       cgd 		fprintf(stderr, "%s: read error: %s\n", fn,
     82  1.2       cgd 		    rv == -1 ? strerror(errno) : "short read");
     83  1.2       cgd 		return -1;
     84  1.2       cgd 	}
     85  1.2       cgd 	return size;
     86  1.2       cgd }
     87  1.2       cgd 
     88  1.2       cgd static ssize_t
     89  1.2       cgd xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
     90  1.2       cgd {
     91  1.2       cgd 	ssize_t rv;
     92  1.2       cgd 
     93  1.2       cgd 	if (lseek(fd, off, SEEK_SET) != off) {
     94  1.2       cgd 		perror(fn);
     95  1.2       cgd 		return -1;
     96  1.2       cgd 	}
     97  1.2       cgd 	if ((rv = write(fd, buf, size)) != size) {
     98  1.2       cgd 		fprintf(stderr, "%s: write error: %s\n", fn,
     99  1.2       cgd 		    rv == -1 ? strerror(errno) : "short write");
    100  1.2       cgd 		return -1;
    101  1.2       cgd 	}
    102  1.2       cgd 	return size;
    103  1.2       cgd }
    104  1.2       cgd 
    105  1.2       cgd static void *
    106  1.2       cgd xmalloc(size_t size, const char *fn, const char *use)
    107  1.2       cgd {
    108  1.2       cgd 	void *rv;
    109  1.2       cgd 
    110  1.2       cgd 	rv = malloc(size);
    111  1.2       cgd 	if (rv == NULL)
    112  1.2       cgd 		fprintf(stderr, "%s: out of memory (allocating for %s)\n",
    113  1.2       cgd 		    fn, use);
    114  1.2       cgd 	return (rv);
    115  1.2       cgd }
    116  1.2       cgd 
    117  1.5       cgd static void *
    118  1.5       cgd xrealloc(void *ptr, size_t size, const char *fn, const char *use)
    119  1.5       cgd {
    120  1.5       cgd 	void *rv;
    121  1.5       cgd 
    122  1.5       cgd 	rv = realloc(ptr, size);
    123  1.5       cgd 	if (rv == NULL) {
    124  1.5       cgd 		free(ptr);
    125  1.5       cgd 		fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
    126  1.5       cgd 		    fn, use);
    127  1.5       cgd 	}
    128  1.5       cgd 	return (rv);
    129  1.5       cgd }
    130  1.5       cgd 
    131  1.1       cgd int
    132  1.2       cgd ELFNAMEEND(check)(int fd, const char *fn)
    133  1.1       cgd {
    134  1.1       cgd 	Elf_Ehdr eh;
    135  1.1       cgd 	struct stat sb;
    136  1.1       cgd 
    137  1.1       cgd 	/*
    138  1.1       cgd 	 * Check the header to maek sure it's an ELF file (of the
    139  1.1       cgd 	 * appropriate size).
    140  1.1       cgd 	 */
    141  1.1       cgd 	if (fstat(fd, &sb) == -1)
    142  1.1       cgd 		return 0;
    143  1.1       cgd 	if (sb.st_size < sizeof eh)
    144  1.1       cgd 		return 0;
    145  1.1       cgd 	if (read(fd, &eh, sizeof eh) != sizeof eh)
    146  1.1       cgd 		return 0;
    147  1.1       cgd 
    148  1.1       cgd 	if (memcmp(eh.e_ident, Elf_e_ident, Elf_e_siz))
    149  1.1       cgd                 return 0;
    150  1.1       cgd 
    151  1.1       cgd         switch (eh.e_machine) {
    152  1.1       cgd         ELFDEFNNAME(MACHDEP_ID_CASES)
    153  1.1       cgd 
    154  1.1       cgd         default:
    155  1.1       cgd                 return 0;
    156  1.1       cgd         }
    157  1.1       cgd 
    158  1.1       cgd 	return 1;
    159  1.1       cgd }
    160  1.1       cgd 
    161  1.5       cgd /*
    162  1.5       cgd  * This function 'hides' (some of) ELF executable file's symbols.
    163  1.5       cgd  * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
    164  1.5       cgd  * Symbols in the global keep list, or which are marked as being undefined,
    165  1.5       cgd  * are left alone.
    166  1.5       cgd  *
    167  1.5       cgd  * An old version of this code shuffled various tables around, turning
    168  1.5       cgd  * global symbols to be hidden into local symbols.  That lost on the
    169  1.5       cgd  * mips, because CALL16 relocs must reference global symbols, and, if
    170  1.5       cgd  * those symbols were being hidden, they were no longer global.
    171  1.5       cgd  *
    172  1.5       cgd  * The new renaming behaviour doesn't take global symbols out of the
    173  1.5       cgd  * namespace.  However, it's ... unlikely that there will ever be
    174  1.5       cgd  * any collisions in practice because of the new method.
    175  1.5       cgd  */
    176  1.1       cgd int
    177  1.2       cgd ELFNAMEEND(hide)(int fd, const char *fn)
    178  1.1       cgd {
    179  1.2       cgd 	Elf_Ehdr ehdr;
    180  1.5       cgd 	Elf_Shdr *shdrp = NULL;
    181  1.5       cgd 	int symtabsnum, strtabsnum;
    182  1.2       cgd 	Elf_Sym *symtabp = NULL;
    183  1.5       cgd 	char *strtabp = NULL, *nstrtabp = NULL;
    184  1.5       cgd 	Elf_Word nsyms;
    185  1.5       cgd 	Elf_Off stroff, maxoff;
    186  1.5       cgd 	const char *weirdreason;
    187  1.2       cgd 	ssize_t shdrsize;
    188  1.5       cgd 	size_t nstrtab_size, nstrtab_nextoff, fn_size;
    189  1.2       cgd 	int rv, i, weird;
    190  1.2       cgd 
    191  1.2       cgd 	rv = 0;
    192  1.2       cgd 	if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
    193  1.2       cgd 		goto bad;
    194  1.2       cgd 
    195  1.2       cgd 	shdrsize = ehdr.e_shnum * ehdr.e_shentsize;
    196  1.2       cgd 	if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
    197  1.2       cgd 		goto bad;
    198  1.2       cgd 	if (xreadatoff(fd, shdrp, ehdr.e_shoff, shdrsize, fn) != shdrsize)
    199  1.2       cgd 		goto bad;
    200  1.2       cgd 
    201  1.5       cgd 	symtabsnum = strtabsnum = -1;
    202  1.5       cgd 	maxoff = stroff = 0;
    203  1.2       cgd 	weird = 0;
    204  1.5       cgd 	weirdreason = "???";
    205  1.2       cgd 	for (i = 0; i < ehdr.e_shnum; i++) {
    206  1.5       cgd 		if (shdrp[i].sh_offset > maxoff) {
    207  1.5       cgd 			maxoff = shdrp[i].sh_offset;
    208  1.5       cgd 		}
    209  1.2       cgd 		switch (shdrp[i].sh_type) {
    210  1.2       cgd 		case Elf_sht_symtab:
    211  1.5       cgd 			if (!weird && symtabsnum != -1) {
    212  1.5       cgd 				weird = 1;
    213  1.5       cgd 				weirdreason = "multiple symbol tables";
    214  1.5       cgd 			}
    215  1.5       cgd 			symtabsnum = i;
    216  1.5       cgd 			strtabsnum = shdrp[i].sh_link;
    217  1.5       cgd 			stroff = shdrp[strtabsnum].sh_offset;
    218  1.5       cgd 			if (!weird && strtabsnum != (ehdr.e_shnum - 1)) {
    219  1.2       cgd 				weird = 1;
    220  1.5       cgd 				weirdreason = "string table not last section";
    221  1.5       cgd 			}
    222  1.2       cgd 			break;
    223  1.2       cgd 		}
    224  1.2       cgd 	}
    225  1.5       cgd 	if (symtabsnum == -1)
    226  1.2       cgd 		goto out;
    227  1.5       cgd 	if (!weird && strtabsnum == -1) {
    228  1.5       cgd 		weird = 1;
    229  1.5       cgd 		weirdreason = "no string table found";
    230  1.5       cgd 	}
    231  1.5       cgd 	if (!weird && stroff != maxoff) {
    232  1.2       cgd 		weird = 1;
    233  1.5       cgd 		weirdreason = "string table section not last in file";
    234  1.5       cgd 	}
    235  1.2       cgd 	if (weird) {
    236  1.5       cgd 		fprintf(stderr, "%s: weird executable (%s); unsupported\n", fn,
    237  1.5       cgd 		    weirdreason);
    238  1.2       cgd 		goto bad;
    239  1.2       cgd 	}
    240  1.2       cgd 
    241  1.2       cgd 	/*
    242  1.2       cgd 	 * load up everything we need
    243  1.2       cgd 	 */
    244  1.2       cgd 
    245  1.2       cgd 	/* symbol table */
    246  1.5       cgd 	if ((symtabp = xmalloc(shdrp[symtabsnum].sh_size, fn, "symbol table"))
    247  1.2       cgd 	    == NULL)
    248  1.2       cgd 		goto bad;
    249  1.5       cgd 	if (xreadatoff(fd, symtabp, shdrp[symtabsnum].sh_offset,
    250  1.5       cgd 	    shdrp[symtabsnum].sh_size, fn) != shdrp[symtabsnum].sh_size)
    251  1.2       cgd 		goto bad;
    252  1.2       cgd 
    253  1.2       cgd 	/* string table */
    254  1.5       cgd 	if ((strtabp = xmalloc(shdrp[strtabsnum].sh_size, fn, "string table"))
    255  1.2       cgd 	    == NULL)
    256  1.2       cgd 		goto bad;
    257  1.5       cgd 	if (xreadatoff(fd, strtabp, shdrp[strtabsnum].sh_offset,
    258  1.5       cgd 	    shdrp[strtabsnum].sh_size, fn) != shdrp[strtabsnum].sh_size)
    259  1.2       cgd 		goto bad;
    260  1.2       cgd 
    261  1.5       cgd 	nsyms = shdrp[symtabsnum].sh_size / shdrp[symtabsnum].sh_entsize;
    262  1.5       cgd 
    263  1.5       cgd 	nstrtab_size = 256;
    264  1.5       cgd 	nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
    265  1.5       cgd 	if (nstrtabp == NULL)
    266  1.5       cgd 		goto bad;
    267  1.5       cgd 	nstrtab_nextoff = 0;
    268  1.2       cgd 
    269  1.5       cgd 	fn_size = strlen(fn);
    270  1.2       cgd 
    271  1.5       cgd 	for (i = 0; i < nsyms; i++) {
    272  1.5       cgd 		Elf_Sym *sp = &symtabp[i];
    273  1.5       cgd 		const char *symname = strtabp + sp->st_name;
    274  1.5       cgd 		size_t newent_len;
    275  1.2       cgd 
    276  1.2       cgd 		/*
    277  1.5       cgd 		 * make sure there's size for the next entry, even if it's
    278  1.5       cgd 		 * as large as it can be.
    279  1.5       cgd 		 *
    280  1.5       cgd 		 * "_$$hide$$ <filename> <symname><NUL>" ->
    281  1.5       cgd 		 *    9 + 3 + sizes of fn and sym name
    282  1.2       cgd 		 */
    283  1.5       cgd 		while ((nstrtab_size - nstrtab_nextoff) <
    284  1.5       cgd 		    strlen(symname) + fn_size + 12) {
    285  1.5       cgd 			nstrtab_size *= 2;
    286  1.5       cgd 			nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
    287  1.5       cgd 			    "new string table");
    288  1.5       cgd 			if (nstrtabp == NULL)
    289  1.5       cgd 				goto bad;
    290  1.5       cgd 		}
    291  1.2       cgd 
    292  1.5       cgd 		sp->st_name = nstrtab_nextoff;
    293  1.2       cgd 
    294  1.5       cgd 		/* if it's a keeper or is undefined, don't rename it. */
    295  1.5       cgd 		if (in_keep_list(symname) ||
    296  1.5       cgd 		    sp->st_shndx == Elf_eshn_undefined) {
    297  1.5       cgd 			newent_len = sprintf(nstrtabp + nstrtab_nextoff,
    298  1.5       cgd 			    "%s", symname) + 1;
    299  1.5       cgd 		} else {
    300  1.5       cgd 			newent_len = sprintf(nstrtabp + nstrtab_nextoff,
    301  1.5       cgd 			    "_$$hide$$ %s %s", fn, symname) + 1;
    302  1.2       cgd 		}
    303  1.5       cgd 		nstrtab_nextoff += newent_len;
    304  1.5       cgd 	}
    305  1.5       cgd 	shdrp[strtabsnum].sh_size = nstrtab_nextoff;
    306  1.1       cgd 
    307  1.2       cgd 	/*
    308  1.2       cgd 	 * write new tables to the file
    309  1.2       cgd 	 */
    310  1.2       cgd 	if (xwriteatoff(fd, shdrp, ehdr.e_shoff, shdrsize, fn) != shdrsize)
    311  1.2       cgd 		goto bad;
    312  1.5       cgd 	if (xwriteatoff(fd, symtabp, shdrp[symtabsnum].sh_offset,
    313  1.5       cgd 	    shdrp[symtabsnum].sh_size, fn) != shdrp[symtabsnum].sh_size)
    314  1.5       cgd 		goto bad;
    315  1.5       cgd 	if (xwriteatoff(fd, nstrtabp, shdrp[strtabsnum].sh_offset,
    316  1.5       cgd 	    shdrp[strtabsnum].sh_size, fn) != shdrp[strtabsnum].sh_size)
    317  1.2       cgd 		goto bad;
    318  1.2       cgd 
    319  1.2       cgd out:
    320  1.2       cgd 	if (shdrp != NULL)
    321  1.2       cgd 		free(shdrp);
    322  1.2       cgd 	if (symtabp != NULL)
    323  1.2       cgd 		free(symtabp);
    324  1.2       cgd 	if (strtabp != NULL)
    325  1.2       cgd 		free(strtabp);
    326  1.5       cgd 	if (nstrtabp != NULL)
    327  1.6  christos 		free(nstrtabp);
    328  1.2       cgd 	return (rv);
    329  1.2       cgd 
    330  1.2       cgd bad:
    331  1.2       cgd 	rv = 1;
    332  1.2       cgd 	goto out;
    333  1.1       cgd }
    334  1.1       cgd 
    335  1.1       cgd #endif /* include this size of ELF */
    336