1 //===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ELFWriter class.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
32 class MachineCodeEmitter;
33 class MachineConstantPoolEntry;
34 class ObjectCodeEmitter;
36 class TargetELFWriterInfo;
37 class TargetLoweringObjectFile;
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.
52 class ELFWriter : public MachineFunctionPass {
53 friend class ELFCodeEmitter;
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);
66 /// Output stream to send the resultant object file to.
69 /// Target machine description.
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.
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 /// TAI - Target Asm Info, provide information about section names for
90 /// globals and other target specific stuff.
91 const TargetAsmInfo *TAI;
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);
111 /// Blob containing the Elf header
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 TAI 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 // 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)
158 Pos = Name.find(".section ");
159 if (Pos != std::string::npos)
162 Pos = Name.find("\n");
163 if (Pos != std::string::npos)
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);
174 ELFSection *&SN = SectionLookup[SName];
177 SectionList.push_back(new ELFSection(SName, isLittleEndian, is64Bit));
178 SN = SectionList.back();
179 SN->SectionIdx = NumSections++;
182 SN->Link = ELFSection::SHN_UNDEF;
187 ELFSection &getNonExecStackSection() {
188 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
191 ELFSection &getSymbolTableSection() {
192 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
195 ELFSection &getStringTableSection() {
196 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
199 ELFSection &getSectionHeaderStringTableSection() {
200 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
203 ELFSection &getDataSection() {
204 return getSection(".data", ELFSection::SHT_PROGBITS,
205 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
208 ELFSection &getBSSSection() {
209 return getSection(".bss", ELFSection::SHT_NOBITS,
210 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
213 ELFSection &getNullSection() {
214 return getSection("", ELFSection::SHT_NULL, 0);
217 ELFSection &getCtorSection();
218 ELFSection &getDtorSection();
219 ELFSection &getJumpTableSection();
220 ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
221 ELFSection &getTextSection(Function *F);
222 ELFSection &getRelocSection(ELFSection &S);
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);
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);
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);
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.
249 void EmitGlobal(const GlobalValue *GV);
250 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
251 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
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,
268 unsigned SortSymbols();
269 CstExprResTy ResolveConstantExpr(const Constant *CV);