1 // © 2021 Qualcomm Innovation Center, Inc. All rights reserved.
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 typedef uint16_t Elf_Half;
6 typedef uint32_t Elf_Word;
7 
8 #if defined(USE_ELF64)
9 
10 typedef int32_t	 Elf_Sword;
11 typedef int64_t	 Elf_Sxword;
12 typedef uint64_t Elf_Xword;
13 typedef uint64_t Elf_Addr;
14 typedef uint64_t Elf_Off;
15 
16 #define R_TYPE(r_info) ((r_info)&0x7fffffffU)
17 #define R_SYM(r_info)  ((r_info) >> 32U)
18 
19 #define ELF_CLASS ELF_CLASS_64
20 
21 #elif defined(USE_ELF32)
22 
23 #error unsupported USE_ELF32
24 
25 #else
26 #error please define USE_ELF32 or USE_ELF64
27 #endif
28 
29 #define EI_NIDENT 16
30 
31 #define EI_MAG_STR                                                             \
32 	"\x7f"                                                                 \
33 	"ELF"
34 #define EI_MAG_SIZE 4
35 
36 #define EI_CLASS      4
37 #define EI_DATA	      5
38 #define EI_VERSION    6
39 #define EI_OSABI      7
40 #define EI_ABIVERSION 8
41 #define EI_PAD	      9
42 
43 #define ELF_CLASS_NONE 0U
44 #define ELF_CLASS_32   1U
45 #define ELF_CLASS_64   2U
46 
47 #define ELF_DATA_NONE 0U
48 #define ELF_DATA_2LSB 1U
49 #define ELF_DATA_2MSB 2U
50 
51 #define EV_NONE	   0U
52 #define EV_CURRENT 1U
53 
54 #define ET_NONE 0U
55 #define ET_REL	1U
56 #define ET_EXEC 2U
57 #define ET_DYN	3U
58 #define ET_CORE 4U
59 
60 #define EM_AARCH64 183U
61 
62 #define PT_NULL	   0U
63 #define PT_LOAD	   1U
64 #define PT_DYNAMIC 2U
65 #define PT_INTERP  3U
66 #define PT_NOTE	   4U
67 #define PT_SHLIB   5U
68 #define PT_PHDR	   6U
69 #define PT_TLS	   7U
70 #define PT_NUM	   8U
71 
72 #define PF_X 1U
73 #define PF_W 2U
74 #define PF_R 4U
75 
76 #define DT_NULL	  0
77 #define DT_REL	  17
78 #define DT_RELSZ  18
79 #define DT_RELA	  7
80 #define DT_RELASZ 8
81 #define DT_CNT	  19
82 
83 // Architecture relocation types
84 #define R_AARCH64_NONE	   0U
85 #define R_AARCH64_NULL	   256U
86 #define R_AARCH64_RELATIVE 1027U
87 
88 typedef struct {
89 	Elf_Sxword d_tag;
90 
91 	union {
92 		Elf_Xword d_val;
93 		Elf_Addr  d_ptr;
94 	} d_un;
95 } Elf_Dyn;
96 
97 typedef struct {
98 	Elf_Addr  r_offset;
99 	Elf_Xword r_info;
100 } Elf_Rel;
101 
102 typedef struct {
103 	Elf_Addr   r_offset;
104 	Elf_Xword  r_info;
105 	Elf_Sxword r_addend;
106 } Elf_Rela;
107 
108 typedef struct {
109 	unsigned char e_ident[EI_NIDENT];
110 
111 	Elf_Half e_type;
112 	Elf_Half e_machine;
113 	Elf_Word e_version;
114 	Elf_Addr e_entry;
115 	Elf_Off	 e_phoff;
116 	Elf_Off	 e_shoff;
117 	Elf_Word e_flags;
118 
119 	Elf_Half e_ehsize;
120 	Elf_Half e_phentsize;
121 	Elf_Half e_phnum;
122 	Elf_Half e_shentsize;
123 	Elf_Half e_shnum;
124 	Elf_Half e_shstrndx;
125 } Elf_Ehdr;
126 
127 typedef struct {
128 	Elf_Word  p_type;
129 	Elf_Word  p_flags;
130 	Elf_Off	  p_offset;
131 	Elf_Addr  p_vaddr;
132 	Elf_Addr  p_paddr;
133 	Elf_Xword p_filesz;
134 	Elf_Xword p_memsz;
135 	Elf_Xword p_align;
136 } Elf_Phdr;
137