public DynamicSectionRenderer32(ElfLoader32 loader, ElfSection shdr, ElfMachine machine)
 {
     this.loader = loader;
     this.shdr   = shdr;
     if (!machineSpecificEntries.TryGetValue(machine, out machineSpecific))
     {
         machineSpecific = new Dictionary <long, Entry>();
     }
 }
Example #2
0
        private void Given_Linker()
        {
            BuildObjectFile32();
            mr.ReplayAll();

            var eil = new ElfImageLoader(sc, "foo.o", rawBytes);
            eil.LoadElfIdentification();
            var eh = Elf32_EHdr.Load(new BeImageReader(rawBytes, ElfImageLoader.HEADER_OFFSET));
            var el = new ElfLoader32(eil, eh, rawBytes);
            el.LoadSectionHeaders();
            el.LoadSymbols();
            this.linker = new ElfObjectLinker32(el, arch, rawBytes);
        }
Example #3
0
 protected void Relocate32(ElfLoader32 loader)
 {
     DumpRela32(loader);
     foreach (var relSection in loader.Sections.Where(s => s.Type == SectionHeaderType.SHT_RELA))
     {
         var symbols          = loader.Symbols[relSection.LinkedSection];
         var referringSection = relSection.RelocatedSection;
         var rdr = loader.CreateReader(relSection.FileOffset);
         for (uint i = 0; i < relSection.EntryCount(); ++i)
         {
             var rela = Elf32_Rela.Read(rdr);
             var sym  = symbols[(int)(rela.r_info >> 8)];
             RelocateEntry(sym, referringSection, rela);
         }
     }
 }
Example #4
0
        protected void DumpRela32(ElfLoader32 loader)
        {
            foreach (var section in loader.Sections.Where(s => s.Type == SectionHeaderType.SHT_RELA))
            {
                Debug.Print("RELA: offset {0:X} link section {1}",
                            section.FileOffset,
                            section.LinkedSection.Name);

                var symbols = loader.Symbols[section.LinkedSection];
                var rdr     = loader.CreateReader(section.FileOffset);
                for (uint i = 0; i < section.EntryCount(); ++i)
                {
                    var rela = Elf32_Rela.Read(rdr);
                    Debug.Print("  off:{0:X8} type:{1,-16} add:{3,-20} {4,3} {2}",
                                rela.r_offset,
                                (SparcRt)(rela.r_info & 0xFF),
                                symbols[(int)(rela.r_info >> 8)].Name,
                                rela.r_addend,
                                (int)(rela.r_info >> 8));
                }
            }
        }
Example #5
0
        protected void DumpRela32(ElfLoader32 loader)
        {
            foreach (var section in loader.Sections.Where(s => s.Type == SectionHeaderType.SHT_RELA))
            {
                Debug.Print("RELA: offset {0:X} symbol section {1}, relocating in section {2}",
                    section.FileOffset,
                    section.LinkedSection.Name,
                    section.RelocatedSection.Name);

                var symbols = loader.Symbols[section.LinkedSection];
                var rdr = loader.CreateReader(section.FileOffset);
                for (uint i = 0; i < section.EntryCount(); ++i)
                {
                    var rela = Elf32_Rela.Read(rdr);
                    Debug.Print("  off:{0:X8} type:{1,-16} add:{3,-20} {4,3} {2}",
                        rela.r_offset,
                        RelocationTypeToString(rela.r_info & 0xFF),
                        symbols[(int)(rela.r_info >> 8)].Name,
                        rela.r_addend,
                        (int)(rela.r_info >> 8));
                }
            }
        }
Example #6
0
 public PpcRelocator(ElfLoader32 loader)
 {
     this.loader = loader;
 }
Example #7
0
 public ElfRelocator32(ElfLoader32 loader)
 {
     this.loader = loader;
 }
Example #8
0
 public RelaSegmentRenderer(ElfLoader32 imgLoader, ElfSection shdr)
 {
     this.loader = imgLoader;
     this.shdr   = shdr;
 }
Example #9
0
 public ArmRelocator(ElfLoader32 loader) : base(loader)
 {
     this.loader = loader;
 }
Example #10
0
 public MipsRelocator(ElfLoader32 elfLoader) : base(elfLoader)
 {
     this.elfLoader = elfLoader;
 }
Example #11
0
 public ElfObjectLinker32(ElfLoader32 loader, IProcessorArchitecture arch, byte[] rawImage)
     : base(loader, arch, rawImage)
 {
     this.loader = loader;
     this.Segments = new List<Elf32_PHdr>();
 }
Example #12
0
 private void When_CreateLoader32(bool big_endian)
 {
     this.eil = new ElfImageLoader(sc, "foo", this.bytes);
     this.el32 = new ElfLoader32(eil, eih, this.bytes, big_endian ? ElfLoader.ELFDATA2MSB : ElfLoader.ELFDATA2LSB);
     el32.ProgramHeaders.AddRange(programHeaders);
     el32.Sections.AddRange(sections);
 }
Example #13
0
 public RelSegmentRenderer(ElfLoader32 loader, Elf32_SHdr shdr)
 {
     this.loader = loader;
     this.shdr   = shdr;
 }
Example #14
0
 public ElfRelocator32(ElfLoader32 loader)
 {
     this.loader = loader;
 }
Example #15
0
 public DynamicSectionRenderer32(ElfLoader32 loader, ElfSection shdr)
 {
     this.loader = loader;
     this.shdr = shdr;
 }
Example #16
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");
                        }
                    }
                }
            }
        }
Example #17
0
 public x86Relocator(ElfLoader32 loader) : base(loader)
 {
     this.loader = loader;
 }
Example #18
0
 public MipsRelocator(ElfLoader32 elfLoader)
 {
     this.elfLoader = elfLoader;
 }
Example #19
0
 public DynamicSectionRenderer32(ElfLoader32 loader, ElfSection shdr, ElfMachine machine)
 {
     this.loader = loader;
     this.shdr = shdr;
     if (!machineSpecificEntries.TryGetValue(machine, out machineSpecific))
     {
         machineSpecific = new Dictionary<long, Entry>();
     }
 }
Example #20
0
 public DynamicSectionRenderer(ElfLoader32 loader, ElfSection shdr)
 {
     this.loader = loader;
     this.shdr   = shdr;
 }
Example #21
0
 public ElfObjectLinker32(ElfLoader32 loader, IProcessorArchitecture arch, byte[] rawImage)
     : base(loader, arch, rawImage)
 {
     this.loader   = loader;
     this.Segments = new List <Elf32_PHdr>();
 }
Example #22
0
 private void When_CreateLoader32()
 {
     this.eil = new ElfImageLoader(sc, "foo", this.bytes);
     this.el32 = new ElfLoader32(eil, eih, this.bytes);
     el32.ProgramHeaders.AddRange(programHeaders);
     el32.Sections.AddRange(sections);
 }
Example #23
0
 public SymtabSegmentRenderer32(ElfLoader32 loader, Elf32_SHdr shdr)
 {
     this.loader = loader;
     this.shdr   = shdr;
 }
Example #24
0
 public RelSegmentRenderer(ElfLoader32 loader, ElfSection shdr)
 {
     this.loader = loader;
     this.shdr = shdr;
 }
Example #25
0
 public SymtabSegmentRenderer32(ElfLoader32 loader, ElfSection shdr)
 {
     this.loader = loader;
     this.shdr   = shdr;
 }
Example #26
0
 public XtensaRelocator(ElfLoader32 elfLoader32)
 {
     this.elfLoader32 = elfLoader32;
 }