exec_elf32.c revision 1.4 1 1.4 mikel /* $NetBSD: exec_elf32.c,v 1.4 1997/08/12 06:07:24 mikel Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1997 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.4 mikel __RCSID("$NetBSD: exec_elf32.c,v 1.4 1997/08/12 06:07:24 mikel 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.1 cgd
58 1.1 cgd #define CONCAT(x,y) __CONCAT(x,y)
59 1.1 cgd #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
60 1.1 cgd #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
61 1.1 cgd #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
62 1.1 cgd #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.1 cgd int
118 1.2 cgd ELFNAMEEND(check)(int fd, const char *fn)
119 1.1 cgd {
120 1.1 cgd Elf_Ehdr eh;
121 1.1 cgd struct stat sb;
122 1.1 cgd
123 1.1 cgd /*
124 1.1 cgd * Check the header to maek sure it's an ELF file (of the
125 1.1 cgd * appropriate size).
126 1.1 cgd */
127 1.1 cgd if (fstat(fd, &sb) == -1)
128 1.1 cgd return 0;
129 1.1 cgd if (sb.st_size < sizeof eh)
130 1.1 cgd return 0;
131 1.1 cgd if (read(fd, &eh, sizeof eh) != sizeof eh)
132 1.1 cgd return 0;
133 1.1 cgd
134 1.1 cgd if (memcmp(eh.e_ident, Elf_e_ident, Elf_e_siz))
135 1.1 cgd return 0;
136 1.1 cgd
137 1.1 cgd switch (eh.e_machine) {
138 1.1 cgd ELFDEFNNAME(MACHDEP_ID_CASES)
139 1.1 cgd
140 1.1 cgd default:
141 1.1 cgd return 0;
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd return 1;
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd int
148 1.2 cgd ELFNAMEEND(hide)(int fd, const char *fn)
149 1.1 cgd {
150 1.2 cgd Elf_Ehdr ehdr;
151 1.2 cgd Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr;
152 1.2 cgd Elf_Sym *symtabp = NULL;
153 1.2 cgd char *strtabp = NULL;
154 1.2 cgd Elf_Word *symfwmap = NULL, *symrvmap = NULL, nsyms, nlocalsyms, ewi;
155 1.2 cgd struct listelem *relalist = NULL, *rellist = NULL, *tmpl;
156 1.2 cgd ssize_t shdrsize;
157 1.2 cgd int rv, i, weird;
158 1.2 cgd
159 1.2 cgd rv = 0;
160 1.2 cgd if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
161 1.2 cgd goto bad;
162 1.2 cgd
163 1.2 cgd shdrsize = ehdr.e_shnum * ehdr.e_shentsize;
164 1.2 cgd if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
165 1.2 cgd goto bad;
166 1.2 cgd if (xreadatoff(fd, shdrp, ehdr.e_shoff, shdrsize, fn) != shdrsize)
167 1.2 cgd goto bad;
168 1.2 cgd
169 1.2 cgd symtabshdr = strtabshdr = NULL;
170 1.2 cgd weird = 0;
171 1.2 cgd for (i = 0; i < ehdr.e_shnum; i++) {
172 1.2 cgd switch (shdrp[i].sh_type) {
173 1.2 cgd case Elf_sht_symtab:
174 1.2 cgd if (symtabshdr != NULL)
175 1.2 cgd weird = 1;
176 1.2 cgd symtabshdr = &shdrp[i];
177 1.2 cgd strtabshdr = &shdrp[shdrp[i].sh_link];
178 1.2 cgd break;
179 1.2 cgd case Elf_sht_rela:
180 1.2 cgd tmpl = xmalloc(sizeof *tmpl, fn, "rela list element");
181 1.2 cgd if (tmpl == NULL)
182 1.2 cgd goto bad;
183 1.2 cgd tmpl->mem = NULL;
184 1.2 cgd tmpl->file = shdrp[i].sh_offset;
185 1.2 cgd tmpl->size = shdrp[i].sh_size;
186 1.2 cgd tmpl->next = relalist;
187 1.2 cgd relalist = tmpl;
188 1.2 cgd break;
189 1.2 cgd case Elf_sht_rel:
190 1.2 cgd tmpl = xmalloc(sizeof *tmpl, fn, "rel list element");
191 1.2 cgd if (tmpl == NULL)
192 1.2 cgd goto bad;
193 1.2 cgd tmpl->mem = NULL;
194 1.2 cgd tmpl->file = shdrp[i].sh_offset;
195 1.2 cgd tmpl->size = shdrp[i].sh_size;
196 1.2 cgd tmpl->next = rellist;
197 1.2 cgd rellist = tmpl;
198 1.2 cgd break;
199 1.2 cgd }
200 1.2 cgd }
201 1.2 cgd if (symtabshdr == NULL)
202 1.2 cgd goto out;
203 1.2 cgd if (strtabshdr == NULL)
204 1.2 cgd weird = 1;
205 1.2 cgd if (weird) {
206 1.2 cgd fprintf(stderr, "%s: weird executable (unsupported)\n", fn);
207 1.2 cgd goto bad;
208 1.2 cgd }
209 1.2 cgd
210 1.2 cgd /*
211 1.2 cgd * load up everything we need
212 1.2 cgd */
213 1.2 cgd
214 1.2 cgd /* symbol table */
215 1.2 cgd if ((symtabp = xmalloc(symtabshdr->sh_size, fn, "symbol table"))
216 1.2 cgd == NULL)
217 1.2 cgd goto bad;
218 1.2 cgd if (xreadatoff(fd, symtabp, symtabshdr->sh_offset, symtabshdr->sh_size,
219 1.2 cgd fn) != symtabshdr->sh_size)
220 1.2 cgd goto bad;
221 1.2 cgd
222 1.2 cgd /* string table */
223 1.2 cgd if ((strtabp = xmalloc(strtabshdr->sh_size, fn, "string table"))
224 1.2 cgd == NULL)
225 1.2 cgd goto bad;
226 1.2 cgd if (xreadatoff(fd, strtabp, strtabshdr->sh_offset, strtabshdr->sh_size,
227 1.2 cgd fn) != strtabshdr->sh_size)
228 1.2 cgd goto bad;
229 1.2 cgd
230 1.2 cgd /* any rela tables */
231 1.2 cgd for (tmpl = relalist; tmpl != NULL; tmpl = tmpl->next) {
232 1.2 cgd if ((tmpl->mem = xmalloc(tmpl->size, fn, "rela table"))
233 1.2 cgd == NULL)
234 1.2 cgd goto bad;
235 1.2 cgd if (xreadatoff(fd, tmpl->mem, tmpl->file, tmpl->size, fn) !=
236 1.2 cgd tmpl->size)
237 1.2 cgd goto bad;
238 1.2 cgd }
239 1.2 cgd
240 1.2 cgd /* any rel tables */
241 1.2 cgd for (tmpl = rellist; tmpl != NULL; tmpl = tmpl->next) {
242 1.2 cgd if ((tmpl->mem = xmalloc(tmpl->size, fn, "rel table"))
243 1.2 cgd == NULL)
244 1.2 cgd goto bad;
245 1.2 cgd if (xreadatoff(fd, tmpl->mem, tmpl->file, tmpl->size, fn) !=
246 1.2 cgd tmpl->size)
247 1.2 cgd goto bad;
248 1.2 cgd }
249 1.2 cgd
250 1.2 cgd /* Prepare data structures for symbol movement. */
251 1.2 cgd nsyms = symtabshdr->sh_size / symtabshdr->sh_entsize;
252 1.2 cgd nlocalsyms = symtabshdr->sh_info;
253 1.2 cgd if ((symfwmap = xmalloc(nsyms * sizeof (Elf_Word), fn,
254 1.2 cgd "symbol forward mapping table")) == NULL)
255 1.2 cgd goto bad;
256 1.2 cgd if ((symrvmap = xmalloc(nsyms * sizeof (Elf_Word), fn,
257 1.2 cgd "symbol reverse mapping table")) == NULL)
258 1.2 cgd goto bad;
259 1.2 cgd
260 1.2 cgd /* init location -> symbol # table */
261 1.2 cgd for (ewi = 0; ewi < nsyms; ewi++)
262 1.2 cgd symrvmap[ewi] = ewi;
263 1.2 cgd
264 1.2 cgd /* move symbols, making them local */
265 1.2 cgd for (ewi = nlocalsyms; ewi < nsyms; ewi++) {
266 1.2 cgd Elf_Sym *sp, symswap;
267 1.2 cgd Elf_Word mapswap;
268 1.2 cgd
269 1.2 cgd sp = &symtabp[ewi];
270 1.2 cgd
271 1.2 cgd /* if it's on our keep list, don't move it */
272 1.2 cgd if (in_keep_list(strtabp + sp->st_name))
273 1.2 cgd continue;
274 1.2 cgd
275 1.2 cgd /* if it's an undefined symbol, keep it */
276 1.2 cgd if (sp->st_shndx == Elf_eshn_undefined)
277 1.2 cgd continue;
278 1.2 cgd
279 1.2 cgd /* adjust the symbol so that it's local */
280 1.2 cgd sp->st_info =
281 1.2 cgd (Elf_estb_local << 4) | ELF_SYM_TYPE(sp->st_info); /* XXX */
282 1.2 cgd
283 1.2 cgd /*
284 1.2 cgd * move the symbol to its new location
285 1.2 cgd */
286 1.2 cgd
287 1.2 cgd /* note that symbols in those locations have been swapped */
288 1.2 cgd mapswap = symrvmap[ewi];
289 1.2 cgd symrvmap[ewi] = symrvmap[nlocalsyms];
290 1.2 cgd symrvmap[nlocalsyms] = mapswap;
291 1.2 cgd
292 1.2 cgd /* and swap the symbols */
293 1.2 cgd symswap = *sp;
294 1.2 cgd *sp = symtabp[nlocalsyms];
295 1.2 cgd symtabp[nlocalsyms] = symswap;
296 1.2 cgd
297 1.2 cgd nlocalsyms++; /* note new local sym */
298 1.2 cgd }
299 1.2 cgd symtabshdr->sh_info = nlocalsyms;
300 1.2 cgd
301 1.2 cgd /* set up symbol # -> location mapping table */
302 1.2 cgd for (ewi = 0; ewi < nsyms; ewi++)
303 1.2 cgd symfwmap[symrvmap[ewi]] = ewi;
304 1.2 cgd
305 1.2 cgd /* any rela tables */
306 1.2 cgd for (tmpl = relalist; tmpl != NULL; tmpl = tmpl->next) {
307 1.2 cgd Elf_RelA *relap = tmpl->mem;
308 1.2 cgd
309 1.2 cgd for (ewi = 0; ewi < tmpl->size / sizeof *relap; ewi++) {
310 1.2 cgd relap[ewi].r_info =
311 1.2 cgd #if (ELFSIZE == 32) /* XXX */
312 1.2 cgd symfwmap[ELF_R_SYM(relap[ewi].r_info)] << 8 |
313 1.2 cgd ELF_R_TYPE(relap[ewi].r_info);
314 1.2 cgd #elif (ELFSIZE == 64) /* XXX */
315 1.2 cgd symfwmap[ELF_R_SYM(relap[ewi].r_info)] << 32 |
316 1.2 cgd ELF_R_TYPE(relap[ewi].r_info);
317 1.2 cgd #endif /* XXX */
318 1.2 cgd }
319 1.2 cgd }
320 1.2 cgd
321 1.2 cgd /* any rel tables */
322 1.2 cgd for (tmpl = rellist; tmpl != NULL; tmpl = tmpl->next) {
323 1.2 cgd Elf_Rel *relp = tmpl->mem;
324 1.2 cgd
325 1.2 cgd for (ewi = 0; ewi < tmpl->size / sizeof *relp; ewi++) {
326 1.2 cgd relp[ewi].r_info =
327 1.2 cgd #if (ELFSIZE == 32) /* XXX */
328 1.2 cgd symfwmap[ELF_R_SYM(relp[ewi].r_info)] << 8 |
329 1.2 cgd ELF_R_TYPE(relp[ewi].r_info);
330 1.2 cgd #elif (ELFSIZE == 64) /* XXX */
331 1.2 cgd symfwmap[ELF_R_SYM(relp[ewi].r_info)] << 32 |
332 1.2 cgd ELF_R_TYPE(relp[ewi].r_info);
333 1.2 cgd #endif /* XXX */
334 1.2 cgd }
335 1.2 cgd }
336 1.1 cgd
337 1.2 cgd /*
338 1.2 cgd * write new tables to the file
339 1.2 cgd */
340 1.2 cgd if (xwriteatoff(fd, shdrp, ehdr.e_shoff, shdrsize, fn) != shdrsize)
341 1.2 cgd goto bad;
342 1.2 cgd if (xwriteatoff(fd, symtabp, symtabshdr->sh_offset,
343 1.2 cgd symtabshdr->sh_size, fn) != symtabshdr->sh_size)
344 1.2 cgd goto bad;
345 1.2 cgd for (tmpl = relalist; tmpl != NULL; tmpl = tmpl->next) {
346 1.2 cgd if (xwriteatoff(fd, tmpl->mem, tmpl->file, tmpl->size, fn) !=
347 1.2 cgd tmpl->size)
348 1.2 cgd goto bad;
349 1.2 cgd }
350 1.2 cgd for (tmpl = rellist; tmpl != NULL; tmpl = tmpl->next) {
351 1.2 cgd if (xwriteatoff(fd, tmpl->mem, tmpl->file, tmpl->size, fn) !=
352 1.2 cgd tmpl->size)
353 1.2 cgd goto bad;
354 1.2 cgd }
355 1.2 cgd
356 1.2 cgd out:
357 1.2 cgd if (shdrp != NULL)
358 1.2 cgd free(shdrp);
359 1.2 cgd if (symtabp != NULL)
360 1.2 cgd free(symtabp);
361 1.2 cgd if (strtabp != NULL)
362 1.2 cgd free(strtabp);
363 1.2 cgd if (symfwmap != NULL)
364 1.2 cgd free(symfwmap);
365 1.2 cgd if (symrvmap != NULL)
366 1.2 cgd free(symrvmap);
367 1.2 cgd while ((tmpl = relalist) != NULL) {
368 1.2 cgd relalist = tmpl->next;
369 1.2 cgd if (tmpl->mem != NULL)
370 1.2 cgd free(tmpl->mem);
371 1.2 cgd free(tmpl);
372 1.2 cgd }
373 1.2 cgd while ((tmpl = rellist) != NULL) {
374 1.2 cgd rellist = tmpl->next;
375 1.2 cgd if (tmpl->mem != NULL)
376 1.2 cgd free(tmpl->mem);
377 1.2 cgd free(tmpl);
378 1.2 cgd }
379 1.2 cgd return (rv);
380 1.2 cgd
381 1.2 cgd bad:
382 1.2 cgd rv = 1;
383 1.2 cgd goto out;
384 1.1 cgd }
385 1.1 cgd
386 1.1 cgd #endif /* include this size of ELF */
387