1 /* $NetBSD: exec_aout.c,v 1.6 1997/08/02 21:30:17 perry Exp $ */
3 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
4 * Copyright (c) 1994 University of Maryland
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.
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.
24 * Author: James da Silva, Systems Design and Analysis Group
25 * Computer Science Department
26 * University of Maryland at College Park
28 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: exec_aout.c,v 1.6 1997/08/02 21:30:17 perry Exp $");
31 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
40 #include <sys/endian.h>
42 #include <sys/errno.h>
43 #include <netinet/in.h>
47 #if defined(NLIST_AOUT)
49 int nsyms, ntextrel, ndatarel;
51 char *aoutdata, *strbase;
52 struct relocation_info *textrel, *datarel;
53 struct nlist *symbase;
56 #define SYMSTR(sp) (&strbase[(sp)->n_un.n_strx])
58 /* is the symbol a global symbol defined in the current file? */
59 #define IS_GLOBAL_DEFINED(sp) \
60 (((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
62 /* is the relocation entry dependent on a symbol? */
63 #define IS_SYMBOL_RELOC(rp) \
64 ((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable)
66 static void check_reloc(const char *filename, struct relocation_info *relp);
68 int check_aout(int inf, const char *filename)
74 * check the header to make sure it's an a.out-format file.
77 if(fstat(inf, &infstat) == -1)
79 if(infstat.st_size < sizeof eh)
81 if(read(inf, &eh, sizeof eh) != sizeof eh)
90 int hide_aout(int inf, const char *filename)
93 struct relocation_info *relp;
98 * do some error checking.
101 if(fstat(inf, &infstat) == -1) {
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.
111 if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
112 fprintf(stderr, "%s: too big to read into memory\n", filename);
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");
123 * Calculate offsets and sizes from the header.
126 hdrp = (struct exec *) aoutdata;
129 textrel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp));
130 datarel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp) +
133 textrel = (struct relocation_info *) (aoutdata + N_TRELOFF(*hdrp));
134 datarel = (struct relocation_info *) (aoutdata + N_DRELOFF(*hdrp));
136 symbase = (struct nlist *) (aoutdata + N_SYMOFF(*hdrp));
137 strbase = (char *) (aoutdata + N_STROFF(*hdrp));
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);
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
149 for(symp = symbase; symp < symbase + nsyms; symp++) {
150 if(!IS_GLOBAL_DEFINED(symp)) /* keep undefined syms */
153 /* keep (C) symbols which are on the keep list */
154 if(SYMSTR(symp)[0] == '_' && in_keep_list(SYMSTR(symp) + 1))
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.
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);
173 * Write the .o file back out to disk. XXX - Really, we only need to
174 * write the symbol table entries back out.
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");
187 static void check_reloc(const char *filename, struct relocation_info *relp)
189 /* bail out if we zapped a symbol that is needed */
190 if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
192 "%s: oops, have hanging relocation for %s: bailing out!\n",
193 filename, SYMSTR(&symbase[relp->r_symbolnum]));
198 #endif /* defined(NLIST_AOUT) */