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"
31
class JITDebugRegisterer;
33
class MachineCodeEmitter;
34
class MachineConstantPoolEntry;
35
class ObjectCodeEmitter;
37
class TargetELFWriterInfo;
38
class TargetLoweringObjectFile;
43
typedef std::vector<ELFSym*>::iterator ELFSymIter;
44
typedef std::vector<ELFSection*>::iterator ELFSectionIter;
45
typedef SetVector<const GlobalValue*>::const_iterator PendingGblsIter;
46
typedef SetVector<const char *>::const_iterator PendingExtsIter;
47
typedef std::pair<const Constant *, int64_t> CstExprResTy;
49
/// ELFWriter - This class implements the common target-independent code for
50
/// writing ELF files. Targets should derive a class from this to
51
/// parameterize the output format.
53
class ELFWriter : public MachineFunctionPass {
54
friend class ELFCodeEmitter;
55
friend class JITDebugRegisterer;
59
/// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
60
ObjectCodeEmitter *getObjectCodeEmitter() {
61
return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
64
ELFWriter(raw_ostream &O, TargetMachine &TM);
68
/// Output stream to send the resultant object file to.
71
/// Target machine description.
74
/// Context object for machine code objects.
75
MCContext &OutContext;
77
/// Target Elf Writer description.
78
const TargetELFWriterInfo *TEW;
80
/// Mang - The object used to perform name mangling for this module.
83
/// MCE - The MachineCodeEmitter object that we are exposing to emit machine
84
/// code for functions to the .o file.
85
ELFCodeEmitter *ElfCE;
87
/// TLOF - Target Lowering Object File, provide section names for globals
88
/// and other object file specific stuff
89
const TargetLoweringObjectFile &TLOF;
91
/// MAI - Target Asm Info, provide information about section names for
92
/// globals and other target specific stuff.
95
//===------------------------------------------------------------------===//
96
// Properties inferred automatically from the target machine.
97
//===------------------------------------------------------------------===//
99
/// is64Bit/isLittleEndian - This information is inferred from the target
100
/// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
101
bool is64Bit, isLittleEndian;
103
/// doInitialization - Emit the file header and all of the global variables
104
/// for the module to the ELF file.
105
bool doInitialization(Module &M);
106
bool runOnMachineFunction(MachineFunction &MF);
108
/// doFinalization - Now that the module has been completely processed, emit
109
/// the ELF file to 'O'.
110
bool doFinalization(Module &M);
113
/// Blob containing the Elf header
116
/// SectionList - This is the list of sections that we have emitted to the
117
/// file. Once the file has been completely built, the section header table
118
/// is constructed from this info.
119
std::vector<ELFSection*> SectionList;
120
unsigned NumSections; // Always = SectionList.size()
122
/// SectionLookup - This is a mapping from section name to section number in
123
/// the SectionList. Used to quickly gather the Section Index from MAI names
124
std::map<std::string, ELFSection*> SectionLookup;
126
/// PendingGlobals - Globals not processed as symbols yet.
127
SetVector<const GlobalValue*> PendingGlobals;
129
/// GblSymLookup - This is a mapping from global value to a symbol index
130
/// in the symbol table or private symbols list. This is useful since reloc
131
/// symbol references must be quickly mapped to their indices on the lists.
132
std::map<const GlobalValue*, uint32_t> GblSymLookup;
134
/// PendingExternals - Externals not processed as symbols yet.
135
SetVector<const char *> PendingExternals;
137
/// ExtSymLookup - This is a mapping from externals to a symbol index
138
/// in the symbol table list. This is useful since reloc symbol references
139
/// must be quickly mapped to their symbol table indices.
140
std::map<const char *, uint32_t> ExtSymLookup;
142
/// SymbolList - This is the list of symbols emitted to the symbol table.
143
/// When the SymbolList is finally built, local symbols must be placed in
144
/// the beginning while non-locals at the end.
145
std::vector<ELFSym*> SymbolList;
147
/// PrivateSyms - Record private symbols, every symbol here must never be
148
/// present in the SymbolList.
149
std::vector<ELFSym*> PrivateSyms;
151
/// getSection - Return the section with the specified name, creating a new
152
/// section if one does not already exist.
153
ELFSection &getSection(const std::string &Name, unsigned Type,
154
unsigned Flags = 0, unsigned Align = 0) {
155
ELFSection *&SN = SectionLookup[Name];
158
SectionList.push_back(new ELFSection(Name, isLittleEndian, is64Bit));
159
SN = SectionList.back();
160
SN->SectionIdx = NumSections++;
163
SN->Link = ELFSection::SHN_UNDEF;
168
ELFSection &getNonExecStackSection() {
169
return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
172
ELFSection &getSymbolTableSection() {
173
return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
176
ELFSection &getStringTableSection() {
177
return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
180
ELFSection &getSectionHeaderStringTableSection() {
181
return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
184
ELFSection &getNullSection() {
185
return getSection("", ELFSection::SHT_NULL, 0);
188
ELFSection &getDataSection();
189
ELFSection &getBSSSection();
190
ELFSection &getCtorSection();
191
ELFSection &getDtorSection();
192
ELFSection &getJumpTableSection();
193
ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
194
ELFSection &getTextSection(Function *F);
195
ELFSection &getRelocSection(ELFSection &S);
197
// Helpers for obtaining ELF specific info.
198
unsigned getGlobalELFBinding(const GlobalValue *GV);
199
unsigned getGlobalELFType(const GlobalValue *GV);
200
unsigned getGlobalELFVisibility(const GlobalValue *GV);
202
// AddPendingGlobalSymbol - Add a global to be processed and to
203
// the global symbol lookup, use a zero index because the table
204
// index will be determined later.
205
void AddPendingGlobalSymbol(const GlobalValue *GV,
206
bool AddToLookup = false);
208
// AddPendingExternalSymbol - Add the external to be processed
209
// and to the external symbol lookup, use a zero index because
210
// the symbol table index will be determined later.
211
void AddPendingExternalSymbol(const char *External);
213
// AddToSymbolList - Update the symbol lookup and If the symbol is
214
// private add it to PrivateSyms list, otherwise to SymbolList.
215
void AddToSymbolList(ELFSym *GblSym);
217
// As we complete the ELF file, we need to update fields in the ELF header
218
// (e.g. the location of the section table). These members keep track of
219
// the offset in ELFHeader of these various pieces to update and other
220
// locations in the file.
221
unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
222
unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
223
unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
226
void EmitGlobal(const GlobalValue *GV);
227
void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
228
void EmitGlobalConstantStruct(const ConstantStruct *CVS,
230
void EmitGlobalConstantLargeInt(const ConstantInt *CI, ELFSection &S);
231
void EmitGlobalDataRelocation(const GlobalValue *GV, unsigned Size,
232
ELFSection &GblS, int64_t Offset = 0);
233
bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
234
void EmitXXStructorList(Constant *List, ELFSection &Xtor);
235
void EmitRelocations();
236
void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
237
void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
238
void EmitSectionTableStringTable();
239
void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
240
void EmitSymbolTable();
241
void EmitStringTable(const std::string &ModuleName);
242
void OutputSectionsAndSectionTable();
243
void RelocateField(BinaryObject &BO, uint32_t Offset, int64_t Value,
245
unsigned SortSymbols();
246
CstExprResTy ResolveConstantExpr(const Constant *CV);