]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/mips/mips/elf_machdep.c
MFC r362623:
[FreeBSD/stable/8.git] / sys / mips / mips / 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  *      from: src/sys/i386/i386/elf_machdep.c,v 1.20 2004/08/11 02:35:05 marcel
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/exec.h>
35 #include <sys/imgact.h>
36 #include <sys/linker.h>
37 #include <sys/sysent.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/proc.h>
40 #include <sys/syscall.h>
41 #include <sys/signalvar.h>
42 #include <sys/vnode.h>
43
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_param.h>
47
48 #include <machine/elf.h>
49 #include <machine/md_var.h>
50 #include <machine/cache.h>
51
52 #ifdef __mips_n64
53 struct sysentvec elf64_freebsd_sysvec = {
54         .sv_size        = SYS_MAXSYSCALL,
55         .sv_table       = sysent,
56         .sv_mask        = 0,
57         .sv_sigsize     = 0,
58         .sv_sigtbl      = NULL,
59         .sv_errsize     = 0,
60         .sv_errtbl      = NULL,
61         .sv_transtrap   = NULL,
62         .sv_fixup       = __elfN(freebsd_fixup),
63         .sv_sendsig     = sendsig,
64         .sv_sigcode     = sigcode,
65         .sv_szsigcode   = &szsigcode,
66         .sv_prepsyscall = NULL,
67         .sv_name        = "FreeBSD ELF64",
68         .sv_coredump    = __elfN(coredump),
69         .sv_imgact_try  = NULL,
70         .sv_minsigstksz = MINSIGSTKSZ,
71         .sv_pagesize    = PAGE_SIZE,
72         .sv_minuser     = VM_MIN_ADDRESS,
73         .sv_maxuser     = VM_MAXUSER_ADDRESS,
74         .sv_usrstack    = USRSTACK,
75         .sv_psstrings   = PS_STRINGS,
76         .sv_stackprot   = VM_PROT_ALL,
77         .sv_copyout_strings = exec_copyout_strings,
78         .sv_setregs     = exec_setregs,
79         .sv_fixlimit    = NULL,
80         .sv_maxssiz     = NULL,
81         .sv_flags       = SV_ABI_FREEBSD | SV_LP64,
82         .sv_schedtail   = NULL,
83 };
84
85 static Elf64_Brandinfo freebsd_brand_info = {
86         .brand          = ELFOSABI_FREEBSD,
87         .machine        = EM_MIPS,
88         .compat_3_brand = "FreeBSD",
89         .emul_path      = NULL,
90         .interp_path    = "/libexec/ld-elf.so.1",
91         .sysvec         = &elf64_freebsd_sysvec,
92         .interp_newpath = NULL,
93         .flags          = 0
94 };
95
96 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
97     (sysinit_cfunc_t) elf64_insert_brand_entry,
98     &freebsd_brand_info);
99
100 void
101 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
102     size_t *off __unused)
103 {
104 }
105 #else
106 struct sysentvec elf32_freebsd_sysvec = {
107         .sv_size        = SYS_MAXSYSCALL,
108         .sv_table       = sysent,
109         .sv_mask        = 0,
110         .sv_sigsize     = 0,
111         .sv_sigtbl      = NULL,
112         .sv_errsize     = 0,
113         .sv_errtbl      = NULL,
114         .sv_transtrap   = NULL,
115         .sv_fixup       = __elfN(freebsd_fixup),
116         .sv_sendsig     = sendsig,
117         .sv_sigcode     = sigcode,
118         .sv_szsigcode   = &szsigcode,
119         .sv_prepsyscall = NULL,
120         .sv_name        = "FreeBSD ELF32",
121         .sv_coredump    = __elfN(coredump),
122         .sv_imgact_try  = NULL,
123         .sv_minsigstksz = MINSIGSTKSZ,
124         .sv_pagesize    = PAGE_SIZE,
125         .sv_minuser     = VM_MIN_ADDRESS,
126         .sv_maxuser     = VM_MAXUSER_ADDRESS,
127         .sv_usrstack    = USRSTACK,
128         .sv_psstrings   = PS_STRINGS,
129         .sv_stackprot   = VM_PROT_ALL,
130         .sv_copyout_strings = exec_copyout_strings,
131         .sv_setregs     = exec_setregs,
132         .sv_fixlimit    = NULL,
133         .sv_maxssiz     = NULL,
134         .sv_flags       = SV_ABI_FREEBSD | SV_ILP32,
135         .sv_set_syscall_retval = cpu_set_syscall_retval,
136         .sv_fetch_syscall_args = NULL, /* XXXKIB */
137         .sv_syscallnames = syscallnames,
138         .sv_schedtail   = NULL,
139 };
140
141 static Elf32_Brandinfo freebsd_brand_info = {
142         .brand          = ELFOSABI_FREEBSD,
143         .machine        = EM_MIPS,
144         .compat_3_brand = "FreeBSD",
145         .emul_path      = NULL,
146         .interp_path    = "/libexec/ld-elf.so.1",
147         .sysvec         = &elf32_freebsd_sysvec,
148         .interp_newpath = NULL,
149         .flags          = 0
150 };
151
152 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
153     (sysinit_cfunc_t) elf32_insert_brand_entry,
154     &freebsd_brand_info);
155
156 void
157 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
158     size_t *off __unused)
159 {
160 }
161 #endif
162
163 /* Process one elf relocation with addend. */
164 static int
165 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
166     int type, int local, elf_lookup_fn lookup)
167 {
168         Elf_Addr *where = (Elf_Addr *)NULL;
169         Elf_Addr addr;
170         Elf_Addr addend = (Elf_Addr)0;
171         Elf_Word rtype = (Elf_Word)0, symidx;
172         const Elf_Rel *rel;
173
174         /*
175          * Stash R_MIPS_HI16 info so we can use it when processing R_MIPS_LO16
176          */
177         static Elf_Addr ahl;
178         static Elf_Addr *where_hi16;
179
180         switch (type) {
181         case ELF_RELOC_REL:
182                 rel = (const Elf_Rel *)data;
183                 where = (Elf_Addr *) (relocbase + rel->r_offset);
184                 addend = *where;
185                 rtype = ELF_R_TYPE(rel->r_info);
186                 symidx = ELF_R_SYM(rel->r_info);
187                 break;
188         default:
189                 panic("unknown reloc type %d\n", type);
190         }
191
192         switch (rtype) {
193         case R_MIPS_NONE:       /* none */
194                 break;
195
196         case R_MIPS_32:         /* S + A */
197                 addr = lookup(lf, symidx, 1);
198                 if (addr == 0)
199                         return (-1);
200                 addr += addend;
201                 if (*where != addr)
202                         *where = addr;
203                 break;
204
205         case R_MIPS_26:         /* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
206                 addr = lookup(lf, symidx, 1);
207                 if (addr == 0)
208                         return (-1);
209
210                 addend &= 0x03ffffff;
211                 addend <<= 2;
212
213                 addr += ((Elf_Addr)where & 0xf0000000) | addend;
214                 addr >>= 2;
215
216                 *where &= ~0x03ffffff;
217                 *where |= addr & 0x03ffffff;
218                 break;
219
220         case R_MIPS_HI16:       /* ((AHL + S) - ((short)(AHL + S)) >> 16 */
221                 ahl = addend << 16;
222                 where_hi16 = where;
223                 break;
224
225         case R_MIPS_LO16:       /* AHL + S */
226                 ahl += (int16_t)addend;
227                 addr = lookup(lf, symidx, 1);
228                 if (addr == 0)
229                         return (-1);
230
231                 addend &= 0xffff0000;
232                 addend |= (uint16_t)(ahl + addr);
233                 *where = addend;
234
235                 addend = *where_hi16;
236                 addend &= 0xffff0000;
237                 addend |= ((ahl + addr) - (int16_t)(ahl + addr)) >> 16;
238                 *where_hi16 = addend;
239                 break;
240
241         default:
242                 printf("kldload: unexpected relocation type %d\n",
243                         rtype);
244                 return (-1);
245         }
246         return(0);
247 }
248
249 int
250 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
251     elf_lookup_fn lookup)
252 {
253
254         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
255 }
256
257 int
258 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
259     int type, elf_lookup_fn lookup)
260 {
261
262         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
263 }
264
265 int
266 elf_cpu_load_file(linker_file_t lf __unused)
267 {
268
269         /*
270          * Sync the I and D caches to make sure our relocations are visible.
271          */
272         mips_icache_sync_all();
273
274         return (0);
275 }
276
277 int
278 elf_cpu_unload_file(linker_file_t lf __unused)
279 {
280
281         return (0);
282 }