exec_aout.c revision 129652
1/* $NetBSD: exec_aout.c,v 1.6 1997/08/02 21:30:17 perry Exp $ */ 2/* 3 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved. 4 * Copyright (c) 1994 University of Maryland 5 * All Rights Reserved. 6 * 7 * Permission to use, copy, modify, distribute, and sell this software and its 8 * documentation for any purpose is hereby granted without fee, provided that 9 * the above copyright notice appear in all copies and that both that 10 * copyright notice and this permission notice appear in supporting 11 * documentation, and that the name of U.M. not be used in advertising or 12 * publicity pertaining to distribution of the software without specific, 13 * written prior permission. U.M. makes no representations about the 14 * suitability of this software for any purpose. It is provided "as is" 15 * without express or implied warranty. 16 * 17 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. 19 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 23 * 24 * Author: James da Silva, Systems Design and Analysis Group 25 * Computer Science Department 26 * University of Maryland at College Park 27 */ 28#include <sys/cdefs.h> 29#ifndef lint 30__RCSID("$NetBSD: exec_aout.c,v 1.6 1997/08/02 21:30:17 perry Exp $"); 31__FBSDID("$FreeBSD: head/usr.sbin/crunch/crunchide/exec_aout.c 129652 2004-05-24 11:59:17Z stefanf $"); 32#endif 33 34#include <unistd.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <string.h> 38#include <a.out.h> 39#include <sys/types.h> 40#include <sys/stat.h> 41#include <sys/errno.h> 42#include <netinet/in.h> 43 44#include "extern.h" 45 46#if defined(NLIST_AOUT) 47 48int nsyms, ntextrel, ndatarel; 49struct exec *hdrp; 50char *aoutdata, *strbase; 51struct relocation_info *textrel, *datarel; 52struct nlist *symbase; 53 54 55#define SYMSTR(sp) (&strbase[(sp)->n_un.n_strx]) 56 57/* is the symbol a global symbol defined in the current file? */ 58#define IS_GLOBAL_DEFINED(sp) \ 59 (((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF) 60 61/* is the relocation entry dependent on a symbol? */ 62#define IS_SYMBOL_RELOC(rp) \ 63 ((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable) 64 65static void check_reloc(const char *filename, struct relocation_info *relp); 66 67int check_aout(int inf, const char *filename) 68{ 69 struct stat infstat; 70 struct exec eh; 71 72 /* 73 * check the header to make sure it's an a.out-format file. 74 */ 75 76 if(fstat(inf, &infstat) == -1) 77 return 0; 78 if(infstat.st_size < sizeof eh) 79 return 0; 80 if(read(inf, &eh, sizeof eh) != sizeof eh) 81 return 0; 82 83 if(N_BADMAG(eh)) 84 return 0; 85 86 return 1; 87} 88 89int hide_aout(int inf, const char *filename) 90{ 91 struct stat infstat; 92 struct relocation_info *relp; 93 struct nlist *symp; 94 int rc; 95 96 /* 97 * do some error checking. 98 */ 99 100 if(fstat(inf, &infstat) == -1) { 101 perror(filename); 102 return 1; 103 } 104 105 /* 106 * Read the entire file into memory. XXX - Really, we only need to 107 * read the header and from TRELOFF to the end of the file. 108 */ 109 110 if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) { 111 fprintf(stderr, "%s: too big to read into memory\n", filename); 112 return 1; 113 } 114 115 if((rc = read(inf, aoutdata, infstat.st_size)) < infstat.st_size) { 116 fprintf(stderr, "%s: read error: %s\n", filename, 117 rc == -1? strerror(errno) : "short read"); 118 return 1; 119 } 120 121 /* 122 * Calculate offsets and sizes from the header. 123 */ 124 125 hdrp = (struct exec *) aoutdata; 126 127#ifdef __FreeBSD__ 128 textrel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp)); 129 datarel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp) + 130 hdrp->a_trsize); 131#else 132 textrel = (struct relocation_info *) (aoutdata + N_TRELOFF(*hdrp)); 133 datarel = (struct relocation_info *) (aoutdata + N_DRELOFF(*hdrp)); 134#endif 135 symbase = (struct nlist *) (aoutdata + N_SYMOFF(*hdrp)); 136 strbase = (char *) (aoutdata + N_STROFF(*hdrp)); 137 138 ntextrel = hdrp->a_trsize / sizeof(struct relocation_info); 139 ndatarel = hdrp->a_drsize / sizeof(struct relocation_info); 140 nsyms = hdrp->a_syms / sizeof(struct nlist); 141 142 /* 143 * Zap the type field of all globally-defined symbols. The linker will 144 * subsequently ignore these entries. Don't zap any symbols in the 145 * keep list. 146 */ 147 148 for(symp = symbase; symp < symbase + nsyms; symp++) { 149 if(!IS_GLOBAL_DEFINED(symp)) /* keep undefined syms */ 150 continue; 151 152 /* keep (C) symbols which are on the keep list */ 153 if(SYMSTR(symp)[0] == '_' && in_keep_list(SYMSTR(symp) + 1)) 154 continue; 155 156 symp->n_type = 0; 157 } 158 159 /* 160 * Check whether the relocation entries reference any symbols that we 161 * just zapped. I don't know whether ld can handle this case, but I 162 * haven't encountered it yet. These checks are here so that the program 163 * doesn't fail silently should such symbols be encountered. 164 */ 165 166 for(relp = textrel; relp < textrel + ntextrel; relp++) 167 check_reloc(filename, relp); 168 for(relp = datarel; relp < datarel + ndatarel; relp++) 169 check_reloc(filename, relp); 170 171 /* 172 * Write the .o file back out to disk. XXX - Really, we only need to 173 * write the symbol table entries back out. 174 */ 175 lseek(inf, 0, SEEK_SET); 176 if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) { 177 fprintf(stderr, "%s: write error: %s\n", filename, 178 rc == -1? strerror(errno) : "short write"); 179 return 1; 180 } 181 182 return 0; 183} 184 185 186static void check_reloc(const char *filename, struct relocation_info *relp) 187{ 188 /* bail out if we zapped a symbol that is needed */ 189 if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) { 190 fprintf(stderr, 191 "%s: oops, have hanging relocation for %s: bailing out!\n", 192 filename, SYMSTR(&symbase[relp->r_symbolnum])); 193 exit(1); 194 } 195} 196 197#endif /* defined(NLIST_AOUT) */ 198