The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/linux/common/linux_exec_elf32.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: linux_exec_elf32.c,v 1.66 2003/10/31 14:04:36 drochner Exp $   */
    2 
    3 /*-
    4  * Copyright (c) 1995, 1998, 2000, 2001 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
    9  * Emmanuel Dreyfus.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the NetBSD
   22  *      Foundation, Inc. and its contributors.
   23  * 4. Neither the name of The NetBSD Foundation nor the names of its
   24  *    contributors may be used to endorse or promote products derived
   25  *    from this software without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 /*
   41  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
   42  */
   43 
   44 #include <sys/cdefs.h>
   45 __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.66 2003/10/31 14:04:36 drochner Exp $");
   46 
   47 #ifndef ELFSIZE
   48 /* XXX should die */
   49 #define ELFSIZE         32
   50 #endif
   51 
   52 #include <sys/param.h>
   53 #include <sys/systm.h>
   54 #include <sys/kernel.h>
   55 #include <sys/proc.h>
   56 #include <sys/malloc.h>
   57 #include <sys/namei.h>
   58 #include <sys/vnode.h>
   59 #include <sys/mount.h>
   60 #include <sys/exec.h>
   61 #include <sys/exec_elf.h>
   62 #include <sys/stat.h>
   63 
   64 #include <sys/mman.h>
   65 #include <sys/sa.h>
   66 #include <sys/syscallargs.h>
   67 
   68 #include <machine/cpu.h>
   69 #include <machine/reg.h>
   70 
   71 #include <compat/linux/common/linux_types.h>
   72 #include <compat/linux/common/linux_signal.h>
   73 #include <compat/linux/common/linux_util.h>
   74 #include <compat/linux/common/linux_exec.h>
   75 #include <compat/linux/common/linux_machdep.h>
   76 
   77 #include <compat/linux/linux_syscallargs.h>
   78 #include <compat/linux/linux_syscall.h>
   79 
   80 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
   81         Elf_Ehdr *, char *));
   82 #ifdef LINUX_GCC_SIGNATURE
   83 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
   84         struct exec_package *, Elf_Ehdr *));
   85 #endif
   86 #ifdef LINUX_ATEXIT_SIGNATURE
   87 static int ELFNAME2(linux,atexit_signature) __P((struct proc *p,
   88         struct exec_package *, Elf_Ehdr *));
   89 #endif
   90 
   91 #ifdef DEBUG_LINUX
   92 #define DPRINTF(a)      uprintf a
   93 #else
   94 #define DPRINTF(a)
   95 #endif
   96 
   97 #ifdef LINUX_ATEXIT_SIGNATURE
   98 /*
   99  * On the PowerPC, statically linked Linux binaries are not recognized
  100  * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
  101  * binaries features a __libc_atexit ELF section. We therefore assume we
  102  * have a Linux binary if we find this section.
  103  */
  104 static int
  105 ELFNAME2(linux,atexit_signature)(p, epp, eh)
  106         struct proc *p;
  107         struct exec_package *epp;
  108         Elf_Ehdr *eh;
  109 {
  110         size_t shsize;
  111         int strndx;
  112         size_t i;
  113         static const char signature[] = "__libc_atexit";
  114         char *strtable = NULL;
  115         Elf_Shdr *sh;
  116         
  117         int error;
  118 
  119         /*
  120          * load the section header table 
  121          */
  122         shsize = eh->e_shnum * sizeof(Elf_Shdr);
  123         sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
  124         error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
  125         if (error)
  126                 goto out;
  127 
  128         /* 
  129          * Now let's find the string table. If it does not exists, give up.
  130          */
  131         strndx = (int)(eh->e_shstrndx);
  132         if (strndx == SHN_UNDEF) {
  133                 error = ENOEXEC;
  134                 goto out;
  135         }
  136 
  137         /*
  138          * strndx is the index in section header table of the string table
  139          * section get the whole string table in strtable, and then we get access to the names
  140          * s->sh_name is the offset of the section name in strtable.
  141          */
  142         strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
  143         error = exec_read_from(p, epp->ep_vp, sh[strndx].sh_offset, strtable,
  144             sh[strndx].sh_size);
  145         if (error)
  146                 goto out;
  147 
  148         for (i = 0; i < eh->e_shnum; i++) {
  149                 Elf_Shdr *s = &sh[i];
  150                 if (!memcmp((void*)(&(strtable[s->sh_name])), signature, 
  151                                 sizeof(signature))) {
  152                         DPRINTF(("linux_atexit_sig=%s\n",
  153                             &(strtable[s->sh_name])));
  154                         error = 0;
  155                         goto out;
  156                 }
  157         }
  158         error = ENOEXEC;
  159 
  160 out:
  161         free(sh, M_TEMP);
  162         if (strtable)
  163                 free(strtable, M_TEMP);
  164         return (error);
  165 }
  166 #endif
  167 
  168 #ifdef LINUX_GCC_SIGNATURE
  169 /*
  170  * Take advantage of the fact that all the linux binaries are compiled
  171  * with gcc, and gcc sticks in the comment field a signature. Note that
  172  * on SVR4 binaries, the gcc signature will follow the OS name signature,
  173  * that will not be a problem. We don't bother to read in the string table,
  174  * but we check all the progbits headers.
  175  *
  176  * XXX This only works in the i386.  On the alpha (at least)
  177  * XXX we have the same gcc signature which incorrectly identifies
  178  * XXX NetBSD binaries as Linux.
  179  */
  180 static int
  181 ELFNAME2(linux,gcc_signature)(p, epp, eh)
  182         struct proc *p;
  183         struct exec_package *epp;
  184         Elf_Ehdr *eh;
  185 {
  186         size_t shsize;
  187         size_t i;
  188         static const char signature[] = "\0GCC: (GNU) ";
  189         char buf[sizeof(signature) - 1];
  190         Elf_Shdr *sh;
  191         int error;
  192 
  193         shsize = eh->e_shnum * sizeof(Elf_Shdr);
  194         sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
  195         error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
  196         if (error)
  197                 goto out;
  198 
  199         for (i = 0; i < eh->e_shnum; i++) {
  200                 Elf_Shdr *s = &sh[i];
  201 
  202                 /*
  203                  * Identify candidates for the comment header;
  204                  * Header cannot have a load address, or flags and
  205                  * it must be large enough.
  206                  */
  207                 if (s->sh_type != SHT_PROGBITS ||
  208                     s->sh_addr != 0 ||
  209                     s->sh_flags != 0 ||
  210                     s->sh_size < sizeof(signature) - 1)
  211                         continue;
  212 
  213                 error = exec_read_from(p, epp->ep_vp, s->sh_offset, buf,
  214                     sizeof(signature) - 1);
  215                 if (error)
  216                         continue;
  217 
  218                 /*
  219                  * error is 0, if the signatures match we are done.
  220                  */
  221                 DPRINTF(("linux_gcc_sig: sig=%s\n", buf));
  222                 if (!memcmp(buf, signature, sizeof(signature) - 1)) {
  223                         error = 0;
  224                         goto out;
  225                 }
  226         }
  227         error = ENOEXEC;
  228 
  229 out:
  230         free(sh, M_TEMP);
  231         return (error);
  232 }
  233 #endif
  234 
  235 static int
  236 ELFNAME2(linux,signature)(p, epp, eh, itp)
  237         struct proc *p;
  238         struct exec_package *epp;
  239         Elf_Ehdr *eh;
  240         char *itp;
  241 {
  242         size_t i;
  243         Elf_Phdr *ph;
  244         size_t phsize;
  245         int error;
  246         static const char linux[] = "Linux";
  247 
  248         if (eh->e_ident[EI_OSABI] == 3 ||
  249             memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
  250                 return 0;
  251 
  252         phsize = eh->e_phnum * sizeof(Elf_Phdr);
  253         ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
  254         error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
  255         if (error)
  256                 goto out;
  257 
  258         for (i = 0; i < eh->e_phnum; i++) {
  259                 Elf_Phdr *ephp = &ph[i];
  260                 Elf_Nhdr *np;
  261                 u_int32_t *abi;
  262 
  263                 if (ephp->p_type != PT_NOTE ||
  264                     ephp->p_filesz > 1024 ||
  265                     ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
  266                         continue;
  267 
  268                 np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
  269                 error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
  270                     ephp->p_filesz);
  271                 if (error)
  272                         goto next;
  273 
  274                 if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
  275                     np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
  276                     np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
  277                     memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
  278                     ELF_NOTE_ABI_NAMESZ))
  279                         goto next;
  280 
  281                 /* Make sure the OS is Linux. */
  282                 abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
  283                     np->n_namesz);
  284                 if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
  285                         error = 0;
  286                 else
  287                         error = ENOEXEC;
  288                 free(np, M_TEMP);
  289                 goto out;
  290 
  291         next:
  292                 free(np, M_TEMP);
  293                 continue;
  294         }
  295 
  296         /* Check for certain intepreter names. */
  297         if (itp) {
  298                 if (!strncmp(itp, "/lib/ld-linux", 13) ||
  299                     !strncmp(itp, "/lib/ld.so.", 11))
  300                         error = 0;
  301                 else
  302                         error = ENOEXEC;
  303                 goto out;
  304         }
  305 
  306         error = ENOEXEC;
  307 out:
  308         free(ph, M_TEMP);
  309         return (error);
  310 }
  311 
  312 int
  313 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
  314         struct proc *p;
  315         struct exec_package *epp;
  316         void *eh;
  317         char *itp;
  318         vaddr_t *pos;
  319 {
  320         int error;
  321 
  322         if (((error = ELFNAME2(linux,signature)(p, epp, eh, itp)) != 0) &&
  323 #ifdef LINUX_GCC_SIGNATURE
  324             ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0) &&
  325 #endif
  326 #ifdef LINUX_ATEXIT_SIGNATURE
  327             ((error = ELFNAME2(linux,atexit_signature)(p, epp, eh)) != 0) &&
  328 #endif
  329             1) 
  330                         return error;
  331 
  332         if (itp) {
  333                 if ((error = emul_find_interp(p, epp->ep_esch->es_emul->e_path,
  334                     itp)))
  335                         return (error);
  336         }
  337         DPRINTF(("linux_probe: returning 0\n"));
  338         return 0;
  339 }
  340 
  341 #ifndef LINUX_MACHDEP_ELF_COPYARGS
  342 /*
  343  * Copy arguments onto the stack in the normal way, but add some
  344  * extra information in case of dynamic binding.
  345  */
  346 int
  347 ELFNAME2(linux,copyargs)(struct proc *p, struct exec_package *pack,
  348     struct ps_strings *arginfo, char **stackp, void *argp)
  349 {
  350         size_t len;
  351         AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
  352         struct elf_args *ap;
  353         int error;
  354         struct vattr *vap;
  355 
  356         if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0)
  357                 return error;
  358 
  359         a = ai;
  360 
  361         /*
  362          * Push extra arguments used by glibc on the stack.
  363          */
  364 
  365         a->a_type = AT_PAGESZ;
  366         a->a_v = PAGE_SIZE;
  367         a++;
  368 
  369         if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
  370 
  371                 a->a_type = AT_PHDR;
  372                 a->a_v = ap->arg_phaddr;
  373                 a++;
  374 
  375                 a->a_type = AT_PHENT;
  376                 a->a_v = ap->arg_phentsize;
  377                 a++;
  378 
  379                 a->a_type = AT_PHNUM;
  380                 a->a_v = ap->arg_phnum;
  381                 a++;
  382 
  383                 a->a_type = AT_BASE;
  384                 a->a_v = ap->arg_interp;
  385                 a++;
  386 
  387                 a->a_type = AT_FLAGS;
  388                 a->a_v = 0;
  389                 a++;
  390 
  391                 a->a_type = AT_ENTRY;
  392                 a->a_v = ap->arg_entry;
  393                 a++;
  394 
  395                 free(pack->ep_emul_arg, M_TEMP);
  396                 pack->ep_emul_arg = NULL;
  397         }
  398 
  399         /* Linux-specific items */
  400         a->a_type = LINUX_AT_CLKTCK;
  401         a->a_v = hz;
  402         a++;
  403 
  404         vap = pack->ep_vap;
  405 
  406         a->a_type = LINUX_AT_UID;
  407         a->a_v = p->p_cred->p_ruid;
  408         a++;
  409 
  410         a->a_type = LINUX_AT_EUID;
  411         if (vap->va_mode & S_ISUID)
  412                 a->a_v = vap->va_uid;
  413         else
  414                 a->a_v = p->p_ucred->cr_uid;
  415         a++;
  416 
  417         a->a_type = LINUX_AT_GID;
  418         a->a_v = p->p_cred->p_rgid;
  419         a++;
  420 
  421         a->a_type = LINUX_AT_EGID;
  422         if (vap->va_mode & S_ISGID)
  423                 a->a_v = vap->va_gid;
  424         else
  425                 a->a_v = p->p_ucred->cr_gid;
  426         a++;
  427 
  428         a->a_type = AT_NULL;
  429         a->a_v = 0;
  430         a++;
  431 
  432         len = (a - ai) * sizeof(AuxInfo);
  433         if ((error = copyout(ai, *stackp, len)) != 0)
  434                 return error;
  435         *stackp += len;
  436 
  437         return 0;
  438 }
  439 #endif /* !LINUX_MACHDEP_ELF_COPYARGS */

Cache object: aca2c10cd038c2fd534727fc2082c6f5


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.