2000-06-14 21:24:02 +04:00
|
|
|
/* $NetBSD: exec_elf32.c,v 1.12 2000/06/14 17:26:00 cgd Exp $ */
|
1997-01-23 02:57:18 +03:00
|
|
|
|
|
|
|
/*
|
2000-06-14 21:24:02 +04:00
|
|
|
* Copyright (c) 1997, 1998 Christopher G. Demetriou
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1997-01-23 02:57:18 +03:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
2000-06-14 21:24:02 +04:00
|
|
|
* This product includes software developed for the
|
|
|
|
* NetBSD Project. See http://www.netbsd.org/ for
|
|
|
|
* information about NetBSD.
|
1997-01-23 02:57:18 +03:00
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
2000-06-14 21:24:02 +04:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
1997-01-23 02:57:18 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2000-06-14 21:24:02 +04:00
|
|
|
*
|
|
|
|
* <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
|
1997-01-23 02:57:18 +03:00
|
|
|
*/
|
|
|
|
|
1997-08-03 01:30:05 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-01-23 02:57:18 +03:00
|
|
|
#ifndef lint
|
2000-06-14 21:24:02 +04:00
|
|
|
__RCSID("$NetBSD: exec_elf32.c,v 1.12 2000/06/14 17:26:00 cgd Exp $");
|
1997-08-03 01:30:05 +04:00
|
|
|
#endif
|
|
|
|
|
1997-01-23 02:57:18 +03:00
|
|
|
#ifndef ELFSIZE
|
|
|
|
#define ELFSIZE 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
1997-08-12 10:07:23 +04:00
|
|
|
|
|
|
|
#include <errno.h>
|
1997-01-23 02:57:18 +03:00
|
|
|
#include <stdio.h>
|
1997-01-23 08:43:29 +03:00
|
|
|
#include <stdlib.h>
|
1997-01-23 02:57:18 +03:00
|
|
|
#include <string.h>
|
1997-08-12 10:07:23 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1997-01-23 02:57:18 +03:00
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
|
|
|
|
(defined(NLIST_ELF64) && (ELFSIZE == 64))
|
|
|
|
|
|
|
|
#include <sys/exec_elf.h>
|
|
|
|
|
1997-01-23 08:43:29 +03:00
|
|
|
struct listelem {
|
|
|
|
struct listelem *next;
|
|
|
|
void *mem;
|
|
|
|
off_t file;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
|
|
|
|
{
|
|
|
|
ssize_t rv;
|
|
|
|
|
|
|
|
if (lseek(fd, off, SEEK_SET) != off) {
|
|
|
|
perror(fn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((rv = read(fd, buf, size)) != size) {
|
|
|
|
fprintf(stderr, "%s: read error: %s\n", fn,
|
|
|
|
rv == -1 ? strerror(errno) : "short read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
|
|
|
|
{
|
|
|
|
ssize_t rv;
|
|
|
|
|
|
|
|
if (lseek(fd, off, SEEK_SET) != off) {
|
|
|
|
perror(fn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((rv = write(fd, buf, size)) != size) {
|
|
|
|
fprintf(stderr, "%s: write error: %s\n", fn,
|
|
|
|
rv == -1 ? strerror(errno) : "short write");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
xmalloc(size_t size, const char *fn, const char *use)
|
|
|
|
{
|
|
|
|
void *rv;
|
|
|
|
|
|
|
|
rv = malloc(size);
|
|
|
|
if (rv == NULL)
|
|
|
|
fprintf(stderr, "%s: out of memory (allocating for %s)\n",
|
|
|
|
fn, use);
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
1998-11-03 05:30:16 +03:00
|
|
|
static void *
|
|
|
|
xrealloc(void *ptr, size_t size, const char *fn, const char *use)
|
|
|
|
{
|
|
|
|
void *rv;
|
|
|
|
|
|
|
|
rv = realloc(ptr, size);
|
|
|
|
if (rv == NULL) {
|
|
|
|
free(ptr);
|
|
|
|
fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
|
|
|
|
fn, use);
|
|
|
|
}
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
1997-01-23 02:57:18 +03:00
|
|
|
int
|
1997-01-23 08:43:29 +03:00
|
|
|
ELFNAMEEND(check)(int fd, const char *fn)
|
1997-01-23 02:57:18 +03:00
|
|
|
{
|
|
|
|
Elf_Ehdr eh;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the header to maek sure it's an ELF file (of the
|
|
|
|
* appropriate size).
|
|
|
|
*/
|
|
|
|
if (fstat(fd, &sb) == -1)
|
|
|
|
return 0;
|
|
|
|
if (sb.st_size < sizeof eh)
|
|
|
|
return 0;
|
|
|
|
if (read(fd, &eh, sizeof eh) != sizeof eh)
|
|
|
|
return 0;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
if (memcmp(eh.e_ident, ELFMAG, SELFMAG) != 0 ||
|
|
|
|
eh.e_ident[EI_CLASS] != ELFCLASS)
|
1997-01-23 02:57:18 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (eh.e_machine) {
|
|
|
|
ELFDEFNNAME(MACHDEP_ID_CASES)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-11-03 05:30:16 +03:00
|
|
|
/*
|
|
|
|
* This function 'hides' (some of) ELF executable file's symbols.
|
|
|
|
* It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
|
|
|
|
* Symbols in the global keep list, or which are marked as being undefined,
|
|
|
|
* are left alone.
|
|
|
|
*
|
|
|
|
* An old version of this code shuffled various tables around, turning
|
|
|
|
* global symbols to be hidden into local symbols. That lost on the
|
|
|
|
* mips, because CALL16 relocs must reference global symbols, and, if
|
|
|
|
* those symbols were being hidden, they were no longer global.
|
|
|
|
*
|
|
|
|
* The new renaming behaviour doesn't take global symbols out of the
|
|
|
|
* namespace. However, it's ... unlikely that there will ever be
|
|
|
|
* any collisions in practice because of the new method.
|
|
|
|
*/
|
1997-01-23 02:57:18 +03:00
|
|
|
int
|
1997-01-23 08:43:29 +03:00
|
|
|
ELFNAMEEND(hide)(int fd, const char *fn)
|
1997-01-23 02:57:18 +03:00
|
|
|
{
|
1997-01-23 08:43:29 +03:00
|
|
|
Elf_Ehdr ehdr;
|
1998-11-03 05:30:16 +03:00
|
|
|
Elf_Shdr *shdrp = NULL;
|
|
|
|
int symtabsnum, strtabsnum;
|
1997-01-23 08:43:29 +03:00
|
|
|
Elf_Sym *symtabp = NULL;
|
1998-11-03 05:30:16 +03:00
|
|
|
char *strtabp = NULL, *nstrtabp = NULL;
|
|
|
|
Elf_Word nsyms;
|
|
|
|
Elf_Off stroff, maxoff;
|
|
|
|
const char *weirdreason;
|
1997-01-23 08:43:29 +03:00
|
|
|
ssize_t shdrsize;
|
1998-11-03 05:30:16 +03:00
|
|
|
size_t nstrtab_size, nstrtab_nextoff, fn_size;
|
1997-01-23 08:43:29 +03:00
|
|
|
int rv, i, weird;
|
1997-01-23 02:57:18 +03:00
|
|
|
|
1997-01-23 08:43:29 +03:00
|
|
|
rv = 0;
|
|
|
|
if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
shdrsize = ehdr.e_shnum * ehdr.e_shentsize;
|
|
|
|
if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
|
|
|
|
goto bad;
|
|
|
|
if (xreadatoff(fd, shdrp, ehdr.e_shoff, shdrsize, fn) != shdrsize)
|
|
|
|
goto bad;
|
|
|
|
|
1998-11-03 05:30:16 +03:00
|
|
|
symtabsnum = strtabsnum = -1;
|
|
|
|
maxoff = stroff = 0;
|
1997-01-23 08:43:29 +03:00
|
|
|
weird = 0;
|
1998-11-03 05:30:16 +03:00
|
|
|
weirdreason = "???";
|
1997-01-23 08:43:29 +03:00
|
|
|
for (i = 0; i < ehdr.e_shnum; i++) {
|
1998-11-03 05:30:16 +03:00
|
|
|
if (shdrp[i].sh_offset > maxoff) {
|
|
|
|
maxoff = shdrp[i].sh_offset;
|
|
|
|
}
|
1997-01-23 08:43:29 +03:00
|
|
|
switch (shdrp[i].sh_type) {
|
1999-10-25 17:55:06 +04:00
|
|
|
case SHT_SYMTAB:
|
1998-11-03 05:30:16 +03:00
|
|
|
if (!weird && symtabsnum != -1) {
|
1997-01-23 08:43:29 +03:00
|
|
|
weird = 1;
|
1998-11-03 05:30:16 +03:00
|
|
|
weirdreason = "multiple symbol tables";
|
|
|
|
}
|
|
|
|
symtabsnum = i;
|
|
|
|
strtabsnum = shdrp[i].sh_link;
|
|
|
|
stroff = shdrp[strtabsnum].sh_offset;
|
|
|
|
if (!weird && strtabsnum != (ehdr.e_shnum - 1)) {
|
|
|
|
weird = 1;
|
|
|
|
weirdreason = "string table not last section";
|
|
|
|
}
|
1997-01-23 08:43:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1998-11-03 05:30:16 +03:00
|
|
|
if (symtabsnum == -1)
|
1997-01-23 08:43:29 +03:00
|
|
|
goto out;
|
1998-11-03 05:30:16 +03:00
|
|
|
if (!weird && strtabsnum == -1) {
|
1997-01-23 08:43:29 +03:00
|
|
|
weird = 1;
|
1998-11-03 05:30:16 +03:00
|
|
|
weirdreason = "no string table found";
|
|
|
|
}
|
|
|
|
if (!weird && stroff != maxoff) {
|
|
|
|
weird = 1;
|
|
|
|
weirdreason = "string table section not last in file";
|
|
|
|
}
|
1997-01-23 08:43:29 +03:00
|
|
|
if (weird) {
|
1998-11-03 05:30:16 +03:00
|
|
|
fprintf(stderr, "%s: weird executable (%s); unsupported\n", fn,
|
|
|
|
weirdreason);
|
1997-01-23 08:43:29 +03:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* load up everything we need
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* symbol table */
|
1998-11-03 05:30:16 +03:00
|
|
|
if ((symtabp = xmalloc(shdrp[symtabsnum].sh_size, fn, "symbol table"))
|
1997-01-23 08:43:29 +03:00
|
|
|
== NULL)
|
|
|
|
goto bad;
|
1998-11-03 05:30:16 +03:00
|
|
|
if (xreadatoff(fd, symtabp, shdrp[symtabsnum].sh_offset,
|
|
|
|
shdrp[symtabsnum].sh_size, fn) != shdrp[symtabsnum].sh_size)
|
1997-01-23 08:43:29 +03:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
/* string table */
|
1998-11-03 05:30:16 +03:00
|
|
|
if ((strtabp = xmalloc(shdrp[strtabsnum].sh_size, fn, "string table"))
|
1997-01-23 08:43:29 +03:00
|
|
|
== NULL)
|
|
|
|
goto bad;
|
1998-11-03 05:30:16 +03:00
|
|
|
if (xreadatoff(fd, strtabp, shdrp[strtabsnum].sh_offset,
|
|
|
|
shdrp[strtabsnum].sh_size, fn) != shdrp[strtabsnum].sh_size)
|
1997-01-23 08:43:29 +03:00
|
|
|
goto bad;
|
|
|
|
|
1998-11-03 05:30:16 +03:00
|
|
|
nsyms = shdrp[symtabsnum].sh_size / shdrp[symtabsnum].sh_entsize;
|
1997-01-23 08:43:29 +03:00
|
|
|
|
1998-11-03 05:30:16 +03:00
|
|
|
nstrtab_size = 256;
|
|
|
|
nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
|
|
|
|
if (nstrtabp == NULL)
|
1997-01-23 08:43:29 +03:00
|
|
|
goto bad;
|
1998-11-03 05:30:16 +03:00
|
|
|
nstrtab_nextoff = 0;
|
1997-01-23 08:43:29 +03:00
|
|
|
|
1998-11-03 05:30:16 +03:00
|
|
|
fn_size = strlen(fn);
|
1997-01-23 08:43:29 +03:00
|
|
|
|
1998-11-03 05:30:16 +03:00
|
|
|
for (i = 0; i < nsyms; i++) {
|
|
|
|
Elf_Sym *sp = &symtabp[i];
|
|
|
|
const char *symname = strtabp + sp->st_name;
|
|
|
|
size_t newent_len;
|
1997-01-23 08:43:29 +03:00
|
|
|
|
|
|
|
/*
|
1998-11-03 05:30:16 +03:00
|
|
|
* make sure there's size for the next entry, even if it's
|
|
|
|
* as large as it can be.
|
|
|
|
*
|
|
|
|
* "_$$hide$$ <filename> <symname><NUL>" ->
|
|
|
|
* 9 + 3 + sizes of fn and sym name
|
1997-01-23 08:43:29 +03:00
|
|
|
*/
|
1998-11-03 05:30:16 +03:00
|
|
|
while ((nstrtab_size - nstrtab_nextoff) <
|
|
|
|
strlen(symname) + fn_size + 12) {
|
|
|
|
nstrtab_size *= 2;
|
|
|
|
nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
|
|
|
|
"new string table");
|
|
|
|
if (nstrtabp == NULL)
|
|
|
|
goto bad;
|
1997-01-23 08:43:29 +03:00
|
|
|
}
|
|
|
|
|
1998-11-03 05:30:16 +03:00
|
|
|
sp->st_name = nstrtab_nextoff;
|
|
|
|
|
|
|
|
/* if it's a keeper or is undefined, don't rename it. */
|
|
|
|
if (in_keep_list(symname) ||
|
1999-10-25 17:55:06 +04:00
|
|
|
sp->st_shndx == SHN_UNDEF) {
|
1998-11-03 05:30:16 +03:00
|
|
|
newent_len = sprintf(nstrtabp + nstrtab_nextoff,
|
|
|
|
"%s", symname) + 1;
|
|
|
|
} else {
|
|
|
|
newent_len = sprintf(nstrtabp + nstrtab_nextoff,
|
|
|
|
"_$$hide$$ %s %s", fn, symname) + 1;
|
1997-01-23 08:43:29 +03:00
|
|
|
}
|
1998-11-03 05:30:16 +03:00
|
|
|
nstrtab_nextoff += newent_len;
|
|
|
|
}
|
|
|
|
shdrp[strtabsnum].sh_size = nstrtab_nextoff;
|
1997-01-23 08:43:29 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* write new tables to the file
|
|
|
|
*/
|
|
|
|
if (xwriteatoff(fd, shdrp, ehdr.e_shoff, shdrsize, fn) != shdrsize)
|
|
|
|
goto bad;
|
1998-11-03 05:30:16 +03:00
|
|
|
if (xwriteatoff(fd, symtabp, shdrp[symtabsnum].sh_offset,
|
|
|
|
shdrp[symtabsnum].sh_size, fn) != shdrp[symtabsnum].sh_size)
|
|
|
|
goto bad;
|
|
|
|
if (xwriteatoff(fd, nstrtabp, shdrp[strtabsnum].sh_offset,
|
|
|
|
shdrp[strtabsnum].sh_size, fn) != shdrp[strtabsnum].sh_size)
|
1997-01-23 08:43:29 +03:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (shdrp != NULL)
|
|
|
|
free(shdrp);
|
|
|
|
if (symtabp != NULL)
|
|
|
|
free(symtabp);
|
|
|
|
if (strtabp != NULL)
|
|
|
|
free(strtabp);
|
1998-11-03 05:30:16 +03:00
|
|
|
if (nstrtabp != NULL)
|
1999-09-20 08:12:16 +04:00
|
|
|
free(nstrtabp);
|
1997-01-23 08:43:29 +03:00
|
|
|
return (rv);
|
|
|
|
|
|
|
|
bad:
|
|
|
|
rv = 1;
|
|
|
|
goto out;
|
1997-01-23 02:57:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* include this size of ELF */
|