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 * $FreeBSD$
26 */
27
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
31
32 #define __ELF_WORD_SIZE 32
33
34 #include <sys/exec.h>
35 #include <sys/imgact.h>
36 #include <sys/malloc.h>
37 #include <sys/proc.h>
38 #include <sys/namei.h>
39 #include <sys/fcntl.h>
40 #include <sys/sysent.h>
41 #include <sys/imgact_elf.h>
42 #include <sys/syscall.h>
43 #include <sys/signalvar.h>
44 #include <sys/vnode.h>
45 #include <sys/linker.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49
50 #include <machine/altivec.h>
51 #include <machine/cpu.h>
52 #include <machine/elf.h>
53 #include <machine/reg.h>
54 #include <machine/md_var.h>
55
56 #ifdef __powerpc64__
57 #include <compat/freebsd32/freebsd32_proto.h>
58 #include <compat/freebsd32/freebsd32_util.h>
59
60 extern const char *freebsd32_syscallnames[];
61 #endif
62
63 struct sysentvec elf32_freebsd_sysvec = {
64 .sv_size = SYS_MAXSYSCALL,
65 #ifdef __powerpc64__
66 .sv_table = freebsd32_sysent,
67 #else
68 .sv_table = sysent,
69 #endif
70 .sv_mask = 0,
71 .sv_errsize = 0,
72 .sv_errtbl = NULL,
73 .sv_transtrap = NULL,
74 .sv_fixup = __elfN(freebsd_fixup),
75 .sv_sendsig = sendsig,
76 .sv_sigcode = sigcode32,
77 .sv_szsigcode = &szsigcode32,
78 .sv_name = "FreeBSD ELF32",
79 .sv_coredump = __elfN(coredump),
80 .sv_imgact_try = NULL,
81 .sv_minsigstksz = MINSIGSTKSZ,
82 .sv_pagesize = PAGE_SIZE,
83 .sv_minuser = VM_MIN_ADDRESS,
84 .sv_stackprot = VM_PROT_ALL,
85 #ifdef __powerpc64__
86 .sv_maxuser = VM_MAXUSER_ADDRESS,
87 .sv_usrstack = FREEBSD32_USRSTACK,
88 .sv_psstrings = FREEBSD32_PS_STRINGS,
89 .sv_copyout_strings = freebsd32_copyout_strings,
90 .sv_setregs = ppc32_setregs,
91 .sv_syscallnames = freebsd32_syscallnames,
92 #else
93 .sv_maxuser = VM_MAXUSER_ADDRESS,
94 .sv_usrstack = USRSTACK,
95 .sv_psstrings = PS_STRINGS,
96 .sv_copyout_strings = exec_copyout_strings,
97 .sv_setregs = exec_setregs,
98 .sv_syscallnames = syscallnames,
99 #endif
100 .sv_fixlimit = NULL,
101 .sv_maxssiz = NULL,
102 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP,
103 .sv_set_syscall_retval = cpu_set_syscall_retval,
104 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
105 .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
106 .sv_shared_page_len = PAGE_SIZE,
107 .sv_schedtail = NULL,
108 .sv_thread_detach = NULL,
109 .sv_trap = NULL,
110 };
111 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
112
113 static Elf32_Brandinfo freebsd_brand_info = {
114 .brand = ELFOSABI_FREEBSD,
115 .machine = EM_PPC,
116 .compat_3_brand = "FreeBSD",
117 .emul_path = NULL,
118 .interp_path = "/libexec/ld-elf.so.1",
119 .sysvec = &elf32_freebsd_sysvec,
120 #ifdef __powerpc64__
121 .interp_newpath = "/libexec/ld-elf32.so.1",
122 #else
123 .interp_newpath = NULL,
124 #endif
125 .brand_note = &elf32_freebsd_brandnote,
126 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
127 };
128
129 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
130 (sysinit_cfunc_t) elf32_insert_brand_entry,
131 &freebsd_brand_info);
132
133 static Elf32_Brandinfo freebsd_brand_oinfo = {
134 .brand = ELFOSABI_FREEBSD,
135 .machine = EM_PPC,
136 .compat_3_brand = "FreeBSD",
137 .emul_path = NULL,
138 .interp_path = "/usr/libexec/ld-elf.so.1",
139 .sysvec = &elf32_freebsd_sysvec,
140 .interp_newpath = NULL,
141 .brand_note = &elf32_freebsd_brandnote,
142 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
143 };
144
145 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
146 (sysinit_cfunc_t) elf32_insert_brand_entry,
147 &freebsd_brand_oinfo);
148
149 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
150
151 void
152 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
153 {
154 size_t len;
155 struct pcb *pcb;
156
157 len = 0;
158 pcb = td->td_pcb;
159 if (pcb->pcb_flags & PCB_VEC) {
160 save_vec_nodrop(td);
161 if (dst != NULL) {
162 len += elf32_populate_note(NT_PPC_VMX,
163 &pcb->pcb_vec, dst,
164 sizeof(pcb->pcb_vec), NULL);
165 } else
166 len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
167 sizeof(pcb->pcb_vec), NULL);
168 }
169 *off = len;
170 }
171
172 #ifndef __powerpc64__
173 bool
174 elf_is_ifunc_reloc(Elf_Size r_info __unused)
175 {
176
177 return (false);
178 }
179
180 /* Process one elf relocation with addend. */
181 static int
182 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
183 int type, int local, elf_lookup_fn lookup)
184 {
185 Elf_Addr *where;
186 Elf_Half *hwhere;
187 Elf_Addr addr;
188 Elf_Addr addend;
189 Elf_Word rtype, symidx;
190 const Elf_Rela *rela;
191 int error;
192
193 switch (type) {
194 case ELF_RELOC_REL:
195 panic("PPC only supports RELA relocations");
196 break;
197 case ELF_RELOC_RELA:
198 rela = (const Elf_Rela *)data;
199 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
200 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
201 addend = rela->r_addend;
202 rtype = ELF_R_TYPE(rela->r_info);
203 symidx = ELF_R_SYM(rela->r_info);
204 break;
205 default:
206 panic("elf_reloc: unknown relocation mode %d\n", type);
207 }
208
209 switch (rtype) {
210
211 case R_PPC_NONE:
212 break;
213
214 case R_PPC_ADDR32: /* word32 S + A */
215 error = lookup(lf, symidx, 1, &addr);
216 if (error != 0)
217 return -1;
218 *where = elf_relocaddr(lf, addr + addend);
219 break;
220
221 case R_PPC_ADDR16_LO: /* #lo(S) */
222 error = lookup(lf, symidx, 1, &addr);
223 if (error != 0)
224 return -1;
225 /*
226 * addend values are sometimes relative to sections
227 * (i.e. .rodata) in rela, where in reality they
228 * are relative to relocbase. Detect this condition.
229 */
230 if (addr > relocbase && addr <= (relocbase + addend))
231 addr = relocbase;
232 addr = elf_relocaddr(lf, addr + addend);
233 *hwhere = addr & 0xffff;
234 break;
235
236 case R_PPC_ADDR16_HA: /* #ha(S) */
237 error = lookup(lf, symidx, 1, &addr);
238 if (error != 0)
239 return -1;
240 /*
241 * addend values are sometimes relative to sections
242 * (i.e. .rodata) in rela, where in reality they
243 * are relative to relocbase. Detect this condition.
244 */
245 if (addr > relocbase && addr <= (relocbase + addend))
246 addr = relocbase;
247 addr = elf_relocaddr(lf, addr + addend);
248 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
249 & 0xffff;
250 break;
251
252 case R_PPC_RELATIVE: /* word32 B + A */
253 *where = elf_relocaddr(lf, relocbase + addend);
254 break;
255
256 default:
257 printf("kldload: unexpected relocation type %d\n",
258 (int) rtype);
259 return -1;
260 }
261 return(0);
262 }
263
264 void
265 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
266 {
267 Elf_Rela *rela = NULL, *relalim;
268 Elf_Addr relasz = 0;
269 Elf_Addr *where;
270
271 /*
272 * Extract the rela/relasz values from the dynamic section
273 */
274 for (; dynp->d_tag != DT_NULL; dynp++) {
275 switch (dynp->d_tag) {
276 case DT_RELA:
277 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
278 break;
279 case DT_RELASZ:
280 relasz = dynp->d_un.d_val;
281 break;
282 }
283 }
284
285 /*
286 * Relocate these values
287 */
288 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
289 for (; rela < relalim; rela++) {
290 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
291 continue;
292 where = (Elf_Addr *)(relocbase + rela->r_offset);
293 *where = (Elf_Addr)(relocbase + rela->r_addend);
294 }
295 }
296
297 int
298 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
299 elf_lookup_fn lookup)
300 {
301
302 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
303 }
304
305 int
306 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
307 int type, elf_lookup_fn lookup)
308 {
309
310 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
311 }
312
313 int
314 elf_cpu_load_file(linker_file_t lf)
315 {
316 /* Only sync the cache for non-kernel modules */
317 if (lf->id != 1)
318 __syncicache(lf->address, lf->size);
319 return (0);
320 }
321
322 int
323 elf_cpu_unload_file(linker_file_t lf __unused)
324 {
325
326 return (0);
327 }
328 #endif
Cache object: 3b2adcf5b444007d89f168c6d797003a
|