Support for ELF Visibility
[oota-llvm.git] / lib / CodeGen / ELF.h
1 //===-- lib/CodeGen/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
14 // the Tool Interface Standard (TIS) Executable and Linking Format
15 // (ELF) Specification Version 1.2, May 1995. The ELF64 stuff is not
16 // standardized, as far as I can tell. It was largely based on information
17 // I found in OpenBSD header files.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef CODEGEN_ELF_H
22 #define CODEGEN_ELF_H
23
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/CodeGen/MachineRelocation.h"
26 #include "llvm/Support/DataTypes.h"
27 #include <cstring>
28
29 namespace llvm {
30
31   // Identification Indexes
32   enum {
33     EI_MAG0 = 0,
34     EI_MAG1 = 1,
35     EI_MAG2 = 2,
36     EI_MAG3 = 3
37   };
38
39   // File types
40   enum {
41     ET_NONE   = 0,      // No file type
42     ET_REL    = 1,      // Relocatable file
43     ET_EXEC   = 2,      // Executable file
44     ET_DYN    = 3,      // Shared object file
45     ET_CORE   = 4,      // Core file
46     ET_LOPROC = 0xff00, // Beginning of processor-specific codes
47     ET_HIPROC = 0xffff  // Processor-specific
48   };
49
50   // Object file classes.
51   enum {
52     ELFCLASS32 = 1, // 32-bit object file
53     ELFCLASS64 = 2  // 64-bit object file
54   };
55
56   // Object file byte orderings.
57   enum {
58     ELFDATA2LSB = 1, // Little-endian object file
59     ELFDATA2MSB = 2  // Big-endian object file
60   };
61
62   // Versioning
63   enum {
64     EV_NONE = 0,
65     EV_CURRENT = 1
66   };
67
68   struct ELFHeader {
69     // e_machine - This field is the target specific value to emit as the
70     // e_machine member of the ELF header.
71     unsigned short e_machine;
72
73     // e_flags - The machine flags for the target.  This defaults to zero.
74     unsigned e_flags;
75
76     // e_size - Holds the ELF header's size in bytes
77     unsigned e_ehsize;
78
79     // Endianess and ELF Class (64 or 32 bits)
80     unsigned ByteOrder;
81     unsigned ElfClass;
82
83     unsigned getByteOrder() const { return ByteOrder; }
84     unsigned getElfClass() const { return ElfClass; }
85     unsigned getSize() const { return e_ehsize; }
86     unsigned getMachine() const { return e_machine; }
87     unsigned getFlags() const { return e_flags; }
88
89     ELFHeader(unsigned short machine, unsigned flags,
90               bool is64Bit, bool isLittleEndian)
91       : e_machine(machine), e_flags(flags) {
92         ElfClass  = is64Bit ? ELFCLASS64 : ELFCLASS32;
93         ByteOrder = isLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
94         e_ehsize  = is64Bit ? 64 : 52;
95       }
96   };
97
98   /// ELFSection - This struct contains information about each section that is
99   /// emitted to the file.  This is eventually turned into the section header
100   /// table at the end of the file.
101   struct ELFSection {
102
103     // Name of the section
104     std::string Name;
105
106     // ELF specific fields
107     unsigned NameIdx;   // sh_name - .shstrtab idx of name, once emitted.
108     unsigned Type;      // sh_type - Section contents & semantics 
109     unsigned Flags;     // sh_flags - Section flags.
110     uint64_t Addr;      // sh_addr - The mem addr this section is in.
111     unsigned Offset;    // sh_offset - Offset from the file start
112     unsigned Size;      // sh_size - The section size.
113     unsigned Link;      // sh_link - Section header table index link.
114     unsigned Info;      // sh_info - Auxillary information.
115     unsigned Align;     // sh_addralign - Alignment of section.
116     unsigned EntSize;   // sh_entsize - Size of entries in the section e
117
118     // Section Header Flags
119     enum {
120       SHF_WRITE            = 1 << 0, // Writable
121       SHF_ALLOC            = 1 << 1, // Mapped into the process addr space
122       SHF_EXECINSTR        = 1 << 2, // Executable
123       SHF_MERGE            = 1 << 4, // Might be merged if equal
124       SHF_STRINGS          = 1 << 5, // Contains null-terminated strings
125       SHF_INFO_LINK        = 1 << 6, // 'sh_info' contains SHT index
126       SHF_LINK_ORDER       = 1 << 7, // Preserve order after combining
127       SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
128       SHF_GROUP            = 1 << 9, // Section is a member of a group
129       SHF_TLS              = 1 << 10 // Section holds thread-local data
130     };
131
132     // Section Types
133     enum {
134       SHT_NULL     = 0,  // No associated section (inactive entry).
135       SHT_PROGBITS = 1,  // Program-defined contents.
136       SHT_SYMTAB   = 2,  // Symbol table.
137       SHT_STRTAB   = 3,  // String table.
138       SHT_RELA     = 4,  // Relocation entries; explicit addends.
139       SHT_HASH     = 5,  // Symbol hash table.
140       SHT_DYNAMIC  = 6,  // Information for dynamic linking.
141       SHT_NOTE     = 7,  // Information about the file.
142       SHT_NOBITS   = 8,  // Data occupies no space in the file.
143       SHT_REL      = 9,  // Relocation entries; no explicit addends.
144       SHT_SHLIB    = 10, // Reserved.
145       SHT_DYNSYM   = 11, // Symbol table.
146       SHT_LOPROC   = 0x70000000, // Lowest processor architecture-specific type.
147       SHT_HIPROC   = 0x7fffffff, // Highest processor architecture-specific type.
148       SHT_LOUSER   = 0x80000000, // Lowest type reserved for applications.
149       SHT_HIUSER   = 0xffffffff  // Highest type reserved for applications.
150     };
151
152     // Special section indices.
153     enum {
154       SHN_UNDEF     = 0,      // Undefined, missing, irrelevant
155       SHN_LORESERVE = 0xff00, // Lowest reserved index
156       SHN_LOPROC    = 0xff00, // Lowest processor-specific index
157       SHN_HIPROC    = 0xff1f, // Highest processor-specific index
158       SHN_ABS       = 0xfff1, // Symbol has absolute value; no relocation
159       SHN_COMMON    = 0xfff2, // FORTRAN COMMON or C external global variables
160       SHN_HIRESERVE = 0xffff  // Highest reserved index
161     };
162
163     /// SectionIdx - The number of the section in the Section Table.
164     unsigned short SectionIdx;
165
166     /// SectionData - The actual data for this section which we are building
167     /// up for emission to the file.
168     std::vector<unsigned char> SectionData;
169
170     /// Relocations - The relocations that we have encountered so far in this 
171     /// section that we will need to convert to Elf relocation entries when
172     /// the file is written.
173     std::vector<MachineRelocation> Relocations;
174
175     /// Section Header Size 
176     static unsigned getSectionHdrSize(bool is64Bit)
177       { return is64Bit ? 64 : 40; }
178
179     ELFSection(const std::string &name)
180       : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0),
181         Link(0), Info(0), Align(0), EntSize(0) {}
182   };
183
184   /// ELFSym - This struct contains information about each symbol that is
185   /// added to logical symbol table for the module.  This is eventually
186   /// turned into a real symbol table in the file.
187   struct ELFSym {
188     const GlobalValue *GV;    // The global value this corresponds to.
189
190     // ELF specific fields
191     unsigned NameIdx;         // Index in .strtab of name, once emitted.
192     uint64_t Value;
193     unsigned Size;
194     uint8_t Info;
195     uint8_t Other;
196     unsigned short SectionIdx;
197
198     enum { 
199       STB_LOCAL = 0,
200       STB_GLOBAL = 1,
201       STB_WEAK = 2 
202     };
203
204     enum { 
205       STT_NOTYPE = 0,
206       STT_OBJECT = 1,
207       STT_FUNC = 2,
208       STT_SECTION = 3,
209       STT_FILE = 4 
210     };
211
212     enum {
213       STV_DEFAULT = 0,  // Visibility is specified by binding type
214       STV_INTERNAL = 1, // Defined by processor supplements
215       STV_HIDDEN = 2,   // Not visible to other components
216       STV_PROTECTED = 3 // Visible in other components but not preemptable
217     };
218
219     ELFSym(const GlobalValue *gv) : GV(gv), NameIdx(0), Value(0),
220                                     Size(0), Info(0), Other(0),
221                                     SectionIdx(ELFSection::SHN_UNDEF) {
222       if (!GV)
223         return;
224
225       switch (GV->getVisibility()) {
226       default:
227         assert(0 && "unknown visibility type");
228       case GlobalValue::DefaultVisibility:
229         Other = STV_DEFAULT;
230         break;
231       case GlobalValue::HiddenVisibility:
232         Other = STV_HIDDEN;
233         break;
234       case GlobalValue::ProtectedVisibility:
235         Other = STV_PROTECTED;
236         break;
237       }
238     }
239
240     void SetBind(unsigned X) {
241       assert(X == (X & 0xF) && "Bind value out of range!");
242       Info = (Info & 0x0F) | (X << 4);
243     }
244     void SetType(unsigned X) {
245       assert(X == (X & 0xF) && "Type value out of range!");
246       Info = (Info & 0xF0) | X;
247     }
248
249     static unsigned getEntrySize(bool is64Bit)
250       { return is64Bit ? 24 : 16; }
251   };
252
253 } // end namespace llvm
254
255 #endif