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 <arch/types.h> 00040 #include <typedefs.h> 00041 00045 #define EV_CURRENT 1 00046 00050 #define ET_NONE 0 /* No type */ 00051 #define ET_REL 1 /* Relocatable file */ 00052 #define ET_EXEC 2 /* Executable */ 00053 #define ET_DYN 3 /* Shared object */ 00054 #define ET_CORE 4 /* Core */ 00055 #define ET_LOPROC 0xff00 /* Processor specific */ 00056 #define ET_HIPROC 0xffff /* Processor specific */ 00057 00061 #define EM_NO 0 /* No machine */ 00062 #define EM_SPARC 2 /* SPARC */ 00063 #define EM_386 3 /* i386 */ 00064 #define EM_MIPS 8 /* MIPS RS3000 */ 00065 #define EM_MIPS_RS3_LE 10 /* MIPS RS3000 LE */ 00066 #define EM_PPC 20 /* PPC32 */ 00067 #define EM_PPC64 21 /* PPC64 */ 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_MASKPROC 0xf0000000 00148 00152 #define STB_LOCAL 0 00153 #define STB_GLOBAL 1 00154 #define STB_WEAK 2 00155 #define STB_LOPROC 13 00156 #define STB_HIPROC 15 00157 00161 #define STT_NOTYPE 0 00162 #define STT_OBJECT 1 00163 #define STT_FUNC 2 00164 #define STT_SECTION 3 00165 #define STT_FILE 4 00166 #define STT_LOPROC 13 00167 #define STT_HIPROC 15 00168 00172 #define PT_NULL 0 00173 #define PT_LOAD 1 00174 #define PT_DYNAMIC 2 00175 #define PT_INTERP 3 00176 #define PT_NOTE 4 00177 #define PT_SHLIB 5 00178 #define PT_PHDR 6 00179 #define PT_LOPROC 0x70000000 00180 #define PT_HIPROC 0x7fffffff 00181 00185 #define PF_X 1 00186 #define PF_W 2 00187 #define PF_R 4 00188 00196 typedef __u64 elf_xword; 00197 typedef __s64 elf_sxword; 00198 typedef __u32 elf_word; 00199 typedef __s32 elf_sword; 00200 typedef __u16 elf_half; 00201 00207 typedef __u32 elf32_addr; 00208 typedef __u32 elf32_off; 00209 00215 typedef __u64 elf64_addr; 00216 typedef __u64 elf64_off; 00217 00219 struct elf32_header { 00220 __u8 e_ident[EI_NIDENT]; 00221 elf_half e_type; 00222 elf_half e_machine; 00223 elf_word e_version; 00224 elf32_addr e_entry; 00225 elf32_off e_phoff; 00226 elf32_off e_shoff; 00227 elf_word e_flags; 00228 elf_half e_ehsize; 00229 elf_half e_phentsize; 00230 elf_half e_phnum; 00231 elf_half e_shentsize; 00232 elf_half e_shnum; 00233 elf_half e_shstrndx; 00234 }; 00235 struct elf64_header { 00236 __u8 e_ident[EI_NIDENT]; 00237 elf_half e_type; 00238 elf_half e_machine; 00239 elf_word e_version; 00240 elf64_addr e_entry; 00241 elf64_off e_phoff; 00242 elf64_off e_shoff; 00243 elf_word e_flags; 00244 elf_half e_ehsize; 00245 elf_half e_phentsize; 00246 elf_half e_phnum; 00247 elf_half e_shentsize; 00248 elf_half e_shnum; 00249 elf_half e_shstrndx; 00250 }; 00251 00252 /* 00253 * ELF segment header. 00254 * Segments headers are also known as program headers. 00255 */ 00256 struct elf32_segment_header { 00257 elf_word p_type; 00258 elf32_off p_offset; 00259 elf32_addr p_vaddr; 00260 elf32_addr p_paddr; 00261 elf_word p_filesz; 00262 elf_word p_memsz; 00263 elf_word p_flags; 00264 elf_word p_align; 00265 }; 00266 struct elf64_segment_header { 00267 elf_word p_type; 00268 elf_word p_flags; 00269 elf64_off p_offset; 00270 elf64_addr p_vaddr; 00271 elf64_addr p_paddr; 00272 elf_xword p_filesz; 00273 elf_xword p_memsz; 00274 elf_xword p_align; 00275 }; 00276 00277 /* 00278 * ELF section header 00279 */ 00280 struct elf32_section_header { 00281 elf_word sh_name; 00282 elf_word sh_type; 00283 elf_word sh_flags; 00284 elf32_addr sh_addr; 00285 elf32_off sh_offset; 00286 elf_word sh_size; 00287 elf_word sh_link; 00288 elf_word sh_info; 00289 elf_word sh_addralign; 00290 elf_word sh_entsize; 00291 }; 00292 struct elf64_section_header { 00293 elf_word sh_name; 00294 elf_word sh_type; 00295 elf_xword sh_flags; 00296 elf64_addr sh_addr; 00297 elf64_off sh_offset; 00298 elf_xword sh_size; 00299 elf_word sh_link; 00300 elf_word sh_info; 00301 elf_xword sh_addralign; 00302 elf_xword sh_entsize; 00303 }; 00304 00305 /* 00306 * ELF symbol table entry 00307 */ 00308 struct elf32_symbol { 00309 elf_word st_name; 00310 elf32_addr st_value; 00311 elf_word st_size; 00312 __u8 st_info; 00313 __u8 st_other; 00314 elf_half st_shndx; 00315 }; 00316 struct elf64_symbol { 00317 elf_word st_name; 00318 __u8 st_info; 00319 __u8 st_other; 00320 elf_half st_shndx; 00321 elf64_addr st_value; 00322 elf_xword st_size; 00323 }; 00324 00325 #ifdef __32_BITS__ 00326 typedef struct elf32_header elf_header_t; 00327 typedef struct elf32_segment_header elf_segment_header_t; 00328 typedef struct elf32_section_header elf_section_header_t; 00329 typedef struct elf32_symbol elf_symbol_t; 00330 #endif 00331 #ifdef __64_BITS__ 00332 typedef struct elf64_header elf_header_t; 00333 typedef struct elf64_segment_header elf_segment_header_t; 00334 typedef struct elf64_section_header elf_section_header_t; 00335 typedef struct elf64_symbol elf_symbol_t; 00336 #endif 00337 00338 extern int elf_load(elf_header_t *header, as_t * as); 00339 extern char *elf_error(int rc); 00340 00341 #endif 00342