exec_aout.c revision 1.1 1 /*
2 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
3 * Copyright (c) 1994 University of Maryland
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
15 *
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 *
23 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
26 */
27
28 #if defined(NLIST_AOUT)
29
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <a.out.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/errno.h>
38
39 #include "extern.h"
40
41 int nsyms, ntextrel, ndatarel;
42 struct exec *hdrp;
43 char *aoutdata, *strbase;
44 struct relocation_info *textrel, *datarel;
45 struct nlist *symbase;
46
47
48 #define SYMSTR(sp) &strbase[(sp)->n_un.n_strx]
49
50 /* is the symbol a global symbol defined in the current file? */
51 #define IS_GLOBAL_DEFINED(sp) \
52 (((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
53
54 #ifdef __sparc
55 /* is the relocation entry dependent on a symbol? */
56 #define IS_SYMBOL_RELOC(rp) \
57 ((rp)->r_extern || \
58 ((rp)->r_type >= RELOC_BASE10 && (rp)->r_type <= RELOC_BASE22) || \
59 (rp)->r_type == RELOC_JMP_TBL)
60 #else
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 #endif
65
66 static void check_reloc(const char *filename, struct relocation_info *relp);
67
68 int 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(*hdrp))
85 return 0;
86
87 return 1;
88 }
89
90 void 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;
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;
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;
120 }
121
122 /*
123 * Check the header and calculate offsets and sizes from it.
124 */
125
126 hdrp = (struct exec *) aoutdata;
127
128 if(N_BADMAG(*hdrp)) {
129 fprintf(stderr, "%s: bad magic: not an a.out file\n", filename);
130 return;
131 }
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) && !in_keep_list(SYMSTR(symp)))
156 symp->n_type = 0;
157
158 /*
159 * Check whether the relocation entries reference any symbols that we
160 * just zapped. I don't know whether ld can handle this case, but I
161 * haven't encountered it yet. These checks are here so that the program
162 * doesn't fail silently should such symbols be encountered.
163 */
164
165 for(relp = textrel; relp < textrel + ntextrel; relp++)
166 check_reloc(filename, relp);
167 for(relp = datarel; relp < datarel + ndatarel; relp++)
168 check_reloc(filename, relp);
169
170 /*
171 * Write the .o file back out to disk. XXX - Really, we only need to
172 * write the symbol table entries back out.
173 */
174 lseek(inf, 0, SEEK_SET);
175 if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
176 fprintf(stderr, "%s: write error: %s\n", filename,
177 rc == -1? strerror(errno) : "short write");
178 }
179 }
180
181
182 static void check_reloc(const char *filename, struct relocation_info *relp)
183 {
184 /* bail out if we zapped a symbol that is needed */
185 if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
186 fprintf(stderr,
187 "%s: oops, have hanging relocation for %s: bailing out!\n",
188 filename, SYMSTR(&symbase[relp->r_symbolnum]));
189 exit(1);
190 }
191 }
192
193 #endif /* defined(NLIST_AOUT) */
194