Beispiel #1
0
        public binary_library.ISymbol FindSymbol(string s)
        {
            if (bf == null)
            {
                throw new Exception("BinaryFile not set");
            }

            uint hash   = Hash.HashFunction(s);
            long bucket = (long)hash % nbucket;

            r.BaseStream.Seek(bucket_start + bucket * EntrySize(), SeekOrigin.Begin);
            long cur_sym_idx = ReadIntPtr();

            while (cur_sym_idx != 0)
            {
                binary_library.ISymbol cur_sym = bf.GetSymbol((int)cur_sym_idx);
                if (s.Equals(cur_sym.Name))
                {
                    return(cur_sym);
                }

                r.BaseStream.Seek(chain_start + cur_sym_idx * EntrySize(), SeekOrigin.Begin);
                cur_sym_idx = ReadIntPtr();
            }

            return(null);
        }
Beispiel #2
0
        static void LoadELFFile(string name, Trie t)
        {
            binary_library.IBinaryFile file = new binary_library.elf.ElfFile();
            file.Filename = name;
            file.Read();

            int sym_count = file.GetSymbolCount();

            for (int i = 0; i < sym_count; i++)
            {
                binary_library.ISymbol sym = file.GetSymbol(i);
                if ((sym.Name != null) && (sym.Name != ""))
                {
                    t.AddSymbol(sym.Name, sym.Offset);
                }
            }
        }
Beispiel #3
0
        // Build the hash table
        List <int>[] BuildHashTable(List <ElfSymbol> f)
        {
            int sc = f.Count;
            int bc = CalculateBucketCount(sc);

            List <int>[] ht = new List <int> [bc];

            for (int i = 1; i < sc; i++)
            {
                binary_library.ISymbol sym = f[i];
                uint hash      = HashFunction(sym.Name);
                int  bucket_no = (int)(hash % (uint)bc);

                if (ht[bucket_no] == null)
                {
                    ht[bucket_no] = new List <int>();
                }
                ht[bucket_no].Add(i);
            }

            return(ht);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            if (!ParseArgs(args))
            {
                return;
            }

            // Ensure the input file is readable
            System.IO.FileInfo input_fi = new System.IO.FileInfo(input);
            if (!input_fi.Exists)
            {
                System.Console.WriteLine("Unable to open: " + input);
                return;
            }

            // Determine the output filename if not specified
            if (output == null)
            {
                output = input_fi.FullName + ".hash";
            }

            // Load up the input file
            binary_library.IBinaryFile f = new binary_library.elf.ElfFile();
            f.Filename = input;
            f.Read();

            // Determine the bitness if not specified
            if (bitness == -1)
            {
                switch (f.Bitness)
                {
                case binary_library.Bitness.Bits32:
                    bitness = 0;
                    break;

                case binary_library.Bitness.Bits64:
                    bitness = 1;
                    break;

                case binary_library.Bitness.BitsUnknown:
                    System.Console.WriteLine("Warning: unable to determine bitness of " + input + " - defaulting to 32 bits");
                    bitness = 0;
                    break;

                default:
                    System.Console.WriteLine("Unsupported bitness of " + input + " - " + f.Bitness.ToString());
                    return;
                }
            }

            // Write the output file
            if (ver == 2)
            {
                // output copy of the original ELF file with a .hash section
                var hs = f.CreateSection();
                hs.Name         = ".hash";
                hs.AddrAlign    = 0x1000;
                hs.IsAlloc      = true;
                hs.IsWriteable  = false;
                hs.IsExecutable = false;
                hs.HasData      = true;

                // locate it somewhere after the last offset and virtual address in the file
                if (f.IsExecutable)
                {
                    long  last_offset = 0;
                    ulong last_vaddr  = 0;
                    for (int i = 0; i < f.GetSectionCount(); i++)
                    {
                        var s = f.GetSection(i);
                        if (s != null && s.IsAlloc)
                        {
                            if (s.LoadAddress + (ulong)s.Length > last_vaddr)
                            {
                                last_vaddr = s.LoadAddress + (ulong)s.Length;
                            }
                            if (s.FileOffset + s.Length > last_offset)
                            {
                                last_offset = s.FileOffset + s.Length;
                            }
                        }
                    }

                    if ((last_vaddr & 0xfff) != 0)
                    {
                        last_vaddr = (last_vaddr + 0x1000UL) & ~0xfffUL;
                    }
                    if ((last_offset & 0xfff) != 0)
                    {
                        last_offset = (last_offset + 0x1000L) & ~0xfffL;
                    }

                    hs.LoadAddress = last_vaddr;
                    hs.FileOffset  = last_offset;

                    // Create start symbol
                    binary_library.ISymbol hss = null;

                    // see if one exists first
                    foreach (var sym in f.GetSymbols())
                    {
                        if (sym.Name == hash_sym)
                        {
                            hss = sym;
                            break;
                        }
                    }
                    if (hss == null)
                    {
                        hss      = f.CreateSymbol();
                        hss.Name = hash_sym;
                        hs.AddSymbol(hss);
                    }
                    hss.Type       = binary_library.SymbolType.Global;
                    hss.ObjectType = binary_library.SymbolObjectType.Object;
                    hss.Offset     = hs.LoadAddress;

                    // Update the value of update_sym if it exists
                    if (update_sym != null)
                    {
                        foreach (var sym in f.GetSymbols())
                        {
                            if (sym.Name == update_sym)
                            {
                                // get offset
                                var offset = sym.Offset - sym.DefinedIn.LoadAddress;

                                // write as lsb depending on bitness
                                byte[] v;
                                switch (f.Bitness)
                                {
                                case binary_library.Bitness.Bits32:
                                    v = BitConverter.GetBytes((uint)hs.LoadAddress);
                                    break;

                                case binary_library.Bitness.Bits64:
                                    v = BitConverter.GetBytes((ulong)hs.LoadAddress);
                                    break;

                                default:
                                    throw new NotSupportedException();
                                }
                                for (int i = 0; i < v.Length; i++)
                                {
                                    sym.DefinedIn.Data[(int)offset + i] = v[i];
                                }
                            }
                        }
                    }
                }

                f.AddSection(hs);

                f.Filename = output;
                ((binary_library.elf.ElfFile)f).CreateHashSection = true;
                ((binary_library.elf.ElfFile)f).SortSymbols       = true;
                f.Write();
            }
            else
            {
                // generate a separate hash file
                Hash h = new Hash();
                System.IO.FileStream   fs = new System.IO.FileStream(output, System.IO.FileMode.Create);
                System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
                h.Write(bw, f, ver, 0, bitness);
            }
        }