blob: be3b39b476c500926175381ee8fba983e0e27ac6 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ELFWriter class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef ELFWRITER_H
15#define ELFWRITER_H
16
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include <list>
Dan Gohman249ddbf2008-03-21 23:51:57 +000019#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020
21namespace llvm {
22 class GlobalVariable;
23 class Mangler;
24 class MachineCodeEmitter;
25 class ELFCodeEmitter;
26
27 /// ELFWriter - This class implements the common target-independent code for
28 /// writing ELF files. Targets should derive a class from this to
29 /// parameterize the output format.
30 ///
31 class ELFWriter : public MachineFunctionPass {
32 friend class ELFCodeEmitter;
33 public:
34 static char ID;
35
36 MachineCodeEmitter &getMachineCodeEmitter() const {
37 return *(MachineCodeEmitter*)MCE;
38 }
39
40 ELFWriter(std::ostream &O, TargetMachine &TM);
41 ~ELFWriter();
42
43 typedef std::vector<unsigned char> DataBuffer;
44
45 protected:
46 /// Output stream to send the resultant object file to.
47 ///
48 std::ostream &O;
49
50 /// Target machine description.
51 ///
52 TargetMachine &TM;
53
54 /// Mang - The object used to perform name mangling for this module.
55 ///
56 Mangler *Mang;
57
58 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
59 /// code for functions to the .o file.
60 ELFCodeEmitter *MCE;
61
62 //===------------------------------------------------------------------===//
63 // Properties to be set by the derived class ctor, used to configure the
64 // ELFWriter.
65
66 // e_machine - This field is the target specific value to emit as the
67 // e_machine member of the ELF header.
68 unsigned short e_machine;
69
70 // e_flags - The machine flags for the target. This defaults to zero.
71 unsigned e_flags;
72
73 //===------------------------------------------------------------------===//
74 // Properties inferred automatically from the target machine.
75 //
76
77 /// is64Bit/isLittleEndian - This information is inferred from the target
78 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
79 bool is64Bit, isLittleEndian;
80
81 /// doInitialization - Emit the file header and all of the global variables
82 /// for the module to the ELF file.
83 bool doInitialization(Module &M);
84
85 bool runOnMachineFunction(MachineFunction &MF);
86
87
88 /// doFinalization - Now that the module has been completely processed, emit
89 /// the ELF file to 'O'.
90 bool doFinalization(Module &M);
91
92 private:
93 // The buffer we accumulate the file header into. Note that this should be
94 // changed into something much more efficient later (and the bitcode writer
95 // as well!).
96 DataBuffer FileHeader;
97
98 /// ELFSection - This struct contains information about each section that is
99 /// emitted to the file. This is eventually turned into the section header
100 /// table at the end of the file.
101 struct ELFSection {
102 std::string Name; // Name of the section.
103 unsigned NameIdx; // Index in .shstrtab of name, once emitted.
104 unsigned Type;
105 unsigned Flags;
106 uint64_t Addr;
107 unsigned Offset;
108 unsigned Size;
109 unsigned Link;
110 unsigned Info;
111 unsigned Align;
112 unsigned EntSize;
113
114 /// SectionIdx - The number of the section in the Section Table.
115 ///
116 unsigned short SectionIdx;
117
118 /// SectionData - The actual data for this section which we are building
119 /// up for emission to the file.
120 DataBuffer SectionData;
121
122 enum { SHT_NULL = 0, SHT_PROGBITS = 1, SHT_SYMTAB = 2, SHT_STRTAB = 3,
123 SHT_RELA = 4, SHT_HASH = 5, SHT_DYNAMIC = 6, SHT_NOTE = 7,
124 SHT_NOBITS = 8, SHT_REL = 9, SHT_SHLIB = 10, SHT_DYNSYM = 11 };
125 enum { SHN_UNDEF = 0, SHN_ABS = 0xFFF1, SHN_COMMON = 0xFFF2 };
126 enum { // SHF - ELF Section Header Flags
127 SHF_WRITE = 1 << 0, // Writable
128 SHF_ALLOC = 1 << 1, // Mapped into the process addr space
129 SHF_EXECINSTR = 1 << 2, // Executable
130 SHF_MERGE = 1 << 4, // Might be merged if equal
131 SHF_STRINGS = 1 << 5, // Contains null-terminated strings
132 SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index
133 SHF_LINK_ORDER = 1 << 7, // Preserve order after combining
134 SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
135 SHF_GROUP = 1 << 9, // Section is a member of a group
136 SHF_TLS = 1 << 10 // Section holds thread-local data
137 };
138
139 ELFSection(const std::string &name)
140 : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0),
141 Link(0), Info(0), Align(0), EntSize(0) {
142 }
143 };
144
145 /// SectionList - This is the list of sections that we have emitted to the
146 /// file. Once the file has been completely built, the section header table
147 /// is constructed from this info.
148 std::list<ELFSection> SectionList;
149 unsigned NumSections; // Always = SectionList.size()
150
151 /// SectionLookup - This is a mapping from section name to section number in
152 /// the SectionList.
153 std::map<std::string, ELFSection*> SectionLookup;
154
155 /// getSection - Return the section with the specified name, creating a new
156 /// section if one does not already exist.
157 ELFSection &getSection(const std::string &Name,
158 unsigned Type, unsigned Flags = 0) {
159 ELFSection *&SN = SectionLookup[Name];
160 if (SN) return *SN;
161
162 SectionList.push_back(Name);
163 SN = &SectionList.back();
164 SN->SectionIdx = NumSections++;
165 SN->Type = Type;
166 SN->Flags = Flags;
167 return *SN;
168 }
169
170 ELFSection &getDataSection() {
171 return getSection(".data", ELFSection::SHT_PROGBITS,
172 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
173 }
174 ELFSection &getBSSSection() {
175 return getSection(".bss", ELFSection::SHT_NOBITS,
176 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
177 }
178
179 /// ELFSym - This struct contains information about each symbol that is
180 /// added to logical symbol table for the module. This is eventually
181 /// turned into a real symbol table in the file.
182 struct ELFSym {
183 const GlobalValue *GV; // The global value this corresponds to.
184 unsigned NameIdx; // Index in .strtab of name, once emitted.
185 uint64_t Value;
186 unsigned Size;
187 unsigned char Info;
188 unsigned char Other;
189 unsigned short SectionIdx;
190
191 enum { STB_LOCAL = 0, STB_GLOBAL = 1, STB_WEAK = 2 };
192 enum { STT_NOTYPE = 0, STT_OBJECT = 1, STT_FUNC = 2, STT_SECTION = 3,
193 STT_FILE = 4 };
194 ELFSym(const GlobalValue *gv) : GV(gv), Value(0), Size(0), Info(0),
195 Other(0), SectionIdx(0) {}
196
197 void SetBind(unsigned X) {
198 assert(X == (X & 0xF) && "Bind value out of range!");
199 Info = (Info & 0x0F) | (X << 4);
200 }
201 void SetType(unsigned X) {
202 assert(X == (X & 0xF) && "Type value out of range!");
203 Info = (Info & 0xF0) | X;
204 }
205 };
206
207 /// SymbolTable - This is the list of symbols we have emitted to the file.
208 /// This actually gets rearranged before emission to the file (to put the
209 /// local symbols first in the list).
210 std::vector<ELFSym> SymbolTable;
211
212 // As we complete the ELF file, we need to update fields in the ELF header
213 // (e.g. the location of the section table). These members keep track of
214 // the offset in ELFHeader of these various pieces to update and other
215 // locations in the file.
216 unsigned ELFHeader_e_shoff_Offset; // e_shoff in ELF header.
217 unsigned ELFHeader_e_shstrndx_Offset; // e_shstrndx in ELF header.
218 unsigned ELFHeader_e_shnum_Offset; // e_shnum in ELF header.
219 private:
220 void EmitGlobal(GlobalVariable *GV);
221
222 void EmitSymbolTable();
223
224 void EmitSectionTableStringTable();
225 void OutputSectionsAndSectionTable();
226 };
227}
228
229#endif