1/* Copyright (C) 2007-2010 The Android Open Source Project 2** 3** This software is licensed under the terms of the GNU General Public 4** License version 2, as published by the Free Software Foundation, and 5** may be copied, distributed, and modified under those terms. 6** 7** This program is distributed in the hope that it will be useful, 8** but WITHOUT ANY WARRANTY; without even the implied warranty of 9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10** GNU General Public License for more details. 11*/ 12 13/* 14 * Contains declarations of types, constants and structures 15 * describing ELF file format. 16 */ 17 18#ifndef ELFF_ELH_H_ 19#define ELFF_ELH_H_ 20 21extern "C" { 22#include "qemu-common.h" 23} 24#include "elff-common.h" 25 26//============================================================================= 27// ELF file definitions 28//============================================================================= 29 30/* 31 * ELF format documentation uses Elf##_Xxx notation for data types, where 32 * ## stands for CPU architecture (32, or 64 bit), and Xxx stands for a 33 * specific type. For the sake of compliance, we will follow doc's notation 34 * when defining types used in ELF file descriptors. However, for the sake of 35 * code simplicity, we will drop CPU architecture index from the types that 36 * have equal sizes on both, 32 and 64 bit architectures. 37 */ 38 39/* 40 * Architecture independent types. 41 */ 42 43typedef uint8_t Elf_Byte; 44typedef int8_t Elf_Sbyte; 45 46typedef uint16_t Elf_Half; 47typedef int16_t Elf_Shalf; 48 49typedef uint32_t Elf_Word; 50typedef int32_t Elf_Sword; 51 52typedef uint64_t Elf_Xword; 53typedef int64_t Elf_Sxword; 54 55/* 56 * Architecture dependent types. 57 */ 58 59/* 32-bit ELF address. */ 60typedef uint32_t Elf32_Addr; 61/* 32-bit ELF offset. */ 62typedef uint32_t Elf32_Off; 63 64/* 64-bit ELF address. */ 65typedef uint64_t Elf64_Addr; 66/* 64-bit ELF offset. */ 67typedef uint64_t Elf64_Off; 68 69//============================================================================= 70// ELF file header 71//============================================================================= 72 73/* Byte size of the fixed portion of ELF header. */ 74#define EI_NIDENT 16 75 76/* Common (architecture independent portion of) ELF file header, 77 * that starts at offset 0 in ELF file. 78 */ 79typedef struct Elf_CommonHdr { 80 union { 81 struct { 82 /* ei_mag0 - ei_mag3 contain ELF header signature. See ELFMAGx bellow. */ 83 Elf_Byte ei_mag0; 84 Elf_Byte ei_mag1; 85 Elf_Byte ei_mag2; 86 Elf_Byte ei_mag3; 87 88 /* File class (32, or 64 bits). See ELFCLASSxxx bellow. */ 89 Elf_Byte ei_class; 90 91 /* Data encoding (endianness). See ELFDATAxxx bellow. */ 92 Elf_Byte ei_data; 93 94 /* ELF header version number. */ 95 Elf_Byte ei_version; 96 } ei_info; 97 unsigned char e_ident[EI_NIDENT]; 98 }; 99 100 /* File type (executable, shared object, etc.) */ 101 Elf_Half e_type; 102 103 /* Processor type. */ 104 Elf_Half e_machine; 105 106 /* File version. */ 107 Elf_Word e_version; 108} Elf_CommonHdr; 109 110 111/* ELF header signature. */ 112#define ELFMAG0 0x7f 113#define ELFMAG1 'E' 114#define ELFMAG2 'L' 115#define ELFMAG3 'F' 116#define ELFMAG "\177ELF" 117#define SELFMAG 4 118 119/* 120 * Possible ei_class values. 121 */ 122 123/* Invalid. */ 124#define ELFCLASSNONE 0 125/* It's 32-bit ELF file. */ 126#define ELFCLASS32 1 127/* It's 64-bit ELF file. */ 128#define ELFCLASS64 2 129 130/* 131 * Possible ei_data values. 132 */ 133 134/* Invalid. */ 135#define ELFDATANONE 0 136/* ELF data is formatted in little-endian. */ 137#define ELFDATA2LSB 1 138/* ELF data is formatted in big-endian. */ 139#define ELFDATA2MSB 2 140 141/* Tempated (architecture dependent) ELF file header. 142 * Template param: 143 * Elf_Addr - Actual type for address encoding (Elf32_Addr, or Elf64_Addr). 144 * Elf_Off - Actual type for offset encoding (Elf32_Off, or Elf64_Off). 145 */ 146template <typename Elf_Addr, typename Elf_Off> 147struct Elf_FHdr { 148 /* Common header. */ 149 Elf_CommonHdr common; 150 151 /* Module entry point. */ 152 Elf_Addr e_entry; 153 154 /* Programm header table offset (in bytes) from the beginning of the file. 155 * Zero if there is no programm header in this file. 156 */ 157 Elf_Off e_phoff; 158 159 /* Section header table offset (in bytes) from the beginning of the file. 160 * Zero if there is no section header in this file. 161 */ 162 Elf_Off e_shoff; 163 164 /* Processor-specific flags. */ 165 Elf_Word e_flags; 166 167 /* This header size in bytes. */ 168 Elf_Half e_ehsize; 169 170 /* Byte size of an entry in programm header table. All entries 171 * in the table are the same size. 172 */ 173 Elf_Half e_phentsize; 174 175 /* Number of entries in programm header table. */ 176 Elf_Half e_phnum; 177 178 /* Byte size of an entry in section header table. All entries 179 * in the table are the same size. 180 */ 181 Elf_Half e_shentsize; 182 183 /* Number of entries in section header table. */ 184 Elf_Half e_shnum; 185 186 /* Zero-based index of an entry for name string table section in the section 187 * header table. If no such section exists in the file this field contains 188 * SHN_UNDEF value. 189 */ 190 Elf_Half e_shstrndx; 191}; 192/* 32-bit ELF header. */ 193typedef Elf_FHdr<Elf32_Addr, Elf32_Off> Elf32_FHdr; 194/* 64-bit ELF header. */ 195typedef Elf_FHdr<Elf64_Addr, Elf64_Off> Elf64_FHdr; 196 197//============================================================================= 198// ELF section header 199//============================================================================= 200 201/* Templated (architecture dependent) section header for ELF file. 202 * Template param: 203 * Elf_Addr - Actual type for address encoding (Elf32_Addr, or Elf64_Addr). 204 * Elf_Off - Actual type for offset encoding (Elf32_Off, or Elf64_Off). 205 */ 206template <typename Elf_Addr, typename Elf_Off> 207struct Elf_SHdr { 208 /* Index (byte offset) of section name in the name string table section. */ 209 Elf_Word sh_name; 210 211 /* Section type and semantics. */ 212 Elf_Word sh_type; 213 214 /* Section flags and attributes. */ 215 Elf_Word sh_flags; 216 217 /* Section address in the memory image of the process. */ 218 Elf_Addr sh_addr; 219 220 /* Byte offset from the beginning of the ELF file to the first 221 * byte in the section. 222 */ 223 Elf_Off sh_offset; 224 225 /* Section size in bytes. */ 226 Elf_Word sh_size; 227 228 /* Section header table index link. Depends on section type. */ 229 Elf_Word sh_link; 230 231 /* Extra section information, depending on the section type. */ 232 Elf_Word sh_info; 233 234 /* Address alignment constrains. 0 and 1 means that section has no 235 * alignment constrains. 236 */ 237 Elf_Word sh_addralign; 238 239 /* Entry size for sections that hold some kind of a table. */ 240 Elf_Word sh_entsize; 241}; 242/* 32-bit section header. */ 243typedef Elf_SHdr<Elf32_Addr, Elf32_Off> Elf32_SHdr; 244/* 64-bit section header. */ 245typedef Elf_SHdr<Elf64_Addr, Elf64_Off> Elf64_SHdr; 246 247/* 248 * Special section indices 249 */ 250#define SHN_UNDEF 0 251#define SHN_LORESERVE 0xff00 252#define SHN_LOPROC 0xff00 253#define SHN_HIPROC 0xff1f 254#define SHN_LOOS 0xff20 255#define SHN_HIOS 0xff3f 256#define SHN_ABS 0xfff1 257#define SHN_COMMON 0xfff2 258#define SHN_XINDEX 0xffff 259#define SHN_HIRESERVE 0xffff 260 261/* 262 * Values for sh_type 263 */ 264#define SHT_NULL 0 265#define SHT_PROGBITS 1 266#define SHT_SYMTAB 2 267#define SHT_STRTAB 3 268#define SHT_RELA 4 269#define SHT_HASH 5 270#define SHT_DYNAMIC 6 271#define SHT_NOTE 7 272#define SHT_NOBITS 8 273#define SHT_REL 9 274#define SHT_SHLIB 10 275#define SHT_DYNSYM 11 276#define SHT_INIT_ARRAY 14 277#define SHT_FINI_ARRAY 15 278#define SHT_PREINIT_ARRAY 16 279#define SHT_GROUP 17 280#define SHT_SYMTAB_SHNDX 18 281#define SHT_NUM 19 282 283#endif // ELFF_ELH_H_ 284