0xd4d / dnlib

Reads and writes .NET assemblies and modules
MIT License
2.13k stars 582 forks source link

InvalidOperationException when md rva isn't alignment #543

Open CreateAndInject opened 7 months ago

CreateAndInject commented 7 months ago

https://github.com/0xd4d/dnlib/blob/71946df40541d1d97f62e249f3f39036e04e60da/src/DotNet/Writer/Metadata.cs#L3695-L3736

this?

        public void SetOffset(FileOffset offset, RVA rva) {
            uint rem = (uint)rva % HEAP_ALIGNMENT;
                        ......
            foreach (var heap in metadataHeader.Heaps) {
                offset = (offset - rem).AlignUp(HEAP_ALIGNMENT) + rem;
                rva = (rva - rem).AlignUp(HEAP_ALIGNMENT) + rem;
                ...
            }
wtfsck commented 7 months ago

Why isn't it aligned?

CreateAndInject commented 7 months ago

test.zip

var module = ModuleDefMD.Load(@"test.exe");
module.NativeWrite("output.exe"); // InvalidOperationException 
CreateAndInject commented 6 months ago

What's wrong?

wtfsck commented 6 months ago

I haven't looked at this yet, but if you know how to fix this, you can send a PR.

CreateAndInject commented 6 months ago

No idea if alignment is requirement and how the alignment should be.