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