Windows DLL中的字符串表在哪里?

问题描述 投票:3回答:3

我编写了一个例程来从一个装有LoadLibrary的DLL中转储符号和部分,但不知道如何解码部分名称长于IMAGE_SIZEOF_SHORT_NAME的MinGW 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] = '\0';
  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];

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

c++ winapi dll mingw portable-executable
3个回答
3
投票

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

符号不一定映射到内存中。

PointerToSymbolTable(我认为)是一个文件偏移量,而不是内存偏移量(并且,如上所述,它们不是同一个东西)。

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

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


1
投票

我知道我已经迟到了,但似乎问题没有得到完全回答。在找到解决方案之前,我来到这里寻找相同的答案。

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

字符串表紧跟在符号表之后。符号数与符号表起始地址一起在文件头信息中,每个符号为18个字节。很容易计算到字符串表。

前4个字节是字符串表本身的大小(因此第一个条目的/ 4)。每个字符串都是从前一个字符串开始的。每个字符串都以null结尾。这并不重要,因为偏移是在部分名称本身,所以你可以直视它们。


-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.