0xd4d / dnlib

Reads and writes .NET assemblies and modules
MIT License
2.18k stars 587 forks source link

The precise method for calculating 'RVA' #539

Open SoheilMV opened 10 months ago

SoheilMV commented 10 months ago

In the research I conducted, I realized that the RVA value depends on "DataDirectories.Length" which can be observed in the path "moduleDefMD.Metadata.PEImage.ImageNTHeaders.OptionalHeader".

example :

ModuleContext _modCtx = ModuleDef.CreateModuleContext();
ModuleDefMD _moduleDefMD = ModuleDefMD.Load("path", _modCtx);
...
IImageOptionalHeader _optionalHeader = _moduleDefMD.Metadata.PEImage.ImageNTHeaders.OptionalHeader;
uint _numberOfRvaAndSizes = (uint)_optionalHeader.DataDirectories.Length;
var _options = new ModuleWriterOptions(_moduleDefMD)
{
    MetadataOptions = { Flags = MetadataFlags.PreserveAll },
    PEHeadersOptions = { NumberOfRvaAndSizes = _numberOfRvaAndSizes },
    Logger = DummyLogger.NoThrowInstance,
    WritePdb = true
};
_moduleDefMD.Write("path", _options);