blob: be8edce08d7473d974af1ee14e6e0d9e389a70e5 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner35f0a4f2005-06-27 06:29:00 +00007//
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.
Chris Lattner35f0a4f2005-06-27 06:29:00 +000028//
29// NOTE: This code should eventually be extended to support 64-bit ELF (this
30// won't be hard), but we haven't done so yet!
31//
32//===----------------------------------------------------------------------===//
33
Bill Wendling8f84f1f2007-02-08 01:35:27 +000034#include "ELFWriter.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000035#include "ELFCodeEmitter.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000036#include "llvm/Module.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000037#include "llvm/PassManager.h"
Chris Lattner02b73ab2009-01-04 20:19:20 +000038#include "llvm/DerivedTypes.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000039#include "llvm/CodeGen/FileWriters.h"
Chris Lattneraa507db2005-07-11 05:17:18 +000040#include "llvm/CodeGen/MachineCodeEmitter.h"
41#include "llvm/CodeGen/MachineConstantPool.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000042#include "llvm/CodeGen/MachineFunctionPass.h"
Owen Anderson07000c62006-05-12 06:33:49 +000043#include "llvm/Target/TargetData.h"
Bill Wendling5d73a2a2007-01-27 02:55:44 +000044#include "llvm/Target/TargetELFWriterInfo.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000045#include "llvm/Target/TargetMachine.h"
Chris Lattner5acd1202005-07-11 03:11:47 +000046#include "llvm/Support/Mangler.h"
Bill Wendling203d3e42007-01-17 22:22:31 +000047#include "llvm/Support/OutputBuffer.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000048#include "llvm/Support/Streams.h"
Owen Andersoncb371882008-08-21 00:14:44 +000049#include "llvm/Support/raw_ostream.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000050#include <list>
Chris Lattner35f0a4f2005-06-27 06:29:00 +000051using namespace llvm;
52
Devang Patel19974732007-05-03 01:11:54 +000053char ELFWriter::ID = 0;
Bill Wendling8f84f1f2007-02-08 01:35:27 +000054/// AddELFWriter - Concrete function to add the ELF writer to the function pass
55/// manager.
Dan Gohmanbfae8312008-03-11 22:29:46 +000056MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000057 raw_ostream &O,
Bill Wendling8f84f1f2007-02-08 01:35:27 +000058 TargetMachine &TM) {
59 ELFWriter *EW = new ELFWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000060 PM.add(EW);
Bill Wendling8f84f1f2007-02-08 01:35:27 +000061 return &EW->getMachineCodeEmitter();
62}
63
Chris Lattner0e180502005-07-11 06:34:30 +000064//===----------------------------------------------------------------------===//
Chris Lattner0e180502005-07-11 06:34:30 +000065// ELFWriter Implementation
66//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +000067
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000068ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm)
Dan Gohmanae73dc12008-09-04 17:05:41 +000069 : MachineFunctionPass(&ID), O(o), TM(tm) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +000070 e_flags = 0; // e_flags defaults to 0, no flags.
71
Owen Andersona69571c2006-05-03 01:29:57 +000072 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
73 isLittleEndian = TM.getTargetData()->isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +000074
75 // Create the machine code emitter object for this target.
Bill Wendlinge9116152007-01-17 09:06:13 +000076 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +000077 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000078}
79
80ELFWriter::~ELFWriter() {
81 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +000082}
83
84// doInitialization - Emit the file header and all of the global variables for
85// the module to the ELF file.
86bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +000087 Mang = new Mangler(M);
88
Chris Lattner5f48ff72005-07-16 08:01:13 +000089 // Local alias to shortenify coming code.
90 std::vector<unsigned char> &FH = FileHeader;
Bill Wendlingc904a5b2007-01-18 01:23:11 +000091 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Jeff Cohen00b168892005-07-27 06:12:32 +000092
Bill Wendling203d3e42007-01-17 22:22:31 +000093 FHOut.outbyte(0x7F); // EI_MAG0
94 FHOut.outbyte('E'); // EI_MAG1
95 FHOut.outbyte('L'); // EI_MAG2
96 FHOut.outbyte('F'); // EI_MAG3
97 FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
98 FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
99 FHOut.outbyte(1); // EI_VERSION
Bill Wendlinge9116152007-01-17 09:06:13 +0000100 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen00b168892005-07-27 06:12:32 +0000101
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000102 // This should change for shared objects.
Bill Wendling203d3e42007-01-17 22:22:31 +0000103 FHOut.outhalf(1); // e_type = ET_REL
Dan Gohman87c3d412008-05-05 16:48:32 +0000104 FHOut.outhalf(TM.getELFWriterInfo()->getEMachine()); // target-defined
Bill Wendling203d3e42007-01-17 22:22:31 +0000105 FHOut.outword(1); // e_version = 1
106 FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
107 FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000108
Chris Lattner5f48ff72005-07-16 08:01:13 +0000109 ELFHeader_e_shoff_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000110 FHOut.outaddr(0); // e_shoff
111 FHOut.outword(e_flags); // e_flags = whatever the target wants
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000112
Bill Wendling203d3e42007-01-17 22:22:31 +0000113 FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
114 FHOut.outhalf(0); // e_phentsize = prog header entry size
115 FHOut.outhalf(0); // e_phnum = # prog header entries = 0
116 FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000117
Jeff Cohen00b168892005-07-27 06:12:32 +0000118
Chris Lattner5f48ff72005-07-16 08:01:13 +0000119 ELFHeader_e_shnum_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000120 FHOut.outhalf(0); // e_shnum = # of section header ents
Chris Lattner5f48ff72005-07-16 08:01:13 +0000121 ELFHeader_e_shstrndx_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000122 FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000123
Chris Lattner5f48ff72005-07-16 08:01:13 +0000124 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000125 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000126
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000127 // Start up the symbol table. The first entry in the symtab is the null
128 // entry.
129 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000130
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000131 return false;
132}
133
Chris Lattner05895232005-07-16 17:41:06 +0000134void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000135 // If this is an external global, emit it now. TODO: Note that it would be
136 // better to ignore the symbol here and only add it to the symbol table if
137 // referenced.
138 if (!GV->hasInitializer()) {
139 ELFSym ExternalSym(GV);
140 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
141 ExternalSym.SetType(ELFSym::STT_NOTYPE);
142 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
143 SymbolTable.push_back(ExternalSym);
144 return;
145 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000146
Duncan Sandsd1025932008-01-29 06:23:44 +0000147 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
Chris Lattner02b73ab2009-01-04 20:19:20 +0000148 unsigned Size =
Duncan Sands777d2302009-05-09 07:06:46 +0000149 TM.getTargetData()->getTypeAllocSize(GV->getType()->getElementType());
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000150
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000151 // If this global has a zero initializer, it is part of the .bss or common
152 // section.
153 if (GV->getInitializer()->isNullValue()) {
154 // If this global is part of the common block, add it now. Variables are
155 // part of the common block if they are zero initialized and allowed to be
156 // merged with other symbols.
Dale Johannesenaafce772008-05-14 20:12:51 +0000157 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
158 GV->hasCommonLinkage()) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000159 ELFSym CommonSym(GV);
160 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000161 CommonSym.Value = Align;
162 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000163 CommonSym.SetBind(ELFSym::STB_GLOBAL);
164 CommonSym.SetType(ELFSym::STT_OBJECT);
165 // TODO SOMEDAY: add ELF visibility.
166 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
167 SymbolTable.push_back(CommonSym);
168 return;
169 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000170
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000171 // Otherwise, this symbol is part of the .bss section. Emit it now.
172
173 // Handle alignment. Ensure section is aligned at least as much as required
174 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000175 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000176 BSSSection.Align = std::max(BSSSection.Align, Align);
177
178 // Within the section, emit enough virtual padding to get us to an alignment
179 // boundary.
180 if (Align)
181 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
182
183 ELFSym BSSSym(GV);
184 BSSSym.Value = BSSSection.Size;
185 BSSSym.Size = Size;
186 BSSSym.SetType(ELFSym::STT_OBJECT);
187
188 switch (GV->getLinkage()) {
Dale Johannesenaafce772008-05-14 20:12:51 +0000189 default: // weak/linkonce/common handled above
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000190 assert(0 && "Unexpected linkage type!");
191 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
192 case GlobalValue::ExternalLinkage:
193 BSSSym.SetBind(ELFSym::STB_GLOBAL);
194 break;
195 case GlobalValue::InternalLinkage:
196 BSSSym.SetBind(ELFSym::STB_LOCAL);
197 break;
198 }
199
200 // Set the idx of the .bss section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000201 BSSSym.SectionIdx = BSSSection.SectionIdx;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000202 if (!GV->hasPrivateLinkage())
203 SymbolTable.push_back(BSSSym);
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000204
205 // Reserve space in the .bss section for this symbol.
206 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000207 return;
208 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000209
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000210 // FIXME: handle .rodata
211 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000212
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000213 // FIXME: handle .data
214 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000215}
216
217
218bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000219 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000220 return false;
221}
222
223/// doFinalization - Now that the module has been completely processed, emit
224/// the ELF file to 'O'.
225bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000226 // Okay, the ELF header and .text sections have been completed, build the
227 // .data, .bss, and "common" sections next.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000228 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
229 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000230 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000231
232 // Emit the symbol table now, if non-empty.
233 EmitSymbolTable();
234
235 // FIXME: Emit the relocations now.
236
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000237 // Emit the string table for the sections in the ELF file we have.
238 EmitSectionTableStringTable();
239
Chris Lattner5f48ff72005-07-16 08:01:13 +0000240 // Emit the sections to the .o file, and emit the section table for the file.
241 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000242
Chris Lattner5f48ff72005-07-16 08:01:13 +0000243 // We are done with the abstract symbols.
244 SectionList.clear();
245 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000246
247 // Release the name mangler object.
248 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000249 return false;
250}
251
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000252/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
253/// table for it and then the symbol table itself.
254void ELFWriter::EmitSymbolTable() {
255 if (SymbolTable.size() == 1) return; // Only the null entry.
256
257 // FIXME: compact all local symbols to the start of the symtab.
258 unsigned FirstNonLocalSymbol = 1;
259
Chris Lattner00033952005-07-16 17:36:04 +0000260 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000261 StrTab.Align = 1;
262
Chris Lattner5f48ff72005-07-16 08:01:13 +0000263 DataBuffer &StrTabBuf = StrTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000264 OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000265
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000266 // Set the zero'th symbol to a null byte, as required.
Bill Wendling203d3e42007-01-17 22:22:31 +0000267 StrTabOut.outbyte(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000268 SymbolTable[0].NameIdx = 0;
269 unsigned Index = 1;
270 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000271 // Use the name mangler to uniquify the LLVM symbol.
272 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000273
274 if (Name.empty()) {
275 SymbolTable[i].NameIdx = 0;
276 } else {
277 SymbolTable[i].NameIdx = Index;
278
279 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000280 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000281
282 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000283 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000284
285 // Keep track of the number of bytes emitted to this section.
286 Index += Name.size()+1;
287 }
288 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000289 assert(Index == StrTabBuf.size());
290 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000291
292 // Now that we have emitted the string table and know the offset into the
293 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000294 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000295 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000296 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000297 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
298 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000299 DataBuffer &SymTabBuf = SymTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000300 OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000301
Chris Lattner46c53052005-07-12 06:57:52 +0000302 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
303 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
304 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000305 SymTabOut.outword(Sym.NameIdx);
306 SymTabOut.outaddr32(Sym.Value);
307 SymTabOut.outword(Sym.Size);
308 SymTabOut.outbyte(Sym.Info);
309 SymTabOut.outbyte(Sym.Other);
310 SymTabOut.outhalf(Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000311 }
312 } else {
313 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
314 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000315 SymTabOut.outword(Sym.NameIdx);
316 SymTabOut.outbyte(Sym.Info);
317 SymTabOut.outbyte(Sym.Other);
318 SymTabOut.outhalf(Sym.SectionIdx);
319 SymTabOut.outaddr64(Sym.Value);
320 SymTabOut.outxword(Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000321 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000322 }
323
Chris Lattner5f48ff72005-07-16 08:01:13 +0000324 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000325}
326
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000327/// EmitSectionTableStringTable - This method adds and emits a section for the
328/// ELF Section Table string table: the string table that holds all of the
329/// section names.
330void ELFWriter::EmitSectionTableStringTable() {
331 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000332 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000333
334 // Now that we know which section number is the .shstrtab section, update the
335 // e_shstrndx entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000336 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000337 FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000338
339 // Set the NameIdx of each section in the string table and emit the bytes for
340 // the string table.
341 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000342 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000343
Chris Lattner5f48ff72005-07-16 08:01:13 +0000344 for (std::list<ELFSection>::iterator I = SectionList.begin(),
345 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000346 // Set the index into the table. Note if we have lots of entries with
347 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000348 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000349
350 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000351 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
352
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000353 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000354 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000355
356 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000357 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000358 }
359
360 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000361 assert(Index == Buf.size());
362 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000363}
364
Chris Lattner5f48ff72005-07-16 08:01:13 +0000365/// OutputSectionsAndSectionTable - Now that we have constructed the file header
366/// and all of the sections, emit these to the ostream destination and emit the
367/// SectionTable.
368void ELFWriter::OutputSectionsAndSectionTable() {
369 // Pass #1: Compute the file offset for each section.
370 size_t FileOff = FileHeader.size(); // File header first.
371
372 // Emit all of the section data in order.
373 for (std::list<ELFSection>::iterator I = SectionList.begin(),
374 E = SectionList.end(); I != E; ++I) {
375 // Align FileOff to whatever the alignment restrictions of the section are.
376 if (I->Align)
377 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
378 I->Offset = FileOff;
379 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000380 }
381
Chris Lattner5f48ff72005-07-16 08:01:13 +0000382 // Align Section Header.
383 unsigned TableAlign = is64Bit ? 8 : 4;
384 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
385
386 // Now that we know where all of the sections will be emitted, set the e_shnum
387 // entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000388 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000389 FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000390
Chris Lattner5f48ff72005-07-16 08:01:13 +0000391 // Now that we know the offset in the file of the section table, update the
392 // e_shoff address in the ELF header.
Bill Wendling203d3e42007-01-17 22:22:31 +0000393 FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000394
Chris Lattner5f48ff72005-07-16 08:01:13 +0000395 // Now that we know all of the data in the file header, emit it and all of the
396 // sections!
397 O.write((char*)&FileHeader[0], FileHeader.size());
398 FileOff = FileHeader.size();
399 DataBuffer().swap(FileHeader);
400
401 DataBuffer Table;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000402 OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000403
404 // Emit all of the section data and build the section table itself.
405 while (!SectionList.empty()) {
406 const ELFSection &S = *SectionList.begin();
407
408 // Align FileOff to whatever the alignment restrictions of the section are.
409 if (S.Align)
410 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
411 FileOff != NewFileOff; ++FileOff)
Owen Andersoncb371882008-08-21 00:14:44 +0000412 O << (char)0xAB;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000413 O.write((char*)&S.SectionData[0], S.SectionData.size());
414 FileOff += S.SectionData.size();
415
Bill Wendling203d3e42007-01-17 22:22:31 +0000416 TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
417 TableOut.outword(S.Type); // sh_type - Section contents & semantics
418 TableOut.outword(S.Flags); // sh_flags - Section flags.
419 TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
420 TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
421 TableOut.outword(S.Size); // sh_size - The section size.
422 TableOut.outword(S.Link); // sh_link - Section header table index link.
423 TableOut.outword(S.Info); // sh_info - Auxillary information.
424 TableOut.outword(S.Align); // sh_addralign - Alignment of section.
425 TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000426
427 SectionList.pop_front();
428 }
429
430 // Align output for the section table.
431 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
432 FileOff != NewFileOff; ++FileOff)
Owen Andersoncb371882008-08-21 00:14:44 +0000433 O << (char)0xAB;
Jeff Cohen00b168892005-07-27 06:12:32 +0000434
Chris Lattner5f48ff72005-07-16 08:01:13 +0000435 // Emit the section table itself.
436 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000437}