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