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