Index: /trunk/src/VBox/Runtime/Makefile.kmk
===================================================================
--- /trunk/src/VBox/Runtime/Makefile.kmk	(revision 37559)
+++ /trunk/src/VBox/Runtime/Makefile.kmk	(revision 37560)
@@ -1741,4 +1741,5 @@
 	r0drv/darwin/assert-r0drv-darwin.cpp \
 	r0drv/darwin/initterm-r0drv-darwin.cpp \
+	r0drv/darwin/mach_kernel-r0drv-darwin.cpp \
 	r0drv/darwin/memobj-r0drv-darwin.cpp \
 	r0drv/darwin/mp-r0drv-darwin.cpp \
Index: /trunk/src/VBox/Runtime/include/internal/ldrELF.h
===================================================================
--- /trunk/src/VBox/Runtime/include/internal/ldrELF.h	(revision 37559)
+++ /trunk/src/VBox/Runtime/include/internal/ldrELF.h	(revision 37560)
@@ -83,4 +83,4 @@
 #endif
 
-#endif  /* ___internal_ldrELF_h */
+#endif /* !___internal_ldrELF_h */
 
Index: /trunk/src/VBox/Runtime/include/internal/ldrELF32.h
===================================================================
--- /trunk/src/VBox/Runtime/include/internal/ldrELF32.h	(revision 37559)
+++ /trunk/src/VBox/Runtime/include/internal/ldrELF32.h	(revision 37560)
@@ -185,4 +185,4 @@
 
 
-#endif /* ___internal_ldrELF32_h */
+#endif /* !___internal_ldrELF32_h */
 
Index: /trunk/src/VBox/Runtime/include/internal/ldrELF64.h
===================================================================
--- /trunk/src/VBox/Runtime/include/internal/ldrELF64.h	(revision 37559)
+++ /trunk/src/VBox/Runtime/include/internal/ldrELF64.h	(revision 37560)
@@ -184,4 +184,4 @@
 
 
-#endif /* ___internal_ldrELF64_h */
+#endif /* !___internal_ldrELF64_h */
 
Index: /trunk/src/VBox/Runtime/include/internal/ldrMach-O.h
===================================================================
--- /trunk/src/VBox/Runtime/include/internal/ldrMach-O.h	(revision 37560)
+++ /trunk/src/VBox/Runtime/include/internal/ldrMach-O.h	(revision 37560)
@@ -0,0 +1,601 @@
+/* $Id$ */
+/** @file
+ * IPRT - Mach-O Structures and Constants.
+ */
+
+/*
+ * Copyright (C) 2011 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * The contents of this file may alternatively be used under the terms
+ * of the Common Development and Distribution License Version 1.0
+ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+ * VirtualBox OSE distribution, in which case the provisions of the
+ * CDDL are applicable instead of those of the GPL.
+ *
+ * You may elect to license modified versions of this file under the
+ * terms and conditions of either the GPL or the CDDL or both.
+ */
+
+#ifndef ___internal_ldrMach_O_h
+#define ___internal_ldrMach_O_h
+
+#include <iprt/types.h>
+
+#ifndef CPU_ARCH_MASK
+
+/* cputype */
+#define CPU_ARCH_MASK               INT32_C(0xff000000)
+#define CPU_ARCH_ABI64              INT32_C(0x01000000)
+#define CPU_TYPE_ANY                INT32_C(-1)
+#define CPU_TYPE_VAX                INT32_C(1)
+#define CPU_TYPE_MC680x0            INT32_C(6)
+#define CPU_TYPE_X86                INT32_C(7)
+#define CPU_TYPE_I386               CPU_TYPE_X86
+#define CPU_TYPE_X86_64             (CPU_TYPE_X86 | CPU_ARCH_ABI64)
+#define CPU_TYPE_MC98000            INT32_C(10)
+#define CPU_TYPE_HPPA               INT32_C(11)
+#define CPU_TYPE_MC88000            INT32_C(13)
+#define CPU_TYPE_SPARC              INT32_C(14)
+#define CPU_TYPE_I860               INT32_C(15)
+#define CPU_TYPE_POWERPC            INT32_C(18)
+#define CPU_TYPE_POWERPC64          (CPU_TYPE_POWERPC | CPU_ARCH_ABI64)
+
+/* cpusubtype */
+#define CPU_SUBTYPE_MULTIPLE        INT32_C(-1)
+#define CPU_SUBTYPE_LITTLE_ENDIAN   INT32_C(0)
+#define CPU_SUBTYPE_BIG_ENDIAN      INT32_C(1)
+
+#define CPU_SUBTYPE_VAX_ALL         INT32_C(0)
+#define CPU_SUBTYPE_VAX780          INT32_C(1)
+#define CPU_SUBTYPE_VAX785          INT32_C(2)
+#define CPU_SUBTYPE_VAX750          INT32_C(3)
+#define CPU_SUBTYPE_VAX730          INT32_C(4)
+#define CPU_SUBTYPE_UVAXI           INT32_C(5)
+#define CPU_SUBTYPE_UVAXII          INT32_C(6)
+#define CPU_SUBTYPE_VAX8200         INT32_C(7)
+#define CPU_SUBTYPE_VAX8500         INT32_C(8)
+#define CPU_SUBTYPE_VAX8600         INT32_C(9)
+#define CPU_SUBTYPE_VAX8650         INT32_C(10)
+#define CPU_SUBTYPE_VAX8800         INT32_C(11)
+#define CPU_SUBTYPE_UVAXIII         INT32_C(12)
+
+#define CPU_SUBTYPE_MC680x0_ALL     INT32_C(1)
+#define CPU_SUBTYPE_MC68030         INT32_C(1)
+#define CPU_SUBTYPE_MC68040         INT32_C(2)
+#define CPU_SUBTYPE_MC68030_ONLY    INT32_C(3)
+
+#define CPU_SUBTYPE_INTEL(fam, model)       ( (int32_t )(((model) << 4) | (fam)) )
+#define CPU_SUBTYPE_INTEL_FAMILY(subtype)   ( (subtype) & 0xf )
+#define CPU_SUBTYPE_INTEL_MODEL(subtype)    ( (subtype) >> 4 )
+#define CPU_SUBTYPE_INTEL_FAMILY_MAX        0xf
+#define CPU_SUBTYPE_INTEL_MODEL_ALL         0
+
+#define CPU_SUBTYPE_I386_ALL        CPU_SUBTYPE_INTEL(3, 0)
+#define CPU_SUBTYPE_386             CPU_SUBTYPE_INTEL(3, 0)
+#define CPU_SUBTYPE_486             CPU_SUBTYPE_INTEL(4, 0)
+#define CPU_SUBTYPE_486SX           CPU_SUBTYPE_INTEL(4, 8)
+#define CPU_SUBTYPE_586             CPU_SUBTYPE_INTEL(5, 0)
+#define CPU_SUBTYPE_PENT            CPU_SUBTYPE_INTEL(5, 0)
+#define CPU_SUBTYPE_PENTPRO         CPU_SUBTYPE_INTEL(6, 1)
+#define CPU_SUBTYPE_PENTII_M3       CPU_SUBTYPE_INTEL(6, 3)
+#define CPU_SUBTYPE_PENTII_M5       CPU_SUBTYPE_INTEL(6, 5)
+#define CPU_SUBTYPE_CELERON         CPU_SUBTYPE_INTEL(7, 6)
+#define CPU_SUBTYPE_CELERON_MOBILE  CPU_SUBTYPE_INTEL(7, 7)
+#define CPU_SUBTYPE_PENTIUM_3       CPU_SUBTYPE_INTEL(8, 0)
+#define CPU_SUBTYPE_PENTIUM_3_M     CPU_SUBTYPE_INTEL(8, 1)
+#define CPU_SUBTYPE_PENTIUM_3_XEON  CPU_SUBTYPE_INTEL(8, 2)
+#define CPU_SUBTYPE_PENTIUM_M       CPU_SUBTYPE_INTEL(9, 0)
+#define CPU_SUBTYPE_PENTIUM_4       CPU_SUBTYPE_INTEL(10, 0)
+#define CPU_SUBTYPE_PENTIUM_4_M     CPU_SUBTYPE_INTEL(10, 1)
+#define CPU_SUBTYPE_ITANIUM         CPU_SUBTYPE_INTEL(11, 0)
+#define CPU_SUBTYPE_ITANIUM_2       CPU_SUBTYPE_INTEL(11, 1)
+#define CPU_SUBTYPE_XEON            CPU_SUBTYPE_INTEL(12, 0)
+#define CPU_SUBTYPE_XEON_MP         CPU_SUBTYPE_INTEL(12, 1)
+
+#define CPU_SUBTYPE_X86_ALL         INT32_C(3)
+#define CPU_SUBTYPE_X86_64_ALL      INT32_C(3)
+#define CPU_SUBTYPE_X86_ARCH1       INT32_C(4)
+
+#define CPU_SUBTYPE_MIPS_ALL        INT32_C(0)
+#define CPU_SUBTYPE_MIPS_R2300      INT32_C(1)
+#define CPU_SUBTYPE_MIPS_R2600      INT32_C(2)
+#define CPU_SUBTYPE_MIPS_R2800      INT32_C(3)
+#define CPU_SUBTYPE_MIPS_R2000a     INT32_C(4)
+#define CPU_SUBTYPE_MIPS_R2000      INT32_C(5)
+#define CPU_SUBTYPE_MIPS_R3000a     INT32_C(6)
+#define CPU_SUBTYPE_MIPS_R3000      INT32_C(7)
+
+#define CPU_SUBTYPE_MC98000_ALL     INT32_C(0)
+#define CPU_SUBTYPE_MC98601         INT32_C(1)
+
+#define CPU_SUBTYPE_HPPA_ALL        INT32_C(0)
+#define CPU_SUBTYPE_HPPA_7100       INT32_C(0)
+#define CPU_SUBTYPE_HPPA_7100LC     INT32_C(1)
+
+#define CPU_SUBTYPE_MC88000_ALL     INT32_C(0)
+#define CPU_SUBTYPE_MC88100         INT32_C(1)
+#define CPU_SUBTYPE_MC88110         INT32_C(2)
+
+#define CPU_SUBTYPE_SPARC_ALL       INT32_C(0)
+
+#define CPU_SUBTYPE_I860_ALL        INT32_C(0)
+#define CPU_SUBTYPE_I860_860        INT32_C(1)
+
+#define CPU_SUBTYPE_POWERPC_ALL     INT32_C(0)
+#define CPU_SUBTYPE_POWERPC_601     INT32_C(1)
+#define CPU_SUBTYPE_POWERPC_602     INT32_C(2)
+#define CPU_SUBTYPE_POWERPC_603     INT32_C(3)
+#define CPU_SUBTYPE_POWERPC_603e    INT32_C(4)
+#define CPU_SUBTYPE_POWERPC_603ev   INT32_C(5)
+#define CPU_SUBTYPE_POWERPC_604     INT32_C(6)
+#define CPU_SUBTYPE_POWERPC_604e    INT32_C(7)
+#define CPU_SUBTYPE_POWERPC_620     INT32_C(8)
+#define CPU_SUBTYPE_POWERPC_750     INT32_C(9)
+#define CPU_SUBTYPE_POWERPC_7400    INT32_C(10)
+#define CPU_SUBTYPE_POWERPC_7450    INT32_C(11)
+#define CPU_SUBTYPE_POWERPC_Max     INT32_C(10)
+#define CPU_SUBTYPE_POWERPC_SCVger  INT32_C(11)
+#define CPU_SUBTYPE_POWERPC_970     INT32_C(100)
+
+#define CPU_SUBTYPE_MASK            UINT32_C(0xff000000)
+#define CPU_SUBTYPE_LIB64           UINT32_C(0x80000000)
+
+#endif /* !CPU_ARCH_MASK */
+
+
+typedef struct fat_header
+{
+    uint32_t            magic;
+    uint32_t            nfat_arch;
+} fat_header_t;
+
+#ifndef IMAGE_FAT_SIGNATURE
+# define IMAGE_FAT_SIGNATURE        UINT32_C(0xcafebabe)
+#endif
+#ifndef IMAGE_FAT_SIGNATURE_OE
+# define IMAGE_FAT_SIGNATURE_OE     UINT32_C(0xbebafeca)
+#endif
+
+typedef struct fat_arch
+{
+    int32_t             cputype;
+    int32_t             cpusubtype;
+    uint32_t            offset;
+    uint32_t            size;
+    uint32_t            align;
+} fat_arch_t;
+
+typedef struct mach_header_32
+{
+    uint32_t            magic;
+    int32_t             cputype;
+    int32_t             cpusubtype;
+    uint32_t            filetype;
+    uint32_t            ncmds;
+    uint32_t            sizeofcmds;
+    uint32_t            flags;
+} mach_header_32_t;
+
+/* magic */
+#ifndef IMAGE_MACHO32_SIGNATURE
+# define IMAGE_MACHO32_SIGNATURE    UINT32_C(0xfeedface)
+#endif
+#ifndef IMAGE_MACHO32_SIGNATURE_OE
+# define IMAGE_MACHO32_SIGNATURE_OE UINT32_C(0xcefaedfe)
+#endif
+#define MH_MAGIC                    IMAGE_MACHO32_SIGNATURE
+#define MH_CIGAM                    IMAGE_MACHO32_SIGNATURE_OE
+
+typedef struct mach_header_64
+{
+    uint32_t            magic;
+    int32_t             cputype;
+    int32_t             cpusubtype;
+    uint32_t            filetype;
+    uint32_t            ncmds;
+    uint32_t            sizeofcmds;
+    uint32_t            flags;
+    uint32_t            reserved;
+} mach_header_64_t;
+
+/* magic */
+#ifndef IMAGE_MACHO64_SIGNATURE
+# define IMAGE_MACHO64_SIGNATURE    UINT32_C(0xfeedfacf)
+#endif
+#ifndef IMAGE_MACHO64_SIGNATURE_OE
+# define IMAGE_MACHO64_SIGNATURE_OE UINT32_C(0xfefaedfe)
+#endif
+#define MH_MAGIC_64                 IMAGE_MACHO64_SIGNATURE
+#define MH_CIGAM_64                 IMAGE_MACHO64_SIGNATURE_OE
+
+/* mach_header_* filetype */
+#define MH_OBJECT                   UINT32_C(1)
+#define MH_EXECUTE                  UINT32_C(2)
+#define MH_FVMLIB                   UINT32_C(3)
+#define MH_CORE                     UINT32_C(4)
+#define MH_PRELOAD                  UINT32_C(5)
+#define MH_DYLIB                    UINT32_C(6)
+#define MH_DYLINKER                 UINT32_C(7)
+#define MH_BUNDLE                   UINT32_C(8)
+#define MH_DYLIB_STUB               UINT32_C(9)
+#define MH_DSYM                     UINT32_C(10)
+
+/* mach_header_* flags */
+#define MH_NOUNDEFS                 UINT32_C(0x00000001)
+#define MH_INCRLINK                 UINT32_C(0x00000002)
+#define MH_DYLDLINK                 UINT32_C(0x00000004)
+#define MH_BINDATLOAD               UINT32_C(0x00000008)
+#define MH_PREBOUND                 UINT32_C(0x00000010)
+#define MH_SPLIT_SEGS               UINT32_C(0x00000020)
+#define MH_LAZY_INIT                UINT32_C(0x00000040)
+#define MH_TWOLEVEL                 UINT32_C(0x00000080)
+#define MH_FORCE_FLAT               UINT32_C(0x00000100)
+#define MH_NOMULTIDEFS              UINT32_C(0x00000200)
+#define MH_NOFIXPREBINDING          UINT32_C(0x00000400)
+#define MH_PREBINDABLE              UINT32_C(0x00000800)
+#define MH_ALLMODSBOUND             UINT32_C(0x00001000)
+#define MH_SUBSECTIONS_VIA_SYMBOLS  UINT32_C(0x00002000)
+#define MH_CANONICAL                UINT32_C(0x00004000)
+#define MH_WEAK_DEFINES             UINT32_C(0x00008000)
+#define MH_BINDS_TO_WEAK            UINT32_C(0x00010000)
+#define MH_ALLOW_STACK_EXECUTION    UINT32_C(0x00020000)
+#define MH_VALID_FLAGS              UINT32_C(0x0003ffff)
+
+
+typedef struct load_command
+{
+    uint32_t            cmd;
+    uint32_t            cmdsize;
+} load_command_t;
+
+/* load cmd */
+#define LC_REQ_DYLD                 UINT32_C(0x80000000)
+#define LC_SEGMENT_32               UINT32_C(0x01)
+#define LC_SYMTAB                   UINT32_C(0x02)
+#define LC_SYMSEG                   UINT32_C(0x03)
+#define LC_THREAD                   UINT32_C(0x04)
+#define LC_UNIXTHREAD               UINT32_C(0x05)
+#define LC_LOADFVMLIB               UINT32_C(0x06)
+#define LC_IDFVMLIB                 UINT32_C(0x07)
+#define LC_IDENT                    UINT32_C(0x08)
+#define LC_FVMFILE                  UINT32_C(0x09)
+#define LC_PREPAGE                  UINT32_C(0x0a)
+#define LC_DYSYMTAB                 UINT32_C(0x0b)
+#define LC_LOAD_DYLIB               UINT32_C(0x0c)
+#define LC_ID_DYLIB                 UINT32_C(0x0d)
+#define LC_LOAD_DYLINKER            UINT32_C(0x0e)
+#define LC_ID_DYLINKER              UINT32_C(0x0f)
+#define LC_PREBOUND_DYLIB           UINT32_C(0x10)
+#define LC_ROUTINES                 UINT32_C(0x11)
+#define LC_SUB_FRAMEWORK            UINT32_C(0x12)
+#define LC_SUB_UMBRELLA             UINT32_C(0x13)
+#define LC_SUB_CLIENT               UINT32_C(0x14)
+#define LC_SUB_LIBRARY              UINT32_C(0x15)
+#define LC_TWOLEVEL_HINTS           UINT32_C(0x16)
+#define LC_PREBIND_CKSUM            UINT32_C(0x17)
+#define LC_LOAD_WEAK_DYLIB (        UINT32_C(0x18) | LC_REQ_DYLD)
+#define LC_SEGMENT_64               UINT32_C(0x19)
+#define LC_ROUTINES_64              UINT32_C(0x1a)
+#define LC_UUID                     UINT32_C(0x1b)
+
+
+typedef struct lc_str
+{
+    uint32_t            offset;
+} lc_str_t;
+
+typedef struct segment_command_32
+{
+    uint32_t            cmd;
+    uint32_t            cmdsize;
+    char                segname[16];
+    uint32_t            vmaddr;
+    uint32_t            vmsize;
+    uint32_t            fileoff;
+    uint32_t            filesize;
+    uint32_t            maxprot;
+    uint32_t            initprot;
+    uint32_t            nsects;
+    uint32_t            flags;
+} segment_command_32_t;
+
+typedef struct segment_command_64
+{
+    uint32_t            cmd;
+    uint32_t            cmdsize;
+    char                segname[16];
+    uint64_t            vmaddr;
+    uint64_t            vmsize;
+    uint64_t            fileoff;
+    uint64_t            filesize;
+    uint32_t            maxprot;
+    uint32_t            initprot;
+    uint32_t            nsects;
+    uint32_t            flags;
+} segment_command_64_t;
+
+/* segment flags */
+#define SG_HIGHVM           UINT32_C(0x00000001)
+#define SG_FVMLIB           UINT32_C(0x00000002)
+#define SG_NORELOC          UINT32_C(0x00000004)
+#define SG_PROTECTED_VERSION_1 UINT32_C(0x00000008)
+
+/* maxprot/initprot */
+#ifndef VM_PROT_NONE
+# define VM_PROT_NONE       UINT32_C(0x00000000)
+# define VM_PROT_READ       UINT32_C(0x00000001)
+# define VM_PROT_WRITE      UINT32_C(0x00000002)
+# define VM_PROT_EXECUTE    UINT32_C(0x00000004)
+# define VM_PROT_ALL        UINT32_C(0x00000007)
+#endif
+
+typedef struct section_32
+{
+    char                sectname[16];
+    char                segname[16];
+    uint32_t            addr;
+    uint32_t            size;
+    uint32_t            offset;
+    uint32_t            align;
+    uint32_t            reloff;
+    uint32_t            nreloc;
+    uint32_t            flags;
+    uint32_t            reserved1;
+    uint32_t            reserved2;
+} section_32_t;
+
+typedef struct section_64
+{
+    char                sectname[16];
+    char                segname[16];
+    uint64_t            addr;
+    uint64_t            size;
+    uint32_t            offset;
+    uint32_t            align;
+    uint32_t            reloff;
+    uint32_t            nreloc;
+    uint32_t            flags;
+    uint32_t            reserved1;
+    uint32_t            reserved2;
+    uint32_t            reserved3;
+} section_64_t;
+
+/* section flags */
+#define SECTION_TYPE                UINT32_C(0x000000ff)
+#define S_REGULAR                   0x0
+#define S_ZEROFILL                  0x1
+#define S_CSTRING_LITERALS          0x2
+#define S_4BYTE_LITERALS            0x3
+#define S_8BYTE_LITERALS            0x4
+#define S_LITERAL_POINTERS          0x5
+#define S_NON_LAZY_SYMBOL_POINTERS  0x6
+#define S_LAZY_SYMBOL_POINTERS      0x7
+#define S_SYMBOL_STUBS              0x8
+#define S_MOD_INIT_FUNC_POINTERS    0x9
+#define S_MOD_TERM_FUNC_POINTERS    0xa
+#define S_COALESCED                 0xb
+#define S_GB_ZEROFILL               0xc
+#define S_INTERPOSING               0xd
+#define S_16BYTE_LITERALS           0xe
+#define S_DTRACE_DOF                0xf
+#define S_LAZY_DYLIB_SYMBOL_POINTERS 0x10
+
+#define SECTION_ATTRIBUTES          UINT32_C(0xffffff00)
+#define SECTION_ATTRIBUTES_USR      UINT32_C(0xff000000)
+#define S_ATTR_PURE_INSTRUCTIONS    UINT32_C(0x80000000)
+#define S_ATTR_NO_TOC               UINT32_C(0x40000000)
+#define S_ATTR_STRIP_STATIC_SYMS    UINT32_C(0x20000000)
+#define S_ATTR_NO_DEAD_STRIP        UINT32_C(0x10000000)
+#define S_ATTR_LIVE_SUPPORT         UINT32_C(0x08000000)
+#define S_ATTR_SELF_MODIFYING_CODE  UINT32_C(0x04000000)
+#define S_ATTR_DEBUG                UINT32_C(0x02000000)
+#define SECTION_ATTRIBUTES_SYS      UINT32_C(0x00ffff00)
+#define S_ATTR_SOME_INSTRUCTIONS    UINT32_C(0x00000400)
+#define S_ATTR_EXT_RELOC            UINT32_C(0x00000200)
+#define S_ATTR_LOC_RELOC            UINT32_C(0x00000100)
+
+/* standard section names */
+#define SEG_PAGEZERO                "__PAGEZERO"
+#define SEG_TEXT                    "__TEXT"
+#define SECT_TEXT                   "__text"
+#define SECT_FVMLIB_INIT0           "__fvmlib_init0"
+#define SECT_FVMLIB_INIT1           "__fvmlib_init1"
+#define SEG_DATA                    "__DATA"
+#define SECT_DATA                   "__data"
+#define SECT_BSS                    "__bss"
+#define SECT_COMMON                 "__common"
+#define SEG_OBJC                    "__OBJC"
+#define SECT_OBJC_SYMBOLS           "__symbol_table"
+#define SECT_OBJC_MODULES           "__module_info"
+#define SECT_OBJC_STRINGS           "__selector_strs"
+#define SECT_OBJC_REFS              "__selector_refs"
+#define SEG_ICON                    "__ICON"
+#define SECT_ICON_HEADER            "__header"
+#define SECT_ICON_TIFF              "__tiff"
+#define SEG_LINKEDIT                "__LINKEDIT"
+#define SEG_UNIXSTACK               "__UNIXSTACK"
+#define SEG_IMPORT                  "__IMPORT"
+
+typedef struct thread_command
+{
+    uint32_t            cmd;
+    uint32_t            cmdsize;
+} thread_command_t;
+
+typedef struct symtab_command
+{
+    uint32_t            cmd;
+    uint32_t            cmdsize;
+    uint32_t            symoff;
+    uint32_t            nsyms;
+    uint32_t            stroff;
+    uint32_t            strsize;
+} symtab_command_t;
+
+typedef struct uuid_command
+{
+    uint32_t            cmd;
+    uint32_t            cmdsize;
+    uint8_t             uuid[16];
+} uuid_command_t;
+
+typedef struct macho_nlist_32
+{
+    union
+    {
+        int32_t         n_strx;
+    }                   n_un;
+    uint8_t             n_type;
+    uint8_t             n_sect;
+    int16_t             n_desc;
+    uint32_t            n_value;
+} macho_nlist_32_t;
+
+
+typedef struct macho_nlist_64
+{
+    union
+    {
+        uint32_t        n_strx;
+    }                   n_un;
+    uint8_t             n_type;
+    uint8_t             n_sect;
+    int16_t             n_desc;
+    uint64_t            n_value;
+} macho_nlist_64_t;
+
+#define MACHO_N_EXT                 UINT8_C(0x01)
+#define MACHO_N_PEXT                UINT8_C(0x10)
+
+#define MACHO_N_TYPE                UINT8_C(0x0e)
+#define MACHO_N_UNDF                UINT8_C(0x00)
+#define MACHO_N_ABS                 UINT8_C(0x02)
+#define MACHO_N_INDR                UINT8_C(0x0a)
+#define MACHO_N_PBUD                UINT8_C(0x0c)
+#define MACHO_N_SECT                UINT8_C(0x0e)
+
+#define MACHO_N_STAB                UINT8_C(0xe0)
+#define MACHO_N_GSYM                UINT8_C(0x20)
+#define MACHO_N_FNAME               UINT8_C(0x22)
+#define MACHO_N_FUN                 UINT8_C(0x24)
+#define MACHO_N_STSYM               UINT8_C(0x26)
+#define MACHO_N_LCSYM               UINT8_C(0x28)
+#define MACHO_N_BNSYM               UINT8_C(0x2e)
+#define MACHO_N_PC                  UINT8_C(0x30)
+#define MACHO_N_OPT                 UINT8_C(0x3c)
+#define MACHO_N_RSYM                UINT8_C(0x40)
+#define MACHO_N_SLINE               UINT8_C(0x44)
+#define MACHO_N_ENSYM               UINT8_C(0x4e)
+#define MACHO_N_SSYM                UINT8_C(0x60)
+#define MACHO_N_SO                  UINT8_C(0x64)
+#define MACHO_N_OSO                 UINT8_C(0x66)
+#define MACHO_N_LSYM                UINT8_C(0x80)
+#define MACHO_N_BINCL               UINT8_C(0x82)
+#define MACHO_N_SOL                 UINT8_C(0x84)
+#define MACHO_N_PARAMS              UINT8_C(0x86)
+#define MACHO_N_VERSION             UINT8_C(0x88)
+#define MACHO_N_OLEVEL              UINT8_C(0x8A)
+#define MACHO_N_PSYM                UINT8_C(0xa0)
+#define MACHO_N_EINCL               UINT8_C(0xa2)
+#define MACHO_N_ENTRY               UINT8_C(0xa4)
+#define MACHO_N_LBRAC               UINT8_C(0xc0)
+#define MACHO_N_EXCL                UINT8_C(0xc2)
+#define MACHO_N_RBRAC               UINT8_C(0xe0)
+#define MACHO_N_BCOMM               UINT8_C(0xe2)
+#define MACHO_N_ECOMM               UINT8_C(0xe4)
+#define MACHO_N_ECOML               UINT8_C(0xe8)
+#define MACHO_N_LENG                UINT8_C(0xfe)
+
+#define MACHO_NO_SECT               UINT8_C(0x00)
+#define MACHO_MAX_SECT              UINT8_C(0xff)
+
+#define REFERENCE_TYPE              UINT16_C(0x000f)
+#define REFERENCE_FLAG_UNDEFINED_NON_LAZY             0
+#define REFERENCE_FLAG_UNDEFINED_LAZY                 1
+#define REFERENCE_FLAG_DEFINED                        2
+#define REFERENCE_FLAG_PRIVATE_DEFINED                3
+#define REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY     4
+#define REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY         5
+#define REFERENCED_DYNAMICALLY      UINT16_C(0x0010)
+
+#define GET_LIBRARY_ORDINAL(a_n_desc) \
+    RT_BYTE2(a_n_desc)
+#define SET_LIBRARY_ORDINAL(a_n_desc, a_ordinal) \
+    do { (a_n_desc) = RT_MAKE_U16(RT_BYTE1(a_n_desc), a_ordinal); } while (0)
+
+#define SELF_LIBRARY_ORDINAL        0x00
+#define MAX_LIBRARY_ORDINAL         0xfd
+#define DYNAMIC_LOOKUP_ORDINAL      0xfe
+#define EXECUTABLE_ORDINAL          0xff
+
+#define N_NO_DEAD_STRIP             UINT16_C(0x0020)
+#define N_DESC_DISCARDED            UINT16_C(0x0020)
+#define N_WEAK_REF                  UINT16_C(0x0040)
+#define N_WEAK_DEF                  UINT16_C(0x0080)
+#define N_REF_TO_WEAK               UINT16_C(0x0080)
+
+typedef struct macho_relocation_info
+{
+    int32_t             r_address;
+    uint32_t            r_symbolnum : 24;
+    uint32_t            r_pcrel     : 1;
+    uint32_t            r_length    : 2;
+    uint32_t            r_extern    : 1;
+    uint32_t            r_type      : 4;
+} macho_relocation_info_t;
+
+#define R_ABS                       0
+#define R_SCATTERED                 UINT32_C(0x80000000)
+
+typedef struct scattered_relocation_info
+{
+#ifdef RT_LITTLE_ENDIAN
+    uint32_t            r_address   : 24;
+    uint32_t            r_type      : 4;
+    uint32_t            r_length    : 2;
+    uint32_t            r_pcrel     : 1;
+    uint32_t            r_scattered : 1;
+#elif defined(RT_BIG_ENDIAN)
+    uint32_t            r_scattered : 1;
+    uint32_t            r_pcrel     : 1;
+    uint32_t            r_length    : 2;
+    uint32_t            r_type      : 4;
+    uint32_t            r_address   : 24;
+#else
+# error "Neither K_ENDIAN isn't LITTLE or BIG!"
+#endif
+    int32_t             r_value;
+} scattered_relocation_info_t;
+
+typedef enum reloc_type_generic
+{
+    GENERIC_RELOC_VANILLA = 0,
+    GENERIC_RELOC_PAIR,
+    GENERIC_RELOC_SECTDIFF,
+    GENERIC_RELOC_PB_LA_PTR,
+    GENERIC_RELOC_LOCAL_SECTDIFF
+} reloc_type_generic_t;
+
+typedef enum reloc_type_x86_64
+{
+    X86_64_RELOC_UNSIGNED = 0,
+    X86_64_RELOC_SIGNED,
+    X86_64_RELOC_BRANCH,
+    X86_64_RELOC_GOT_LOAD,
+    X86_64_RELOC_GOT,
+    X86_64_RELOC_SUBTRACTOR,
+    X86_64_RELOC_SIGNED_1,
+    X86_64_RELOC_SIGNED_2,
+    X86_64_RELOC_SIGNED_4
+} reloc_type_x86_64_t;
+
+#endif
+
Index: /trunk/src/VBox/Runtime/r0drv/darwin/mach_kernel-r0drv-darwin.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r0drv/darwin/mach_kernel-r0drv-darwin.cpp	(revision 37560)
+++ /trunk/src/VBox/Runtime/r0drv/darwin/mach_kernel-r0drv-darwin.cpp	(revision 37560)
@@ -0,0 +1,715 @@
+/* $Id$ */
+/** @file
+ * IPRT - mach_kernel symbol resolving hack, R0 Driver, Darwin.
+ */
+
+/*
+ * Copyright (C) 2011 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * The contents of this file may alternatively be used under the terms
+ * of the Common Development and Distribution License Version 1.0
+ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+ * VirtualBox OSE distribution, in which case the provisions of the
+ * CDDL are applicable instead of those of the GPL.
+ *
+ * You may elect to license modified versions of this file under the
+ * terms and conditions of either the GPL or the CDDL or both.
+ */
+
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#ifdef IN_RING0
+# include "the-darwin-kernel.h"
+#endif
+#include "../../include/internal/iprt.h"
+
+#include <iprt/asm.h>
+#include <iprt/assert.h>
+#include <iprt/err.h>
+#include <iprt/assert.h>
+#include <iprt/file.h>
+#include <iprt/mem.h>
+#include <iprt/string.h>
+#include "../../include/internal/ldrMach-O.h"
+
+/** @def MY_CPU_TYPE
+ * The CPU type targeted by the compiler. */
+/** @def MY_CPU_TYPE
+ * The "ALL" CPU subtype targeted by the compiler. */
+/** @def MY_MACHO_HEADER
+ * The Mach-O header targeted by the compiler.  */
+/** @def MY_MACHO_MAGIC
+ * The Mach-O header magic we're targeting.  */
+/** @def MY_SEGMENT_COMMAND
+ * The segment command targeted by the compiler.  */
+/** @def MY_SECTION
+ * The section struture targeted by the compiler.  */
+/** @def MY_NLIST
+ * The symbol table entry targeted by the compiler.  */
+#ifdef RT_ARCH_X86
+# define MY_CPU_TYPE            CPU_TYPE_I386
+# define MY_CPU_SUBTYPE_ALL     CPU_SUBTYPE_I386_ALL
+# define MY_MACHO_HEADER        mach_header_32_t
+# define MY_MACHO_MAGIC         IMAGE_MACHO32_SIGNATURE
+# define MY_SEGMENT_COMMAND     segment_command_32_t
+# define MY_SECTION             section_32_t
+# define MY_NLIST               macho_nlist_32_t
+
+#elif defined(RT_ARCH_AMD64)
+# define MY_CPU_TYPE            CPU_TYPE_X86_64
+# define MY_CPU_SUBTYPE_ALL     CPU_SUBTYPE_X86_64_ALL
+# define MY_MACHO_HEADER        mach_header_64_t
+# define MY_MACHO_MAGIC         IMAGE_MACHO64_SIGNATURE
+# define MY_SEGMENT_COMMAND     segment_command_64_t
+# define MY_SECTION             section_64_t
+# define MY_NLIST               macho_nlist_64_t
+
+#else
+# error "Port me!"
+#endif
+
+/** @name Return macros for make it simpler to track down too paranoid code.
+ * @{
+ */
+#ifdef DEBUG
+# define RETURN_VERR_BAD_EXE_FORMAT \
+    do { Assert(!g_fBreakpointOnError);         return VERR_BAD_EXE_FORMAT; } while (0)
+# define RETURN_VERR_LDR_UNEXPECTED \
+    do { Assert(!g_fBreakpointOnError);         return VERR_LDR_UNEXPECTED; } while (0)
+# define RETURN_VERR_LDR_ARCH_MISMATCH \
+    do { Assert(!g_fBreakpointOnError);         return VERR_LDR_ARCH_MISMATCH; } while (0)
+#else
+# define RETURN_VERR_BAD_EXE_FORMAT     do {    return VERR_BAD_EXE_FORMAT; } while (0)
+# define RETURN_VERR_LDR_UNEXPECTED     do {    return VERR_LDR_UNEXPECTED; } while (0)
+# define RETURN_VERR_LDR_ARCH_MISMATCH  do {    return VERR_LDR_ARCH_MISMATCH; } while (0)
+#endif
+/** @} */
+
+#define VERR_LDR_UNEXPECTED     (-641)
+
+
+/*******************************************************************************
+*   Structures and Typedefs                                                    *
+*******************************************************************************/
+/**
+ * Our internal representation of the mach_kernel after loading it's symbols
+ * and successfully resolving their addresses.
+ */
+typedef struct RTR0DARWINKERNEL
+{
+    /** @name Result.
+     * @{ */
+    /** Pointer to the string table. */
+    char               *pachStrTab;
+    /** The size of the string table. */
+    uint32_t            cbStrTab;
+    /** The file offset of the string table. */
+    uint32_t            offStrTab;
+    /** Pointer to the symbol table. */
+    MY_NLIST           *paSyms;
+    /** The size of the symbol table. */
+    uint32_t            cSyms;
+    /** The file offset of the symbol table. */
+    uint32_t            offSyms;
+    /** @} */
+
+    /** @name Used during loading.
+     * @{ */
+    /** The file handle.  */
+    RTFILE              hFile;
+    /** The architecture image offset (fat_arch_t::offset). */
+    uint64_t            offArch;
+    /** The architecture image size (fat_arch_t::size). */
+    uint32_t            cbArch;
+    /** The number of load commands (mach_header_XX_t::ncmds). */
+    uint32_t            cLoadCmds;
+    /** The size of the load commands. */
+    uint32_t            cbLoadCmds;
+    /** The load commands. */
+    load_command_t     *pLoadCmds;
+    /** Section pointer table (points into the load commands). */
+    MY_SECTION const   *apSections[MACHO_MAX_SECT];
+    /** The number of sections. */
+    uint32_t            cSections;
+    /** @} */
+
+    /** Buffer space. */
+    char                abBuf[_4K];
+} RTR0DARWINKERNEL;
+typedef RTR0DARWINKERNEL *PRTR0DARWINKERNEL;
+
+
+/*******************************************************************************
+*   Structures and Typedefs                                                    *
+*******************************************************************************/
+#ifdef DEBUG
+static bool g_fBreakpointOnError = true;
+#endif
+
+
+/**
+ * Frees up the internal scratch data when done looking up symbols.
+ *
+ * @param   pKernel             The internal scratch data.
+ */
+static void rtR0DarwinMachKernelClose(PRTR0DARWINKERNEL pKernel)
+{
+    RTMemFree(pKernel->pachStrTab);
+    pKernel->pachStrTab = NULL;
+    RTMemFree(pKernel->paSyms);
+    pKernel->paSyms = NULL;
+
+    RTMemFree(pKernel);
+}
+
+
+/**
+ * Close and free up resources we no longer needs.
+ *
+ * @param   pKernel             The internal scratch data.
+ */
+static void rtR0DarwinMachKernelLoadDone(PRTR0DARWINKERNEL pKernel)
+{
+    RTFileClose(pKernel->hFile);
+    pKernel->hFile = NIL_RTFILE;
+
+    RTMemFree(pKernel->pLoadCmds);
+    pKernel->pLoadCmds = NULL;
+    RT_ZERO(pKernel->apSections);
+}
+
+
+/**
+ * Looks up a kernel symbol.
+ *
+ *
+ * @returns The symbol address on success, 0 on failure.
+ * @param   pKernel             The internal scratch data.
+ * @param   pszSymbol           The symbol to resolve.  Automatically prefixed
+ *                              with an underscore.
+ */
+static uintptr_t rtR0DarwinMachKernelLookup(PRTR0DARWINKERNEL pKernel, const char *pszSymbol)
+{
+    uint32_t const  cSyms = pKernel->cSyms;
+    MY_NLIST const *pSym = pKernel->paSyms;
+
+#if 1
+    /* linear search. */
+    for (uint32_t iSym = 0; iSym < cSyms; iSym++, pSym++)
+    {
+        if (pSym->n_type & MACHO_N_STAB)
+            continue;
+        const char *pszSym = &pKernel->pachStrTab[(uint32_t)pSym->n_un.n_strx];
+        if (   *pszSym == '_'
+            && strcmp(pszSym + 1, pszSymbol) == 0)
+            return pSym->n_value;
+    }
+#else
+    /** @todo binary search. */
+
+#endif
+    return 0;
+}
+
+
+static int rtR0DarwinMachKernelCheckStandardSymbols(PRTR0DARWINKERNEL pKernel)
+{
+    static struct
+    {
+        const char *pszName;
+        uintptr_t   uAddr;
+    } const s_aStandardCandles[] =
+    {
+#if 0/// @todo def IN_RING0
+# define KNOWN_ENTRY(a_Sym)  { #a_Sym, (uintptr_t)&a_Sym }
+#else
+# define KNOWN_ENTRY(a_Sym)  { #a_Sym, 0 }
+#endif
+        KNOWN_ENTRY(IOMAlloc),
+        KNOWN_ENTRY(IOFree),
+        KNOWN_ENTRY(OSRuntimeFinalizeCPP),
+        KNOWN_ENTRY(OSRuntimeInitializeCPP)
+    };
+
+    for (unsigned i = 0; i < RT_ELEMENTS(s_aStandardCandles); i++)
+    {
+        uintptr_t uAddr = rtR0DarwinMachKernelLookup(pKernel, s_aStandardCandles[i].pszName);
+#ifdef IN_RING0
+        if (uAddr != s_aStandardCandles[i].uAddr)
+#else
+        if (uAddr == 0)
+#endif
+        {
+            AssertLogRelMsgFailed(("%s (%p != %p)\n", s_aStandardCandles[i].pszName, uAddr, s_aStandardCandles[i].uAddr));
+            return VERR_INTERNAL_ERROR_2;
+        }
+    }
+    return VINF_SUCCESS;
+}
+
+
+/**
+ * Loads and validates the symbol and string tables.
+ *
+ * @returns IPRT status code.
+ * @param   pKernel             The internal scratch data.
+ */
+static int rtR0DarwinMachKernelLoadSymTab(PRTR0DARWINKERNEL pKernel)
+{
+    /*
+     * Load the tables.
+     */
+    pKernel->paSyms = (MY_NLIST *)RTMemAllocZ(pKernel->cSyms * sizeof(MY_NLIST));
+    if (!pKernel->paSyms)
+        return VERR_NO_MEMORY;
+
+    int rc = RTFileReadAt(pKernel->hFile, pKernel->offArch + pKernel->offSyms,
+                          pKernel->paSyms, pKernel->cSyms * sizeof(MY_NLIST), NULL);
+    if (RT_FAILURE(rc))
+        return rc;
+
+    pKernel->pachStrTab = (char *)RTMemAllocZ(pKernel->cbStrTab + 1);
+    if (!pKernel->pachStrTab)
+        return VERR_NO_MEMORY;
+
+    rc = RTFileReadAt(pKernel->hFile, pKernel->offArch + pKernel->offStrTab,
+                      pKernel->pachStrTab, pKernel->cbStrTab, NULL);
+    if (RT_FAILURE(rc))
+        return rc;
+
+    /*
+     * The first string table symbol must be a zero length name.
+     */
+    if (pKernel->pachStrTab[0] != '\0')
+        RETURN_VERR_BAD_EXE_FORMAT;
+
+    /*
+     * Validate the symbol table.
+     */
+    const char     *pszPrev = "";
+    uint32_t const  cSyms   = pKernel->cSyms;
+    MY_NLIST const  *pSym   = pKernel->paSyms;
+    for (uint32_t iSym = 0; iSym < cSyms; iSym++, pSym++)
+    {
+        if ((uint32_t)pSym->n_un.n_strx >= pKernel->cbStrTab)
+            RETURN_VERR_BAD_EXE_FORMAT;
+        const char *pszSym = &pKernel->pachStrTab[(uint32_t)pSym->n_un.n_strx];
+#ifdef IN_RING3
+        RTAssertMsg2("%05i: %02x:%08x %02x %04x %s\n", iSym, pSym->n_sect, pSym->n_value, pSym->n_type, pSym->n_desc, pszSym);
+#endif
+
+        if (strcmp(pszSym, pszPrev) < 0)
+            RETURN_VERR_BAD_EXE_FORMAT; /* not sorted */
+
+        if (!(pSym->n_type & MACHO_N_STAB))
+        {
+            switch (pSym->n_type & MACHO_N_TYPE)
+            {
+                case MACHO_N_SECT:
+                    if (pSym->n_sect == MACHO_NO_SECT)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    if (pSym->n_sect > pKernel->cSections)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    if (pSym->n_desc != 0)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    if (pSym->n_value < pKernel->apSections[pSym->n_sect - 1]->addr)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    if (   pSym->n_value - pKernel->apSections[pSym->n_sect - 1]->addr
+                        > pKernel->apSections[pSym->n_sect - 1]->size)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    break;
+
+                case MACHO_N_ABS:
+                    if (pSym->n_sect != MACHO_NO_SECT)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    if (pSym->n_desc != 0)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    break;
+
+                case MACHO_N_UNDF:
+                    /* No undefined or common symbols in the kernel. */
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                case MACHO_N_INDR:
+                    /* No indirect symbols in the kernel. */
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                case MACHO_N_PBUD:
+                    /* No prebound symbols in the kernel. */
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                default:
+                    RETURN_VERR_BAD_EXE_FORMAT;
+            }
+        }
+        /* else: Ignore debug symbols. */
+    }
+
+    return VINF_SUCCESS;
+}
+
+
+/**
+ * Loads the load commands and validates them.
+ *
+ * @returns IPRT status code.
+ * @param   pKernel             The internal scratch data.
+ */
+static int rtR0DarwinMachKernelLoadCommands(PRTR0DARWINKERNEL pKernel)
+{
+    pKernel->offStrTab = 0;
+    pKernel->cbStrTab  = 0;
+    pKernel->offSyms   = 0;
+    pKernel->cSyms     = 0;
+    pKernel->cSections = 0;
+
+    pKernel->pLoadCmds = (load_command_t *)RTMemAlloc(pKernel->cbLoadCmds);
+    if (!pKernel->pLoadCmds)
+        return VERR_NO_MEMORY;
+
+    int rc = RTFileReadAt(pKernel->hFile, pKernel->offArch + sizeof(MY_MACHO_HEADER),
+                          pKernel->pLoadCmds, pKernel->cbLoadCmds, NULL);
+    if (RT_FAILURE(rc))
+        return rc;
+
+    /*
+     * Validate the relevant commands, picking up sections and the symbol
+     * table location.
+     */
+    load_command_t const   *pCmd = pKernel->pLoadCmds;
+    for (uint32_t iCmd = 0; ; iCmd++)
+    {
+        /* cmd index & offset. */
+        uintptr_t offCmd = (uintptr_t)pCmd - (uintptr_t)pKernel->pLoadCmds;
+        if (offCmd == pKernel->cbLoadCmds && iCmd == pKernel->cLoadCmds)
+            break;
+        if (offCmd + sizeof(*pCmd) > pKernel->cbLoadCmds)
+            RETURN_VERR_BAD_EXE_FORMAT;
+        if (iCmd >= pKernel->cLoadCmds)
+            RETURN_VERR_BAD_EXE_FORMAT;
+
+        /* cmdsize */
+        if (pCmd->cmdsize < sizeof(*pCmd))
+            RETURN_VERR_BAD_EXE_FORMAT;
+        if (pCmd->cmdsize > pKernel->cbLoadCmds)
+            RETURN_VERR_BAD_EXE_FORMAT;
+        if (RT_ALIGN_32(pCmd->cmdsize, 4) != pCmd->cmdsize)
+            RETURN_VERR_BAD_EXE_FORMAT;
+
+        /* cmd */
+        switch (pCmd->cmd & ~LC_REQ_DYLD)
+        {
+            /* Validate and store the symbol table details. */
+            case LC_SYMTAB:
+            {
+                struct symtab_command const *pSymTab = (struct symtab_command const *)pCmd;
+                if (pSymTab->cmdsize != sizeof(*pSymTab))
+                    RETURN_VERR_BAD_EXE_FORMAT;
+                if (pSymTab->nsyms > _1M)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+                if (pSymTab->strsize > _2M)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                pKernel->offStrTab = pSymTab->stroff;
+                pKernel->cbStrTab  = pSymTab->strsize;
+                pKernel->offSyms   = pSymTab->symoff;
+                pKernel->cSyms     = pSymTab->nsyms;
+                break;
+            }
+
+            /* Validate the segment. */
+#if ARCH_BITS == 32
+            case LC_SEGMENT_32:
+#elif ARCH_BITS == 64
+            case LC_SEGMENT_64:
+#else
+# error ARCH_BITS
+#endif
+            {
+                MY_SEGMENT_COMMAND const *pSeg = (MY_SEGMENT_COMMAND const *)pCmd;
+                if (pSeg->cmdsize < sizeof(*pSeg))
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                if (pSeg->segname[0] == '\0')
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                if (pSeg->nsects > MACHO_MAX_SECT)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+                if (pSeg->nsects * sizeof(MY_SECTION) + sizeof(*pSeg) != pSeg->cmdsize)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                if (pSeg->flags & ~(SG_HIGHVM | SG_FVMLIB | SG_NORELOC | SG_PROTECTED_VERSION_1))
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                if (pSeg->vmaddr != 0)
+                {
+                    if (pSeg->vmaddr + RT_ALIGN_Z(pSeg->vmsize, RT_BIT_32(12)) < pSeg->vmaddr)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                }
+                else if (pSeg->vmsize)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                if (pSeg->maxprot & ~VM_PROT_ALL)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+                if (pSeg->initprot & ~VM_PROT_ALL)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+
+                /* Validate the sections. */
+                uint32_t            uAlignment = 0;
+                uintptr_t           uAddr      = pSeg->vmaddr;
+                MY_SECTION const   *paSects    = (MY_SECTION const *)(pSeg + 1);
+                for (uint32_t i = 0; i < pSeg->nsects; i++)
+                {
+                    if (paSects[i].sectname[0] == '\0')
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    if (memcmp(paSects[i].segname, pSeg->segname, sizeof(pSeg->segname)))
+                        RETURN_VERR_BAD_EXE_FORMAT;
+
+                    switch (paSects[i].flags & SECTION_TYPE)
+                    {
+                        case S_REGULAR:
+                        case S_CSTRING_LITERALS:
+                        case S_NON_LAZY_SYMBOL_POINTERS:
+                        case S_MOD_INIT_FUNC_POINTERS:
+                        case S_MOD_TERM_FUNC_POINTERS:
+                        case S_COALESCED:
+                            if (  pSeg->filesize != 0
+                                ? paSects[i].offset - pSeg->fileoff >= pSeg->filesize
+                                : paSects[i].offset - pSeg->fileoff != pSeg->filesize)
+                                RETURN_VERR_BAD_EXE_FORMAT;
+                            if (   paSects[i].addr != 0
+                                && paSects[i].offset - pSeg->fileoff != paSects[i].addr - pSeg->vmaddr)
+                                RETURN_VERR_BAD_EXE_FORMAT;
+                            break;
+
+                        case S_ZEROFILL:
+                            if (paSects[i].offset != 0)
+                                RETURN_VERR_BAD_EXE_FORMAT;
+                            break;
+
+                        /* not observed */
+                        case S_SYMBOL_STUBS:
+                        case S_INTERPOSING:
+                        case S_4BYTE_LITERALS:
+                        case S_8BYTE_LITERALS:
+                        case S_16BYTE_LITERALS:
+                        case S_DTRACE_DOF:
+                        case S_LAZY_SYMBOL_POINTERS:
+                        case S_LAZY_DYLIB_SYMBOL_POINTERS:
+                            RETURN_VERR_LDR_UNEXPECTED;
+                        case S_GB_ZEROFILL:
+                            RETURN_VERR_LDR_UNEXPECTED;
+                        default:
+                            RETURN_VERR_BAD_EXE_FORMAT;
+                    }
+
+                    if (paSects[i].align > 12)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    if (paSects[i].align > uAlignment)
+                        uAlignment = paSects[i].align;
+
+                    /* Add to the section table. */
+                    if (pKernel->cSections == MACHO_MAX_SECT)
+                        RETURN_VERR_BAD_EXE_FORMAT;
+                    pKernel->apSections[pKernel->cSections++] = &paSects[i];
+                }
+
+                if (RT_ALIGN_Z(pSeg->vmaddr, RT_BIT_32(uAlignment)) != pSeg->vmaddr)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+                if (   pSeg->filesize > RT_ALIGN_Z(pSeg->vmsize, RT_BIT_32(uAlignment))
+                    && pSeg->vmsize != 0)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+                break;
+            }
+
+            case LC_UUID:
+                if (pCmd->cmdsize != sizeof(uuid_command))
+                    RETURN_VERR_BAD_EXE_FORMAT;
+                break;
+
+            case LC_DYSYMTAB:
+            case LC_UNIXTHREAD:
+                break;
+
+            /* not observed */
+            case LC_SYMSEG:
+#if ARCH_BITS == 32
+            case LC_SEGMENT_64:
+#elif ARCH_BITS == 64
+            case LC_SEGMENT_32:
+#endif
+            case LC_ROUTINES_64:
+            case LC_ROUTINES:
+            case LC_THREAD:
+            case LC_LOADFVMLIB:
+            case LC_IDFVMLIB:
+            case LC_IDENT:
+            case LC_FVMFILE:
+            case LC_PREPAGE:
+            case LC_TWOLEVEL_HINTS:
+            case LC_PREBIND_CKSUM:
+                RETURN_VERR_LDR_UNEXPECTED;
+
+            /* dylib */
+            case LC_LOAD_DYLIB:
+            case LC_ID_DYLIB:
+            case LC_LOAD_DYLINKER:
+            case LC_ID_DYLINKER:
+            case LC_PREBOUND_DYLIB:
+            case LC_LOAD_WEAK_DYLIB & ~LC_REQ_DYLD:
+            case LC_SUB_FRAMEWORK:
+            case LC_SUB_UMBRELLA:
+            case LC_SUB_CLIENT:
+            case LC_SUB_LIBRARY:
+                RETURN_VERR_LDR_UNEXPECTED;
+
+            default:
+                RETURN_VERR_BAD_EXE_FORMAT;
+        }
+
+        /* next */
+        pCmd = (load_command_t *)((uintptr_t)pCmd + pCmd->cmdsize);
+    }
+
+    return VINF_SUCCESS;
+}
+
+
+/**
+ * Loads the FAT and MACHO headers, noting down the relevant info.
+ *
+ * @returns IPRT status code.
+ * @param   pKernel             The internal scratch data.
+ */
+static int rtR0DarwinMachKernelLoadFileHeaders(PRTR0DARWINKERNEL pKernel)
+{
+    uint32_t i;
+
+    pKernel->offArch = 0;
+    pKernel->cbArch  = 0;
+
+    /*
+     * Read the first bit of the file, parse the FAT if found there.
+     */
+    int rc = RTFileReadAt(pKernel->hFile, 0, pKernel->abBuf, sizeof(fat_header_t) + sizeof(fat_arch_t) * 16, NULL);
+    if (RT_FAILURE(rc))
+        return rc;
+
+    fat_header_t   *pFat        = (fat_header *)pKernel->abBuf;
+    fat_arch_t     *paFatArches = (fat_arch_t *)(pFat + 1);
+
+    /* Correct FAT endian first. */
+    if (pFat->magic == IMAGE_FAT_SIGNATURE_OE)
+    {
+        pFat->magic     = RT_BSWAP_U32(pFat->magic);
+        pFat->nfat_arch = RT_BSWAP_U32(pFat->nfat_arch);
+        i = RT_MIN(pFat->nfat_arch, 16);
+        while (i-- > 0)
+        {
+            paFatArches[i].cputype    = RT_BSWAP_U32(paFatArches[i].cputype);
+            paFatArches[i].cpusubtype = RT_BSWAP_U32(paFatArches[i].cpusubtype);
+            paFatArches[i].offset     = RT_BSWAP_U32(paFatArches[i].offset);
+            paFatArches[i].size       = RT_BSWAP_U32(paFatArches[i].size);
+            paFatArches[i].align      = RT_BSWAP_U32(paFatArches[i].align);
+        }
+    }
+
+    /* Lookup our architecture in the FAT. */
+    if (pFat->magic == IMAGE_FAT_SIGNATURE)
+    {
+        if (pFat->nfat_arch > 16)
+            RETURN_VERR_BAD_EXE_FORMAT;
+
+        for (i = 0; i < pFat->nfat_arch; i++)
+        {
+            if (   paFatArches[i].cputype == MY_CPU_TYPE
+                && paFatArches[i].cpusubtype == MY_CPU_SUBTYPE_ALL)
+            {
+                pKernel->offArch = paFatArches[i].offset;
+                pKernel->cbArch  = paFatArches[i].size;
+                if (!pKernel->cbArch)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+                if (pKernel->offArch < sizeof(fat_header_t) + sizeof(fat_arch_t) * pFat->nfat_arch)
+                    RETURN_VERR_BAD_EXE_FORMAT;
+                if (pKernel->offArch + pKernel->cbArch <= pKernel->offArch)
+                    RETURN_VERR_LDR_ARCH_MISMATCH;
+                break;
+            }
+        }
+        if (i >= pFat->nfat_arch)
+            RETURN_VERR_LDR_ARCH_MISMATCH;
+    }
+
+    /*
+     * Read the Mach-O header and validate it.
+     */
+    rc = RTFileReadAt(pKernel->hFile, pKernel->offArch, pKernel->abBuf, sizeof(MY_MACHO_HEADER), NULL);
+    if (RT_FAILURE(rc))
+        return rc;
+    MY_MACHO_HEADER const *pHdr = (MY_MACHO_HEADER const *)pKernel->abBuf;
+    if (pHdr->magic != MY_MACHO_MAGIC)
+    {
+        if (   pHdr->magic == IMAGE_MACHO32_SIGNATURE
+            || pHdr->magic == IMAGE_MACHO32_SIGNATURE_OE
+            || pHdr->magic == IMAGE_MACHO64_SIGNATURE
+            || pHdr->magic == IMAGE_MACHO64_SIGNATURE_OE)
+            RETURN_VERR_LDR_ARCH_MISMATCH;
+        RETURN_VERR_BAD_EXE_FORMAT;
+    }
+
+    if (pHdr->cputype    != MY_CPU_TYPE)
+        RETURN_VERR_LDR_ARCH_MISMATCH;
+    if (pHdr->cpusubtype != MY_CPU_SUBTYPE_ALL)
+        RETURN_VERR_LDR_ARCH_MISMATCH;
+    if (pHdr->filetype   != MH_EXECUTE)
+        RETURN_VERR_LDR_UNEXPECTED;
+    if (pHdr->ncmds      < 4)
+        RETURN_VERR_LDR_UNEXPECTED;
+    if (pHdr->ncmds      > 256)
+        RETURN_VERR_LDR_UNEXPECTED;
+    if (pHdr->sizeofcmds <= pHdr->ncmds * sizeof(load_command_t))
+        RETURN_VERR_LDR_UNEXPECTED;
+    if (pHdr->sizeofcmds >= _1M)
+        RETURN_VERR_LDR_UNEXPECTED;
+    if (pHdr->flags & ~MH_VALID_FLAGS)
+        RETURN_VERR_LDR_UNEXPECTED;
+
+    pKernel->cLoadCmds  = pHdr->ncmds;
+    pKernel->cbLoadCmds = pHdr->sizeofcmds;
+    return VINF_SUCCESS;
+}
+
+
+static int rtR0DarwinMachKernelOpen(const char *pszMachKernel, PRTR0DARWINKERNEL *ppHandle)
+{
+    PRTR0DARWINKERNEL pKernel = (PRTR0DARWINKERNEL)RTMemAllocZ(sizeof(*pKernel));
+    if (!pKernel)
+        return VERR_NO_MEMORY;
+    pKernel->hFile = NIL_RTFILE;
+
+    int rc = RTFileOpen(&pKernel->hFile, pszMachKernel, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
+    if (RT_SUCCESS(rc))
+        rc = rtR0DarwinMachKernelLoadFileHeaders(pKernel);
+    if (RT_SUCCESS(rc))
+        rc = rtR0DarwinMachKernelLoadCommands(pKernel);
+    if (RT_SUCCESS(rc))
+        rc = rtR0DarwinMachKernelLoadSymTab(pKernel);
+    if (RT_SUCCESS(rc))
+        rc = rtR0DarwinMachKernelCheckStandardSymbols(pKernel);
+
+    rtR0DarwinMachKernelLoadDone(pKernel);
+    if (RT_FAILURE(rc))
+        rtR0DarwinMachKernelClose(pKernel);
+    return rc;
+}
+
Index: /trunk/src/VBox/Runtime/testcase/Makefile.kmk
===================================================================
--- /trunk/src/VBox/Runtime/testcase/Makefile.kmk	(revision 37559)
+++ /trunk/src/VBox/Runtime/testcase/Makefile.kmk	(revision 37560)
@@ -142,5 +142,6 @@
 	tstIoCtl
 PROGRAMS.darwin += \
-	tstDarwinSched
+	tstDarwinSched \
+	tstRTDarwinMachKernel
 ifdef VBOX_WITH_LIBCURL
  PROGRAMS += \
@@ -576,4 +577,7 @@
 tstDarwinSched_SOURCES = tstDarwinSched.cpp
 
+tstRTDarwinMachKernel_TEMPLATE = VBOXR3TSTEXE
+tstRTDarwinMachKernel_SOURCES  = tstRTDarwinMachKernel.cpp
+
 ntGetTimerResolution_SOURCES = ntGetTimerResolution.cpp
 ntGetTimerResolution_SDKS.win = WINPSDK W2K3DDK VBOX_NTDLL
Index: /trunk/src/VBox/Runtime/testcase/tstRTDarwinMachKernel.cpp
===================================================================
--- /trunk/src/VBox/Runtime/testcase/tstRTDarwinMachKernel.cpp	(revision 37560)
+++ /trunk/src/VBox/Runtime/testcase/tstRTDarwinMachKernel.cpp	(revision 37560)
@@ -0,0 +1,56 @@
+/* $Id$ */
+/** @file
+ * IPRT Testcase - mach_kernel symbol resolving hack.
+ */
+
+/*
+ * Copyright (C) 2011 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * The contents of this file may alternatively be used under the terms
+ * of the Common Development and Distribution License Version 1.0
+ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+ * VirtualBox OSE distribution, in which case the provisions of the
+ * CDDL are applicable instead of those of the GPL.
+ *
+ * You may elect to license modified versions of this file under the
+ * terms and conditions of either the GPL or the CDDL or both.
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include <iprt/err.h>
+#include <iprt/string.h>
+#include <iprt/test.h>
+
+#include "../r0drv/darwin/mach_kernel-r0drv-darwin.cpp"
+
+
+static void dotest(const char *pszMachKernel)
+{
+    PRTR0DARWINKERNEL pKernel;
+    RTTESTI_CHECK_RC_RETV(rtR0DarwinMachKernelOpen(pszMachKernel, &pKernel), VINF_SUCCESS);
+}
+
+
+int main(int argc, char **argv)
+{
+    RTTEST hTest;
+    RTEXITCODE rcExit = RTTestInitAndCreate("tstRTDarwinMachKernel", &hTest);
+    if (rcExit != RTEXITCODE_SUCCESS)
+        return rcExit;
+    RTTestBanner(hTest);
+
+    dotest("/mach_kernel");
+
+    return RTTestSummaryAndDestroy(hTest);
+}
+
