#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: exec_aout.c,v 1.12 2009/08/20 17:40:26 he Exp $");
#endif
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "extern.h"
#if defined(NLIST_AOUT)
#include <a.out.h>
#include <sys/exec_aout.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])
#define IS_GLOBAL_DEFINED(sp) \
(((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
#ifdef __sparc__
#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
#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;
if(fstat(inf, &infstat) == -1)
return 0;
if(infstat.st_size < (ssize_t)sizeof eh)
return 0;
if(read(inf, &eh, sizeof eh) != sizeof eh)
return 0;
if(N_BADMAG(eh))
return 0;
return 1;
}
int hide_aout(int inf, const char *filename)
{
struct stat infstat;
struct relocation_info *relp;
struct nlist *symp;
int rc;
if(fstat(inf, &infstat) == -1) {
perror(filename);
return 1;
}
if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
fprintf(stderr, "%s: too big to read into memory\n", filename);
return 1;
}
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 1;
}
hdrp = (struct exec *) aoutdata;
#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);
for(symp = symbase; symp < symbase + nsyms; symp++) {
if(!IS_GLOBAL_DEFINED(symp))
continue;
if(SYMSTR(symp)[0] == '_' && in_keep_list(SYMSTR(symp) + 1))
continue;
symp->n_type = 0;
}
for(relp = textrel; relp < textrel + ntextrel; relp++)
check_reloc(filename, relp);
for(relp = datarel; relp < datarel + ndatarel; relp++)
check_reloc(filename, relp);
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");
return 1;
}
return 0;
}
static void check_reloc(const char *filename, struct relocation_info *relp)
{
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