source: mainline/abi/include/abi/elf.h@ b58728f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b58728f was b58728f, checked in by Jakub Jermář <jakub@…>, 7 years ago

Move dynamic-linking related ELF definitions to ABI includes

Move dynamic-linking related ELF definitions from
uspace/lib/c/include/rtld/elf_dyn.h to abi/include/abi/elf.h. This
allows their use by the AArch64 boot code.

  • Property mode set to 100644
File size: 10.3 KB
Line 
1/*
2 * Copyright (c) 2006 Sergey Bondari
3 * All rights reserved.
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup abi_generic
30 * @{
31 */
32/** @file
33 */
34
35#ifndef _ABI_ELF_H_
36#define _ABI_ELF_H_
37
38#include <stdint.h>
39#include <abi/arch/elf.h>
40
41/**
42 * Current ELF version
43 */
44enum {
45 EV_CURRENT = 1,
46};
47
48/**
49 * ELF types
50 */
51enum elf_type {
52 ET_NONE = 0, /* No type */
53 ET_REL = 1, /* Relocatable file */
54 ET_EXEC = 2, /* Executable */
55 ET_DYN = 3, /* Shared object */
56 ET_CORE = 4, /* Core */
57};
58
59enum {
60 ET_LOPROC = 0xff00, /* Lowest processor specific */
61 ET_HIPROC = 0xffff, /* Highest processor specific */
62};
63
64/**
65 * ELF machine types
66 */
67enum elf_machine {
68 EM_NO = 0, /* No machine */
69 EM_SPARC = 2, /* SPARC */
70 EM_386 = 3, /* i386 */
71 EM_MIPS = 8, /* MIPS RS3000 */
72 EM_MIPS_RS3_LE = 10, /* MIPS RS3000 LE */
73 EM_PPC = 20, /* PPC32 */
74 EM_PPC64 = 21, /* PPC64 */
75 EM_ARM = 40, /* ARM */
76 EM_SPARCV9 = 43, /* SPARC64 */
77 EM_IA_64 = 50, /* IA-64 */
78 EM_X86_64 = 62, /* AMD64/EMT64 */
79 EM_RISCV = 243, /* RISC-V */
80};
81
82/**
83 * ELF identification indexes
84 */
85enum {
86 EI_MAG0 = 0,
87 EI_MAG1 = 1,
88 EI_MAG2 = 2,
89 EI_MAG3 = 3,
90 EI_CLASS = 4, /* File class */
91 EI_DATA = 5, /* Data encoding */
92 EI_VERSION = 6, /* File version */
93 EI_OSABI = 7,
94 EI_ABIVERSION = 8,
95 EI_PAD = 9, /* Start of padding bytes */
96 EI_NIDENT = 16, /* ELF identification table size */
97};
98
99/**
100 * ELF magic number
101 */
102enum {
103 ELFMAG0 = 0x7f,
104 ELFMAG1 = 'E',
105 ELFMAG2 = 'L',
106 ELFMAG3 = 'F',
107};
108
109/**
110 * ELF file classes
111 */
112enum elf_class {
113 ELFCLASSNONE = 0,
114 ELFCLASS32 = 1,
115 ELFCLASS64 = 2,
116};
117
118/**
119 * ELF data encoding types
120 */
121enum elf_data_encoding {
122 ELFDATANONE = 0,
123 ELFDATA2LSB = 1, /* Least significant byte first (little endian) */
124 ELFDATA2MSB = 2, /* Most signigicant byte first (big endian) */
125};
126
127/**
128 * ELF section types
129 */
130enum elf_section_type {
131 SHT_NULL = 0,
132 SHT_PROGBITS = 1,
133 SHT_SYMTAB = 2,
134 SHT_STRTAB = 3,
135 SHT_RELA = 4,
136 SHT_HASH = 5,
137 SHT_DYNAMIC = 6,
138 SHT_NOTE = 7,
139 SHT_NOBITS = 8,
140 SHT_REL = 9,
141 SHT_SHLIB = 10,
142 SHT_DYNSYM = 11,
143};
144
145enum {
146 SHT_LOOS = 0x60000000,
147 SHT_HIOS = 0x6fffffff,
148 SHT_LOPROC = 0x70000000,
149 SHT_HIPROC = 0x7fffffff,
150 SHT_LOUSER = 0x80000000,
151 SHT_HIUSER = 0xffffffff,
152};
153
154/**
155 * ELF section flags
156 */
157enum {
158 SHF_WRITE = 0x1,
159 SHF_ALLOC = 0x2,
160 SHF_EXECINSTR = 0x4,
161 SHF_TLS = 0x400,
162 SHF_MASKPROC = 0xf0000000,
163};
164
165/** Functions for decomposing elf_symbol.st_info into binding and type */
166static inline uint8_t elf_st_bind(uint8_t info)
167{
168 return info >> 4;
169}
170
171static inline uint8_t elf_st_type(uint8_t info)
172{
173 return info & 0x0f;
174}
175
176static inline uint8_t elf_st_info(uint8_t bind, uint8_t type)
177{
178 return (bind << 4) | (type & 0x0f);
179}
180
181/**
182 * Symbol binding
183 */
184enum elf_symbol_binding {
185 STB_LOCAL = 0,
186 STB_GLOBAL = 1,
187 STB_WEAK = 2,
188};
189
190enum {
191 STB_LOPROC = 13,
192 STB_HIPROC = 15,
193};
194
195/**
196 * Symbol types
197 */
198enum elf_symbol_type {
199 STT_NOTYPE = 0,
200 STT_OBJECT = 1,
201 STT_FUNC = 2,
202 STT_SECTION = 3,
203 STT_FILE = 4,
204 STT_TLS = 6,
205};
206
207enum {
208 STT_LOPROC = 13,
209 STT_HIPROC = 15,
210};
211
212/**
213 * Program segment types
214 */
215enum elf_segment_type {
216 PT_NULL = 0,
217 PT_LOAD = 1,
218 PT_DYNAMIC = 2,
219 PT_INTERP = 3,
220 PT_NOTE = 4,
221 PT_SHLIB = 5,
222 PT_PHDR = 6,
223 PT_TLS = 7,
224
225 PT_GNU_EH_FRAME = 0x6474e550,
226 PT_GNU_STACK = 0x6474e551,
227 PT_GNU_RELRO = 0x6474e552,
228};
229
230enum {
231 PT_LOOS = 0x60000000,
232 PT_HIOS = 0x6fffffff,
233 PT_LOPROC = 0x70000000,
234 PT_HIPROC = 0x7fffffff,
235};
236
237/**
238 * Program segment attributes.
239 */
240enum elf_segment_access {
241 PF_X = 1,
242 PF_W = 2,
243 PF_R = 4,
244};
245
246/**
247 * Dynamic array tags
248 */
249enum elf_dynamic_tag {
250 DT_NULL = 0,
251 DT_NEEDED = 1,
252 DT_PLTRELSZ = 2,
253 DT_PLTGOT = 3,
254 DT_HASH = 4,
255 DT_STRTAB = 5,
256 DT_SYMTAB = 6,
257 DT_RELA = 7,
258 DT_RELASZ = 8,
259 DT_RELAENT = 9,
260 DT_STRSZ = 10,
261 DT_SYMENT = 11,
262 DT_INIT = 12,
263 DT_FINI = 13,
264 DT_SONAME = 14,
265 DT_RPATH = 15,
266 DT_SYMBOLIC = 16,
267 DT_REL = 17,
268 DT_RELSZ = 18,
269 DT_RELENT = 19,
270 DT_PLTREL = 20,
271 DT_DEBUG = 21,
272 DT_TEXTREL = 22,
273 DT_JMPREL = 23,
274 DT_BIND_NOW = 24,
275 DT_LOPROC = 0x70000000,
276 DT_HIPROC = 0x7fffffff,
277};
278
279/**
280 * Special section indexes
281 */
282enum {
283 SHN_UNDEF = 0,
284 SHN_LORESERVE = 0xff00,
285 SHN_LOPROC = 0xff00,
286 SHN_HIPROC = 0xff1f,
287 SHN_ABS = 0xfff1,
288 SHN_COMMON = 0xfff2,
289 SHN_HIRESERVE = 0xffff,
290};
291
292/**
293 * Special symbol table index
294 */
295enum {
296 STN_UNDEF = 0,
297};
298
299/**
300 * ELF data types
301 *
302 * These types are found to be identical in both 32-bit and 64-bit
303 * ELF object file specifications. They are the only types used
304 * in ELF header.
305 */
306typedef uint64_t elf_xword;
307typedef int64_t elf_sxword;
308typedef uint32_t elf_word;
309typedef int32_t elf_sword;
310typedef uint16_t elf_half;
311
312/**
313 * 32-bit ELF data types.
314 *
315 * These types are specific for 32-bit format.
316 */
317typedef uint32_t elf32_addr;
318typedef uint32_t elf32_off;
319
320/**
321 * 64-bit ELF data types.
322 *
323 * These types are specific for 64-bit format.
324 */
325typedef uint64_t elf64_addr;
326typedef uint64_t elf64_off;
327
328/** ELF header */
329struct elf32_header {
330 uint8_t e_ident[EI_NIDENT];
331 elf_half e_type;
332 elf_half e_machine;
333 elf_word e_version;
334 elf32_addr e_entry;
335 elf32_off e_phoff;
336 elf32_off e_shoff;
337 elf_word e_flags;
338 elf_half e_ehsize;
339 elf_half e_phentsize;
340 elf_half e_phnum;
341 elf_half e_shentsize;
342 elf_half e_shnum;
343 elf_half e_shstrndx;
344};
345
346struct elf64_header {
347 uint8_t e_ident[EI_NIDENT];
348 elf_half e_type;
349 elf_half e_machine;
350 elf_word e_version;
351 elf64_addr e_entry;
352 elf64_off e_phoff;
353 elf64_off e_shoff;
354 elf_word e_flags;
355 elf_half e_ehsize;
356 elf_half e_phentsize;
357 elf_half e_phnum;
358 elf_half e_shentsize;
359 elf_half e_shnum;
360 elf_half e_shstrndx;
361};
362
363/**
364 * ELF segment header.
365 * Segments headers are also known as program headers.
366 */
367struct elf32_segment_header {
368 elf_word p_type;
369 elf32_off p_offset;
370 elf32_addr p_vaddr;
371 elf32_addr p_paddr;
372 elf_word p_filesz;
373 elf_word p_memsz;
374 elf_word p_flags;
375 elf_word p_align;
376};
377
378struct elf64_segment_header {
379 elf_word p_type;
380 elf_word p_flags;
381 elf64_off p_offset;
382 elf64_addr p_vaddr;
383 elf64_addr p_paddr;
384 elf_xword p_filesz;
385 elf_xword p_memsz;
386 elf_xword p_align;
387};
388
389/**
390 * ELF section header
391 */
392struct elf32_section_header {
393 elf_word sh_name;
394 elf_word sh_type;
395 elf_word sh_flags;
396 elf32_addr sh_addr;
397 elf32_off sh_offset;
398 elf_word sh_size;
399 elf_word sh_link;
400 elf_word sh_info;
401 elf_word sh_addralign;
402 elf_word sh_entsize;
403};
404
405struct elf64_section_header {
406 elf_word sh_name;
407 elf_word sh_type;
408 elf_xword sh_flags;
409 elf64_addr sh_addr;
410 elf64_off sh_offset;
411 elf_xword sh_size;
412 elf_word sh_link;
413 elf_word sh_info;
414 elf_xword sh_addralign;
415 elf_xword sh_entsize;
416};
417
418/**
419 * ELF symbol table entry
420 */
421struct elf32_symbol {
422 elf_word st_name;
423 elf32_addr st_value;
424 elf_word st_size;
425 uint8_t st_info;
426 uint8_t st_other;
427 elf_half st_shndx;
428};
429
430struct elf64_symbol {
431 elf_word st_name;
432 uint8_t st_info;
433 uint8_t st_other;
434 elf_half st_shndx;
435 elf64_addr st_value;
436 elf_xword st_size;
437};
438
439/*
440 * ELF note segment entry
441 */
442struct elf32_note {
443 elf_word namesz;
444 elf_word descsz;
445 elf_word type;
446};
447
448/*
449 * NOTE: namesz, descsz and type should be 64-bits wide (elf_xword)
450 * per the 64-bit ELF spec. The Linux kernel however screws up and
451 * defines them as Elf64_Word, which is 32-bits wide(!). We are trying
452 * to make our core files compatible with Linux GDB target so we copy
453 * the blunder here.
454 */
455struct elf64_note {
456 elf_word namesz;
457 elf_word descsz;
458 elf_word type;
459};
460
461/**
462 * Dynamic structure
463 */
464struct elf32_dyn {
465 elf_sword d_tag;
466 union {
467 elf_word d_val;
468 elf32_addr d_ptr;
469 } d_un;
470};
471
472struct elf64_dyn {
473 elf_sxword d_tag;
474 union {
475 elf_xword d_val;
476 elf64_addr d_ptr;
477 } d_un;
478};
479
480struct elf32_rel {
481 elf32_addr r_offset;
482 elf_word r_info;
483};
484
485struct elf32_rela {
486 elf32_addr r_offset;
487 elf_word r_info;
488 elf_sword r_addend;
489};
490
491struct elf64_rel {
492 elf64_addr r_offset;
493 elf_xword r_info;
494};
495
496struct elf64_rela {
497 elf64_addr r_offset;
498 elf_xword r_info;
499 elf_sxword r_addend;
500};
501
502#define ELF32_R_SYM(i) ((i) >> 8)
503#define ELF32_R_TYPE(i) ((unsigned char)(i))
504
505#define ELF64_R_SYM(i) ((i) >> 32)
506#define ELF64_R_TYPE(i) ((i) & 0xffffffffL)
507
508#ifdef __32_BITS__
509typedef struct elf32_header elf_header_t;
510typedef struct elf32_segment_header elf_segment_header_t;
511typedef struct elf32_section_header elf_section_header_t;
512typedef struct elf32_symbol elf_symbol_t;
513typedef struct elf32_note elf_note_t;
514typedef struct elf32_dyn elf_dyn_t;
515typedef struct elf32_rel elf_rel_t;
516typedef struct elf32_rela elf_rela_t;
517#endif
518
519#ifdef __64_BITS__
520typedef struct elf64_header elf_header_t;
521typedef struct elf64_segment_header elf_segment_header_t;
522typedef struct elf64_section_header elf_section_header_t;
523typedef struct elf64_symbol elf_symbol_t;
524typedef struct elf64_note elf_note_t;
525typedef struct elf64_dyn elf_dyn_t;
526typedef struct elf64_rel elf_rel_t;
527typedef struct elf64_rela elf_rela_t;
528#endif
529
530#endif
531
532/** @}
533 */
Note: See TracBrowser for help on using the repository browser.