其中 是 Windows DLL 中的字符串表

Where is the string table in a Windows DLL?

本文关键字:字符串 DLL Windows 其中      更新时间:2023-10-16

我编写了一个例程来从加载LoadLibrary但不确定如何解码节名长于IMAGE_SIZEOF_SHORT_NAME的 DLL 中转储符号和节

例如,如果我将它们打印为字符串,则 MinGW DLL 会输出以下部分:

[".text", ".data", ".rdata", ".pdata", ".xdata", ".bss", ".edata", ".idata",
 ".CRT", ".tls", ".reloc", "/4", "/19", "/31", "/45", "/57", "/70", "/81",
 "/92"]

objdump.exe的其他部分得到它们:

.debug_aranges
.debug_info
.debug_abbrev
.debug_line
.debug_frame
.debug_str
.debug_loc
.debug_ranges

都比IMAGE_SIZEOF_SHORT_NAME长. MSDN 解释说:

For longer names, this member contains a forward slash (/) followed by an ASCII representation of a decimal number that is an offset into the string table.

所以我有以下代码:

  Char buffer[IMAGE_SIZEOF_SHORT_NAME + 1];
  std::strncpy(buffer, reinterpret_cast<const Char * const>(section_header_ptr[i].Name), IMAGE_SIZEOF_SHORT_NAME);
  buffer[IMAGE_SIZEOF_SHORT_NAME] = '';
  const Char * name = buffer;
  if (name[0] == '/') {
    const Long rva = std::strtol(name + 1, NULL, 10);
    if ((LONG_MAX == rva) || (LONG_MIN == rva) || ((0 == rva) && (name[0] != '0'))) {
      static const Char * const failure = "failed to convert offset";
      name = failure;
    }
    // -- How do I get the string table here? and use the offset? --
  }

阅读 COFF 规范,我看到字符串表在符号条目之后,所以它应该是

HMODULE handle = LoadLibrary("some_mingw_library.dll");
PIMAGE_DOS_HEADER idh = (PIMAGE_DOS_HEADER)(handle);
PIMAGE_NT_HEADERS inh = (PIMAGE_NT_HEADERS)(((const uint8_t*)(idh)) + idh->e_lfanew)
PIMAGE_FILE_HEADER ifh = &inh->FileHeader;
PIMAGE_SYMBOL is = (PIMAGE_SYMBOL)(((const uint8_t*)(idh)) + ifh->PointerToSymbolTable)
const char * const string_table = &is[ifh->NumberOfSymbols];

但我得到的东西绝对不是字符串表。 我可以在我的十六进制编辑器中看到字符串表。 可移植可执行文件中的字符串表在哪里?

当可执行文件映射到内存中时,它不会作为一个连续的块加载。 各节分散在各处,如节标题中所述。

符号根本不必然映射到内存中。

PointerToSymbolTable(我认为)是文件偏移量,而不是内存偏移量(如上所述,它们不是一回事)。

EXE 和 DLL 根本不应该有 COFF 符号,尽管这个文件显然有。

大多数此类问题的答案都可以在PEDUMP中找到。

我知道

我来晚了,但似乎这个问题没有得到完全回答。在找到解决方案之前,我自己来到这里寻找相同的答案。

可移植可执行文件中的字符串表在哪里?

字符串表紧跟在符号表之后。符号的数量与符号表起始地址一起出现在文件头信息中,每个符号为 18 个字节。一个很好的简单计算来获得字符串表。

前 4 个字节是字符串表本身的大小(因此第一个条目为/4)。每个字符串都遵循前一个字符串。每个字符串都以 null 结尾。这并不重要,因为偏移量位于部分名称本身中,因此您可以直接查看它们。

字符串表在可执行文件 (DLL) 中不可用,仅在 OBJ 文件中可用。这就是在分析 PE 文件时无法枚举这些内容的原因。PE 文件的节名称永远不会超过 8 个字符。