-2

My teacher asked me to do this task on a Windows system. Could you please help me to find some data segment and code segment address range via program?

Balder
  • 8,623
  • 4
  • 39
  • 61
  • There is no such segment(code) ..Its known as text segment where program instruction gets stored. – Mantosh Kumar Mar 23 '14 at 07:28
  • Check out this question which has good information regarding this. http://stackoverflow.com/questions/7873579/why-is-a-processs-address-space-divided-into-four-segments-text-data-stack-a – Mantosh Kumar Mar 23 '14 at 07:31
  • http://stackoverflow.com/questions/4308996/finding-the-address-range-of-the-data-segment – tesseract Mar 23 '14 at 07:58

1 Answers1

1

These are Windows API that would help you.

//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++;
}

Define ImageSectionInfo as,

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

Read Nawaz's answer.

Community
  • 1
  • 1
Sahil Sareen
  • 1,813
  • 3
  • 25
  • 40