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