Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame^] | 1 | //===-- elf.h ---------------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef __elf_h__ |
| 11 | #define __elf_h__ |
| 12 | |
| 13 | typedef uint16_t Elf32_Half; |
| 14 | typedef uint32_t Elf32_Word; |
| 15 | typedef int32_t Elf32_Sword; |
| 16 | typedef uint32_t Elf32_Addr; |
| 17 | typedef uint32_t Elf32_Off; |
| 18 | |
| 19 | |
| 20 | #define EI_NIDENT 16 |
| 21 | |
| 22 | //---------------------------------------------------------------------- |
| 23 | // ELF Header |
| 24 | //---------------------------------------------------------------------- |
| 25 | typedef struct Elf32_Ehdr_Tag |
| 26 | { |
| 27 | unsigned char e_ident[EI_NIDENT]; |
| 28 | Elf32_Half e_type; |
| 29 | Elf32_Half e_machine; |
| 30 | Elf32_Word e_version; |
| 31 | Elf32_Addr e_entry; |
| 32 | Elf32_Off e_phoff; |
| 33 | Elf32_Off e_shoff; |
| 34 | Elf32_Word e_flags; |
| 35 | Elf32_Half e_ehsize; |
| 36 | Elf32_Half e_phentsize; |
| 37 | Elf32_Half e_phnum; |
| 38 | Elf32_Half e_shentsize; |
| 39 | Elf32_Half e_shnum; |
| 40 | Elf32_Half e_shstrndx; |
| 41 | } Elf32_Ehdr; |
| 42 | |
| 43 | //---------------------------------------------------------------------- |
| 44 | // e_type |
| 45 | // |
| 46 | // This member identifies the object file type. |
| 47 | //---------------------------------------------------------------------- |
| 48 | #define ET_NONE 0 // No file type |
| 49 | #define ET_REL 1 // Relocatable file |
| 50 | #define ET_EXEC 2 // Executable file |
| 51 | #define ET_DYN 3 // Shared object file |
| 52 | #define ET_CORE 4 // Core file |
| 53 | #define ET_LOPROC 0xff00 // Processor-specific |
| 54 | #define ET_HIPROC 0xffff // Processor-specific |
| 55 | |
| 56 | //---------------------------------------------------------------------- |
| 57 | // e_machine |
| 58 | // |
| 59 | // Machine Type |
| 60 | //---------------------------------------------------------------------- |
| 61 | #define EM_NONE 0 // No machine |
| 62 | #define EM_M32 1 // AT&T WE 32100 |
| 63 | #define EM_SPARC 2 // SPARC |
| 64 | #define EM_386 3 // Intel 80386 |
| 65 | #define EM_68K 4 // Motorola 68000 |
| 66 | #define EM_88K 5 // Motorola 88000 |
| 67 | #define EM_860 7 // Intel 80860 |
| 68 | #define EM_MIPS 8 // MIPS RS3000 |
| 69 | #define EM_PPC 20 // PowerPC |
| 70 | #define EM_PPC64 21 // PowerPC64 |
| 71 | #define EM_ARM 40 // ARM |
| 72 | |
| 73 | |
| 74 | //---------------------------------------------------------------------- |
| 75 | // e_ident indexes |
| 76 | //---------------------------------------------------------------------- |
| 77 | #define EI_MAG0 0 // File identification |
| 78 | #define EI_MAG1 1 // File identification |
| 79 | #define EI_MAG2 2 // File identification |
| 80 | #define EI_MAG3 3 // File identification |
| 81 | #define EI_CLASS 4 // File class |
| 82 | #define EI_DATA 5 // Data encoding |
| 83 | #define EI_VERSION 6 // File version |
| 84 | #define EI_PAD 7 // Start of padding bytes |
| 85 | |
| 86 | |
| 87 | //---------------------------------------------------------------------- |
| 88 | // EI_DATA definitions |
| 89 | //---------------------------------------------------------------------- |
| 90 | #define ELFDATANONE 0 // Invalid data encoding |
| 91 | #define ELFDATA2LSB 1 // Little Endian |
| 92 | #define ELFDATA2MSB 2 // Big Endian |
| 93 | |
| 94 | //---------------------------------------------------------------------- |
| 95 | // Section Header |
| 96 | //---------------------------------------------------------------------- |
| 97 | typedef struct Elf32_Shdr_Tag |
| 98 | { |
| 99 | Elf32_Word sh_name; |
| 100 | Elf32_Word sh_type; |
| 101 | Elf32_Word sh_flags; |
| 102 | Elf32_Addr sh_addr; |
| 103 | Elf32_Off sh_offset; |
| 104 | Elf32_Word sh_size; |
| 105 | Elf32_Word sh_link; |
| 106 | Elf32_Word sh_info; |
| 107 | Elf32_Word sh_addralign; |
| 108 | Elf32_Word sh_entsize; |
| 109 | } Elf32_Shdr; |
| 110 | |
| 111 | //---------------------------------------------------------------------- |
| 112 | // Section Types (sh_type) |
| 113 | //---------------------------------------------------------------------- |
| 114 | #define SHT_NULL 0 |
| 115 | #define SHT_PROGBITS 1 |
| 116 | #define SHT_SYMTAB 2 |
| 117 | #define SHT_STRTAB 3 |
| 118 | #define SHT_RELA 4 |
| 119 | #define SHT_HASH 5 |
| 120 | #define SHT_DYNAMIC 6 |
| 121 | #define SHT_NOTE 7 |
| 122 | #define SHT_NOBITS 8 |
| 123 | #define SHT_REL 9 |
| 124 | #define SHT_SHLIB 10 |
| 125 | #define SHT_DYNSYM 11 |
| 126 | #define SHT_LOPROC 0x70000000 |
| 127 | #define SHT_HIPROC 0x7fffffff |
| 128 | #define SHT_LOUSER 0x80000000 |
| 129 | #define SHT_HIUSER 0xffffffff |
| 130 | |
| 131 | //---------------------------------------------------------------------- |
| 132 | // Special Section Indexes |
| 133 | //---------------------------------------------------------------------- |
| 134 | #define SHN_UNDEF 0 |
| 135 | #define SHN_LORESERVE 0xff00 |
| 136 | #define SHN_LOPROC 0xff00 |
| 137 | #define SHN_HIPROC 0xff1f |
| 138 | #define SHN_ABS 0xfff1 |
| 139 | #define SHN_COMMON 0xfff2 |
| 140 | #define SHN_HIRESERVE 0xffff |
| 141 | |
| 142 | //---------------------------------------------------------------------- |
| 143 | // Section Attribute Flags (sh_flags) |
| 144 | //---------------------------------------------------------------------- |
| 145 | #define SHF_WRITE 0x1 |
| 146 | #define SHF_ALLOC 0x2 |
| 147 | #define SHF_EXECINSTR 0x4 |
| 148 | #define SHF_MASKPROC 0xf0000000 |
| 149 | |
| 150 | |
| 151 | //---------------------------------------------------------------------- |
| 152 | // Symbol Table Entry Header |
| 153 | //---------------------------------------------------------------------- |
| 154 | typedef struct Elf32_Sym_Tag |
| 155 | { |
| 156 | Elf32_Word st_name; |
| 157 | Elf32_Addr st_value; |
| 158 | Elf32_Word st_size; |
| 159 | unsigned char st_info; |
| 160 | unsigned char st_other; |
| 161 | Elf32_Half st_shndx; |
| 162 | } Elf32_Sym; |
| 163 | |
| 164 | |
| 165 | #define ELF32_ST_BIND(i) ((i)>>4) |
| 166 | #define ELF32_ST_TYPE(i) ((i)&0xf) |
| 167 | #define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf)) |
| 168 | |
| 169 | // ST_BIND |
| 170 | #define STB_LOCAL 0 |
| 171 | #define STB_GLOBAL 1 |
| 172 | #define STB_WEAK 2 |
| 173 | #define STB_LOPROC 13 |
| 174 | #define STB_HIPROC 15 |
| 175 | |
| 176 | // ST_TYPE |
| 177 | #define STT_NOTYPE 0 |
| 178 | #define STT_OBJECT 1 |
| 179 | #define STT_FUNC 2 |
| 180 | #define STT_SECTION 3 |
| 181 | #define STT_FILE 4 |
| 182 | #define STT_LOPROC 13 |
| 183 | #define STT_HIPROC 15 |
| 184 | |
| 185 | |
| 186 | //---------------------------------------------------------------------- |
| 187 | // Relocation Entries |
| 188 | //---------------------------------------------------------------------- |
| 189 | typedef struct Elf32_Rel_Tag |
| 190 | { |
| 191 | Elf32_Addr r_offset; |
| 192 | Elf32_Word r_info; |
| 193 | } Elf32_Rel; |
| 194 | |
| 195 | typedef struct Elf32_Rela_Tag |
| 196 | { |
| 197 | Elf32_Addr r_offset; |
| 198 | Elf32_Word r_info; |
| 199 | Elf32_Sword r_addend; |
| 200 | } Elf32_Rela; |
| 201 | |
| 202 | #define ELF32_R_SYM(i) ((i)>>8) |
| 203 | #define ELF32_R_TYPE(i) ((unsignedchar)(i)) |
| 204 | #define ELF32_R_INFO(s,t) (((s)<<8)+(unsignedchar)(t)) |
| 205 | |
| 206 | |
| 207 | //---------------------------------------------------------------------- |
| 208 | // Program Headers |
| 209 | //---------------------------------------------------------------------- |
| 210 | typedef struct Elf32_Phdr_Tag |
| 211 | { |
| 212 | Elf32_Word p_type; |
| 213 | Elf32_Off p_offset; |
| 214 | Elf32_Addr p_vaddr; |
| 215 | Elf32_Addr p_paddr; |
| 216 | Elf32_Word p_filesz; |
| 217 | Elf32_Word p_memsz; |
| 218 | Elf32_Word p_flags; |
| 219 | Elf32_Word p_align; |
| 220 | } Elf32_Phdr; |
| 221 | |
| 222 | //---------------------------------------------------------------------- |
| 223 | // Program Header Type (p_type) |
| 224 | //---------------------------------------------------------------------- |
| 225 | #define PT_NULL 0 |
| 226 | #define PT_LOAD 1 |
| 227 | #define PT_DYNAMIC 2 |
| 228 | #define PT_INTERP 3 |
| 229 | #define PT_NOTE 4 |
| 230 | #define PT_SHLIB 5 |
| 231 | #define PT_PHDR 6 |
| 232 | #define PT_LOPROC 0x70000000 |
| 233 | #define PT_HIPROC 0x7fffffff |
| 234 | |
| 235 | #define PF_X (1 << 0) // executable |
| 236 | #define PF_W (1 << 1) // writable |
| 237 | #define PF_R (1 << 2) // readable |
| 238 | |
| 239 | |
| 240 | #endif // __elf_h__ |