1 /*
2  * Copyright (C) 2021 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  *  SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright (c) 2017, 2018 Dell EMC
9  * Copyright (c) 2000, 2001, 2008, 2011, David E. O'Brien
10  * Copyright (c) 1998 John D. Polstra.
11  * All rights reserved.
12  */
13 
14 /*
15  * source:
16  * https://github.com/freebsd/freebsd-src/blob/main/sys/sys/elf_common.h
17  */
18 
19 #ifndef	ELF_H
20 #define	ELF_H
21 
22 /* Indexes into the e_ident array.  Keep synced with
23    http://www.sco.com/developers/gabi/latest/ch4.eheader.html */
24 #define	EI_MAG0			0	/* Magic number, byte 0. */
25 #define	EI_MAG1			1	/* Magic number, byte 1. */
26 #define	EI_MAG2			2	/* Magic number, byte 2. */
27 #define	EI_MAG3			3	/* Magic number, byte 3. */
28 #define	EI_CLASS		4	/* Class of machine. */
29 #define	EI_DATA			5	/* Data format. */
30 #define	EI_VERSION		6	/* ELF format version. */
31 #define	EI_OSABI		7	/* Operating system / ABI identification */
32 #define	EI_ABIVERSION	8	/* ABI version */
33 #define	OLD_EI_BRAND	8	/* Start of architecture identification. */
34 #define	EI_PAD			9	/* Start of padding (per SVR4 ABI). */
35 #define	EI_NIDENT		16	/* Size of e_ident array. */
36 
37 /* Values for the magic number bytes. */
38 #define ELFMAGIC 0x464C457f
39 
40 /* Values for e_ident[EI_CLASS]. */
41 #define	ELFCLASSNONE	0	/* Unknown class. */
42 #define	ELFCLASS32		1	/* 32-bit architecture. */
43 #define	ELFCLASS64		2	/* 64-bit architecture. */
44 
45 /* Values for e_type of program entry */
46 #define	ET_NONE		0	/* Unknown type. */
47 #define	ET_REL		1	/* Relocatable. */
48 #define	ET_EXEC		2	/* Executable. */
49 #define	ET_DYN		3	/* Shared object. */
50 #define	ET_CORE		4	/* Core file. */
51 #define	ET_LOOS		0xfe00	/* First operating system specific. */
52 #define	ET_HIOS		0xfeff	/* Last operating system-specific. */
53 #define	ET_LOPROC	0xff00	/* First processor-specific. */
54 #define	ET_HIPROC	0xffff	/* Last processor-specific. */
55 
56 /* Values for p_type of program entry */
57 #define	PT_NULL		0	/* Unused entry. */
58 #define	PT_LOAD		1	/* Loadable segment. */
59 #define	PT_DYNAMIC	2	/* Dynamic linking information segment. */
60 #define	PT_INTERP	3	/* Pathname of interpreter. */
61 #define	PT_NOTE		4	/* Auxiliary information. */
62 #define	PT_SHLIB	5	/* Reserved (not used). */
63 #define	PT_PHDR		6	/* Location of program header itself. */
64 #define	PT_TLS		7	/* Thread local storage segment */
65 
66 /* Header struct for elf64 file */
67 struct elf64_hdr
68 {
69 	uint8_t e_ident[EI_NIDENT];	/*ELF identification */
70 	uint16_t e_type;	/*Object file type */
71 	uint16_t e_machine;	/*Machine type */
72 	uint32_t e_version;	/*Object file version */
73 	uint64_t e_entry;	/*Entry point address */
74 	uint64_t e_phoff;	/*Program header offset */
75 	uint64_t e_shoff;	/*Section header offset */
76 	uint32_t e_flags;	/*Processor-specific flags */
77 	uint16_t e_ehsize;	/*ELF header size */
78 	uint16_t e_phentsize;	/*Size of program header entry */
79 	uint16_t e_phnum;	/*Number of program header entries */
80 	uint16_t e_shentsize;	/*Size of section header entry */
81 	uint16_t e_shnum;	/*Number of section header entries */
82 	uint16_t e_shstrndx;	/*Section name string table index */
83 };
84 
85 /* Program entry struct for elf64, describes segments loaded in ram*/
86 struct elf64_prog_entry
87 {
88 	uint32_t p_type;	/* Type of segment */
89 	uint32_t p_flags;	/* Segment attributes */
90 	uint64_t p_offset;	/* Offset in file */
91 	uint64_t p_vaddr;	/* Virtual address in memory */
92 	uint64_t p_paddr;	/* Physical address in memory */
93 	uint64_t p_filesz;	/* Size of segment in file */
94 	uint64_t p_memsz;	/* Size of segment in memory */
95 	uint64_t p_align;	/* Alignment of segment */
96 };
97 
98 /* Section entry struct for elf64, contains sections info of the program*/
99 struct elf64_sec_entry
100 {
101 	uint32_t sh_name;	/*Section name */
102 	uint32_t sh_type;	/*Section type */
103 	uint64_t sh_flags;	/*Section attributes */
104 	uint64_t sh_addr;	/*Virtual address in memory */
105 	uint64_t sh_offset;	/*Offset in file */
106 	uint64_t sh_size;	/*Size of section */
107 	uint32_t sh_link;	/*Link to other section */
108 	uint32_t sh_info;	/*Miscellaneous information */
109 	uint64_t sh_addralign;	/*Address alignment boundary */
110 	uint64_t sh_entsize;	/*Size of entries, if section has table */
111 };
112 
113 /* Header struct for elf32 file */
114 struct elf32_hdr
115 {
116 	uint8_t e_ident[EI_NIDENT];	/*ELF identification */
117 	uint16_t e_type;	/*Object file type */
118 	uint16_t e_machine;	/*Machine type */
119 	uint32_t e_version;	/*Object file version */
120 	uint32_t e_entry;	/*Entry point address */
121 	uint32_t e_phoff;	/*Program header offset */
122 	uint32_t e_shoff;	/*Section header offset */
123 	uint32_t e_flags;	/*Processor-specific flags */
124 	uint16_t e_ehsize;	/*ELF header size */
125 	uint16_t e_phentsize;	/*Size of program header entry */
126 	uint16_t e_phnum;	/*Number of program header entries */
127 	uint16_t e_shentsize;	/*Size of section header entry */
128 	uint16_t e_shnum;	/*Number of section header entries */
129 	uint16_t e_shstrndx;	/*Section name string table index */
130 
131 };
132 
133 /* Program entry struct for elf32, describes segments loaded in ram*/
134 struct elf32_prog_entry
135 {
136 	uint32_t p_type;	/* Type of segment */
137 	uint32_t p_offset;	/* Offset in file */
138 	uint32_t p_vaddr;	/* Virtual address in memory */
139 	uint32_t p_paddr;	/* Physical address in memory */
140 	uint32_t p_filesz;	/* Size of segment in file */
141 	uint32_t p_memsz;	/* Size of segment in memory */
142 	uint32_t p_flags;	/* Segment attributes */
143 	uint32_t p_align;	/* Alignment of segment */
144 };
145 
146 /* Section entry struct for elf32, contains sections info of the program*/
147 struct elf32_sec_entry
148 {
149 	uint32_t sh_name;
150 	uint32_t sh_type;
151 	uint32_t sh_flags;
152 	uint32_t sh_addr;
153 	uint32_t sh_offset;
154 	uint32_t sh_size;
155 	uint32_t sh_link;
156 	uint32_t sh_info;
157 	uint32_t sh_addralign;
158 	uint32_t sh_entsize;
159 };
160 
161 #endif /* !ELF_H */
162