Initial xloong code
[xloong.git] / pmon / loaders / elf.h
blob7ef3d4dd609b5b2abc362b4febfd39798c25f14c
1 /* $Id: elf.h,v 1.1.1.1 2006/09/14 01:59:08 root Exp $ */
2 /*
3 * Copyright (c) 2001-2002 Opsycon AB (www.opsycon.se / www.opsycon.com)
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Opsycon AB, Sweden.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
33 * ELF data structures and values
36 typedef unsigned short Elf32_Half;
37 typedef unsigned int Elf32_Word;
38 typedef signed int Elf32_Sword;
39 typedef unsigned int Elf32_Off;
40 typedef unsigned int Elf32_Addr;
41 typedef unsigned char Elf_Char;
44 * File Header
47 #define EI_NIDENT 16
49 typedef struct {
50 Elf_Char e_ident[EI_NIDENT];
51 Elf32_Half e_type;
52 Elf32_Half e_machine;
53 Elf32_Word e_version;
54 Elf32_Addr e_entry;
55 Elf32_Off e_phoff;
56 Elf32_Off e_shoff;
57 Elf32_Word e_flags;
58 Elf32_Half e_ehsize;
59 Elf32_Half e_phentsize;
60 Elf32_Half e_phnum;
61 Elf32_Half e_shentsize;
62 Elf32_Half e_shnum;
63 Elf32_Half e_shstrndx;
64 } Elf32_Ehdr;
66 /* e_indent */
67 #define EI_MAG0 0 /* File identification byte 0 index */
68 #define EI_MAG1 1 /* File identification byte 1 index */
69 #define EI_MAG2 2 /* File identification byte 2 index */
70 #define EI_MAG3 3 /* File identification byte 3 index */
71 #define EI_CLASS 4 /* File class */
72 #define ELFCLASSNONE 0 /* Invalid class */
73 #define ELFCLASS32 1 /* 32-bit objects */
74 #define ELFCLASS64 2 /* 64-bit objects */
75 #define EI_DATA 5 /* Data encoding */
76 #define ELFDATANONE 0 /* Invalid data encoding */
77 #define ELFDATA2LSB 1 /* 2's complement, little endian */
78 #define ELFDATA2MSB 2 /* 2's complement, big endian */
79 #define EI_VERSION 6 /* File version */
80 #define EI_PAD 7 /* Start of padding bytes */
82 #define ELFMAG0 0x7F /* Magic number byte 0 */
83 #define ELFMAG1 'E' /* Magic number byte 1 */
84 #define ELFMAG2 'L' /* Magic number byte 2 */
85 #define ELFMAG3 'F' /* Magic number byte 3 */
87 /* e_type */
88 #define ET_NONE 0 /* No file type */
89 #define ET_REL 1 /* Relocatable file */
90 #define ET_EXEC 2 /* Executable file */
91 #define ET_DYN 3 /* Shared object file */
92 #define ET_CORE 4 /* Core file */
93 #define ET_LOPROC 0xFF00 /* Processor-specific */
94 #define ET_HIPROC 0xFFFF /* Processor-specific */
96 /* e_machine */
97 #define EM_NONE 0 /* No machine */
98 #define EM_M32 1 /* AT&T WE 32100 */
99 #define EM_SPARC 2 /* SUN SPARC */
100 #define EM_386 3 /* Intel 80386 */
101 #define EM_68K 4 /* Motorola m68k family */
102 #define EM_88K 5 /* Motorola m88k family */
103 #define EM_860 7 /* Intel 80860 */
104 #define EM_MIPS 8 /* MIPS R3000 */
106 #define EM_PPC 20 /* PowerPC */
108 /* e_version */
109 #define EV_NONE 0 /* Invalid ELF version */
110 #define EV_CURRENT 1 /* Current version */
114 * Program Header
116 typedef struct {
117 Elf32_Word p_type; /* Identifies program segment type */
118 Elf32_Off p_offset; /* Segment file offset */
119 Elf32_Addr p_vaddr; /* Segment virtual address */
120 Elf32_Addr p_paddr; /* Segment physical address */
121 Elf32_Word p_filesz; /* Segment size in file */
122 Elf32_Word p_memsz; /* Segment size in memory */
123 Elf32_Word p_flags; /* Segment flags */
124 Elf32_Word p_align; /* Segment alignment, file & memory */
125 } Elf32_Phdr;
128 /* p_type */
129 #define PT_NULL 0 /* Program header table entry unused */
130 #define PT_LOAD 1 /* Loadable program segment */
131 #define PT_DYNAMIC 2 /* Dynamic linking information */
132 #define PT_INTERP 3 /* Program interpreter */
133 #define PT_NOTE 4 /* Auxiliary information */
134 #define PT_SHLIB 5 /* Reserved, unspecified semantics */
135 #define PT_PHDR 6 /* Entry for header table itself */
136 #define PT_LOPROC 0x70000000 /* Processor-specific */
137 #define PT_HIPROC 0x7FFFFFFF /* Processor-specific */
139 /* p_flags */
140 #define PF_X (1 << 0) /* Segment is executable */
141 #define PF_W (1 << 1) /* Segment is writable */
142 #define PF_R (1 << 2) /* Segment is readable */
143 #define PF_MASKPROC 0xF0000000 /* Processor-specific reserved bits */
147 * Section Header
149 typedef struct {
150 Elf32_Word sh_name;
151 Elf32_Word sh_type;
152 Elf32_Word sh_flags;
153 Elf32_Addr sh_addr;
154 Elf32_Off sh_offset;
155 Elf32_Word sh_size;
156 Elf32_Word sh_link;
157 Elf32_Word sh_info;
158 Elf32_Word sh_addralign;
159 Elf32_Word sh_entsize;
160 } Elf32_Shdr;
162 /* sh_type */
163 #define SHT_NULL 0 /* Section header table entry unused */
164 #define SHT_PROGBITS 1 /* Program specific (private) data */
165 #define SHT_SYMTAB 2 /* Link editing symbol table */
166 #define SHT_STRTAB 3 /* A string table */
167 #define SHT_RELA 4 /* Relocation entries with addends */
168 #define SHT_HASH 5 /* A symbol hash table */
169 #define SHT_DYNAMIC 6 /* Information for dynamic linking */
170 #define SHT_NOTE 7 /* Information that marks file */
171 #define SHT_NOBITS 8 /* Section occupies no space in file */
172 #define SHT_REL 9 /* Relocation entries, no addends */
173 #define SHT_SHLIB 10 /* Reserved, unspecified semantics */
174 #define SHT_DYNSYM 11 /* Dynamic linking symbol table */
175 #define SHT_LOPROC 0x70000000 /* Processor-specific semantics, lo */
176 #define SHT_HIPROC 0x7FFFFFFF /* Processor-specific semantics, hi */
177 #define SHT_LOUSER 0x80000000 /* Application-specific semantics */
178 #define SHT_HIUSER 0x8FFFFFFF /* Application-specific semantics */
180 /* sh_flags */
181 #define SHF_WRITE (1 << 0) /* Writable data during execution */
182 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
183 #define SHF_EXECINSTR (1 << 2) /* Executable machine instructions */
184 #define SHF_MASKPROC 0xF0000000 /* Processor-specific semantics */
188 * Symbol Table
190 typedef struct {
191 Elf32_Word st_name;
192 Elf32_Addr st_value;
193 Elf32_Word st_size;
194 Elf_Char st_info;
195 Elf_Char st_other;
196 Elf32_Half st_shndx;
197 } Elf32_Sym;
200 #define ELF_ST_BIND(val) (((unsigned int)(val)) >> 4)
201 #define ELF_ST_TYPE(val) ((val) & 0xF)
202 #define ELF_ST_INFO(bind,type) (((bind) << 4) | ((type) & 0xF))
204 /* symbol binding */
205 #define STB_LOCAL 0 /* Symbol not visible outside obj */
206 #define STB_GLOBAL 1 /* Symbol visible outside obj */
207 #define STB_WEAK 2 /* Like globals, lower precedence */
208 #define STB_LOPROC 13 /* Application-specific semantics */
209 #define STB_HIPROC 15 /* Application-specific semantics */
211 /* symbol type */
212 #define STT_NOTYPE 0 /* Symbol type is unspecified */
213 #define STT_OBJECT 1 /* Symbol is a data object */
214 #define STT_FUNC 2 /* Symbol is a code object */
215 #define STT_SECTION 3 /* Symbol associated with a section */
216 #define STT_FILE 4 /* Symbol gives a file name */
217 #define STT_LOPROC 13 /* Application-specific semantics */
218 #define STT_HIPROC 15 /* Application-specific semantics */
220 /* special values st_shndx */
221 #define SHN_UNDEF 0 /* Undefined section reference */
222 #define SHN_LORESERV 0xFF00 /* Begin range of reserved indices */
223 #define SHN_LOPROC 0xFF00 /* Begin range of appl-specific */
224 #define SHN_HIPROC 0xFF1F /* End range of appl-specific */
225 #define SHN_ABS 0xFFF1 /* Associated symbol is absolute */
226 #define SHN_COMMON 0xFFF2 /* Associated symbol is in common */
227 #define SHN_HIRESERVE 0xFFFF /* End range of reserved indices */
228 /* for elf64 */
230 typedef unsigned long long __u64;
231 typedef unsigned int __u32;
232 typedef unsigned short __u16;
233 typedef signed int __s32;
234 typedef signed short __s16;
235 typedef long long __s64;
237 /* 64-bit ELF base types. */
238 typedef __u64 Elf64_Addr;
239 typedef __u16 Elf64_Half;
240 typedef __s16 Elf64_SHalf;
241 typedef __u64 Elf64_Off;
242 typedef __s32 Elf64_Sword;
243 typedef __u32 Elf64_Word;
244 typedef __u64 Elf64_Xword;
245 typedef __s64 Elf64_Sxword;
247 typedef struct {
248 Elf64_Sxword d_tag; /* entry tag value */
249 union {
250 Elf64_Xword d_val;
251 Elf64_Addr d_ptr;
252 } d_un;
253 } Elf64_Dyn;
256 #define ELF64_R_SYM(i) ((i) >> 32)
257 #define ELF64_R_TYPE(i) ((i) & 0xffffffff)
260 typedef struct elf64_rel {
261 Elf64_Addr r_offset; /* Location at which to apply the action */
262 Elf64_Xword r_info; /* index and type of relocation */
263 } Elf64_Rel;
265 typedef struct elf64_rela {
266 Elf64_Addr r_offset; /* Location at which to apply the action */
267 Elf64_Xword r_info; /* index and type of relocation */
268 Elf64_Sxword r_addend; /* Constant addend used to compute value */
269 } Elf64_Rela;
271 typedef struct elf64_sym {
272 Elf64_Word st_name; /* Symbol name, index in string tbl */
273 unsigned char st_info; /* Type and binding attributes */
274 unsigned char st_other; /* No defined meaning, 0 */
275 Elf64_Half st_shndx; /* Associated section index */
276 Elf64_Addr st_value; /* Value of the symbol */
277 Elf64_Xword st_size; /* Associated symbol size */
278 } Elf64_Sym;
281 typedef struct elf64_hdr {
282 unsigned char e_ident[16]; /* ELF "magic number" */
283 Elf64_Half e_type;
284 Elf64_Half e_machine;
285 Elf64_Word e_version;
286 Elf64_Addr e_entry; /* Entry point virtual address */
287 Elf64_Off e_phoff; /* Program header table file offset */
288 Elf64_Off e_shoff; /* Section header table file offset */
289 Elf64_Word e_flags;
290 Elf64_Half e_ehsize;
291 Elf64_Half e_phentsize;
292 Elf64_Half e_phnum;
293 Elf64_Half e_shentsize;
294 Elf64_Half e_shnum;
295 Elf64_Half e_shstrndx;
296 } Elf64_Ehdr;
299 typedef struct elf64_phdr {
300 Elf64_Word p_type;
301 Elf64_Word p_flags;
302 Elf64_Off p_offset; /* Segment file offset */
303 Elf64_Addr p_vaddr; /* Segment virtual address */
304 Elf64_Addr p_paddr; /* Segment physical address */
305 Elf64_Xword p_filesz; /* Segment size in file */
306 Elf64_Xword p_memsz; /* Segment size in memory */
307 Elf64_Xword p_align; /* Segment alignment, file & memory */
308 } Elf64_Phdr;
310 typedef struct elf64_shdr {
311 Elf64_Word sh_name; /* Section name, index in string tbl */
312 Elf64_Word sh_type; /* Type of section */
313 Elf64_Xword sh_flags; /* Miscellaneous section attributes */
314 Elf64_Addr sh_addr; /* Section virtual addr at execution */
315 Elf64_Off sh_offset; /* Section file offset */
316 Elf64_Xword sh_size; /* Size of section in bytes */
317 Elf64_Word sh_link; /* Index of another section */
318 Elf64_Word sh_info; /* Additional section information */
319 Elf64_Xword sh_addralign; /* Section alignment */
320 Elf64_Xword sh_entsize; /* Entry size if section holds table */
321 } Elf64_Shdr;
323 /* Note header in a PT_NOTE section */
324 typedef struct elf64_note {
325 Elf64_Word n_namesz; /* Name size */
326 Elf64_Word n_descsz; /* Content size */
327 Elf64_Word n_type; /* Content type */
328 } Elf64_Nhdr;
331 #if ELF_CLASS == ELFCLASS32
333 extern Elf32_Dyn _DYNAMIC [];
334 #define elfhdr elf32_hdr
335 #define elf_phdr elf32_phdr
336 #define elf_note elf32_note
338 #else
340 extern Elf64_Dyn _DYNAMIC [];
341 #define elfhdr elf64_hdr
342 #define elf_phdr elf64_phdr
343 #define elf_note elf64_note
345 #endif