1 /* This file defines standard ELF types, structures, and macros.
 
   2    Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
 
   3    This file is part of the GNU C Library.
 
   4    Contributed by Ian Lance Taylor <ian@cygnus.com>.
 
   6    The GNU C Library is free software; you can redistribute it and/or
 
   7    modify it under the terms of the GNU Library General Public License as
 
   8    published by the Free Software Foundation; either version 2 of the
 
   9    License, or (at your option) any later version.
 
  11    The GNU C Library is distributed in the hope that it will be useful,
 
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
  14    Library General Public License for more details.
 
  16    You should have received a copy of the GNU Library General Public
 
  17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
 
  18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
  19    Boston, MA 02111-1307, USA.  */
 
  26 /* Standard ELF types.  */
 
  28 /* Type for a 16-bit quantity.  */
 
  29 typedef uint16_t Elf32_Half;
 
  30 typedef uint16_t Elf64_Half;
 
  32 /* Types for signed and unsigned 32-bit quantities.  */
 
  33 typedef uint32_t Elf32_Word;
 
  34 typedef int32_t  Elf32_Sword;
 
  35 typedef uint32_t Elf64_Word;
 
  36 typedef int32_t  Elf64_Sword;
 
  38 /* Types for signed and unsigned 64-bit quantities.  */
 
  39 typedef uint64_t Elf32_Xword;
 
  40 typedef int64_t  Elf32_Sxword;
 
  41 typedef uint64_t Elf64_Xword;
 
  42 typedef int64_t  Elf64_Sxword;
 
  44 /* Type of addresses.  */
 
  45 typedef uint32_t Elf32_Addr;
 
  46 typedef uint64_t Elf64_Addr;
 
  48 /* Type of file offsets.  */
 
  49 typedef uint32_t Elf32_Off;
 
  50 typedef uint64_t Elf64_Off;
 
  52 /* Type for section indices, which are 16-bit quantities.  */
 
  53 typedef uint16_t Elf32_Section;
 
  54 typedef uint16_t Elf64_Section;
 
  56 /* Type of symbol indices.  */
 
  57 typedef uint32_t Elf32_Symndx;
 
  58 typedef uint64_t Elf64_Symndx;
 
  61 /* The ELF file header.  This appears at the start of every ELF file.  */
 
  63 #define EI_NIDENT (16)
 
  67   unsigned char e_ident[EI_NIDENT];     /** Magic number and other info */
 
  68   Elf32_Half    e_type;                 /** Object file type */
 
  69   Elf32_Half    e_machine;              /** Architecture */
 
  70   Elf32_Word    e_version;              /** Object file version */
 
  71   Elf32_Addr    e_entry;                /** Entry point virtual address */
 
  72   Elf32_Off     e_phoff;                /** Program header table file offset */
 
  73   Elf32_Off     e_shoff;                /** Section header table file offset */
 
  74   Elf32_Word    e_flags;                /** Processor-specific flags */
 
  75   Elf32_Half    e_ehsize;               /** ELF header size in bytes */
 
  76   Elf32_Half    e_phentsize;            /** Program header table entry size */
 
  77   Elf32_Half    e_phnum;                /** Program header table entry count */
 
  78   Elf32_Half    e_shentsize;            /** Section header table entry size */
 
  79   Elf32_Half    e_shnum;                /** Section header table entry count */
 
  80   Elf32_Half    e_shstrndx;             /** Section header string table index */
 
  83 /* Fields in the e_ident array.  The EI_* macros are indices into the
 
  84    array.  The macros under each EI_* macro are the values the byte
 
  87 #define EI_MAG0         0               /* File identification byte 0 index */
 
  88 #define ELFMAG0         0x7f            /* Magic number byte 0 */
 
  90 #define EI_MAG1         1               /* File identification byte 1 index */
 
  91 #define ELFMAG1         'E'             /* Magic number byte 1 */
 
  93 #define EI_MAG2         2               /* File identification byte 2 index */
 
  94 #define ELFMAG2         'L'             /* Magic number byte 2 */
 
  96 #define EI_MAG3         3               /* File identification byte 3 index */
 
  97 #define ELFMAG3         'F'             /* Magic number byte 3 */
 
  99 /* Conglomeration of the identification bytes, for easy testing as a word.  */
 
 100 #define ELFMAG          "\177ELF"
 
 103 #define EI_CLASS        4               /* File class byte index */
 
 104 #define ELFCLASSNONE    0               /* Invalid class */
 
 105 #define ELFCLASS32      1               /* 32-bit objects */
 
 106 #define ELFCLASS64      2               /* 64-bit objects */
 
 107 #define ELFCLASSNUM     3
 
 109 #define EI_DATA         5               /* Data encoding byte index */
 
 110 #define ELFDATANONE     0               /* Invalid data encoding */
 
 111 #define ELFDATA2LSB     1               /* 2's complement, little endian */
 
 112 #define ELFDATA2MSB     2               /* 2's complement, big endian */
 
 115 #define EI_VERSION      6               /* File version byte index */
 
 116                                         /* Value must be EV_CURRENT */
 
 118 #define EI_OSABI        7               /* OS ABI identification */
 
 119 #define ELFOSABI_SYSV           0       /* UNIX System V ABI */
 
 120 #define ELFOSABI_HPUX           1       /* HP-UX */
 
 121 #define ELFOSABI_FREEBSD        9       /* Free BSD */
 
 122 #define ELFOSABI_ARM            97      /* ARM */
 
 123 #define ELFOSABI_STANDALONE     255     /* Standalone (embedded) application */
 
 125 #define EI_ABIVERSION   8               /* ABI version */
 
 127 #define EI_PAD          9               /* Byte index of padding bytes */
 
 129 /* Legal values for e_type (object file type).  */
 
 131 #define ET_NONE         0               /* No file type */
 
 132 #define ET_REL          1               /* Relocatable file */
 
 133 #define ET_EXEC         2               /* Executable file */
 
 134 #define ET_DYN          3               /* Shared object file */
 
 135 #define ET_CORE         4               /* Core file */
 
 136 #define ET_NUM          5               /* Number of defined types */
 
 137 #define ET_LOPROC       0xff00          /* Processor-specific */
 
 138 #define ET_HIPROC       0xffff          /* Processor-specific */
 
 140 /* Legal values for e_machine (architecture).  */
 
 142 #define EM_NONE          0              /* No machine */
 
 143 #define EM_M32           1              /* AT&T WE 32100 */
 
 144 #define EM_SPARC         2              /* SUN SPARC */
 
 145 #define EM_386           3              /* Intel 80386 */
 
 146 #define EM_68K           4              /* Motorola m68k family */
 
 147 #define EM_88K           5              /* Motorola m88k family */
 
 148 #define EM_486           6              /* Intel 80486 */
 
 149 #define EM_860           7              /* Intel 80860 */
 
 150 #define EM_MIPS          8              /* MIPS R3000 big-endian */
 
 151 #define EM_S370          9              /* Amdahl */
 
 152 #define EM_MIPS_RS4_BE  10              /* MIPS R4000 big-endian */
 
 153 #define EM_RS6000       11              /* RS6000 */
 
 155 #define EM_PARISC       15              /* HPPA */
 
 156 #define EM_nCUBE        16              /* nCUBE */
 
 157 #define EM_VPP500       17              /* Fujitsu VPP500 */
 
 158 #define EM_SPARC32PLUS  18              /* Sun's "v8plus" */
 
 159 #define EM_960          19              /* Intel 80960 */
 
 160 #define EM_PPC          20              /* PowerPC */
 
 162 #define EM_V800         36              /* NEC V800 series */
 
 163 #define EM_FR20         37              /* Fujitsu FR20 */
 
 164 #define EM_RH32         38              /* TRW RH32 */
 
 165 #define EM_MMA          39              /* Fujitsu MMA */
 
 166 #define EM_ARM          40              /* ARM */
 
 167 #define EM_FAKE_ALPHA   41              /* Digital Alpha */
 
 168 #define EM_SH           42              /* Hitachi SH */
 
 169 #define EM_SPARCV9      43              /* SPARC v9 64-bit */
 
 170 #define EM_TRICORE      44              /* Siemens Tricore */
 
 171 #define EM_ARC          45              /* Argonaut RISC Core */
 
 172 #define EM_H8_300       46              /* Hitachi H8/300 */
 
 173 #define EM_H8_300H      47              /* Hitachi H8/300H */
 
 174 #define EM_H8S          48              /* Hitachi H8S */
 
 175 #define EM_H8_500       49              /* Hitachi H8/500 */
 
 176 #define EM_IA_64        50              /* Intel Merced */
 
 177 #define EM_MIPS_X       51              /* Stanford MIPS-X */
 
 178 #define EM_COLDFIRE     52              /* Motorola Coldfire */
 
 179 #define EM_68HC12       53              /* Motorola M68HC12 */
 
 182 /* If it is necessary to assign new unofficial EM_* values, please
 
 183    pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the
 
 184    chances of collision with official or non-GNU unofficial values.  */
 
 186 #define EM_ALPHA        0x9026
 
 188 /* Legal values for e_version (version).  */
 
 190 #define EV_NONE         0               /* Invalid ELF version */
 
 191 #define EV_CURRENT      1               /* Current version */
 
 194 /* Section header.  */
 
 198   Elf32_Word    sh_name;                /** Section name (string tbl index) */
 
 199   Elf32_Word    sh_type;                /** Section type */
 
 200   Elf32_Word    sh_flags;               /** Section flags */
 
 201   Elf32_Addr    sh_addr;                /** Section virtual addr at execution */
 
 202   Elf32_Off     sh_offset;              /** Section file offset */
 
 203   Elf32_Word    sh_size;                /** Section size in bytes */
 
 204   Elf32_Word    sh_link;                /** Link to another section */
 
 205   Elf32_Word    sh_info;                /** Additional section information */
 
 206   Elf32_Word    sh_addralign;           /** Section alignment */
 
 207   Elf32_Word    sh_entsize;             /** Entry size if section holds table */
 
 210 /* Special section indices.  */
 
 212 #define SHN_UNDEF       0               /* Undefined section */
 
 213 #define SHN_LORESERVE   0xff00          /* Start of reserved indices */
 
 214 #define SHN_LOPROC      0xff00          /* Start of processor-specific */
 
 215 #define SHN_HIPROC      0xff1f          /* End of processor-specific */
 
 216 #define SHN_ABS         0xfff1          /* Associated symbol is absolute */
 
 217 #define SHN_COMMON      0xfff2          /* Associated symbol is common */
 
 218 #define SHN_HIRESERVE   0xffff          /* End of reserved indices */
 
 220 /* Legal values for sh_type (section type).  */
 
 222 #define SHT_NULL         0              /* Section header table entry unused */
 
 223 #define SHT_PROGBITS     1              /* Program data */
 
 224 #define SHT_SYMTAB       2              /* Symbol table */
 
 225 #define SHT_STRTAB       3              /* String table */
 
 226 #define SHT_RELA         4              /* Relocation entries with addends */
 
 227 #define SHT_HASH         5              /* Symbol hash table */
 
 228 #define SHT_DYNAMIC      6              /* Dynamic linking information */
 
 229 #define SHT_NOTE         7              /* Notes */
 
 230 #define SHT_NOBITS       8              /* Program space with no data (bss) */
 
 231 #define SHT_REL          9              /* Relocation entries, no addends */
 
 232 #define SHT_SHLIB        10             /* Reserved */
 
 233 #define SHT_DYNSYM       11             /* Dynamic linker symbol table */
 
 234 #define SHT_NUM          12             /* Number of defined types.  */
 
 235 #define SHT_LOOS         0x60000000     /* Start OS-specific */
 
 236 #define SHT_LOSUNW       0x6ffffffb     /* Sun-specific low bound.  */
 
 237 #define SHT_SUNW_COMDAT  0x6ffffffb
 
 238 #define SHT_SUNW_syminfo 0x6ffffffc
 
 239 #define SHT_GNU_verdef   0x6ffffffd     /* Version definition section.  */
 
 240 #define SHT_GNU_verneed  0x6ffffffe     /* Version needs section.  */
 
 241 #define SHT_GNU_versym   0x6fffffff     /* Version symbol table.  */
 
 242 #define SHT_HISUNW       0x6fffffff     /* Sun-specific high bound.  */
 
 243 #define SHT_HIOS         0x6fffffff     /* End OS-specific type */
 
 244 #define SHT_LOPROC       0x70000000     /* Start of processor-specific */
 
 245 #define SHT_HIPROC       0x7fffffff     /* End of processor-specific */
 
 246 #define SHT_LOUSER       0x80000000     /* Start of application-specific */
 
 247 #define SHT_HIUSER       0x8fffffff     /* End of application-specific */
 
 249 /* Legal values for sh_flags (section flags).  */
 
 251 #define SHF_WRITE       (1 << 0)        /* Writable */
 
 252 #define SHF_ALLOC       (1 << 1)        /* Occupies memory during execution */
 
 253 #define SHF_EXECINSTR   (1 << 2)        /* Executable */
 
 254 #define SHF_MASKPROC    0xf0000000      /* Processor-specific */
 
 256 /* --- Simplified ELF header --- */
 
 257 typedef struct simple_elf {
 
 258   const char *  e_fname;       /* filename of binary */
 
 259   unsigned long e_entry;       /* entry point virtual address */
 
 260   unsigned long e_txtoff;      /* offset of text segment in file */
 
 261   unsigned long e_txtlen;      /* length of text segment in bytes */
 
 262   unsigned long e_txtstart;    /* start of text segment virtual address */
 
 263   unsigned long e_datoff;      /* offset of data segment in file */
 
 264   unsigned long e_datlen;      /* length of data segment in bytes */
 
 265   unsigned long e_datstart;    /* start of data segment in virtual memory */
 
 266   unsigned long e_rodatoff;    /* offset of rodata segment in file */
 
 267   unsigned long e_rodatlen;    /* length of rodata segment in bytes */
 
 268   unsigned long e_rodatstart;  /* start of rodata segment in virtual memory*/
 
 269   unsigned long e_bsslen;      /* length of bss  segment in bytes */
 
 272  /* --- Defines --- */
 
 273 #define NMAGIC 0410 /* 0x108 */
 
 274 #define N_MAGIC(exec) ((exec).a_midmag & 0xffff)
 
 275 #define N_BADMAG(x) (N_MAGIC(x) != NMAGIC) 
 
 277 #define ELF_NOTELF  -1
 
 278 #define ELF_SUCCESS  0
 
 279 #define NOT_PRESENT  -2
 
 282  * elf function prototypes
 
 284 extern int getbytes( const char *filename, int offset, int size, char *buf);
 
 285 extern int elf_load_helper( simple_elf_t *se_hdr, const char *fname );
 
 286 extern int elf_check_header( const char *fname );