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: releng/8.2/sys/mips/mips/elf_machdep.c 215938 2010-11-27 12:26:40Z jchandra $");
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 };
83
84 static Elf64_Brandinfo freebsd_brand_info = {
85 .brand = ELFOSABI_FREEBSD,
86 .machine = EM_MIPS,
87 .compat_3_brand = "FreeBSD",
88 .emul_path = NULL,
89 .interp_path = "/libexec/ld-elf.so.1",
90 .sysvec = &elf64_freebsd_sysvec,
91 .interp_newpath = NULL,
92 .flags = 0
93 };
94
95 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
96 (sysinit_cfunc_t) elf64_insert_brand_entry,
97 &freebsd_brand_info);
98
99 void
100 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
101 size_t *off __unused)
102 {
103 }
104 #else
105 struct sysentvec elf32_freebsd_sysvec = {
106 .sv_size = SYS_MAXSYSCALL,
107 .sv_table = sysent,
108 .sv_mask = 0,
109 .sv_sigsize = 0,
110 .sv_sigtbl = NULL,
111 .sv_errsize = 0,
112 .sv_errtbl = NULL,
113 .sv_transtrap = NULL,
114 .sv_fixup = __elfN(freebsd_fixup),
115 .sv_sendsig = sendsig,
116 .sv_sigcode = sigcode,
117 .sv_szsigcode = &szsigcode,
118 .sv_prepsyscall = NULL,
119 .sv_name = "FreeBSD ELF32",
120 .sv_coredump = __elfN(coredump),
121 .sv_imgact_try = NULL,
122 .sv_minsigstksz = MINSIGSTKSZ,
123 .sv_pagesize = PAGE_SIZE,
124 .sv_minuser = VM_MIN_ADDRESS,
125 .sv_maxuser = VM_MAXUSER_ADDRESS,
126 .sv_usrstack = USRSTACK,
127 .sv_psstrings = PS_STRINGS,
128 .sv_stackprot = VM_PROT_ALL,
129 .sv_copyout_strings = exec_copyout_strings,
130 .sv_setregs = exec_setregs,
131 .sv_fixlimit = NULL,
132 .sv_maxssiz = NULL,
133 .sv_flags = SV_ABI_FREEBSD | SV_ILP32,
134 .sv_set_syscall_retval = cpu_set_syscall_retval,
135 .sv_fetch_syscall_args = NULL, /* XXXKIB */
136 .sv_syscallnames = syscallnames,
137 };
138
139 static Elf32_Brandinfo freebsd_brand_info = {
140 .brand = ELFOSABI_FREEBSD,
141 .machine = EM_MIPS,
142 .compat_3_brand = "FreeBSD",
143 .emul_path = NULL,
144 .interp_path = "/libexec/ld-elf.so.1",
145 .sysvec = &elf32_freebsd_sysvec,
146 .interp_newpath = NULL,
147 .flags = 0
148 };
149
150 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
151 (sysinit_cfunc_t) elf32_insert_brand_entry,
152 &freebsd_brand_info);
153
154 void
155 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
156 size_t *off __unused)
157 {
158 }
159 #endif
160
161 /* Process one elf relocation with addend. */
162 static int
163 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
164 int type, int local, elf_lookup_fn lookup)
165 {
166 Elf_Addr *where = (Elf_Addr *)NULL;
167 Elf_Addr addr;
168 Elf_Addr addend = (Elf_Addr)0;
169 Elf_Word rtype = (Elf_Word)0, symidx;
170 const Elf_Rel *rel;
171
172 /*
173 * Stash R_MIPS_HI16 info so we can use it when processing R_MIPS_LO16
174 */
175 static Elf_Addr ahl;
176 static Elf_Addr *where_hi16;
177
178 switch (type) {
179 case ELF_RELOC_REL:
180 rel = (const Elf_Rel *)data;
181 where = (Elf_Addr *) (relocbase + rel->r_offset);
182 addend = *where;
183 rtype = ELF_R_TYPE(rel->r_info);
184 symidx = ELF_R_SYM(rel->r_info);
185 break;
186 default:
187 panic("unknown reloc type %d\n", type);
188 }
189
190 switch (rtype) {
191 case R_MIPS_NONE: /* none */
192 break;
193
194 case R_MIPS_32: /* S + A */
195 addr = lookup(lf, symidx, 1);
196 if (addr == 0)
197 return (-1);
198 addr += addend;
199 if (*where != addr)
200 *where = addr;
201 break;
202
203 case R_MIPS_26: /* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
204 addr = lookup(lf, symidx, 1);
205 if (addr == 0)
206 return (-1);
207
208 addend &= 0x03ffffff;
209 addend <<= 2;
210
211 addr += ((Elf_Addr)where & 0xf0000000) | addend;
212 addr >>= 2;
213
214 *where &= ~0x03ffffff;
215 *where |= addr & 0x03ffffff;
216 break;
217
218 case R_MIPS_HI16: /* ((AHL + S) - ((short)(AHL + S)) >> 16 */
219 ahl = addend << 16;
220 where_hi16 = where;
221 break;
222
223 case R_MIPS_LO16: /* AHL + S */
224 ahl += (int16_t)addend;
225 addr = lookup(lf, symidx, 1);
226 if (addr == 0)
227 return (-1);
228
229 addend &= 0xffff0000;
230 addend |= (uint16_t)(ahl + addr);
231 *where = addend;
232
233 addend = *where_hi16;
234 addend &= 0xffff0000;
235 addend |= ((ahl + addr) - (int16_t)(ahl + addr)) >> 16;
236 *where_hi16 = addend;
237 break;
238
239 default:
240 printf("kldload: unexpected relocation type %d\n",
241 rtype);
242 return (-1);
243 }
244 return(0);
245 }
246
247 int
248 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
249 elf_lookup_fn lookup)
250 {
251
252 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
253 }
254
255 int
256 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
257 int type, elf_lookup_fn lookup)
258 {
259
260 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
261 }
262
263 int
264 elf_cpu_load_file(linker_file_t lf __unused)
265 {
266
267 /*
268 * Sync the I and D caches to make sure our relocations are visible.
269 */
270 mips_icache_sync_all();
271
272 return (0);
273 }
274
275 int
276 elf_cpu_unload_file(linker_file_t lf __unused)
277 {
278
279 return (0);
280 }
Cache object: bfc9e8349b79c6608402a1c2c4da6eac
|