exec_aout.c revision 1.5 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 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <a.out.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/errno.h>
36
37 #include "extern.h"
38
39 #if defined(NLIST_AOUT)
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(eh))
85 return 0;
86
87 return 1;
88 }
89
90 int 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
187 static 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