xref: /freebsd/lib/libc/csu/i386/reloc.c (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
1 /*-
2  * Copyright (c) 2018 The FreeBSD Foundation
3  *
4  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
5  * under sponsorship from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 
28 #include <machine/specialreg.h>
29 #include <machine/cpufunc.h>
30 
31 static uint32_t cpu_feature, cpu_feature2;
32 static uint32_t cpu_stdext_feature, cpu_stdext_feature2;
33 
34 static void
35 ifunc_init(const Elf_Auxinfo *aux __unused)
36 {
37 	u_int cpuid_supported, p[4];
38 
39 	__asm __volatile(
40 	    "	pushfl\n"
41 	    "	popl	%%eax\n"
42 	    "	movl    %%eax,%%ecx\n"
43 	    "	xorl    $0x200000,%%eax\n"
44 	    "	pushl	%%eax\n"
45 	    "	popfl\n"
46 	    "	pushfl\n"
47 	    "	popl    %%eax\n"
48 	    "	xorl    %%eax,%%ecx\n"
49 	    "	je	1f\n"
50 	    "	movl	$1,%0\n"
51 	    "	jmp	2f\n"
52 	    "1:	movl	$0,%0\n"
53 	    "2:\n"
54 	    : "=r" (cpuid_supported) : : "eax", "ecx", "cc");
55 	if (cpuid_supported) {
56 		do_cpuid(1, p);
57 		cpu_feature = p[3];
58 		cpu_feature2 = p[2];
59 		do_cpuid(0, p);
60 		if (p[0] >= 7) {
61 			cpuid_count(7, 0, p);
62 			cpu_stdext_feature = p[1];
63 			cpu_stdext_feature2 = p[2];
64 		} else {
65 			cpu_stdext_feature = 0;
66 			cpu_stdext_feature2 = 0;
67 		}
68 	} else {
69 		cpu_feature = 0;
70 		cpu_feature2 = 0;
71 		cpu_stdext_feature = 0;
72 		cpu_stdext_feature2 = 0;
73 	}
74 }
75 
76 static void
77 crt1_handle_rel(const Elf_Rel *r)
78 {
79 	Elf_Addr *where, target;
80 
81 	switch (ELF_R_TYPE(r->r_info)) {
82 	case R_386_IRELATIVE:
83 		where = (Elf_Addr *)r->r_offset;
84 		target = ((Elf_Addr (*)(uint32_t, uint32_t, uint32_t,
85 		    uint32_t))*where)(cpu_feature, cpu_feature2,
86 		    cpu_stdext_feature, cpu_stdext_feature2);
87 		*where = target;
88 		break;
89 	}
90 }
91