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 MachineCodeEmitter
;
32 class MachineConstantPoolEntry
;
33 class ObjectCodeEmitter
;
35 class TargetELFWriterInfo
;
36 class TargetLoweringObjectFile
;
41 typedef std::vector
<ELFSym
*>::iterator ELFSymIter
;
42 typedef std::vector
<ELFSection
*>::iterator ELFSectionIter
;
43 typedef SetVector
<const GlobalValue
*>::const_iterator PendingGblsIter
;
44 typedef SetVector
<const char *>::const_iterator PendingExtsIter
;
46 /// ELFWriter - This class implements the common target-independent code for
47 /// writing ELF files. Targets should derive a class from this to
48 /// parameterize the output format.
50 class ELFWriter
: public MachineFunctionPass
{
51 friend class ELFCodeEmitter
;
55 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
56 ObjectCodeEmitter
*getObjectCodeEmitter() {
57 return reinterpret_cast<ObjectCodeEmitter
*>(ElfCE
);
60 ELFWriter(raw_ostream
&O
, TargetMachine
&TM
);
64 /// Output stream to send the resultant object file to.
67 /// Target machine description.
70 /// Context object for machine code objects.
71 MCContext
&OutContext
;
73 /// Target Elf Writer description.
74 const TargetELFWriterInfo
*TEW
;
76 /// Mang - The object used to perform name mangling for this module.
79 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
80 /// code for functions to the .o file.
81 ELFCodeEmitter
*ElfCE
;
83 /// TLOF - Target Lowering Object File, provide section names for globals
84 /// and other object file specific stuff
85 const TargetLoweringObjectFile
&TLOF
;
87 /// TAI - Target Asm Info, provide information about section names for
88 /// globals and other target specific stuff.
89 const TargetAsmInfo
*TAI
;
91 //===------------------------------------------------------------------===//
92 // Properties inferred automatically from the target machine.
93 //===------------------------------------------------------------------===//
95 /// is64Bit/isLittleEndian - This information is inferred from the target
96 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
97 bool is64Bit
, isLittleEndian
;
99 /// doInitialization - Emit the file header and all of the global variables
100 /// for the module to the ELF file.
101 bool doInitialization(Module
&M
);
102 bool runOnMachineFunction(MachineFunction
&MF
);
104 /// doFinalization - Now that the module has been completely processed, emit
105 /// the ELF file to 'O'.
106 bool doFinalization(Module
&M
);
109 /// Blob containing the Elf header
112 /// SectionList - This is the list of sections that we have emitted to the
113 /// file. Once the file has been completely built, the section header table
114 /// is constructed from this info.
115 std::vector
<ELFSection
*> SectionList
;
116 unsigned NumSections
; // Always = SectionList.size()
118 /// SectionLookup - This is a mapping from section name to section number in
119 /// the SectionList. Used to quickly gather the Section Index from TAI names
120 std::map
<std::string
, ELFSection
*> SectionLookup
;
122 /// PendingGlobals - Globals not processed as symbols yet.
123 SetVector
<const GlobalValue
*> PendingGlobals
;
125 /// GblSymLookup - This is a mapping from global value to a symbol index
126 /// in the symbol table or private symbols list. This is useful since reloc
127 /// symbol references must be quickly mapped to their indices on the lists.
128 std::map
<const GlobalValue
*, uint32_t> GblSymLookup
;
130 /// PendingExternals - Externals not processed as symbols yet.
131 SetVector
<const char *> PendingExternals
;
133 /// ExtSymLookup - This is a mapping from externals to a symbol index
134 /// in the symbol table list. This is useful since reloc symbol references
135 /// must be quickly mapped to their symbol table indices.
136 std::map
<const char *, uint32_t> ExtSymLookup
;
138 /// SymbolList - This is the list of symbols emitted to the symbol table.
139 /// When the SymbolList is finally built, local symbols must be placed in
140 /// the beginning while non-locals at the end.
141 std::vector
<ELFSym
*> SymbolList
;
143 /// PrivateSyms - Record private symbols, every symbol here must never be
144 /// present in the SymbolList.
145 std::vector
<ELFSym
*> PrivateSyms
;
147 // Remove tab from section name prefix. This is necessary becase TAI
148 // sometimes return a section name prefixed with elf unused chars. This is
149 // a little bit dirty. FIXME: find a better approach, maybe add more
150 // methods to TAI to get the clean name?
151 void fixNameForSection(std::string
&Name
) {
152 size_t Pos
= Name
.find("\t");
153 if (Pos
!= std::string::npos
)
156 Pos
= Name
.find(".section ");
157 if (Pos
!= std::string::npos
)
160 Pos
= Name
.find("\n");
161 if (Pos
!= std::string::npos
)
165 /// getSection - Return the section with the specified name, creating a new
166 /// section if one does not already exist.
167 ELFSection
&getSection(const std::string
&Name
, unsigned Type
,
168 unsigned Flags
= 0, unsigned Align
= 0) {
169 std::string
SName(Name
);
170 fixNameForSection(SName
);
172 ELFSection
*&SN
= SectionLookup
[SName
];
175 SectionList
.push_back(new ELFSection(SName
, isLittleEndian
, is64Bit
));
176 SN
= SectionList
.back();
177 SN
->SectionIdx
= NumSections
++;
180 SN
->Link
= ELFSection::SHN_UNDEF
;
185 ELFSection
&getNonExecStackSection() {
186 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS
, 0, 1);
189 ELFSection
&getSymbolTableSection() {
190 return getSection(".symtab", ELFSection::SHT_SYMTAB
, 0);
193 ELFSection
&getStringTableSection() {
194 return getSection(".strtab", ELFSection::SHT_STRTAB
, 0, 1);
197 ELFSection
&getSectionHeaderStringTableSection() {
198 return getSection(".shstrtab", ELFSection::SHT_STRTAB
, 0, 1);
201 ELFSection
&getDataSection() {
202 return getSection(".data", ELFSection::SHT_PROGBITS
,
203 ELFSection::SHF_WRITE
| ELFSection::SHF_ALLOC
, 4);
206 ELFSection
&getBSSSection() {
207 return getSection(".bss", ELFSection::SHT_NOBITS
,
208 ELFSection::SHF_WRITE
| ELFSection::SHF_ALLOC
, 4);
211 ELFSection
&getNullSection() {
212 return getSection("", ELFSection::SHT_NULL
, 0);
215 ELFSection
&getCtorSection();
216 ELFSection
&getDtorSection();
217 ELFSection
&getJumpTableSection();
218 ELFSection
&getConstantPoolSection(MachineConstantPoolEntry
&CPE
);
219 ELFSection
&getTextSection(Function
*F
);
220 ELFSection
&getRelocSection(ELFSection
&S
);
222 // Helpers for obtaining ELF specific info.
223 unsigned getGlobalELFBinding(const GlobalValue
*GV
);
224 unsigned getGlobalELFType(const GlobalValue
*GV
);
225 unsigned getGlobalELFVisibility(const GlobalValue
*GV
);
226 unsigned getElfSectionFlags(SectionKind Kind
, bool IsAlloc
= true);
228 // addGlobalSymbol - Add a global to be processed and to
229 // the global symbol lookup, use a zero index because the table
230 // index will be determined later.
231 void addGlobalSymbol(const GlobalValue
*GV
, bool AddToLookup
= false);
233 // addExternalSymbol - Add the external to be processed and to the
234 // external symbol lookup, use a zero index because the symbol
235 // table index will be determined later
236 void addExternalSymbol(const char *External
);
238 // As we complete the ELF file, we need to update fields in the ELF header
239 // (e.g. the location of the section table). These members keep track of
240 // the offset in ELFHeader of these various pieces to update and other
241 // locations in the file.
242 unsigned ELFHdr_e_shoff_Offset
; // e_shoff in ELF header.
243 unsigned ELFHdr_e_shstrndx_Offset
; // e_shstrndx in ELF header.
244 unsigned ELFHdr_e_shnum_Offset
; // e_shnum in ELF header.
247 void EmitGlobal(const GlobalValue
*GV
);
248 void EmitGlobalConstant(const Constant
*C
, ELFSection
&GblS
);
249 void EmitGlobalConstantStruct(const ConstantStruct
*CVS
,
251 void emitGlobalDataRelocation(const GlobalValue
*GV
, unsigned Size
,
253 bool EmitSpecialLLVMGlobal(const GlobalVariable
*GV
);
254 void EmitXXStructorList(Constant
*List
, ELFSection
&Xtor
);
255 void EmitRelocations();
256 void EmitRelocation(BinaryObject
&RelSec
, ELFRelocation
&Rel
, bool HasRelA
);
257 void EmitSectionHeader(BinaryObject
&SHdrTab
, const ELFSection
&SHdr
);
258 void EmitSectionTableStringTable();
259 void EmitSymbol(BinaryObject
&SymbolTable
, ELFSym
&Sym
);
260 void EmitSymbolTable();
261 void EmitStringTable(const std::string
&ModuleName
);
262 void OutputSectionsAndSectionTable();
263 void RelocateField(BinaryObject
&BO
, uint32_t Offset
, int64_t Value
,
265 unsigned SortSymbols();