split this up so that support for other executable formats can be more
easily added.
This commit is contained in:
parent
c794772836
commit
5247fa0ce9
|
@ -1,4 +1,5 @@
|
|||
|
||||
PROG= crunchide
|
||||
SRCS= crunchide.c exec_aout.c
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
|
||||
* Copyright (c) 1994 University of Maryland
|
||||
* All Rights Reserved.
|
||||
*
|
||||
|
@ -73,18 +74,19 @@ void usage(void);
|
|||
void add_to_keep_list(char *symbol);
|
||||
void add_file_to_keep_list(char *filename);
|
||||
|
||||
void hide_syms(char *filename);
|
||||
int hide_syms(const char *filename);
|
||||
|
||||
int verbose;
|
||||
|
||||
int main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
int ch;
|
||||
int ch, errors;
|
||||
|
||||
if(argc > 0) pname = argv[0];
|
||||
|
||||
while ((ch = getopt(argc, argv, "k:f:")) != EOF)
|
||||
while ((ch = getopt(argc, argv, "k:f:v")) != EOF)
|
||||
switch(ch) {
|
||||
case 'k':
|
||||
add_to_keep_list(optarg);
|
||||
|
@ -92,6 +94,9 @@ char **argv;
|
|||
case 'f':
|
||||
add_file_to_keep_list(optarg);
|
||||
break;
|
||||
case 'v':
|
||||
verbose = 1;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
|
@ -101,12 +106,14 @@ char **argv;
|
|||
|
||||
if(argc == 0) usage();
|
||||
|
||||
errors = 0;
|
||||
while(argc) {
|
||||
hide_syms(*argv);
|
||||
if (hide_syms(*argv))
|
||||
errors = 1;
|
||||
argc--, argv++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return errors;
|
||||
}
|
||||
|
||||
void usage(void)
|
||||
|
@ -147,7 +154,7 @@ void add_to_keep_list(char *symbol)
|
|||
else keep_list = newp;
|
||||
}
|
||||
|
||||
int in_keep_list(char *symbol)
|
||||
int in_keep_list(const char *symbol)
|
||||
{
|
||||
struct keep *curp;
|
||||
int cmp;
|
||||
|
@ -179,151 +186,65 @@ void add_file_to_keep_list(char *filename)
|
|||
fclose(keepf);
|
||||
}
|
||||
|
||||
/* ---------------------- */
|
||||
/* ---------------------------- */
|
||||
|
||||
int nsyms, ntextrel, ndatarel;
|
||||
struct exec *hdrp;
|
||||
char *aoutdata, *strbase;
|
||||
struct relocation_info *textrel, *datarel;
|
||||
struct nlist *symbase;
|
||||
|
||||
|
||||
#define SYMSTR(sp) &strbase[(sp)->n_un.n_strx]
|
||||
|
||||
/* is the symbol a global symbol defined in the current file? */
|
||||
#define IS_GLOBAL_DEFINED(sp) \
|
||||
(((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
|
||||
|
||||
#ifdef __sparc
|
||||
/* is the relocation entry dependent on a symbol? */
|
||||
#define IS_SYMBOL_RELOC(rp) \
|
||||
((rp)->r_extern || \
|
||||
((rp)->r_type >= RELOC_BASE10 && (rp)->r_type <= RELOC_BASE22) || \
|
||||
(rp)->r_type == RELOC_JMP_TBL)
|
||||
#else
|
||||
/* is the relocation entry dependent on a symbol? */
|
||||
#define IS_SYMBOL_RELOC(rp) \
|
||||
((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable)
|
||||
struct {
|
||||
const char *name;
|
||||
int (*check)(int, const char *); /* 1 if match, zero if not */
|
||||
int (*hide)(int, const char *); /* non-zero if error */
|
||||
} exec_formats[] = {
|
||||
#ifdef NLIST_AOUT
|
||||
{ "a.out", check_aout, hide_aout, },
|
||||
#endif
|
||||
|
||||
void check_reloc(char *filename, struct relocation_info *relp);
|
||||
|
||||
void hide_syms(char *filename)
|
||||
{
|
||||
int inf, outf, rc;
|
||||
struct stat infstat;
|
||||
struct relocation_info *relp;
|
||||
struct nlist *symp;
|
||||
|
||||
/*
|
||||
* Open the file and do some error checking.
|
||||
*/
|
||||
|
||||
if((inf = open(filename, O_RDWR)) == -1) {
|
||||
perror(filename);
|
||||
return;
|
||||
}
|
||||
|
||||
if(fstat(inf, &infstat) == -1) {
|
||||
perror(filename);
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
if(infstat.st_size < sizeof(struct exec)) {
|
||||
fprintf(stderr, "%s: short file\n", filename);
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the entire file into memory. XXX - Really, we only need to
|
||||
* read the header and from TRELOFF to the end of the file.
|
||||
*/
|
||||
|
||||
if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
|
||||
fprintf(stderr, "%s: too big to read into memory\n", filename);
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
if((rc = read(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
|
||||
fprintf(stderr, "%s: read error: %s\n", filename,
|
||||
rc == -1? strerror(errno) : "short read");
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the header and calculate offsets and sizes from it.
|
||||
*/
|
||||
|
||||
hdrp = (struct exec *) aoutdata;
|
||||
|
||||
if(N_BADMAG(*hdrp)) {
|
||||
fprintf(stderr, "%s: bad magic: not an a.out file\n", filename);
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
textrel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp));
|
||||
datarel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp) +
|
||||
hdrp->a_trsize);
|
||||
#else
|
||||
textrel = (struct relocation_info *) (aoutdata + N_TRELOFF(*hdrp));
|
||||
datarel = (struct relocation_info *) (aoutdata + N_DRELOFF(*hdrp));
|
||||
#ifdef NLIST_ELF32
|
||||
{ "ELF32", check_elf32, hide_elf32, },
|
||||
#endif
|
||||
symbase = (struct nlist *) (aoutdata + N_SYMOFF(*hdrp));
|
||||
strbase = (char *) (aoutdata + N_STROFF(*hdrp));
|
||||
#ifdef NLIST_ELF64
|
||||
{ "ELF64", check_elf64, hide_elf64, },
|
||||
#endif
|
||||
};
|
||||
|
||||
ntextrel = hdrp->a_trsize / sizeof(struct relocation_info);
|
||||
ndatarel = hdrp->a_drsize / sizeof(struct relocation_info);
|
||||
nsyms = hdrp->a_syms / sizeof(struct nlist);
|
||||
|
||||
/*
|
||||
* Zap the type field of all globally-defined symbols. The linker will
|
||||
* subsequently ignore these entries. Don't zap any symbols in the
|
||||
* keep list.
|
||||
*/
|
||||
|
||||
for(symp = symbase; symp < symbase + nsyms; symp++)
|
||||
if(IS_GLOBAL_DEFINED(symp) && !in_keep_list(SYMSTR(symp)))
|
||||
symp->n_type = 0;
|
||||
|
||||
/*
|
||||
* Check whether the relocation entries reference any symbols that we
|
||||
* just zapped. I don't know whether ld can handle this case, but I
|
||||
* haven't encountered it yet. These checks are here so that the program
|
||||
* doesn't fail silently should such symbols be encountered.
|
||||
*/
|
||||
|
||||
for(relp = textrel; relp < textrel + ntextrel; relp++)
|
||||
check_reloc(filename, relp);
|
||||
for(relp = datarel; relp < datarel + ndatarel; relp++)
|
||||
check_reloc(filename, relp);
|
||||
|
||||
/*
|
||||
* Write the .o file back out to disk. XXX - Really, we only need to
|
||||
* write the symbol table entries back out.
|
||||
*/
|
||||
lseek(inf, 0, SEEK_SET);
|
||||
if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
|
||||
fprintf(stderr, "%s: write error: %s\n", filename,
|
||||
rc == -1? strerror(errno) : "short write");
|
||||
}
|
||||
|
||||
close(inf);
|
||||
}
|
||||
|
||||
|
||||
void check_reloc(char *filename, struct relocation_info *relp)
|
||||
int hide_syms(const char *filename)
|
||||
{
|
||||
/* bail out if we zapped a symbol that is needed */
|
||||
if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
|
||||
fprintf(stderr,
|
||||
"%s: oops, have hanging relocation for %s: bailing out!\n",
|
||||
filename, SYMSTR(&symbase[relp->r_symbolnum]));
|
||||
exit(1);
|
||||
}
|
||||
int fd, i, n, rv;
|
||||
|
||||
fd = open(filename, O_RDWR, 0);
|
||||
if (fd == -1) {
|
||||
perror(filename);
|
||||
return 1;
|
||||
}
|
||||
|
||||
rv = 0;
|
||||
|
||||
n = sizeof exec_formats / sizeof exec_formats[0];
|
||||
for (i = 0; i < n; i++) {
|
||||
if (lseek(fd, 0, SEEK_SET) != 0) {
|
||||
perror(filename);
|
||||
goto err;
|
||||
}
|
||||
if ((*exec_formats[i].check)(fd, filename) != 0)
|
||||
break;
|
||||
}
|
||||
if (i == n) {
|
||||
fprintf(stderr, "%s: unknown executable format", filename);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "%s is an %s binary\n", filename,
|
||||
exec_formats[i].name);
|
||||
|
||||
if (lseek(fd, 0, SEEK_SET) != 0) {
|
||||
perror(filename);
|
||||
goto err;
|
||||
}
|
||||
rv = (*exec_formats[i].hide)(fd, filename);
|
||||
|
||||
out:
|
||||
close (fd);
|
||||
return (rv);
|
||||
|
||||
err:
|
||||
rv = 1;
|
||||
goto out;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,193 @@
|
|||
/*
|
||||
* Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
|
||||
* Copyright (c) 1994 University of Maryland
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Permission to use, copy, modify, distribute, and sell this software and its
|
||||
* documentation for any purpose is hereby granted without fee, provided that
|
||||
* the above copyright notice appear in all copies and that both that
|
||||
* copyright notice and this permission notice appear in supporting
|
||||
* documentation, and that the name of U.M. not be used in advertising or
|
||||
* publicity pertaining to distribution of the software without specific,
|
||||
* written prior permission. U.M. makes no representations about the
|
||||
* suitability of this software for any purpose. It is provided "as is"
|
||||
* without express or implied warranty.
|
||||
*
|
||||
* U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
|
||||
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* Author: James da Silva, Systems Design and Analysis Group
|
||||
* Computer Science Department
|
||||
* University of Maryland at College Park
|
||||
*/
|
||||
|
||||
#if defined(NLIST_AOUT)
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <a.out.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/errno.h>
|
||||
|
||||
#include "extern.h"
|
||||
|
||||
int nsyms, ntextrel, ndatarel;
|
||||
struct exec *hdrp;
|
||||
char *aoutdata, *strbase;
|
||||
struct relocation_info *textrel, *datarel;
|
||||
struct nlist *symbase;
|
||||
|
||||
|
||||
#define SYMSTR(sp) &strbase[(sp)->n_un.n_strx]
|
||||
|
||||
/* is the symbol a global symbol defined in the current file? */
|
||||
#define IS_GLOBAL_DEFINED(sp) \
|
||||
(((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
|
||||
|
||||
#ifdef __sparc
|
||||
/* is the relocation entry dependent on a symbol? */
|
||||
#define IS_SYMBOL_RELOC(rp) \
|
||||
((rp)->r_extern || \
|
||||
((rp)->r_type >= RELOC_BASE10 && (rp)->r_type <= RELOC_BASE22) || \
|
||||
(rp)->r_type == RELOC_JMP_TBL)
|
||||
#else
|
||||
/* is the relocation entry dependent on a symbol? */
|
||||
#define IS_SYMBOL_RELOC(rp) \
|
||||
((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable)
|
||||
#endif
|
||||
|
||||
static void check_reloc(const char *filename, struct relocation_info *relp);
|
||||
|
||||
int check_aout(int inf, const char *filename)
|
||||
{
|
||||
struct stat infstat;
|
||||
struct exec eh;
|
||||
|
||||
/*
|
||||
* check the header to make sure it's an a.out-format file.
|
||||
*/
|
||||
|
||||
if(fstat(inf, &infstat) == -1)
|
||||
return 0;
|
||||
if(infstat.st_size < sizeof eh)
|
||||
return 0;
|
||||
if(read(inf, &eh, sizeof eh) < sizeof eh)
|
||||
return 0;
|
||||
|
||||
if(N_BADMAG(*hdrp))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void hide_aout(int inf, const char *filename)
|
||||
{
|
||||
struct stat infstat;
|
||||
struct relocation_info *relp;
|
||||
struct nlist *symp;
|
||||
int rc;
|
||||
|
||||
/*
|
||||
* do some error checking.
|
||||
*/
|
||||
|
||||
if(fstat(inf, &infstat) == -1) {
|
||||
perror(filename);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the entire file into memory. XXX - Really, we only need to
|
||||
* read the header and from TRELOFF to the end of the file.
|
||||
*/
|
||||
|
||||
if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
|
||||
fprintf(stderr, "%s: too big to read into memory\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
if((rc = read(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
|
||||
fprintf(stderr, "%s: read error: %s\n", filename,
|
||||
rc == -1? strerror(errno) : "short read");
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the header and calculate offsets and sizes from it.
|
||||
*/
|
||||
|
||||
hdrp = (struct exec *) aoutdata;
|
||||
|
||||
if(N_BADMAG(*hdrp)) {
|
||||
fprintf(stderr, "%s: bad magic: not an a.out file\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
textrel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp));
|
||||
datarel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp) +
|
||||
hdrp->a_trsize);
|
||||
#else
|
||||
textrel = (struct relocation_info *) (aoutdata + N_TRELOFF(*hdrp));
|
||||
datarel = (struct relocation_info *) (aoutdata + N_DRELOFF(*hdrp));
|
||||
#endif
|
||||
symbase = (struct nlist *) (aoutdata + N_SYMOFF(*hdrp));
|
||||
strbase = (char *) (aoutdata + N_STROFF(*hdrp));
|
||||
|
||||
ntextrel = hdrp->a_trsize / sizeof(struct relocation_info);
|
||||
ndatarel = hdrp->a_drsize / sizeof(struct relocation_info);
|
||||
nsyms = hdrp->a_syms / sizeof(struct nlist);
|
||||
|
||||
/*
|
||||
* Zap the type field of all globally-defined symbols. The linker will
|
||||
* subsequently ignore these entries. Don't zap any symbols in the
|
||||
* keep list.
|
||||
*/
|
||||
|
||||
for(symp = symbase; symp < symbase + nsyms; symp++)
|
||||
if(IS_GLOBAL_DEFINED(symp) && !in_keep_list(SYMSTR(symp)))
|
||||
symp->n_type = 0;
|
||||
|
||||
/*
|
||||
* Check whether the relocation entries reference any symbols that we
|
||||
* just zapped. I don't know whether ld can handle this case, but I
|
||||
* haven't encountered it yet. These checks are here so that the program
|
||||
* doesn't fail silently should such symbols be encountered.
|
||||
*/
|
||||
|
||||
for(relp = textrel; relp < textrel + ntextrel; relp++)
|
||||
check_reloc(filename, relp);
|
||||
for(relp = datarel; relp < datarel + ndatarel; relp++)
|
||||
check_reloc(filename, relp);
|
||||
|
||||
/*
|
||||
* Write the .o file back out to disk. XXX - Really, we only need to
|
||||
* write the symbol table entries back out.
|
||||
*/
|
||||
lseek(inf, 0, SEEK_SET);
|
||||
if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
|
||||
fprintf(stderr, "%s: write error: %s\n", filename,
|
||||
rc == -1? strerror(errno) : "short write");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void check_reloc(const char *filename, struct relocation_info *relp)
|
||||
{
|
||||
/* bail out if we zapped a symbol that is needed */
|
||||
if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
|
||||
fprintf(stderr,
|
||||
"%s: oops, have hanging relocation for %s: bailing out!\n",
|
||||
filename, SYMSTR(&symbase[relp->r_symbolnum]));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* defined(NLIST_AOUT) */
|
|
@ -0,0 +1,58 @@
|
|||
/* $NetBSD: extern.h,v 1.1 1997/01/22 22:49:06 cgd Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
|
||||
*
|
||||
* 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:
|
||||
* This product includes software developed by Christopher G. Demetriou
|
||||
* for the NetBSD Project.
|
||||
* 4. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* ECOFF will likely never be supported, because of its wacky way of
|
||||
* storing symbols and strings.
|
||||
*/
|
||||
#ifdef __alpha__
|
||||
#define NLIST_ELF64
|
||||
#else
|
||||
#define NLIST_AOUT
|
||||
/* #define NLIST_ELF32 */
|
||||
/* #define NLIST_ELF64 */
|
||||
#endif
|
||||
|
||||
#ifdef NLIST_AOUT
|
||||
int check_aout(int, const char *);
|
||||
void hide_aout(int, const char *);
|
||||
#endif
|
||||
#ifdef NLIST_ELF32
|
||||
int check_elf32(int, const char *);
|
||||
void hide_elf32(int, const char *);
|
||||
#endif
|
||||
#ifdef NLIST_ELF64
|
||||
int check_elf64(int, const char *);
|
||||
void hide_elf64(int, const char *);
|
||||
#endif
|
||||
|
||||
int in_keep_list(const char *symbol);
|
|
@ -1,4 +1,5 @@
|
|||
|
||||
PROG= crunchide
|
||||
SRCS= crunchide.c exec_aout.c
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
|
||||
* Copyright (c) 1994 University of Maryland
|
||||
* All Rights Reserved.
|
||||
*
|
||||
|
@ -73,18 +74,19 @@ void usage(void);
|
|||
void add_to_keep_list(char *symbol);
|
||||
void add_file_to_keep_list(char *filename);
|
||||
|
||||
void hide_syms(char *filename);
|
||||
int hide_syms(const char *filename);
|
||||
|
||||
int verbose;
|
||||
|
||||
int main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
int ch;
|
||||
int ch, errors;
|
||||
|
||||
if(argc > 0) pname = argv[0];
|
||||
|
||||
while ((ch = getopt(argc, argv, "k:f:")) != EOF)
|
||||
while ((ch = getopt(argc, argv, "k:f:v")) != EOF)
|
||||
switch(ch) {
|
||||
case 'k':
|
||||
add_to_keep_list(optarg);
|
||||
|
@ -92,6 +94,9 @@ char **argv;
|
|||
case 'f':
|
||||
add_file_to_keep_list(optarg);
|
||||
break;
|
||||
case 'v':
|
||||
verbose = 1;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
|
@ -101,12 +106,14 @@ char **argv;
|
|||
|
||||
if(argc == 0) usage();
|
||||
|
||||
errors = 0;
|
||||
while(argc) {
|
||||
hide_syms(*argv);
|
||||
if (hide_syms(*argv))
|
||||
errors = 1;
|
||||
argc--, argv++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return errors;
|
||||
}
|
||||
|
||||
void usage(void)
|
||||
|
@ -147,7 +154,7 @@ void add_to_keep_list(char *symbol)
|
|||
else keep_list = newp;
|
||||
}
|
||||
|
||||
int in_keep_list(char *symbol)
|
||||
int in_keep_list(const char *symbol)
|
||||
{
|
||||
struct keep *curp;
|
||||
int cmp;
|
||||
|
@ -179,151 +186,65 @@ void add_file_to_keep_list(char *filename)
|
|||
fclose(keepf);
|
||||
}
|
||||
|
||||
/* ---------------------- */
|
||||
/* ---------------------------- */
|
||||
|
||||
int nsyms, ntextrel, ndatarel;
|
||||
struct exec *hdrp;
|
||||
char *aoutdata, *strbase;
|
||||
struct relocation_info *textrel, *datarel;
|
||||
struct nlist *symbase;
|
||||
|
||||
|
||||
#define SYMSTR(sp) &strbase[(sp)->n_un.n_strx]
|
||||
|
||||
/* is the symbol a global symbol defined in the current file? */
|
||||
#define IS_GLOBAL_DEFINED(sp) \
|
||||
(((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
|
||||
|
||||
#ifdef __sparc
|
||||
/* is the relocation entry dependent on a symbol? */
|
||||
#define IS_SYMBOL_RELOC(rp) \
|
||||
((rp)->r_extern || \
|
||||
((rp)->r_type >= RELOC_BASE10 && (rp)->r_type <= RELOC_BASE22) || \
|
||||
(rp)->r_type == RELOC_JMP_TBL)
|
||||
#else
|
||||
/* is the relocation entry dependent on a symbol? */
|
||||
#define IS_SYMBOL_RELOC(rp) \
|
||||
((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable)
|
||||
struct {
|
||||
const char *name;
|
||||
int (*check)(int, const char *); /* 1 if match, zero if not */
|
||||
int (*hide)(int, const char *); /* non-zero if error */
|
||||
} exec_formats[] = {
|
||||
#ifdef NLIST_AOUT
|
||||
{ "a.out", check_aout, hide_aout, },
|
||||
#endif
|
||||
|
||||
void check_reloc(char *filename, struct relocation_info *relp);
|
||||
|
||||
void hide_syms(char *filename)
|
||||
{
|
||||
int inf, outf, rc;
|
||||
struct stat infstat;
|
||||
struct relocation_info *relp;
|
||||
struct nlist *symp;
|
||||
|
||||
/*
|
||||
* Open the file and do some error checking.
|
||||
*/
|
||||
|
||||
if((inf = open(filename, O_RDWR)) == -1) {
|
||||
perror(filename);
|
||||
return;
|
||||
}
|
||||
|
||||
if(fstat(inf, &infstat) == -1) {
|
||||
perror(filename);
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
if(infstat.st_size < sizeof(struct exec)) {
|
||||
fprintf(stderr, "%s: short file\n", filename);
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the entire file into memory. XXX - Really, we only need to
|
||||
* read the header and from TRELOFF to the end of the file.
|
||||
*/
|
||||
|
||||
if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
|
||||
fprintf(stderr, "%s: too big to read into memory\n", filename);
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
if((rc = read(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
|
||||
fprintf(stderr, "%s: read error: %s\n", filename,
|
||||
rc == -1? strerror(errno) : "short read");
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the header and calculate offsets and sizes from it.
|
||||
*/
|
||||
|
||||
hdrp = (struct exec *) aoutdata;
|
||||
|
||||
if(N_BADMAG(*hdrp)) {
|
||||
fprintf(stderr, "%s: bad magic: not an a.out file\n", filename);
|
||||
close(inf);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
textrel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp));
|
||||
datarel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp) +
|
||||
hdrp->a_trsize);
|
||||
#else
|
||||
textrel = (struct relocation_info *) (aoutdata + N_TRELOFF(*hdrp));
|
||||
datarel = (struct relocation_info *) (aoutdata + N_DRELOFF(*hdrp));
|
||||
#ifdef NLIST_ELF32
|
||||
{ "ELF32", check_elf32, hide_elf32, },
|
||||
#endif
|
||||
symbase = (struct nlist *) (aoutdata + N_SYMOFF(*hdrp));
|
||||
strbase = (char *) (aoutdata + N_STROFF(*hdrp));
|
||||
#ifdef NLIST_ELF64
|
||||
{ "ELF64", check_elf64, hide_elf64, },
|
||||
#endif
|
||||
};
|
||||
|
||||
ntextrel = hdrp->a_trsize / sizeof(struct relocation_info);
|
||||
ndatarel = hdrp->a_drsize / sizeof(struct relocation_info);
|
||||
nsyms = hdrp->a_syms / sizeof(struct nlist);
|
||||
|
||||
/*
|
||||
* Zap the type field of all globally-defined symbols. The linker will
|
||||
* subsequently ignore these entries. Don't zap any symbols in the
|
||||
* keep list.
|
||||
*/
|
||||
|
||||
for(symp = symbase; symp < symbase + nsyms; symp++)
|
||||
if(IS_GLOBAL_DEFINED(symp) && !in_keep_list(SYMSTR(symp)))
|
||||
symp->n_type = 0;
|
||||
|
||||
/*
|
||||
* Check whether the relocation entries reference any symbols that we
|
||||
* just zapped. I don't know whether ld can handle this case, but I
|
||||
* haven't encountered it yet. These checks are here so that the program
|
||||
* doesn't fail silently should such symbols be encountered.
|
||||
*/
|
||||
|
||||
for(relp = textrel; relp < textrel + ntextrel; relp++)
|
||||
check_reloc(filename, relp);
|
||||
for(relp = datarel; relp < datarel + ndatarel; relp++)
|
||||
check_reloc(filename, relp);
|
||||
|
||||
/*
|
||||
* Write the .o file back out to disk. XXX - Really, we only need to
|
||||
* write the symbol table entries back out.
|
||||
*/
|
||||
lseek(inf, 0, SEEK_SET);
|
||||
if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
|
||||
fprintf(stderr, "%s: write error: %s\n", filename,
|
||||
rc == -1? strerror(errno) : "short write");
|
||||
}
|
||||
|
||||
close(inf);
|
||||
}
|
||||
|
||||
|
||||
void check_reloc(char *filename, struct relocation_info *relp)
|
||||
int hide_syms(const char *filename)
|
||||
{
|
||||
/* bail out if we zapped a symbol that is needed */
|
||||
if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
|
||||
fprintf(stderr,
|
||||
"%s: oops, have hanging relocation for %s: bailing out!\n",
|
||||
filename, SYMSTR(&symbase[relp->r_symbolnum]));
|
||||
exit(1);
|
||||
}
|
||||
int fd, i, n, rv;
|
||||
|
||||
fd = open(filename, O_RDWR, 0);
|
||||
if (fd == -1) {
|
||||
perror(filename);
|
||||
return 1;
|
||||
}
|
||||
|
||||
rv = 0;
|
||||
|
||||
n = sizeof exec_formats / sizeof exec_formats[0];
|
||||
for (i = 0; i < n; i++) {
|
||||
if (lseek(fd, 0, SEEK_SET) != 0) {
|
||||
perror(filename);
|
||||
goto err;
|
||||
}
|
||||
if ((*exec_formats[i].check)(fd, filename) != 0)
|
||||
break;
|
||||
}
|
||||
if (i == n) {
|
||||
fprintf(stderr, "%s: unknown executable format", filename);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "%s is an %s binary\n", filename,
|
||||
exec_formats[i].name);
|
||||
|
||||
if (lseek(fd, 0, SEEK_SET) != 0) {
|
||||
perror(filename);
|
||||
goto err;
|
||||
}
|
||||
rv = (*exec_formats[i].hide)(fd, filename);
|
||||
|
||||
out:
|
||||
close (fd);
|
||||
return (rv);
|
||||
|
||||
err:
|
||||
rv = 1;
|
||||
goto out;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,193 @@
|
|||
/*
|
||||
* Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
|
||||
* Copyright (c) 1994 University of Maryland
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Permission to use, copy, modify, distribute, and sell this software and its
|
||||
* documentation for any purpose is hereby granted without fee, provided that
|
||||
* the above copyright notice appear in all copies and that both that
|
||||
* copyright notice and this permission notice appear in supporting
|
||||
* documentation, and that the name of U.M. not be used in advertising or
|
||||
* publicity pertaining to distribution of the software without specific,
|
||||
* written prior permission. U.M. makes no representations about the
|
||||
* suitability of this software for any purpose. It is provided "as is"
|
||||
* without express or implied warranty.
|
||||
*
|
||||
* U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
|
||||
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* Author: James da Silva, Systems Design and Analysis Group
|
||||
* Computer Science Department
|
||||
* University of Maryland at College Park
|
||||
*/
|
||||
|
||||
#if defined(NLIST_AOUT)
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <a.out.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/errno.h>
|
||||
|
||||
#include "extern.h"
|
||||
|
||||
int nsyms, ntextrel, ndatarel;
|
||||
struct exec *hdrp;
|
||||
char *aoutdata, *strbase;
|
||||
struct relocation_info *textrel, *datarel;
|
||||
struct nlist *symbase;
|
||||
|
||||
|
||||
#define SYMSTR(sp) &strbase[(sp)->n_un.n_strx]
|
||||
|
||||
/* is the symbol a global symbol defined in the current file? */
|
||||
#define IS_GLOBAL_DEFINED(sp) \
|
||||
(((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
|
||||
|
||||
#ifdef __sparc
|
||||
/* is the relocation entry dependent on a symbol? */
|
||||
#define IS_SYMBOL_RELOC(rp) \
|
||||
((rp)->r_extern || \
|
||||
((rp)->r_type >= RELOC_BASE10 && (rp)->r_type <= RELOC_BASE22) || \
|
||||
(rp)->r_type == RELOC_JMP_TBL)
|
||||
#else
|
||||
/* is the relocation entry dependent on a symbol? */
|
||||
#define IS_SYMBOL_RELOC(rp) \
|
||||
((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable)
|
||||
#endif
|
||||
|
||||
static void check_reloc(const char *filename, struct relocation_info *relp);
|
||||
|
||||
int check_aout(int inf, const char *filename)
|
||||
{
|
||||
struct stat infstat;
|
||||
struct exec eh;
|
||||
|
||||
/*
|
||||
* check the header to make sure it's an a.out-format file.
|
||||
*/
|
||||
|
||||
if(fstat(inf, &infstat) == -1)
|
||||
return 0;
|
||||
if(infstat.st_size < sizeof eh)
|
||||
return 0;
|
||||
if(read(inf, &eh, sizeof eh) < sizeof eh)
|
||||
return 0;
|
||||
|
||||
if(N_BADMAG(*hdrp))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void hide_aout(int inf, const char *filename)
|
||||
{
|
||||
struct stat infstat;
|
||||
struct relocation_info *relp;
|
||||
struct nlist *symp;
|
||||
int rc;
|
||||
|
||||
/*
|
||||
* do some error checking.
|
||||
*/
|
||||
|
||||
if(fstat(inf, &infstat) == -1) {
|
||||
perror(filename);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the entire file into memory. XXX - Really, we only need to
|
||||
* read the header and from TRELOFF to the end of the file.
|
||||
*/
|
||||
|
||||
if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
|
||||
fprintf(stderr, "%s: too big to read into memory\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
if((rc = read(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
|
||||
fprintf(stderr, "%s: read error: %s\n", filename,
|
||||
rc == -1? strerror(errno) : "short read");
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the header and calculate offsets and sizes from it.
|
||||
*/
|
||||
|
||||
hdrp = (struct exec *) aoutdata;
|
||||
|
||||
if(N_BADMAG(*hdrp)) {
|
||||
fprintf(stderr, "%s: bad magic: not an a.out file\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
textrel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp));
|
||||
datarel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp) +
|
||||
hdrp->a_trsize);
|
||||
#else
|
||||
textrel = (struct relocation_info *) (aoutdata + N_TRELOFF(*hdrp));
|
||||
datarel = (struct relocation_info *) (aoutdata + N_DRELOFF(*hdrp));
|
||||
#endif
|
||||
symbase = (struct nlist *) (aoutdata + N_SYMOFF(*hdrp));
|
||||
strbase = (char *) (aoutdata + N_STROFF(*hdrp));
|
||||
|
||||
ntextrel = hdrp->a_trsize / sizeof(struct relocation_info);
|
||||
ndatarel = hdrp->a_drsize / sizeof(struct relocation_info);
|
||||
nsyms = hdrp->a_syms / sizeof(struct nlist);
|
||||
|
||||
/*
|
||||
* Zap the type field of all globally-defined symbols. The linker will
|
||||
* subsequently ignore these entries. Don't zap any symbols in the
|
||||
* keep list.
|
||||
*/
|
||||
|
||||
for(symp = symbase; symp < symbase + nsyms; symp++)
|
||||
if(IS_GLOBAL_DEFINED(symp) && !in_keep_list(SYMSTR(symp)))
|
||||
symp->n_type = 0;
|
||||
|
||||
/*
|
||||
* Check whether the relocation entries reference any symbols that we
|
||||
* just zapped. I don't know whether ld can handle this case, but I
|
||||
* haven't encountered it yet. These checks are here so that the program
|
||||
* doesn't fail silently should such symbols be encountered.
|
||||
*/
|
||||
|
||||
for(relp = textrel; relp < textrel + ntextrel; relp++)
|
||||
check_reloc(filename, relp);
|
||||
for(relp = datarel; relp < datarel + ndatarel; relp++)
|
||||
check_reloc(filename, relp);
|
||||
|
||||
/*
|
||||
* Write the .o file back out to disk. XXX - Really, we only need to
|
||||
* write the symbol table entries back out.
|
||||
*/
|
||||
lseek(inf, 0, SEEK_SET);
|
||||
if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
|
||||
fprintf(stderr, "%s: write error: %s\n", filename,
|
||||
rc == -1? strerror(errno) : "short write");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void check_reloc(const char *filename, struct relocation_info *relp)
|
||||
{
|
||||
/* bail out if we zapped a symbol that is needed */
|
||||
if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
|
||||
fprintf(stderr,
|
||||
"%s: oops, have hanging relocation for %s: bailing out!\n",
|
||||
filename, SYMSTR(&symbase[relp->r_symbolnum]));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* defined(NLIST_AOUT) */
|
|
@ -0,0 +1,58 @@
|
|||
/* $NetBSD: extern.h,v 1.1 1997/01/22 22:49:06 cgd Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
|
||||
*
|
||||
* 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:
|
||||
* This product includes software developed by Christopher G. Demetriou
|
||||
* for the NetBSD Project.
|
||||
* 4. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* ECOFF will likely never be supported, because of its wacky way of
|
||||
* storing symbols and strings.
|
||||
*/
|
||||
#ifdef __alpha__
|
||||
#define NLIST_ELF64
|
||||
#else
|
||||
#define NLIST_AOUT
|
||||
/* #define NLIST_ELF32 */
|
||||
/* #define NLIST_ELF64 */
|
||||
#endif
|
||||
|
||||
#ifdef NLIST_AOUT
|
||||
int check_aout(int, const char *);
|
||||
void hide_aout(int, const char *);
|
||||
#endif
|
||||
#ifdef NLIST_ELF32
|
||||
int check_elf32(int, const char *);
|
||||
void hide_elf32(int, const char *);
|
||||
#endif
|
||||
#ifdef NLIST_ELF64
|
||||
int check_elf64(int, const char *);
|
||||
void hide_elf64(int, const char *);
|
||||
#endif
|
||||
|
||||
int in_keep_list(const char *symbol);
|
Loading…
Reference in New Issue