1 /* elfparse.h 2 * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 3 * Use of this source code is governed by a BSD-style license that can be 4 * found in the LICENSE file. 5 * 6 * Elf parsing. 7 */ 8 9 #ifndef _ELFPARSE_H_ 10 #define _ELFPARSE_H_ 11 12 #include <elf.h> 13 14 /* 15 * These structs come from elf.h 16 * The version in elf.h do not pack these structs so 17 * portability could be an issue. 18 * The compiler could mess with aligmment depending on arch 19 * so I'm redefining them here and packing them to 1-byte alignment. 20 */ 21 #if !defined(EI_NIDENT) 22 #define EI_NIDENT (16) 23 #endif 24 #pragma pack(push) 25 #pragma pack(1) 26 typedef struct 27 { 28 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ 29 Elf32_Half e_type; /* Object file type */ 30 Elf32_Half e_machine; /* Architecture */ 31 Elf32_Word e_version; /* Object file version */ 32 Elf32_Addr e_entry; /* Entry point virtual address */ 33 Elf32_Off e_phoff; /* Program header table file offset */ 34 Elf32_Off e_shoff; /* Section header table file offset */ 35 Elf32_Word e_flags; /* Processor-specific flags */ 36 Elf32_Half e_ehsize; /* ELF header size in bytes */ 37 Elf32_Half e_phentsize; /* Program header table entry size */ 38 Elf32_Half e_phnum; /* Program header table entry count */ 39 Elf32_Half e_shentsize; /* Section header table entry size */ 40 Elf32_Half e_shnum; /* Section header table entry count */ 41 Elf32_Half e_shstrndx; /* Section header string table index */ 42 } Minijail_Elf32_Ehdr; 43 44 typedef struct 45 { 46 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ 47 Elf64_Half e_type; /* Object file type */ 48 Elf64_Half e_machine; /* Architecture */ 49 Elf64_Word e_version; /* Object file version */ 50 Elf64_Addr e_entry; /* Entry point virtual address */ 51 Elf64_Off e_phoff; /* Program header table file offset */ 52 Elf64_Off e_shoff; /* Section header table file offset */ 53 Elf64_Word e_flags; /* Processor-specific flags */ 54 Elf64_Half e_ehsize; /* ELF header size in bytes */ 55 Elf64_Half e_phentsize; /* Program header table entry size */ 56 Elf64_Half e_phnum; /* Program header table entry count */ 57 Elf64_Half e_shentsize; /* Section header table entry size */ 58 Elf64_Half e_shnum; /* Section header table entry count */ 59 Elf64_Half e_shstrndx; /* Section header string table index */ 60 } Minijail_Elf64_Ehdr; 61 62 typedef struct 63 { 64 Elf32_Word p_type; /* Segment type */ 65 Elf32_Off p_offset; /* Segment file offset */ 66 Elf32_Addr p_vaddr; /* Segment virtual address */ 67 Elf32_Addr p_paddr; /* Segment physical address */ 68 Elf32_Word p_filesz; /* Segment size in file */ 69 Elf32_Word p_memsz; /* Segment size in memory */ 70 Elf32_Word p_flags; /* Segment flags */ 71 Elf32_Word p_align; /* Segment alignment */ 72 } Minijail_Elf32_Phdr; 73 74 typedef struct 75 { 76 Elf64_Word p_type; /* Segment type */ 77 Elf64_Word p_flags; /* Segment flags */ 78 Elf64_Off p_offset; /* Segment file offset */ 79 Elf64_Addr p_vaddr; /* Segment virtual address */ 80 Elf64_Addr p_paddr; /* Segment physical address */ 81 Elf64_Xword p_filesz; /* Segment size in file */ 82 Elf64_Xword p_memsz; /* Segment size in memory */ 83 Elf64_Xword p_align; /* Segment alignment */ 84 } Minijail_Elf64_Phdr; 85 #pragma pack(pop) 86 /* End of definitions from elf.h */ 87 88 enum ElfTypeEnum { ELFERROR=0, ELFSTATIC=1, ELFDYNAMIC=2 }; 89 typedef enum ElfTypeEnum ElfType; 90 91 /* 92 * This is the initial amount of the ELF file we try and read. 93 * It is the same value that the kernel uses (BINPRM_BUF_SIZE). 94 */ 95 #define HEADERSIZE 128 96 97 ElfType get_elf_linkage(const char *path); 98 99 #endif /* _ELFPARSE_H_ */ 100