add a new MCInstPrinter class, move the (trivial) MCDisassmbler ctor inline.
[llvm/avr.git] / lib / CodeGen / ELFWriter.h
blobe44ab3f838ec04df7dbe9bb9a256b4684ca8ed95
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.
12 //===----------------------------------------------------------------------===//
14 #ifndef ELFWRITER_H
15 #define ELFWRITER_H
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include <map>
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 MCAsmInfo;
36 class TargetELFWriterInfo;
37 class TargetLoweringObjectFile;
38 class raw_ostream;
39 class SectionKind;
40 class MCContext;
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;
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;
57 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
58 ObjectCodeEmitter *getObjectCodeEmitter() {
59 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
62 ELFWriter(raw_ostream &O, TargetMachine &TM);
63 ~ELFWriter();
65 protected:
66 /// Output stream to send the resultant object file to.
67 raw_ostream &O;
69 /// Target machine description.
70 TargetMachine &TM;
72 /// Context object for machine code objects.
73 MCContext &OutContext;
75 /// Target Elf Writer description.
76 const TargetELFWriterInfo *TEW;
78 /// Mang - The object used to perform name mangling for this module.
79 Mangler *Mang;
81 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
82 /// code for functions to the .o file.
83 ELFCodeEmitter *ElfCE;
85 /// TLOF - Target Lowering Object File, provide section names for globals
86 /// and other object file specific stuff
87 const TargetLoweringObjectFile &TLOF;
89 /// MAI - Target Asm Info, provide information about section names for
90 /// globals and other target specific stuff.
91 const MCAsmInfo *MAI;
93 //===------------------------------------------------------------------===//
94 // Properties inferred automatically from the target machine.
95 //===------------------------------------------------------------------===//
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;
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);
106 /// doFinalization - Now that the module has been completely processed, emit
107 /// the ELF file to 'O'.
108 bool doFinalization(Module &M);
110 private:
111 /// Blob containing the Elf header
112 BinaryObject ElfHdr;
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()
120 /// SectionLookup - This is a mapping from section name to section number in
121 /// the SectionList. Used to quickly gather the Section Index from MAI names
122 std::map<std::string, ELFSection*> SectionLookup;
124 /// PendingGlobals - Globals not processed as symbols yet.
125 SetVector<const GlobalValue*> PendingGlobals;
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;
132 /// PendingExternals - Externals not processed as symbols yet.
133 SetVector<const char *> PendingExternals;
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;
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;
145 /// PrivateSyms - Record private symbols, every symbol here must never be
146 /// present in the SymbolList.
147 std::vector<ELFSym*> PrivateSyms;
149 /// getSection - Return the section with the specified name, creating a new
150 /// section if one does not already exist.
151 ELFSection &getSection(const std::string &Name, unsigned Type,
152 unsigned Flags = 0, unsigned Align = 0) {
153 ELFSection *&SN = SectionLookup[Name];
154 if (SN) return *SN;
156 SectionList.push_back(new ELFSection(Name, isLittleEndian, is64Bit));
157 SN = SectionList.back();
158 SN->SectionIdx = NumSections++;
159 SN->Type = Type;
160 SN->Flags = Flags;
161 SN->Link = ELFSection::SHN_UNDEF;
162 SN->Align = Align;
163 return *SN;
166 ELFSection &getNonExecStackSection() {
167 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
170 ELFSection &getSymbolTableSection() {
171 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
174 ELFSection &getStringTableSection() {
175 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
178 ELFSection &getSectionHeaderStringTableSection() {
179 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
182 ELFSection &getNullSection() {
183 return getSection("", ELFSection::SHT_NULL, 0);
186 ELFSection &getDataSection();
187 ELFSection &getBSSSection();
188 ELFSection &getCtorSection();
189 ELFSection &getDtorSection();
190 ELFSection &getJumpTableSection();
191 ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
192 ELFSection &getTextSection(Function *F);
193 ELFSection &getRelocSection(ELFSection &S);
195 // Helpers for obtaining ELF specific info.
196 unsigned getGlobalELFBinding(const GlobalValue *GV);
197 unsigned getGlobalELFType(const GlobalValue *GV);
198 unsigned getGlobalELFVisibility(const GlobalValue *GV);
200 // AddPendingGlobalSymbol - Add a global to be processed and to
201 // the global symbol lookup, use a zero index because the table
202 // index will be determined later.
203 void AddPendingGlobalSymbol(const GlobalValue *GV,
204 bool AddToLookup = false);
206 // AddPendingExternalSymbol - Add the external to be processed
207 // and to the external symbol lookup, use a zero index because
208 // the symbol table index will be determined later.
209 void AddPendingExternalSymbol(const char *External);
211 // AddToSymbolList - Update the symbol lookup and If the symbol is
212 // private add it to PrivateSyms list, otherwise to SymbolList.
213 void AddToSymbolList(ELFSym *GblSym);
215 // As we complete the ELF file, we need to update fields in the ELF header
216 // (e.g. the location of the section table). These members keep track of
217 // the offset in ELFHeader of these various pieces to update and other
218 // locations in the file.
219 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
220 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
221 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
223 private:
224 void EmitGlobal(const GlobalValue *GV);
225 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
226 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
227 ELFSection &GblS);
228 void EmitGlobalConstantLargeInt(const ConstantInt *CI, ELFSection &S);
229 void EmitGlobalDataRelocation(const GlobalValue *GV, unsigned Size,
230 ELFSection &GblS, int64_t Offset = 0);
231 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
232 void EmitXXStructorList(Constant *List, ELFSection &Xtor);
233 void EmitRelocations();
234 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
235 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
236 void EmitSectionTableStringTable();
237 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
238 void EmitSymbolTable();
239 void EmitStringTable(const std::string &ModuleName);
240 void OutputSectionsAndSectionTable();
241 void RelocateField(BinaryObject &BO, uint32_t Offset, int64_t Value,
242 unsigned Size);
243 unsigned SortSymbols();
244 CstExprResTy ResolveConstantExpr(const Constant *CV);
248 #endif