public override void Render(ImageSegment segment, Program program, Formatter formatter)
        {
            // Get the entry that has the segment# for the string table.
            var dynStrtab = loader.GetDynEntries(shdr.FileOffset).Where(d => d.d_tag == DT_STRTAB).FirstOrDefault();

            if (dynStrtab == null)
            {
                return;
            }
            this.strtabSection = loader.GetSectionInfoByAddr(dynStrtab.d_ptr);
            foreach (var entry in loader.GetDynEntries(shdr.FileOffset))
            {
                DtFormat fmt;
                string   entryName;
                if (!machineSpecific.TryGetValue(entry.d_tag, out Entry dser) &&
                    !entries.TryGetValue(entry.d_tag, out dser))
                {
                    entryName = string.Format("{0:X8}    ", entry.d_tag);
                    fmt       = DtFormat.Hexadecimal;
                }
                else
                {
                    entryName = dser.Name;
                    fmt       = dser.Format;
                }
                RenderEntry(entryName, fmt, entry, formatter);
                formatter.WriteLine();
            }
        }
Example #2
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 #3
0
        public override void Render(ImageSegment segment, Program program, Formatter formatter)
        {
            // Get the entry that has the segment# for the string table.
            var dynStrtab = loader.GetDynEntries(shdr.FileOffset).Where(d => d.d_tag == DT_STRTAB).FirstOrDefault();

            if (dynStrtab == null)
            {
                return;
            }
            var strtabSection = loader.GetSectionInfoByAddr(dynStrtab.d_ptr);

            foreach (var entry in loader.GetDynEntries(shdr.FileOffset))
            {
                switch (entry.d_tag)
                {
                default:
                    formatter.Write("{0,-12} {1:X8}", entry.d_tag, entry.d_val);
                    break;

                case DT_DEBUG:
                    formatter.Write("{0,-12} {1:X8}", "DT_DEBUG", entry.d_val);
                    break;

                case DT_FINI:
                    formatter.Write("{0,-12} ", "DT_FINI");
                    formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
                    break;

                case DT_HASH:
                    formatter.Write("{0,-12} ", "DT_HASH");
                    formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
                    break;

                case DT_INIT:
                    formatter.Write("{0,-12} ", "DT_INIT");
                    formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
                    break;

                case DT_JMPREL:
                    formatter.Write("{0,-12} ", "DT_JMPREL");
                    formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
                    break;

                case DT_NEEDED:
                    formatter.Write("{0,-12} {1}", "DT_NEEDED", loader.ReadAsciiString(strtabSection.FileOffset + entry.d_ptr));
                    break;

                case DT_STRSZ:
                    formatter.Write("{0,-12} {1:X}", "DT_STRSZ", entry.d_val);
                    break;

                case DT_STRTAB:
                    formatter.Write("{0,-12} ", "DT_STRTAB");
                    formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
                    break;

                case DT_SYMENT:
                    formatter.Write("{0,-12} {1}", "DT_SYMENTTRTAB", entry.d_val);
                    break;

                case DT_SYMTAB:
                    formatter.Write("{0,-12} ", "DT_SYMTAB");
                    formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
                    break;
                }
                formatter.WriteLine();
            }
        }