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