21

作为一个编程练习,我正在用 C 编写一个标记和清除垃圾收集器。我希望扫描数据段(全局等)以查找指向已分配内存的指针,但我不知道如何获取该段的地址。我怎么能这样做?

4

5 回答 5

33

如果你在 Windows 上工作,那么有一些 Windows API 可以帮助你。

//store the base address the loaded Module
dllImageBase = (char*)hModule; //suppose hModule is the handle to the loaded Module (.exe or .dll)

//get the address of NT Header
IMAGE_NT_HEADERS *pNtHdr = ImageNtHeader(hModule);

//after Nt headers comes the table of section, so get the addess of section table
IMAGE_SECTION_HEADER *pSectionHdr = (IMAGE_SECTION_HEADER *) (pNtHdr + 1);

ImageSectionInfo *pSectionInfo = NULL;

//iterate through the list of all sections, and check the section name in the if conditon. etc
for ( int i = 0 ; i < pNtHdr->FileHeader.NumberOfSections ; i++ )
{
     char *name = (char*) pSectionHdr->Name;
     if ( memcmp(name, ".data", 5) == 0 )
     {
          pSectionInfo = new ImageSectionInfo(".data");
          pSectionInfo->SectionAddress = dllImageBase + pSectionHdr->VirtualAddress;

          **//range of the data segment - something you're looking for**
          pSectionInfo->SectionSize = pSectionHdr->Misc.VirtualSize;
          break;
      }
      pSectionHdr++;
}

将 ImageSectionInfo 定义为,

struct ImageSectionInfo
{
      char SectionName[IMAGE_SIZEOF_SHORT_NAME];//the macro is defined WinNT.h
      char *SectionAddress;
      int SectionSize;
      ImageSectionInfo(const char* name)
      {
            strcpy(SectioName, name); 
       }
};

这是一个完整的、最小的 WIN32 控制台程序,您可以在 Visual Studio 中运行,它演示了 Windows API 的使用:

#include <stdio.h>
#include <Windows.h>
#include <DbgHelp.h>
#pragma comment( lib, "dbghelp.lib" )

void print_PE_section_info(HANDLE hModule) // hModule is the handle to a loaded Module (.exe or .dll)
{
   // get the location of the module's IMAGE_NT_HEADERS structure
   IMAGE_NT_HEADERS *pNtHdr = ImageNtHeader(hModule);

   // section table immediately follows the IMAGE_NT_HEADERS
   IMAGE_SECTION_HEADER *pSectionHdr = (IMAGE_SECTION_HEADER *)(pNtHdr + 1);

   const char* imageBase = (const char*)hModule;
   char scnName[sizeof(pSectionHdr->Name) + 1];
   scnName[sizeof(scnName) - 1] = '\0'; // enforce nul-termination for scn names that are the whole length of pSectionHdr->Name[]

   for (int scn = 0; scn < pNtHdr->FileHeader.NumberOfSections; ++scn)
   {
      // Note: pSectionHdr->Name[] is 8 bytes long. If the scn name is 8 bytes long, ->Name[] will
      // not be nul-terminated. For this reason, copy it to a local buffer that's nul-terminated
      // to be sure we only print the real scn name, and no extra garbage beyond it.
      strncpy(scnName, (const char*)pSectionHdr->Name, sizeof(pSectionHdr->Name));

      printf("  Section %3d: %p...%p %-10s (%u bytes)\n",
         scn,
         imageBase + pSectionHdr->VirtualAddress,
         imageBase + pSectionHdr->VirtualAddress + pSectionHdr->Misc.VirtualSize - 1,
         scnName,
         pSectionHdr->Misc.VirtualSize);
      ++pSectionHdr;
   }
}

// For demo purpopses, create an extra constant data section whose name is exactly 8 bytes long (the max)
#pragma const_seg(".t_const") // begin allocating const data in a new section whose name is 8 bytes long (the max)
const char const_string1[] = "This string is allocated in a special const data segment named \".t_const\".";
#pragma const_seg() // resume allocating const data in the normal .rdata section

int main(int argc, const char* argv[])
{
   print_PE_section_info(GetModuleHandle(NULL)); // print section info for "this process's .exe file" (NULL)
}

如果您对 DbgHelp 库的其他用途感兴趣,此页面可能会有所帮助。

您可以在此处阅读 PE 图像格式,以了解详细信息。一旦你理解了 PE 格式,你就可以使用上面的代码,甚至可以修改它以满足你的需要。

  • 体育格式

窥视 PE:Win32 可移植可执行文件格式之旅

深入了解 Win32 可移植可执行文件格式,第 1 部分

深入了解 Win32 可移植可执行文件格式,第 2 部分

  • Windows API 和结构

IMAGE_SECTION_HEADER 结构

ImageNtHeader 函数

IMAGE_NT_HEADERS 结构

我认为这将在很大程度上帮助您,其余的您可以自己研究:-)

顺便说一句,您还可以看到这个线程,因为所有这些都与此相关:

场景:多线程应用程序使用的 DLL 中的全局变量

于 2010-11-30T17:50:48.827 回答
21

linux(和其他unix)的文本(程序代码)和数据的界限:

#include <stdio.h>
#include <stdlib.h>

/* these are in no header file, and on some
systems they have a _ prepended 
These symbols have to be typed to keep the compiler happy
Also check out brk() and sbrk() for information
about heap */

extern char  etext, edata, end; 

int
main(int argc, char **argv)
{
    printf("First address beyond:\n");
    printf("    program text segment(etext)      %10p\n", &etext);
    printf("    initialized data segment(edata)  %10p\n", &edata);
    printf("    uninitialized data segment (end) %10p\n", &end);

    return EXIT_SUCCESS;
}

这些符号来自哪里:符号 etext ,edata 和 end 定义在哪里?

于 2010-11-30T00:56:19.547 回答
1

由于您可能必须使垃圾收集器成为程序运行的环境,因此您可以直接从 elf 文件中获取它。

于 2010-11-29T23:10:37.217 回答
0

加载可执行文件来自的文件并解析 PE 标头,用于 Win32。我不知道在其他操作系统上。请记住,如果您的程序包含多个文件(例如 DLL),您可能有多个数据段。

于 2010-11-29T23:09:40.810 回答
0

对于 iOS,您可以使用此解决方案。它显示了如何查找文本段范围,但您可以轻松更改它以找到您喜欢的任何段。

于 2014-08-24T11:20:23.833 回答