Jump to content
Tuts 4 You
Sign in to follow this  
CreateAndInject

How to get RVA from MethodBase?

Recommended Posts

CreateAndInject

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

Share this post


Link to post
CreateAndInject

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

Share this post


Link to post
Reza-HNA

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?

Share this post


Link to post
CreateAndInject

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

Share this post


Link to post
ewwink

maybe this

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

 

Share this post


Link to post
CreateAndInject

@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

Share this post


Link to post
wwh1004
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

Share this post


Link to post
CreateAndInject

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

Share this post


Link to post

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
Sign in to follow this  
×
×
  • Create New...