Пример #1
0
        public static object ReadPointer(Type pointerType, int size, ImageReader rdr, ReaderContext ctx)
        {
            Debug.Print("Reading pointer at offset {0}, size {1}", rdr.Offset, size);
            uint newOffset;

            switch (size)
            {
            default:
                throw new InvalidOperationException("Field size must be > 0.");

            case 1: newOffset = rdr.ReadByte(); break;

            case 2: newOffset = rdr.ReadUInt16(); break;

            case 4: newOffset = rdr.ReadUInt32(); break;
            }
            Debug.Print("Structure of type {0} must start at offset {1:X}", pointerType.Name, newOffset);
            rdr        = rdr.Clone();
            rdr.Offset = newOffset;

            var dst = Activator.CreateInstance(pointerType);
            var sr  = new StructureReader(dst);

            sr.Read(rdr);
            return(dst);
        }
Пример #2
0
 public List<ProgramResource> ReadLanguageDirectory(ImageReader rdr, uint resourceType, string resourceId)
 {
     const uint DIR_MASK = 0x80000000;
     var flags = rdr.ReadUInt32();
     var date = rdr.ReadUInt32();
     var version = rdr.ReadUInt32();
     var cNameEntries = rdr.ReadUInt16();
     var cIdEntries = rdr.ReadUInt16();
     var entries = new List<ProgramResource>();
     for (int i = 0; i < cNameEntries; ++i)
     {
         var rvaName = rdr.ReadUInt32();
         var rvaEntry = rdr.ReadUInt32();
         var subRdr = new LeImageReader(imgLoaded, rvaResources + (rvaEntry & ~DIR_MASK));
         if ((rvaEntry & DIR_MASK) != 0)
             throw new BadImageFormatException();
         entries.Add(ReadResourceEntry(subRdr, resourceId, ReadResourceString(rvaName), resourceType));
     }
     for (int i = 0; i < cIdEntries; ++i)
     {
         var id = rdr.ReadUInt32();
         var rvaEntry = rdr.ReadUInt32();
         var subRdr = new LeImageReader(imgLoaded, rvaResources + (rvaEntry & ~DIR_MASK));
         if ((rvaEntry & DIR_MASK) != 0)
             throw new BadImageFormatException();
         entries.Add(ReadResourceEntry(subRdr, resourceId, id.ToString(), resourceType));
     }
     return entries;
 }
Пример #3
0
        public void VisitPrimitive(PrimitiveType pt)
        {
            switch (pt.Size)
            {
            case 1:
                fmt.WriteKeyword("db");
                fmt.Write("\t");
                fmt.Write(string.Format("0x{0:X2}", rdr.ReadByte()));
                fmt.WriteLine();
                return;

            case 2:
                fmt.WriteKeyword("dw");
                fmt.Write("\t");
                fmt.Write(string.Format("0x{0:X4}", rdr.ReadUInt16()));
                fmt.WriteLine();
                return;

            case 4:
                fmt.WriteKeyword("dd");
                fmt.Write("\t");
                fmt.Write(string.Format("0x{0:X8}", rdr.ReadUInt32()));
                fmt.WriteLine();
                return;

            case 8:
                fmt.WriteKeyword("dq");
                fmt.Write("\t");
                fmt.Write(string.Format("0x{0:X16}", rdr.ReadUInt32()));
                fmt.WriteLine();
                return;
            }
            throw new NotImplementedException();
        }
Пример #4
0
 public List<ProgramResource> ReadResourceDirectory(ImageReader rdr)
 {
     const uint DIR_MASK = 0x80000000;
     var flags = rdr.ReadUInt32();
     var date = rdr.ReadUInt32();
     var version = rdr.ReadUInt32();
     var cNameEntries = rdr.ReadUInt16();
     var cIdEntries = rdr.ReadUInt16();
     var entries = new List<ProgramResource>();
     for (int i = 0; i < cNameEntries; ++i)
     {
         var rvaName = rdr.ReadUInt32();
         var rvaEntry = rdr.ReadUInt32();
         var subRdr = new LeImageReader(imgLoaded, rvaResources + (rvaEntry & ~DIR_MASK));
         if ((rvaEntry & DIR_MASK) == 0)
             throw new BadImageFormatException();
         if ((rvaName & DIR_MASK) != 0)
         {
             var e = new ProgramResourceGroup
             {
                 //Name = ReadResourceString(rvaName),
                 Name = ReadResourceUtf16leString(rvaResources + (rvaName & ~DIR_MASK)),
             };
             e.Resources.AddRange(ReadNameDirectory(subRdr, 0));
             entries.Add(e);
         }
     }
     for (int i = 0; i < cIdEntries; ++i)
     {
         var id = rdr.ReadUInt32();
         var rvaEntry = rdr.ReadUInt32();
         var subRdr = new LeImageReader(imgLoaded, rvaResources + (rvaEntry & ~DIR_MASK));
         if ((rvaEntry & DIR_MASK) == 0)
             throw new BadImageFormatException();
         var e = new ProgramResourceGroup
         {
             Name = GenerateResourceName(id),
         };
         e.Resources.AddRange(ReadNameDirectory(subRdr, id));
         entries.Add(e);
     }
     return entries;
 }
Пример #5
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;
 }
Пример #6
0
        public ushort e_shstrndx;       // section name string table index

        public static Elf32_EHdr Load(ImageReader rdr)
        {
            return new Elf32_EHdr
            {
                e_type = rdr.ReadUInt16(),
                e_machine = rdr.ReadUInt16(),
                e_version = rdr.ReadUInt32(),
                e_entry = rdr.ReadUInt32(),
                e_phoff = rdr.ReadUInt32(),
                e_shoff = rdr.ReadUInt32(),
                e_flags = rdr.ReadUInt32(),
                e_ehsize = rdr.ReadUInt16(),
                e_phentsize = rdr.ReadUInt16(),
                e_phnum = rdr.ReadUInt16(),
                e_shentsize = rdr.ReadUInt16(),
                e_shnum = rdr.ReadUInt16(),
                e_shstrndx = rdr.ReadUInt16(),
            };
        }
Пример #7
0
        public static object ReadPointer(Type pointerType, int size, ImageReader rdr, ReaderContext ctx)
        {
            Debug.Print("Reading pointer at offset {0}, size {1}", rdr.Offset, size);
            uint newOffset;
            switch (size)
            {
            default:
                throw new InvalidOperationException("Field size must be > 0.");
            case 1: newOffset = rdr.ReadByte(); break;
            case 2: newOffset = rdr.ReadUInt16(); break;
            case 4: newOffset = rdr.ReadUInt32(); break;
            }
            Debug.Print("Structure of type {0} must start at offset {1:X}", pointerType.Name, newOffset);
            rdr = rdr.Clone();
            rdr.Offset = newOffset;

            var dst = Activator.CreateInstance(pointerType);
            var sr = new StructureReader(dst);
            sr.Read(rdr);
            return dst;
        }
Пример #8
0
public const short 	IMAGE_REL_MIPS_PAIR    	    =  0x0025; // This relocation is only valid when it immediately follows a REFHI or SECRELHI relocation. Its SymbolTableIndex contains a displacement and not an index into the symbol table. 

        public override void ApplyRelocation(Address baseOfImage, uint page, ImageReader rdr, RelocationDictionary relocations)
        {
            ushort fixup = rdr.ReadUInt16();
            Address offset = baseOfImage + page + (fixup & 0x0FFFu);
            var imgR = program.CreateImageReader(offset);
            var imgW = program.CreateImageWriter(offset);
            uint w = imgR.ReadUInt32();
            int s;
            switch (fixup >> 12)
            {
            case IMAGE_REL_MIPS_ABSOLUTE:
                // Used for padding to 4-byte boundary, ignore.
                break;
            case IMAGE_REL_MIPS_REFWORD:
                break;
            case IMAGE_REL_MIPS_JMPADDR:
                break;
            case IMAGE_REL_MIPS_REFHI:
                w = imgR.ReadUInt32();
                //w += (fixup & 0x0FFFu);
                //imgW.WriteUInt32(w);
                s = rdr.ReadInt16();
                w = (uint)(w + s);
                // w points to something.
                break;
            case IMAGE_REL_MIPS_REFLO:
                // w points to something.
                break;
            default:
                dcSvc.Warn(
                    dcSvc.CreateAddressNavigator(program, offset),
                    string.Format(
                        "Unsupported MIPS PE fixup type: {0:X}",
                        fixup >> 12));
                break;
            }
        }