1 //===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
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 implements the target-independent ELF writer. This file writes out
11 // the ELF file in the following order:
14 // #2. '.text' section
15 // #3. '.data' section
16 // #4. '.bss' section (conceptual position in file)
18 // #X. '.shstrtab' section
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 ]
27 // #N. ".shstrtab" entry - String table for the section names.
29 //===----------------------------------------------------------------------===//
31 #define DEBUG_TYPE "elfwriter"
33 #include "ELFWriter.h"
34 #include "ELFCodeEmitter.h"
36 #include "llvm/Constants.h"
37 #include "llvm/Module.h"
38 #include "llvm/PassManager.h"
39 #include "llvm/DerivedTypes.h"
40 #include "llvm/CodeGen/BinaryObject.h"
41 #include "llvm/CodeGen/FileWriters.h"
42 #include "llvm/CodeGen/MachineCodeEmitter.h"
43 #include "llvm/CodeGen/MachineConstantPool.h"
44 #include "llvm/CodeGen/MachineFunctionPass.h"
45 #include "llvm/Target/TargetData.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Support/Mangler.h"
48 #include "llvm/Support/Streams.h"
49 #include "llvm/Support/raw_ostream.h"
50 #include "llvm/Support/Debug.h"
54 char ELFWriter::ID = 0;
55 /// AddELFWriter - Concrete function to add the ELF writer to the function pass
57 MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
60 ELFWriter *EW = new ELFWriter(O, TM);
62 return &EW->getMachineCodeEmitter();
65 //===----------------------------------------------------------------------===//
66 // ELFWriter Implementation
67 //===----------------------------------------------------------------------===//
69 ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm)
70 : MachineFunctionPass(&ID), O(o), TM(tm),
71 is64Bit(TM.getTargetData()->getPointerSizeInBits() == 64),
72 isLittleEndian(TM.getTargetData()->isLittleEndian()),
73 ElfHdr(isLittleEndian, is64Bit) {
75 TAI = TM.getTargetAsmInfo();
76 TEW = TM.getELFWriterInfo();
78 // Create the machine code emitter object for this target.
79 MCE = new ELFCodeEmitter(*this);
81 // Inital number of sections
85 ELFWriter::~ELFWriter() {
89 // doInitialization - Emit the file header and all of the global variables for
90 // the module to the ELF file.
91 bool ELFWriter::doInitialization(Module &M) {
92 Mang = new Mangler(M);
96 // Fields e_shnum e_shstrndx are only known after all section have
97 // been emitted. They locations in the ouput buffer are recorded so
98 // to be patched up later.
102 // emitWord method behaves differently for ELF32 and ELF64, writing
103 // 4 bytes in the former and 8 in the last for *_off and *_addr elf types
105 ElfHdr.emitByte(0x7f); // e_ident[EI_MAG0]
106 ElfHdr.emitByte('E'); // e_ident[EI_MAG1]
107 ElfHdr.emitByte('L'); // e_ident[EI_MAG2]
108 ElfHdr.emitByte('F'); // e_ident[EI_MAG3]
110 ElfHdr.emitByte(TEW->getEIClass()); // e_ident[EI_CLASS]
111 ElfHdr.emitByte(TEW->getEIData()); // e_ident[EI_DATA]
112 ElfHdr.emitByte(EV_CURRENT); // e_ident[EI_VERSION]
113 ElfHdr.emitAlignment(16); // e_ident[EI_NIDENT-EI_PAD]
115 ElfHdr.emitWord16(ET_REL); // e_type
116 ElfHdr.emitWord16(TEW->getEMachine()); // e_machine = target
117 ElfHdr.emitWord32(EV_CURRENT); // e_version
118 ElfHdr.emitWord(0); // e_entry, no entry point in .o file
119 ElfHdr.emitWord(0); // e_phoff, no program header for .o
120 ELFHdr_e_shoff_Offset = ElfHdr.size();
121 ElfHdr.emitWord(0); // e_shoff = sec hdr table off in bytes
122 ElfHdr.emitWord32(TEW->getEFlags()); // e_flags = whatever the target wants
123 ElfHdr.emitWord16(TEW->getHdrSize()); // e_ehsize = ELF header size
124 ElfHdr.emitWord16(0); // e_phentsize = prog header entry size
125 ElfHdr.emitWord16(0); // e_phnum = # prog header entries = 0
127 // e_shentsize = Section header entry size
128 ElfHdr.emitWord16(TEW->getSHdrSize());
130 // e_shnum = # of section header ents
131 ELFHdr_e_shnum_Offset = ElfHdr.size();
132 ElfHdr.emitWord16(0); // Placeholder
134 // e_shstrndx = Section # of '.shstrtab'
135 ELFHdr_e_shstrndx_Offset = ElfHdr.size();
136 ElfHdr.emitWord16(0); // Placeholder
138 // Add the null section, which is required to be first in the file.
144 unsigned ELFWriter::getGlobalELFLinkage(const GlobalVariable *GV) {
145 if (GV->hasInternalLinkage())
146 return ELFSym::STB_LOCAL;
148 if (GV->hasWeakLinkage())
149 return ELFSym::STB_WEAK;
151 return ELFSym::STB_GLOBAL;
154 // For global symbols without a section, return the Null section as a
156 ELFSection &ELFWriter::getGlobalSymELFSection(const GlobalVariable *GV,
158 const Section *S = TAI->SectionForGlobal(GV);
159 unsigned Flags = S->getFlags();
160 unsigned SectionType = ELFSection::SHT_PROGBITS;
161 unsigned SHdrFlags = ELFSection::SHF_ALLOC;
162 const TargetData *TD = TM.getTargetData();
163 unsigned Align = TD->getPreferredAlignment(GV);
164 Constant *CV = GV->getInitializer();
166 DOUT << "Section " << S->getName() << " for global " << GV->getName() << "\n";
168 // If this is an external global, the symbol does not have a section.
169 if (!GV->hasInitializer()) {
170 Sym.SectionIdx = ELFSection::SHN_UNDEF;
171 return getNullSection();
174 if (Flags & SectionFlags::Code)
175 SHdrFlags |= ELFSection::SHF_EXECINSTR;
176 if (Flags & SectionFlags::Writeable)
177 SHdrFlags |= ELFSection::SHF_WRITE;
178 if (Flags & SectionFlags::Mergeable)
179 SHdrFlags |= ELFSection::SHF_MERGE;
180 if (Flags & SectionFlags::TLS)
181 SHdrFlags |= ELFSection::SHF_TLS;
182 if (Flags & SectionFlags::Strings)
183 SHdrFlags |= ELFSection::SHF_STRINGS;
185 // If this global has a zero initializer, go to .bss or common section.
186 // Variables are part of the common block if they are zero initialized
187 // and allowed to be merged with other symbols.
188 if (CV->isNullValue() || isa<UndefValue>(CV)) {
189 SectionType = ELFSection::SHT_NOBITS;
190 ELFSection &ElfS = getSection(S->getName(), SectionType, SHdrFlags);
191 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
192 GV->hasCommonLinkage()) {
193 Sym.SectionIdx = ELFSection::SHN_COMMON;
198 Sym.SectionIdx = ElfS.SectionIdx;
199 if (Align) ElfS.Size = (ElfS.Size + Align-1) & ~(Align-1);
200 ElfS.Align = std::max(ElfS.Align, Align);
204 Sym.IsConstant = true;
205 ELFSection &ElfS = getSection(S->getName(), SectionType, SHdrFlags);
206 Sym.SectionIdx = ElfS.SectionIdx;
207 ElfS.Align = std::max(ElfS.Align, Align);
211 void ELFWriter::EmitFunctionDeclaration(const Function *F) {
213 GblSym.setBind(ELFSym::STB_GLOBAL);
214 GblSym.setType(ELFSym::STT_NOTYPE);
215 GblSym.SectionIdx = ELFSection::SHN_UNDEF;
216 SymbolList.push_back(GblSym);
219 void ELFWriter::EmitGlobalVar(const GlobalVariable *GV) {
220 unsigned SymBind = getGlobalELFLinkage(GV);
222 GblSym.setBind(SymBind);
224 if (GV->hasInitializer())
225 GblSym.setType(ELFSym::STT_OBJECT);
227 GblSym.setType(ELFSym::STT_NOTYPE);
229 ELFSection &GblSection = getGlobalSymELFSection(GV, GblSym);
230 const TargetData *TD = TM.getTargetData();
231 unsigned Align = TD->getPreferredAlignment(GV);
232 unsigned Size = TD->getTypeAllocSize(GV->getInitializer()->getType());
235 if (GblSym.IsCommon) {
236 GblSym.Value = Align;
237 } else if (GblSym.IsBss) {
238 GblSym.Value = GblSection.Size;
239 GblSection.Size += Size;
240 } else if (GblSym.IsConstant){
241 // GblSym.Value should contain the symbol index inside the section,
242 // and all symbols should start on their required alignment boundary
243 GblSym.Value = (GblSection.size() + (Align-1)) & (-Align);
244 GblSection.emitAlignment(Align);
245 EmitGlobalConstant(GV->getInitializer(), GblSection);
248 // Local symbols should come first on the symbol table.
249 if (!GV->hasPrivateLinkage()) {
250 if (SymBind == ELFSym::STB_LOCAL)
251 SymbolList.push_front(GblSym);
253 SymbolList.push_back(GblSym);
257 void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
260 // Print the fields in successive locations. Pad to align if needed!
261 const TargetData *TD = TM.getTargetData();
262 unsigned Size = TD->getTypeAllocSize(CVS->getType());
263 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
264 uint64_t sizeSoFar = 0;
265 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
266 const Constant* field = CVS->getOperand(i);
268 // Check if padding is needed and insert one or more 0s.
269 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
270 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
271 - cvsLayout->getElementOffset(i)) - fieldSize;
272 sizeSoFar += fieldSize + padSize;
274 // Now print the actual field value.
275 EmitGlobalConstant(field, GblS);
277 // Insert padding - this may include padding to increase the size of the
278 // current field up to the ABI size (if the struct is not packed) as well
279 // as padding to ensure that the next field starts at the right offset.
280 for (unsigned p=0; p < padSize; p++)
283 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
284 "Layout of constant struct may be incorrect!");
287 void ELFWriter::EmitGlobalConstant(const Constant *CV, ELFSection &GblS) {
288 const TargetData *TD = TM.getTargetData();
289 unsigned Size = TD->getTypeAllocSize(CV->getType());
291 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
292 if (CVA->isString()) {
293 std::string GblStr = CVA->getAsString();
294 GblStr.resize(GblStr.size()-1);
295 GblS.emitString(GblStr);
296 } else { // Not a string. Print the values in successive locations
297 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
298 EmitGlobalConstant(CVA->getOperand(i), GblS);
301 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
302 EmitGlobalConstantStruct(CVS, GblS);
304 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
305 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
306 if (CFP->getType() == Type::DoubleTy)
307 GblS.emitWord64(Val);
308 else if (CFP->getType() == Type::FloatTy)
309 GblS.emitWord32(Val);
310 else if (CFP->getType() == Type::X86_FP80Ty) {
311 assert(0 && "X86_FP80Ty global emission not implemented");
312 } else if (CFP->getType() == Type::PPC_FP128Ty)
313 assert(0 && "PPC_FP128Ty global emission not implemented");
315 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
317 GblS.emitWord32(CI->getZExtValue());
319 GblS.emitWord64(CI->getZExtValue());
321 assert(0 && "LargeInt global emission not implemented");
323 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
324 const VectorType *PTy = CP->getType();
325 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
326 EmitGlobalConstant(CP->getOperand(I), GblS);
329 assert(0 && "unknown global constant");
333 bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
334 // Nothing to do here, this is all done through the MCE object above.
338 /// doFinalization - Now that the module has been completely processed, emit
339 /// the ELF file to 'O'.
340 bool ELFWriter::doFinalization(Module &M) {
341 /// FIXME: This should be removed when moving to ObjectCodeEmiter. Since the
342 /// current ELFCodeEmiter uses CurrBuff, ... it doesn't update S.Data
343 /// vector size for .text sections, so this is a quick dirty fix
344 ELFSection &TS = getTextSection();
346 BinaryData &BD = TS.getData();
347 for (unsigned e=0; e<TS.Size; ++e)
351 // Emit .data section placeholder
354 // Emit .bss section placeholder
357 // Build and emit data, bss and "common" sections.
358 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
364 // Emit all pending globals
365 // TODO: this should be done only for referenced symbols
366 for (SetVector<GlobalValue*>::const_iterator I = PendingGlobals.begin(),
367 E = PendingGlobals.end(); I != E; ++I) {
369 // No need to emit the symbol again
370 if (GblSymLookup.find(*I) != GblSymLookup.end())
373 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
375 } else if (Function *F = dyn_cast<Function>(*I)) {
376 // If function is not in GblSymLookup, it doesn't have a body,
377 // so emit the symbol as a function declaration (no section associated)
378 EmitFunctionDeclaration(F);
380 assert("unknown howto handle pending global");
382 GblSymLookup[*I] = 0;
385 // Emit non-executable stack note
386 if (TAI->getNonexecutableStackDirective())
387 getNonExecStackSection();
392 // Emit the symbol table now, if non-empty.
395 // Emit the relocation sections.
398 // Emit the sections string table.
399 EmitSectionTableStringTable();
401 // Dump the sections and section table to the .o file.
402 OutputSectionsAndSectionTable();
404 // We are done with the abstract symbols.
408 // Release the name mangler object.
409 delete Mang; Mang = 0;
413 /// EmitRelocations - Emit relocations
414 void ELFWriter::EmitRelocations() {
416 // Create Relocation sections for each section which needs it.
417 for (std::list<ELFSection>::iterator I = SectionList.begin(),
418 E = SectionList.end(); I != E; ++I) {
420 // This section does not have relocations
421 if (!I->hasRelocations()) continue;
423 // Get the relocation section for section 'I'
424 bool HasRelA = TEW->hasRelocationAddend();
425 ELFSection &RelSec = getRelocSection(I->getName(), HasRelA);
427 // 'Link' - Section hdr idx of the associated symbol table
428 // 'Info' - Section hdr idx of the section to which the relocation applies
429 ELFSection &SymTab = getSymbolTableSection();
430 RelSec.Link = SymTab.SectionIdx;
431 RelSec.Info = I->SectionIdx;
432 RelSec.EntSize = TEW->getRelocationEntrySize();
434 // Get the relocations from Section
435 std::vector<MachineRelocation> Relos = I->getRelocations();
436 for (std::vector<MachineRelocation>::iterator MRI = Relos.begin(),
437 MRE = Relos.end(); MRI != MRE; ++MRI) {
438 MachineRelocation &MR = *MRI;
440 // Offset from the start of the section containing the symbol
441 unsigned Offset = MR.getMachineCodeOffset();
443 // Symbol index in the symbol table
446 // Target specific ELF relocation type
447 unsigned RelType = TEW->getRelocationType(MR.getRelocationType());
449 // Constant addend used to compute the value to be stored
450 // into the relocatable field
451 int64_t Addend = TEW->getAddendForRelTy(RelType);
453 // There are several machine relocations types, and each one of
454 // them needs a different approach to retrieve the symbol table index.
455 if (MR.isGlobalValue()) {
456 const GlobalValue *G = MR.getGlobalValue();
457 SymIdx = GblSymLookup[G];
459 assert(0 && "dunno how to handle other relocation types");
462 // Get the relocation entry and emit to the relocation section
463 ELFRelocation Rel(Offset, SymIdx, RelType, HasRelA, Addend);
464 EmitRelocation(RelSec, Rel, HasRelA);
469 /// EmitRelocation - Write relocation 'Rel' to the relocation section 'Rel'
470 void ELFWriter::EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel,
472 RelSec.emitWord(Rel.getOffset());
473 RelSec.emitWord(Rel.getInfo(is64Bit));
475 RelSec.emitWord(Rel.getAddend());
478 /// EmitSymbol - Write symbol 'Sym' to the symbol table 'SymbolTable'
479 void ELFWriter::EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym) {
481 SymbolTable.emitWord32(Sym.NameIdx);
482 SymbolTable.emitByte(Sym.Info);
483 SymbolTable.emitByte(Sym.Other);
484 SymbolTable.emitWord16(Sym.SectionIdx);
485 SymbolTable.emitWord64(Sym.Value);
486 SymbolTable.emitWord64(Sym.Size);
488 SymbolTable.emitWord32(Sym.NameIdx);
489 SymbolTable.emitWord32(Sym.Value);
490 SymbolTable.emitWord32(Sym.Size);
491 SymbolTable.emitByte(Sym.Info);
492 SymbolTable.emitByte(Sym.Other);
493 SymbolTable.emitWord16(Sym.SectionIdx);
497 /// EmitSectionHeader - Write section 'Section' header in 'SHdrTab'
498 /// Section Header Table
499 void ELFWriter::EmitSectionHeader(BinaryObject &SHdrTab,
500 const ELFSection &SHdr) {
501 SHdrTab.emitWord32(SHdr.NameIdx);
502 SHdrTab.emitWord32(SHdr.Type);
504 SHdrTab.emitWord64(SHdr.Flags);
505 SHdrTab.emitWord(SHdr.Addr);
506 SHdrTab.emitWord(SHdr.Offset);
507 SHdrTab.emitWord64(SHdr.Size);
508 SHdrTab.emitWord32(SHdr.Link);
509 SHdrTab.emitWord32(SHdr.Info);
510 SHdrTab.emitWord64(SHdr.Align);
511 SHdrTab.emitWord64(SHdr.EntSize);
513 SHdrTab.emitWord32(SHdr.Flags);
514 SHdrTab.emitWord(SHdr.Addr);
515 SHdrTab.emitWord(SHdr.Offset);
516 SHdrTab.emitWord32(SHdr.Size);
517 SHdrTab.emitWord32(SHdr.Link);
518 SHdrTab.emitWord32(SHdr.Info);
519 SHdrTab.emitWord32(SHdr.Align);
520 SHdrTab.emitWord32(SHdr.EntSize);
524 /// EmitStringTable - If the current symbol table is non-empty, emit the string
526 void ELFWriter::EmitStringTable() {
527 if (!SymbolList.size()) return; // Empty symbol table.
528 ELFSection &StrTab = getStringTableSection();
530 // Set the zero'th symbol to a null byte, as required.
533 // Walk on the symbol list and write symbol names into the
536 for (std::list<ELFSym>::iterator I = SymbolList.begin(),
537 E = SymbolList.end(); I != E; ++I) {
539 // Use the name mangler to uniquify the LLVM symbol.
540 std::string Name = Mang->getValueName(I->GV);
546 StrTab.emitString(Name);
548 // Keep track of the number of bytes emitted to this section.
549 Index += Name.size()+1;
552 assert(Index == StrTab.size());
556 /// EmitSymbolTable - Emit the symbol table itself.
557 void ELFWriter::EmitSymbolTable() {
558 if (!SymbolList.size()) return; // Empty symbol table.
560 unsigned FirstNonLocalSymbol = 1;
561 // Now that we have emitted the string table and know the offset into the
562 // string table of each symbol, emit the symbol table itself.
563 ELFSection &SymTab = getSymbolTableSection();
564 SymTab.Align = TEW->getPrefELFAlignment();
566 // Section Index of .strtab.
567 SymTab.Link = getStringTableSection().SectionIdx;
569 // Size of each symtab entry.
570 SymTab.EntSize = TEW->getSymTabEntrySize();
572 // The first entry in the symtab is the null symbol
573 ELFSym NullSym = ELFSym(0);
574 EmitSymbol(SymTab, NullSym);
576 // Emit all the symbols to the symbol table. Skip the null
577 // symbol, cause it's emitted already
579 for (std::list<ELFSym>::iterator I = SymbolList.begin(),
580 E = SymbolList.end(); I != E; ++I, ++Index) {
581 // Keep track of the first non-local symbol
582 if (I->getBind() == ELFSym::STB_LOCAL)
583 FirstNonLocalSymbol++;
585 // Emit symbol to the symbol table
586 EmitSymbol(SymTab, *I);
588 // Record the symbol table index for each global value
589 GblSymLookup[I->GV] = Index;
592 SymTab.Info = FirstNonLocalSymbol;
593 SymTab.Size = SymTab.size();
596 /// EmitSectionTableStringTable - This method adds and emits a section for the
597 /// ELF Section Table string table: the string table that holds all of the
599 void ELFWriter::EmitSectionTableStringTable() {
600 // First step: add the section for the string table to the list of sections:
601 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
603 // Now that we know which section number is the .shstrtab section, update the
604 // e_shstrndx entry in the ELF header.
605 ElfHdr.fixWord16(SHStrTab.SectionIdx, ELFHdr_e_shstrndx_Offset);
607 // Set the NameIdx of each section in the string table and emit the bytes for
611 for (std::list<ELFSection>::iterator I = SectionList.begin(),
612 E = SectionList.end(); I != E; ++I) {
613 // Set the index into the table. Note if we have lots of entries with
614 // common suffixes, we could memoize them here if we cared.
616 SHStrTab.emitString(I->getName());
618 // Keep track of the number of bytes emitted to this section.
619 Index += I->getName().size()+1;
622 // Set the size of .shstrtab now that we know what it is.
623 assert(Index == SHStrTab.size());
624 SHStrTab.Size = Index;
627 /// OutputSectionsAndSectionTable - Now that we have constructed the file header
628 /// and all of the sections, emit these to the ostream destination and emit the
630 void ELFWriter::OutputSectionsAndSectionTable() {
631 // Pass #1: Compute the file offset for each section.
632 size_t FileOff = ElfHdr.size(); // File header first.
634 // Adjust alignment of all section if needed.
635 for (std::list<ELFSection>::iterator I = SectionList.begin(),
636 E = SectionList.end(); I != E; ++I) {
638 // Section idx 0 has 0 offset
647 // Update Section size
651 // Align FileOff to whatever the alignment restrictions of the section are.
653 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
659 // Align Section Header.
660 unsigned TableAlign = TEW->getPrefELFAlignment();
661 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
663 // Now that we know where all of the sections will be emitted, set the e_shnum
664 // entry in the ELF header.
665 ElfHdr.fixWord16(NumSections, ELFHdr_e_shnum_Offset);
667 // Now that we know the offset in the file of the section table, update the
668 // e_shoff address in the ELF header.
669 ElfHdr.fixWord(FileOff, ELFHdr_e_shoff_Offset);
671 // Now that we know all of the data in the file header, emit it and all of the
673 O.write((char *)&ElfHdr.getData()[0], ElfHdr.size());
674 FileOff = ElfHdr.size();
676 // Section Header Table blob
677 BinaryObject SHdrTable(isLittleEndian, is64Bit);
679 // Emit all of sections to the file and build the section header table.
680 while (!SectionList.empty()) {
681 ELFSection &S = *SectionList.begin();
682 DOUT << "SectionIdx: " << S.SectionIdx << ", Name: " << S.getName()
683 << ", Size: " << S.Size << ", Offset: " << S.Offset
684 << ", SectionData Size: " << S.size() << "\n";
686 // Align FileOff to whatever the alignment restrictions of the section are.
688 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
689 FileOff != NewFileOff; ++FileOff)
694 O.write((char *)&S.getData()[0], S.Size);
698 EmitSectionHeader(SHdrTable, S);
699 SectionList.pop_front();
702 // Align output for the section table.
703 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
704 FileOff != NewFileOff; ++FileOff)
707 // Emit the section table itself.
708 O.write((char *)&SHdrTable.getData()[0], SHdrTable.size());