52 constexpr
unsigned DefaultSectionAlign = 4;
53 constexpr int16_t MaxSectionIndex = INT16_MAX;
58 struct XCOFFRelocation {
96 : MCSec(MCSec), SymbolTableIndex(-1),
Address(-1),
Size(0) {}
103 using CsectGroup = std::deque<XCOFFSection>;
104 using CsectGroups = std::deque<CsectGroup *>;
132 static constexpr int16_t UninitializedIndex =
137 FileOffsetToRelocations(0), RelocationCount(0),
Flags(
Flags),
138 Index(UninitializedIndex) {
143 virtual void reset() {
146 FileOffsetToData = 0;
147 FileOffsetToRelocations = 0;
149 Index = UninitializedIndex;
162 const bool IsVirtual;
174 void reset()
override {
175 SectionEntry::reset();
177 for (
auto *Group :
Groups)
181 virtual ~CsectSectionEntry() =
default;
186 std::unique_ptr<XCOFFSection> DwarfSect;
193 std::unique_ptr<XCOFFSection> Sect)
196 assert(DwarfSect->MCSec->isDwarfSect() &&
197 "This should be a DWARF section!");
202 DwarfSectionEntry(DwarfSectionEntry &&
s) =
default;
204 virtual ~DwarfSectionEntry() =
default;
207 struct ExceptionTableEntry {
213 ExceptionTableEntry(
const MCSymbol *Trap,
unsigned Lang,
unsigned Reason)
214 :
Trap(
Trap), Lang(Lang), Reason(Reason) {}
217 struct ExceptionInfo {
219 unsigned FunctionSize;
220 std::vector<ExceptionTableEntry> Entries;
224 std::map<const StringRef, ExceptionInfo> ExceptionTable;
225 bool isDebugEnabled =
false;
227 ExceptionSectionEntry(
StringRef N, int32_t Flags)
233 virtual ~ExceptionSectionEntry() =
default;
242 std::vector<std::pair<std::string, size_t>> FileNames;
243 bool HasVisibility =
false;
246 std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter;
250 TargetObjectWriter->is64Bit() ?
UINT64_MAX : UINT32_MAX;
264 CsectGroup UndefinedCsects;
265 CsectGroup ProgramCodeCsects;
266 CsectGroup ReadOnlyCsects;
267 CsectGroup DataCsects;
268 CsectGroup FuncDSCsects;
269 CsectGroup TOCCsects;
270 CsectGroup BSSCsects;
271 CsectGroup TDataCsects;
272 CsectGroup TBSSCsects;
275 CsectSectionEntry
Text;
276 CsectSectionEntry
Data;
277 CsectSectionEntry BSS;
278 CsectSectionEntry TData;
279 CsectSectionEntry TBSS;
283 std::array<CsectSectionEntry *const, 5> Sections{
284 {&
Text, &
Data, &BSS, &TData, &TBSS}};
286 std::vector<DwarfSectionEntry> DwarfSections;
287 std::vector<SectionEntry> OverflowSections;
289 ExceptionSectionEntry ExceptionSection;
293 void reset()
override;
302 bool is64Bit()
const {
return TargetObjectWriter->is64Bit(); }
303 bool nameShouldBeInStringTable(
const StringRef &);
306 void writeSymbolEntryForCsectMemberLabel(
const Symbol &SymbolRef,
307 const XCOFFSection &CSectionRef,
308 int16_t SectionIndex,
310 void writeSymbolEntryForControlSection(
const XCOFFSection &CSectionRef,
311 int16_t SectionIndex,
313 void writeSymbolEntryForDwarfSection(
const XCOFFSection &DwarfSectionRef,
314 int16_t SectionIndex);
315 void writeFileHeader();
316 void writeAuxFileHeader();
318 void writeSectionHeaderTable();
322 const CsectSectionEntry &CsectEntry,
326 const DwarfSectionEntry &DwarfEntry,
328 void writeSectionForExceptionSectionEntry(
330 ExceptionSectionEntry &ExceptionEntry,
uint64_t &CurrentAddressLocation);
332 void writeSymbolAuxDwarfEntry(
uint64_t LengthOfSectionPortion,
334 void writeSymbolAuxCsectEntry(
uint64_t SectionOrLength,
335 uint8_t SymbolAlignmentAndType,
337 void writeSymbolAuxFunctionEntry(
uint32_t EntryOffset,
uint32_t FunctionSize,
340 void writeSymbolAuxExceptionEntry(
uint64_t EntryOffset,
uint32_t FunctionSize,
345 void writeRelocations();
346 void writeRelocation(XCOFFRelocation Reloc,
const XCOFFSection &Section);
356 void assignAddressesAndIndices(
const MCAsmLayout &);
358 void finalizeSectionInfo();
363 unsigned LanguageCode,
unsigned ReasonCode,
364 unsigned FunctionSize,
bool hasDebug)
override;
365 bool hasExceptionSection() {
366 return !ExceptionSection.ExceptionTable.empty();
368 unsigned getExceptionSectionSize();
369 unsigned getExceptionOffset(
const MCSymbol *Symbol);
371 size_t auxiliaryHeaderSize()
const {
377 XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
385 XCOFFObjectWriter::XCOFFObjectWriter(
390 CsectGroups{&ProgramCodeCsects, &ReadOnlyCsects}),
392 CsectGroups{&DataCsects, &FuncDSCsects, &TOCCsects}),
394 CsectGroups{&BSSCsects}),
396 CsectGroups{&TDataCsects}),
398 CsectGroups{&TBSSCsects}),
401 void XCOFFObjectWriter::reset() {
403 SymbolIndexMap.
clear();
406 UndefinedCsects.clear();
408 for (
auto *Sec : Sections)
410 for (
auto &DwarfSec : DwarfSections)
412 for (
auto &OverflowSec : OverflowSections)
414 ExceptionSection.reset();
417 SymbolTableEntryCount = 0;
418 SymbolTableOffset = 0;
420 PaddingsBeforeDwarf = 0;
423 MCObjectWriter::reset();
426 CsectGroup &XCOFFObjectWriter::getCsectGroup(
const MCSectionXCOFF *MCSec) {
430 "Only an initialized csect can contain program code.");
431 return ProgramCodeCsects;
434 "Only an initialized csect can contain read only data.");
435 return ReadOnlyCsects;
448 "Mapping invalid csect. CSECT with bss storage class must be "
453 "Mapping invalid csect. CSECT with tdata storage class must be "
454 "an initialized csect.");
458 "Mapping invalid csect. CSECT with tbss storage class must be "
459 "an uninitialized csect.");
463 "Only an initialized csect can contain TOC-base.");
464 assert(TOCCsects.empty() &&
465 "We should have only one TOC-base, and it should be the first csect "
466 "in this CsectGroup.");
472 "Only an initialized csect can contain TC entry.");
473 assert(!TOCCsects.empty() &&
474 "We should at least have a TOC-base in this CsectGroup.");
487 void XCOFFObjectWriter::executePostLayoutBinding(
MCAssembler &
Asm,
489 for (
const auto &
S :
Asm) {
490 const auto *MCSec = cast<const MCSectionXCOFF>(&
S);
492 "Cannot add a section twice.");
503 "An undefined csect should not get registered.");
504 CsectGroup &Group = getCsectGroup(MCSec);
505 Group.emplace_back(MCSec);
506 SectionMap[MCSec] = &Group.back();
509 std::unique_ptr<XCOFFSection> DwarfSec =
510 std::make_unique<XCOFFSection>(MCSec);
511 SectionMap[MCSec] = DwarfSec.get();
513 DwarfSectionEntry SecEntry(MCSec->
getName(),
516 DwarfSections.push_back(
std::move(SecEntry));
530 HasVisibility =
true;
534 UndefinedCsects.emplace_back(ContainingCsect);
535 SectionMap[ContainingCsect] = &UndefinedCsects.back();
550 assert(SectionMap.
find(ContainingCsect) != SectionMap.
end() &&
551 "Expected containing csect to exist in map");
552 XCOFFSection *Csect = SectionMap[ContainingCsect];
554 assert(Csect->MCSec->isCsect() &&
"only csect is supported now!");
555 Csect->Syms.emplace_back(XSym);
563 FileNames =
Asm.getFileNames();
565 if (FileNames.empty())
566 FileNames.emplace_back(
".file", 0);
567 for (
const std::pair<std::string, size_t> &
F : FileNames) {
568 if (nameShouldBeInStringTable(
F.first))
569 Strings.add(
F.first);
573 assignAddressesAndIndices(Layout);
581 auto getIndex = [
this](
const MCSymbol *Sym,
586 return SymbolIndexMap.
find(Sym) != SymbolIndexMap.
end()
587 ? SymbolIndexMap[Sym]
591 auto getVirtualAddress =
592 [
this, &Layout](
const MCSymbol *Sym,
595 if (ContainingSect->isDwarfSect())
600 return SectionMap[ContainingSect]->Address;
604 return SectionMap[ContainingSect]->Address + Layout.
getSymbolOffset(*Sym);
611 MCFixupKindInfo::FKF_IsPCRel;
615 std::tie(
Type, SignAndSize) =
616 TargetObjectWriter->getRelocTypeAndSignSize(
Target,
Fixup, IsPCRel);
618 const MCSectionXCOFF *SymASec = getContainingCsect(cast<MCSymbolXCOFF>(SymA));
620 "Expected containing csect to exist in map.");
627 FixedValue = getVirtualAddress(SymA, SymASec) +
Target.getConstant();
644 const int64_t TOCEntryOffset = SectionMap[SymASec]->Address -
645 TOCCsects.front().Address +
650 FixedValue = TOCEntryOffset;
656 "Only XMC_PR csect may have the R_RBR relocation.");
660 uint64_t BRInstrAddress = SectionMap[ParentSec]->Address +
666 SectionMap[SymASec]->Address - BRInstrAddress +
Target.getConstant();
671 "Fragment offset + fixup offset is overflowed.");
675 XCOFFRelocation Reloc = {
Index, FixupOffsetInCsect, SignAndSize,
Type};
678 "Expected containing csect to exist in map.");
679 SectionMap[RelocationSec]->Relocations.push_back(Reloc);
688 const MCSectionXCOFF *SymBSec = getContainingCsect(cast<MCSymbolXCOFF>(SymB));
690 "Expected containing csect to exist in map.");
691 if (SymASec == SymBSec)
693 "relocation for paired relocatable term is not yet supported");
696 "SymA must be R_POS here if it's not opposite term or paired "
697 "relocatable term.");
698 const uint32_t IndexB = getIndex(SymB, SymBSec);
702 XCOFFRelocation RelocB = {IndexB, FixupOffsetInCsect, SignAndSize, TypeB};
703 SectionMap[RelocationSec]->Relocations.push_back(RelocB);
706 FixedValue -= getVirtualAddress(SymB, SymBSec);
711 uint64_t CurrentAddressLocation = 0;
712 for (
const auto *
Section : Sections)
713 writeSectionForControlSectionEntry(
Asm, Layout, *
Section,
714 CurrentAddressLocation);
715 for (
const auto &DwarfSection : DwarfSections)
716 writeSectionForDwarfSectionEntry(
Asm, Layout, DwarfSection,
717 CurrentAddressLocation);
718 writeSectionForExceptionSectionEntry(
Asm, Layout, ExceptionSection,
719 CurrentAddressLocation);
727 if (
Asm.isIncrementalLinkerCompatible())
730 finalizeSectionInfo();
734 writeAuxFileHeader();
735 writeSectionHeaderTable();
736 writeSections(
Asm, Layout);
742 return W.OS.tell() - StartOffset;
763 int16_t SectionNumber,
766 uint8_t NumberOfAuxEntries) {
774 W.write<int16_t>(SectionNumber);
777 W.write<uint8_t>(NumberOfAuxEntries);
780 void XCOFFObjectWriter::writeSymbolAuxCsectEntry(
uint64_t SectionOrLength,
781 uint8_t SymbolAlignmentAndType,
786 W.write<uint8_t>(SymbolAlignmentAndType);
798 void XCOFFObjectWriter::writeSymbolAuxDwarfEntry(
800 writeWord(LengthOfSectionPortion);
803 writeWord(NumberOfRelocEnt);
812 void XCOFFObjectWriter::writeSymbolEntryForCsectMemberLabel(
813 const Symbol &SymbolRef,
const XCOFFSection &CSectionRef,
814 int16_t SectionIndex,
uint64_t SymbolOffset) {
815 assert(SymbolOffset <= MaxRawDataSize - CSectionRef.Address &&
816 "Symbol address overflowed.");
818 auto Entry = ExceptionSection.ExceptionTable.find(SymbolRef.MCSym->getName());
819 if (Entry != ExceptionSection.ExceptionTable.end()) {
820 writeSymbolEntry(SymbolRef.getSymbolTableName(),
821 CSectionRef.Address + SymbolOffset, SectionIndex,
825 is64Bit() ? SymbolRef.getVisibilityType()
826 : SymbolRef.getVisibilityType() | 0x0020,
827 SymbolRef.getStorageClass(),
828 (
is64Bit() && ExceptionSection.isDebugEnabled) ? 3 : 2);
829 if (
is64Bit() && ExceptionSection.isDebugEnabled) {
832 writeSymbolAuxExceptionEntry(
833 ExceptionSection.FileOffsetToData +
834 getExceptionOffset(Entry->second.FunctionSymbol),
835 Entry->second.FunctionSize,
836 SymbolIndexMap[Entry->second.FunctionSymbol] + 4);
840 writeSymbolAuxFunctionEntry(
841 ExceptionSection.FileOffsetToData +
842 getExceptionOffset(Entry->second.FunctionSymbol),
843 Entry->second.FunctionSize, 0,
844 (
is64Bit() && ExceptionSection.isDebugEnabled)
845 ? SymbolIndexMap[Entry->second.FunctionSymbol] + 4
846 : SymbolIndexMap[Entry->second.FunctionSymbol] + 3);
848 writeSymbolEntry(SymbolRef.getSymbolTableName(),
849 CSectionRef.Address + SymbolOffset, SectionIndex,
850 SymbolRef.getVisibilityType(),
851 SymbolRef.getStorageClass());
853 writeSymbolAuxCsectEntry(CSectionRef.SymbolTableIndex,
XCOFF::XTY_LD,
854 CSectionRef.MCSec->getMappingClass());
857 void XCOFFObjectWriter::writeSymbolEntryForDwarfSection(
858 const XCOFFSection &DwarfSectionRef, int16_t SectionIndex) {
859 assert(DwarfSectionRef.MCSec->isDwarfSect() &&
"Not a DWARF section!");
861 writeSymbolEntry(DwarfSectionRef.getSymbolTableName(), 0,
864 writeSymbolAuxDwarfEntry(DwarfSectionRef.Size);
867 void XCOFFObjectWriter::writeSymbolEntryForControlSection(
868 const XCOFFSection &CSectionRef, int16_t SectionIndex,
870 writeSymbolEntry(CSectionRef.getSymbolTableName(), CSectionRef.Address,
871 SectionIndex, CSectionRef.getVisibilityType(),
StorageClass);
873 writeSymbolAuxCsectEntry(CSectionRef.Size, getEncodedType(CSectionRef.MCSec),
874 CSectionRef.MCSec->getMappingClass());
877 void XCOFFObjectWriter::writeSymbolAuxFunctionEntry(
uint32_t EntryOffset,
882 writeWord(LineNumberPointer);
887 writeWord(LineNumberPointer);
897 void XCOFFObjectWriter::writeSymbolAuxExceptionEntry(
uint64_t EntryOffset,
900 assert(
is64Bit() &&
"Exception auxilliary entries are 64-bit only.");
908 void XCOFFObjectWriter::writeFileHeader() {
912 writeWord(SymbolTableOffset);
914 W.write<
uint16_t>(auxiliaryHeaderSize());
916 W.write<int32_t>(SymbolTableEntryCount);
918 W.write<int32_t>(SymbolTableEntryCount);
919 W.write<
uint16_t>(auxiliaryHeaderSize());
924 void XCOFFObjectWriter::writeAuxFileHeader() {
925 if (!auxiliaryHeaderSize())
940 void XCOFFObjectWriter::writeSectionHeader(
const SectionEntry *Sec) {
944 if (Sec->Index == SectionEntry::UninitializedIndex)
953 writeWord(IsDwarf ? 0 : Sec->Address);
955 writeWord((IsDwarf || IsOvrflo) ? 0 : Sec->Address);
957 writeWord(Sec->Size);
958 writeWord(Sec->FileOffsetToData);
959 writeWord(Sec->FileOffsetToRelocations);
965 W.write<int32_t>(Sec->Flags);
974 ? Sec->RelocationCount
976 W.write<int32_t>(Sec->Flags);
980 void XCOFFObjectWriter::writeSectionHeaderTable() {
981 for (
const auto *CsectSec : Sections)
982 writeSectionHeader(CsectSec);
983 for (
const auto &DwarfSec : DwarfSections)
984 writeSectionHeader(&DwarfSec);
985 for (
const auto &OverflowSec : OverflowSections)
986 writeSectionHeader(&OverflowSec);
987 if (hasExceptionSection())
988 writeSectionHeader(&ExceptionSection);
991 void XCOFFObjectWriter::writeRelocation(XCOFFRelocation Reloc,
994 writeWord(
Section.Address + Reloc.FixupOffsetInCsect);
997 assert(
Section.MCSec->isDwarfSect() &&
"unsupport section type!");
998 writeWord(Reloc.FixupOffsetInCsect);
1000 W.write<
uint32_t>(Reloc.SymbolTableIndex);
1001 W.write<uint8_t>(Reloc.SignAndSize);
1002 W.write<uint8_t>(Reloc.Type);
1005 void XCOFFObjectWriter::writeRelocations() {
1006 for (
const auto *
Section : Sections) {
1007 if (
Section->Index == SectionEntry::UninitializedIndex)
1011 for (
const auto *Group :
Section->Groups) {
1015 for (
const auto &Csect : *Group) {
1016 for (
const auto Reloc : Csect.Relocations)
1017 writeRelocation(Reloc, Csect);
1022 for (
const auto &DwarfSection : DwarfSections)
1023 for (
const auto &Reloc : DwarfSection.DwarfSect->Relocations)
1024 writeRelocation(Reloc, *DwarfSection.DwarfSect);
1031 for (
const std::pair<std::string, size_t> &
F : FileNames) {
1037 for (
const auto &Csect : UndefinedCsects) {
1039 Csect.MCSec->getStorageClass());
1042 for (
const auto *
Section : Sections) {
1043 if (
Section->Index == SectionEntry::UninitializedIndex)
1047 for (
const auto *Group :
Section->Groups) {
1051 const int16_t SectionIndex =
Section->Index;
1052 for (
const auto &Csect : *Group) {
1054 writeSymbolEntryForControlSection(Csect, SectionIndex,
1055 Csect.MCSec->getStorageClass());
1057 for (
const auto &Sym : Csect.Syms)
1058 writeSymbolEntryForCsectMemberLabel(
1064 for (
const auto &DwarfSection : DwarfSections)
1065 writeSymbolEntryForDwarfSection(*DwarfSection.DwarfSect,
1066 DwarfSection.Index);
1069 void XCOFFObjectWriter::finalizeRelocationInfo(
SectionEntry *Sec,
1079 SecEntry.RelocationCount = Sec->Index;
1083 SecEntry.Address = RelCount;
1084 SecEntry.Index = ++SectionCount;
1085 OverflowSections.push_back(
std::move(SecEntry));
1091 Sec->RelocationCount = RelCount;
1095 void XCOFFObjectWriter::calcOffsetToRelocations(
SectionEntry *Sec,
1097 if (!Sec->RelocationCount)
1100 Sec->FileOffsetToRelocations = RawPointer;
1105 for (
auto &OverflowSec : OverflowSections) {
1106 if (OverflowSec.RelocationCount ==
static_cast<uint32_t>(Sec->Index)) {
1107 RelocationSizeInSec =
1112 OverflowSec.FileOffsetToRelocations = Sec->FileOffsetToRelocations;
1115 assert(RelocationSizeInSec &&
"Overflow section header doesn't exist.");
1117 RelocationSizeInSec = Sec->RelocationCount *
1122 RawPointer += RelocationSizeInSec;
1123 if (RawPointer > MaxRawDataSize)
1127 void XCOFFObjectWriter::finalizeSectionInfo() {
1128 for (
auto *
Section : Sections) {
1129 if (
Section->Index == SectionEntry::UninitializedIndex)
1134 for (
const auto *Group :
Section->Groups) {
1138 for (
auto &Csect : *Group)
1139 RelCount += Csect.Relocations.size();
1141 finalizeRelocationInfo(
Section, RelCount);
1144 for (
auto &DwarfSection : DwarfSections)
1145 finalizeRelocationInfo(&DwarfSection,
1146 DwarfSection.DwarfSect->Relocations.size());
1154 auxiliaryHeaderSize();
1157 for (
auto *Sec : Sections) {
1158 if (Sec->Index == SectionEntry::UninitializedIndex || Sec->IsVirtual)
1161 Sec->FileOffsetToData = RawPointer;
1162 RawPointer += Sec->Size;
1163 if (RawPointer > MaxRawDataSize)
1167 if (!DwarfSections.empty()) {
1168 RawPointer += PaddingsBeforeDwarf;
1169 for (
auto &DwarfSection : DwarfSections) {
1170 DwarfSection.FileOffsetToData = RawPointer;
1171 RawPointer += DwarfSection.MemorySize;
1172 if (RawPointer > MaxRawDataSize)
1177 if (hasExceptionSection()) {
1178 ExceptionSection.FileOffsetToData = RawPointer;
1179 RawPointer += ExceptionSection.Size;
1181 assert(RawPointer <= MaxRawDataSize &&
1182 "Section raw data overflowed this object file.");
1185 for (
auto *Sec : Sections) {
1186 if (Sec->Index != SectionEntry::UninitializedIndex)
1187 calcOffsetToRelocations(Sec, RawPointer);
1190 for (
auto &DwarfSec : DwarfSections)
1191 calcOffsetToRelocations(&DwarfSec, RawPointer);
1195 if (SymbolTableEntryCount)
1196 SymbolTableOffset = RawPointer;
1199 void XCOFFObjectWriter::addExceptionEntry(
1201 unsigned ReasonCode,
unsigned FunctionSize,
bool hasDebug) {
1205 ExceptionSection.isDebugEnabled =
true;
1206 auto Entry = ExceptionSection.ExceptionTable.find(
Symbol->getName());
1207 if (Entry != ExceptionSection.ExceptionTable.end()) {
1208 Entry->second.Entries.push_back(
1209 ExceptionTableEntry(
Trap, LanguageCode, ReasonCode));
1212 ExceptionInfo NewEntry;
1213 NewEntry.FunctionSymbol =
Symbol;
1214 NewEntry.FunctionSize = FunctionSize;
1215 NewEntry.Entries.push_back(
1216 ExceptionTableEntry(
Trap, LanguageCode, ReasonCode));
1217 ExceptionSection.ExceptionTable.insert(
1218 std::pair<const StringRef, ExceptionInfo>(
Symbol->getName(), NewEntry));
1221 unsigned XCOFFObjectWriter::getExceptionSectionSize() {
1222 unsigned EntryNum = 0;
1224 for (
auto it = ExceptionSection.ExceptionTable.begin();
1225 it != ExceptionSection.ExceptionTable.end(); ++
it)
1228 EntryNum +=
it->second.Entries.size() + 1;
1234 unsigned XCOFFObjectWriter::getExceptionOffset(
const MCSymbol *
Symbol) {
1235 unsigned EntryNum = 0;
1236 for (
auto it = ExceptionSection.ExceptionTable.begin();
1237 it != ExceptionSection.ExceptionTable.end(); ++
it) {
1238 if (
Symbol ==
it->second.FunctionSymbol)
1240 EntryNum +=
it->second.Entries.size() + 1;
1246 void XCOFFObjectWriter::assignAddressesAndIndices(
const MCAsmLayout &Layout) {
1248 uint32_t SymbolTableIndex = FileNames.size();
1251 for (
auto &Csect : UndefinedCsects) {
1254 Csect.SymbolTableIndex = SymbolTableIndex;
1255 SymbolIndexMap[Csect.MCSec->getQualNameSymbol()] = Csect.SymbolTableIndex;
1257 SymbolTableIndex += 2;
1265 int32_t SectionIndex = 1;
1266 bool HasTDataSection =
false;
1268 for (
auto *
Section : Sections) {
1269 const bool IsEmpty =
1271 [](
const CsectGroup *Group) { return Group->empty(); });
1275 if (SectionIndex > MaxSectionIndex)
1277 Section->Index = SectionIndex++;
1280 bool SectionAddressSet =
false;
1284 HasTDataSection =
true;
1291 for (
auto *Group :
Section->Groups) {
1295 for (
auto &Csect : *Group) {
1299 Address = Csect.Address + Csect.Size;
1300 Csect.SymbolTableIndex = SymbolTableIndex;
1303 SymbolTableIndex += 2;
1305 for (
auto &Sym : Csect.Syms) {
1306 bool hasExceptEntry =
false;
1308 ExceptionSection.ExceptionTable.find(Sym.MCSym->
getName());
1309 if (Entry != ExceptionSection.ExceptionTable.end()) {
1310 hasExceptEntry =
true;
1311 for (
auto &TrapEntry : Entry->second.Entries) {
1313 TrapEntry.Trap->getOffset();
1316 Sym.SymbolTableIndex = SymbolTableIndex;
1317 SymbolIndexMap[Sym.MCSym] = Sym.SymbolTableIndex;
1322 SymbolTableIndex += 2;
1323 if (hasExceptionSection() && hasExceptEntry) {
1324 if (
is64Bit() && ExceptionSection.isDebugEnabled)
1325 SymbolTableIndex += 2;
1327 SymbolTableIndex += 1;
1332 if (!SectionAddressSet) {
1333 Section->Address = Group->front().Address;
1334 SectionAddressSet =
true;
1349 if (!DwarfSections.empty())
1350 PaddingsBeforeDwarf =
1352 (*DwarfSections.begin()).DwarfSect->MCSec->getAlign()) -
1355 DwarfSectionEntry *LastDwarfSection =
nullptr;
1356 for (
auto &DwarfSection : DwarfSections) {
1357 assert((SectionIndex <= MaxSectionIndex) &&
"Section index overflow!");
1359 XCOFFSection &DwarfSect = *DwarfSection.DwarfSect;
1363 DwarfSection.Index = SectionIndex++;
1367 DwarfSect.SymbolTableIndex = SymbolTableIndex;
1370 SymbolTableIndex += 2;
1376 DwarfSection.Address = DwarfSect.Address =
1383 Address = DwarfSection.Address + DwarfSection.Size;
1385 if (LastDwarfSection)
1386 LastDwarfSection->MemorySize =
1387 DwarfSection.Address - LastDwarfSection->Address;
1388 LastDwarfSection = &DwarfSection;
1390 if (LastDwarfSection) {
1393 Address =
alignTo(LastDwarfSection->Address + LastDwarfSection->Size,
1394 DefaultSectionAlign);
1395 LastDwarfSection->MemorySize =
Address - LastDwarfSection->Address;
1397 if (hasExceptionSection()) {
1398 ExceptionSection.Index = SectionIndex++;
1400 ExceptionSection.Address = 0;
1401 ExceptionSection.Size = getExceptionSectionSize();
1402 Address += ExceptionSection.Size;
1406 SymbolTableEntryCount = SymbolTableIndex;
1409 void XCOFFObjectWriter::writeSectionForControlSectionEntry(
1411 const CsectSectionEntry &CsectEntry,
uint64_t &CurrentAddressLocation) {
1413 if (CsectEntry.Index == SectionEntry::UninitializedIndex)
1420 assert(((CurrentAddressLocation <= CsectEntry.Address) ||
1423 "CurrentAddressLocation should be less than or equal to section "
1424 "address if the section is not TData or TBSS.");
1426 CurrentAddressLocation = CsectEntry.Address;
1431 if (CsectEntry.IsVirtual) {
1432 CurrentAddressLocation += CsectEntry.Size;
1436 for (
const auto &Group : CsectEntry.Groups) {
1437 for (
const auto &Csect : *Group) {
1438 if (
uint32_t PaddingSize = Csect.Address - CurrentAddressLocation)
1439 W.OS.write_zeros(PaddingSize);
1441 Asm.writeSectionData(
W.OS, Csect.MCSec, Layout);
1442 CurrentAddressLocation = Csect.Address + Csect.Size;
1450 CsectEntry.Address + CsectEntry.Size - CurrentAddressLocation) {
1451 W.OS.write_zeros(PaddingSize);
1452 CurrentAddressLocation += PaddingSize;
1456 void XCOFFObjectWriter::writeSectionForDwarfSectionEntry(
1458 const DwarfSectionEntry &DwarfEntry,
uint64_t &CurrentAddressLocation) {
1462 assert(CurrentAddressLocation <= DwarfEntry.Address &&
1463 "CurrentAddressLocation should be less than or equal to section "
1466 if (
uint64_t PaddingSize = DwarfEntry.Address - CurrentAddressLocation)
1467 W.OS.write_zeros(PaddingSize);
1469 if (DwarfEntry.Size)
1470 Asm.writeSectionData(
W.OS, DwarfEntry.DwarfSect->MCSec, Layout);
1472 CurrentAddressLocation = DwarfEntry.Address + DwarfEntry.Size;
1476 uint32_t Mod = CurrentAddressLocation % DefaultSectionAlign;
1478 if (TailPaddingSize)
1479 W.OS.write_zeros(TailPaddingSize);
1481 CurrentAddressLocation += TailPaddingSize;
1484 void XCOFFObjectWriter::writeSectionForExceptionSectionEntry(
1486 ExceptionSectionEntry &ExceptionEntry,
uint64_t &CurrentAddressLocation) {
1487 for (
auto it = ExceptionEntry.ExceptionTable.begin();
1488 it != ExceptionEntry.ExceptionTable.end();
it++) {
1491 W.write<
uint32_t>(SymbolIndexMap[
it->second.FunctionSymbol]);
1494 W.OS.write_zeros(4);
1496 W.OS.write_zeros(2);
1497 for (
auto &TrapEntry :
it->second.Entries) {
1498 writeWord(TrapEntry.TrapAddress);
1499 W.write<uint8_t>(TrapEntry.Lang);
1500 W.write<uint8_t>(TrapEntry.Reason);
1504 CurrentAddressLocation += getExceptionSectionSize();
1515 uint8_t EncodedAlign = Log2Align << 3;
1521 std::unique_ptr<MCObjectWriter>
1524 return std::make_unique<XCOFFObjectWriter>(
std::move(MOTW), OS);