shrinking down #includes
[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 <list>
20 #include <map>
21
22 namespace llvm {
23   class BinaryObject;
24   class Constant;
25   class ConstantStruct;
26   class ELFCodeEmitter;
27   class GlobalVariable;
28   class Mangler;
29   class MachineCodeEmitter;
30   class TargetAsmInfo;
31   class TargetELFWriterInfo;
32   class raw_ostream;
33   class ELFSection;
34   class ELFSym;
35   class ELFRelocation;
36
37   /// ELFWriter - This class implements the common target-independent code for
38   /// writing ELF files.  Targets should derive a class from this to
39   /// parameterize the output format.
40   ///
41   class ELFWriter : public MachineFunctionPass {
42     friend class ELFCodeEmitter;
43   public:
44     static char ID;
45
46     MachineCodeEmitter &getMachineCodeEmitter() const {
47       return *(MachineCodeEmitter*)MCE;
48     }
49
50     ELFWriter(raw_ostream &O, TargetMachine &TM);
51     ~ELFWriter();
52
53     typedef std::vector<unsigned char> DataBuffer;
54
55   protected:
56     /// Output stream to send the resultant object file to.
57     raw_ostream &O;
58
59     /// Target machine description.
60     TargetMachine &TM;
61
62     /// Target Elf Writer description.
63     const TargetELFWriterInfo *TEW;
64
65     /// Mang - The object used to perform name mangling for this module.
66     Mangler *Mang;
67
68     /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
69     /// code for functions to the .o file.
70     ELFCodeEmitter *MCE;
71
72     /// TAI - Target Asm Info, provide information about section names for
73     /// globals and other target specific stuff.
74     const TargetAsmInfo *TAI;
75
76     //===------------------------------------------------------------------===//
77     // Properties inferred automatically from the target machine.
78     //===------------------------------------------------------------------===//
79
80     /// is64Bit/isLittleEndian - This information is inferred from the target
81     /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
82     bool is64Bit, isLittleEndian;
83
84     /// doInitialization - Emit the file header and all of the global variables
85     /// for the module to the ELF file.
86     bool doInitialization(Module &M);
87     bool runOnMachineFunction(MachineFunction &MF);
88
89     /// doFinalization - Now that the module has been completely processed, emit
90     /// the ELF file to 'O'.
91     bool doFinalization(Module &M);
92
93   private:
94     /// Blob containing the Elf header
95     BinaryObject ElfHdr;
96
97     /// SectionList - This is the list of sections that we have emitted to the
98     /// file.  Once the file has been completely built, the section header table
99     /// is constructed from this info.
100     std::list<ELFSection> SectionList;
101     unsigned NumSections;   // Always = SectionList.size()
102
103     /// SectionLookup - This is a mapping from section name to section number in
104     /// the SectionList.
105     std::map<std::string, ELFSection*> SectionLookup;
106
107     /// GblSymLookup - This is a mapping from global value to a symbol index
108     /// in the symbol table. This is useful since relocations symbol references
109     /// must be quickly mapped to a symbol table index
110     std::map<const GlobalValue*, uint32_t> GblSymLookup;
111
112     /// SymbolList - This is the list of symbols emitted to the symbol table
113     /// Local symbols go to the front and Globals to the back.
114     std::list<ELFSym> SymbolList;
115
116     /// PendingGlobals - List of externally defined symbols that we have been
117     /// asked to emit, but have not seen a reference to.  When a reference
118     /// is seen, the symbol will move from this list to the SymbolList.
119     SetVector<GlobalValue*> PendingGlobals;
120
121     /// getSection - Return the section with the specified name, creating a new
122     /// section if one does not already exist.
123     ELFSection &getSection(const std::string &Name, unsigned Type,
124                            unsigned Flags = 0, unsigned Align = 0) {
125       ELFSection *&SN = SectionLookup[Name];
126       if (SN) return *SN;
127
128       // Remove tab from section name prefix. This is necessary becase TAI 
129       // sometimes return a section name prefixed with a "\t" char.
130       std::string SectionName(Name);
131       size_t Pos = SectionName.find("\t");
132       if (Pos != std::string::npos)
133         SectionName.erase(Pos, 1);
134
135       SectionList.push_back(ELFSection(SectionName, isLittleEndian, is64Bit));
136       SN = &SectionList.back();
137       SN->SectionIdx = NumSections++;
138       SN->Type = Type;
139       SN->Flags = Flags;
140       SN->Link = ELFSection::SHN_UNDEF;
141       SN->Align = Align;
142       return *SN;
143     }
144
145     /// TODO: support mangled names here to emit the right .text section
146     /// for c++ object files.
147     ELFSection &getTextSection() {
148       return getSection(".text", ELFSection::SHT_PROGBITS,
149                         ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
150     }
151
152     /// Get a constant pool section based on the section name returned by TAI
153     ELFSection &getConstantPoolSection(std::string SName, unsigned Align) {
154       return getSection(SName, ELFSection::SHT_PROGBITS,
155                         ELFSection::SHF_MERGE | ELFSection::SHF_ALLOC, Align);
156     }
157
158     /// Return the relocation section of section 'S'. 'RelA' is true
159     /// if the relocation section contains entries with addends.
160     ELFSection &getRelocSection(std::string SName, bool RelA, unsigned Align) {
161       std::string RelSName(".rel");
162       unsigned SHdrTy = RelA ? ELFSection::SHT_RELA : ELFSection::SHT_REL;
163
164       if (RelA) RelSName.append("a");
165       RelSName.append(SName);
166
167       return getSection(RelSName, SHdrTy, 0, Align);
168     }
169
170     ELFSection &getNonExecStackSection() {
171       return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
172     }
173
174     ELFSection &getSymbolTableSection() {
175       return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
176     }
177
178     ELFSection &getStringTableSection() {
179       return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
180     }
181
182     ELFSection &getSectionHeaderStringTableSection() {
183       return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
184     }
185
186     ELFSection &getDataSection() {
187       return getSection(".data", ELFSection::SHT_PROGBITS,
188                         ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
189     }
190
191     ELFSection &getBSSSection() {
192       return getSection(".bss", ELFSection::SHT_NOBITS,
193                         ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
194     }
195
196     ELFSection &getNullSection() {
197       return getSection("", ELFSection::SHT_NULL, 0);
198     }
199
200     // Helpers for obtaining ELF specific Linkage and Visibility info.
201     unsigned getGlobalELFLinkage(const GlobalValue *GV);
202     unsigned getGlobalELFVisibility(const GlobalValue *GV);
203
204     // As we complete the ELF file, we need to update fields in the ELF header
205     // (e.g. the location of the section table).  These members keep track of
206     // the offset in ELFHeader of these various pieces to update and other
207     // locations in the file.
208     unsigned ELFHdr_e_shoff_Offset;     // e_shoff    in ELF header.
209     unsigned ELFHdr_e_shstrndx_Offset;  // e_shstrndx in ELF header.
210     unsigned ELFHdr_e_shnum_Offset;     // e_shnum    in ELF header.
211
212   private:
213     void EmitFunctionDeclaration(const Function *F);
214     void EmitGlobalVar(const GlobalVariable *GV);
215     void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
216     void EmitGlobalConstantStruct(const ConstantStruct *CVS,
217                                   ELFSection &GblS);
218     ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym);
219     void EmitRelocations();
220     void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
221     void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
222     void EmitSectionTableStringTable();
223     void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
224     void EmitSymbolTable();
225     void EmitStringTable();
226     void OutputSectionsAndSectionTable();
227   };
228 }
229
230 #endif