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/CodeGen/MachineFunctionPass.h"
23 class MachineCodeEmitter;
26 /// ELFWriter - This class implements the common target-independent code for
27 /// writing ELF files. Targets should derive a class from this to
28 /// parameterize the output format.
30 class ELFWriter : public MachineFunctionPass {
31 friend class ELFCodeEmitter;
35 MachineCodeEmitter &getMachineCodeEmitter() const {
36 return *(MachineCodeEmitter*)MCE;
39 ELFWriter(std::ostream &O, TargetMachine &TM);
42 typedef std::vector<unsigned char> DataBuffer;
45 /// Output stream to send the resultant object file to.
49 /// Target machine description.
53 /// Mang - The object used to perform name mangling for this module.
57 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
58 /// code for functions to the .o file.
61 //===------------------------------------------------------------------===//
62 // Properties to be set by the derived class ctor, used to configure the
65 // e_machine - This field is the target specific value to emit as the
66 // e_machine member of the ELF header.
67 unsigned short e_machine;
69 // e_flags - The machine flags for the target. This defaults to zero.
72 //===------------------------------------------------------------------===//
73 // Properties inferred automatically from the target machine.
76 /// is64Bit/isLittleEndian - This information is inferred from the target
77 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
78 bool is64Bit, isLittleEndian;
80 /// doInitialization - Emit the file header and all of the global variables
81 /// for the module to the ELF file.
82 bool doInitialization(Module &M);
84 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 // The buffer we accumulate the file header into. Note that this should be
93 // changed into something much more efficient later (and the bitcode writer
95 DataBuffer FileHeader;
97 /// ELFSection - This struct contains information about each section that is
98 /// emitted to the file. This is eventually turned into the section header
99 /// table at the end of the file.
101 std::string Name; // Name of the section.
102 unsigned NameIdx; // Index in .shstrtab of name, once emitted.
113 /// SectionIdx - The number of the section in the Section Table.
115 unsigned short SectionIdx;
117 /// SectionData - The actual data for this section which we are building
118 /// up for emission to the file.
119 DataBuffer SectionData;
121 enum { SHT_NULL = 0, SHT_PROGBITS = 1, SHT_SYMTAB = 2, SHT_STRTAB = 3,
122 SHT_RELA = 4, SHT_HASH = 5, SHT_DYNAMIC = 6, SHT_NOTE = 7,
123 SHT_NOBITS = 8, SHT_REL = 9, SHT_SHLIB = 10, SHT_DYNSYM = 11 };
124 enum { SHN_UNDEF = 0, SHN_ABS = 0xFFF1, SHN_COMMON = 0xFFF2 };
125 enum { // SHF - ELF Section Header Flags
126 SHF_WRITE = 1 << 0, // Writable
127 SHF_ALLOC = 1 << 1, // Mapped into the process addr space
128 SHF_EXECINSTR = 1 << 2, // Executable
129 SHF_MERGE = 1 << 4, // Might be merged if equal
130 SHF_STRINGS = 1 << 5, // Contains null-terminated strings
131 SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index
132 SHF_LINK_ORDER = 1 << 7, // Preserve order after combining
133 SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
134 SHF_GROUP = 1 << 9, // Section is a member of a group
135 SHF_TLS = 1 << 10 // Section holds thread-local data
138 ELFSection(const std::string &name)
139 : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0),
140 Link(0), Info(0), Align(0), EntSize(0) {
144 /// SectionList - This is the list of sections that we have emitted to the
145 /// file. Once the file has been completely built, the section header table
146 /// is constructed from this info.
147 std::list<ELFSection> SectionList;
148 unsigned NumSections; // Always = SectionList.size()
150 /// SectionLookup - This is a mapping from section name to section number in
152 std::map<std::string, ELFSection*> SectionLookup;
154 /// getSection - Return the section with the specified name, creating a new
155 /// section if one does not already exist.
156 ELFSection &getSection(const std::string &Name,
157 unsigned Type, unsigned Flags = 0) {
158 ELFSection *&SN = SectionLookup[Name];
161 SectionList.push_back(Name);
162 SN = &SectionList.back();
163 SN->SectionIdx = NumSections++;
169 ELFSection &getDataSection() {
170 return getSection(".data", ELFSection::SHT_PROGBITS,
171 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
173 ELFSection &getBSSSection() {
174 return getSection(".bss", ELFSection::SHT_NOBITS,
175 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
178 /// ELFSym - This struct contains information about each symbol that is
179 /// added to logical symbol table for the module. This is eventually
180 /// turned into a real symbol table in the file.
182 const GlobalValue *GV; // The global value this corresponds to.
183 unsigned NameIdx; // Index in .strtab of name, once emitted.
188 unsigned short SectionIdx;
190 enum { STB_LOCAL = 0, STB_GLOBAL = 1, STB_WEAK = 2 };
191 enum { STT_NOTYPE = 0, STT_OBJECT = 1, STT_FUNC = 2, STT_SECTION = 3,
193 ELFSym(const GlobalValue *gv) : GV(gv), Value(0), Size(0), Info(0),
194 Other(0), SectionIdx(0) {}
196 void SetBind(unsigned X) {
197 assert(X == (X & 0xF) && "Bind value out of range!");
198 Info = (Info & 0x0F) | (X << 4);
200 void SetType(unsigned X) {
201 assert(X == (X & 0xF) && "Type value out of range!");
202 Info = (Info & 0xF0) | X;
206 /// SymbolTable - This is the list of symbols we have emitted to the file.
207 /// This actually gets rearranged before emission to the file (to put the
208 /// local symbols first in the list).
209 std::vector<ELFSym> SymbolTable;
211 // As we complete the ELF file, we need to update fields in the ELF header
212 // (e.g. the location of the section table). These members keep track of
213 // the offset in ELFHeader of these various pieces to update and other
214 // locations in the file.
215 unsigned ELFHeader_e_shoff_Offset; // e_shoff in ELF header.
216 unsigned ELFHeader_e_shstrndx_Offset; // e_shstrndx in ELF header.
217 unsigned ELFHeader_e_shnum_Offset; // e_shnum in ELF header.
219 void EmitGlobal(GlobalVariable *GV);
221 void EmitSymbolTable();
223 void EmitSectionTableStringTable();
224 void OutputSectionsAndSectionTable();