来自不相关类的虚拟功能表条目 [英] Virtual Function Table entry from class that is not related
问题描述
我正在浏览一个简单的C ++ Windows程序(我没有源代码,只有二进制文件)的VFT(VMT),这些程序是由Visual Studio编译并进行了某种优化的.
I am browsing through VFTs (VMTs) of a simple C++ Windows program (I don't have a source code, only binary), compiled by Visual Studio with some sort of optimization on.
我注意到这是使用继承和多态性.我找到了该程序具有的每个类的struct s_RTTIBaseClassArray
的位置.在该位置,有一个指向struct _s_RTTIBaseClassDescriptor
的指针数组.基类描述符数组应该为您提供有关当前类所源自的所有类的信息.
I noticed that is uses inheritance and polymorphism. I found the location of struct s_RTTIBaseClassArray
for each class that the program has. In that location there is an array of pointers to struct _s_RTTIBaseClassDescriptor
. The array of base class descriptors should give you information about all the classes that the current class is derived from.
虚函数(方法)表是一个表,其中包含指向当前类的所有虚函数的指针.但是在一些类的VFT中,我找到了一个指向虚拟方法的指针,该方法实际上属于另一个类(根据基类数组),该类与当前类无关.下面的示例:
Virtual Function (Method) Table is a table that contains pointers to all the virtual functions of the current class. However in VFT of a few classes I found a pointer to a virtual method that actually belongs to a different class that is (acording to the Base Class Array) not related to the current class. Example below:
ClassA_BaseClassArray:
dd offset ClassA_BaseClassDescriptor
dd offset ClassB_BaseClassDescriptor ; ClassA inherits from ClassB
ClassB_BaseClassArray:
dd offset ClassB_BaseClassDescriptor
ClassC_BaseClassArray:
dd offset ClassC_BaseClassDescriptor
ClassA_VMT:
dd offset ClassA_VM1 ; virtual method of ClassA
dd offset ClassA_VM2
dd offset ClassB_VM2 ; virtual method of ClassB - override
dd offset ClassC_VM3 ; virtual method of ClassC - NOTHING TO DO HERE
dd offset ClassA_VM3
这个例子很简短,实际的类有更多的虚方法.
The example is short, the actual classes have much more virtual methods.
在检查ClassC_VM3
之后,我发现它仅由两条指令组成:
After examination of ClassC_VM3
I noticed, that it consists of just two instructions:
mov eax, [ecx+10h]
retn
到目前为止,我发现大约有3个类似于该示例的VMT,无关的方法总是这么短.
I found about 3 VMTs similar to this example so far, the unrelated method is always this short.
我的问题是:是什么原因造成的? ClassC_VM3
的代码是否与某些ClassA
方法的代码相同,所以编译器只是对其进行了优化?
My question is: what is causing this? Could the code of ClassC_VM3
be identical to the code of some ClassA
method, so the compiler just optimized it out?
推荐答案
这可能是由 COMDAT折叠,该优化将具有完全相同的机器代码的功能合并为一个.由于它是一个简单的函数,因此机会很大.
This might be caused by COMDAT folding, an optimization that merges functions that have the same exact machine code into one. Since it's such a simple function the chances of that are good.
这篇关于来自不相关类的虚拟功能表条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!