xref: /freebsd/lib/libc/csu/i386/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 cpuid_supported, p[4];
3851015e6dSKonstantin Belousov 
3951015e6dSKonstantin Belousov 	__asm __volatile(
4051015e6dSKonstantin Belousov 	    "	pushfl\n"
4151015e6dSKonstantin Belousov 	    "	popl	%%eax\n"
4251015e6dSKonstantin Belousov 	    "	movl    %%eax,%%ecx\n"
4351015e6dSKonstantin Belousov 	    "	xorl    $0x200000,%%eax\n"
4451015e6dSKonstantin Belousov 	    "	pushl	%%eax\n"
4551015e6dSKonstantin Belousov 	    "	popfl\n"
4651015e6dSKonstantin Belousov 	    "	pushfl\n"
4751015e6dSKonstantin Belousov 	    "	popl    %%eax\n"
4851015e6dSKonstantin Belousov 	    "	xorl    %%eax,%%ecx\n"
4951015e6dSKonstantin Belousov 	    "	je	1f\n"
5051015e6dSKonstantin Belousov 	    "	movl	$1,%0\n"
5151015e6dSKonstantin Belousov 	    "	jmp	2f\n"
5251015e6dSKonstantin Belousov 	    "1:	movl	$0,%0\n"
5351015e6dSKonstantin Belousov 	    "2:\n"
5451015e6dSKonstantin Belousov 	    : "=r" (cpuid_supported) : : "eax", "ecx", "cc");
5551015e6dSKonstantin Belousov 	if (cpuid_supported) {
5651015e6dSKonstantin Belousov 		do_cpuid(1, p);
5751015e6dSKonstantin Belousov 		cpu_feature = p[3];
5851015e6dSKonstantin Belousov 		cpu_feature2 = p[2];
5951015e6dSKonstantin Belousov 		do_cpuid(0, p);
6051015e6dSKonstantin Belousov 		if (p[0] >= 7) {
6151015e6dSKonstantin Belousov 			cpuid_count(7, 0, p);
6251015e6dSKonstantin Belousov 			cpu_stdext_feature = p[1];
6351015e6dSKonstantin Belousov 			cpu_stdext_feature2 = p[2];
6451015e6dSKonstantin Belousov 		} else {
6551015e6dSKonstantin Belousov 			cpu_stdext_feature = 0;
6651015e6dSKonstantin Belousov 			cpu_stdext_feature2 = 0;
6751015e6dSKonstantin Belousov 		}
6851015e6dSKonstantin Belousov 	} else {
6951015e6dSKonstantin Belousov 		cpu_feature = 0;
7051015e6dSKonstantin Belousov 		cpu_feature2 = 0;
7151015e6dSKonstantin Belousov 		cpu_stdext_feature = 0;
7251015e6dSKonstantin Belousov 		cpu_stdext_feature2 = 0;
7351015e6dSKonstantin Belousov 	}
7403039385SKonstantin Belousov }
7503039385SKonstantin Belousov 
7603039385SKonstantin Belousov static void
crt1_handle_rel(const Elf_Rel * r)7703039385SKonstantin Belousov crt1_handle_rel(const Elf_Rel *r)
7803039385SKonstantin Belousov {
7903039385SKonstantin Belousov 	Elf_Addr *where, target;
8051015e6dSKonstantin Belousov 
8151015e6dSKonstantin Belousov 	switch (ELF_R_TYPE(r->r_info)) {
8251015e6dSKonstantin Belousov 	case R_386_IRELATIVE:
8351015e6dSKonstantin Belousov 		where = (Elf_Addr *)r->r_offset;
8451015e6dSKonstantin Belousov 		target = ((Elf_Addr (*)(uint32_t, uint32_t, uint32_t,
8551015e6dSKonstantin Belousov 		    uint32_t))*where)(cpu_feature, cpu_feature2,
8651015e6dSKonstantin Belousov 		    cpu_stdext_feature, cpu_stdext_feature2);
8751015e6dSKonstantin Belousov 		*where = target;
8851015e6dSKonstantin Belousov 		break;
8951015e6dSKonstantin Belousov 	}
9051015e6dSKonstantin Belousov }
91