blob: e7fe76ab964bf59707fb15b493ba46b99941d33b [file] [log] [blame]
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001/* 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#define EI_NIDENT (16)
28#pragma pack(push)
29#pragma pack(1)
30typedef struct
31{
32 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
33 Elf32_Half e_type; /* Object file type */
34 Elf32_Half e_machine; /* Architecture */
35 Elf32_Word e_version; /* Object file version */
36 Elf32_Addr e_entry; /* Entry point virtual address */
37 Elf32_Off e_phoff; /* Program header table file offset */
38 Elf32_Off e_shoff; /* Section header table file offset */
39 Elf32_Word e_flags; /* Processor-specific flags */
40 Elf32_Half e_ehsize; /* ELF header size in bytes */
41 Elf32_Half e_phentsize; /* Program header table entry size */
42 Elf32_Half e_phnum; /* Program header table entry count */
43 Elf32_Half e_shentsize; /* Section header table entry size */
44 Elf32_Half e_shnum; /* Section header table entry count */
45 Elf32_Half e_shstrndx; /* Section header string table index */
46} Minijail_Elf32_Ehdr;
47
48typedef struct
49{
50 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
51 Elf64_Half e_type; /* Object file type */
52 Elf64_Half e_machine; /* Architecture */
53 Elf64_Word e_version; /* Object file version */
54 Elf64_Addr e_entry; /* Entry point virtual address */
55 Elf64_Off e_phoff; /* Program header table file offset */
56 Elf64_Off e_shoff; /* Section header table file offset */
57 Elf64_Word e_flags; /* Processor-specific flags */
58 Elf64_Half e_ehsize; /* ELF header size in bytes */
59 Elf64_Half e_phentsize; /* Program header table entry size */
60 Elf64_Half e_phnum; /* Program header table entry count */
61 Elf64_Half e_shentsize; /* Section header table entry size */
62 Elf64_Half e_shnum; /* Section header table entry count */
63 Elf64_Half e_shstrndx; /* Section header string table index */
64} Minijail_Elf64_Ehdr;
65
66typedef struct
67{
68 Elf32_Word p_type; /* Segment type */
69 Elf32_Off p_offset; /* Segment file offset */
70 Elf32_Addr p_vaddr; /* Segment virtual address */
71 Elf32_Addr p_paddr; /* Segment physical address */
72 Elf32_Word p_filesz; /* Segment size in file */
73 Elf32_Word p_memsz; /* Segment size in memory */
74 Elf32_Word p_flags; /* Segment flags */
75 Elf32_Word p_align; /* Segment alignment */
76} Minijail_Elf32_Phdr;
77
78typedef struct
79{
80 Elf64_Word p_type; /* Segment type */
81 Elf64_Word p_flags; /* Segment flags */
82 Elf64_Off p_offset; /* Segment file offset */
83 Elf64_Addr p_vaddr; /* Segment virtual address */
84 Elf64_Addr p_paddr; /* Segment physical address */
85 Elf64_Xword p_filesz; /* Segment size in file */
86 Elf64_Xword p_memsz; /* Segment size in memory */
87 Elf64_Xword p_align; /* Segment alignment */
88} Minijail_Elf64_Phdr;
89#pragma pack(pop)
90/* End of definitions from elf.h */
91
92
93#define ELFERROR 0
94#define ELFSTATIC 1
95#define ELFDYNAMIC 2
96
97/*
98 * This is the inital amout of the elf we try and read.
99 * It is the same value that the kernel uses (BINPRM_BUF_SIZE).
100 */
101#define HEADERSIZE 128
102
103typedef int ElfType;
104
105ElfType get_elf_linkage(const char *path);
106
107
108
109#endif /* _ELFPARSE_H_ */