1 //===-- lib/MC/XCOFFObjectWriter.cpp - XCOFF file writer ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements XCOFF object file writer information.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/BinaryFormat/XCOFF.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSectionXCOFF.h"
18 #include "llvm/MC/MCSymbolXCOFF.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/MC/MCXCOFFObjectWriter.h"
21 #include "llvm/MC/StringTableBuilder.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/MathExtras.h"
24 
25 #include <deque>
26 
27 using namespace llvm;
28 
29 // An XCOFF object file has a limited set of predefined sections. The most
30 // important ones for us (right now) are:
31 // .text --> contains program code and read-only data.
32 // .data --> contains initialized data, function descriptors, and the TOC.
33 // .bss  --> contains uninitialized data.
34 // Each of these sections is composed of 'Control Sections'. A Control Section
35 // is more commonly referred to as a csect. A csect is an indivisible unit of
36 // code or data, and acts as a container for symbols. A csect is mapped
37 // into a section based on its storage-mapping class, with the exception of
38 // XMC_RW which gets mapped to either .data or .bss based on whether it's
39 // explicitly initialized or not.
40 //
41 // We don't represent the sections in the MC layer as there is nothing
42 // interesting about them at at that level: they carry information that is
43 // only relevant to the ObjectWriter, so we materialize them in this class.
44 namespace {
45 
46 constexpr unsigned DefaultSectionAlign = 4;
47 constexpr int16_t MaxSectionIndex = INT16_MAX;
48 
49 // Packs the csect's alignment and type into a byte.
50 uint8_t getEncodedType(const MCSectionXCOFF *);
51 
52 // Wrapper around an MCSymbolXCOFF.
53 struct Symbol {
54   const MCSymbolXCOFF *const MCSym;
55   uint32_t SymbolTableIndex;
56 
57   XCOFF::StorageClass getStorageClass() const {
58     return MCSym->getStorageClass();
59   }
60   StringRef getName() const { return MCSym->getName(); }
61   Symbol(const MCSymbolXCOFF *MCSym) : MCSym(MCSym), SymbolTableIndex(-1) {}
62 };
63 
64 // Wrapper for an MCSectionXCOFF.
65 struct ControlSection {
66   const MCSectionXCOFF *const MCCsect;
67   uint32_t SymbolTableIndex;
68   uint32_t Address;
69   uint32_t Size;
70 
71   SmallVector<Symbol, 1> Syms;
72   StringRef getName() const { return MCCsect->getSectionName(); }
73   ControlSection(const MCSectionXCOFF *MCSec)
74       : MCCsect(MCSec), SymbolTableIndex(-1), Address(-1), Size(0) {}
75 };
76 
77 // Type to be used for a container representing a set of csects with
78 // (approximately) the same storage mapping class. For example all the csects
79 // with a storage mapping class of `xmc_pr` will get placed into the same
80 // container.
81 using CsectGroup = std::deque<ControlSection>;
82 
83 using CsectGroups = std::deque<CsectGroup *>;
84 
85 // Represents the data related to a section excluding the csects that make up
86 // the raw data of the section. The csects are stored separately as not all
87 // sections contain csects, and some sections contain csects which are better
88 // stored separately, e.g. the .data section containing read-write, descriptor,
89 // TOCBase and TOC-entry csects.
90 struct Section {
91   char Name[XCOFF::NameSize];
92   // The physical/virtual address of the section. For an object file
93   // these values are equivalent.
94   uint32_t Address;
95   uint32_t Size;
96   uint32_t FileOffsetToData;
97   uint32_t FileOffsetToRelocations;
98   uint32_t RelocationCount;
99   int32_t Flags;
100 
101   int16_t Index;
102 
103   // Virtual sections do not need storage allocated in the object file.
104   const bool IsVirtual;
105 
106   // XCOFF has special section numbers for symbols:
107   // -2 Specifies N_DEBUG, a special symbolic debugging symbol.
108   // -1 Specifies N_ABS, an absolute symbol. The symbol has a value but is not
109   // relocatable.
110   //  0 Specifies N_UNDEF, an undefined external symbol.
111   // Therefore, we choose -3 (N_DEBUG - 1) to represent a section index that
112   // hasn't been initialized.
113   static constexpr int16_t UninitializedIndex =
114       XCOFF::ReservedSectionNum::N_DEBUG - 1;
115 
116   CsectGroups Groups;
117 
118   void reset() {
119     Address = 0;
120     Size = 0;
121     FileOffsetToData = 0;
122     FileOffsetToRelocations = 0;
123     RelocationCount = 0;
124     Index = UninitializedIndex;
125     // Clear any csects we have stored.
126     for (auto *Group : Groups)
127       Group->clear();
128   }
129 
130   Section(const char *N, XCOFF::SectionTypeFlags Flags, bool IsVirtual,
131           CsectGroups Groups)
132       : Address(0), Size(0), FileOffsetToData(0), FileOffsetToRelocations(0),
133         RelocationCount(0), Flags(Flags), Index(UninitializedIndex),
134         IsVirtual(IsVirtual), Groups(Groups) {
135     strncpy(Name, N, XCOFF::NameSize);
136   }
137 };
138 
139 class XCOFFObjectWriter : public MCObjectWriter {
140 
141   uint32_t SymbolTableEntryCount = 0;
142   uint32_t SymbolTableOffset = 0;
143   uint16_t SectionCount = 0;
144 
145   support::endian::Writer W;
146   std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter;
147   StringTableBuilder Strings;
148 
149   // CsectGroups. These store the csects which make up different parts of
150   // the sections. Should have one for each set of csects that get mapped into
151   // the same section and get handled in a 'similar' way.
152   CsectGroup UndefinedCsects;
153   CsectGroup ProgramCodeCsects;
154   CsectGroup ReadOnlyCsects;
155   CsectGroup DataCsects;
156   CsectGroup FuncDSCsects;
157   CsectGroup TOCCsects;
158   CsectGroup BSSCsects;
159 
160   // The Predefined sections.
161   Section Text;
162   Section Data;
163   Section BSS;
164 
165   // All the XCOFF sections, in the order they will appear in the section header
166   // table.
167   std::array<Section *const, 3> Sections{{&Text, &Data, &BSS}};
168 
169   CsectGroup &getCsectGroup(const MCSectionXCOFF *MCSec);
170 
171   virtual void reset() override;
172 
173   void executePostLayoutBinding(MCAssembler &, const MCAsmLayout &) override;
174 
175   void recordRelocation(MCAssembler &, const MCAsmLayout &, const MCFragment *,
176                         const MCFixup &, MCValue, uint64_t &) override;
177 
178   uint64_t writeObject(MCAssembler &, const MCAsmLayout &) override;
179 
180   static bool nameShouldBeInStringTable(const StringRef &);
181   void writeSymbolName(const StringRef &);
182   void writeSymbolTableEntryForCsectMemberLabel(const Symbol &,
183                                                 const ControlSection &, int16_t,
184                                                 uint64_t);
185   void writeSymbolTableEntryForControlSection(const ControlSection &, int16_t,
186                                               XCOFF::StorageClass);
187   void writeFileHeader();
188   void writeSectionHeaderTable();
189   void writeSections(const MCAssembler &Asm, const MCAsmLayout &Layout);
190   void writeSymbolTable(const MCAsmLayout &Layout);
191 
192   // Called after all the csects and symbols have been processed by
193   // `executePostLayoutBinding`, this function handles building up the majority
194   // of the structures in the object file representation. Namely:
195   // *) Calculates physical/virtual addresses, raw-pointer offsets, and section
196   //    sizes.
197   // *) Assigns symbol table indices.
198   // *) Builds up the section header table by adding any non-empty sections to
199   //    `Sections`.
200   void assignAddressesAndIndices(const MCAsmLayout &);
201 
202   bool
203   needsAuxiliaryHeader() const { /* TODO aux header support not implemented. */
204     return false;
205   }
206 
207   // Returns the size of the auxiliary header to be written to the object file.
208   size_t auxiliaryHeaderSize() const {
209     assert(!needsAuxiliaryHeader() &&
210            "Auxiliary header support not implemented.");
211     return 0;
212   }
213 
214 public:
215   XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
216                     raw_pwrite_stream &OS);
217 };
218 
219 XCOFFObjectWriter::XCOFFObjectWriter(
220     std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
221     : W(OS, support::big), TargetObjectWriter(std::move(MOTW)),
222       Strings(StringTableBuilder::XCOFF),
223       Text(".text", XCOFF::STYP_TEXT, /* IsVirtual */ false,
224            CsectGroups{&ProgramCodeCsects, &ReadOnlyCsects}),
225       Data(".data", XCOFF::STYP_DATA, /* IsVirtual */ false,
226            CsectGroups{&DataCsects, &FuncDSCsects, &TOCCsects}),
227       BSS(".bss", XCOFF::STYP_BSS, /* IsVirtual */ true,
228           CsectGroups{&BSSCsects}) {}
229 
230 void XCOFFObjectWriter::reset() {
231   UndefinedCsects.clear();
232 
233   // Reset any sections we have written to, and empty the section header table.
234   for (auto *Sec : Sections)
235     Sec->reset();
236 
237   // Reset the symbol table and string table.
238   SymbolTableEntryCount = 0;
239   SymbolTableOffset = 0;
240   SectionCount = 0;
241   Strings.clear();
242 
243   MCObjectWriter::reset();
244 }
245 
246 CsectGroup &XCOFFObjectWriter::getCsectGroup(const MCSectionXCOFF *MCSec) {
247   switch (MCSec->getMappingClass()) {
248   case XCOFF::XMC_PR:
249     assert(XCOFF::XTY_SD == MCSec->getCSectType() &&
250            "Only an initialized csect can contain program code.");
251     return ProgramCodeCsects;
252   case XCOFF::XMC_RO:
253     assert(XCOFF::XTY_SD == MCSec->getCSectType() &&
254            "Only an initialized csect can contain read only data.");
255     return ReadOnlyCsects;
256   case XCOFF::XMC_RW:
257     if (XCOFF::XTY_CM == MCSec->getCSectType())
258       return BSSCsects;
259 
260     if (XCOFF::XTY_SD == MCSec->getCSectType())
261       return DataCsects;
262 
263     report_fatal_error("Unhandled mapping of read-write csect to section.");
264   case XCOFF::XMC_DS:
265     return FuncDSCsects;
266   case XCOFF::XMC_BS:
267     assert(XCOFF::XTY_CM == MCSec->getCSectType() &&
268            "Mapping invalid csect. CSECT with bss storage class must be "
269            "common type.");
270     return BSSCsects;
271   case XCOFF::XMC_TC0:
272     assert(XCOFF::XTY_SD == MCSec->getCSectType() &&
273            "Only an initialized csect can contain TOC-base.");
274     assert(TOCCsects.empty() &&
275            "We should have only one TOC-base, and it should be the first csect "
276            "in this CsectGroup.");
277     return TOCCsects;
278   case XCOFF::XMC_TC:
279     assert(XCOFF::XTY_SD == MCSec->getCSectType() &&
280            "Only an initialized csect can contain TC entry.");
281     assert(!TOCCsects.empty() &&
282            "We should at least have a TOC-base in this CsectGroup.");
283     return TOCCsects;
284   default:
285     report_fatal_error("Unhandled mapping of csect to section.");
286   }
287 }
288 
289 void XCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
290                                                  const MCAsmLayout &Layout) {
291   if (TargetObjectWriter->is64Bit())
292     report_fatal_error("64-bit XCOFF object files are not supported yet.");
293 
294   // Maps the MC Section representation to its corresponding ControlSection
295   // wrapper. Needed for finding the ControlSection to insert an MCSymbol into
296   // from its containing MCSectionXCOFF.
297   DenseMap<const MCSectionXCOFF *, ControlSection *> WrapperMap;
298 
299   for (const auto &S : Asm) {
300     const auto *MCSec = cast<const MCSectionXCOFF>(&S);
301     assert(WrapperMap.find(MCSec) == WrapperMap.end() &&
302            "Cannot add a csect twice.");
303     assert(XCOFF::XTY_ER != MCSec->getCSectType() &&
304            "An undefined csect should not get registered.");
305 
306     // If the name does not fit in the storage provided in the symbol table
307     // entry, add it to the string table.
308     if (nameShouldBeInStringTable(MCSec->getSectionName()))
309       Strings.add(MCSec->getSectionName());
310 
311     CsectGroup &Group = getCsectGroup(MCSec);
312     Group.emplace_back(MCSec);
313     WrapperMap[MCSec] = &Group.back();
314   }
315 
316   for (const MCSymbol &S : Asm.symbols()) {
317     // Nothing to do for temporary symbols.
318     if (S.isTemporary())
319       continue;
320 
321     const MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(&S);
322     const MCSectionXCOFF *ContainingCsect = XSym->getContainingCsect();
323 
324     // Handle undefined symbol.
325     if (ContainingCsect->getCSectType() == XCOFF::XTY_ER) {
326       UndefinedCsects.emplace_back(ContainingCsect);
327       continue;
328     }
329 
330     // If the symbol is the csect itself, we don't need to put the symbol
331     // into csect's Syms.
332     if (XSym == ContainingCsect->getQualNameSymbol())
333       continue;
334 
335     assert(WrapperMap.find(ContainingCsect) != WrapperMap.end() &&
336            "Expected containing csect to exist in map");
337 
338     // Lookup the containing csect and add the symbol to it.
339     WrapperMap[ContainingCsect]->Syms.emplace_back(XSym);
340 
341     // If the name does not fit in the storage provided in the symbol table
342     // entry, add it to the string table.
343     if (nameShouldBeInStringTable(XSym->getName()))
344       Strings.add(XSym->getName());
345     }
346 
347   Strings.finalize();
348   assignAddressesAndIndices(Layout);
349 }
350 
351 void XCOFFObjectWriter::recordRelocation(MCAssembler &, const MCAsmLayout &,
352                                          const MCFragment *, const MCFixup &,
353                                          MCValue, uint64_t &) {
354   // TODO: recordRelocation is not yet implemented.
355 }
356 
357 void XCOFFObjectWriter::writeSections(const MCAssembler &Asm,
358                                       const MCAsmLayout &Layout) {
359   uint32_t CurrentAddressLocation = 0;
360   for (const auto *Section : Sections) {
361     // Nothing to write for this Section.
362     if (Section->Index == Section::UninitializedIndex || Section->IsVirtual)
363       continue;
364 
365     assert(CurrentAddressLocation == Section->Address &&
366            "Sections should be written consecutively.");
367     for (const auto *Group : Section->Groups) {
368       for (const auto &Csect : *Group) {
369         if (uint32_t PaddingSize = Csect.Address - CurrentAddressLocation)
370           W.OS.write_zeros(PaddingSize);
371         if (Csect.Size)
372           Asm.writeSectionData(W.OS, Csect.MCCsect, Layout);
373         CurrentAddressLocation = Csect.Address + Csect.Size;
374       }
375     }
376 
377     // The size of the tail padding in a section is the end virtual address of
378     // the current section minus the the end virtual address of the last csect
379     // in that section.
380     if (uint32_t PaddingSize =
381             Section->Address + Section->Size - CurrentAddressLocation) {
382       W.OS.write_zeros(PaddingSize);
383       CurrentAddressLocation += PaddingSize;
384     }
385   }
386 }
387 
388 uint64_t XCOFFObjectWriter::writeObject(MCAssembler &Asm,
389                                         const MCAsmLayout &Layout) {
390   // We always emit a timestamp of 0 for reproducibility, so ensure incremental
391   // linking is not enabled, in case, like with Windows COFF, such a timestamp
392   // is incompatible with incremental linking of XCOFF.
393   if (Asm.isIncrementalLinkerCompatible())
394     report_fatal_error("Incremental linking not supported for XCOFF.");
395 
396   if (TargetObjectWriter->is64Bit())
397     report_fatal_error("64-bit XCOFF object files are not supported yet.");
398 
399   uint64_t StartOffset = W.OS.tell();
400 
401   writeFileHeader();
402   writeSectionHeaderTable();
403   writeSections(Asm, Layout);
404   // TODO writeRelocations();
405 
406   writeSymbolTable(Layout);
407   // Write the string table.
408   Strings.write(W.OS);
409 
410   return W.OS.tell() - StartOffset;
411 }
412 
413 bool XCOFFObjectWriter::nameShouldBeInStringTable(const StringRef &SymbolName) {
414   return SymbolName.size() > XCOFF::NameSize;
415 }
416 
417 void XCOFFObjectWriter::writeSymbolName(const StringRef &SymbolName) {
418   if (nameShouldBeInStringTable(SymbolName)) {
419     W.write<int32_t>(0);
420     W.write<uint32_t>(Strings.getOffset(SymbolName));
421   } else {
422     char Name[XCOFF::NameSize+1];
423     std::strncpy(Name, SymbolName.data(), XCOFF::NameSize);
424     ArrayRef<char> NameRef(Name, XCOFF::NameSize);
425     W.write(NameRef);
426   }
427 }
428 
429 void XCOFFObjectWriter::writeSymbolTableEntryForCsectMemberLabel(
430     const Symbol &SymbolRef, const ControlSection &CSectionRef,
431     int16_t SectionIndex, uint64_t SymbolOffset) {
432   // Name or Zeros and string table offset
433   writeSymbolName(SymbolRef.getName());
434   assert(SymbolOffset <= UINT32_MAX - CSectionRef.Address &&
435          "Symbol address overflows.");
436   W.write<uint32_t>(CSectionRef.Address + SymbolOffset);
437   W.write<int16_t>(SectionIndex);
438   // Basic/Derived type. See the description of the n_type field for symbol
439   // table entries for a detailed description. Since we don't yet support
440   // visibility, and all other bits are either optionally set or reserved, this
441   // is always zero.
442   // TODO FIXME How to assert a symbol's visibilty is default?
443   // TODO Set the function indicator (bit 10, 0x0020) for functions
444   // when debugging is enabled.
445   W.write<uint16_t>(0);
446   W.write<uint8_t>(SymbolRef.getStorageClass());
447   // Always 1 aux entry for now.
448   W.write<uint8_t>(1);
449 
450   // Now output the auxiliary entry.
451   W.write<uint32_t>(CSectionRef.SymbolTableIndex);
452   // Parameter typecheck hash. Not supported.
453   W.write<uint32_t>(0);
454   // Typecheck section number. Not supported.
455   W.write<uint16_t>(0);
456   // Symbol type: Label
457   W.write<uint8_t>(XCOFF::XTY_LD);
458   // Storage mapping class.
459   W.write<uint8_t>(CSectionRef.MCCsect->getMappingClass());
460   // Reserved (x_stab).
461   W.write<uint32_t>(0);
462   // Reserved (x_snstab).
463   W.write<uint16_t>(0);
464 }
465 
466 void XCOFFObjectWriter::writeSymbolTableEntryForControlSection(
467     const ControlSection &CSectionRef, int16_t SectionIndex,
468     XCOFF::StorageClass StorageClass) {
469   // n_name, n_zeros, n_offset
470   writeSymbolName(CSectionRef.getName());
471   // n_value
472   W.write<uint32_t>(CSectionRef.Address);
473   // n_scnum
474   W.write<int16_t>(SectionIndex);
475   // Basic/Derived type. See the description of the n_type field for symbol
476   // table entries for a detailed description. Since we don't yet support
477   // visibility, and all other bits are either optionally set or reserved, this
478   // is always zero.
479   // TODO FIXME How to assert a symbol's visibilty is default?
480   // TODO Set the function indicator (bit 10, 0x0020) for functions
481   // when debugging is enabled.
482   W.write<uint16_t>(0);
483   // n_sclass
484   W.write<uint8_t>(StorageClass);
485   // Always 1 aux entry for now.
486   W.write<uint8_t>(1);
487 
488   // Now output the auxiliary entry.
489   W.write<uint32_t>(CSectionRef.Size);
490   // Parameter typecheck hash. Not supported.
491   W.write<uint32_t>(0);
492   // Typecheck section number. Not supported.
493   W.write<uint16_t>(0);
494   // Symbol type.
495   W.write<uint8_t>(getEncodedType(CSectionRef.MCCsect));
496   // Storage mapping class.
497   W.write<uint8_t>(CSectionRef.MCCsect->getMappingClass());
498   // Reserved (x_stab).
499   W.write<uint32_t>(0);
500   // Reserved (x_snstab).
501   W.write<uint16_t>(0);
502 }
503 
504 void XCOFFObjectWriter::writeFileHeader() {
505   // Magic.
506   W.write<uint16_t>(0x01df);
507   // Number of sections.
508   W.write<uint16_t>(SectionCount);
509   // Timestamp field. For reproducible output we write a 0, which represents no
510   // timestamp.
511   W.write<int32_t>(0);
512   // Byte Offset to the start of the symbol table.
513   W.write<uint32_t>(SymbolTableOffset);
514   // Number of entries in the symbol table.
515   W.write<int32_t>(SymbolTableEntryCount);
516   // Size of the optional header.
517   W.write<uint16_t>(0);
518   // Flags.
519   W.write<uint16_t>(0);
520 }
521 
522 void XCOFFObjectWriter::writeSectionHeaderTable() {
523   for (const auto *Sec : Sections) {
524     // Nothing to write for this Section.
525     if (Sec->Index == Section::UninitializedIndex)
526       continue;
527 
528     // Write Name.
529     ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize);
530     W.write(NameRef);
531 
532     // Write the Physical Address and Virtual Address. In an object file these
533     // are the same.
534     W.write<uint32_t>(Sec->Address);
535     W.write<uint32_t>(Sec->Address);
536 
537     W.write<uint32_t>(Sec->Size);
538     W.write<uint32_t>(Sec->FileOffsetToData);
539 
540     // Relocation pointer and Lineno pointer. Not supported yet.
541     W.write<uint32_t>(0);
542     W.write<uint32_t>(0);
543 
544     // Relocation and line-number counts. Not supported yet.
545     W.write<uint16_t>(0);
546     W.write<uint16_t>(0);
547 
548     W.write<int32_t>(Sec->Flags);
549   }
550 }
551 
552 void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) {
553   for (const auto &Csect : UndefinedCsects) {
554     writeSymbolTableEntryForControlSection(
555         Csect, XCOFF::ReservedSectionNum::N_UNDEF, Csect.MCCsect->getStorageClass());
556   }
557 
558   for (const auto *Section : Sections) {
559     // Nothing to write for this Section.
560     if (Section->Index == Section::UninitializedIndex)
561       continue;
562 
563     for (const auto *Group : Section->Groups) {
564       if (Group->empty())
565         continue;
566 
567       const int16_t SectionIndex = Section->Index;
568       for (const auto &Csect : *Group) {
569         // Write out the control section first and then each symbol in it.
570         writeSymbolTableEntryForControlSection(
571             Csect, SectionIndex, Csect.MCCsect->getStorageClass());
572 
573         for (const auto &Sym : Csect.Syms)
574           writeSymbolTableEntryForCsectMemberLabel(
575               Sym, Csect, SectionIndex, Layout.getSymbolOffset(*(Sym.MCSym)));
576       }
577     }
578   }
579 }
580 
581 void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
582   // The first symbol table entry is for the file name. We are not emitting it
583   // yet, so start at index 0.
584   uint32_t SymbolTableIndex = 0;
585 
586   // Calculate indices for undefined symbols.
587   for (auto &Csect : UndefinedCsects) {
588     Csect.Size = 0;
589     Csect.Address = 0;
590     Csect.SymbolTableIndex = SymbolTableIndex;
591     // 1 main and 1 auxiliary symbol table entry for each contained symbol.
592     SymbolTableIndex += 2;
593   }
594 
595   // The address corrresponds to the address of sections and symbols in the
596   // object file. We place the shared address 0 immediately after the
597   // section header table.
598   uint32_t Address = 0;
599   // Section indices are 1-based in XCOFF.
600   int32_t SectionIndex = 1;
601 
602   for (auto *Section : Sections) {
603     const bool IsEmpty =
604         llvm::all_of(Section->Groups,
605                      [](const CsectGroup *Group) { return Group->empty(); });
606     if (IsEmpty)
607       continue;
608 
609     if (SectionIndex > MaxSectionIndex)
610       report_fatal_error("Section index overflow!");
611     Section->Index = SectionIndex++;
612     SectionCount++;
613 
614     bool SectionAddressSet = false;
615     for (auto *Group : Section->Groups) {
616       if (Group->empty())
617         continue;
618 
619       for (auto &Csect : *Group) {
620         const MCSectionXCOFF *MCSec = Csect.MCCsect;
621         Csect.Address = alignTo(Address, MCSec->getAlignment());
622         Csect.Size = Layout.getSectionAddressSize(MCSec);
623         Address = Csect.Address + Csect.Size;
624         Csect.SymbolTableIndex = SymbolTableIndex;
625         // 1 main and 1 auxiliary symbol table entry for the csect.
626         SymbolTableIndex += 2;
627 
628         for (auto &Sym : Csect.Syms) {
629           Sym.SymbolTableIndex = SymbolTableIndex;
630           // 1 main and 1 auxiliary symbol table entry for each contained
631           // symbol.
632           SymbolTableIndex += 2;
633         }
634       }
635 
636       if (!SectionAddressSet) {
637         Section->Address = Group->front().Address;
638         SectionAddressSet = true;
639       }
640     }
641 
642     // Make sure the address of the next section aligned to
643     // DefaultSectionAlign.
644     Address = alignTo(Address, DefaultSectionAlign);
645     Section->Size = Address - Section->Address;
646   }
647 
648   SymbolTableEntryCount = SymbolTableIndex;
649 
650   // Calculate the RawPointer value for each section.
651   uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() +
652                         SectionCount * sizeof(XCOFF::SectionHeader32);
653   for (auto *Sec : Sections) {
654     if (Sec->Index == Section::UninitializedIndex || Sec->IsVirtual)
655       continue;
656 
657     Sec->FileOffsetToData = RawPointer;
658     RawPointer += Sec->Size;
659   }
660 
661   // TODO Add in Relocation storage to the RawPointer Calculation.
662   // TODO What to align the SymbolTable to?
663   // TODO Error check that the number of symbol table entries fits in 32-bits
664   // signed ...
665   if (SymbolTableEntryCount)
666     SymbolTableOffset = RawPointer;
667 }
668 
669 // Takes the log base 2 of the alignment and shifts the result into the 5 most
670 // significant bits of a byte, then or's in the csect type into the least
671 // significant 3 bits.
672 uint8_t getEncodedType(const MCSectionXCOFF *Sec) {
673   unsigned Align = Sec->getAlignment();
674   assert(isPowerOf2_32(Align) && "Alignment must be a power of 2.");
675   unsigned Log2Align = Log2_32(Align);
676   // Result is a number in the range [0, 31] which fits in the 5 least
677   // significant bits. Shift this value into the 5 most significant bits, and
678   // bitwise-or in the csect type.
679   uint8_t EncodedAlign = Log2Align << 3;
680   return EncodedAlign | Sec->getCSectType();
681 }
682 
683 } // end anonymous namespace
684 
685 std::unique_ptr<MCObjectWriter>
686 llvm::createXCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
687                               raw_pwrite_stream &OS) {
688   return std::make_unique<XCOFFObjectWriter>(std::move(MOTW), OS);
689 }
690