Опубликован: 28.06.2006 | Уровень: специалист | Доступ: платный | ВУЗ: Московский государственный технический университет им. Н.Э. Баумана
Дополнительный материал 1:

Приложение A

< Лекция 15 || Дополнительный материал 1: 1234 || Дополнительный материал 2 >

A.2. pe.h

#ifndef PE_H

#include <stdio.h>

// Block of input parameters
struct INPUT_PARAMETERS {
  unsigned long      Type;    
  unsigned char      *metadata;
  unsigned char      *cilcode;

  unsigned long      SizeOfMetadata;
  unsigned long      SizeOfCilCode;

  unsigned long      ImageBase;
  unsigned long      FileAlignment;
  unsigned long      EntryPointToken;
  unsigned short     Subsystem;
};
typedef struct INPUT_PARAMETERS *PINPUT_PARAMETERS;

void  make_file      (FILE *file, PINPUT_PARAMETERS inP);

// Struct IMAGE_DATA_DIRECTORY
struct IMAGE_DATA_DIRECTORY {
  unsigned long   RVA;
  unsigned long   Size;
};

typedef struct IMAGE_DATA_DIRECTORY *PIMAGE_DATA_DIRECTORY;

struct IMAGE_SECTION_HEADER {
  unsigned char  Name[8];
  unsigned long  VirtualSize;
  unsigned long  VirtualAddress;
  unsigned long  SizeOfRawData;
  unsigned long  PointerToRawData;
  unsigned long  PointerToRelocations;
  unsigned long  PointerToLinenumbers;
  unsigned short NumberOfRelocations;
  unsigned short NumberOfLinenumbers;
  unsigned long  Characteristics;
};

struct HEADERS {

  char ms_dos_header[128];          // MS-DOS header
  unsigned long  signature;         // PE signature

  struct _IMAGE_FILE_HEADER {       // PE header
    unsigned short  Machine;
    unsigned short  NumberOfSections;
    unsigned long   TimeDateStamp;
    unsigned long   PointerToSymbolTable;
    unsigned long   NumberOfSymbols;
    unsigned short  OptionalHeaderSize;
    unsigned short  Characteristics;
  }PeHdr;

  struct _IMAGE_OPTIONAL_HEADER {      // optional PE header
    unsigned short  Magic;
    unsigned char   LMajor;
    unsigned char   LMinor;
    unsigned long   CodeSize;
    unsigned long   SizeOfInitializedData;
    unsigned long   SizeOfUninitializedData;
    unsigned long   EntryPointRVA;
    unsigned long   BaseOfCode;
    unsigned long   BaseOfData;
    unsigned long   ImageBase;
    unsigned long   SectionAlignment;
    unsigned long   FileAlignment;
    unsigned short  OSMajor;
    unsigned short  OSMinor;
    unsigned short  UserMajor;
    unsigned short  UserMinor;
    unsigned short  SubsysMajor;
    unsigned short  SubsysMinor;
    unsigned long   Reserved;
    unsigned long   ImageSize;
    unsigned long   HeaderSize;
    unsigned long   FileCheckSum;
    unsigned short  Subsystem;
    unsigned short  DllFlags;
    unsigned long   StackReserveSize;
    unsigned long   StackCommitSize;
    unsigned long   HeapReserveSize;
    unsigned long   HeapCommitSize;
    unsigned long   LoaderFlags;
    unsigned long   NumberOfDataDirectories;
  }OptHdr;

struct IMAGE_DATA_DIRECTORY STUB1;     
struct IMAGE_DATA_DIRECTORY IMPORT_DIRECTORY; // import directory
struct IMAGE_DATA_DIRECTORY STUB2[3];
struct IMAGE_DATA_DIRECTORY BASE_RELOC_DIRECTORY;
struct IMAGE_DATA_DIRECTORY STUB3[6];
struct IMAGE_DATA_DIRECTORY IAT_DIRECTORY;   // IAT directory
struct IMAGE_DATA_DIRECTORY STUB4;
struct IMAGE_DATA_DIRECTORY CLI_DIRECTORY;   // CLI directory
struct IMAGE_DATA_DIRECTORY STUB5;

struct IMAGE_SECTION_HEADER TEXT_SECTION; // .text section header
struct IMAGE_SECTION_HEADER CLI_SECTION;  // .cli section header
struct IMAGE_SECTION_HEADER RELOC_SECTION; // .reloc section header

};

typedef struct HEADERS *PHEADERS;

// .CLI Section
struct CLI_SECTION_IMAGE {
  
  struct _JMP_STUB {               // entry point
    unsigned short     JmpInstruction;
    unsigned long      JmpAddress;
  }JMP_STUB;

  struct _CLI_HEADER {              // CLI header
    unsigned long      cb;       
    unsigned short     MajorRuntimeVersion;
    unsigned short     MinorRuntimeVersion;
    struct IMAGE_DATA_DIRECTORY  MetaData;    
    unsigned long      Flags;      
    unsigned long      EntryPointToken;
    struct IMAGE_DATA_DIRECTORY  NotUsed[6];
  }CLI_HEADER;

  struct _IMPORT_TABLE {             // Import table

    /* Import Address Table */
    unsigned long      HintNameTableRVA2;
    unsigned long      zero2;
    /* Import Directory Entry */
    unsigned long      ImportLookupTableRVA;
    unsigned long      TimeDateStamp;
    unsigned long      ForwarderChain;
    unsigned long      NameRVA;
    unsigned long      ImportAddressTableRVA;
    unsigned char      zero[20];
  
    /* Import Lookup Table */
    unsigned long      HintNameTableRVA1;
    unsigned long      zero1;

    /* Hint/Name Table */
    unsigned short     Hint;
    char          Name[12];

    /* Dll name ("mscoree.dll") */
    char          DllName[12];
  }IMPORT_TABLE;

};

//.reloc Section
struct RELOC_SECTION
{
  unsigned long        PageRVA;
  unsigned long        BlockSize;
  unsigned short       TypeOffset;
  unsigned short       Padding;      
};

struct IMAGE_COR20_HEADER
{
  unsigned long      cb;       
  unsigned short     MajorRuntimeVersion;
  unsigned short     MinorRuntimeVersion;
  struct IMAGE_DATA_DIRECTORY  MetaData;    
  unsigned long      Flags;      
  unsigned long      EntryPointToken;
  struct IMAGE_DATA_DIRECTORY  Resources;
  struct IMAGE_DATA_DIRECTORY  StrongNameSignature;
  struct IMAGE_DATA_DIRECTORY  CodeManagerTable;
  struct IMAGE_DATA_DIRECTORY  VTableFixups;
  struct IMAGE_DATA_DIRECTORY  ExportAddressTableJumps;
  struct IMAGE_DATA_DIRECTORY  ManagedNativeHeader;
};

#endif
< Лекция 15 || Дополнительный материал 1: 1234 || Дополнительный материал 2 >
Анастасия Булинкова
Анастасия Булинкова
Рабочим названием платформы .NET было
Bogdan Drumov
Bogdan Drumov
Молдова, Республика
Azamat Nurmanbetov
Azamat Nurmanbetov
Киргизия, Bishkek