假设我正在编写一些 C++ 程序,但我不希望有关程序工作方式的二进制泄漏信息。我知道我们可能可以从二进制文件中删除符号,但类名仍然存储并可通过 RTTI 信息检索。那么我该如何避免呢?如果我需要禁用 RTTI,哪些 C++ 功能将不再可用?
最佳答案
禁用 RTTI 时的损失取决于您的编译器(和编译器版本)。
对于 gcc 4.5,您可以阅读:
-fno-rtti Disable generation of information about every class with virtual functions for use by the C++ runtime type identification features (dynamic_cast' andtypeid'). If you don't use those parts of the language, you can save some space by using this flag. Note that exception handling uses the same information, but it will generate it as needed. The `dynamic_cast' operator can still be used for casts that do not require runtime type information, i.e. casts to void * or to unambiguous base classes.
对于 Visual Studio 2013:
Use /GR if the compiler cannot statically resolve an object type in your code. You usually need the /GR option when your code uses dynamic_cast Operator or typeid. However, /GR increases the size of the .rdata sections of your image. If your code does not use dynamic_cast or typeid, /GR- may produce a smaller image
因此,实际上,在这些情况下,它仅禁用 dynamic_cast
和 typeid
。
但是,如果目标是隐藏有关可执行文件的行为和一些信息,我认为 strip
和禁用 RTTI 不是最佳解决方案。这可能是一个好的开始,但要获得真正的二进制保护,您必须看到打包程序/可执行文件保护程序/混淆工具 (http://en.wikipedia.org/wiki/Executable_compression)。
关于c++ - 从 rtti 中剥离类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23304820/