• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DFX_NONLINUX_DEFINE_H
17 #define DFX_NONLINUX_DEFINE_H
18 
19 #include <stdint.h>
20 
21 #ifndef PROT_READ
22 #define PROT_READ	0x1 // Page can be read.
23 #endif
24 
25 #ifndef PROT_WRITE
26 #define PROT_WRITE	0x2 // Page can be written.
27 #endif
28 
29 #ifndef PROT_EXEC
30 #define PROT_EXEC	0x4 // Page can be executed.
31 #endif
32 
33 #ifndef PROT_NONE
34 #define PROT_NONE	0x0 // Page can not be accessed.
35 #endif
36 
37 #ifndef MAP_FAILED
38 #define MAP_FAILED ((void *) -1)
39 #endif
40 
41 #if defined __x86_64__ && !defined __ILP32__
42 # define __WORDSIZE	64
43 #elif defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
44 # define __WORDSIZE	64
45 #else
46 # define __WORDSIZE	32
47 #endif
48 
49 #define __ELF_NATIVE_CLASS __WORDSIZE
50 
51 #define ElfW(type)	_ElfW (Elf, __ELF_NATIVE_CLASS, type)
52 #define _ElfW(e, w, t)	_ElfW_1 (e, w, _##t)
53 #define _ElfW_1(e, w, t)	e##w##t
54 
55 #define EM_386		 3 // Intel 80386
56 #define EM_ARM		40 // ARM
57 #define EM_X86_64	62 // AMD x86-64 architecture
58 #define EM_AARCH64	183 // ARM AARCH64
59 #define EM_RISCV	243 // RISCV
60 
61 #define PT_LOAD		1 // Loadable program segment
62 #define PT_DYNAMIC	2 // Dynamic linking information
63 
64 #define PF_X (1 << 0) // Segment is executable
65 
66 #define SHT_SYMTAB 2 // Symbol table
67 #define SHT_DYNSYM 11 // Dynamic linker symbol table
68 
69 #define DT_NULL		0 // Marks end of dynamic section
70 #define DT_PLTGOT	3 // Processor defined value
71 #define DT_STRTAB	5 // Address of string table
72 #define DT_STRSZ	10 // Size of string table
73 #define DT_SONAME	14 // Name of shared object
74 
75 #define SHN_UNDEF	0 // Undefined section
76 
77 #define STT_FUNC	2 // Symbol is a code object
78 #define STT_GNU_IFUNC	10 // Symbol is indirect code object
79 
80 #define NT_GNU_BUILD_ID	3
81 
82 #define ELF32_ST_TYPE(val)		((val) & 0xf)
83 
84 /* Type for a 16-bit quantity. */
85 typedef uint16_t Elf32_Half;
86 typedef uint16_t Elf64_Half;
87 
88 /* Types for signed and unsigned 32-bit quantities. */
89 typedef uint32_t Elf32_Word;
90 typedef	int32_t Elf32_Sword;
91 typedef uint32_t Elf64_Word;
92 typedef	int32_t Elf64_Sword;
93 
94 /* Types for signed and unsigned 64-bit quantities. */
95 typedef uint64_t Elf32_Xword;
96 typedef	int64_t Elf32_Sxword;
97 typedef uint64_t Elf64_Xword;
98 typedef	int64_t Elf64_Sxword;
99 
100 /* Type of addresses. */
101 typedef uint32_t Elf32_Addr;
102 typedef uint64_t Elf64_Addr;
103 
104 /* Type of file offsets. */
105 typedef uint32_t Elf32_Off;
106 typedef uint64_t Elf64_Off;
107 
108 /* Type for section indices, which are 16-bit quantities. */
109 typedef uint16_t Elf32_Section;
110 typedef uint16_t Elf64_Section;
111 
112 /* Type for version symbol information. */
113 typedef Elf32_Half Elf32_Versym;
114 typedef Elf64_Half Elf64_Versym;
115 
116 #define EI_NIDENT (16)
117 
118 typedef struct {
119     unsigned char   e_ident[EI_NIDENT]; // Magic number and other info
120     Elf32_Half      e_type; // Object file type
121     Elf32_Half      e_machine; // Architecture
122     Elf32_Word      e_version; // Object file version
123     Elf32_Addr      e_entry; // Entry point virtual address
124     Elf32_Off       e_phoff; // Program header table file offset
125     Elf32_Off       e_shoff; // Section header table file offset
126     Elf32_Word      e_flags; // Processor-specific flags
127     Elf32_Half      e_ehsize; // ELF header size in bytes
128     Elf32_Half      e_phentsize; // Program header table entry size
129     Elf32_Half      e_phnum; // Program header table entry count
130     Elf32_Half      e_shentsize; // Section header table entry size
131     Elf32_Half      e_shnum; // Section header table entry count
132     Elf32_Half      e_shstrndx; // Section header string table index
133 } Elf32_Ehdr;
134 
135 typedef struct {
136     unsigned char   e_ident[EI_NIDENT]; // Magic number and other info
137     Elf64_Half      e_type; // Object file type
138     Elf64_Half      e_machine; // Architecture
139     Elf64_Word      e_version; // Object file version
140     Elf64_Addr      e_entry; // Entry point virtual address
141     Elf64_Off       e_phoff; // Program header table file offset
142     Elf64_Off       e_shoff; // Section header table file offset
143     Elf64_Word      e_flags; // Processor-specific flags
144     Elf64_Half      e_ehsize; // ELF header size in bytes
145     Elf64_Half      e_phentsize; // Program header table entry size
146     Elf64_Half      e_phnum; // Program header table entry count
147     Elf64_Half      e_shentsize; // Section header table entry size
148     Elf64_Half      e_shnum; // Section header table entry count
149     Elf64_Half      e_shstrndx; // Section header string table index
150 } Elf64_Ehdr;
151 
152 typedef struct {
153     Elf32_Word	p_type; // Segment type
154     Elf32_Off	p_offset; // Segment file offset
155     Elf32_Addr	p_vaddr; // Segment virtual address
156     Elf32_Addr	p_paddr; // Segment physical address
157     Elf32_Word	p_filesz; // Segment size in file
158     Elf32_Word	p_memsz; // Segment size in memory
159     Elf32_Word	p_flags; // Segment flags
160     Elf32_Word	p_align; // Segment alignment
161 } Elf32_Phdr;
162 
163 typedef struct {
164     Elf64_Word	p_type; // Segment type
165     Elf64_Word	p_flags; // Segment flags
166     Elf64_Off	p_offset; // Segment file offset
167     Elf64_Addr	p_vaddr; // Segment virtual address
168     Elf64_Addr	p_paddr; // Segment physical address
169     Elf64_Xword	p_filesz; // Segment size in file
170     Elf64_Xword	p_memsz; // Segment size in memory
171     Elf64_Xword	p_align; // Segment alignment
172 } Elf64_Phdr;
173 
174 typedef struct {
175     Elf32_Word	sh_name; // Section name (string tbl index)
176     Elf32_Word	sh_type; // Section type
177     Elf32_Word	sh_flags; // Section flags
178     Elf32_Addr	sh_addr;  // Section virtual addr at execution
179     Elf32_Off	sh_offset; // Section file offset
180     Elf32_Word	sh_size; // Section size in bytes
181     Elf32_Word	sh_link; // Link to another section
182     Elf32_Word	sh_info;  // Additional section information
183     Elf32_Word	sh_addralign; // Section alignment
184     Elf32_Word	sh_entsize; // Entry size if section holds table
185 } Elf32_Shdr;
186 
187 typedef struct {
188     Elf64_Word	sh_name; // Section name (string tbl index)
189     Elf64_Word	sh_type; // Section type
190     Elf64_Xword	sh_flags; // Section flags
191     Elf64_Addr	sh_addr; // Section virtual addr at execution
192     Elf64_Off	sh_offset; // Section file offset
193     Elf64_Xword	sh_size; // Section size in bytes
194     Elf64_Word	sh_link; // Link to another section
195     Elf64_Word	sh_info; // Additional section information
196     Elf64_Xword	sh_addralign; // Section alignment
197     Elf64_Xword	sh_entsize; // Entry size if section holds table
198 } Elf64_Shdr;
199 
200 typedef struct {
201     Elf32_Sword	   d_tag; // Dynamic entry type
202     union {
203         Elf32_Word d_val; // Integer value
204         Elf32_Addr d_ptr; // Address value
205     } d_un;
206 } Elf32_Dyn;
207 
208 typedef struct {
209     Elf64_Sxword	d_tag; // Dynamic entry type
210     union {
211         Elf64_Xword d_val; // Integer value
212         Elf64_Addr  d_ptr; // Address value
213     } d_un;
214 } Elf64_Dyn;
215 
216 typedef struct {
217     Elf32_Word      st_name; // Symbol name (string tbl index)
218     Elf32_Addr      st_value; // Symbol value
219     Elf32_Word      st_size; // Symbol size
220     unsigned char   st_info; // Symbol type and binding
221     unsigned char   st_other; // Symbol visibility
222     Elf32_Section   st_shndx; // Section index
223 } Elf32_Sym;
224 
225 typedef struct {
226     Elf64_Word      st_name; // Symbol name (string tbl index)
227     unsigned char	st_info; // Symbol type and binding
228     unsigned char   st_other; // Symbol visibility
229     Elf64_Section   st_shndx; // Section index
230     Elf64_Addr      st_value; // Symbol value
231     Elf64_Xword	    st_size; // Symbol size
232 } Elf64_Sym;
233 
234 typedef struct {
235     Elf32_Word n_namesz; // Length of the note's name.
236     Elf32_Word n_descsz; // Length of the note's descriptor.
237     Elf32_Word n_type; // Type of the note.
238 } Elf32_Nhdr;
239 
240 typedef struct {
241     Elf64_Word n_namesz; // Length of the note's name.
242     Elf64_Word n_descsz; // Length of the note's descriptor.
243     Elf64_Word n_type; // Type of the note.
244 } Elf64_Nhdr;
245 
246 #define	ELFMAG  "\177ELF"
247 #define	SELFMAG	 4
248 
249 #define EI_CLASS  4 // File class byte index
250 #define ELFCLASSNONE  0 // Invalid class
251 #define ELFCLASS32	1 // 32-bit objects
252 #define ELFCLASS64	2 // 64-bit objects
253 
254 /* Sharing types (must choose one and only one of these). */
255 #define MAP_SHARED	0x01 // Share changes.
256 #define MAP_PRIVATE	0x02 // Changes are private.
257 
258 #endif
259