Changeset View
Changeset View
Standalone View
Standalone View
llvm/lib/MC/XCOFFObjectWriter.cpp
Show First 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | Section(const char *N, XCOFF::SectionTypeFlags Flags, bool IsVirtual, | ||||
strncpy(Name, N, XCOFF::NameSize); | strncpy(Name, N, XCOFF::NameSize); | ||||
} | } | ||||
}; | }; | ||||
class XCOFFObjectWriter : public MCObjectWriter { | class XCOFFObjectWriter : public MCObjectWriter { | ||||
uint32_t SymbolTableEntryCount = 0; | uint32_t SymbolTableEntryCount = 0; | ||||
uint32_t SymbolTableOffset = 0; | uint32_t SymbolTableOffset = 0; | ||||
uint16_t SectionCount = 0; | |||||
daltenty: Should this be uint16_t? | |||||
Originally choose the signed type at the mind set of "don't use unsigned to represent integer that could not be negative." And also, want this to be the same type as the Maximum section index could be. jasonliu: Originally choose the signed type at the mind set of "don't use unsigned to represent integer… | |||||
support::endian::Writer W; | support::endian::Writer W; | ||||
std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter; | std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter; | ||||
StringTableBuilder Strings; | StringTableBuilder Strings; | ||||
// CsectGroups. These store the csects which make up different parts of | // CsectGroups. These store the csects which make up different parts of | ||||
// the sections. Should have one for each set of csects that get mapped into | // the sections. Should have one for each set of csects that get mapped into | ||||
// the same section and get handled in a 'similar' way. | // the same section and get handled in a 'similar' way. | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | |||||
void XCOFFObjectWriter::reset() { | void XCOFFObjectWriter::reset() { | ||||
// Reset any sections we have written to, and empty the section header table. | // Reset any sections we have written to, and empty the section header table. | ||||
for (auto *Sec : Sections) | for (auto *Sec : Sections) | ||||
Sec->reset(); | Sec->reset(); | ||||
// Reset the symbol table and string table. | // Reset the symbol table and string table. | ||||
SymbolTableEntryCount = 0; | SymbolTableEntryCount = 0; | ||||
SymbolTableOffset = 0; | SymbolTableOffset = 0; | ||||
SectionCount = 0; | |||||
Strings.clear(); | Strings.clear(); | ||||
MCObjectWriter::reset(); | MCObjectWriter::reset(); | ||||
} | } | ||||
CsectGroup &XCOFFObjectWriter::getCsectGroup(const MCSectionXCOFF *MCSec) { | CsectGroup &XCOFFObjectWriter::getCsectGroup(const MCSectionXCOFF *MCSec) { | ||||
switch (MCSec->getMappingClass()) { | switch (MCSec->getMappingClass()) { | ||||
case XCOFF::XMC_PR: | case XCOFF::XMC_PR: | ||||
▲ Show 20 Lines • Show All 217 Lines • ▼ Show 20 Lines | void XCOFFObjectWriter::writeSymbolTableEntryForControlSection( | ||||
// Reserved (x_snstab). | // Reserved (x_snstab). | ||||
W.write<uint16_t>(0); | W.write<uint16_t>(0); | ||||
} | } | ||||
void XCOFFObjectWriter::writeFileHeader() { | void XCOFFObjectWriter::writeFileHeader() { | ||||
// Magic. | // Magic. | ||||
W.write<uint16_t>(0x01df); | W.write<uint16_t>(0x01df); | ||||
// Number of sections. | // Number of sections. | ||||
W.write<uint16_t>(Sections.size()); | W.write<uint16_t>(SectionCount); | ||||
// Timestamp field. For reproducible output we write a 0, which represents no | // Timestamp field. For reproducible output we write a 0, which represents no | ||||
// timestamp. | // timestamp. | ||||
W.write<int32_t>(0); | W.write<int32_t>(0); | ||||
// Byte Offset to the start of the symbol table. | // Byte Offset to the start of the symbol table. | ||||
W.write<uint32_t>(SymbolTableOffset); | W.write<uint32_t>(SymbolTableOffset); | ||||
// Number of entries in the symbol table. | // Number of entries in the symbol table. | ||||
W.write<int32_t>(SymbolTableEntryCount); | W.write<int32_t>(SymbolTableEntryCount); | ||||
// Size of the optional header. | // Size of the optional header. | ||||
W.write<uint16_t>(0); | W.write<uint16_t>(0); | ||||
// Flags. | // Flags. | ||||
W.write<uint16_t>(0); | W.write<uint16_t>(0); | ||||
} | } | ||||
void XCOFFObjectWriter::writeSectionHeaderTable() { | void XCOFFObjectWriter::writeSectionHeaderTable() { | ||||
for (const auto *Sec : Sections) { | for (const auto *Sec : Sections) { | ||||
// Nothing to write for this Section. | |||||
if (Sec->Index == Section::UninitializedIndex) | |||||
continue; | |||||
// Write Name. | // Write Name. | ||||
ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize); | ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize); | ||||
W.write(NameRef); | W.write(NameRef); | ||||
// Write the Physical Address and Virtual Address. In an object file these | // Write the Physical Address and Virtual Address. In an object file these | ||||
// are the same. | // are the same. | ||||
W.write<uint32_t>(Sec->Address); | W.write<uint32_t>(Sec->Address); | ||||
W.write<uint32_t>(Sec->Address); | W.write<uint32_t>(Sec->Address); | ||||
Show All 10 Lines | for (const auto *Sec : Sections) { | ||||
W.write<uint16_t>(0); | W.write<uint16_t>(0); | ||||
W.write<int32_t>(Sec->Flags); | W.write<int32_t>(Sec->Flags); | ||||
} | } | ||||
} | } | ||||
void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) { | void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) { | ||||
for (const auto *Section : Sections) { | for (const auto *Section : Sections) { | ||||
// Nothing to write for this Section. | |||||
if (Section->Index == Section::UninitializedIndex) | |||||
continue; | |||||
for (const auto *Group : Section->Groups) { | for (const auto *Group : Section->Groups) { | ||||
if (Group->Csects.empty()) | if (Group->Csects.empty()) | ||||
continue; | continue; | ||||
const bool SupportLabelDef = Group->SupportLabelDef; | const bool SupportLabelDef = Group->SupportLabelDef; | ||||
const int16_t SectionIndex = Section->Index; | const int16_t SectionIndex = Section->Index; | ||||
for (const auto &Csect : Group->Csects) { | for (const auto &Csect : Group->Csects) { | ||||
// Write out the control section first and then each symbol in it. | // Write out the control section first and then each symbol in it. | ||||
Show All 30 Lines | const bool IsEmpty = | ||||
return Group->Csects.empty(); | return Group->Csects.empty(); | ||||
}); | }); | ||||
if (IsEmpty) | if (IsEmpty) | ||||
continue; | continue; | ||||
if (SectionIndex > MaxSectionIndex) | if (SectionIndex > MaxSectionIndex) | ||||
report_fatal_error("Section index overflow!"); | report_fatal_error("Section index overflow!"); | ||||
Section->Index = SectionIndex++; | Section->Index = SectionIndex++; | ||||
SectionCount++; | |||||
bool SectionAddressSet = false; | bool SectionAddressSet = false; | ||||
for (auto *Group : Section->Groups) { | for (auto *Group : Section->Groups) { | ||||
if (Group->Csects.empty()) | if (Group->Csects.empty()) | ||||
continue; | continue; | ||||
const bool SupportLabelDef = Group->SupportLabelDef; | const bool SupportLabelDef = Group->SupportLabelDef; | ||||
for (auto &Csect : Group->Csects) { | for (auto &Csect : Group->Csects) { | ||||
Show All 27 Lines | for (auto *Section : Sections) { | ||||
Address = alignTo(Address, DefaultSectionAlign); | Address = alignTo(Address, DefaultSectionAlign); | ||||
Section->Size = Address - Section->Address; | Section->Size = Address - Section->Address; | ||||
} | } | ||||
SymbolTableEntryCount = SymbolTableIndex; | SymbolTableEntryCount = SymbolTableIndex; | ||||
// Calculate the RawPointer value for each section. | // Calculate the RawPointer value for each section. | ||||
uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() + | uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() + | ||||
Sections.size() * sizeof(XCOFF::SectionHeader32); | SectionCount * sizeof(XCOFF::SectionHeader32); | ||||
for (auto *Sec : Sections) { | for (auto *Sec : Sections) { | ||||
if (!Sec->IsVirtual) { | if (Sec->Index == Section::UninitializedIndex || Sec->IsVirtual) | ||||
continue; | |||||
Sec->FileOffsetToData = RawPointer; | Sec->FileOffsetToData = RawPointer; | ||||
RawPointer += Sec->Size; | RawPointer += Sec->Size; | ||||
} | } | ||||
} | |||||
// TODO Add in Relocation storage to the RawPointer Calculation. | // TODO Add in Relocation storage to the RawPointer Calculation. | ||||
// TODO What to align the SymbolTable to? | // TODO What to align the SymbolTable to? | ||||
// TODO Error check that the number of symbol table entries fits in 32-bits | // TODO Error check that the number of symbol table entries fits in 32-bits | ||||
// signed ... | // signed ... | ||||
if (SymbolTableEntryCount) | if (SymbolTableEntryCount) | ||||
SymbolTableOffset = RawPointer; | SymbolTableOffset = RawPointer; | ||||
} | } | ||||
Show All 22 Lines |
Should this be uint16_t?