]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powerpc/elf_common.c
libarchive: merge from vendor branch
[FreeBSD/FreeBSD.git] / sys / powerpc / powerpc / elf_common.c
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  * $FreeBSD$
30  */
31
32 static int
33 __elfN(powerpc_copyout_auxargs)(struct image_params *imgp, uintptr_t base)
34 {
35         Elf_Auxargs *args;
36         Elf_Auxinfo *argarray, *pos;
37         struct vmspace *vmspace;
38         int error;
39
40         /*
41          * XXX If we can't find image's OSREL, assume it uses the new auxv
42          * format.
43          *
44          * This is specially important for rtld, that is not tagged. Using
45          * direct exec mode with new (ELFv2) binaries that expect the new auxv
46          * format would result in crashes otherwise.
47          *
48          * Unfortunately, this may break direct exec'ing old binaries,
49          * but it seems better to correctly support new binaries by default,
50          * considering the transition to ELFv2 happened quite some time
51          * ago. If needed, a sysctl may be added to allow old auxv format to
52          * be used when OSREL is not found.
53          */
54         if (imgp->proc->p_osrel >= P_OSREL_POWERPC_NEW_AUX_ARGS ||
55             imgp->proc->p_osrel == 0)
56                 return (__elfN(freebsd_copyout_auxargs)(imgp, base));
57
58         args = (Elf_Auxargs *)imgp->auxargs;
59         argarray = pos = malloc(AT_OLD_COUNT * sizeof(*pos), M_TEMP,
60             M_WAITOK | M_ZERO);
61
62         vmspace = imgp->proc->p_vmspace;
63
64         if (args->execfd != -1)
65                 AUXARGS_ENTRY(pos, AT_OLD_EXECFD, args->execfd);
66         AUXARGS_ENTRY(pos, AT_OLD_PHDR, args->phdr);
67         AUXARGS_ENTRY(pos, AT_OLD_PHENT, args->phent);
68         AUXARGS_ENTRY(pos, AT_OLD_PHNUM, args->phnum);
69         AUXARGS_ENTRY(pos, AT_OLD_PAGESZ, args->pagesz);
70         AUXARGS_ENTRY(pos, AT_OLD_FLAGS, args->flags);
71         AUXARGS_ENTRY(pos, AT_OLD_ENTRY, args->entry);
72         AUXARGS_ENTRY(pos, AT_OLD_BASE, args->base);
73         AUXARGS_ENTRY(pos, AT_OLD_EHDRFLAGS, args->hdr_eflags);
74         if (imgp->execpathp != 0)
75                 AUXARGS_ENTRY_PTR(pos, AT_OLD_EXECPATH, imgp->execpathp);
76         AUXARGS_ENTRY(pos, AT_OLD_OSRELDATE,
77             imgp->proc->p_ucred->cr_prison->pr_osreldate);
78         if (imgp->canary != 0) {
79                 AUXARGS_ENTRY_PTR(pos, AT_OLD_CANARY, imgp->canary);
80                 AUXARGS_ENTRY(pos, AT_OLD_CANARYLEN, imgp->canarylen);
81         }
82         AUXARGS_ENTRY(pos, AT_OLD_NCPUS, mp_ncpus);
83         if (imgp->pagesizes != 0) {
84                 AUXARGS_ENTRY_PTR(pos, AT_OLD_PAGESIZES, imgp->pagesizes);
85                 AUXARGS_ENTRY(pos, AT_OLD_PAGESIZESLEN, imgp->pagesizeslen);
86         }
87         if ((imgp->sysent->sv_flags & SV_TIMEKEEP) != 0) {
88                 AUXARGS_ENTRY(pos, AT_OLD_TIMEKEEP,
89                     vmspace->vm_shp_base + imgp->sysent->sv_timekeep_offset);
90         }
91         AUXARGS_ENTRY(pos, AT_OLD_STACKPROT, imgp->sysent->sv_shared_page_obj
92             != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
93             imgp->sysent->sv_stackprot);
94         if (imgp->sysent->sv_hwcap != NULL)
95                 AUXARGS_ENTRY(pos, AT_OLD_HWCAP, *imgp->sysent->sv_hwcap);
96         if (imgp->sysent->sv_hwcap2 != NULL)
97                 AUXARGS_ENTRY(pos, AT_OLD_HWCAP2, *imgp->sysent->sv_hwcap2);
98         AUXARGS_ENTRY(pos, AT_OLD_NULL, 0);
99
100         free(imgp->auxargs, M_TEMP);
101         imgp->auxargs = NULL;
102         KASSERT(pos - argarray <= AT_OLD_COUNT, ("Too many auxargs"));
103
104         error = copyout(argarray, (void *)base, sizeof(*argarray) * AT_OLD_COUNT);
105         free(argarray, M_TEMP);
106         return (error);
107 }