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: releng/10.2/usr.sbin/crunch/crunchide/exec_aout.c 130927 2004-06-22 17:05:39Z obrien $");
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/endian.h>
41#include <sys/stat.h>
42#include <sys/errno.h>
43#include <netinet/in.h>
44
45#include "extern.h"
46
47#if defined(NLIST_AOUT)
48
49int nsyms, ntextrel, ndatarel;
50struct exec *hdrp;
51char *aoutdata, *strbase;
52struct relocation_info *textrel, *datarel;
53struct nlist *symbase;
54
55
56#define SYMSTR(sp)	(&strbase[(sp)->n_un.n_strx])
57
58/* is the symbol a global symbol defined in the current file? */
59#define IS_GLOBAL_DEFINED(sp) \
60                  (((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
61
62/* is the relocation entry dependent on a symbol? */
63#define IS_SYMBOL_RELOC(rp)   \
64                  ((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable)
65
66static void check_reloc(const char *filename, struct relocation_info *relp);
67
68int check_aout(int inf, const char *filename)
69{
70    struct stat infstat;
71    struct exec eh;
72
73    /*
74     * check the header to make sure it's an a.out-format file.
75     */
76
77    if(fstat(inf, &infstat) == -1)
78	return 0;
79    if(infstat.st_size < sizeof eh)
80	return 0;
81    if(read(inf, &eh, sizeof eh) != sizeof eh)
82	return 0;
83
84    if(N_BADMAG(eh))
85	return 0;
86
87    return 1;
88}
89
90int hide_aout(int inf, const char *filename)
91{
92    struct stat infstat;
93    struct relocation_info *relp;
94    struct nlist *symp;
95    int rc;
96
97    /*
98     * do some error checking.
99     */
100
101    if(fstat(inf, &infstat) == -1) {
102	perror(filename);
103	return 1;
104    }
105
106    /*
107     * Read the entire file into memory.  XXX - Really, we only need to
108     * read the header and from TRELOFF to the end of the file.
109     */
110
111    if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
112	fprintf(stderr, "%s: too big to read into memory\n", filename);
113	return 1;
114    }
115
116    if((rc = read(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
117	fprintf(stderr, "%s: read error: %s\n", filename,
118		rc == -1? strerror(errno) : "short read");
119	return 1;
120    }
121
122    /*
123     * Calculate offsets and sizes from the header.
124     */
125
126    hdrp = (struct exec *) aoutdata;
127
128#ifdef __FreeBSD__
129    textrel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp));
130    datarel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp) +
131					  hdrp->a_trsize);
132#else
133    textrel = (struct relocation_info *) (aoutdata + N_TRELOFF(*hdrp));
134    datarel = (struct relocation_info *) (aoutdata + N_DRELOFF(*hdrp));
135#endif
136    symbase = (struct nlist *)		 (aoutdata + N_SYMOFF(*hdrp));
137    strbase = (char *) 			 (aoutdata + N_STROFF(*hdrp));
138
139    ntextrel = hdrp->a_trsize / sizeof(struct relocation_info);
140    ndatarel = hdrp->a_drsize / sizeof(struct relocation_info);
141    nsyms    = hdrp->a_syms   / sizeof(struct nlist);
142
143    /*
144     * Zap the type field of all globally-defined symbols.  The linker will
145     * subsequently ignore these entries.  Don't zap any symbols in the
146     * keep list.
147     */
148
149    for(symp = symbase; symp < symbase + nsyms; symp++) {
150	if(!IS_GLOBAL_DEFINED(symp))		/* keep undefined syms */
151	    continue;
152
153	/* keep (C) symbols which are on the keep list */
154	if(SYMSTR(symp)[0] == '_' && in_keep_list(SYMSTR(symp) + 1))
155	    continue;
156
157	symp->n_type = 0;
158    }
159
160    /*
161     * Check whether the relocation entries reference any symbols that we
162     * just zapped.  I don't know whether ld can handle this case, but I
163     * haven't encountered it yet.  These checks are here so that the program
164     * doesn't fail silently should such symbols be encountered.
165     */
166
167    for(relp = textrel; relp < textrel + ntextrel; relp++)
168	check_reloc(filename, relp);
169    for(relp = datarel; relp < datarel + ndatarel; relp++)
170	check_reloc(filename, relp);
171
172    /*
173     * Write the .o file back out to disk.  XXX - Really, we only need to
174     * write the symbol table entries back out.
175     */
176    lseek(inf, 0, SEEK_SET);
177    if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
178	fprintf(stderr, "%s: write error: %s\n", filename,
179		rc == -1? strerror(errno) : "short write");
180	return 1;
181    }
182
183    return 0;
184}
185
186
187static void check_reloc(const char *filename, struct relocation_info *relp)
188{
189    /* bail out if we zapped a symbol that is needed */
190    if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
191	fprintf(stderr,
192		"%s: oops, have hanging relocation for %s: bailing out!\n",
193		filename, SYMSTR(&symbase[relp->r_symbolnum]));
194	exit(1);
195    }
196}
197
198#endif /* defined(NLIST_AOUT) */
199