Add more methods to gather target specific elf stuff
[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 the Tool
14 // Interface Standard (TIS) Executable and Linking Format (ELF) Specification
15 // Version 1.2, May 1995. The ELF64 is based on HP/Intel definition of the
16 // ELF-64 object file format document, Version 1.5 Draft 2 May 27, 1998
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef CODEGEN_ELF_H
21 #define CODEGEN_ELF_H
22
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/CodeGen/BinaryObject.h"
25 #include "llvm/CodeGen/MachineRelocation.h"
26 #include "llvm/Support/DataTypes.h"
27 #include <cstring>
28
29 namespace llvm {
30   class BinaryObject;
31
32   // Identification Indexes
33   enum {
34     EI_MAG0 = 0,
35     EI_MAG1 = 1,
36     EI_MAG2 = 2,
37     EI_MAG3 = 3
38   };
39
40   // File types
41   enum {
42     ET_NONE   = 0,      // No file type
43     ET_REL    = 1,      // Relocatable file
44     ET_EXEC   = 2,      // Executable file
45     ET_DYN    = 3,      // Shared object file
46     ET_CORE   = 4,      // Core file
47     ET_LOPROC = 0xff00, // Beginning of processor-specific codes
48     ET_HIPROC = 0xffff  // Processor-specific
49   };
50
51   // Versioning
52   enum {
53     EV_NONE = 0,
54     EV_CURRENT = 1
55   };
56
57   /// ELFSection - This struct contains information about each section that is
58   /// emitted to the file.  This is eventually turned into the section header
59   /// table at the end of the file.
60   class ELFSection : public BinaryObject {
61     public:
62     // ELF specific fields
63     unsigned NameIdx;   // sh_name - .shstrtab idx of name, once emitted.
64     unsigned Type;      // sh_type - Section contents & semantics 
65     unsigned Flags;     // sh_flags - Section flags.
66     uint64_t Addr;      // sh_addr - The mem addr this section is in.
67     unsigned Offset;    // sh_offset - Offset from the file start
68     unsigned Size;      // sh_size - The section size.
69     unsigned Link;      // sh_link - Section header table index link.
70     unsigned Info;      // sh_info - Auxillary information.
71     unsigned Align;     // sh_addralign - Alignment of section.
72     unsigned EntSize;   // sh_entsize - Size of entries in the section e
73
74     // Section Header Flags
75     enum {
76       SHF_WRITE            = 1 << 0, // Writable
77       SHF_ALLOC            = 1 << 1, // Mapped into the process addr space
78       SHF_EXECINSTR        = 1 << 2, // Executable
79       SHF_MERGE            = 1 << 4, // Might be merged if equal
80       SHF_STRINGS          = 1 << 5, // Contains null-terminated strings
81       SHF_INFO_LINK        = 1 << 6, // 'sh_info' contains SHT index
82       SHF_LINK_ORDER       = 1 << 7, // Preserve order after combining
83       SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
84       SHF_GROUP            = 1 << 9, // Section is a member of a group
85       SHF_TLS              = 1 << 10 // Section holds thread-local data
86     };
87
88     // Section Types
89     enum {
90       SHT_NULL     = 0,  // No associated section (inactive entry).
91       SHT_PROGBITS = 1,  // Program-defined contents.
92       SHT_SYMTAB   = 2,  // Symbol table.
93       SHT_STRTAB   = 3,  // String table.
94       SHT_RELA     = 4,  // Relocation entries; explicit addends.
95       SHT_HASH     = 5,  // Symbol hash table.
96       SHT_DYNAMIC  = 6,  // Information for dynamic linking.
97       SHT_NOTE     = 7,  // Information about the file.
98       SHT_NOBITS   = 8,  // Data occupies no space in the file.
99       SHT_REL      = 9,  // Relocation entries; no explicit addends.
100       SHT_SHLIB    = 10, // Reserved.
101       SHT_DYNSYM   = 11, // Symbol table.
102       SHT_LOPROC   = 0x70000000, // Lowest processor arch-specific type.
103       SHT_HIPROC   = 0x7fffffff, // Highest processor arch-specific type.
104       SHT_LOUSER   = 0x80000000, // Lowest type reserved for applications.
105       SHT_HIUSER   = 0xffffffff  // Highest type reserved for applications.
106     };
107
108     // Special section indices.
109     enum {
110       SHN_UNDEF     = 0,      // Undefined, missing, irrelevant
111       SHN_LORESERVE = 0xff00, // Lowest reserved index
112       SHN_LOPROC    = 0xff00, // Lowest processor-specific index
113       SHN_HIPROC    = 0xff1f, // Highest processor-specific index
114       SHN_ABS       = 0xfff1, // Symbol has absolute value; no relocation
115       SHN_COMMON    = 0xfff2, // FORTRAN COMMON or C external global variables
116       SHN_HIRESERVE = 0xffff  // Highest reserved index
117     };
118
119     /// SectionIdx - The number of the section in the Section Table.
120     unsigned short SectionIdx;
121
122     ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
123       : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
124         Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0) {}
125   };
126
127   /// ELFSym - This struct contains information about each symbol that is
128   /// added to logical symbol table for the module.  This is eventually
129   /// turned into a real symbol table in the file.
130   struct ELFSym {
131     // The global value this corresponds to. Global symbols can be on of the 
132     // 3 types : if this symbol has a zero initializer, it is common or should
133     // be placed in bss section otherwise it's a constant.
134     const GlobalValue *GV;
135     bool IsCommon;
136     bool IsBss;
137     bool IsConstant;
138
139     // ELF specific fields
140     unsigned NameIdx;         // Index in .strtab of name, once emitted.
141     uint64_t Value;
142     unsigned Size;
143     uint8_t Info;
144     uint8_t Other;
145     unsigned short SectionIdx;
146
147     enum { 
148       STB_LOCAL = 0,
149       STB_GLOBAL = 1,
150       STB_WEAK = 2 
151     };
152
153     enum { 
154       STT_NOTYPE = 0,
155       STT_OBJECT = 1,
156       STT_FUNC = 2,
157       STT_SECTION = 3,
158       STT_FILE = 4 
159     };
160
161     enum {
162       STV_DEFAULT = 0,  // Visibility is specified by binding type
163       STV_INTERNAL = 1, // Defined by processor supplements
164       STV_HIDDEN = 2,   // Not visible to other components
165       STV_PROTECTED = 3 // Visible in other components but not preemptable
166     };
167
168     ELFSym(const GlobalValue *gv) : GV(gv), IsCommon(false), IsBss(false),
169                                     IsConstant(false), NameIdx(0), Value(0),
170                                     Size(0), Info(0), Other(STV_DEFAULT),
171                                     SectionIdx(ELFSection::SHN_UNDEF) {
172       if (!GV)
173         return;
174
175       switch (GV->getVisibility()) {
176       default:
177         assert(0 && "unknown visibility type");
178       case GlobalValue::DefaultVisibility:
179         Other = STV_DEFAULT;
180         break;
181       case GlobalValue::HiddenVisibility:
182         Other = STV_HIDDEN;
183         break;
184       case GlobalValue::ProtectedVisibility:
185         Other = STV_PROTECTED;
186         break;
187       }
188     }
189
190     unsigned getBind() {
191       return (Info >> 4) & 0xf;
192     }
193
194     void setBind(unsigned X) {
195       assert(X == (X & 0xF) && "Bind value out of range!");
196       Info = (Info & 0x0F) | (X << 4);
197     }
198     void setType(unsigned X) {
199       assert(X == (X & 0xF) && "Type value out of range!");
200       Info = (Info & 0xF0) | X;
201     }
202   };
203
204   /// ELFRelocation - This class contains all the information necessary to
205   /// to generate any 32-bit or 64-bit ELF relocation entry.
206   class ELFRelocation {
207     uint64_t r_offset;    // offset in the section of the object this applies to
208     uint32_t r_symidx;    // symbol table index of the symbol to use
209     uint32_t r_type;      // machine specific relocation type
210     int64_t  r_add;       // explicit relocation addend
211     bool     r_rela;      // if true then the addend is part of the entry
212                           // otherwise the addend is at the location specified
213                           // by r_offset
214   public:
215     uint64_t getInfo(bool is64Bit) const {
216       if (is64Bit)
217         return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL);
218       else
219         return (r_symidx << 8)  + (r_type & 0xFFL);
220     }
221
222     uint64_t getOffset() const { return r_offset; }
223     int64_t getAddend() const { return r_add; }
224
225     ELFRelocation(uint64_t off, uint32_t sym, uint32_t type,
226                   bool rela = true, int64_t addend = 0) :
227       r_offset(off), r_symidx(sym), r_type(type),
228       r_add(addend), r_rela(rela) {}
229   };
230
231 } // end namespace llvm
232
233 #endif