xref: /freebsd/lib/libc/csu/amd64/reloc.c (revision 9684658e35ab033c79e0519e3681d9a194976b71)
151015e6dSKonstantin Belousov /*-
251015e6dSKonstantin Belousov  * Copyright (c) 2018 The FreeBSD Foundation
351015e6dSKonstantin Belousov  *
451015e6dSKonstantin Belousov  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
551015e6dSKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
651015e6dSKonstantin Belousov  *
751015e6dSKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
851015e6dSKonstantin Belousov  * modification, are permitted provided that the following conditions
951015e6dSKonstantin Belousov  * are met:
1051015e6dSKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
1151015e6dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
1251015e6dSKonstantin Belousov  *
1351015e6dSKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1451015e6dSKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1551015e6dSKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1651015e6dSKonstantin Belousov  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1751015e6dSKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1851015e6dSKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1951015e6dSKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2051015e6dSKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2151015e6dSKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2251015e6dSKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2351015e6dSKonstantin Belousov  * SUCH DAMAGE.
2451015e6dSKonstantin Belousov  */
2551015e6dSKonstantin Belousov 
26*9684658eSJessica Clarke #include <sys/cdefs.h>
27*9684658eSJessica Clarke 
2851015e6dSKonstantin Belousov #include <machine/specialreg.h>
2951015e6dSKonstantin Belousov #include <machine/cpufunc.h>
3051015e6dSKonstantin Belousov 
3103039385SKonstantin Belousov static uint32_t cpu_feature, cpu_feature2;
3203039385SKonstantin Belousov static uint32_t cpu_stdext_feature, cpu_stdext_feature2;
3303039385SKonstantin Belousov 
3451015e6dSKonstantin Belousov static void
ifunc_init(const Elf_Auxinfo * aux __unused)35*9684658eSJessica Clarke ifunc_init(const Elf_Auxinfo *aux __unused)
3651015e6dSKonstantin Belousov {
3751015e6dSKonstantin Belousov 	u_int p[4];
3851015e6dSKonstantin Belousov 
3951015e6dSKonstantin Belousov 	do_cpuid(1, p);
4051015e6dSKonstantin Belousov 	cpu_feature = p[3];
4151015e6dSKonstantin Belousov 	cpu_feature2 = p[2];
4251015e6dSKonstantin Belousov 	do_cpuid(0, p);
4351015e6dSKonstantin Belousov 	if (p[0] >= 7) {
4451015e6dSKonstantin Belousov 		cpuid_count(7, 0, p);
4551015e6dSKonstantin Belousov 		cpu_stdext_feature = p[1];
4651015e6dSKonstantin Belousov 		cpu_stdext_feature2 = p[2];
4751015e6dSKonstantin Belousov 	} else {
4851015e6dSKonstantin Belousov 		cpu_stdext_feature = 0;
4951015e6dSKonstantin Belousov 		cpu_stdext_feature2 = 0;
5051015e6dSKonstantin Belousov 	}
5103039385SKonstantin Belousov }
5203039385SKonstantin Belousov 
5303039385SKonstantin Belousov static void
crt1_handle_rela(const Elf_Rela * r)5403039385SKonstantin Belousov crt1_handle_rela(const Elf_Rela *r)
5503039385SKonstantin Belousov {
5603039385SKonstantin Belousov 	Elf_Addr *ptr, *where, target;
5751015e6dSKonstantin Belousov 
5851015e6dSKonstantin Belousov 	switch (ELF_R_TYPE(r->r_info)) {
5951015e6dSKonstantin Belousov 	case R_X86_64_IRELATIVE:
6051015e6dSKonstantin Belousov 		ptr = (Elf_Addr *)r->r_addend;
6151015e6dSKonstantin Belousov 		where = (Elf_Addr *)r->r_offset;
6251015e6dSKonstantin Belousov 		target = ((Elf_Addr (*)(uint32_t, uint32_t, uint32_t,
6351015e6dSKonstantin Belousov 		    uint32_t))ptr)(cpu_feature, cpu_feature2,
6451015e6dSKonstantin Belousov 		    cpu_stdext_feature, cpu_stdext_feature2);
6551015e6dSKonstantin Belousov 		*where = target;
6651015e6dSKonstantin Belousov 		break;
6751015e6dSKonstantin Belousov 	}
6851015e6dSKonstantin Belousov }
69