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 /// ELFWriter - This class implements the common target-independent code for
38 /// writing ELF files. Targets should derive a class from this to
39 /// parameterize the output format.
41 class ELFWriter : public MachineFunctionPass {
42 friend class ELFCodeEmitter;
46 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
47 ObjectCodeEmitter *getObjectCodeEmitter() {
48 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
51 ELFWriter(raw_ostream &O, TargetMachine &TM);
55 /// Output stream to send the resultant object file to.
58 /// Target machine description.
61 /// Target Elf Writer description.
62 const TargetELFWriterInfo *TEW;
64 /// Mang - The object used to perform name mangling for this module.
67 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
68 /// code for functions to the .o file.
69 ELFCodeEmitter *ElfCE;
71 /// TAI - Target Asm Info, provide information about section names for
72 /// globals and other target specific stuff.
73 const TargetAsmInfo *TAI;
75 //===------------------------------------------------------------------===//
76 // Properties inferred automatically from the target machine.
77 //===------------------------------------------------------------------===//
79 /// is64Bit/isLittleEndian - This information is inferred from the target
80 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
81 bool is64Bit, isLittleEndian;
83 /// doInitialization - Emit the file header and all of the global variables
84 /// for the module to the ELF file.
85 bool doInitialization(Module &M);
86 bool runOnMachineFunction(MachineFunction &MF);
88 /// doFinalization - Now that the module has been completely processed, emit
89 /// the ELF file to 'O'.
90 bool doFinalization(Module &M);
93 /// Blob containing the Elf header
96 /// SectionList - This is the list of sections that we have emitted to the
97 /// file. Once the file has been completely built, the section header table
98 /// is constructed from this info.
99 std::vector<ELFSection*> SectionList;
100 unsigned NumSections; // Always = SectionList.size()
102 /// SectionLookup - This is a mapping from section name to section number in
103 /// the SectionList. Used to quickly gather the Section Index from TAI names
104 std::map<std::string, ELFSection*> SectionLookup;
106 /// GblSymLookup - This is a mapping from global value to a symbol index
107 /// in the symbol table. This is useful since relocations symbol references
108 /// must be quickly mapped to a symbol table index
109 std::map<const GlobalValue*, uint32_t> GblSymLookup;
111 /// SymbolList - This is the list of symbols emitted to the symbol table.
112 /// When the SymbolList is finally built, local symbols must be placed in
113 /// the beginning while non-locals at the end.
114 std::vector<ELFSym*> SymbolList;
116 /// PendingGlobals - List of externally defined symbols that we have been
117 /// asked to emit, but have not seen a reference to. When a reference
118 /// is seen, the symbol will move from this list to the SymbolList.
119 SetVector<GlobalValue*> PendingGlobals;
121 // Remove tab from section name prefix. This is necessary becase TAI
122 // sometimes return a section name prefixed with elf unused chars. This is
123 // a little bit dirty. FIXME: find a better approach, maybe add more
124 // methods to TAI to get the clean name?
125 void fixNameForSection(std::string &Name) {
126 size_t Pos = Name.find("\t");
127 if (Pos != std::string::npos)
130 Pos = Name.find(".section ");
131 if (Pos != std::string::npos)
134 Pos = Name.find("\n");
135 if (Pos != std::string::npos)
139 /// getSection - Return the section with the specified name, creating a new
140 /// section if one does not already exist.
141 ELFSection &getSection(const std::string &Name, unsigned Type,
142 unsigned Flags = 0, unsigned Align = 0) {
143 std::string SName(Name);
144 fixNameForSection(SName);
146 ELFSection *&SN = SectionLookup[SName];
149 SectionList.push_back(new ELFSection(SName, isLittleEndian, is64Bit));
150 SN = SectionList.back();
151 SN->SectionIdx = NumSections++;
154 SN->Link = ELFSection::SHN_UNDEF;
159 /// TODO: support mangled names here to emit the right .text section
160 /// for c++ object files.
161 ELFSection &getTextSection() {
162 return getSection(".text", ELFSection::SHT_PROGBITS,
163 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
166 /// Get jump table section on the section name returned by TAI
167 ELFSection &getJumpTableSection(std::string SName, unsigned Align) {
168 return getSection(SName, ELFSection::SHT_PROGBITS,
169 ELFSection::SHF_ALLOC, Align);
172 /// Get a constant pool section based on the section name returned by TAI
173 ELFSection &getConstantPoolSection(std::string SName, unsigned Align) {
174 return getSection(SName, ELFSection::SHT_PROGBITS,
175 ELFSection::SHF_MERGE | ELFSection::SHF_ALLOC, Align);
178 /// Return the relocation section of section 'S'. 'RelA' is true
179 /// if the relocation section contains entries with addends.
180 ELFSection &getRelocSection(std::string SName, bool RelA, unsigned Align) {
181 std::string RelSName(".rel");
182 unsigned SHdrTy = RelA ? ELFSection::SHT_RELA : ELFSection::SHT_REL;
184 if (RelA) RelSName.append("a");
185 RelSName.append(SName);
187 return getSection(RelSName, SHdrTy, 0, Align);
190 ELFSection &getNonExecStackSection() {
191 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
194 ELFSection &getSymbolTableSection() {
195 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
198 ELFSection &getStringTableSection() {
199 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
202 ELFSection &getSectionHeaderStringTableSection() {
203 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
206 ELFSection &getDataSection() {
207 return getSection(".data", ELFSection::SHT_PROGBITS,
208 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
211 ELFSection &getBSSSection() {
212 return getSection(".bss", ELFSection::SHT_NOBITS,
213 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
216 ELFSection &getNullSection() {
217 return getSection("", ELFSection::SHT_NULL, 0);
220 // Helpers for obtaining ELF specific info.
221 unsigned getGlobalELFBinding(const GlobalValue *GV);
222 unsigned getGlobalELFType(const GlobalValue *GV);
223 unsigned getGlobalELFVisibility(const GlobalValue *GV);
224 unsigned getElfSectionFlags(unsigned Flags);
226 // As we complete the ELF file, we need to update fields in the ELF header
227 // (e.g. the location of the section table). These members keep track of
228 // the offset in ELFHeader of these various pieces to update and other
229 // locations in the file.
230 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
231 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
232 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
235 void EmitGlobal(const GlobalValue *GV);
236 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
237 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
239 ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym);
240 void EmitRelocations();
241 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
242 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
243 void EmitSectionTableStringTable();
244 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
245 void EmitSymbolTable();
246 void EmitStringTable();
247 void OutputSectionsAndSectionTable();
248 unsigned SortSymbols();