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>
40 #include <sys/syscall.h>
41 #include <sys/signalvar.h>
42 #include <sys/vnode.h>
46 #include <vm/vm_param.h>
48 #include <machine/elf.h>
49 #include <machine/md_var.h>
50 #include <machine/cache.h>
53 struct sysentvec elf64_freebsd_sysvec = {
54 .sv_size = SYS_MAXSYSCALL,
60 .sv_fixup = __elfN(freebsd_fixup),
61 .sv_sendsig = sendsig,
62 .sv_sigcode = sigcode,
63 .sv_szsigcode = &szsigcode,
64 .sv_name = "FreeBSD ELF64",
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,
78 .sv_flags = SV_ABI_FREEBSD | SV_LP64,
79 .sv_set_syscall_retval = cpu_set_syscall_retval,
80 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
81 .sv_syscallnames = syscallnames,
83 .sv_thread_detach = NULL,
87 static Elf64_Brandinfo freebsd_brand_info = {
88 .brand = ELFOSABI_FREEBSD,
90 .compat_3_brand = "FreeBSD",
92 .interp_path = "/libexec/ld-elf.so.1",
93 .sysvec = &elf64_freebsd_sysvec,
94 .interp_newpath = NULL,
95 .brand_note = &elf64_freebsd_brandnote,
96 .flags = BI_BRAND_NOTE
99 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
100 (sysinit_cfunc_t) elf64_insert_brand_entry,
101 &freebsd_brand_info);
104 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
105 size_t *off __unused)
109 struct sysentvec elf32_freebsd_sysvec = {
110 .sv_size = SYS_MAXSYSCALL,
115 .sv_transtrap = NULL,
116 .sv_fixup = __elfN(freebsd_fixup),
117 .sv_sendsig = sendsig,
118 .sv_sigcode = sigcode,
119 .sv_szsigcode = &szsigcode,
120 .sv_name = "FreeBSD ELF32",
121 .sv_coredump = __elfN(coredump),
122 .sv_imgact_try = NULL,
123 .sv_minsigstksz = MINSIGSTKSZ,
124 .sv_pagesize = PAGE_SIZE,
125 .sv_minuser = VM_MIN_ADDRESS,
126 .sv_maxuser = VM_MAXUSER_ADDRESS,
127 .sv_usrstack = USRSTACK,
128 .sv_psstrings = PS_STRINGS,
129 .sv_stackprot = VM_PROT_ALL,
130 .sv_copyout_strings = exec_copyout_strings,
131 .sv_setregs = exec_setregs,
134 .sv_flags = SV_ABI_FREEBSD | SV_ILP32,
135 .sv_set_syscall_retval = cpu_set_syscall_retval,
136 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
137 .sv_syscallnames = syscallnames,
138 .sv_schedtail = NULL,
139 .sv_thread_detach = NULL,
143 static Elf32_Brandinfo freebsd_brand_info = {
144 .brand = ELFOSABI_FREEBSD,
146 .compat_3_brand = "FreeBSD",
148 .interp_path = "/libexec/ld-elf.so.1",
149 .sysvec = &elf32_freebsd_sysvec,
150 .interp_newpath = NULL,
151 .brand_note = &elf32_freebsd_brandnote,
152 .flags = BI_BRAND_NOTE
155 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
156 (sysinit_cfunc_t) elf32_insert_brand_entry,
157 &freebsd_brand_info);
160 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
161 size_t *off __unused)
167 * The following MIPS relocation code for tracking multiple
168 * consecutive HI32/LO32 entries is because of the following:
170 * https://dmz-portal.mips.com/wiki/MIPS_relocation_types
176 * An R_MIPS_HI16 must be followed eventually by an associated R_MIPS_LO16
177 * relocation record in the same SHT_REL section. The contents of the two
178 * fields to be relocated are combined to form a full 32-bit addend AHL.
179 * An R_MIPS_LO16 entry which does not immediately follow a R_MIPS_HI16 is
180 * combined with the most recent one encountered, i.e. multiple R_MIPS_LO16
181 * entries may be associated with a single R_MIPS_HI16. Use of these
182 * relocation types in a SHT_REL section is discouraged and may be
183 * forbidden to avoid this complication.
185 * A GNU extension allows multiple R_MIPS_HI16 records to share the same
186 * R_MIPS_LO16 relocation record(s). The association works like this within
187 * a single relocation section:
189 * + From the beginning of the section moving to the end of the section,
190 * until R_MIPS_LO16 is not found each found R_MIPS_HI16 relocation will
191 * be associated with the first R_MIPS_LO16.
193 * + Until another R_MIPS_HI16 record is found all found R_MIPS_LO16
194 * relocations found are associated with the last R_MIPS_HI16.
198 * This is so gcc can do dead code detection/removal without having to
199 * generate HI/LO pairs even if one of them would be deleted.
201 * So, the summary is:
203 * + A HI16 entry must occur before any LO16 entries;
204 * + Multiple consecutive HI16 RELA entries need to be buffered until the
205 * first LO16 RELA entry occurs - and then all HI16 RELA relocations use
206 * the offset in the LOW16 RELA for calculating their offsets;
207 * + The last HI16 RELA entry before a LO16 RELA entry is used (the AHL)
208 * for the first subsequent LO16 calculation;
209 * + If multiple consecutive LO16 RELA entries occur, only the first
210 * LO16 RELA entry triggers an update of buffered HI16 RELA entries;
211 * any subsequent LO16 RELA entry before another HI16 RELA entry will
212 * not cause any further updates to the HI16 RELA entries.
214 * Additionally, flush out any outstanding HI16 entries that don't have
215 * a LO16 entry in case some garbage entries are left in the file.
218 struct mips_tmp_reloc;
219 struct mips_tmp_reloc {
220 struct mips_tmp_reloc *next;
223 Elf32_Addr *where_hi16;
226 static struct mips_tmp_reloc *ml = NULL;
229 * Add a temporary relocation (ie, a HI16 reloc type.)
232 mips_tmp_reloc_add(Elf_Addr ahl, Elf32_Addr *where_hi16)
234 struct mips_tmp_reloc *r;
236 r = malloc(sizeof(struct mips_tmp_reloc), M_TEMP, M_NOWAIT);
238 printf("%s: failed to malloc\n", __func__);
243 r->where_hi16 = where_hi16;
251 * Flush the temporary relocation list.
253 * This should be done after a file is completely loaded
254 * so no stale relocations exist to confuse the next
258 mips_tmp_reloc_flush(void)
260 struct mips_tmp_reloc *r, *rn;
272 * Get an entry from the reloc list; or NULL if we've run out.
274 static struct mips_tmp_reloc *
275 mips_tmp_reloc_get(void)
277 struct mips_tmp_reloc *r;
287 * Free a relocation entry.
290 mips_tmp_reloc_free(struct mips_tmp_reloc *r)
296 /* Process one elf relocation with addend. */
298 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
299 int type, int local, elf_lookup_fn lookup)
301 Elf32_Addr *where = (Elf32_Addr *)NULL;
303 Elf_Addr addend = (Elf_Addr)0;
304 Elf_Word rtype = (Elf_Word)0, symidx;
305 struct mips_tmp_reloc *r;
306 const Elf_Rel *rel = NULL;
307 const Elf_Rela *rela = NULL;
310 /* Store the last seen ahl from a HI16 for LO16 processing */
311 static Elf_Addr last_ahl;
315 rel = (const Elf_Rel *)data;
316 where = (Elf32_Addr *) (relocbase + rel->r_offset);
317 rtype = ELF_R_TYPE(rel->r_info);
318 symidx = ELF_R_SYM(rel->r_info);
321 addend = *(Elf64_Addr *)where;
330 rela = (const Elf_Rela *)data;
331 where = (Elf32_Addr *) (relocbase + rela->r_offset);
332 addend = rela->r_addend;
333 rtype = ELF_R_TYPE(rela->r_info);
334 symidx = ELF_R_SYM(rela->r_info);
337 panic("unknown reloc type %d\n", type);
341 case R_MIPS_NONE: /* none */
344 case R_MIPS_32: /* S + A */
345 error = lookup(lf, symidx, 1, &addr);
350 *where = (Elf32_Addr)addr;
353 case R_MIPS_26: /* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
354 error = lookup(lf, symidx, 1, &addr);
358 addend &= 0x03ffffff;
360 * Addendum for .rela R_MIPS_26 is not shifted right
365 addr += ((Elf_Addr)where & 0xf0000000) | addend;
368 *where &= ~0x03ffffff;
369 *where |= addr & 0x03ffffff;
372 case R_MIPS_64: /* S + A */
373 error = lookup(lf, symidx, 1, &addr);
377 if (*(Elf64_Addr*)where != addr)
378 *(Elf64_Addr*)where = addr;
382 * Handle the two GNU extension cases:
384 * + Multiple HI16s followed by a LO16, and
385 * + A HI16 followed by multiple LO16s.
387 * The former is tricky - the HI16 relocations need
388 * to be buffered until a LO16 occurs, at which point
389 * each HI16 is replayed against the LO16 relocation entry
390 * (with the relevant overflow information.)
392 * The latter should be easy to handle - when the
393 * first LO16 is seen, write out and flush the
394 * HI16 buffer. Any subsequent LO16 entries will
395 * find a blank relocation buffer.
399 case R_MIPS_HI16: /* ((AHL + S) - ((short)(AHL + S)) >> 16 */
401 error = lookup(lf, symidx, 1, &addr);
405 *where &= 0xffff0000;
406 *where |= ((((long long) addr + 0x8000LL) >> 16) & 0xffff);
409 * Add a temporary relocation to the list;
410 * will pop it off / free the list when
411 * we've found a suitable HI16.
413 if (mips_tmp_reloc_add(addend << 16, where) == 0)
416 * Track the last seen HI16 AHL for use by
417 * the first LO16 AHL calculation.
419 * The assumption is any intermediary deleted
420 * LO16's were optimised out, so the last
421 * HI16 before the LO16 is the "true" relocation
422 * entry to use for that LO16 write.
424 last_ahl = addend << 16;
428 case R_MIPS_LO16: /* AHL + S */
430 error = lookup(lf, symidx, 1, &addr);
434 *where &= 0xffff0000;
435 *where |= addr & 0xffff;
440 tmp_ahl = last_ahl + (int16_t) addend;
441 error = lookup(lf, symidx, 1, &addr);
445 tmp_addend = addend & 0xffff0000;
447 /* Use the last seen ahl for calculating addend */
448 tmp_addend |= (uint16_t)(tmp_ahl + addr);
452 * This logic implements the "we saw multiple HI16
453 * before a LO16" assignment /and/ "we saw multiple
456 * Multiple LO16s will be handled as a blank
459 * Multple HI16's are iterated over here.
461 while ((r = mips_tmp_reloc_get()) != NULL) {
465 * We have the ahl from the HI16 entry, so
466 * offset it by the 16 bits of the low ahl.
469 rahl += (int16_t) addend;
471 tmp_addend = *(r->where_hi16);
472 tmp_addend &= 0xffff0000;
473 tmp_addend |= ((rahl + addr) -
474 (int16_t)(rahl + addr)) >> 16;
475 *(r->where_hi16) = tmp_addend;
476 mips_tmp_reloc_free(r);
482 case R_MIPS_HIGHER: /* %higher(A+S) */
483 error = lookup(lf, symidx, 1, &addr);
487 *where &= 0xffff0000;
488 *where |= (((long long)addr + 0x80008000LL) >> 32) & 0xffff;
491 case R_MIPS_HIGHEST: /* %highest(A+S) */
492 error = lookup(lf, symidx, 1, &addr);
496 *where &= 0xffff0000;
497 *where |= (((long long)addr + 0x800080008000LL) >> 48) & 0xffff;
501 printf("kldload: unexpected relocation type %d\n",
510 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
511 elf_lookup_fn lookup)
514 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
518 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
519 int type, elf_lookup_fn lookup)
522 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
526 elf_cpu_load_file(linker_file_t lf __unused)
530 * Sync the I and D caches to make sure our relocations are visible.
532 mips_icache_sync_all();
534 /* Flush outstanding relocations */
535 mips_tmp_reloc_flush();
541 elf_cpu_unload_file(linker_file_t lf __unused)