1 /* 2 * GRUB -- GRand Unified Bootloader 3 * Copyright (C) 2001,2002 Free Software Foundation, Inc. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 */ 19 20 /* 32-bit data types */ 21 22 typedef unsigned long Elf32_Addr; 23 typedef unsigned short Elf32_Half; 24 typedef unsigned long Elf32_Off; 25 typedef signed long Elf32_Sword; 26 typedef unsigned long Elf32_Word; 27 /* "unsigned char" already exists */ 28 29 /* ELF header */ 30 typedef struct 31 { 32 33 #define EI_NIDENT 16 34 35 /* first four characters are defined below */ 36 #define EI_MAG0 0 37 #define ELFMAG0 0x7f 38 #define EI_MAG1 1 39 #define ELFMAG1 'E' 40 #define EI_MAG2 2 41 #define ELFMAG2 'L' 42 #define EI_MAG3 3 43 #define ELFMAG3 'F' 44 45 #define EI_CLASS 4 /* data sizes */ 46 #define ELFCLASS32 1 /* i386 -- up to 32-bit data sizes present */ 47 48 #define EI_DATA 5 /* data type and ordering */ 49 #define ELFDATA2LSB 1 /* i386 -- LSB 2's complement */ 50 51 #define EI_VERSION 6 /* version number. "e_version" must be the same */ 52 #define EV_CURRENT 1 /* current version number */ 53 54 #define EI_OSABI 7 /* operating system/ABI indication */ 55 #define ELFOSABI_FREEBSD 9 56 57 #define EI_ABIVERSION 8 /* ABI version */ 58 59 #define EI_PAD 9 /* from here in is just padding */ 60 61 #define EI_BRAND 8 /* start of OS branding (This is 62 obviously illegal against the ELF 63 standard.) */ 64 65 unsigned char e_ident[EI_NIDENT]; /* basic identification block */ 66 67 #define ET_EXEC 2 /* we only care about executable types */ 68 Elf32_Half e_type; /* file types */ 69 70 #define EM_386 3 /* i386 -- obviously use this one */ 71 Elf32_Half e_machine; /* machine types */ 72 Elf32_Word e_version; /* use same as "EI_VERSION" above */ 73 Elf32_Addr e_entry; /* entry point of the program */ 74 Elf32_Off e_phoff; /* program header table file offset */ 75 Elf32_Off e_shoff; /* section header table file offset */ 76 Elf32_Word e_flags; /* flags */ 77 Elf32_Half e_ehsize; /* elf header size in bytes */ 78 Elf32_Half e_phentsize; /* program header entry size */ 79 Elf32_Half e_phnum; /* number of entries in program header */ 80 Elf32_Half e_shentsize; /* section header entry size */ 81 Elf32_Half e_shnum; /* number of entries in section header */ 82 83 #define SHN_UNDEF 0 84 #define SHN_LORESERVE 0xff00 85 #define SHN_LOPROC 0xff00 86 #define SHN_HIPROC 0xff1f 87 #define SHN_ABS 0xfff1 88 #define SHN_COMMON 0xfff2 89 #define SHN_HIRESERVE 0xffff 90 Elf32_Half e_shstrndx; /* section header table index */ 91 } 92 Elf32_Ehdr; 93 94 95 #define BOOTABLE_I386_ELF(h) \ 96 ((h.e_ident[EI_MAG0] == ELFMAG0) & (h.e_ident[EI_MAG1] == ELFMAG1) \ 97 & (h.e_ident[EI_MAG2] == ELFMAG2) & (h.e_ident[EI_MAG3] == ELFMAG3) \ 98 & (h.e_ident[EI_CLASS] == ELFCLASS32) & (h.e_ident[EI_DATA] == ELFDATA2LSB) \ 99 & (h.e_ident[EI_VERSION] == EV_CURRENT) & (h.e_type == ET_EXEC) \ 100 & (h.e_machine == EM_386) & (h.e_version == EV_CURRENT)) 101 102 /* section table - ? */ 103 typedef struct 104 { 105 Elf32_Word sh_name; /* Section name (string tbl index) */ 106 Elf32_Word sh_type; /* Section type */ 107 Elf32_Word sh_flags; /* Section flags */ 108 Elf32_Addr sh_addr; /* Section virtual addr at execution */ 109 Elf32_Off sh_offset; /* Section file offset */ 110 Elf32_Word sh_size; /* Section size in bytes */ 111 Elf32_Word sh_link; /* Link to another section */ 112 Elf32_Word sh_info; /* Additional section information */ 113 Elf32_Word sh_addralign; /* Section alignment */ 114 Elf32_Word sh_entsize; /* Entry size if section holds table */ 115 } 116 Elf32_Shdr; 117 118 /* symbol table - page 4-25, figure 4-15 */ 119 typedef struct 120 { 121 Elf32_Word st_name; 122 Elf32_Addr st_value; 123 Elf32_Word st_size; 124 unsigned char st_info; 125 unsigned char st_other; 126 Elf32_Half st_shndx; 127 } 128 Elf32_Sym; 129 130 /* symbol type and binding attributes - page 4-26 */ 131 132 #define ELF32_ST_BIND(i) ((i) >> 4) 133 #define ELF32_ST_TYPE(i) ((i) & 0xf) 134 #define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf)) 135 136 /* symbol binding - page 4-26, figure 4-16 */ 137 138 #define STB_LOCAL 0 139 #define STB_GLOBAL 1 140 #define STB_WEAK 2 141 #define STB_LOPROC 13 142 #define STB_HIPROC 15 143 144 /* symbol types - page 4-28, figure 4-17 */ 145 146 #define STT_NOTYPE 0 147 #define STT_OBJECT 1 148 #define STT_FUNC 2 149 #define STT_SECTION 3 150 #define STT_FILE 4 151 #define STT_LOPROC 13 152 #define STT_HIPROC 15 153 154 155 /* Macros to split/combine relocation type and symbol page 4-32 */ 156 157 #define ELF32_R_SYM(__i) ((__i)>>8) 158 #define ELF32_R_TYPE(__i) ((unsigned char) (__i)) 159 #define ELF32_R_INFO(__s, __t) (((__s)<<8) + (unsigned char) (__t)) 160 161 162 /* program header - page 5-2, figure 5-1 */ 163 164 typedef struct 165 { 166 Elf32_Word p_type; 167 Elf32_Off p_offset; 168 Elf32_Addr p_vaddr; 169 Elf32_Addr p_paddr; 170 Elf32_Word p_filesz; 171 Elf32_Word p_memsz; 172 Elf32_Word p_flags; 173 Elf32_Word p_align; 174 } 175 Elf32_Phdr; 176 177 /* segment types - page 5-3, figure 5-2 */ 178 179 #define PT_NULL 0 180 #define PT_LOAD 1 181 #define PT_DYNAMIC 2 182 #define PT_INTERP 3 183 #define PT_NOTE 4 184 #define PT_SHLIB 5 185 #define PT_PHDR 6 186 187 #define PT_LOPROC 0x70000000 188 #define PT_HIPROC 0x7fffffff 189 190 /* segment permissions - page 5-6 */ 191 192 #define PF_X 0x1 193 #define PF_W 0x2 194 #define PF_R 0x4 195 #define PF_MASKPROC 0xf0000000 196 197 198 /* dynamic structure - page 5-15, figure 5-9 */ 199 200 typedef struct 201 { 202 Elf32_Sword d_tag; 203 union 204 { 205 Elf32_Word d_val; 206 Elf32_Addr d_ptr; 207 } 208 d_un; 209 } 210 Elf32_Dyn; 211 212 /* Dynamic array tags - page 5-16, figure 5-10. */ 213 214 #define DT_NULL 0 215 #define DT_NEEDED 1 216 #define DT_PLTRELSZ 2 217 #define DT_PLTGOT 3 218 #define DT_HASH 4 219 #define DT_STRTAB 5 220 #define DT_SYMTAB 6 221 #define DT_RELA 7 222 #define DT_RELASZ 8 223 #define DT_RELAENT 9 224 #define DT_STRSZ 10 225 #define DT_SYMENT 11 226 #define DT_INIT 12 227 #define DT_FINI 13 228 #define DT_SONAME 14 229 #define DT_RPATH 15 230 #define DT_SYMBOLIC 16 231 #define DT_REL 17 232 #define DT_RELSZ 18 233 #define DT_RELENT 19 234 #define DT_PLTREL 20 235 #define DT_DEBUG 21 236 #define DT_TEXTREL 22 237 #define DT_JMPREL 23 238