2 * Copyright 1996-1998 John D. Polstra.
3 * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
4 * Copyright (c) 2016 Yukishige Shibata <y-shibat@mtd.biglobe.ne.jp>
7 * Portions of this software were developed by SRI International and the
8 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
9 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
11 * Portions of this software were developed by the University of Cambridge
12 * Computer Laboratory as part of the CTSRD Project, with support from the
13 * UK Higher Education Innovation Fund (HEIF).
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
44 #include <sys/imgact.h>
45 #include <sys/linker.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysent.h>
49 #include <sys/imgact_elf.h>
50 #include <sys/syscall.h>
51 #include <sys/signalvar.h>
52 #include <sys/vnode.h>
56 #include <vm/vm_param.h>
58 #include <machine/elf.h>
59 #include <machine/md_var.h>
63 struct sysentvec elf64_freebsd_sysvec = {
64 .sv_size = SYS_MAXSYSCALL,
69 .sv_fixup = __elfN(freebsd_fixup),
70 .sv_sendsig = sendsig,
71 .sv_sigcode = sigcode,
72 .sv_szsigcode = &szsigcode,
73 .sv_name = "FreeBSD ELF64",
74 .sv_coredump = __elfN(coredump),
75 .sv_imgact_try = NULL,
76 .sv_minsigstksz = MINSIGSTKSZ,
77 .sv_minuser = VM_MIN_ADDRESS,
78 .sv_maxuser = VM_MAXUSER_ADDRESS,
79 .sv_usrstack = USRSTACK,
80 .sv_psstrings = PS_STRINGS,
81 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
82 .sv_copyout_strings = exec_copyout_strings,
83 .sv_setregs = exec_setregs,
86 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR,
87 .sv_set_syscall_retval = cpu_set_syscall_retval,
88 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
89 .sv_syscallnames = syscallnames,
90 .sv_shared_page_base = SHAREDPAGE,
91 .sv_shared_page_len = PAGE_SIZE,
93 .sv_thread_detach = NULL,
95 .sv_hwcap = &elf_hwcap,
97 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
99 static Elf64_Brandinfo freebsd_brand_info = {
100 .brand = ELFOSABI_FREEBSD,
102 .compat_3_brand = "FreeBSD",
104 .interp_path = "/libexec/ld-elf.so.1",
105 .sysvec = &elf64_freebsd_sysvec,
106 .interp_newpath = NULL,
107 .brand_note = &elf64_freebsd_brandnote,
108 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
111 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
112 (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
114 static bool debug_kld;
115 SYSCTL_BOOL(_debug, OID_AUTO, kld_reloc, CTLFLAG_RW, &debug_kld, 0,
116 "Activate debug prints in elf_reloc_internal()");
118 struct type2str_ent {
124 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
130 * Following 4 functions are used to manupilate bits on 32bit interger value.
131 * FIXME: I implemetend for ease-to-understand rather than for well-optimized.
134 gen_bitmask(int msb, int lsb)
138 if (msb == sizeof(mask) * 8 - 1)
141 mask = (1U << (msb + 1)) - 1;
144 mask &= ~((1U << lsb) - 1);
150 extract_bits(uint32_t x, int msb, int lsb)
154 mask = gen_bitmask(msb, lsb);
163 insert_bits(uint32_t d, uint32_t s, int msb, int lsb)
167 mask = gen_bitmask(msb, lsb);
178 insert_imm(uint32_t insn, uint32_t imm, int imm_msb, int imm_lsb,
184 v = extract_bits(imm, imm_msb, imm_lsb);
185 insn_msb = (imm_msb - imm_lsb) + insn_lsb;
187 return (insert_bits(insn, v, insn_msb, insn_lsb));
191 * The RISC-V ISA is designed so that all of immediate values are
193 * An immediate value is sometimes generated at runtime by adding
194 * 12bit sign integer and 20bit signed integer. This requests 20bit
195 * immediate value to be ajusted if the MSB of the 12bit immediate
196 * value is asserted (sign-extended value is treated as negative value).
198 * For example, 0x123800 can be calculated by adding upper 20 bit of
199 * 0x124000 and sign-extended 12bit immediate whose bit pattern is
203 * = (0x123000 + 0x1000) + (-0x1000 + 0x800)
204 * = (0x123000 + 0x1000) + (0xff...ff800)
205 * = 0x124000 + sign-extention(0x800)
208 calc_hi20_imm(uint32_t value)
211 * There is the arithmetical hack that can remove conditional
212 * statement. But I implement it in straightforward way.
214 if ((value & 0x800) != 0)
216 return (value & ~0xfff);
219 static const struct type2str_ent t2s[] = {
220 { R_RISCV_NONE, "R_RISCV_NONE" },
221 { R_RISCV_64, "R_RISCV_64" },
222 { R_RISCV_JUMP_SLOT, "R_RISCV_JUMP_SLOT" },
223 { R_RISCV_RELATIVE, "R_RISCV_RELATIVE" },
224 { R_RISCV_JAL, "R_RISCV_JAL" },
225 { R_RISCV_CALL, "R_RISCV_CALL" },
226 { R_RISCV_PCREL_HI20, "R_RISCV_PCREL_HI20" },
227 { R_RISCV_PCREL_LO12_I, "R_RISCV_PCREL_LO12_I" },
228 { R_RISCV_PCREL_LO12_S, "R_RISCV_PCREL_LO12_S" },
229 { R_RISCV_HI20, "R_RISCV_HI20" },
230 { R_RISCV_LO12_I, "R_RISCV_LO12_I" },
231 { R_RISCV_LO12_S, "R_RISCV_LO12_S" },
235 reloctype_to_str(int type)
239 for (i = 0; i < sizeof(t2s) / sizeof(t2s[0]); ++i) {
240 if (type == t2s[i].type)
248 elf_is_ifunc_reloc(Elf_Size r_info __unused)
255 * Currently kernel loadable module for RISCV is compiled with -fPIC option.
256 * (see also additional CFLAGS definition for RISCV in sys/conf/kmod.mk)
257 * Only R_RISCV_64, R_RISCV_JUMP_SLOT and RISCV_RELATIVE are emitted in
258 * the module. Other relocations will be processed when kernel loadable
259 * modules are built in non-PIC.
261 * FIXME: only RISCV64 is supported.
264 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
265 int type, int local, elf_lookup_fn lookup)
267 Elf_Size rtype, symidx;
268 const Elf_Rela *rela;
281 rela = (const Elf_Rela *)data;
282 where = (Elf_Addr *)(relocbase + rela->r_offset);
283 insn32p = (uint32_t *)where;
284 addend = rela->r_addend;
285 rtype = ELF_R_TYPE(rela->r_info);
286 symidx = ELF_R_SYM(rela->r_info);
289 printf("%s:%d unknown reloc type %d\n",
290 __FUNCTION__, __LINE__, type);
299 case R_RISCV_JUMP_SLOT:
300 error = lookup(lf, symidx, 1, &addr);
309 printf("%p %c %-24s %016lx -> %016lx\n", where,
310 (local ? 'l' : 'g'), reloctype_to_str(rtype),
314 case R_RISCV_RELATIVE:
316 *where = elf_relocaddr(lf, relocbase + addend);
318 printf("%p %c %-24s %016lx -> %016lx\n", where,
319 (local ? 'l' : 'g'), reloctype_to_str(rtype),
324 error = lookup(lf, symidx, 1, &addr);
328 val = addr - (Elf_Addr)where;
329 if (val <= -(1UL << 20) || (1UL << 20) <= val) {
330 printf("kldload: huge offset against R_RISCV_JAL\n");
335 *insn32p = insert_imm(*insn32p, val, 20, 20, 31);
336 *insn32p = insert_imm(*insn32p, val, 10, 1, 21);
337 *insn32p = insert_imm(*insn32p, val, 11, 11, 20);
338 *insn32p = insert_imm(*insn32p, val, 19, 12, 12);
340 printf("%p %c %-24s %08x -> %08x\n", where,
341 (local ? 'l' : 'g'), reloctype_to_str(rtype),
347 * R_RISCV_CALL relocates 8-byte region that consists
348 * of the sequence of AUIPC and JALR.
350 /* Calculate and check the pc relative offset. */
351 error = lookup(lf, symidx, 1, &addr);
355 val = addr - (Elf_Addr)where;
356 if (val <= -(1UL << 32) || (1UL << 32) <= val) {
357 printf("kldload: huge offset against R_RISCV_CALL\n");
361 /* Relocate AUIPC. */
362 before32 = insn32p[0];
363 imm20 = calc_hi20_imm(val);
364 insn32p[0] = insert_imm(insn32p[0], imm20, 31, 12, 12);
367 before32_1 = insn32p[1];
368 insn32p[1] = insert_imm(insn32p[1], val, 11, 0, 20);
370 printf("%p %c %-24s %08x %08x -> %08x %08x\n", where,
371 (local ? 'l' : 'g'), reloctype_to_str(rtype),
372 before32, insn32p[0], before32_1, insn32p[1]);
375 case R_RISCV_PCREL_HI20:
376 error = lookup(lf, symidx, 1, &addr);
380 val = addr - (Elf_Addr)where;
381 insn32p = (uint32_t *)where;
383 imm20 = calc_hi20_imm(val);
384 *insn32p = insert_imm(*insn32p, imm20, 31, 12, 12);
386 printf("%p %c %-24s %08x -> %08x\n", where,
387 (local ? 'l' : 'g'), reloctype_to_str(rtype),
391 case R_RISCV_PCREL_LO12_I:
392 error = lookup(lf, symidx, 1, &addr);
396 val = addr - (Elf_Addr)where;
397 insn32p = (uint32_t *)where;
399 *insn32p = insert_imm(*insn32p, addr, 11, 0, 20);
401 printf("%p %c %-24s %08x -> %08x\n", where,
402 (local ? 'l' : 'g'), reloctype_to_str(rtype),
406 case R_RISCV_PCREL_LO12_S:
407 error = lookup(lf, symidx, 1, &addr);
411 val = addr - (Elf_Addr)where;
412 insn32p = (uint32_t *)where;
414 *insn32p = insert_imm(*insn32p, addr, 11, 5, 25);
415 *insn32p = insert_imm(*insn32p, addr, 4, 0, 7);
417 printf("%p %c %-24s %08x -> %08x\n", where,
418 (local ? 'l' : 'g'), reloctype_to_str(rtype),
423 error = lookup(lf, symidx, 1, &addr);
428 insn32p = (uint32_t *)where;
430 imm20 = calc_hi20_imm(val);
431 *insn32p = insert_imm(*insn32p, imm20, 31, 12, 12);
433 printf("%p %c %-24s %08x -> %08x\n", where,
434 (local ? 'l' : 'g'), reloctype_to_str(rtype),
439 error = lookup(lf, symidx, 1, &addr);
444 insn32p = (uint32_t *)where;
446 *insn32p = insert_imm(*insn32p, addr, 11, 0, 20);
448 printf("%p %c %-24s %08x -> %08x\n", where,
449 (local ? 'l' : 'g'), reloctype_to_str(rtype),
454 error = lookup(lf, symidx, 1, &addr);
459 insn32p = (uint32_t *)where;
461 *insn32p = insert_imm(*insn32p, addr, 11, 5, 25);
462 *insn32p = insert_imm(*insn32p, addr, 4, 0, 7);
464 printf("%p %c %-24s %08x -> %08x\n", where,
465 (local ? 'l' : 'g'), reloctype_to_str(rtype),
470 printf("kldload: unexpected relocation type %ld\n", rtype);
478 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
479 elf_lookup_fn lookup)
482 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
486 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
487 int type, elf_lookup_fn lookup)
490 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
494 elf_cpu_load_file(linker_file_t lf __unused)
501 elf_cpu_unload_file(linker_file_t lf __unused)