1 /* elfparse.h 2 * Copyright 2014 The ChromiumOS Authors 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 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ 28 Elf32_Half e_type; /* Object file type */ 29 Elf32_Half e_machine; /* Architecture */ 30 Elf32_Word e_version; /* Object file version */ 31 Elf32_Addr e_entry; /* Entry point virtual address */ 32 Elf32_Off e_phoff; /* Program header table file offset */ 33 Elf32_Off e_shoff; /* Section header table file offset */ 34 Elf32_Word e_flags; /* Processor-specific flags */ 35 Elf32_Half e_ehsize; /* ELF header size in bytes */ 36 Elf32_Half e_phentsize; /* Program header table entry size */ 37 Elf32_Half e_phnum; /* Program header table entry count */ 38 Elf32_Half e_shentsize; /* Section header table entry size */ 39 Elf32_Half e_shnum; /* Section header table entry count */ 40 Elf32_Half e_shstrndx; /* Section header string table index */ 41 } Minijail_Elf32_Ehdr; 42 43 typedef struct { 44 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ 45 Elf64_Half e_type; /* Object file type */ 46 Elf64_Half e_machine; /* Architecture */ 47 Elf64_Word e_version; /* Object file version */ 48 Elf64_Addr e_entry; /* Entry point virtual address */ 49 Elf64_Off e_phoff; /* Program header table file offset */ 50 Elf64_Off e_shoff; /* Section header table file offset */ 51 Elf64_Word e_flags; /* Processor-specific flags */ 52 Elf64_Half e_ehsize; /* ELF header size in bytes */ 53 Elf64_Half e_phentsize; /* Program header table entry size */ 54 Elf64_Half e_phnum; /* Program header table entry count */ 55 Elf64_Half e_shentsize; /* Section header table entry size */ 56 Elf64_Half e_shnum; /* Section header table entry count */ 57 Elf64_Half e_shstrndx; /* Section header string table index */ 58 } Minijail_Elf64_Ehdr; 59 60 typedef struct { 61 Elf32_Word p_type; /* Segment type */ 62 Elf32_Off p_offset; /* Segment file offset */ 63 Elf32_Addr p_vaddr; /* Segment virtual address */ 64 Elf32_Addr p_paddr; /* Segment physical address */ 65 Elf32_Word p_filesz; /* Segment size in file */ 66 Elf32_Word p_memsz; /* Segment size in memory */ 67 Elf32_Word p_flags; /* Segment flags */ 68 Elf32_Word p_align; /* Segment alignment */ 69 } Minijail_Elf32_Phdr; 70 71 typedef struct { 72 Elf64_Word p_type; /* Segment type */ 73 Elf64_Word p_flags; /* Segment flags */ 74 Elf64_Off p_offset; /* Segment file offset */ 75 Elf64_Addr p_vaddr; /* Segment virtual address */ 76 Elf64_Addr p_paddr; /* Segment physical address */ 77 Elf64_Xword p_filesz; /* Segment size in file */ 78 Elf64_Xword p_memsz; /* Segment size in memory */ 79 Elf64_Xword p_align; /* Segment alignment */ 80 } Minijail_Elf64_Phdr; 81 #pragma pack(pop) 82 /* End of definitions from elf.h */ 83 84 enum ElfTypeEnum { 85 ELFERROR = 0, 86 ELFSTATIC = 1, 87 ELFDYNAMIC = 2 88 }; 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