aboutsummaryrefslogtreecommitdiff
path: root/elfparse.h
blob: 9083e0c7ae40a22b1a987299f191e8a249b3dfdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* elfparse.h
 * Copyright 2014 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Elf parsing.
 */

#ifndef _ELFPARSE_H_
#define _ELFPARSE_H_

#include <elf.h>

/*
 * These structs come from elf.h
 * The version in elf.h do not pack these structs so
 * portability could be an issue.
 * The compiler could mess with aligmment depending on arch
 * so I'm redefining them here and packing them to 1-byte alignment.
 */
#if !defined(EI_NIDENT)
#define EI_NIDENT (16)
#endif
#pragma pack(push)
#pragma pack(1)
typedef struct
{
	unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
	Elf32_Half    e_type;             /* Object file type */
	Elf32_Half    e_machine;          /* Architecture */
	Elf32_Word    e_version;          /* Object file version */
	Elf32_Addr    e_entry;            /* Entry point virtual address */
	Elf32_Off     e_phoff;            /* Program header table file offset */
	Elf32_Off     e_shoff;            /* Section header table file offset */
	Elf32_Word    e_flags;            /* Processor-specific flags */
	Elf32_Half    e_ehsize;           /* ELF header size in bytes */
	Elf32_Half    e_phentsize;        /* Program header table entry size */
	Elf32_Half    e_phnum;            /* Program header table entry count */
	Elf32_Half    e_shentsize;        /* Section header table entry size */
	Elf32_Half    e_shnum;            /* Section header table entry count */
	Elf32_Half    e_shstrndx;         /* Section header string table index */
} Minijail_Elf32_Ehdr;

typedef struct
{
	unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
	Elf64_Half    e_type;             /* Object file type */
	Elf64_Half    e_machine;          /* Architecture */
	Elf64_Word    e_version;          /* Object file version */
	Elf64_Addr    e_entry;            /* Entry point virtual address */
	Elf64_Off     e_phoff;            /* Program header table file offset */
	Elf64_Off     e_shoff;            /* Section header table file offset */
	Elf64_Word    e_flags;            /* Processor-specific flags */
	Elf64_Half    e_ehsize;           /* ELF header size in bytes */
	Elf64_Half    e_phentsize;        /* Program header table entry size */
	Elf64_Half    e_phnum;            /* Program header table entry count */
	Elf64_Half    e_shentsize;        /* Section header table entry size */
	Elf64_Half    e_shnum;            /* Section header table entry count */
	Elf64_Half    e_shstrndx;         /* Section header string table index */
} Minijail_Elf64_Ehdr;

typedef struct
{
	Elf32_Word      p_type;           /* Segment type */
	Elf32_Off       p_offset;         /* Segment file offset */
	Elf32_Addr      p_vaddr;          /* Segment virtual address */
	Elf32_Addr      p_paddr;          /* Segment physical address */
	Elf32_Word      p_filesz;         /* Segment size in file */
	Elf32_Word      p_memsz;          /* Segment size in memory */
	Elf32_Word      p_flags;          /* Segment flags */
	Elf32_Word      p_align;          /* Segment alignment */
} Minijail_Elf32_Phdr;

typedef struct
{
	Elf64_Word      p_type;           /* Segment type */
	Elf64_Word      p_flags;          /* Segment flags */
	Elf64_Off       p_offset;         /* Segment file offset */
	Elf64_Addr      p_vaddr;          /* Segment virtual address */
	Elf64_Addr      p_paddr;          /* Segment physical address */
	Elf64_Xword     p_filesz;         /* Segment size in file */
	Elf64_Xword     p_memsz;          /* Segment size in memory */
	Elf64_Xword     p_align;          /* Segment alignment */
} Minijail_Elf64_Phdr;
#pragma pack(pop)
/* End of definitions from elf.h */

enum ElfTypeEnum { ELFERROR=0, ELFSTATIC=1, ELFDYNAMIC=2 };
typedef enum ElfTypeEnum ElfType;

/*
 * This is the initial amount of the ELF file we try and read.
 * It is the same value that the kernel uses (BINPRM_BUF_SIZE).
 */
#define HEADERSIZE  128

ElfType get_elf_linkage(const char *path);

#endif /* _ELFPARSE_H_ */