1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#ifndef DFX_NONLINUX_DEFINE_H
17800b99b8Sopenharmony_ci#define DFX_NONLINUX_DEFINE_H
18800b99b8Sopenharmony_ci
19800b99b8Sopenharmony_ci#include <stdint.h>
20800b99b8Sopenharmony_ci
21800b99b8Sopenharmony_ci#ifndef PROT_READ
22800b99b8Sopenharmony_ci#define PROT_READ	0x1 // Page can be read.
23800b99b8Sopenharmony_ci#endif
24800b99b8Sopenharmony_ci
25800b99b8Sopenharmony_ci#ifndef PROT_WRITE
26800b99b8Sopenharmony_ci#define PROT_WRITE	0x2 // Page can be written.
27800b99b8Sopenharmony_ci#endif
28800b99b8Sopenharmony_ci
29800b99b8Sopenharmony_ci#ifndef PROT_EXEC
30800b99b8Sopenharmony_ci#define PROT_EXEC	0x4 // Page can be executed.
31800b99b8Sopenharmony_ci#endif
32800b99b8Sopenharmony_ci
33800b99b8Sopenharmony_ci#ifndef PROT_NONE
34800b99b8Sopenharmony_ci#define PROT_NONE	0x0 // Page can not be accessed.
35800b99b8Sopenharmony_ci#endif
36800b99b8Sopenharmony_ci
37800b99b8Sopenharmony_ci#ifndef MAP_FAILED
38800b99b8Sopenharmony_ci#define MAP_FAILED ((void *) -1)
39800b99b8Sopenharmony_ci#endif
40800b99b8Sopenharmony_ci
41800b99b8Sopenharmony_ci#if defined __x86_64__ && !defined __ILP32__
42800b99b8Sopenharmony_ci# define __WORDSIZE	64
43800b99b8Sopenharmony_ci#elif defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
44800b99b8Sopenharmony_ci# define __WORDSIZE	64
45800b99b8Sopenharmony_ci#else
46800b99b8Sopenharmony_ci# define __WORDSIZE	32
47800b99b8Sopenharmony_ci#endif
48800b99b8Sopenharmony_ci
49800b99b8Sopenharmony_ci#define __ELF_NATIVE_CLASS __WORDSIZE
50800b99b8Sopenharmony_ci
51800b99b8Sopenharmony_ci#define ElfW(type)	_ElfW (Elf, __ELF_NATIVE_CLASS, type)
52800b99b8Sopenharmony_ci#define _ElfW(e, w, t)	_ElfW_1 (e, w, _##t)
53800b99b8Sopenharmony_ci#define _ElfW_1(e, w, t)	e##w##t
54800b99b8Sopenharmony_ci
55800b99b8Sopenharmony_ci#define EM_386		 3 // Intel 80386
56800b99b8Sopenharmony_ci#define EM_ARM		40 // ARM
57800b99b8Sopenharmony_ci#define EM_X86_64	62 // AMD x86-64 architecture
58800b99b8Sopenharmony_ci#define EM_AARCH64	183 // ARM AARCH64
59800b99b8Sopenharmony_ci#define EM_RISCV	243 // RISCV
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_ci#define PT_LOAD		1 // Loadable program segment
62800b99b8Sopenharmony_ci#define PT_DYNAMIC	2 // Dynamic linking information
63800b99b8Sopenharmony_ci
64800b99b8Sopenharmony_ci#define PF_X (1 << 0) // Segment is executable
65800b99b8Sopenharmony_ci
66800b99b8Sopenharmony_ci#define SHT_SYMTAB 2 // Symbol table
67800b99b8Sopenharmony_ci#define SHT_DYNSYM 11 // Dynamic linker symbol table
68800b99b8Sopenharmony_ci
69800b99b8Sopenharmony_ci#define DT_NULL		0 // Marks end of dynamic section
70800b99b8Sopenharmony_ci#define DT_PLTGOT	3 // Processor defined value
71800b99b8Sopenharmony_ci#define DT_STRTAB	5 // Address of string table
72800b99b8Sopenharmony_ci#define DT_STRSZ	10 // Size of string table
73800b99b8Sopenharmony_ci#define DT_SONAME	14 // Name of shared object
74800b99b8Sopenharmony_ci
75800b99b8Sopenharmony_ci#define SHN_UNDEF	0 // Undefined section
76800b99b8Sopenharmony_ci
77800b99b8Sopenharmony_ci#define STT_FUNC	2 // Symbol is a code object
78800b99b8Sopenharmony_ci#define STT_GNU_IFUNC	10 // Symbol is indirect code object
79800b99b8Sopenharmony_ci
80800b99b8Sopenharmony_ci#define NT_GNU_BUILD_ID	3
81800b99b8Sopenharmony_ci
82800b99b8Sopenharmony_ci#define ELF32_ST_TYPE(val)		((val) & 0xf)
83800b99b8Sopenharmony_ci
84800b99b8Sopenharmony_ci/* Type for a 16-bit quantity. */
85800b99b8Sopenharmony_citypedef uint16_t Elf32_Half;
86800b99b8Sopenharmony_citypedef uint16_t Elf64_Half;
87800b99b8Sopenharmony_ci
88800b99b8Sopenharmony_ci/* Types for signed and unsigned 32-bit quantities. */
89800b99b8Sopenharmony_citypedef uint32_t Elf32_Word;
90800b99b8Sopenharmony_citypedef	int32_t Elf32_Sword;
91800b99b8Sopenharmony_citypedef uint32_t Elf64_Word;
92800b99b8Sopenharmony_citypedef	int32_t Elf64_Sword;
93800b99b8Sopenharmony_ci
94800b99b8Sopenharmony_ci/* Types for signed and unsigned 64-bit quantities. */
95800b99b8Sopenharmony_citypedef uint64_t Elf32_Xword;
96800b99b8Sopenharmony_citypedef	int64_t Elf32_Sxword;
97800b99b8Sopenharmony_citypedef uint64_t Elf64_Xword;
98800b99b8Sopenharmony_citypedef	int64_t Elf64_Sxword;
99800b99b8Sopenharmony_ci
100800b99b8Sopenharmony_ci/* Type of addresses. */
101800b99b8Sopenharmony_citypedef uint32_t Elf32_Addr;
102800b99b8Sopenharmony_citypedef uint64_t Elf64_Addr;
103800b99b8Sopenharmony_ci
104800b99b8Sopenharmony_ci/* Type of file offsets. */
105800b99b8Sopenharmony_citypedef uint32_t Elf32_Off;
106800b99b8Sopenharmony_citypedef uint64_t Elf64_Off;
107800b99b8Sopenharmony_ci
108800b99b8Sopenharmony_ci/* Type for section indices, which are 16-bit quantities. */
109800b99b8Sopenharmony_citypedef uint16_t Elf32_Section;
110800b99b8Sopenharmony_citypedef uint16_t Elf64_Section;
111800b99b8Sopenharmony_ci
112800b99b8Sopenharmony_ci/* Type for version symbol information. */
113800b99b8Sopenharmony_citypedef Elf32_Half Elf32_Versym;
114800b99b8Sopenharmony_citypedef Elf64_Half Elf64_Versym;
115800b99b8Sopenharmony_ci
116800b99b8Sopenharmony_ci#define EI_NIDENT (16)
117800b99b8Sopenharmony_ci
118800b99b8Sopenharmony_citypedef struct {
119800b99b8Sopenharmony_ci    unsigned char   e_ident[EI_NIDENT]; // Magic number and other info
120800b99b8Sopenharmony_ci    Elf32_Half      e_type; // Object file type
121800b99b8Sopenharmony_ci    Elf32_Half      e_machine; // Architecture
122800b99b8Sopenharmony_ci    Elf32_Word      e_version; // Object file version
123800b99b8Sopenharmony_ci    Elf32_Addr      e_entry; // Entry point virtual address
124800b99b8Sopenharmony_ci    Elf32_Off       e_phoff; // Program header table file offset
125800b99b8Sopenharmony_ci    Elf32_Off       e_shoff; // Section header table file offset
126800b99b8Sopenharmony_ci    Elf32_Word      e_flags; // Processor-specific flags
127800b99b8Sopenharmony_ci    Elf32_Half      e_ehsize; // ELF header size in bytes
128800b99b8Sopenharmony_ci    Elf32_Half      e_phentsize; // Program header table entry size
129800b99b8Sopenharmony_ci    Elf32_Half      e_phnum; // Program header table entry count
130800b99b8Sopenharmony_ci    Elf32_Half      e_shentsize; // Section header table entry size
131800b99b8Sopenharmony_ci    Elf32_Half      e_shnum; // Section header table entry count
132800b99b8Sopenharmony_ci    Elf32_Half      e_shstrndx; // Section header string table index
133800b99b8Sopenharmony_ci} Elf32_Ehdr;
134800b99b8Sopenharmony_ci
135800b99b8Sopenharmony_citypedef struct {
136800b99b8Sopenharmony_ci    unsigned char   e_ident[EI_NIDENT]; // Magic number and other info
137800b99b8Sopenharmony_ci    Elf64_Half      e_type; // Object file type
138800b99b8Sopenharmony_ci    Elf64_Half      e_machine; // Architecture
139800b99b8Sopenharmony_ci    Elf64_Word      e_version; // Object file version
140800b99b8Sopenharmony_ci    Elf64_Addr      e_entry; // Entry point virtual address
141800b99b8Sopenharmony_ci    Elf64_Off       e_phoff; // Program header table file offset
142800b99b8Sopenharmony_ci    Elf64_Off       e_shoff; // Section header table file offset
143800b99b8Sopenharmony_ci    Elf64_Word      e_flags; // Processor-specific flags
144800b99b8Sopenharmony_ci    Elf64_Half      e_ehsize; // ELF header size in bytes
145800b99b8Sopenharmony_ci    Elf64_Half      e_phentsize; // Program header table entry size
146800b99b8Sopenharmony_ci    Elf64_Half      e_phnum; // Program header table entry count
147800b99b8Sopenharmony_ci    Elf64_Half      e_shentsize; // Section header table entry size
148800b99b8Sopenharmony_ci    Elf64_Half      e_shnum; // Section header table entry count
149800b99b8Sopenharmony_ci    Elf64_Half      e_shstrndx; // Section header string table index
150800b99b8Sopenharmony_ci} Elf64_Ehdr;
151800b99b8Sopenharmony_ci
152800b99b8Sopenharmony_citypedef struct {
153800b99b8Sopenharmony_ci    Elf32_Word	p_type; // Segment type
154800b99b8Sopenharmony_ci    Elf32_Off	p_offset; // Segment file offset
155800b99b8Sopenharmony_ci    Elf32_Addr	p_vaddr; // Segment virtual address
156800b99b8Sopenharmony_ci    Elf32_Addr	p_paddr; // Segment physical address
157800b99b8Sopenharmony_ci    Elf32_Word	p_filesz; // Segment size in file
158800b99b8Sopenharmony_ci    Elf32_Word	p_memsz; // Segment size in memory
159800b99b8Sopenharmony_ci    Elf32_Word	p_flags; // Segment flags
160800b99b8Sopenharmony_ci    Elf32_Word	p_align; // Segment alignment
161800b99b8Sopenharmony_ci} Elf32_Phdr;
162800b99b8Sopenharmony_ci
163800b99b8Sopenharmony_citypedef struct {
164800b99b8Sopenharmony_ci    Elf64_Word	p_type; // Segment type
165800b99b8Sopenharmony_ci    Elf64_Word	p_flags; // Segment flags
166800b99b8Sopenharmony_ci    Elf64_Off	p_offset; // Segment file offset
167800b99b8Sopenharmony_ci    Elf64_Addr	p_vaddr; // Segment virtual address
168800b99b8Sopenharmony_ci    Elf64_Addr	p_paddr; // Segment physical address
169800b99b8Sopenharmony_ci    Elf64_Xword	p_filesz; // Segment size in file
170800b99b8Sopenharmony_ci    Elf64_Xword	p_memsz; // Segment size in memory
171800b99b8Sopenharmony_ci    Elf64_Xword	p_align; // Segment alignment
172800b99b8Sopenharmony_ci} Elf64_Phdr;
173800b99b8Sopenharmony_ci
174800b99b8Sopenharmony_citypedef struct {
175800b99b8Sopenharmony_ci    Elf32_Word	sh_name; // Section name (string tbl index)
176800b99b8Sopenharmony_ci    Elf32_Word	sh_type; // Section type
177800b99b8Sopenharmony_ci    Elf32_Word	sh_flags; // Section flags
178800b99b8Sopenharmony_ci    Elf32_Addr	sh_addr;  // Section virtual addr at execution
179800b99b8Sopenharmony_ci    Elf32_Off	sh_offset; // Section file offset
180800b99b8Sopenharmony_ci    Elf32_Word	sh_size; // Section size in bytes
181800b99b8Sopenharmony_ci    Elf32_Word	sh_link; // Link to another section
182800b99b8Sopenharmony_ci    Elf32_Word	sh_info;  // Additional section information
183800b99b8Sopenharmony_ci    Elf32_Word	sh_addralign; // Section alignment
184800b99b8Sopenharmony_ci    Elf32_Word	sh_entsize; // Entry size if section holds table
185800b99b8Sopenharmony_ci} Elf32_Shdr;
186800b99b8Sopenharmony_ci
187800b99b8Sopenharmony_citypedef struct {
188800b99b8Sopenharmony_ci    Elf64_Word	sh_name; // Section name (string tbl index)
189800b99b8Sopenharmony_ci    Elf64_Word	sh_type; // Section type
190800b99b8Sopenharmony_ci    Elf64_Xword	sh_flags; // Section flags
191800b99b8Sopenharmony_ci    Elf64_Addr	sh_addr; // Section virtual addr at execution
192800b99b8Sopenharmony_ci    Elf64_Off	sh_offset; // Section file offset
193800b99b8Sopenharmony_ci    Elf64_Xword	sh_size; // Section size in bytes
194800b99b8Sopenharmony_ci    Elf64_Word	sh_link; // Link to another section
195800b99b8Sopenharmony_ci    Elf64_Word	sh_info; // Additional section information
196800b99b8Sopenharmony_ci    Elf64_Xword	sh_addralign; // Section alignment
197800b99b8Sopenharmony_ci    Elf64_Xword	sh_entsize; // Entry size if section holds table
198800b99b8Sopenharmony_ci} Elf64_Shdr;
199800b99b8Sopenharmony_ci
200800b99b8Sopenharmony_citypedef struct {
201800b99b8Sopenharmony_ci    Elf32_Sword	   d_tag; // Dynamic entry type
202800b99b8Sopenharmony_ci    union {
203800b99b8Sopenharmony_ci        Elf32_Word d_val; // Integer value
204800b99b8Sopenharmony_ci        Elf32_Addr d_ptr; // Address value
205800b99b8Sopenharmony_ci    } d_un;
206800b99b8Sopenharmony_ci} Elf32_Dyn;
207800b99b8Sopenharmony_ci
208800b99b8Sopenharmony_citypedef struct {
209800b99b8Sopenharmony_ci    Elf64_Sxword	d_tag; // Dynamic entry type
210800b99b8Sopenharmony_ci    union {
211800b99b8Sopenharmony_ci        Elf64_Xword d_val; // Integer value
212800b99b8Sopenharmony_ci        Elf64_Addr  d_ptr; // Address value
213800b99b8Sopenharmony_ci    } d_un;
214800b99b8Sopenharmony_ci} Elf64_Dyn;
215800b99b8Sopenharmony_ci
216800b99b8Sopenharmony_citypedef struct {
217800b99b8Sopenharmony_ci    Elf32_Word      st_name; // Symbol name (string tbl index)
218800b99b8Sopenharmony_ci    Elf32_Addr      st_value; // Symbol value
219800b99b8Sopenharmony_ci    Elf32_Word      st_size; // Symbol size
220800b99b8Sopenharmony_ci    unsigned char   st_info; // Symbol type and binding
221800b99b8Sopenharmony_ci    unsigned char   st_other; // Symbol visibility
222800b99b8Sopenharmony_ci    Elf32_Section   st_shndx; // Section index
223800b99b8Sopenharmony_ci} Elf32_Sym;
224800b99b8Sopenharmony_ci
225800b99b8Sopenharmony_citypedef struct {
226800b99b8Sopenharmony_ci    Elf64_Word      st_name; // Symbol name (string tbl index)
227800b99b8Sopenharmony_ci    unsigned char	st_info; // Symbol type and binding
228800b99b8Sopenharmony_ci    unsigned char   st_other; // Symbol visibility
229800b99b8Sopenharmony_ci    Elf64_Section   st_shndx; // Section index
230800b99b8Sopenharmony_ci    Elf64_Addr      st_value; // Symbol value
231800b99b8Sopenharmony_ci    Elf64_Xword	    st_size; // Symbol size
232800b99b8Sopenharmony_ci} Elf64_Sym;
233800b99b8Sopenharmony_ci
234800b99b8Sopenharmony_citypedef struct {
235800b99b8Sopenharmony_ci    Elf32_Word n_namesz; // Length of the note's name.
236800b99b8Sopenharmony_ci    Elf32_Word n_descsz; // Length of the note's descriptor.
237800b99b8Sopenharmony_ci    Elf32_Word n_type; // Type of the note.
238800b99b8Sopenharmony_ci} Elf32_Nhdr;
239800b99b8Sopenharmony_ci
240800b99b8Sopenharmony_citypedef struct {
241800b99b8Sopenharmony_ci    Elf64_Word n_namesz; // Length of the note's name.
242800b99b8Sopenharmony_ci    Elf64_Word n_descsz; // Length of the note's descriptor.
243800b99b8Sopenharmony_ci    Elf64_Word n_type; // Type of the note.
244800b99b8Sopenharmony_ci} Elf64_Nhdr;
245800b99b8Sopenharmony_ci
246800b99b8Sopenharmony_ci#define	ELFMAG  "\177ELF"
247800b99b8Sopenharmony_ci#define	SELFMAG	 4
248800b99b8Sopenharmony_ci
249800b99b8Sopenharmony_ci#define EI_CLASS  4 // File class byte index
250800b99b8Sopenharmony_ci#define ELFCLASSNONE  0 // Invalid class
251800b99b8Sopenharmony_ci#define ELFCLASS32	1 // 32-bit objects
252800b99b8Sopenharmony_ci#define ELFCLASS64	2 // 64-bit objects
253800b99b8Sopenharmony_ci
254800b99b8Sopenharmony_ci/* Sharing types (must choose one and only one of these). */
255800b99b8Sopenharmony_ci#define MAP_SHARED	0x01 // Share changes.
256800b99b8Sopenharmony_ci#define MAP_PRIVATE	0x02 // Changes are private.
257800b99b8Sopenharmony_ci
258800b99b8Sopenharmony_ci#endif
259