Fix coding style issues pointed by Bill.
[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     // The global value this symbol matches. This should be null if the symbol
60     // is not a global value.
61     const GlobalValue *GV;
62
63     // ELF specific fields
64     unsigned NameIdx;         // Index in .strtab of name, once emitted.
65     uint64_t Value;
66     unsigned Size;
67     uint8_t Info;
68     uint8_t Other;
69     unsigned short SectionIdx;
70
71     // Symbol index into the Symbol table
72     unsigned SymTabIdx;
73
74     enum {
75       STB_LOCAL = 0,  // Local sym, not visible outside obj file containing def
76       STB_GLOBAL = 1, // Global sym, visible to all object files being combined
77       STB_WEAK = 2    // Weak symbol, like global but lower-precedence
78     };
79
80     enum {
81       STT_NOTYPE = 0,  // Symbol's type is not specified
82       STT_OBJECT = 1,  // Symbol is a data object (variable, array, etc.)
83       STT_FUNC = 2,    // Symbol is executable code (function, etc.)
84       STT_SECTION = 3, // Symbol refers to a section
85       STT_FILE = 4     // Local, absolute symbol that refers to a file
86     };
87
88     enum {
89       STV_DEFAULT = 0,  // Visibility is specified by binding type
90       STV_INTERNAL = 1, // Defined by processor supplements
91       STV_HIDDEN = 2,   // Not visible to other components
92       STV_PROTECTED = 3 // Visible in other components but not preemptable
93     };
94
95     ELFSym(const GlobalValue *gv) : GV(gv), NameIdx(0), Value(0),
96                                     Size(0), Info(0), Other(STV_DEFAULT),
97                                     SectionIdx(0), SymTabIdx(0) {}
98
99     unsigned getBind() const { return (Info >> 4) & 0xf; }
100     unsigned getType() const { return Info & 0xf; }
101     bool isLocalBind() const { return getBind() == STB_LOCAL; }
102
103     void setBind(unsigned X) {
104       assert(X == (X & 0xF) && "Bind value out of range!");
105       Info = (Info & 0x0F) | (X << 4);
106     }
107
108     void setType(unsigned X) {
109       assert(X == (X & 0xF) && "Type value out of range!");
110       Info = (Info & 0xF0) | X;
111     }
112
113     void setVisibility(unsigned V) {
114       assert(V == (V & 0x3) && "Visibility value out of range!");
115       Other = V;
116     }
117   };
118
119   /// ELFSection - This struct contains information about each section that is
120   /// emitted to the file.  This is eventually turned into the section header
121   /// table at the end of the file.
122   class ELFSection : public BinaryObject {
123     public:
124     // ELF specific fields
125     unsigned NameIdx;   // sh_name - .shstrtab idx of name, once emitted.
126     unsigned Type;      // sh_type - Section contents & semantics 
127     unsigned Flags;     // sh_flags - Section flags.
128     uint64_t Addr;      // sh_addr - The mem addr this section is in.
129     unsigned Offset;    // sh_offset - Offset from the file start
130     unsigned Size;      // sh_size - The section size.
131     unsigned Link;      // sh_link - Section header table index link.
132     unsigned Info;      // sh_info - Auxillary information.
133     unsigned Align;     // sh_addralign - Alignment of section.
134     unsigned EntSize;   // sh_entsize - Size of entries in the section e
135
136     // Section Header Flags
137     enum {
138       SHF_WRITE            = 1 << 0, // Writable
139       SHF_ALLOC            = 1 << 1, // Mapped into the process addr space
140       SHF_EXECINSTR        = 1 << 2, // Executable
141       SHF_MERGE            = 1 << 4, // Might be merged if equal
142       SHF_STRINGS          = 1 << 5, // Contains null-terminated strings
143       SHF_INFO_LINK        = 1 << 6, // 'sh_info' contains SHT index
144       SHF_LINK_ORDER       = 1 << 7, // Preserve order after combining
145       SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
146       SHF_GROUP            = 1 << 9, // Section is a member of a group
147       SHF_TLS              = 1 << 10 // Section holds thread-local data
148     };
149
150     // Section Types
151     enum {
152       SHT_NULL     = 0,  // No associated section (inactive entry).
153       SHT_PROGBITS = 1,  // Program-defined contents.
154       SHT_SYMTAB   = 2,  // Symbol table.
155       SHT_STRTAB   = 3,  // String table.
156       SHT_RELA     = 4,  // Relocation entries; explicit addends.
157       SHT_HASH     = 5,  // Symbol hash table.
158       SHT_DYNAMIC  = 6,  // Information for dynamic linking.
159       SHT_NOTE     = 7,  // Information about the file.
160       SHT_NOBITS   = 8,  // Data occupies no space in the file.
161       SHT_REL      = 9,  // Relocation entries; no explicit addends.
162       SHT_SHLIB    = 10, // Reserved.
163       SHT_DYNSYM   = 11, // Symbol table.
164       SHT_LOPROC   = 0x70000000, // Lowest processor arch-specific type.
165       SHT_HIPROC   = 0x7fffffff, // Highest processor arch-specific type.
166       SHT_LOUSER   = 0x80000000, // Lowest type reserved for applications.
167       SHT_HIUSER   = 0xffffffff  // Highest type reserved for applications.
168     };
169
170     // Special section indices.
171     enum {
172       SHN_UNDEF     = 0,      // Undefined, missing, irrelevant
173       SHN_LORESERVE = 0xff00, // Lowest reserved index
174       SHN_LOPROC    = 0xff00, // Lowest processor-specific index
175       SHN_HIPROC    = 0xff1f, // Highest processor-specific index
176       SHN_ABS       = 0xfff1, // Symbol has absolute value; no relocation
177       SHN_COMMON    = 0xfff2, // FORTRAN COMMON or C external global variables
178       SHN_HIRESERVE = 0xffff  // Highest reserved index
179     };
180
181     /// SectionIdx - The number of the section in the Section Table.
182     unsigned short SectionIdx;
183
184     /// Sym - The symbol to represent this section if it has one.
185     ELFSym *Sym;
186
187     ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
188       : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
189         Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0), Sym(0) {}
190   };
191
192   /// ELFRelocation - This class contains all the information necessary to
193   /// to generate any 32-bit or 64-bit ELF relocation entry.
194   class ELFRelocation {
195     uint64_t r_offset;    // offset in the section of the object this applies to
196     uint32_t r_symidx;    // symbol table index of the symbol to use
197     uint32_t r_type;      // machine specific relocation type
198     int64_t  r_add;       // explicit relocation addend
199     bool     r_rela;      // if true then the addend is part of the entry
200                           // otherwise the addend is at the location specified
201                           // by r_offset
202   public:
203     uint64_t getInfo(bool is64Bit) const {
204       if (is64Bit)
205         return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL);
206       else
207         return (r_symidx << 8)  + (r_type & 0xFFL);
208     }
209
210     uint64_t getOffset() const { return r_offset; }
211     int64_t getAddend() const { return r_add; }
212
213     ELFRelocation(uint64_t off, uint32_t sym, uint32_t type,
214                   bool rela = true, int64_t addend = 0) :
215       r_offset(off), r_symidx(sym), r_type(type),
216       r_add(addend), r_rela(rela) {}
217   };
218
219 } // end namespace llvm
220
221 #endif