Exemplo n.º 1
0
        public override Dictionary <int, ElfSymbol> LoadSymbolsSection(ElfSection symSection)
        {
            ElfImageLoader.trace.Inform("== Symbols from {0} ==", symSection.Name);
            var stringtableSection = symSection.LinkedSection !;
            var rdr     = CreateReader(symSection.FileOffset);
            var symbols = new Dictionary <int, ElfSymbol>();

            for (ulong i = 0; i < symSection.Size / symSection.EntrySize; ++i)
            {
                if (!Elf32_Sym.TryLoad(rdr, out var sym))
                {
                    ElfImageLoader.trace.Warn("Unable to load symbol entry {0} from {1}", i, symSection.Name);
                    continue;
                }
                var symName = RemoveModuleSuffix(ReadAsciiString(stringtableSection.FileOffset + sym.st_name));
                ElfImageLoader.trace.Verbose("  {0,3} {1,-25} {2,-12} {3,6} {4} {5,-15} {6:X8} {7,9}",
                                             i,
                                             string.IsNullOrWhiteSpace(symName) ? "<empty>" : symName,
                                             (ElfSymbolType)(sym.st_info & 0xF),
                                             sym.st_shndx,
                                             GetBindingName((ElfSymbolBinding)(sym.st_info >> 4)),
                                             GetSectionName(sym.st_shndx),
                                             sym.st_value,
                                             sym.st_size);
                var name = RemoveModuleSuffix(ReadAsciiString(stringtableSection.FileOffset + sym.st_name));
                symbols.Add((int)i, new ElfSymbol(name)
                {
                    Type         = (ElfSymbolType)(sym.st_info & 0xF),
                    SectionIndex = sym.st_shndx,
                    Value        = sym.st_value,
                    Size         = sym.st_size,
                });
            }
            return(symbols);
        }
Exemplo n.º 2
0
        public override Dictionary <int, ElfSymbol> LoadSymbolsSection(ElfSection symSection)
        {
            ElfImageLoader.trace.Inform("== Symbols from {0} ==", symSection.Name);
            var stringtableSection = symSection.LinkedSection;
            var rdr     = CreateReader(symSection.FileOffset);
            var symbols = new Dictionary <int, ElfSymbol>();

            for (ulong i = 0; i < symSection.Size / symSection.EntrySize; ++i)
            {
                var sym     = Elf32_Sym.Load(rdr);
                var symName = RemoveModuleSuffix(ReadAsciiString(stringtableSection.FileOffset + sym.st_name));
                ElfImageLoader.trace.Verbose("  {0,3} {1,-25} {2,-12} {3,6} {4,-15} {5:X8} {6,9}",
                                             i,
                                             string.IsNullOrWhiteSpace(symName) ? "<empty>" : symName,
                                             (ElfSymbolType)(sym.st_info & 0xF),
                                             sym.st_shndx,
                                             GetSectionName(sym.st_shndx),
                                             sym.st_value,
                                             sym.st_size);
                symbols.Add((int)i, new ElfSymbol
                {
                    Name         = RemoveModuleSuffix(ReadAsciiString(stringtableSection.FileOffset + sym.st_name)),
                    Type         = (ElfSymbolType)(sym.st_info & 0xF),
                    SectionIndex = sym.st_shndx,
                    Value        = sym.st_value,
                    Size         = sym.st_size,
                });
            }
            return(symbols);
        }
Exemplo n.º 3
0
 public static Elf32_Sym Load(ImageReader rdr)
 {
     var sym = new Elf32_Sym();
     sym.st_name = rdr.ReadUInt32();
     sym.st_value = rdr.ReadUInt32();
     sym.st_size = rdr.ReadUInt32();
     sym.st_info = rdr.ReadByte();
     sym.st_other = rdr.ReadByte();
     sym.st_shndx = rdr.ReadUInt16();
     return sym;
 }
Exemplo n.º 4
0
        public static Elf32_Sym Load(ImageReader rdr)
        {
            var sym = new Elf32_Sym();

            sym.st_name  = rdr.ReadUInt32();
            sym.st_value = rdr.ReadUInt32();
            sym.st_size  = rdr.ReadUInt32();
            sym.st_info  = rdr.ReadByte();
            sym.st_other = rdr.ReadByte();
            sym.st_shndx = rdr.ReadUInt16();
            return(sym);
        }
Exemplo n.º 5
0
        public override ElfSymbol LoadSymbol(ulong offsetSymtab, ulong symbolIndex, ulong entrySize, ulong offsetStringTable)
        {
            var rdr = CreateReader(offsetSymtab + entrySize * symbolIndex);
            var sym = Elf32_Sym.Load(rdr);

            return(new ElfSymbol
            {
                Name = RemoveModuleSuffix(ReadAsciiString(offsetStringTable + sym.st_name)),
                Type = (ElfSymbolType)(sym.st_info & 0xF),
                Bind = sym.st_info >> 4,
                SectionIndex = sym.st_shndx,
                Value = sym.st_value,
                Size = sym.st_size,
            });
        }
Exemplo n.º 6
0
 public static bool TryLoad(EndianImageReader rdr, out Elf32_Sym sym)
 {
     sym = new Elf32_Sym();
     if (
         rdr.TryReadUInt32(out sym.st_name) &&
         rdr.TryReadUInt32(out sym.st_value) &&
         rdr.TryReadUInt32(out sym.st_size) &&
         rdr.TryReadByte(out sym.st_info) &&
         rdr.TryReadByte(out sym.st_other) &&
         rdr.TryReadUInt16(out sym.st_shndx))
     {
         return(true);
     }
     else
     {
         sym = null !;
         return(false);
     }
 }
Exemplo n.º 7
0
        public override ElfSymbol?LoadSymbol(ulong offsetSymtab, ulong symbolIndex, ulong entrySize, ulong offsetStringTable)
        {
            var rdr = CreateReader(offsetSymtab + entrySize * symbolIndex);

            if (Elf32_Sym.TryLoad(rdr, out var sym))
            {
                var name = RemoveModuleSuffix(ReadAsciiString(offsetStringTable + sym.st_name));
                return(new ElfSymbol(name)
                {
                    Type = (ElfSymbolType)(sym.st_info & 0xF),
                    Bind = (ElfSymbolBinding)(sym.st_info >> 4),
                    SectionIndex = sym.st_shndx,
                    Value = sym.st_value,
                    Size = sym.st_size,
                });
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 8
0
        public void RelocateOld(Program program)
        {
            uint nextFakeLibAddr = ~1u; // See R_386_PC32 below; -1 sometimes used for main

            for (int i = 1; i < loader.Sections.Count; ++i)
            {
                var ps = loader.Sections[i];
                if (ps.Type == SectionHeaderType.SHT_REL)
                {
                    // A section such as .rel.dyn or .rel.plt (without an addend field).
                    // Each entry has 2 words: r_offset and r_info. The r_offset is just the offset from the beginning
                    // of the section (section given by the section header's sh_info) to the word to be modified.
                    // r_info has the type in the bottom byte, and a symbol table index in the top 3 bytes.
                    // A symbol table offset of 0 (STN_UNDEF) means use value 0. The symbol table involved comes from
                    // the section header's sh_link field.
                    var   pReloc = loader.CreateReader(ps.FileOffset);
                    ulong size   = ps.Size;
                    // NOTE: the r_offset is different for .o files (ET_REL in the e_type header field) than for exe's
                    // and shared objects!
                    uint destNatOrigin  = 0;
                    uint destHostOrigin = 0;
                    if (loader.Header.e_type == ElfImageLoader.ET_REL)
                    {
                        var destSection = loader.Sections[i].RelocatedSection;
                        destNatOrigin  = destSection.Address.ToUInt32();
                        destHostOrigin = (uint)destSection.FileOffset;
                    }
                    var symSection  = loader.Sections[i].LinkedSection; // associated symbol table
                    var strSection  = symSection.LinkedSection;         // Section index for the string section assoc with this
                    var pStrSection = strSection.FileOffset;
                    var symOrigin   = symSection.FileOffset;
                    var relocR      = loader.CreateReader(0);
                    var relocW      = loader.CreateWriter(0);
                    for (uint u = 0; u < size; u += 2 * sizeof(uint))
                    {
                        uint r_offset = pReloc.ReadUInt32();
                        uint info     = pReloc.ReadUInt32();

                        byte relType     = (byte)info;
                        uint symTabIndex = info >> 8;
                        uint pRelWord; // Pointer to the word to be relocated
                        if (loader.Header.e_type == ElfImageLoader.ET_REL)
                        {
                            pRelWord = destHostOrigin + r_offset;
                        }
                        else
                        {
                            if (r_offset == 0)
                            {
                                continue;
                            }
                            var destSec = loader.GetSectionInfoByAddr(r_offset);
                            pRelWord      = ~0u; // destSec.uHostAddr - destSec.uNativeAddr + r_offset;
                            destNatOrigin = 0;
                        }
                        uint A, S = 0, P;
                        int  nsec;
                        var  sym = Elf32_Sym.Load(loader.CreateReader(symOrigin + symTabIndex * Elf32_Sym.Size));
                        switch (relType)
                        {
                        case 0: // R_386_NONE: just ignore (common)
                            break;

                        case 1: // R_386_32: S + A
                            // Read the symTabIndex'th symbol.
                            S = sym.st_value;
                            if (loader.Header.e_type == ElfImageLoader.ET_REL)
                            {
                                nsec = sym.st_shndx;
                                if (nsec >= 0 && nsec < loader.Sections.Count)
                                {
                                    S += loader.Sections[nsec].Address.ToUInt32();
                                }
                            }
                            A = relocR.ReadUInt32(pRelWord);
                            relocW.WriteUInt32(pRelWord, S + A);
                            break;

                        case 2: // R_386_PC32: S + A - P
                            if (ElfLoader32.ELF32_ST_TYPE(sym.st_info) == ElfLoader.STT_SECTION)
                            {
                                nsec = sym.st_shndx;
                                if (nsec >= 0 && nsec < loader.Sections.Count)
                                {
                                    S = loader.Sections[nsec].Address.ToUInt32();
                                }
                            }
                            else
                            {
                                S = sym.st_value;
                                if (S == 0)
                                {
                                    // This means that the symbol doesn't exist in this module, and is not accessed
                                    // through the PLT, i.e. it will be statically linked, e.g. strcmp. We have the
                                    // name of the symbol right here in the symbol table entry, but the only way
                                    // to communicate with the loader is through the target address of the call.
                                    // So we use some very improbable addresses (e.g. -1, -2, etc) and give them entries
                                    // in the symbol table
                                    uint   nameOffset = sym.st_name;
                                    string pName      = loader.ReadAsciiString(pStrSection + nameOffset);
                                    // this is too slow, I'm just going to assume it is 0
                                    //S = GetAddressByName(pName);
                                    //if (S == (e_type == E_REL ? 0x8000000 : 0)) {
                                    S = nextFakeLibAddr--; // Allocate a new fake address
                                    loader.AddSymbol(S, pName);
                                    //}
                                }
                                else if (loader.Header.e_type == ElfImageLoader.ET_REL)
                                {
                                    nsec = sym.st_shndx;
                                    if (nsec >= 0 && nsec < loader.Sections.Count)
                                    {
                                        S += loader.Sections[nsec].Address.ToUInt32();
                                    }
                                }
                            }
                            A = relocR.ReadUInt32(pRelWord);
                            P = destNatOrigin + r_offset;
                            relocW.WriteUInt32(pRelWord, S + A - P);
                            break;

                        case 6: // R_386_GLOB_DAT
                            // This relocation type is used to set a global offset table entry to the address of the
                            // specified symbol. The special relocation type allows one to determine the
                            // correspondence between symbols and global offset table entries.
                            S = sym.st_value;
                            relocW.WriteUInt32(pRelWord, S);
                            break;

                        case 7:
                        case 8:    // R_386_RELATIVE
                            break; // No need to do anything with these, if a shared object

                        default:
                            throw new NotSupportedException("Relocation type " + (int)relType + " not handled yet");
                        }
                    }
                }
            }
        }