1725 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements XCOFF object file writer information.10//11//===----------------------------------------------------------------------===//12 13#include "llvm/BinaryFormat/XCOFF.h"14#include "llvm/MC/MCAsmBackend.h"15#include "llvm/MC/MCAssembler.h"16#include "llvm/MC/MCFixup.h"17#include "llvm/MC/MCObjectWriter.h"18#include "llvm/MC/MCSectionXCOFF.h"19#include "llvm/MC/MCSymbolXCOFF.h"20#include "llvm/MC/MCValue.h"21#include "llvm/MC/MCXCOFFObjectWriter.h"22#include "llvm/MC/StringTableBuilder.h"23#include "llvm/Support/EndianStream.h"24#include "llvm/Support/ErrorHandling.h"25#include "llvm/Support/MathExtras.h"26 27#include <deque>28#include <map>29 30using namespace llvm;31 32// An XCOFF object file has a limited set of predefined sections. The most33// important ones for us (right now) are:34// .text --> contains program code and read-only data.35// .data --> contains initialized data, function descriptors, and the TOC.36// .bss --> contains uninitialized data.37// Each of these sections is composed of 'Control Sections'. A Control Section38// is more commonly referred to as a csect. A csect is an indivisible unit of39// code or data, and acts as a container for symbols. A csect is mapped40// into a section based on its storage-mapping class, with the exception of41// XMC_RW which gets mapped to either .data or .bss based on whether it's42// explicitly initialized or not.43//44// We don't represent the sections in the MC layer as there is nothing45// interesting about them at at that level: they carry information that is46// only relevant to the ObjectWriter, so we materialize them in this class.47namespace {48 49constexpr unsigned DefaultSectionAlign = 4;50constexpr int16_t MaxSectionIndex = INT16_MAX;51 52// Packs the csect's alignment and type into a byte.53uint8_t getEncodedType(const MCSectionXCOFF *);54 55struct XCOFFRelocation {56 uint32_t SymbolTableIndex;57 uint32_t FixupOffsetInCsect;58 uint8_t SignAndSize;59 uint8_t Type;60};61 62// Wrapper around an MCSymbolXCOFF.63struct Symbol {64 const MCSymbolXCOFF *const MCSym;65 uint32_t SymbolTableIndex;66 67 XCOFF::VisibilityType getVisibilityType() const {68 return MCSym->getVisibilityType();69 }70 71 XCOFF::StorageClass getStorageClass() const {72 return MCSym->getStorageClass();73 }74 StringRef getSymbolTableName() const { return MCSym->getSymbolTableName(); }75 Symbol(const MCSymbolXCOFF *MCSym) : MCSym(MCSym), SymbolTableIndex(-1) {}76};77 78// Wrapper for an MCSectionXCOFF.79// It can be a Csect or debug section or DWARF section and so on.80struct XCOFFSection {81 const MCSectionXCOFF *const MCSec;82 uint32_t SymbolTableIndex;83 uint64_t Address;84 uint64_t Size;85 86 SmallVector<Symbol, 1> Syms;87 SmallVector<XCOFFRelocation, 1> Relocations;88 StringRef getSymbolTableName() const { return MCSec->getSymbolTableName(); }89 XCOFF::VisibilityType getVisibilityType() const {90 return MCSec->getVisibilityType();91 }92 XCOFFSection(const MCSectionXCOFF *MCSec)93 : MCSec(MCSec), SymbolTableIndex(-1), Address(-1), Size(0) {}94};95 96// Type to be used for a container representing a set of csects with97// (approximately) the same storage mapping class. For example all the csects98// with a storage mapping class of `xmc_pr` will get placed into the same99// container.100using CsectGroup = std::deque<XCOFFSection>;101using CsectGroups = std::deque<CsectGroup *>;102 103// The basic section entry defination. This Section represents a section entry104// in XCOFF section header table.105struct SectionEntry {106 char Name[XCOFF::NameSize];107 // The physical/virtual address of the section. For an object file these108 // values are equivalent, except for in the overflow section header, where109 // the physical address specifies the number of relocation entries and the110 // virtual address specifies the number of line number entries.111 // TODO: Divide Address into PhysicalAddress and VirtualAddress when line112 // number entries are supported.113 uint64_t Address;114 uint64_t Size;115 uint64_t FileOffsetToData;116 uint64_t FileOffsetToRelocations;117 uint32_t RelocationCount;118 int32_t Flags;119 120 int16_t Index;121 122 virtual uint64_t advanceFileOffset(const uint64_t MaxRawDataSize,123 const uint64_t RawPointer) {124 FileOffsetToData = RawPointer;125 uint64_t NewPointer = RawPointer + Size;126 if (NewPointer > MaxRawDataSize)127 report_fatal_error("Section raw data overflowed this object file.");128 return NewPointer;129 }130 131 // XCOFF has special section numbers for symbols:132 // -2 Specifies N_DEBUG, a special symbolic debugging symbol.133 // -1 Specifies N_ABS, an absolute symbol. The symbol has a value but is not134 // relocatable.135 // 0 Specifies N_UNDEF, an undefined external symbol.136 // Therefore, we choose -3 (N_DEBUG - 1) to represent a section index that137 // hasn't been initialized.138 static constexpr int16_t UninitializedIndex =139 XCOFF::ReservedSectionNum::N_DEBUG - 1;140 141 SectionEntry(StringRef N, int32_t Flags)142 : Name(), Address(0), Size(0), FileOffsetToData(0),143 FileOffsetToRelocations(0), RelocationCount(0), Flags(Flags),144 Index(UninitializedIndex) {145 assert(N.size() <= XCOFF::NameSize && "section name too long");146 memcpy(Name, N.data(), N.size());147 }148 149 virtual void reset() {150 Address = 0;151 Size = 0;152 FileOffsetToData = 0;153 FileOffsetToRelocations = 0;154 RelocationCount = 0;155 Index = UninitializedIndex;156 }157 158 virtual ~SectionEntry() = default;159};160 161// Represents the data related to a section excluding the csects that make up162// the raw data of the section. The csects are stored separately as not all163// sections contain csects, and some sections contain csects which are better164// stored separately, e.g. the .data section containing read-write, descriptor,165// TOCBase and TOC-entry csects.166struct CsectSectionEntry : public SectionEntry {167 // Virtual sections do not need storage allocated in the object file.168 const bool IsVirtual;169 170 // This is a section containing csect groups.171 CsectGroups Groups;172 173 CsectSectionEntry(StringRef N, XCOFF::SectionTypeFlags Flags, bool IsVirtual,174 CsectGroups Groups)175 : SectionEntry(N, Flags), IsVirtual(IsVirtual), Groups(Groups) {176 assert(N.size() <= XCOFF::NameSize && "section name too long");177 memcpy(Name, N.data(), N.size());178 }179 180 void reset() override {181 SectionEntry::reset();182 // Clear any csects we have stored.183 for (auto *Group : Groups)184 Group->clear();185 }186 187 ~CsectSectionEntry() override = default;188};189 190struct DwarfSectionEntry : public SectionEntry {191 // For DWARF section entry.192 std::unique_ptr<XCOFFSection> DwarfSect;193 194 // For DWARF section, we must use real size in the section header. MemorySize195 // is for the size the DWARF section occupies including paddings.196 uint32_t MemorySize;197 198 // TODO: Remove this override. Loadable sections (e.g., .text, .data) may need199 // to be aligned. Other sections generally don't need any alignment, but if200 // they're aligned, the RawPointer should be adjusted before writing the201 // section. Then a dwarf-specific function wouldn't be needed.202 uint64_t advanceFileOffset(const uint64_t MaxRawDataSize,203 const uint64_t RawPointer) override {204 FileOffsetToData = RawPointer;205 uint64_t NewPointer = RawPointer + MemorySize;206 assert(NewPointer <= MaxRawDataSize &&207 "Section raw data overflowed this object file.");208 return NewPointer;209 }210 211 DwarfSectionEntry(StringRef N, int32_t Flags,212 std::unique_ptr<XCOFFSection> Sect)213 : SectionEntry(N, Flags | XCOFF::STYP_DWARF), DwarfSect(std::move(Sect)),214 MemorySize(0) {215 assert(DwarfSect->MCSec->isDwarfSect() &&216 "This should be a DWARF section!");217 assert(N.size() <= XCOFF::NameSize && "section name too long");218 memcpy(Name, N.data(), N.size());219 }220 221 DwarfSectionEntry(DwarfSectionEntry &&s) = default;222 223 ~DwarfSectionEntry() override = default;224};225 226struct ExceptionTableEntry {227 const MCSymbol *Trap;228 uint64_t TrapAddress = ~0ul;229 unsigned Lang;230 unsigned Reason;231 232 ExceptionTableEntry(const MCSymbol *Trap, unsigned Lang, unsigned Reason)233 : Trap(Trap), Lang(Lang), Reason(Reason) {}234};235 236struct ExceptionInfo {237 const MCSymbol *FunctionSymbol;238 unsigned FunctionSize;239 std::vector<ExceptionTableEntry> Entries;240};241 242struct ExceptionSectionEntry : public SectionEntry {243 std::map<const StringRef, ExceptionInfo> ExceptionTable;244 bool isDebugEnabled = false;245 246 ExceptionSectionEntry(StringRef N, int32_t Flags)247 : SectionEntry(N, Flags | XCOFF::STYP_EXCEPT) {248 assert(N.size() <= XCOFF::NameSize && "Section too long.");249 memcpy(Name, N.data(), N.size());250 }251 252 ~ExceptionSectionEntry() override = default;253};254 255struct CInfoSymInfo {256 // Name of the C_INFO symbol associated with the section257 std::string Name;258 std::string Metadata;259 // Offset into the start of the metadata in the section260 uint64_t Offset;261 262 CInfoSymInfo(std::string Name, std::string Metadata)263 : Name(Name), Metadata(Metadata) {}264 // Metadata needs to be padded out to an even word size.265 uint32_t paddingSize() const {266 return alignTo(Metadata.size(), sizeof(uint32_t)) - Metadata.size();267 };268 269 // Total size of the entry, including the 4 byte length270 uint32_t size() const {271 return Metadata.size() + paddingSize() + sizeof(uint32_t);272 };273};274 275struct CInfoSymSectionEntry : public SectionEntry {276 std::unique_ptr<CInfoSymInfo> Entry;277 278 CInfoSymSectionEntry(StringRef N, int32_t Flags) : SectionEntry(N, Flags) {}279 ~CInfoSymSectionEntry() override = default;280 void addEntry(std::unique_ptr<CInfoSymInfo> NewEntry) {281 Entry = std::move(NewEntry);282 Entry->Offset = sizeof(uint32_t);283 Size += Entry->size();284 }285 void reset() override {286 SectionEntry::reset();287 Entry.reset();288 }289};290 291class XCOFFWriter final : public XCOFFObjectWriter {292 uint32_t SymbolTableEntryCount = 0;293 uint64_t SymbolTableOffset = 0;294 uint16_t SectionCount = 0;295 uint32_t PaddingsBeforeDwarf = 0;296 bool HasVisibility = false;297 298 support::endian::Writer W;299 std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter;300 StringTableBuilder Strings;301 302 const uint64_t MaxRawDataSize =303 TargetObjectWriter->is64Bit() ? UINT64_MAX : UINT32_MAX;304 305 // Maps the MCSection representation to its corresponding XCOFFSection306 // wrapper. Needed for finding the XCOFFSection to insert an MCSymbol into307 // from its containing MCSectionXCOFF.308 DenseMap<const MCSectionXCOFF *, XCOFFSection *> SectionMap;309 310 // Maps the MCSymbol representation to its corrresponding symbol table index.311 // Needed for relocation.312 DenseMap<const MCSymbol *, uint32_t> SymbolIndexMap;313 314 // CsectGroups. These store the csects which make up different parts of315 // the sections. Should have one for each set of csects that get mapped into316 // the same section and get handled in a 'similar' way.317 CsectGroup UndefinedCsects;318 CsectGroup ProgramCodeCsects;319 CsectGroup ReadOnlyCsects;320 CsectGroup DataCsects;321 CsectGroup FuncDSCsects;322 CsectGroup TOCCsects;323 CsectGroup BSSCsects;324 CsectGroup TDataCsects;325 CsectGroup TBSSCsects;326 327 // The Predefined sections.328 CsectSectionEntry Text;329 CsectSectionEntry Data;330 CsectSectionEntry BSS;331 CsectSectionEntry TData;332 CsectSectionEntry TBSS;333 334 // All the XCOFF sections, in the order they will appear in the section header335 // table.336 std::array<CsectSectionEntry *const, 5> Sections{337 {&Text, &Data, &BSS, &TData, &TBSS}};338 339 std::vector<DwarfSectionEntry> DwarfSections;340 std::vector<SectionEntry> OverflowSections;341 342 ExceptionSectionEntry ExceptionSection;343 CInfoSymSectionEntry CInfoSymSection;344 345 CsectGroup &getCsectGroup(const MCSectionXCOFF *MCSec);346 347 void reset() override;348 349 void executePostLayoutBinding() override;350 351 void recordRelocation(const MCFragment &, const MCFixup &, MCValue,352 uint64_t &) override;353 354 uint64_t writeObject() override;355 356 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }357 bool nameShouldBeInStringTable(const StringRef &);358 void writeSymbolName(const StringRef &);359 bool auxFileSymNameShouldBeInStringTable(const StringRef &);360 void writeAuxFileSymName(const StringRef &);361 362 void writeSymbolEntryForCsectMemberLabel(const Symbol &SymbolRef,363 const XCOFFSection &CSectionRef,364 int16_t SectionIndex,365 uint64_t SymbolOffset);366 void writeSymbolEntryForControlSection(const XCOFFSection &CSectionRef,367 int16_t SectionIndex,368 XCOFF::StorageClass StorageClass);369 void writeSymbolEntryForDwarfSection(const XCOFFSection &DwarfSectionRef,370 int16_t SectionIndex);371 void writeFileHeader();372 void writeAuxFileHeader();373 void writeSectionHeader(const SectionEntry *Sec);374 void writeSectionHeaderTable();375 void writeSections(const MCAssembler &Asm);376 void writeSectionForControlSectionEntry(const MCAssembler &Asm,377 const CsectSectionEntry &CsectEntry,378 uint64_t &CurrentAddressLocation);379 void writeSectionForDwarfSectionEntry(const MCAssembler &Asm,380 const DwarfSectionEntry &DwarfEntry,381 uint64_t &CurrentAddressLocation);382 void383 writeSectionForExceptionSectionEntry(const MCAssembler &Asm,384 ExceptionSectionEntry &ExceptionEntry,385 uint64_t &CurrentAddressLocation);386 void writeSectionForCInfoSymSectionEntry(const MCAssembler &Asm,387 CInfoSymSectionEntry &CInfoSymEntry,388 uint64_t &CurrentAddressLocation);389 void writeSymbolTable(MCAssembler &Asm);390 void writeSymbolAuxFileEntry(StringRef &Name, uint8_t ftype);391 void writeSymbolAuxDwarfEntry(uint64_t LengthOfSectionPortion,392 uint64_t NumberOfRelocEnt = 0);393 void writeSymbolAuxCsectEntry(uint64_t SectionOrLength,394 uint8_t SymbolAlignmentAndType,395 uint8_t StorageMappingClass);396 void writeSymbolAuxFunctionEntry(uint32_t EntryOffset, uint32_t FunctionSize,397 uint64_t LineNumberPointer,398 uint32_t EndIndex);399 void writeSymbolAuxExceptionEntry(uint64_t EntryOffset, uint32_t FunctionSize,400 uint32_t EndIndex);401 void writeSymbolEntry(StringRef SymbolName, uint64_t Value,402 int16_t SectionNumber, uint16_t SymbolType,403 uint8_t StorageClass, uint8_t NumberOfAuxEntries = 1);404 void writeRelocations();405 void writeRelocation(XCOFFRelocation Reloc, const XCOFFSection &Section);406 407 // Called after all the csects and symbols have been processed by408 // `executePostLayoutBinding`, this function handles building up the majority409 // of the structures in the object file representation. Namely:410 // *) Calculates physical/virtual addresses, raw-pointer offsets, and section411 // sizes.412 // *) Assigns symbol table indices.413 // *) Builds up the section header table by adding any non-empty sections to414 // `Sections`.415 void assignAddressesAndIndices(MCAssembler &Asm);416 // Called after relocations are recorded.417 void finalizeSectionInfo();418 void finalizeRelocationInfo(SectionEntry *Sec, uint64_t RelCount);419 void calcOffsetToRelocations(SectionEntry *Sec, uint64_t &RawPointer);420 421 bool hasExceptionSection() {422 return !ExceptionSection.ExceptionTable.empty();423 }424 unsigned getExceptionSectionSize();425 unsigned getExceptionOffset(const MCSymbol *Symbol);426 427 size_t auxiliaryHeaderSize() const {428 // 64-bit object files have no auxiliary header.429 return HasVisibility && !is64Bit() ? XCOFF::AuxFileHeaderSizeShort : 0;430 }431 432public:433 XCOFFWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,434 raw_pwrite_stream &OS);435 436 void writeWord(uint64_t Word) {437 is64Bit() ? W.write<uint64_t>(Word) : W.write<uint32_t>(Word);438 }439 440 void addExceptionEntry(const MCSymbol *Symbol, const MCSymbol *Trap,441 unsigned LanguageCode, unsigned ReasonCode,442 unsigned FunctionSize, bool hasDebug) override;443 void addCInfoSymEntry(StringRef Name, StringRef Metadata) override;444};445 446XCOFFWriter::XCOFFWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,447 raw_pwrite_stream &OS)448 : W(OS, llvm::endianness::big), TargetObjectWriter(std::move(MOTW)),449 Strings(StringTableBuilder::XCOFF),450 Text(".text", XCOFF::STYP_TEXT, /* IsVirtual */ false,451 CsectGroups{&ProgramCodeCsects, &ReadOnlyCsects}),452 Data(".data", XCOFF::STYP_DATA, /* IsVirtual */ false,453 CsectGroups{&DataCsects, &FuncDSCsects, &TOCCsects}),454 BSS(".bss", XCOFF::STYP_BSS, /* IsVirtual */ true,455 CsectGroups{&BSSCsects}),456 TData(".tdata", XCOFF::STYP_TDATA, /* IsVirtual */ false,457 CsectGroups{&TDataCsects}),458 TBSS(".tbss", XCOFF::STYP_TBSS, /* IsVirtual */ true,459 CsectGroups{&TBSSCsects}),460 ExceptionSection(".except", XCOFF::STYP_EXCEPT),461 CInfoSymSection(".info", XCOFF::STYP_INFO) {}462 463void XCOFFWriter::reset() {464 // Clear the mappings we created.465 SymbolIndexMap.clear();466 SectionMap.clear();467 468 UndefinedCsects.clear();469 // Reset any sections we have written to, and empty the section header table.470 for (auto *Sec : Sections)471 Sec->reset();472 for (auto &DwarfSec : DwarfSections)473 DwarfSec.reset();474 for (auto &OverflowSec : OverflowSections)475 OverflowSec.reset();476 ExceptionSection.reset();477 CInfoSymSection.reset();478 479 // Reset states in XCOFFWriter.480 SymbolTableEntryCount = 0;481 SymbolTableOffset = 0;482 SectionCount = 0;483 PaddingsBeforeDwarf = 0;484 Strings.clear();485 486 MCObjectWriter::reset();487}488 489CsectGroup &XCOFFWriter::getCsectGroup(const MCSectionXCOFF *MCSec) {490 switch (MCSec->getMappingClass()) {491 case XCOFF::XMC_PR:492 assert(XCOFF::XTY_SD == MCSec->getCSectType() &&493 "Only an initialized csect can contain program code.");494 return ProgramCodeCsects;495 case XCOFF::XMC_RO:496 assert(XCOFF::XTY_SD == MCSec->getCSectType() &&497 "Only an initialized csect can contain read only data.");498 return ReadOnlyCsects;499 case XCOFF::XMC_RW:500 if (XCOFF::XTY_CM == MCSec->getCSectType())501 return BSSCsects;502 503 if (XCOFF::XTY_SD == MCSec->getCSectType())504 return DataCsects;505 506 report_fatal_error("Unhandled mapping of read-write csect to section.");507 case XCOFF::XMC_DS:508 return FuncDSCsects;509 case XCOFF::XMC_BS:510 assert(XCOFF::XTY_CM == MCSec->getCSectType() &&511 "Mapping invalid csect. CSECT with bss storage class must be "512 "common type.");513 return BSSCsects;514 case XCOFF::XMC_TL:515 assert(XCOFF::XTY_SD == MCSec->getCSectType() &&516 "Mapping invalid csect. CSECT with tdata storage class must be "517 "an initialized csect.");518 return TDataCsects;519 case XCOFF::XMC_UL:520 assert(XCOFF::XTY_CM == MCSec->getCSectType() &&521 "Mapping invalid csect. CSECT with tbss storage class must be "522 "an uninitialized csect.");523 return TBSSCsects;524 case XCOFF::XMC_TC0:525 assert(XCOFF::XTY_SD == MCSec->getCSectType() &&526 "Only an initialized csect can contain TOC-base.");527 assert(TOCCsects.empty() &&528 "We should have only one TOC-base, and it should be the first csect "529 "in this CsectGroup.");530 return TOCCsects;531 case XCOFF::XMC_TC:532 case XCOFF::XMC_TE:533 assert(XCOFF::XTY_SD == MCSec->getCSectType() &&534 "A TOC symbol must be an initialized csect.");535 assert(!TOCCsects.empty() &&536 "We should at least have a TOC-base in this CsectGroup.");537 return TOCCsects;538 case XCOFF::XMC_TD:539 assert((XCOFF::XTY_SD == MCSec->getCSectType() ||540 XCOFF::XTY_CM == MCSec->getCSectType()) &&541 "Symbol type incompatible with toc-data.");542 assert(!TOCCsects.empty() &&543 "We should at least have a TOC-base in this CsectGroup.");544 return TOCCsects;545 default:546 report_fatal_error("Unhandled mapping of csect to section.");547 }548}549 550static MCSectionXCOFF *getContainingCsect(const MCSymbolXCOFF *XSym) {551 if (XSym->isDefined())552 return static_cast<MCSectionXCOFF *>(XSym->getFragment()->getParent());553 return XSym->getRepresentedCsect();554}555 556void XCOFFWriter::executePostLayoutBinding() {557 for (const auto &S : *Asm) {558 auto *MCSec = static_cast<const MCSectionXCOFF *>(&S);559 assert(!SectionMap.contains(MCSec) && "Cannot add a section twice.");560 561 // If the name does not fit in the storage provided in the symbol table562 // entry, add it to the string table.563 if (nameShouldBeInStringTable(MCSec->getSymbolTableName()))564 Strings.add(MCSec->getSymbolTableName());565 if (MCSec->isCsect()) {566 // A new control section. Its CsectSectionEntry should already be staticly567 // generated as Text/Data/BSS/TDATA/TBSS. Add this section to the group of568 // the CsectSectionEntry.569 assert(XCOFF::XTY_ER != MCSec->getCSectType() &&570 "An undefined csect should not get registered.");571 CsectGroup &Group = getCsectGroup(MCSec);572 Group.emplace_back(MCSec);573 SectionMap[MCSec] = &Group.back();574 } else if (MCSec->isDwarfSect()) {575 // A new DwarfSectionEntry.576 std::unique_ptr<XCOFFSection> DwarfSec =577 std::make_unique<XCOFFSection>(MCSec);578 SectionMap[MCSec] = DwarfSec.get();579 580 DwarfSectionEntry SecEntry(MCSec->getName(),581 *MCSec->getDwarfSubtypeFlags(),582 std::move(DwarfSec));583 DwarfSections.push_back(std::move(SecEntry));584 } else585 llvm_unreachable("unsupport section type!");586 }587 588 for (const MCSymbol &S : Asm->symbols()) {589 // Nothing to do for temporary symbols.590 if (S.isTemporary())591 continue;592 593 auto *XSym = static_cast<const MCSymbolXCOFF *>(&S);594 const MCSectionXCOFF *ContainingCsect = getContainingCsect(XSym);595 596 if (ContainingCsect->isDwarfSect())597 continue;598 599 if (XSym->getVisibilityType() != XCOFF::SYM_V_UNSPECIFIED)600 HasVisibility = true;601 602 if (ContainingCsect->getCSectType() == XCOFF::XTY_ER) {603 // Handle undefined symbol.604 UndefinedCsects.emplace_back(ContainingCsect);605 SectionMap[ContainingCsect] = &UndefinedCsects.back();606 if (nameShouldBeInStringTable(ContainingCsect->getSymbolTableName()))607 Strings.add(ContainingCsect->getSymbolTableName());608 continue;609 }610 611 // If the symbol is the csect itself, we don't need to put the symbol612 // into csect's Syms.613 if (XSym == ContainingCsect->getQualNameSymbol())614 continue;615 616 // Only put a label into the symbol table when it is an external label.617 if (!XSym->isExternal())618 continue;619 620 assert(SectionMap.contains(ContainingCsect) &&621 "Expected containing csect to exist in map");622 XCOFFSection *Csect = SectionMap[ContainingCsect];623 // Lookup the containing csect and add the symbol to it.624 assert(Csect->MCSec->isCsect() && "only csect is supported now!");625 Csect->Syms.emplace_back(XSym);626 627 // If the name does not fit in the storage provided in the symbol table628 // entry, add it to the string table.629 if (nameShouldBeInStringTable(XSym->getSymbolTableName()))630 Strings.add(XSym->getSymbolTableName());631 }632 633 std::unique_ptr<CInfoSymInfo> &CISI = CInfoSymSection.Entry;634 if (CISI && nameShouldBeInStringTable(CISI->Name))635 Strings.add(CISI->Name);636 637 // Emit ".file" as the source file name when there is no file name.638 if (FileNames.empty())639 FileNames.emplace_back(".file", 0);640 for (const std::pair<std::string, size_t> &F : FileNames) {641 if (auxFileSymNameShouldBeInStringTable(F.first))642 Strings.add(F.first);643 }644 645 // Always add ".file" to the symbol table. The actual file name will be in646 // the AUX_FILE auxiliary entry.647 if (nameShouldBeInStringTable(".file"))648 Strings.add(".file");649 StringRef Vers = CompilerVersion;650 if (auxFileSymNameShouldBeInStringTable(Vers))651 Strings.add(Vers);652 653 Strings.finalize();654 assignAddressesAndIndices(*Asm);655}656 657void XCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup,658 MCValue Target, uint64_t &FixedValue) {659 auto getIndex = [this](const MCSymbol *Sym,660 const MCSectionXCOFF *ContainingCsect) {661 // If we could not find the symbol directly in SymbolIndexMap, this symbol662 // could either be a temporary symbol or an undefined symbol. In this case,663 // we would need to have the relocation reference its csect instead.664 auto It = SymbolIndexMap.find(Sym);665 return It != SymbolIndexMap.end()666 ? It->second667 : SymbolIndexMap[ContainingCsect->getQualNameSymbol()];668 };669 670 auto getVirtualAddress =671 [this](const MCSymbol *Sym,672 const MCSectionXCOFF *ContainingSect) -> uint64_t {673 // A DWARF section.674 if (ContainingSect->isDwarfSect())675 return Asm->getSymbolOffset(*Sym);676 677 // A csect.678 if (!Sym->isDefined())679 return SectionMap[ContainingSect]->Address;680 681 // A label.682 assert(Sym->isDefined() && "not a valid object that has address!");683 return SectionMap[ContainingSect]->Address + Asm->getSymbolOffset(*Sym);684 };685 686 const MCSymbol *const SymA = Target.getAddSym();687 uint8_t Type;688 uint8_t SignAndSize;689 std::tie(Type, SignAndSize) = TargetObjectWriter->getRelocTypeAndSignSize(690 Target, Fixup, Fixup.isPCRel());691 692 const MCSectionXCOFF *SymASec =693 getContainingCsect(static_cast<const MCSymbolXCOFF *>(SymA));694 assert(SectionMap.contains(SymASec) &&695 "Expected containing csect to exist in map.");696 697 assert((Fixup.getOffset() <= MaxRawDataSize - Asm->getFragmentOffset(F)) &&698 "Fragment offset + fixup offset is overflowed.");699 uint32_t FixupOffsetInCsect = Asm->getFragmentOffset(F) + Fixup.getOffset();700 701 const uint32_t Index = getIndex(SymA, SymASec);702 if (Type == XCOFF::RelocationType::R_POS ||703 Type == XCOFF::RelocationType::R_TLS ||704 Type == XCOFF::RelocationType::R_TLS_LE ||705 Type == XCOFF::RelocationType::R_TLS_IE ||706 Type == XCOFF::RelocationType::R_TLS_LD)707 // The FixedValue should be symbol's virtual address in this object file708 // plus any constant value that we might get.709 FixedValue = getVirtualAddress(SymA, SymASec) + Target.getConstant();710 else if (Type == XCOFF::RelocationType::R_TLSM)711 // The FixedValue should always be zero since the region handle is only712 // known at load time.713 FixedValue = 0;714 else if (Type == XCOFF::RelocationType::R_TOC ||715 Type == XCOFF::RelocationType::R_TOCL) {716 // For non toc-data external symbols, R_TOC type relocation will relocate to717 // data symbols that have XCOFF::XTY_SD type csect. For toc-data external718 // symbols, R_TOC type relocation will relocate to data symbols that have719 // XCOFF_ER type csect. For XCOFF_ER kind symbols, there will be no TOC720 // entry for them, so the FixedValue should always be 0.721 if (SymASec->getCSectType() == XCOFF::XTY_ER) {722 FixedValue = 0;723 } else {724 // The FixedValue should be the TOC entry offset from the TOC-base plus725 // any constant offset value.726 int64_t TOCEntryOffset = SectionMap[SymASec]->Address -727 TOCCsects.front().Address + Target.getConstant();728 // For small code model, if the TOCEntryOffset overflows the 16-bit value,729 // we truncate it back down to 16 bits. The linker will be able to insert730 // fix-up code when needed.731 // For non toc-data symbols, we already did the truncation in732 // PPCAsmPrinter.cpp through setting Target.getConstant() in the733 // expression above by calling getTOCEntryLoadingExprForXCOFF for the734 // various TOC PseudoOps.735 // For toc-data symbols, we were not able to calculate the offset from736 // the TOC in PPCAsmPrinter.cpp since the TOC has not been finalized at737 // that point, so we are adjusting it here though738 // llvm::SignExtend64<16>(TOCEntryOffset);739 // TODO: Since the time that the handling for offsets over 16-bits was740 // added in PPCAsmPrinter.cpp using getTOCEntryLoadingExprForXCOFF, the741 // system assembler and linker have been updated to be able to handle the742 // overflowing offsets, so we no longer need to keep743 // getTOCEntryLoadingExprForXCOFF.744 if (Type == XCOFF::RelocationType::R_TOC && !isInt<16>(TOCEntryOffset))745 TOCEntryOffset = llvm::SignExtend64<16>(TOCEntryOffset);746 747 FixedValue = TOCEntryOffset;748 }749 } else if (Type == XCOFF::RelocationType::R_RBR) {750 auto *ParentSec = static_cast<MCSectionXCOFF *>(F.getParent());751 assert((SymASec->getMappingClass() == XCOFF::XMC_PR &&752 ParentSec->getMappingClass() == XCOFF::XMC_PR) &&753 "Only XMC_PR csect may have the R_RBR relocation.");754 755 // The address of the branch instruction should be the sum of section756 // address, fragment offset and Fixup offset.757 uint64_t BRInstrAddress =758 SectionMap[ParentSec]->Address + FixupOffsetInCsect;759 // The FixedValue should be the difference between symbol's virtual address760 // and BR instr address plus any constant value.761 FixedValue = getVirtualAddress(SymA, SymASec) - BRInstrAddress +762 Target.getConstant();763 } else if (Type == XCOFF::RelocationType::R_REF) {764 // The FixedValue and FixupOffsetInCsect should always be 0 since it765 // specifies a nonrelocating reference.766 FixedValue = 0;767 FixupOffsetInCsect = 0;768 }769 770 XCOFFRelocation Reloc = {Index, FixupOffsetInCsect, SignAndSize, Type};771 auto *RelocationSec = static_cast<MCSectionXCOFF *>(F.getParent());772 assert(SectionMap.contains(RelocationSec) &&773 "Expected containing csect to exist in map.");774 SectionMap[RelocationSec]->Relocations.push_back(Reloc);775 776 auto SymB = static_cast<const MCSymbolXCOFF *>(Target.getSubSym());777 if (!SymB)778 return;779 if (SymA == SymB)780 report_fatal_error("relocation for opposite term is not yet supported");781 782 const MCSectionXCOFF *SymBSec = getContainingCsect(SymB);783 assert(SectionMap.contains(SymBSec) &&784 "Expected containing csect to exist in map.");785 if (SymASec == SymBSec)786 report_fatal_error(787 "relocation for paired relocatable term is not yet supported");788 789 assert(Type == XCOFF::RelocationType::R_POS &&790 "SymA must be R_POS here if it's not opposite term or paired "791 "relocatable term.");792 const uint32_t IndexB = getIndex(SymB, SymBSec);793 // SymB must be R_NEG here, given the general form of Target(MCValue) is794 // "SymbolA - SymbolB + imm64".795 const uint8_t TypeB = XCOFF::RelocationType::R_NEG;796 XCOFFRelocation RelocB = {IndexB, FixupOffsetInCsect, SignAndSize, TypeB};797 SectionMap[RelocationSec]->Relocations.push_back(RelocB);798 // We already folded "SymbolA + imm64" above when Type is R_POS for SymbolA,799 // now we just need to fold "- SymbolB" here.800 FixedValue -= getVirtualAddress(SymB, SymBSec);801}802 803void XCOFFWriter::writeSections(const MCAssembler &Asm) {804 uint64_t CurrentAddressLocation = 0;805 for (const auto *Section : Sections)806 writeSectionForControlSectionEntry(Asm, *Section, CurrentAddressLocation);807 for (const auto &DwarfSection : DwarfSections)808 writeSectionForDwarfSectionEntry(Asm, DwarfSection, CurrentAddressLocation);809 writeSectionForExceptionSectionEntry(Asm, ExceptionSection,810 CurrentAddressLocation);811 writeSectionForCInfoSymSectionEntry(Asm, CInfoSymSection,812 CurrentAddressLocation);813}814 815uint64_t XCOFFWriter::writeObject() {816 // We always emit a timestamp of 0 for reproducibility, so ensure incremental817 // linking is not enabled, in case, like with Windows COFF, such a timestamp818 // is incompatible with incremental linking of XCOFF.819 820 finalizeSectionInfo();821 uint64_t StartOffset = W.OS.tell();822 823 writeFileHeader();824 writeAuxFileHeader();825 writeSectionHeaderTable();826 writeSections(*Asm);827 writeRelocations();828 writeSymbolTable(*Asm);829 // Write the string table.830 Strings.write(W.OS);831 832 return W.OS.tell() - StartOffset;833}834 835bool XCOFFWriter::nameShouldBeInStringTable(const StringRef &SymbolName) {836 return SymbolName.size() > XCOFF::NameSize || is64Bit();837}838 839void XCOFFWriter::writeSymbolName(const StringRef &SymbolName) {840 // Magic, Offset or SymbolName.841 if (nameShouldBeInStringTable(SymbolName)) {842 W.write<int32_t>(0);843 W.write<uint32_t>(Strings.getOffset(SymbolName));844 } else {845 char Name[XCOFF::NameSize + 1];846 std::strncpy(Name, SymbolName.data(), XCOFF::NameSize);847 ArrayRef<char> NameRef(Name, XCOFF::NameSize);848 W.write(NameRef);849 }850}851 852void XCOFFWriter::writeSymbolEntry(StringRef SymbolName, uint64_t Value,853 int16_t SectionNumber, uint16_t SymbolType,854 uint8_t StorageClass,855 uint8_t NumberOfAuxEntries) {856 if (is64Bit()) {857 W.write<uint64_t>(Value);858 W.write<uint32_t>(Strings.getOffset(SymbolName));859 } else {860 writeSymbolName(SymbolName);861 W.write<uint32_t>(Value);862 }863 W.write<int16_t>(SectionNumber);864 W.write<uint16_t>(SymbolType);865 W.write<uint8_t>(StorageClass);866 W.write<uint8_t>(NumberOfAuxEntries);867}868 869void XCOFFWriter::writeSymbolAuxCsectEntry(uint64_t SectionOrLength,870 uint8_t SymbolAlignmentAndType,871 uint8_t StorageMappingClass) {872 W.write<uint32_t>(is64Bit() ? Lo_32(SectionOrLength) : SectionOrLength);873 W.write<uint32_t>(0); // ParameterHashIndex874 W.write<uint16_t>(0); // TypeChkSectNum875 W.write<uint8_t>(SymbolAlignmentAndType);876 W.write<uint8_t>(StorageMappingClass);877 if (is64Bit()) {878 W.write<uint32_t>(Hi_32(SectionOrLength));879 W.OS.write_zeros(1); // Reserved880 W.write<uint8_t>(XCOFF::AUX_CSECT);881 } else {882 W.write<uint32_t>(0); // StabInfoIndex883 W.write<uint16_t>(0); // StabSectNum884 }885}886 887bool XCOFFWriter::auxFileSymNameShouldBeInStringTable(888 const StringRef &SymbolName) {889 return SymbolName.size() > XCOFF::AuxFileEntNameSize;890}891 892void XCOFFWriter::writeAuxFileSymName(const StringRef &SymbolName) {893 // Magic, Offset or SymbolName.894 if (auxFileSymNameShouldBeInStringTable(SymbolName)) {895 W.write<int32_t>(0);896 W.write<uint32_t>(Strings.getOffset(SymbolName));897 W.OS.write_zeros(XCOFF::FileNamePadSize);898 } else {899 char Name[XCOFF::AuxFileEntNameSize + 1];900 std::strncpy(Name, SymbolName.data(), XCOFF::AuxFileEntNameSize);901 ArrayRef<char> NameRef(Name, XCOFF::AuxFileEntNameSize);902 W.write(NameRef);903 }904}905 906void XCOFFWriter::writeSymbolAuxFileEntry(StringRef &Name, uint8_t ftype) {907 writeAuxFileSymName(Name);908 W.write<uint8_t>(ftype);909 W.OS.write_zeros(2);910 if (is64Bit())911 W.write<uint8_t>(XCOFF::AUX_FILE);912 else913 W.OS.write_zeros(1);914}915 916void XCOFFWriter::writeSymbolAuxDwarfEntry(uint64_t LengthOfSectionPortion,917 uint64_t NumberOfRelocEnt) {918 writeWord(LengthOfSectionPortion);919 if (!is64Bit())920 W.OS.write_zeros(4); // Reserved921 writeWord(NumberOfRelocEnt);922 if (is64Bit()) {923 W.OS.write_zeros(1); // Reserved924 W.write<uint8_t>(XCOFF::AUX_SECT);925 } else {926 W.OS.write_zeros(6); // Reserved927 }928}929 930void XCOFFWriter::writeSymbolEntryForCsectMemberLabel(931 const Symbol &SymbolRef, const XCOFFSection &CSectionRef,932 int16_t SectionIndex, uint64_t SymbolOffset) {933 assert(SymbolOffset <= MaxRawDataSize - CSectionRef.Address &&934 "Symbol address overflowed.");935 936 auto Entry = ExceptionSection.ExceptionTable.find(SymbolRef.MCSym->getName());937 if (Entry != ExceptionSection.ExceptionTable.end()) {938 writeSymbolEntry(SymbolRef.getSymbolTableName(),939 CSectionRef.Address + SymbolOffset, SectionIndex,940 // In the old version of the 32-bit XCOFF interpretation,941 // symbols may require bit 10 (0x0020) to be set if the942 // symbol is a function, otherwise the bit should be 0.943 is64Bit() ? SymbolRef.getVisibilityType()944 : SymbolRef.getVisibilityType() | 0x0020,945 SymbolRef.getStorageClass(),946 (is64Bit() && ExceptionSection.isDebugEnabled) ? 3 : 2);947 if (is64Bit() && ExceptionSection.isDebugEnabled) {948 // On 64 bit with debugging enabled, we have a csect, exception, and949 // function auxilliary entries, so we must increment symbol index by 4.950 writeSymbolAuxExceptionEntry(951 ExceptionSection.FileOffsetToData +952 getExceptionOffset(Entry->second.FunctionSymbol),953 Entry->second.FunctionSize,954 SymbolIndexMap[Entry->second.FunctionSymbol] + 4);955 }956 // For exception section entries, csect and function auxilliary entries957 // must exist. On 64-bit there is also an exception auxilliary entry.958 writeSymbolAuxFunctionEntry(959 ExceptionSection.FileOffsetToData +960 getExceptionOffset(Entry->second.FunctionSymbol),961 Entry->second.FunctionSize, 0,962 (is64Bit() && ExceptionSection.isDebugEnabled)963 ? SymbolIndexMap[Entry->second.FunctionSymbol] + 4964 : SymbolIndexMap[Entry->second.FunctionSymbol] + 3);965 } else {966 writeSymbolEntry(SymbolRef.getSymbolTableName(),967 CSectionRef.Address + SymbolOffset, SectionIndex,968 SymbolRef.getVisibilityType(),969 SymbolRef.getStorageClass());970 }971 writeSymbolAuxCsectEntry(CSectionRef.SymbolTableIndex, XCOFF::XTY_LD,972 CSectionRef.MCSec->getMappingClass());973}974 975void XCOFFWriter::writeSymbolEntryForDwarfSection(976 const XCOFFSection &DwarfSectionRef, int16_t SectionIndex) {977 assert(DwarfSectionRef.MCSec->isDwarfSect() && "Not a DWARF section!");978 979 writeSymbolEntry(DwarfSectionRef.getSymbolTableName(), /*Value=*/0,980 SectionIndex, /*SymbolType=*/0, XCOFF::C_DWARF);981 982 writeSymbolAuxDwarfEntry(DwarfSectionRef.Size);983}984 985void XCOFFWriter::writeSymbolEntryForControlSection(986 const XCOFFSection &CSectionRef, int16_t SectionIndex,987 XCOFF::StorageClass StorageClass) {988 writeSymbolEntry(CSectionRef.getSymbolTableName(), CSectionRef.Address,989 SectionIndex, CSectionRef.getVisibilityType(), StorageClass);990 991 writeSymbolAuxCsectEntry(CSectionRef.Size, getEncodedType(CSectionRef.MCSec),992 CSectionRef.MCSec->getMappingClass());993}994 995void XCOFFWriter::writeSymbolAuxFunctionEntry(uint32_t EntryOffset,996 uint32_t FunctionSize,997 uint64_t LineNumberPointer,998 uint32_t EndIndex) {999 if (is64Bit())1000 writeWord(LineNumberPointer);1001 else1002 W.write<uint32_t>(EntryOffset);1003 W.write<uint32_t>(FunctionSize);1004 if (!is64Bit())1005 writeWord(LineNumberPointer);1006 W.write<uint32_t>(EndIndex);1007 if (is64Bit()) {1008 W.OS.write_zeros(1);1009 W.write<uint8_t>(XCOFF::AUX_FCN);1010 } else {1011 W.OS.write_zeros(2);1012 }1013}1014 1015void XCOFFWriter::writeSymbolAuxExceptionEntry(uint64_t EntryOffset,1016 uint32_t FunctionSize,1017 uint32_t EndIndex) {1018 assert(is64Bit() && "Exception auxilliary entries are 64-bit only.");1019 W.write<uint64_t>(EntryOffset);1020 W.write<uint32_t>(FunctionSize);1021 W.write<uint32_t>(EndIndex);1022 W.OS.write_zeros(1); // Pad (unused)1023 W.write<uint8_t>(XCOFF::AUX_EXCEPT);1024}1025 1026void XCOFFWriter::writeFileHeader() {1027 W.write<uint16_t>(is64Bit() ? XCOFF::XCOFF64 : XCOFF::XCOFF32);1028 W.write<uint16_t>(SectionCount);1029 W.write<int32_t>(0); // TimeStamp1030 writeWord(SymbolTableOffset);1031 if (is64Bit()) {1032 W.write<uint16_t>(auxiliaryHeaderSize());1033 W.write<uint16_t>(0); // Flags1034 W.write<int32_t>(SymbolTableEntryCount);1035 } else {1036 W.write<int32_t>(SymbolTableEntryCount);1037 W.write<uint16_t>(auxiliaryHeaderSize());1038 W.write<uint16_t>(0); // Flags1039 }1040}1041 1042void XCOFFWriter::writeAuxFileHeader() {1043 if (!auxiliaryHeaderSize())1044 return;1045 W.write<uint16_t>(0); // Magic1046 W.write<uint16_t>(1047 XCOFF::NEW_XCOFF_INTERPRET); // Version. The new interpretation of the1048 // n_type field in the symbol table entry is1049 // used in XCOFF32.1050 W.write<uint32_t>(Sections[0]->Size); // TextSize1051 W.write<uint32_t>(Sections[1]->Size); // InitDataSize1052 W.write<uint32_t>(Sections[2]->Size); // BssDataSize1053 W.write<uint32_t>(0); // EntryPointAddr1054 W.write<uint32_t>(Sections[0]->Address); // TextStartAddr1055 W.write<uint32_t>(Sections[1]->Address); // DataStartAddr1056}1057 1058void XCOFFWriter::writeSectionHeader(const SectionEntry *Sec) {1059 bool IsDwarf = (Sec->Flags & XCOFF::STYP_DWARF) != 0;1060 bool IsOvrflo = (Sec->Flags & XCOFF::STYP_OVRFLO) != 0;1061 // Nothing to write for this Section.1062 if (Sec->Index == SectionEntry::UninitializedIndex)1063 return;1064 1065 // Write Name.1066 ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize);1067 W.write(NameRef);1068 1069 // Write the Physical Address and Virtual Address.1070 // We use 0 for DWARF sections' Physical and Virtual Addresses.1071 writeWord(IsDwarf ? 0 : Sec->Address);1072 // Since line number is not supported, we set it to 0 for overflow sections.1073 writeWord((IsDwarf || IsOvrflo) ? 0 : Sec->Address);1074 1075 writeWord(Sec->Size);1076 writeWord(Sec->FileOffsetToData);1077 writeWord(Sec->FileOffsetToRelocations);1078 writeWord(0); // FileOffsetToLineNumberInfo. Not supported yet.1079 1080 if (is64Bit()) {1081 W.write<uint32_t>(Sec->RelocationCount);1082 W.write<uint32_t>(0); // NumberOfLineNumbers. Not supported yet.1083 W.write<int32_t>(Sec->Flags);1084 W.OS.write_zeros(4);1085 } else {1086 // For the overflow section header, s_nreloc provides a reference to the1087 // primary section header and s_nlnno must have the same value.1088 // For common section headers, if either of s_nreloc or s_nlnno are set to1089 // 65535, the other one must also be set to 65535.1090 W.write<uint16_t>(Sec->RelocationCount);1091 W.write<uint16_t>((IsOvrflo || Sec->RelocationCount == XCOFF::RelocOverflow)1092 ? Sec->RelocationCount1093 : 0); // NumberOfLineNumbers. Not supported yet.1094 W.write<int32_t>(Sec->Flags);1095 }1096}1097 1098void XCOFFWriter::writeSectionHeaderTable() {1099 for (const auto *CsectSec : Sections)1100 writeSectionHeader(CsectSec);1101 for (const auto &DwarfSec : DwarfSections)1102 writeSectionHeader(&DwarfSec);1103 for (const auto &OverflowSec : OverflowSections)1104 writeSectionHeader(&OverflowSec);1105 if (hasExceptionSection())1106 writeSectionHeader(&ExceptionSection);1107 if (CInfoSymSection.Entry)1108 writeSectionHeader(&CInfoSymSection);1109}1110 1111void XCOFFWriter::writeRelocation(XCOFFRelocation Reloc,1112 const XCOFFSection &Section) {1113 if (Section.MCSec->isCsect())1114 writeWord(Section.Address + Reloc.FixupOffsetInCsect);1115 else {1116 // DWARF sections' address is set to 0.1117 assert(Section.MCSec->isDwarfSect() && "unsupport section type!");1118 writeWord(Reloc.FixupOffsetInCsect);1119 }1120 W.write<uint32_t>(Reloc.SymbolTableIndex);1121 W.write<uint8_t>(Reloc.SignAndSize);1122 W.write<uint8_t>(Reloc.Type);1123}1124 1125void XCOFFWriter::writeRelocations() {1126 for (const auto *Section : Sections) {1127 if (Section->Index == SectionEntry::UninitializedIndex)1128 // Nothing to write for this Section.1129 continue;1130 1131 for (const auto *Group : Section->Groups) {1132 if (Group->empty())1133 continue;1134 1135 for (const auto &Csect : *Group) {1136 for (const auto Reloc : Csect.Relocations)1137 writeRelocation(Reloc, Csect);1138 }1139 }1140 }1141 1142 for (const auto &DwarfSection : DwarfSections)1143 for (const auto &Reloc : DwarfSection.DwarfSect->Relocations)1144 writeRelocation(Reloc, *DwarfSection.DwarfSect);1145}1146 1147void XCOFFWriter::writeSymbolTable(MCAssembler &Asm) {1148 // Write C_FILE symbols.1149 StringRef Vers = CompilerVersion;1150 1151 for (const std::pair<std::string, size_t> &F : FileNames) {1152 // The n_name of a C_FILE symbol is the source file's name when no auxiliary1153 // entries are present.1154 StringRef FileName = F.first;1155 1156 // For C_FILE symbols, the Source Language ID overlays the high-order byte1157 // of the SymbolType field, and the CPU Version ID is defined as the1158 // low-order byte.1159 // AIX's system assembler determines the source language ID based on the1160 // source file's name suffix, and the behavior here is consistent with it.1161 uint8_t LangID;1162 if (FileName.ends_with(".c"))1163 LangID = XCOFF::TB_C;1164 else if (FileName.ends_with_insensitive(".f") ||1165 FileName.ends_with_insensitive(".f77") ||1166 FileName.ends_with_insensitive(".f90") ||1167 FileName.ends_with_insensitive(".f95") ||1168 FileName.ends_with_insensitive(".f03") ||1169 FileName.ends_with_insensitive(".f08"))1170 LangID = XCOFF::TB_Fortran;1171 else1172 LangID = XCOFF::TB_CPLUSPLUS;1173 1174 uint8_t CpuID = XCOFF::getCpuID(getCPUType());1175 1176 int NumberOfFileAuxEntries = 1;1177 if (!Vers.empty())1178 ++NumberOfFileAuxEntries;1179 writeSymbolEntry(".file", /*Value=*/0, XCOFF::ReservedSectionNum::N_DEBUG,1180 /*SymbolType=*/(LangID << 8) | CpuID, XCOFF::C_FILE,1181 NumberOfFileAuxEntries);1182 writeSymbolAuxFileEntry(FileName, XCOFF::XFT_FN);1183 if (!Vers.empty())1184 writeSymbolAuxFileEntry(Vers, XCOFF::XFT_CV);1185 }1186 1187 if (CInfoSymSection.Entry)1188 writeSymbolEntry(CInfoSymSection.Entry->Name, CInfoSymSection.Entry->Offset,1189 CInfoSymSection.Index,1190 /*SymbolType=*/0, XCOFF::C_INFO,1191 /*NumberOfAuxEntries=*/0);1192 1193 for (const auto &Csect : UndefinedCsects) {1194 writeSymbolEntryForControlSection(Csect, XCOFF::ReservedSectionNum::N_UNDEF,1195 Csect.MCSec->getStorageClass());1196 }1197 1198 for (const auto *Section : Sections) {1199 if (Section->Index == SectionEntry::UninitializedIndex)1200 // Nothing to write for this Section.1201 continue;1202 1203 for (const auto *Group : Section->Groups) {1204 if (Group->empty())1205 continue;1206 1207 const int16_t SectionIndex = Section->Index;1208 for (const auto &Csect : *Group) {1209 // Write out the control section first and then each symbol in it.1210 writeSymbolEntryForControlSection(Csect, SectionIndex,1211 Csect.MCSec->getStorageClass());1212 1213 for (const auto &Sym : Csect.Syms)1214 writeSymbolEntryForCsectMemberLabel(1215 Sym, Csect, SectionIndex, Asm.getSymbolOffset(*(Sym.MCSym)));1216 }1217 }1218 }1219 1220 for (const auto &DwarfSection : DwarfSections)1221 writeSymbolEntryForDwarfSection(*DwarfSection.DwarfSect,1222 DwarfSection.Index);1223}1224 1225void XCOFFWriter::finalizeRelocationInfo(SectionEntry *Sec, uint64_t RelCount) {1226 // Handles relocation field overflows in an XCOFF32 file. An XCOFF64 file1227 // may not contain an overflow section header.1228 if (!is64Bit() && (RelCount >= static_cast<uint32_t>(XCOFF::RelocOverflow))) {1229 // Generate an overflow section header.1230 SectionEntry SecEntry(".ovrflo", XCOFF::STYP_OVRFLO);1231 1232 // This field specifies the file section number of the section header that1233 // overflowed.1234 SecEntry.RelocationCount = Sec->Index;1235 1236 // This field specifies the number of relocation entries actually1237 // required.1238 SecEntry.Address = RelCount;1239 SecEntry.Index = ++SectionCount;1240 OverflowSections.push_back(std::move(SecEntry));1241 1242 // The field in the primary section header is always 655351243 // (XCOFF::RelocOverflow).1244 Sec->RelocationCount = XCOFF::RelocOverflow;1245 } else {1246 Sec->RelocationCount = RelCount;1247 }1248}1249 1250void XCOFFWriter::calcOffsetToRelocations(SectionEntry *Sec,1251 uint64_t &RawPointer) {1252 if (!Sec->RelocationCount)1253 return;1254 1255 Sec->FileOffsetToRelocations = RawPointer;1256 uint64_t RelocationSizeInSec = 0;1257 if (!is64Bit() &&1258 Sec->RelocationCount == static_cast<uint32_t>(XCOFF::RelocOverflow)) {1259 // Find its corresponding overflow section.1260 for (auto &OverflowSec : OverflowSections) {1261 if (OverflowSec.RelocationCount == static_cast<uint32_t>(Sec->Index)) {1262 RelocationSizeInSec =1263 OverflowSec.Address * XCOFF::RelocationSerializationSize32;1264 1265 // This field must have the same values as in the corresponding1266 // primary section header.1267 OverflowSec.FileOffsetToRelocations = Sec->FileOffsetToRelocations;1268 }1269 }1270 assert(RelocationSizeInSec && "Overflow section header doesn't exist.");1271 } else {1272 RelocationSizeInSec = Sec->RelocationCount *1273 (is64Bit() ? XCOFF::RelocationSerializationSize641274 : XCOFF::RelocationSerializationSize32);1275 }1276 1277 RawPointer += RelocationSizeInSec;1278 if (RawPointer > MaxRawDataSize)1279 report_fatal_error("Relocation data overflowed this object file.");1280}1281 1282void XCOFFWriter::finalizeSectionInfo() {1283 for (auto *Section : Sections) {1284 if (Section->Index == SectionEntry::UninitializedIndex)1285 // Nothing to record for this Section.1286 continue;1287 1288 uint64_t RelCount = 0;1289 for (const auto *Group : Section->Groups) {1290 if (Group->empty())1291 continue;1292 1293 for (auto &Csect : *Group)1294 RelCount += Csect.Relocations.size();1295 }1296 finalizeRelocationInfo(Section, RelCount);1297 }1298 1299 for (auto &DwarfSection : DwarfSections)1300 finalizeRelocationInfo(&DwarfSection,1301 DwarfSection.DwarfSect->Relocations.size());1302 1303 // Calculate the RawPointer value for all headers.1304 uint64_t RawPointer =1305 (is64Bit() ? (XCOFF::FileHeaderSize64 +1306 SectionCount * XCOFF::SectionHeaderSize64)1307 : (XCOFF::FileHeaderSize32 +1308 SectionCount * XCOFF::SectionHeaderSize32)) +1309 auxiliaryHeaderSize();1310 1311 // Calculate the file offset to the section data.1312 for (auto *Sec : Sections) {1313 if (Sec->Index == SectionEntry::UninitializedIndex || Sec->IsVirtual)1314 continue;1315 1316 RawPointer = Sec->advanceFileOffset(MaxRawDataSize, RawPointer);1317 }1318 1319 if (!DwarfSections.empty()) {1320 RawPointer += PaddingsBeforeDwarf;1321 for (auto &DwarfSection : DwarfSections) {1322 RawPointer = DwarfSection.advanceFileOffset(MaxRawDataSize, RawPointer);1323 }1324 }1325 1326 if (hasExceptionSection())1327 RawPointer = ExceptionSection.advanceFileOffset(MaxRawDataSize, RawPointer);1328 1329 if (CInfoSymSection.Entry)1330 RawPointer = CInfoSymSection.advanceFileOffset(MaxRawDataSize, RawPointer);1331 1332 for (auto *Sec : Sections) {1333 if (Sec->Index != SectionEntry::UninitializedIndex)1334 calcOffsetToRelocations(Sec, RawPointer);1335 }1336 1337 for (auto &DwarfSec : DwarfSections)1338 calcOffsetToRelocations(&DwarfSec, RawPointer);1339 1340 // TODO Error check that the number of symbol table entries fits in 32-bits1341 // signed ...1342 if (SymbolTableEntryCount)1343 SymbolTableOffset = RawPointer;1344}1345 1346void XCOFFWriter::addExceptionEntry(const MCSymbol *Symbol,1347 const MCSymbol *Trap, unsigned LanguageCode,1348 unsigned ReasonCode, unsigned FunctionSize,1349 bool hasDebug) {1350 // If a module had debug info, debugging is enabled and XCOFF emits the1351 // exception auxilliary entry.1352 if (hasDebug)1353 ExceptionSection.isDebugEnabled = true;1354 auto Entry = ExceptionSection.ExceptionTable.find(Symbol->getName());1355 if (Entry != ExceptionSection.ExceptionTable.end()) {1356 Entry->second.Entries.push_back(1357 ExceptionTableEntry(Trap, LanguageCode, ReasonCode));1358 return;1359 }1360 ExceptionInfo NewEntry;1361 NewEntry.FunctionSymbol = Symbol;1362 NewEntry.FunctionSize = FunctionSize;1363 NewEntry.Entries.push_back(1364 ExceptionTableEntry(Trap, LanguageCode, ReasonCode));1365 ExceptionSection.ExceptionTable.insert(1366 std::pair<const StringRef, ExceptionInfo>(Symbol->getName(), NewEntry));1367}1368 1369unsigned XCOFFWriter::getExceptionSectionSize() {1370 unsigned EntryNum = 0;1371 1372 for (const auto &TableEntry : ExceptionSection.ExceptionTable)1373 // The size() gets +1 to account for the initial entry containing the1374 // symbol table index.1375 EntryNum += TableEntry.second.Entries.size() + 1;1376 1377 return EntryNum * (is64Bit() ? XCOFF::ExceptionSectionEntrySize641378 : XCOFF::ExceptionSectionEntrySize32);1379}1380 1381unsigned XCOFFWriter::getExceptionOffset(const MCSymbol *Symbol) {1382 unsigned EntryNum = 0;1383 for (const auto &TableEntry : ExceptionSection.ExceptionTable) {1384 if (Symbol == TableEntry.second.FunctionSymbol)1385 break;1386 EntryNum += TableEntry.second.Entries.size() + 1;1387 }1388 return EntryNum * (is64Bit() ? XCOFF::ExceptionSectionEntrySize641389 : XCOFF::ExceptionSectionEntrySize32);1390}1391 1392void XCOFFWriter::addCInfoSymEntry(StringRef Name, StringRef Metadata) {1393 assert(!CInfoSymSection.Entry && "Multiple entries are not supported");1394 CInfoSymSection.addEntry(1395 std::make_unique<CInfoSymInfo>(Name.str(), Metadata.str()));1396}1397 1398void XCOFFWriter::assignAddressesAndIndices(MCAssembler &Asm) {1399 // The symbol table starts with all the C_FILE symbols. Each C_FILE symbol1400 // requires 1 or 2 auxiliary entries.1401 uint32_t SymbolTableIndex =1402 (2 + (CompilerVersion.empty() ? 0 : 1)) * FileNames.size();1403 1404 if (CInfoSymSection.Entry)1405 SymbolTableIndex++;1406 1407 // Calculate indices for undefined symbols.1408 for (auto &Csect : UndefinedCsects) {1409 Csect.Size = 0;1410 Csect.Address = 0;1411 Csect.SymbolTableIndex = SymbolTableIndex;1412 SymbolIndexMap[Csect.MCSec->getQualNameSymbol()] = Csect.SymbolTableIndex;1413 // 1 main and 1 auxiliary symbol table entry for each contained symbol.1414 SymbolTableIndex += 2;1415 }1416 1417 // The address corrresponds to the address of sections and symbols in the1418 // object file. We place the shared address 0 immediately after the1419 // section header table.1420 uint64_t Address = 0;1421 // Section indices are 1-based in XCOFF.1422 int32_t SectionIndex = 1;1423 bool HasTDataSection = false;1424 1425 for (auto *Section : Sections) {1426 const bool IsEmpty =1427 llvm::all_of(Section->Groups,1428 [](const CsectGroup *Group) { return Group->empty(); });1429 if (IsEmpty)1430 continue;1431 1432 if (SectionIndex > MaxSectionIndex)1433 report_fatal_error("Section index overflow!");1434 Section->Index = SectionIndex++;1435 SectionCount++;1436 1437 bool SectionAddressSet = false;1438 // Reset the starting address to 0 for TData section.1439 if (Section->Flags == XCOFF::STYP_TDATA) {1440 Address = 0;1441 HasTDataSection = true;1442 }1443 // Reset the starting address to 0 for TBSS section if the object file does1444 // not contain TData Section.1445 if ((Section->Flags == XCOFF::STYP_TBSS) && !HasTDataSection)1446 Address = 0;1447 1448 for (auto *Group : Section->Groups) {1449 if (Group->empty())1450 continue;1451 1452 for (auto &Csect : *Group) {1453 const MCSectionXCOFF *MCSec = Csect.MCSec;1454 Csect.Address = alignTo(Address, MCSec->getAlign());1455 Csect.Size = Asm.getSectionAddressSize(*MCSec);1456 Address = Csect.Address + Csect.Size;1457 Csect.SymbolTableIndex = SymbolTableIndex;1458 SymbolIndexMap[MCSec->getQualNameSymbol()] = Csect.SymbolTableIndex;1459 // 1 main and 1 auxiliary symbol table entry for the csect.1460 SymbolTableIndex += 2;1461 1462 for (auto &Sym : Csect.Syms) {1463 bool hasExceptEntry = false;1464 auto Entry =1465 ExceptionSection.ExceptionTable.find(Sym.MCSym->getName());1466 if (Entry != ExceptionSection.ExceptionTable.end()) {1467 hasExceptEntry = true;1468 for (auto &TrapEntry : Entry->second.Entries) {1469 TrapEntry.TrapAddress = Asm.getSymbolOffset(*(Sym.MCSym)) +1470 TrapEntry.Trap->getOffset();1471 }1472 }1473 Sym.SymbolTableIndex = SymbolTableIndex;1474 SymbolIndexMap[Sym.MCSym] = Sym.SymbolTableIndex;1475 // 1 main and 1 auxiliary symbol table entry for each contained1476 // symbol. For symbols with exception section entries, a function1477 // auxilliary entry is needed, and on 64-bit XCOFF with debugging1478 // enabled, an additional exception auxilliary entry is needed.1479 SymbolTableIndex += 2;1480 if (hasExceptionSection() && hasExceptEntry) {1481 if (is64Bit() && ExceptionSection.isDebugEnabled)1482 SymbolTableIndex += 2;1483 else1484 SymbolTableIndex += 1;1485 }1486 }1487 }1488 1489 if (!SectionAddressSet) {1490 Section->Address = Group->front().Address;1491 SectionAddressSet = true;1492 }1493 }1494 1495 // Make sure the address of the next section aligned to1496 // DefaultSectionAlign.1497 Address = alignTo(Address, DefaultSectionAlign);1498 Section->Size = Address - Section->Address;1499 }1500 1501 // Start to generate DWARF sections. Sections other than DWARF section use1502 // DefaultSectionAlign as the default alignment, while DWARF sections have1503 // their own alignments. If these two alignments are not the same, we need1504 // some paddings here and record the paddings bytes for FileOffsetToData1505 // calculation.1506 if (!DwarfSections.empty())1507 PaddingsBeforeDwarf =1508 alignTo(Address,1509 (*DwarfSections.begin()).DwarfSect->MCSec->getAlign()) -1510 Address;1511 1512 DwarfSectionEntry *LastDwarfSection = nullptr;1513 for (auto &DwarfSection : DwarfSections) {1514 assert((SectionIndex <= MaxSectionIndex) && "Section index overflow!");1515 1516 XCOFFSection &DwarfSect = *DwarfSection.DwarfSect;1517 const MCSectionXCOFF *MCSec = DwarfSect.MCSec;1518 1519 // Section index.1520 DwarfSection.Index = SectionIndex++;1521 SectionCount++;1522 1523 // Symbol index.1524 DwarfSect.SymbolTableIndex = SymbolTableIndex;1525 SymbolIndexMap[MCSec->getQualNameSymbol()] = DwarfSect.SymbolTableIndex;1526 // 1 main and 1 auxiliary symbol table entry for the csect.1527 SymbolTableIndex += 2;1528 1529 // Section address. Make it align to section alignment.1530 // We use address 0 for DWARF sections' Physical and Virtual Addresses.1531 // This address is used to tell where is the section in the final object.1532 // See writeSectionForDwarfSectionEntry().1533 DwarfSection.Address = DwarfSect.Address =1534 alignTo(Address, MCSec->getAlign());1535 1536 // Section size.1537 // For DWARF section, we must use the real size which may be not aligned.1538 DwarfSection.Size = DwarfSect.Size = Asm.getSectionAddressSize(*MCSec);1539 1540 Address = DwarfSection.Address + DwarfSection.Size;1541 1542 if (LastDwarfSection)1543 LastDwarfSection->MemorySize =1544 DwarfSection.Address - LastDwarfSection->Address;1545 LastDwarfSection = &DwarfSection;1546 }1547 if (LastDwarfSection) {1548 // Make the final DWARF section address align to the default section1549 // alignment for follow contents.1550 Address = alignTo(LastDwarfSection->Address + LastDwarfSection->Size,1551 DefaultSectionAlign);1552 LastDwarfSection->MemorySize = Address - LastDwarfSection->Address;1553 }1554 if (hasExceptionSection()) {1555 ExceptionSection.Index = SectionIndex++;1556 SectionCount++;1557 ExceptionSection.Address = 0;1558 ExceptionSection.Size = getExceptionSectionSize();1559 Address += ExceptionSection.Size;1560 Address = alignTo(Address, DefaultSectionAlign);1561 }1562 1563 if (CInfoSymSection.Entry) {1564 CInfoSymSection.Index = SectionIndex++;1565 SectionCount++;1566 CInfoSymSection.Address = 0;1567 Address += CInfoSymSection.Size;1568 Address = alignTo(Address, DefaultSectionAlign);1569 }1570 1571 SymbolTableEntryCount = SymbolTableIndex;1572}1573 1574void XCOFFWriter::writeSectionForControlSectionEntry(1575 const MCAssembler &Asm, const CsectSectionEntry &CsectEntry,1576 uint64_t &CurrentAddressLocation) {1577 // Nothing to write for this Section.1578 if (CsectEntry.Index == SectionEntry::UninitializedIndex)1579 return;1580 1581 // There could be a gap (without corresponding zero padding) between1582 // sections.1583 // There could be a gap (without corresponding zero padding) between1584 // sections.1585 assert(((CurrentAddressLocation <= CsectEntry.Address) ||1586 (CsectEntry.Flags == XCOFF::STYP_TDATA) ||1587 (CsectEntry.Flags == XCOFF::STYP_TBSS)) &&1588 "CurrentAddressLocation should be less than or equal to section "1589 "address if the section is not TData or TBSS.");1590 1591 CurrentAddressLocation = CsectEntry.Address;1592 1593 // For virtual sections, nothing to write. But need to increase1594 // CurrentAddressLocation for later sections like DWARF section has a correct1595 // writing location.1596 if (CsectEntry.IsVirtual) {1597 CurrentAddressLocation += CsectEntry.Size;1598 return;1599 }1600 1601 for (const auto &Group : CsectEntry.Groups) {1602 for (const auto &Csect : *Group) {1603 if (uint32_t PaddingSize = Csect.Address - CurrentAddressLocation)1604 W.OS.write_zeros(PaddingSize);1605 if (Csect.Size)1606 Asm.writeSectionData(W.OS, Csect.MCSec);1607 CurrentAddressLocation = Csect.Address + Csect.Size;1608 }1609 }1610 1611 // The size of the tail padding in a section is the end virtual address of1612 // the current section minus the end virtual address of the last csect1613 // in that section.1614 if (uint64_t PaddingSize =1615 CsectEntry.Address + CsectEntry.Size - CurrentAddressLocation) {1616 W.OS.write_zeros(PaddingSize);1617 CurrentAddressLocation += PaddingSize;1618 }1619}1620 1621void XCOFFWriter::writeSectionForDwarfSectionEntry(1622 const MCAssembler &Asm, const DwarfSectionEntry &DwarfEntry,1623 uint64_t &CurrentAddressLocation) {1624 // There could be a gap (without corresponding zero padding) between1625 // sections. For example DWARF section alignment is bigger than1626 // DefaultSectionAlign.1627 assert(CurrentAddressLocation <= DwarfEntry.Address &&1628 "CurrentAddressLocation should be less than or equal to section "1629 "address.");1630 1631 if (uint64_t PaddingSize = DwarfEntry.Address - CurrentAddressLocation)1632 W.OS.write_zeros(PaddingSize);1633 1634 if (DwarfEntry.Size)1635 Asm.writeSectionData(W.OS, DwarfEntry.DwarfSect->MCSec);1636 1637 CurrentAddressLocation = DwarfEntry.Address + DwarfEntry.Size;1638 1639 // DWARF section size is not aligned to DefaultSectionAlign.1640 // Make sure CurrentAddressLocation is aligned to DefaultSectionAlign.1641 uint32_t Mod = CurrentAddressLocation % DefaultSectionAlign;1642 uint32_t TailPaddingSize = Mod ? DefaultSectionAlign - Mod : 0;1643 if (TailPaddingSize)1644 W.OS.write_zeros(TailPaddingSize);1645 1646 CurrentAddressLocation += TailPaddingSize;1647}1648 1649void XCOFFWriter::writeSectionForExceptionSectionEntry(1650 const MCAssembler &Asm, ExceptionSectionEntry &ExceptionEntry,1651 uint64_t &CurrentAddressLocation) {1652 for (const auto &TableEntry : ExceptionEntry.ExceptionTable) {1653 // For every symbol that has exception entries, you must start the entries1654 // with an initial symbol table index entry1655 W.write<uint32_t>(SymbolIndexMap[TableEntry.second.FunctionSymbol]);1656 if (is64Bit()) {1657 // 4-byte padding on 64-bit.1658 W.OS.write_zeros(4);1659 }1660 W.OS.write_zeros(2);1661 for (auto &TrapEntry : TableEntry.second.Entries) {1662 writeWord(TrapEntry.TrapAddress);1663 W.write<uint8_t>(TrapEntry.Lang);1664 W.write<uint8_t>(TrapEntry.Reason);1665 }1666 }1667 1668 CurrentAddressLocation += getExceptionSectionSize();1669}1670 1671void XCOFFWriter::writeSectionForCInfoSymSectionEntry(1672 const MCAssembler &Asm, CInfoSymSectionEntry &CInfoSymEntry,1673 uint64_t &CurrentAddressLocation) {1674 if (!CInfoSymSection.Entry)1675 return;1676 1677 constexpr int WordSize = sizeof(uint32_t);1678 std::unique_ptr<CInfoSymInfo> &CISI = CInfoSymEntry.Entry;1679 const std::string &Metadata = CISI->Metadata;1680 1681 // Emit the 4-byte length of the metadata.1682 W.write<uint32_t>(Metadata.size());1683 1684 if (Metadata.size() == 0)1685 return;1686 1687 // Write out the payload one word at a time.1688 size_t Index = 0;1689 while (Index + WordSize <= Metadata.size()) {1690 uint32_t NextWord =1691 llvm::support::endian::read32be(Metadata.data() + Index);1692 W.write<uint32_t>(NextWord);1693 Index += WordSize;1694 }1695 1696 // If there is padding, we have at least one byte of payload left to emit.1697 if (CISI->paddingSize()) {1698 std::array<uint8_t, WordSize> LastWord = {0};1699 ::memcpy(LastWord.data(), Metadata.data() + Index, Metadata.size() - Index);1700 W.write<uint32_t>(llvm::support::endian::read32be(LastWord.data()));1701 }1702 1703 CurrentAddressLocation += CISI->size();1704}1705 1706// Takes the log base 2 of the alignment and shifts the result into the 5 most1707// significant bits of a byte, then or's in the csect type into the least1708// significant 3 bits.1709uint8_t getEncodedType(const MCSectionXCOFF *Sec) {1710 unsigned Log2Align = Log2(Sec->getAlign());1711 // Result is a number in the range [0, 31] which fits in the 5 least1712 // significant bits. Shift this value into the 5 most significant bits, and1713 // bitwise-or in the csect type.1714 uint8_t EncodedAlign = Log2Align << 3;1715 return EncodedAlign | Sec->getCSectType();1716}1717 1718} // end anonymous namespace1719 1720std::unique_ptr<MCObjectWriter>1721llvm::createXCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,1722 raw_pwrite_stream &OS) {1723 return std::make_unique<XCOFFWriter>(std::move(MOTW), OS);1724}1725