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/MCCodeEmitter.h"
17 #include "llvm/MC/MCDirectives.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSymbolXCOFF.h"
20 #include "llvm/Support/TargetRegistry.h"
21 
22 using namespace llvm;
23 
24 MCXCOFFStreamer::MCXCOFFStreamer(MCContext &Context,
25                                  std::unique_ptr<MCAsmBackend> MAB,
26                                  std::unique_ptr<MCObjectWriter> OW,
27                                  std::unique_ptr<MCCodeEmitter> Emitter)
28     : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
29                        std::move(Emitter)) {}
30 
31 bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym,
32                                           MCSymbolAttr Attribute) {
33   auto *Symbol = cast<MCSymbolXCOFF>(Sym);
34   getAssembler().registerSymbol(*Symbol);
35 
36   switch (Attribute) {
37   case MCSA_Global:
38     Symbol->setStorageClass(XCOFF::C_EXT);
39     Symbol->setExternal(true);
40     break;
41   case MCSA_LGlobal:
42     Symbol->setStorageClass(XCOFF::C_HIDEXT);
43     Symbol->setExternal(true);
44     break;
45   default:
46     report_fatal_error("Not implemented yet.");
47   }
48   return true;
49 }
50 
51 void MCXCOFFStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
52                                        unsigned ByteAlignment) {
53   getAssembler().registerSymbol(*Symbol);
54   Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() !=
55                       XCOFF::C_HIDEXT);
56   Symbol->setCommon(Size, ByteAlignment);
57 
58   // Emit the alignment and storage for the variable to the section.
59   emitValueToAlignment(ByteAlignment);
60   emitZeros(Size);
61 }
62 
63 void MCXCOFFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
64                                    uint64_t Size, unsigned ByteAlignment,
65                                    SMLoc Loc) {
66   report_fatal_error("Zero fill not implemented for XCOFF.");
67 }
68 
69 void MCXCOFFStreamer::emitInstToData(const MCInst &Inst,
70                                      const MCSubtargetInfo &STI) {
71   MCAssembler &Assembler = getAssembler();
72   SmallVector<MCFixup, 4> Fixups;
73   SmallString<256> Code;
74   raw_svector_ostream VecOS(Code);
75   Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
76 
77   // Add the fixups and data.
78   MCDataFragment *DF = getOrCreateDataFragment(&STI);
79   const size_t ContentsSize = DF->getContents().size();
80   auto &DataFragmentFixups = DF->getFixups();
81   for (auto &Fixup : Fixups) {
82     Fixup.setOffset(Fixup.getOffset() + ContentsSize);
83     DataFragmentFixups.push_back(Fixup);
84   }
85 
86   DF->setHasInstructions(STI);
87   DF->getContents().append(Code.begin(), Code.end());
88 }
89 
90 MCStreamer *llvm::createXCOFFStreamer(MCContext &Context,
91                                       std::unique_ptr<MCAsmBackend> &&MAB,
92                                       std::unique_ptr<MCObjectWriter> &&OW,
93                                       std::unique_ptr<MCCodeEmitter> &&CE,
94                                       bool RelaxAll) {
95   MCXCOFFStreamer *S = new MCXCOFFStreamer(Context, std::move(MAB),
96                                            std::move(OW), std::move(CE));
97   if (RelaxAll)
98     S->getAssembler().setRelaxAll(true);
99   return S;
100 }
101 
102 void MCXCOFFStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym,
103                                                  uint64_t Size,
104                                                  MCSymbol *CsectSym,
105                                                  unsigned ByteAlignment) {
106   emitCommonSymbol(CsectSym, Size, ByteAlignment);
107 }
108