Introduce new BinaryObject (blob) class, ELF Writer modified to use it. BinaryObject...
[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     const GlobalValue *GV;    // The global value this corresponds to.
132
133     // ELF specific fields
134     unsigned NameIdx;         // Index in .strtab of name, once emitted.
135     uint64_t Value;
136     unsigned Size;
137     uint8_t Info;
138     uint8_t Other;
139     unsigned short SectionIdx;
140
141     enum { 
142       STB_LOCAL = 0,
143       STB_GLOBAL = 1,
144       STB_WEAK = 2 
145     };
146
147     enum { 
148       STT_NOTYPE = 0,
149       STT_OBJECT = 1,
150       STT_FUNC = 2,
151       STT_SECTION = 3,
152       STT_FILE = 4 
153     };
154
155     enum {
156       STV_DEFAULT = 0,  // Visibility is specified by binding type
157       STV_INTERNAL = 1, // Defined by processor supplements
158       STV_HIDDEN = 2,   // Not visible to other components
159       STV_PROTECTED = 3 // Visible in other components but not preemptable
160     };
161
162     ELFSym(const GlobalValue *gv) : GV(gv), NameIdx(0), Value(0),
163                                     Size(0), Info(0), Other(0),
164                                     SectionIdx(ELFSection::SHN_UNDEF) {
165       if (!GV)
166         return;
167
168       switch (GV->getVisibility()) {
169       default:
170         assert(0 && "unknown visibility type");
171       case GlobalValue::DefaultVisibility:
172         Other = STV_DEFAULT;
173         break;
174       case GlobalValue::HiddenVisibility:
175         Other = STV_HIDDEN;
176         break;
177       case GlobalValue::ProtectedVisibility:
178         Other = STV_PROTECTED;
179         break;
180       }
181     }
182
183     void SetBind(unsigned X) {
184       assert(X == (X & 0xF) && "Bind value out of range!");
185       Info = (Info & 0x0F) | (X << 4);
186     }
187     void SetType(unsigned X) {
188       assert(X == (X & 0xF) && "Type value out of range!");
189       Info = (Info & 0xF0) | X;
190     }
191   };
192
193 } // end namespace llvm
194
195 #endif