• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHRE_PLATFORM_SHARED_LOADER_UTIL_H_
18 #define CHRE_PLATFORM_SHARED_LOADER_UTIL_H_
19 
20 // Macros used to define a symbol that can be exported by the nanoapp loader
21 #define ADD_EXPORTED_SYMBOL(function_name, function_string) \
22   { reinterpret_cast<void *>(function_name), function_string }
23 #define ADD_EXPORTED_C_SYMBOL(function_name) \
24   ADD_EXPORTED_SYMBOL(function_name, STRINGIFY(function_name))
25 
26 // The below macros allow switching the ELF symbol type between 32/64-bit
27 // depending on what the chipset supports.
28 #ifndef ELFW
29 #ifndef __WORDSIZE
30 // Until we can get a hold of wordsize.h, we need to define it here.
31 // Only 32-bit architectures currently supported.
32 #ifdef CHRE_32_BIT_WORD_SIZE
33 #define __WORDSIZE 32
34 #else
35 #error "Architecture not supported by CHRE dynamic loading"
36 #endif
37 #endif
38 // https://refspecs.linuxbase.org/elf/gabi4+/ch4.reloc.html
39 #define ELF32_R_SYM(info) ((info) >> 8)
40 #define ELF32_R_TYPE(info) ((unsigned char)(info))
41 #define ELF32_R_INFO(sym, type) (((sym) << 8) + (unsigned char)(type))
42 #define __ELF_NATIVE_CLASS __WORDSIZE
43 #define ELFW(type) _ELFW(__ELF_NATIVE_CLASS, type)
44 #define _ELFW(bits, type) __ELFW(bits, type)
45 #define __ELFW(bits, type) ELF##bits##_##type
46 #endif
47 #define ELFW_R_TYPE(x) ELFW(R_TYPE)(x)
48 #define ELFW_R_SYM(x) ELFW(R_SYM)(x)
49 
50 struct ExportedData {
51   void *data;
52   const char *dataName;
53 };
54 
55 // The below is copied from bionic/libc/kernel/uapi/linux/elf.h
56 // to avoid pulling those deps into the build.
57 #if defined(__LP64__)
58 #define ElfW(type) Elf64_##type
59 #else
60 #define ElfW(type) Elf32_##type
61 #endif
62 
63 #define EM_ARM 40
64 #define EM_RISCV 243
65 #define EI_MAG0 0
66 #define EI_MAG1 1
67 #define EI_MAG2 2
68 #define EI_MAG3 3
69 #define EI_CLASS 4
70 #define EI_DATA 5
71 #define EI_VERSION 6
72 #define EI_OSABI 7
73 #define EI_PAD 8
74 #define ELFMAG0 0x7f
75 #define ELFMAG1 'E'
76 #define ELFMAG2 'L'
77 #define ELFMAG3 'F'
78 #define ELFMAG "\177ELF"
79 #define SELFMAG 4
80 #define ELFCLASSNONE 0
81 #define ELFCLASS32 1
82 #define ELFCLASS64 2
83 #define ELFCLASSNUM 3
84 #define ELFDATANONE 0
85 #define ELFDATA2LSB 1
86 #define ELFDATA2MSB 2
87 #define EV_NONE 0
88 #define EV_CURRENT 1
89 #define EV_NUM 2
90 #define ELFOSABI_NONE 0
91 #define ELFOSABI_LINUX 3
92 #define PT_NULL 0
93 #define PT_LOAD 1
94 #define PT_DYNAMIC 2
95 #define PT_INTERP 3
96 #define PT_NOTE 4
97 #define PT_SHLIB 5
98 #define PT_PHDR 6
99 #define PT_TLS 7
100 #define PT_LOOS 0x60000000
101 #define PT_HIOS 0x6fffffff
102 #define PT_LOPROC 0x70000000
103 #define PT_HIPROC 0x7fffffff
104 #define PT_GNU_EH_FRAME 0x6474e550
105 #define PT_GNU_STACK (PT_LOOS + 0x474e551)
106 #define PN_XNUM 0xffff
107 #define ET_NONE 0
108 #define ET_REL 1
109 #define ET_EXEC 2
110 #define ET_DYN 3
111 #define ET_CORE 4
112 #define ET_LOPROC 0xff00
113 #define ET_HIPROC 0xffff
114 #define DT_NULL 0
115 #define DT_NEEDED 1
116 #define DT_PLTRELSZ 2
117 #define DT_PLTGOT 3
118 #define DT_HASH 4
119 #define DT_STRTAB 5
120 #define DT_SYMTAB 6
121 #define DT_RELA 7
122 #define DT_RELASZ 8
123 #define DT_RELAENT 9
124 #define DT_STRSZ 10
125 #define DT_SYMENT 11
126 #define DT_INIT 12
127 #define DT_FINI 13
128 #define DT_SONAME 14
129 #define DT_RPATH 15
130 #define DT_SYMBOLIC 16
131 #define DT_REL 17
132 #define DT_RELSZ 18
133 #define DT_RELENT 19
134 #define DT_PLTREL 20
135 #define DT_DEBUG 21
136 #define DT_TEXTREL 22
137 #define DT_JMPREL 23
138 #define DT_ENCODING 32
139 
140 typedef __signed__ char __s8;
141 typedef unsigned char __u8;
142 typedef __signed__ short __s16;
143 typedef unsigned short __u16;
144 typedef __signed__ int __s32;
145 typedef unsigned int __u32;
146 typedef __signed__ long __s64;
147 typedef unsigned long __u64;
148 
149 typedef __u32 Elf32_Addr;
150 typedef __u16 Elf32_Half;
151 typedef __u32 Elf32_Off;
152 typedef __s32 Elf32_Sword;
153 typedef __u32 Elf32_Word;
154 
155 #define EI_NIDENT 16
156 typedef struct elf32_hdr {
157   unsigned char e_ident[EI_NIDENT];
158   Elf32_Half e_type;
159   Elf32_Half e_machine;
160   Elf32_Word e_version;
161   Elf32_Addr e_entry;
162   Elf32_Off e_phoff;
163   Elf32_Off e_shoff;
164   Elf32_Word e_flags;
165   Elf32_Half e_ehsize;
166   Elf32_Half e_phentsize;
167   Elf32_Half e_phnum;
168   Elf32_Half e_shentsize;
169   Elf32_Half e_shnum;
170   Elf32_Half e_shstrndx;
171 } Elf32_Ehdr;
172 
173 typedef struct dynamic {
174   Elf32_Sword d_tag;
175   union {
176     Elf32_Sword d_val;
177     Elf32_Addr d_ptr;
178   } d_un;
179 } Elf32_Dyn;
180 
181 typedef struct elf32_phdr {
182   Elf32_Word p_type;
183   Elf32_Off p_offset;
184   Elf32_Addr p_vaddr;
185   Elf32_Addr p_paddr;
186   Elf32_Word p_filesz;
187   Elf32_Word p_memsz;
188   Elf32_Word p_flags;
189   Elf32_Word p_align;
190 } Elf32_Phdr;
191 
192 typedef struct elf32_shdr {
193   Elf32_Word sh_name;
194   Elf32_Word sh_type;
195   Elf32_Word sh_flags;
196   Elf32_Addr sh_addr;
197   Elf32_Off sh_offset;
198   Elf32_Word sh_size;
199   Elf32_Word sh_link;
200   Elf32_Word sh_info;
201   Elf32_Word sh_addralign;
202   Elf32_Word sh_entsize;
203 } Elf32_Shdr;
204 
205 typedef struct elf32_rela {
206   Elf32_Addr r_offset;
207   Elf32_Word r_info;
208   Elf32_Sword r_addend;
209 } Elf32_Rela;
210 
211 typedef struct elf32_rel {
212   Elf32_Addr r_offset;
213   Elf32_Word r_info;
214 } Elf32_Rel;
215 
216 typedef struct elf32_sym {
217   Elf32_Word st_name;
218   Elf32_Addr st_value;
219   Elf32_Word st_size;
220   unsigned char st_info;
221   unsigned char st_other;
222   Elf32_Half st_shndx;
223 } Elf32_Sym;
224 
225 // The following defines are copied from bionic's elf_arm.h header
226 // at bionic/libc/kernel/uapi/linux/elf.h
227 // Only the relocation types currently supported are copied.
228 #define R_ARM_NONE 0
229 #define R_ARM_ABS32 2
230 #define R_ARM_COPY 20
231 #define R_ARM_GLOB_DAT 21
232 #define R_ARM_JUMP_SLOT 22
233 #define R_ARM_RELATIVE 23
234 #define R_RISCV_NONE 0
235 #define R_RISCV_32 1
236 #define R_RISCV_RELATIVE 3
237 #define R_RISCV_JUMP_SLOT 5
238 // Undefined symbol.
239 #define SHN_UNDEF 0
240 
241 // The following (legal values for segment flags) are copied from
242 // bionic's elf.h
243 /* http://www.sco.com/developers/gabi/latest/ch5.pheader.html */
244 #define PF_X 0x1
245 #define PF_W 0x2
246 #define PF_R 0x4
247 #define PF_MASKOS 0x0ff00000
248 #define PF_MASKPROC 0xf0000000
249 
250 #endif  // CHRE_PLATFORM_SHARED_LOADER_UTIL_H_
251