blob: b698178d780db6007b36cf325c3548f65d46ca05 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
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 implements the target-independent ELF writer. This file writes out
11// the ELF file in the following order:
12//
13// #1. ELF Header
14// #2. '.text' section
15// #3. '.data' section
16// #4. '.bss' section (conceptual position in file)
17// ...
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]
23// #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 ]
26// ...
27// #N. ".shstrtab" entry - String table for the section names.
28//
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
34#include "ELFWriter.h"
35#include "llvm/Module.h"
36#include "llvm/PassManager.h"
Chris Lattnerb1528b22009-01-04 20:19:20 +000037#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/CodeGen/FileWriters.h"
39#include "llvm/CodeGen/MachineCodeEmitter.h"
40#include "llvm/CodeGen/MachineConstantPool.h"
41#include "llvm/CodeGen/MachineFunctionPass.h"
42#include "llvm/Target/TargetData.h"
43#include "llvm/Target/TargetELFWriterInfo.h"
44#include "llvm/Target/TargetMachine.h"
45#include "llvm/Support/Mangler.h"
46#include "llvm/Support/OutputBuffer.h"
47#include "llvm/Support/Streams.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000048#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049#include <list>
50using namespace llvm;
51
52char ELFWriter::ID = 0;
53/// AddELFWriter - Concrete function to add the ELF writer to the function pass
54/// manager.
Dan Gohmane34aa772008-03-11 22:29:46 +000055MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
Owen Anderson847b99b2008-08-21 00:14:44 +000056 raw_ostream &O,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 TargetMachine &TM) {
58 ELFWriter *EW = new ELFWriter(O, TM);
Dan Gohmane34aa772008-03-11 22:29:46 +000059 PM.add(EW);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 return &EW->getMachineCodeEmitter();
61}
62
63//===----------------------------------------------------------------------===//
64// ELFCodeEmitter Implementation
65//===----------------------------------------------------------------------===//
66
67namespace llvm {
68 /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
69 /// functions to the ELF file.
70 class ELFCodeEmitter : public MachineCodeEmitter {
71 ELFWriter &EW;
72 TargetMachine &TM;
73 ELFWriter::ELFSection *ES; // Section to write to.
74 std::vector<unsigned char> *OutBuffer;
75 size_t FnStart;
76 public:
Dan Gohmanc43c7f42007-12-14 15:41:34 +000077 explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
79 void startFunction(MachineFunction &F);
80 bool finishFunction(MachineFunction &F);
81
82 void addRelocation(const MachineRelocation &MR) {
83 assert(0 && "relo not handled yet!");
84 }
85
86 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
87 }
88
Evan Cheng6e561c72008-12-10 02:32:19 +000089 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 assert(0 && "CP not implementated yet!");
91 return 0;
92 }
Evan Cheng6e561c72008-12-10 02:32:19 +000093 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 assert(0 && "JT not implementated yet!");
95 return 0;
96 }
97
Evan Cheng6e561c72008-12-10 02:32:19 +000098 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 assert(0 && "JT not implementated yet!");
100 return 0;
101 }
102
Evan Cheng6e561c72008-12-10 02:32:19 +0000103 virtual uintptr_t getLabelAddress(uint64_t Label) const {
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000104 assert(0 && "Label address not implementated yet!");
105 abort();
106 return 0;
107 }
108
109 virtual void emitLabel(uint64_t LabelID) {
110 assert(0 && "emit Label not implementated yet!");
111 abort();
112 }
113
114
115 virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
116
117
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Evan Cheng2c3267a2008-11-08 08:02:53 +0000119 void startGVStub(const GlobalValue* F, unsigned StubSize,
120 unsigned Alignment = 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 assert(0 && "JIT specific function called!");
122 abort();
123 }
Evan Cheng2c3267a2008-11-08 08:02:53 +0000124 void *finishGVStub(const GlobalValue *F) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 assert(0 && "JIT specific function called!");
126 abort();
127 return 0;
128 }
129 };
130}
131
132/// startFunction - This callback is invoked when a new machine function is
133/// about to be emitted.
134void ELFCodeEmitter::startFunction(MachineFunction &F) {
135 // Align the output buffer to the appropriate alignment.
136 unsigned Align = 16; // FIXME: GENERICIZE!!
137 // Get the ELF Section that this function belongs in.
138 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
139 ELFWriter::ELFSection::SHF_EXECINSTR |
140 ELFWriter::ELFSection::SHF_ALLOC);
141 OutBuffer = &ES->SectionData;
142 cerr << "FIXME: This code needs to be updated for changes in the "
143 << "CodeEmitter interfaces. In particular, this should set "
144 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
145 abort();
146
147 // Upgrade the section alignment if required.
148 if (ES->Align < Align) ES->Align = Align;
149
150 // Add padding zeros to the end of the buffer to make sure that the
151 // function will start on the correct byte alignment within the section.
152 OutputBuffer OB(*OutBuffer,
153 TM.getTargetData()->getPointerSizeInBits() == 64,
154 TM.getTargetData()->isLittleEndian());
155 OB.align(Align);
156 FnStart = OutBuffer->size();
157}
158
159/// finishFunction - This callback is invoked after the function is completely
160/// finished.
161bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
162 // We now know the size of the function, add a symbol to represent it.
163 ELFWriter::ELFSym FnSym(F.getFunction());
164
165 // Figure out the binding (linkage) of the symbol.
166 switch (F.getFunction()->getLinkage()) {
167 default:
168 // appending linkage is illegal for functions.
169 assert(0 && "Unknown linkage type!");
170 case GlobalValue::ExternalLinkage:
171 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
172 break;
173 case GlobalValue::LinkOnceLinkage:
174 case GlobalValue::WeakLinkage:
175 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
176 break;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000177 case GlobalValue::PrivateLinkage:
178 assert (0 && "PrivateLinkage should not be in the symbol table.");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 case GlobalValue::InternalLinkage:
180 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
181 break;
182 }
183
184 ES->Size = OutBuffer->size();
185
186 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
187 FnSym.SectionIdx = ES->SectionIdx;
188 FnSym.Value = FnStart; // Value = Offset from start of Section.
189 FnSym.Size = OutBuffer->size()-FnStart;
190
191 // Finally, add it to the symtab.
192 EW.SymbolTable.push_back(FnSym);
193 return false;
194}
195
196//===----------------------------------------------------------------------===//
197// ELFWriter Implementation
198//===----------------------------------------------------------------------===//
199
Owen Anderson847b99b2008-08-21 00:14:44 +0000200ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm)
Dan Gohman26f8c272008-09-04 17:05:41 +0000201 : MachineFunctionPass(&ID), O(o), TM(tm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 e_flags = 0; // e_flags defaults to 0, no flags.
203
204 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
205 isLittleEndian = TM.getTargetData()->isLittleEndian();
206
207 // Create the machine code emitter object for this target.
208 MCE = new ELFCodeEmitter(*this);
209 NumSections = 0;
210}
211
212ELFWriter::~ELFWriter() {
213 delete MCE;
214}
215
216// doInitialization - Emit the file header and all of the global variables for
217// the module to the ELF file.
218bool ELFWriter::doInitialization(Module &M) {
219 Mang = new Mangler(M);
220
221 // Local alias to shortenify coming code.
222 std::vector<unsigned char> &FH = FileHeader;
223 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
224
225 FHOut.outbyte(0x7F); // EI_MAG0
226 FHOut.outbyte('E'); // EI_MAG1
227 FHOut.outbyte('L'); // EI_MAG2
228 FHOut.outbyte('F'); // EI_MAG3
229 FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
230 FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
231 FHOut.outbyte(1); // EI_VERSION
232 FH.resize(16); // EI_PAD up to 16 bytes.
233
234 // This should change for shared objects.
235 FHOut.outhalf(1); // e_type = ET_REL
Dan Gohman8926a642008-05-05 16:48:32 +0000236 FHOut.outhalf(TM.getELFWriterInfo()->getEMachine()); // target-defined
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 FHOut.outword(1); // e_version = 1
238 FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
239 FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
240
241 ELFHeader_e_shoff_Offset = FH.size();
242 FHOut.outaddr(0); // e_shoff
243 FHOut.outword(e_flags); // e_flags = whatever the target wants
244
245 FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
246 FHOut.outhalf(0); // e_phentsize = prog header entry size
247 FHOut.outhalf(0); // e_phnum = # prog header entries = 0
248 FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
249
250
251 ELFHeader_e_shnum_Offset = FH.size();
252 FHOut.outhalf(0); // e_shnum = # of section header ents
253 ELFHeader_e_shstrndx_Offset = FH.size();
254 FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
255
256 // Add the null section, which is required to be first in the file.
257 getSection("", 0, 0);
258
259 // Start up the symbol table. The first entry in the symtab is the null
260 // entry.
261 SymbolTable.push_back(ELFSym(0));
262
263 return false;
264}
265
266void ELFWriter::EmitGlobal(GlobalVariable *GV) {
267 // If this is an external global, emit it now. TODO: Note that it would be
268 // better to ignore the symbol here and only add it to the symbol table if
269 // referenced.
270 if (!GV->hasInitializer()) {
271 ELFSym ExternalSym(GV);
272 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
273 ExternalSym.SetType(ELFSym::STT_NOTYPE);
274 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
275 SymbolTable.push_back(ExternalSym);
276 return;
277 }
278
Duncan Sands935686e2008-01-29 06:23:44 +0000279 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
Chris Lattnerb1528b22009-01-04 20:19:20 +0000280 unsigned Size =
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000281 TM.getTargetData()->getTypePaddedSize(GV->getType()->getElementType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282
283 // If this global has a zero initializer, it is part of the .bss or common
284 // section.
285 if (GV->getInitializer()->isNullValue()) {
286 // If this global is part of the common block, add it now. Variables are
287 // part of the common block if they are zero initialized and allowed to be
288 // merged with other symbols.
Dale Johannesen49c44122008-05-14 20:12:51 +0000289 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
290 GV->hasCommonLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 ELFSym CommonSym(GV);
292 // Value for common symbols is the alignment required.
293 CommonSym.Value = Align;
294 CommonSym.Size = Size;
295 CommonSym.SetBind(ELFSym::STB_GLOBAL);
296 CommonSym.SetType(ELFSym::STT_OBJECT);
297 // TODO SOMEDAY: add ELF visibility.
298 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
299 SymbolTable.push_back(CommonSym);
300 return;
301 }
302
303 // Otherwise, this symbol is part of the .bss section. Emit it now.
304
305 // Handle alignment. Ensure section is aligned at least as much as required
306 // by this symbol.
307 ELFSection &BSSSection = getBSSSection();
308 BSSSection.Align = std::max(BSSSection.Align, Align);
309
310 // Within the section, emit enough virtual padding to get us to an alignment
311 // boundary.
312 if (Align)
313 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
314
315 ELFSym BSSSym(GV);
316 BSSSym.Value = BSSSection.Size;
317 BSSSym.Size = Size;
318 BSSSym.SetType(ELFSym::STT_OBJECT);
319
320 switch (GV->getLinkage()) {
Dale Johannesen49c44122008-05-14 20:12:51 +0000321 default: // weak/linkonce/common handled above
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 assert(0 && "Unexpected linkage type!");
323 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
324 case GlobalValue::ExternalLinkage:
325 BSSSym.SetBind(ELFSym::STB_GLOBAL);
326 break;
327 case GlobalValue::InternalLinkage:
328 BSSSym.SetBind(ELFSym::STB_LOCAL);
329 break;
330 }
331
332 // Set the idx of the .bss section
333 BSSSym.SectionIdx = BSSSection.SectionIdx;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000334 if (!GV->hasPrivateLinkage())
335 SymbolTable.push_back(BSSSym);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336
337 // Reserve space in the .bss section for this symbol.
338 BSSSection.Size += Size;
339 return;
340 }
341
342 // FIXME: handle .rodata
343 //assert(!GV->isConstant() && "unimp");
344
345 // FIXME: handle .data
346 //assert(0 && "unimp");
347}
348
349
350bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
351 // Nothing to do here, this is all done through the MCE object above.
352 return false;
353}
354
355/// doFinalization - Now that the module has been completely processed, emit
356/// the ELF file to 'O'.
357bool ELFWriter::doFinalization(Module &M) {
358 // Okay, the ELF header and .text sections have been completed, build the
359 // .data, .bss, and "common" sections next.
360 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
361 I != E; ++I)
362 EmitGlobal(I);
363
364 // Emit the symbol table now, if non-empty.
365 EmitSymbolTable();
366
367 // FIXME: Emit the relocations now.
368
369 // Emit the string table for the sections in the ELF file we have.
370 EmitSectionTableStringTable();
371
372 // Emit the sections to the .o file, and emit the section table for the file.
373 OutputSectionsAndSectionTable();
374
375 // We are done with the abstract symbols.
376 SectionList.clear();
377 NumSections = 0;
378
379 // Release the name mangler object.
380 delete Mang; Mang = 0;
381 return false;
382}
383
384/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
385/// table for it and then the symbol table itself.
386void ELFWriter::EmitSymbolTable() {
387 if (SymbolTable.size() == 1) return; // Only the null entry.
388
389 // FIXME: compact all local symbols to the start of the symtab.
390 unsigned FirstNonLocalSymbol = 1;
391
392 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
393 StrTab.Align = 1;
394
395 DataBuffer &StrTabBuf = StrTab.SectionData;
396 OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
397
398 // Set the zero'th symbol to a null byte, as required.
399 StrTabOut.outbyte(0);
400 SymbolTable[0].NameIdx = 0;
401 unsigned Index = 1;
402 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
403 // Use the name mangler to uniquify the LLVM symbol.
404 std::string Name = Mang->getValueName(SymbolTable[i].GV);
405
406 if (Name.empty()) {
407 SymbolTable[i].NameIdx = 0;
408 } else {
409 SymbolTable[i].NameIdx = Index;
410
411 // Add the name to the output buffer, including the null terminator.
412 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
413
414 // Add a null terminator.
415 StrTabBuf.push_back(0);
416
417 // Keep track of the number of bytes emitted to this section.
418 Index += Name.size()+1;
419 }
420 }
421 assert(Index == StrTabBuf.size());
422 StrTab.Size = Index;
423
424 // Now that we have emitted the string table and know the offset into the
425 // string table of each symbol, emit the symbol table itself.
426 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
427 SymTab.Align = is64Bit ? 8 : 4;
428 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
429 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
430 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
431 DataBuffer &SymTabBuf = SymTab.SectionData;
432 OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
433
434 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
435 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
436 ELFSym &Sym = SymbolTable[i];
437 SymTabOut.outword(Sym.NameIdx);
438 SymTabOut.outaddr32(Sym.Value);
439 SymTabOut.outword(Sym.Size);
440 SymTabOut.outbyte(Sym.Info);
441 SymTabOut.outbyte(Sym.Other);
442 SymTabOut.outhalf(Sym.SectionIdx);
443 }
444 } else {
445 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
446 ELFSym &Sym = SymbolTable[i];
447 SymTabOut.outword(Sym.NameIdx);
448 SymTabOut.outbyte(Sym.Info);
449 SymTabOut.outbyte(Sym.Other);
450 SymTabOut.outhalf(Sym.SectionIdx);
451 SymTabOut.outaddr64(Sym.Value);
452 SymTabOut.outxword(Sym.Size);
453 }
454 }
455
456 SymTab.Size = SymTabBuf.size();
457}
458
459/// EmitSectionTableStringTable - This method adds and emits a section for the
460/// ELF Section Table string table: the string table that holds all of the
461/// section names.
462void ELFWriter::EmitSectionTableStringTable() {
463 // First step: add the section for the string table to the list of sections:
464 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
465
466 // Now that we know which section number is the .shstrtab section, update the
467 // e_shstrndx entry in the ELF header.
468 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
469 FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
470
471 // Set the NameIdx of each section in the string table and emit the bytes for
472 // the string table.
473 unsigned Index = 0;
474 DataBuffer &Buf = SHStrTab.SectionData;
475
476 for (std::list<ELFSection>::iterator I = SectionList.begin(),
477 E = SectionList.end(); I != E; ++I) {
478 // Set the index into the table. Note if we have lots of entries with
479 // common suffixes, we could memoize them here if we cared.
480 I->NameIdx = Index;
481
482 // Add the name to the output buffer, including the null terminator.
483 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
484
485 // Add a null terminator.
486 Buf.push_back(0);
487
488 // Keep track of the number of bytes emitted to this section.
489 Index += I->Name.size()+1;
490 }
491
492 // Set the size of .shstrtab now that we know what it is.
493 assert(Index == Buf.size());
494 SHStrTab.Size = Index;
495}
496
497/// OutputSectionsAndSectionTable - Now that we have constructed the file header
498/// and all of the sections, emit these to the ostream destination and emit the
499/// SectionTable.
500void ELFWriter::OutputSectionsAndSectionTable() {
501 // Pass #1: Compute the file offset for each section.
502 size_t FileOff = FileHeader.size(); // File header first.
503
504 // Emit all of the section data in order.
505 for (std::list<ELFSection>::iterator I = SectionList.begin(),
506 E = SectionList.end(); I != E; ++I) {
507 // Align FileOff to whatever the alignment restrictions of the section are.
508 if (I->Align)
509 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
510 I->Offset = FileOff;
511 FileOff += I->SectionData.size();
512 }
513
514 // Align Section Header.
515 unsigned TableAlign = is64Bit ? 8 : 4;
516 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
517
518 // Now that we know where all of the sections will be emitted, set the e_shnum
519 // entry in the ELF header.
520 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
521 FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
522
523 // Now that we know the offset in the file of the section table, update the
524 // e_shoff address in the ELF header.
525 FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
526
527 // Now that we know all of the data in the file header, emit it and all of the
528 // sections!
529 O.write((char*)&FileHeader[0], FileHeader.size());
530 FileOff = FileHeader.size();
531 DataBuffer().swap(FileHeader);
532
533 DataBuffer Table;
534 OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
535
536 // Emit all of the section data and build the section table itself.
537 while (!SectionList.empty()) {
538 const ELFSection &S = *SectionList.begin();
539
540 // Align FileOff to whatever the alignment restrictions of the section are.
541 if (S.Align)
542 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
543 FileOff != NewFileOff; ++FileOff)
Owen Anderson847b99b2008-08-21 00:14:44 +0000544 O << (char)0xAB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 O.write((char*)&S.SectionData[0], S.SectionData.size());
546 FileOff += S.SectionData.size();
547
548 TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
549 TableOut.outword(S.Type); // sh_type - Section contents & semantics
550 TableOut.outword(S.Flags); // sh_flags - Section flags.
551 TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
552 TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
553 TableOut.outword(S.Size); // sh_size - The section size.
554 TableOut.outword(S.Link); // sh_link - Section header table index link.
555 TableOut.outword(S.Info); // sh_info - Auxillary information.
556 TableOut.outword(S.Align); // sh_addralign - Alignment of section.
557 TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
558
559 SectionList.pop_front();
560 }
561
562 // Align output for the section table.
563 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
564 FileOff != NewFileOff; ++FileOff)
Owen Anderson847b99b2008-08-21 00:14:44 +0000565 O << (char)0xAB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566
567 // Emit the section table itself.
568 O.write((char*)&Table[0], Table.size());
569}