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