29constexpr unsigned DefaultSectionAlign = 4;
30constexpr int16_t MaxSectionIndex = INT16_MAX;
31constexpr uint32_t MaxRawDataSize = UINT32_MAX;
37 StrTblBuilder(StringTableBuilder::XCOFF) {
43 void reportOverwrite(uint64_t currentOffset, uint64_t specifiedOffset,
44 const Twine &fieldName);
45 bool nameShouldBeInStringTable(StringRef SymbolName);
46 bool initFileHeader(uint64_t CurrentOffset);
47 void initAuxFileHeader();
48 bool initSectionHeaders(uint64_t &CurrentOffset);
50 bool initStringTable();
51 bool assignAddressesAndIndices();
53 void writeFileHeader();
54 void writeAuxFileHeader();
55 void writeSectionHeaders();
56 bool writeSectionData();
57 bool writeRelocations();
61 bool writeAuxSymbol(
const XCOFFYAML::CsectAuxEnt &AuxSym);
62 bool writeAuxSymbol(
const XCOFFYAML::FileAuxEnt &AuxSym);
63 bool writeAuxSymbol(
const XCOFFYAML::FunctionAuxEnt &AuxSym);
64 bool writeAuxSymbol(
const XCOFFYAML::ExcpetionAuxEnt &AuxSym);
65 bool writeAuxSymbol(
const XCOFFYAML::BlockAuxEnt &AuxSym);
66 bool writeAuxSymbol(
const XCOFFYAML::SectAuxEntForDWARF &AuxSym);
67 bool writeAuxSymbol(
const XCOFFYAML::SectAuxEntForStat &AuxSym);
68 bool writeAuxSymbol(
const std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym);
70 XCOFFYAML::Object &Obj;
72 support::endian::Writer W;
74 StringTableBuilder StrTblBuilder;
75 uint64_t StartOffset = 0
u;
77 DenseMap<StringRef, int16_t> SectionIndexMap = {
81 XCOFFYAML::FileHeader InitFileHdr = Obj.
Header;
82 XCOFFYAML::AuxiliaryHeader InitAuxFileHdr;
83 std::vector<XCOFFYAML::Section> InitSections = Obj.
Sections;
90 memcpy(Name, StrName.
size() ? StrName.
data() : SrcName, StrName.
size());
95void XCOFFWriter::reportOverwrite(
uint64_t CurrentOffset,
97 const Twine &fieldName) {
98 ErrHandler(
"current file offset (" + Twine(CurrentOffset) +
99 ") is bigger than the specified " + fieldName +
" (" +
100 Twine(specifiedOffset) +
") ");
103bool XCOFFWriter::nameShouldBeInStringTable(StringRef SymbolName) {
108bool XCOFFWriter::initRelocations(uint64_t &CurrentOffset) {
109 for (XCOFFYAML::Section &InitSection : InitSections) {
110 if (!InitSection.Relocations.empty()) {
113 uint64_t UsedSize = RelSize * InitSection.Relocations.size();
117 if (!InitSection.NumberOfRelocations)
118 InitSection.NumberOfRelocations = InitSection.Relocations.size();
121 if (InitSection.FileOffsetToRelocations) {
122 if (CurrentOffset > InitSection.FileOffsetToRelocations) {
123 reportOverwrite(CurrentOffset, InitSection.FileOffsetToRelocations,
124 "FileOffsetToRelocations for the " +
125 InitSection.SectionName +
" section");
128 CurrentOffset = InitSection.FileOffsetToRelocations;
130 InitSection.FileOffsetToRelocations = CurrentOffset;
131 CurrentOffset += UsedSize;
132 if (CurrentOffset > MaxRawDataSize) {
133 ErrHandler(
"maximum object size (" + Twine(MaxRawDataSize) +
134 ") exceeded when writing relocation data for section " +
135 Twine(InitSection.SectionName));
143bool XCOFFWriter::initSectionHeaders(uint64_t &CurrentOffset) {
144 uint64_t CurrentEndDataAddr = 0;
145 uint64_t CurrentEndTDataAddr = 0;
146 for (uint16_t
I = 0,
E = InitSections.size();
I <
E; ++
I) {
149 int16_t &SectionIndex = SectionIndexMap[InitSections[
I].SectionName];
152 SectionIndex =
I + 1;
153 if ((
I + 1) > MaxSectionIndex) {
154 ErrHandler(
"exceeded the maximum permitted section index of " +
155 Twine(MaxSectionIndex));
161 if (!InitSections[
I].
Size)
162 InitSections[
I].Size = InitSections[
I].SectionData.binary_size();
175 switch (InitSections[
I].Flags) {
177 CurrentEndDataAddr = InitSections[
I].Address + InitSections[
I].Size;
181 InitSections[
I].Address = CurrentEndDataAddr;
184 CurrentEndTDataAddr = InitSections[
I].Address + InitSections[
I].Size;
188 InitSections[
I].Address = CurrentEndTDataAddr;
192 if (InitSections[
I].SectionData.binary_size()) {
193 if (InitSections[
I].FileOffsetToData) {
195 if (CurrentOffset > InitSections[
I].FileOffsetToData) {
196 reportOverwrite(CurrentOffset, InitSections[
I].FileOffsetToData,
197 "FileOffsetToData for the " +
198 InitSections[
I].SectionName +
" section");
201 CurrentOffset = InitSections[
I].FileOffsetToData;
203 CurrentOffset =
alignTo(CurrentOffset, DefaultSectionAlign);
204 InitSections[
I].FileOffsetToData = CurrentOffset;
206 CurrentOffset += InitSections[
I].SectionData.binary_size();
207 if (CurrentOffset > MaxRawDataSize) {
208 ErrHandler(
"maximum object size (" + Twine(MaxRawDataSize) +
209 ") exceeded when writing data for section " + Twine(
I + 1) +
210 " (" + Twine(InitSections[
I].SectionName) +
")");
214 if (InitSections[
I].SectionSubtype) {
215 uint32_t DWARFSubtype =
216 static_cast<uint32_t
>(*InitSections[
I].SectionSubtype);
218 ErrHandler(
"a DWARFSectionSubtype is only allowed for a DWARF section");
221 unsigned Mask = Is64Bit ? XCOFFSectionHeader64::SectionFlagsTypeMask
222 : XCOFFSectionHeader32::SectionFlagsTypeMask;
223 if (DWARFSubtype & Mask) {
224 ErrHandler(
"the low-order bits of DWARFSectionSubtype must be 0");
227 InitSections[
I].Flags |= DWARFSubtype;
233bool XCOFFWriter::initStringTable() {
234 if (Obj.StrTbl.RawContent) {
235 size_t RawSize = Obj.StrTbl.RawContent->binary_size();
236 if (Obj.StrTbl.Strings || Obj.StrTbl.Length) {
238 "can't specify Strings or Length when RawContent is specified");
241 if (Obj.StrTbl.ContentSize && *Obj.StrTbl.ContentSize < RawSize) {
242 ErrHandler(
"specified ContentSize (" + Twine(*Obj.StrTbl.ContentSize) +
243 ") is less than the RawContent data size (" + Twine(RawSize) +
249 if (Obj.StrTbl.ContentSize && *Obj.StrTbl.ContentSize <= 3) {
250 ErrHandler(
"ContentSize shouldn't be less than 4 without RawContent");
255 StrTblBuilder.clear();
257 if (Obj.StrTbl.Strings) {
259 for (StringRef StringEnt : *Obj.StrTbl.Strings)
260 StrTblBuilder.add(StringEnt);
262 size_t StrTblIdx = 0;
263 size_t NumOfStrings = Obj.StrTbl.Strings->size();
264 for (XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
265 if (nameShouldBeInStringTable(YamlSym.
SymbolName)) {
266 if (StrTblIdx < NumOfStrings) {
268 YamlSym.
SymbolName = (*Obj.StrTbl.Strings)[StrTblIdx];
277 for (
const XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
278 if (nameShouldBeInStringTable(YamlSym.
SymbolName))
285 for (
const XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
286 for (
const std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym :
289 if (nameShouldBeInStringTable(AS->FileNameOrString.value_or(
"")))
290 StrTblBuilder.add(AS->FileNameOrString.value_or(
""));
294 StrTblBuilder.finalize();
296 size_t StrTblSize = StrTblBuilder.getSize();
297 if (Obj.StrTbl.ContentSize && *Obj.StrTbl.ContentSize < StrTblSize) {
298 ErrHandler(
"specified ContentSize (" + Twine(*Obj.StrTbl.ContentSize) +
299 ") is less than the size of the data that would otherwise be "
301 Twine(StrTblSize) +
")");
308bool XCOFFWriter::initFileHeader(uint64_t CurrentOffset) {
311 InitFileHdr.NumberOfSections = Obj.Sections.size();
312 InitFileHdr.NumberOfSymTableEntries = Obj.Symbols.size();
314 for (XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
315 uint32_t AuxCount = YamlSym.
AuxEntries.size();
317 ErrHandler(
"specified NumberOfAuxEntries " +
319 " is less than the actual number "
320 "of auxiliary entries " +
330 if (InitFileHdr.NumberOfSymTableEntries) {
331 if (Obj.Header.SymbolTableOffset) {
332 if (CurrentOffset > Obj.Header.SymbolTableOffset) {
333 reportOverwrite(CurrentOffset, Obj.Header.SymbolTableOffset,
334 "SymbolTableOffset");
337 CurrentOffset = Obj.Header.SymbolTableOffset;
339 InitFileHdr.SymbolTableOffset = CurrentOffset;
342 if (CurrentOffset > MaxRawDataSize) {
343 ErrHandler(
"maximum object size of " + Twine(MaxRawDataSize) +
344 " exceeded when writing symbols");
352void XCOFFWriter::initAuxFileHeader() {
354 InitAuxFileHdr = *Obj.AuxHeader;
363 for (uint16_t
I = 0,
E = InitSections.size();
I <
E; ++
I) {
364 switch (InitSections[
I].Flags) {
366 if (!InitAuxFileHdr.TextSize)
367 InitAuxFileHdr.TextSize = InitSections[
I].Size;
368 if (!InitAuxFileHdr.TextStartAddr)
369 InitAuxFileHdr.TextStartAddr = InitSections[
I].Address;
370 if (!InitAuxFileHdr.SecNumOfText)
371 InitAuxFileHdr.SecNumOfText =
I + 1;
374 if (!InitAuxFileHdr.InitDataSize)
375 InitAuxFileHdr.InitDataSize = InitSections[
I].Size;
376 if (!InitAuxFileHdr.DataStartAddr)
377 InitAuxFileHdr.DataStartAddr = InitSections[
I].Address;
378 if (!InitAuxFileHdr.SecNumOfData)
379 InitAuxFileHdr.SecNumOfData =
I + 1;
382 if (!InitAuxFileHdr.BssDataSize)
383 InitAuxFileHdr.BssDataSize = InitSections[
I].Size;
384 if (!InitAuxFileHdr.SecNumOfBSS)
385 InitAuxFileHdr.SecNumOfBSS =
I + 1;
388 if (!InitAuxFileHdr.SecNumOfTData)
389 InitAuxFileHdr.SecNumOfTData =
I + 1;
392 if (!InitAuxFileHdr.SecNumOfTBSS)
393 InitAuxFileHdr.SecNumOfTBSS =
I + 1;
396 if (!InitAuxFileHdr.SecNumOfLoader)
397 InitAuxFileHdr.SecNumOfLoader =
I + 1;
405bool XCOFFWriter::assignAddressesAndIndices() {
406 uint64_t FileHdrSize =
411 uint64_t AuxFileHdrSize = 0;
413 if (Obj.Header.AuxHeaderSize)
414 AuxFileHdrSize = Obj.Header.AuxHeaderSize;
415 else if (Obj.AuxHeader)
418 uint64_t SecHdrSize =
420 uint64_t CurrentOffset =
421 FileHdrSize + AuxFileHdrSize + InitSections.size() * SecHdrSize;
424 if (!initSectionHeaders(CurrentOffset))
428 if (!initFileHeader(CurrentOffset))
430 InitFileHdr.AuxHeaderSize = AuxFileHdrSize;
437 return initStringTable();
440void XCOFFWriter::writeFileHeader() {
441 W.write<uint16_t>(Obj.Header.Magic ? Obj.Header.Magic : InitFileHdr.Magic);
442 W.write<uint16_t>(Obj.Header.NumberOfSections ? Obj.Header.NumberOfSections
443 : InitFileHdr.NumberOfSections);
444 W.write<int32_t>(Obj.Header.TimeStamp);
446 W.write<uint64_t>(InitFileHdr.SymbolTableOffset);
447 W.write<uint16_t>(InitFileHdr.AuxHeaderSize);
448 W.write<uint16_t>(Obj.Header.Flags);
449 W.write<int32_t>(Obj.Header.NumberOfSymTableEntries
450 ? Obj.Header.NumberOfSymTableEntries
451 : InitFileHdr.NumberOfSymTableEntries);
453 W.write<uint32_t>(InitFileHdr.SymbolTableOffset);
454 W.write<int32_t>(Obj.Header.NumberOfSymTableEntries
455 ? Obj.Header.NumberOfSymTableEntries
456 : InitFileHdr.NumberOfSymTableEntries);
457 W.write<uint16_t>(InitFileHdr.AuxHeaderSize);
458 W.write<uint16_t>(Obj.Header.Flags);
462void XCOFFWriter::writeAuxFileHeader() {
463 W.write<uint16_t>(InitAuxFileHdr.Magic.value_or(yaml::Hex16(1)));
464 W.write<uint16_t>(InitAuxFileHdr.Version.value_or(yaml::Hex16(1)));
467 W.write<uint64_t>(InitAuxFileHdr.TextStartAddr.value_or(yaml::Hex64(0)));
468 W.write<uint64_t>(InitAuxFileHdr.DataStartAddr.value_or(yaml::Hex64(0)));
469 W.write<uint64_t>(InitAuxFileHdr.TOCAnchorAddr.value_or(yaml::Hex64(0)));
471 W.write<uint32_t>(InitAuxFileHdr.TextSize.value_or(yaml::Hex64(0)));
472 W.write<uint32_t>(InitAuxFileHdr.InitDataSize.value_or(yaml::Hex64(0)));
473 W.write<uint32_t>(InitAuxFileHdr.BssDataSize.value_or(yaml::Hex64(0)));
474 W.write<uint32_t>(InitAuxFileHdr.EntryPointAddr.value_or(yaml::Hex64(0)));
475 W.write<uint32_t>(InitAuxFileHdr.TextStartAddr.value_or(yaml::Hex64(0)));
476 W.write<uint32_t>(InitAuxFileHdr.DataStartAddr.value_or(yaml::Hex64(0)));
480 W.write<uint32_t>(InitAuxFileHdr.TOCAnchorAddr.value_or(yaml::Hex64(0)));
482 W.write<uint16_t>(InitAuxFileHdr.SecNumOfEntryPoint.value_or(0));
483 W.write<uint16_t>(InitAuxFileHdr.SecNumOfText.value_or(0));
484 W.write<uint16_t>(InitAuxFileHdr.SecNumOfData.value_or(0));
485 W.write<uint16_t>(InitAuxFileHdr.SecNumOfTOC.value_or(0));
486 W.write<uint16_t>(InitAuxFileHdr.SecNumOfLoader.value_or(0));
487 W.write<uint16_t>(InitAuxFileHdr.SecNumOfBSS.value_or(0));
488 W.write<uint16_t>(InitAuxFileHdr.MaxAlignOfText.value_or(yaml::Hex16(0)));
489 W.write<uint16_t>(InitAuxFileHdr.MaxAlignOfData.value_or(yaml::Hex16(0)));
490 W.write<uint16_t>(InitAuxFileHdr.ModuleType.value_or(yaml::Hex16(0)));
491 W.write<uint8_t>(InitAuxFileHdr.CpuFlag.value_or(yaml::Hex8(0)));
494 W.write<uint8_t>(InitAuxFileHdr.TextPageSize.value_or(yaml::Hex8(0)));
495 W.write<uint8_t>(InitAuxFileHdr.DataPageSize.value_or(yaml::Hex8(0)));
496 W.write<uint8_t>(InitAuxFileHdr.StackPageSize.value_or(yaml::Hex8(0)));
498 InitAuxFileHdr.FlagAndTDataAlignment.value_or(yaml::Hex8(0x80)));
499 W.write<uint64_t>(InitAuxFileHdr.TextSize.value_or(yaml::Hex64(0)));
500 W.write<uint64_t>(InitAuxFileHdr.InitDataSize.value_or(yaml::Hex64(0)));
501 W.write<uint64_t>(InitAuxFileHdr.BssDataSize.value_or(yaml::Hex64(0)));
502 W.write<uint64_t>(InitAuxFileHdr.EntryPointAddr.value_or(yaml::Hex64(0)));
503 W.write<uint64_t>(InitAuxFileHdr.MaxStackSize.value_or(yaml::Hex64(0)));
504 W.write<uint64_t>(InitAuxFileHdr.MaxDataSize.value_or(yaml::Hex64(0)));
506 W.write<uint32_t>(InitAuxFileHdr.MaxStackSize.value_or(yaml::Hex64(0)));
507 W.write<uint32_t>(InitAuxFileHdr.MaxDataSize.value_or(yaml::Hex64(0)));
509 W.write<uint8_t>(InitAuxFileHdr.TextPageSize.value_or(yaml::Hex8(0)));
510 W.write<uint8_t>(InitAuxFileHdr.DataPageSize.value_or(yaml::Hex8(0)));
511 W.write<uint8_t>(InitAuxFileHdr.StackPageSize.value_or(yaml::Hex8(0)));
513 InitAuxFileHdr.FlagAndTDataAlignment.value_or(yaml::Hex8(0)));
515 W.write<uint16_t>(InitAuxFileHdr.SecNumOfTData.value_or(0));
516 W.write<uint16_t>(InitAuxFileHdr.SecNumOfTBSS.value_or(0));
528void XCOFFWriter::writeSectionHeaders() {
529 for (uint16_t
I = 0,
E = Obj.Sections.size();
I <
E; ++
I) {
530 XCOFFYAML::Section DerivedSec = InitSections[
I];
534 W.write<uint64_t>(DerivedSec.
Address);
535 W.write<uint64_t>(DerivedSec.
Address);
536 W.write<uint64_t>(DerivedSec.
Size);
542 W.write<int32_t>(DerivedSec.
Flags);
546 W.write<uint32_t>(DerivedSec.
Address);
547 W.write<uint32_t>(DerivedSec.
Address);
548 W.write<uint32_t>(DerivedSec.
Size);
554 W.write<int32_t>(DerivedSec.
Flags);
559bool XCOFFWriter::writeSectionData() {
560 for (uint16_t
I = 0,
E = Obj.Sections.size();
I <
E; ++
I) {
561 XCOFFYAML::Section YamlSec = Obj.Sections[
I];
564 int64_t PaddingSize = (uint64_t)InitSections[
I].FileOffsetToData -
565 (
W.OS.tell() - StartOffset);
566 if (PaddingSize < 0) {
567 ErrHandler(
"redundant data was written before section data");
570 W.OS.write_zeros(PaddingSize);
577bool XCOFFWriter::writeRelocations() {
578 for (uint16_t
I = 0,
E = Obj.Sections.size();
I <
E; ++
I) {
579 XCOFFYAML::Section YamlSec = Obj.Sections[
I];
581 int64_t PaddingSize =
582 InitSections[
I].FileOffsetToRelocations - (
W.OS.tell() - StartOffset);
583 if (PaddingSize < 0) {
584 ErrHandler(
"redundant data was written before relocations");
587 W.OS.write_zeros(PaddingSize);
588 for (
const XCOFFYAML::Relocation &YamlRel : YamlSec.
Relocations) {
594 W.write<uint8_t>(YamlRel.
Info);
595 W.write<uint8_t>(YamlRel.
Type);
602bool XCOFFWriter::writeAuxSymbol(
const XCOFFYAML::CsectAuxEnt &AuxSym) {
603 uint8_t SymAlignAndType = 0;
606 ErrHandler(
"cannot specify SymbolType or SymbolAlignment if "
607 "SymbolAlignmentAndType is specified");
615 ErrHandler(
"symbol type must be less than " +
622 const uint8_t ShiftedSymbolAlignmentMask =
627 ErrHandler(
"symbol alignment must be less than " +
628 Twine(1 + ShiftedSymbolAlignmentMask));
639 W.write<uint8_t>(SymAlignAndType);
648 W.write<uint8_t>(SymAlignAndType);
656bool XCOFFWriter::writeAuxSymbol(
const XCOFFYAML::ExcpetionAuxEnt &AuxSym) {
657 assert(Is64Bit &&
"can't write the exception auxiliary symbol for XCOFF32");
666bool XCOFFWriter::writeAuxSymbol(
const XCOFFYAML::FunctionAuxEnt &AuxSym) {
683bool XCOFFWriter::writeAuxSymbol(
const XCOFFYAML::FileAuxEnt &AuxSym) {
685 if (nameShouldBeInStringTable(FileName)) {
687 W.write<uint32_t>(StrTblBuilder.getOffset(FileName));
689 writeName(FileName, W);
702bool XCOFFWriter::writeAuxSymbol(
const XCOFFYAML::BlockAuxEnt &AuxSym) {
704 W.write<uint32_t>(AuxSym.
LineNum.value_or(0));
705 W.OS.write_zeros(13);
709 W.write<uint16_t>(AuxSym.
LineNumHi.value_or(0));
710 W.write<uint16_t>(AuxSym.
LineNumLo.value_or(0));
711 W.OS.write_zeros(12);
716bool XCOFFWriter::writeAuxSymbol(
const XCOFFYAML::SectAuxEntForDWARF &AuxSym) {
731bool XCOFFWriter::writeAuxSymbol(
const XCOFFYAML::SectAuxEntForStat &AuxSym) {
732 assert(!Is64Bit &&
"can't write the stat auxiliary symbol for XCOFF64");
736 W.OS.write_zeros(10);
740bool XCOFFWriter::writeAuxSymbol(
741 const std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym) {
743 return writeAuxSymbol(*AS);
745 return writeAuxSymbol(*AS);
747 return writeAuxSymbol(*AS);
749 return writeAuxSymbol(*AS);
751 return writeAuxSymbol(*AS);
753 return writeAuxSymbol(*AS);
755 return writeAuxSymbol(*AS);
760bool XCOFFWriter::writeSymbols() {
761 int64_t PaddingSize =
762 InitFileHdr.SymbolTableOffset - (
W.OS.tell() - StartOffset);
763 if (PaddingSize < 0) {
764 ErrHandler(
"redundant data was written before symbols");
767 W.OS.write_zeros(PaddingSize);
768 for (
const XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
770 W.write<uint64_t>(YamlSym.
Value);
771 W.write<uint32_t>(StrTblBuilder.getOffset(YamlSym.
SymbolName));
773 if (nameShouldBeInStringTable(YamlSym.
SymbolName)) {
777 W.write<uint32_t>(StrTblBuilder.getOffset(YamlSym.
SymbolName));
781 W.write<uint32_t>(YamlSym.
Value);
784 auto It = SectionIndexMap.find(*YamlSym.
SectionName);
785 if (It == SectionIndexMap.end()) {
786 ErrHandler(
"the SectionName " + *YamlSym.
SectionName +
787 " specified in the symbol does not exist");
791 ErrHandler(
"the SectionName " + *YamlSym.
SectionName +
792 " and the SectionIndex (" + Twine(*YamlSym.
SectionIndex) +
793 ") refer to different sections");
796 W.write<int16_t>(It->second);
800 W.write<uint16_t>(YamlSym.
Type);
804 W.write<uint8_t>(NumOfAuxSym);
806 if (!NumOfAuxSym && !YamlSym.
AuxEntries.size())
813 for (
const std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym :
815 if (!writeAuxSymbol(AuxSym))
827void XCOFFWriter::writeStringTable() {
828 if (Obj.StrTbl.RawContent) {
829 Obj.StrTbl.RawContent->writeAsBinary(
W.OS);
830 if (Obj.StrTbl.ContentSize) {
831 assert(*Obj.StrTbl.ContentSize >= Obj.StrTbl.RawContent->binary_size() &&
832 "Specified ContentSize is less than the RawContent size.");
833 W.OS.write_zeros(*Obj.StrTbl.ContentSize -
834 Obj.StrTbl.RawContent->binary_size());
839 size_t StrTblBuilderSize = StrTblBuilder.getSize();
842 if (!Obj.StrTbl.Length && !Obj.StrTbl.ContentSize) {
843 if (StrTblBuilderSize <= 4)
845 StrTblBuilder.write(
W.OS);
850 std::unique_ptr<WritableMemoryBuffer> Buf =
852 uint8_t *
Ptr =
reinterpret_cast<uint8_t *
>(Buf->getBufferStart());
853 StrTblBuilder.write(
Ptr);
858 : *Obj.StrTbl.ContentSize);
860 W.OS.write(Buf->getBufferStart(), Buf->getBufferSize());
862 if (Obj.StrTbl.ContentSize) {
863 assert(*Obj.StrTbl.ContentSize >= StrTblBuilderSize &&
864 "Specified ContentSize is less than the StringTableBuilder size.");
865 W.OS.write_zeros(*Obj.StrTbl.ContentSize - StrTblBuilderSize);
869bool XCOFFWriter::writeXCOFF() {
870 if (!assignAddressesAndIndices())
872 StartOffset =
W.OS.tell();
874 if (InitFileHdr.AuxHeaderSize)
875 writeAuxFileHeader();
876 if (!Obj.Sections.empty()) {
877 writeSectionHeaders();
878 if (!writeSectionData())
880 if (!writeRelocations())
883 if (!Obj.Symbols.empty() && !writeSymbols())
895 XCOFFWriter Writer(Doc, Out, EH);
896 return Writer.writeXCOFF();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
static Error initRelocations(RelocationSection *Relocs, T RelRange)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static LLVM_ABI std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
static constexpr uint8_t SymbolTypeMask
static constexpr size_t SymbolAlignmentBitOffset
static constexpr uint8_t SymbolAlignmentMask
This class implements an extremely fast bulk output stream that can only output to a stream.
ArrayRef< uint8_t >::size_type binary_size() const
The number of bytes that are represented by this BinaryRef.
LLVM_ABI void writeAsBinary(raw_ostream &OS, uint64_t N=UINT64_MAX) const
Write the contents (regardless of whether it is binary or a hex string) as binary to the given raw_os...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ SHR_SYMTAB
At exec time, create shared symbol table for program (main program only).
constexpr size_t RelocationSerializationSize32
constexpr size_t RelocationSerializationSize64
constexpr size_t FileHeaderSize64
constexpr size_t AuxFileHeaderSize64
constexpr size_t SectionHeaderSize64
@ AUX_SECT
Identifies a SECT auxiliary entry.
@ AUX_FILE
Identifies a file auxiliary entry.
@ AUX_EXCEPT
Identifies an exception auxiliary entry.
@ AUX_FCN
Identifies a function auxiliary entry.
@ AUX_SYM
Identifies a symbol auxiliary entry.
@ AUX_CSECT
Identifies a csect auxiliary entry.
constexpr size_t NameSize
constexpr size_t AuxFileHeaderSizeShort
constexpr size_t AuxFileHeaderSize32
@ XFT_FN
Specifies the source-file name.
constexpr size_t FileHeaderSize32
constexpr size_t SectionHeaderSize32
constexpr size_t SymbolTableEntrySize
constexpr size_t FileNamePadSize
static void writeStringTable(std::vector< uint8_t > &B, ArrayRef< const std::string_view > Strings)
void write32be(void *P, uint32_t V)
llvm::function_ref< void(const Twine &Msg)> ErrorHandler
LLVM_ABI bool yaml2xcoff(XCOFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
std::optional< uint32_t > LineNum
std::optional< uint16_t > LineNumHi
std::optional< uint16_t > LineNumLo
std::optional< uint16_t > TypeChkSectNum
std::optional< uint32_t > ParameterHashIndex
std::optional< uint32_t > SectionOrLength
std::optional< uint32_t > SectionOrLengthHi
std::optional< uint8_t > SymbolAlignmentAndType
std::optional< uint32_t > SectionOrLengthLo
std::optional< uint8_t > SymbolAlignment
std::optional< uint16_t > StabSectNum
std::optional< XCOFF::StorageMappingClass > StorageMappingClass
std::optional< uint32_t > StabInfoIndex
std::optional< XCOFF::SymbolType > SymbolType
std::optional< int32_t > SymIdxOfNextBeyond
std::optional< uint32_t > SizeOfFunction
std::optional< uint64_t > OffsetToExceptionTbl
std::optional< XCOFF::CFileStringType > FileStringType
std::optional< StringRef > FileNameOrString
std::optional< int32_t > SymIdxOfNextBeyond
std::optional< uint32_t > OffsetToExceptionTbl
std::optional< uint64_t > PtrToLineNum
std::optional< uint32_t > SizeOfFunction
std::vector< Section > Sections
llvm::yaml::Hex64 SymbolIndex
llvm::yaml::Hex64 VirtualAddress
std::optional< uint32_t > NumberOfRelocEnt
std::optional< uint32_t > LengthOfSectionPortion
std::optional< uint16_t > NumberOfRelocEnt
std::optional< uint16_t > NumberOfLineNum
std::optional< uint32_t > SectionLength
llvm::yaml::Hex64 FileOffsetToRelocations
llvm::yaml::Hex64 Address
llvm::yaml::Hex64 FileOffsetToData
yaml::BinaryRef SectionData
std::vector< Relocation > Relocations
llvm::yaml::Hex16 NumberOfLineNumbers
llvm::yaml::Hex16 NumberOfRelocations
llvm::yaml::Hex64 FileOffsetToLineNumbers
std::vector< std::unique_ptr< AuxSymbolEnt > > AuxEntries
std::optional< uint16_t > SectionIndex
std::optional< uint8_t > NumberOfAuxEntries
XCOFF::StorageClass StorageClass
std::optional< StringRef > SectionName
Adapter to write values to a stream in a particular byte order.
Common declarations for yaml2obj.