exec_aout.c revision 1.4 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
3 1.1 cgd * Copyright (c) 1994 University of Maryland
4 1.1 cgd * All Rights Reserved.
5 1.1 cgd *
6 1.1 cgd * Permission to use, copy, modify, distribute, and sell this software and its
7 1.1 cgd * documentation for any purpose is hereby granted without fee, provided that
8 1.1 cgd * the above copyright notice appear in all copies and that both that
9 1.1 cgd * copyright notice and this permission notice appear in supporting
10 1.1 cgd * documentation, and that the name of U.M. not be used in advertising or
11 1.1 cgd * publicity pertaining to distribution of the software without specific,
12 1.1 cgd * written prior permission. U.M. makes no representations about the
13 1.1 cgd * suitability of this software for any purpose. It is provided "as is"
14 1.1 cgd * without express or implied warranty.
15 1.1 cgd *
16 1.1 cgd * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 1.1 cgd * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 1.1 cgd * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 1.1 cgd * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 1.1 cgd * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 1.1 cgd *
23 1.1 cgd * Author: James da Silva, Systems Design and Analysis Group
24 1.1 cgd * Computer Science Department
25 1.1 cgd * University of Maryland at College Park
26 1.1 cgd */
27 1.1 cgd
28 1.1 cgd #include <unistd.h>
29 1.1 cgd #include <stdio.h>
30 1.1 cgd #include <stdlib.h>
31 1.1 cgd #include <string.h>
32 1.1 cgd #include <a.out.h>
33 1.1 cgd #include <sys/types.h>
34 1.1 cgd #include <sys/stat.h>
35 1.1 cgd #include <sys/errno.h>
36 1.1 cgd
37 1.1 cgd #include "extern.h"
38 1.1 cgd
39 1.2 cgd #if defined(NLIST_AOUT)
40 1.2 cgd
41 1.1 cgd int nsyms, ntextrel, ndatarel;
42 1.1 cgd struct exec *hdrp;
43 1.1 cgd char *aoutdata, *strbase;
44 1.1 cgd struct relocation_info *textrel, *datarel;
45 1.1 cgd struct nlist *symbase;
46 1.1 cgd
47 1.1 cgd
48 1.1 cgd #define SYMSTR(sp) &strbase[(sp)->n_un.n_strx]
49 1.1 cgd
50 1.1 cgd /* is the symbol a global symbol defined in the current file? */
51 1.1 cgd #define IS_GLOBAL_DEFINED(sp) \
52 1.1 cgd (((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
53 1.1 cgd
54 1.1 cgd #ifdef __sparc
55 1.1 cgd /* is the relocation entry dependent on a symbol? */
56 1.1 cgd #define IS_SYMBOL_RELOC(rp) \
57 1.1 cgd ((rp)->r_extern || \
58 1.1 cgd ((rp)->r_type >= RELOC_BASE10 && (rp)->r_type <= RELOC_BASE22) || \
59 1.1 cgd (rp)->r_type == RELOC_JMP_TBL)
60 1.1 cgd #else
61 1.1 cgd /* is the relocation entry dependent on a symbol? */
62 1.1 cgd #define IS_SYMBOL_RELOC(rp) \
63 1.1 cgd ((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable)
64 1.1 cgd #endif
65 1.1 cgd
66 1.1 cgd static void check_reloc(const char *filename, struct relocation_info *relp);
67 1.1 cgd
68 1.1 cgd int check_aout(int inf, const char *filename)
69 1.1 cgd {
70 1.1 cgd struct stat infstat;
71 1.1 cgd struct exec eh;
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * check the header to make sure it's an a.out-format file.
75 1.1 cgd */
76 1.1 cgd
77 1.1 cgd if(fstat(inf, &infstat) == -1)
78 1.1 cgd return 0;
79 1.1 cgd if(infstat.st_size < sizeof eh)
80 1.1 cgd return 0;
81 1.2 cgd if(read(inf, &eh, sizeof eh) != sizeof eh)
82 1.1 cgd return 0;
83 1.1 cgd
84 1.3 cgd if(N_BADMAG(eh))
85 1.1 cgd return 0;
86 1.1 cgd
87 1.1 cgd return 1;
88 1.1 cgd }
89 1.1 cgd
90 1.2 cgd int hide_aout(int inf, const char *filename)
91 1.1 cgd {
92 1.1 cgd struct stat infstat;
93 1.1 cgd struct relocation_info *relp;
94 1.1 cgd struct nlist *symp;
95 1.1 cgd int rc;
96 1.1 cgd
97 1.1 cgd /*
98 1.1 cgd * do some error checking.
99 1.1 cgd */
100 1.1 cgd
101 1.1 cgd if(fstat(inf, &infstat) == -1) {
102 1.1 cgd perror(filename);
103 1.2 cgd return 1;
104 1.1 cgd }
105 1.1 cgd
106 1.1 cgd /*
107 1.1 cgd * Read the entire file into memory. XXX - Really, we only need to
108 1.1 cgd * read the header and from TRELOFF to the end of the file.
109 1.1 cgd */
110 1.1 cgd
111 1.1 cgd if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
112 1.1 cgd fprintf(stderr, "%s: too big to read into memory\n", filename);
113 1.2 cgd return 1;
114 1.1 cgd }
115 1.1 cgd
116 1.1 cgd if((rc = read(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
117 1.1 cgd fprintf(stderr, "%s: read error: %s\n", filename,
118 1.1 cgd rc == -1? strerror(errno) : "short read");
119 1.2 cgd return 1;
120 1.1 cgd }
121 1.1 cgd
122 1.1 cgd /*
123 1.3 cgd * Calculate offsets and sizes from the header.
124 1.1 cgd */
125 1.1 cgd
126 1.1 cgd hdrp = (struct exec *) aoutdata;
127 1.1 cgd
128 1.1 cgd #ifdef __FreeBSD__
129 1.1 cgd textrel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp));
130 1.1 cgd datarel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp) +
131 1.1 cgd hdrp->a_trsize);
132 1.1 cgd #else
133 1.1 cgd textrel = (struct relocation_info *) (aoutdata + N_TRELOFF(*hdrp));
134 1.1 cgd datarel = (struct relocation_info *) (aoutdata + N_DRELOFF(*hdrp));
135 1.1 cgd #endif
136 1.1 cgd symbase = (struct nlist *) (aoutdata + N_SYMOFF(*hdrp));
137 1.1 cgd strbase = (char *) (aoutdata + N_STROFF(*hdrp));
138 1.1 cgd
139 1.1 cgd ntextrel = hdrp->a_trsize / sizeof(struct relocation_info);
140 1.1 cgd ndatarel = hdrp->a_drsize / sizeof(struct relocation_info);
141 1.1 cgd nsyms = hdrp->a_syms / sizeof(struct nlist);
142 1.1 cgd
143 1.1 cgd /*
144 1.1 cgd * Zap the type field of all globally-defined symbols. The linker will
145 1.1 cgd * subsequently ignore these entries. Don't zap any symbols in the
146 1.1 cgd * keep list.
147 1.1 cgd */
148 1.1 cgd
149 1.4 cgd for(symp = symbase; symp < symbase + nsyms; symp++) {
150 1.4 cgd if(!IS_GLOBAL_DEFINED(symp)) /* keep undefined syms */
151 1.4 cgd continue;
152 1.4 cgd
153 1.4 cgd /* keep (C) symbols which are on the keep list */
154 1.4 cgd if(SYMSTR(symp)[0] == '_' && in_keep_list(SYMSTR(symp) + 1))
155 1.4 cgd continue;
156 1.4 cgd
157 1.4 cgd symp->n_type = 0;
158 1.4 cgd }
159 1.1 cgd
160 1.1 cgd /*
161 1.1 cgd * Check whether the relocation entries reference any symbols that we
162 1.1 cgd * just zapped. I don't know whether ld can handle this case, but I
163 1.1 cgd * haven't encountered it yet. These checks are here so that the program
164 1.1 cgd * doesn't fail silently should such symbols be encountered.
165 1.1 cgd */
166 1.1 cgd
167 1.1 cgd for(relp = textrel; relp < textrel + ntextrel; relp++)
168 1.1 cgd check_reloc(filename, relp);
169 1.1 cgd for(relp = datarel; relp < datarel + ndatarel; relp++)
170 1.1 cgd check_reloc(filename, relp);
171 1.1 cgd
172 1.1 cgd /*
173 1.1 cgd * Write the .o file back out to disk. XXX - Really, we only need to
174 1.1 cgd * write the symbol table entries back out.
175 1.1 cgd */
176 1.1 cgd lseek(inf, 0, SEEK_SET);
177 1.1 cgd if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
178 1.1 cgd fprintf(stderr, "%s: write error: %s\n", filename,
179 1.1 cgd rc == -1? strerror(errno) : "short write");
180 1.2 cgd return 1;
181 1.1 cgd }
182 1.2 cgd
183 1.2 cgd return 0;
184 1.1 cgd }
185 1.1 cgd
186 1.1 cgd
187 1.1 cgd static void check_reloc(const char *filename, struct relocation_info *relp)
188 1.1 cgd {
189 1.1 cgd /* bail out if we zapped a symbol that is needed */
190 1.1 cgd if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
191 1.1 cgd fprintf(stderr,
192 1.1 cgd "%s: oops, have hanging relocation for %s: bailing out!\n",
193 1.1 cgd filename, SYMSTR(&symbase[relp->r_symbolnum]));
194 1.1 cgd exit(1);
195 1.1 cgd }
196 1.1 cgd }
197 1.1 cgd
198 1.1 cgd #endif /* defined(NLIST_AOUT) */
199