Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 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 | */ |
Jorge Lucangeli Obes | d99a40d | 2016-01-26 13:50:44 -0800 | [diff] [blame] | 27 | #if !defined(EI_NIDENT) |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 28 | #define EI_NIDENT (16) |
Jorge Lucangeli Obes | d99a40d | 2016-01-26 13:50:44 -0800 | [diff] [blame] | 29 | #endif |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 30 | #pragma pack(push) |
| 31 | #pragma pack(1) |
| 32 | typedef 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 | |
| 50 | typedef 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 | |
| 68 | typedef 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 | |
| 80 | typedef 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 | |
Jorge Lucangeli Obes | 2a44bef | 2014-08-22 17:10:35 -0700 | [diff] [blame] | 94 | enum ElfTypeEnum { ELFERROR=0, ELFSTATIC=1, ELFDYNAMIC=2 }; |
| 95 | typedef enum ElfTypeEnum ElfType; |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 96 | |
| 97 | /* |
Jorge Lucangeli Obes | 2a44bef | 2014-08-22 17:10:35 -0700 | [diff] [blame] | 98 | * This is the initial amount of the ELF file we try and read. |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 99 | * It is the same value that the kernel uses (BINPRM_BUF_SIZE). |
| 100 | */ |
| 101 | #define HEADERSIZE 128 |
| 102 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 103 | ElfType get_elf_linkage(const char *path); |
| 104 | |
Jorge Lucangeli Obes | 2a44bef | 2014-08-22 17:10:35 -0700 | [diff] [blame] | 105 | #endif /* _ELFPARSE_H_ */ |