Jump to content
Tuts 4 You

How to get RVA from MethodBase?


CreateAndInject

Recommended Posts

CreateAndInject
Posted

How to get the RVA of a method from MethodBase/MethodInfo?

CreateAndInject
Posted

Anyone know? I can only get RVA by dnlib. Is there any way to get the RVA of a method from System.Reflection.MethodBase?

Posted

you can read MetaData table and get method File Offset then convert that to RVA (which dnlib does that). but i think you looking for a direct way to get RVA from methodbase, right?

CreateAndInject
Posted

Yes, I want to get RVA from memory rather than file.

Posted

maybe this

public static IntPtr GetAddress(MethodBase methodBase)
{
    RuntimeHelpers.PrepareMethod(methodBase.MethodHandle);
    return methodBase.MethodHandle.GetFunctionPointer();
}

 

CreateAndInject
Posted

@ewwink RVA isn't function pointer, please see the picture below.

@wwh1004 It seems MetadataLocator can't get RVA, I can't search any results by "RVA". MethodsMap contains some infomation of MethodBase, but still doesn't contain RVA.

 

image.png.4263cca2cba4667fe7186653c3e462e6.png

Posted
9 hours ago, CreateAndInject said:

@ewwink RVA isn't function pointer, please see the picture below.

@wwh1004 It seems MetadataLocator can't get RVA, I can't search any results by "RVA". MethodsMap contains some infomation of MethodBase, but still doesn't contain RVA.

 

image.png.4263cca2cba4667fe7186653c3e462e6.png

You can use MetadataLocator to get an instance of IMetaDataTables then use https://docs.microsoft.com/en-us/dotnet/framework/unmanaged-api/metadata/imetadatatables-getrow-method

  • Thanks 1
CreateAndInject
Posted

It works with the following code, thanks!

static int GetRVA(MethodBase mb) {
	var mdInfo = MetadataInfo.GetMetadataInfo(mb.Module);
	int table = mb.MetadataToken >> 24;
	int rid = mb.MetadataToken & 0xffffff;
	mdInfo.MetaDataTables.GetRow((uint)table, (uint)rid, out var ppRow);
	return *(int*)ppRow;
}

 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...