elf32.h revision 25984
1/*- 2 * Copyright (c) 1996-1997 John D. Polstra. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id$ 27 */ 28 29#ifndef _SYS_ELF32_H_ 30#define _SYS_ELF32_H_ 1 31 32/* 33 * ELF definitions common to all 32-bit architectures. 34 */ 35 36typedef u_int32_t Elf32_Addr; 37typedef u_int16_t Elf32_Half; 38typedef u_int32_t Elf32_Off; 39typedef int32_t Elf32_Sword; 40typedef u_int32_t Elf32_Word; 41 42/* 43 * ELF header. 44 */ 45 46#define EI_NIDENT 16 /* Size of e_ident array. */ 47 48typedef struct { 49 unsigned char e_ident[EI_NIDENT]; /* File identification. */ 50 Elf32_Half e_type; /* File type. */ 51 Elf32_Half e_machine; /* Machine architecture. */ 52 Elf32_Word e_version; /* ELF format version. */ 53 Elf32_Addr e_entry; /* Entry point. */ 54 Elf32_Off e_phoff; /* Program header file offset. */ 55 Elf32_Off e_shoff; /* Section header file offset. */ 56 Elf32_Word e_flags; /* Architecture-specific flags. */ 57 Elf32_Half e_ehsize; /* Size of ELF header in bytes. */ 58 Elf32_Half e_phentsize; /* Size of program header entry. */ 59 Elf32_Half e_phnum; /* Number of program header entries. */ 60 Elf32_Half e_shentsize; /* Size of section header entry. */ 61 Elf32_Half e_shnum; /* Number of section header entries. */ 62 Elf32_Half e_shstrndx; /* Section name strings section. */ 63} Elf32_Ehdr; 64 65/* Indexes into the e_ident array. */ 66#define EI_MAG0 0 /* Magic number, byte 0. */ 67#define EI_MAG1 1 /* Magic number, byte 1. */ 68#define EI_MAG2 2 /* Magic number, byte 2. */ 69#define EI_MAG3 3 /* Magic number, byte 3. */ 70#define EI_CLASS 4 /* Class of machine. */ 71#define EI_DATA 5 /* Data format. */ 72#define EI_VERSION 6 /* ELF format version. */ 73#define EI_PAD 7 /* Start of padding (per SVR4 ABI). */ 74#define EI_BRAND 8 /* Start of architecture identification. */ 75 76/* Values for the magic number bytes. */ 77#define ELFMAG0 0x7f 78#define ELFMAG1 'E' 79#define ELFMAG2 'L' 80#define ELFMAG3 'F' 81 82/* Values for e_ident[EI_VERSION] and e_version. */ 83#define EV_NONE 0 84#define EV_CURRENT 1 85 86/* Values for e_ident[EI_CLASS]. */ 87#define ELFCLASSNONE 0 /* Unknown class. */ 88#define ELFCLASS32 1 /* 32-bit architecture. */ 89#define ELFCLASS64 2 /* 64-bit architecture. */ 90 91/* Values for e_ident[EI_DATA]. */ 92#define ELFDATANONE 0 /* Unknown data format. */ 93#define ELFDATA2LSB 1 /* 2's complement little-endian. */ 94#define ELFDATA2MSB 2 /* 2's complement big-endian. */ 95 96/* Values for e_type. */ 97#define ET_NONE 0 /* Unknown type. */ 98#define ET_REL 1 /* Relocatable. */ 99#define ET_EXEC 2 /* Executable. */ 100#define ET_DYN 3 /* Shared object. */ 101#define ET_CORE 4 /* Core file. */ 102 103/* Values for e_machine. */ 104#define EM_NONE 0 /* Unknown machine. */ 105#define EM_M32 1 /* AT&T WE32100. */ 106#define EM_SPARC 2 /* Sun SPARC. */ 107#define EM_386 3 /* Intel i386. */ 108#define EM_68K 4 /* Motorola 68000. */ 109#define EM_88K 5 /* Motorola 88000. */ 110#define EM_486 6 /* Intel i486. */ 111#define EM_860 7 /* Intel i860. */ 112 113/* 114 * Section header. 115 */ 116 117typedef struct { 118 Elf32_Word sh_name; /* Section name (index into the 119 section header string table). */ 120 Elf32_Word sh_type; /* Section type. */ 121 Elf32_Word sh_flags; /* Section flags. */ 122 Elf32_Addr sh_addr; /* Address in memory image. */ 123 Elf32_Off sh_offset; /* Offset in file. */ 124 Elf32_Word sh_size; /* Size in bytes. */ 125 Elf32_Word sh_link; /* Index of a related section. */ 126 Elf32_Word sh_info; /* Depends on section type. */ 127 Elf32_Word sh_addralign; /* Alignment in bytes. */ 128 Elf32_Word sh_entsize; /* Size of each entry in section. */ 129} Elf32_Shdr; 130 131/* Special section indexes. */ 132#define SHN_UNDEF 0 /* Undefined, missing, irrelevant. */ 133#define SHN_LORESERVE 0xff00 /* First of reserved range. */ 134#define SHN_LOPROC 0xff00 /* First processor-specific. */ 135#define SHN_HIPROC 0xff1f /* Last processor-specific. */ 136#define SHN_ABS 0xfff1 /* Absolute values. */ 137#define SHN_COMMON 0xfff2 /* Common data. */ 138#define SHN_HIRESERVE 0xffff /* Last of reserved range. */ 139 140/* 141 * Program header. 142 */ 143 144typedef struct { 145 Elf32_Word p_type; /* Entry type. */ 146 Elf32_Off p_offset; /* File offset of contents. */ 147 Elf32_Addr p_vaddr; /* Virtual address in memory image. */ 148 Elf32_Addr p_paddr; /* Physical address (not used). */ 149 Elf32_Word p_filesz; /* Size of contents in file. */ 150 Elf32_Word p_memsz; /* Size of contents in memory. */ 151 Elf32_Word p_flags; /* Access permission flags. */ 152 Elf32_Word p_align; /* Alignment in memory and file. */ 153} Elf32_Phdr; 154 155/* Values for p_type. */ 156#define PT_NULL 0 /* Unused entry. */ 157#define PT_LOAD 1 /* Loadable segment. */ 158#define PT_DYNAMIC 2 /* Dynamic linking information segment. */ 159#define PT_INTERP 3 /* Pathname of interpreter. */ 160#define PT_NOTE 4 /* Auxiliary information. */ 161#define PT_SHLIB 5 /* Reserved (not used). */ 162#define PT_PHDR 6 /* Location of program header itself. */ 163 164#define PT_COUNT 7 /* Number of defined p_type values. */ 165 166/* Values for p_flags. */ 167#define PF_X 0x1 /* Executable. */ 168#define PF_W 0x2 /* Writable. */ 169#define PF_R 0x4 /* Readable. */ 170 171/* 172 * Dynamic structure. The ".dynamic" section contains an array of them. 173 */ 174 175typedef struct { 176 Elf32_Sword d_tag; /* Entry type. */ 177 union { 178 Elf32_Word d_val; /* Integer value. */ 179 Elf32_Addr d_ptr; /* Address value. */ 180 } d_un; 181} Elf32_Dyn; 182 183/* Values for d_tag. */ 184#define DT_NULL 0 /* Terminating entry. */ 185#define DT_NEEDED 1 /* String table offset of a needed shared 186 library. */ 187#define DT_PLTRELSZ 2 /* Total size in bytes of PLT relocations. */ 188#define DT_PLTGOT 3 /* Processor-dependent address. */ 189#define DT_HASH 4 /* Address of symbol hash table. */ 190#define DT_STRTAB 5 /* Address of string table. */ 191#define DT_SYMTAB 6 /* Address of symbol table. */ 192#define DT_RELA 7 /* Address of Elf32_Rela relocations. */ 193#define DT_RELASZ 8 /* Total size of Elf32_Rela relocations. */ 194#define DT_RELAENT 9 /* Size of each Elf32_Rela relocation entry. */ 195#define DT_STRSZ 10 /* Size of string table. */ 196#define DT_SYMENT 11 /* Size of each symbol table entry. */ 197#define DT_INIT 12 /* Address of initialization function. */ 198#define DT_FINI 13 /* Address of finalization function. */ 199#define DT_SONAME 14 /* String table offset of shared object 200 name. */ 201#define DT_RPATH 15 /* String table offset of library path. */ 202#define DT_SYMBOLIC 16 /* Indicates "symbolic" linking. */ 203#define DT_REL 17 /* Address of Elf32_Rel relocations. */ 204#define DT_RELSZ 18 /* Total size of Elf32_Rel relocations. */ 205#define DT_RELENT 19 /* Size of each Elf32_Rel relocation. */ 206#define DT_PLTREL 20 /* Type of relocation used for PLT. */ 207#define DT_DEBUG 21 /* Reserved (not used). */ 208#define DT_TEXTREL 22 /* Indicates there may be relocations in 209 non-writable segments. */ 210#define DT_JMPREL 23 /* Address of PLT relocations. */ 211 212#define DT_COUNT 24 /* Number of defined d_tag values. */ 213 214/* 215 * Relocation entries. 216 */ 217 218/* Relocations that don't need an addend field. */ 219typedef struct { 220 Elf32_Addr r_offset; /* Location to be relocated. */ 221 Elf32_Word r_info; /* Relocation type and symbol index. */ 222} Elf32_Rel; 223 224/* Relocations that need an addend field. */ 225typedef struct { 226 Elf32_Addr r_offset; /* Location to be relocated. */ 227 Elf32_Word r_info; /* Relocation type and symbol index. */ 228 Elf32_Sword r_addend; /* Addend. */ 229} Elf32_Rela; 230 231/* Macros for accessing the fields of r_info. */ 232#define ELF32_R_SYM(info) ((info) >> 8) 233#define ELF32_R_TYPE(info) ((unsigned char)(info)) 234 235/* Macro for constructing r_info from field values. */ 236#define ELF32_R_INFO(sym, type) (((sym) << 8) + (unsigned char)(type)) 237 238/* 239 * Symbol table entries. 240 */ 241 242typedef struct { 243 Elf32_Word st_name; /* String table index of name. */ 244 Elf32_Addr st_value; /* Symbol value. */ 245 Elf32_Word st_size; /* Size of associated object. */ 246 unsigned char st_info; /* Type and binding information. */ 247 unsigned char st_other; /* Reserved (not used). */ 248 Elf32_Half st_shndx; /* Section index of symbol. */ 249} Elf32_Sym; 250 251/* Macros for accessing the fields of st_info. */ 252#define ELF32_ST_BIND(info) ((info) >> 4) 253#define ELF32_ST_TYPE(info) ((info) & 0xf) 254 255/* Macro for constructing st_info from field values. */ 256#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) 257 258/* Symbol types. */ 259#define STT_NOTYPE 0 /* Unspecified type. */ 260#define STT_OBJECT 1 /* Data object. */ 261#define STT_FUNC 2 /* Function. */ 262#define STT_SECTION 3 /* Section. */ 263#define STT_FILE 4 /* Source file. */ 264 265/* Special symbol table indexes. */ 266#define STN_UNDEF 0 /* Undefined symbol index. */ 267 268#endif /* !_SYS_ELF32_H_ */ 269