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 /// 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
];
156 SectionList
.push_back(new ELFSection(Name
, isLittleEndian
, is64Bit
));
157 SN
= SectionList
.back();
158 SN
->SectionIdx
= NumSections
++;
161 SN
->Link
= ELFSection::SHN_UNDEF
;
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.
224 void EmitGlobal(const GlobalValue
*GV
);
225 void EmitGlobalConstant(const Constant
*C
, ELFSection
&GblS
);
226 void EmitGlobalConstantStruct(const ConstantStruct
*CVS
,
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
,
243 unsigned SortSymbols();
244 CstExprResTy
ResolveConstantExpr(const Constant
*CV
);