Exemple #1
0
 private void Write(RelocationSection rs)
 {
     sb.AppendLine($"[Relocation section]");
     for (int i = 0; i < rs.Refs.Length; i++)
     {
         sb.AppendLine($"\tpointer[{i}] = {rs.Refs[i].Value:x4}; {rs.Refs[i]}");
     }
 }
        public IReadable Read()
        {
            var peHeaderReader = (PeHeaderReader) new PeHeaderReader(_filePath).Read();

            BaseRelocationTable = peHeaderReader.Is32BitPeHeader
                ? peHeaderReader.PeHeader32.OptionalHeader.BaseRelocationTable
                : peHeaderReader.PeHeader64.OptionalHeader.BaseRelocationTable;

            var address = RvaToRawFormatConverter.RvaToOffset32(BaseRelocationTable.VirtualAddress,
                                                                peHeaderReader.SectionHeaders,
                                                                peHeaderReader.Is32BitPeHeader
                    ? peHeaderReader.PeHeader32.OptionalHeader.SectionAlignment
                    : peHeaderReader.PeHeader64.OptionalHeader.SectionAlignment);

            using var fs = new FileStream(_filePath, FileMode.Open, FileAccess.Read);
            var br = new BinaryReader(fs);

            fs.Seek(address, SeekOrigin.Begin);

            Int32 currentSize;

            for (var i = 0; i < BaseRelocationTable.Size; i += currentSize)
            {
                currentSize = 0;
                var relocationBlock = new RelocationBlock
                {
                    PageRva   = br.ReadUInt32(),
                    BlockSize = br.ReadUInt32()
                };

                currentSize += Marshal.SizeOf(relocationBlock.PageRva);
                currentSize += Marshal.SizeOf(relocationBlock.BlockSize);

                for (var j = 0; j < (relocationBlock.BlockSize - 8) / 2; j++)
                {
                    var relocationSectionInfo = br.ReadUInt16();
                    var relocationSection     = new RelocationSection
                    {
                        Type   = (RelocationType)((relocationSectionInfo & 0xf000) >> 12),
                        Offset = (UInt16)(relocationSectionInfo & 0x0fff)
                    };
                    relocationBlock.RelocationSections.Add(relocationSection);
                    currentSize += Marshal.SizeOf(relocationSection.Offset);
                }

                RelocationBlocks.Add(relocationBlock);
            }

            br.Dispose();
            br.Close();

            return(this);
        }
Exemple #3
0
        private void SetupRelocationTable()
        {
            this.Align(BezelEngineArchive.RawAlignment);
            RelocationSection FileMainSect;

            long RelocationTableOffset = Position;

            int  EntryIndex = 0;
            uint EntryPos   = 0;

            foreach (RelocationEntry entry in _savedSection1Entries)
            {
                Console.WriteLine("Pos = " + entry.Position + " " + entry.StructCount + " " + entry.OffsetCount + " " + entry.PadingCount + " " + entry.Hint);
            }

            _savedSection1Entries = _savedSection1Entries.OrderBy(o => o.Position).ToList();
            FileMainSect          = new RelocationSection(EntryPos, EntryIndex, Section1Size, _savedSection1Entries);

            _savedRelocatedSections.Add(FileMainSect);
        }