Newer
Older
for (ELFSectionIter I=SectionList.begin(), E=SectionList.end(); I != E; ++I) {
ELFSection &S = *(*I);
// Set the index into the table. Note if we have lots of entries with
// common suffixes, we could memoize them here if we cared.
S.NameIdx = Index;
SHStrTab.emitString(S.getName());
// Keep track of the number of bytes emitted to this section.
Index += S.getName().size()+1;
}
// Set the size of .shstrtab now that we know what it is.
Bruno Cardoso Lopes
committed
assert(Index == SHStrTab.size());
SHStrTab.Size = Index;
/// OutputSectionsAndSectionTable - Now that we have constructed the file header
/// and all of the sections, emit these to the ostream destination and emit the
/// SectionTable.
void ELFWriter::OutputSectionsAndSectionTable() {
// Pass #1: Compute the file offset for each section.
Bruno Cardoso Lopes
committed
size_t FileOff = ElfHdr.size(); // File header first.
Bruno Cardoso Lopes
committed
// Adjust alignment of all section if needed, skip the null section.
for (unsigned i=1, e=SectionList.size(); i < e; ++i) {
ELFSection &ES = *SectionList[i];
if (!ES.size()) {
ES.Offset = FileOff;
Bruno Cardoso Lopes
committed
// Update Section size
Bruno Cardoso Lopes
committed
if (!ES.Size)
ES.Size = ES.size();
Bruno Cardoso Lopes
committed
// Align FileOff to whatever the alignment restrictions of the section are.
Bruno Cardoso Lopes
committed
if (ES.Align)
FileOff = (FileOff+ES.Align-1) & ~(ES.Align-1);
Bruno Cardoso Lopes
committed
Bruno Cardoso Lopes
committed
ES.Offset = FileOff;
FileOff += ES.Size;
}
// Align Section Header.
unsigned TableAlign = TEW->getPrefELFAlignment();
FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
// Now that we know where all of the sections will be emitted, set the e_shnum
// entry in the ELF header.
Bruno Cardoso Lopes
committed
ElfHdr.fixWord16(NumSections, ELFHdr_e_shnum_Offset);
// Now that we know the offset in the file of the section table, update the
// e_shoff address in the ELF header.
Bruno Cardoso Lopes
committed
ElfHdr.fixWord(FileOff, ELFHdr_e_shoff_Offset);
// Now that we know all of the data in the file header, emit it and all of the
// sections!
Bruno Cardoso Lopes
committed
O.write((char *)&ElfHdr.getData()[0], ElfHdr.size());
FileOff = ElfHdr.size();
Bruno Cardoso Lopes
committed
// Section Header Table blob
BinaryObject SHdrTable(isLittleEndian, is64Bit);
Bruno Cardoso Lopes
committed
// Emit all of sections to the file and build the section header table.
for (ELFSectionIter I=SectionList.begin(), E=SectionList.end(); I != E; ++I) {
ELFSection &S = *(*I);
Bruno Cardoso Lopes
committed
DOUT << "SectionIdx: " << S.SectionIdx << ", Name: " << S.getName()
<< ", Size: " << S.Size << ", Offset: " << S.Offset
Bruno Cardoso Lopes
committed
<< ", SectionData Size: " << S.size() << "\n";
// Align FileOff to whatever the alignment restrictions of the section are.
Bruno Cardoso Lopes
committed
if (S.size()) {
if (S.Align) {
for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
FileOff != NewFileOff; ++FileOff)
O << (char)0xAB;
}
Bruno Cardoso Lopes
committed
O.write((char *)&S.getData()[0], S.Size);
Bruno Cardoso Lopes
committed
EmitSectionHeader(SHdrTable, S);
// Align output for the section table.
for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
FileOff != NewFileOff; ++FileOff)
// Emit the section table itself.
Bruno Cardoso Lopes
committed
O.write((char *)&SHdrTable.getData()[0], SHdrTable.size());