add module identifier to the elf object file
[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/CodeGen/BinaryObject.h"
24 #include "llvm/CodeGen/MachineRelocation.h"
25 #include "llvm/Support/DataTypes.h"
26
27 namespace llvm {
28   class GlobalValue;
29
30   // Identification Indexes
31   enum {
32     EI_MAG0 = 0,
33     EI_MAG1 = 1,
34     EI_MAG2 = 2,
35     EI_MAG3 = 3
36   };
37
38   // File types
39   enum {
40     ET_NONE   = 0,      // No file type
41     ET_REL    = 1,      // Relocatable file
42     ET_EXEC   = 2,      // Executable file
43     ET_DYN    = 3,      // Shared object file
44     ET_CORE   = 4,      // Core file
45     ET_LOPROC = 0xff00, // Beginning of processor-specific codes
46     ET_HIPROC = 0xffff  // Processor-specific
47   };
48
49   // Versioning
50   enum {
51     EV_NONE = 0,
52     EV_CURRENT = 1
53   };
54
55   /// ELFSym - This struct contains information about each symbol that is
56   /// added to logical symbol table for the module.  This is eventually
57   /// turned into a real symbol table in the file.
58   struct ELFSym {
59
60     // ELF symbols are related to llvm ones by being one of the two llvm
61     // types, for the other ones (section, file, func) a null pointer is
62     // assumed by default.
63     union {
64       const GlobalValue *GV;  // If this is a pointer to a GV
65       const char *Ext;        // If this is a pointer to a named symbol
66     } Source;
67
68     // Describes from which source type this ELF symbol comes from,
69     // they can be GlobalValue, ExternalSymbol or neither.
70     enum {
71       isGV,      // The Source.GV field is valid.
72       isExtSym,  // The Source.ExtSym field is valid.
73       isOther    // Not a GlobalValue or External Symbol
74     };
75     unsigned SourceType;
76
77     bool isGlobalValue() { return SourceType == isGV; }
78     bool isExternalSym() { return SourceType == isExtSym; }
79
80     // getGlobalValue - If this is a global value which originated the
81     // elf symbol, return a reference to it.
82     const GlobalValue *getGlobalValue() {
83       assert(SourceType == isGV && "This is not a global value");
84       return Source.GV;
85     };
86
87     // getExternalSym - If this is an external symbol which originated the
88     // elf symbol, return a reference to it.
89     const char *getExternalSymbol() {
90       assert(SourceType == isExtSym && "This is not an external symbol");
91       return Source.Ext;
92     };
93
94     // getGV - From a global value return a elf symbol to represent it
95     static ELFSym *getGV(const GlobalValue *GV, unsigned Bind,
96                          unsigned Type, unsigned Visibility) {
97       ELFSym *Sym = new ELFSym();
98       Sym->Source.GV = GV;
99       Sym->setBind(Bind);
100       Sym->setType(Type);
101       Sym->setVisibility(Visibility);
102       Sym->SourceType = isGV;
103       return Sym;
104     }
105
106     // getExtSym - Create and return an elf symbol to represent an
107     // external symbol
108     static ELFSym *getExtSym(const char *Ext) {
109       ELFSym *Sym = new ELFSym();
110       Sym->Source.Ext = Ext;
111       Sym->setBind(STB_GLOBAL);
112       Sym->setType(STT_NOTYPE);
113       Sym->setVisibility(STV_DEFAULT);
114       Sym->SourceType = isExtSym;
115       return Sym;
116     }
117
118     // getSectionSym - Returns a elf symbol to represent an elf section
119     static ELFSym *getSectionSym() {
120       ELFSym *Sym = new ELFSym();
121       Sym->setBind(STB_LOCAL);
122       Sym->setType(STT_SECTION);
123       Sym->setVisibility(STV_DEFAULT);
124       Sym->SourceType = isOther;
125       return Sym;
126     }
127
128     // getSectionSym - Returns a elf symbol to represent an elf section
129     static ELFSym *getFileSym() {
130       ELFSym *Sym = new ELFSym();
131       Sym->setBind(STB_LOCAL);
132       Sym->setType(STT_FILE);
133       Sym->setVisibility(STV_DEFAULT);
134       Sym->SectionIdx = 0xfff1;  // ELFSection::SHN_ABS;
135       Sym->SourceType = isOther;
136       return Sym;
137     }
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     // Symbol index into the Symbol table
148     unsigned SymTabIdx;
149
150     enum {
151       STB_LOCAL = 0,  // Local sym, not visible outside obj file containing def
152       STB_GLOBAL = 1, // Global sym, visible to all object files being combined
153       STB_WEAK = 2    // Weak symbol, like global but lower-precedence
154     };
155
156     enum {
157       STT_NOTYPE = 0,  // Symbol's type is not specified
158       STT_OBJECT = 1,  // Symbol is a data object (variable, array, etc.)
159       STT_FUNC = 2,    // Symbol is executable code (function, etc.)
160       STT_SECTION = 3, // Symbol refers to a section
161       STT_FILE = 4     // Local, absolute symbol that refers to a file
162     };
163
164     enum {
165       STV_DEFAULT = 0,  // Visibility is specified by binding type
166       STV_INTERNAL = 1, // Defined by processor supplements
167       STV_HIDDEN = 2,   // Not visible to other components
168       STV_PROTECTED = 3 // Visible in other components but not preemptable
169     };
170
171     ELFSym() : SourceType(isOther), NameIdx(0), Value(0),
172                Size(0), Info(0), Other(STV_DEFAULT), SectionIdx(0),
173                SymTabIdx(0) {}
174
175     unsigned getBind() const { return (Info >> 4) & 0xf; }
176     unsigned getType() const { return Info & 0xf; }
177     bool isLocalBind() const { return getBind() == STB_LOCAL; }
178     bool isFileType() const { return getType() == STT_FILE; }
179
180     void setBind(unsigned X) {
181       assert(X == (X & 0xF) && "Bind value out of range!");
182       Info = (Info & 0x0F) | (X << 4);
183     }
184
185     void setType(unsigned X) {
186       assert(X == (X & 0xF) && "Type value out of range!");
187       Info = (Info & 0xF0) | X;
188     }
189
190     void setVisibility(unsigned V) {
191       assert(V == (V & 0x3) && "Visibility value out of range!");
192       Other = V;
193     }
194   };
195
196   /// ELFSection - This struct contains information about each section that is
197   /// emitted to the file.  This is eventually turned into the section header
198   /// table at the end of the file.
199   class ELFSection : public BinaryObject {
200     public:
201     // ELF specific fields
202     unsigned NameIdx;   // sh_name - .shstrtab idx of name, once emitted.
203     unsigned Type;      // sh_type - Section contents & semantics 
204     unsigned Flags;     // sh_flags - Section flags.
205     uint64_t Addr;      // sh_addr - The mem addr this section is in.
206     unsigned Offset;    // sh_offset - Offset from the file start
207     unsigned Size;      // sh_size - The section size.
208     unsigned Link;      // sh_link - Section header table index link.
209     unsigned Info;      // sh_info - Auxillary information.
210     unsigned Align;     // sh_addralign - Alignment of section.
211     unsigned EntSize;   // sh_entsize - Size of entries in the section e
212
213     // Section Header Flags
214     enum {
215       SHF_WRITE            = 1 << 0, // Writable
216       SHF_ALLOC            = 1 << 1, // Mapped into the process addr space
217       SHF_EXECINSTR        = 1 << 2, // Executable
218       SHF_MERGE            = 1 << 4, // Might be merged if equal
219       SHF_STRINGS          = 1 << 5, // Contains null-terminated strings
220       SHF_INFO_LINK        = 1 << 6, // 'sh_info' contains SHT index
221       SHF_LINK_ORDER       = 1 << 7, // Preserve order after combining
222       SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
223       SHF_GROUP            = 1 << 9, // Section is a member of a group
224       SHF_TLS              = 1 << 10 // Section holds thread-local data
225     };
226
227     // Section Types
228     enum {
229       SHT_NULL     = 0,  // No associated section (inactive entry).
230       SHT_PROGBITS = 1,  // Program-defined contents.
231       SHT_SYMTAB   = 2,  // Symbol table.
232       SHT_STRTAB   = 3,  // String table.
233       SHT_RELA     = 4,  // Relocation entries; explicit addends.
234       SHT_HASH     = 5,  // Symbol hash table.
235       SHT_DYNAMIC  = 6,  // Information for dynamic linking.
236       SHT_NOTE     = 7,  // Information about the file.
237       SHT_NOBITS   = 8,  // Data occupies no space in the file.
238       SHT_REL      = 9,  // Relocation entries; no explicit addends.
239       SHT_SHLIB    = 10, // Reserved.
240       SHT_DYNSYM   = 11, // Symbol table.
241       SHT_LOPROC   = 0x70000000, // Lowest processor arch-specific type.
242       SHT_HIPROC   = 0x7fffffff, // Highest processor arch-specific type.
243       SHT_LOUSER   = 0x80000000, // Lowest type reserved for applications.
244       SHT_HIUSER   = 0xffffffff  // Highest type reserved for applications.
245     };
246
247     // Special section indices.
248     enum {
249       SHN_UNDEF     = 0,      // Undefined, missing, irrelevant
250       SHN_LORESERVE = 0xff00, // Lowest reserved index
251       SHN_LOPROC    = 0xff00, // Lowest processor-specific index
252       SHN_HIPROC    = 0xff1f, // Highest processor-specific index
253       SHN_ABS       = 0xfff1, // Symbol has absolute value; no relocation
254       SHN_COMMON    = 0xfff2, // FORTRAN COMMON or C external global variables
255       SHN_HIRESERVE = 0xffff  // Highest reserved index
256     };
257
258     /// SectionIdx - The number of the section in the Section Table.
259     unsigned short SectionIdx;
260
261     /// Sym - The symbol to represent this section if it has one.
262     ELFSym *Sym;
263
264     /// getSymIndex - Returns the symbol table index of the symbol
265     /// representing this section.
266     unsigned getSymbolTableIndex() const {
267       assert(Sym && "section not present in the symbol table");
268       return Sym->SymTabIdx;
269     }
270
271     ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
272       : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
273         Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0), Sym(0) {}
274   };
275
276   /// ELFRelocation - This class contains all the information necessary to
277   /// to generate any 32-bit or 64-bit ELF relocation entry.
278   class ELFRelocation {
279     uint64_t r_offset;    // offset in the section of the object this applies to
280     uint32_t r_symidx;    // symbol table index of the symbol to use
281     uint32_t r_type;      // machine specific relocation type
282     int64_t  r_add;       // explicit relocation addend
283     bool     r_rela;      // if true then the addend is part of the entry
284                           // otherwise the addend is at the location specified
285                           // by r_offset
286   public:
287     uint64_t getInfo(bool is64Bit) const {
288       if (is64Bit)
289         return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL);
290       else
291         return (r_symidx << 8)  + (r_type & 0xFFL);
292     }
293
294     uint64_t getOffset() const { return r_offset; }
295     int64_t getAddend() const { return r_add; }
296
297     ELFRelocation(uint64_t off, uint32_t sym, uint32_t type,
298                   bool rela = true, int64_t addend = 0) :
299       r_offset(off), r_symidx(sym), r_type(type),
300       r_add(addend), r_rela(rela) {}
301   };
302
303 } // end namespace llvm
304
305 #endif