• 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 #elif defined (__loongarch_lp64)
46 # define __WORDSIZE	64
47 #else
48 # define __WORDSIZE	32
49 #endif
50 
51 #define __ELF_NATIVE_CLASS __WORDSIZE
52 
53 #define ElfW(type)	_ElfW (Elf, __ELF_NATIVE_CLASS, type)
54 #define _ElfW(e, w, t)	_ElfW_1 (e, w, _##t)
55 #define _ElfW_1(e, w, t)	e##w##t
56 
57 #define EM_386		 3 // Intel 80386
58 #define EM_ARM		40 // ARM
59 #define EM_X86_64	62 // AMD x86-64 architecture
60 #define EM_AARCH64	183 // ARM AARCH64
61 #define EM_RISCV	243 // RISCV
62 #define EM_LOONGARCH	258 // LOONGARCH
63 
64 
65 #define PT_LOAD		1 // Loadable program segment
66 #define PT_DYNAMIC	2 // Dynamic linking information
67 
68 #define PF_X (1 << 0) // Segment is executable
69 
70 #define SHT_SYMTAB 2 // Symbol table
71 #define SHT_DYNSYM 11 // Dynamic linker symbol table
72 
73 #define DT_NULL		0 // Marks end of dynamic section
74 #define DT_PLTGOT	3 // Processor defined value
75 #define DT_STRTAB	5 // Address of string table
76 #define DT_STRSZ	10 // Size of string table
77 #define DT_SONAME	14 // Name of shared object
78 
79 #define SHN_UNDEF	0 // Undefined section
80 
81 #define STT_FUNC	2 // Symbol is a code object
82 #define STT_GNU_IFUNC	10 // Symbol is indirect code object
83 
84 #define NT_GNU_BUILD_ID	3
85 
86 #define ELF32_ST_TYPE(val)		((val) & 0xf)
87 
88 /* Type for a 16-bit quantity. */
89 typedef uint16_t Elf32_Half;
90 typedef uint16_t Elf64_Half;
91 
92 /* Types for signed and unsigned 32-bit quantities. */
93 typedef uint32_t Elf32_Word;
94 typedef	int32_t Elf32_Sword;
95 typedef uint32_t Elf64_Word;
96 typedef	int32_t Elf64_Sword;
97 
98 /* Types for signed and unsigned 64-bit quantities. */
99 typedef uint64_t Elf32_Xword;
100 typedef	int64_t Elf32_Sxword;
101 typedef uint64_t Elf64_Xword;
102 typedef	int64_t Elf64_Sxword;
103 
104 /* Type of addresses. */
105 typedef uint32_t Elf32_Addr;
106 typedef uint64_t Elf64_Addr;
107 
108 /* Type of file offsets. */
109 typedef uint32_t Elf32_Off;
110 typedef uint64_t Elf64_Off;
111 
112 /* Type for section indices, which are 16-bit quantities. */
113 typedef uint16_t Elf32_Section;
114 typedef uint16_t Elf64_Section;
115 
116 /* Type for version symbol information. */
117 typedef Elf32_Half Elf32_Versym;
118 typedef Elf64_Half Elf64_Versym;
119 
120 #define EI_NIDENT (16)
121 
122 typedef struct {
123     unsigned char   e_ident[EI_NIDENT]; // Magic number and other info
124     Elf32_Half      e_type; // Object file type
125     Elf32_Half      e_machine; // Architecture
126     Elf32_Word      e_version; // Object file version
127     Elf32_Addr      e_entry; // Entry point virtual address
128     Elf32_Off       e_phoff; // Program header table file offset
129     Elf32_Off       e_shoff; // Section header table file offset
130     Elf32_Word      e_flags; // Processor-specific flags
131     Elf32_Half      e_ehsize; // ELF header size in bytes
132     Elf32_Half      e_phentsize; // Program header table entry size
133     Elf32_Half      e_phnum; // Program header table entry count
134     Elf32_Half      e_shentsize; // Section header table entry size
135     Elf32_Half      e_shnum; // Section header table entry count
136     Elf32_Half      e_shstrndx; // Section header string table index
137 } Elf32_Ehdr;
138 
139 typedef struct {
140     unsigned char   e_ident[EI_NIDENT]; // Magic number and other info
141     Elf64_Half      e_type; // Object file type
142     Elf64_Half      e_machine; // Architecture
143     Elf64_Word      e_version; // Object file version
144     Elf64_Addr      e_entry; // Entry point virtual address
145     Elf64_Off       e_phoff; // Program header table file offset
146     Elf64_Off       e_shoff; // Section header table file offset
147     Elf64_Word      e_flags; // Processor-specific flags
148     Elf64_Half      e_ehsize; // ELF header size in bytes
149     Elf64_Half      e_phentsize; // Program header table entry size
150     Elf64_Half      e_phnum; // Program header table entry count
151     Elf64_Half      e_shentsize; // Section header table entry size
152     Elf64_Half      e_shnum; // Section header table entry count
153     Elf64_Half      e_shstrndx; // Section header string table index
154 } Elf64_Ehdr;
155 
156 typedef struct {
157     Elf32_Word	p_type; // Segment type
158     Elf32_Off	p_offset; // Segment file offset
159     Elf32_Addr	p_vaddr; // Segment virtual address
160     Elf32_Addr	p_paddr; // Segment physical address
161     Elf32_Word	p_filesz; // Segment size in file
162     Elf32_Word	p_memsz; // Segment size in memory
163     Elf32_Word	p_flags; // Segment flags
164     Elf32_Word	p_align; // Segment alignment
165 } Elf32_Phdr;
166 
167 typedef struct {
168     Elf64_Word	p_type; // Segment type
169     Elf64_Word	p_flags; // Segment flags
170     Elf64_Off	p_offset; // Segment file offset
171     Elf64_Addr	p_vaddr; // Segment virtual address
172     Elf64_Addr	p_paddr; // Segment physical address
173     Elf64_Xword	p_filesz; // Segment size in file
174     Elf64_Xword	p_memsz; // Segment size in memory
175     Elf64_Xword	p_align; // Segment alignment
176 } Elf64_Phdr;
177 
178 typedef struct {
179     Elf32_Word	sh_name; // Section name (string tbl index)
180     Elf32_Word	sh_type; // Section type
181     Elf32_Word	sh_flags; // Section flags
182     Elf32_Addr	sh_addr;  // Section virtual addr at execution
183     Elf32_Off	sh_offset; // Section file offset
184     Elf32_Word	sh_size; // Section size in bytes
185     Elf32_Word	sh_link; // Link to another section
186     Elf32_Word	sh_info;  // Additional section information
187     Elf32_Word	sh_addralign; // Section alignment
188     Elf32_Word	sh_entsize; // Entry size if section holds table
189 } Elf32_Shdr;
190 
191 typedef struct {
192     Elf64_Word	sh_name; // Section name (string tbl index)
193     Elf64_Word	sh_type; // Section type
194     Elf64_Xword	sh_flags; // Section flags
195     Elf64_Addr	sh_addr; // Section virtual addr at execution
196     Elf64_Off	sh_offset; // Section file offset
197     Elf64_Xword	sh_size; // Section size in bytes
198     Elf64_Word	sh_link; // Link to another section
199     Elf64_Word	sh_info; // Additional section information
200     Elf64_Xword	sh_addralign; // Section alignment
201     Elf64_Xword	sh_entsize; // Entry size if section holds table
202 } Elf64_Shdr;
203 
204 typedef struct {
205     Elf32_Sword	   d_tag; // Dynamic entry type
206     union {
207         Elf32_Word d_val; // Integer value
208         Elf32_Addr d_ptr; // Address value
209     } d_un;
210 } Elf32_Dyn;
211 
212 typedef struct {
213     Elf64_Sxword	d_tag; // Dynamic entry type
214     union {
215         Elf64_Xword d_val; // Integer value
216         Elf64_Addr  d_ptr; // Address value
217     } d_un;
218 } Elf64_Dyn;
219 
220 typedef struct {
221     Elf32_Word      st_name; // Symbol name (string tbl index)
222     Elf32_Addr      st_value; // Symbol value
223     Elf32_Word      st_size; // Symbol size
224     unsigned char   st_info; // Symbol type and binding
225     unsigned char   st_other; // Symbol visibility
226     Elf32_Section   st_shndx; // Section index
227 } Elf32_Sym;
228 
229 typedef struct {
230     Elf64_Word      st_name; // Symbol name (string tbl index)
231     unsigned char	st_info; // Symbol type and binding
232     unsigned char   st_other; // Symbol visibility
233     Elf64_Section   st_shndx; // Section index
234     Elf64_Addr      st_value; // Symbol value
235     Elf64_Xword	    st_size; // Symbol size
236 } Elf64_Sym;
237 
238 typedef struct {
239     Elf32_Word n_namesz; // Length of the note's name.
240     Elf32_Word n_descsz; // Length of the note's descriptor.
241     Elf32_Word n_type; // Type of the note.
242 } Elf32_Nhdr;
243 
244 typedef struct {
245     Elf64_Word n_namesz; // Length of the note's name.
246     Elf64_Word n_descsz; // Length of the note's descriptor.
247     Elf64_Word n_type; // Type of the note.
248 } Elf64_Nhdr;
249 
250 #define	ELFMAG  "\177ELF"
251 #define	SELFMAG	 4
252 
253 #define EI_CLASS  4 // File class byte index
254 #define ELFCLASSNONE  0 // Invalid class
255 #define ELFCLASS32	1 // 32-bit objects
256 #define ELFCLASS64	2 // 64-bit objects
257 
258 /* Sharing types (must choose one and only one of these). */
259 #define MAP_SHARED	0x01 // Share changes.
260 #define MAP_PRIVATE	0x02 // Changes are private.
261 
262 #endif
263