xref: /freebsd/lib/libc/csu/powerpc64/reloc.c (revision 559a218c)
151015e6dSKonstantin Belousov /*-
251015e6dSKonstantin Belousov  * Copyright (c) 2019 Leandro Lupori
351015e6dSKonstantin Belousov  *
451015e6dSKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
551015e6dSKonstantin Belousov  * modification, are permitted provided that the following conditions
651015e6dSKonstantin Belousov  * are met:
751015e6dSKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
851015e6dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
951015e6dSKonstantin Belousov  *
1051015e6dSKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1151015e6dSKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1251015e6dSKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1351015e6dSKonstantin Belousov  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1451015e6dSKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1551015e6dSKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1651015e6dSKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1751015e6dSKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1851015e6dSKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1951015e6dSKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2051015e6dSKonstantin Belousov  * SUCH DAMAGE.
2151015e6dSKonstantin Belousov  */
2251015e6dSKonstantin Belousov 
2351015e6dSKonstantin Belousov static uint32_t cpu_features;
2451015e6dSKonstantin Belousov static uint32_t cpu_features2;
2551015e6dSKonstantin Belousov 
2651015e6dSKonstantin Belousov static void
init_cpu_features(char ** env)2751015e6dSKonstantin Belousov init_cpu_features(char **env)
2851015e6dSKonstantin Belousov {
2951015e6dSKonstantin Belousov 	const Elf_Auxinfo *aux;
3051015e6dSKonstantin Belousov 
3151015e6dSKonstantin Belousov 	/* Find the auxiliary vector on the stack. */
3251015e6dSKonstantin Belousov 	while (*env++ != 0)	/* Skip over environment, and NULL terminator */
3351015e6dSKonstantin Belousov 		;
3451015e6dSKonstantin Belousov 	aux = (const Elf_Auxinfo *)env;
3551015e6dSKonstantin Belousov 
3651015e6dSKonstantin Belousov 	/* Digest the auxiliary vector. */
3751015e6dSKonstantin Belousov 	for (;  aux->a_type != AT_NULL; aux++) {
3851015e6dSKonstantin Belousov 		switch (aux->a_type) {
3951015e6dSKonstantin Belousov 		case AT_HWCAP:
4051015e6dSKonstantin Belousov 			cpu_features = (uint32_t)aux->a_un.a_val;
4151015e6dSKonstantin Belousov 			break;
4251015e6dSKonstantin Belousov 		case AT_HWCAP2:
4351015e6dSKonstantin Belousov 			cpu_features2 = (uint32_t)aux->a_un.a_val;
4451015e6dSKonstantin Belousov 			break;
4551015e6dSKonstantin Belousov 		}
4651015e6dSKonstantin Belousov 	}
4751015e6dSKonstantin Belousov }
4851015e6dSKonstantin Belousov 
4951015e6dSKonstantin Belousov static void
crt1_handle_rela(const Elf_Rela * r)5051015e6dSKonstantin Belousov crt1_handle_rela(const Elf_Rela *r)
5151015e6dSKonstantin Belousov {
5251015e6dSKonstantin Belousov 	typedef Elf_Addr (*ifunc_resolver_t)(
5351015e6dSKonstantin Belousov 	    uint32_t, uint32_t, uint64_t, uint64_t,
5451015e6dSKonstantin Belousov 	    uint64_t, uint64_t, uint64_t, uint64_t);
5551015e6dSKonstantin Belousov 	Elf_Addr *ptr, *where, target;
5651015e6dSKonstantin Belousov 
5751015e6dSKonstantin Belousov 	switch (ELF_R_TYPE(r->r_info)) {
5851015e6dSKonstantin Belousov 	case R_PPC_IRELATIVE:
5951015e6dSKonstantin Belousov 		ptr = (Elf_Addr *)r->r_addend;
6051015e6dSKonstantin Belousov 		where = (Elf_Addr *)r->r_offset;
6151015e6dSKonstantin Belousov 		target = ((ifunc_resolver_t)ptr)(cpu_features, cpu_features2,
6251015e6dSKonstantin Belousov 		    0, 0, 0, 0, 0, 0);
6351015e6dSKonstantin Belousov 		*where = target;
6451015e6dSKonstantin Belousov 		break;
6551015e6dSKonstantin Belousov 	}
6651015e6dSKonstantin Belousov }
67