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