1 /*-
2 * Copyright (c) 2019 Leandro Lupori
3 * Copyright (c) 2024 Jessica Clarke <jrtc27@FreeBSD.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
12 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
20 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21 * SUCH DAMAGE.
22 */
23
24 static unsigned long elf_hwcap;
25
26 static void
ifunc_init(const Elf_Auxinfo * aux)27 ifunc_init(const Elf_Auxinfo *aux)
28 {
29 /* Digest the auxiliary vector. */
30 for (; aux->a_type != AT_NULL; aux++) {
31 switch (aux->a_type) {
32 case AT_HWCAP:
33 elf_hwcap = (uint32_t)aux->a_un.a_val;
34 break;
35 }
36 }
37 }
38
39 static void
crt1_handle_rela(const Elf_Rela * r)40 crt1_handle_rela(const Elf_Rela *r)
41 {
42 typedef Elf_Addr (*ifunc_resolver_t)(
43 unsigned long, unsigned long, unsigned long, unsigned long,
44 unsigned long, unsigned long, unsigned long, unsigned long);
45 Elf_Addr *ptr, *where, target;
46
47 switch (ELF_R_TYPE(r->r_info)) {
48 case R_RISCV_IRELATIVE:
49 ptr = (Elf_Addr *)r->r_addend;
50 where = (Elf_Addr *)r->r_offset;
51 target = ((ifunc_resolver_t)ptr)(elf_hwcap,
52 0, 0, 0, 0, 0, 0, 0);
53 *where = target;
54 break;
55 }
56 }
57