Ejemplo n.º 1
0
        public void ShouldOpenNotFatMachOUsingFatMethod()
        {
            var machOs = MachOReader.LoadFat(Utilities.GetBinaryStream("MachO-OSX-ppc-openssl-1.0.1h"), true);

            Assert.AreEqual(1, machOs.Count);
            Assert.AreEqual(Machine.PowerPc, machOs[0].Machine);
        }
Ejemplo n.º 2
0
        public void ShouldOpenMachO()
        {
            var fileName = Utilities.GetBinaryLocation("simple-mach-o");

            ELFSharp.MachO.MachO machO;
            Assert.AreEqual(MachOResult.OK, MachOReader.TryLoad(fileName, out machO));
        }
Ejemplo n.º 3
0
        public void ShouldOpen32BitMachOAsStream()
        {
            var fileName = Utilities.GetBinary("simple-32-mach-o");
            var machO    = MachOReader.Load(File.OpenRead(fileName));

            Assert.AreEqual(Machine.I386, machO.Machine);
        }
Ejemplo n.º 4
0
        public void ShouldFindCorrectMachine()
        {
            var fileName = Utilities.GetBinaryLocation("simple-mach-o");
            var machO    = MachOReader.Load(fileName);

            Assert.AreEqual(Machine.X86_64, machO.Machine);
        }
Ejemplo n.º 5
0
        public void ShouldOpen32BitMachO()
        {
            var fileName = Utilities.GetBinaryLocation("simple-32-mach-o");
            var machO    = MachOReader.Load(fileName);

            Assert.AreEqual(Machine.I386, machO.Machine);
        }
Ejemplo n.º 6
0
        public void ShouldListSymbols()
        {
            var machO       = MachOReader.Load(Utilities.GetBinaryStream("shared-32-mach-o"), true);
            var symbolTable = machO.GetCommandsOfType <SymbolTable>().Single();

            CollectionAssert.IsSubsetOf(new [] { "_funkcja", "_inna_funkcja", "_jeszcze_inna_funkcja" }, symbolTable.Symbols.Select(x => x.Name).ToArray());
        }
Ejemplo n.º 7
0
        public void ShouldHaveZeroExceptions()
        {
            var fileName = Utilities.GetBinary("simple-mach-o");
            var machO    = MachOReader.Load(fileName, false);

            Assert.AreEqual(0, machO.Exceptions.Count);
        }
Ejemplo n.º 8
0
        public void ShouldFind64BitEntryPoint()
        {
            var machO      = MachOReader.Load(Utilities.GetBinaryStream("simple-mach-o"), true);
            var entryPoint = machO.GetCommandsOfType <EntryPoint>().Single();

            Assert.AreEqual(0xF6B, entryPoint.Value);
            Assert.AreEqual(0, entryPoint.StackSize);
        }
Ejemplo n.º 9
0
        public void ShouldFindSectionWithProperData64()
        {
            var machO   = MachOReader.Load(Utilities.GetBinaryStream("simple-mach-o"), true);
            var segment = machO.GetCommandsOfType <Segment>().Single(x => x.Name == "__TEXT");
            var data    = segment.Sections.Single(x => x.Name == "__cstring").GetData();

            Assert.AreEqual(Encoding.ASCII.GetBytes("Hello world\0"), data);
        }
Ejemplo n.º 10
0
        public void ShouldOpenFatMachO()
        {
            var machOs = MachOReader.LoadFat(Utilities.GetBinaryStream("Undecimus"), true);

            Assert.AreEqual(2, machOs.Count);
            Assert.AreEqual(Machine.Arm64, machOs[0].Machine);
            Assert.AreEqual(Machine.Arm64, machOs[1].Machine);
        }
Ejemplo n.º 11
0
        public static void ShouldFindSectionWithDataAndNoDataArm64(string segmentName, string sectionName)
        {
            var machO   = MachOReader.Load(Utilities.GetBinaryStream(@"3dengine_libretro_ios.dylib"), true);
            var segment = machO.GetCommandsOfType <Segment>().Single(x => x.Name == "__TEXT");
            var section = segment.Sections.Single(x => x.Name == "__text");

            section.GetData();
        }
Ejemplo n.º 12
0
        public void ShouldOpenMachOObjectFile()
        {
            // intermediate object file has only 1 segment.
            var result = MachOReader.TryLoad(Utilities.GetBinaryStream("simple-mach-o-object.o"), true, out ELFSharp.MachO.MachO machO);

            Assert.AreEqual(MachOResult.OK, result);
            Assert.AreEqual(machO.FileType, FileType.Object);
            Assert.AreEqual(machO.GetCommandsOfType <Segment>().Count(), 1);
        }
Ejemplo n.º 13
0
        public void ShouldFind32BitEntryPoint()
        {
            var fileName   = Utilities.GetBinary("simple-32-mach-o");
            var machO      = MachOReader.Load(fileName);
            var entryPoint = machO.GetCommandsOfType <EntryPoint>().Single();

            Assert.AreEqual(0xF60, entryPoint.Value);
            Assert.AreEqual(0, entryPoint.StackSize);
        }
Ejemplo n.º 14
0
        public void ShouldFindIdDylib()
        {
            var machO   = MachOReader.Load(Utilities.GetBinaryStream("3dengine_libretro_ios.dylib"), true);
            var idDylib = machO.GetCommandsOfType <IdDylib>().Single();

            Assert.AreEqual("3dengine_libretro_ios.dylib", idDylib.Name);
            Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 1, 0, DateTimeKind.Utc), idDylib.Timestamp);
            Assert.AreEqual(new Version(0, 0, 0), idDylib.CurrentVersion);
            Assert.AreEqual(new Version(0, 0, 0), idDylib.CompatibilityVersion);
        }
Ejemplo n.º 15
0
        public static byte[] ReadBytesOrThrow(this Stream stream, int count, Dictionary <string, long> exceptionsToQueue = null)
        {
            var result = new byte[count];

            while (count > 0)
            {
                var readThisTurn = stream.Read(result, result.Length - count, count);
                if (readThisTurn == 0)
                {
                    if (exceptionsToQueue != null)
                    {
                        MachOReader.AddException(exceptionsToQueue, new EndOfStreamException().Message);
                        Array.Resize(ref result, result.Length - count);
                        return(result);
                    }
                    throw new EndOfStreamException($"End of stream reached while {count} bytes more expected.");
                }
                count -= readThisTurn;
            }
            return(result);
        }
Ejemplo n.º 16
0
        public void ShouldFindLoadDylibs()
        {
            var machO      = MachOReader.Load(Utilities.GetBinaryStream("3dengine_libretro_ios.dylib"), true);
            var loadDylibs = machO.GetCommandsOfType <LoadDylib>().ToList();

            Assert.AreEqual(3, loadDylibs.Count);

            Assert.AreEqual("/System/Library/Frameworks/OpenGLES.framework/OpenGLES", loadDylibs[0].Name);
            Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 2, 0, DateTimeKind.Utc), loadDylibs[0].Timestamp);
            Assert.AreEqual(new Version(1, 0, 0), loadDylibs[0].CurrentVersion);
            Assert.AreEqual(new Version(1, 0, 0), loadDylibs[0].CompatibilityVersion);

            Assert.AreEqual("/usr/lib/libSystem.B.dylib", loadDylibs[1].Name);
            Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 2, 0, DateTimeKind.Utc), loadDylibs[1].Timestamp);
            Assert.AreEqual(new Version(1281, 0, 0), loadDylibs[1].CurrentVersion);
            Assert.AreEqual(new Version(1, 0, 0), loadDylibs[1].CompatibilityVersion);

            Assert.AreEqual("/usr/lib/libc++.1.dylib", loadDylibs[2].Name);
            Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 2, 0, DateTimeKind.Utc), loadDylibs[2].Timestamp);
            Assert.AreEqual(new Version(800, 7, 0), loadDylibs[2].CurrentVersion);
            Assert.AreEqual(new Version(1, 0, 0), loadDylibs[2].CompatibilityVersion);
        }
Ejemplo n.º 17
0
        public void ShouldOpen32BitMachO()
        {
            var machO = MachOReader.Load(Utilities.GetBinaryStream("simple-32-mach-o"), true);

            Assert.AreEqual(Machine.X86, machO.Machine);
        }
Ejemplo n.º 18
0
        internal override unsafe bool GetFileVersion(string dll, out int major, out int minor, out int revision, out int patch)
        {
            using FileStream stream = File.OpenRead(dll);
            MachOReader reader = new MachOReader(stream);

            MachOHeader64 header = reader.Read <MachOHeader64>();

            if (header.Magic != MachOHeader64.ExpectedMagic)
            {
                throw new NotSupportedException();
            }

            long dataOffset = 0;
            long dataSize   = 0;

            byte[] dataSegmentName = Encoding.ASCII.GetBytes("__DATA\0");
            byte[] dataSectionName = Encoding.ASCII.GetBytes("__data\0");
            for (int c = 0; c < header.NumberOfCommands; c++)
            {
                MachOCommand     command     = reader.Read <MachOCommand>();
                MachOCommandType commandType = command.Command;
                int commandSize = command.CommandSize;

                if (commandType == MachOCommandType.Segment64)
                {
                    long position = stream.Position;
                    MachOSegmentCommand64 segmentCommand = reader.Read <MachOSegmentCommand64>();
                    if (new ReadOnlySpan <byte>(segmentCommand.SegmentName, dataSegmentName.Length).SequenceEqual(dataSegmentName))
                    {
                        for (int s = 0; s < segmentCommand.NumberOfSections; s++)
                        {
                            MachOSection64 section = reader.Read <MachOSection64>();
                            if (new ReadOnlySpan <byte>(section.SectionName, dataSectionName.Length).SequenceEqual(dataSectionName))
                            {
                                dataOffset = section.Offset;
                                dataSize   = section.Size;
                                break;
                            }
                        }

                        break;
                    }

                    stream.Position = position;
                }

                stream.Seek(commandSize - sizeof(MachOCommand), SeekOrigin.Current);
            }

            Span <byte> buffer     = stackalloc byte[s_versionLength];
            long        address    = dataOffset;
            long        endAddress = address + dataSize;

            Span <byte> bytes = stackalloc byte[1];
            Span <char> chars = stackalloc char[1];

            while (address < endAddress)
            {
                int read = reader.Read(address, buffer);
                if (read < s_versionLength)
                {
                    break;
                }

                if (!buffer.SequenceEqual(s_versionString))
                {
                    address++;
                    continue;
                }

                address += s_versionLength;

                // TODO:  This should be cleaned up to not read byte by byte in the future.  Leaving it here
                // until we decide whether to rewrite the Linux coredumpreader or not.
                StringBuilder builder = new StringBuilder();
                while (address < endAddress)
                {
                    read = reader.Read(address, bytes);
                    if (read < bytes.Length)
                    {
                        break;
                    }

                    if (bytes[0] == '\0')
                    {
                        break;
                    }

                    if (bytes[0] == ' ')
                    {
                        try
                        {
                            Version v = Version.Parse(builder.ToString());
                            major    = v.Major;
                            minor    = v.Minor;
                            revision = v.Build;
                            patch    = v.Revision;
                            return(true);
                        }
                        catch (FormatException)
                        {
                            break;
                        }
                    }

                    fixed(byte *bytesPtr = &MemoryMarshal.GetReference(bytes))
                    fixed(char *charsPtr = &MemoryMarshal.GetReference(chars))
                    {
                        _ = Encoding.ASCII.GetChars(bytesPtr, bytes.Length, charsPtr, chars.Length);
                    }

                    _ = builder.Append(chars[0]);
                    address++;
                }

                break;
            }

            major = minor = revision = patch = 0;
            return(false);
        }
Ejemplo n.º 19
0
        public void ShouldOpenMachO()
        {
            var fileName = Utilities.GetBinary("simple-mach-o");

            Assert.AreEqual(MachOResult.OK, MachOReader.IsMachO(fileName));
        }
Ejemplo n.º 20
0
 public void ShouldOpenMachO()
 {
     Assert.AreEqual(MachOResult.OK,
                     MachOReader.TryLoad(Utilities.GetBinaryStream("simple-mach-o"), true, out _));
 }
Ejemplo n.º 21
0
        public void ShouldFindCorrectMachine()
        {
            var machO = MachOReader.Load(Utilities.GetBinaryStream("simple-mach-o"), true);

            Assert.AreEqual(Machine.X86_64, machO.Machine);
        }
Ejemplo n.º 22
0
        public void ShouldOpenBigEndianMachO()
        {
            var machO = MachOReader.Load(Utilities.GetBinaryStream("MachO-OSX-ppc-openssl-1.0.1h"), true);

            Assert.AreEqual(Machine.PowerPc, machO.Machine);
        }
Ejemplo n.º 23
0
        public void ShouldRecognizeFatMachO()
        {
            var result = MachOReader.TryLoad(Utilities.GetBinaryStream("Undecimus"), true, out _);

            Assert.AreEqual(MachOResult.FatMachO, result);
        }
Ejemplo n.º 24
0
 public void ShouldThrowOnFatMachOWhenOpenedAsNotFat()
 {
     Assert.Throws <InvalidOperationException>(() => MachOReader.Load(Utilities.GetBinaryStream("Undecimus"), true));
 }