xref: /freebsd/sys/i386/i386/elf_machdep.c (revision e0c4386e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 1996-1998 John D. Polstra.
5  * All rights reserved.
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  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include "opt_cpu.h"
30 
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/exec.h>
35 #include <sys/imgact.h>
36 #include <sys/linker.h>
37 #include <sys/reg.h>
38 #include <sys/proc.h>
39 #include <sys/sysent.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/syscall.h>
42 #include <sys/signalvar.h>
43 #include <sys/vnode.h>
44 
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_param.h>
48 
49 #include <machine/elf.h>
50 #include <machine/md_var.h>
51 #include <machine/npx.h>
52 
53 struct sysentvec elf32_freebsd_sysvec = {
54 	.sv_size	= SYS_MAXSYSCALL,
55 	.sv_table	= sysent,
56 	.sv_fixup	= __elfN(freebsd_fixup),
57 	.sv_sendsig	= sendsig,
58 	.sv_sigcode	= sigcode,
59 	.sv_szsigcode	= &szsigcode,
60 	.sv_name	= "FreeBSD ELF32",
61 	.sv_coredump	= __elfN(coredump),
62 	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
63 	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
64 	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
65 	.sv_minsigstksz	= MINSIGSTKSZ,
66 	.sv_minuser	= VM_MIN_ADDRESS,
67 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
68 	.sv_usrstack	= USRSTACK,
69 	.sv_psstrings	= PS_STRINGS,
70 	.sv_psstringssz	= sizeof(struct ps_strings),
71 	.sv_stackprot	= VM_PROT_ALL,
72 	.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
73 	.sv_copyout_strings	= exec_copyout_strings,
74 	.sv_setregs	= exec_setregs,
75 	.sv_fixlimit	= NULL,
76 	.sv_maxssiz	= NULL,
77 	.sv_flags	= SV_ABI_FREEBSD | SV_ASLR | SV_IA32 | SV_ILP32 |
78 			    SV_SHP | SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS,
79 	.sv_set_syscall_retval = cpu_set_syscall_retval,
80 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
81 	.sv_syscallnames = syscallnames,
82 	.sv_shared_page_base = SHAREDPAGE,
83 	.sv_shared_page_len = PAGE_SIZE,
84 	.sv_schedtail	= NULL,
85 	.sv_thread_detach = NULL,
86 	.sv_trap	= NULL,
87 	.sv_onexec_old	= exec_onexec_old,
88 	.sv_onexit	= exit_onexit,
89 	.sv_set_fork_retval = x86_set_fork_retval,
90 	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
91 	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
92 };
93 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
94 
95 static Elf32_Brandinfo freebsd_brand_info = {
96 	.brand		= ELFOSABI_FREEBSD,
97 	.machine	= EM_386,
98 	.compat_3_brand	= "FreeBSD",
99 	.interp_path	= "/libexec/ld-elf.so.1",
100 	.sysvec		= &elf32_freebsd_sysvec,
101 	.interp_newpath	= NULL,
102 	.brand_note	= &elf32_freebsd_brandnote,
103 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
104 };
105 
106 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
107 	(sysinit_cfunc_t) elf32_insert_brand_entry,
108 	&freebsd_brand_info);
109 
110 static Elf32_Brandinfo freebsd_brand_oinfo = {
111 	.brand		= ELFOSABI_FREEBSD,
112 	.machine	= EM_386,
113 	.compat_3_brand	= "FreeBSD",
114 	.interp_path	= "/usr/libexec/ld-elf.so.1",
115 	.sysvec		= &elf32_freebsd_sysvec,
116 	.interp_newpath	= NULL,
117 	.brand_note	= &elf32_freebsd_brandnote,
118 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
119 };
120 
121 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
122 	(sysinit_cfunc_t) elf32_insert_brand_entry,
123 	&freebsd_brand_oinfo);
124 
125 static Elf32_Brandinfo kfreebsd_brand_info = {
126 	.brand		= ELFOSABI_FREEBSD,
127 	.machine	= EM_386,
128 	.compat_3_brand	= "FreeBSD",
129 	.interp_path	= "/lib/ld.so.1",
130 	.sysvec		= &elf32_freebsd_sysvec,
131 	.interp_newpath	= NULL,
132 	.brand_note	= &elf32_kfreebsd_brandnote,
133 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
134 };
135 
136 SYSINIT(kelf32, SI_SUB_EXEC, SI_ORDER_ANY,
137 	(sysinit_cfunc_t) elf32_insert_brand_entry,
138 	&kfreebsd_brand_info);
139 
140 void
141 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
142 {
143 	void *buf;
144 	size_t len;
145 
146 	len = 0;
147 	if (use_xsave) {
148 		if (dst != NULL) {
149 			npxgetregs(td);
150 			len += elf32_populate_note(NT_X86_XSTATE,
151 			    get_pcb_user_save_td(td), dst,
152 			    cpu_max_ext_state_size, &buf);
153 			*(uint64_t *)((char *)buf + X86_XSTATE_XCR0_OFFSET) =
154 			    xsave_mask;
155 		} else
156 			len += elf32_populate_note(NT_X86_XSTATE, NULL, NULL,
157 			    cpu_max_ext_state_size, NULL);
158 	}
159 	*off = len;
160 }
161 
162 bool
163 elf_is_ifunc_reloc(Elf_Size r_info)
164 {
165 
166 	return (ELF_R_TYPE(r_info) == R_386_IRELATIVE);
167 }
168 
169 #define	ERI_LOCAL	0x0001
170 
171 /* Process one elf relocation with addend. */
172 static int
173 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
174     int type, elf_lookup_fn lookup, int flags)
175 {
176 	Elf_Addr *where;
177 	Elf_Addr addr;
178 	Elf_Addr addend;
179 	Elf_Word rtype, symidx;
180 	const Elf_Rel *rel;
181 	const Elf_Rela *rela;
182 	int error;
183 
184 	switch (type) {
185 	case ELF_RELOC_REL:
186 		rel = (const Elf_Rel *)data;
187 		where = (Elf_Addr *) (relocbase + rel->r_offset);
188 		addend = *where;
189 		rtype = ELF_R_TYPE(rel->r_info);
190 		symidx = ELF_R_SYM(rel->r_info);
191 		break;
192 	case ELF_RELOC_RELA:
193 		rela = (const Elf_Rela *)data;
194 		where = (Elf_Addr *) (relocbase + rela->r_offset);
195 		addend = rela->r_addend;
196 		rtype = ELF_R_TYPE(rela->r_info);
197 		symidx = ELF_R_SYM(rela->r_info);
198 		break;
199 	default:
200 		panic("unknown reloc type %d\n", type);
201 	}
202 
203 	if ((flags & ERI_LOCAL) != 0) {
204 		if (rtype == R_386_RELATIVE) {	/* A + B */
205 			addr = elf_relocaddr(lf, relocbase + addend);
206 			if (*where != addr)
207 				*where = addr;
208 		}
209 		return (0);
210 	}
211 
212 	switch (rtype) {
213 		case R_386_NONE:	/* none */
214 			break;
215 
216 		case R_386_32:		/* S + A */
217 			error = lookup(lf, symidx, 1, &addr);
218 			if (error != 0)
219 				return (-1);
220 			addr += addend;
221 			if (*where != addr)
222 				*where = addr;
223 			break;
224 
225 		case R_386_PC32:	/* S + A - P */
226 			error = lookup(lf, symidx, 1, &addr);
227 			if (error != 0)
228 				return (-1);
229 			addr += addend - (Elf_Addr)where;
230 			if (*where != addr)
231 				*where = addr;
232 			break;
233 
234 		case R_386_COPY:	/* none */
235 			/*
236 			 * There shouldn't be copy relocations in kernel
237 			 * objects.
238 			 */
239 			printf("kldload: unexpected R_COPY relocation, "
240 			    "symbol index %d\n", symidx);
241 			return (-1);
242 			break;
243 
244 		case R_386_GLOB_DAT:	/* S */
245 		case R_386_JMP_SLOT:	/* S */
246 			error = lookup(lf, symidx, 1, &addr);
247 			if (error != 0)
248 				return (-1);
249 			if (*where != addr)
250 				*where = addr;
251 			break;
252 
253 		case R_386_RELATIVE:
254 			break;
255 
256 		case R_386_IRELATIVE:
257 			addr = relocbase + addend;
258 			addr = ((Elf_Addr (*)(void))addr)();
259 			if (*where != addr)
260 				*where = addr;
261 			break;
262 
263 		default:
264 			printf("kldload: unexpected relocation type %d, "
265 			    "symbol index %d\n", rtype, symidx);
266 			return (-1);
267 	}
268 	return(0);
269 }
270 
271 int
272 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
273     elf_lookup_fn lookup)
274 {
275 
276 	return (elf_reloc_internal(lf, relocbase, data, type, lookup, 0));
277 }
278 
279 int
280 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
281     int type, elf_lookup_fn lookup)
282 {
283 
284 	return (elf_reloc_internal(lf, relocbase, data, type, lookup,
285 	    ERI_LOCAL));
286 }
287 
288 int
289 elf_cpu_load_file(linker_file_t lf __unused)
290 {
291 
292 	return (0);
293 }
294 
295 int
296 elf_cpu_unload_file(linker_file_t lf __unused)
297 {
298 
299 	return (0);
300 }
301 
302 int
303 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
304 {
305 
306 	return (0);
307 }
308