2010-11-19 09:44:33 +03:00
|
|
|
/* $NetBSD: exec_macho.c,v 1.44 2010/11/19 06:44:42 dholland Exp $ */
|
2001-07-14 06:09:41 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2001 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Christos Zoulas.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2001-11-12 18:25:01 +03:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2010-11-19 09:44:33 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: exec_macho.c,v 1.44 2010/11/19 06:44:42 dholland Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
2001-07-14 06:09:41 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/exec_macho.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
|
|
|
|
#ifdef DEBUG_MACHO
|
|
|
|
#define DPRINTF(a) printf a
|
|
|
|
#else
|
|
|
|
#define DPRINTF(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int exec_macho_load_segment(struct exec_package *, struct vnode *,
|
|
|
|
u_long, struct exec_macho_segment_command *, int);
|
2005-12-11 15:16:03 +03:00
|
|
|
static int exec_macho_load_dylinker(struct lwp *, struct exec_package *,
|
2002-11-25 00:59:43 +03:00
|
|
|
struct exec_macho_dylinker_command *, u_long *, int);
|
2005-12-11 15:16:03 +03:00
|
|
|
static int exec_macho_load_dylib(struct lwp *, struct exec_package *,
|
2002-11-25 00:59:43 +03:00
|
|
|
struct exec_macho_dylib_command *, int);
|
2001-07-14 06:09:41 +04:00
|
|
|
static u_long exec_macho_load_thread(struct exec_macho_thread_command *);
|
2005-12-11 15:16:03 +03:00
|
|
|
static int exec_macho_load_file(struct lwp *, struct exec_package *,
|
2002-11-29 18:49:09 +03:00
|
|
|
const char *, u_long *, int, int, int);
|
2005-12-11 15:16:03 +03:00
|
|
|
static int exec_macho_load_vnode(struct lwp *, struct exec_package *,
|
2002-11-29 18:49:09 +03:00
|
|
|
struct vnode *, struct exec_macho_fat_header *, u_long *, int, int, int);
|
2001-07-14 06:09:41 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_MACHO
|
|
|
|
static void
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_print_segment_command(struct exec_macho_segment_command *ls)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
2001-07-14 06:09:41 +04:00
|
|
|
printf("ls.cmd 0x%lx\n", ls->cmd);
|
|
|
|
printf("ls.cmdsize 0x%ld\n", ls->cmdsize);
|
|
|
|
printf("ls.segname %s\n", ls->segname);
|
|
|
|
printf("ls.vmaddr 0x%lx\n", ls->vmaddr);
|
|
|
|
printf("ls.vmsize %ld\n", ls->vmsize);
|
|
|
|
printf("ls.fileoff 0x%lx\n", ls->fileoff);
|
|
|
|
printf("ls.filesize %ld\n", ls->filesize);
|
|
|
|
printf("ls.maxprot 0x%x\n", ls->maxprot);
|
|
|
|
printf("ls.initprot 0x%x\n", ls->initprot);
|
|
|
|
printf("ls.nsects %ld\n", ls->nsects);
|
|
|
|
printf("ls.flags 0x%lx\n", ls->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_print_fat_header(struct exec_macho_fat_header *fat)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
2001-07-14 07:05:31 +04:00
|
|
|
printf("fat.magic 0x%x\n", be32toh(fat->magic));
|
|
|
|
printf("fat.nfat_arch %d\n", be32toh(fat->nfat_arch));
|
2001-07-14 06:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_print_fat_arch(struct exec_macho_fat_arch *arch)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
2001-07-14 07:05:31 +04:00
|
|
|
printf("arch.cputype %x\n", be32toh(arch->cputype));
|
|
|
|
printf("arch.cpusubtype %d\n", be32toh(arch->cpusubtype));
|
2002-10-31 05:40:41 +03:00
|
|
|
printf("arch.offset 0x%x\n", (int32_t)be32toh(arch->offset));
|
|
|
|
printf("arch.size %d\n", (int32_t)be32toh(arch->size));
|
|
|
|
printf("arch.align 0x%x\n", (int32_t)be32toh(arch->align));
|
2001-07-14 06:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_print_object_header(struct exec_macho_object_header *hdr)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
2001-07-14 06:09:41 +04:00
|
|
|
printf("hdr.magic 0x%lx\n", hdr->magic);
|
|
|
|
printf("hdr.cputype %x\n", hdr->cputype);
|
|
|
|
printf("hdr.cpusubtype %d\n", hdr->cpusubtype);
|
|
|
|
printf("hdr.filetype 0x%lx\n", hdr->filetype);
|
|
|
|
printf("hdr.ncmds %ld\n", hdr->ncmds);
|
|
|
|
printf("hdr.sizeofcmds %ld\n", hdr->sizeofcmds);
|
|
|
|
printf("hdr.flags 0x%lx\n", hdr->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_print_load_command(struct exec_macho_load_command *lc)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
2001-07-14 06:09:41 +04:00
|
|
|
printf("lc.cmd %lx\n", lc->cmd);
|
|
|
|
printf("lc.cmdsize %ld\n", lc->cmdsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_print_dylinker_command(struct exec_macho_dylinker_command *dy)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
2001-07-14 06:09:41 +04:00
|
|
|
printf("dy.cmd %lx\n", dy->cmd);
|
|
|
|
printf("dy.cmdsize %ld\n", dy->cmdsize);
|
|
|
|
printf("dy.name.offset 0x%lx\n", dy->name.offset);
|
|
|
|
printf("dy.name %s\n", ((char *)dy) + dy->name.offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_print_dylib_command(struct exec_macho_dylib_command *dy)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
2001-07-14 06:09:41 +04:00
|
|
|
printf("dy.cmd %lx\n", dy->cmd);
|
|
|
|
printf("dy.cmdsize %ld\n", dy->cmdsize);
|
|
|
|
printf("dy.dylib.name.offset 0x%lx\n", dy->dylib.name.offset);
|
|
|
|
printf("dy.dylib.name %s\n", ((char *)dy) + dy->dylib.name.offset);
|
|
|
|
printf("dy.dylib.timestamp %ld\n", dy->dylib.timestamp);
|
|
|
|
printf("dy.dylib.current_version %ld\n", dy->dylib.current_version);
|
|
|
|
printf("dy.dylib.compatibility_version %ld\n",
|
|
|
|
dy->dylib.compatibility_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_print_thread_command(struct exec_macho_thread_command *th)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
2001-07-14 06:09:41 +04:00
|
|
|
printf("th.cmd %lx\n", th->cmd);
|
|
|
|
printf("th.cmdsize %ld\n", th->cmdsize);
|
|
|
|
printf("th.flavor %ld\n", th->flavor);
|
|
|
|
printf("th.count %ld\n", th->count);
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_MACHO */
|
|
|
|
|
|
|
|
static int
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_load_segment(struct exec_package *epp, struct vnode *vp,
|
|
|
|
u_long foff, struct exec_macho_segment_command *ls, int type)
|
2001-07-14 06:09:41 +04:00
|
|
|
{
|
|
|
|
int flags;
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
struct exec_macho_emul_arg *emea;
|
2001-07-14 06:09:41 +04:00
|
|
|
u_long addr = trunc_page(ls->vmaddr), size = round_page(ls->filesize);
|
|
|
|
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
emea = (struct exec_macho_emul_arg *)epp->ep_emul_arg;
|
|
|
|
|
2002-11-23 02:09:46 +03:00
|
|
|
flags = VMCMD_BASE;
|
2001-07-14 06:09:41 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_MACHO
|
|
|
|
exec_macho_print_segment_command(ls);
|
|
|
|
#endif
|
|
|
|
if (strcmp(ls->segname, "__PAGEZERO") == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (strcmp(ls->segname, "__TEXT") != 0 &&
|
|
|
|
strcmp(ls->segname, "__DATA") != 0 &&
|
|
|
|
strcmp(ls->segname, "__LOCK") != 0 &&
|
2002-12-11 22:28:41 +03:00
|
|
|
strcmp(ls->segname, "__OBJC") != 0 &&
|
|
|
|
strcmp(ls->segname, "__CGSERVER") != 0 &&
|
2003-10-21 02:42:40 +04:00
|
|
|
strcmp(ls->segname, "__IMAGE") != 0 &&
|
2001-07-14 06:09:41 +04:00
|
|
|
strcmp(ls->segname, "__LINKEDIT") != 0) {
|
|
|
|
DPRINTF(("Unknown exec_macho segment %s\n", ls->segname));
|
|
|
|
return ENOEXEC;
|
|
|
|
}
|
|
|
|
if (type == MACHO_MOH_EXECUTE) {
|
|
|
|
if (strcmp(ls->segname, "__TEXT") == 0) {
|
|
|
|
epp->ep_taddr = addr;
|
|
|
|
epp->ep_tsize = round_page(ls->vmsize);
|
2004-03-23 16:22:32 +03:00
|
|
|
emea->macho_hdr =
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
(struct exec_macho_object_header *)addr;
|
2001-07-14 06:09:41 +04:00
|
|
|
}
|
2003-10-21 02:42:40 +04:00
|
|
|
if ((strcmp(ls->segname, "__DATA") == 0) ||
|
|
|
|
(strcmp(ls->segname, "__OBJC") == 0) ||
|
|
|
|
(strcmp(ls->segname, "__IMAGE") == 0) ||
|
2002-12-11 22:28:41 +03:00
|
|
|
(strcmp(ls->segname, "__CGSERVER") == 0)) {
|
|
|
|
epp->ep_daddr = addr;
|
|
|
|
epp->ep_dsize = round_page(ls->vmsize);
|
|
|
|
}
|
2001-07-14 06:09:41 +04:00
|
|
|
}
|
|
|
|
|
2004-03-23 16:22:32 +03:00
|
|
|
/*
|
2003-04-21 18:32:25 +04:00
|
|
|
* Some libraries do not have a load base address. The Darwin
|
|
|
|
* kernel seems to skip them, and dyld will do the job.
|
|
|
|
*/
|
|
|
|
if (addr == 0)
|
|
|
|
return ENOMEM;
|
|
|
|
|
2001-07-14 06:09:41 +04:00
|
|
|
if (ls->filesize > 0) {
|
2004-03-23 16:22:32 +03:00
|
|
|
NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_pagedvn, size,
|
2001-07-14 06:09:41 +04:00
|
|
|
addr, vp, (off_t)(ls->fileoff + foff),
|
|
|
|
ls->initprot, flags);
|
|
|
|
DPRINTF(("map(0x%lx, 0x%lx, %o, fd@ 0x%lx)\n",
|
|
|
|
addr, size, ls->initprot,
|
|
|
|
ls->fileoff + foff));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ls->vmsize > size) {
|
|
|
|
addr += size;
|
|
|
|
size = round_page(ls->vmsize - size);
|
2004-03-23 16:22:32 +03:00
|
|
|
NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, size,
|
2001-07-14 06:09:41 +04:00
|
|
|
addr, vp, (off_t)(ls->fileoff + foff),
|
|
|
|
ls->initprot, flags);
|
|
|
|
DPRINTF(("mmap(0x%lx, 0x%lx, %o, zero)\n",
|
|
|
|
ls->vmaddr + ls->filesize, ls->vmsize - ls->filesize,
|
|
|
|
ls->initprot));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2005-12-11 15:16:03 +03:00
|
|
|
exec_macho_load_dylinker(struct lwp *l, struct exec_package *epp,
|
2005-12-08 06:06:31 +03:00
|
|
|
struct exec_macho_dylinker_command *dy, u_long *entry, int depth)
|
2001-07-14 06:09:41 +04:00
|
|
|
{
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
struct exec_macho_emul_arg *emea;
|
2001-07-14 06:09:41 +04:00
|
|
|
const char *name = ((const char *)dy) + dy->name.offset;
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
int error;
|
|
|
|
#ifdef DEBUG_MACHO
|
|
|
|
exec_macho_print_dylinker_command(dy);
|
|
|
|
#endif
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
emea = (struct exec_macho_emul_arg *)epp->ep_emul_arg;
|
|
|
|
|
|
|
|
(void)snprintf(path, sizeof(path), "%s%s", emea->path, name);
|
2001-07-14 06:09:41 +04:00
|
|
|
DPRINTF(("loading linker %s\n", path));
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_macho_load_file(l, epp, path, entry,
|
2002-11-29 18:49:09 +03:00
|
|
|
MACHO_MOH_DYLINKER, 1, depth)) != 0)
|
2001-07-14 06:09:41 +04:00
|
|
|
return error;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-12-11 15:16:03 +03:00
|
|
|
exec_macho_load_dylib(struct lwp *l, struct exec_package *epp,
|
2005-12-08 06:06:31 +03:00
|
|
|
struct exec_macho_dylib_command *dy, int depth)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
struct exec_macho_emul_arg *emea;
|
2001-07-14 06:09:41 +04:00
|
|
|
const char *name = ((const char *)dy) + dy->dylib.name.offset;
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
int error;
|
|
|
|
u_long entry;
|
|
|
|
#ifdef DEBUG_MACHO
|
|
|
|
exec_macho_print_dylib_command(dy);
|
|
|
|
#endif
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
emea = (struct exec_macho_emul_arg *)epp->ep_emul_arg;
|
|
|
|
(void)snprintf(path, sizeof(path), "%s%s", emea->path, name);
|
2001-07-14 06:09:41 +04:00
|
|
|
DPRINTF(("loading library %s\n", path));
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_macho_load_file(l, epp, path, &entry,
|
2002-11-29 18:49:09 +03:00
|
|
|
MACHO_MOH_DYLIB, 0, depth)) != 0)
|
2001-07-14 06:09:41 +04:00
|
|
|
return error;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u_long
|
2005-12-08 06:06:31 +03:00
|
|
|
exec_macho_load_thread(struct exec_macho_thread_command *th)
|
2002-11-23 02:09:46 +03:00
|
|
|
{
|
2001-07-14 06:09:41 +04:00
|
|
|
#ifdef DEBUG_MACHO
|
|
|
|
exec_macho_print_thread_command(th);
|
|
|
|
#endif
|
|
|
|
return exec_macho_thread_entry(th);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exec_macho_load_file(): Load a macho-binary. This is used
|
|
|
|
* for the dynamic linker and library recursive loading.
|
|
|
|
*/
|
|
|
|
static int
|
2005-12-11 15:16:03 +03:00
|
|
|
exec_macho_load_file(struct lwp *l, struct exec_package *epp,
|
2005-12-08 06:06:31 +03:00
|
|
|
const char *path, u_long *entry, int type, int recursive, int depth)
|
2001-07-14 06:09:41 +04:00
|
|
|
{
|
|
|
|
int error;
|
2010-11-19 09:44:33 +03:00
|
|
|
struct pathbuf *pb;
|
2001-07-14 06:09:41 +04:00
|
|
|
struct nameidata nd;
|
|
|
|
struct vnode *vp;
|
|
|
|
struct vattr attr;
|
|
|
|
struct exec_macho_fat_header fat;
|
2002-11-22 01:30:32 +03:00
|
|
|
|
|
|
|
/*
|
2002-11-25 00:59:43 +03:00
|
|
|
* Check for excessive rercursive loading
|
2002-11-22 01:30:32 +03:00
|
|
|
*/
|
2002-11-25 00:59:43 +03:00
|
|
|
if (depth++ > 6)
|
2002-11-22 01:30:32 +03:00
|
|
|
return E2BIG;
|
2004-03-23 16:22:32 +03:00
|
|
|
|
2001-07-14 06:09:41 +04:00
|
|
|
/*
|
|
|
|
* 1. open file
|
|
|
|
* 2. read filehdr
|
|
|
|
* 3. map text, data, and bss out of it using VM_*
|
|
|
|
*/
|
2010-11-19 09:44:33 +03:00
|
|
|
pb = pathbuf_create(path);
|
|
|
|
if (pb == NULL) {
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, pb);
|
|
|
|
if ((error = namei(&nd)) != 0) {
|
|
|
|
pathbuf_destroy(pb);
|
2001-07-14 06:09:41 +04:00
|
|
|
return error;
|
2010-11-19 09:44:33 +03:00
|
|
|
}
|
2001-07-14 06:09:41 +04:00
|
|
|
vp = nd.ni_vp;
|
2010-11-19 09:44:33 +03:00
|
|
|
pathbuf_destroy(pb);
|
2001-07-14 06:09:41 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Similarly, if it's not marked as executable, or it's not a regular
|
|
|
|
* file, we don't allow it to be used.
|
|
|
|
*/
|
|
|
|
if (vp->v_type != VREG) {
|
|
|
|
error = EACCES;
|
|
|
|
goto badunlock;
|
|
|
|
}
|
|
|
|
|
2002-10-06 02:34:02 +04:00
|
|
|
error = vn_marktext(vp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2001-07-14 06:09:41 +04:00
|
|
|
|
2007-11-26 22:01:26 +03:00
|
|
|
if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
|
2001-07-14 06:09:41 +04:00
|
|
|
goto badunlock;
|
|
|
|
|
|
|
|
/* get attributes */
|
2007-11-26 22:01:26 +03:00
|
|
|
if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
|
2001-07-14 06:09:41 +04:00
|
|
|
goto badunlock;
|
|
|
|
|
|
|
|
#ifdef notyet /* XXX cgd 960926 */
|
|
|
|
XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
|
|
|
|
#endif
|
2010-06-24 16:58:48 +04:00
|
|
|
VOP_UNLOCK(vp);
|
2001-07-14 06:09:41 +04:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_read_from(l, vp, 0, &fat, sizeof(fat))) != 0)
|
2001-07-14 06:09:41 +04:00
|
|
|
goto bad;
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_macho_load_vnode(l, epp, vp, &fat,
|
2002-11-29 18:49:09 +03:00
|
|
|
entry, type, recursive, depth)) != 0)
|
2001-07-14 06:09:41 +04:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
vrele(vp);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
badunlock:
|
2010-06-24 16:58:48 +04:00
|
|
|
VOP_UNLOCK(vp);
|
2001-07-14 06:09:41 +04:00
|
|
|
|
|
|
|
bad:
|
|
|
|
#ifdef notyet /* XXX cgd 960926 */
|
|
|
|
(maybe) VOP_CLOSE it
|
|
|
|
#endif
|
|
|
|
vrele(vp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exec_macho_load_vnode(): Map a file from the given vnode.
|
|
|
|
* The fat signature is checked,
|
|
|
|
* and it will return the address of the entry point in entry.
|
|
|
|
* The type determines what we are loading, a dynamic linker,
|
|
|
|
* a dynamic library, or a binary. We use that to guess at
|
|
|
|
* the entry point.
|
|
|
|
*/
|
|
|
|
static int
|
2005-12-11 15:16:03 +03:00
|
|
|
exec_macho_load_vnode(struct lwp *l, struct exec_package *epp,
|
2005-12-08 06:06:31 +03:00
|
|
|
struct vnode *vp, struct exec_macho_fat_header *fat, u_long *entry,
|
|
|
|
int type, int recursive, int depth)
|
2001-07-14 06:09:41 +04:00
|
|
|
{
|
|
|
|
u_long aoffs, offs;
|
|
|
|
struct exec_macho_fat_arch arch;
|
|
|
|
struct exec_macho_object_header hdr;
|
|
|
|
struct exec_macho_load_command lc;
|
2003-10-19 11:52:22 +04:00
|
|
|
struct exec_macho_emul_arg *emea;
|
2001-07-14 06:09:41 +04:00
|
|
|
int error = ENOEXEC, i;
|
|
|
|
size_t size;
|
2005-05-30 02:24:14 +04:00
|
|
|
void *bf = &lc;
|
2005-12-26 21:41:36 +03:00
|
|
|
uint32_t *sc = NULL;
|
2001-07-14 06:09:41 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_MACHO
|
|
|
|
exec_macho_print_fat_header(fat);
|
|
|
|
#endif
|
|
|
|
|
2005-06-25 06:22:57 +04:00
|
|
|
switch (fat->magic) {
|
2004-03-23 16:22:32 +03:00
|
|
|
case MACHO_FAT_MAGIC:
|
2002-10-29 22:28:19 +03:00
|
|
|
for (i = 0; i < be32toh(fat->nfat_arch); i++, arch) {
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_read_from(l, vp, sizeof(*fat) +
|
2002-10-29 22:28:19 +03:00
|
|
|
sizeof(arch) * i, &arch, sizeof(arch))) != 0)
|
|
|
|
goto bad;
|
2001-07-14 06:09:41 +04:00
|
|
|
#ifdef DEBUG_MACHO
|
2002-10-29 22:28:19 +03:00
|
|
|
exec_macho_print_fat_arch(&arch);
|
2001-07-14 06:09:41 +04:00
|
|
|
#endif
|
2002-10-30 01:22:30 +03:00
|
|
|
for (sc = exec_macho_supported_cpu; *sc; sc++)
|
|
|
|
if (*sc == be32toh(arch.cputype))
|
|
|
|
break;
|
2003-09-07 15:16:59 +04:00
|
|
|
|
2004-03-23 16:22:32 +03:00
|
|
|
if (sc != NULL)
|
2003-09-07 15:16:59 +04:00
|
|
|
break;
|
2002-10-30 18:21:01 +03:00
|
|
|
}
|
|
|
|
if (sc == NULL || *sc == 0) {
|
|
|
|
DPRINTF(("CPU %d not supported by this binary",
|
|
|
|
be32toh(arch.cputype)));
|
|
|
|
goto bad;
|
2001-07-14 06:09:41 +04:00
|
|
|
}
|
2002-10-29 22:28:19 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MACHO_MOH_MAGIC:
|
2004-03-23 16:22:32 +03:00
|
|
|
/*
|
2002-10-29 22:28:19 +03:00
|
|
|
* This is not a FAT Mach-O binary, the file starts
|
|
|
|
* with the object header.
|
|
|
|
*/
|
|
|
|
arch.offset = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DPRINTF(("bad exec_macho magic %x\n", fat->magic));
|
|
|
|
goto bad;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_read_from(l, vp, be32toh(arch.offset), &hdr,
|
2001-07-14 06:09:41 +04:00
|
|
|
sizeof(hdr))) != 0)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
if (hdr.magic != MACHO_MOH_MAGIC) {
|
|
|
|
DPRINTF(("bad exec_macho header magic %lx\n", hdr.magic));
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_MACHO
|
|
|
|
exec_macho_print_object_header(&hdr);
|
|
|
|
#endif
|
|
|
|
switch (hdr.filetype) {
|
|
|
|
case MACHO_MOH_PRELOAD:
|
|
|
|
case MACHO_MOH_EXECUTE:
|
|
|
|
case MACHO_MOH_DYLINKER:
|
|
|
|
case MACHO_MOH_DYLIB:
|
2004-09-05 03:21:26 +04:00
|
|
|
case MACHO_MOH_BUNDLE:
|
2001-07-14 06:09:41 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(("Unsupported exec_macho filetype 0x%lx\n",
|
|
|
|
hdr.filetype));
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2004-03-23 16:22:32 +03:00
|
|
|
|
2001-07-14 07:05:31 +04:00
|
|
|
aoffs = be32toh(arch.offset);
|
2001-07-14 06:09:41 +04:00
|
|
|
offs = aoffs + sizeof(hdr);
|
|
|
|
size = sizeof(lc);
|
|
|
|
for (i = 0; i < hdr.ncmds; i++) {
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_read_from(l, vp, offs, &lc, sizeof(lc))) != 0)
|
2001-07-14 06:09:41 +04:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
#ifdef DEBUG_MACHO
|
|
|
|
exec_macho_print_load_command(&lc);
|
|
|
|
#endif
|
|
|
|
if (size < lc.cmdsize) {
|
|
|
|
if (lc.cmdsize > 4096) {
|
|
|
|
DPRINTF(("Bad command size %ld\n", lc.cmdsize));
|
|
|
|
goto bad;
|
|
|
|
}
|
2005-05-30 02:24:14 +04:00
|
|
|
if (bf != &lc)
|
|
|
|
free(bf, M_TEMP);
|
|
|
|
bf = malloc(size = lc.cmdsize, M_TEMP, M_WAITOK);
|
2001-07-14 06:09:41 +04:00
|
|
|
}
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_read_from(l, vp, offs, bf, lc.cmdsize)) != 0)
|
2001-07-14 06:09:41 +04:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
switch (lc.cmd) {
|
|
|
|
case MACHO_LC_SEGMENT:
|
2004-03-23 16:22:32 +03:00
|
|
|
error = exec_macho_load_segment(epp, vp, aoffs,
|
2005-05-30 02:24:14 +04:00
|
|
|
(struct exec_macho_segment_command *)bf, type);
|
2003-04-21 18:32:25 +04:00
|
|
|
|
|
|
|
switch(error) {
|
|
|
|
case ENOMEM: /* Just skip, dyld will load it */
|
|
|
|
DPRINTF(("load segment failed, skipping\n"));
|
|
|
|
i = hdr.ncmds;
|
|
|
|
break;
|
|
|
|
case 0: /* No error, carry on loading file */
|
|
|
|
break;
|
|
|
|
default: /* Abort file load */
|
|
|
|
DPRINTF(("load segment failed, aborting\n"));
|
2001-07-14 06:09:41 +04:00
|
|
|
goto bad;
|
2003-04-21 18:32:25 +04:00
|
|
|
break;
|
2001-07-14 06:09:41 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MACHO_LC_LOAD_DYLINKER:
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_macho_load_dylinker(l, epp,
|
2005-05-30 02:24:14 +04:00
|
|
|
(struct exec_macho_dylinker_command *)bf,
|
2002-11-25 00:59:43 +03:00
|
|
|
entry, depth)) != 0) {
|
2001-07-14 06:09:41 +04:00
|
|
|
DPRINTF(("load linker failed\n"));
|
|
|
|
goto bad;
|
|
|
|
}
|
2003-10-19 11:52:22 +04:00
|
|
|
emea = (struct exec_macho_emul_arg *)epp->ep_emul_arg;
|
|
|
|
emea->dynamic = 1;
|
2001-07-14 06:09:41 +04:00
|
|
|
break;
|
|
|
|
case MACHO_LC_LOAD_DYLIB:
|
2004-03-23 16:22:32 +03:00
|
|
|
/*
|
|
|
|
* We should only load libraries required by the
|
|
|
|
* binary we want to load, not libraries required
|
2002-11-29 18:49:09 +03:00
|
|
|
* by theses libraries.
|
|
|
|
*/
|
|
|
|
if (recursive == 0)
|
|
|
|
break;
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_macho_load_dylib(l, epp,
|
2005-05-30 02:24:14 +04:00
|
|
|
(struct exec_macho_dylib_command *)bf,
|
2002-11-25 00:59:43 +03:00
|
|
|
depth)) != 0) {
|
2001-07-14 06:09:41 +04:00
|
|
|
DPRINTF(("load dylib failed\n"));
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MACHO_LC_THREAD:
|
|
|
|
case MACHO_LC_UNIXTHREAD:
|
|
|
|
if (type == MACHO_MOH_DYLINKER || *entry == 0) {
|
|
|
|
*entry = exec_macho_load_thread(
|
2005-05-30 02:24:14 +04:00
|
|
|
(struct exec_macho_thread_command *)bf);
|
2001-07-14 06:09:41 +04:00
|
|
|
} else {
|
|
|
|
(void)exec_macho_load_thread(
|
2005-05-30 02:24:14 +04:00
|
|
|
(struct exec_macho_thread_command *)bf);
|
2001-07-14 06:09:41 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MACHO_LC_ID_DYLINKER:
|
|
|
|
case MACHO_LC_ID_DYLIB:
|
|
|
|
case MACHO_LC_SYMTAB:
|
|
|
|
case MACHO_LC_DYSYMTAB:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(("Unhandled exec_macho command 0x%lx\n",
|
|
|
|
lc.cmd));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offs += lc.cmdsize;
|
|
|
|
}
|
|
|
|
error = 0;
|
|
|
|
bad:
|
2005-05-30 02:24:14 +04:00
|
|
|
if (bf != &lc)
|
|
|
|
free(bf, M_TEMP);
|
2001-07-14 06:09:41 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exec_macho_makecmds(): Prepare an Mach-O binary's exec package
|
|
|
|
*
|
|
|
|
* First, set of the various offsets/lengths in the exec package.
|
|
|
|
*
|
|
|
|
* Then, mark the text image busy (so it can be demand paged) or error
|
|
|
|
* out if this is not possible. Finally, set up vmcmds for the
|
|
|
|
* text, data, bss, and stack segments.
|
|
|
|
*/
|
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
exec_macho_makecmds(struct lwp *l, struct exec_package *epp)
|
2001-07-14 06:09:41 +04:00
|
|
|
{
|
|
|
|
struct exec_macho_fat_header *fat = epp->ep_hdr;
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
struct exec_macho_emul_arg *emea;
|
2001-07-14 06:09:41 +04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (epp->ep_hdrvalid < sizeof(*fat))
|
|
|
|
return ENOEXEC;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check mount point. Though we're not trying to exec this binary,
|
|
|
|
* we will be executing code from it, so if the mount point
|
|
|
|
* disallows execution or set-id-ness, we punt or kill the set-id.
|
|
|
|
*/
|
|
|
|
if (epp->ep_vp->v_mount->mnt_flag & MNT_NOEXEC)
|
|
|
|
return EACCES;
|
|
|
|
|
|
|
|
if (epp->ep_vp->v_mount->mnt_flag & MNT_NOSUID)
|
|
|
|
epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
|
|
|
|
|
2002-10-06 02:34:02 +04:00
|
|
|
error = vn_marktext(epp->ep_vp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2001-07-14 06:09:41 +04:00
|
|
|
|
2008-01-03 17:25:49 +03:00
|
|
|
emea = malloc(sizeof(struct exec_macho_emul_arg), M_TEMP, M_WAITOK);
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
epp->ep_emul_arg = (void *)emea;
|
2003-10-19 11:52:22 +04:00
|
|
|
emea->dynamic = 0;
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
|
2001-07-14 06:09:41 +04:00
|
|
|
if (!epp->ep_esch->u.mach_probe_func)
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
emea->path = "/";
|
2001-07-14 06:09:41 +04:00
|
|
|
else {
|
2005-05-30 02:24:14 +04:00
|
|
|
if ((error = (*epp->ep_esch->u.mach_probe_func)(&emea->path)) != 0)
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
goto bad2;
|
2001-07-14 06:09:41 +04:00
|
|
|
}
|
2003-10-19 11:52:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the underlying functions will not get
|
|
|
|
* a random value here. 0 means that no entry point
|
|
|
|
* has been found yet.
|
|
|
|
*/
|
|
|
|
epp->ep_entry = 0;
|
2004-03-23 16:22:32 +03:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = exec_macho_load_vnode(l, epp, epp->ep_vp, fat,
|
2002-11-29 18:49:09 +03:00
|
|
|
&epp->ep_entry, MACHO_MOH_EXECUTE, 1, 0)) != 0)
|
2001-07-14 06:09:41 +04:00
|
|
|
goto bad;
|
|
|
|
|
2001-10-28 07:46:42 +03:00
|
|
|
/*
|
|
|
|
* stash a copy of the program name in epp->ep_emul_arg because
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
* we will need it later.
|
2001-10-28 07:46:42 +03:00
|
|
|
*/
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
if ((error = copyinstr(epp->ep_name, emea->filename,
|
2001-10-28 07:46:42 +03:00
|
|
|
MAXPATHLEN, NULL)) != 0) {
|
|
|
|
DPRINTF(("Copyinstr %p failed\n", epp->ep_name));
|
|
|
|
goto bad;
|
|
|
|
}
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
return (*epp->ep_esch->es_setup_stack)(l, epp);
|
2001-07-14 06:09:41 +04:00
|
|
|
bad:
|
|
|
|
kill_vmcmds(&epp->ep_vmcmds);
|
We now have the exact stack initial stack layout of Darwin:
macho_hdr, argc, *argv, NULL, *envp, NULL, progname, NULL,
*progname, **argv, **envp
Where progname is a pointer to the program name as given in the first
argument to execve(), and macho_hdr a pointer to the Mach-O header at
the beginning of the executable file.
2002-11-21 22:53:40 +03:00
|
|
|
bad2:
|
2008-01-03 17:25:49 +03:00
|
|
|
free(emea, M_TEMP);
|
2001-07-14 06:09:41 +04:00
|
|
|
return error;
|
|
|
|
}
|