#define MAX_PHNUM 10
#include "mmi_include.h"
#include "fs_type.h"
typedef unsigned long Elf32_Addr; //Unsigned program address 4
typedef unsigned short Elf32_Half; //Unsigned medium integer 2
typedef unsigned long Elf32_Off; //Unsigned file offset 4
typedef long Elf32_Sword; //Signed large integer 4
typedef unsigned long Elf32_Word; //Unsigned large integer 4
//ELF Header
//e_ident[] Identification Indexes
#define EI_MAG0 0 //File identification
#define EI_MAG1 1 //File identification
#define EI_MAG2 2 //File identification
#define EI_MAG3 3 //File identification
#define EI_CLASS 4 //File class
#define EI_DATA 5 //Data encoding
#define EI_VERSION 6 //File version
#define EI_PAD 7 //Start of padding bytes
#define EI_NIDENT 16 //Size of e_ident[]
typedef struct
{
unsigned char e_ident[EI_NIDENT]; //The initial bytes mark the file as an object file and provide machine-independent data with which to decode and interpret the file¡¯s contents.
Elf32_Half e_type; //This member identifies the object file type.
Elf32_Half e_machine; //This member¡¯s value specifies the required architecture for an individual file.
Elf32_Word e_version; //This member identifies the object file version.
Elf32_Addr e_entry; //This member gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero.
Elf32_Off e_phoff; //This member holds the program header table¡¯s file offset in bytes. If the file has no program header table, this member holds zero.
Elf32_Off e_shoff; //This member holds the section header table¡¯s file offset in bytes. If the file has no section header table, this member holds zero.
Elf32_Word e_flags; //This member holds processor-specific flags associated with the file. Flag names take the form EF_machine_flag.
Elf32_Half e_ehsize; //This member holds the ELF header¡¯s size in bytes.
Elf32_Half e_phentsize; //This member holds the size in bytes of one entry in the file¡¯s program header table; all entries are the same size.
Elf32_Half e_phnum; //This member holds the number of entries in the program header table. Thus the product of e_phentsize and e_phnum gives the table¡¯s size in bytes. If a file has no program header table, e_phnum holds the value zero.
Elf32_Half e_shentsize; //This member holds a section header¡¯s size in bytes. A section header is one entry in the section header table; all entries are the same size.
Elf32_Half e_shnum; //shnum¡ªThis member holds the number of entries in the section header table. Thus the product of e_shentsize and e_shnum gives the section header table¡¯s size in bytes. If a file has no section header table, e_shnum holds the value zero.
Elf32_Half e_shstrndx; //This member holds the section header table index of the entry associated with the section name string table. If the file has no section name string table, this member holds the value SHN_UNDEF.
} Elf32_Ehdr;
//E_type¡ªThis member identifies the object file type.
#define ET_NONE 0 //No file type
#define ET_REL 1 //Re-locatable file
#define ET_EXEC 2 //Executable file
#define ET_DYN 3 //Shared object file
#define ET_CORE 4 //Core file
#define ET_LOPROC 0xFF00 //Processor-specific
#define ET_HIPROC 0xFFFF //Processor-specific
//E_machine¡ªThis member¡¯s value specifies the required architecture for an individual file.
#define EM_NONE 0 //No machine
#define EM_M32 1 //AT&T WE 32100
#define EM_SPARC 2 //SPARC
#define EM_386 3 //Intel Architecture
#define EM_68K 4 //Motorola 68000
#define EM_88K 5 //Motorola 88000
#define EM_860 7 //Intel 80860
#define EM_MIPS 8 //MIPS RS3000 Big-Endian
#define EM_MIPS_RS4_BE 10 //MIPS RS4000 Big-Endian
#define EM_ARM 40 //ARM/Thumb Architecture
//E_version¡ªThis member identifies the object file version.
#define EV_NONE 0 //Invalid version
#define EV_CURRENT 1 //Current version
//EI_MAG0 0 to EI_MAG3¡ªA file¡¯s first 4 bytes hold a magic number, identifying the file as an ELF object file. Name Value Meaning
#define ELFMAG0 0x7f //e_ident[EI_MAG0]
#define ELFMAG1 'E' //e_ident[EI_MAG1]
#define ELFMAG2 'L' //e_ident[EI_MAG2]
#define ELFMAG3 'F' //e_ident[EI_MAG3]
//EI_CLASS¡ªThe next byte, e_ident[EI_CLASS], identifies the file¡¯s class, or capacity. Name Value Meaning
#define ELFCLASSNONE 0 //Invalid class
#define ELFCLASS32 1 //32-bit objects
#define ELFCLASS64 2 //64-bit objects
//EI_DATA¡ªByte e_ident[EI_DATA]specifies the data encoding of all data1 in the object file. The following encodings are currently defined. Name Value Meaning
#define ELFDATANONE 0 //Invalid data encoding
#define ELFDATA2LSB 1 //See Data encodings ELFDATA2LSB, below
#define ELFDATA2MSB 2 //See Data encodings ELFDATA2MSB, below
//Sections
//Section Header
typedef struct
{
Elf32_Word sh_name; //This member specifies the name of the section. Its value is an index into the section header string table section [see section 3.4, String Table below], giving the location of a null-terminated string.
Elf32_Word sh_type; //type¡ªThis member categorizes the section¡¯s contents and semantics. Section types and their descriptions appear in Figure 3-9 below.
Elf32_Word sh_flags; //Sections support 1-bit flags that describe miscellaneous attributes. Flag definitions appear in Figure 3-11, below.
Elf32_Addr sh_addr; //If the section will appear in the memory image of a process, this member gives the address at which the section¡¯s first byte should reside. Otherwise, the member contains 0.
Elf32_Off sh_offset; //This member¡¯s value gives the byte offset from the beginning of the file to the first byte in the section. One section type, SHT_NOBITS described in Figure 3-9 below, occupies no space in the file, and its sh_offset member locates the conceptual placement in the file.
Elf32_Word sh_size; //This member gives the section¡¯s size in bytes. Unless the section type is SHT_NOBITS, the section occupies sh_size bytes in the file. A section of type SHT_NOBITS may have a non-zero size, but it occupies no space in the file.
Elf32_Word sh_link; //This member holds a section header table index link, whose interpretation depends on the section type. Figure 3-12 below describes the values.
Elf32_Word sh_info; //This member holds extra information, whose interpretation depends on the section type. Figure 3-12 below describes the values.
Elf32_Word sh_addralign; //Some sections have address alignment constraints. For example, if a section holds a doubleword, the system must ensure double-word alignment for the entire section. That is, the value of sh_addr must be congruent to 0, modulo the value of sh_addralign. Currently, only 0 and positive integral powers of two are allowed. Values 0 and 1 mean the section has no alignment constraints.
Elf32_Word sh_entsize; //Some sections hold a table of fixed-size entries, such as a symbol table. For such a section, this member gives the size in bytes of each entry. The member contains 0 if the section does not hold a table of fixedsize entries. A section header¡¯s sh_type member specifies the section¡¯s semantics.
} Elf32_Shdr;
//Special Section Indexes
#define SHN_UNDEF 0 //This value marks an undefined, missing, irrelevant, or otherwise meaningless section reference. For example, a symbol ¡°defined¡± relative to section number SHN_UNDEF is an undefined symbol.
#define SHN_LORESERVE 0xff00 //This value specifies the lower bound of the range of reserved indexes.
#define SHN_LOPROC 0xff00 //Values in this range are reserved for processor-specific semantics.
#define SHN_HIPROC 0xff1f //Values in this range are reserved for processor-specific semantics.
#define SHN_ABS 0xfff1 //This value