Moved from include/llvm/CodeGen to lib/CodeGen.
[oota-llvm.git] / lib / CodeGen / ELFWriter.h
1 //===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ELFWriter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef ELFWRITER_H
15 #define ELFWRITER_H
16
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include <list>
19
20 namespace llvm {
21   class GlobalVariable;
22   class Mangler;
23   class MachineCodeEmitter;
24   class ELFCodeEmitter;
25
26   /// ELFWriter - This class implements the common target-independent code for
27   /// writing ELF files.  Targets should derive a class from this to
28   /// parameterize the output format.
29   ///
30   class ELFWriter : public MachineFunctionPass {
31     friend class ELFCodeEmitter;
32   public:
33     MachineCodeEmitter &getMachineCodeEmitter() const {
34       return *(MachineCodeEmitter*)MCE;
35     }
36
37     ELFWriter(std::ostream &O, TargetMachine &TM);
38     ~ELFWriter();
39
40     typedef std::vector<unsigned char> DataBuffer;
41
42   protected:
43     /// Output stream to send the resultant object file to.
44     ///
45     std::ostream &O;
46
47     /// Target machine description.
48     ///
49     TargetMachine &TM;
50
51     /// Mang - The object used to perform name mangling for this module.
52     ///
53     Mangler *Mang;
54
55     /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
56     /// code for functions to the .o file.
57     ELFCodeEmitter *MCE;
58
59     //===------------------------------------------------------------------===//
60     // Properties to be set by the derived class ctor, used to configure the
61     // ELFWriter.
62
63     // e_machine - This field is the target specific value to emit as the
64     // e_machine member of the ELF header.
65     unsigned short e_machine;
66
67     // e_flags - The machine flags for the target.  This defaults to zero.
68     unsigned e_flags;
69
70     //===------------------------------------------------------------------===//
71     // Properties inferred automatically from the target machine.
72     //
73
74     /// is64Bit/isLittleEndian - This information is inferred from the target
75     /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
76     bool is64Bit, isLittleEndian;
77
78     /// doInitialization - Emit the file header and all of the global variables
79     /// for the module to the ELF file.
80     bool doInitialization(Module &M);
81
82     bool runOnMachineFunction(MachineFunction &MF);
83
84
85     /// doFinalization - Now that the module has been completely processed, emit
86     /// the ELF file to 'O'.
87     bool doFinalization(Module &M);
88
89   private:
90     // The buffer we accumulate the file header into.  Note that this should be
91     // changed into something much more efficient later (and the bytecode writer
92     // as well!).
93     DataBuffer FileHeader;
94
95     /// ELFSection - This struct contains information about each section that is
96     /// emitted to the file.  This is eventually turned into the section header
97     /// table at the end of the file.
98     struct ELFSection {
99       std::string Name;       // Name of the section.
100       unsigned NameIdx;       // Index in .shstrtab of name, once emitted.
101       unsigned Type;
102       unsigned Flags;
103       uint64_t Addr;
104       unsigned Offset;
105       unsigned Size;
106       unsigned Link;
107       unsigned Info;
108       unsigned Align;
109       unsigned EntSize;
110
111       /// SectionIdx - The number of the section in the Section Table.
112       ///
113       unsigned short SectionIdx;
114
115       /// SectionData - The actual data for this section which we are building
116       /// up for emission to the file.
117       DataBuffer SectionData;
118
119       enum { SHT_NULL = 0, SHT_PROGBITS = 1, SHT_SYMTAB = 2, SHT_STRTAB = 3,
120              SHT_RELA = 4, SHT_HASH = 5, SHT_DYNAMIC = 6, SHT_NOTE = 7,
121              SHT_NOBITS = 8, SHT_REL = 9, SHT_SHLIB = 10, SHT_DYNSYM = 11 };
122       enum { SHN_UNDEF = 0, SHN_ABS = 0xFFF1, SHN_COMMON = 0xFFF2 };
123       enum {   // SHF - ELF Section Header Flags
124         SHF_WRITE            = 1 << 0, // Writable
125         SHF_ALLOC            = 1 << 1, // Mapped into the process addr space
126         SHF_EXECINSTR        = 1 << 2, // Executable
127         SHF_MERGE            = 1 << 4, // Might be merged if equal
128         SHF_STRINGS          = 1 << 5, // Contains null-terminated strings
129         SHF_INFO_LINK        = 1 << 6, // 'sh_info' contains SHT index
130         SHF_LINK_ORDER       = 1 << 7, // Preserve order after combining
131         SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
132         SHF_GROUP            = 1 << 9, // Section is a member of a group
133         SHF_TLS              = 1 << 10 // Section holds thread-local data
134       };
135
136       ELFSection(const std::string &name)
137         : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0),
138           Link(0), Info(0), Align(0), EntSize(0) {
139       }
140     };
141
142     /// SectionList - This is the list of sections that we have emitted to the
143     /// file.  Once the file has been completely built, the section header table
144     /// is constructed from this info.
145     std::list<ELFSection> SectionList;
146     unsigned NumSections;   // Always = SectionList.size()
147
148     /// SectionLookup - This is a mapping from section name to section number in
149     /// the SectionList.
150     std::map<std::string, ELFSection*> SectionLookup;
151
152     /// getSection - Return the section with the specified name, creating a new
153     /// section if one does not already exist.
154     ELFSection &getSection(const std::string &Name,
155                            unsigned Type, unsigned Flags = 0) {
156       ELFSection *&SN = SectionLookup[Name];
157       if (SN) return *SN;
158
159       SectionList.push_back(Name);
160       SN = &SectionList.back();
161       SN->SectionIdx = NumSections++;
162       SN->Type = Type;
163       SN->Flags = Flags;
164       return *SN;
165     }
166
167     ELFSection &getDataSection() {
168       return getSection(".data", ELFSection::SHT_PROGBITS,
169                         ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
170     }
171     ELFSection &getBSSSection() {
172       return getSection(".bss", ELFSection::SHT_NOBITS,
173                         ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
174     }
175
176     /// ELFSym - This struct contains information about each symbol that is
177     /// added to logical symbol table for the module.  This is eventually
178     /// turned into a real symbol table in the file.
179     struct ELFSym {
180       const GlobalValue *GV;    // The global value this corresponds to.
181       unsigned NameIdx;         // Index in .strtab of name, once emitted.
182       uint64_t Value;
183       unsigned Size;
184       unsigned char Info;
185       unsigned char Other;
186       unsigned short SectionIdx;
187
188       enum { STB_LOCAL = 0, STB_GLOBAL = 1, STB_WEAK = 2 };
189       enum { STT_NOTYPE = 0, STT_OBJECT = 1, STT_FUNC = 2, STT_SECTION = 3,
190              STT_FILE = 4 };
191       ELFSym(const GlobalValue *gv) : GV(gv), Value(0), Size(0), Info(0),
192                                       Other(0), SectionIdx(0) {}
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     /// SymbolTable - This is the list of symbols we have emitted to the file.
205     /// This actually gets rearranged before emission to the file (to put the
206     /// local symbols first in the list).
207     std::vector<ELFSym> SymbolTable;
208
209     // As we complete the ELF file, we need to update fields in the ELF header
210     // (e.g. the location of the section table).  These members keep track of
211     // the offset in ELFHeader of these various pieces to update and other
212     // locations in the file.
213     unsigned ELFHeader_e_shoff_Offset;     // e_shoff    in ELF header.
214     unsigned ELFHeader_e_shstrndx_Offset;  // e_shstrndx in ELF header.
215     unsigned ELFHeader_e_shnum_Offset;     // e_shnum    in ELF header.
216   private:
217     void EmitGlobal(GlobalVariable *GV);
218
219     void EmitSymbolTable();
220
221     void EmitSectionTableStringTable();
222     void OutputSectionsAndSectionTable();
223   };
224 }
225
226 #endif