Example #1
0
        internal void Read(BinaryReader reader)
        {
            /* definitions from: /usr/include/mach-o/loader.h */

            /*
             * The 32-bit mach header appears at the very beginning of the object file for
             * 32-bit architectures.
             */
            //	struct mach_header {
            //		uint32_t	magic;		/* mach magic number identifier */
            //		cpu_type_t	cputype;	/* cpu specifier */
            //		cpu_subtype_t	cpusubtype;	/* machine specifier */
            //		uint32_t	filetype;	/* type of file */
            //		uint32_t	ncmds;		/* number of load commands */
            //		uint32_t	sizeofcmds;	/* the size of all the load commands */
            //		uint32_t	flags;		/* flags */
            //	};

            /*
             * The 64-bit mach header appears at the very beginning of object files for
             * 64-bit architectures.
             */
            //	struct mach_header_64 {
            //		uint32_t	magic;		/* mach magic number identifier */
            //		cpu_type_t	cputype;	/* cpu specifier */
            //		cpu_subtype_t	cpusubtype;	/* machine specifier */
            //		uint32_t	filetype;	/* type of file */
            //		uint32_t	ncmds;		/* number of load commands */
            //		uint32_t	sizeofcmds;	/* the size of all the load commands */
            //		uint32_t	flags;		/* flags */
            //		uint32_t	reserved;	/* reserved */
            //	};

            magic = reader.ReadUInt32();
            switch (magic)
            {
            case MachO.MH_CIGAM:
            case MachO.MH_MAGIC:
                is64bitheader = false;
                break;

            case MachO.MH_CIGAM_64:
            case MachO.MH_MAGIC_64:
                is64bitheader = true;
                break;

            default:
                throw ErrorHelper.CreateError(1602, "Not a Mach-O dynamic library (unknown header '0x{0}'): {1}.", magic.ToString("x"), fat_parent != null ? fat_parent.Parent.Filename : filename);
            }
            _cputype    = reader.ReadInt32();
            _cpusubtype = reader.ReadInt32();
            _filetype   = reader.ReadUInt32();
            _ncmds      = reader.ReadUInt32();
            _sizeofcmds = reader.ReadUInt32();
            _flags      = reader.ReadUInt32();
            if (is64bitheader)
            {
                _reserved = reader.ReadUInt32();
            }
            var cmds = new List <LoadCommand> ((int)ncmds);

            for (int i = 0; i < ncmds; i++)
            {
                var cmd = (MachO.LoadCommands)reader.ReadUInt32();
                reader.BaseStream.Position -= 4;
                LoadCommand lc;
                switch (cmd)
                {
                case MachO.LoadCommands.LoadDylib:
                case MachO.LoadCommands.LoadWeakDylib:
                case MachO.LoadCommands.ReexportDylib:
                    var dlc = new DylibLoadCommand();
                    dlc.cmd     = reader.ReadUInt32();
                    dlc.cmdsize = reader.ReadUInt32();
                    /*var nameofs = */ reader.ReadUInt32();
                    dlc.timestamp             = reader.ReadUInt32();
                    dlc.current_version       = reader.ReadUInt32();
                    dlc.compatibility_version = reader.ReadUInt32();
                    var namelength = dlc.cmdsize - 6 * 4;
                    var namechars  = reader.ReadBytes((int)namelength);
                    // strip off any null characters at the end.
                    for (int n = namechars.Length - 1; n >= 0; n--)
                    {
                        if (namechars [n] == 0)
                        {
                            namelength--;
                        }
                        else
                        {
                            break;
                        }
                    }
                    dlc.name = System.Text.UTF8Encoding.UTF8.GetString(namechars, 0, (int)namelength);

                    lc = dlc;
                    break;

                default:
                    lc         = new LoadCommand();
                    lc.cmd     = reader.ReadUInt32();
                    lc.cmdsize = reader.ReadUInt32();
                    reader.BaseStream.Position += lc.cmdsize - 8;
                    break;
                }
                cmds.Add(lc);
            }
            load_commands = cmds;
        }
Example #2
0
        internal void Read(BinaryReader reader)
        {
            /* definitions from: /usr/include/mach-o/loader.h */

            /*
             * The 32-bit mach header appears at the very beginning of the object file for
             * 32-bit architectures.
             */
            //	struct mach_header {
            //		uint32_t	magic;		/* mach magic number identifier */
            //		cpu_type_t	cputype;	/* cpu specifier */
            //		cpu_subtype_t	cpusubtype;	/* machine specifier */
            //		uint32_t	filetype;	/* type of file */
            //		uint32_t	ncmds;		/* number of load commands */
            //		uint32_t	sizeofcmds;	/* the size of all the load commands */
            //		uint32_t	flags;		/* flags */
            //	};

            /*
             * The 64-bit mach header appears at the very beginning of object files for
             * 64-bit architectures.
             */
            //	struct mach_header_64 {
            //		uint32_t	magic;		/* mach magic number identifier */
            //		cpu_type_t	cputype;	/* cpu specifier */
            //		cpu_subtype_t	cpusubtype;	/* machine specifier */
            //		uint32_t	filetype;	/* type of file */
            //		uint32_t	ncmds;		/* number of load commands */
            //		uint32_t	sizeofcmds;	/* the size of all the load commands */
            //		uint32_t	flags;		/* flags */
            //		uint32_t	reserved;	/* reserved */
            //	};

            magic = reader.ReadUInt32();
            switch (magic)
            {
            case MachO.MH_CIGAM:
            case MachO.MH_MAGIC:
                is64bitheader = false;
                break;

            case MachO.MH_CIGAM_64:
            case MachO.MH_MAGIC_64:
                is64bitheader = true;
                break;

            default:
                throw ErrorHelper.CreateError(1602, Errors.MX1602, magic.ToString("x"), fat_parent != null ? fat_parent.Parent.Filename : filename);
            }
            _cputype    = reader.ReadInt32();
            _cpusubtype = reader.ReadInt32();
            _filetype   = reader.ReadUInt32();
            _ncmds      = reader.ReadUInt32();
            _sizeofcmds = reader.ReadUInt32();
            _flags      = reader.ReadUInt32();
            if (is64bitheader)
            {
                _reserved = reader.ReadUInt32();
            }
            var cmds = new List <LoadCommand> ((int)ncmds);

            for (int i = 0; i < ncmds; i++)
            {
                var cmd = (MachO.LoadCommands)reader.ReadUInt32();
                reader.BaseStream.Position -= 4;
                LoadCommand lc;
                switch (cmd)
                {
                case MachO.LoadCommands.LoadDylib:
                case MachO.LoadCommands.LoadWeakDylib:
                case MachO.LoadCommands.ReexportDylib:
                    var dlc = new DylibLoadCommand();
                    dlc.cmd     = reader.ReadUInt32();
                    dlc.cmdsize = reader.ReadUInt32();
                    /*var nameofs = */ reader.ReadUInt32();
                    dlc.timestamp             = reader.ReadUInt32();
                    dlc.current_version       = reader.ReadUInt32();
                    dlc.compatibility_version = reader.ReadUInt32();
                    var namelength = dlc.cmdsize - 6 * 4;
                    var namechars  = reader.ReadBytes((int)namelength);
                    // strip off any null characters at the end.
                    for (int n = namechars.Length - 1; n >= 0; n--)
                    {
                        if (namechars [n] == 0)
                        {
                            namelength--;
                        }
                        else
                        {
                            break;
                        }
                    }
                    dlc.name = System.Text.UTF8Encoding.UTF8.GetString(namechars, 0, (int)namelength);

                    lc = dlc;
                    break;

                case MachO.LoadCommands.Uuid:
                    var uuidCmd = new UuidCommand();
                    uuidCmd.cmd     = reader.ReadUInt32();
                    uuidCmd.cmdsize = reader.ReadUInt32();
                    uuidCmd.uuid    = reader.ReadBytes(16);                   // defined in the header as uint8_t uuid [16]
                    lc = uuidCmd;
                    break;

                case MachO.LoadCommands.MintvOS:
                case MachO.LoadCommands.MinMacOSX:
                case MachO.LoadCommands.MiniPhoneOS:
                case MachO.LoadCommands.MinwatchOS:
                    var minCmd = new MinCommand();
                    minCmd.cmd     = reader.ReadUInt32();
                    minCmd.cmdsize = reader.ReadUInt32();
                    minCmd.version = reader.ReadUInt32();
                    minCmd.sdk     = reader.ReadUInt32();
                    lc             = minCmd;
                    break;

                case MachO.LoadCommands.BuildVersion:
                    var buildVer = new BuildVersionCommand();
                    buildVer.cmd      = reader.ReadUInt32();
                    buildVer.cmdsize  = reader.ReadUInt32();
                    buildVer.platform = reader.ReadUInt32();
                    buildVer.minos    = reader.ReadUInt32();
                    buildVer.sdk      = reader.ReadUInt32();
                    buildVer.ntools   = reader.ReadUInt32();
                    buildVer.tools    = new BuildVersionCommand.BuildToolVersion[buildVer.ntools];
                    for (int j = 0; j < buildVer.ntools; j++)
                    {
                        var buildToolVer = new BuildVersionCommand.BuildToolVersion();
                        buildToolVer.tool    = reader.ReadUInt32();
                        buildToolVer.version = reader.ReadUInt32();
                        buildVer.tools[j]    = buildToolVer;
                    }
                    lc = buildVer;
                    break;

                default:
                    lc         = new LoadCommand();
                    lc.cmd     = reader.ReadUInt32();
                    lc.cmdsize = reader.ReadUInt32();
                    reader.BaseStream.Position += lc.cmdsize - 8;
                    break;
                }
                cmds.Add(lc);
            }
            load_commands = cmds;
        }
Example #3
0
        internal void Read(BinaryReader reader)
        {
            /* definitions from: /usr/include/mach-o/loader.h */
            /*
            * The 32-bit mach header appears at the very beginning of the object file for
                * 32-bit architectures.
                */
                //	struct mach_header {
                //		uint32_t	magic;		/* mach magic number identifier */
                //		cpu_type_t	cputype;	/* cpu specifier */
                //		cpu_subtype_t	cpusubtype;	/* machine specifier */
                //		uint32_t	filetype;	/* type of file */
                //		uint32_t	ncmds;		/* number of load commands */
                //		uint32_t	sizeofcmds;	/* the size of all the load commands */
                //		uint32_t	flags;		/* flags */
                //	};

                /*
            * The 64-bit mach header appears at the very beginning of object files for
            * 64-bit architectures.
            */
                //	struct mach_header_64 {
                //		uint32_t	magic;		/* mach magic number identifier */
                //		cpu_type_t	cputype;	/* cpu specifier */
                //		cpu_subtype_t	cpusubtype;	/* machine specifier */
                //		uint32_t	filetype;	/* type of file */
                //		uint32_t	ncmds;		/* number of load commands */
                //		uint32_t	sizeofcmds;	/* the size of all the load commands */
                //		uint32_t	flags;		/* flags */
                //		uint32_t	reserved;	/* reserved */
                //	};

            magic = reader.ReadUInt32 ();
            switch (magic) {
            case MachO.MH_CIGAM:
            case MachO.MH_MAGIC:
                is64bitheader = false;
                break;
            case MachO.MH_CIGAM_64:
            case MachO.MH_MAGIC_64:
                is64bitheader = true;
                break;
            default:
                throw ErrorHelper.CreateError (1602, "Not a Mach-O dynamic library (unknown header '0x{0}'): {1}.", magic.ToString ("x"), fat_parent != null ? fat_parent.Parent.Filename : filename);
            }
            _cputype = reader.ReadInt32 ();
            _cpusubtype = reader.ReadInt32 ();
            _filetype = reader.ReadUInt32 ();
            _ncmds = reader.ReadUInt32 ();
            _sizeofcmds = reader.ReadUInt32 ();
            _flags = reader.ReadUInt32 ();
            if (is64bitheader)
                _reserved = reader.ReadUInt32 ();
            var cmds = new List<LoadCommand> ((int) ncmds);
            for (int i = 0; i < ncmds; i++) {
                var cmd = (MachO.LoadCommands) reader.ReadUInt32 ();
                reader.BaseStream.Position -= 4;
                LoadCommand lc;
                switch (cmd) {
                case MachO.LoadCommands.LoadDylib:
                case MachO.LoadCommands.LoadWeakDylib:
                case MachO.LoadCommands.ReexportDylib:
                    var dlc = new DylibLoadCommand ();
                    dlc.cmd = reader.ReadUInt32 ();
                    dlc.cmdsize = reader.ReadUInt32 ();
                    /*var nameofs = */reader.ReadUInt32 ();
                    dlc.timestamp = reader.ReadUInt32 ();
                    dlc.current_version = reader.ReadUInt32 ();
                    dlc.compatibility_version = reader.ReadUInt32 ();
                    var namelength = dlc.cmdsize - 6 * 4;
                    var namechars = reader.ReadBytes ((int) namelength);
                    // strip off any null characters at the end.
                    for (int n = namechars.Length - 1; n >= 0; n--) {
                        if (namechars [n] == 0)
                            namelength--;
                        else
                            break;
                    }
                    dlc.name = System.Text.UTF8Encoding.UTF8.GetString (namechars, 0, (int) namelength);

                    lc = dlc;
                    break;
                default:
                    lc = new LoadCommand ();
                    lc.cmd = reader.ReadUInt32 ();
                    lc.cmdsize = reader.ReadUInt32 ();
                    reader.BaseStream.Position += lc.cmdsize - 8;
                    break;
                }
                cmds.Add (lc);
            }
            load_commands = cmds;
        }