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/OutputBuffer.h"
20 #include "llvm/Target/TargetAsmInfo.h"
21 #include "llvm/Target/TargetELFWriterInfo.h"
31 class MachineCodeEmitter;
34 /// ELFWriter - This class implements the common target-independent code for
35 /// writing ELF files. Targets should derive a class from this to
36 /// parameterize the output format.
38 class ELFWriter : public MachineFunctionPass {
39 friend class ELFCodeEmitter;
43 MachineCodeEmitter &getMachineCodeEmitter() const {
44 return *(MachineCodeEmitter*)MCE;
47 ELFWriter(raw_ostream &O, TargetMachine &TM);
50 typedef std::vector<unsigned char> DataBuffer;
53 /// Output stream to send the resultant object file to.
56 /// Target machine description.
59 /// Mang - The object used to perform name mangling for this module.
62 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
63 /// code for functions to the .o file.
66 /// TAI - Target Asm Info, provide information about section names for
67 /// globals and other target specific stuff.
68 const TargetAsmInfo *TAI;
70 //===------------------------------------------------------------------===//
71 // Properties inferred automatically from the target machine.
72 //===------------------------------------------------------------------===//
74 /// is64Bit/isLittleEndian - This information is inferred from the target
75 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
76 bool is64Bit, isLittleEndian;
78 /// doInitialization - Emit the file header and all of the global variables
79 /// for the module to the ELF file.
80 bool doInitialization(Module &M);
81 bool runOnMachineFunction(MachineFunction &MF);
83 /// doFinalization - Now that the module has been completely processed, emit
84 /// the ELF file to 'O'.
85 bool doFinalization(Module &M);
88 // The buffer we accumulate the file header into. Note that this should be
89 // changed into something much more efficient later (and the bitcode writer
91 DataBuffer FileHeader;
93 /// ElfHdr - Hold information about 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::list<ELFSection> SectionList;
100 unsigned NumSections; // Always = SectionList.size()
102 /// SectionLookup - This is a mapping from section name to section number in
104 std::map<std::string, ELFSection*> SectionLookup;
106 /// getSection - Return the section with the specified name, creating a new
107 /// section if one does not already exist.
108 ELFSection &getSection(const std::string &Name, unsigned Type,
109 unsigned Flags = 0, unsigned Align = 0) {
110 ELFSection *&SN = SectionLookup[Name];
113 SectionList.push_back(Name);
114 SN = &SectionList.back();
115 SN->SectionIdx = NumSections++;
118 SN->Link = ELFSection::SHN_UNDEF;
123 ELFSection &getTextSection() {
124 return getSection(".text", ELFSection::SHT_PROGBITS,
125 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
128 ELFSection &getSymbolTableSection() {
129 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
132 ELFSection &getStringTableSection() {
133 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
136 ELFSection &getDataSection() {
137 return getSection(".data", ELFSection::SHT_PROGBITS,
138 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
141 ELFSection &getBSSSection() {
142 return getSection(".bss", ELFSection::SHT_NOBITS,
143 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
146 /// SymbolTable - This is the list of symbols we have emitted to the file.
147 /// This actually gets rearranged before emission to the file (to put the
148 /// local symbols first in the list).
149 std::vector<ELFSym> SymbolTable;
151 /// PendingSyms - This is a list of externally defined symbols that we have
152 /// been asked to emit, but have not seen a reference to. When a reference
153 /// is seen, the symbol will move from this list to the SymbolTable.
154 SetVector<GlobalValue*> PendingGlobals;
156 // As we complete the ELF file, we need to update fields in the ELF header
157 // (e.g. the location of the section table). These members keep track of
158 // the offset in ELFHeader of these various pieces to update and other
159 // locations in the file.
160 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
161 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
162 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
164 void EmitGlobal(GlobalVariable *GV);
165 void EmitGlobalConstant(const Constant *C, OutputBuffer &GblCstTab);
166 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
167 OutputBuffer &GblCstTab);
168 void EmitRelocations();
169 void EmitSectionHeader(OutputBuffer &TableOut, const ELFSection &Section);
170 void EmitSectionTableStringTable();
171 void EmitSymbol(OutputBuffer &SymTabOut, ELFSym &Sym);
172 void EmitSymbolTable();
173 void OutputSectionsAndSectionTable();