Does Mono.Cecil take care of branches etc location

2019-02-09 01:34发布

问题:

Well this question may seem odd but it's simple - my point is if i have a "goto" (brtrue etc) in the decompiled code like example

br IL_0003
call *****
IL_0003: ret

and I add a command after that **** call will the br at the top point to ret like it should or to that code.

does Cecil do it by itself or I have to take care of all those branches ? :/ it wouldn't be very hard to fix them but if Cecil doesn't then I simply won't start this project, I've no time (or knowledge) for advanced IL magic :P

(yes I know it won't be IL_0003 it's just for example)

回答1:

Yes, Cecil will update the branch for you.

The only case you have to take care of, is the case where the branch is a short form branch. If you inject too much instructions, it might overflow.

There's a very simple way to handle this. Before injecting code, simply call the extension methods SimplifyMacros from the Mono.Cecil.Rocks, like this:

method.Body.SimplifyMacros ();

This will turn the br.s into br.

And when you're done injecting code, simply call:

method.Body.OptimizeMacros ();

Which is the opposite operation, that is, it will try to turn br into br.s if possible.