mirror of
https://github.com/reactos/reactos.git
synced 2025-02-23 17:05:46 +00:00
- PSEH changes imported in the main tree
- initial (dead, disabled) import of ELF support svn path=/trunk/; revision=12398
This commit is contained in:
parent
269d17ca59
commit
395ae7d908
20 changed files with 5675 additions and 625 deletions
|
@ -1,488 +1,20 @@
|
|||
#ifndef __INCLUDE_ELF_H
|
||||
#define __INCLUDE_ELF_H
|
||||
#ifndef _REACTOS_ELF_H_
|
||||
#define _REACTOS_ELF_H_ 1
|
||||
|
||||
|
||||
#ifdef __64BITS__ /* FIXME: how to check for 64 bits? */
|
||||
# define ELF_ARCH_SIZE 64
|
||||
#else
|
||||
# define ELF_ARCH_SIZE 32
|
||||
#endif
|
||||
|
||||
|
||||
/* 32-bit data types */
|
||||
typedef unsigned long ELF32_ADDR; /* Unsigned program address */
|
||||
typedef unsigned short ELF32_HALF; /* Unsigned medium integer */
|
||||
typedef unsigned long ELF32_OFF; /* Unsigned file offset */
|
||||
typedef unsigned long ELF32_SWORD; /* Signed large integer */
|
||||
typedef unsigned long ELF32_WORD; /* Unsigned large integer */
|
||||
typedef ELF32_OFF ELF32_HASHELT; /* Hash element? */
|
||||
|
||||
/* Elf data encodings */
|
||||
#define IMAGE_ELF_DATA_NONE 0 /* Invalid data encoding */
|
||||
#define IMAGE_ELF_DATA_2LSB 1 /* 2's complement, LSB first */
|
||||
#define IMAGE_ELF_DATA_2MSB 2 /* 2's complement, MSB first */
|
||||
|
||||
/* Elf object file types */
|
||||
#define IMAGE_ELF_TYPE_NONE 0 /* No file type */
|
||||
#define IMAGE_ELF_TYPE_REL 1 /* Relocatable file */
|
||||
#define IMAGE_ELF_TYPE_EXEC 2 /* Executable file */
|
||||
#define IMAGE_ELF_TYPE_DYN 3 /* Shared object file */
|
||||
#define IMAGE_ELF_TYPE_CORE 4 /* Core file */
|
||||
|
||||
/* Elf machines */
|
||||
#define IMAGE_ELF_MACHINE_NONE 0 /* No machine */
|
||||
#define IMAGE_ELF_MACHINE_M32 1 /* AT&T WE 32100 */
|
||||
#define IMAGE_ELF_MACHINE_SPARC 2 /* SPARC */
|
||||
#define IMAGE_ELF_MACHINE_386 3 /* Intel 80386 */
|
||||
#define IMAGE_ELF_MACHINE_68K 4 /* Motorola 68000 */
|
||||
#define IMAGE_ELF_MACHINE_88K 5 /* Motorola 88000 */
|
||||
/* 6 is reserved */
|
||||
#define IMAGE_ELF_MACHINE_860 7 /* Intel 80860 */
|
||||
#define IMAGE_ELF_MACHINE_MIPS 8 /* MIPS RS3000 (MIPS I) */
|
||||
#define IMAGE_ELF_MACHINE_S370 9 /* IBM System/370 Processor */
|
||||
#define IMAGE_ELF_MACHINE_MIPS_RS3_LE 10 /* MIPS RS3000 Little-endian */
|
||||
/* 11-14 are reserved */
|
||||
#define IMAGE_ELF_MACHINE_PARISC 15 /* Hewlett-Packard PA-RISC */
|
||||
/* 16 is reserved */
|
||||
#define IMAGE_ELF_MACHINE_VPP500 17 /* Fujitsu VPP500 */
|
||||
#define IMAGE_ELF_MACHINE_SPARC32PLUS 18 /* Enhanced instruction set SPARC */
|
||||
#define IMAGE_ELF_MACHINE_960 19 /* Intel 80960 */
|
||||
#define IMAGE_ELF_MACHINE_PPC 20 /* PowerPC */
|
||||
#define IMAGE_ELF_MACHINE_PPC64 21 /* 64-bit PowerPC */
|
||||
/* 22-35 are reserved */
|
||||
#define IMAGE_ELF_MACHINE_V800 36 /* NEC V800 */
|
||||
#define IMAGE_ELF_MACHINE_FR20 37 /* Fujitsu FR20 */
|
||||
#define IMAGE_ELF_MACHINE_RH32 38 /* TRW RH-32 */
|
||||
#define IMAGE_ELF_MACHINE_RCE 39 /* Motorola RCE */
|
||||
#define IMAGE_ELF_MACHINE_ARM 40 /* Advanced RISC Machines ARM */
|
||||
#define IMAGE_ELF_MACHINE_ALPHA 41 /* Digital Alpha */
|
||||
#define IMAGE_ELF_MACHINE_SH 42 /* Hitachi SH */
|
||||
#define IMAGE_ELF_MACHINE_SPARCV9 43 /* SPARC Version 9 */
|
||||
#define IMAGE_ELF_MACHINE_TRICORE 44 /* Siemens Tricore embedded processor */
|
||||
#define IMAGE_ELF_MACHINE_ARC 45 /* Argonaut RISC Core, Argonaut Technologies Inc. */
|
||||
#define IMAGE_ELF_MACHINE_H8_300 46 /* Hitachi H8/300 */
|
||||
#define IMAGE_ELF_MACHINE_H8_300H 47 /* Hitachi H8/300H */
|
||||
#define IMAGE_ELF_MACHINE_H8S 48 /* Hitachi H8S */
|
||||
#define IMAGE_ELF_MACHINE_H8_500 49 /* Hitachi H8/500 */
|
||||
#define IMAGE_ELF_MACHINE_IA_64 50 /* Intel IA-64 processor architecture */
|
||||
#define IMAGE_ELF_MACHINE_MIPS_X 51 /* Stanford MIPS-X */
|
||||
#define IMAGE_ELF_MACHINE_COLDFIRE 52 /* Motorola ColdFire */
|
||||
#define IMAGE_ELF_MACHINE_68HC12 53 /* Motorola M68HC12 */
|
||||
#define IMAGE_ELF_MACHINE_MMA 54 /* Fujitsu MMA Multimedia Accelerator */
|
||||
#define IMAGE_ELF_MACHINE_PCP 55 /* Siemens PCP */
|
||||
#define IMAGE_ELF_MACHINE_NCPU 56 /* Sony nCPU embedded RISC processor */
|
||||
#define IMAGE_ELF_MACHINE_NDR1 57 /* Denso NDR1 microprocessor */
|
||||
#define IMAGE_ELF_MACHINE_STARCORE 58 /* Motorola Star*Core processor */
|
||||
#define IMAGE_ELF_MACHINE_ME16 59 /* Toyota ME16 processor */
|
||||
#define IMAGE_ELF_MACHINE_ST100 60 /* STMicroelectronics ST100 processor */
|
||||
#define IMAGE_ELF_MACHINE_TINYJ 61 /* Advanced Logic Corp. TinyJ embedded processor family */
|
||||
/* 62-65 are reserved */
|
||||
#define IMAGE_ELF_MACHINE_FX66 66 /* Siemens FX66 microcontroller */
|
||||
#define IMAGE_ELF_MACHINE_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 bit microcontroller */
|
||||
#define IMAGE_ELF_MACHINE_ST7 68 /* STMicroelectronics ST7 8-bit microcontroller */
|
||||
#define IMAGE_ELF_MACHINE_68HC16 69 /* Motorola MC68HC16 Microcontroller */
|
||||
#define IMAGE_ELF_MACHINE_68HC11 70 /* Motorola MC68HC11 Microcontroller */
|
||||
#define IMAGE_ELF_MACHINE_68HC08 71 /* Motorola MC68HC08 Microcontroller */
|
||||
#define IMAGE_ELF_MACHINE_68HC05 72 /* Motorola MC68HC05 Microcontroller */
|
||||
#define IMAGE_ELF_MACHINE_SVX 73 /* Silicon Graphics SVx */
|
||||
#define IMAGE_ELF_MACHINE_ST19 74 /* STMicroelectronics ST19 8-bit microcontroller */
|
||||
#define IMAGE_ELF_MACHINE_VAX 75 /* Digital VAX */
|
||||
#define IMAGE_ELF_MACHINE_CRIS 76 /* Axis Communications 32-bit embedded processor */
|
||||
#define IMAGE_ELF_MACHINE_JAVELIN 77 /* Infineon Technologies 32-bit embedded processor */
|
||||
#define IMAGE_ELF_MACHINE_FIREPATH 78 /* Element 14 64-bit DSP Processor */
|
||||
#define IMAGE_ELF_MACHINE_ZSP 79 /* LSI Logic 16-bit DSP Processor */
|
||||
#define IMAGE_ELF_MACHINE_MMIX 80 /* Donald Knuth's educational 64-bit processor */
|
||||
#define IMAGE_ELF_MACHINE_HUANY 81 /* Harvard University machine-independent object files */
|
||||
#define IMAGE_ELF_MACHINE_PRISM 82 /* SiTera Prism */
|
||||
#define IMAGE_ELF_MACHINE_AVR 83 /* Atmel AVR 8-bit microcontroller */
|
||||
#define IMAGE_ELF_MACHINE_FR30 84 /* Fujitsu FR30 */
|
||||
#define IMAGE_ELF_MACHINE_D10V 85 /* Mitsubishi D10V */
|
||||
#define IMAGE_ELF_MACHINE_D30V 86 /* Mitsubishi D30V */
|
||||
#define IMAGE_ELF_MACHINE_V850 87 /* NEC v850 */
|
||||
#define IMAGE_ELF_MACHINE_M32R 88 /* Mitsubishi M32R */
|
||||
#define IMAGE_ELF_MACHINE_MN10300 89 /* Matsushita MN10300 */
|
||||
#define IMAGE_ELF_MACHINE_MN10200 90 /* Matsushita MN10200 */
|
||||
#define IMAGE_ELF_MACHINE_PJ 91 /* picoJava */
|
||||
#define IMAGE_ELF_MACHINE_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
|
||||
|
||||
/* Elf versions */
|
||||
#define IMAGE_ELF_VERSION_NONE 0 /* Invalid version */
|
||||
#define IMAGE_ELF_VERSION_CURRENT 1 /* Current version */
|
||||
|
||||
/* Elf identification */
|
||||
#define IMAGE_ELF_SIZEOF_IDENT 16
|
||||
#define IMAGE_ELF_IDENT_MAGIC0 0 /* Magic */
|
||||
#define IMAGE_ELF_IDENT_MAGIC1 1
|
||||
#define IMAGE_ELF_IDENT_MAGIC2 2
|
||||
#define IMAGE_ELF_IDENT_MAGIC3 3
|
||||
#define IMAGE_ELF_IDENT_CLASS 4 /* File class */
|
||||
#define IMAGE_ELF_IDENT_DATA 5 /* Data encoding */
|
||||
#define IMAGE_ELF_IDENT_VERSION 6 /* File version */
|
||||
#define IMAGE_ELF_IDENT_OSABI 7 /* Operating system/ABI identification */
|
||||
#define IMAGE_ELF_IDENT_ABIVERSION 8 /* ABI version */
|
||||
#define IMAGE_ELF_IDENT_PAD 9 /* Start of padding bytes */
|
||||
|
||||
/* Magic numbers */
|
||||
#define IMAGE_ELF_MAGIC0 0x7f
|
||||
#define IMAGE_ELF_MAGIC1 'E'
|
||||
#define IMAGE_ELF_MAGIC2 'L'
|
||||
#define IMAGE_ELF_MAGIC3 'F'
|
||||
|
||||
/* Elf file classes */
|
||||
#define IMAGE_ELF_CLASS_NONE 0 /* Invalid class */
|
||||
#define IMAGE_ELF_CLASS_32 1 /* 32-bit object */
|
||||
#define IMAGE_ELF_CLASS_64 2 /* 64-bit object */
|
||||
|
||||
/* Check elf magic */
|
||||
#define IMAGE_IS_ELF(hdr) ((hdr).Ident[IMAGE_ELF_IDENT_MAGIC0] == IMAGE_ELF_MAGIC0 && \
|
||||
(hdr).Ident[IMAGE_ELF_IDENT_MAGIC1] == IMAGE_ELF_MAGIC1 && \
|
||||
(hdr).Ident[IMAGE_ELF_IDENT_MAGIC2] == IMAGE_ELF_MAGIC2 && \
|
||||
(hdr).Ident[IMAGE_ELF_IDENT_MAGIC3] == IMAGE_ELF_MAGIC3)
|
||||
|
||||
|
||||
|
||||
/* 32-bit Elf header */
|
||||
typedef struct _IMAGE_ELF32_HEADER {
|
||||
unsigned char Ident[IMAGE_ELF_SIZEOF_IDENT]; /* Identification */
|
||||
ELF32_HALF Type; /* Object file type */
|
||||
ELF32_HALF Machine; /* Required architecture */
|
||||
ELF32_WORD Version; /* Object file version */
|
||||
ELF32_ADDR Entry; /* Virtual address of entry point */
|
||||
ELF32_OFF PhOff; /* Program header table offset in file */
|
||||
ELF32_OFF ShOff; /* Section header offset in file */
|
||||
ELF32_WORD Flags; /* Processor specific flags - zero for SPARC and x86 */
|
||||
ELF32_HALF EhSize; /* Elf header size in bytes (this struct) */
|
||||
ELF32_HALF PhEntSize; /* Size of an entry in the program header table */
|
||||
ELF32_HALF PhNum; /* Number of entries in the program header table */
|
||||
ELF32_HALF ShEntSize; /* Size of an entry in the section header table */
|
||||
ELF32_HALF ShNum; /* Number of entries in the section header table */
|
||||
ELF32_HALF ShStrNdx; /* Index into the section header table for the entry of the section name string table */
|
||||
} IMAGE_ELF32_HEADER, *PIMAGE_ELF32_HEADER;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Special section indexes */
|
||||
#define IMAGE_ELF_SECTION_INDEX_UNDEF 0
|
||||
/*#define IMAGE_ELF_SECTION_INDEX_LORESERVE 0xff00
|
||||
#define IMAGE_ELF_SECTION_INDEX_LOPROC 0xff00
|
||||
#define IMAGE_ELF_SECTION_INDEX_HIPROC 0xff1f*/
|
||||
#define IMAGE_ELF_SECTION_INDEX_ABS 0xfff1
|
||||
#define IMAGE_ELF_SECTION_INDEX_COMMON 0xfff2
|
||||
/*#define IMAGE_ELF_SECTION_INDEX_HIRESERVE 0xffff*/
|
||||
|
||||
/* Section types */
|
||||
#define IMAGE_ELF_SECTION_TYPE_NULL 0 /* Incactive section header */
|
||||
#define IMAGE_ELF_SECTION_TYPE_PROGBITS 1 /* Program defined section */
|
||||
#define IMAGE_ELF_SECTION_TYPE_SYMTAB 2 /* Symbol table (for link editing) */
|
||||
#define IMAGE_ELF_SECTION_TYPE_STRTAB 3 /* String table */
|
||||
#define IMAGE_ELF_SECTION_TYPE_RELA 4 /* Relocation table (with explicit addends) */
|
||||
#define IMAGE_ELF_SECTION_TYPE_HASH 5 /* Symbol hash table */
|
||||
#define IMAGE_ELF_SECTION_TYPE_DYNAMIC 6 /* Information for dynamic linking */
|
||||
#define IMAGE_ELF_SECTION_TYPE_NOTE 7 /* Note section ;-) */
|
||||
#define IMAGE_ELF_SECTION_TYPE_NOBITS 8 /* Occupies no space in the file, otherwise like PROGBITS */
|
||||
#define IMAGE_ELF_SECTION_TYPE_REL 9 /* Relocation table (without explicit addends) */
|
||||
#define IMAGE_ELF_SECTION_TYPE_SHLIB 10 /* Reserved, unspecified */
|
||||
#define IMAGE_ELF_SECTION_TYPE_DYNSYM 11 /* Symbol table (for dynamic linking) */
|
||||
/*#define IMAGE_ELF_SECTION_TYPE_LOPROC 0x70000000
|
||||
#define IMAGE_ELF_SECTION_TYPE_HIPROC 0x7fffffff
|
||||
#define IMAGE_ELF_SECTION_TYPE_LOUSER 0x80000000
|
||||
#define IMAGE_ELF_SECTION_TYPE_HIUSER 0xffffffff*/
|
||||
|
||||
/* Section flags/attributes */
|
||||
#define SHF_WRITE 0x1 /* Section must be writeable */
|
||||
#define SHF_ALLOC 0x2 /* Section must be loaded/mapped into memory */
|
||||
#define SHF_EXECINSTR 0x4 /* The section contains executable code */
|
||||
/*#define SHF_MASKPROC 0xf0000000*/
|
||||
|
||||
/* 32-bit Section header entry */
|
||||
typedef struct _IMAGE_ELF32_SECTION_HEADER {
|
||||
ELF32_WORD Name; /* Name of the section (index into the section header string table) */
|
||||
ELF32_WORD Type; /* Type of section */
|
||||
ELF32_WORD Flags; /* Attributes */
|
||||
ELF32_ADDR Addr; /* Virtual address to load section at */
|
||||
ELF32_OFF Offset; /* Offset into the file of the section's data */
|
||||
ELF32_WORD Size; /* Size of the section */
|
||||
ELF32_WORD Link; /* Section header table index link... */
|
||||
ELF32_WORD Info; /* Extra information... */
|
||||
ELF32_WORD AddrAlign; /* Required alignment */
|
||||
ELF32_WORD EntSize; /* Size of entries in the table */
|
||||
} IMAGE_ELF32_SECTION_HEADER, *PIMAGE_ELF32_SECTION_HEADER;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Symbol table indexes */
|
||||
#define IMAGE_ELF_SYMBOL_INDEX_UNDEF 0 /* Undefined symbol */
|
||||
|
||||
/* Symbol binding/types */
|
||||
#define IMAGE_ELF32_SYMBOL_BIND(Info) ((Info) >> 4)
|
||||
#define IMAGE_ELF32_SYMBOL_TYPE(Info) ((Info) & 0x0f)
|
||||
#define IMAGE_ELF32_SYMBOL_INFO(Bind, Type) (((Bind) << 4) | ((Type) & 0x0f))
|
||||
|
||||
#define IMAGE_ELF_SYMBOL_BINDING_LOCAL 0 /* Local ("static") symbol */
|
||||
#define IMAGE_ELF_SYMBOL_BINDING_GLOBAL 1 /* Global symbol */
|
||||
#define IMAGE_ELF_SYMBOL_BINDING_WEAK 2 /* Weak symbol... */
|
||||
/*#define IMAGE_ELF_SYMBOL_BINDING_LOPROC 13
|
||||
#define IMAGE_ELF_SYMBOL_BINDING_HIPROC 15*/
|
||||
|
||||
#define IMAGE_ELF_SYMBOL_TYPE_NOTYPE 0 /* Unspecified symbol type */
|
||||
#define IMAGE_ELF_SYMBOL_TYPE_OBJECT 1 /* Data object (i.e. an array, variable, ...) */
|
||||
#define IMAGE_ELF_SYMBOL_TYPE_FUNC 2 /* Function (or other executable code) */
|
||||
#define IMAGE_ELF_SYMBOL_TYPE_SECTION 3 /* Symbol for relocating (usually has local binding) */
|
||||
#define IMAGE_ELF_SYMBOL_TYPE_FILE 4 /* Name of the associated source file */
|
||||
#define IMAGE_ELF_SYMBOL_TYPE_LOPROC 13
|
||||
#define IMAGE_ELF_SYMBOL_TYPE_HIPROC 15
|
||||
|
||||
/* 32-bit Symbol entry */
|
||||
typedef struct _IMAGE_ELF32_SYMBOL {
|
||||
ELF32_WORD Name; /* Symbol name (index into the symbol string table) */
|
||||
ELF32_ADDR Value; /* Value of symbol */
|
||||
ELF32_WORD Size; /* Size of symbol (0 means unknown) */
|
||||
unsigned char Info; /* Type and binding attributes */
|
||||
unsigned char Other; /* Unused - 0 */
|
||||
ELF32_HALF Shndx; /* Section index */
|
||||
} IMAGE_ELF32_SYMBOL, *PIMAGE_ELF32_SYMBOL;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Relocation macros */
|
||||
#define IMAGE_ELF32_RELOC_SYM(Info) ((Info) >> 8)
|
||||
#define IMAGE_ELF32_RELOC_TYPE(Info) ((unsigned char)(Info))
|
||||
#define IMAGE_ELF32_RELOC_INFO(Sym,Type) (((Sym) << 8) | (unsigned char)(Type))
|
||||
|
||||
/* 386 Relocation types */
|
||||
#define IMAGE_ELF_RELOC_386_NONE 0 /* none */
|
||||
#define IMAGE_ELF_RELOC_386_32 1
|
||||
#define IMAGE_ELF_RELOC_386_PC32 2
|
||||
#define IMAGE_ELF_RELOC_386_GOT32 3
|
||||
#define IMAGE_ELF_RELOC_386_PLT32 4
|
||||
#define IMAGE_ELF_RELOC_386_COPY 5
|
||||
#define IMAGE_ELF_RELOC_386_GLOB_DAT 6
|
||||
#define IMAGE_ELF_RELOC_386_JMP_SLOT 7
|
||||
#define IMAGE_ELF_RELOC_386_RELATIVE 8
|
||||
#define IMAGE_ELF_RELOC_386_GOTOFF 9
|
||||
#define IMAGE_ELF_RELOC_386_GOTPC 10
|
||||
|
||||
/* 386 TLS Relocation types */
|
||||
#define IMAGE_ELF_RELOC_386_TLS_GD_PLT 12
|
||||
#define IMAGE_ELF_RELOC_386_TLS_LDM_PLT 13
|
||||
#define IMAGE_ELF_RELOC_386_TLS_TPOFF 14
|
||||
#define IMAGE_ELF_RELOC_386_TLS_IE 15
|
||||
#define IMAGE_ELF_RELOC_386_TLS_GOTIE 16
|
||||
#define IMAGE_ELF_RELOC_386_TLS_LE 17
|
||||
#define IMAGE_ELF_RELOC_386_TLS_GD 18
|
||||
#define IMAGE_ELF_RELOC_386_TLS_LDM 19
|
||||
#define IMAGE_ELF_RELOC_386_TLS_LDO_32 32
|
||||
#define IMAGE_ELF_RELOC_386_TLS_DTPMOD32 35
|
||||
#define IMAGE_ELF_RELOC_386_TLS_DTPOFF32 36
|
||||
|
||||
/* 32-bit Relocation entries */
|
||||
typedef struct _IMAGE_ELF32_RELOC {
|
||||
ELF32_ADDR Offset; /* Section offset/virtual address */
|
||||
ELF32_WORD Info; /* Symbol table index/relocation type */
|
||||
} IMAGE_ELF32_RELOC, *PIMAGE_ELF32_RELOC;
|
||||
|
||||
typedef struct _IMAGE_ELF32_RELOCA {
|
||||
ELF32_ADDR Offset; /* Section offset/virtual address */
|
||||
ELF32_WORD Info; /* Symbol table index/relocation type */
|
||||
ELF32_SWORD Addend; /* Addend */
|
||||
} IMAGE_ELF32_RELOCA, *PIMAGE_ELF32_RELOCA;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Program header/segment types */
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_NULL 0 /* Unused array entry */
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_LOAD 1 /* Loadable segment */
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_DYNAMIC 2 /* Dynamic linking info... */
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_INTERP 3 /* Interpreter */
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_NOTE 4 /* Note... */
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_SHLIB 5 /* Reserved but unspecified */
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_PHDR 6 /* Program header table */
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_TLS 7 /* Thread local storage */
|
||||
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_GNU_STACK 0x6474e551 /* Indicates stack executability */
|
||||
/*#define IMAGE_ELF_SEGMENT_TYPE_LOPROC 0x70000000
|
||||
#define IMAGE_ELF_SEGMENT_TYPE_HIPROC 0x7fffffff*/
|
||||
|
||||
/* Program header/segment flags */
|
||||
#define IMAGE_ELF_SEGMENT_FLAG_EXEC 0x01
|
||||
#define IMAGE_ELF_SEGMENT_FLAG_WRITE 0x02
|
||||
#define IMAGE_ELF_SEGMENT_FLAG_READ 0x04
|
||||
/*#define IMAGE_ELF_SEGMENT_FLAG_MASKPROC 0xf0000000*/
|
||||
|
||||
/* 32-bit Program header entry */
|
||||
typedef struct _IMAGE_ELF32_PROGRAM_HEADER {
|
||||
ELF32_WORD Type; /* Type of segment */
|
||||
ELF32_OFF Offset; /* File offset of segment data */
|
||||
ELF32_ADDR VAddr; /* Virtual address to load segment at */
|
||||
ELF32_ADDR PAddr; /* Physical address to load segment at */
|
||||
ELF32_WORD FileSz; /* Size in file of segment */
|
||||
ELF32_WORD MemSz; /* Size in memory of segment */
|
||||
ELF32_WORD Flags; /* Flags of segment */
|
||||
ELF32_WORD Align; /* Required alignment */
|
||||
} IMAGE_ELF32_PROGRAM_HEADER, *PIMAGE_ELF32_PROGRAM_HEADER;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Dynamic array tags */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_NULL 0 /* End of array */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_NEEDED 1 /* Dependency */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_PLTRELSZ 2 /* Size in bytes of the relocation entries associated with the plt */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_PLTGOT 3 /* Address associated with got/plt */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_HASH 4 /* Hash table for symbol table indicated by SYMTAB */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_STRTAB 5 /* Address of string table */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_SYMTAB 6 /* Address of the symbol table */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_RELA 7 /* Address of reloc table with addends */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_RELASZ 8 /* Size of reloc table in bytes */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_RELAENT 9 /* Size of reloc table entry in bytes? */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_STRSZ 10 /* Size of the string table in bytes */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_SYMENT 11 /* Size of symbol table entry in bytes */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_INIT 12 /* Address of initialization function */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_FINI 13 /* Address of termination function */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_SONAME 14 /* Name of the shared object (string table offset) */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_RPATH 15 /* Library search path (string table offset) */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_SYMBOLIC 16 /* Alter runtime-linkers symbol resolution... */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_REL 17 /* Address of reloc table without addends */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_RELSZ 18 /* Size of reloc table in bytes */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_RELENT 19 /* Size of reloc table entry in bytes? */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_PLTREL 20 /* Type of relocation entry for plt (...TAG_REL or ...TAG_RELA) */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_DEBUG 21 /* Used for debugging */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_TEXTREL 22 /* If present informs the linker that a relocation might update a non-writable segment */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_JMPREL 23 /* Address of relocation entries associated solely with the plt */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_FILTER 24 /* Specifies the name of a shared objects for which this one acts as a filter */
|
||||
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_RUNPATH 29 /* String table offset of a null-terminated library search path string. */
|
||||
#define IMAGE_ELF_DYNAMIC_TAG_FLAGS 30 /* Object specific flag values. */
|
||||
|
||||
/* Dynamic flags (for IMAGE_ELF_DYNAMIC_TAG_FLAGS) */
|
||||
#define IMAGE_ELF_DYNAMIC_FLAG_ORIGIN 0x0001 /* Indicates that the object being loaded may make reference to the $ORIGIN substitution string.*/
|
||||
#define IMAGE_ELF_DYNAMIC_FLAG_SYMBOLIC 0x0002 /* Indicates "symbolic" linking. */
|
||||
#define IMAGE_ELF_DYNAMIC_FLAG_TEXTREL 0x0004 /* Indicates there may be relocations in non-writable segments. */
|
||||
#define IMAGE_ELF_DYNAMIC_FLAG_BIND_NOW 0x0008 /* Indicates that the dynamic linker should process all relocations for the object
|
||||
containing this entry before transferring control to the program. */
|
||||
#define IMAGE_ELF_DYNAMIC_FLAG_STATIC_TLS 0x0010 /* Indicates that the shared object or executable contains code using a static
|
||||
thread-local storage scheme. */
|
||||
|
||||
/* Dynamic array entry */
|
||||
typedef struct _IMAGE_ELF32_DYNAMIC {
|
||||
ELF32_SWORD Tag;
|
||||
union {
|
||||
ELF32_WORD Val;
|
||||
ELF32_ADDR Ptr;
|
||||
} Un;
|
||||
} IMAGE_ELF32_DYNAMIC, *PIMAGE_ELF32_DYNAMIC;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Auxiliary types */
|
||||
#define IMAGE_ELF_AUX_TYPE_NULL 0 /* Terminates the vector. */
|
||||
#define IMAGE_ELF_AUX_TYPE_IGNORE 1 /* Ignored entry. */
|
||||
#define IMAGE_ELF_AUX_TYPE_EXECFD 2 /* File descriptor of program to load. */
|
||||
#define IMAGE_ELF_AUX_TYPE_PHDR 3 /* Program header of program already loaded. */
|
||||
#define IMAGE_ELF_AUX_TYPE_PHENT 4 /* Size of each program header entry. */
|
||||
#define IMAGE_ELF_AUX_TYPE_PHNUM 5 /* Number of program header entries. */
|
||||
#define IMAGE_ELF_AUX_TYPE_PAGESZ 6 /* Page size in bytes. */
|
||||
#define IMAGE_ELF_AUX_TYPE_BASE 7 /* Interpreter's base address. */
|
||||
#define IMAGE_ELF_AUX_TYPE_FLAGS 8 /* Flags (unused for i386). */
|
||||
#define IMAGE_ELF_AUX_TYPE_ENTRY 9 /* Where interpreter should transfer control. */
|
||||
/* Machine-independent and word-size-independent definitions */
|
||||
#include <elf/common.h>
|
||||
|
||||
/*
|
||||
* The following non-standard values are used for passing information
|
||||
* from John Polstra's testbed program to the dynamic linker. These
|
||||
* are expected to go away soon.
|
||||
*
|
||||
* Unfortunately, these overlap the Linux non-standard values, so they
|
||||
* must not be used in the same context.
|
||||
* Word-size-dependent definitions. All ReactOS builds support all of them,
|
||||
* even if (obviously) code for the wrong architecture cannot be executed - the
|
||||
* files can still be used in machine-independent ways, e.g. as resource DLLs
|
||||
*/
|
||||
#define IMAGE_ELF_AUX_TYPE_BRK 10 /* Starting point for sbrk and brk. */
|
||||
#define IMAGE_ELF_AUX_TYPE_DEBUG 11 /* Debugging level. */
|
||||
#include <elf/elf32.h>
|
||||
#include <elf/elf64.h>
|
||||
|
||||
/*
|
||||
* The following non-standard values are used in Linux ELF binaries.
|
||||
*/
|
||||
#define IMAGE_ELF_AUX_TYPE_NOTELF 10 /* Program is not ELF ?? */
|
||||
#define IMAGE_ELF_AUX_TYPE_UID 11 /* Real uid. */
|
||||
#define IMAGE_ELF_AUX_TYPE_EUID 12 /* Effective uid. */
|
||||
#define IMAGE_ELF_AUX_TYPE_GID 13 /* Real gid. */
|
||||
#define IMAGE_ELF_AUX_TYPE_EGID 14 /* Effective gid. */
|
||||
/* Machine-dependent definitions */
|
||||
#include <elf/machine.h>
|
||||
|
||||
#define IMAGE_ELF_AUX_TYPE_COUNT 15 /* Count of defined aux entry types. */
|
||||
|
||||
|
||||
/* Auxiliary vector entry on initial stack */
|
||||
typedef struct _IMAGE_ELF32_AUXINFO {
|
||||
ELF32_SWORD Type; /* Entry type. */
|
||||
union {
|
||||
ELF32_SWORD Val; /* Integer value. */
|
||||
ELF32_ADDR Ptr; /* Address. */
|
||||
void (*Fcn)(void); /* Function pointer (not used). */
|
||||
} Un;
|
||||
} IMAGE_ELF32_AUXINFO, *PIMAGE_ELF32_AUXINFO;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* arch data types */
|
||||
|
||||
#if ELF_ARCH_SIZE == 32
|
||||
|
||||
#define IMAGE_ELF_SYMBOL_BIND(args...) IMAGE_ELF32_SYMBOL_BIND(args)
|
||||
#define IMAGE_ELF_SYMBOL_TYPE(args...) IMAGE_ELF32_SYMBOL_TYPE(args)
|
||||
#define IMAGE_ELF_SYMBOL_INFO(args...) IMAGE_ELF32_SYMBOL_INFO(args)
|
||||
|
||||
#define IMAGE_ELF_RELOC_SYM(args...) IMAGE_ELF32_RELOC_SYM(args)
|
||||
#define IMAGE_ELF_RELOC_TYPE(args...) IMAGE_ELF32_RELOC_TYPE(args)
|
||||
#define IMAGE_ELF_RELOC_INFO(args...) IMAGE_ELF32_RELOC_INFO(args)
|
||||
|
||||
typedef ELF32_ADDR ELF_ADDR;
|
||||
typedef ELF32_HALF ELF_HALF;
|
||||
typedef ELF32_OFF ELF_OFF;
|
||||
typedef ELF32_SWORD ELF_SWORD;
|
||||
typedef ELF32_WORD ELF_WORD;
|
||||
typedef ELF32_HASHELT ELF_HASHELT;
|
||||
|
||||
typedef IMAGE_ELF32_HEADER IMAGE_ELF_HEADER, *PIMAGE_ELF_HEADER;
|
||||
typedef IMAGE_ELF32_SECTION_HEADER IMAGE_ELF_SECTION_HEADER, *PIMAGE_ELF_SECTION_HEADER;
|
||||
typedef IMAGE_ELF32_SYMBOL IMAGE_ELF_SYMBOL, *PIMAGE_ELF_SYMBOL;
|
||||
typedef IMAGE_ELF32_RELOC IMAGE_ELF_RELOC, *PIMAGE_ELF_RELOC;
|
||||
typedef IMAGE_ELF32_RELOCA IMAGE_ELF_RELOCA, *PIMAGE_ELF_RELOCA;
|
||||
typedef IMAGE_ELF32_PROGRAM_HEADER IMAGE_ELF_PROGRAM_HEADER, *PIMAGE_ELF_PROGRAM_HEADER;
|
||||
typedef IMAGE_ELF32_DYNAMIC IMAGE_ELF_DYNAMIC, *PIMAGE_ELF_DYNAMIC;
|
||||
typedef IMAGE_ELF32_AUXINFO IMAGE_ELF_AUXINFO, *PIMAGE_ELF_AUXINFO;
|
||||
|
||||
#elif ELF_ARCH_SIZE == 64
|
||||
# error 64 bits unsupported
|
||||
#else
|
||||
# error Undefined architecture size!
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
/* target macros */
|
||||
#ifdef _M_IX86
|
||||
# define IMAGE_ELF_TARGET_CLASS IMAGE_ELF_CLASS_32
|
||||
# define IMAGE_ELF_TARGET_DATA IMAGE_ELF_DATA_2LSB
|
||||
# define IMAGE_ELF_TARGET_MACHINE IMAGE_ELF_MACHINE_386
|
||||
# define IMAGE_ELF_TARGET_VERSION 1
|
||||
#else
|
||||
# error Unsupported architecture!
|
||||
#endif
|
||||
|
||||
#undef ELF_ARCH_SIZE
|
||||
|
||||
#endif /* __INCLUDE_ELF_H */
|
||||
|
||||
/* EOF */
|
||||
|
|
392
reactos/include/elf/common.h
Normal file
392
reactos/include/elf/common.h
Normal file
|
@ -0,0 +1,392 @@
|
|||
/*-
|
||||
* Copyright (c) 1998 John D. Polstra.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/sys/sys/elf_common.h,v 1.15 2004/05/05 02:38:54 marcel Exp $
|
||||
*/
|
||||
|
||||
#ifndef _SYS_ELF_COMMON_H_
|
||||
#define _SYS_ELF_COMMON_H_ 1
|
||||
|
||||
/*
|
||||
* ELF definitions that are independent of architecture or word size.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Note header. The ".note" section contains an array of notes. Each
|
||||
* begins with this header, aligned to a word boundary. Immediately
|
||||
* following the note header is n_namesz bytes of name, padded to the
|
||||
* next word boundary. Then comes n_descsz bytes of descriptor, again
|
||||
* padded to a word boundary. The values of n_namesz and n_descsz do
|
||||
* not include the padding.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
UINT32 n_namesz; /* Length of name. */
|
||||
UINT32 n_descsz; /* Length of descriptor. */
|
||||
UINT32 n_type; /* Type of this note. */
|
||||
} Elf_Note;
|
||||
|
||||
/* Indexes into the e_ident array. Keep synced with
|
||||
http://www.sco.com/developer/gabi/ch4.eheader.html */
|
||||
#define EI_MAG0 0 /* Magic number, byte 0. */
|
||||
#define EI_MAG1 1 /* Magic number, byte 1. */
|
||||
#define EI_MAG2 2 /* Magic number, byte 2. */
|
||||
#define EI_MAG3 3 /* Magic number, byte 3. */
|
||||
#define EI_CLASS 4 /* Class of machine. */
|
||||
#define EI_DATA 5 /* Data format. */
|
||||
#define EI_VERSION 6 /* ELF format version. */
|
||||
#define EI_OSABI 7 /* Operating system / ABI identification */
|
||||
#define EI_ABIVERSION 8 /* ABI version */
|
||||
#define OLD_EI_BRAND 8 /* Start of architecture identification. */
|
||||
#define EI_PAD 9 /* Start of padding (per SVR4 ABI). */
|
||||
#define EI_NIDENT 16 /* Size of e_ident array. */
|
||||
|
||||
/* Values for the magic number bytes. */
|
||||
#define ELFMAG0 0x7f
|
||||
#define ELFMAG1 'E'
|
||||
#define ELFMAG2 'L'
|
||||
#define ELFMAG3 'F'
|
||||
#define ELFMAG "\177ELF" /* magic string */
|
||||
#define SELFMAG 4 /* magic string size */
|
||||
|
||||
/* Values for e_ident[EI_VERSION] and e_version. */
|
||||
#define EV_NONE 0
|
||||
#define EV_CURRENT 1
|
||||
|
||||
/* Values for e_ident[EI_CLASS]. */
|
||||
#define ELFCLASSNONE 0 /* Unknown class. */
|
||||
#define ELFCLASS32 1 /* 32-bit architecture. */
|
||||
#define ELFCLASS64 2 /* 64-bit architecture. */
|
||||
|
||||
/* Values for e_ident[EI_DATA]. */
|
||||
#define ELFDATANONE 0 /* Unknown data format. */
|
||||
#define ELFDATA2LSB 1 /* 2's complement little-endian. */
|
||||
#define ELFDATA2MSB 2 /* 2's complement big-endian. */
|
||||
|
||||
/* Values for e_ident[EI_OSABI]. */
|
||||
#define ELFOSABI_SYSV 0 /* UNIX System V ABI */
|
||||
#define ELFOSABI_NONE ELFOSABI_SYSV /* symbol used in old spec */
|
||||
#define ELFOSABI_HPUX 1 /* HP-UX operating system */
|
||||
#define ELFOSABI_NETBSD 2 /* NetBSD */
|
||||
#define ELFOSABI_LINUX 3 /* GNU/Linux */
|
||||
#define ELFOSABI_HURD 4 /* GNU/Hurd */
|
||||
#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */
|
||||
#define ELFOSABI_SOLARIS 6 /* Solaris */
|
||||
#define ELFOSABI_MONTEREY 7 /* Monterey */
|
||||
#define ELFOSABI_IRIX 8 /* IRIX */
|
||||
#define ELFOSABI_FREEBSD 9 /* FreeBSD */
|
||||
#define ELFOSABI_TRU64 10 /* TRU64 UNIX */
|
||||
#define ELFOSABI_MODESTO 11 /* Novell Modesto */
|
||||
#define ELFOSABI_OPENBSD 12 /* OpenBSD */
|
||||
#define ELFOSABI_ARM 97 /* ARM */
|
||||
#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
|
||||
|
||||
/* e_ident */
|
||||
#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
|
||||
(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
|
||||
(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
|
||||
(ehdr).e_ident[EI_MAG3] == ELFMAG3)
|
||||
|
||||
/* Values for e_type. */
|
||||
#define ET_NONE 0 /* Unknown type. */
|
||||
#define ET_REL 1 /* Relocatable. */
|
||||
#define ET_EXEC 2 /* Executable. */
|
||||
#define ET_DYN 3 /* Shared object. */
|
||||
#define ET_CORE 4 /* Core file. */
|
||||
|
||||
/* Values for e_machine. */
|
||||
/*
|
||||
* Source:
|
||||
* System V Application Binary Interface (DRAFT) 2003-12-17, Chapter 4, Section
|
||||
* "ELF Header"
|
||||
*/
|
||||
#define EM_NONE 0 /* No machine */
|
||||
#define EM_M32 1 /* AT&T WE 32100 */
|
||||
#define EM_SPARC 2 /* SPARC */
|
||||
#define EM_386 3 /* Intel 80386 */
|
||||
#define EM_68K 4 /* Motorola 68000 */
|
||||
#define EM_88K 5 /* Motorola 88000 */
|
||||
/* reserved 6 Reserved for future use (was EM_486) */
|
||||
#define EM_860 7 /* Intel 80860 */
|
||||
#define EM_MIPS 8 /* MIPS I Architecture */
|
||||
#define EM_S370 9 /* IBM System/370 Processor */
|
||||
#define EM_MIPS_RS3_LE 10 /* MIPS RS3000 Little-endian */
|
||||
/* reserved 11-14 Reserved for future use */
|
||||
#define EM_PARISC 15 /* Hewlett-Packard PA-RISC */
|
||||
/* reserved 16 Reserved for future use */
|
||||
#define EM_VPP500 17 /* Fujitsu VPP500 */
|
||||
#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */
|
||||
#define EM_960 19 /* Intel 80960 */
|
||||
#define EM_PPC 20 /* PowerPC */
|
||||
#define EM_PPC64 21 /* 64-bit PowerPC */
|
||||
#define EM_S390 22 /* IBM System/390 Processor */
|
||||
/* reserved 23-35 Reserved for future use */
|
||||
#define EM_V800 36 /* NEC V800 */
|
||||
#define EM_FR20 37 /* Fujitsu FR20 */
|
||||
#define EM_RH32 38 /* TRW RH-32 */
|
||||
#define EM_RCE 39 /* Motorola RCE */
|
||||
#define EM_ARM 40 /* Advanced RISC Machines ARM */
|
||||
#define EM_ALPHA 41 /* Digital Alpha */
|
||||
#define EM_SH 42 /* Hitachi SH */
|
||||
#define EM_SPARCV9 43 /* SPARC Version 9 */
|
||||
#define EM_TRICORE 44 /* Siemens TriCore embedded processor */
|
||||
#define EM_ARC 45 /* Argonaut RISC Core, Argonaut Technologies
|
||||
Inc. */
|
||||
#define EM_H8_300 46 /* Hitachi H8/300 */
|
||||
#define EM_H8_300H 47 /* Hitachi H8/300H */
|
||||
#define EM_H8S 48 /* Hitachi H8S */
|
||||
#define EM_H8_500 49 /* Hitachi H8/500 */
|
||||
#define EM_IA_64 50 /* Intel IA-64 processor architecture */
|
||||
#define EM_MIPS_X 51 /* Stanford MIPS-X */
|
||||
#define EM_COLDFIRE 52 /* Motorola ColdFire */
|
||||
#define EM_68HC12 53 /* Motorola M68HC12 */
|
||||
#define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator */
|
||||
#define EM_PCP 55 /* Siemens PCP */
|
||||
#define EM_NCPU 56 /* Sony nCPU embedded RISC processor */
|
||||
#define EM_NDR1 57 /* Denso NDR1 microprocessor */
|
||||
#define EM_STARCORE 58 /* Motorola Star*Core processor */
|
||||
#define EM_ME16 59 /* Toyota ME16 processor */
|
||||
#define EM_ST100 60 /* STMicroelectronics ST100 processor */
|
||||
#define EM_TINYJ 61 /* Advanced Logic Corp. TinyJ embedded processor
|
||||
family */
|
||||
#define EM_X86_64 62 /* AMD x86-64 architecture */
|
||||
#define EM_PDSP 63 /* Sony DSP Processor */
|
||||
#define EM_PDP10 64 /* Digital Equipment Corp. PDP-10 */
|
||||
#define EM_PDP11 65 /* Digital Equipment Corp. PDP-11 */
|
||||
#define EM_FX66 66 /* Siemens FX66 microcontroller */
|
||||
#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 bit
|
||||
microcontroller */
|
||||
#define EM_ST7 68 /* STMicroelectronics ST7 8-bit
|
||||
microcontroller */
|
||||
#define EM_68HC16 69 /* Motorola MC68HC16 Microcontroller */
|
||||
#define EM_68HC11 70 /* Motorola MC68HC11 Microcontroller */
|
||||
#define EM_68HC08 71 /* Motorola MC68HC08 Microcontroller */
|
||||
#define EM_68HC05 72 /* Motorola MC68HC05 Microcontroller */
|
||||
#define EM_SVX 73 /* Silicon Graphics SVx */
|
||||
#define EM_ST19 74 /* STMicroelectronics ST19 8-bit
|
||||
microcontroller */
|
||||
#define EM_VAX 75 /* Digital VAX */
|
||||
#define EM_CRIS 76 /* Axis Communications 32-bit embedded
|
||||
processor */
|
||||
#define EM_JAVELIN 77 /* Infineon Technologies 32-bit embedded
|
||||
processor */
|
||||
#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */
|
||||
#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */
|
||||
#define EM_MMIX 80 /* Donald Knuth's educational 64-bit
|
||||
processor */
|
||||
#define EM_HUANY 81 /* Harvard University machine-independent object
|
||||
files */
|
||||
#define EM_PRISM 82 /* SiTera Prism */
|
||||
#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */
|
||||
#define EM_FR30 84 /* Fujitsu FR30 */
|
||||
#define EM_D10V 85 /* Mitsubishi D10V */
|
||||
#define EM_D30V 86 /* Mitsubishi D30V */
|
||||
#define EM_V850 87 /* NEC v850 */
|
||||
#define EM_M32R 88 /* Mitsubishi M32R */
|
||||
#define EM_MN10300 89 /* Matsushita MN10300 */
|
||||
#define EM_MN10200 90 /* Matsushita MN10200 */
|
||||
#define EM_PJ 91 /* picoJava */
|
||||
#define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
|
||||
#define EM_ARC_A5 93 /* ARC Cores Tangent-A5 */
|
||||
#define EM_XTENSA 94 /* Tensilica Xtensa Architecture */
|
||||
#define EM_VIDEOCORE 95 /* Alphamosaic VideoCore processor */
|
||||
#define EM_TMM_GPP 96 /* Thompson Multimedia General Purpose
|
||||
Processor */
|
||||
#define EM_NS32K 97 /* National Semiconductor 32000 series */
|
||||
#define EM_TPC 98 /* Tenor Network TPC processor */
|
||||
#define EM_SNP1K 99 /* Trebia SNP 1000 processor */
|
||||
#define EM_ST200 100 /* STMicroelectronics (www.st.com) ST200
|
||||
microcontroller */
|
||||
#define EM_IP2K 101 /* Ubicom IP2xxx microcontroller family */
|
||||
#define EM_MAX 102 /* MAX Processor */
|
||||
#define EM_CR 103 /* National Semiconductor CompactRISC
|
||||
microprocessor */
|
||||
#define EM_F2MC16 104 /* Fujitsu F2MC16 */
|
||||
#define EM_MSP430 105 /* Texas Instruments embedded microcontroller
|
||||
msp430 */
|
||||
#define EM_BLACKFIN 106 /* Analog Devices Blackfin (DSP) processor */
|
||||
#define EM_SE_C33 107 /* S1C33 Family of Seiko Epson processors */
|
||||
#define EM_SEP 108 /* Sharp embedded microprocessor */
|
||||
#define EM_ARCA 109 /* Arca RISC Microprocessor */
|
||||
#define EM_UNICORE 110 /* Microprocessor series from PKU-Unity Ltd. and
|
||||
MPRC of Peking University */
|
||||
|
||||
/* Special section indexes. */
|
||||
#define SHN_UNDEF 0 /* Undefined, missing, irrelevant. */
|
||||
#define SHN_LORESERVE 0xff00 /* First of reserved range. */
|
||||
#define SHN_LOPROC 0xff00 /* First processor-specific. */
|
||||
#define SHN_HIPROC 0xff1f /* Last processor-specific. */
|
||||
#define SHN_ABS 0xfff1 /* Absolute values. */
|
||||
#define SHN_COMMON 0xfff2 /* Common data. */
|
||||
#define SHN_HIRESERVE 0xffff /* Last of reserved range. */
|
||||
|
||||
/* sh_type */
|
||||
#define SHT_NULL 0 /* inactive */
|
||||
#define SHT_PROGBITS 1 /* program defined information */
|
||||
#define SHT_SYMTAB 2 /* symbol table section */
|
||||
#define SHT_STRTAB 3 /* string table section */
|
||||
#define SHT_RELA 4 /* relocation section with addends */
|
||||
#define SHT_HASH 5 /* symbol hash table section */
|
||||
#define SHT_DYNAMIC 6 /* dynamic section */
|
||||
#define SHT_NOTE 7 /* note section */
|
||||
#define SHT_NOBITS 8 /* no space section */
|
||||
#define SHT_REL 9 /* relocation section - no addends */
|
||||
#define SHT_SHLIB 10 /* reserved - purpose unknown */
|
||||
#define SHT_DYNSYM 11 /* dynamic symbol table section */
|
||||
#define SHT_NUM 12 /* number of section types */
|
||||
#define SHT_LOOS 0x60000000 /* First of OS specific semantics */
|
||||
#define SHT_HIOS 0x6fffffff /* Last of OS specific semantics */
|
||||
#define SHT_LOPROC 0x70000000 /* reserved range for processor */
|
||||
#define SHT_HIPROC 0x7fffffff /* specific section header types */
|
||||
#define SHT_LOUSER 0x80000000 /* reserved range for application */
|
||||
#define SHT_HIUSER 0xffffffff /* specific indexes */
|
||||
|
||||
/* Flags for sh_flags. */
|
||||
#define SHF_WRITE 0x1 /* Section contains writable data. */
|
||||
#define SHF_ALLOC 0x2 /* Section occupies memory. */
|
||||
#define SHF_EXECINSTR 0x4 /* Section contains instructions. */
|
||||
#define SHF_TLS 0x400 /* Section contains TLS data. */
|
||||
#define SHF_MASKPROC 0xf0000000 /* Reserved for processor-specific. */
|
||||
|
||||
/* Values for p_type. */
|
||||
#define PT_NULL 0 /* Unused entry. */
|
||||
#define PT_LOAD 1 /* Loadable segment. */
|
||||
#define PT_DYNAMIC 2 /* Dynamic linking information segment. */
|
||||
#define PT_INTERP 3 /* Pathname of interpreter. */
|
||||
#define PT_NOTE 4 /* Auxiliary information. */
|
||||
#define PT_SHLIB 5 /* Reserved (not used). */
|
||||
#define PT_PHDR 6 /* Location of program header itself. */
|
||||
#define PT_TLS 7 /* Thread local storage segment */
|
||||
|
||||
#define PT_COUNT 8 /* Number of defined p_type values. */
|
||||
|
||||
#define PT_LOOS 0x60000000 /* OS-specific */
|
||||
#define PT_HIOS 0x6fffffff /* OS-specific */
|
||||
#define PT_LOPROC 0x70000000 /* First processor-specific type. */
|
||||
#define PT_HIPROC 0x7fffffff /* Last processor-specific type. */
|
||||
|
||||
/* Values for p_flags. */
|
||||
#define PF_X 0x1 /* Executable. */
|
||||
#define PF_W 0x2 /* Writable. */
|
||||
#define PF_R 0x4 /* Readable. */
|
||||
|
||||
/* Values for d_tag. */
|
||||
#define DT_NULL 0 /* Terminating entry. */
|
||||
#define DT_NEEDED 1 /* String table offset of a needed shared
|
||||
library. */
|
||||
#define DT_PLTRELSZ 2 /* Total size in bytes of PLT relocations. */
|
||||
#define DT_PLTGOT 3 /* Processor-dependent address. */
|
||||
#define DT_HASH 4 /* Address of symbol hash table. */
|
||||
#define DT_STRTAB 5 /* Address of string table. */
|
||||
#define DT_SYMTAB 6 /* Address of symbol table. */
|
||||
#define DT_RELA 7 /* Address of ElfNN_Rela relocations. */
|
||||
#define DT_RELASZ 8 /* Total size of ElfNN_Rela relocations. */
|
||||
#define DT_RELAENT 9 /* Size of each ElfNN_Rela relocation entry. */
|
||||
#define DT_STRSZ 10 /* Size of string table. */
|
||||
#define DT_SYMENT 11 /* Size of each symbol table entry. */
|
||||
#define DT_INIT 12 /* Address of initialization function. */
|
||||
#define DT_FINI 13 /* Address of finalization function. */
|
||||
#define DT_SONAME 14 /* String table offset of shared object
|
||||
name. */
|
||||
#define DT_RPATH 15 /* String table offset of library path. [sup] */
|
||||
#define DT_SYMBOLIC 16 /* Indicates "symbolic" linking. [sup] */
|
||||
#define DT_REL 17 /* Address of ElfNN_Rel relocations. */
|
||||
#define DT_RELSZ 18 /* Total size of ElfNN_Rel relocations. */
|
||||
#define DT_RELENT 19 /* Size of each ElfNN_Rel relocation. */
|
||||
#define DT_PLTREL 20 /* Type of relocation used for PLT. */
|
||||
#define DT_DEBUG 21 /* Reserved (not used). */
|
||||
#define DT_TEXTREL 22 /* Indicates there may be relocations in
|
||||
non-writable segments. [sup] */
|
||||
#define DT_JMPREL 23 /* Address of PLT relocations. */
|
||||
#define DT_BIND_NOW 24 /* [sup] */
|
||||
#define DT_INIT_ARRAY 25 /* Address of the array of pointers to
|
||||
initialization functions */
|
||||
#define DT_FINI_ARRAY 26 /* Address of the array of pointers to
|
||||
termination functions */
|
||||
#define DT_INIT_ARRAYSZ 27 /* Size in bytes of the array of
|
||||
initialization functions. */
|
||||
#define DT_FINI_ARRAYSZ 28 /* Size in bytes of the array of
|
||||
terminationfunctions. */
|
||||
#define DT_RUNPATH 29 /* String table offset of a null-terminated
|
||||
library search path string. */
|
||||
#define DT_FLAGS 30 /* Object specific flag values. */
|
||||
#define DT_ENCODING 32 /* Values greater than or equal to DT_ENCODING
|
||||
and less than DT_LOOS follow the rules for
|
||||
the interpretation of the d_un union
|
||||
as follows: even == 'd_ptr', even == 'd_val'
|
||||
or none */
|
||||
#define DT_PREINIT_ARRAY 32 /* Address of the array of pointers to
|
||||
pre-initialization functions. */
|
||||
#define DT_PREINIT_ARRAYSZ 33 /* Size in bytes of the array of
|
||||
pre-initialization functions. */
|
||||
|
||||
#define DT_COUNT 33 /* Number of defined d_tag values. */
|
||||
|
||||
#define DT_LOOS 0x6000000d /* First OS-specific */
|
||||
#define DT_HIOS 0x6fff0000 /* Last OS-specific */
|
||||
#define DT_LOPROC 0x70000000 /* First processor-specific type. */
|
||||
#define DT_HIPROC 0x7fffffff /* Last processor-specific type. */
|
||||
|
||||
/* Values for DT_FLAGS */
|
||||
#define DF_ORIGIN 0x0001 /* Indicates that the object being loaded may
|
||||
make reference to the $ORIGIN substitution
|
||||
string */
|
||||
#define DF_SYMBOLIC 0x0002 /* Indicates "symbolic" linking. */
|
||||
#define DF_TEXTREL 0x0004 /* Indicates there may be relocations in
|
||||
non-writable segments. */
|
||||
#define DF_BIND_NOW 0x0008 /* Indicates that the dynamic linker should
|
||||
process all relocations for the object
|
||||
containing this entry before transferring
|
||||
control to the program. */
|
||||
#define DF_STATIC_TLS 0x0010 /* Indicates that the shared object or
|
||||
executable contains code using a static
|
||||
thread-local storage scheme. */
|
||||
|
||||
/* Values for n_type. Used in core files. */
|
||||
#define NT_PRSTATUS 1 /* Process status. */
|
||||
#define NT_FPREGSET 2 /* Floating point registers. */
|
||||
#define NT_PRPSINFO 3 /* Process state info. */
|
||||
|
||||
/* Symbol Binding - ELFNN_ST_BIND - st_info */
|
||||
#define STB_LOCAL 0 /* Local symbol */
|
||||
#define STB_GLOBAL 1 /* Global symbol */
|
||||
#define STB_WEAK 2 /* like global - lower precedence */
|
||||
#define STB_LOPROC 13 /* reserved range for processor */
|
||||
#define STB_HIPROC 15 /* specific symbol bindings */
|
||||
|
||||
/* Symbol type - ELFNN_ST_TYPE - st_info */
|
||||
#define STT_NOTYPE 0 /* Unspecified type. */
|
||||
#define STT_OBJECT 1 /* Data object. */
|
||||
#define STT_FUNC 2 /* Function. */
|
||||
#define STT_SECTION 3 /* Section. */
|
||||
#define STT_FILE 4 /* Source file. */
|
||||
#define STT_TLS 6 /* TLS object. */
|
||||
#define STT_LOPROC 13 /* reserved range for processor */
|
||||
#define STT_HIPROC 15 /* specific symbol types */
|
||||
|
||||
/* Special symbol table indexes. */
|
||||
#define STN_UNDEF 0 /* Undefined symbol index. */
|
||||
|
||||
#endif /* !_SYS_ELF_COMMON_H_ */
|
153
reactos/include/elf/elf-i386.h
Normal file
153
reactos/include/elf/elf-i386.h
Normal file
|
@ -0,0 +1,153 @@
|
|||
/*-
|
||||
* Copyright (c) 1996-1997 John D. Polstra.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/sys/i386/include/elf.h,v 1.16 2004/08/02 19:12:17 dfr Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MACHINE_ELF_H_
|
||||
#define _MACHINE_ELF_H_ 1
|
||||
|
||||
/*
|
||||
* ELF definitions for the i386 architecture.
|
||||
*/
|
||||
|
||||
#ifdef _REACTOS_ELF_MACHINE_IS_TARGET
|
||||
|
||||
#ifndef __ELF_WORD_SIZE
|
||||
#define __ELF_WORD_SIZE 32 /* Used by <elf/generic.h> */
|
||||
#endif
|
||||
|
||||
#include <elf/generic.h>
|
||||
|
||||
#define ELF_ARCH EM_386
|
||||
|
||||
#define ELF_MACHINE_OK(x) ((x) == EM_386 || (x) == EM_486)
|
||||
|
||||
/*
|
||||
* Auxiliary vector entries for passing information to the interpreter.
|
||||
*
|
||||
* The i386 supplement to the SVR4 ABI specification names this "auxv_t",
|
||||
* but POSIX lays claim to all symbols ending with "_t".
|
||||
*/
|
||||
|
||||
typedef struct { /* Auxiliary vector entry on initial stack */
|
||||
int a_type; /* Entry type. */
|
||||
union {
|
||||
long a_val; /* Integer value. */
|
||||
void *a_ptr; /* Address. */
|
||||
void (*a_fcn)(void); /* Function pointer (not used). */
|
||||
} a_un;
|
||||
} Elf32_Auxinfo;
|
||||
|
||||
#if __ELF_WORD_SIZE == 64
|
||||
/* Fake for amd64 loader support */
|
||||
typedef struct {
|
||||
int fake;
|
||||
} Elf64_Auxinfo;
|
||||
#endif
|
||||
|
||||
__ElfType(Auxinfo);
|
||||
|
||||
/* Values for a_type. */
|
||||
#define AT_NULL 0 /* Terminates the vector. */
|
||||
#define AT_IGNORE 1 /* Ignored entry. */
|
||||
#define AT_EXECFD 2 /* File descriptor of program to load. */
|
||||
#define AT_PHDR 3 /* Program header of program already loaded. */
|
||||
#define AT_PHENT 4 /* Size of each program header entry. */
|
||||
#define AT_PHNUM 5 /* Number of program header entries. */
|
||||
#define AT_PAGESZ 6 /* Page size in bytes. */
|
||||
#define AT_BASE 7 /* Interpreter's base address. */
|
||||
#define AT_FLAGS 8 /* Flags (unused for i386). */
|
||||
#define AT_ENTRY 9 /* Where interpreter should transfer control. */
|
||||
|
||||
/*
|
||||
* The following non-standard values are used for passing information
|
||||
* from John Polstra's testbed program to the dynamic linker. These
|
||||
* are expected to go away soon.
|
||||
*
|
||||
* Unfortunately, these overlap the Linux non-standard values, so they
|
||||
* must not be used in the same context.
|
||||
*/
|
||||
#define AT_BRK 10 /* Starting point for sbrk and brk. */
|
||||
#define AT_DEBUG 11 /* Debugging level. */
|
||||
|
||||
/*
|
||||
* The following non-standard values are used in Linux ELF binaries.
|
||||
*/
|
||||
#define AT_NOTELF 10 /* Program is not ELF ?? */
|
||||
#define AT_UID 11 /* Real uid. */
|
||||
#define AT_EUID 12 /* Effective uid. */
|
||||
#define AT_GID 13 /* Real gid. */
|
||||
#define AT_EGID 14 /* Effective gid. */
|
||||
|
||||
#define AT_COUNT 15 /* Count of defined aux entry types. */
|
||||
|
||||
/* Define "machine" characteristics */
|
||||
#define ELF_TARG_CLASS ELFCLASS32
|
||||
#define ELF_TARG_DATA ELFDATA2LSB
|
||||
#define ELF_TARG_MACH EM_386
|
||||
#define ELF_TARG_VER 1
|
||||
|
||||
#endif /* _REACTOS_ELF_MACHINE_IS_TARGET */
|
||||
|
||||
/*
|
||||
* Relocation types.
|
||||
*/
|
||||
|
||||
#define R_386_NONE 0 /* No relocation. */
|
||||
#define R_386_32 1 /* Add symbol value. */
|
||||
#define R_386_PC32 2 /* Add PC-relative symbol value. */
|
||||
#define R_386_GOT32 3 /* Add PC-relative GOT offset. */
|
||||
#define R_386_PLT32 4 /* Add PC-relative PLT offset. */
|
||||
#define R_386_COPY 5 /* Copy data from shared object. */
|
||||
#define R_386_GLOB_DAT 6 /* Set GOT entry to data address. */
|
||||
#define R_386_JMP_SLOT 7 /* Set GOT entry to code address. */
|
||||
#define R_386_RELATIVE 8 /* Add load address of shared object. */
|
||||
#define R_386_GOTOFF 9 /* Add GOT-relative symbol address. */
|
||||
#define R_386_GOTPC 10 /* Add PC-relative GOT table address. */
|
||||
#define R_386_TLS_TPOFF 14 /* Negative offset in static TLS block */
|
||||
#define R_386_TLS_IE 15 /* Absolute address of GOT for -ve static TLS */
|
||||
#define R_386_TLS_GOTIE 16 /* GOT entry for negative static TLS block */
|
||||
#define R_386_TLS_LE 17 /* Negative offset relative to static TLS */
|
||||
#define R_386_TLS_GD 18 /* 32 bit offset to GOT (index,off) pair */
|
||||
#define R_386_TLS_LDM 19 /* 32 bit offset to GOT (index,zero) pair */
|
||||
#define R_386_TLS_GD_32 24 /* 32 bit offset to GOT (index,off) pair */
|
||||
#define R_386_TLS_GD_PUSH 25 /* pushl instruction for Sun ABI GD sequence */
|
||||
#define R_386_TLS_GD_CALL 26 /* call instruction for Sun ABI GD sequence */
|
||||
#define R_386_TLS_GD_POP 27 /* popl instruction for Sun ABI GD sequence */
|
||||
#define R_386_TLS_LDM_32 28 /* 32 bit offset to GOT (index,zero) pair */
|
||||
#define R_386_TLS_LDM_PUSH 29 /* pushl instruction for Sun ABI LD sequence */
|
||||
#define R_386_TLS_LDM_CALL 30 /* call instruction for Sun ABI LD sequence */
|
||||
#define R_386_TLS_LDM_POP 31 /* popl instruction for Sun ABI LD sequence */
|
||||
#define R_386_TLS_LDO_32 32 /* 32 bit offset from start of TLS block */
|
||||
#define R_386_TLS_IE_32 33 /* 32 bit offset to GOT static TLS offset entry */
|
||||
#define R_386_TLS_LE_32 34 /* 32 bit offset within static TLS block */
|
||||
#define R_386_TLS_DTPMOD32 35 /* GOT entry containing TLS index */
|
||||
#define R_386_TLS_DTPOFF32 36 /* GOT entry containing TLS offset */
|
||||
#define R_386_TLS_TPOFF32 37 /* GOT entry of -ve static TLS offset */
|
||||
|
||||
#define R_386_COUNT 38 /* Count of defined relocation types. */
|
||||
|
||||
#endif /* !_MACHINE_ELF_H_ */
|
154
reactos/include/elf/elf32.h
Normal file
154
reactos/include/elf/elf32.h
Normal file
|
@ -0,0 +1,154 @@
|
|||
/*-
|
||||
* Copyright (c) 1996-1998 John D. Polstra.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/sys/sys/elf32.h,v 1.8 2002/05/30 08:32:18 dfr Exp $
|
||||
*/
|
||||
|
||||
#ifndef _SYS_ELF32_H_
|
||||
#define _SYS_ELF32_H_ 1
|
||||
|
||||
/*
|
||||
* ELF definitions common to all 32-bit architectures.
|
||||
*/
|
||||
|
||||
typedef ULONG32 Elf32_Addr;
|
||||
typedef USHORT Elf32_Half;
|
||||
typedef ULONG32 Elf32_Off;
|
||||
typedef LONG32 Elf32_Sword;
|
||||
typedef ULONG32 Elf32_Word;
|
||||
typedef ULONG32 Elf32_Size;
|
||||
typedef Elf32_Off Elf32_Hashelt;
|
||||
|
||||
/*
|
||||
* ELF header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
unsigned char e_ident[EI_NIDENT]; /* File identification. */
|
||||
Elf32_Half e_type; /* File type. */
|
||||
Elf32_Half e_machine; /* Machine architecture. */
|
||||
Elf32_Word e_version; /* ELF format version. */
|
||||
Elf32_Addr e_entry; /* Entry point. */
|
||||
Elf32_Off e_phoff; /* Program header file offset. */
|
||||
Elf32_Off e_shoff; /* Section header file offset. */
|
||||
Elf32_Word e_flags; /* Architecture-specific flags. */
|
||||
Elf32_Half e_ehsize; /* Size of ELF header in bytes. */
|
||||
Elf32_Half e_phentsize; /* Size of program header entry. */
|
||||
Elf32_Half e_phnum; /* Number of program header entries. */
|
||||
Elf32_Half e_shentsize; /* Size of section header entry. */
|
||||
Elf32_Half e_shnum; /* Number of section header entries. */
|
||||
Elf32_Half e_shstrndx; /* Section name strings section. */
|
||||
} Elf32_Ehdr;
|
||||
|
||||
/*
|
||||
* Section header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf32_Word sh_name; /* Section name (index into the
|
||||
section header string table). */
|
||||
Elf32_Word sh_type; /* Section type. */
|
||||
Elf32_Word sh_flags; /* Section flags. */
|
||||
Elf32_Addr sh_addr; /* Address in memory image. */
|
||||
Elf32_Off sh_offset; /* Offset in file. */
|
||||
Elf32_Size sh_size; /* Size in bytes. */
|
||||
Elf32_Word sh_link; /* Index of a related section. */
|
||||
Elf32_Word sh_info; /* Depends on section type. */
|
||||
Elf32_Size sh_addralign; /* Alignment in bytes. */
|
||||
Elf32_Size sh_entsize; /* Size of each entry in section. */
|
||||
} Elf32_Shdr;
|
||||
|
||||
/*
|
||||
* Program header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf32_Word p_type; /* Entry type. */
|
||||
Elf32_Off p_offset; /* File offset of contents. */
|
||||
Elf32_Addr p_vaddr; /* Virtual address in memory image. */
|
||||
Elf32_Addr p_paddr; /* Physical address (not used). */
|
||||
Elf32_Size p_filesz; /* Size of contents in file. */
|
||||
Elf32_Size p_memsz; /* Size of contents in memory. */
|
||||
Elf32_Word p_flags; /* Access permission flags. */
|
||||
Elf32_Size p_align; /* Alignment in memory and file. */
|
||||
} Elf32_Phdr;
|
||||
|
||||
/*
|
||||
* Dynamic structure. The ".dynamic" section contains an array of them.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf32_Sword d_tag; /* Entry type. */
|
||||
union {
|
||||
Elf32_Size d_val; /* Integer value. */
|
||||
Elf32_Addr d_ptr; /* Address value. */
|
||||
} d_un;
|
||||
} Elf32_Dyn;
|
||||
|
||||
/*
|
||||
* Relocation entries.
|
||||
*/
|
||||
|
||||
/* Relocations that don't need an addend field. */
|
||||
typedef struct {
|
||||
Elf32_Addr r_offset; /* Location to be relocated. */
|
||||
Elf32_Word r_info; /* Relocation type and symbol index. */
|
||||
} Elf32_Rel;
|
||||
|
||||
/* Relocations that need an addend field. */
|
||||
typedef struct {
|
||||
Elf32_Addr r_offset; /* Location to be relocated. */
|
||||
Elf32_Word r_info; /* Relocation type and symbol index. */
|
||||
Elf32_Sword r_addend; /* Addend. */
|
||||
} Elf32_Rela;
|
||||
|
||||
/* Macros for accessing the fields of r_info. */
|
||||
#define ELF32_R_SYM(info) ((info) >> 8)
|
||||
#define ELF32_R_TYPE(info) ((unsigned char)(info))
|
||||
|
||||
/* Macro for constructing r_info from field values. */
|
||||
#define ELF32_R_INFO(sym, type) (((sym) << 8) + (unsigned char)(type))
|
||||
|
||||
/*
|
||||
* Symbol table entries.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf32_Word st_name; /* String table index of name. */
|
||||
Elf32_Addr st_value; /* Symbol value. */
|
||||
Elf32_Size st_size; /* Size of associated object. */
|
||||
unsigned char st_info; /* Type and binding information. */
|
||||
unsigned char st_other; /* Reserved (not used). */
|
||||
Elf32_Half st_shndx; /* Section index of symbol. */
|
||||
} Elf32_Sym;
|
||||
|
||||
/* Macros for accessing the fields of st_info. */
|
||||
#define ELF32_ST_BIND(info) ((info) >> 4)
|
||||
#define ELF32_ST_TYPE(info) ((info) & 0xf)
|
||||
|
||||
/* Macro for constructing st_info from field values. */
|
||||
#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
|
||||
|
||||
#endif /* !_SYS_ELF32_H_ */
|
167
reactos/include/elf/elf64.h
Normal file
167
reactos/include/elf/elf64.h
Normal file
|
@ -0,0 +1,167 @@
|
|||
/*-
|
||||
* Copyright (c) 1996-1998 John D. Polstra.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/sys/sys/elf64.h,v 1.10 2002/05/30 08:32:18 dfr Exp $
|
||||
*/
|
||||
|
||||
#ifndef _SYS_ELF64_H_
|
||||
#define _SYS_ELF64_H_ 1
|
||||
|
||||
/*
|
||||
* ELF definitions common to all 64-bit architectures.
|
||||
*/
|
||||
|
||||
typedef ULONG64 Elf64_Addr;
|
||||
typedef ULONG32 Elf64_Half;
|
||||
typedef ULONG64 Elf64_Off;
|
||||
typedef LONG64 Elf64_Sword;
|
||||
typedef ULONG64 Elf64_Word;
|
||||
typedef ULONG64 Elf64_Size;
|
||||
typedef USHORT Elf64_Quarter;
|
||||
|
||||
/*
|
||||
* Types of dynamic symbol hash table bucket and chain elements.
|
||||
*
|
||||
* This is inconsistent among 64 bit architectures, so a machine dependent
|
||||
* typedef is required.
|
||||
*/
|
||||
|
||||
#ifdef _ALPHA_
|
||||
typedef Elf64_Off Elf64_Hashelt;
|
||||
#else
|
||||
typedef Elf64_Half Elf64_Hashelt;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ELF header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
unsigned char e_ident[EI_NIDENT]; /* File identification. */
|
||||
Elf64_Quarter e_type; /* File type. */
|
||||
Elf64_Quarter e_machine; /* Machine architecture. */
|
||||
Elf64_Half e_version; /* ELF format version. */
|
||||
Elf64_Addr e_entry; /* Entry point. */
|
||||
Elf64_Off e_phoff; /* Program header file offset. */
|
||||
Elf64_Off e_shoff; /* Section header file offset. */
|
||||
Elf64_Half e_flags; /* Architecture-specific flags. */
|
||||
Elf64_Quarter e_ehsize; /* Size of ELF header in bytes. */
|
||||
Elf64_Quarter e_phentsize; /* Size of program header entry. */
|
||||
Elf64_Quarter e_phnum; /* Number of program header entries. */
|
||||
Elf64_Quarter e_shentsize; /* Size of section header entry. */
|
||||
Elf64_Quarter e_shnum; /* Number of section header entries. */
|
||||
Elf64_Quarter e_shstrndx; /* Section name strings section. */
|
||||
} Elf64_Ehdr;
|
||||
|
||||
/*
|
||||
* Section header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf64_Half sh_name; /* Section name (index into the
|
||||
section header string table). */
|
||||
Elf64_Half sh_type; /* Section type. */
|
||||
Elf64_Size sh_flags; /* Section flags. */
|
||||
Elf64_Addr sh_addr; /* Address in memory image. */
|
||||
Elf64_Off sh_offset; /* Offset in file. */
|
||||
Elf64_Size sh_size; /* Size in bytes. */
|
||||
Elf64_Half sh_link; /* Index of a related section. */
|
||||
Elf64_Half sh_info; /* Depends on section type. */
|
||||
Elf64_Size sh_addralign; /* Alignment in bytes. */
|
||||
Elf64_Size sh_entsize; /* Size of each entry in section. */
|
||||
} Elf64_Shdr;
|
||||
|
||||
/*
|
||||
* Program header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf64_Half p_type; /* Entry type. */
|
||||
Elf64_Half p_flags; /* Access permission flags. */
|
||||
Elf64_Off p_offset; /* File offset of contents. */
|
||||
Elf64_Addr p_vaddr; /* Virtual address in memory image. */
|
||||
Elf64_Addr p_paddr; /* Physical address (not used). */
|
||||
Elf64_Size p_filesz; /* Size of contents in file. */
|
||||
Elf64_Size p_memsz; /* Size of contents in memory. */
|
||||
Elf64_Size p_align; /* Alignment in memory and file. */
|
||||
} Elf64_Phdr;
|
||||
|
||||
/*
|
||||
* Dynamic structure. The ".dynamic" section contains an array of them.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf64_Size d_tag; /* Entry type. */
|
||||
union {
|
||||
Elf64_Size d_val; /* Integer value. */
|
||||
Elf64_Addr d_ptr; /* Address value. */
|
||||
} d_un;
|
||||
} Elf64_Dyn;
|
||||
|
||||
/*
|
||||
* Relocation entries.
|
||||
*/
|
||||
|
||||
/* Relocations that don't need an addend field. */
|
||||
typedef struct {
|
||||
Elf64_Addr r_offset; /* Location to be relocated. */
|
||||
Elf64_Size r_info; /* Relocation type and symbol index. */
|
||||
} Elf64_Rel;
|
||||
|
||||
/* Relocations that need an addend field. */
|
||||
typedef struct {
|
||||
Elf64_Addr r_offset; /* Location to be relocated. */
|
||||
Elf64_Size r_info; /* Relocation type and symbol index. */
|
||||
Elf64_Off r_addend; /* Addend. */
|
||||
} Elf64_Rela;
|
||||
|
||||
/* Macros for accessing the fields of r_info. */
|
||||
#define ELF64_R_SYM(info) ((info) >> 32)
|
||||
#define ELF64_R_TYPE(info) ((unsigned char)(info))
|
||||
|
||||
/* Macro for constructing r_info from field values. */
|
||||
#define ELF64_R_INFO(sym, type) (((sym) << 32) + (unsigned char)(type))
|
||||
|
||||
/*
|
||||
* Symbol table entries.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf64_Half st_name; /* String table index of name. */
|
||||
unsigned char st_info; /* Type and binding information. */
|
||||
unsigned char st_other; /* Reserved (not used). */
|
||||
Elf64_Quarter st_shndx; /* Section index of symbol. */
|
||||
Elf64_Addr st_value; /* Symbol value. */
|
||||
Elf64_Size st_size; /* Size of associated object. */
|
||||
} Elf64_Sym;
|
||||
|
||||
/* Macros for accessing the fields of st_info. */
|
||||
#define ELF64_ST_BIND(info) ((info) >> 4)
|
||||
#define ELF64_ST_TYPE(info) ((info) & 0xf)
|
||||
|
||||
/* Macro for constructing st_info from field values. */
|
||||
#define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
|
||||
|
||||
#endif /* !_SYS_ELF64_H_ */
|
87
reactos/include/elf/generic.h
Normal file
87
reactos/include/elf/generic.h
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*-
|
||||
* Copyright (c) 1998 John D. Polstra.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/sys/sys/elf_generic.h,v 1.6 2002/07/20 02:56:11 peter Exp $
|
||||
*/
|
||||
|
||||
#ifndef _SYS_ELF_GENERIC_H_
|
||||
#define _SYS_ELF_GENERIC_H_ 1
|
||||
|
||||
/* [REACTOS] TODO: we need to define this somewhere global */
|
||||
#define __ELF_CONCAT1(x,y) x ## y
|
||||
#define __ELF_CONCAT(x,y) __ELF_CONCAT1(x,y)
|
||||
|
||||
/*
|
||||
* Definitions of generic ELF names which relieve applications from
|
||||
* needing to know the word size.
|
||||
*/
|
||||
|
||||
#if __ELF_WORD_SIZE != 32 && __ELF_WORD_SIZE != 64
|
||||
#error "__ELF_WORD_SIZE must be defined as 32 or 64"
|
||||
#endif
|
||||
|
||||
#define ELF_CLASS __ELF_CONCAT(ELFCLASS,__ELF_WORD_SIZE)
|
||||
|
||||
#if 0
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
#define ELF_DATA ELFDATA2LSB
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
#define ELF_DATA ELFDATA2MSB
|
||||
#else
|
||||
#error "Unknown byte order"
|
||||
#endif
|
||||
#else
|
||||
/* [REACTOS] FIXME: we need to add this to our build system */
|
||||
#define ELF_DATA ELFDATA2LSB
|
||||
#endif
|
||||
|
||||
#define __elfN(x) __ELF_CONCAT(__ELF_CONCAT(__ELF_CONCAT(elf,__ELF_WORD_SIZE),_),x)
|
||||
#define __ElfN(x) __ELF_CONCAT(__ELF_CONCAT(__ELF_CONCAT(Elf,__ELF_WORD_SIZE),_),x)
|
||||
#define __ELFN(x) __ELF_CONCAT(__ELF_CONCAT(__ELF_CONCAT(ELF,__ELF_WORD_SIZE),_),x)
|
||||
#define __ElfType(x) typedef __ElfN(x) __ELF_CONCAT(Elf_,x)
|
||||
|
||||
__ElfType(Addr);
|
||||
__ElfType(Half);
|
||||
__ElfType(Off);
|
||||
__ElfType(Sword);
|
||||
__ElfType(Word);
|
||||
__ElfType(Size);
|
||||
__ElfType(Hashelt);
|
||||
__ElfType(Ehdr);
|
||||
__ElfType(Shdr);
|
||||
__ElfType(Phdr);
|
||||
__ElfType(Dyn);
|
||||
__ElfType(Rel);
|
||||
__ElfType(Rela);
|
||||
__ElfType(Sym);
|
||||
|
||||
#define ELF_R_SYM __ELFN(R_SYM)
|
||||
#define ELF_R_TYPE __ELFN(R_TYPE)
|
||||
#define ELF_R_INFO __ELFN(R_INFO)
|
||||
#define ELF_ST_BIND __ELFN(ST_BIND)
|
||||
#define ELF_ST_TYPE __ELFN(ST_TYPE)
|
||||
#define ELF_ST_INFO __ELFN(ST_INFO)
|
||||
|
||||
#endif /* !_SYS_ELF_GENERIC_H_ */
|
12
reactos/include/elf/machine.h
Normal file
12
reactos/include/elf/machine.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef _REACTOS_ELF_MACHINE_H_
|
||||
#define _REACTOS_ELF_MACHINE_H_ 1
|
||||
|
||||
#ifdef _M_IX86
|
||||
#define _REACTOS_ELF_MACHINE_IS_TARGET
|
||||
#include <elf/elf-i386.h>
|
||||
#undef _REACTOS_ELF_MACHINE_IS_TARGET
|
||||
#else
|
||||
#error Unsupported target architecture
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -40,6 +40,7 @@
|
|||
#define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002 // File is executable (i.e. no unresolved externel references).
|
||||
#define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004 // Line nunbers stripped from file.
|
||||
#define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008 // Local symbols stripped from file.
|
||||
#define IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020 // Application supports addresses >2GB
|
||||
#define IMAGE_FILE_BYTES_REVERSED_LO 0x0080 // Bytes of machine word are reversed.
|
||||
#define IMAGE_FILE_32BIT_MACHINE 0x0100 // 32 bit word machine.
|
||||
#define IMAGE_FILE_DEBUG_STRIPPED 0x0200 // Debugging info stripped from file in .DBG file
|
||||
|
@ -57,6 +58,12 @@
|
|||
#define IMAGE_FILE_MACHINE_R10000 0x168 // MIPS little-endian
|
||||
#define IMAGE_FILE_MACHINE_ALPHA 0x184 // Alpha_AXP
|
||||
#define IMAGE_FILE_MACHINE_POWERPC 0x1F0 // IBM PowerPC Little-Endian
|
||||
#define IMAGE_FILE_MACHINE_ARM 0x01c0 // ARM little-endian
|
||||
#define IMAGE_FILE_MACHINE_IA64 0x0200 // Intel IA64
|
||||
#define IMAGE_FILE_MACHINE_AXP64 IMAGE_FILE_MACHINE_ALPHA64
|
||||
#define IMAGE_FILE_MACHINE_ALPHA64 0x0284 // Alpha AXP, full 64-bit support
|
||||
#define IMAGE_FILE_MACHINE_AMD64 0x8664 // AMD x86-64
|
||||
#define IMAGE_FILE_MACHINE_M32R 0x9041 // M32R little-endian
|
||||
|
||||
#pragma pack(push,4)
|
||||
typedef struct _IMAGE_FILE_HEADER {
|
||||
|
@ -104,7 +111,46 @@ typedef struct _IMAGE_OPTIONAL_HEADER {
|
|||
DWORD LoaderFlags;
|
||||
DWORD NumberOfRvaAndSizes;
|
||||
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
|
||||
} IMAGE_OPTIONAL_HEADER,*PIMAGE_OPTIONAL_HEADER;
|
||||
} IMAGE_OPTIONAL_HEADER32,*PIMAGE_OPTIONAL_HEADER32;
|
||||
typedef struct _IMAGE_OPTIONAL_HEADER64 {
|
||||
WORD Magic;
|
||||
BYTE MajorLinkerVersion;
|
||||
BYTE MinorLinkerVersion;
|
||||
DWORD SizeOfCode;
|
||||
DWORD SizeOfInitializedData;
|
||||
DWORD SizeOfUninitializedData;
|
||||
DWORD AddressOfEntryPoint;
|
||||
DWORD BaseOfCode;
|
||||
ULONGLONG ImageBase;
|
||||
DWORD SectionAlignment;
|
||||
DWORD FileAlignment;
|
||||
WORD MajorOperatingSystemVersion;
|
||||
WORD MinorOperatingSystemVersion;
|
||||
WORD MajorImageVersion;
|
||||
WORD MinorImageVersion;
|
||||
WORD MajorSubsystemVersion;
|
||||
WORD MinorSubsystemVersion;
|
||||
DWORD Reserved1;
|
||||
DWORD SizeOfImage;
|
||||
DWORD SizeOfHeaders;
|
||||
DWORD CheckSum;
|
||||
WORD Subsystem;
|
||||
WORD DllCharacteristics;
|
||||
ULONGLONG SizeOfStackReserve;
|
||||
ULONGLONG SizeOfStackCommit;
|
||||
ULONGLONG SizeOfHeapReserve;
|
||||
ULONGLONG SizeOfHeapCommit;
|
||||
DWORD LoaderFlags;
|
||||
DWORD NumberOfRvaAndSizes;
|
||||
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
|
||||
} IMAGE_OPTIONAL_HEADER64,*PIMAGE_OPTIONAL_HEADER64;
|
||||
#ifdef _WIN64
|
||||
typedef IMAGE_OPTIONAL_HEADER64 IMAGE_OPTIONAL_HEADER;
|
||||
typedef PIMAGE_OPTIONAL_HEADER64 PIMAGE_OPTIONAL_HEADER;
|
||||
#else
|
||||
typedef IMAGE_OPTIONAL_HEADER32 IMAGE_OPTIONAL_HEADER;
|
||||
typedef PIMAGE_OPTIONAL_HEADER32 PIMAGE_OPTIONAL_HEADER;
|
||||
#endif
|
||||
typedef struct _IMAGE_ROM_OPTIONAL_HEADER {
|
||||
WORD Magic;
|
||||
BYTE MajorLinkerVersion;
|
||||
|
@ -180,8 +226,20 @@ typedef struct _IMAGE_OS2_HEADER {
|
|||
typedef struct _IMAGE_NT_HEADERS {
|
||||
DWORD Signature;
|
||||
IMAGE_FILE_HEADER FileHeader;
|
||||
IMAGE_OPTIONAL_HEADER OptionalHeader;
|
||||
} IMAGE_NT_HEADERS,*PIMAGE_NT_HEADERS;
|
||||
IMAGE_OPTIONAL_HEADER32 OptionalHeader;
|
||||
} IMAGE_NT_HEADERS32,*PIMAGE_NT_HEADERS32;
|
||||
typedef struct _IMAGE_NT_HEADERS64 {
|
||||
DWORD Signature;
|
||||
IMAGE_FILE_HEADER FileHeader;
|
||||
IMAGE_OPTIONAL_HEADER64 OptionalHeader;
|
||||
} IMAGE_NT_HEADERS64,*PIMAGE_NT_HEADERS64;
|
||||
#ifdef _WIN64
|
||||
typedef IMAGE_NT_HEADERS64 IMAGE_NT_HEADERS;
|
||||
typedef PIMAGE_NT_HEADERS64 PIMAGE_NT_HEADERS;
|
||||
#else
|
||||
typedef IMAGE_NT_HEADERS32 IMAGE_NT_HEADERS;
|
||||
typedef PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS;
|
||||
#endif
|
||||
typedef struct _IMAGE_ROM_HEADERS {
|
||||
IMAGE_FILE_HEADER FileHeader;
|
||||
IMAGE_ROM_OPTIONAL_HEADER OptionalHeader;
|
||||
|
@ -494,6 +552,12 @@ typedef struct _IMAGE_RESOURCE_DIRECTORY {
|
|||
} IMAGE_RESOURCE_DIRECTORY, *PIMAGE_RESOURCE_DIRECTORY;
|
||||
*/
|
||||
|
||||
#ifdef _WIN64
|
||||
#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR64_MAGIC
|
||||
#else
|
||||
#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC
|
||||
#endif
|
||||
|
||||
#endif /* !__USE_W32API */
|
||||
|
||||
#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
|
||||
|
@ -555,7 +619,8 @@ typedef struct _IMAGE_RESOURCE_DIRECTORY {
|
|||
#define IMAGE_SIZEOF_FILE_HEADER 20
|
||||
#define IMAGE_FILE_MACHINE_UNKNOWN 0
|
||||
#define IMAGE_NT_SIGNATURE 0x00004550
|
||||
#define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
|
||||
#define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
|
||||
#define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
|
||||
#define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
|
||||
#define IMAGE_SEPARATE_DEBUG_SIGNATURE 0x4944
|
||||
#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
|
||||
|
|
|
@ -23,6 +23,50 @@
|
|||
#ifndef KJK_PSEH_H_
|
||||
#define KJK_PSEH_H_
|
||||
|
||||
/* Some useful macros */
|
||||
#if defined(__cplusplus)
|
||||
# define _SEH_PVOID_CAST(TYPE_, P_) ((TYPE_)(P_))
|
||||
#else
|
||||
# define _SEH_PVOID_CAST(TYPE_, P_) (P_)
|
||||
#endif
|
||||
|
||||
#if defined(FIELD_OFFSET)
|
||||
# define _SEH_FIELD_OFFSET FIELD_OFFSET
|
||||
#else
|
||||
# include <stddef.h>
|
||||
# define _SEH_FIELD_OFFSET offsetof
|
||||
#endif
|
||||
|
||||
#if defined(CONTAINING_RECORD)
|
||||
# define _SEH_CONTAINING_RECORD CONTAINING_RECORD
|
||||
#else
|
||||
# define _SEH_CONTAINING_RECORD(ADDR_, TYPE_, FIELD_) \
|
||||
((TYPE_ *)(((char *)(ADDR_)) - _SEH_FIELD_OFFSET(TYPE_, FIELD_)))
|
||||
#endif
|
||||
|
||||
#if defined(__CONCAT)
|
||||
# define _SEH_CONCAT __CONCAT
|
||||
#else
|
||||
# define _SEH_CONCAT1(X_, Y_) X_ ## Y_
|
||||
# define _SEH_CONCAT(X_, Y_) _SEH_CONCAT1(X_, Y_)
|
||||
#endif
|
||||
|
||||
/* Locals sharing support */
|
||||
#define _SEH_LOCALS_TYPENAME(BASENAME_) \
|
||||
struct _SEH_CONCAT(_SEHLocalsTag, BASENAME_)
|
||||
|
||||
#define _SEH_DEFINE_LOCALS(BASENAME_) \
|
||||
_SEH_LOCALS_TYPENAME(BASENAME_)
|
||||
|
||||
#define _SEH_DECLARE_LOCALS(BASENAME_) \
|
||||
_SEH_LOCALS_TYPENAME(BASENAME_) _SEHLocals; \
|
||||
_SEH_LOCALS_TYPENAME(BASENAME_) * _SEHPLocals; \
|
||||
_SEHPLocals = &_SEHLocals;
|
||||
|
||||
/* Dummy locals */
|
||||
static _SEH_LOCALS_TYPENAME(_SEHDummyLocals) { int Dummy_; } _SEHLocals;
|
||||
static void __inline _SEHDummyLocalsUser(void) { (void)_SEHLocals; }
|
||||
|
||||
/* TODO: <pseh/native.h> to wrap native SEH implementations */
|
||||
#include <pseh/framebased.h>
|
||||
|
||||
|
|
|
@ -72,21 +72,66 @@ static __declspec(noreturn) __inline void __stdcall _SEHCompilerSpecificHandler
|
|||
_SEHLongJmp(myframe->SEH_JmpBuf, 1);
|
||||
}
|
||||
|
||||
/* SHARED LOCALS */
|
||||
/* Access the locals for the current frame */
|
||||
#define _SEH_ACCESS_LOCALS(LOCALS_) \
|
||||
_SEH_LOCALS_TYPENAME(LOCALS_) * _SEHPLocals; \
|
||||
_SEHPLocals = \
|
||||
_SEH_PVOID_CAST \
|
||||
( \
|
||||
_SEH_LOCALS_TYPENAME(LOCALS_) *, \
|
||||
_SEH_CONTAINING_RECORD(_SEHPortableFrame, _SEHFrame_t, SEH_Header) \
|
||||
->SEH_Locals \
|
||||
);
|
||||
|
||||
/* Access local variable VAR_ */
|
||||
#define _SEH_VAR(VAR_) _SEHPLocals->VAR_
|
||||
|
||||
/* FILTER FUNCTIONS */
|
||||
/* Declares a filter function's prototype */
|
||||
#define _SEH_FILTER(NAME_) \
|
||||
int __stdcall NAME_ \
|
||||
long __stdcall NAME_ \
|
||||
( \
|
||||
struct _EXCEPTION_POINTERS * _SEHExceptionPointers, \
|
||||
struct __SEHPortableFrame * _SEHPortableFrame \
|
||||
)
|
||||
|
||||
/* Declares a static filter */
|
||||
#define _SEH_STATIC_FILTER(ACTION_) ((_SEHFilter_t)((ACTION_) + 2))
|
||||
|
||||
/* Declares a PSEH filter wrapping a regular filter function */
|
||||
#define _SEH_WRAP_FILTER(WRAPPER_, NAME_) \
|
||||
static __inline _SEH_FILTER(WRAPPER_) \
|
||||
{ \
|
||||
return (NAME_)(_SEHExceptionPointers); \
|
||||
}
|
||||
|
||||
/* FINALLY FUNCTIONS */
|
||||
/* Declares a finally function's prototype */
|
||||
#define _SEH_FINALLY(NAME_) \
|
||||
void __stdcall NAME_ \
|
||||
( \
|
||||
struct __SEHPortableFrame * _SEHPortableFrame \
|
||||
)
|
||||
|
||||
/* Declares a PSEH finally function wrapping a regular function */
|
||||
#define _SEH_WRAP_FINALLY(WRAPPER_, NAME_) \
|
||||
_SEH_WRAP_FINALLY_ARGS(WRAPPER_, NAME_, ())
|
||||
|
||||
#define _SEH_WRAP_FINALLY_ARGS(WRAPPER_, NAME_, ARGS_) \
|
||||
static __inline _SEH_FINALLY(WRAPPER_) \
|
||||
{ \
|
||||
NAME_ ARGS_; \
|
||||
}
|
||||
|
||||
#define _SEH_WRAP_FINALLY_LOCALS_ARGS(WRAPPER_, LOCALS_, NAME_, ARGS_) \
|
||||
static __inline _SEH_FINALLY(WRAPPER_) \
|
||||
{ \
|
||||
_SEH_ACCESS_LOCALS(LOCALS_); \
|
||||
NAME_ ARGS_; \
|
||||
}
|
||||
|
||||
/* SAFE BLOCKS */
|
||||
#define _SEH_TRY_FINALLY(FINALLY_) \
|
||||
_SEH_TRY_FILTER_FINALLY \
|
||||
( \
|
||||
|
@ -109,22 +154,36 @@ static __declspec(noreturn) __inline void __stdcall _SEHCompilerSpecificHandler
|
|||
#define _SEH_TRY \
|
||||
_SEH_TRY_HANDLE_FINALLY(NULL)
|
||||
|
||||
#ifdef __cplusplus
|
||||
# define _SEH_DECLARE_HANDLERS(FILTER_, FINALLY_) \
|
||||
const _SEHHandlers_t _SEHHandlers = \
|
||||
{ \
|
||||
(FILTER_), \
|
||||
_SEHCompilerSpecificHandler, \
|
||||
(FINALLY_) \
|
||||
};
|
||||
#else
|
||||
# define _SEH_DECLARE_HANDLERS(FILTER_, FINALLY_) \
|
||||
_SEHHandlers_t _SEHHandlers = \
|
||||
{ \
|
||||
(0), \
|
||||
_SEHCompilerSpecificHandler, \
|
||||
(0) \
|
||||
}; \
|
||||
_SEHHandlers.SH_Filter = (FILTER_); \
|
||||
_SEHHandlers.SH_Finally = (FINALLY_);
|
||||
#endif
|
||||
|
||||
#define _SEH_TRY_FILTER_FINALLY(FILTER_, FINALLY_) \
|
||||
{ \
|
||||
static _SEHHandlers_t _SEHHandlers = \
|
||||
{ \
|
||||
(NULL), \
|
||||
_SEHCompilerSpecificHandler, \
|
||||
(NULL) \
|
||||
}; \
|
||||
_SEHHandlers.SH_Filter = FILTER_; \
|
||||
_SEHHandlers.SH_Finally = FINALLY_; \
|
||||
_SEH_DECLARE_HANDLERS(FILTER_, FINALLY_); \
|
||||
\
|
||||
_SEHFrame_t * _SEHFrame; \
|
||||
volatile _SEHPortableFrame_t * _SEHPortableFrame; \
|
||||
\
|
||||
_SEHFrame = _alloca(sizeof(_SEHFrame_t)); \
|
||||
_SEHFrame->SEH_Header.SPF_Handlers = &_SEHHandlers; \
|
||||
_SEHFrame->SEH_Locals = &_SEHLocals; \
|
||||
\
|
||||
_SEHPortableFrame = &_SEHFrame->SEH_Header; \
|
||||
(void)_SEHPortableFrame; \
|
||||
|
|
|
@ -44,7 +44,7 @@ _SEHRegistration_t;
|
|||
|
||||
struct __SEHPortableFrame;
|
||||
|
||||
typedef int (__stdcall * _SEHFilter_t)
|
||||
typedef long (__stdcall * _SEHFilter_t)
|
||||
(
|
||||
struct _EXCEPTION_POINTERS *,
|
||||
struct __SEHPortableFrame *
|
||||
|
|
100
reactos/include/reactos/exeformat.h
Normal file
100
reactos/include/reactos/exeformat.h
Normal file
|
@ -0,0 +1,100 @@
|
|||
#ifndef REACTOS_EXEFORMAT_H_INCLUDED_
|
||||
#define REACTOS_EXEFORMAT_H_INCLUDED_ 1
|
||||
|
||||
/*
|
||||
* LOADER API
|
||||
*/
|
||||
/* OUT flags returned by a loader */
|
||||
#define EXEFMT_LOAD_ASSUME_SEGMENTS_SORTED (1 << 0)
|
||||
#define EXEFMT_LOAD_ASSUME_SEGMENTS_NO_OVERLAP (1 << 1)
|
||||
#define EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED (1 << 2)
|
||||
|
||||
#define EXEFMT_LOAD_ASSUME_SEGMENTS_OK \
|
||||
( \
|
||||
EXEFMT_LOAD_ASSUME_SEGMENTS_SORTED | \
|
||||
EXEFMT_LOAD_ASSUME_SEGMENTS_NO_OVERLAP | \
|
||||
EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED \
|
||||
)
|
||||
|
||||
/*
|
||||
Minumum size of the buffer passed to each loader for identification of the
|
||||
executable
|
||||
*/
|
||||
#define EXEFMT_LOAD_HEADER_SIZE (0x2000)
|
||||
|
||||
/* Special values for the base address of images */
|
||||
/*
|
||||
Base address can't be represented in an ULONG_PTR: any effective load address
|
||||
will require relocation
|
||||
*/
|
||||
#define EXEFMT_LOAD_BASE_NONE ((ULONG_PTR)-1)
|
||||
|
||||
/* Base address never matters, relocation never required */
|
||||
#define EXEFMT_LOAD_BASE_ANY ((ULONG_PTR)-2)
|
||||
|
||||
typedef NTSTATUS (NTAPI * PEXEFMT_CB_READ_FILE)
|
||||
(
|
||||
IN PVOID File,
|
||||
IN PLARGE_INTEGER Offset,
|
||||
IN ULONG Length,
|
||||
OUT PVOID * Data,
|
||||
OUT PVOID * AllocBase,
|
||||
OUT PULONG ReadSize
|
||||
);
|
||||
|
||||
typedef PMM_SECTION_SEGMENT (NTAPI * PEXEFMT_CB_ALLOCATE_SEGMENTS)
|
||||
(
|
||||
IN ULONG NrSegments
|
||||
);
|
||||
|
||||
typedef NTSTATUS (NTAPI * PEXEFMT_LOADER)
|
||||
(
|
||||
IN CONST VOID * FileHeader,
|
||||
IN SIZE_T FileHeaderSize,
|
||||
IN PVOID File,
|
||||
OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
|
||||
OUT PULONG Flags,
|
||||
IN PEXEFMT_CB_READ_FILE ReadFileCb,
|
||||
IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
|
||||
);
|
||||
|
||||
/*
|
||||
* STATUS CONSTANTS
|
||||
*/
|
||||
|
||||
#define FACILITY_ROS_EXEFMT (0x10)
|
||||
|
||||
/*
|
||||
* Returned by ExeFormat loaders to tell the caller the format isn't supported,
|
||||
* as opposed to STATUS_INVALID_IMAGE_FORMAT meaning the format is supported,
|
||||
* but the particular file is malformed
|
||||
*/
|
||||
#define STATUS_ROS_EXEFMT_UNKNOWN_FORMAT (0xA0100001)
|
||||
|
||||
/*
|
||||
* Returned by MmCreateSection to signal successful loading of an executable
|
||||
* image, saving the caller the effort of determining the executable's format
|
||||
* again. The full status to return is obtained by performing a bitwise OR of
|
||||
* STATUS_ROS_EXEFMT_LOADED_FORMAT and the appropriate EXEFMT_LOADED_XXX
|
||||
*/
|
||||
#define FACILITY_ROS_EXEFMT_FORMAT (0x11)
|
||||
#define STATUS_ROS_EXEFMT_LOADED_FORMAT (0x60110000)
|
||||
|
||||
/* non-standard format, ZwQuerySection required to retrieve the format tag */
|
||||
#define EXEFMT_LOADED_EXTENDED (0x0000FFFF)
|
||||
|
||||
/* Windows PE32/PE32+ */
|
||||
#define EXEFMT_LOADED_PE32 (0x00000000)
|
||||
#define EXEFMT_LOADED_PE64 (0x00000001)
|
||||
|
||||
/* Wine ELF */
|
||||
#define EXEFMT_LOADED_WINE32 (0x00000002)
|
||||
#define EXEFMT_LOADED_WINE64 (0x00000003)
|
||||
|
||||
/* regular ELF */
|
||||
#define EXEFMT_LOADED_ELF32 (0x00000004)
|
||||
#define EXEFMT_LOADED_ELF64 (0x00000005)
|
||||
|
||||
#endif
|
||||
|
||||
/* EOF */
|
2822
reactos/lib/ntdll/ldr/elf.c
Normal file
2822
reactos/lib/ntdll/ldr/elf.c
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,127 +0,0 @@
|
|||
#define RTL_FOREACH_LIST(PLIST_HEAD__, TYPE__, FIELD__) \
|
||||
{ \
|
||||
PLIST_ENTRY _RTL_FOREACH_LIST_END = (PLIST_HEAD__); \
|
||||
PLIST_ENTRY _RTL_FOREACH_LIST_CUR = _RTL_FOREACH_LIST_END->Flink; \
|
||||
TYPE__ * _RTL_ITEM = \
|
||||
CONTAINING_RECORD(_RTL_FOREACH_LIST_CUR, TYPE__, FIELD__); \
|
||||
\
|
||||
for \
|
||||
( \
|
||||
; \
|
||||
_RTL_FOREACH_LIST_CUR != _RTL_FOREACH_LIST_END; \
|
||||
_RTL_ITEM = CONTAINING_RECORD(_RTL_FOREACH_LIST_CUR, TYPE__, FIELD__) \
|
||||
)
|
||||
|
||||
#define RTL_FOREACH_END }
|
||||
|
||||
NTSTATUS STDCALL LdrpRosTlsAction
|
||||
(
|
||||
IN ULONG Reason
|
||||
)
|
||||
{
|
||||
typedef struct _LDRP_TLS_ENTRY
|
||||
{
|
||||
LIST_ENTRY TlsList;
|
||||
PIMAGE_TLS_DIRECTORY TlsDir;
|
||||
PLDR_MODULE LdrModule;
|
||||
}
|
||||
LDRP_TLS_ENTRY, * PLDRP_TLS_ENTRY;
|
||||
|
||||
static LIST_ENTRY s_leTlsList;
|
||||
static SIZE_T s_nTlsImagesCount;
|
||||
|
||||
switch(Reason)
|
||||
{
|
||||
case DLL_PROCESS_ATTACH:
|
||||
{
|
||||
PPEB_LDR_DATA pLdr = NtCurrentPeb()->Ldr;
|
||||
|
||||
InitializeListHead(&s_leTlsList);
|
||||
s_nTlsImagesCount = 0;
|
||||
|
||||
RTL_FOREACH_LIST
|
||||
(
|
||||
&pLdr->InLoadOrderModuleList,
|
||||
LDR_MODULE,
|
||||
InLoadOrderModuleList
|
||||
)
|
||||
{
|
||||
SIZE_T nSize;
|
||||
PIMAGE_TLS_DIRECTORY pitdTlsDir = RtlImageDirectoryEntryToData
|
||||
(
|
||||
_RTL_ITEM->BaseAddress,
|
||||
TRUE,
|
||||
IMAGE_DIRECTORY_ENTRY_TLS,
|
||||
&nSize
|
||||
);
|
||||
|
||||
if(pitdTlsDir == NULL || nSize < sizeof(IMAGE_TLS_DIRECTORY)) continue;
|
||||
|
||||
PLDRP_TLS_ENTRY plteCur =
|
||||
RtlAllocateHeap(pPeb->ProcessHeap, 0, sizeof(LDRP_TLS_ENTRY));
|
||||
|
||||
if(plteCur == NULL) return STATUS_NO_MEMORY;
|
||||
|
||||
InsertTailList(&s_leTlsList, &plteCur->TlsList);
|
||||
|
||||
_RTL_ITEM->TlsIndex = s_nTlsImagesCount;
|
||||
++ s_nTlsImagesCount; /* TODO: check for out-of-bounds index */
|
||||
|
||||
plteCur->LdrModule = _RTL_ITEM;
|
||||
plteCur->TlsDir = pitdTlsDir;
|
||||
|
||||
*pitdTlsDir->AddressOfIndex = _RTL_ITEM->TlsIndex;
|
||||
}
|
||||
RTL_FOREACH_END;
|
||||
|
||||
RtlSetBits(pPeb->TlsBitmap, 0, s_nTlsImagesCount);
|
||||
|
||||
/* fall through */
|
||||
}
|
||||
|
||||
case DLL_THREAD_ATTACH:
|
||||
{
|
||||
RTL_FOREACH_LIST(&s_leTlsList, LDRP_TLS_ENTRY, TlsList)
|
||||
{
|
||||
PBYTE pcTlsData;
|
||||
PIMAGE_TLS_DIRECTORY pitdTlsDir = _RTL_ITEM->TlsDir;
|
||||
SIZE_T nInitDataSize =
|
||||
pitdTlsDir->EndAddressOfRawData - pitdTlsDir->StartAddressOfRawData;
|
||||
|
||||
PIMAGE_TLS_CALLBACK * pitcCallbacks =
|
||||
(PIMAGE_TLS_CALLBACK *)_RTL_ITEM->TlsDir->AddressOfCallbacks;
|
||||
|
||||
pcTlsData = RtlAllocateHeap
|
||||
(
|
||||
pPeb->ProcessHeap,
|
||||
0,
|
||||
nInitDataSize + pitdTlsDir->SizeOfZeroFill
|
||||
);
|
||||
|
||||
if(pcTlsData == NULL) return STATUS_NO_MEMORY;
|
||||
|
||||
pTeb->TlsSlots[_RTL_ITEM->LdrModule->TlsIndex] = pcTlsData;
|
||||
|
||||
RtlMoveMemory(pcTlsData, pitdTlsDir->StartAddressOfRawData, nInitDataSize);
|
||||
RtlZeroMemory(pcTlsData + nInitDataSize, pitdTlsDir->SizeOfZeroFill);
|
||||
|
||||
if(pitcCallbacks)
|
||||
for(; *pitcCallbacks; ++ pitcCallbacks)
|
||||
(*pitcCallbacks)(_RTL_ITEM->LdrModule->BaseAddress, Reason, NULL);
|
||||
}
|
||||
RTL_FOREACH_END;
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
case DLL_THREAD_DETACH:
|
||||
{
|
||||
}
|
||||
|
||||
case DLL_PROCESS_DETACH:
|
||||
{
|
||||
}
|
||||
|
||||
default: return STATUS_SUCCESS;
|
||||
}
|
||||
}
|
|
@ -18,8 +18,6 @@
|
|||
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
; SOFTWARE.
|
||||
|
||||
; DON'T USE THE "cpu" DIRECTIVE BECAUSE NASM 0.98 DOESN'T SUPPORT IT
|
||||
; cpu 486
|
||||
segment .text use32
|
||||
|
||||
global __SEHCleanHandlerEnvironment
|
||||
|
|
|
@ -18,8 +18,6 @@
|
|||
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
; SOFTWARE.
|
||||
|
||||
; DON'T USE THE "cpu" DIRECTIVE BECAUSE NASM 0.98 DOESN'T SUPPORT IT
|
||||
; cpu 486
|
||||
segment .text use32
|
||||
|
||||
; Note: the undecorated names are for Borland C++ (and possibly other compilers
|
||||
|
|
702
reactos/ntoskrnl/mm/elf.c
Normal file
702
reactos/ntoskrnl/mm/elf.c
Normal file
|
@ -0,0 +1,702 @@
|
|||
/* $Id: elf.c,v 1.2 2004/12/30 05:59:11 hyperion Exp $
|
||||
*/
|
||||
|
||||
#include <ntoskrnl.h>
|
||||
|
||||
/*#define NDEBUG*/
|
||||
#include <internal/debug.h>
|
||||
|
||||
#include <reactos/exeformat.h>
|
||||
|
||||
#ifndef __ELF_WORD_SIZE
|
||||
#error __ELF_WORD_SIZE must be defined
|
||||
#endif
|
||||
|
||||
#include <elf.h>
|
||||
|
||||
/* TODO: Intsafe should be made into a library, as it's generally useful */
|
||||
static __inline BOOLEAN Intsafe_CanAddULongPtr
|
||||
(
|
||||
IN ULONG_PTR Addend1,
|
||||
IN ULONG_PTR Addend2
|
||||
)
|
||||
{
|
||||
return Addend1 <= (MAXULONG_PTR - Addend2);
|
||||
}
|
||||
|
||||
#define Intsafe_CanAddSizeT Intsafe_CanAddULongPtr
|
||||
|
||||
static __inline BOOLEAN Intsafe_CanAddULong32
|
||||
(
|
||||
IN ULONG Addend1,
|
||||
IN ULONG Addend2
|
||||
)
|
||||
{
|
||||
return Addend1 <= (MAXULONG - Addend2);
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_AddULong32
|
||||
(
|
||||
OUT PULONG Result,
|
||||
IN ULONG Addend1,
|
||||
IN ULONG Addend2
|
||||
)
|
||||
{
|
||||
if(!Intsafe_CanAddULong32(Addend1, Addend2))
|
||||
return FALSE;
|
||||
|
||||
*Result = Addend1 + Addend2;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_CanAddULong64
|
||||
(
|
||||
IN ULONG64 Addend1,
|
||||
IN ULONG64 Addend2
|
||||
)
|
||||
{
|
||||
return Addend1 <= (((ULONG64)-1) - Addend2);
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_AddULong64
|
||||
(
|
||||
OUT PULONG64 Result,
|
||||
IN ULONG64 Addend1,
|
||||
IN ULONG64 Addend2
|
||||
)
|
||||
{
|
||||
if(!Intsafe_CanAddULong64(Addend1, Addend2))
|
||||
return FALSE;
|
||||
|
||||
*Result = Addend1 + Addend2;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_CanMulULong32
|
||||
(
|
||||
IN ULONG Factor1,
|
||||
IN ULONG Factor2
|
||||
)
|
||||
{
|
||||
return Factor1 <= (MAXULONG / Factor2);
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_MulULong32
|
||||
(
|
||||
OUT PULONG Result,
|
||||
IN ULONG Factor1,
|
||||
IN ULONG Factor2
|
||||
)
|
||||
{
|
||||
if(!Intsafe_CanMulULong32(Factor1, Factor2))
|
||||
return FALSE;
|
||||
|
||||
*Result = Factor1 * Factor2;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_CanOffsetPointer
|
||||
(
|
||||
IN CONST VOID * Pointer,
|
||||
IN SIZE_T Offset
|
||||
)
|
||||
{
|
||||
/* FIXME: (PVOID)MAXULONG_PTR isn't necessarily a valid address */
|
||||
return Intsafe_CanAddULongPtr((ULONG_PTR)Pointer, Offset);
|
||||
}
|
||||
|
||||
#if __ELF_WORD_SIZE == 32
|
||||
#define ElfFmtpAddSize Intsafe_AddULong32
|
||||
#define ElfFmtpReadAddr ElfFmtpReadULong
|
||||
#define ElfFmtpReadOff ElfFmtpReadULong
|
||||
#define ElfFmtpSafeReadAddr ElfFmtpSafeReadULong
|
||||
#define ElfFmtpSafeReadOff ElfFmtpSafeReadULong
|
||||
#define ElfFmtpSafeReadSize ElfFmtpSafeReadULong
|
||||
#elif __ELF_WORD_SIZE == 64
|
||||
#define ElfFmtpAddSize Intsafe_AddULong64
|
||||
#define ElfFmtpReadAddr ElfFmtpReadULong64
|
||||
#define ElfFmtpReadOff ElfFmtpReadULong64
|
||||
#define ElfFmtpSafeReadAddr ElfFmtpSafeReadULong64
|
||||
#define ElfFmtpSafeReadOff ElfFmtpSafeReadULong64
|
||||
#define ElfFmtpSafeReadSize ElfFmtpSafeReadULong64
|
||||
#endif
|
||||
|
||||
/* TODO: these are standard DDK/PSDK macros */
|
||||
#define RtlRetrieveUlonglong(DST_, SRC_) \
|
||||
(RtlCopyMemory((DST_), (SRC_), sizeof(ULONG64)))
|
||||
|
||||
#ifndef RTL_FIELD_SIZE
|
||||
#define RTL_FIELD_SIZE(TYPE_, FIELD_) (sizeof(((TYPE_ *)0)->FIELD_))
|
||||
#endif
|
||||
|
||||
#ifndef RTL_SIZEOF_THROUGH_FIELD
|
||||
#define RTL_SIZEOF_THROUGH_FIELD(TYPE_, FIELD_) \
|
||||
(FIELD_OFFSET(TYPE_, FIELD_) + RTL_FIELD_SIZE(TYPE_, FIELD_))
|
||||
#endif
|
||||
|
||||
#ifndef RTL_CONTAINS_FIELD
|
||||
#define RTL_CONTAINS_FIELD(P_, SIZE_, FIELD_) \
|
||||
((((char *)(P_)) + (SIZE_)) > (((char *)(&((P_)->FIELD_))) + sizeof((P_)->FIELD_)))
|
||||
#endif
|
||||
|
||||
#define ELFFMT_FIELDS_EQUAL(TYPE1_, TYPE2_, FIELD_) \
|
||||
( \
|
||||
(FIELD_OFFSET(TYPE1_, FIELD_) == FIELD_OFFSET(TYPE2_, FIELD_)) && \
|
||||
(RTL_FIELD_SIZE(TYPE1_, FIELD_) == RTL_FIELD_SIZE(TYPE2_, FIELD_)) \
|
||||
)
|
||||
|
||||
#define ELFFMT_MAKE_ULONG64(BYTE1_, BYTE2_, BYTE3_, BYTE4_, BYTE5_, BYTE6_, BYTE7_, BYTE8_) \
|
||||
( \
|
||||
(((ULONG64)ELFFMT_MAKE_ULONG(BYTE1_, BYTE2_, BYTE3_, BYTE4_)) << 0) | \
|
||||
(((ULONG64)ELFFMT_MAKE_ULONG(BYTE5_, BYTE6_, BYTE7_, BYTE8_)) << 32) \
|
||||
)
|
||||
|
||||
#define ELFFMT_MAKE_ULONG(BYTE1_, BYTE2_, BYTE3_, BYTE4_) \
|
||||
( \
|
||||
(((ULONG)ELFFMT_MAKE_USHORT(BYTE1_, BYTE2_)) << 0) | \
|
||||
(((ULONG)ELFFMT_MAKE_USHORT(BYTE3_, BYTE4_)) << 16) \
|
||||
)
|
||||
|
||||
#define ELFFMT_MAKE_USHORT(BYTE1_, BYTE2_) \
|
||||
( \
|
||||
(((USHORT)(BYTE1_)) << 0) | \
|
||||
(((USHORT)(BYTE2_)) << 8) \
|
||||
)
|
||||
|
||||
static __inline ULONG64 ElfFmtpReadULong64
|
||||
(
|
||||
IN ULONG64 Input,
|
||||
IN ULONG DataType
|
||||
)
|
||||
{
|
||||
PBYTE p;
|
||||
|
||||
if(DataType == ELF_TARG_DATA)
|
||||
return Input;
|
||||
|
||||
p = (PBYTE)&Input;
|
||||
|
||||
switch(DataType)
|
||||
{
|
||||
case ELFDATA2LSB: return ELFFMT_MAKE_ULONG64(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
|
||||
case ELFDATA2MSB: return ELFFMT_MAKE_ULONG64(p[7], p[6], p[5], p[4], p[3], p[2], p[1], p[0]);
|
||||
}
|
||||
|
||||
ASSERT(FALSE);
|
||||
return (ULONG64)-1;
|
||||
}
|
||||
|
||||
static __inline ULONG ElfFmtpReadULong
|
||||
(
|
||||
IN ULONG Input,
|
||||
IN ULONG DataType
|
||||
)
|
||||
{
|
||||
PBYTE p;
|
||||
|
||||
if(DataType == ELF_TARG_DATA)
|
||||
return Input;
|
||||
|
||||
p = (PBYTE)&Input;
|
||||
|
||||
switch(DataType)
|
||||
{
|
||||
case ELFDATA2LSB: return ELFFMT_MAKE_ULONG(p[0], p[1], p[2], p[3]);
|
||||
case ELFDATA2MSB: return ELFFMT_MAKE_ULONG(p[3], p[2], p[1], p[0]);
|
||||
}
|
||||
|
||||
ASSERT(FALSE);
|
||||
return (ULONG)-1;
|
||||
}
|
||||
|
||||
static __inline USHORT ElfFmtpReadUShort
|
||||
(
|
||||
IN USHORT Input,
|
||||
IN ULONG DataType
|
||||
)
|
||||
{
|
||||
PBYTE p;
|
||||
|
||||
if(DataType == ELF_TARG_DATA)
|
||||
return Input;
|
||||
|
||||
p = (PBYTE)&Input;
|
||||
|
||||
switch(DataType)
|
||||
{
|
||||
case ELFDATA2LSB: return ELFFMT_MAKE_USHORT(p[0], p[1]);
|
||||
case ELFDATA2MSB: return ELFFMT_MAKE_USHORT(p[1], p[0]);
|
||||
}
|
||||
|
||||
ASSERT(FALSE);
|
||||
return (USHORT)-1;
|
||||
}
|
||||
|
||||
static __inline ULONG64 ElfFmtpSafeReadULong64
|
||||
(
|
||||
IN CONST ULONG64 * Input,
|
||||
IN ULONG DataType
|
||||
)
|
||||
{
|
||||
PBYTE p;
|
||||
ULONG nSafeInput;
|
||||
|
||||
RtlRetrieveUlonglong(&nSafeInput, Input);
|
||||
|
||||
p = (PBYTE)&nSafeInput;
|
||||
|
||||
switch(DataType)
|
||||
{
|
||||
case ELFDATA2LSB: return ELFFMT_MAKE_ULONG64(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
|
||||
case ELFDATA2MSB: return ELFFMT_MAKE_ULONG64(p[7], p[6], p[5], p[4], p[3], p[2], p[1], p[0]);
|
||||
}
|
||||
|
||||
ASSERT(FALSE);
|
||||
return (ULONG64)-1;
|
||||
}
|
||||
|
||||
static __inline ULONG ElfFmtpSafeReadULong
|
||||
(
|
||||
IN CONST ULONG * Input,
|
||||
IN ULONG DataType
|
||||
)
|
||||
{
|
||||
PBYTE p;
|
||||
ULONG nSafeInput;
|
||||
|
||||
RtlRetrieveUlong(&nSafeInput, Input);
|
||||
|
||||
if(DataType == ELF_TARG_DATA)
|
||||
return nSafeInput;
|
||||
|
||||
p = (PBYTE)&nSafeInput;
|
||||
|
||||
switch(DataType)
|
||||
{
|
||||
case ELFDATA2LSB: return ELFFMT_MAKE_ULONG(p[0], p[1], p[2], p[3]);
|
||||
case ELFDATA2MSB: return ELFFMT_MAKE_ULONG(p[3], p[2], p[1], p[0]);
|
||||
}
|
||||
|
||||
ASSERT(FALSE);
|
||||
return (ULONG)-1;
|
||||
}
|
||||
|
||||
static __inline BOOLEAN ElfFmtpIsPowerOf2(IN Elf_Addr Number)
|
||||
{
|
||||
if(Number == 0)
|
||||
return FALSE;
|
||||
|
||||
while((Number % 2) == 0)
|
||||
Number /= 2;
|
||||
|
||||
return Number == 1;
|
||||
}
|
||||
|
||||
static __inline Elf_Addr ElfFmtpModPow2
|
||||
(
|
||||
IN Elf_Addr Address,
|
||||
IN Elf_Addr Alignment
|
||||
)
|
||||
{
|
||||
ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Size));
|
||||
ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Off));
|
||||
ASSERT(ElfFmtpIsPowerOf2(Alignment));
|
||||
return Address & (Alignment - 1);
|
||||
}
|
||||
|
||||
static __inline Elf_Addr ElfFmtpAlignDown
|
||||
(
|
||||
IN Elf_Addr Address,
|
||||
IN Elf_Addr Alignment
|
||||
)
|
||||
{
|
||||
ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Size));
|
||||
ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Off));
|
||||
ASSERT(ElfFmtpIsPowerOf2(Alignment));
|
||||
return Address & ~(Alignment - 1);
|
||||
}
|
||||
|
||||
static __inline BOOLEAN ElfFmtpAlignUp
|
||||
(
|
||||
OUT Elf_Addr * AlignedAddress,
|
||||
IN Elf_Addr Address,
|
||||
IN Elf_Addr Alignment
|
||||
)
|
||||
{
|
||||
Elf_Addr nExcess = ElfFmtpModPow2(Address, Alignment);
|
||||
|
||||
if(nExcess == 0)
|
||||
{
|
||||
*AlignedAddress = Address;
|
||||
return nExcess == 0;
|
||||
}
|
||||
else
|
||||
return ElfFmtpAddSize(AlignedAddress, Address, Alignment - nExcess);
|
||||
}
|
||||
|
||||
/*
|
||||
References:
|
||||
[1] Tool Interface Standards (TIS) Committee, "Executable and Linking Format
|
||||
(ELF) Specification", Version 1.2
|
||||
*/
|
||||
NTSTATUS NTAPI
|
||||
#if __ELF_WORD_SIZE == 32
|
||||
Elf32FmtCreateSection
|
||||
#elif __ELF_WORD_SIZE == 64
|
||||
Elf64FmtCreateSection
|
||||
#endif
|
||||
(
|
||||
IN CONST VOID * FileHeader,
|
||||
IN SIZE_T FileHeaderSize,
|
||||
IN PVOID File,
|
||||
OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
|
||||
OUT PULONG Flags,
|
||||
IN PEXEFMT_CB_READ_FILE ReadFileCb,
|
||||
IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
|
||||
)
|
||||
{
|
||||
NTSTATUS nStatus;
|
||||
const Elf_Ehdr * pehHeader;
|
||||
const Elf_Phdr * pphPHdrs;
|
||||
BOOLEAN fPageAligned;
|
||||
ULONG nData;
|
||||
ULONG nPHdrCount;
|
||||
ULONG cbPHdrSize;
|
||||
Elf_Off cbPHdrOffset;
|
||||
PVOID pBuffer;
|
||||
PMM_SECTION_SEGMENT pssSegments;
|
||||
Elf_Addr nImageBase;
|
||||
Elf_Addr nEntryPoint;
|
||||
ULONG nPrevVirtualEndOfSegment;
|
||||
ULONG i;
|
||||
ULONG j;
|
||||
|
||||
(void)Intsafe_AddULong64;
|
||||
(void)Intsafe_MulULong32;
|
||||
(void)ElfFmtpReadULong64;
|
||||
(void)ElfFmtpSafeReadULong64;
|
||||
(void)ElfFmtpReadULong;
|
||||
|
||||
#define DIE(ARGS_) { DPRINT ARGS_; goto l_Return; }
|
||||
|
||||
pBuffer = NULL;
|
||||
|
||||
nStatus = STATUS_INVALID_IMAGE_FORMAT;
|
||||
|
||||
/* Ensure the file contains the full header */
|
||||
/*
|
||||
EXEFMT_LOAD_HEADER_SIZE is 8KB: enough to contain an ELF header (at least in
|
||||
all the classes defined as of December 2004). If FileHeaderSize is less than
|
||||
sizeof(Elf_Ehdr), it means the file itself is small enough not to contain a
|
||||
full ELF header
|
||||
*/
|
||||
ASSERT(sizeof(Elf_Ehdr) <= EXEFMT_LOAD_HEADER_SIZE);
|
||||
|
||||
if(FileHeaderSize < sizeof(Elf_Ehdr))
|
||||
DIE(("The file is truncated, doesn't contain the full header\n"));
|
||||
|
||||
pehHeader = FileHeader;
|
||||
ASSERT(((ULONG_PTR)pehHeader % TYPE_ALIGNMENT(Elf_Ehdr)) == 0);
|
||||
|
||||
nData = pehHeader->e_ident[EI_DATA];
|
||||
|
||||
/* Validate the header */
|
||||
if(ElfFmtpReadUShort(pehHeader->e_ehsize, nData) < sizeof(Elf_Ehdr))
|
||||
DIE(("Inconsistent value for e_ehsize\n"));
|
||||
|
||||
/* Calculate size and offset of the program headers */
|
||||
cbPHdrSize = ElfFmtpReadUShort(pehHeader->e_phentsize, nData);
|
||||
|
||||
if(cbPHdrSize != sizeof(Elf_Phdr))
|
||||
DIE(("Inconsistent value for e_phentsize\n"));
|
||||
|
||||
/* MAXUSHORT * MAXUSHORT < MAXULONG */
|
||||
nPHdrCount = ElfFmtpReadUShort(pehHeader->e_phnum, nData);
|
||||
ASSERT(Intsafe_CanMulULong32(cbPHdrSize, nPHdrCount));
|
||||
cbPHdrSize *= nPHdrCount;
|
||||
|
||||
cbPHdrOffset = ElfFmtpReadOff(pehHeader->e_phoff, nData);
|
||||
|
||||
/* The initial header doesn't contain the program headers */
|
||||
if(cbPHdrOffset > FileHeaderSize || cbPHdrSize > (FileHeaderSize - cbPHdrOffset))
|
||||
{
|
||||
NTSTATUS nReadStatus;
|
||||
LARGE_INTEGER lnOffset;
|
||||
PVOID pData;
|
||||
ULONG cbReadSize;
|
||||
|
||||
/* Will worry about this when ELF128 comes */
|
||||
ASSERT(sizeof(cbPHdrOffset) <= sizeof(lnOffset.QuadPart));
|
||||
|
||||
lnOffset.QuadPart = (LONG64)cbPHdrOffset;
|
||||
|
||||
/*
|
||||
We can't support executable files larger than 8 Exabytes - it's a limitation
|
||||
of the I/O system (only 63-bit offsets are supported). Quote:
|
||||
|
||||
[...] the total amount of printed material in the world is estimated to be
|
||||
around a fifth of an exabyte. [...] [Source: Wikipedia]
|
||||
*/
|
||||
if(lnOffset.u.HighPart < 0)
|
||||
DIE(("The program header is too far into the file\n"));
|
||||
|
||||
nReadStatus = ReadFileCb
|
||||
(
|
||||
File,
|
||||
&lnOffset,
|
||||
cbPHdrSize,
|
||||
&pData,
|
||||
&pBuffer,
|
||||
&cbReadSize
|
||||
);
|
||||
|
||||
if(!NT_SUCCESS(nReadStatus))
|
||||
{
|
||||
nStatus = nReadStatus;
|
||||
DIE(("ReadFile failed, status %08X\n", nStatus));
|
||||
}
|
||||
|
||||
ASSERT(pData);
|
||||
ASSERT(pBuffer);
|
||||
ASSERT(Intsafe_CanOffsetPointer(pData, cbReadSize));
|
||||
|
||||
if(cbReadSize < cbPHdrSize)
|
||||
DIE(("The file didn't contain the program headers\n"));
|
||||
|
||||
/* Force the buffer to be aligned */
|
||||
if((ULONG_PTR)pData % TYPE_ALIGNMENT(Elf_Phdr))
|
||||
{
|
||||
ASSERT(((ULONG_PTR)pBuffer % TYPE_ALIGNMENT(Elf_Phdr)) == 0);
|
||||
RtlMoveMemory(pBuffer, pData, cbPHdrSize);
|
||||
pphPHdrs = pBuffer;
|
||||
}
|
||||
else
|
||||
pphPHdrs = pData;
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT(Intsafe_CanAddSizeT(cbPHdrOffset, 0));
|
||||
ASSERT(Intsafe_CanOffsetPointer(FileHeader, cbPHdrOffset));
|
||||
pphPHdrs = (PVOID)((ULONG_PTR)FileHeader + (ULONG_PTR)cbPHdrOffset);
|
||||
}
|
||||
|
||||
/* Allocate the segments */
|
||||
pssSegments = AllocateSegmentsCb(nPHdrCount);
|
||||
|
||||
if(pssSegments == NULL)
|
||||
{
|
||||
nStatus = STATUS_INSUFFICIENT_RESOURCES;
|
||||
DIE(("Out of memory\n"));
|
||||
}
|
||||
|
||||
ImageSectionObject->Segments = pssSegments;
|
||||
|
||||
fPageAligned = TRUE;
|
||||
|
||||
/* Fill in the segments */
|
||||
for(i = 0, j = 0; i < nPHdrCount; ++ i)
|
||||
{
|
||||
switch(ElfFmtpSafeReadULong(&pphPHdrs[i].p_type, nData))
|
||||
{
|
||||
case PT_LOAD:
|
||||
{
|
||||
static const ULONG ProgramHeaderFlagsToProtect[8] =
|
||||
{
|
||||
PAGE_NOACCESS, /* 0 */
|
||||
PAGE_EXECUTE_READ, /* PF_X */
|
||||
PAGE_READWRITE, /* PF_W */
|
||||
PAGE_EXECUTE_READWRITE, /* PF_X | PF_W */
|
||||
PAGE_READONLY, /* PF_R */
|
||||
PAGE_EXECUTE_READ, /* PF_X | PF_R */
|
||||
PAGE_READWRITE, /* PF_W | PF_R */
|
||||
PAGE_EXECUTE_READWRITE /* PF_X | PF_W | PF_R */
|
||||
};
|
||||
|
||||
Elf_Size nAlignment;
|
||||
Elf_Off nFileOffset;
|
||||
Elf_Addr nVirtualAddr;
|
||||
Elf_Size nAdj;
|
||||
Elf_Size nVirtualSize;
|
||||
Elf_Size nFileSize;
|
||||
|
||||
ASSERT(j <= nPHdrCount);
|
||||
|
||||
/* Retrieve and validate the segment alignment */
|
||||
nAlignment = ElfFmtpSafeReadSize(&pphPHdrs[i].p_align, nData);
|
||||
|
||||
if(nAlignment == 0)
|
||||
nAlignment = 1;
|
||||
else if(!ElfFmtpIsPowerOf2(nAlignment))
|
||||
DIE(("Alignment of loadable segment isn't a power of 2\n"));
|
||||
|
||||
if(nAlignment < PAGE_SIZE)
|
||||
fPageAligned = FALSE;
|
||||
|
||||
/* Retrieve the addresses and calculate the adjustment */
|
||||
nFileOffset = ElfFmtpSafeReadOff(&pphPHdrs[i].p_offset, nData);
|
||||
nVirtualAddr = ElfFmtpSafeReadAddr(&pphPHdrs[i].p_vaddr, nData);
|
||||
|
||||
nAdj = ElfFmtpModPow2(nFileOffset, nAlignment);
|
||||
|
||||
if(nAdj != ElfFmtpModPow2(nVirtualAddr, nAlignment))
|
||||
DIE(("File and memory address of loadable segment not congruent modulo alignment\n"));
|
||||
|
||||
/* Retrieve, adjust and align the file size and memory size */
|
||||
if(!ElfFmtpAddSize(&nFileSize, ElfFmtpSafeReadSize(&pphPHdrs[i].p_filesz, nData), nAdj))
|
||||
DIE(("Can't adjust the file size of loadable segment\n"));
|
||||
|
||||
if(!ElfFmtpAddSize(&nVirtualSize, ElfFmtpSafeReadSize(&pphPHdrs[i].p_memsz, nData), nAdj))
|
||||
DIE(("Can't adjust the memory size of lodable segment\n"));
|
||||
|
||||
if(!ElfFmtpAlignUp(&nVirtualSize, nVirtualSize, nAlignment))
|
||||
DIE(("Can't align the memory size of lodable segment\n"));
|
||||
|
||||
if(nFileSize > nVirtualSize)
|
||||
nFileSize = nVirtualSize;
|
||||
|
||||
if(nVirtualSize > MAXULONG)
|
||||
DIE(("Virtual image larger than 4GB\n"));
|
||||
|
||||
ASSERT(nFileSize <= MAXULONG);
|
||||
|
||||
pssSegments[j].Length = (ULONG)(nVirtualSize & 0xFFFFFFFF);
|
||||
pssSegments[j].RawLength = (ULONG)(nFileSize & 0xFFFFFFFF);
|
||||
|
||||
/* File offset */
|
||||
nFileOffset = ElfFmtpAlignDown(nFileOffset, nAlignment);
|
||||
|
||||
#if __ELF_WORD_SIZE >= 64
|
||||
ASSERT(sizeof(nFileOffset) == sizeof(LONG64));
|
||||
|
||||
if(((LONG64)nFileOffset) < 0)
|
||||
DIE(("File offset of loadable segment is too large\n"));
|
||||
#endif
|
||||
|
||||
pssSegments[j].FileOffset = (LONG64)nFileOffset;
|
||||
|
||||
/* Virtual address */
|
||||
nVirtualAddr = ElfFmtpAlignDown(nVirtualAddr, nAlignment);
|
||||
|
||||
if(j == 0)
|
||||
{
|
||||
/* First segment: its address is the base address of the image */
|
||||
nImageBase = nVirtualAddr;
|
||||
pssSegments[j].VirtualAddress = 0;
|
||||
|
||||
/* Several places make this assumption */
|
||||
if(pssSegments[j].FileOffset != 0)
|
||||
DIE(("First loadable segment doesn't contain the ELF header\n"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Elf_Size nVirtualOffset;
|
||||
|
||||
/* Other segment: store the offset from the base address */
|
||||
if(nVirtualAddr <= nImageBase)
|
||||
DIE(("Loadable segments are not sorted\n"));
|
||||
|
||||
nVirtualOffset = nVirtualAddr - nImageBase;
|
||||
|
||||
if(nVirtualOffset > MAXULONG)
|
||||
DIE(("Virtual image larger than 4GB\n"));
|
||||
|
||||
pssSegments[j].VirtualAddress = (ULONG)(nVirtualOffset & 0xFFFFFFFF);
|
||||
|
||||
if(pssSegments[j].VirtualAddress != nPrevVirtualEndOfSegment)
|
||||
DIE(("Loadable segments are not sorted and contiguous\n"));
|
||||
}
|
||||
|
||||
/* Memory protection */
|
||||
pssSegments[j].Protection = ProgramHeaderFlagsToProtect
|
||||
[
|
||||
ElfFmtpSafeReadULong(&pphPHdrs[i].p_flags, nData) & (PF_R | PF_W | PF_X)
|
||||
];
|
||||
|
||||
/* Characteristics */
|
||||
/*
|
||||
TODO: need to add support for the shared, non-pageable, non-cacheable and
|
||||
discardable attributes. This involves extensions to the ELF format, so it's
|
||||
nothing to be taken lightly
|
||||
*/
|
||||
if(pssSegments[j].Protection & PAGE_IS_EXECUTABLE)
|
||||
{
|
||||
ImageSectionObject->Executable = TRUE;
|
||||
pssSegments[j].Characteristics = IMAGE_SCN_CNT_CODE;
|
||||
}
|
||||
else if(pssSegments[j].RawLength == 0)
|
||||
pssSegments[j].Characteristics = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
|
||||
else
|
||||
pssSegments[j].Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA;
|
||||
|
||||
/* Copy-on-write */
|
||||
pssSegments[j].WriteCopy = TRUE;
|
||||
|
||||
if(!Intsafe_AddULong32(&nPrevVirtualEndOfSegment, pssSegments[j].VirtualAddress, pssSegments[j].Length))
|
||||
DIE(("Virtual image larger than 4GB\n"));
|
||||
|
||||
++ j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(j == 0)
|
||||
DIE(("No loadable segments\n"));
|
||||
|
||||
ImageSectionObject->NrSegments = j;
|
||||
|
||||
*Flags =
|
||||
EXEFMT_LOAD_ASSUME_SEGMENTS_SORTED |
|
||||
EXEFMT_LOAD_ASSUME_SEGMENTS_NO_OVERLAP;
|
||||
|
||||
if(fPageAligned)
|
||||
*Flags |= EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED;
|
||||
|
||||
nEntryPoint = ElfFmtpReadAddr(pehHeader->e_entry, nData);
|
||||
|
||||
if(nEntryPoint < nImageBase || nEntryPoint - nImageBase > nPrevVirtualEndOfSegment)
|
||||
DIE(("Entry point not within the virtual image\n"));
|
||||
|
||||
ASSERT(nEntryPoint >= nImageBase);
|
||||
ASSERT((nEntryPoint - nImageBase) <= MAXULONG);
|
||||
ImageSectionObject->EntryPoint = nEntryPoint - nImageBase;
|
||||
|
||||
/* TODO: support Wine executables and read these values from nt_headers */
|
||||
ImageSectionObject->ImageCharacteristics |=
|
||||
IMAGE_FILE_EXECUTABLE_IMAGE |
|
||||
IMAGE_FILE_LINE_NUMS_STRIPPED |
|
||||
IMAGE_FILE_LOCAL_SYMS_STRIPPED |
|
||||
(nImageBase > MAXULONG ? IMAGE_FILE_LARGE_ADDRESS_AWARE : 0) |
|
||||
IMAGE_FILE_DEBUG_STRIPPED;
|
||||
|
||||
if(nData == ELFDATA2LSB)
|
||||
ImageSectionObject->ImageCharacteristics |= IMAGE_FILE_BYTES_REVERSED_LO;
|
||||
else if(nData == ELFDATA2MSB)
|
||||
ImageSectionObject->ImageCharacteristics |= IMAGE_FILE_BYTES_REVERSED_HI;
|
||||
|
||||
/* Base address outside the possible address space */
|
||||
if(nImageBase > MAXULONG_PTR)
|
||||
ImageSectionObject->ImageBase = EXEFMT_LOAD_BASE_NONE;
|
||||
/* Position-independent image, base address doesn't matter */
|
||||
else if(nImageBase == 0)
|
||||
ImageSectionObject->ImageBase = EXEFMT_LOAD_BASE_ANY;
|
||||
/* Use the specified base address */
|
||||
else
|
||||
ImageSectionObject->ImageBase = (ULONG_PTR)nImageBase;
|
||||
|
||||
/* safest bet */
|
||||
ImageSectionObject->Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
|
||||
ImageSectionObject->MinorSubsystemVersion = 0;
|
||||
ImageSectionObject->MajorSubsystemVersion = 4;
|
||||
|
||||
/* Success, at last */
|
||||
nStatus = STATUS_SUCCESS;
|
||||
|
||||
l_Return:
|
||||
if(pBuffer)
|
||||
ExFreePool(pBuffer);
|
||||
|
||||
return nStatus;
|
||||
}
|
||||
|
||||
/* EOF */
|
145
reactos/ntoskrnl/mm/elf32.c
Normal file
145
reactos/ntoskrnl/mm/elf32.c
Normal file
|
@ -0,0 +1,145 @@
|
|||
#define __ELF_WORD_SIZE 32
|
||||
#include "elf.c"
|
||||
|
||||
extern NTSTATUS NTAPI Elf64FmtCreateSection
|
||||
(
|
||||
IN CONST VOID * FileHeader,
|
||||
IN SIZE_T FileHeaderSize,
|
||||
IN PVOID File,
|
||||
OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
|
||||
OUT PULONG Flags,
|
||||
IN PEXEFMT_CB_READ_FILE ReadFileCb,
|
||||
IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
|
||||
);
|
||||
|
||||
NTSTATUS NTAPI ElfFmtCreateSection
|
||||
(
|
||||
IN CONST VOID * FileHeader,
|
||||
IN SIZE_T FileHeaderSize,
|
||||
IN PVOID File,
|
||||
OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
|
||||
OUT PULONG Flags,
|
||||
IN PEXEFMT_CB_READ_FILE ReadFileCb,
|
||||
IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
|
||||
)
|
||||
{
|
||||
ULONG nDataType;
|
||||
const Elf32_Ehdr * pehTempHeader;
|
||||
|
||||
ASSERT(FileHeader);
|
||||
ASSERT(FileHeaderSize > 0);
|
||||
ASSERT(Intsafe_CanOffsetPointer(FileHeader, FileHeaderSize));
|
||||
ASSERT(File);
|
||||
ASSERT(ImageSectionObject);
|
||||
ASSERT(Flags);
|
||||
ASSERT(ReadFileCb);
|
||||
ASSERT(AllocateSegmentsCb);
|
||||
|
||||
pehTempHeader = FileHeader;
|
||||
ASSERT(((ULONG_PTR)pehTempHeader % TYPE_ALIGNMENT(Elf32_Ehdr)) == 0);
|
||||
ASSERT(((ULONG_PTR)pehTempHeader % TYPE_ALIGNMENT(Elf64_Ehdr)) == 0);
|
||||
|
||||
ASSERT(ELFFMT_FIELDS_EQUAL(Elf32_Ehdr, Elf64_Ehdr, e_ident));
|
||||
|
||||
/* File too small to be identified */
|
||||
if(!RTL_CONTAINS_FIELD(pehTempHeader, FileHeaderSize, e_ident[EI_MAG3]))
|
||||
return STATUS_ROS_EXEFMT_UNKNOWN_FORMAT;
|
||||
|
||||
/* Not an ELF file */
|
||||
if
|
||||
(
|
||||
pehTempHeader->e_ident[EI_MAG0] != ELFMAG0 ||
|
||||
pehTempHeader->e_ident[EI_MAG1] != ELFMAG1 ||
|
||||
pehTempHeader->e_ident[EI_MAG2] != ELFMAG2 ||
|
||||
pehTempHeader->e_ident[EI_MAG3] != ELFMAG3
|
||||
)
|
||||
return STATUS_ROS_EXEFMT_UNKNOWN_FORMAT;
|
||||
|
||||
/* Validate the data type */
|
||||
nDataType = pehTempHeader->e_ident[EI_DATA];
|
||||
|
||||
switch(nDataType)
|
||||
{
|
||||
case ELFDATA2LSB:
|
||||
case ELFDATA2MSB:
|
||||
break;
|
||||
|
||||
default:
|
||||
return STATUS_INVALID_IMAGE_FORMAT;
|
||||
}
|
||||
|
||||
/* Validate the version */
|
||||
ASSERT(ELFFMT_FIELDS_EQUAL(Elf32_Ehdr, Elf64_Ehdr, e_version));
|
||||
|
||||
if
|
||||
(
|
||||
pehTempHeader->e_ident[EI_VERSION] != EV_CURRENT ||
|
||||
ElfFmtpReadULong(pehTempHeader->e_version, nDataType) != EV_CURRENT
|
||||
)
|
||||
return STATUS_INVALID_IMAGE_FORMAT;
|
||||
|
||||
/* Validate the file type */
|
||||
ASSERT(ELFFMT_FIELDS_EQUAL(Elf32_Ehdr, Elf64_Ehdr, e_type));
|
||||
|
||||
switch(ElfFmtpReadUShort(pehTempHeader->e_type, nDataType))
|
||||
{
|
||||
case ET_DYN: ImageSectionObject->ImageCharacteristics |= IMAGE_FILE_DLL;
|
||||
case ET_EXEC: break;
|
||||
default: return STATUS_INVALID_IMAGE_FORMAT;
|
||||
}
|
||||
|
||||
/* Convert the target machine */
|
||||
ASSERT(ELFFMT_FIELDS_EQUAL(Elf32_Ehdr, Elf64_Ehdr, e_machine));
|
||||
ASSERT(ImageSectionObject->Machine == IMAGE_FILE_MACHINE_UNKNOWN);
|
||||
|
||||
switch(ElfFmtpReadUShort(pehTempHeader->e_machine, nDataType))
|
||||
{
|
||||
case EM_386: ImageSectionObject->Machine = IMAGE_FILE_MACHINE_I386; break;
|
||||
case EM_MIPS_RS3_LE: ImageSectionObject->Machine = IMAGE_FILE_MACHINE_R3000; break;
|
||||
|
||||
#if 0
|
||||
/* TODO: need to read e_flags for full identification */
|
||||
case EM_SH: break;
|
||||
#endif
|
||||
|
||||
case EM_ARM: ImageSectionObject->Machine = IMAGE_FILE_MACHINE_ARM; break;
|
||||
case EM_PPC: ImageSectionObject->Machine = IMAGE_FILE_MACHINE_POWERPC; break;
|
||||
case EM_IA_64: ImageSectionObject->Machine = IMAGE_FILE_MACHINE_IA64; break;
|
||||
case EM_ALPHA: ImageSectionObject->Machine = IMAGE_FILE_MACHINE_AXP64; break;
|
||||
case EM_X86_64: ImageSectionObject->Machine = IMAGE_FILE_MACHINE_AMD64; break;
|
||||
case EM_M32R: ImageSectionObject->Machine = IMAGE_FILE_MACHINE_M32R; break;
|
||||
}
|
||||
|
||||
/* Call the appropriate handler for the class-specific fields */
|
||||
switch(pehTempHeader->e_ident[EI_CLASS])
|
||||
{
|
||||
case ELFCLASS32:
|
||||
return Elf32FmtCreateSection
|
||||
(
|
||||
FileHeader,
|
||||
FileHeaderSize,
|
||||
File,
|
||||
ImageSectionObject,
|
||||
Flags,
|
||||
ReadFileCb,
|
||||
AllocateSegmentsCb
|
||||
);
|
||||
|
||||
case ELFCLASS64:
|
||||
return Elf64FmtCreateSection
|
||||
(
|
||||
FileHeader,
|
||||
FileHeaderSize,
|
||||
File,
|
||||
ImageSectionObject,
|
||||
Flags,
|
||||
ReadFileCb,
|
||||
AllocateSegmentsCb
|
||||
);
|
||||
}
|
||||
|
||||
/* Unknown class */
|
||||
return STATUS_INVALID_IMAGE_FORMAT;
|
||||
}
|
||||
|
||||
/* EOF */
|
2
reactos/ntoskrnl/mm/elf64.c
Normal file
2
reactos/ntoskrnl/mm/elf64.c
Normal file
|
@ -0,0 +1,2 @@
|
|||
#define __ELF_WORD_SIZE 64
|
||||
#include "elf.c"
|
745
reactos/ntoskrnl/mm/pe.c
Normal file
745
reactos/ntoskrnl/mm/pe.c
Normal file
|
@ -0,0 +1,745 @@
|
|||
/*
|
||||
* ReactOS kernel
|
||||
* Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
/* $Id: pe.c,v 1.2 2004/12/30 05:59:11 hyperion Exp $
|
||||
*
|
||||
* PROJECT: ReactOS kernel
|
||||
* FILE: ntoskrnl/mm/pe.c
|
||||
* PURPOSE: Loader for PE executables
|
||||
* PROGRAMMER: KJK::Hyperion <hackbunny@reactos.com>
|
||||
* UPDATE HISTORY:
|
||||
* 2004-12-06 Created
|
||||
* 2004-12-09 Compiles
|
||||
* 2004-12-26 Actually works, several checks relaxed to support
|
||||
* the majority of existing executables, corrected
|
||||
* the alignment helper functions, debug messages to
|
||||
* explain failure.
|
||||
*/
|
||||
|
||||
/* INCLUDES *****************************************************************/
|
||||
|
||||
#include <ntoskrnl.h>
|
||||
|
||||
#define NDEBUG
|
||||
#include <internal/debug.h>
|
||||
|
||||
#include <reactos/exeformat.h>
|
||||
|
||||
static ULONG SectionCharacteristicsToProtect[16] =
|
||||
{
|
||||
PAGE_NOACCESS, /* 0 = NONE */
|
||||
PAGE_NOACCESS, /* 1 = SHARED */
|
||||
PAGE_EXECUTE, /* 2 = EXECUTABLE */
|
||||
PAGE_EXECUTE, /* 3 = EXECUTABLE, SHARED */
|
||||
PAGE_READONLY, /* 4 = READABLE */
|
||||
PAGE_READONLY, /* 5 = READABLE, SHARED */
|
||||
PAGE_EXECUTE_READ, /* 6 = READABLE, EXECUTABLE */
|
||||
PAGE_EXECUTE_READ, /* 7 = READABLE, EXECUTABLE, SHARED */
|
||||
/*
|
||||
* FIXME? do we really need the WriteCopy field in segments? can't we use
|
||||
* PAGE_WRITECOPY here?
|
||||
*/
|
||||
PAGE_READWRITE, /* 8 = WRITABLE */
|
||||
PAGE_READWRITE, /* 9 = WRITABLE, SHARED */
|
||||
PAGE_EXECUTE_READWRITE, /* 10 = WRITABLE, EXECUTABLE */
|
||||
PAGE_EXECUTE_READWRITE, /* 11 = WRITABLE, EXECUTABLE, SHARED */
|
||||
PAGE_READWRITE, /* 12 = WRITABLE, READABLE */
|
||||
PAGE_READWRITE, /* 13 = WRITABLE, READABLE, SHARED */
|
||||
PAGE_EXECUTE_READWRITE, /* 14 = WRITABLE, READABLE, EXECUTABLE */
|
||||
PAGE_EXECUTE_READWRITE, /* 15 = WRITABLE, READABLE, EXECUTABLE, SHARED */
|
||||
};
|
||||
|
||||
/* TODO: Intsafe should be made into a library, as it's generally useful */
|
||||
static __inline BOOLEAN Intsafe_CanAddULongPtr
|
||||
(
|
||||
IN ULONG_PTR Addend1,
|
||||
IN ULONG_PTR Addend2
|
||||
)
|
||||
{
|
||||
return Addend1 <= (MAXULONG_PTR - Addend2);
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_CanAddULong32
|
||||
(
|
||||
IN ULONG Addend1,
|
||||
IN ULONG Addend2
|
||||
)
|
||||
{
|
||||
return Addend1 <= (MAXULONG - Addend2);
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_AddULong32
|
||||
(
|
||||
OUT PULONG Result,
|
||||
IN ULONG Addend1,
|
||||
IN ULONG Addend2
|
||||
)
|
||||
{
|
||||
if(!Intsafe_CanAddULong32(Addend1, Addend2))
|
||||
return FALSE;
|
||||
|
||||
*Result = Addend1 + Addend2;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_CanMulULong32
|
||||
(
|
||||
IN ULONG Factor1,
|
||||
IN ULONG Factor2
|
||||
)
|
||||
{
|
||||
return Factor1 <= (MAXULONG / Factor2);
|
||||
}
|
||||
|
||||
static __inline BOOLEAN Intsafe_CanOffsetPointer
|
||||
(
|
||||
IN CONST VOID * Pointer,
|
||||
IN SIZE_T Offset
|
||||
)
|
||||
{
|
||||
/* FIXME: (PVOID)MAXULONG_PTR isn't necessarily a valid address */
|
||||
return Intsafe_CanAddULongPtr((ULONG_PTR)Pointer, Offset);
|
||||
}
|
||||
|
||||
/* TODO: these are standard DDK/PSDK macros */
|
||||
#ifndef RTL_FIELD_SIZE
|
||||
#define RTL_FIELD_SIZE(TYPE_, FIELD_) (sizeof(((TYPE_ *)0)->FIELD_))
|
||||
#endif
|
||||
|
||||
#ifndef RTL_SIZEOF_THROUGH_FIELD
|
||||
#define RTL_SIZEOF_THROUGH_FIELD(TYPE_, FIELD_) \
|
||||
(FIELD_OFFSET(TYPE_, FIELD_) + RTL_FIELD_SIZE(TYPE_, FIELD_))
|
||||
#endif
|
||||
|
||||
#ifndef RTL_CONTAINS_FIELD
|
||||
#define RTL_CONTAINS_FIELD(P_, SIZE_, FIELD_) \
|
||||
((((char *)(P_)) + (SIZE_)) > (((char *)(&((P_)->FIELD_))) + sizeof((P_)->FIELD_)))
|
||||
#endif
|
||||
|
||||
static __inline BOOLEAN IsPowerOf2(IN ULONG Number)
|
||||
{
|
||||
if(Number == 0)
|
||||
return FALSE;
|
||||
|
||||
while((Number % 2) == 0)
|
||||
Number /= 2;
|
||||
|
||||
return Number == 1;
|
||||
}
|
||||
|
||||
static __inline ULONG ModPow2(IN ULONG Address, IN ULONG Alignment)
|
||||
{
|
||||
ASSERT(IsPowerOf2(Alignment));
|
||||
return Address & (Alignment - 1);
|
||||
}
|
||||
|
||||
static __inline BOOLEAN IsAligned(IN ULONG Address, IN ULONG Alignment)
|
||||
{
|
||||
return ModPow2(Address, Alignment) == 0;
|
||||
}
|
||||
|
||||
static __inline BOOLEAN AlignUp
|
||||
(
|
||||
OUT PULONG AlignedAddress,
|
||||
IN ULONG Address,
|
||||
IN ULONG Alignment
|
||||
)
|
||||
{
|
||||
ULONG nExcess = ModPow2(Address, Alignment);
|
||||
|
||||
if(nExcess == 0)
|
||||
{
|
||||
*AlignedAddress = Address;
|
||||
return nExcess == 0;
|
||||
}
|
||||
else
|
||||
return Intsafe_AddULong32(AlignedAddress, Address, Alignment - nExcess);
|
||||
}
|
||||
|
||||
#define PEFMT_FIELDS_EQUAL(TYPE1_, TYPE2_, FIELD_) \
|
||||
( \
|
||||
(FIELD_OFFSET(TYPE1_, FIELD_) == FIELD_OFFSET(TYPE2_, FIELD_)) && \
|
||||
(RTL_FIELD_SIZE(TYPE1_, FIELD_) == RTL_FIELD_SIZE(TYPE2_, FIELD_)) \
|
||||
)
|
||||
|
||||
/*
|
||||
References:
|
||||
[1] Microsoft Corporation, "Microsoft Portable Executable and Common Object
|
||||
File Format Specification", revision 6.0 (February 1999)
|
||||
*/
|
||||
NTSTATUS NTAPI PeFmtCreateSection
|
||||
(
|
||||
IN CONST VOID * FileHeader,
|
||||
IN SIZE_T FileHeaderSize,
|
||||
IN PVOID File,
|
||||
OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
|
||||
OUT PULONG Flags,
|
||||
IN PEXEFMT_CB_READ_FILE ReadFileCb,
|
||||
IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
|
||||
)
|
||||
{
|
||||
NTSTATUS nStatus;
|
||||
ULONG cbFileHeaderOffsetSize;
|
||||
ULONG cbSectionHeadersOffset;
|
||||
ULONG cbSectionHeadersSize;
|
||||
ULONG cbSectionHeadersOffsetSize;
|
||||
ULONG cbOptHeaderSize;
|
||||
ULONG cbHeadersSize;
|
||||
ULONG nSectionAlignment;
|
||||
ULONG nFileAlignment;
|
||||
const IMAGE_DOS_HEADER * pidhDosHeader;
|
||||
const IMAGE_NT_HEADERS32 * pinhNtHeader;
|
||||
const IMAGE_OPTIONAL_HEADER32 * piohOptHeader;
|
||||
const IMAGE_SECTION_HEADER * pishSectionHeaders;
|
||||
PMM_SECTION_SEGMENT pssSegments;
|
||||
LARGE_INTEGER lnOffset;
|
||||
PVOID pBuffer;
|
||||
ULONG nPrevVirtualEndOfSegment;
|
||||
ULONG nPrevFileEndOfSegment;
|
||||
ULONG i;
|
||||
|
||||
ASSERT(FileHeader);
|
||||
ASSERT(FileHeaderSize > 0);
|
||||
ASSERT(File);
|
||||
ASSERT(ImageSectionObject);
|
||||
ASSERT(ReadFileCb);
|
||||
ASSERT(AllocateSegmentsCb);
|
||||
|
||||
ASSERT(Intsafe_CanOffsetPointer(FileHeader, FileHeaderSize));
|
||||
|
||||
ASSERT(FileHeaderSize >= sizeof(IMAGE_DOS_HEADER));
|
||||
ASSERT(((UINT_PTR)FileHeader % TYPE_ALIGNMENT(IMAGE_DOS_HEADER)) == 0);
|
||||
|
||||
#define DIE(ARGS_) { DPRINT ARGS_; goto l_Return; }
|
||||
|
||||
pBuffer = NULL;
|
||||
pidhDosHeader = FileHeader;
|
||||
|
||||
/* DOS HEADER */
|
||||
nStatus = STATUS_ROS_EXEFMT_UNKNOWN_FORMAT;
|
||||
|
||||
/* no MZ signature */
|
||||
if(pidhDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
|
||||
DIE(("No MZ signature found, e_magic is %hX\n", pidhDosHeader->e_magic));
|
||||
|
||||
/* not a Windows executable */
|
||||
if(pidhDosHeader->e_lfanew <= 0)
|
||||
DIE(("Not a Windows executable, e_lfanew is %d\n", pidhDosHeader->e_lfanew));
|
||||
|
||||
/* NT HEADER */
|
||||
nStatus = STATUS_INVALID_IMAGE_FORMAT;
|
||||
|
||||
if(!Intsafe_AddULong32(&cbFileHeaderOffsetSize, pidhDosHeader->e_lfanew, RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS32, FileHeader)))
|
||||
DIE(("The DOS stub is too large, e_lfanew is %X\n", pidhDosHeader->e_lfanew));
|
||||
|
||||
if(FileHeaderSize < cbFileHeaderOffsetSize)
|
||||
pinhNtHeader = NULL;
|
||||
else
|
||||
{
|
||||
/*
|
||||
we already know that Intsafe_CanOffsetPointer(FileHeader, FileHeaderSize),
|
||||
and FileHeaderSize >= cbFileHeaderOffsetSize, so this holds true too
|
||||
*/
|
||||
ASSERT(Intsafe_CanOffsetPointer(FileHeader, pidhDosHeader->e_lfanew));
|
||||
pinhNtHeader = (PVOID)((UINT_PTR)FileHeader + pidhDosHeader->e_lfanew);
|
||||
}
|
||||
|
||||
ASSERT(sizeof(IMAGE_NT_HEADERS32) <= sizeof(IMAGE_NT_HEADERS64));
|
||||
ASSERT(TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) == TYPE_ALIGNMENT(IMAGE_NT_HEADERS64));
|
||||
ASSERT(RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS32, FileHeader) == RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS64, FileHeader));
|
||||
ASSERT(FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader) == FIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader));
|
||||
|
||||
/*
|
||||
the buffer doesn't contain the NT file header, or the alignment is wrong: we
|
||||
need to read the header from the file
|
||||
*/
|
||||
if
|
||||
(
|
||||
FileHeaderSize < cbFileHeaderOffsetSize ||
|
||||
(UINT_PTR)pinhNtHeader % TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) != 0
|
||||
)
|
||||
{
|
||||
ULONG cbNtHeaderSize;
|
||||
ULONG cbReadSize;
|
||||
PVOID pData;
|
||||
|
||||
l_ReadHeaderFromFile:
|
||||
lnOffset.QuadPart = pidhDosHeader->e_lfanew;
|
||||
|
||||
/* read the header from the file */
|
||||
nStatus = ReadFileCb
|
||||
(
|
||||
File,
|
||||
&lnOffset,
|
||||
sizeof(IMAGE_NT_HEADERS64),
|
||||
&pData,
|
||||
&pBuffer,
|
||||
&cbReadSize
|
||||
);
|
||||
|
||||
if(!NT_SUCCESS(nStatus))
|
||||
DIE(("ReadFile failed, status %08X\n", nStatus));
|
||||
|
||||
ASSERT(pData);
|
||||
ASSERT(pBuffer);
|
||||
ASSERT(cbReadSize > 0);
|
||||
|
||||
nStatus = STATUS_INVALID_IMAGE_FORMAT;
|
||||
|
||||
/* the buffer doesn't contain the file header */
|
||||
if(cbReadSize < RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS32, FileHeader))
|
||||
DIE(("The file doesn't contain the PE file header\n"));
|
||||
|
||||
pinhNtHeader = pData;
|
||||
|
||||
/* object still not aligned: copy it to the beginning of the buffer */
|
||||
if((UINT_PTR)pinhNtHeader % TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) != 0)
|
||||
{
|
||||
ASSERT((UINT_PTR)pBuffer % TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) == 0);
|
||||
RtlMoveMemory(pBuffer, pData, cbReadSize);
|
||||
pinhNtHeader = pBuffer;
|
||||
}
|
||||
|
||||
/* invalid NT header */
|
||||
if(pinhNtHeader->Signature != IMAGE_NT_SIGNATURE)
|
||||
DIE(("The file isn't a PE executable, Signature is %X\n", pinhNtHeader->Signature));
|
||||
|
||||
if(!Intsafe_AddULong32(&cbNtHeaderSize, pinhNtHeader->FileHeader.SizeOfOptionalHeader, FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader)))
|
||||
DIE(("The full NT header is too large\n"));
|
||||
|
||||
nStatus = STATUS_UNSUCCESSFUL;
|
||||
|
||||
/* the buffer doesn't contain the whole NT header */
|
||||
if(cbReadSize < cbNtHeaderSize)
|
||||
DIE(("The file doesn't contain the full NT header\n"));
|
||||
}
|
||||
else
|
||||
{
|
||||
SIZE_T cbOptHeaderOffsetSize;
|
||||
|
||||
nStatus = STATUS_INVALID_IMAGE_FORMAT;
|
||||
|
||||
/* don't trust an invalid NT header */
|
||||
if(pinhNtHeader->Signature != IMAGE_NT_SIGNATURE)
|
||||
DIE(("The file isn't a PE executable, Signature is %X\n", pinhNtHeader->Signature));
|
||||
|
||||
if(!Intsafe_AddULong32(&cbOptHeaderOffsetSize, pidhDosHeader->e_lfanew, FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader)))
|
||||
DIE(("The DOS stub is too large, e_lfanew is %X\n", pidhDosHeader->e_lfanew));
|
||||
|
||||
if(!Intsafe_AddULong32(&cbOptHeaderOffsetSize, cbOptHeaderOffsetSize, pinhNtHeader->FileHeader.SizeOfOptionalHeader))
|
||||
DIE(("The NT header is too large, SizeOfOptionalHeader is %X\n", pinhNtHeader->FileHeader.SizeOfOptionalHeader));
|
||||
|
||||
/* the buffer doesn't contain the whole NT header: read it from the file */
|
||||
if(cbOptHeaderOffsetSize > FileHeaderSize)
|
||||
goto l_ReadHeaderFromFile;
|
||||
}
|
||||
|
||||
/* read information from the NT header */
|
||||
piohOptHeader = &pinhNtHeader->OptionalHeader;
|
||||
cbOptHeaderSize = pinhNtHeader->FileHeader.SizeOfOptionalHeader;
|
||||
|
||||
nStatus = STATUS_INVALID_IMAGE_FORMAT;
|
||||
|
||||
ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Magic));
|
||||
|
||||
if(!RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Magic))
|
||||
DIE(("The optional header doesn't contain the Magic field, SizeOfOptionalHeader is %X\n", cbOptHeaderSize));
|
||||
|
||||
/* ASSUME: RtlZeroMemory(ImageSectionObject, sizeof(*ImageSectionObject)); */
|
||||
|
||||
switch(piohOptHeader->Magic)
|
||||
{
|
||||
case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
|
||||
case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
|
||||
break;
|
||||
|
||||
default:
|
||||
DIE(("Unrecognized optional header, Magic is %X\n", piohOptHeader->Magic));
|
||||
}
|
||||
|
||||
ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SectionAlignment));
|
||||
ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, FileAlignment));
|
||||
|
||||
if
|
||||
(
|
||||
RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SectionAlignment) &&
|
||||
RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, FileAlignment)
|
||||
)
|
||||
{
|
||||
/* See [1], section 3.4.2 */
|
||||
if(piohOptHeader->SectionAlignment < PAGE_SIZE)
|
||||
{
|
||||
if(piohOptHeader->FileAlignment != piohOptHeader->SectionAlignment)
|
||||
DIE(("Sections aren't page-aligned and the file alignment isn't the same\n"));
|
||||
}
|
||||
else if(piohOptHeader->SectionAlignment < piohOptHeader->FileAlignment)
|
||||
DIE(("The section alignment is smaller than the file alignment\n"));
|
||||
|
||||
nSectionAlignment = piohOptHeader->SectionAlignment;
|
||||
nFileAlignment = piohOptHeader->FileAlignment;
|
||||
|
||||
if(!IsPowerOf2(nSectionAlignment) || !IsPowerOf2(nFileAlignment))
|
||||
DIE(("The section alignment (%u) and file alignment (%u) aren't both powers of 2\n", nSectionAlignment, nFileAlignment));
|
||||
}
|
||||
else
|
||||
{
|
||||
nSectionAlignment = PAGE_SIZE;
|
||||
nFileAlignment = PAGE_SIZE;
|
||||
}
|
||||
|
||||
ASSERT(IsPowerOf2(nSectionAlignment));
|
||||
ASSERT(IsPowerOf2(nFileAlignment));
|
||||
|
||||
switch(piohOptHeader->Magic)
|
||||
{
|
||||
/* PE32 */
|
||||
case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
|
||||
{
|
||||
if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, ImageBase))
|
||||
ImageSectionObject->ImageBase = piohOptHeader->ImageBase;
|
||||
|
||||
if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackReserve))
|
||||
ImageSectionObject->StackReserve = piohOptHeader->SizeOfStackReserve;
|
||||
|
||||
if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackCommit))
|
||||
ImageSectionObject->StackCommit = piohOptHeader->SizeOfStackCommit;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* PE32+ */
|
||||
case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
|
||||
{
|
||||
const IMAGE_OPTIONAL_HEADER64 * pioh64OptHeader;
|
||||
|
||||
pioh64OptHeader = (const IMAGE_OPTIONAL_HEADER64 *)piohOptHeader;
|
||||
|
||||
if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, ImageBase))
|
||||
{
|
||||
if(pioh64OptHeader->ImageBase > MAXULONG_PTR)
|
||||
DIE(("ImageBase exceeds the address space\n"));
|
||||
|
||||
ImageSectionObject->ImageBase = pioh64OptHeader->ImageBase;
|
||||
}
|
||||
|
||||
if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackReserve))
|
||||
{
|
||||
if(pioh64OptHeader->SizeOfStackReserve > MAXULONG_PTR)
|
||||
DIE(("SizeOfStackReserve exceeds the address space\n"));
|
||||
|
||||
ImageSectionObject->StackReserve = pioh64OptHeader->SizeOfStackReserve;
|
||||
}
|
||||
|
||||
if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackCommit))
|
||||
{
|
||||
if(pioh64OptHeader->SizeOfStackCommit > MAXULONG_PTR)
|
||||
DIE(("SizeOfStackCommit exceeds the address space\n"));
|
||||
|
||||
ImageSectionObject->StackCommit = pioh64OptHeader->SizeOfStackCommit;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* [1], section 3.4.2 */
|
||||
if((ULONG_PTR)ImageSectionObject->ImageBase % 0x10000)
|
||||
DIE(("ImageBase is not aligned on a 64KB boundary"));
|
||||
|
||||
ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Subsystem));
|
||||
ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MinorSubsystemVersion));
|
||||
ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MajorSubsystemVersion));
|
||||
|
||||
if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Subsystem))
|
||||
{
|
||||
ImageSectionObject->Subsystem = piohOptHeader->Subsystem;
|
||||
|
||||
if
|
||||
(
|
||||
RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
|
||||
RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MajorSubsystemVersion)
|
||||
)
|
||||
{
|
||||
ImageSectionObject->MinorSubsystemVersion = piohOptHeader->MinorSubsystemVersion;
|
||||
ImageSectionObject->MajorSubsystemVersion = piohOptHeader->MajorSubsystemVersion;
|
||||
}
|
||||
}
|
||||
|
||||
ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, AddressOfEntryPoint));
|
||||
|
||||
if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
|
||||
ImageSectionObject->EntryPoint = piohOptHeader->AddressOfEntryPoint;
|
||||
|
||||
ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfCode));
|
||||
|
||||
if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfCode))
|
||||
ImageSectionObject->Executable = piohOptHeader->SizeOfCode != 0;
|
||||
else
|
||||
ImageSectionObject->Executable = TRUE;
|
||||
|
||||
ImageSectionObject->ImageCharacteristics = pinhNtHeader->FileHeader.Characteristics;
|
||||
ImageSectionObject->Machine = pinhNtHeader->FileHeader.Machine;
|
||||
|
||||
/* SECTION HEADERS */
|
||||
nStatus = STATUS_INVALID_IMAGE_FORMAT;
|
||||
|
||||
/* see [1], section 3.3 */
|
||||
if(pinhNtHeader->FileHeader.NumberOfSections > 96)
|
||||
DIE(("Too many sections, NumberOfSections is %u\n", pinhNtHeader->FileHeader.NumberOfSections));
|
||||
|
||||
/*
|
||||
the additional segment is for the file's headers. They need to be present for
|
||||
the benefit of the dynamic loader (to locate exports, defaults for thread
|
||||
parameters, resources, etc.)
|
||||
*/
|
||||
ImageSectionObject->NrSegments = pinhNtHeader->FileHeader.NumberOfSections + 1;
|
||||
|
||||
/* file offset for the section headers */
|
||||
if(!Intsafe_AddULong32(&cbSectionHeadersOffset, pidhDosHeader->e_lfanew, FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader)))
|
||||
DIE(("Offset overflow\n"));
|
||||
|
||||
if(!Intsafe_AddULong32(&cbSectionHeadersOffset, cbSectionHeadersOffset, pinhNtHeader->FileHeader.SizeOfOptionalHeader))
|
||||
DIE(("Offset overflow\n"));
|
||||
|
||||
/* size of the section headers */
|
||||
ASSERT(Intsafe_CanMulULong32(pinhNtHeader->FileHeader.NumberOfSections, sizeof(IMAGE_SECTION_HEADER)));
|
||||
cbSectionHeadersSize = pinhNtHeader->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
|
||||
|
||||
if(!Intsafe_AddULong32(&cbSectionHeadersOffsetSize, cbSectionHeadersOffset, cbSectionHeadersSize))
|
||||
DIE(("Section headers too large\n"));
|
||||
|
||||
ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfHeaders));
|
||||
|
||||
/* size of the executable's headers */
|
||||
if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfHeaders))
|
||||
{
|
||||
if(!IsAligned(piohOptHeader->SizeOfHeaders, nFileAlignment))
|
||||
DIE(("SizeOfHeaders is not aligned\n"));
|
||||
|
||||
if(cbSectionHeadersSize > piohOptHeader->SizeOfHeaders)
|
||||
DIE(("The section headers overflow SizeOfHeaders\n"));
|
||||
|
||||
cbHeadersSize = piohOptHeader->SizeOfHeaders;
|
||||
}
|
||||
else if(!AlignUp(&cbHeadersSize, cbSectionHeadersOffsetSize, nFileAlignment))
|
||||
DIE(("Overflow aligning the size of headers\n"));
|
||||
|
||||
if(pBuffer)
|
||||
{
|
||||
ExFreePool(pBuffer);
|
||||
pBuffer = NULL;
|
||||
}
|
||||
/* WARNING: pinhNtHeader IS NO LONGER USABLE */
|
||||
/* WARNING: piohOptHeader IS NO LONGER USABLE */
|
||||
/* WARNING: pioh64OptHeader IS NO LONGER USABLE */
|
||||
|
||||
if(FileHeaderSize < cbSectionHeadersOffsetSize)
|
||||
pishSectionHeaders = NULL;
|
||||
else
|
||||
{
|
||||
/*
|
||||
we already know that Intsafe_CanOffsetPointer(FileHeader, FileHeaderSize),
|
||||
and FileHeaderSize >= cbSectionHeadersOffsetSize, so this holds true too
|
||||
*/
|
||||
ASSERT(Intsafe_CanOffsetPointer(FileHeader, cbSectionHeadersOffset));
|
||||
pishSectionHeaders = (PVOID)((UINT_PTR)FileHeader + cbSectionHeadersOffset);
|
||||
}
|
||||
|
||||
/*
|
||||
the buffer doesn't contain the section headers, or the alignment is wrong:
|
||||
read the headers from the file
|
||||
*/
|
||||
if
|
||||
(
|
||||
FileHeaderSize < cbSectionHeadersOffsetSize ||
|
||||
(UINT_PTR)pishSectionHeaders % TYPE_ALIGNMENT(IMAGE_SECTION_HEADER) != 0
|
||||
)
|
||||
{
|
||||
PVOID pData;
|
||||
ULONG cbReadSize;
|
||||
|
||||
lnOffset.QuadPart = cbSectionHeadersOffset;
|
||||
|
||||
/* read the header from the file */
|
||||
nStatus = ReadFileCb
|
||||
(
|
||||
File,
|
||||
&lnOffset,
|
||||
cbSectionHeadersSize,
|
||||
&pData,
|
||||
&pBuffer,
|
||||
&cbReadSize
|
||||
);
|
||||
|
||||
if(!NT_SUCCESS(nStatus))
|
||||
DIE(("ReadFile failed with status %08X\n", nStatus));
|
||||
|
||||
ASSERT(pData);
|
||||
ASSERT(pBuffer);
|
||||
ASSERT(cbReadSize > 0);
|
||||
|
||||
nStatus = STATUS_INVALID_IMAGE_FORMAT;
|
||||
|
||||
/* the buffer doesn't contain all the section headers */
|
||||
if(cbReadSize < cbSectionHeadersSize)
|
||||
DIE(("The file doesn't contain all of the section headers\n"));
|
||||
|
||||
pishSectionHeaders = pData;
|
||||
|
||||
/* object still not aligned: copy it to the beginning of the buffer */
|
||||
if((UINT_PTR)pishSectionHeaders % TYPE_ALIGNMENT(IMAGE_SECTION_HEADER) != 0)
|
||||
{
|
||||
ASSERT((UINT_PTR)pBuffer % TYPE_ALIGNMENT(IMAGE_SECTION_HEADER) == 0);
|
||||
RtlMoveMemory(pBuffer, pData, cbReadSize);
|
||||
pishSectionHeaders = pBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
/* SEGMENTS */
|
||||
/* allocate the segments */
|
||||
nStatus = STATUS_INSUFFICIENT_RESOURCES;
|
||||
ImageSectionObject->Segments = AllocateSegmentsCb(ImageSectionObject->NrSegments);
|
||||
|
||||
if(ImageSectionObject->Segments == NULL)
|
||||
DIE(("AllocateSegments failed\n"));
|
||||
|
||||
/* initialize the headers segment */
|
||||
pssSegments = ImageSectionObject->Segments;
|
||||
|
||||
ASSERT(IsAligned(cbHeadersSize, nFileAlignment));
|
||||
|
||||
if(!AlignUp(&nPrevFileEndOfSegment, cbHeadersSize, nFileAlignment))
|
||||
DIE(("Cannot align the size of the section headers\n"));
|
||||
|
||||
if(!AlignUp(&nPrevVirtualEndOfSegment, cbHeadersSize, nSectionAlignment))
|
||||
DIE(("Cannot align the size of the section headers\n"));
|
||||
|
||||
pssSegments[0].FileOffset = 0;
|
||||
pssSegments[0].Protection = PAGE_READONLY;
|
||||
pssSegments[0].Length = nPrevVirtualEndOfSegment;
|
||||
pssSegments[0].RawLength = nPrevFileEndOfSegment;
|
||||
pssSegments[0].VirtualAddress = 0;
|
||||
pssSegments[0].Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA;
|
||||
pssSegments[0].WriteCopy = TRUE;
|
||||
|
||||
/* skip the headers segment */
|
||||
++ pssSegments;
|
||||
|
||||
nStatus = STATUS_INVALID_IMAGE_FORMAT;
|
||||
|
||||
/* convert the executable sections into segments. See also [1], section 4 */
|
||||
for(i = 0; i < ImageSectionObject->NrSegments - 1; ++ i)
|
||||
{
|
||||
ULONG nCharacteristics;
|
||||
|
||||
/* validate the alignment */
|
||||
if(!IsAligned(pishSectionHeaders[i].VirtualAddress, nSectionAlignment))
|
||||
DIE(("VirtualAddress[%u] is not aligned\n", i));
|
||||
|
||||
/* sections must be contiguous, ordered by base address and non-overlapping */
|
||||
if(pishSectionHeaders[i].VirtualAddress != nPrevVirtualEndOfSegment)
|
||||
DIE(("Memory gap between section %u and the previous\n", i));
|
||||
|
||||
/* ignore explicit BSS sections */
|
||||
if(pishSectionHeaders[i].SizeOfRawData != 0)
|
||||
{
|
||||
/* validate the alignment */
|
||||
if(!IsAligned(pishSectionHeaders[i].SizeOfRawData, nFileAlignment))
|
||||
DIE(("SizeOfRawData[%u] is not aligned\n", i));
|
||||
|
||||
if(!IsAligned(pishSectionHeaders[i].PointerToRawData, nFileAlignment))
|
||||
DIE(("PointerToRawData[%u] is not aligned\n", i));
|
||||
|
||||
/* sections must be contiguous, ordered by base address and non-overlapping */
|
||||
if(pishSectionHeaders[i].PointerToRawData != nPrevFileEndOfSegment)
|
||||
DIE(("File gap between section %u and the previous\n", i));
|
||||
|
||||
/* conversion */
|
||||
pssSegments[i].FileOffset = pishSectionHeaders[i].PointerToRawData;
|
||||
pssSegments[i].RawLength = pishSectionHeaders[i].SizeOfRawData;
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT(pssSegments[i].FileOffset == 0);
|
||||
ASSERT(pssSegments[i].RawLength == 0);
|
||||
}
|
||||
|
||||
nCharacteristics = pishSectionHeaders[i].Characteristics;
|
||||
|
||||
/* no explicit protection */
|
||||
if((nCharacteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)) == 0)
|
||||
{
|
||||
if(nCharacteristics & IMAGE_SCN_CNT_CODE)
|
||||
nCharacteristics |= IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ;
|
||||
|
||||
if(nCharacteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
|
||||
nCharacteristics |= IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
|
||||
|
||||
if(nCharacteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
|
||||
nCharacteristics |= IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
|
||||
}
|
||||
|
||||
/* see table above */
|
||||
pssSegments[i].Protection = SectionCharacteristicsToProtect[nCharacteristics >> 28];
|
||||
pssSegments[i].WriteCopy = !(nCharacteristics & IMAGE_SCN_MEM_SHARED);
|
||||
|
||||
if(pishSectionHeaders[i].Misc.VirtualSize == 0 || pishSectionHeaders[i].Misc.VirtualSize < pishSectionHeaders[i].SizeOfRawData)
|
||||
pssSegments[i].Length = pishSectionHeaders[i].SizeOfRawData;
|
||||
else
|
||||
pssSegments[i].Length = pishSectionHeaders[i].Misc.VirtualSize;
|
||||
|
||||
if(!AlignUp(&pssSegments[i].Length, pssSegments[i].Length, nSectionAlignment))
|
||||
DIE(("Cannot align the virtual size of section %u\n", i));
|
||||
|
||||
ASSERT(IsAligned(pssSegments[i].Length, nSectionAlignment));
|
||||
|
||||
if(pssSegments[i].Length == 0)
|
||||
DIE(("Virtual size of section %u is null\n", i));
|
||||
|
||||
pssSegments[i].VirtualAddress = pishSectionHeaders[i].VirtualAddress;
|
||||
pssSegments[i].Characteristics = pishSectionHeaders[i].Characteristics;
|
||||
|
||||
/* ensure the executable is no larger than 4GB */
|
||||
if(pssSegments[i].RawLength != 0)
|
||||
{
|
||||
if(!Intsafe_AddULong32(&nPrevFileEndOfSegment, pssSegments[i].FileOffset, pssSegments[i].RawLength))
|
||||
DIE(("The executable is larger than 4GB\n"));
|
||||
}
|
||||
|
||||
/* ensure the memory image is no larger than 4GB */
|
||||
if(!Intsafe_AddULong32(&nPrevVirtualEndOfSegment, pssSegments[i].VirtualAddress, pssSegments[i].Length))
|
||||
DIE(("The image is larger than 4GB\n"));
|
||||
}
|
||||
|
||||
/* spare our caller some work in validating the segments */
|
||||
*Flags = EXEFMT_LOAD_ASSUME_SEGMENTS_SORTED | EXEFMT_LOAD_ASSUME_SEGMENTS_NO_OVERLAP;
|
||||
|
||||
if(nSectionAlignment >= PAGE_SIZE)
|
||||
*Flags |= EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED;
|
||||
|
||||
/* Success */
|
||||
nStatus = STATUS_ROS_EXEFMT_LOADED_FORMAT & EXEFMT_LOADED_PE32;
|
||||
|
||||
l_Return:
|
||||
if(pBuffer)
|
||||
ExFreePool(pBuffer);
|
||||
|
||||
return nStatus;
|
||||
}
|
||||
|
||||
/* EOF */
|
Loading…
Reference in a new issue