freebsd_exec_elf32.c revision 1.5 1 1.5 jdolecek /* $NetBSD: freebsd_exec_elf32.c,v 1.5 2001/10/27 09:50:22 jdolecek Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*
4 1.1 jdolecek * Copyright (c) 1993, 1994 Christopher G. Demetriou
5 1.1 jdolecek * All rights reserved.
6 1.1 jdolecek *
7 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.1 jdolecek * modification, are permitted provided that the following conditions
9 1.1 jdolecek * are met:
10 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.1 jdolecek * documentation and/or other materials provided with the distribution.
15 1.1 jdolecek * 3. All advertising materials mentioning features or use of this software
16 1.1 jdolecek * must display the following acknowledgement:
17 1.1 jdolecek * This product includes software developed by Christopher G. Demetriou.
18 1.1 jdolecek * 4. The name of the author may not be used to endorse or promote products
19 1.1 jdolecek * derived from this software without specific prior written permission
20 1.1 jdolecek *
21 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 jdolecek * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 jdolecek * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 jdolecek * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 jdolecek * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 jdolecek * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 jdolecek * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 jdolecek * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 jdolecek */
32 1.1 jdolecek
33 1.1 jdolecek #include <sys/param.h>
34 1.1 jdolecek #include <sys/systm.h>
35 1.1 jdolecek #include <sys/proc.h>
36 1.1 jdolecek #include <sys/malloc.h>
37 1.1 jdolecek #include <sys/vnode.h>
38 1.1 jdolecek #include <sys/exec.h>
39 1.1 jdolecek #ifndef ELFSIZE
40 1.1 jdolecek # define ELFSIZE 32
41 1.1 jdolecek #endif /* !ELFSIZE */
42 1.1 jdolecek #include <sys/exec_elf.h>
43 1.1 jdolecek
44 1.1 jdolecek #include <machine/freebsd_machdep.h>
45 1.1 jdolecek
46 1.1 jdolecek #include <compat/freebsd/freebsd_exec.h>
47 1.1 jdolecek #include <compat/common/compat_util.h>
48 1.1 jdolecek
49 1.1 jdolecek int
50 1.1 jdolecek ELFNAME2(freebsd,probe)(p, epp, veh, itp, pos)
51 1.1 jdolecek struct proc *p;
52 1.1 jdolecek struct exec_package *epp;
53 1.1 jdolecek void *veh;
54 1.1 jdolecek char *itp;
55 1.1 jdolecek vaddr_t *pos;
56 1.1 jdolecek {
57 1.1 jdolecek int error;
58 1.1 jdolecek size_t i;
59 1.1 jdolecek size_t phsize;
60 1.1 jdolecek Elf_Ehdr *eh = (Elf_Ehdr *) veh;
61 1.1 jdolecek Elf_Phdr *ph;
62 1.1 jdolecek Elf_Phdr *ephp;
63 1.1 jdolecek Elf_Nhdr *np;
64 1.1 jdolecek const char *bp;
65 1.1 jdolecek
66 1.1 jdolecek static const char wantBrand[] = FREEBSD_ELF_BRAND_STRING;
67 1.1 jdolecek static const char wantInterp[] = FREEBSD_ELF_INTERP_PREFIX_STRING;
68 1.1 jdolecek
69 1.5 jdolecek /*
70 1.5 jdolecek * Insist that the executable have a brand, and that it be "FreeBSD".
71 1.5 jdolecek * Newer FreeBSD binaries have OSABI set to ELFOSABI_FREEBSD. This
72 1.5 jdolecek * is arguably broken, but they seem to think they need it, for
73 1.5 jdolecek * whatever reason.
74 1.5 jdolecek */
75 1.1 jdolecek #ifndef EI_BRAND
76 1.1 jdolecek #define EI_BRAND 8
77 1.1 jdolecek #endif
78 1.5 jdolecek if ((eh->e_ident[EI_BRAND] == '\0'
79 1.5 jdolecek || strcmp(&eh->e_ident[EI_BRAND], wantBrand) != 0)
80 1.5 jdolecek && eh->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
81 1.5 jdolecek return ENOEXEC;
82 1.1 jdolecek
83 1.1 jdolecek i = eh->e_phnum;
84 1.1 jdolecek if (i != 0) {
85 1.1 jdolecek phsize = i * sizeof(Elf_Phdr);
86 1.1 jdolecek ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
87 1.2 christos if ((error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph,
88 1.2 christos phsize)) != 0)
89 1.1 jdolecek goto bad1;
90 1.1 jdolecek
91 1.1 jdolecek for (ephp = ph; i--; ephp++) {
92 1.1 jdolecek if (ephp->p_type != PT_INTERP)
93 1.1 jdolecek continue;
94 1.1 jdolecek
95 1.1 jdolecek /* Check for "legal" intepreter name. */
96 1.1 jdolecek if (ephp->p_filesz < sizeof wantInterp)
97 1.1 jdolecek goto bad1;
98 1.1 jdolecek
99 1.1 jdolecek np = (Elf_Nhdr *) malloc(ephp->p_filesz+1,
100 1.1 jdolecek M_TEMP, M_WAITOK);
101 1.1 jdolecek
102 1.2 christos if (((error = exec_read_from(p, epp->ep_vp,
103 1.2 christos ephp->p_offset, np, ephp->p_filesz)) != 0))
104 1.1 jdolecek goto bad2;
105 1.1 jdolecek
106 1.1 jdolecek if (strncmp((char *)np, wantInterp,
107 1.1 jdolecek sizeof wantInterp - 1))
108 1.1 jdolecek goto bad2;
109 1.1 jdolecek
110 1.1 jdolecek free(np, M_TEMP);
111 1.1 jdolecek break;
112 1.1 jdolecek }
113 1.1 jdolecek free(ph, M_TEMP);
114 1.1 jdolecek }
115 1.1 jdolecek
116 1.1 jdolecek if (itp[0]) {
117 1.1 jdolecek if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
118 1.1 jdolecek itp, &bp, 0)))
119 1.1 jdolecek return error;
120 1.1 jdolecek if ((error = copystr(bp, itp, MAXPATHLEN, &i)) != 0)
121 1.1 jdolecek return error;
122 1.1 jdolecek free((void *)bp, M_TEMP);
123 1.1 jdolecek }
124 1.1 jdolecek *pos = ELF_NO_ADDR;
125 1.1 jdolecek #ifdef DEBUG_FREEBSD_ELF
126 1.1 jdolecek printf("freebsd_elf32_probe: returning 0\n");
127 1.1 jdolecek #endif
128 1.1 jdolecek return 0;
129 1.1 jdolecek
130 1.1 jdolecek bad2:
131 1.1 jdolecek free(np, M_TEMP);
132 1.1 jdolecek bad1:
133 1.1 jdolecek free(ph, M_TEMP);
134 1.1 jdolecek return ENOEXEC;
135 1.1 jdolecek }
136