2 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Christopher G. Demetriou
15 * for the NetBSD Project.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $NetBSD: exec_elf32.c,v 1.4 1997/08/12 06:07:24 mikel Exp $
31 * $FreeBSD: src/usr.sbin/crunch/crunchide/exec_elf32.c,v 1.1.6.2 2002/07/25 09:33:17 ru Exp $
32 * $DragonFly: src/usr.sbin/crunch/crunchide/exec_elf32.c,v 1.2 2003/06/17 04:29:53 dillon Exp $
35 #include <sys/cdefs.h>
41 #include <sys/types.h>
52 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
53 (defined(NLIST_ELF64) && (ELFSIZE == 64))
55 #define __ELF_WORD_SIZE ELFSIZE
57 #include <sys/elf32.h>
59 #include <sys/elf64.h>
61 #include <sys/elf_generic.h>
63 #define CONCAT(x,y) __CONCAT(x,y)
64 #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
65 #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
66 #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
67 #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
70 struct listelem
*next
;
77 xreadatoff(int fd
, void *buf
, off_t off
, size_t size
, const char *fn
)
81 if (lseek(fd
, off
, SEEK_SET
) != off
) {
85 if ((rv
= read(fd
, buf
, size
)) != size
) {
86 fprintf(stderr
, "%s: read error: %s\n", fn
,
87 rv
== -1 ? strerror(errno
) : "short read");
94 xwriteatoff(int fd
, void *buf
, off_t off
, size_t size
, const char *fn
)
98 if (lseek(fd
, off
, SEEK_SET
) != off
) {
102 if ((rv
= write(fd
, buf
, size
)) != size
) {
103 fprintf(stderr
, "%s: write error: %s\n", fn
,
104 rv
== -1 ? strerror(errno
) : "short write");
111 xmalloc(size_t size
, const char *fn
, const char *use
)
117 fprintf(stderr
, "%s: out of memory (allocating for %s)\n",
123 ELFNAMEEND(check
)(int fd
, const char *fn
)
129 * Check the header to maek sure it's an ELF file (of the
132 if (fstat(fd
, &sb
) == -1)
134 if (sb
.st_size
< sizeof eh
)
136 if (read(fd
, &eh
, sizeof eh
) != sizeof eh
)
142 switch (eh
.e_machine
) {
144 case EM_ALPHA
: break;
145 /* ELFDEFNNAME(MACHDEP_ID_CASES) */
155 ELFNAMEEND(hide
)(int fd
, const char *fn
)
158 Elf_Shdr
*shdrp
= NULL
, *symtabshdr
, *strtabshdr
;
159 Elf_Sym
*symtabp
= NULL
;
160 char *strtabp
= NULL
;
161 Elf_Word
*symfwmap
= NULL
, *symrvmap
= NULL
, nsyms
, nlocalsyms
, ewi
;
162 struct listelem
*relalist
= NULL
, *rellist
= NULL
, *tmpl
;
167 if (xreadatoff(fd
, &ehdr
, 0, sizeof ehdr
, fn
) != sizeof ehdr
)
170 shdrsize
= ehdr
.e_shnum
* ehdr
.e_shentsize
;
171 if ((shdrp
= xmalloc(shdrsize
, fn
, "section header table")) == NULL
)
173 if (xreadatoff(fd
, shdrp
, ehdr
.e_shoff
, shdrsize
, fn
) != shdrsize
)
176 symtabshdr
= strtabshdr
= NULL
;
178 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
179 switch (shdrp
[i
].sh_type
) {
181 if (symtabshdr
!= NULL
)
183 symtabshdr
= &shdrp
[i
];
184 strtabshdr
= &shdrp
[shdrp
[i
].sh_link
];
187 tmpl
= xmalloc(sizeof *tmpl
, fn
, "rela list element");
191 tmpl
->file
= shdrp
[i
].sh_offset
;
192 tmpl
->size
= shdrp
[i
].sh_size
;
193 tmpl
->next
= relalist
;
197 tmpl
= xmalloc(sizeof *tmpl
, fn
, "rel list element");
201 tmpl
->file
= shdrp
[i
].sh_offset
;
202 tmpl
->size
= shdrp
[i
].sh_size
;
203 tmpl
->next
= rellist
;
208 if (symtabshdr
== NULL
)
210 if (strtabshdr
== NULL
)
213 fprintf(stderr
, "%s: weird executable (unsupported)\n", fn
);
218 * load up everything we need
222 if ((symtabp
= xmalloc(symtabshdr
->sh_size
, fn
, "symbol table"))
225 if (xreadatoff(fd
, symtabp
, symtabshdr
->sh_offset
, symtabshdr
->sh_size
,
226 fn
) != symtabshdr
->sh_size
)
230 if ((strtabp
= xmalloc(strtabshdr
->sh_size
, fn
, "string table"))
233 if (xreadatoff(fd
, strtabp
, strtabshdr
->sh_offset
, strtabshdr
->sh_size
,
234 fn
) != strtabshdr
->sh_size
)
237 /* any rela tables */
238 for (tmpl
= relalist
; tmpl
!= NULL
; tmpl
= tmpl
->next
) {
239 if ((tmpl
->mem
= xmalloc(tmpl
->size
, fn
, "rela table"))
242 if (xreadatoff(fd
, tmpl
->mem
, tmpl
->file
, tmpl
->size
, fn
) !=
248 for (tmpl
= rellist
; tmpl
!= NULL
; tmpl
= tmpl
->next
) {
249 if ((tmpl
->mem
= xmalloc(tmpl
->size
, fn
, "rel table"))
252 if (xreadatoff(fd
, tmpl
->mem
, tmpl
->file
, tmpl
->size
, fn
) !=
257 /* Prepare data structures for symbol movement. */
258 nsyms
= symtabshdr
->sh_size
/ symtabshdr
->sh_entsize
;
259 nlocalsyms
= symtabshdr
->sh_info
;
260 if ((symfwmap
= xmalloc(nsyms
* sizeof (Elf_Word
), fn
,
261 "symbol forward mapping table")) == NULL
)
263 if ((symrvmap
= xmalloc(nsyms
* sizeof (Elf_Word
), fn
,
264 "symbol reverse mapping table")) == NULL
)
267 /* init location -> symbol # table */
268 for (ewi
= 0; ewi
< nsyms
; ewi
++)
271 /* move symbols, making them local */
272 for (ewi
= nlocalsyms
; ewi
< nsyms
; ewi
++) {
273 Elf_Sym
*sp
, symswap
;
278 /* if it's on our keep list, don't move it */
279 if (in_keep_list(strtabp
+ sp
->st_name
))
282 /* if it's an undefined symbol, keep it */
283 if (sp
->st_shndx
== SHN_UNDEF
)
286 /* adjust the symbol so that it's local */
288 ELF_ST_INFO(STB_LOCAL
, sp
->st_info
);
289 /* (STB_LOCAL << 4) | ELF_SYM_TYPE(sp->st_info); *//* XXX */
292 * move the symbol to its new location
295 /* note that symbols in those locations have been swapped */
296 mapswap
= symrvmap
[ewi
];
297 symrvmap
[ewi
] = symrvmap
[nlocalsyms
];
298 symrvmap
[nlocalsyms
] = mapswap
;
300 /* and swap the symbols */
302 *sp
= symtabp
[nlocalsyms
];
303 symtabp
[nlocalsyms
] = symswap
;
305 nlocalsyms
++; /* note new local sym */
307 symtabshdr
->sh_info
= nlocalsyms
;
309 /* set up symbol # -> location mapping table */
310 for (ewi
= 0; ewi
< nsyms
; ewi
++)
311 symfwmap
[symrvmap
[ewi
]] = ewi
;
313 /* any rela tables */
314 for (tmpl
= relalist
; tmpl
!= NULL
; tmpl
= tmpl
->next
) {
315 Elf_Rela
*relap
= tmpl
->mem
;
317 for (ewi
= 0; ewi
< tmpl
->size
/ sizeof(*relap
); ewi
++) {
319 #if (ELFSIZE == 32) /* XXX */
320 symfwmap
[ELF_R_SYM(relap
[ewi
].r_info
)] << 8 |
321 ELF_R_TYPE(relap
[ewi
].r_info
);
322 #elif (ELFSIZE == 64) /* XXX */
323 symfwmap
[ELF_R_SYM(relap
[ewi
].r_info
)] << 32 |
324 ELF_R_TYPE(relap
[ewi
].r_info
);
330 for (tmpl
= rellist
; tmpl
!= NULL
; tmpl
= tmpl
->next
) {
331 Elf_Rel
*relp
= tmpl
->mem
;
333 for (ewi
= 0; ewi
< tmpl
->size
/ sizeof *relp
; ewi
++) {
335 #if (ELFSIZE == 32) /* XXX */
336 symfwmap
[ELF_R_SYM(relp
[ewi
].r_info
)] << 8 |
337 ELF_R_TYPE(relp
[ewi
].r_info
);
338 #elif (ELFSIZE == 64) /* XXX */
339 symfwmap
[ELF_R_SYM(relp
[ewi
].r_info
)] << 32 |
340 ELF_R_TYPE(relp
[ewi
].r_info
);
346 * write new tables to the file
348 if (xwriteatoff(fd
, shdrp
, ehdr
.e_shoff
, shdrsize
, fn
) != shdrsize
)
350 if (xwriteatoff(fd
, symtabp
, symtabshdr
->sh_offset
,
351 symtabshdr
->sh_size
, fn
) != symtabshdr
->sh_size
)
353 for (tmpl
= relalist
; tmpl
!= NULL
; tmpl
= tmpl
->next
) {
354 if (xwriteatoff(fd
, tmpl
->mem
, tmpl
->file
, tmpl
->size
, fn
) !=
358 for (tmpl
= rellist
; tmpl
!= NULL
; tmpl
= tmpl
->next
) {
359 if (xwriteatoff(fd
, tmpl
->mem
, tmpl
->file
, tmpl
->size
, fn
) !=
371 if (symfwmap
!= NULL
)
373 if (symrvmap
!= NULL
)
375 while ((tmpl
= relalist
) != NULL
) {
376 relalist
= tmpl
->next
;
377 if (tmpl
->mem
!= NULL
)
381 while ((tmpl
= rellist
) != NULL
) {
382 rellist
= tmpl
->next
;
383 if (tmpl
->mem
!= NULL
)
394 #endif /* include this size of ELF */