xref: /freebsd/sys/powerpc/powerpc/elf_common.c (revision 95ee2897)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2019 Justin Hibbits
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 static int
__elfN(powerpc_copyout_auxargs)31 __elfN(powerpc_copyout_auxargs)(struct image_params *imgp, uintptr_t base)
32 {
33 	Elf_Auxargs *args;
34 	Elf_Auxinfo *argarray, *pos;
35 	struct vmspace *vmspace;
36 	int error;
37 
38 	/*
39 	 * XXX If we can't find image's OSREL, assume it uses the new auxv
40 	 * format.
41 	 *
42 	 * This is specially important for rtld, that is not tagged. Using
43 	 * direct exec mode with new (ELFv2) binaries that expect the new auxv
44 	 * format would result in crashes otherwise.
45 	 *
46 	 * Unfortunately, this may break direct exec'ing old binaries,
47 	 * but it seems better to correctly support new binaries by default,
48 	 * considering the transition to ELFv2 happened quite some time
49 	 * ago. If needed, a sysctl may be added to allow old auxv format to
50 	 * be used when OSREL is not found.
51 	 */
52 	if (imgp->proc->p_osrel >= P_OSREL_POWERPC_NEW_AUX_ARGS ||
53 	    imgp->proc->p_osrel == 0)
54 		return (__elfN(freebsd_copyout_auxargs)(imgp, base));
55 
56 	args = (Elf_Auxargs *)imgp->auxargs;
57 	argarray = pos = malloc(AT_OLD_COUNT * sizeof(*pos), M_TEMP,
58 	    M_WAITOK | M_ZERO);
59 
60 	vmspace = imgp->proc->p_vmspace;
61 
62 	if (args->execfd != -1)
63 		AUXARGS_ENTRY(pos, AT_OLD_EXECFD, args->execfd);
64 	AUXARGS_ENTRY(pos, AT_OLD_PHDR, args->phdr);
65 	AUXARGS_ENTRY(pos, AT_OLD_PHENT, args->phent);
66 	AUXARGS_ENTRY(pos, AT_OLD_PHNUM, args->phnum);
67 	AUXARGS_ENTRY(pos, AT_OLD_PAGESZ, args->pagesz);
68 	AUXARGS_ENTRY(pos, AT_OLD_FLAGS, args->flags);
69 	AUXARGS_ENTRY(pos, AT_OLD_ENTRY, args->entry);
70 	AUXARGS_ENTRY(pos, AT_OLD_BASE, args->base);
71 	AUXARGS_ENTRY(pos, AT_OLD_EHDRFLAGS, args->hdr_eflags);
72 	if (imgp->execpathp != 0)
73 		AUXARGS_ENTRY_PTR(pos, AT_OLD_EXECPATH, imgp->execpathp);
74 	AUXARGS_ENTRY(pos, AT_OLD_OSRELDATE,
75 	    imgp->proc->p_ucred->cr_prison->pr_osreldate);
76 	if (imgp->canary != 0) {
77 		AUXARGS_ENTRY_PTR(pos, AT_OLD_CANARY, imgp->canary);
78 		AUXARGS_ENTRY(pos, AT_OLD_CANARYLEN, imgp->canarylen);
79 	}
80 	AUXARGS_ENTRY(pos, AT_OLD_NCPUS, mp_ncpus);
81 	if (imgp->pagesizes != 0) {
82 		AUXARGS_ENTRY_PTR(pos, AT_OLD_PAGESIZES, imgp->pagesizes);
83 		AUXARGS_ENTRY(pos, AT_OLD_PAGESIZESLEN, imgp->pagesizeslen);
84 	}
85 	if ((imgp->sysent->sv_flags & SV_TIMEKEEP) != 0) {
86 		AUXARGS_ENTRY(pos, AT_OLD_TIMEKEEP,
87 		    vmspace->vm_shp_base + imgp->sysent->sv_timekeep_offset);
88 	}
89 	AUXARGS_ENTRY(pos, AT_OLD_STACKPROT, imgp->sysent->sv_shared_page_obj
90 	    != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
91 	    imgp->sysent->sv_stackprot);
92 	if (imgp->sysent->sv_hwcap != NULL)
93 		AUXARGS_ENTRY(pos, AT_OLD_HWCAP, *imgp->sysent->sv_hwcap);
94 	if (imgp->sysent->sv_hwcap2 != NULL)
95 		AUXARGS_ENTRY(pos, AT_OLD_HWCAP2, *imgp->sysent->sv_hwcap2);
96 	AUXARGS_ENTRY(pos, AT_OLD_NULL, 0);
97 
98 	free(imgp->auxargs, M_TEMP);
99 	imgp->auxargs = NULL;
100 	KASSERT(pos - argarray <= AT_OLD_COUNT, ("Too many auxargs"));
101 
102 	error = copyout(argarray, (void *)base, sizeof(*argarray) * AT_OLD_COUNT);
103 	free(argarray, M_TEMP);
104 	return (error);
105 }
106