The accumulator tail recursion transform claims to work for any associative
[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 is distributed under the University of Illinois Open Source
6 // 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/ADT/SetVector.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include <map>
20
21 namespace llvm {
22   class BinaryObject;
23   class Constant;
24   class ConstantInt;
25   class ConstantStruct;
26   class ELFCodeEmitter;
27   class ELFRelocation;
28   class ELFSection;
29   struct ELFSym;
30   class GlobalVariable;
31   class JITDebugRegisterer;
32   class Mangler;
33   class MachineCodeEmitter;
34   class MachineConstantPoolEntry;
35   class ObjectCodeEmitter;
36   class MCAsmInfo;
37   class TargetELFWriterInfo;
38   class TargetLoweringObjectFile;
39   class raw_ostream;
40   class SectionKind;
41   class MCContext;
42
43   typedef std::vector<ELFSym*>::iterator ELFSymIter;
44   typedef std::vector<ELFSection*>::iterator ELFSectionIter;
45   typedef SetVector<const GlobalValue*>::const_iterator PendingGblsIter;
46   typedef SetVector<const char *>::const_iterator PendingExtsIter;
47   typedef std::pair<const Constant *, int64_t> CstExprResTy;
48
49   /// ELFWriter - This class implements the common target-independent code for
50   /// writing ELF files.  Targets should derive a class from this to
51   /// parameterize the output format.
52   ///
53   class ELFWriter : public MachineFunctionPass {
54     friend class ELFCodeEmitter;
55     friend class JITDebugRegisterer;
56   public:
57     static char ID;
58
59     /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
60     ObjectCodeEmitter *getObjectCodeEmitter() {
61       return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
62     }
63
64     ELFWriter(raw_ostream &O, TargetMachine &TM);
65     ~ELFWriter();
66
67   protected:
68     /// Output stream to send the resultant object file to.
69     raw_ostream &O;
70
71     /// Target machine description.
72     TargetMachine &TM;
73
74     /// Context object for machine code objects.
75     MCContext &OutContext;
76     
77     /// Target Elf Writer description.
78     const TargetELFWriterInfo *TEW;
79
80     /// Mang - The object used to perform name mangling for this module.
81     Mangler *Mang;
82
83     /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
84     /// code for functions to the .o file.
85     ELFCodeEmitter *ElfCE;
86
87     /// TLOF - Target Lowering Object File, provide section names for globals 
88     /// and other object file specific stuff
89     const TargetLoweringObjectFile &TLOF;
90
91     /// MAI - Target Asm Info, provide information about section names for
92     /// globals and other target specific stuff.
93     const MCAsmInfo *MAI;
94
95     //===------------------------------------------------------------------===//
96     // Properties inferred automatically from the target machine.
97     //===------------------------------------------------------------------===//
98
99     /// is64Bit/isLittleEndian - This information is inferred from the target
100     /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
101     bool is64Bit, isLittleEndian;
102
103     /// doInitialization - Emit the file header and all of the global variables
104     /// for the module to the ELF file.
105     bool doInitialization(Module &M);
106     bool runOnMachineFunction(MachineFunction &MF);
107
108     /// doFinalization - Now that the module has been completely processed, emit
109     /// the ELF file to 'O'.
110     bool doFinalization(Module &M);
111
112   private:
113     /// Blob containing the Elf header
114     BinaryObject ElfHdr;
115
116     /// SectionList - This is the list of sections that we have emitted to the
117     /// file. Once the file has been completely built, the section header table
118     /// is constructed from this info.
119     std::vector<ELFSection*> SectionList;
120     unsigned NumSections;   // Always = SectionList.size()
121
122     /// SectionLookup - This is a mapping from section name to section number in
123     /// the SectionList. Used to quickly gather the Section Index from MAI names
124     std::map<std::string, ELFSection*> SectionLookup;
125
126     /// PendingGlobals - Globals not processed as symbols yet.
127     SetVector<const GlobalValue*> PendingGlobals;
128
129     /// GblSymLookup - This is a mapping from global value to a symbol index
130     /// in the symbol table or private symbols list. This is useful since reloc
131     /// symbol references must be quickly mapped to their indices on the lists.
132     std::map<const GlobalValue*, uint32_t> GblSymLookup;
133
134     /// PendingExternals - Externals not processed as symbols yet.
135     SetVector<const char *> PendingExternals;
136
137     /// ExtSymLookup - This is a mapping from externals to a symbol index
138     /// in the symbol table list. This is useful since reloc symbol references
139     /// must be quickly mapped to their symbol table indices.
140     std::map<const char *, uint32_t> ExtSymLookup;
141
142     /// SymbolList - This is the list of symbols emitted to the symbol table.
143     /// When the SymbolList is finally built, local symbols must be placed in
144     /// the beginning while non-locals at the end.
145     std::vector<ELFSym*> SymbolList;
146
147     /// PrivateSyms - Record private symbols, every symbol here must never be
148     /// present in the SymbolList.
149     std::vector<ELFSym*> PrivateSyms;
150
151     /// getSection - Return the section with the specified name, creating a new
152     /// section if one does not already exist.
153     ELFSection &getSection(const std::string &Name, unsigned Type,
154                            unsigned Flags = 0, unsigned Align = 0) {
155       ELFSection *&SN = SectionLookup[Name];
156       if (SN) return *SN;
157
158       SectionList.push_back(new ELFSection(Name, isLittleEndian, is64Bit));
159       SN = SectionList.back();
160       SN->SectionIdx = NumSections++;
161       SN->Type = Type;
162       SN->Flags = Flags;
163       SN->Link = ELFSection::SHN_UNDEF;
164       SN->Align = Align;
165       return *SN;
166     }
167
168     ELFSection &getNonExecStackSection() {
169       return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
170     }
171
172     ELFSection &getSymbolTableSection() {
173       return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
174     }
175
176     ELFSection &getStringTableSection() {
177       return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
178     }
179
180     ELFSection &getSectionHeaderStringTableSection() {
181       return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
182     }
183
184     ELFSection &getNullSection() {
185       return getSection("", ELFSection::SHT_NULL, 0);
186     }
187
188     ELFSection &getDataSection();
189     ELFSection &getBSSSection();
190     ELFSection &getCtorSection();
191     ELFSection &getDtorSection();
192     ELFSection &getJumpTableSection();
193     ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
194     ELFSection &getTextSection(const Function *F);
195     ELFSection &getRelocSection(ELFSection &S);
196
197     // Helpers for obtaining ELF specific info.
198     unsigned getGlobalELFBinding(const GlobalValue *GV);
199     unsigned getGlobalELFType(const GlobalValue *GV);
200     unsigned getGlobalELFVisibility(const GlobalValue *GV);
201
202     // AddPendingGlobalSymbol - Add a global to be processed and to
203     // the global symbol lookup, use a zero index because the table
204     // index will be determined later.
205     void AddPendingGlobalSymbol(const GlobalValue *GV, 
206                                 bool AddToLookup = false);
207     
208     // AddPendingExternalSymbol - Add the external to be processed
209     // and to the external symbol lookup, use a zero index because
210     // the symbol table index will be determined later.
211     void AddPendingExternalSymbol(const char *External);
212
213     // AddToSymbolList - Update the symbol lookup and If the symbol is 
214     // private add it to PrivateSyms list, otherwise to SymbolList. 
215     void AddToSymbolList(ELFSym *GblSym);
216
217     // As we complete the ELF file, we need to update fields in the ELF header
218     // (e.g. the location of the section table).  These members keep track of
219     // the offset in ELFHeader of these various pieces to update and other
220     // locations in the file.
221     unsigned ELFHdr_e_shoff_Offset;     // e_shoff    in ELF header.
222     unsigned ELFHdr_e_shstrndx_Offset;  // e_shstrndx in ELF header.
223     unsigned ELFHdr_e_shnum_Offset;     // e_shnum    in ELF header.
224
225   private:
226     void EmitGlobal(const GlobalValue *GV);
227     void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
228     void EmitGlobalConstantStruct(const ConstantStruct *CVS,
229                                   ELFSection &GblS);
230     void EmitGlobalConstantLargeInt(const ConstantInt *CI, ELFSection &S);
231     void EmitGlobalDataRelocation(const GlobalValue *GV, unsigned Size, 
232                                   ELFSection &GblS, int64_t Offset = 0);
233     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
234     void EmitXXStructorList(Constant *List, ELFSection &Xtor);
235     void EmitRelocations();
236     void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
237     void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
238     void EmitSectionTableStringTable();
239     void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
240     void EmitSymbolTable();
241     void EmitStringTable(const std::string &ModuleName);
242     void OutputSectionsAndSectionTable();
243     void RelocateField(BinaryObject &BO, uint32_t Offset, int64_t Value,
244                        unsigned Size);
245     unsigned SortSymbols();
246     CstExprResTy ResolveConstantExpr(const Constant *CV);
247   };
248 }
249
250 #endif