00001 /* 00002 * Copyright (c) 2006 Sergey Bondari 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * - Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * - Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * - The name of the author may not be used to endorse or promote products 00015 * derived from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00035 #ifndef ELF_H_ 00036 #define ELF_H_ 00037 00038 #include <arch/elf.h> 00039 #include <sys/types.h> 00040 00044 #define EV_CURRENT 1 00045 00049 #define ET_NONE 0 /* No type */ 00050 #define ET_REL 1 /* Relocatable file */ 00051 #define ET_EXEC 2 /* Executable */ 00052 #define ET_DYN 3 /* Shared object */ 00053 #define ET_CORE 4 /* Core */ 00054 #define ET_LOPROC 0xff00 /* Processor specific */ 00055 #define ET_HIPROC 0xffff /* Processor specific */ 00056 00060 #define EM_NO 0 /* No machine */ 00061 #define EM_SPARC 2 /* SPARC */ 00062 #define EM_386 3 /* i386 */ 00063 #define EM_MIPS 8 /* MIPS RS3000 */ 00064 #define EM_MIPS_RS3_LE 10 /* MIPS RS3000 LE */ 00065 #define EM_PPC 20 /* PPC32 */ 00066 #define EM_PPC64 21 /* PPC64 */ 00067 #define EM_ARM 40 /* ARM */ 00068 #define EM_SPARCV9 43 /* SPARC64 */ 00069 #define EM_IA_64 50 /* IA-64 */ 00070 #define EM_X86_64 62 /* AMD64/EMT64 */ 00071 00075 #define EI_MAG0 0 00076 #define EI_MAG1 1 00077 #define EI_MAG2 2 00078 #define EI_MAG3 3 00079 #define EI_CLASS 4 /* File class */ 00080 #define EI_DATA 5 /* Data encoding */ 00081 #define EI_VERSION 6 /* File version */ 00082 #define EI_OSABI 7 00083 #define EI_ABIVERSION 8 00084 #define EI_PAD 9 /* Start of padding bytes */ 00085 #define EI_NIDENT 16 /* ELF identification table size */ 00086 00090 #define ELFMAG0 0x7f 00091 #define ELFMAG1 'E' 00092 #define ELFMAG2 'L' 00093 #define ELFMAG3 'F' 00094 00098 #define ELFCLASSNONE 0 00099 #define ELFCLASS32 1 00100 #define ELFCLASS64 2 00101 00105 #define ELFDATANONE 0 00106 #define ELFDATA2LSB 1 /* Least significant byte first (little endian) */ 00107 #define ELFDATA2MSB 2 /* Most signigicant byte first (big endian) */ 00108 00112 #define EE_OK 0 /* No error */ 00113 #define EE_INVALID 1 /* Invalid ELF image */ 00114 #define EE_MEMORY 2 /* Cannot allocate address space */ 00115 #define EE_INCOMPATIBLE 3 /* ELF image is not compatible with current architecture */ 00116 #define EE_UNSUPPORTED 4 /* Non-supported ELF (e.g. dynamic ELFs) */ 00117 #define EE_IRRECOVERABLE 5 00118 00122 #define SHT_NULL 0 00123 #define SHT_PROGBITS 1 00124 #define SHT_SYMTAB 2 00125 #define SHT_STRTAB 3 00126 #define SHT_RELA 4 00127 #define SHT_HASH 5 00128 #define SHT_DYNAMIC 6 00129 #define SHT_NOTE 7 00130 #define SHT_NOBITS 8 00131 #define SHT_REL 9 00132 #define SHT_SHLIB 10 00133 #define SHT_DYNSYM 11 00134 #define SHT_LOOS 0x60000000 00135 #define SHT_HIOS 0x6fffffff 00136 #define SHT_LOPROC 0x70000000 00137 #define SHT_HIPROC 0x7fffffff 00138 #define SHT_LOUSER 0x80000000 00139 #define SHT_HIUSER 0xffffffff 00140 00144 #define SHF_WRITE 0x1 00145 #define SHF_ALLOC 0x2 00146 #define SHF_EXECINSTR 0x4 00147 #define SHF_TLS 0x400 00148 #define SHF_MASKPROC 0xf0000000 00149 00153 #define STB_LOCAL 0 00154 #define STB_GLOBAL 1 00155 #define STB_WEAK 2 00156 #define STB_LOPROC 13 00157 #define STB_HIPROC 15 00158 00162 #define STT_NOTYPE 0 00163 #define STT_OBJECT 1 00164 #define STT_FUNC 2 00165 #define STT_SECTION 3 00166 #define STT_FILE 4 00167 #define STT_LOPROC 13 00168 #define STT_HIPROC 15 00169 00173 #define PT_NULL 0 00174 #define PT_LOAD 1 00175 #define PT_DYNAMIC 2 00176 #define PT_INTERP 3 00177 #define PT_NOTE 4 00178 #define PT_SHLIB 5 00179 #define PT_PHDR 6 00180 #define PT_LOPROC 0x70000000 00181 #define PT_HIPROC 0x7fffffff 00182 00186 #define PF_X 1 00187 #define PF_W 2 00188 #define PF_R 4 00189 00197 typedef uint64_t elf_xword; 00198 typedef int64_t elf_sxword; 00199 typedef uint32_t elf_word; 00200 typedef int32_t elf_sword; 00201 typedef uint16_t elf_half; 00202 00208 typedef uint32_t elf32_addr; 00209 typedef uint32_t elf32_off; 00210 00216 typedef uint64_t elf64_addr; 00217 typedef uint64_t elf64_off; 00218 00220 struct elf32_header { 00221 uint8_t e_ident[EI_NIDENT]; 00222 elf_half e_type; 00223 elf_half e_machine; 00224 elf_word e_version; 00225 elf32_addr e_entry; 00226 elf32_off e_phoff; 00227 elf32_off e_shoff; 00228 elf_word e_flags; 00229 elf_half e_ehsize; 00230 elf_half e_phentsize; 00231 elf_half e_phnum; 00232 elf_half e_shentsize; 00233 elf_half e_shnum; 00234 elf_half e_shstrndx; 00235 }; 00236 struct elf64_header { 00237 uint8_t e_ident[EI_NIDENT]; 00238 elf_half e_type; 00239 elf_half e_machine; 00240 elf_word e_version; 00241 elf64_addr e_entry; 00242 elf64_off e_phoff; 00243 elf64_off e_shoff; 00244 elf_word e_flags; 00245 elf_half e_ehsize; 00246 elf_half e_phentsize; 00247 elf_half e_phnum; 00248 elf_half e_shentsize; 00249 elf_half e_shnum; 00250 elf_half e_shstrndx; 00251 }; 00252 00253 /* 00254 * ELF segment header. 00255 * Segments headers are also known as program headers. 00256 */ 00257 struct elf32_segment_header { 00258 elf_word p_type; 00259 elf32_off p_offset; 00260 elf32_addr p_vaddr; 00261 elf32_addr p_paddr; 00262 elf_word p_filesz; 00263 elf_word p_memsz; 00264 elf_word p_flags; 00265 elf_word p_align; 00266 }; 00267 struct elf64_segment_header { 00268 elf_word p_type; 00269 elf_word p_flags; 00270 elf64_off p_offset; 00271 elf64_addr p_vaddr; 00272 elf64_addr p_paddr; 00273 elf_xword p_filesz; 00274 elf_xword p_memsz; 00275 elf_xword p_align; 00276 }; 00277 00278 /* 00279 * ELF section header 00280 */ 00281 struct elf32_section_header { 00282 elf_word sh_name; 00283 elf_word sh_type; 00284 elf_word sh_flags; 00285 elf32_addr sh_addr; 00286 elf32_off sh_offset; 00287 elf_word sh_size; 00288 elf_word sh_link; 00289 elf_word sh_info; 00290 elf_word sh_addralign; 00291 elf_word sh_entsize; 00292 }; 00293 struct elf64_section_header { 00294 elf_word sh_name; 00295 elf_word sh_type; 00296 elf_xword sh_flags; 00297 elf64_addr sh_addr; 00298 elf64_off sh_offset; 00299 elf_xword sh_size; 00300 elf_word sh_link; 00301 elf_word sh_info; 00302 elf_xword sh_addralign; 00303 elf_xword sh_entsize; 00304 }; 00305 00306 /* 00307 * ELF symbol table entry 00308 */ 00309 struct elf32_symbol { 00310 elf_word st_name; 00311 elf32_addr st_value; 00312 elf_word st_size; 00313 uint8_t st_info; 00314 uint8_t st_other; 00315 elf_half st_shndx; 00316 }; 00317 struct elf64_symbol { 00318 elf_word st_name; 00319 uint8_t st_info; 00320 uint8_t st_other; 00321 elf_half st_shndx; 00322 elf64_addr st_value; 00323 elf_xword st_size; 00324 }; 00325 00326 #ifdef __32_BITS__ 00327 typedef struct elf32_header elf_header_t; 00328 typedef struct elf32_segment_header elf_segment_header_t; 00329 typedef struct elf32_section_header elf_section_header_t; 00330 typedef struct elf32_symbol elf_symbol_t; 00331 #endif 00332 #ifdef __64_BITS__ 00333 typedef struct elf64_header elf_header_t; 00334 typedef struct elf64_segment_header elf_segment_header_t; 00335 typedef struct elf64_section_header elf_section_header_t; 00336 typedef struct elf64_symbol elf_symbol_t; 00337 #endif 00338 00339 extern const char *elf_error(unsigned int rc); 00340 00341 #endif 00342