xref: /freebsd/sys/amd64/amd64/elf_machdep.c (revision b00ab754)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
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/proc.h>
38 #include <sys/sysent.h>
39 #include <sys/imgact_elf.h>
40 #include <sys/syscall.h>
41 #include <sys/signalvar.h>
42 #include <sys/vnode.h>
43 
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_param.h>
47 
48 #include <machine/elf.h>
49 #include <machine/fpu.h>
50 #include <machine/md_var.h>
51 
52 struct sysentvec elf64_freebsd_sysvec = {
53 	.sv_size	= SYS_MAXSYSCALL,
54 	.sv_table	= sysent,
55 	.sv_mask	= 0,
56 	.sv_errsize	= 0,
57 	.sv_errtbl	= NULL,
58 	.sv_transtrap	= NULL,
59 	.sv_fixup	= __elfN(freebsd_fixup),
60 	.sv_sendsig	= sendsig,
61 	.sv_sigcode	= sigcode,
62 	.sv_szsigcode	= &szsigcode,
63 	.sv_name	= "FreeBSD ELF64",
64 	.sv_coredump	= __elfN(coredump),
65 	.sv_imgact_try	= NULL,
66 	.sv_minsigstksz	= MINSIGSTKSZ,
67 	.sv_pagesize	= PAGE_SIZE,
68 	.sv_minuser	= VM_MIN_ADDRESS,
69 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
70 	.sv_usrstack	= USRSTACK,
71 	.sv_psstrings	= PS_STRINGS,
72 	.sv_stackprot	= VM_PROT_ALL,
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_LP64 | SV_SHP | SV_TIMEKEEP,
78 	.sv_set_syscall_retval = cpu_set_syscall_retval,
79 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
80 	.sv_syscallnames = syscallnames,
81 	.sv_shared_page_base = SHAREDPAGE,
82 	.sv_shared_page_len = PAGE_SIZE,
83 	.sv_schedtail	= NULL,
84 	.sv_thread_detach = NULL,
85 	.sv_trap	= NULL,
86 };
87 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
88 
89 void
90 amd64_lower_shared_page(struct sysentvec *sv)
91 {
92 	if (hw_lower_amd64_sharedpage != 0) {
93 		sv->sv_maxuser -= PAGE_SIZE;
94 		sv->sv_shared_page_base -= PAGE_SIZE;
95 		sv->sv_usrstack -= PAGE_SIZE;
96 		sv->sv_psstrings -= PAGE_SIZE;
97 	}
98 }
99 
100 /*
101  * Do this fixup before INIT_SYSENTVEC (SI_ORDER_ANY) because the latter
102  * uses the value of sv_shared_page_base.
103  */
104 SYSINIT(elf64_sysvec_fixup, SI_SUB_EXEC, SI_ORDER_FIRST,
105 	(sysinit_cfunc_t) amd64_lower_shared_page,
106 	&elf64_freebsd_sysvec);
107 
108 static Elf64_Brandinfo freebsd_brand_info = {
109 	.brand		= ELFOSABI_FREEBSD,
110 	.machine	= EM_X86_64,
111 	.compat_3_brand	= "FreeBSD",
112 	.emul_path	= NULL,
113 	.interp_path	= "/libexec/ld-elf.so.1",
114 	.sysvec		= &elf64_freebsd_sysvec,
115 	.interp_newpath	= NULL,
116 	.brand_note	= &elf64_freebsd_brandnote,
117 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
118 };
119 
120 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
121 	(sysinit_cfunc_t) elf64_insert_brand_entry,
122 	&freebsd_brand_info);
123 
124 static Elf64_Brandinfo freebsd_brand_oinfo = {
125 	.brand		= ELFOSABI_FREEBSD,
126 	.machine	= EM_X86_64,
127 	.compat_3_brand	= "FreeBSD",
128 	.emul_path	= NULL,
129 	.interp_path	= "/usr/libexec/ld-elf.so.1",
130 	.sysvec		= &elf64_freebsd_sysvec,
131 	.interp_newpath	= NULL,
132 	.brand_note	= &elf64_freebsd_brandnote,
133 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
134 };
135 
136 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
137 	(sysinit_cfunc_t) elf64_insert_brand_entry,
138 	&freebsd_brand_oinfo);
139 
140 static Elf64_Brandinfo kfreebsd_brand_info = {
141 	.brand		= ELFOSABI_FREEBSD,
142 	.machine	= EM_X86_64,
143 	.compat_3_brand	= "FreeBSD",
144 	.emul_path	= NULL,
145 	.interp_path	= "/lib/ld-kfreebsd-x86-64.so.1",
146 	.sysvec		= &elf64_freebsd_sysvec,
147 	.interp_newpath	= NULL,
148 	.brand_note	= &elf64_kfreebsd_brandnote,
149 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
150 };
151 
152 SYSINIT(kelf64, SI_SUB_EXEC, SI_ORDER_ANY,
153 	(sysinit_cfunc_t) elf64_insert_brand_entry,
154 	&kfreebsd_brand_info);
155 
156 void
157 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
158 {
159 	void *buf;
160 	size_t len;
161 
162 	len = 0;
163 	if (use_xsave) {
164 		if (dst != NULL) {
165 			fpugetregs(td);
166 			len += elf64_populate_note(NT_X86_XSTATE,
167 			    get_pcb_user_save_td(td), dst,
168 			    cpu_max_ext_state_size, &buf);
169 			*(uint64_t *)((char *)buf + X86_XSTATE_XCR0_OFFSET) =
170 			    xsave_mask;
171 		} else
172 			len += elf64_populate_note(NT_X86_XSTATE, NULL, NULL,
173 			    cpu_max_ext_state_size, NULL);
174 	}
175 	*off = len;
176 }
177 
178 #define	ERI_LOCAL	0x0001
179 #define	ERI_ONLYIFUNC	0x0002
180 
181 /* Process one elf relocation with addend. */
182 static int
183 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
184     int type, elf_lookup_fn lookup, int flags)
185 {
186 	Elf64_Addr *where, val;
187 	Elf32_Addr *where32, val32;
188 	Elf_Addr addr;
189 	Elf_Addr addend;
190 	Elf_Size rtype, symidx;
191 	const Elf_Rel *rel;
192 	const Elf_Rela *rela;
193 	int error;
194 
195 	switch (type) {
196 	case ELF_RELOC_REL:
197 		rel = (const Elf_Rel *)data;
198 		where = (Elf_Addr *) (relocbase + rel->r_offset);
199 		rtype = ELF_R_TYPE(rel->r_info);
200 		symidx = ELF_R_SYM(rel->r_info);
201 		/* Addend is 32 bit on 32 bit relocs */
202 		switch (rtype) {
203 		case R_X86_64_PC32:
204 		case R_X86_64_32S:
205 		case R_X86_64_PLT32:
206 			addend = *(Elf32_Addr *)where;
207 			break;
208 		default:
209 			addend = *where;
210 			break;
211 		}
212 		break;
213 	case ELF_RELOC_RELA:
214 		rela = (const Elf_Rela *)data;
215 		where = (Elf_Addr *) (relocbase + rela->r_offset);
216 		addend = rela->r_addend;
217 		rtype = ELF_R_TYPE(rela->r_info);
218 		symidx = ELF_R_SYM(rela->r_info);
219 		break;
220 	default:
221 		panic("unknown reloc type %d\n", type);
222 	}
223 
224 	if (((flags & ERI_ONLYIFUNC) == 0) ^ (rtype != R_X86_64_IRELATIVE))
225 		return (0);
226 
227 	switch (rtype) {
228 		case R_X86_64_NONE:	/* none */
229 			break;
230 
231 		case R_X86_64_64:		/* S + A */
232 			error = lookup(lf, symidx, 1, &addr);
233 			val = addr + addend;
234 			if (error != 0)
235 				return -1;
236 			if (*where != val)
237 				*where = val;
238 			break;
239 
240 		case R_X86_64_PC32:	/* S + A - P */
241 		case R_X86_64_PLT32:	/* L + A - P, L is PLT location for
242 					   the symbol, which we treat as S */
243 			error = lookup(lf, symidx, 1, &addr);
244 			where32 = (Elf32_Addr *)where;
245 			val32 = (Elf32_Addr)(addr + addend - (Elf_Addr)where);
246 			if (error != 0)
247 				return -1;
248 			if (*where32 != val32)
249 				*where32 = val32;
250 			break;
251 
252 		case R_X86_64_32S:	/* S + A sign extend */
253 			error = lookup(lf, symidx, 1, &addr);
254 			val32 = (Elf32_Addr)(addr + addend);
255 			where32 = (Elf32_Addr *)where;
256 			if (error != 0)
257 				return -1;
258 			if (*where32 != val32)
259 				*where32 = val32;
260 			break;
261 
262 		case R_X86_64_COPY:	/* none */
263 			/*
264 			 * There shouldn't be copy relocations in kernel
265 			 * objects.
266 			 */
267 			printf("kldload: unexpected R_COPY relocation\n");
268 			return (-1);
269 			break;
270 
271 		case R_X86_64_GLOB_DAT:	/* S */
272 		case R_X86_64_JMP_SLOT:	/* XXX need addend + offset */
273 			error = lookup(lf, symidx, 1, &addr);
274 			if (error != 0)
275 				return -1;
276 			if (*where != addr)
277 				*where = addr;
278 			break;
279 
280 		case R_X86_64_RELATIVE:	/* B + A */
281 			addr = relocbase + addend;
282 			val = addr;
283 			if (*where != val)
284 				*where = val;
285 			break;
286 
287 		case R_X86_64_IRELATIVE:
288 			addr = relocbase + addend;
289 			val = ((Elf64_Addr (*)(void))addr)();
290 			if (*where != val)
291 				*where = val;
292 			break;
293 
294 		default:
295 			printf("kldload: unexpected relocation type %ld\n",
296 			       rtype);
297 			return (-1);
298 	}
299 	return (0);
300 }
301 
302 int
303 elf_reloc_ifunc(linker_file_t lf, Elf_Addr relocbase, const void *data,
304     int type, elf_lookup_fn lookup)
305 {
306 
307 	return (elf_reloc_internal(lf, relocbase, data, type, lookup,
308 	    ERI_ONLYIFUNC));
309 }
310 
311 int
312 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
313     elf_lookup_fn lookup)
314 {
315 
316 	return (elf_reloc_internal(lf, relocbase, data, type, lookup, 0));
317 }
318 
319 int
320 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
321     int type, elf_lookup_fn lookup)
322 {
323 
324 	return (elf_reloc_internal(lf, relocbase, data, type, lookup,
325 	    ERI_LOCAL));
326 }
327 
328 int
329 elf_cpu_load_file(linker_file_t lf __unused)
330 {
331 
332 	return (0);
333 }
334 
335 int
336 elf_cpu_unload_file(linker_file_t lf __unused)
337 {
338 
339 	return (0);
340 }
341