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"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Target/TargetAsmInfo.h"
21 #include "llvm/Target/TargetELFWriterInfo.h"
32 class MachineCodeEmitter;
35 /// ELFWriter - This class implements the common target-independent code for
36 /// writing ELF files. Targets should derive a class from this to
37 /// parameterize the output format.
39 class ELFWriter : public MachineFunctionPass {
40 friend class ELFCodeEmitter;
44 MachineCodeEmitter &getMachineCodeEmitter() const {
45 return *(MachineCodeEmitter*)MCE;
48 ELFWriter(raw_ostream &O, TargetMachine &TM);
51 typedef std::vector<unsigned char> DataBuffer;
54 /// Output stream to send the resultant object file to.
57 /// Target machine description.
60 /// Target Elf Writer description.
61 const TargetELFWriterInfo *TEW;
63 /// Mang - The object used to perform name mangling for this module.
66 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
67 /// code for functions to the .o file.
70 /// TAI - Target Asm Info, provide information about section names for
71 /// globals and other target specific stuff.
72 const TargetAsmInfo *TAI;
74 //===------------------------------------------------------------------===//
75 // Properties inferred automatically from the target machine.
76 //===------------------------------------------------------------------===//
78 /// is64Bit/isLittleEndian - This information is inferred from the target
79 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
80 bool is64Bit, isLittleEndian;
82 /// doInitialization - Emit the file header and all of the global variables
83 /// for the module to the ELF file.
84 bool doInitialization(Module &M);
85 bool runOnMachineFunction(MachineFunction &MF);
87 /// doFinalization - Now that the module has been completely processed, emit
88 /// the ELF file to 'O'.
89 bool doFinalization(Module &M);
92 /// Blob containing the Elf header
95 /// SectionList - This is the list of sections that we have emitted to the
96 /// file. Once the file has been completely built, the section header table
97 /// is constructed from this info.
98 std::list<ELFSection> SectionList;
99 unsigned NumSections; // Always = SectionList.size()
101 /// SectionLookup - This is a mapping from section name to section number in
103 std::map<std::string, ELFSection*> SectionLookup;
105 /// GblSymLookup - This is a mapping from global value to a symbol index
106 /// in the symbol table. This is useful since relocations symbol references
107 /// must be quickly mapped to a symbol table index
108 std::map<const GlobalValue*, uint32_t> GblSymLookup;
110 /// SymbolList - This is the list of symbols emitted to the symbol table
111 /// Local symbols go to the front and Globals to the back.
112 std::list<ELFSym> SymbolList;
114 /// PendingGlobals - List of externally defined symbols that we have been
115 /// asked to emit, but have not seen a reference to. When a reference
116 /// is seen, the symbol will move from this list to the SymbolList.
117 SetVector<GlobalValue*> PendingGlobals;
119 /// getSection - Return the section with the specified name, creating a new
120 /// section if one does not already exist.
121 ELFSection &getSection(const std::string &Name, unsigned Type,
122 unsigned Flags = 0, unsigned Align = 0) {
123 ELFSection *&SN = SectionLookup[Name];
126 // Remove tab from section name prefix. This is necessary becase TAI
127 // sometimes return a section name prefixed with a "\t" char.
128 std::string SectionName(Name);
129 size_t Pos = SectionName.find("\t");
130 if (Pos != std::string::npos)
131 SectionName.erase(Pos, 1);
133 SectionList.push_back(ELFSection(SectionName, isLittleEndian, is64Bit));
134 SN = &SectionList.back();
135 SN->SectionIdx = NumSections++;
138 SN->Link = ELFSection::SHN_UNDEF;
143 /// TODO: support mangled names here to emit the right .text section
144 /// for c++ object files.
145 ELFSection &getTextSection() {
146 return getSection(".text", ELFSection::SHT_PROGBITS,
147 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
150 /// Return the relocation section of section 'S'. 'RelA' is true
151 /// if the relocation section contains entries with addends.
152 ELFSection &getRelocSection(std::string SName, bool RelA) {
153 std::string RelSName(".rel");
154 unsigned SHdrTy = RelA ? ELFSection::SHT_RELA : ELFSection::SHT_REL;
156 if (RelA) RelSName.append("a");
157 RelSName.append(SName);
159 return getSection(RelSName, SHdrTy, 0, TEW->getPrefELFAlignment());
162 ELFSection &getNonExecStackSection() {
163 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
166 ELFSection &getSymbolTableSection() {
167 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
170 ELFSection &getStringTableSection() {
171 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
174 ELFSection &getSectionHeaderStringTableSection() {
175 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
178 ELFSection &getDataSection() {
179 return getSection(".data", ELFSection::SHT_PROGBITS,
180 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
183 ELFSection &getBSSSection() {
184 return getSection(".bss", ELFSection::SHT_NOBITS,
185 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
188 ELFSection &getNullSection() {
189 return getSection("", ELFSection::SHT_NULL, 0);
192 // As we complete the ELF file, we need to update fields in the ELF header
193 // (e.g. the location of the section table). These members keep track of
194 // the offset in ELFHeader of these various pieces to update and other
195 // locations in the file.
196 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
197 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
198 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
201 void EmitFunctionDeclaration(const Function *F);
202 void EmitGlobalVar(const GlobalVariable *GV);
203 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
204 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
206 unsigned getGlobalELFLinkage(const GlobalVariable *GV);
207 ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym);
208 void EmitRelocations();
209 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
210 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
211 void EmitSectionTableStringTable();
212 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
213 void EmitSymbolTable();
214 void EmitStringTable();
215 void OutputSectionsAndSectionTable();