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>
61 struct sysentvec elf64_freebsd_sysvec = {
62 .sv_size = SYS_MAXSYSCALL,
68 .sv_fixup = __elfN(freebsd_fixup),
69 .sv_sendsig = sendsig,
70 .sv_sigcode = sigcode,
71 .sv_szsigcode = &szsigcode,
72 .sv_name = "FreeBSD ELF64",
73 .sv_coredump = __elfN(coredump),
74 .sv_imgact_try = NULL,
75 .sv_minsigstksz = MINSIGSTKSZ,
76 .sv_pagesize = PAGE_SIZE,
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_ALL,
82 .sv_copyout_strings = exec_copyout_strings,
83 .sv_setregs = exec_setregs,
86 .sv_flags = SV_ABI_FREEBSD | SV_LP64,
87 .sv_set_syscall_retval = cpu_set_syscall_retval,
88 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
89 .sv_syscallnames = syscallnames,
91 .sv_thread_detach = NULL,
94 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
96 static Elf64_Brandinfo freebsd_brand_info = {
97 .brand = ELFOSABI_FREEBSD,
99 .compat_3_brand = "FreeBSD",
101 .interp_path = "/libexec/ld-elf.so.1",
102 .sysvec = &elf64_freebsd_sysvec,
103 .interp_newpath = NULL,
104 .brand_note = &elf64_freebsd_brandnote,
105 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
108 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
109 (sysinit_cfunc_t) elf64_insert_brand_entry,
110 &freebsd_brand_info);
112 static Elf64_Brandinfo freebsd_brand_oinfo = {
113 .brand = ELFOSABI_FREEBSD,
115 .compat_3_brand = "FreeBSD",
117 .interp_path = "/usr/libexec/ld-elf.so.1",
118 .sysvec = &elf64_freebsd_sysvec,
119 .interp_newpath = NULL,
120 .brand_note = &elf64_freebsd_brandnote,
121 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
124 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
125 (sysinit_cfunc_t) elf64_insert_brand_entry,
126 &freebsd_brand_oinfo);
128 static int debug_kld;
129 SYSCTL_INT(_kern, OID_AUTO, debug_kld,
130 CTLFLAG_RW, &debug_kld, 0,
131 "Activate debug prints in elf_reloc_internal()");
133 struct type2str_ent {
139 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
145 * Following 4 functions are used to manupilate bits on 32bit interger value.
146 * FIXME: I implemetend for ease-to-understand rather than for well-optimized.
149 gen_bitmask(int msb, int lsb)
153 if (msb == sizeof(mask) * 8 - 1)
156 mask = (1U << (msb + 1)) - 1;
159 mask &= ~((1U << lsb) - 1);
165 extract_bits(uint32_t x, int msb, int lsb)
169 mask = gen_bitmask(msb, lsb);
178 insert_bits(uint32_t d, uint32_t s, int msb, int lsb)
182 mask = gen_bitmask(msb, lsb);
193 insert_imm(uint32_t insn, uint32_t imm, int imm_msb, int imm_lsb,
199 v = extract_bits(imm, imm_msb, imm_lsb);
200 insn_msb = (imm_msb - imm_lsb) + insn_lsb;
202 return (insert_bits(insn, v, insn_msb, insn_lsb));
206 * The RISC-V ISA is designed so that all of immediate values are
208 * An immediate value is sometimes generated at runtime by adding
209 * 12bit sign integer and 20bit signed integer. This requests 20bit
210 * immediate value to be ajusted if the MSB of the 12bit immediate
211 * value is asserted (sign-extended value is treated as negative value).
213 * For example, 0x123800 can be calculated by adding upper 20 bit of
214 * 0x124000 and sign-extended 12bit immediate whose bit pattern is
218 * = (0x123000 + 0x1000) + (-0x1000 + 0x800)
219 * = (0x123000 + 0x1000) + (0xff...ff800)
220 * = 0x124000 + sign-extention(0x800)
223 calc_hi20_imm(uint32_t value)
226 * There is the arithmetical hack that can remove conditional
227 * statement. But I implement it in straightforward way.
229 if ((value & 0x800) != 0)
231 return (value & ~0xfff);
234 static const struct type2str_ent t2s[] = {
235 { R_RISCV_NONE, "R_RISCV_NONE" },
236 { R_RISCV_64, "R_RISCV_64" },
237 { R_RISCV_JUMP_SLOT, "R_RISCV_JUMP_SLOT" },
238 { R_RISCV_RELATIVE, "R_RISCV_RELATIVE" },
239 { R_RISCV_JAL, "R_RISCV_JAL" },
240 { R_RISCV_CALL, "R_RISCV_CALL" },
241 { R_RISCV_PCREL_HI20, "R_RISCV_PCREL_HI20" },
242 { R_RISCV_PCREL_LO12_I, "R_RISCV_PCREL_LO12_I" },
243 { R_RISCV_PCREL_LO12_S, "R_RISCV_PCREL_LO12_S" },
244 { R_RISCV_HI20, "R_RISCV_HI20" },
245 { R_RISCV_LO12_I, "R_RISCV_LO12_I" },
246 { R_RISCV_LO12_S, "R_RISCV_LO12_S" },
250 reloctype_to_str(int type)
254 for (i = 0; i < sizeof(t2s) / sizeof(t2s[0]); ++i) {
255 if (type == t2s[i].type)
263 elf_is_ifunc_reloc(Elf_Size r_info __unused)
270 * Currently kernel loadable module for RISCV is compiled with -fPIC option.
271 * (see also additional CFLAGS definition for RISCV in sys/conf/kmod.mk)
272 * Only R_RISCV_64, R_RISCV_JUMP_SLOT and RISCV_RELATIVE are emitted in
273 * the module. Other relocations will be processed when kernel loadable
274 * modules are built in non-PIC.
276 * FIXME: only RISCV64 is supported.
279 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
280 int type, int local, elf_lookup_fn lookup)
282 Elf_Size rtype, symidx;
283 const Elf_Rela *rela;
296 rela = (const Elf_Rela *)data;
297 where = (Elf_Addr *)(relocbase + rela->r_offset);
298 insn32p = (uint32_t*)where;
299 addend = rela->r_addend;
300 rtype = ELF_R_TYPE(rela->r_info);
301 symidx = ELF_R_SYM(rela->r_info);
304 printf("%s:%d unknown reloc type %d\n",
305 __FUNCTION__, __LINE__, type);
314 case R_RISCV_JUMP_SLOT:
315 error = lookup(lf, symidx, 1, &addr);
325 printf("%p %c %-24s %016lx -> %016lx\n",
328 reloctype_to_str(rtype),
332 case R_RISCV_RELATIVE:
335 *where = elf_relocaddr(lf, relocbase + addend);
338 printf("%p %c %-24s %016lx -> %016lx\n",
341 reloctype_to_str(rtype),
346 error = lookup(lf, symidx, 1, &addr);
350 val = addr - (Elf_Addr)where;
351 if ((val <= -(1UL << 20) || (1UL << 20) <= val)) {
352 printf("kldload: huge offset against R_RISCV_JAL\n");
357 *insn32p = insert_imm(*insn32p, val, 20, 20, 31);
358 *insn32p = insert_imm(*insn32p, val, 10, 1, 21);
359 *insn32p = insert_imm(*insn32p, val, 11, 11, 20);
360 *insn32p = insert_imm(*insn32p, val, 19, 12, 12);
363 printf("%p %c %-24s %08x -> %08x\n",
366 reloctype_to_str(rtype),
372 * R_RISCV_CALL relocates 8-byte region that consists
373 * of the sequence of AUIPC and JALR.
375 /* calculate and check the pc relative offset. */
376 error = lookup(lf, symidx, 1, &addr);
379 val = addr - (Elf_Addr)where;
380 if ((val <= -(1UL << 32) || (1UL << 32) <= val)) {
381 printf("kldload: huge offset against R_RISCV_CALL\n");
385 /* Relocate AUIPC. */
386 before32 = insn32p[0];
387 imm20 = calc_hi20_imm(val);
388 insn32p[0] = insert_imm(insn32p[0], imm20, 31, 12, 12);
391 before32_1 = insn32p[1];
392 insn32p[1] = insert_imm(insn32p[1], val, 11, 0, 20);
395 printf("%p %c %-24s %08x %08x -> %08x %08x\n",
398 reloctype_to_str(rtype),
399 before32, insn32p[0],
400 before32_1, insn32p[1]);
403 case R_RISCV_PCREL_HI20:
404 val = addr - (Elf_Addr)where;
405 insn32p = (uint32_t*)where;
407 imm20 = calc_hi20_imm(val);
408 *insn32p = insert_imm(*insn32p, imm20, 31, 12, 12);
411 printf("%p %c %-24s %08x -> %08x\n",
414 reloctype_to_str(rtype),
418 case R_RISCV_PCREL_LO12_I:
419 val = addr - (Elf_Addr)where;
420 insn32p = (uint32_t*)where;
422 *insn32p = insert_imm(*insn32p, addr, 11, 0, 20);
425 printf("%p %c %-24s %08x -> %08x\n",
428 reloctype_to_str(rtype),
432 case R_RISCV_PCREL_LO12_S:
433 val = addr - (Elf_Addr)where;
434 insn32p = (uint32_t*)where;
436 *insn32p = insert_imm(*insn32p, addr, 11, 5, 25);
437 *insn32p = insert_imm(*insn32p, addr, 4, 0, 7);
439 printf("%p %c %-24s %08x -> %08x\n",
442 reloctype_to_str(rtype),
447 error = lookup(lf, symidx, 1, &addr);
451 insn32p = (uint32_t*)where;
453 imm20 = calc_hi20_imm(val);
454 *insn32p = insert_imm(*insn32p, imm20, 31, 12, 12);
457 printf("%p %c %-24s %08x -> %08x\n",
460 reloctype_to_str(rtype),
465 error = lookup(lf, symidx, 1, &addr);
470 insn32p = (uint32_t*)where;
472 *insn32p = insert_imm(*insn32p, addr, 11, 0, 20);
475 printf("%p %c %-24s %08x -> %08x\n",
478 reloctype_to_str(rtype),
483 error = lookup(lf, symidx, 1, &addr);
488 insn32p = (uint32_t*)where;
490 *insn32p = insert_imm(*insn32p, addr, 11, 5, 25);
491 *insn32p = insert_imm(*insn32p, addr, 4, 0, 7);
494 printf("%p %c %-24s %08x -> %08x\n",
497 reloctype_to_str(rtype),
502 printf("kldload: unexpected relocation type %ld\n", rtype);
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)
533 elf_cpu_unload_file(linker_file_t lf __unused)