1 /* $NetBSD: exec_aout.c,v 1.11 2009/08/16 18:15:28 martin Exp $ */
4 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
5 * Copyright (c) 1994 University of Maryland
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation, and that the name of U.M. not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. U.M. makes no representations about the
15 * suitability of this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
18 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
20 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Author: James da Silva, Systems Design and Analysis Group
26 * Computer Science Department
27 * University of Maryland at College Park
30 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: exec_aout.c,v 1.11 2009/08/16 18:15:28 martin Exp $");
40 #include <sys/types.h>
45 #if defined(NLIST_AOUT)
48 #include <sys/exec_aout.h>
50 int nsyms
, ntextrel
, ndatarel
;
52 char *aoutdata
, *strbase
;
53 struct relocation_info
*textrel
, *datarel
;
54 struct nlist
*symbase
;
57 #define SYMSTR(sp) (&strbase[(sp)->n_un.n_strx])
59 /* is the symbol a global symbol defined in the current file? */
60 #define IS_GLOBAL_DEFINED(sp) \
61 (((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
64 /* is the relocation entry dependent on a symbol? */
65 #define IS_SYMBOL_RELOC(rp) \
67 ((rp)->r_type >= RELOC_BASE10 && (rp)->r_type <= RELOC_BASE22) || \
68 (rp)->r_type == RELOC_JMP_TBL)
70 /* is the relocation entry dependent on a symbol? */
71 #define IS_SYMBOL_RELOC(rp) \
72 ((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable)
75 static void check_reloc(const char *filename
, struct relocation_info
*relp
);
77 int check_aout(int inf
, const char *filename
)
83 * check the header to make sure it's an a.out-format file.
86 if(fstat(inf
, &infstat
) == -1)
88 if(infstat
.st_size
< (ssize_t
)sizeof eh
)
90 if(read(inf
, &eh
, sizeof eh
) != sizeof eh
)
99 int hide_aout(int inf
, const char *filename
)
102 struct relocation_info
*relp
;
107 * do some error checking.
110 if(fstat(inf
, &infstat
) == -1) {
116 * Read the entire file into memory. XXX - Really, we only need to
117 * read the header and from TRELOFF to the end of the file.
120 if((aoutdata
= (char *) malloc(infstat
.st_size
)) == NULL
) {
121 fprintf(stderr
, "%s: too big to read into memory\n", filename
);
125 if((rc
= read(inf
, aoutdata
, infstat
.st_size
)) < infstat
.st_size
) {
126 fprintf(stderr
, "%s: read error: %s\n", filename
,
127 rc
== -1? strerror(errno
) : "short read");
132 * Calculate offsets and sizes from the header.
135 hdrp
= (struct exec
*) aoutdata
;
138 textrel
= (struct relocation_info
*) (aoutdata
+ N_RELOFF(*hdrp
));
139 datarel
= (struct relocation_info
*) (aoutdata
+ N_RELOFF(*hdrp
) +
142 textrel
= (struct relocation_info
*) (aoutdata
+ N_TRELOFF(*hdrp
));
143 datarel
= (struct relocation_info
*) (aoutdata
+ N_DRELOFF(*hdrp
));
145 symbase
= (struct nlist
*) (aoutdata
+ N_SYMOFF(*hdrp
));
146 strbase
= (char *) (aoutdata
+ N_STROFF(*hdrp
));
148 ntextrel
= hdrp
->a_trsize
/ sizeof(struct relocation_info
);
149 ndatarel
= hdrp
->a_drsize
/ sizeof(struct relocation_info
);
150 nsyms
= hdrp
->a_syms
/ sizeof(struct nlist
);
153 * Zap the type field of all globally-defined symbols. The linker will
154 * subsequently ignore these entries. Don't zap any symbols in the
158 for(symp
= symbase
; symp
< symbase
+ nsyms
; symp
++) {
159 if(!IS_GLOBAL_DEFINED(symp
)) /* keep undefined syms */
162 /* keep (C) symbols which are on the keep list */
163 if(SYMSTR(symp
)[0] == '_' && in_keep_list(SYMSTR(symp
) + 1))
170 * Check whether the relocation entries reference any symbols that we
171 * just zapped. I don't know whether ld can handle this case, but I
172 * haven't encountered it yet. These checks are here so that the program
173 * doesn't fail silently should such symbols be encountered.
176 for(relp
= textrel
; relp
< textrel
+ ntextrel
; relp
++)
177 check_reloc(filename
, relp
);
178 for(relp
= datarel
; relp
< datarel
+ ndatarel
; relp
++)
179 check_reloc(filename
, relp
);
182 * Write the .o file back out to disk. XXX - Really, we only need to
183 * write the symbol table entries back out.
185 lseek(inf
, 0, SEEK_SET
);
186 if((rc
= write(inf
, aoutdata
, infstat
.st_size
)) < infstat
.st_size
) {
187 fprintf(stderr
, "%s: write error: %s\n", filename
,
188 rc
== -1? strerror(errno
) : "short write");
196 static void check_reloc(const char *filename
, struct relocation_info
*relp
)
198 /* bail out if we zapped a symbol that is needed */
199 if(IS_SYMBOL_RELOC(relp
) && symbase
[relp
->r_symbolnum
].n_type
== 0) {
201 "%s: oops, have hanging relocation for %s: bailing out!\n",
202 filename
, SYMSTR(&symbase
[relp
->r_symbolnum
]));
207 #endif /* defined(NLIST_AOUT) */