Example #1
0
        public void HasOneRealPublicSymbol()
        {
            Stream           lib   = HelloSwiftAsLibrary(null);
            List <MachOFile> macho = MachO.Read(lib, null).ToList();

            Assert.IsNotNull(macho);
            Assert.AreEqual(1, macho.Count);
            MachOFile file = macho [0];
            List <SymTabLoadCommand> symbols = file.load_commands.OfType <SymTabLoadCommand> ().ToList();

            Assert.AreEqual(1, symbols.Count);
            NListEntryType    nlet    = symbols [0].nlist [0].EntryType;
            List <NListEntry> entries = symbols [0].nlist.
                                        Where((nle, i) => nle.IsPublic && nle.EntryType == NListEntryType.InSection).ToList();

            Assert.AreEqual(1, entries.Count);
        }
Example #2
0
        static void TLFunctionsForStream(Stream stm, PlatformName platform, Dictionary <string, string> functions)
        {
            List <NListEntry> entries = null;
            List <MachOFile>  macho   = MachO.Read(stm, null).ToList();
            MachOFile         file    = macho [0];

            List <SymTabLoadCommand> symbols = file.load_commands.OfType <SymTabLoadCommand> ().ToList();
            NListEntryType           nlet    = symbols [0].nlist [0].EntryType;

            entries = symbols [0].nlist.
                      Where((nle, i) => nle.IsPublic && nle.EntryType == NListEntryType.InSection).ToList();

            bool isOldVersion = IsOldVersion(entries);

            foreach (var entry in entries)
            {
                if (!entry.IsSwiftEntryPoint())
                {
                    continue;
                }
                TLDefinition def = null;
                try {
                    def = Decomposer.Decompose(entry.str, isOldVersion, Offset(entry));
                } catch { }
                if (def != null)
                {
                    // this skips over privatized names
                    var tlf = def as TLFunction;
                    if (tlf != null && tlf.Name != null && tlf.Name.Name.Contains("..."))
                    {
                        continue;
                    }
                    if (tlf?.Name != null)
                    {
                        if (!functions.ContainsKey(tlf.Name.Name))
                        {
                            functions.Add(tlf.Name.Name, tlf.MangledName);
                        }
                    }
                }
            }
        }
Example #3
0
        static ModuleInventory FromStreamInto(Stream stm, ModuleInventory inventory,
                                              ErrorHandling errors, string fileName = null)
        {
            Ex.ThrowOnNull(errors, "errors");
            Ex.ThrowOnNull(stm, "stm");
            OffsetStream      osstm   = null;
            List <NListEntry> entries = null;

            try {
                List <MachOFile> macho = MachO.Read(stm, null).ToList();
                MachOFile        file  = macho [0];

                List <SymTabLoadCommand> symbols = file.load_commands.OfType <SymTabLoadCommand> ().ToList();
                NListEntryType           nlet    = symbols [0].nlist [0].EntryType;
                entries = symbols [0].nlist.
                          Where((nle, i) => nle.IsPublic && nle.EntryType == NListEntryType.InSection).ToList();
                inventory.Architecture = file.Architecture;
                osstm = new OffsetStream(stm, file.StartOffset);
            } catch (Exception e) {
                errors.Add(ErrorHelper.CreateError(ReflectorError.kCantHappenBase + 58, e, "Unable to retrieve functions from {0}: {1}",
                                                   fileName ?? "stream", e.Message));
                return(inventory);
            }

            bool isOldVersion = IsOldVersion(entries);

            foreach (var entry in entries)
            {
                if (!entry.IsSwiftEntryPoint())
                {
                    continue;
                }
                TLDefinition def = null;
                try {
                    def = Decomposer.Decompose(entry.str, isOldVersion, Offset(entry));
                } catch (RuntimeException e) {
                    var except = new RuntimeException(e.Code, false, e, $"error decomposing {entry.str}: {e.Message}, skipping");
                    errors.Add(except);
                }
                catch (Exception e) {
                    var except = new RuntimeException(ReflectorError.kDecomposeBase + 0, false, e, $"unexpected error handling {entry.str}: {e.Message}, skipping.");
                    errors.Add(except);
                }
                if (def != null)
                {
                    // this skips over privatized names
                    var tlf = def as TLFunction;
                    if (tlf != null && tlf.Name != null && tlf.Name.Name.Contains("..."))
                    {
                        continue;
                    }
                    try {
                        inventory.Add(def, osstm);
                    } catch (RuntimeException e) {
                        e = new RuntimeException(e.Code, e.Error, $"error dispensing top level definition of type {def.GetType ().Name} decomposed from {entry.str}: {e.Message}");
                        errors.Add(e);
                    }
                }
                else
                {
                    var ex = ErrorHelper.CreateWarning(ReflectorError.kInventoryBase + 18, $"entry {entry.str} uses an unsupported swift feature, skipping.");
                    errors.Add(ex);
                }
            }

            return(inventory);
        }