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