Debian Codegraph @

output/elf.h

raw
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /* Copyright 1996-2018 The NASM Authors - All Rights Reserved */
3
4 #ifndef OUTPUT_ELF_H
5 #define OUTPUT_ELF_H
6
7 /*
8 * Since NASM support both Elf32/64 file formats
9 * we need to cover all types, structures, typedefs and etc
10 */
11
12 #include "compiler.h"
13
14 /* Segment types */
15 #define PT_NULL 0
16 #define PT_LOAD 1
17 #define PT_DYNAMIC 2
18 #define PT_INTERP 3
19 #define PT_NOTE 4
20 #define PT_SHLIB 5
21 #define PT_PHDR 6
22 #define PT_LOOS 0x60000000
23 #define PT_HIOS 0x6fffffff
24 #define PT_LOPROC 0x70000000
25 #define PT_HIPROC 0x7fffffff
26 #define PT_GNU_EH_FRAME 0x6474e550 /* Extension, eh? */
27
28 /* ELF file types */
29 #define ET_NONE 0
30 #define ET_REL 1
31 #define ET_EXEC 2
32 #define ET_DYN 3
33 #define ET_CORE 4
34 #define ET_LOPROC 0xff00
35 #define ET_HIPROC 0xffff
36
37 /* ELF machine types */
38 #define EM_NONE 0
39 #define EM_M32 1
40 #define EM_SPARC 2
41 #define EM_386 3
42 #define EM_68K 4
43 #define EM_88K 5
44 #define EM_486 6 /* Not used in Linux at least */
45 #define EM_860 7
46 #define EM_MIPS 8 /* R3k, bigendian(?) */
47 #define EM_MIPS_RS4_BE 10 /* R4k BE */
48 #define EM_PARISC 15
49 #define EM_SPARC32PLUS 18
50 #define EM_PPC 20
51 #define EM_PPC64 21
52 #define EM_S390 22
53 #define EM_SH 42
54 #define EM_SPARCV9 43 /* v9 = SPARC64 */
55 #define EM_H8_300H 47
56 #define EM_H8S 48
57 #define EM_IA_64 50
58 #define EM_X86_64 62
59 #define EM_CRIS 76
60 #define EM_V850 87
61 #define EM_ALPHA 0x9026 /* Interim Alpha that stuck around */
62 #define EM_CYGNUS_V850 0x9080 /* Old v850 ID used by Cygnus */
63 #define EM_S390_OLD 0xA390 /* Obsolete interim value for S/390 */
64
65 /* Dynamic type values */
66 #define DT_NULL 0
67 #define DT_NEEDED 1
68 #define DT_PLTRELSZ 2
69 #define DT_PLTGOT 3
70 #define DT_HASH 4
71 #define DT_STRTAB 5
72 #define DT_SYMTAB 6
73 #define DT_RELA 7
74 #define DT_RELASZ 8
75 #define DT_RELAENT 9
76 #define DT_STRSZ 10
77 #define DT_SYMENT 11
78 #define DT_INIT 12
79 #define DT_FINI 13
80 #define DT_SONAME 14
81 #define DT_RPATH 15
82 #define DT_SYMBOLIC 16
83 #define DT_REL 17
84 #define DT_RELSZ 18
85 #define DT_RELENT 19
86 #define DT_PLTREL 20
87 #define DT_DEBUG 21
88 #define DT_TEXTREL 22
89 #define DT_JMPREL 23
90 #define DT_LOPROC 0x70000000
91 #define DT_HIPROC 0x7fffffff
92
93 /* Auxiliary table entries */
94 #define AT_NULL 0 /* end of vector */
95 #define AT_IGNORE 1 /* entry should be ignored */
96 #define AT_EXECFD 2 /* file descriptor of program */
97 #define AT_PHDR 3 /* program headers for program */
98 #define AT_PHENT 4 /* size of program header entry */
99 #define AT_PHNUM 5 /* number of program headers */
100 #define AT_PAGESZ 6 /* system page size */
101 #define AT_BASE 7 /* base address of interpreter */
102 #define AT_FLAGS 8 /* flags */
103 #define AT_ENTRY 9 /* entry point of program */
104 #define AT_NOTELF 10 /* program is not ELF */
105 #define AT_UID 11 /* real uid */
106 #define AT_EUID 12 /* effective uid */
107 #define AT_GID 13 /* real gid */
108 #define AT_EGID 14 /* effective gid */
109 #define AT_PLATFORM 15 /* string identifying CPU for optimizations */
110 #define AT_HWCAP 16 /* arch dependent hints at CPU capabilities */
111 #define AT_CLKTCK 17 /* frequency at which times() increments */
112 /* 18..22 = ? */
113 #define AT_SECURE 23 /* secure mode boolean */
114
115 /* Program header permission flags */
116 #define PF_X 0x1
117 #define PF_W 0x2
118 #define PF_R 0x4
119
120 /* Section header types */
121 #define SHT_NULL 0
122 #define SHT_PROGBITS 1
123 #define SHT_SYMTAB 2
124 #define SHT_STRTAB 3
125 #define SHT_RELA 4
126 #define SHT_HASH 5
127 #define SHT_DYNAMIC 6
128 #define SHT_NOTE 7
129 #define SHT_NOBITS 8
130 #define SHT_REL 9
131 #define SHT_SHLIB 10
132 #define SHT_DYNSYM 11
133 #define SHT_INIT_ARRAY 14
134 #define SHT_FINI_ARRAY 15
135 #define SHT_PREINIT_ARRAY 16
136 #define SHT_GROUP 17
137 #define SHT_SYMTAB_SHNDX 18
138 #define SHT_LOPROC 0x70000000
139 #define SHT_HIPROC 0x7fffffff
140 #define SHT_LOUSER 0x80000000
141 #define SHT_HIUSER 0xffffffff
142
143 /* Section header flags */
144 #define SHF_WRITE (1 << 0) /* Writable */
145 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
146 #define SHF_EXECINSTR (1 << 2) /* Executable */
147 #define SHF_MERGE (1 << 4) /* Might be merged */
148 #define SHF_STRINGS (1 << 5) /* Contains nul-terminated strings */
149 #define SHF_INFO_LINK (1 << 6) /* `sh_info' contains SHT index */
150 #define SHF_LINK_ORDER (1 << 7) /* Preserve order after combining */
151 #define SHF_OS_NONCONFORMING (1 << 8) /* Non-standard OS specific handling required */
152 #define SHF_GROUP (1 << 9) /* Section is member of a group */
153 #define SHF_TLS (1 << 10) /* Section hold thread-local data */
154
155 /* Special section numbers */
156 #define SHN_UNDEF 0x0000
157 #define SHN_LORESERVE 0xff00
158 #define SHN_LOPROC 0xff00
159 #define SHN_HIPROC 0xff1f
160 #define SHN_ABS 0xfff1
161 #define SHN_COMMON 0xfff2
162 #define SHN_XINDEX 0xffff
163 #define SHN_HIRESERVE 0xffff
164
165 /* Same, but signed/sign-extended */
166 #define XSHN_UNDEF ((int16_t)SHN_UNDEF)
167 #define XSHN_LORESERVE ((int16_t)SHN_LORESERVE)
168 #define XSHN_LOPROC ((int16_t)SHN_LOPROC)
169 #define XSHN_HIPROC ((int16_t)SHN_HIPROC)
170 #define XSHN_ABS ((int16_t)SHN_ABS)
171 #define XSHN_COMMON ((int16_t)SHN_COMMON)
172 #define XSHN_XINDEX ((int16_t)SHN_XINDEX)
173 #define XSHN_HIRESERVE ((int16_t)SHN_HIRESERVE)
174
175 /* Section align flag */
176 #define SHA_ANY 1 /* No alignment constraint */
177
178 /* Length of magic at the start of a file */
179 #define EI_NIDENT 16
180
181 /* Magic number constants... */
182 #define EI_MAG0 0 /* e_ident[] indexes */
183 #define EI_MAG1 1
184 #define EI_MAG2 2
185 #define EI_MAG3 3
186 #define EI_CLASS 4
187 #define EI_DATA 5
188 #define EI_VERSION 6
189 #define EI_OSABI 7
190 #define EI_ABIVERSION 8
191 #define EI_NINDENT 16
192
193 #define ELFMAG0 0x7f /* EI_MAG */
194 #define ELFMAG1 'E'
195 #define ELFMAG2 'L'
196 #define ELFMAG3 'F'
197 #define ELFMAG "\177ELF"
198 #define SELFMAG 4
199
200 #define ELFCLASSNONE 0 /* EI_CLASS */
201 #define ELFCLASS32 1
202 #define ELFCLASS64 2
203 #define ELFCLASSNUM 3
204
205 #define ELFDATANONE 0 /* e_ident[EI_DATA] */
206 #define ELFDATA2LSB 1
207 #define ELFDATA2MSB 2
208
209 #define EV_NONE 0 /* e_version, EI_VERSION */
210 #define EV_CURRENT 1
211 #define EV_NUM 2
212
213 #define ELFOSABI_NONE 0
214 #define ELFOSABI_LINUX 3
215
216 /* Legal values for ST_BIND subfield of st_info (symbol binding) */
217 #define STB_LOCAL 0 /* Local symbol */
218 #define STB_GLOBAL 1 /* Global symbol */
219 #define STB_WEAK 2 /* Weak symbol */
220 #define STB_NUM 3 /* Number of defined types */
221 #define STB_LOOS 10 /* Start of OS-specific */
222 #define STB_HIOS 12 /* End of OS-specific */
223 #define STB_LOPROC 13 /* Start of processor-specific */
224 #define STB_HIPROC 15 /* End of processor-specific */
225
226 /* Symbol types */
227 #define STT_NOTYPE 0 /* Symbol type is unspecified */
228 #define STT_OBJECT 1 /* Symbol is a data object */
229 #define STT_FUNC 2 /* Symbol is a code object */
230 #define STT_SECTION 3 /* Symbol associated with a section */
231 #define STT_FILE 4 /* Symbol's name is file name */
232 #define STT_COMMON 5 /* Symbol is a common data object */
233 #define STT_TLS 6 /* Symbol is thread-local data object */
234 #define STT_NUM 7 /* Number of defined types */
235
236 /* Symbol visibilities */
237 #define STV_DEFAULT 0 /* Default symbol visibility rules */
238 #define STV_INTERNAL 1 /* Processor specific hidden class */
239 #define STV_HIDDEN 2 /* Sym unavailable in other modules */
240 #define STV_PROTECTED 3 /* Not preemptible, not exported */
241
242 /* Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field */
243 #define ELF32_ST_BIND(i) ((i) >> 4)
244 #define ELF32_ST_MKBIND(i) ((i) << 4) /* just a helper */
245 #define ELF32_ST_TYPE(i) ((i) & 0xf)
246 #define ELF32_ST_INFO(b, i) (ELF32_ST_MKBIND(b) + ELF32_ST_TYPE(i))
247
248 #define ELF64_ST_BIND(i) ELF32_ST_BIND(i)
249 #define ELF64_ST_MKBIND(i) ELF32_ST_MKBIND(i)
250 #define ELF64_ST_TYPE(i) ELF32_ST_TYPE(i)
251 #define ELF64_ST_INFO(b, i) ELF32_ST_INFO(b, i)
252
253 /*
254 * ELF standard typedefs (yet more proof that <stdint.h> was way overdue)
255 */
256
257 typedef uint16_t Elf32_Half;
258 typedef int16_t Elf32_SHalf;
259 typedef uint32_t Elf32_Word;
260 typedef int32_t Elf32_Sword;
261 typedef uint64_t Elf32_Xword;
262 typedef int64_t Elf32_Sxword;
263
264 typedef uint32_t Elf32_Off;
265 typedef uint32_t Elf32_Addr;
266 typedef uint16_t Elf32_Section;
267
268 typedef uint16_t Elf64_Half;
269 typedef int16_t Elf64_SHalf;
270 typedef uint32_t Elf64_Word;
271 typedef int32_t Elf64_Sword;
272 typedef uint64_t Elf64_Xword;
273 typedef int64_t Elf64_Sxword;
274
275 typedef uint64_t Elf64_Off;
276 typedef uint64_t Elf64_Addr;
277 typedef uint16_t Elf64_Section;
278
279 /*
280 * Dynamic header
281 */
282
283 typedef struct elf32_dyn {
284 Elf32_Sword d_tag;
285 union {
286 Elf32_Sword d_val;
287 Elf32_Addr d_ptr;
288 } d_un;
289 } Elf32_Dyn;
290
291 typedef struct elf64_dyn {
292 Elf64_Sxword d_tag;
293 union {
294 Elf64_Xword d_val;
295 Elf64_Addr d_ptr;
296 } d_un;
297 } Elf64_Dyn;
298
299 /*
300 * Relocations
301 */
302
303 #define ELF32_R_SYM(x) ((x) >> 8)
304 #define ELF32_R_TYPE(x) ((x) & 0xff)
305 #define ELF32_R_INFO(s,t) (((Elf32_Word)(s) << 8) + ELF32_R_TYPE(t))
306
307 typedef struct elf32_rel {
308 Elf32_Addr r_offset;
309 Elf32_Word r_info;
310 } Elf32_Rel;
311
312 typedef struct elf32_rela {
313 Elf32_Addr r_offset;
314 Elf32_Word r_info;
315 Elf32_Sword r_addend;
316 } Elf32_Rela;
317
318 enum reloc32_type {
319 R_386_32 = 1, /* ordinary absolute relocation */
320 R_386_PC32 = 2, /* PC-relative relocation */
321 R_386_GOT32 = 3, /* an offset into GOT */
322 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
323 R_386_COPY = 5, /* ??? */
324 R_386_GLOB_DAT = 6, /* ??? */
325 R_386_JUMP_SLOT = 7, /* ??? */
326 R_386_RELATIVE = 8, /* ??? */
327 R_386_GOTOFF = 9, /* an offset from GOT base */
328 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
329 R_386_TLS_TPOFF = 14, /* Offset in static TLS block */
330 R_386_TLS_IE = 15, /* Address of GOT entry for static TLS block offset */
331 /* These are GNU extensions, but useful */
332 R_386_16 = 20, /* A 16-bit absolute relocation */
333 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
334 R_386_8 = 22, /* An 8-bit absolute relocation */
335 R_386_PC8 = 23, /* An 8-bit PC-relative relocation */
336 R_386_SEG16 = 45, /* A 16-bit real-mode segment */
337 R_386_SUB16 = 46, /* Subtract 16-bit value */
338 R_386_SUB32 = 47 /* Subtract 32-bit value */
339 };
340
341 #define ELF64_R_SYM(x) ((x) >> 32)
342 #define ELF64_R_TYPE(x) ((x) & 0xffffffff)
343 #define ELF64_R_INFO(s,t) (((Elf64_Xword)(s) << 32) + ELF64_R_TYPE(t))
344
345 typedef struct elf64_rel {
346 Elf64_Addr r_offset;
347 Elf64_Xword r_info;
348 } Elf64_Rel;
349
350 typedef struct elf64_rela {
351 Elf64_Addr r_offset;
352 Elf64_Xword r_info;
353 Elf64_Sxword r_addend;
354 } Elf64_Rela;
355
356 enum reloc64_type {
357 R_X86_64_NONE = 0, /* No reloc */
358 R_X86_64_64 = 1, /* Direct 64 bit */
359 R_X86_64_PC32 = 2, /* PC relative 32 bit signed */
360 R_X86_64_GOT32 = 3, /* 32 bit GOT entry */
361 R_X86_64_PLT32 = 4, /* 32 bit PLT address */
362 R_X86_64_COPY = 5, /* Copy symbol at runtime */
363 R_X86_64_GLOB_DAT = 6, /* Create GOT entry */
364 R_X86_64_JUMP_SLOT = 7, /* Create PLT entry */
365 R_X86_64_RELATIVE = 8, /* Adjust by program base */
366 R_X86_64_GOTPCREL = 9, /* 32 bit signed PC relative offset to GOT */
367 R_X86_64_32 = 10, /* Direct 32 bit zero extended */
368 R_X86_64_32S = 11, /* Direct 32 bit sign extended */
369 R_X86_64_16 = 12, /* Direct 16 bit zero extended */
370 R_X86_64_PC16 = 13, /* 16 bit sign extended pc relative */
371 R_X86_64_8 = 14, /* Direct 8 bit sign extended */
372 R_X86_64_PC8 = 15, /* 8 bit sign extended pc relative */
373 R_X86_64_DTPMOD64 = 16, /* ID of module containing symbol */
374 R_X86_64_DTPOFF64 = 17, /* Offset in module's TLS block */
375 R_X86_64_TPOFF64 = 18, /* Offset in initial TLS block */
376 R_X86_64_TLSGD = 19, /* 32 bit signed PC relative offset to two GOT entries for GD symbol */
377 R_X86_64_TLSLD = 20, /* 32 bit signed PC relative offset to two GOT entries for LD symbol */
378 R_X86_64_DTPOFF32 = 21, /* Offset in TLS block */
379 R_X86_64_GOTTPOFF = 22, /* 32 bit signed PC relative offset to GOT entry for IE symbol */
380 R_X86_64_TPOFF32 = 23, /* Offset in initial TLS block */
381 R_X86_64_PC64 = 24, /* word64 S + A - P */
382 R_X86_64_GOTOFF64 = 25, /* word64 S + A - GOT */
383 R_X86_64_GOTPC32 = 26, /* word32 GOT + A - P */
384 R_X86_64_GOT64 = 27, /* word64 G + A */
385 R_X86_64_GOTPCREL64 = 28, /* word64 G + GOT - P + A */
386 R_X86_64_GOTPC64 = 29, /* word64 GOT - P + A */
387 R_X86_64_GOTPLT64 = 30, /* word64 G + A */
388 R_X86_64_PLTOFF64 = 31, /* word64 L - GOT + A */
389 R_X86_64_SIZE32 = 32, /* word32 Z + A */
390 R_X86_64_SIZE64 = 33, /* word64 Z + A */
391 R_X86_64_GOTPC32_TLSDESC= 34, /* word32 */
392 R_X86_64_TLSDESC_CALL = 35, /* none */
393 R_X86_64_TLSDESC = 36 /* word64?2 */
394 };
395
396 /*
397 * Symbol
398 */
399
400 typedef struct elf32_sym {
401 Elf32_Word st_name;
402 Elf32_Addr st_value;
403 Elf32_Word st_size;
404 unsigned char st_info;
405 unsigned char st_other;
406 Elf32_Half st_shndx;
407 } Elf32_Sym;
408
409 typedef struct elf64_sym {
410 Elf64_Word st_name;
411 unsigned char st_info;
412 unsigned char st_other;
413 Elf64_Half st_shndx;
414 Elf64_Addr st_value;
415 Elf64_Xword st_size;
416 } Elf64_Sym;
417
418 /*
419 * Main file header
420 */
421
422 typedef struct elf32_hdr {
423 unsigned char e_ident[EI_NIDENT];
424 Elf32_Half e_type;
425 Elf32_Half e_machine;
426 Elf32_Word e_version;
427 Elf32_Addr e_entry;
428 Elf32_Off e_phoff;
429 Elf32_Off e_shoff;
430 Elf32_Word e_flags;
431 Elf32_Half e_ehsize;
432 Elf32_Half e_phentsize;
433 Elf32_Half e_phnum;
434 Elf32_Half e_shentsize;
435 Elf32_Half e_shnum;
436 Elf32_Half e_shstrndx;
437 } Elf32_Ehdr;
438
439 typedef struct elf64_hdr {
440 unsigned char e_ident[EI_NIDENT];
441 Elf64_Half e_type;
442 Elf64_Half e_machine;
443 Elf64_Word e_version;
444 Elf64_Addr e_entry;
445 Elf64_Off e_phoff;
446 Elf64_Off e_shoff;
447 Elf64_Word e_flags;
448 Elf64_Half e_ehsize;
449 Elf64_Half e_phentsize;
450 Elf64_Half e_phnum;
451 Elf64_Half e_shentsize;
452 Elf64_Half e_shnum;
453 Elf64_Half e_shstrndx;
454 } Elf64_Ehdr;
455
456 /*
457 * Program header
458 */
459
460 typedef struct elf32_phdr {
461 Elf32_Word p_type;
462 Elf32_Off p_offset;
463 Elf32_Addr p_vaddr;
464 Elf32_Addr p_paddr;
465 Elf32_Word p_filesz;
466 Elf32_Word p_memsz;
467 Elf32_Word p_flags;
468 Elf32_Word p_align;
469 } Elf32_Phdr;
470
471 typedef struct elf64_phdr {
472 Elf64_Word p_type;
473 Elf64_Word p_flags;
474 Elf64_Off p_offset;
475 Elf64_Addr p_vaddr;
476 Elf64_Addr p_paddr;
477 Elf64_Xword p_filesz;
478 Elf64_Xword p_memsz;
479 Elf64_Xword p_align;
480 } Elf64_Phdr;
481
482 /*
483 * Section header
484 */
485
486 typedef struct elf32_shdr {
487 Elf32_Word sh_name;
488 Elf32_Word sh_type;
489 Elf32_Word sh_flags;
490 Elf32_Addr sh_addr;
491 Elf32_Off sh_offset;
492 Elf32_Word sh_size;
493 Elf32_Word sh_link;
494 Elf32_Word sh_info;
495 Elf32_Word sh_addralign;
496 Elf32_Word sh_entsize;
497 } Elf32_Shdr;
498
499 typedef struct elf64_shdr {
500 Elf64_Word sh_name;
501 Elf64_Word sh_type;
502 Elf64_Xword sh_flags;
503 Elf64_Addr sh_addr;
504 Elf64_Off sh_offset;
505 Elf64_Xword sh_size;
506 Elf64_Word sh_link;
507 Elf64_Word sh_info;
508 Elf64_Xword sh_addralign;
509 Elf64_Xword sh_entsize;
510 } Elf64_Shdr;
511
512 /*
513 * Note header
514 */
515 typedef struct elf32_note {
516 Elf32_Word n_namesz; /* Name size */
517 Elf32_Word n_descsz; /* Content size */
518 Elf32_Word n_type; /* Content type */
519 } Elf32_Nhdr;
520
521 typedef struct elf64_note {
522 Elf64_Word n_namesz; /* Name size */
523 Elf64_Word n_descsz; /* Content size */
524 Elf64_Word n_type; /* Content type */
525 } Elf64_Nhdr;
526
527 #endif /* OUTPUT_ELF_H */