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); 
       }
};

以下は、Visual Studio で実行できる完全で最小限の WIN32 コンソール プログラムで、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 フォーマット

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

実行可能ファイルの元のファイルをロードし、Win32 の PE ヘッダーを解析します。他のOSについてはわかりません。プログラムが複数のファイル (DLL など) で構成されている場合は、複数のデータ セグメントが存在する可能性があることに注意してください。

于 2010-11-29T23:09:40.810 に答える
0

iOS の場合、このソリューションを使用できます。テキストセグメントの範囲を見つける方法を示していますが、好きなセグメントを見つけるために簡単に変更できます.

于 2014-08-24T11:20:23.833 に答える