xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCXCOFFStreamer.cpp (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 //===- lib/MC/MCXCOFFStreamer.cpp - XCOFF Object Output -------------------===//
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 assembles .s files and emits XCOFF .o object files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/MC/MCXCOFFStreamer.h"
14 #include "llvm/BinaryFormat/XCOFF.h"
15 #include "llvm/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCDirectives.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSectionXCOFF.h"
21 #include "llvm/MC/MCSymbolXCOFF.h"
22 #include "llvm/MC/TargetRegistry.h"
23 #include "llvm/Support/Casting.h"
24 
25 using namespace llvm;
26 
27 MCXCOFFStreamer::MCXCOFFStreamer(MCContext &Context,
28                                  std::unique_ptr<MCAsmBackend> MAB,
29                                  std::unique_ptr<MCObjectWriter> OW,
30                                  std::unique_ptr<MCCodeEmitter> Emitter)
31     : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
32                        std::move(Emitter)) {}
33 
34 bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym,
35                                           MCSymbolAttr Attribute) {
36   auto *Symbol = cast<MCSymbolXCOFF>(Sym);
37   getAssembler().registerSymbol(*Symbol);
38 
39   switch (Attribute) {
40   // XCOFF doesn't support the cold feature.
41   case MCSA_Cold:
42     return false;
43 
44   case MCSA_Global:
45   case MCSA_Extern:
46     Symbol->setStorageClass(XCOFF::C_EXT);
47     Symbol->setExternal(true);
48     break;
49   case MCSA_LGlobal:
50     Symbol->setStorageClass(XCOFF::C_HIDEXT);
51     Symbol->setExternal(true);
52     break;
53   case llvm::MCSA_Weak:
54     Symbol->setStorageClass(XCOFF::C_WEAKEXT);
55     Symbol->setExternal(true);
56     break;
57   case llvm::MCSA_Hidden:
58     Symbol->setVisibilityType(XCOFF::SYM_V_HIDDEN);
59     break;
60   case llvm::MCSA_Protected:
61     Symbol->setVisibilityType(XCOFF::SYM_V_PROTECTED);
62     break;
63   case llvm::MCSA_Exported:
64     Symbol->setVisibilityType(XCOFF::SYM_V_EXPORTED);
65     break;
66   default:
67     report_fatal_error("Not implemented yet.");
68   }
69   return true;
70 }
71 
72 void MCXCOFFStreamer::emitXCOFFSymbolLinkageWithVisibility(
73     MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) {
74 
75   emitSymbolAttribute(Symbol, Linkage);
76 
77   // When the caller passes `MCSA_Invalid` for the visibility, do not emit one.
78   if (Visibility == MCSA_Invalid)
79     return;
80 
81   emitSymbolAttribute(Symbol, Visibility);
82 }
83 
84 void MCXCOFFStreamer::emitXCOFFExceptDirective(const MCSymbol *Symbol,
85                                                const MCSymbol *Trap,
86                                                unsigned Lang, unsigned Reason,
87                                                unsigned FunctionSize,
88                                                bool hasDebug) {
89   getAssembler().getWriter().addExceptionEntry(Symbol, Trap, Lang, Reason,
90                                                FunctionSize, hasDebug);
91 }
92 
93 void MCXCOFFStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
94                                        Align ByteAlignment) {
95   getAssembler().registerSymbol(*Symbol);
96   Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() !=
97                       XCOFF::C_HIDEXT);
98   Symbol->setCommon(Size, ByteAlignment);
99 
100   // Default csect align is 4, but common symbols have explicit alignment values
101   // and we should honor it.
102   cast<MCSymbolXCOFF>(Symbol)->getRepresentedCsect()->setAlignment(
103       ByteAlignment);
104 
105   // Emit the alignment and storage for the variable to the section.
106   emitValueToAlignment(ByteAlignment);
107   emitZeros(Size);
108 }
109 
110 void MCXCOFFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
111                                    uint64_t Size, Align ByteAlignment,
112                                    SMLoc Loc) {
113   report_fatal_error("Zero fill not implemented for XCOFF.");
114 }
115 
116 void MCXCOFFStreamer::emitInstToData(const MCInst &Inst,
117                                      const MCSubtargetInfo &STI) {
118   MCAssembler &Assembler = getAssembler();
119   SmallVector<MCFixup, 4> Fixups;
120   SmallString<256> Code;
121   raw_svector_ostream VecOS(Code);
122   Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
123 
124   // Add the fixups and data.
125   MCDataFragment *DF = getOrCreateDataFragment(&STI);
126   const size_t ContentsSize = DF->getContents().size();
127   auto &DataFragmentFixups = DF->getFixups();
128   for (auto &Fixup : Fixups) {
129     Fixup.setOffset(Fixup.getOffset() + ContentsSize);
130     DataFragmentFixups.push_back(Fixup);
131   }
132 
133   DF->setHasInstructions(STI);
134   DF->getContents().append(Code.begin(), Code.end());
135 }
136 
137 MCStreamer *llvm::createXCOFFStreamer(MCContext &Context,
138                                       std::unique_ptr<MCAsmBackend> &&MAB,
139                                       std::unique_ptr<MCObjectWriter> &&OW,
140                                       std::unique_ptr<MCCodeEmitter> &&CE,
141                                       bool RelaxAll) {
142   MCXCOFFStreamer *S = new MCXCOFFStreamer(Context, std::move(MAB),
143                                            std::move(OW), std::move(CE));
144   if (RelaxAll)
145     S->getAssembler().setRelaxAll(true);
146   return S;
147 }
148 
149 void MCXCOFFStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym,
150                                                  uint64_t Size,
151                                                  MCSymbol *CsectSym,
152                                                  Align Alignment) {
153   emitCommonSymbol(CsectSym, Size, Alignment);
154 }
155