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