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.
25 * from: src/sys/i386/i386/elf_machdep.c,v 1.20 2004/08/11 02:35:05 marcel
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.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>
45 #include <vm/vm_param.h>
47 #include <machine/elf.h>
48 #include <machine/md_var.h>
50 struct sysentvec elf32_freebsd_sysvec = {
51 .sv_size = SYS_MAXSYSCALL,
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,
80 static Elf32_Brandinfo freebsd_brand_info = {
81 .brand = ELFOSABI_FREEBSD,
83 .compat_3_brand = "FreeBSD",
85 .interp_path = "/libexec/ld-elf.so.1",
86 .sysvec = &elf32_freebsd_sysvec,
87 .interp_newpath = NULL,
91 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY,
92 (sysinit_cfunc_t) elf32_insert_brand_entry,
95 static Elf32_Brandinfo freebsd_brand_oinfo = {
96 .brand = ELFOSABI_FREEBSD,
98 .compat_3_brand = "FreeBSD",
100 .interp_path = "/usr/libexec/ld-elf.so.1",
101 .sysvec = &elf32_freebsd_sysvec,
102 .interp_newpath = NULL,
106 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
107 (sysinit_cfunc_t) elf32_insert_brand_entry,
108 &freebsd_brand_oinfo);
112 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
113 size_t *off __unused)
117 /* Process one elf relocation with addend. */
119 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
120 int type, int local, elf_lookup_fn lookup)
122 Elf_Addr *where = (Elf_Addr *)NULL;;
124 Elf_Addr addend = (Elf_Addr)0;
125 Elf_Word rtype = (Elf_Word)0, symidx;
127 const Elf_Rela *rela;
131 rel = (const Elf_Rel *)data;
132 where = (Elf_Addr *) (relocbase + rel->r_offset);
134 rtype = ELF_R_TYPE(rel->r_info);
135 symidx = ELF_R_SYM(rel->r_info);
138 rela = (const Elf_Rela *)data;
139 where = (Elf_Addr *) (relocbase + rela->r_offset);
140 addend = rela->r_addend;
141 rtype = ELF_R_TYPE(rela->r_info);
142 symidx = ELF_R_SYM(rela->r_info);
145 panic("unknown reloc type %d\n", type);
150 if (rtype == R_386_RELATIVE) { /* A + B */
151 addr = relocbase + addend;
161 case R_MIPS_NONE: /* none */
164 case R_MIPS_16: /* S + sign-extend(A) */
166 * There shouldn't be R_MIPS_16 relocs in kernel objects.
168 printf("kldload: unexpected R_MIPS_16 relocation\n");
172 case R_MIPS_32: /* S + A - P */
173 addr = lookup(lf, symidx, 1);
181 case R_MIPS_REL32: /* A - EA + S */
183 * There shouldn't be R_MIPS_REL32 relocs in kernel objects?
185 printf("kldload: unexpected R_MIPS_REL32 relocation\n");
189 case R_MIPS_26: /* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
193 /* extern/local: ((AHL + S) - ((short)(AHL + S)) >> 16 */
194 /* _gp_disp: ((AHL + GP - P) - (short)(AHL + GP - P)) >> 16 */
198 /* extern/local: AHL + S */
199 /* _gp_disp: AHL + GP - P + 4 */
203 /* extern/local: ((AHL + S) - ((short)(AHL + S)) >> 16 */
204 /* _gp_disp: ((AHL + GP - P) - (short)(AHL + GP - P)) >> 16 */
207 case R_MIPS_LITERAL: /* sign-extend(A) + L */
210 case R_MIPS_GOT16: /* external: G */
214 case R_MIPS_PC16: /* sign-extend(A) + S - P */
217 case R_MIPS_CALL16: /* G */
220 case R_MIPS_GPREL32: /* A + S + GP0 - GP */
223 case R_MIPS_GOTHI16: /* (G - (short)G) >> 16 + A */
226 case R_MIPS_GOTLO16: /* G & 0xffff */
229 case R_MIPS_CALLHI16: /* (G - (short)G) >> 16 + A */
232 case R_MIPS_CALLLO16: /* G & 0xffff */
236 printf("kldload: unexpected relocation type %d\n",
244 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
245 elf_lookup_fn lookup)
248 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
252 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
253 int type, elf_lookup_fn lookup)
256 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
260 elf_cpu_load_file(linker_file_t lf __unused)
267 elf_cpu_unload_file(linker_file_t lf __unused)