ab11e0c383ddf9156960d16524cfb9a8e80cbccf
[oota-llvm.git] / include / llvm / Support / ELF.h
1 //===-- Support/ELF.h - ELF constants and data structures -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 this file are largely based on the Tool Interface Standard
14 // (TIS) Executable and Linking Format (ELF) Specification Version 1.2,
15 // May 1995.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "Support/DataTypes.h"
20 #include <cstring>
21 #include <cstdlib>
22
23 namespace llvm {
24
25 namespace ELF {
26
27 typedef uint32_t Elf32_Addr; // Program address
28 typedef uint16_t Elf32_Half;
29 typedef uint32_t Elf32_Off;  // File offset
30 typedef int32_t  Elf32_Sword;
31 typedef uint32_t Elf32_Word;
32
33 // Object file magic string.
34 static const char ElfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' };
35
36 struct Elf32_Ehdr {
37   unsigned char e_ident[16]; // ELF Identification bytes
38   Elf32_Half    e_type;      // Type of file (see ET_* below)
39   Elf32_Half    e_machine;   // Required architecture for this file (see EM_*)
40   Elf32_Word    e_version;   // Must be equal to 1
41   Elf32_Addr    e_entry;     // Address to jump to in order to start program
42   Elf32_Off     e_phoff;     // Program header table's file offset, in bytes
43   Elf32_Off     e_shoff;     // Section header table's file offset, in bytes
44   Elf32_Word    e_flags;     // Processor-specific flags
45   Elf32_Half    e_ehsize;    // Size of ELF header, in bytes
46   Elf32_Half    e_phentsize; // Size of an entry in the program header table
47   Elf32_Half    e_phnum;     // Number of entries in the program header table
48   Elf32_Half    e_shentsize; // Size of an entry in the section header table
49   Elf32_Half    e_shnum;     // Number of entries in the section header table
50   Elf32_Half    e_shstrndx;  // Sect hdr table index of sect name string table
51   bool checkMagic () const {
52     return (memcmp (e_ident, ElfMagic, strlen (ElfMagic))) == 0;
53   }
54   unsigned char getFileClass () const { return e_ident[4]; }
55   unsigned char getDataEncoding () { return e_ident[5]; }
56 };
57
58 // File types
59 enum {
60   ET_NONE   = 0,      // No file type
61   ET_REL    = 1,      // Relocatable file
62   ET_EXEC   = 2,      // Executable file
63   ET_DYN    = 3,      // Shared object file
64   ET_CORE   = 4,      // Core file
65   ET_LOPROC = 0xff00, // Beginning of processor-specific codes
66   ET_HIPROC = 0xffff  // Processor-specific
67 };
68
69 // Machine architectures
70 enum {
71   EM_NONE = 0,  // No machine
72   EM_M32 = 1,   // AT&T WE 32100
73   EM_SPARC = 2, // SPARC
74   EM_386 = 3,   // Intel 386
75   EM_68K = 4,   // Motorola 68000
76   EM_88K = 5,   // Motorola 88000
77   EM_860 = 7,   // Intel 80860
78   EM_MIPS = 8   // MIPS R3000
79 };
80
81 // Object file classes.
82 enum {
83   ELFCLASS32 = 1, // 32-bit object file
84   ELFCLASS64 = 2  // 64-bit object file
85 };
86
87 // Object file byte orderings.
88 enum {
89   ELFDATA2LSB = 1, // Little-endian object file
90   ELFDATA2MSB = 2  // Big-endian object file
91 };
92
93 // Section header.
94 struct Elf32_Shdr {
95   Elf32_Word sh_name;      // Section name (index into string table)
96   Elf32_Word sh_type;      // Section type (SHT_*)
97   Elf32_Word sh_flags;     // Section flags (SHF_*)
98   Elf32_Addr sh_addr;      // Address where section is to be loaded
99   Elf32_Off  sh_offset;    // File offset of section data, in bytes
100   Elf32_Word sh_size;      // Size of section, in bytes
101   Elf32_Word sh_link;      // Section type-specific header table index link 
102   Elf32_Word sh_info;      // Section type-specific extra information
103   Elf32_Word sh_addralign; // Section address alignment
104   Elf32_Word sh_entsize;   // Size of records contained within the section
105 };
106
107 // Special section indices.
108 enum {
109   SHN_UNDEF     = 0,      // Undefined, missing, irrelevant, or meaningless
110   SHN_LORESERVE = 0xff00, // Lowest reserved index
111   SHN_LOPROC    = 0xff00, // Lowest processor-specific index
112   SHN_HIPROC    = 0xff1f, // Highest processor-specific index
113   SHN_ABS       = 0xfff1, // Symbol has absolute value; does not need relocation
114   SHN_COMMON    = 0xfff2, // FORTRAN COMMON or C external global variables
115   SHN_HIRESERVE = 0xffff  // Highest reserved index
116 };
117
118 // Section types.
119 enum {
120   SHT_NULL     = 0,  // No associated section (inactive entry).
121   SHT_PROGBITS = 1,  // Program-defined contents.
122   SHT_SYMTAB   = 2,  // Symbol table.
123   SHT_STRTAB   = 3,  // String table.
124   SHT_RELA     = 4,  // Relocation entries; explicit addends.
125   SHT_HASH     = 5,  // Symbol hash table.
126   SHT_DYNAMIC  = 6,  // Information for dynamic linking.
127   SHT_NOTE     = 7,  // Information about the file.
128   SHT_NOBITS   = 8,  // Data occupies no space in the file.
129   SHT_REL      = 9,  // Relocation entries; no explicit addends.
130   SHT_SHLIB    = 10, // Reserved.
131   SHT_DYNSYM   = 11, // Symbol table.
132   SHT_LOPROC   = 0x70000000, // Lowest processor architecture-specific type.
133   SHT_HIPROC   = 0x7fffffff, // Highest processor architecture-specific type.
134   SHT_LOUSER   = 0x80000000, // Lowest type reserved for applications.
135   SHT_HIUSER   = 0xffffffff  // Highest type reserved for applications.
136 };
137
138 // Section flags.
139 enum {
140   SHF_WRITE     = 0x1, // Section data should be writable during execution.
141   SHF_ALLOC     = 0x2, // Section occupies memory during program execution.
142   SHF_EXECINSTR = 0x4, // Section contains executable machine instructions.
143   SHF_MASKPROC  = 0xf0000000 // Bits indicating processor-specific flags.
144 };
145
146 // Symbol table entries.
147 struct Elf32_Sym {
148   Elf32_Word    st_name;  // Symbol name (index into string table)
149   Elf32_Addr    st_value; // Value or address associated with the symbol
150   Elf32_Word    st_size;  // Size of the symbol
151   unsigned char st_info;  // Symbol's type and binding attributes
152   unsigned char st_other; // Must be zero; reserved
153   Elf32_Half    st_shndx; // Which section (header table index) it's defined in
154   
155   // These accessors and mutators correspond to the ELF32_ST_BIND,
156   // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
157   unsigned char getBinding () const { return st_info >> 4; }
158   unsigned char getType () const { return st_info & 0x0f; }
159   void setBinding (unsigned char b) { setBindingAndType (b, getType ()); }
160   void setType (unsigned char t) { setBindingAndType (getBinding (), t); }
161   void setBindingAndType (unsigned char b, unsigned char t) {
162     st_info = (b << 4) + (t & 0x0f);
163   }
164 };
165
166 // Symbol bindings.
167 enum {
168   STB_LOCAL = 0,   // Local symbol, not visible outside obj file containing def
169   STB_GLOBAL = 1,  // Global symbol, visible to all object files being combined
170   STB_WEAK = 2,    // Weak symbol, like global but lower-precedence
171   STB_LOPROC = 13, // Lowest processor-specific binding type
172   STB_HIPROC = 15  // Highest processor-specific binding type
173 };
174
175 // Symbol types.
176 enum {
177   STT_NOTYPE  = 0,   // Symbol's type is not specified
178   STT_OBJECT  = 1,   // Symbol is a data object (variable, array, etc.)
179   STT_FUNC    = 2,   // Symbol is executable code (function, etc.)
180   STT_SECTION = 3,   // Symbol refers to a section
181   STT_FILE    = 4,   // Local, absolute symbol that refers to a file
182   STT_LOPROC  = 13,  // Lowest processor-specific symbol type
183   STT_HIPROC  = 15   // Highest processor-specific symbol type
184 };
185
186 // Relocation entry, without explicit addend.
187 struct Elf32_Rel {
188   Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr) 
189   Elf32_Word r_info;   // Symbol table index and type of relocation to apply
190   
191   // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
192   // and ELF32_R_INFO macros defined in the ELF specification:
193   Elf32_Word getSymbol () const { return (r_info >> 8); }
194   unsigned char getType () const { return (unsigned char) (r_info & 0x0ff); }
195   void setSymbol (Elf32_Word s) { setSymbolAndType (s, getType ()); }
196   void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); }
197   void setSymbolAndType (Elf32_Word s, unsigned char t) {
198     r_info = (s << 8) + t;
199   };
200 };
201
202 // Relocation entry with explicit addend.
203 struct Elf32_Rela {
204   Elf32_Addr  r_offset; // Location (file byte offset, or program virtual addr)     
205   Elf32_Word  r_info;   // Symbol table index and type of relocation to apply
206   Elf32_Sword r_addend; // Compute value for relocatable field by adding this
207   
208   // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
209   // and ELF32_R_INFO macros defined in the ELF specification:
210   Elf32_Word getSymbol () const { return (r_info >> 8); }
211   unsigned char getType () const { return (unsigned char) (r_info & 0x0ff); }
212   void setSymbol (Elf32_Word s) { setSymbolAndType (s, getType ()); }
213   void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); }
214   void setSymbolAndType (Elf32_Word s, unsigned char t) {
215     r_info = (s << 8) + t;
216   };
217 };
218
219 // Program header.
220 struct Elf32_Phdr {
221   Elf32_Word p_type;   // Type of segment
222   Elf32_Off  p_offset; // File offset where segment is located, in bytes
223   Elf32_Addr p_vaddr;  // Virtual address of beginning of segment
224   Elf32_Addr p_paddr;  // Physical address of beginning of segment (OS-specific)
225   Elf32_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
226   Elf32_Word p_memsz;  // Num. of bytes in mem image of segment (may be zero)
227   Elf32_Word p_flags;  // Segment flags
228   Elf32_Word p_align;  // Segment alignment constraint
229 };
230
231 enum {
232   PT_NULL    = 0, // Unused segment.
233   PT_LOAD    = 1, // Loadable segment.
234   PT_DYNAMIC = 2, // Dynamic linking information.
235   PT_INTERP  = 3, // Interpreter pathname.
236   PT_NOTE    = 4, // Auxiliary information.
237   PT_SHLIB   = 5, // Reserved.
238   PT_PHDR    = 6, // The program header table itself.
239   PT_LOPROC  = 0x70000000, // Lowest processor-specific program hdr entry type.
240   PT_HIPROC  = 0x7fffffff  // Highest processor-specific program hdr entry type.
241 };
242
243 } // end namespace ELF
244
245 } // end namespace llvm