[8b80b72] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Sergey Bondari
|
---|
[8b80b72] | 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 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[666f492] | 29 | /** @addtogroup generic
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_ELF_H_
|
---|
| 36 | #define KERN_ELF_H_
|
---|
[8b80b72] | 37 |
|
---|
| 38 | #include <arch/elf.h>
|
---|
[d99c1d2] | 39 | #include <typedefs.h>
|
---|
[de6b301] | 40 |
|
---|
| 41 | /**
|
---|
| 42 | * current ELF version
|
---|
| 43 | */
|
---|
[666f492] | 44 | #define EV_CURRENT 1
|
---|
[de6b301] | 45 |
|
---|
[d99c1d2] | 46 | /**
|
---|
| 47 | * ELF types
|
---|
[de6b301] | 48 | */
|
---|
[666f492] | 49 | #define ET_NONE 0 /* No type */
|
---|
| 50 | #define ET_REL 1 /* Relocatable file */
|
---|
| 51 | #define ET_EXEC 2 /* Executable */
|
---|
| 52 | #define ET_DYN 3 /* Shared object */
|
---|
| 53 | #define ET_CORE 4 /* Core */
|
---|
| 54 | #define ET_LOPROC 0xff00 /* Processor specific */
|
---|
| 55 | #define ET_HIPROC 0xffff /* Processor specific */
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
[de6b301] | 58 | * ELF machine types
|
---|
| 59 | */
|
---|
[666f492] | 60 | #define EM_NO 0 /* No machine */
|
---|
| 61 | #define EM_SPARC 2 /* SPARC */
|
---|
| 62 | #define EM_386 3 /* i386 */
|
---|
| 63 | #define EM_MIPS 8 /* MIPS RS3000 */
|
---|
| 64 | #define EM_MIPS_RS3_LE 10 /* MIPS RS3000 LE */
|
---|
| 65 | #define EM_PPC 20 /* PPC32 */
|
---|
| 66 | #define EM_PPC64 21 /* PPC64 */
|
---|
| 67 | #define EM_ARM 40 /* ARM */
|
---|
| 68 | #define EM_SPARCV9 43 /* SPARC64 */
|
---|
| 69 | #define EM_IA_64 50 /* IA-64 */
|
---|
| 70 | #define EM_X86_64 62 /* AMD64/EMT64 */
|
---|
[de6b301] | 71 |
|
---|
| 72 | /**
|
---|
| 73 | * ELF identification indexes
|
---|
| 74 | */
|
---|
[666f492] | 75 | #define EI_MAG0 0
|
---|
| 76 | #define EI_MAG1 1
|
---|
| 77 | #define EI_MAG2 2
|
---|
| 78 | #define EI_MAG3 3
|
---|
| 79 | #define EI_CLASS 4 /* File class */
|
---|
| 80 | #define EI_DATA 5 /* Data encoding */
|
---|
| 81 | #define EI_VERSION 6 /* File version */
|
---|
| 82 | #define EI_OSABI 7
|
---|
| 83 | #define EI_ABIVERSION 8
|
---|
| 84 | #define EI_PAD 9 /* Start of padding bytes */
|
---|
| 85 | #define EI_NIDENT 16 /* ELF identification table size */
|
---|
[de6b301] | 86 |
|
---|
| 87 | /**
|
---|
| 88 | * ELF magic number
|
---|
| 89 | */
|
---|
[666f492] | 90 | #define ELFMAG0 0x7f
|
---|
| 91 | #define ELFMAG1 'E'
|
---|
| 92 | #define ELFMAG2 'L'
|
---|
| 93 | #define ELFMAG3 'F'
|
---|
[de6b301] | 94 |
|
---|
| 95 | /**
|
---|
| 96 | * ELF file classes
|
---|
| 97 | */
|
---|
[666f492] | 98 | #define ELFCLASSNONE 0
|
---|
| 99 | #define ELFCLASS32 1
|
---|
| 100 | #define ELFCLASS64 2
|
---|
[de6b301] | 101 |
|
---|
| 102 | /**
|
---|
| 103 | * ELF data encoding types
|
---|
| 104 | */
|
---|
[666f492] | 105 | #define ELFDATANONE 0
|
---|
| 106 | #define ELFDATA2LSB 1 /* Least significant byte first (little endian) */
|
---|
| 107 | #define ELFDATA2MSB 2 /* Most signigicant byte first (big endian) */
|
---|
[de6b301] | 108 |
|
---|
| 109 | /**
|
---|
| 110 | * ELF error return codes
|
---|
| 111 | */
|
---|
[666f492] | 112 | #define EE_OK 0 /* No error */
|
---|
| 113 | #define EE_INVALID 1 /* Invalid ELF image */
|
---|
| 114 | #define EE_MEMORY 2 /* Cannot allocate address space */
|
---|
| 115 | #define EE_INCOMPATIBLE 3 /* ELF image is not compatible with current architecture */
|
---|
| 116 | #define EE_UNSUPPORTED 4 /* Non-supported ELF (e.g. dynamic ELFs) */
|
---|
| 117 | #define EE_LOADER 5 /* The image is actually a program loader */
|
---|
| 118 | #define EE_IRRECOVERABLE 6
|
---|
[de6b301] | 119 |
|
---|
| 120 | /**
|
---|
| 121 | * ELF section types
|
---|
| 122 | */
|
---|
[666f492] | 123 | #define SHT_NULL 0
|
---|
| 124 | #define SHT_PROGBITS 1
|
---|
| 125 | #define SHT_SYMTAB 2
|
---|
| 126 | #define SHT_STRTAB 3
|
---|
| 127 | #define SHT_RELA 4
|
---|
| 128 | #define SHT_HASH 5
|
---|
| 129 | #define SHT_DYNAMIC 6
|
---|
| 130 | #define SHT_NOTE 7
|
---|
| 131 | #define SHT_NOBITS 8
|
---|
| 132 | #define SHT_REL 9
|
---|
| 133 | #define SHT_SHLIB 10
|
---|
| 134 | #define SHT_DYNSYM 11
|
---|
| 135 | #define SHT_LOOS 0x60000000
|
---|
| 136 | #define SHT_HIOS 0x6fffffff
|
---|
| 137 | #define SHT_LOPROC 0x70000000
|
---|
| 138 | #define SHT_HIPROC 0x7fffffff
|
---|
| 139 | #define SHT_LOUSER 0x80000000
|
---|
| 140 | #define SHT_HIUSER 0xffffffff
|
---|
[de6b301] | 141 |
|
---|
| 142 | /**
|
---|
| 143 | * ELF section flags
|
---|
| 144 | */
|
---|
[666f492] | 145 | #define SHF_WRITE 0x1
|
---|
| 146 | #define SHF_ALLOC 0x2
|
---|
| 147 | #define SHF_EXECINSTR 0x4
|
---|
| 148 | #define SHF_TLS 0x400
|
---|
| 149 | #define SHF_MASKPROC 0xf0000000
|
---|
[de6b301] | 150 |
|
---|
| 151 | /**
|
---|
| 152 | * Symbol binding
|
---|
| 153 | */
|
---|
[666f492] | 154 | #define STB_LOCAL 0
|
---|
| 155 | #define STB_GLOBAL 1
|
---|
| 156 | #define STB_WEAK 2
|
---|
| 157 | #define STB_LOPROC 13
|
---|
| 158 | #define STB_HIPROC 15
|
---|
[de6b301] | 159 |
|
---|
| 160 | /**
|
---|
| 161 | * Symbol types
|
---|
| 162 | */
|
---|
[666f492] | 163 | #define STT_NOTYPE 0
|
---|
| 164 | #define STT_OBJECT 1
|
---|
| 165 | #define STT_FUNC 2
|
---|
| 166 | #define STT_SECTION 3
|
---|
| 167 | #define STT_FILE 4
|
---|
| 168 | #define STT_LOPROC 13
|
---|
| 169 | #define STT_HIPROC 15
|
---|
[de6b301] | 170 |
|
---|
| 171 | /**
|
---|
| 172 | * Program segment types
|
---|
| 173 | */
|
---|
[666f492] | 174 | #define PT_NULL 0
|
---|
| 175 | #define PT_LOAD 1
|
---|
| 176 | #define PT_DYNAMIC 2
|
---|
| 177 | #define PT_INTERP 3
|
---|
| 178 | #define PT_NOTE 4
|
---|
| 179 | #define PT_SHLIB 5
|
---|
| 180 | #define PT_PHDR 6
|
---|
| 181 | #define PT_LOPROC 0x70000000
|
---|
| 182 | #define PT_HIPROC 0x7fffffff
|
---|
[de6b301] | 183 |
|
---|
| 184 | /**
|
---|
| 185 | * Program segment attributes.
|
---|
| 186 | */
|
---|
[666f492] | 187 | #define PF_X 1
|
---|
| 188 | #define PF_W 2
|
---|
| 189 | #define PF_R 4
|
---|
[de6b301] | 190 |
|
---|
| 191 | /**
|
---|
| 192 | * ELF data types
|
---|
| 193 | *
|
---|
| 194 | * These types are found to be identical in both 32-bit and 64-bit
|
---|
| 195 | * ELF object file specifications. They are the only types used
|
---|
| 196 | * in ELF header.
|
---|
[666f492] | 197 | *
|
---|
[de6b301] | 198 | */
|
---|
[7f1c620] | 199 | typedef uint64_t elf_xword;
|
---|
| 200 | typedef int64_t elf_sxword;
|
---|
| 201 | typedef uint32_t elf_word;
|
---|
| 202 | typedef int32_t elf_sword;
|
---|
| 203 | typedef uint16_t elf_half;
|
---|
[de6b301] | 204 |
|
---|
| 205 | /**
|
---|
| 206 | * 32-bit ELF data types.
|
---|
| 207 | *
|
---|
| 208 | * These types are specific for 32-bit format.
|
---|
[666f492] | 209 | *
|
---|
[de6b301] | 210 | */
|
---|
[7f1c620] | 211 | typedef uint32_t elf32_addr;
|
---|
| 212 | typedef uint32_t elf32_off;
|
---|
[de6b301] | 213 |
|
---|
| 214 | /**
|
---|
| 215 | * 64-bit ELF data types.
|
---|
| 216 | *
|
---|
| 217 | * These types are specific for 64-bit format.
|
---|
[666f492] | 218 | *
|
---|
[de6b301] | 219 | */
|
---|
[7f1c620] | 220 | typedef uint64_t elf64_addr;
|
---|
| 221 | typedef uint64_t elf64_off;
|
---|
[de6b301] | 222 |
|
---|
| 223 | /** ELF header */
|
---|
| 224 | struct elf32_header {
|
---|
[7f1c620] | 225 | uint8_t e_ident[EI_NIDENT];
|
---|
[de6b301] | 226 | elf_half e_type;
|
---|
| 227 | elf_half e_machine;
|
---|
| 228 | elf_word e_version;
|
---|
| 229 | elf32_addr e_entry;
|
---|
| 230 | elf32_off e_phoff;
|
---|
| 231 | elf32_off e_shoff;
|
---|
| 232 | elf_word e_flags;
|
---|
| 233 | elf_half e_ehsize;
|
---|
| 234 | elf_half e_phentsize;
|
---|
| 235 | elf_half e_phnum;
|
---|
| 236 | elf_half e_shentsize;
|
---|
| 237 | elf_half e_shnum;
|
---|
| 238 | elf_half e_shstrndx;
|
---|
| 239 | };
|
---|
[666f492] | 240 |
|
---|
[de6b301] | 241 | struct elf64_header {
|
---|
[7f1c620] | 242 | uint8_t e_ident[EI_NIDENT];
|
---|
[de6b301] | 243 | elf_half e_type;
|
---|
| 244 | elf_half e_machine;
|
---|
| 245 | elf_word e_version;
|
---|
| 246 | elf64_addr e_entry;
|
---|
| 247 | elf64_off e_phoff;
|
---|
| 248 | elf64_off e_shoff;
|
---|
| 249 | elf_word e_flags;
|
---|
| 250 | elf_half e_ehsize;
|
---|
| 251 | elf_half e_phentsize;
|
---|
| 252 | elf_half e_phnum;
|
---|
| 253 | elf_half e_shentsize;
|
---|
| 254 | elf_half e_shnum;
|
---|
| 255 | elf_half e_shstrndx;
|
---|
| 256 | };
|
---|
| 257 |
|
---|
[666f492] | 258 | /**
|
---|
[78a95d6f] | 259 | * ELF segment header.
|
---|
| 260 | * Segments headers are also known as program headers.
|
---|
| 261 | */
|
---|
| 262 | struct elf32_segment_header {
|
---|
| 263 | elf_word p_type;
|
---|
| 264 | elf32_off p_offset;
|
---|
| 265 | elf32_addr p_vaddr;
|
---|
| 266 | elf32_addr p_paddr;
|
---|
| 267 | elf_word p_filesz;
|
---|
| 268 | elf_word p_memsz;
|
---|
| 269 | elf_word p_flags;
|
---|
| 270 | elf_word p_align;
|
---|
| 271 | };
|
---|
[666f492] | 272 |
|
---|
[78a95d6f] | 273 | struct elf64_segment_header {
|
---|
| 274 | elf_word p_type;
|
---|
| 275 | elf_word p_flags;
|
---|
| 276 | elf64_off p_offset;
|
---|
| 277 | elf64_addr p_vaddr;
|
---|
| 278 | elf64_addr p_paddr;
|
---|
| 279 | elf_xword p_filesz;
|
---|
| 280 | elf_xword p_memsz;
|
---|
| 281 | elf_xword p_align;
|
---|
| 282 | };
|
---|
| 283 |
|
---|
[666f492] | 284 | /**
|
---|
[de6b301] | 285 | * ELF section header
|
---|
| 286 | */
|
---|
| 287 | struct elf32_section_header {
|
---|
| 288 | elf_word sh_name;
|
---|
| 289 | elf_word sh_type;
|
---|
| 290 | elf_word sh_flags;
|
---|
| 291 | elf32_addr sh_addr;
|
---|
| 292 | elf32_off sh_offset;
|
---|
| 293 | elf_word sh_size;
|
---|
| 294 | elf_word sh_link;
|
---|
| 295 | elf_word sh_info;
|
---|
| 296 | elf_word sh_addralign;
|
---|
| 297 | elf_word sh_entsize;
|
---|
| 298 | };
|
---|
[666f492] | 299 |
|
---|
[de6b301] | 300 | struct elf64_section_header {
|
---|
| 301 | elf_word sh_name;
|
---|
| 302 | elf_word sh_type;
|
---|
| 303 | elf_xword sh_flags;
|
---|
| 304 | elf64_addr sh_addr;
|
---|
| 305 | elf64_off sh_offset;
|
---|
| 306 | elf_xword sh_size;
|
---|
| 307 | elf_word sh_link;
|
---|
| 308 | elf_word sh_info;
|
---|
| 309 | elf_xword sh_addralign;
|
---|
| 310 | elf_xword sh_entsize;
|
---|
| 311 | };
|
---|
| 312 |
|
---|
[666f492] | 313 | /**
|
---|
[de6b301] | 314 | * ELF symbol table entry
|
---|
| 315 | */
|
---|
| 316 | struct elf32_symbol {
|
---|
| 317 | elf_word st_name;
|
---|
| 318 | elf32_addr st_value;
|
---|
| 319 | elf_word st_size;
|
---|
[7f1c620] | 320 | uint8_t st_info;
|
---|
| 321 | uint8_t st_other;
|
---|
[de6b301] | 322 | elf_half st_shndx;
|
---|
| 323 | };
|
---|
[666f492] | 324 |
|
---|
[de6b301] | 325 | struct elf64_symbol {
|
---|
| 326 | elf_word st_name;
|
---|
[7f1c620] | 327 | uint8_t st_info;
|
---|
| 328 | uint8_t st_other;
|
---|
[de6b301] | 329 | elf_half st_shndx;
|
---|
| 330 | elf64_addr st_value;
|
---|
| 331 | elf_xword st_size;
|
---|
| 332 | };
|
---|
| 333 |
|
---|
[666f492] | 334 | #ifdef __32_BITS__
|
---|
[de6b301] | 335 | typedef struct elf32_header elf_header_t;
|
---|
[78a95d6f] | 336 | typedef struct elf32_segment_header elf_segment_header_t;
|
---|
[de6b301] | 337 | typedef struct elf32_section_header elf_section_header_t;
|
---|
| 338 | typedef struct elf32_symbol elf_symbol_t;
|
---|
| 339 | #endif
|
---|
[666f492] | 340 |
|
---|
[de6b301] | 341 | #ifdef __64_BITS__
|
---|
| 342 | typedef struct elf64_header elf_header_t;
|
---|
[78a95d6f] | 343 | typedef struct elf64_segment_header elf_segment_header_t;
|
---|
[de6b301] | 344 | typedef struct elf64_section_header elf_section_header_t;
|
---|
| 345 | typedef struct elf64_symbol elf_symbol_t;
|
---|
| 346 | #endif
|
---|
| 347 |
|
---|
[a000878c] | 348 | extern const char *elf_error(unsigned int rc);
|
---|
[8b80b72] | 349 |
|
---|
[666f492] | 350 | /** Interpreter string used to recognize the program loader */
|
---|
| 351 | #define ELF_INTERP_ZSTR "kernel"
|
---|
[c98e6ee] | 352 | #define ELF_INTERP_ZLEN sizeof(ELF_INTERP_ZSTR)
|
---|
| 353 |
|
---|
[8b80b72] | 354 | #endif
|
---|
[b45c443] | 355 |
|
---|
[42d3be3] | 356 | /** @}
|
---|
[b45c443] | 357 | */
|
---|