exec_elf32.c revision 1.9 1 1.9 kleink /* $NetBSD: exec_elf32.c,v 1.9 1999/10/25 13:57:11 kleink Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.5 cgd * Copyright (c) 1997, 1998 Christopher G. Demetriou. All rights reserved.
5 1.1 cgd *
6 1.1 cgd * Redistribution and use in source and binary forms, with or without
7 1.1 cgd * modification, are permitted provided that the following conditions
8 1.1 cgd * are met:
9 1.1 cgd * 1. Redistributions of source code must retain the above copyright
10 1.1 cgd * notice, this list of conditions and the following disclaimer.
11 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer in the
13 1.1 cgd * documentation and/or other materials provided with the distribution.
14 1.1 cgd * 3. All advertising materials mentioning features or use of this software
15 1.1 cgd * must display the following acknowledgement:
16 1.1 cgd * This product includes software developed by Christopher G. Demetriou
17 1.1 cgd * for the NetBSD Project.
18 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
19 1.1 cgd * derived from this software without specific prior written permission
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 cgd */
32 1.1 cgd
33 1.3 perry #include <sys/cdefs.h>
34 1.1 cgd #ifndef lint
35 1.9 kleink __RCSID("$NetBSD: exec_elf32.c,v 1.9 1999/10/25 13:57:11 kleink Exp $");
36 1.3 perry #endif
37 1.3 perry
38 1.1 cgd #ifndef ELFSIZE
39 1.1 cgd #define ELFSIZE 32
40 1.1 cgd #endif
41 1.1 cgd
42 1.1 cgd #include <sys/types.h>
43 1.1 cgd #include <sys/stat.h>
44 1.4 mikel
45 1.4 mikel #include <errno.h>
46 1.1 cgd #include <stdio.h>
47 1.2 cgd #include <stdlib.h>
48 1.1 cgd #include <string.h>
49 1.4 mikel #include <unistd.h>
50 1.4 mikel
51 1.1 cgd #include "extern.h"
52 1.1 cgd
53 1.1 cgd #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
54 1.1 cgd (defined(NLIST_ELF64) && (ELFSIZE == 64))
55 1.1 cgd
56 1.1 cgd #include <sys/exec_elf.h>
57 1.8 hannken
58 1.8 hannken #define CONCAT(x,y) __CONCAT(x,y)
59 1.8 hannken #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
60 1.8 hannken #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
61 1.8 hannken #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
62 1.8 hannken #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
63 1.1 cgd
64 1.2 cgd struct listelem {
65 1.2 cgd struct listelem *next;
66 1.2 cgd void *mem;
67 1.2 cgd off_t file;
68 1.2 cgd size_t size;
69 1.2 cgd };
70 1.2 cgd
71 1.2 cgd static ssize_t
72 1.2 cgd xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
73 1.2 cgd {
74 1.2 cgd ssize_t rv;
75 1.2 cgd
76 1.2 cgd if (lseek(fd, off, SEEK_SET) != off) {
77 1.2 cgd perror(fn);
78 1.2 cgd return -1;
79 1.2 cgd }
80 1.2 cgd if ((rv = read(fd, buf, size)) != size) {
81 1.2 cgd fprintf(stderr, "%s: read error: %s\n", fn,
82 1.2 cgd rv == -1 ? strerror(errno) : "short read");
83 1.2 cgd return -1;
84 1.2 cgd }
85 1.2 cgd return size;
86 1.2 cgd }
87 1.2 cgd
88 1.2 cgd static ssize_t
89 1.2 cgd xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
90 1.2 cgd {
91 1.2 cgd ssize_t rv;
92 1.2 cgd
93 1.2 cgd if (lseek(fd, off, SEEK_SET) != off) {
94 1.2 cgd perror(fn);
95 1.2 cgd return -1;
96 1.2 cgd }
97 1.2 cgd if ((rv = write(fd, buf, size)) != size) {
98 1.2 cgd fprintf(stderr, "%s: write error: %s\n", fn,
99 1.2 cgd rv == -1 ? strerror(errno) : "short write");
100 1.2 cgd return -1;
101 1.2 cgd }
102 1.2 cgd return size;
103 1.2 cgd }
104 1.2 cgd
105 1.2 cgd static void *
106 1.2 cgd xmalloc(size_t size, const char *fn, const char *use)
107 1.2 cgd {
108 1.2 cgd void *rv;
109 1.2 cgd
110 1.2 cgd rv = malloc(size);
111 1.2 cgd if (rv == NULL)
112 1.2 cgd fprintf(stderr, "%s: out of memory (allocating for %s)\n",
113 1.2 cgd fn, use);
114 1.2 cgd return (rv);
115 1.2 cgd }
116 1.2 cgd
117 1.5 cgd static void *
118 1.5 cgd xrealloc(void *ptr, size_t size, const char *fn, const char *use)
119 1.5 cgd {
120 1.5 cgd void *rv;
121 1.5 cgd
122 1.5 cgd rv = realloc(ptr, size);
123 1.5 cgd if (rv == NULL) {
124 1.5 cgd free(ptr);
125 1.5 cgd fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
126 1.5 cgd fn, use);
127 1.5 cgd }
128 1.5 cgd return (rv);
129 1.5 cgd }
130 1.5 cgd
131 1.1 cgd int
132 1.2 cgd ELFNAMEEND(check)(int fd, const char *fn)
133 1.1 cgd {
134 1.1 cgd Elf_Ehdr eh;
135 1.1 cgd struct stat sb;
136 1.1 cgd
137 1.1 cgd /*
138 1.1 cgd * Check the header to maek sure it's an ELF file (of the
139 1.1 cgd * appropriate size).
140 1.1 cgd */
141 1.1 cgd if (fstat(fd, &sb) == -1)
142 1.1 cgd return 0;
143 1.1 cgd if (sb.st_size < sizeof eh)
144 1.1 cgd return 0;
145 1.1 cgd if (read(fd, &eh, sizeof eh) != sizeof eh)
146 1.1 cgd return 0;
147 1.1 cgd
148 1.9 kleink if (memcmp(eh.e_ident, ELFMAG, SELFMAG) != 0 ||
149 1.9 kleink eh.e_ident[EI_CLASS] != ELFCLASS)
150 1.1 cgd return 0;
151 1.1 cgd
152 1.1 cgd switch (eh.e_machine) {
153 1.1 cgd ELFDEFNNAME(MACHDEP_ID_CASES)
154 1.1 cgd
155 1.1 cgd default:
156 1.1 cgd return 0;
157 1.1 cgd }
158 1.1 cgd
159 1.1 cgd return 1;
160 1.1 cgd }
161 1.1 cgd
162 1.5 cgd /*
163 1.5 cgd * This function 'hides' (some of) ELF executable file's symbols.
164 1.5 cgd * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
165 1.5 cgd * Symbols in the global keep list, or which are marked as being undefined,
166 1.5 cgd * are left alone.
167 1.5 cgd *
168 1.5 cgd * An old version of this code shuffled various tables around, turning
169 1.5 cgd * global symbols to be hidden into local symbols. That lost on the
170 1.5 cgd * mips, because CALL16 relocs must reference global symbols, and, if
171 1.5 cgd * those symbols were being hidden, they were no longer global.
172 1.5 cgd *
173 1.5 cgd * The new renaming behaviour doesn't take global symbols out of the
174 1.5 cgd * namespace. However, it's ... unlikely that there will ever be
175 1.5 cgd * any collisions in practice because of the new method.
176 1.5 cgd */
177 1.1 cgd int
178 1.2 cgd ELFNAMEEND(hide)(int fd, const char *fn)
179 1.1 cgd {
180 1.2 cgd Elf_Ehdr ehdr;
181 1.5 cgd Elf_Shdr *shdrp = NULL;
182 1.5 cgd int symtabsnum, strtabsnum;
183 1.2 cgd Elf_Sym *symtabp = NULL;
184 1.5 cgd char *strtabp = NULL, *nstrtabp = NULL;
185 1.5 cgd Elf_Word nsyms;
186 1.5 cgd Elf_Off stroff, maxoff;
187 1.5 cgd const char *weirdreason;
188 1.2 cgd ssize_t shdrsize;
189 1.5 cgd size_t nstrtab_size, nstrtab_nextoff, fn_size;
190 1.2 cgd int rv, i, weird;
191 1.2 cgd
192 1.2 cgd rv = 0;
193 1.2 cgd if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
194 1.2 cgd goto bad;
195 1.2 cgd
196 1.2 cgd shdrsize = ehdr.e_shnum * ehdr.e_shentsize;
197 1.2 cgd if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
198 1.2 cgd goto bad;
199 1.2 cgd if (xreadatoff(fd, shdrp, ehdr.e_shoff, shdrsize, fn) != shdrsize)
200 1.2 cgd goto bad;
201 1.2 cgd
202 1.5 cgd symtabsnum = strtabsnum = -1;
203 1.5 cgd maxoff = stroff = 0;
204 1.2 cgd weird = 0;
205 1.5 cgd weirdreason = "???";
206 1.2 cgd for (i = 0; i < ehdr.e_shnum; i++) {
207 1.5 cgd if (shdrp[i].sh_offset > maxoff) {
208 1.5 cgd maxoff = shdrp[i].sh_offset;
209 1.5 cgd }
210 1.2 cgd switch (shdrp[i].sh_type) {
211 1.9 kleink case SHT_SYMTAB:
212 1.5 cgd if (!weird && symtabsnum != -1) {
213 1.5 cgd weird = 1;
214 1.5 cgd weirdreason = "multiple symbol tables";
215 1.5 cgd }
216 1.5 cgd symtabsnum = i;
217 1.5 cgd strtabsnum = shdrp[i].sh_link;
218 1.5 cgd stroff = shdrp[strtabsnum].sh_offset;
219 1.5 cgd if (!weird && strtabsnum != (ehdr.e_shnum - 1)) {
220 1.2 cgd weird = 1;
221 1.5 cgd weirdreason = "string table not last section";
222 1.5 cgd }
223 1.2 cgd break;
224 1.2 cgd }
225 1.2 cgd }
226 1.5 cgd if (symtabsnum == -1)
227 1.2 cgd goto out;
228 1.5 cgd if (!weird && strtabsnum == -1) {
229 1.5 cgd weird = 1;
230 1.5 cgd weirdreason = "no string table found";
231 1.5 cgd }
232 1.5 cgd if (!weird && stroff != maxoff) {
233 1.2 cgd weird = 1;
234 1.5 cgd weirdreason = "string table section not last in file";
235 1.5 cgd }
236 1.2 cgd if (weird) {
237 1.5 cgd fprintf(stderr, "%s: weird executable (%s); unsupported\n", fn,
238 1.5 cgd weirdreason);
239 1.2 cgd goto bad;
240 1.2 cgd }
241 1.2 cgd
242 1.2 cgd /*
243 1.2 cgd * load up everything we need
244 1.2 cgd */
245 1.2 cgd
246 1.2 cgd /* symbol table */
247 1.5 cgd if ((symtabp = xmalloc(shdrp[symtabsnum].sh_size, fn, "symbol table"))
248 1.2 cgd == NULL)
249 1.2 cgd goto bad;
250 1.5 cgd if (xreadatoff(fd, symtabp, shdrp[symtabsnum].sh_offset,
251 1.5 cgd shdrp[symtabsnum].sh_size, fn) != shdrp[symtabsnum].sh_size)
252 1.2 cgd goto bad;
253 1.2 cgd
254 1.2 cgd /* string table */
255 1.5 cgd if ((strtabp = xmalloc(shdrp[strtabsnum].sh_size, fn, "string table"))
256 1.2 cgd == NULL)
257 1.2 cgd goto bad;
258 1.5 cgd if (xreadatoff(fd, strtabp, shdrp[strtabsnum].sh_offset,
259 1.5 cgd shdrp[strtabsnum].sh_size, fn) != shdrp[strtabsnum].sh_size)
260 1.2 cgd goto bad;
261 1.2 cgd
262 1.5 cgd nsyms = shdrp[symtabsnum].sh_size / shdrp[symtabsnum].sh_entsize;
263 1.5 cgd
264 1.5 cgd nstrtab_size = 256;
265 1.5 cgd nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
266 1.5 cgd if (nstrtabp == NULL)
267 1.5 cgd goto bad;
268 1.5 cgd nstrtab_nextoff = 0;
269 1.2 cgd
270 1.5 cgd fn_size = strlen(fn);
271 1.2 cgd
272 1.5 cgd for (i = 0; i < nsyms; i++) {
273 1.5 cgd Elf_Sym *sp = &symtabp[i];
274 1.5 cgd const char *symname = strtabp + sp->st_name;
275 1.5 cgd size_t newent_len;
276 1.2 cgd
277 1.2 cgd /*
278 1.5 cgd * make sure there's size for the next entry, even if it's
279 1.5 cgd * as large as it can be.
280 1.5 cgd *
281 1.5 cgd * "_$$hide$$ <filename> <symname><NUL>" ->
282 1.5 cgd * 9 + 3 + sizes of fn and sym name
283 1.2 cgd */
284 1.5 cgd while ((nstrtab_size - nstrtab_nextoff) <
285 1.5 cgd strlen(symname) + fn_size + 12) {
286 1.5 cgd nstrtab_size *= 2;
287 1.5 cgd nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
288 1.5 cgd "new string table");
289 1.5 cgd if (nstrtabp == NULL)
290 1.5 cgd goto bad;
291 1.5 cgd }
292 1.2 cgd
293 1.5 cgd sp->st_name = nstrtab_nextoff;
294 1.2 cgd
295 1.5 cgd /* if it's a keeper or is undefined, don't rename it. */
296 1.5 cgd if (in_keep_list(symname) ||
297 1.9 kleink sp->st_shndx == SHN_UNDEF) {
298 1.5 cgd newent_len = sprintf(nstrtabp + nstrtab_nextoff,
299 1.5 cgd "%s", symname) + 1;
300 1.5 cgd } else {
301 1.5 cgd newent_len = sprintf(nstrtabp + nstrtab_nextoff,
302 1.5 cgd "_$$hide$$ %s %s", fn, symname) + 1;
303 1.2 cgd }
304 1.5 cgd nstrtab_nextoff += newent_len;
305 1.5 cgd }
306 1.5 cgd shdrp[strtabsnum].sh_size = nstrtab_nextoff;
307 1.1 cgd
308 1.2 cgd /*
309 1.2 cgd * write new tables to the file
310 1.2 cgd */
311 1.2 cgd if (xwriteatoff(fd, shdrp, ehdr.e_shoff, shdrsize, fn) != shdrsize)
312 1.2 cgd goto bad;
313 1.5 cgd if (xwriteatoff(fd, symtabp, shdrp[symtabsnum].sh_offset,
314 1.5 cgd shdrp[symtabsnum].sh_size, fn) != shdrp[symtabsnum].sh_size)
315 1.5 cgd goto bad;
316 1.5 cgd if (xwriteatoff(fd, nstrtabp, shdrp[strtabsnum].sh_offset,
317 1.5 cgd shdrp[strtabsnum].sh_size, fn) != shdrp[strtabsnum].sh_size)
318 1.2 cgd goto bad;
319 1.2 cgd
320 1.2 cgd out:
321 1.2 cgd if (shdrp != NULL)
322 1.2 cgd free(shdrp);
323 1.2 cgd if (symtabp != NULL)
324 1.2 cgd free(symtabp);
325 1.2 cgd if (strtabp != NULL)
326 1.2 cgd free(strtabp);
327 1.5 cgd if (nstrtabp != NULL)
328 1.6 christos free(nstrtabp);
329 1.2 cgd return (rv);
330 1.2 cgd
331 1.2 cgd bad:
332 1.2 cgd rv = 1;
333 1.2 cgd goto out;
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd #endif /* include this size of ELF */
337