1*968bcca2SKa Ho Ng /*-
2*968bcca2SKa Ho Ng * SPDX-License-Identifier: BSD-2-Clause
3*968bcca2SKa Ho Ng *
4*968bcca2SKa Ho Ng * Copyright (c) 2018 John Baldwin <jhb@FreeBSD.org>
5*968bcca2SKa Ho Ng *
6*968bcca2SKa Ho Ng * This software was developed by SRI International and the University of
7*968bcca2SKa Ho Ng * Cambridge Computer Laboratory (Department of Computer Science and
8*968bcca2SKa Ho Ng * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9*968bcca2SKa Ho Ng * DARPA SSITH research programme.
10*968bcca2SKa Ho Ng *
11*968bcca2SKa Ho Ng * Redistribution and use in source and binary forms, with or without
12*968bcca2SKa Ho Ng * modification, are permitted provided that the following conditions
13*968bcca2SKa Ho Ng * are met:
14*968bcca2SKa Ho Ng * 1. Redistributions of source code must retain the above copyright
15*968bcca2SKa Ho Ng * notice, this list of conditions and the following disclaimer.
16*968bcca2SKa Ho Ng * 2. Redistributions in binary form must reproduce the above copyright
17*968bcca2SKa Ho Ng * notice, this list of conditions and the following disclaimer in the
18*968bcca2SKa Ho Ng * documentation and/or other materials provided with the distribution.
19*968bcca2SKa Ho Ng *
20*968bcca2SKa Ho Ng * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*968bcca2SKa Ho Ng * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*968bcca2SKa Ho Ng * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*968bcca2SKa Ho Ng * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*968bcca2SKa Ho Ng * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*968bcca2SKa Ho Ng * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*968bcca2SKa Ho Ng * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*968bcca2SKa Ho Ng * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*968bcca2SKa Ho Ng * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*968bcca2SKa Ho Ng * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*968bcca2SKa Ho Ng * SUCH DAMAGE.
31*968bcca2SKa Ho Ng */
32*968bcca2SKa Ho Ng
33*968bcca2SKa Ho Ng #include <sys/endian.h>
34*968bcca2SKa Ho Ng
35*968bcca2SKa Ho Ng #include <err.h>
36*968bcca2SKa Ho Ng #include <errno.h>
37*968bcca2SKa Ho Ng #include <gelf.h>
38*968bcca2SKa Ho Ng
39*968bcca2SKa Ho Ng #include "kldelf.h"
40*968bcca2SKa Ho Ng
41*968bcca2SKa Ho Ng /*
42*968bcca2SKa Ho Ng * Apply relocations to the values obtained from the file. `relbase' is the
43*968bcca2SKa Ho Ng * target relocation address of the section, and `dataoff/len' is the region
44*968bcca2SKa Ho Ng * that is to be relocated, and has been copied to *dest
45*968bcca2SKa Ho Ng */
46*968bcca2SKa Ho Ng static int
ef_riscv_reloc(struct elf_file * ef,const void * reldata,Elf_Type reltype,GElf_Addr relbase,GElf_Addr dataoff,size_t len,void * dest)47*968bcca2SKa Ho Ng ef_riscv_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype,
48*968bcca2SKa Ho Ng GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest)
49*968bcca2SKa Ho Ng {
50*968bcca2SKa Ho Ng char *where;
51*968bcca2SKa Ho Ng GElf_Addr addr, addend;
52*968bcca2SKa Ho Ng GElf_Size rtype, symidx;
53*968bcca2SKa Ho Ng const GElf_Rela *rela;
54*968bcca2SKa Ho Ng
55*968bcca2SKa Ho Ng switch (reltype) {
56*968bcca2SKa Ho Ng case ELF_T_RELA:
57*968bcca2SKa Ho Ng rela = (const GElf_Rela *)reldata;
58*968bcca2SKa Ho Ng where = (char *)dest + (relbase + rela->r_offset - dataoff);
59*968bcca2SKa Ho Ng addend = rela->r_addend;
60*968bcca2SKa Ho Ng rtype = GELF_R_TYPE(rela->r_info);
61*968bcca2SKa Ho Ng symidx = GELF_R_SYM(rela->r_info);
62*968bcca2SKa Ho Ng break;
63*968bcca2SKa Ho Ng default:
64*968bcca2SKa Ho Ng return (EINVAL);
65*968bcca2SKa Ho Ng }
66*968bcca2SKa Ho Ng
67*968bcca2SKa Ho Ng if (where < (char *)dest || where >= (char *)dest + len)
68*968bcca2SKa Ho Ng return (0);
69*968bcca2SKa Ho Ng
70*968bcca2SKa Ho Ng switch (rtype) {
71*968bcca2SKa Ho Ng case R_RISCV_64: /* S + A */
72*968bcca2SKa Ho Ng addr = EF_SYMADDR(ef, symidx) + addend;
73*968bcca2SKa Ho Ng le64enc(where, addr);
74*968bcca2SKa Ho Ng break;
75*968bcca2SKa Ho Ng case R_RISCV_RELATIVE: /* B + A */
76*968bcca2SKa Ho Ng addr = relbase + addend;
77*968bcca2SKa Ho Ng le64enc(where, addr);
78*968bcca2SKa Ho Ng break;
79*968bcca2SKa Ho Ng default:
80*968bcca2SKa Ho Ng warnx("unhandled relocation type %d", (int)rtype);
81*968bcca2SKa Ho Ng }
82*968bcca2SKa Ho Ng return (0);
83*968bcca2SKa Ho Ng }
84*968bcca2SKa Ho Ng
85*968bcca2SKa Ho Ng ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_RISCV, ef_riscv_reloc);
86