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