| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Sergey Bondari
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup abi_generic
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef _ABI_ELF_H_
|
|---|
| 36 | #define _ABI_ELF_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <stdint.h>
|
|---|
| 39 | #include <abi/arch/elf.h>
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Current ELF version
|
|---|
| 43 | */
|
|---|
| 44 | enum {
|
|---|
| 45 | EV_CURRENT = 1,
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * ELF types
|
|---|
| 50 | */
|
|---|
| 51 | enum elf_type {
|
|---|
| 52 | ET_NONE = 0, /* No type */
|
|---|
| 53 | ET_REL = 1, /* Relocatable file */
|
|---|
| 54 | ET_EXEC = 2, /* Executable */
|
|---|
| 55 | ET_DYN = 3, /* Shared object */
|
|---|
| 56 | ET_CORE = 4, /* Core */
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | enum {
|
|---|
| 60 | ET_LOPROC = 0xff00, /* Lowest processor specific */
|
|---|
| 61 | ET_HIPROC = 0xffff, /* Highest processor specific */
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * ELF machine types
|
|---|
| 66 | */
|
|---|
| 67 | enum elf_machine {
|
|---|
| 68 | EM_NO = 0, /* No machine */
|
|---|
| 69 | EM_SPARC = 2, /* SPARC */
|
|---|
| 70 | EM_386 = 3, /* i386 */
|
|---|
| 71 | EM_MIPS = 8, /* MIPS RS3000 */
|
|---|
| 72 | EM_MIPS_RS3_LE = 10, /* MIPS RS3000 LE */
|
|---|
| 73 | EM_PPC = 20, /* PPC32 */
|
|---|
| 74 | EM_PPC64 = 21, /* PPC64 */
|
|---|
| 75 | EM_ARM = 40, /* ARM */
|
|---|
| 76 | EM_SPARCV9 = 43, /* SPARC64 */
|
|---|
| 77 | EM_IA_64 = 50, /* IA-64 */
|
|---|
| 78 | EM_X86_64 = 62, /* AMD64/EMT64 */
|
|---|
| 79 | EM_RISCV = 243, /* RISC-V */
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * ELF identification indexes
|
|---|
| 84 | */
|
|---|
| 85 | enum {
|
|---|
| 86 | EI_MAG0 = 0,
|
|---|
| 87 | EI_MAG1 = 1,
|
|---|
| 88 | EI_MAG2 = 2,
|
|---|
| 89 | EI_MAG3 = 3,
|
|---|
| 90 | EI_CLASS = 4, /* File class */
|
|---|
| 91 | EI_DATA = 5, /* Data encoding */
|
|---|
| 92 | EI_VERSION = 6, /* File version */
|
|---|
| 93 | EI_OSABI = 7,
|
|---|
| 94 | EI_ABIVERSION = 8,
|
|---|
| 95 | EI_PAD = 9, /* Start of padding bytes */
|
|---|
| 96 | EI_NIDENT = 16, /* ELF identification table size */
|
|---|
| 97 | };
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * ELF magic number
|
|---|
| 101 | */
|
|---|
| 102 | enum {
|
|---|
| 103 | ELFMAG0 = 0x7f,
|
|---|
| 104 | ELFMAG1 = 'E',
|
|---|
| 105 | ELFMAG2 = 'L',
|
|---|
| 106 | ELFMAG3 = 'F',
|
|---|
| 107 | };
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * ELF file classes
|
|---|
| 111 | */
|
|---|
| 112 | enum elf_class {
|
|---|
| 113 | ELFCLASSNONE = 0,
|
|---|
| 114 | ELFCLASS32 = 1,
|
|---|
| 115 | ELFCLASS64 = 2,
|
|---|
| 116 | };
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | * ELF data encoding types
|
|---|
| 120 | */
|
|---|
| 121 | enum elf_data_encoding {
|
|---|
| 122 | ELFDATANONE = 0,
|
|---|
| 123 | ELFDATA2LSB = 1, /* Least significant byte first (little endian) */
|
|---|
| 124 | ELFDATA2MSB = 2, /* Most signigicant byte first (big endian) */
|
|---|
| 125 | };
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * ELF section types
|
|---|
| 129 | */
|
|---|
| 130 | enum elf_section_type {
|
|---|
| 131 | SHT_NULL = 0,
|
|---|
| 132 | SHT_PROGBITS = 1,
|
|---|
| 133 | SHT_SYMTAB = 2,
|
|---|
| 134 | SHT_STRTAB = 3,
|
|---|
| 135 | SHT_RELA = 4,
|
|---|
| 136 | SHT_HASH = 5,
|
|---|
| 137 | SHT_DYNAMIC = 6,
|
|---|
| 138 | SHT_NOTE = 7,
|
|---|
| 139 | SHT_NOBITS = 8,
|
|---|
| 140 | SHT_REL = 9,
|
|---|
| 141 | SHT_SHLIB = 10,
|
|---|
| 142 | SHT_DYNSYM = 11,
|
|---|
| 143 | };
|
|---|
| 144 |
|
|---|
| 145 | enum {
|
|---|
| 146 | SHT_LOOS = 0x60000000,
|
|---|
| 147 | SHT_HIOS = 0x6fffffff,
|
|---|
| 148 | SHT_LOPROC = 0x70000000,
|
|---|
| 149 | SHT_HIPROC = 0x7fffffff,
|
|---|
| 150 | SHT_LOUSER = 0x80000000,
|
|---|
| 151 | SHT_HIUSER = 0xffffffff,
|
|---|
| 152 | };
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * ELF section flags
|
|---|
| 156 | */
|
|---|
| 157 | enum {
|
|---|
| 158 | SHF_WRITE = 0x1,
|
|---|
| 159 | SHF_ALLOC = 0x2,
|
|---|
| 160 | SHF_EXECINSTR = 0x4,
|
|---|
| 161 | SHF_TLS = 0x400,
|
|---|
| 162 | SHF_MASKPROC = 0xf0000000,
|
|---|
| 163 | };
|
|---|
| 164 |
|
|---|
| 165 | /** Functions for decomposing elf_symbol.st_info into binding and type */
|
|---|
| 166 | static inline uint8_t elf_st_bind(uint8_t info)
|
|---|
| 167 | {
|
|---|
| 168 | return info >> 4;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | static inline uint8_t elf_st_type(uint8_t info)
|
|---|
| 172 | {
|
|---|
| 173 | return info & 0x0f;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | static inline uint8_t elf_st_info(uint8_t bind, uint8_t type)
|
|---|
| 177 | {
|
|---|
| 178 | return (bind << 4) | (type & 0x0f);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * Symbol binding
|
|---|
| 183 | */
|
|---|
| 184 | enum elf_symbol_binding {
|
|---|
| 185 | STB_LOCAL = 0,
|
|---|
| 186 | STB_GLOBAL = 1,
|
|---|
| 187 | STB_WEAK = 2,
|
|---|
| 188 | };
|
|---|
| 189 |
|
|---|
| 190 | enum {
|
|---|
| 191 | STB_LOPROC = 13,
|
|---|
| 192 | STB_HIPROC = 15,
|
|---|
| 193 | };
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Symbol types
|
|---|
| 197 | */
|
|---|
| 198 | enum elf_symbol_type {
|
|---|
| 199 | STT_NOTYPE = 0,
|
|---|
| 200 | STT_OBJECT = 1,
|
|---|
| 201 | STT_FUNC = 2,
|
|---|
| 202 | STT_SECTION = 3,
|
|---|
| 203 | STT_FILE = 4,
|
|---|
| 204 | STT_TLS = 6,
|
|---|
| 205 | };
|
|---|
| 206 |
|
|---|
| 207 | enum {
|
|---|
| 208 | STT_LOPROC = 13,
|
|---|
| 209 | STT_HIPROC = 15,
|
|---|
| 210 | };
|
|---|
| 211 |
|
|---|
| 212 | /**
|
|---|
| 213 | * Program segment types
|
|---|
| 214 | */
|
|---|
| 215 | enum elf_segment_type {
|
|---|
| 216 | PT_NULL = 0,
|
|---|
| 217 | PT_LOAD = 1,
|
|---|
| 218 | PT_DYNAMIC = 2,
|
|---|
| 219 | PT_INTERP = 3,
|
|---|
| 220 | PT_NOTE = 4,
|
|---|
| 221 | PT_SHLIB = 5,
|
|---|
| 222 | PT_PHDR = 6,
|
|---|
| 223 | PT_TLS = 7,
|
|---|
| 224 |
|
|---|
| 225 | PT_GNU_EH_FRAME = 0x6474e550,
|
|---|
| 226 | PT_GNU_STACK = 0x6474e551,
|
|---|
| 227 | PT_GNU_RELRO = 0x6474e552,
|
|---|
| 228 | };
|
|---|
| 229 |
|
|---|
| 230 | enum {
|
|---|
| 231 | PT_LOOS = 0x60000000,
|
|---|
| 232 | PT_HIOS = 0x6fffffff,
|
|---|
| 233 | PT_LOPROC = 0x70000000,
|
|---|
| 234 | PT_HIPROC = 0x7fffffff,
|
|---|
| 235 | };
|
|---|
| 236 |
|
|---|
| 237 | /**
|
|---|
| 238 | * Program segment attributes.
|
|---|
| 239 | */
|
|---|
| 240 | enum elf_segment_access {
|
|---|
| 241 | PF_X = 1,
|
|---|
| 242 | PF_W = 2,
|
|---|
| 243 | PF_R = 4,
|
|---|
| 244 | };
|
|---|
| 245 |
|
|---|
| 246 | /**
|
|---|
| 247 | * ELF data types
|
|---|
| 248 | *
|
|---|
| 249 | * These types are found to be identical in both 32-bit and 64-bit
|
|---|
| 250 | * ELF object file specifications. They are the only types used
|
|---|
| 251 | * in ELF header.
|
|---|
| 252 | */
|
|---|
| 253 | typedef uint64_t elf_xword;
|
|---|
| 254 | typedef int64_t elf_sxword;
|
|---|
| 255 | typedef uint32_t elf_word;
|
|---|
| 256 | typedef int32_t elf_sword;
|
|---|
| 257 | typedef uint16_t elf_half;
|
|---|
| 258 |
|
|---|
| 259 | /**
|
|---|
| 260 | * 32-bit ELF data types.
|
|---|
| 261 | *
|
|---|
| 262 | * These types are specific for 32-bit format.
|
|---|
| 263 | */
|
|---|
| 264 | typedef uint32_t elf32_addr;
|
|---|
| 265 | typedef uint32_t elf32_off;
|
|---|
| 266 |
|
|---|
| 267 | /**
|
|---|
| 268 | * 64-bit ELF data types.
|
|---|
| 269 | *
|
|---|
| 270 | * These types are specific for 64-bit format.
|
|---|
| 271 | */
|
|---|
| 272 | typedef uint64_t elf64_addr;
|
|---|
| 273 | typedef uint64_t elf64_off;
|
|---|
| 274 |
|
|---|
| 275 | /** ELF header */
|
|---|
| 276 | struct elf32_header {
|
|---|
| 277 | uint8_t e_ident[EI_NIDENT];
|
|---|
| 278 | elf_half e_type;
|
|---|
| 279 | elf_half e_machine;
|
|---|
| 280 | elf_word e_version;
|
|---|
| 281 | elf32_addr e_entry;
|
|---|
| 282 | elf32_off e_phoff;
|
|---|
| 283 | elf32_off e_shoff;
|
|---|
| 284 | elf_word e_flags;
|
|---|
| 285 | elf_half e_ehsize;
|
|---|
| 286 | elf_half e_phentsize;
|
|---|
| 287 | elf_half e_phnum;
|
|---|
| 288 | elf_half e_shentsize;
|
|---|
| 289 | elf_half e_shnum;
|
|---|
| 290 | elf_half e_shstrndx;
|
|---|
| 291 | };
|
|---|
| 292 |
|
|---|
| 293 | struct elf64_header {
|
|---|
| 294 | uint8_t e_ident[EI_NIDENT];
|
|---|
| 295 | elf_half e_type;
|
|---|
| 296 | elf_half e_machine;
|
|---|
| 297 | elf_word e_version;
|
|---|
| 298 | elf64_addr e_entry;
|
|---|
| 299 | elf64_off e_phoff;
|
|---|
| 300 | elf64_off e_shoff;
|
|---|
| 301 | elf_word e_flags;
|
|---|
| 302 | elf_half e_ehsize;
|
|---|
| 303 | elf_half e_phentsize;
|
|---|
| 304 | elf_half e_phnum;
|
|---|
| 305 | elf_half e_shentsize;
|
|---|
| 306 | elf_half e_shnum;
|
|---|
| 307 | elf_half e_shstrndx;
|
|---|
| 308 | };
|
|---|
| 309 |
|
|---|
| 310 | /**
|
|---|
| 311 | * ELF segment header.
|
|---|
| 312 | * Segments headers are also known as program headers.
|
|---|
| 313 | */
|
|---|
| 314 | struct elf32_segment_header {
|
|---|
| 315 | elf_word p_type;
|
|---|
| 316 | elf32_off p_offset;
|
|---|
| 317 | elf32_addr p_vaddr;
|
|---|
| 318 | elf32_addr p_paddr;
|
|---|
| 319 | elf_word p_filesz;
|
|---|
| 320 | elf_word p_memsz;
|
|---|
| 321 | elf_word p_flags;
|
|---|
| 322 | elf_word p_align;
|
|---|
| 323 | };
|
|---|
| 324 |
|
|---|
| 325 | struct elf64_segment_header {
|
|---|
| 326 | elf_word p_type;
|
|---|
| 327 | elf_word p_flags;
|
|---|
| 328 | elf64_off p_offset;
|
|---|
| 329 | elf64_addr p_vaddr;
|
|---|
| 330 | elf64_addr p_paddr;
|
|---|
| 331 | elf_xword p_filesz;
|
|---|
| 332 | elf_xword p_memsz;
|
|---|
| 333 | elf_xword p_align;
|
|---|
| 334 | };
|
|---|
| 335 |
|
|---|
| 336 | /**
|
|---|
| 337 | * ELF section header
|
|---|
| 338 | */
|
|---|
| 339 | struct elf32_section_header {
|
|---|
| 340 | elf_word sh_name;
|
|---|
| 341 | elf_word sh_type;
|
|---|
| 342 | elf_word sh_flags;
|
|---|
| 343 | elf32_addr sh_addr;
|
|---|
| 344 | elf32_off sh_offset;
|
|---|
| 345 | elf_word sh_size;
|
|---|
| 346 | elf_word sh_link;
|
|---|
| 347 | elf_word sh_info;
|
|---|
| 348 | elf_word sh_addralign;
|
|---|
| 349 | elf_word sh_entsize;
|
|---|
| 350 | };
|
|---|
| 351 |
|
|---|
| 352 | struct elf64_section_header {
|
|---|
| 353 | elf_word sh_name;
|
|---|
| 354 | elf_word sh_type;
|
|---|
| 355 | elf_xword sh_flags;
|
|---|
| 356 | elf64_addr sh_addr;
|
|---|
| 357 | elf64_off sh_offset;
|
|---|
| 358 | elf_xword sh_size;
|
|---|
| 359 | elf_word sh_link;
|
|---|
| 360 | elf_word sh_info;
|
|---|
| 361 | elf_xword sh_addralign;
|
|---|
| 362 | elf_xword sh_entsize;
|
|---|
| 363 | };
|
|---|
| 364 |
|
|---|
| 365 | /**
|
|---|
| 366 | * ELF symbol table entry
|
|---|
| 367 | */
|
|---|
| 368 | struct elf32_symbol {
|
|---|
| 369 | elf_word st_name;
|
|---|
| 370 | elf32_addr st_value;
|
|---|
| 371 | elf_word st_size;
|
|---|
| 372 | uint8_t st_info;
|
|---|
| 373 | uint8_t st_other;
|
|---|
| 374 | elf_half st_shndx;
|
|---|
| 375 | };
|
|---|
| 376 |
|
|---|
| 377 | struct elf64_symbol {
|
|---|
| 378 | elf_word st_name;
|
|---|
| 379 | uint8_t st_info;
|
|---|
| 380 | uint8_t st_other;
|
|---|
| 381 | elf_half st_shndx;
|
|---|
| 382 | elf64_addr st_value;
|
|---|
| 383 | elf_xword st_size;
|
|---|
| 384 | };
|
|---|
| 385 |
|
|---|
| 386 | /*
|
|---|
| 387 | * ELF note segment entry
|
|---|
| 388 | */
|
|---|
| 389 | struct elf32_note {
|
|---|
| 390 | elf_word namesz;
|
|---|
| 391 | elf_word descsz;
|
|---|
| 392 | elf_word type;
|
|---|
| 393 | };
|
|---|
| 394 |
|
|---|
| 395 | /*
|
|---|
| 396 | * NOTE: namesz, descsz and type should be 64-bits wide (elf_xword)
|
|---|
| 397 | * per the 64-bit ELF spec. The Linux kernel however screws up and
|
|---|
| 398 | * defines them as Elf64_Word, which is 32-bits wide(!). We are trying
|
|---|
| 399 | * to make our core files compatible with Linux GDB target so we copy
|
|---|
| 400 | * the blunder here.
|
|---|
| 401 | */
|
|---|
| 402 | struct elf64_note {
|
|---|
| 403 | elf_word namesz;
|
|---|
| 404 | elf_word descsz;
|
|---|
| 405 | elf_word type;
|
|---|
| 406 | };
|
|---|
| 407 |
|
|---|
| 408 | #ifdef __32_BITS__
|
|---|
| 409 | typedef struct elf32_header elf_header_t;
|
|---|
| 410 | typedef struct elf32_segment_header elf_segment_header_t;
|
|---|
| 411 | typedef struct elf32_section_header elf_section_header_t;
|
|---|
| 412 | typedef struct elf32_symbol elf_symbol_t;
|
|---|
| 413 | typedef struct elf32_note elf_note_t;
|
|---|
| 414 | #endif
|
|---|
| 415 |
|
|---|
| 416 | #ifdef __64_BITS__
|
|---|
| 417 | typedef struct elf64_header elf_header_t;
|
|---|
| 418 | typedef struct elf64_segment_header elf_segment_header_t;
|
|---|
| 419 | typedef struct elf64_section_header elf_section_header_t;
|
|---|
| 420 | typedef struct elf64_symbol elf_symbol_t;
|
|---|
| 421 | typedef struct elf64_note elf_note_t;
|
|---|
| 422 | #endif
|
|---|
| 423 |
|
|---|
| 424 | #endif
|
|---|
| 425 |
|
|---|
| 426 | /** @}
|
|---|
| 427 | */
|
|---|