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.1/sys/mips/mips/elf_machdep.c 203786 2010-02-11 18:34:06Z mjacob $");
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/syscall.h>
40 #include <sys/signalvar.h>
41 #include <sys/vnode.h>
42
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <vm/vm_param.h>
46
47 #include <machine/elf.h>
48 #include <machine/md_var.h>
49
50 struct sysentvec elf32_freebsd_sysvec = {
51 .sv_size = SYS_MAXSYSCALL,
52 .sv_table = sysent,
53 .sv_mask = 0,
54 .sv_sigsize = 0,
55 .sv_sigtbl = NULL,
56 .sv_errsize = 0,
57 .sv_errtbl = NULL,
58 .sv_transtrap = NULL,
59 .sv_fixup = __elfN(freebsd_fixup),
60 .sv_sendsig = sendsig,
61 .sv_sigcode = sigcode,
62 .sv_szsigcode = &szsigcode,
63 .sv_prepsyscall = NULL,
64 .sv_name = "FreeBSD ELF32",
65 .sv_coredump = __elfN(coredump),
66 .sv_imgact_try = NULL,
67 .sv_minsigstksz = MINSIGSTKSZ,
68 .sv_pagesize = PAGE_SIZE,
69 .sv_minuser = VM_MIN_ADDRESS,
70 .sv_maxuser = VM_MAXUSER_ADDRESS,
71 .sv_usrstack = USRSTACK,
72 .sv_psstrings = PS_STRINGS,
73 .sv_stackprot = VM_PROT_ALL,
74 .sv_copyout_strings = exec_copyout_strings,
75 .sv_setregs = exec_setregs,
76 .sv_fixlimit = NULL,
77 .sv_maxssiz = NULL,
78 .sv_flags = SV_ABI_FREEBSD | SV_ILP32
79 };
80
81 static Elf32_Brandinfo freebsd_brand_info = {
82 .brand = ELFOSABI_FREEBSD,
83 .machine = EM_MIPS,
84 .compat_3_brand = "FreeBSD",
85 .emul_path = NULL,
86 .interp_path = "/libexec/ld-elf.so.1",
87 .sysvec = &elf32_freebsd_sysvec,
88 .interp_newpath = NULL,
89 .brand_note = &elf32_freebsd_brandnote,
90 .flags = BI_BRAND_NOTE
91 };
92
93 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
94 (sysinit_cfunc_t) elf32_insert_brand_entry,
95 &freebsd_brand_info);
96
97 void
98 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
99 size_t *off __unused)
100 {
101 }
102
103 /* Process one elf relocation with addend. */
104 static int
105 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
106 int type, int local, elf_lookup_fn lookup)
107 {
108 Elf_Addr *where = (Elf_Addr *)NULL;
109 Elf_Addr addr;
110 Elf_Addr addend = (Elf_Addr)0;
111 Elf_Word rtype = (Elf_Word)0, symidx;
112 const Elf_Rel *rel;
113 const Elf_Rela *rela;
114
115 switch (type) {
116 case ELF_RELOC_REL:
117 rel = (const Elf_Rel *)data;
118 where = (Elf_Addr *) (relocbase + rel->r_offset);
119 addend = *where;
120 rtype = ELF_R_TYPE(rel->r_info);
121 symidx = ELF_R_SYM(rel->r_info);
122 break;
123 case ELF_RELOC_RELA:
124 rela = (const Elf_Rela *)data;
125 where = (Elf_Addr *) (relocbase + rela->r_offset);
126 addend = rela->r_addend;
127 rtype = ELF_R_TYPE(rela->r_info);
128 symidx = ELF_R_SYM(rela->r_info);
129 break;
130 default:
131 panic("unknown reloc type %d\n", type);
132 }
133
134 if (local) {
135 #if 0 /* TBD */
136 if (rtype == R_386_RELATIVE) { /* A + B */
137 addr = elf_relocaddr(lf, relocbase + addend);
138 if (*where != addr)
139 *where = addr;
140 }
141 return (0);
142 #endif
143 }
144
145 switch (rtype) {
146
147 case R_MIPS_NONE: /* none */
148 break;
149
150 case R_MIPS_16: /* S + sign-extend(A) */
151 /*
152 * There shouldn't be R_MIPS_16 relocs in kernel objects.
153 */
154 printf("kldload: unexpected R_MIPS_16 relocation\n");
155 return -1;
156 break;
157
158 case R_MIPS_32: /* S + A - P */
159 addr = lookup(lf, symidx, 1);
160 if (addr == 0)
161 return -1;
162 addr += addend;
163 if (*where != addr)
164 *where = addr;
165 break;
166
167 case R_MIPS_REL32: /* A - EA + S */
168 /*
169 * There shouldn't be R_MIPS_REL32 relocs in kernel objects?
170 */
171 printf("kldload: unexpected R_MIPS_REL32 relocation\n");
172 return -1;
173 break;
174
175 case R_MIPS_26: /* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
176 break;
177
178 case R_MIPS_HI16:
179 /* extern/local: ((AHL + S) - ((short)(AHL + S)) >> 16 */
180 /* _gp_disp: ((AHL + GP - P) - (short)(AHL + GP - P)) >> 16 */
181 break;
182
183 case R_MIPS_LO16:
184 /* extern/local: AHL + S */
185 /* _gp_disp: AHL + GP - P + 4 */
186 break;
187
188 case R_MIPS_GPREL16:
189 /* extern/local: ((AHL + S) - ((short)(AHL + S)) >> 16 */
190 /* _gp_disp: ((AHL + GP - P) - (short)(AHL + GP - P)) >> 16 */
191 break;
192
193 case R_MIPS_LITERAL: /* sign-extend(A) + L */
194 break;
195
196 case R_MIPS_GOT16: /* external: G */
197 /* local: tbd */
198 break;
199
200 case R_MIPS_PC16: /* sign-extend(A) + S - P */
201 break;
202
203 case R_MIPS_CALL16: /* G */
204 break;
205
206 case R_MIPS_GPREL32: /* A + S + GP0 - GP */
207 break;
208
209 case R_MIPS_GOTHI16: /* (G - (short)G) >> 16 + A */
210 break;
211
212 case R_MIPS_GOTLO16: /* G & 0xffff */
213 break;
214
215 case R_MIPS_CALLHI16: /* (G - (short)G) >> 16 + A */
216 break;
217
218 case R_MIPS_CALLLO16: /* G & 0xffff */
219 break;
220
221 default:
222 printf("kldload: unexpected relocation type %d\n",
223 rtype);
224 return (-1);
225 }
226 return(0);
227 }
228
229 int
230 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
231 elf_lookup_fn lookup)
232 {
233
234 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
235 }
236
237 int
238 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
239 int type, elf_lookup_fn lookup)
240 {
241
242 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
243 }
244
245 int
246 elf_cpu_load_file(linker_file_t lf __unused)
247 {
248
249 return (0);
250 }
251
252 int
253 elf_cpu_unload_file(linker_file_t lf __unused)
254 {
255
256 return (0);
257 }
Cache object: 75f2fba5967896518f4c610a15a4aded
|