1 /*========================== begin_copyright_notice ============================
2 
3 Copyright (C) 2017-2021 Intel Corporation
4 
5 SPDX-License-Identifier: MIT
6 
7 ============================= end_copyright_notice ===========================*/
8 
9 /*****************************************************************************\
10 Abstract:  Defines the types used for ELF headers/sections.  This uses
11            System V Application Binary Interface - DRAFT - 19 October 2010 as
12            a reference as well as LLVM's ELF.h to ensure compatibility.
13 \******************************************************************************/
14 #pragma once
15 
16 #if !defined(__APPLE__) && !defined(_WIN32) && defined(OGL)
17     #include "os_inc.h"
18 #endif
19 
20 #if !defined(_MSC_VER) // || (_MSC_VER >= 1700) // NOTE: This header will most likely be supported in MSVC 2012
21     #include <inttypes.h>
22 #endif
23 
24 #include <stddef.h>
25 
26 namespace CLElfLib
27 {
28 /******************************************************************************\
29  ELF Enumerates
30 \******************************************************************************/
31 // E_RETVAL - return values used by ELF access/read/write helper functions
32 enum E_RETVAL
33 {
34     SUCCESS               = 0,
35     FAILURE               = 1,
36     OUT_OF_MEMORY         = 2,
37 };
38 
39 // E_ID_IDX - Defines a file as being ELF
40 enum E_ID_IDX
41 {
42     ID_IDX_MAGIC0         = 0,
43     ID_IDX_MAGIC1         = 1,
44     ID_IDX_MAGIC2         = 2,
45     ID_IDX_MAGIC3         = 3,
46     ID_IDX_CLASS          = 4,
47     ID_IDX_VERSION        = 5,
48     ID_IDX_OSABI          = 6,
49     ID_IDX_ABI_VERSION    = 7,
50     ID_IDX_PADDING        = 8,
51     ID_IDX_NUM_BYTES      = 16,
52 };
53 
54 // E_EHT_CLASS - Describes what data types the ELF structures will use.
55 enum E_EH_CLASS
56 {
57     EH_CLASS_NONE         = 0,
58     EH_CLASS_32           = 1,    // Use Elf32 data types
59     EH_CLASS_64           = 2,    // Use Elf64 data types
60 };
61 
62 // E_EHT_TYPE - List of pre-defined types header types.
63 //    OS-specific codes start at 0xfe00 and run to 0xfeff.
64 //    Processor-specific codes start at 0xff00 and end at 0xffff.
65 enum E_EH_TYPE
66 {
67     EH_TYPE_NONE                 = 0,
68     EH_TYPE_RELOCATABLE          = 1,
69     EH_TYPE_EXECUTABLE           = 2,
70     EH_TYPE_DYNAMIC              = 3,
71     EH_TYPE_CORE                 = 4,
72     EH_TYPE_OPENCL_SOURCE        = 0xff01, // format used to pass CL text sections to FE
73     EH_TYPE_OPENCL_OBJECTS       = 0xff02, // format used to pass LLVM objects / store LLVM binary output
74     EH_TYPE_OPENCL_LIBRARY       = 0xff03, // format used to store LLVM archive output
75     EH_TYPE_OPENCL_EXECUTABLE    = 0xff04, // format used to store executable output
76     EH_TYPE_OPENCL_DEBUG         = 0xff05, // format used to store debug output
77 };
78 
79 // E_EH_MACHINE - List of pre-defined machine types.
80 //    For OpenCL, currently, we do not need this information, so this is not
81 //    fully defined.
82 enum E_EH_MACHINE
83 {
84     EH_MACHINE_NONE       = 0,
85     //EHT_MACHINE_LO_RSVD    = 1,   // Beginning of range of reserved types.
86     //EHT_MACHINE_HI_RSVD    = 200, // End of range of reserved types.
87 };
88 
89 // E_EHT_VERSION - ELF header version options.
90 enum E_EHT_VERSION
91 {
92     EH_VERSION_INVALID    = 0,
93     EH_VERSION_CURRENT    = 1,
94 };
95 
96 // E_SH_TYPE - List of pre-defined section header types.
97 //    Processor-specific codes start at 0xff00 and end at 0xffff.
98 enum E_SH_TYPE
99 {
100     SH_TYPE_NULL                    = 0,
101     SH_TYPE_PROG_BITS               = 1,
102     SH_TYPE_SYM_TBL                 = 2,
103     SH_TYPE_STR_TBL                 = 3,
104     SH_TYPE_RELO_ADDS               = 4,
105     SH_TYPE_HASH                    = 5,
106     SH_TYPE_DYN                     = 6,
107     SH_TYPE_NOTE                    = 7,
108     SH_TYPE_NOBITS                  = 8,
109     SH_TYPE_RELO_NO_ADDS            = 9,
110     SH_TYPE_SHLIB                   = 10,
111     SH_TYPE_DYN_SYM_TBL             = 11,
112     SH_TYPE_INIT                    = 14,
113     SH_TYPE_FINI                    = 15,
114     SH_TYPE_PRE_INIT                = 16,
115     SH_TYPE_GROUP                   = 17,
116     SH_TYPE_SYMTBL_SHNDX            = 18,
117     SH_TYPE_OPENCL_SOURCE           = 0xff000000, // CL source to link into LLVM binary
118     SH_TYPE_OPENCL_HEADER           = 0xff000001, // CL header to link into LLVM binary
119     SH_TYPE_OPENCL_LLVM_TEXT        = 0xff000002, // LLVM text
120     SH_TYPE_OPENCL_LLVM_BINARY      = 0xff000003, // LLVM byte code
121     SH_TYPE_OPENCL_LLVM_ARCHIVE     = 0xff000004, // LLVM archives(s)
122     SH_TYPE_OPENCL_DEV_BINARY       = 0xff000005, // Device binary (coherent by default)
123     SH_TYPE_OPENCL_OPTIONS          = 0xff000006, // CL Options
124     SH_TYPE_OPENCL_PCH              = 0xff000007, // PCH (pre-compiled headers)
125     SH_TYPE_OPENCL_DEV_DEBUG        = 0xff000008, // Device debug
126     SH_TYPE_SPIRV                   = 0xff000009, // SPIRV
127     SH_TYPE_NON_COHERENT_DEV_BINARY = 0xff00000a, // Non-coherent Device binary
128     SH_TYPE_SPIRV_SC_IDS            = 0xff00000b, // Specialization Constants IDs
129     SH_TYPE_SPIRV_SC_VALUES         = 0xff00000c  // Specialization Constants values
130 };
131 
132 // E_SH_FLAG - List of section header flags.
133 enum E_SH_FLAG
134 {
135     SH_FLAG_WRITE         = 0x1,
136     SH_FLAG_ALLOC         = 0x2,
137     SH_FLAG_EXEC_INSTR    = 0x4,
138     SH_FLAG_MERGE         = 0x8,
139     SH_FLAG_STRINGS       = 0x10,
140     SH_FLAG_INFO_LINK     = 0x20,
141     SH_FLAG_LINK_ORDER    = 0x40,
142     SH_FLAG_OS_NONCONFORM = 0x100,
143     SH_FLAG_GROUP         = 0x200,
144     SH_FLAG_TLS           = 0x400,
145     SH_FLAG_MASK_OS       = 0x0ff00000,
146     SH_FLAG_MASK_PROC     = 0xf0000000,
147 };
148 
149 /******************************************************************************\
150  ELF-64 Data Types
151 \******************************************************************************/
152 #if defined(_MSC_VER) // && (_MSC_VER < 1700)
153     typedef unsigned __int64   Elf64_Addr;
154     typedef unsigned __int64   Elf64_Off;
155     typedef unsigned __int16   Elf64_Short; // Renaming Elf64_Half to Elf64_Short to avoid a conflict with Android
156     typedef unsigned __int32   Elf64_Word;
157     typedef          __int32   Elf64_Sword;
158     typedef unsigned __int64   Elf64_Xword;
159 #else
160 #if !defined(_UAPI_LINUX_ELF_H)
161     typedef uint64_t   Elf64_Addr;
162     typedef uint64_t   Elf64_Off;
163     typedef uint32_t   Elf64_Word;
164     typedef int32_t    Elf64_Sword;
165     typedef uint64_t   Elf64_Xword;
166 #endif
167     typedef uint16_t   Elf64_Short; // Renaming Elf64_Half to Elf64_Short to avoid a conflict with Android
168 #endif
169 
170 /******************************************************************************\
171  ELF Constants
172 \******************************************************************************/
173 static const unsigned char ELF_MAG0 = 0x7f;      // ELFHeader.Identity[ELF_ID_MAGIC0]
174 static const unsigned char ELF_MAG1 = 'E';       // ELFHeader.Identity[ELF_ID_MAGIC1]
175 static const unsigned char ELF_MAG2 = 'L';       // ELFHeader.Identity[ELF_ID_MAGIC2]
176 static const unsigned char ELF_MAG3 = 'F';       // ELFHeader.Identity[ELF_ID_MAGIC3]
177 static const unsigned  int ELF_ALIGN_BYTES = 16; // Alignment set to 16-bytes
178 
179 /******************************************************************************\
180  ELF-64 Header
181 \******************************************************************************/
182 struct SElf64Header
183 {
184     unsigned char    Identity[ID_IDX_NUM_BYTES];
185     Elf64_Short    Type;
186     Elf64_Short    Machine;
187     Elf64_Word       Version;
188     Elf64_Addr       EntryAddress;
189     Elf64_Off        ProgramHeadersOffset;
190     Elf64_Off        SectionHeadersOffset;
191     Elf64_Word       Flags;
192     Elf64_Short    ElfHeaderSize;
193     Elf64_Short    ProgramHeaderEntrySize;
194     Elf64_Short    NumProgramHeaderEntries;
195     Elf64_Short    SectionHeaderEntrySize;
196     Elf64_Short    NumSectionHeaderEntries;
197     Elf64_Short    SectionNameTableIndex;
198 };
199 
200 /******************************************************************************\
201  ELF-64 Section Header
202 \******************************************************************************/
203 struct SElf64SectionHeader
204 {
205     Elf64_Word    Name;
206     Elf64_Word    Type;
207     Elf64_Xword   Flags;
208     Elf64_Addr    Address;
209     Elf64_Off     DataOffset;
210     Elf64_Xword   DataSize;
211     Elf64_Word    Link;
212     Elf64_Word    Info;
213     Elf64_Xword   Alignment;
214     Elf64_Xword   EntrySize;
215 };
216 
217 } // namespace ELFlib
218