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