blob: a25fbddee3d466b33ddafcab2a33e0cdd0c7d810 [file] [log] [blame]
Chris Lattner35f0a4f2005-06-27 06:29:00 +00001//===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the target-independent ELF writer. This file writes out
11// the ELF file in the following order:
12//
13// #1. ELF Header
Chris Lattner80ed8fa2005-07-07 07:02:20 +000014// #2. '.text' section
15// #3. '.data' section
16// #4. '.bss' section (conceptual position in file)
Chris Lattner35f0a4f2005-06-27 06:29:00 +000017// ...
18// #X. '.shstrtab' section
19// #Y. Section Table
20//
21// The entries in the section table are laid out as:
22// #0. Null entry [required]
Chris Lattner80ed8fa2005-07-07 07:02:20 +000023// #1. ".text" entry - the program code
24// #2. ".data" entry - global variables with initializers. [ if needed ]
25// #3. ".bss" entry - global variables without initializers. [ if needed ]
Chris Lattner35f0a4f2005-06-27 06:29:00 +000026// ...
27// #N. ".shstrtab" entry - String table for the section names.
28
29//
30// NOTE: This code should eventually be extended to support 64-bit ELF (this
31// won't be hard), but we haven't done so yet!
32//
33//===----------------------------------------------------------------------===//
34
35#include "llvm/CodeGen/ELFWriter.h"
36#include "llvm/Module.h"
37#include "llvm/Target/TargetMachine.h"
38using namespace llvm;
39
40ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
41 e_machine = 0; // e_machine defaults to 'No Machine'
42 e_flags = 0; // e_flags defaults to 0, no flags.
43
44 is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
45 isLittleEndian = TM.getTargetData().isLittleEndian();
46}
47
48// doInitialization - Emit the file header and all of the global variables for
49// the module to the ELF file.
50bool ELFWriter::doInitialization(Module &M) {
51 outbyte(0x7F); // EI_MAG0
52 outbyte('E'); // EI_MAG1
53 outbyte('L'); // EI_MAG2
54 outbyte('F'); // EI_MAG3
55 outbyte(is64Bit ? 2 : 1); // EI_CLASS
56 outbyte(isLittleEndian ? 1 : 2); // EI_DATA
57 outbyte(1); // EI_VERSION
58 for (unsigned i = OutputBuffer.size(); i != 16; ++i)
59 outbyte(0); // EI_PAD up to 16 bytes.
60
61 // This should change for shared objects.
62 outhalf(1); // e_type = ET_REL
63 outhalf(e_machine); // e_machine = whatever the target wants
64 outword(1); // e_version = 1
65 outaddr(0); // e_entry = 0 -> no entry point in .o file
66 outaddr(0); // e_phoff = 0 -> no program header for .o
67
68 ELFHeader_e_shoff_Offset = OutputBuffer.size();
69 outaddr(0); // e_shoff
70 outword(e_flags); // e_flags = whatever the target wants
71
72 assert(!is64Bit && "These sizes need to be adjusted for 64-bit!");
73 outhalf(52); // e_ehsize = ELF header size
74 outhalf(0); // e_phentsize = prog header entry size
75 outhalf(0); // e_phnum = # prog header entries = 0
76 outhalf(40); // e_shentsize = sect header entry size
77
78
79 ELFHeader_e_shnum_Offset = OutputBuffer.size();
80 outhalf(0); // e_shnum = # of section header ents
81 ELFHeader_e_shstrndx_Offset = OutputBuffer.size();
82 outhalf(0); // e_shstrndx = Section # of '.shstrtab'
83
84 // Add the null section.
85 SectionList.push_back(ELFSection());
86
Chris Lattner80ed8fa2005-07-07 07:02:20 +000087 // Start up the symbol table. The first entry in the symtab is the null
88 // entry.
89 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +000090
Chris Lattner35f0a4f2005-06-27 06:29:00 +000091
Chris Lattner35f0a4f2005-06-27 06:29:00 +000092
Chris Lattner80ed8fa2005-07-07 07:02:20 +000093 // FIXME: Should start the .text section.
Chris Lattner35f0a4f2005-06-27 06:29:00 +000094 return false;
95}
96
Chris Lattner80ed8fa2005-07-07 07:02:20 +000097void ELFWriter::EmitGlobal(GlobalVariable *GV, ELFSection &DataSection,
98 ELFSection &BSSSection) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +000099 // If this is an external global, emit it now. TODO: Note that it would be
100 // better to ignore the symbol here and only add it to the symbol table if
101 // referenced.
102 if (!GV->hasInitializer()) {
103 ELFSym ExternalSym(GV);
104 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
105 ExternalSym.SetType(ELFSym::STT_NOTYPE);
106 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
107 SymbolTable.push_back(ExternalSym);
108 return;
109 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000110
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000111 const Type *GVType = (const Type*)GV->getType();
112 unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
113 unsigned Size = TM.getTargetData().getTypeSize(GVType);
114
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000115 // If this global has a zero initializer, it is part of the .bss or common
116 // section.
117 if (GV->getInitializer()->isNullValue()) {
118 // If this global is part of the common block, add it now. Variables are
119 // part of the common block if they are zero initialized and allowed to be
120 // merged with other symbols.
121 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
122 ELFSym CommonSym(GV);
123 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000124 CommonSym.Value = Align;
125 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000126 CommonSym.SetBind(ELFSym::STB_GLOBAL);
127 CommonSym.SetType(ELFSym::STT_OBJECT);
128 // TODO SOMEDAY: add ELF visibility.
129 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
130 SymbolTable.push_back(CommonSym);
131 return;
132 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000133
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000134 // Otherwise, this symbol is part of the .bss section. Emit it now.
135
136 // Handle alignment. Ensure section is aligned at least as much as required
137 // by this symbol.
138 BSSSection.Align = std::max(BSSSection.Align, Align);
139
140 // Within the section, emit enough virtual padding to get us to an alignment
141 // boundary.
142 if (Align)
143 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
144
145 ELFSym BSSSym(GV);
146 BSSSym.Value = BSSSection.Size;
147 BSSSym.Size = Size;
148 BSSSym.SetType(ELFSym::STT_OBJECT);
149
150 switch (GV->getLinkage()) {
151 default: // weak/linkonce handled above
152 assert(0 && "Unexpected linkage type!");
153 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
154 case GlobalValue::ExternalLinkage:
155 BSSSym.SetBind(ELFSym::STB_GLOBAL);
156 break;
157 case GlobalValue::InternalLinkage:
158 BSSSym.SetBind(ELFSym::STB_LOCAL);
159 break;
160 }
161
162 // Set the idx of the .bss section
163 BSSSym.SectionIdx = &BSSSection-&SectionList[0];
164 SymbolTable.push_back(BSSSym);
165
166 // Reserve space in the .bss section for this symbol.
167 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000168 return;
169 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000170
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000171 // FIXME: handle .rodata
172 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000173
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000174 // FIXME: handle .data
175 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000176}
177
178
179bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
180 return false;
181}
182
183/// doFinalization - Now that the module has been completely processed, emit
184/// the ELF file to 'O'.
185bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000186 // Okay, the .text section has now been finalized.
187 // FIXME: finalize the .text section.
188
189 // Okay, the ELF header and .text sections have been completed, build the
190 // .data, .bss, and "common" sections next.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000191 SectionList.push_back(ELFSection(".data", OutputBuffer.size()));
192 SectionList.push_back(ELFSection(".bss"));
193 ELFSection &DataSection = *(SectionList.end()-2);
194 ELFSection &BSSSection = SectionList.back();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000195 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
196 I != E; ++I)
197 EmitGlobal(I, DataSection, BSSSection);
198
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000199 // Finish up the data section.
200 DataSection.Type = ELFSection::SHT_PROGBITS;
201 DataSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000202
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000203 // The BSS Section logically starts at the end of the Data Section (adjusted
204 // to the required alignment of the BSSSection).
205 BSSSection.Offset = DataSection.Offset+DataSection.Size;
206 BSSSection.Type = ELFSection::SHT_NOBITS;
207 BSSSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC;
208 if (BSSSection.Align)
209 BSSSection.Offset = (BSSSection.Offset+BSSSection.Align-1) &
210 ~(BSSSection.Align-1);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000211
212 // Emit the symbol table now, if non-empty.
213 EmitSymbolTable();
214
215 // FIXME: Emit the relocations now.
216
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000217 // Emit the string table for the sections in the ELF file we have.
218 EmitSectionTableStringTable();
219
220 // Emit the .o file section table.
221 EmitSectionTable();
222
223 // Emit the .o file to the specified stream.
224 O.write((char*)&OutputBuffer[0], OutputBuffer.size());
225
226 // Free the output buffer.
227 std::vector<unsigned char>().swap(OutputBuffer);
228 return false;
229}
230
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000231/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
232/// table for it and then the symbol table itself.
233void ELFWriter::EmitSymbolTable() {
234 if (SymbolTable.size() == 1) return; // Only the null entry.
235
236 // FIXME: compact all local symbols to the start of the symtab.
237 unsigned FirstNonLocalSymbol = 1;
238
239 SectionList.push_back(ELFSection(".strtab", OutputBuffer.size()));
240 ELFSection &StrTab = SectionList.back();
241 StrTab.Type = ELFSection::SHT_STRTAB;
242 StrTab.Align = 1;
243
244 // Set the zero'th symbol to a null byte, as required.
245 outbyte(0);
246 SymbolTable[0].NameIdx = 0;
247 unsigned Index = 1;
248 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
249 // FIXME: USE A MANGLER!!
250 const std::string &Name = SymbolTable[i].GV->getName();
251
252 if (Name.empty()) {
253 SymbolTable[i].NameIdx = 0;
254 } else {
255 SymbolTable[i].NameIdx = Index;
256
257 // Add the name to the output buffer, including the null terminator.
258 OutputBuffer.insert(OutputBuffer.end(), Name.begin(), Name.end());
259
260 // Add a null terminator.
261 OutputBuffer.push_back(0);
262
263 // Keep track of the number of bytes emitted to this section.
264 Index += Name.size()+1;
265 }
266 }
267
268 StrTab.Size = OutputBuffer.size()-StrTab.Offset;
269
270 // Now that we have emitted the string table and know the offset into the
271 // string table of each symbol, emit the symbol table itself.
272 assert(!is64Bit && "Should this be 8 byte aligned for 64-bit?"
273 " (check .Align below also)");
274 align(4);
275
276 SectionList.push_back(ELFSection(".symtab", OutputBuffer.size()));
277 ELFSection &SymTab = SectionList.back();
278 SymTab.Type = ELFSection::SHT_SYMTAB;
279 SymTab.Align = 4; // FIXME: check for ELF64
280 SymTab.Link = SectionList.size()-2; // Section Index of .strtab.
281 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
282 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
283
284 assert(!is64Bit && "check this!");
285 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
286 ELFSym &Sym = SymbolTable[i];
287 outword(Sym.NameIdx);
288 outaddr(Sym.Value);
289 outword(Sym.Size);
290 outbyte(Sym.Info);
291 outbyte(Sym.Other);
292 outhalf(Sym.SectionIdx);
293 }
294
295 SymTab.Size = OutputBuffer.size()-SymTab.Offset;
296}
297
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000298/// EmitSectionTableStringTable - This method adds and emits a section for the
299/// ELF Section Table string table: the string table that holds all of the
300/// section names.
301void ELFWriter::EmitSectionTableStringTable() {
302 // First step: add the section for the string table to the list of sections:
303 SectionList.push_back(ELFSection(".shstrtab", OutputBuffer.size()));
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000304 SectionList.back().Type = ELFSection::SHT_STRTAB;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000305
306 // Now that we know which section number is the .shstrtab section, update the
307 // e_shstrndx entry in the ELF header.
308 fixhalf(SectionList.size()-1, ELFHeader_e_shstrndx_Offset);
309
310 // Set the NameIdx of each section in the string table and emit the bytes for
311 // the string table.
312 unsigned Index = 0;
313
314 for (unsigned i = 0, e = SectionList.size(); i != e; ++i) {
315 // Set the index into the table. Note if we have lots of entries with
316 // common suffixes, we could memoize them here if we cared.
317 SectionList[i].NameIdx = Index;
318
319 // Add the name to the output buffer, including the null terminator.
320 OutputBuffer.insert(OutputBuffer.end(), SectionList[i].Name.begin(),
321 SectionList[i].Name.end());
322 // Add a null terminator.
323 OutputBuffer.push_back(0);
324
325 // Keep track of the number of bytes emitted to this section.
326 Index += SectionList[i].Name.size()+1;
327 }
328
329 // Set the size of .shstrtab now that we know what it is.
330 SectionList.back().Size = Index;
331}
332
333/// EmitSectionTable - Now that we have emitted the entire contents of the file
334/// (all of the sections), emit the section table which informs the reader where
335/// the boundaries are.
336void ELFWriter::EmitSectionTable() {
337 // Now that all of the sections have been emitted, set the e_shnum entry in
338 // the ELF header.
339 fixhalf(SectionList.size(), ELFHeader_e_shnum_Offset);
340
341 // Now that we know the offset in the file of the section table (which we emit
342 // next), update the e_shoff address in the ELF header.
343 fixaddr(OutputBuffer.size(), ELFHeader_e_shoff_Offset);
344
345 // Emit all of the section table entries.
346 for (unsigned i = 0, e = SectionList.size(); i != e; ++i) {
347 const ELFSection &S = SectionList[i];
348 outword(S.NameIdx); // sh_name - Symbol table name idx
349 outword(S.Type); // sh_type - Section contents & semantics
350 outword(S.Flags); // sh_flags - Section flags.
351 outaddr(S.Addr); // sh_addr - The mem address this section appears in.
352 outaddr(S.Offset); // sh_offset - The offset from the start of the file.
353 outword(S.Size); // sh_size - The section size.
354 outword(S.Link); // sh_link - Section header table index link.
355 outword(S.Info); // sh_info - Auxillary information.
356 outword(S.Align); // sh_addralign - Alignment of section.
357 outword(S.EntSize); // sh_entsize - Size of each entry in the section.
358 }
359
360 // Release the memory allocated for the section list.
361 std::vector<ELFSection>().swap(SectionList);
362}