]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/i386/i386/elf_machdep.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / i386 / i386 / elf_machdep.c
1 /*-
2  * Copyright 1996-1998 John D. Polstra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/exec.h>
33 #include <sys/imgact.h>
34 #include <sys/linker.h>
35 #include <sys/proc.h>
36 #include <sys/sysent.h>
37 #include <sys/imgact_elf.h>
38 #include <sys/syscall.h>
39 #include <sys/signalvar.h>
40 #include <sys/vnode.h>
41
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 #include <vm/vm_param.h>
45
46 #include <machine/elf.h>
47 #include <machine/md_var.h>
48
49 struct sysentvec elf32_freebsd_sysvec = {
50         .sv_size        = SYS_MAXSYSCALL,
51         .sv_table       = sysent,
52         .sv_mask        = 0,
53         .sv_sigsize     = 0,
54         .sv_sigtbl      = NULL,
55         .sv_errsize     = 0,
56         .sv_errtbl      = NULL,
57         .sv_transtrap   = NULL,
58         .sv_fixup       = __elfN(freebsd_fixup),
59         .sv_sendsig     = sendsig,
60         .sv_sigcode     = sigcode,
61         .sv_szsigcode   = &szsigcode,
62         .sv_prepsyscall = NULL,
63         .sv_name        = "FreeBSD ELF32",
64         .sv_coredump    = __elfN(coredump),
65         .sv_imgact_try  = NULL,
66         .sv_minsigstksz = MINSIGSTKSZ,
67         .sv_pagesize    = PAGE_SIZE,
68         .sv_minuser     = VM_MIN_ADDRESS,
69         .sv_maxuser     = VM_MAXUSER_ADDRESS,
70         .sv_usrstack    = USRSTACK,
71         .sv_psstrings   = PS_STRINGS,
72         .sv_stackprot   = VM_PROT_ALL,
73         .sv_copyout_strings     = exec_copyout_strings,
74         .sv_setregs     = exec_setregs,
75         .sv_fixlimit    = NULL,
76         .sv_maxssiz     = NULL,
77         .sv_flags       = SV_ABI_FREEBSD | SV_IA32 | SV_ILP32,
78         .sv_set_syscall_retval = cpu_set_syscall_retval,
79         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
80         .sv_syscallnames = syscallnames,
81 };
82
83 static Elf32_Brandinfo freebsd_brand_info = {
84         .brand          = ELFOSABI_FREEBSD,
85         .machine        = EM_386,
86         .compat_3_brand = "FreeBSD",
87         .emul_path      = NULL,
88         .interp_path    = "/libexec/ld-elf.so.1",
89         .sysvec         = &elf32_freebsd_sysvec,
90         .interp_newpath = NULL,
91         .brand_note     = &elf32_freebsd_brandnote,
92         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
93 };
94
95 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
96         (sysinit_cfunc_t) elf32_insert_brand_entry,
97         &freebsd_brand_info);
98
99 static Elf32_Brandinfo freebsd_brand_oinfo = {
100         .brand          = ELFOSABI_FREEBSD,
101         .machine        = EM_386,
102         .compat_3_brand = "FreeBSD",
103         .emul_path      = NULL,
104         .interp_path    = "/usr/libexec/ld-elf.so.1",
105         .sysvec         = &elf32_freebsd_sysvec,
106         .interp_newpath = NULL,
107         .brand_note     = &elf32_freebsd_brandnote,
108         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
109 };
110
111 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
112         (sysinit_cfunc_t) elf32_insert_brand_entry,
113         &freebsd_brand_oinfo);
114
115 static Elf32_Brandinfo kfreebsd_brand_info = {
116         .brand          = ELFOSABI_FREEBSD,
117         .machine        = EM_386,
118         .compat_3_brand = "FreeBSD",
119         .emul_path      = NULL,
120         .interp_path    = "/lib/ld.so.1",
121         .sysvec         = &elf32_freebsd_sysvec,
122         .interp_newpath = NULL,
123         .brand_note     = &elf32_kfreebsd_brandnote,
124         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
125 };
126
127 SYSINIT(kelf32, SI_SUB_EXEC, SI_ORDER_ANY,
128         (sysinit_cfunc_t) elf32_insert_brand_entry,
129         &kfreebsd_brand_info);
130
131
132 void
133 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
134     size_t *off __unused)
135 {
136 }
137
138
139 /* Process one elf relocation with addend. */
140 static int
141 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
142     int type, int local, elf_lookup_fn lookup)
143 {
144         Elf_Addr *where;
145         Elf_Addr addr;
146         Elf_Addr addend;
147         Elf_Word rtype, symidx;
148         const Elf_Rel *rel;
149         const Elf_Rela *rela;
150
151         switch (type) {
152         case ELF_RELOC_REL:
153                 rel = (const Elf_Rel *)data;
154                 where = (Elf_Addr *) (relocbase + rel->r_offset);
155                 addend = *where;
156                 rtype = ELF_R_TYPE(rel->r_info);
157                 symidx = ELF_R_SYM(rel->r_info);
158                 break;
159         case ELF_RELOC_RELA:
160                 rela = (const Elf_Rela *)data;
161                 where = (Elf_Addr *) (relocbase + rela->r_offset);
162                 addend = rela->r_addend;
163                 rtype = ELF_R_TYPE(rela->r_info);
164                 symidx = ELF_R_SYM(rela->r_info);
165                 break;
166         default:
167                 panic("unknown reloc type %d\n", type);
168         }
169
170         if (local) {
171                 if (rtype == R_386_RELATIVE) {  /* A + B */
172                         addr = elf_relocaddr(lf, relocbase + addend);
173                         if (*where != addr)
174                                 *where = addr;
175                 }
176                 return (0);
177         }
178
179         switch (rtype) {
180
181                 case R_386_NONE:        /* none */
182                         break;
183
184                 case R_386_32:          /* S + A */
185                         addr = lookup(lf, symidx, 1);
186                         if (addr == 0)
187                                 return -1;
188                         addr += addend;
189                         if (*where != addr)
190                                 *where = addr;
191                         break;
192
193                 case R_386_PC32:        /* S + A - P */
194                         addr = lookup(lf, symidx, 1);
195                         if (addr == 0)
196                                 return -1;
197                         addr += addend - (Elf_Addr)where;
198                         if (*where != addr)
199                                 *where = addr;
200                         break;
201
202                 case R_386_COPY:        /* none */
203                         /*
204                          * There shouldn't be copy relocations in kernel
205                          * objects.
206                          */
207                         printf("kldload: unexpected R_COPY relocation\n");
208                         return -1;
209                         break;
210
211                 case R_386_GLOB_DAT:    /* S */
212                         addr = lookup(lf, symidx, 1);
213                         if (addr == 0)
214                                 return -1;
215                         if (*where != addr)
216                                 *where = addr;
217                         break;
218
219                 case R_386_RELATIVE:
220                         break;
221
222                 default:
223                         printf("kldload: unexpected relocation type %d\n",
224                                rtype);
225                         return -1;
226         }
227         return(0);
228 }
229
230 int
231 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
232     elf_lookup_fn lookup)
233 {
234
235         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
236 }
237
238 int
239 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
240     int type, elf_lookup_fn lookup)
241 {
242
243         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
244 }
245
246 int
247 elf_cpu_load_file(linker_file_t lf __unused)
248 {
249
250         return (0);
251 }
252
253 int
254 elf_cpu_unload_file(linker_file_t lf __unused)
255 {
256
257         return (0);
258 }