2 * Copyright 1996-1998 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
32 #include <sys/imgact.h>
33 #include <sys/malloc.h>
35 #include <sys/namei.h>
36 #include <sys/fcntl.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 #include <sys/linker.h>
45 #include <vm/vm_param.h>
47 #include <machine/cpu.h>
48 #include <machine/elf.h>
49 #include <machine/md_var.h>
51 struct sysentvec elf32_freebsd_sysvec = {
60 __elfN(freebsd_fixup),
80 static Elf32_Brandinfo freebsd_brand_info = {
85 "/libexec/ld-elf.so.1",
86 &elf32_freebsd_sysvec,
90 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY,
91 (sysinit_cfunc_t) elf32_insert_brand_entry,
94 static Elf32_Brandinfo freebsd_brand_oinfo = {
99 "/usr/libexec/ld-elf.so.1",
100 &elf32_freebsd_sysvec,
104 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
105 (sysinit_cfunc_t) elf32_insert_brand_entry,
106 &freebsd_brand_oinfo);
110 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
111 size_t *off __unused)
116 /* Process one elf relocation with addend. */
118 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
119 int type, int local, elf_lookup_fn lookup)
125 Elf_Word rtype, symidx;
126 const Elf_Rela *rela;
130 panic("PPC only supports RELA relocations");
133 rela = (const Elf_Rela *)data;
134 where = (Elf_Addr *) (relocbase + rela->r_offset);
135 hwhere = (Elf_Half *) (relocbase + rela->r_offset);
136 addend = rela->r_addend;
137 rtype = ELF_R_TYPE(rela->r_info);
138 symidx = ELF_R_SYM(rela->r_info);
141 panic("elf_reloc: unknown relocation mode %d\n", type);
149 case R_PPC_ADDR32: /* word32 S + A */
150 addr = lookup(lf, symidx, 1);
157 case R_PPC_ADDR16_LO: /* #lo(S) */
158 addr = lookup(lf, symidx, 1);
162 * addend values are sometimes relative to sections
163 * (i.e. .rodata) in rela, where in reality they
164 * are relative to relocbase. Detect this condition.
166 if (addr > relocbase && addr <= (relocbase + addend))
167 addr = relocbase + addend;
170 *hwhere = addr & 0xffff;
173 case R_PPC_ADDR16_HA: /* #ha(S) */
174 addr = lookup(lf, symidx, 1);
178 * addend values are sometimes relative to sections
179 * (i.e. .rodata) in rela, where in reality they
180 * are relative to relocbase. Detect this condition.
182 if (addr > relocbase && addr <= (relocbase + addend))
183 addr = relocbase + addend;
186 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
190 case R_PPC_RELATIVE: /* word32 B + A */
191 *where = relocbase + addend;
195 printf("kldload: unexpected relocation type %d\n",
203 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
204 elf_lookup_fn lookup)
207 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
211 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
212 int type, elf_lookup_fn lookup)
215 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
219 elf_cpu_load_file(linker_file_t lf)
221 /* Only sync the cache for non-kernel modules */
223 __syncicache(lf->address, lf->size);
228 elf_cpu_unload_file(linker_file_t lf __unused)