1*e79e4e7cSIngo Weinhold // Elf.h 2*e79e4e7cSIngo Weinhold 3*e79e4e7cSIngo Weinhold #ifndef _ELF_H 4*e79e4e7cSIngo Weinhold #define _ELF_H 5*e79e4e7cSIngo Weinhold 6*e79e4e7cSIngo Weinhold // types 7*e79e4e7cSIngo Weinhold typedef uint32 Elf32_Addr; 8*e79e4e7cSIngo Weinhold typedef uint16 Elf32_Half; 9*e79e4e7cSIngo Weinhold typedef uint32 Elf32_Off; 10*e79e4e7cSIngo Weinhold typedef int32 Elf32_Sword; 11*e79e4e7cSIngo Weinhold typedef uint32 Elf32_Word; 12*e79e4e7cSIngo Weinhold 13*e79e4e7cSIngo Weinhold // e_ident indices 14*e79e4e7cSIngo Weinhold #define EI_MAG0 0 15*e79e4e7cSIngo Weinhold #define EI_MAG1 1 16*e79e4e7cSIngo Weinhold #define EI_MAG2 2 17*e79e4e7cSIngo Weinhold #define EI_MAG3 3 18*e79e4e7cSIngo Weinhold #define EI_CLASS 4 19*e79e4e7cSIngo Weinhold #define EI_DATA 5 20*e79e4e7cSIngo Weinhold #define EI_VERSION 6 21*e79e4e7cSIngo Weinhold #define EI_PAD 7 22*e79e4e7cSIngo Weinhold #define EI_NIDENT 16 23*e79e4e7cSIngo Weinhold 24*e79e4e7cSIngo Weinhold // ELF magic 25*e79e4e7cSIngo Weinhold #define ELFMAG0 0x7f 26*e79e4e7cSIngo Weinhold #define ELFMAG1 'E' 27*e79e4e7cSIngo Weinhold #define ELFMAG2 'L' 28*e79e4e7cSIngo Weinhold #define ELFMAG3 'F' 29*e79e4e7cSIngo Weinhold 30*e79e4e7cSIngo Weinhold // class 31*e79e4e7cSIngo Weinhold #define ELFCLASSNONE 0 32*e79e4e7cSIngo Weinhold #define ELFCLASS32 1 33*e79e4e7cSIngo Weinhold #define ELFCLASS64 2 34*e79e4e7cSIngo Weinhold 35*e79e4e7cSIngo Weinhold // version 36*e79e4e7cSIngo Weinhold #define EV_NONE 0 37*e79e4e7cSIngo Weinhold #define EV_CURRENT 1 38*e79e4e7cSIngo Weinhold 39*e79e4e7cSIngo Weinhold // object file header 40*e79e4e7cSIngo Weinhold typedef struct { 41*e79e4e7cSIngo Weinhold unsigned char e_ident[EI_NIDENT]; 42*e79e4e7cSIngo Weinhold Elf32_Half e_type; 43*e79e4e7cSIngo Weinhold Elf32_Half e_machine; 44*e79e4e7cSIngo Weinhold Elf32_Word e_version; 45*e79e4e7cSIngo Weinhold Elf32_Addr e_entry; 46*e79e4e7cSIngo Weinhold Elf32_Off e_phoff; 47*e79e4e7cSIngo Weinhold Elf32_Off e_shoff; 48*e79e4e7cSIngo Weinhold Elf32_Word e_flags; 49*e79e4e7cSIngo Weinhold Elf32_Half e_ehsize; 50*e79e4e7cSIngo Weinhold Elf32_Half e_phentsize; 51*e79e4e7cSIngo Weinhold Elf32_Half e_phnum; 52*e79e4e7cSIngo Weinhold Elf32_Half e_shentsize; 53*e79e4e7cSIngo Weinhold Elf32_Half e_shnum; 54*e79e4e7cSIngo Weinhold Elf32_Half e_shstrndx; 55*e79e4e7cSIngo Weinhold } Elf32_Ehdr; 56*e79e4e7cSIngo Weinhold 57*e79e4e7cSIngo Weinhold // e_ident EI_CLASS and EI_DATA values 58*e79e4e7cSIngo Weinhold #define ELFCLASSNONE 0 59*e79e4e7cSIngo Weinhold #define ELFCLASS32 1 60*e79e4e7cSIngo Weinhold #define ELFCLASS64 2 61*e79e4e7cSIngo Weinhold #define ELFDATANONE 0 62*e79e4e7cSIngo Weinhold #define ELFDATA2LSB 1 63*e79e4e7cSIngo Weinhold #define ELFDATA2MSB 2 64*e79e4e7cSIngo Weinhold 65*e79e4e7cSIngo Weinhold // special section header indices 66*e79e4e7cSIngo Weinhold #define SHN_UNDEF 0 67*e79e4e7cSIngo Weinhold #define SHN_LORESERVE 0xff00 68*e79e4e7cSIngo Weinhold #define SHN_LOPROC 0xff00 69*e79e4e7cSIngo Weinhold #define SHN_HIPROC 0xff1f 70*e79e4e7cSIngo Weinhold #define SHN_ABS 0xfff1 71*e79e4e7cSIngo Weinhold #define SHN_COMMON 0xfff2 72*e79e4e7cSIngo Weinhold #define SHN_HIRESERVE 0xffff 73*e79e4e7cSIngo Weinhold 74*e79e4e7cSIngo Weinhold // program header 75*e79e4e7cSIngo Weinhold typedef struct { 76*e79e4e7cSIngo Weinhold Elf32_Word p_type; 77*e79e4e7cSIngo Weinhold Elf32_Off p_offset; 78*e79e4e7cSIngo Weinhold Elf32_Addr p_vaddr; 79*e79e4e7cSIngo Weinhold Elf32_Addr p_paddr; 80*e79e4e7cSIngo Weinhold Elf32_Word p_filesz; 81*e79e4e7cSIngo Weinhold Elf32_Word p_memsz; 82*e79e4e7cSIngo Weinhold Elf32_Word p_flags; 83*e79e4e7cSIngo Weinhold Elf32_Word p_align; 84*e79e4e7cSIngo Weinhold } Elf32_Phdr; 85*e79e4e7cSIngo Weinhold 86*e79e4e7cSIngo Weinhold // p_type 87*e79e4e7cSIngo Weinhold #define PT_NULL 0 88*e79e4e7cSIngo Weinhold #define PT_LOAD 1 89*e79e4e7cSIngo Weinhold #define PT_DYNAMIC 2 90*e79e4e7cSIngo Weinhold #define PT_INTERP 3 91*e79e4e7cSIngo Weinhold #define PT_NOTE 4 92*e79e4e7cSIngo Weinhold #define PT_SHLIB 5 93*e79e4e7cSIngo Weinhold #define PT_PHDIR 6 94*e79e4e7cSIngo Weinhold #define PT_LOPROC 0x70000000 95*e79e4e7cSIngo Weinhold #define PT_HIPROC 0x7fffffff 96*e79e4e7cSIngo Weinhold 97*e79e4e7cSIngo Weinhold // section header 98*e79e4e7cSIngo Weinhold typedef struct { 99*e79e4e7cSIngo Weinhold Elf32_Word sh_name; 100*e79e4e7cSIngo Weinhold Elf32_Word sh_type; 101*e79e4e7cSIngo Weinhold Elf32_Word sh_flags; 102*e79e4e7cSIngo Weinhold Elf32_Addr sh_addr; 103*e79e4e7cSIngo Weinhold Elf32_Off sh_offset; 104*e79e4e7cSIngo Weinhold Elf32_Word sh_size; 105*e79e4e7cSIngo Weinhold Elf32_Word sh_link; 106*e79e4e7cSIngo Weinhold Elf32_Word sh_info; 107*e79e4e7cSIngo Weinhold Elf32_Word sh_addralign; 108*e79e4e7cSIngo Weinhold Elf32_Word sh_entsize; 109*e79e4e7cSIngo Weinhold } Elf32_Shdr; 110*e79e4e7cSIngo Weinhold 111*e79e4e7cSIngo Weinhold // sh_type values 112*e79e4e7cSIngo Weinhold #define SHT_NULL 0 113*e79e4e7cSIngo Weinhold #define SHT_PROGBITS 1 114*e79e4e7cSIngo Weinhold #define SHT_SYMTAB 2 115*e79e4e7cSIngo Weinhold #define SHT_STRTAB 3 116*e79e4e7cSIngo Weinhold #define SHT_RELA 4 117*e79e4e7cSIngo Weinhold #define SHT_HASH 5 118*e79e4e7cSIngo Weinhold #define SHT_DYNAMIC 6 119*e79e4e7cSIngo Weinhold #define SHT_NOTE 7 120*e79e4e7cSIngo Weinhold #define SHT_NOBITS 8 121*e79e4e7cSIngo Weinhold #define SHT_REL 9 122*e79e4e7cSIngo Weinhold #define SHT_SHLIB 10 123*e79e4e7cSIngo Weinhold #define SHT_DYNSYM 11 124*e79e4e7cSIngo Weinhold #define SHT_LOPROC 0x70000000 125*e79e4e7cSIngo Weinhold #define SHT_HIPROC 0x7fffffff 126*e79e4e7cSIngo Weinhold #define SHT_LOUSER 0x80000000 127*e79e4e7cSIngo Weinhold #define SHT_HIUSER 0xffffffff 128*e79e4e7cSIngo Weinhold 129*e79e4e7cSIngo Weinhold // relocation entry 130*e79e4e7cSIngo Weinhold typedef struct { 131*e79e4e7cSIngo Weinhold Elf32_Addr r_offset; 132*e79e4e7cSIngo Weinhold Elf32_Word r_info; 133*e79e4e7cSIngo Weinhold } Elf32_Rel; 134*e79e4e7cSIngo Weinhold 135*e79e4e7cSIngo Weinhold // relocation entry with addend 136*e79e4e7cSIngo Weinhold typedef struct { 137*e79e4e7cSIngo Weinhold Elf32_Addr r_offset; 138*e79e4e7cSIngo Weinhold Elf32_Word r_info; 139*e79e4e7cSIngo Weinhold Elf32_Sword r_addend; 140*e79e4e7cSIngo Weinhold } Elf32_Rela; 141*e79e4e7cSIngo Weinhold 142*e79e4e7cSIngo Weinhold // r_info accessors 143*e79e4e7cSIngo Weinhold #define ELF32_R_SYM(i) ((i) >> 8) 144*e79e4e7cSIngo Weinhold #define ELF32_R_TYPE(i) ((unsigned char)(i)) 145*e79e4e7cSIngo Weinhold #define ELF32_R_INFO(s, t) ((s) << 8 + (unsigned char)(t)) 146*e79e4e7cSIngo Weinhold 147*e79e4e7cSIngo Weinhold // relocation types (i368 specific) 148*e79e4e7cSIngo Weinhold #define R_386_NONE 0 149*e79e4e7cSIngo Weinhold #define R_386_32 1 150*e79e4e7cSIngo Weinhold #define R_386_PC32 2 151*e79e4e7cSIngo Weinhold #define R_386_GOT32 3 152*e79e4e7cSIngo Weinhold #define R_386_PLT32 4 153*e79e4e7cSIngo Weinhold #define R_386_COPY 5 154*e79e4e7cSIngo Weinhold #define R_386_GLOB_DAT 6 155*e79e4e7cSIngo Weinhold #define R_386_JMP_SLOT 7 156*e79e4e7cSIngo Weinhold #define R_386_RELATIVE 8 157*e79e4e7cSIngo Weinhold #define R_386_GOTOFF 9 158*e79e4e7cSIngo Weinhold #define R_386_GOTPC 10 159*e79e4e7cSIngo Weinhold 160*e79e4e7cSIngo Weinhold // symbol table entry 161*e79e4e7cSIngo Weinhold typedef struct { 162*e79e4e7cSIngo Weinhold Elf32_Word st_name; 163*e79e4e7cSIngo Weinhold Elf32_Addr st_value; 164*e79e4e7cSIngo Weinhold Elf32_Word st_size; 165*e79e4e7cSIngo Weinhold Elf32_Word st_info; 166*e79e4e7cSIngo Weinhold Elf32_Word st_other; 167*e79e4e7cSIngo Weinhold Elf32_Word st_shndx; 168*e79e4e7cSIngo Weinhold } Elf32_Sym; 169*e79e4e7cSIngo Weinhold 170*e79e4e7cSIngo Weinhold // st_info accessors 171*e79e4e7cSIngo Weinhold #define ELF32_ST_BIND(i) ((i) >> 4) 172*e79e4e7cSIngo Weinhold #define ELF32_ST_TYPE(i) ((i) & 0xf) 173*e79e4e7cSIngo Weinhold #define ELF32_ST_INFO(b, t) ((b) << 4 + (t) & 0xf) 174*e79e4e7cSIngo Weinhold 175*e79e4e7cSIngo Weinhold // symbol binding 176*e79e4e7cSIngo Weinhold #define STB_LOCAL 0 177*e79e4e7cSIngo Weinhold #define STB_GLOBAL 1 178*e79e4e7cSIngo Weinhold #define STB_WEAK 2 179*e79e4e7cSIngo Weinhold #define STB_LOPROC 13 180*e79e4e7cSIngo Weinhold #define STB_HIPROC 15 181*e79e4e7cSIngo Weinhold 182*e79e4e7cSIngo Weinhold // symbol types 183*e79e4e7cSIngo Weinhold #define STT_NOTYPE 0 184*e79e4e7cSIngo Weinhold #define STT_OBJECT 1 185*e79e4e7cSIngo Weinhold #define STT_FUNC 2 186*e79e4e7cSIngo Weinhold #define STT_SECTION 3 187*e79e4e7cSIngo Weinhold #define STT_FILE 4 188*e79e4e7cSIngo Weinhold #define STT_LOPROC 13 189*e79e4e7cSIngo Weinhold #define STT_HIPROC 15 190*e79e4e7cSIngo Weinhold 191*e79e4e7cSIngo Weinhold 192*e79e4e7cSIngo Weinhold #endif // _ELF_H 193