xref: /freebsd/lib/libc/csu/i386/reloc.c (revision 02e9120893770924227138ba49df1edb3896112a)
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 <machine/specialreg.h>
27 #include <machine/cpufunc.h>
28 
29 static uint32_t cpu_feature, cpu_feature2;
30 static uint32_t cpu_stdext_feature, cpu_stdext_feature2;
31 
32 static void
33 init_cpu_features(void)
34 {
35 	u_int cpuid_supported, p[4];
36 
37 	__asm __volatile(
38 	    "	pushfl\n"
39 	    "	popl	%%eax\n"
40 	    "	movl    %%eax,%%ecx\n"
41 	    "	xorl    $0x200000,%%eax\n"
42 	    "	pushl	%%eax\n"
43 	    "	popfl\n"
44 	    "	pushfl\n"
45 	    "	popl    %%eax\n"
46 	    "	xorl    %%eax,%%ecx\n"
47 	    "	je	1f\n"
48 	    "	movl	$1,%0\n"
49 	    "	jmp	2f\n"
50 	    "1:	movl	$0,%0\n"
51 	    "2:\n"
52 	    : "=r" (cpuid_supported) : : "eax", "ecx", "cc");
53 	if (cpuid_supported) {
54 		do_cpuid(1, p);
55 		cpu_feature = p[3];
56 		cpu_feature2 = p[2];
57 		do_cpuid(0, p);
58 		if (p[0] >= 7) {
59 			cpuid_count(7, 0, p);
60 			cpu_stdext_feature = p[1];
61 			cpu_stdext_feature2 = p[2];
62 		} else {
63 			cpu_stdext_feature = 0;
64 			cpu_stdext_feature2 = 0;
65 		}
66 	} else {
67 		cpu_feature = 0;
68 		cpu_feature2 = 0;
69 		cpu_stdext_feature = 0;
70 		cpu_stdext_feature2 = 0;
71 	}
72 }
73 
74 static void
75 crt1_handle_rel(const Elf_Rel *r)
76 {
77 	Elf_Addr *where, target;
78 
79 	switch (ELF_R_TYPE(r->r_info)) {
80 	case R_386_IRELATIVE:
81 		where = (Elf_Addr *)r->r_offset;
82 		target = ((Elf_Addr (*)(uint32_t, uint32_t, uint32_t,
83 		    uint32_t))*where)(cpu_feature, cpu_feature2,
84 		    cpu_stdext_feature, cpu_stdext_feature2);
85 		*where = target;
86 		break;
87 	}
88 }
89