blob: 4fdd0a1f62f272dabbd98f30bf26f73481989ef7 [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
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070012#include <elf.h>
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070013
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 */
Jorge Lucangeli Obesd99a40d2016-01-26 13:50:44 -080021#if !defined(EI_NIDENT)
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070022#define EI_NIDENT (16)
Jorge Lucangeli Obesd99a40d2016-01-26 13:50:44 -080023#endif
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070024#pragma pack(push)
25#pragma pack(1)
26typedef struct
27{
28 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
29 Elf32_Half e_type; /* Object file type */
30 Elf32_Half e_machine; /* Architecture */
31 Elf32_Word e_version; /* Object file version */
32 Elf32_Addr e_entry; /* Entry point virtual address */
33 Elf32_Off e_phoff; /* Program header table file offset */
34 Elf32_Off e_shoff; /* Section header table file offset */
35 Elf32_Word e_flags; /* Processor-specific flags */
36 Elf32_Half e_ehsize; /* ELF header size in bytes */
37 Elf32_Half e_phentsize; /* Program header table entry size */
38 Elf32_Half e_phnum; /* Program header table entry count */
39 Elf32_Half e_shentsize; /* Section header table entry size */
40 Elf32_Half e_shnum; /* Section header table entry count */
41 Elf32_Half e_shstrndx; /* Section header string table index */
42} Minijail_Elf32_Ehdr;
43
44typedef struct
45{
46 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
47 Elf64_Half e_type; /* Object file type */
48 Elf64_Half e_machine; /* Architecture */
49 Elf64_Word e_version; /* Object file version */
50 Elf64_Addr e_entry; /* Entry point virtual address */
51 Elf64_Off e_phoff; /* Program header table file offset */
52 Elf64_Off e_shoff; /* Section header table file offset */
53 Elf64_Word e_flags; /* Processor-specific flags */
54 Elf64_Half e_ehsize; /* ELF header size in bytes */
55 Elf64_Half e_phentsize; /* Program header table entry size */
56 Elf64_Half e_phnum; /* Program header table entry count */
57 Elf64_Half e_shentsize; /* Section header table entry size */
58 Elf64_Half e_shnum; /* Section header table entry count */
59 Elf64_Half e_shstrndx; /* Section header string table index */
60} Minijail_Elf64_Ehdr;
61
62typedef struct
63{
64 Elf32_Word p_type; /* Segment type */
65 Elf32_Off p_offset; /* Segment file offset */
66 Elf32_Addr p_vaddr; /* Segment virtual address */
67 Elf32_Addr p_paddr; /* Segment physical address */
68 Elf32_Word p_filesz; /* Segment size in file */
69 Elf32_Word p_memsz; /* Segment size in memory */
70 Elf32_Word p_flags; /* Segment flags */
71 Elf32_Word p_align; /* Segment alignment */
72} Minijail_Elf32_Phdr;
73
74typedef struct
75{
76 Elf64_Word p_type; /* Segment type */
77 Elf64_Word p_flags; /* Segment flags */
78 Elf64_Off p_offset; /* Segment file offset */
79 Elf64_Addr p_vaddr; /* Segment virtual address */
80 Elf64_Addr p_paddr; /* Segment physical address */
81 Elf64_Xword p_filesz; /* Segment size in file */
82 Elf64_Xword p_memsz; /* Segment size in memory */
83 Elf64_Xword p_align; /* Segment alignment */
84} Minijail_Elf64_Phdr;
85#pragma pack(pop)
86/* End of definitions from elf.h */
87
Jorge Lucangeli Obes2a44bef2014-08-22 17:10:35 -070088enum ElfTypeEnum { ELFERROR=0, ELFSTATIC=1, ELFDYNAMIC=2 };
89typedef enum ElfTypeEnum ElfType;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070090
91/*
Jorge Lucangeli Obes2a44bef2014-08-22 17:10:35 -070092 * This is the initial amount of the ELF file we try and read.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070093 * It is the same value that the kernel uses (BINPRM_BUF_SIZE).
94 */
95#define HEADERSIZE 128
96
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070097ElfType get_elf_linkage(const char *path);
98
Jorge Lucangeli Obes2a44bef2014-08-22 17:10:35 -070099#endif /* _ELFPARSE_H_ */