Remove more duplicated code.
[oota-llvm.git] / include / llvm / Support / ELF.h
1 //===-- llvm/Support/ELF.h - ELF constants and data structures --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header contains common, non-processor-specific data structures and
11 // constants for the ELF file format.
12 //
13 // The details of the ELF32 bits in this file are largely based on the Tool
14 // Interface Standard (TIS) Executable and Linking Format (ELF) Specification
15 // Version 1.2, May 1995. The ELF64 stuff is based on ELF-64 Object File Format
16 // Version 1.5, Draft 2, May 1998 as well as OpenBSD header files.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_SUPPORT_ELF_H
21 #define LLVM_SUPPORT_ELF_H
22
23 #include "llvm/Support/DataTypes.h"
24 #include <cstring>
25
26 namespace llvm {
27
28 namespace ELF {
29
30 typedef uint32_t Elf32_Addr; // Program address
31 typedef uint16_t Elf32_Half;
32 typedef uint32_t Elf32_Off;  // File offset
33 typedef int32_t  Elf32_Sword;
34 typedef uint32_t Elf32_Word;
35
36 typedef uint64_t Elf64_Addr;
37 typedef uint64_t Elf64_Off;
38 typedef int32_t  Elf64_Shalf;
39 typedef int32_t  Elf64_Sword;
40 typedef uint32_t Elf64_Word;
41 typedef int64_t  Elf64_Sxword;
42 typedef uint64_t Elf64_Xword;
43 typedef uint32_t Elf64_Half;
44 typedef uint16_t Elf64_Quarter;
45
46 // Object file magic string.
47 static const char ElfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' };
48
49 // e_ident size and indices.
50 enum {
51   EI_MAG0       = 0,          // File identification index.
52   EI_MAG1       = 1,          // File identification index.
53   EI_MAG2       = 2,          // File identification index.
54   EI_MAG3       = 3,          // File identification index.
55   EI_CLASS      = 4,          // File class.
56   EI_DATA       = 5,          // Data encoding.
57   EI_VERSION    = 6,          // File version.
58   EI_OSABI      = 7,          // OS/ABI identification.
59   EI_ABIVERSION = 8,          // ABI version.
60   EI_PAD        = 9,          // Start of padding bytes.
61   EI_NIDENT     = 16          // Number of bytes in e_ident.
62 };
63
64 struct Elf32_Ehdr {
65   unsigned char e_ident[EI_NIDENT]; // ELF Identification bytes
66   Elf32_Half    e_type;      // Type of file (see ET_* below)
67   Elf32_Half    e_machine;   // Required architecture for this file (see EM_*)
68   Elf32_Word    e_version;   // Must be equal to 1
69   Elf32_Addr    e_entry;     // Address to jump to in order to start program
70   Elf32_Off     e_phoff;     // Program header table's file offset, in bytes
71   Elf32_Off     e_shoff;     // Section header table's file offset, in bytes
72   Elf32_Word    e_flags;     // Processor-specific flags
73   Elf32_Half    e_ehsize;    // Size of ELF header, in bytes
74   Elf32_Half    e_phentsize; // Size of an entry in the program header table
75   Elf32_Half    e_phnum;     // Number of entries in the program header table
76   Elf32_Half    e_shentsize; // Size of an entry in the section header table
77   Elf32_Half    e_shnum;     // Number of entries in the section header table
78   Elf32_Half    e_shstrndx;  // Sect hdr table index of sect name string table
79   bool checkMagic() const {
80     return (memcmp(e_ident, ElfMagic, strlen(ElfMagic))) == 0;
81   }
82   unsigned char getFileClass() const { return e_ident[EI_CLASS]; }
83   unsigned char getDataEncoding() const { return e_ident[EI_DATA]; }
84 };
85
86 // 64-bit ELF header. Fields are the same as for ELF32, but with different
87 // types (see above).
88 struct Elf64_Ehdr {
89   unsigned char e_ident[EI_NIDENT];
90   Elf64_Quarter e_type;
91   Elf64_Quarter e_machine;
92   Elf64_Half    e_version;
93   Elf64_Addr    e_entry;
94   Elf64_Off     e_phoff;
95   Elf64_Off     e_shoff;
96   Elf64_Half    e_flags;
97   Elf64_Quarter e_ehsize;
98   Elf64_Quarter e_phentsize;
99   Elf64_Quarter e_phnum;
100   Elf64_Quarter e_shentsize;
101   Elf64_Quarter e_shnum;
102   Elf64_Quarter e_shstrndx;
103   bool checkMagic() const {
104     return (memcmp(e_ident, ElfMagic, strlen(ElfMagic))) == 0;
105   }
106   unsigned char getFileClass() const { return e_ident[EI_CLASS]; }
107   unsigned char getDataEncoding() const { return e_ident[EI_DATA]; }
108 };
109
110 // File types
111 enum {
112   ET_NONE   = 0,      // No file type
113   ET_REL    = 1,      // Relocatable file
114   ET_EXEC   = 2,      // Executable file
115   ET_DYN    = 3,      // Shared object file
116   ET_CORE   = 4,      // Core file
117   ET_LOPROC = 0xff00, // Beginning of processor-specific codes
118   ET_HIPROC = 0xffff  // Processor-specific
119 };
120
121 // Versioning
122 enum {
123   EV_NONE = 0,
124   EV_CURRENT = 1
125 };
126
127 // Machine architectures
128 enum {
129   EM_NONE = 0,      // No machine
130   EM_M32 = 1,       // AT&T WE 32100
131   EM_SPARC = 2,     // SPARC
132   EM_386 = 3,       // Intel 386
133   EM_68K = 4,       // Motorola 68000
134   EM_88K = 5,       // Motorola 88000
135   EM_486 = 6,       // Intel 486 (deprecated)
136   EM_860 = 7,       // Intel 80860
137   EM_MIPS = 8,      // MIPS R3000
138   EM_PPC = 20,      // PowerPC
139   EM_PPC64 = 21,    // PowerPC64
140   EM_ARM = 40,      // ARM
141   EM_ALPHA = 41,    // DEC Alpha
142   EM_SPARCV9 = 43,  // SPARC V9
143   EM_X86_64 = 62,   // AMD64
144   EM_MBLAZE = 47787 // Xilinx MicroBlaze
145 };
146
147 // Object file classes.
148 enum {
149   ELFCLASSNONE = 0,
150   ELFCLASS32 = 1, // 32-bit object file
151   ELFCLASS64 = 2  // 64-bit object file
152 };
153
154 // Object file byte orderings.
155 enum {
156   ELFDATANONE = 0, // Invalid data encoding.
157   ELFDATA2LSB = 1, // Little-endian object file
158   ELFDATA2MSB = 2  // Big-endian object file
159 };
160
161 // OS ABI identification.
162 enum {
163   ELFOSABI_NONE = 0,          // UNIX System V ABI
164   ELFOSABI_HPUX = 1,          // HP-UX operating system
165   ELFOSABI_NETBSD = 2,        // NetBSD
166   ELFOSABI_LINUX = 3,         // GNU/Linux
167   ELFOSABI_HURD = 4,          // GNU/Hurd
168   ELFOSABI_SOLARIS = 6,       // Solaris
169   ELFOSABI_AIX = 7,           // AIX
170   ELFOSABI_IRIX = 8,          // IRIX
171   ELFOSABI_FREEBSD = 9,       // FreeBSD
172   ELFOSABI_TRU64 = 10,        // TRU64 UNIX
173   ELFOSABI_MODESTO = 11,      // Novell Modesto
174   ELFOSABI_OPENBSD = 12,      // OpenBSD
175   ELFOSABI_OPENVMS = 13,      // OpenVMS
176   ELFOSABI_NSK = 14,          // Hewlett-Packard Non-Stop Kernel
177   ELFOSABI_AROS = 15,         // AROS
178   ELFOSABI_FENIXOS = 16,      // FenixOS
179   ELFOSABI_C6000_ELFABI = 64, // Bare-metal TMS320C6000
180   ELFOSABI_C6000_LINUX = 65,  // Linux TMS320C6000
181   ELFOSABI_ARM = 97,          // ARM
182   ELFOSABI_STANDALONE = 255   // Standalone (embedded) application
183 };
184
185 // X86_64 relocations.
186 enum {
187   R_X86_64_NONE       = 0,
188   R_X86_64_64         = 1,
189   R_X86_64_PC32       = 2,
190   R_X86_64_GOT32      = 3,
191   R_X86_64_PLT32      = 4,
192   R_X86_64_COPY       = 5,
193   R_X86_64_GLOB_DAT   = 6,
194   R_X86_64_JUMP_SLOT  = 7,
195   R_X86_64_RELATIVE   = 8,
196   R_X86_64_GOTPCREL   = 9,
197   R_X86_64_32         = 10,
198   R_X86_64_32S        = 11,
199   R_X86_64_16         = 12,
200   R_X86_64_PC16       = 13,
201   R_X86_64_8          = 14,
202   R_X86_64_PC8        = 15,
203   R_X86_64_DTPMOD64   = 16,
204   R_X86_64_DTPOFF64   = 17,
205   R_X86_64_TPOFF64    = 18,
206   R_X86_64_TLSGD      = 19,
207   R_X86_64_TLSLD      = 20,
208   R_X86_64_DTPOFF32   = 21,
209   R_X86_64_GOTTPOFF   = 22,
210   R_X86_64_TPOFF32    = 23,
211   R_X86_64_PC64       = 24,
212   R_X86_64_GOTOFF64   = 25,
213   R_X86_64_GOTPC32    = 26,
214   R_X86_64_SIZE32     = 32,
215   R_X86_64_SIZE64     = 33,
216   R_X86_64_GOTPC32_TLSDESC = 34,
217   R_X86_64_TLSDESC_CALL    = 35,
218   R_X86_64_TLSDESC    = 36
219 };
220
221 // i386 relocations.
222 // TODO: this is just a subset
223 enum {
224   R_386_NONE          = 0,
225   R_386_32            = 1,
226   R_386_PC32          = 2,
227   R_386_GOT32         = 3,
228   R_386_PLT32         = 4,
229   R_386_COPY          = 5,
230   R_386_GLOB_DAT      = 6,
231   R_386_JUMP_SLOT     = 7,
232   R_386_RELATIVE      = 8,
233   R_386_GOTOFF        = 9,
234   R_386_GOTPC         = 10,
235   R_386_32PLT         = 11,
236   R_386_TLS_TPOFF     = 14,
237   R_386_TLS_IE        = 15,
238   R_386_TLS_GOTIE     = 16,
239   R_386_TLS_LE        = 17,
240   R_386_TLS_GD        = 18,
241   R_386_TLS_LDM       = 19,
242   R_386_16            = 20,
243   R_386_PC16          = 21,
244   R_386_8             = 22,
245   R_386_PC8           = 23,
246   R_386_TLS_GD_32     = 24,
247   R_386_TLS_GD_PUSH   = 25,
248   R_386_TLS_GD_CALL   = 26,
249   R_386_TLS_GD_POP    = 27,
250   R_386_TLS_LDM_32    = 28,
251   R_386_TLS_LDM_PUSH  = 29,
252   R_386_TLS_LDM_CALL  = 30,
253   R_386_TLS_LDM_POP   = 31,
254   R_386_TLS_LDO_32    = 32,
255   R_386_TLS_IE_32     = 33,
256   R_386_TLS_LE_32     = 34,
257   R_386_TLS_DTPMOD32  = 35,
258   R_386_TLS_DTPOFF32  = 36,
259   R_386_TLS_TPOFF32   = 37,
260   R_386_TLS_GOTDESC   = 39,
261   R_386_TLS_DESC_CALL = 40,
262   R_386_TLS_DESC      = 41,
263   R_386_IRELATIVE     = 42,
264   R_386_NUM           = 43
265 };
266
267 // MBlaze relocations.
268 enum {
269   R_MICROBLAZE_NONE           = 0,
270   R_MICROBLAZE_32             = 1,
271   R_MICROBLAZE_32_PCREL       = 2,
272   R_MICROBLAZE_64_PCREL       = 3,
273   R_MICROBLAZE_32_PCREL_LO    = 4,
274   R_MICROBLAZE_64             = 5,
275   R_MICROBLAZE_32_LO          = 6,
276   R_MICROBLAZE_SRO32          = 7,
277   R_MICROBLAZE_SRW32          = 8,
278   R_MICROBLAZE_64_NONE        = 9,
279   R_MICROBLAZE_32_SYM_OP_SYM  = 10,
280   R_MICROBLAZE_GNU_VTINHERIT  = 11,
281   R_MICROBLAZE_GNU_VTENTRY    = 12,
282   R_MICROBLAZE_GOTPC_64       = 13,
283   R_MICROBLAZE_GOT_64         = 14,
284   R_MICROBLAZE_PLT_64         = 15,
285   R_MICROBLAZE_REL            = 16,
286   R_MICROBLAZE_JUMP_SLOT      = 17,
287   R_MICROBLAZE_GLOB_DAT       = 18,
288   R_MICROBLAZE_GOTOFF_64      = 19,
289   R_MICROBLAZE_GOTOFF_32      = 20,
290   R_MICROBLAZE_COPY           = 21
291 };
292
293 // ELF Relocation types for ARM
294 // Meets 2.08 ABI Specs.
295
296 enum {
297   R_ARM_NONE                  = 0x00,
298   R_ARM_PC24                  = 0x01,
299   R_ARM_ABS32                 = 0x02,
300   R_ARM_REL32                 = 0x03,
301   R_ARM_LDR_PC_G0             = 0x04,
302   R_ARM_ABS16                 = 0x05,
303   R_ARM_ABS12                 = 0x06,
304   R_ARM_THM_ABS5              = 0x07,
305   R_ARM_ABS8                  = 0x08,
306   R_ARM_SBREL32               = 0x09,
307   R_ARM_THM_CALL              = 0x0a,
308   R_ARM_THM_PC8               = 0x0b,
309   R_ARM_BREL_ADJ              = 0x0c,
310   R_ARM_TLS_DESC              = 0x0d,
311   R_ARM_THM_SWI8              = 0x0e,
312   R_ARM_XPC25                 = 0x0f,
313   R_ARM_THM_XPC22             = 0x10,
314   R_ARM_TLS_DTPMOD32          = 0x11,
315   R_ARM_TLS_DTPOFF32          = 0x12,
316   R_ARM_TLS_TPOFF32           = 0x13,
317   R_ARM_COPY                  = 0x14,
318   R_ARM_GLOB_DAT              = 0x15,
319   R_ARM_JUMP_SLOT             = 0x16,
320   R_ARM_RELATIVE              = 0x17,
321   R_ARM_GOTOFF32              = 0x18,
322   R_ARM_BASE_PREL             = 0x19,
323   R_ARM_GOT_BREL              = 0x1a,
324   R_ARM_PLT32                 = 0x1b,
325   R_ARM_CALL                  = 0x1c,
326   R_ARM_JUMP24                = 0x1d,
327   R_ARM_THM_JUMP24            = 0x1e,
328   R_ARM_BASE_ABS              = 0x1f,
329   R_ARM_ALU_PCREL_7_0         = 0x20,
330   R_ARM_ALU_PCREL_15_8        = 0x21,
331   R_ARM_ALU_PCREL_23_15       = 0x22,
332   R_ARM_LDR_SBREL_11_0_NC     = 0x23,
333   R_ARM_ALU_SBREL_19_12_NC    = 0x24,
334   R_ARM_ALU_SBREL_27_20_CK    = 0x25,
335   R_ARM_TARGET1               = 0x26,
336   R_ARM_SBREL31               = 0x27,
337   R_ARM_V4BX                  = 0x28,
338   R_ARM_TARGET2               = 0x29,
339   R_ARM_PREL31                = 0x2a,
340   R_ARM_MOVW_ABS_NC           = 0x2b,
341   R_ARM_MOVT_ABS              = 0x2c,
342   R_ARM_MOVW_PREL_NC          = 0x2d,
343   R_ARM_MOVT_PREL             = 0x2e,
344   R_ARM_THM_MOVW_ABS_NC       = 0x2f,
345   R_ARM_THM_MOVT_ABS          = 0x30,
346   R_ARM_THM_MOVW_PREL_NC      = 0x31,
347   R_ARM_THM_MOVT_PREL         = 0x32,
348   R_ARM_THM_JUMP19            = 0x33,
349   R_ARM_THM_JUMP6             = 0x34,
350   R_ARM_THM_ALU_PREL_11_0     = 0x35,
351   R_ARM_THM_PC12              = 0x36,
352   R_ARM_ABS32_NOI             = 0x37,
353   R_ARM_REL32_NOI             = 0x38,
354   R_ARM_ALU_PC_G0_NC          = 0x39,
355   R_ARM_ALU_PC_G0             = 0x3a,
356   R_ARM_ALU_PC_G1_NC          = 0x3b,
357   R_ARM_ALU_PC_G1             = 0x3c,
358   R_ARM_ALU_PC_G2             = 0x3d,
359   R_ARM_LDR_PC_G1             = 0x3e,
360   R_ARM_LDR_PC_G2             = 0x3f,
361   R_ARM_LDRS_PC_G0            = 0x40,
362   R_ARM_LDRS_PC_G1            = 0x41,
363   R_ARM_LDRS_PC_G2            = 0x42,
364   R_ARM_LDC_PC_G0             = 0x43,
365   R_ARM_LDC_PC_G1             = 0x44,
366   R_ARM_LDC_PC_G2             = 0x45,
367   R_ARM_ALU_SB_G0_NC          = 0x46,
368   R_ARM_ALU_SB_G0             = 0x47,
369   R_ARM_ALU_SB_G1_NC          = 0x48,
370   R_ARM_ALU_SB_G1             = 0x49,
371   R_ARM_ALU_SB_G2             = 0x4a,
372   R_ARM_LDR_SB_G0             = 0x4b,
373   R_ARM_LDR_SB_G1             = 0x4c,
374   R_ARM_LDR_SB_G2             = 0x4d,
375   R_ARM_LDRS_SB_G0            = 0x4e,
376   R_ARM_LDRS_SB_G1            = 0x4f,
377   R_ARM_LDRS_SB_G2            = 0x50,
378   R_ARM_LDC_SB_G0             = 0x51,
379   R_ARM_LDC_SB_G1             = 0x52,
380   R_ARM_LDC_SB_G2             = 0x53,
381   R_ARM_MOVW_BREL_NC          = 0x54,
382   R_ARM_MOVT_BREL             = 0x55,
383   R_ARM_MOVW_BREL             = 0x56,
384   R_ARM_THM_MOVW_BREL_NC      = 0x57,
385   R_ARM_THM_MOVT_BREL         = 0x58,
386   R_ARM_THM_MOVW_BREL         = 0x59,
387   R_ARM_TLS_GOTDESC           = 0x5a,
388   R_ARM_TLS_CALL              = 0x5b,
389   R_ARM_TLS_DESCSEQ           = 0x5c,
390   R_ARM_THM_TLS_CALL          = 0x5d,
391   R_ARM_PLT32_ABS             = 0x5e,
392   R_ARM_GOT_ABS               = 0x5f,
393   R_ARM_GOT_PREL              = 0x60,
394   R_ARM_GOT_BREL12            = 0x61,
395   R_ARM_GOTOFF12              = 0x62,
396   R_ARM_GOTRELAX              = 0x63,
397   R_ARM_GNU_VTENTRY           = 0x64,
398   R_ARM_GNU_VTINHERIT         = 0x65,
399   R_ARM_THM_JUMP11            = 0x66,
400   R_ARM_THM_JUMP8             = 0x67,
401   R_ARM_TLS_GD32              = 0x68,
402   R_ARM_TLS_LDM32             = 0x69,
403   R_ARM_TLS_LDO32             = 0x6a,
404   R_ARM_TLS_IE32              = 0x6b,
405   R_ARM_TLS_LE32              = 0x6c,
406   R_ARM_TLS_LDO12             = 0x6d,
407   R_ARM_TLS_LE12              = 0x6e,
408   R_ARM_TLS_IE12GP            = 0x6f,
409   R_ARM_PRIVATE_0             = 0x70,
410   R_ARM_PRIVATE_1             = 0x71,
411   R_ARM_PRIVATE_2             = 0x72,
412   R_ARM_PRIVATE_3             = 0x73,
413   R_ARM_PRIVATE_4             = 0x74,
414   R_ARM_PRIVATE_5             = 0x75,
415   R_ARM_PRIVATE_6             = 0x76,
416   R_ARM_PRIVATE_7             = 0x77,
417   R_ARM_PRIVATE_8             = 0x78,
418   R_ARM_PRIVATE_9             = 0x79,
419   R_ARM_PRIVATE_10            = 0x7a,
420   R_ARM_PRIVATE_11            = 0x7b,
421   R_ARM_PRIVATE_12            = 0x7c,
422   R_ARM_PRIVATE_13            = 0x7d,
423   R_ARM_PRIVATE_14            = 0x7e,
424   R_ARM_PRIVATE_15            = 0x7f,
425   R_ARM_ME_TOO                = 0x80,
426   R_ARM_THM_TLS_DESCSEQ16     = 0x81,
427   R_ARM_THM_TLS_DESCSEQ32     = 0x82
428 };
429
430
431
432 // Section header.
433 struct Elf32_Shdr {
434   Elf32_Word sh_name;      // Section name (index into string table)
435   Elf32_Word sh_type;      // Section type (SHT_*)
436   Elf32_Word sh_flags;     // Section flags (SHF_*)
437   Elf32_Addr sh_addr;      // Address where section is to be loaded
438   Elf32_Off  sh_offset;    // File offset of section data, in bytes
439   Elf32_Word sh_size;      // Size of section, in bytes
440   Elf32_Word sh_link;      // Section type-specific header table index link
441   Elf32_Word sh_info;      // Section type-specific extra information
442   Elf32_Word sh_addralign; // Section address alignment
443   Elf32_Word sh_entsize;   // Size of records contained within the section
444 };
445
446 // Section header for ELF64 - same fields as ELF32, different types.
447 struct Elf64_Shdr {
448   Elf64_Half  sh_name;
449   Elf64_Half  sh_type;
450   Elf64_Xword sh_flags;
451   Elf64_Addr  sh_addr;
452   Elf64_Off   sh_offset;
453   Elf64_Xword sh_size;
454   Elf64_Half  sh_link;
455   Elf64_Half  sh_info;
456   Elf64_Xword sh_addralign;
457   Elf64_Xword sh_entsize;
458 };
459
460 // Special section indices.
461 enum {
462   SHN_UNDEF     = 0,      // Undefined, missing, irrelevant, or meaningless
463   SHN_LORESERVE = 0xff00, // Lowest reserved index
464   SHN_LOPROC    = 0xff00, // Lowest processor-specific index
465   SHN_HIPROC    = 0xff1f, // Highest processor-specific index
466   SHN_ABS       = 0xfff1, // Symbol has absolute value; does not need relocation
467   SHN_COMMON    = 0xfff2, // FORTRAN COMMON or C external global variables
468   SHN_XINDEX    = 0xffff, // Mark that the index is >= SHN_LORESERVE
469   SHN_HIRESERVE = 0xffff  // Highest reserved index
470 };
471
472 // Section types.
473 enum {
474   SHT_NULL          = 0,  // No associated section (inactive entry).
475   SHT_PROGBITS      = 1,  // Program-defined contents.
476   SHT_SYMTAB        = 2,  // Symbol table.
477   SHT_STRTAB        = 3,  // String table.
478   SHT_RELA          = 4,  // Relocation entries; explicit addends.
479   SHT_HASH          = 5,  // Symbol hash table.
480   SHT_DYNAMIC       = 6,  // Information for dynamic linking.
481   SHT_NOTE          = 7,  // Information about the file.
482   SHT_NOBITS        = 8,  // Data occupies no space in the file.
483   SHT_REL           = 9,  // Relocation entries; no explicit addends.
484   SHT_SHLIB         = 10, // Reserved.
485   SHT_DYNSYM        = 11, // Symbol table.
486   SHT_INIT_ARRAY    = 14, // Pointers to initialisation functions.
487   SHT_FINI_ARRAY    = 15, // Pointers to termination functions.
488   SHT_PREINIT_ARRAY = 16, // Pointers to pre-init functions.
489   SHT_GROUP         = 17, // Section group.
490   SHT_SYMTAB_SHNDX  = 18, // Indicies for SHN_XINDEX entries.
491   SHT_LOOS          = 0x60000000, // Lowest operating system-specific type.
492   SHT_HIOS          = 0x6fffffff, // Highest operating system-specific type.
493   SHT_LOPROC        = 0x70000000, // Lowest processor architecture-specific type.
494   // Fixme: All this is duplicated in MCSectionELF. Why??
495   // Exception Index table
496   SHT_ARM_EXIDX           = 0x70000001U,
497   // BPABI DLL dynamic linking pre-emption map
498   SHT_ARM_PREEMPTMAP      = 0x70000002U,
499   //  Object file compatibility attributes
500   SHT_ARM_ATTRIBUTES      = 0x70000003U,
501   SHT_ARM_DEBUGOVERLAY    = 0x70000004U,
502   SHT_ARM_OVERLAYSECTION  = 0x70000005U,
503
504   SHT_HIPROC        = 0x7fffffff, // Highest processor architecture-specific type.
505   SHT_LOUSER        = 0x80000000, // Lowest type reserved for applications.
506   SHT_HIUSER        = 0xffffffff  // Highest type reserved for applications.
507 };
508
509 // Section flags.
510 enum {
511   // Section data should be writable during execution.
512   SHF_WRITE = 0x1,
513
514   // Section occupies memory during program execution.
515   SHF_ALLOC = 0x2,
516
517   // Section contains executable machine instructions.
518   SHF_EXECINSTR = 0x4,
519
520   // The data in this section may be merged.
521   SHF_MERGE = 0x10,
522
523   // The data in this section is null-terminated strings.
524   SHF_STRINGS = 0x20,
525
526   // A field in this section holds a section header table index.
527   SHF_INFO_LINK = 0x40U,
528
529   // Adds special ordering requirements for link editors.
530   SHF_LINK_ORDER = 0x80U,
531
532   // This section requires special OS-specific processing to avoid incorrect
533   // behavior.
534   SHF_OS_NONCONFORMING = 0x100U,
535
536   // This section is a member of a section group.
537   SHF_GROUP = 0x200U,
538
539   // This section holds Thread-Local Storage.
540   SHF_TLS = 0x400U,
541
542   // Start of target-specific flags.
543
544   /// XCORE_SHF_CP_SECTION - All sections with the "c" flag are grouped
545   /// together by the linker to form the constant pool and the cp register is
546   /// set to the start of the constant pool by the boot code.
547   XCORE_SHF_CP_SECTION = 0x800U,
548
549   /// XCORE_SHF_DP_SECTION - All sections with the "d" flag are grouped
550   /// together by the linker to form the data section and the dp register is
551   /// set to the start of the section by the boot code.
552   XCORE_SHF_DP_SECTION = 0x1000U,
553
554   // Bits indicating processor-specific flags.
555   SHF_MASKPROC = 0xf0000000
556 };
557
558 // Section Group Flags
559 enum {
560   GRP_COMDAT = 0x1,
561   GRP_MASKOS = 0x0ff00000,
562   GRP_MASKPROC = 0xf0000000
563 };
564
565 // Symbol table entries for ELF32.
566 struct Elf32_Sym {
567   Elf32_Word    st_name;  // Symbol name (index into string table)
568   Elf32_Addr    st_value; // Value or address associated with the symbol
569   Elf32_Word    st_size;  // Size of the symbol
570   unsigned char st_info;  // Symbol's type and binding attributes
571   unsigned char st_other; // Must be zero; reserved
572   Elf32_Half    st_shndx; // Which section (header table index) it's defined in
573
574   // These accessors and mutators correspond to the ELF32_ST_BIND,
575   // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
576   unsigned char getBinding() const { return st_info >> 4; }
577   unsigned char getType() const { return st_info & 0x0f; }
578   void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
579   void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
580   void setBindingAndType(unsigned char b, unsigned char t) {
581     st_info = (b << 4) + (t & 0x0f);
582   }
583 };
584
585 // Symbol table entries for ELF64.
586 struct Elf64_Sym {
587   Elf64_Word      st_name;  // Symbol name (index into string table)
588   unsigned char   st_info;  // Symbol's type and binding attributes
589   unsigned char   st_other; // Must be zero; reserved
590   Elf64_Half      st_shndx; // Which section (header table index) it's defined in
591   Elf64_Addr      st_value; // Value or address associated with the symbol
592   Elf64_Xword     st_size;  // Size of the symbol
593
594   // These accessors and mutators are identical to those defined for ELF32
595   // symbol table entries.
596   unsigned char getBinding() const { return st_info >> 4; }
597   unsigned char getType() const { return st_info & 0x0f; }
598   void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
599   void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
600   void setBindingAndType(unsigned char b, unsigned char t) {
601     st_info = (b << 4) + (t & 0x0f);
602   }
603 };
604
605 // The size (in bytes) of symbol table entries.
606 enum {
607   SYMENTRY_SIZE32 = 16, // 32-bit symbol entry size
608   SYMENTRY_SIZE64 = 24  // 64-bit symbol entry size.
609 };
610
611 // Symbol bindings.
612 enum {
613   STB_LOCAL = 0,   // Local symbol, not visible outside obj file containing def
614   STB_GLOBAL = 1,  // Global symbol, visible to all object files being combined
615   STB_WEAK = 2,    // Weak symbol, like global but lower-precedence
616   STB_LOPROC = 13, // Lowest processor-specific binding type
617   STB_HIPROC = 15  // Highest processor-specific binding type
618 };
619
620 // Symbol types.
621 enum {
622   STT_NOTYPE  = 0,   // Symbol's type is not specified
623   STT_OBJECT  = 1,   // Symbol is a data object (variable, array, etc.)
624   STT_FUNC    = 2,   // Symbol is executable code (function, etc.)
625   STT_SECTION = 3,   // Symbol refers to a section
626   STT_FILE    = 4,   // Local, absolute symbol that refers to a file
627   STT_COMMON  = 5,   // An uninitialised common block
628   STT_TLS     = 6,   // Thread local data object
629   STT_LOPROC  = 13,  // Lowest processor-specific symbol type
630   STT_HIPROC  = 15   // Highest processor-specific symbol type
631 };
632
633 enum {
634   STV_DEFAULT   = 0,  // Visibility is specified by binding type
635   STV_INTERNAL  = 1,  // Defined by processor supplements
636   STV_HIDDEN    = 2,  // Not visible to other components
637   STV_PROTECTED = 3   // Visible in other components but not preemptable
638 };
639
640 // Relocation entry, without explicit addend.
641 struct Elf32_Rel {
642   Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr)
643   Elf32_Word r_info;   // Symbol table index and type of relocation to apply
644
645   // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
646   // and ELF32_R_INFO macros defined in the ELF specification:
647   Elf32_Word getSymbol() const { return (r_info >> 8); }
648   unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
649   void setSymbol(Elf32_Word s) { setSymbolAndType(s, getType()); }
650   void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
651   void setSymbolAndType(Elf32_Word s, unsigned char t) {
652     r_info = (s << 8) + t;
653   }
654 };
655
656 // Relocation entry with explicit addend.
657 struct Elf32_Rela {
658   Elf32_Addr  r_offset; // Location (file byte offset, or program virtual addr)
659   Elf32_Word  r_info;   // Symbol table index and type of relocation to apply
660   Elf32_Sword r_addend; // Compute value for relocatable field by adding this
661
662   // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
663   // and ELF32_R_INFO macros defined in the ELF specification:
664   Elf32_Word getSymbol() const { return (r_info >> 8); }
665   unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
666   void setSymbol(Elf32_Word s) { setSymbolAndType(s, getType()); }
667   void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
668   void setSymbolAndType(Elf32_Word s, unsigned char t) {
669     r_info = (s << 8) + t;
670   }
671 };
672
673 // Relocation entry, without explicit addend.
674 struct Elf64_Rel {
675   Elf64_Addr r_offset; // Location (file byte offset, or program virtual addr).
676   Elf64_Xword r_info;   // Symbol table index and type of relocation to apply.
677
678   // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
679   // and ELF64_R_INFO macros defined in the ELF specification:
680   Elf64_Xword getSymbol() const { return (r_info >> 32); }
681   unsigned char getType() const {
682     return (unsigned char) (r_info & 0xffffffffL);
683   }
684   void setSymbol(Elf32_Word s) { setSymbolAndType(s, getType()); }
685   void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
686   void setSymbolAndType(Elf64_Xword s, unsigned char t) {
687     r_info = (s << 32) + (t&0xffffffffL);
688   }
689 };
690
691 // Relocation entry with explicit addend.
692 struct Elf64_Rela {
693   Elf64_Addr  r_offset; // Location (file byte offset, or program virtual addr).
694   Elf64_Xword  r_info;   // Symbol table index and type of relocation to apply.
695   Elf64_Sxword r_addend; // Compute value for relocatable field by adding this.
696
697   // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
698   // and ELF64_R_INFO macros defined in the ELF specification:
699   Elf64_Xword getSymbol() const { return (r_info >> 32); }
700   unsigned char getType() const {
701     return (unsigned char) (r_info & 0xffffffffL);
702   }
703   void setSymbol(Elf64_Xword s) { setSymbolAndType(s, getType()); }
704   void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
705   void setSymbolAndType(Elf64_Xword s, unsigned char t) {
706     r_info = (s << 32) + (t&0xffffffffL);
707   }
708 };
709
710 // Program header for ELF32.
711 struct Elf32_Phdr {
712   Elf32_Word p_type;   // Type of segment
713   Elf32_Off  p_offset; // File offset where segment is located, in bytes
714   Elf32_Addr p_vaddr;  // Virtual address of beginning of segment
715   Elf32_Addr p_paddr;  // Physical address of beginning of segment (OS-specific)
716   Elf32_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
717   Elf32_Word p_memsz;  // Num. of bytes in mem image of segment (may be zero)
718   Elf32_Word p_flags;  // Segment flags
719   Elf32_Word p_align;  // Segment alignment constraint
720 };
721
722 // Program header for ELF64.
723 struct Elf64_Phdr {
724   Elf64_Word   p_type;   // Type of segment
725   Elf64_Word   p_flags;  // Segment flags
726   Elf64_Off    p_offset; // File offset where segment is located, in bytes
727   Elf64_Addr   p_vaddr;  // Virtual address of beginning of segment
728   Elf64_Addr   p_paddr;  // Physical address of beginning of segment (OS-specific)
729   Elf64_Xword  p_filesz; // Num. of bytes in file image of segment (may be zero)
730   Elf64_Xword  p_memsz;  // Num. of bytes in mem image of segment (may be zero)
731   Elf64_Xword  p_align;  // Segment alignment constraint
732 };
733
734 // Segment types.
735 enum {
736   PT_NULL    = 0, // Unused segment.
737   PT_LOAD    = 1, // Loadable segment.
738   PT_DYNAMIC = 2, // Dynamic linking information.
739   PT_INTERP  = 3, // Interpreter pathname.
740   PT_NOTE    = 4, // Auxiliary information.
741   PT_SHLIB   = 5, // Reserved.
742   PT_PHDR    = 6, // The program header table itself.
743   PT_LOPROC  = 0x70000000, // Lowest processor-specific program hdr entry type.
744   PT_HIPROC  = 0x7fffffff  // Highest processor-specific program hdr entry type.
745 };
746
747 // Segment flag bits.
748 enum {
749   PF_X        = 1,         // Execute
750   PF_W        = 2,         // Write
751   PF_R        = 4,         // Read
752   PF_MASKPROC = 0xf0000000 // Unspecified
753 };
754
755 // Dynamic table entry for ELF32.
756 struct Elf32_Dyn
757 {
758   Elf32_Sword d_tag;            // Type of dynamic table entry.
759   union
760   {
761       Elf32_Word d_val;         // Integer value of entry.
762       Elf32_Addr d_ptr;         // Pointer value of entry.
763   } d_un;
764 };
765
766 // Dynamic table entry for ELF64.
767 struct Elf64_Dyn
768 {
769   Elf64_Sxword d_tag;           // Type of dynamic table entry.
770   union
771   {
772       Elf64_Xword d_val;        // Integer value of entry.
773       Elf64_Addr  d_ptr;        // Pointer value of entry.
774   } d_un;
775 };
776
777 // Dynamic table entry tags.
778 enum {
779   DT_NULL         = 0,        // Marks end of dynamic array.
780   DT_NEEDED       = 1,        // String table offset of needed library.
781   DT_PLTRELSZ     = 2,        // Size of relocation entries in PLT.
782   DT_PLTGOT       = 3,        // Address associated with linkage table.
783   DT_HASH         = 4,        // Address of symbolic hash table.
784   DT_STRTAB       = 5,        // Address of dynamic string table.
785   DT_SYMTAB       = 6,        // Address of dynamic symbol table.
786   DT_RELA         = 7,        // Address of relocation table (Rela entries).
787   DT_RELASZ       = 8,        // Size of Rela relocation table.
788   DT_RELAENT      = 9,        // Size of a Rela relocation entry.
789   DT_STRSZ        = 10,       // Total size of the string table.
790   DT_SYMENT       = 11,       // Size of a symbol table entry.
791   DT_INIT         = 12,       // Address of initialization function.
792   DT_FINI         = 13,       // Address of termination function.
793   DT_SONAME       = 14,       // String table offset of a shared objects name.
794   DT_RPATH        = 15,       // String table offset of library search path.
795   DT_SYMBOLIC     = 16,       // Changes symbol resolution algorithm.
796   DT_REL          = 17,       // Address of relocation table (Rel entries).
797   DT_RELSZ        = 18,       // Size of Rel relocation table.
798   DT_RELENT       = 19,       // Size of a Rel relocation entry.
799   DT_PLTREL       = 20,       // Type of relocation entry used for linking.
800   DT_DEBUG        = 21,       // Reserved for debugger.
801   DT_TEXTREL      = 22,       // Relocations exist for non-writable segements.
802   DT_JMPREL       = 23,       // Address of relocations associated with PLT.
803   DT_BIND_NOW     = 24,       // Process all relocations before execution.
804   DT_INIT_ARRAY   = 25,       // Pointer to array of initialization functions.
805   DT_FINI_ARRAY   = 26,       // Pointer to array of termination functions.
806   DT_INIT_ARRAYSZ = 27,       // Size of DT_INIT_ARRAY.
807   DT_FINI_ARRAYSZ = 28,       // Size of DT_FINI_ARRAY.
808   DT_LOOS         = 0x60000000, // Start of environment specific tags.
809   DT_HIOS         = 0x6FFFFFFF, // End of environment specific tags.
810   DT_LOPROC       = 0x70000000, // Start of processor specific tags.
811   DT_HIPROC       = 0x7FFFFFFF  // End of processor specific tags.
812 };
813
814 } // end namespace ELF
815
816 } // end namespace llvm
817
818 #endif