【问题标题】:Why string constants are stored in .text section in dlls?为什么字符串常量存储在 dll 的 .text 部分?
【发布时间】:2019-07-04 14:48:36
【问题描述】:

我正在学习反编译和分析一个名为 d3d9.dll 的 dll 文件,据我所知,字符串常量存储在 dll 的 .rdata 部分中,但如下所示(IDA 字符串窗口),它们存储在 .text 部分中。我不知道为什么会这样。它们何时存储在 .rdata 中,何时存储在 .text 中?

.text:4FE45850  00000050    C   Declaration can't map to legacy FVF because a nonzero stream index is used: %d. 

.text:4FE458A0  0000007B    C   Declaration can't map to fixed function FVF because fixed function pipeline does not support generation methods %s or %s. 

.text:4FE45920  000000B9    C   Declaration can't map to fixed function FVF because fixed function requires that generation methods %s, %s or %s can only be used with type %s and usage being one of: %s, %s, %s or %s.

.text:4FE459E0  0000008D    C   Declaration can't map to fixed function FVF because fixed function requires that generation method %s can only be used with usages %s or %s. 

.text:4FE45A70  000000A3    C   Declaration can't map to fixed function FVF because gaps or overlap between vertex elements are not allowed. Offset encountered is: %d, but expected offset is %d.

.text:4FE45B18  0000005E    C   Declaration can't map to fixed function FVF because the type for this element is unsupported.

.text:4FE45B78  00000072    C   Declaration can't map to fixed function FVF because a generation method other than D3DDECLMETHOD_DEFAULT is used.

【问题讨论】:

  • 我相信答案是 Windows
  • 这是怎么复制的?!它专门讨论 .rdata 中的事情,而不是 .text,而这个问题涵盖了 100% 的反向 - do 进入 .text 的事情,即使链接的答案表明这些字符串应该是在rdata.
  • 一个短字符串可以作为代码的一部分直接放入寄存器。编译器可以将任何东西放在任何地方,只要它可以工作。
  • 我认为这两个问题是不同的,我想知道为什么以及何时将字符串常量存储在 .text 部分而不是 .rdata 中。此外,我认为如果字符串和 int 变量之间存在差异,我尝试了第一种情况 'const char foo[] = "fdsafdsa";',字符串仍然在 const 数据部分而不是文本部分。

标签: c++ c dll compilation


【解决方案1】:

这是一种常见的优化。节标题占用一点空间,因此避免使用.rdata 可能是有益的。 .text 部分具有只读和初始化的所有必需属性。它还具有附加的执行属性,但这并没有什么坏处。因此,不存在阻止编译器将字符串放入 .text 的技术障碍,而且还有一个小优势。

【讨论】:

    【解决方案2】:

    这完全取决于编译器存储数据的方式和位置。例如,如果你参加这个小程序:

    #include <stdio.h>
    
    int main()
    {
            char text[] = "Hey";
            puts(text);
    }
    

    经过优化编译,gcc 完全删除任何部分的字符串并将其存储为字节 - 生成的程序集是:

        movl    $7955784, 4(%rsp)
        call    puts@PLT
    

    它可以看到字符串不需要在其他任何地方访问,所以做这种优化就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-02
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      相关资源
      最近更新 更多