Exemplo n.º 1
0
        void CreateSection(ModuleWriterBase writer)
        {
            // move some PE parts to separate section to prevent it from being hashed
            var  peSection = new PESection("", 0x60000020);
            bool moved     = false;
            uint alignment;

            if (writer.StrongNameSignature != null)
            {
                alignment = writer.TextSection.Remove(writer.StrongNameSignature).Value;
                peSection.Add(writer.StrongNameSignature, alignment);
                moved = true;
            }
            var managedWriter = writer as ModuleWriter;

            if (managedWriter != null)
            {
                if (managedWriter.ImportAddressTable != null)
                {
                    alignment = writer.TextSection.Remove(managedWriter.ImportAddressTable).Value;
                    peSection.Add(managedWriter.ImportAddressTable, alignment);
                    moved = true;
                }
                if (managedWriter.StartupStub != null)
                {
                    alignment = writer.TextSection.Remove(managedWriter.StartupStub).Value;
                    peSection.Add(managedWriter.StartupStub, alignment);
                    moved = true;
                }
            }
            if (moved)
            {
                writer.Sections.Add(peSection);
            }

            // create section
            var nameBuffer = new byte[8];

            nameBuffer[0] = (byte)(name1 >> 0);
            nameBuffer[1] = (byte)(name1 >> 8);
            nameBuffer[2] = (byte)(name1 >> 16);
            nameBuffer[3] = (byte)(name1 >> 24);
            nameBuffer[4] = (byte)(name2 >> 0);
            nameBuffer[5] = (byte)(name2 >> 8);
            nameBuffer[6] = (byte)(name2 >> 16);
            nameBuffer[7] = (byte)(name2 >> 24);
            var newSection = new PESection(Encoding.ASCII.GetString(nameBuffer), 0xE0000040);

            writer.Sections.Insert(random.NextInt32(writer.Sections.Count), newSection);

            // random padding at beginning to prevent revealing hash key
            newSection.Add(new ByteArrayChunk(random.NextBytes(0x10)), 0x10);

            // create index
            var bodyIndex = new JITBodyIndex(methods.Select(method => writer.MetaData.GetToken(method).Raw));

            newSection.Add(bodyIndex, 0x10);

            // save methods
            foreach (MethodDef method in methods.WithProgress(context.Logger))
            {
                if (!method.HasBody)
                {
                    continue;
                }

                MDToken token = writer.MetaData.GetToken(method);

                var jitBody    = new JITMethodBody();
                var bodyWriter = new JITMethodBodyWriter(writer.MetaData, method.Body, jitBody, random.NextUInt32(), writer.MetaData.KeepOldMaxStack || method.Body.KeepOldMaxStack);
                bodyWriter.Write();
                jitBody.Serialize(token.Raw, key, fieldLayout);
                bodyIndex.Add(token.Raw, jitBody);

                method.Body = NopBody;
                writer.MetaData.TablesHeap.MethodTable[token.Rid].ImplFlags |= (ushort)MethodImplAttributes.NoInlining;
                context.CheckCancellation();
            }
            bodyIndex.PopulateSection(newSection);

            // padding to prevent bad size due to shift division
            newSection.Add(new ByteArrayChunk(new byte[4]), 4);
        }
Exemplo n.º 2
0
 public void Add(uint token, JITMethodBody body)
 {
     Debug.Assert(bodies.ContainsKey(token));
     bodies[token] = body;
 }