#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/exec.h>
#include <sys/imgact.h>
#include <sys/linker.h>
#include <sys/proc.h>
#include <sys/reg.h>
#include <sys/sysctl.h>
#include <sys/sysent.h>
#include <sys/imgact_elf.h>
#include <sys/syscall.h>
#include <sys/signalvar.h>
#include <sys/vnode.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_param.h>
#include <machine/elf.h>
#include <machine/md_var.h>
u_long elf_hwcap;
static struct sysentvec elf64_freebsd_sysvec = {
.sv_size = SYS_MAXSYSCALL,
.sv_table = sysent,
.sv_fixup = __elfN(freebsd_fixup),
.sv_sendsig = sendsig,
.sv_sigcode = sigcode,
.sv_szsigcode = &szsigcode,
.sv_name = "FreeBSD ELF64",
.sv_coredump = __elfN(coredump),
.sv_elf_core_osabi = ELFOSABI_FREEBSD,
.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
.sv_elf_core_prepare_notes = __elfN(prepare_notes),
.sv_minsigstksz = MINSIGSTKSZ,
.sv_minuser = VM_MIN_ADDRESS,
.sv_maxuser = 0,
.sv_usrstack = 0,
.sv_psstrings = 0,
.sv_psstringssz = sizeof(struct ps_strings),
.sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,
.sv_maxssiz = NULL,
.sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_TIMEKEEP |
SV_ASLR | SV_RNG_SEED_VER | SV_SIGSYS,
.sv_set_syscall_retval = cpu_set_syscall_retval,
.sv_fetch_syscall_args = cpu_fetch_syscall_args,
.sv_syscallnames = syscallnames,
.sv_shared_page_base = 0,
.sv_shared_page_len = PAGE_SIZE,
.sv_schedtail = NULL,
.sv_thread_detach = NULL,
.sv_trap = NULL,
.sv_hwcap = &elf_hwcap,
.sv_onexec_old = exec_onexec_old,
.sv_onexit = exit_onexit,
.sv_regset_begin = SET_BEGIN(__elfN(regset)),
.sv_regset_end = SET_LIMIT(__elfN(regset)),
};
INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
static const Elf64_Brandinfo freebsd_brand_info = {
.brand = ELFOSABI_FREEBSD,
.machine = EM_RISCV,
.compat_3_brand = "FreeBSD",
.interp_path = "/libexec/ld-elf.so.1",
.sysvec = &elf64_freebsd_sysvec,
.interp_newpath = NULL,
.brand_note = &elf64_freebsd_brandnote,
.flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
};
C_SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
(sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
static void
elf64_register_sysvec(void *arg)
{
struct sysentvec *sv;
sv = arg;
switch (pmap_mode) {
case PMAP_MODE_SV48:
sv->sv_maxuser = VM_MAX_USER_ADDRESS_SV48;
sv->sv_usrstack = USRSTACK_SV48;
sv->sv_psstrings = PS_STRINGS_SV48;
sv->sv_shared_page_base = SHAREDPAGE_SV48;
break;
case PMAP_MODE_SV39:
sv->sv_maxuser = VM_MAX_USER_ADDRESS_SV39;
sv->sv_usrstack = USRSTACK_SV39;
sv->sv_psstrings = PS_STRINGS_SV39;
sv->sv_shared_page_base = SHAREDPAGE_SV39;
break;
}
}
SYSINIT(elf64_register_sysvec, SI_SUB_VM, SI_ORDER_ANY, elf64_register_sysvec,
&elf64_freebsd_sysvec);
static bool debug_kld;
SYSCTL_BOOL(_debug, OID_AUTO, kld_reloc, CTLFLAG_RW, &debug_kld, 0,
"Activate debug prints in elf_reloc_internal()");
struct type2str_ent {
int type;
const char *str;
};
void
elf64_dump_thread(struct thread *td, void *dst, size_t *off)
{
}
static uint32_t
gen_bitmask(int msb, int lsb)
{
uint32_t mask;
if (msb == sizeof(mask) * 8 - 1)
mask = ~0;
else
mask = (1U << (msb + 1)) - 1;
if (lsb > 0)
mask &= ~((1U << lsb) - 1);
return (mask);
}
static uint32_t
extract_bits(uint32_t x, int msb, int lsb)
{
uint32_t mask;
mask = gen_bitmask(msb, lsb);
x &= mask;
x >>= lsb;
return (x);
}
static uint32_t
insert_bits(uint32_t d, uint32_t s, int msb, int lsb)
{
uint32_t mask;
mask = gen_bitmask(msb, lsb);
d &= ~mask;
s <<= lsb;
s &= mask;
return (d | s);
}
static uint32_t
insert_imm(uint32_t insn, uint32_t imm, int imm_msb, int imm_lsb,
int insn_lsb)
{
int insn_msb;
uint32_t v;
v = extract_bits(imm, imm_msb, imm_lsb);
insn_msb = (imm_msb - imm_lsb) + insn_lsb;
return (insert_bits(insn, v, insn_msb, insn_lsb));
}
static uint32_t
calc_hi20_imm(uint32_t value)
{
if ((value & 0x800) != 0)
value += 0x1000;
return (value & ~0xfff);
}
static const struct type2str_ent t2s[] = {
{ R_RISCV_NONE, "R_RISCV_NONE" },
{ R_RISCV_64, "R_RISCV_64" },
{ R_RISCV_JUMP_SLOT, "R_RISCV_JUMP_SLOT" },
{ R_RISCV_RELATIVE, "R_RISCV_RELATIVE" },
{ R_RISCV_JAL, "R_RISCV_JAL" },
{ R_RISCV_CALL, "R_RISCV_CALL" },
{ R_RISCV_PCREL_HI20, "R_RISCV_PCREL_HI20" },
{ R_RISCV_PCREL_LO12_I, "R_RISCV_PCREL_LO12_I" },
{ R_RISCV_PCREL_LO12_S, "R_RISCV_PCREL_LO12_S" },
{ R_RISCV_HI20, "R_RISCV_HI20" },
{ R_RISCV_LO12_I, "R_RISCV_LO12_I" },
{ R_RISCV_LO12_S, "R_RISCV_LO12_S" },
};
static const char *
reloctype_to_str(int type)
{
int i;
for (i = 0; i < sizeof(t2s) / sizeof(t2s[0]); ++i) {
if (type == t2s[i].type)
return t2s[i].str;
}
return "*unknown*";
}
bool
elf_is_ifunc_reloc(Elf_Size r_info)
{
return (ELF_R_TYPE(r_info) == R_RISCV_IRELATIVE);
}
static int
elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
int type, int local, elf_lookup_fn lookup)
{
Elf_Size rtype, symidx;
const Elf_Rela *rela;
Elf_Addr val, addr;
Elf64_Addr *where;
Elf_Addr addend;
uint32_t before32_1;
uint32_t before32;
uint64_t before64;
uint32_t *insn32p;
uint32_t imm20;
int error;
switch (type) {
case ELF_RELOC_RELA:
rela = (const Elf_Rela *)data;
where = (Elf_Addr *)(relocbase + rela->r_offset);
insn32p = (uint32_t *)where;
addend = rela->r_addend;
rtype = ELF_R_TYPE(rela->r_info);
symidx = ELF_R_SYM(rela->r_info);
break;
default:
printf("%s:%d unknown reloc type %d\n",
__FUNCTION__, __LINE__, type);
return (-1);
}
switch (rtype) {
case R_RISCV_NONE:
break;
case R_RISCV_64:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
before64 = *where;
*where = addr + addend;
if (debug_kld)
printf("%p %c %-24s %016lx -> %016lx\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before64, *where);
break;
case R_RISCV_JUMP_SLOT:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
before64 = *where;
*where = addr;
if (debug_kld)
printf("%p %c %-24s %016lx -> %016lx\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before64, *where);
break;
case R_RISCV_RELATIVE:
before64 = *where;
*where = elf_relocaddr(lf, relocbase + addend);
if (debug_kld)
printf("%p %c %-24s %016lx -> %016lx\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before64, *where);
break;
case R_RISCV_JAL:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
val = addr - (Elf_Addr)where;
if (val <= -(1UL << 20) || (1UL << 20) <= val) {
printf("kldload: huge offset against R_RISCV_JAL\n");
return (-1);
}
before32 = *insn32p;
*insn32p = insert_imm(*insn32p, val, 20, 20, 31);
*insn32p = insert_imm(*insn32p, val, 10, 1, 21);
*insn32p = insert_imm(*insn32p, val, 11, 11, 20);
*insn32p = insert_imm(*insn32p, val, 19, 12, 12);
if (debug_kld)
printf("%p %c %-24s %08x -> %08x\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before32, *insn32p);
break;
case R_RISCV_CALL:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
val = addr - (Elf_Addr)where;
if (val <= -(1UL << 32) || (1UL << 32) <= val) {
printf("kldload: huge offset against R_RISCV_CALL\n");
return (-1);
}
before32 = insn32p[0];
imm20 = calc_hi20_imm(val);
insn32p[0] = insert_imm(insn32p[0], imm20, 31, 12, 12);
before32_1 = insn32p[1];
insn32p[1] = insert_imm(insn32p[1], val, 11, 0, 20);
if (debug_kld)
printf("%p %c %-24s %08x %08x -> %08x %08x\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before32, insn32p[0], before32_1, insn32p[1]);
break;
case R_RISCV_PCREL_HI20:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
val = addr - (Elf_Addr)where;
insn32p = (uint32_t *)where;
before32 = *insn32p;
imm20 = calc_hi20_imm(val);
*insn32p = insert_imm(*insn32p, imm20, 31, 12, 12);
if (debug_kld)
printf("%p %c %-24s %08x -> %08x\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before32, *insn32p);
break;
case R_RISCV_PCREL_LO12_I:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
val = addr - (Elf_Addr)where;
insn32p = (uint32_t *)where;
before32 = *insn32p;
*insn32p = insert_imm(*insn32p, addr, 11, 0, 20);
if (debug_kld)
printf("%p %c %-24s %08x -> %08x\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before32, *insn32p);
break;
case R_RISCV_PCREL_LO12_S:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
val = addr - (Elf_Addr)where;
insn32p = (uint32_t *)where;
before32 = *insn32p;
*insn32p = insert_imm(*insn32p, addr, 11, 5, 25);
*insn32p = insert_imm(*insn32p, addr, 4, 0, 7);
if (debug_kld)
printf("%p %c %-24s %08x -> %08x\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before32, *insn32p);
break;
case R_RISCV_HI20:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
val = addr;
insn32p = (uint32_t *)where;
before32 = *insn32p;
imm20 = calc_hi20_imm(val);
*insn32p = insert_imm(*insn32p, imm20, 31, 12, 12);
if (debug_kld)
printf("%p %c %-24s %08x -> %08x\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before32, *insn32p);
break;
case R_RISCV_LO12_I:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
val = addr;
insn32p = (uint32_t *)where;
before32 = *insn32p;
*insn32p = insert_imm(*insn32p, addr, 11, 0, 20);
if (debug_kld)
printf("%p %c %-24s %08x -> %08x\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before32, *insn32p);
break;
case R_RISCV_LO12_S:
error = lookup(lf, symidx, 1, &addr);
if (error != 0)
return (-1);
val = addr;
insn32p = (uint32_t *)where;
before32 = *insn32p;
*insn32p = insert_imm(*insn32p, addr, 11, 5, 25);
*insn32p = insert_imm(*insn32p, addr, 4, 0, 7);
if (debug_kld)
printf("%p %c %-24s %08x -> %08x\n", where,
(local ? 'l' : 'g'), reloctype_to_str(rtype),
before32, *insn32p);
break;
case R_RISCV_IRELATIVE:
addr = relocbase + addend;
val = ((Elf64_Addr (*)(void))addr)();
if (*where != val)
*where = val;
break;
default:
printf("kldload: unexpected relocation type %ld, "
"symbol index %ld\n", rtype, symidx);
return (-1);
}
return (0);
}
int
elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
elf_lookup_fn lookup)
{
return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
}
int
elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
int type, elf_lookup_fn lookup)
{
return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
}
int
elf_cpu_load_file(linker_file_t lf __unused)
{
return (0);
}
int
elf_cpu_unload_file(linker_file_t lf __unused)
{
return (0);
}
int
elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
{
return (0);
}