xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCXCOFFStreamer.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- lib/MC/MCXCOFFStreamer.cpp - XCOFF Object Output -------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file assembles .s files and emits XCOFF .o object files.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
135ffd83dbSDimitry Andric #include "llvm/MC/MCXCOFFStreamer.h"
148bcb0991SDimitry Andric #include "llvm/BinaryFormat/XCOFF.h"
150b57cec5SDimitry Andric #include "llvm/MC/MCAsmBackend.h"
1681ad6265SDimitry Andric #include "llvm/MC/MCAssembler.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCCodeEmitter.h"
185ffd83dbSDimitry Andric #include "llvm/MC/MCDirectives.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCObjectWriter.h"
205ffd83dbSDimitry Andric #include "llvm/MC/MCSectionXCOFF.h"
218bcb0991SDimitry Andric #include "llvm/MC/MCSymbolXCOFF.h"
22*0fca6ea1SDimitry Andric #include "llvm/MC/MCXCOFFObjectWriter.h"
23349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
2481ad6265SDimitry Andric #include "llvm/Support/Casting.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric 
MCXCOFFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> MAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter)280b57cec5SDimitry Andric MCXCOFFStreamer::MCXCOFFStreamer(MCContext &Context,
290b57cec5SDimitry Andric                                  std::unique_ptr<MCAsmBackend> MAB,
300b57cec5SDimitry Andric                                  std::unique_ptr<MCObjectWriter> OW,
310b57cec5SDimitry Andric                                  std::unique_ptr<MCCodeEmitter> Emitter)
320b57cec5SDimitry Andric     : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
330b57cec5SDimitry Andric                        std::move(Emitter)) {}
340b57cec5SDimitry Andric 
emitSymbolAttribute(MCSymbol * Sym,MCSymbolAttr Attribute)355ffd83dbSDimitry Andric bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym,
360b57cec5SDimitry Andric                                           MCSymbolAttr Attribute) {
378bcb0991SDimitry Andric   auto *Symbol = cast<MCSymbolXCOFF>(Sym);
388bcb0991SDimitry Andric   getAssembler().registerSymbol(*Symbol);
398bcb0991SDimitry Andric 
408bcb0991SDimitry Andric   switch (Attribute) {
41bdd1243dSDimitry Andric   // XCOFF doesn't support the cold feature.
42bdd1243dSDimitry Andric   case MCSA_Cold:
43bdd1243dSDimitry Andric     return false;
44bdd1243dSDimitry Andric 
458bcb0991SDimitry Andric   case MCSA_Global:
465ffd83dbSDimitry Andric   case MCSA_Extern:
478bcb0991SDimitry Andric     Symbol->setStorageClass(XCOFF::C_EXT);
488bcb0991SDimitry Andric     Symbol->setExternal(true);
498bcb0991SDimitry Andric     break;
505ffd83dbSDimitry Andric   case MCSA_LGlobal:
515ffd83dbSDimitry Andric     Symbol->setStorageClass(XCOFF::C_HIDEXT);
525ffd83dbSDimitry Andric     Symbol->setExternal(true);
535ffd83dbSDimitry Andric     break;
545ffd83dbSDimitry Andric   case llvm::MCSA_Weak:
555ffd83dbSDimitry Andric     Symbol->setStorageClass(XCOFF::C_WEAKEXT);
565ffd83dbSDimitry Andric     Symbol->setExternal(true);
575ffd83dbSDimitry Andric     break;
585ffd83dbSDimitry Andric   case llvm::MCSA_Hidden:
595ffd83dbSDimitry Andric     Symbol->setVisibilityType(XCOFF::SYM_V_HIDDEN);
605ffd83dbSDimitry Andric     break;
615ffd83dbSDimitry Andric   case llvm::MCSA_Protected:
625ffd83dbSDimitry Andric     Symbol->setVisibilityType(XCOFF::SYM_V_PROTECTED);
635ffd83dbSDimitry Andric     break;
6481ad6265SDimitry Andric   case llvm::MCSA_Exported:
6581ad6265SDimitry Andric     Symbol->setVisibilityType(XCOFF::SYM_V_EXPORTED);
6681ad6265SDimitry Andric     break;
678bcb0991SDimitry Andric   default:
688bcb0991SDimitry Andric     report_fatal_error("Not implemented yet.");
698bcb0991SDimitry Andric   }
708bcb0991SDimitry Andric   return true;
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
emitXCOFFSymbolLinkageWithVisibility(MCSymbol * Symbol,MCSymbolAttr Linkage,MCSymbolAttr Visibility)735ffd83dbSDimitry Andric void MCXCOFFStreamer::emitXCOFFSymbolLinkageWithVisibility(
745ffd83dbSDimitry Andric     MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) {
755ffd83dbSDimitry Andric 
765ffd83dbSDimitry Andric   emitSymbolAttribute(Symbol, Linkage);
775ffd83dbSDimitry Andric 
785ffd83dbSDimitry Andric   // When the caller passes `MCSA_Invalid` for the visibility, do not emit one.
795ffd83dbSDimitry Andric   if (Visibility == MCSA_Invalid)
805ffd83dbSDimitry Andric     return;
815ffd83dbSDimitry Andric 
825ffd83dbSDimitry Andric   emitSymbolAttribute(Symbol, Visibility);
835ffd83dbSDimitry Andric }
845ffd83dbSDimitry Andric 
emitXCOFFRefDirective(const MCSymbol * Symbol)8506c3fb27SDimitry Andric void MCXCOFFStreamer::emitXCOFFRefDirective(const MCSymbol *Symbol) {
8606c3fb27SDimitry Andric   // Add a Fixup here to later record a relocation of type R_REF to prevent the
8706c3fb27SDimitry Andric   // ref symbol from being garbage collected (by the binder).
8806c3fb27SDimitry Andric   MCDataFragment *DF = getOrCreateDataFragment();
8906c3fb27SDimitry Andric   const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
9006c3fb27SDimitry Andric   std::optional<MCFixupKind> MaybeKind =
9106c3fb27SDimitry Andric       getAssembler().getBackend().getFixupKind("R_REF");
9206c3fb27SDimitry Andric   if (!MaybeKind)
9306c3fb27SDimitry Andric     report_fatal_error("failed to get fixup kind for R_REF relocation");
9406c3fb27SDimitry Andric 
9506c3fb27SDimitry Andric   MCFixupKind Kind = *MaybeKind;
9606c3fb27SDimitry Andric   MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, Kind);
9706c3fb27SDimitry Andric   DF->getFixups().push_back(Fixup);
9806c3fb27SDimitry Andric }
9906c3fb27SDimitry Andric 
emitXCOFFRenameDirective(const MCSymbol * Name,StringRef Rename)1005f757f3fSDimitry Andric void MCXCOFFStreamer::emitXCOFFRenameDirective(const MCSymbol *Name,
1015f757f3fSDimitry Andric                                                StringRef Rename) {
1025f757f3fSDimitry Andric   const MCSymbolXCOFF *Symbol = cast<const MCSymbolXCOFF>(Name);
1035f757f3fSDimitry Andric   if (!Symbol->hasRename())
1045f757f3fSDimitry Andric     report_fatal_error("Only explicit .rename is supported for XCOFF.");
1055f757f3fSDimitry Andric }
1065f757f3fSDimitry Andric 
emitXCOFFExceptDirective(const MCSymbol * Symbol,const MCSymbol * Trap,unsigned Lang,unsigned Reason,unsigned FunctionSize,bool hasDebug)107bdd1243dSDimitry Andric void MCXCOFFStreamer::emitXCOFFExceptDirective(const MCSymbol *Symbol,
108bdd1243dSDimitry Andric                                                const MCSymbol *Trap,
109bdd1243dSDimitry Andric                                                unsigned Lang, unsigned Reason,
110bdd1243dSDimitry Andric                                                unsigned FunctionSize,
111bdd1243dSDimitry Andric                                                bool hasDebug) {
112*0fca6ea1SDimitry Andric   // TODO: Export XCOFFObjectWriter to llvm/MC/MCXCOFFObjectWriter.h and access
113*0fca6ea1SDimitry Andric   // it from MCXCOFFStreamer.
114*0fca6ea1SDimitry Andric   XCOFF::addExceptionEntry(getAssembler().getWriter(), Symbol, Trap, Lang,
115*0fca6ea1SDimitry Andric                            Reason, FunctionSize, hasDebug);
116bdd1243dSDimitry Andric }
117bdd1243dSDimitry Andric 
emitXCOFFCInfoSym(StringRef Name,StringRef Metadata)11806c3fb27SDimitry Andric void MCXCOFFStreamer::emitXCOFFCInfoSym(StringRef Name, StringRef Metadata) {
119*0fca6ea1SDimitry Andric   XCOFF::addCInfoSymEntry(getAssembler().getWriter(), Name, Metadata);
12006c3fb27SDimitry Andric }
12106c3fb27SDimitry Andric 
emitCommonSymbol(MCSymbol * Symbol,uint64_t Size,Align ByteAlignment)1225ffd83dbSDimitry Andric void MCXCOFFStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
123bdd1243dSDimitry Andric                                        Align ByteAlignment) {
1248bcb0991SDimitry Andric   getAssembler().registerSymbol(*Symbol);
1258bcb0991SDimitry Andric   Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() !=
1268bcb0991SDimitry Andric                       XCOFF::C_HIDEXT);
1278bcb0991SDimitry Andric   Symbol->setCommon(Size, ByteAlignment);
1288bcb0991SDimitry Andric 
1295ffd83dbSDimitry Andric   // Default csect align is 4, but common symbols have explicit alignment values
1305ffd83dbSDimitry Andric   // and we should honor it.
1315ffd83dbSDimitry Andric   cast<MCSymbolXCOFF>(Symbol)->getRepresentedCsect()->setAlignment(
132bdd1243dSDimitry Andric       ByteAlignment);
1335ffd83dbSDimitry Andric 
1348bcb0991SDimitry Andric   // Emit the alignment and storage for the variable to the section.
1355ffd83dbSDimitry Andric   emitValueToAlignment(ByteAlignment);
1365ffd83dbSDimitry Andric   emitZeros(Size);
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric 
emitZerofill(MCSection * Section,MCSymbol * Symbol,uint64_t Size,Align ByteAlignment,SMLoc Loc)1395ffd83dbSDimitry Andric void MCXCOFFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
140bdd1243dSDimitry Andric                                    uint64_t Size, Align ByteAlignment,
1410b57cec5SDimitry Andric                                    SMLoc Loc) {
1420b57cec5SDimitry Andric   report_fatal_error("Zero fill not implemented for XCOFF.");
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
emitInstToData(const MCInst & Inst,const MCSubtargetInfo & STI)1455ffd83dbSDimitry Andric void MCXCOFFStreamer::emitInstToData(const MCInst &Inst,
1468bcb0991SDimitry Andric                                      const MCSubtargetInfo &STI) {
1478bcb0991SDimitry Andric   MCAssembler &Assembler = getAssembler();
1488bcb0991SDimitry Andric   SmallVector<MCFixup, 4> Fixups;
1498bcb0991SDimitry Andric   SmallString<256> Code;
15006c3fb27SDimitry Andric   Assembler.getEmitter().encodeInstruction(Inst, Code, Fixups, STI);
1518bcb0991SDimitry Andric 
1525ffd83dbSDimitry Andric   // Add the fixups and data.
1538bcb0991SDimitry Andric   MCDataFragment *DF = getOrCreateDataFragment(&STI);
1545ffd83dbSDimitry Andric   const size_t ContentsSize = DF->getContents().size();
1555ffd83dbSDimitry Andric   auto &DataFragmentFixups = DF->getFixups();
1565ffd83dbSDimitry Andric   for (auto &Fixup : Fixups) {
1575ffd83dbSDimitry Andric     Fixup.setOffset(Fixup.getOffset() + ContentsSize);
1585ffd83dbSDimitry Andric     DataFragmentFixups.push_back(Fixup);
1595ffd83dbSDimitry Andric   }
1605ffd83dbSDimitry Andric 
1618bcb0991SDimitry Andric   DF->setHasInstructions(STI);
1628bcb0991SDimitry Andric   DF->getContents().append(Code.begin(), Code.end());
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
emitXCOFFLocalCommonSymbol(MCSymbol * LabelSym,uint64_t Size,MCSymbol * CsectSym,Align Alignment)1655ffd83dbSDimitry Andric void MCXCOFFStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym,
1668bcb0991SDimitry Andric                                                  uint64_t Size,
167480093f4SDimitry Andric                                                  MCSymbol *CsectSym,
168bdd1243dSDimitry Andric                                                  Align Alignment) {
169bdd1243dSDimitry Andric   emitCommonSymbol(CsectSym, Size, Alignment);
1708bcb0991SDimitry Andric }
171