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 ObjectCodeEmitter;
34 class TargetELFWriterInfo;
37 typedef std::vector<ELFSym*>::iterator ELFSymIter;
38 typedef std::vector<ELFSection*>::iterator ELFSectionIter;
40 /// ELFWriter - This class implements the common target-independent code for
41 /// writing ELF files. Targets should derive a class from this to
42 /// parameterize the output format.
44 class ELFWriter : public MachineFunctionPass {
45 friend class ELFCodeEmitter;
49 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
50 ObjectCodeEmitter *getObjectCodeEmitter() {
51 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
54 ELFWriter(raw_ostream &O, TargetMachine &TM);
58 /// Output stream to send the resultant object file to.
61 /// Target machine description.
64 /// Target Elf Writer description.
65 const TargetELFWriterInfo *TEW;
67 /// Mang - The object used to perform name mangling for this module.
70 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
71 /// code for functions to the .o file.
72 ELFCodeEmitter *ElfCE;
74 /// TAI - Target Asm Info, provide information about section names for
75 /// globals and other target specific stuff.
76 const TargetAsmInfo *TAI;
78 //===------------------------------------------------------------------===//
79 // Properties inferred automatically from the target machine.
80 //===------------------------------------------------------------------===//
82 /// is64Bit/isLittleEndian - This information is inferred from the target
83 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
84 bool is64Bit, isLittleEndian;
86 /// doInitialization - Emit the file header and all of the global variables
87 /// for the module to the ELF file.
88 bool doInitialization(Module &M);
89 bool runOnMachineFunction(MachineFunction &MF);
91 /// doFinalization - Now that the module has been completely processed, emit
92 /// the ELF file to 'O'.
93 bool doFinalization(Module &M);
96 /// Blob containing the Elf header
99 /// SectionList - This is the list of sections that we have emitted to the
100 /// file. Once the file has been completely built, the section header table
101 /// is constructed from this info.
102 std::vector<ELFSection*> SectionList;
103 unsigned NumSections; // Always = SectionList.size()
105 /// SectionLookup - This is a mapping from section name to section number in
106 /// the SectionList. Used to quickly gather the Section Index from TAI names
107 std::map<std::string, ELFSection*> SectionLookup;
109 /// GblSymLookup - This is a mapping from global value to a symbol index
110 /// in the symbol table or private symbols list. This is useful since reloc
111 /// symbol references must be quickly mapped to their indices on the lists
112 std::map<const GlobalValue*, uint32_t> GblSymLookup;
114 /// SymbolList - This is the list of symbols emitted to the symbol table.
115 /// When the SymbolList is finally built, local symbols must be placed in
116 /// the beginning while non-locals at the end.
117 std::vector<ELFSym*> SymbolList;
119 /// PrivateSyms - Record private symbols, every symbol here must never be
120 /// present in the SymbolList.
121 std::vector<ELFSym*> PrivateSyms;
123 /// PendingGlobals - List of externally defined symbols that we have been
124 /// asked to emit, but have not seen a reference to. When a reference
125 /// is seen, the symbol will move from this list to the SymbolList.
126 SetVector<GlobalValue*> PendingGlobals;
128 // Remove tab from section name prefix. This is necessary becase TAI
129 // sometimes return a section name prefixed with elf unused chars. This is
130 // a little bit dirty. FIXME: find a better approach, maybe add more
131 // methods to TAI to get the clean name?
132 void fixNameForSection(std::string &Name) {
133 size_t Pos = Name.find("\t");
134 if (Pos != std::string::npos)
137 Pos = Name.find(".section ");
138 if (Pos != std::string::npos)
141 Pos = Name.find("\n");
142 if (Pos != std::string::npos)
146 /// getSection - Return the section with the specified name, creating a new
147 /// section if one does not already exist.
148 ELFSection &getSection(const std::string &Name, unsigned Type,
149 unsigned Flags = 0, unsigned Align = 0) {
150 std::string SName(Name);
151 fixNameForSection(SName);
153 ELFSection *&SN = SectionLookup[SName];
156 SectionList.push_back(new ELFSection(SName, isLittleEndian, is64Bit));
157 SN = SectionList.back();
158 SN->SectionIdx = NumSections++;
161 SN->Link = ELFSection::SHN_UNDEF;
166 /// TODO: support mangled names here to emit the right .text section
167 /// for c++ object files.
168 ELFSection &getTextSection() {
169 return getSection(".text", ELFSection::SHT_PROGBITS,
170 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
173 /// Get jump table section on the section name returned by TAI
174 ELFSection &getJumpTableSection(std::string SName, unsigned Align) {
175 return getSection(SName, ELFSection::SHT_PROGBITS,
176 ELFSection::SHF_ALLOC, Align);
179 /// Get a constant pool section based on the section name returned by TAI
180 ELFSection &getConstantPoolSection(std::string SName, unsigned Align) {
181 return getSection(SName, ELFSection::SHT_PROGBITS,
182 ELFSection::SHF_MERGE | ELFSection::SHF_ALLOC, Align);
185 /// Return the relocation section of section 'S'. 'RelA' is true
186 /// if the relocation section contains entries with addends.
187 ELFSection &getRelocSection(std::string SName, bool RelA, unsigned Align) {
188 std::string RelSName(".rel");
189 unsigned SHdrTy = RelA ? ELFSection::SHT_RELA : ELFSection::SHT_REL;
191 if (RelA) RelSName.append("a");
192 RelSName.append(SName);
194 return getSection(RelSName, SHdrTy, 0, Align);
197 ELFSection &getNonExecStackSection() {
198 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
201 ELFSection &getSymbolTableSection() {
202 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
205 ELFSection &getStringTableSection() {
206 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
209 ELFSection &getSectionHeaderStringTableSection() {
210 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
213 ELFSection &getDataSection() {
214 return getSection(".data", ELFSection::SHT_PROGBITS,
215 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
218 ELFSection &getBSSSection() {
219 return getSection(".bss", ELFSection::SHT_NOBITS,
220 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
223 ELFSection &getNullSection() {
224 return getSection("", ELFSection::SHT_NULL, 0);
227 // Helpers for obtaining ELF specific info.
228 unsigned getGlobalELFBinding(const GlobalValue *GV);
229 unsigned getGlobalELFType(const GlobalValue *GV);
230 unsigned getGlobalELFVisibility(const GlobalValue *GV);
231 unsigned getElfSectionFlags(unsigned Flags);
233 // As we complete the ELF file, we need to update fields in the ELF header
234 // (e.g. the location of the section table). These members keep track of
235 // the offset in ELFHeader of these various pieces to update and other
236 // locations in the file.
237 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
238 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
239 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
242 void EmitGlobal(const GlobalValue *GV);
243 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
244 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
246 ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym);
247 void EmitRelocations();
248 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
249 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
250 void EmitSectionTableStringTable();
251 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
252 void EmitSymbolTable();
253 void EmitStringTable();
254 void OutputSectionsAndSectionTable();
255 unsigned SortSymbols();