1 //===- lib/MC/MCXCOFFStreamer.cpp - XCOFF Object Output -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file assembles .s files and emits XCOFF .o object files.
11 //===----------------------------------------------------------------------===//
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/MCSectionXCOFF.h"
20 #include "llvm/MC/MCSymbolXCOFF.h"
21 #include "llvm/Support/TargetRegistry.h"
25 MCXCOFFStreamer::MCXCOFFStreamer(MCContext
&Context
,
26 std::unique_ptr
<MCAsmBackend
> MAB
,
27 std::unique_ptr
<MCObjectWriter
> OW
,
28 std::unique_ptr
<MCCodeEmitter
> Emitter
)
29 : MCObjectStreamer(Context
, std::move(MAB
), std::move(OW
),
30 std::move(Emitter
)) {}
32 bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol
*Sym
,
33 MCSymbolAttr Attribute
) {
34 auto *Symbol
= cast
<MCSymbolXCOFF
>(Sym
);
35 getAssembler().registerSymbol(*Symbol
);
40 Symbol
->setStorageClass(XCOFF::C_EXT
);
41 Symbol
->setExternal(true);
44 Symbol
->setStorageClass(XCOFF::C_HIDEXT
);
45 Symbol
->setExternal(true);
48 Symbol
->setStorageClass(XCOFF::C_WEAKEXT
);
49 Symbol
->setExternal(true);
51 case llvm::MCSA_Hidden
:
52 Symbol
->setVisibilityType(XCOFF::SYM_V_HIDDEN
);
54 case llvm::MCSA_Protected
:
55 Symbol
->setVisibilityType(XCOFF::SYM_V_PROTECTED
);
58 report_fatal_error("Not implemented yet.");
63 void MCXCOFFStreamer::emitXCOFFSymbolLinkageWithVisibility(
64 MCSymbol
*Symbol
, MCSymbolAttr Linkage
, MCSymbolAttr Visibility
) {
66 emitSymbolAttribute(Symbol
, Linkage
);
68 // When the caller passes `MCSA_Invalid` for the visibility, do not emit one.
69 if (Visibility
== MCSA_Invalid
)
72 emitSymbolAttribute(Symbol
, Visibility
);
75 void MCXCOFFStreamer::emitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
76 unsigned ByteAlignment
) {
77 getAssembler().registerSymbol(*Symbol
);
78 Symbol
->setExternal(cast
<MCSymbolXCOFF
>(Symbol
)->getStorageClass() !=
80 Symbol
->setCommon(Size
, ByteAlignment
);
82 // Default csect align is 4, but common symbols have explicit alignment values
83 // and we should honor it.
84 cast
<MCSymbolXCOFF
>(Symbol
)->getRepresentedCsect()->setAlignment(
85 Align(ByteAlignment
));
87 // Emit the alignment and storage for the variable to the section.
88 emitValueToAlignment(ByteAlignment
);
92 void MCXCOFFStreamer::emitZerofill(MCSection
*Section
, MCSymbol
*Symbol
,
93 uint64_t Size
, unsigned ByteAlignment
,
95 report_fatal_error("Zero fill not implemented for XCOFF.");
98 void MCXCOFFStreamer::emitInstToData(const MCInst
&Inst
,
99 const MCSubtargetInfo
&STI
) {
100 MCAssembler
&Assembler
= getAssembler();
101 SmallVector
<MCFixup
, 4> Fixups
;
102 SmallString
<256> Code
;
103 raw_svector_ostream
VecOS(Code
);
104 Assembler
.getEmitter().encodeInstruction(Inst
, VecOS
, Fixups
, STI
);
106 // Add the fixups and data.
107 MCDataFragment
*DF
= getOrCreateDataFragment(&STI
);
108 const size_t ContentsSize
= DF
->getContents().size();
109 auto &DataFragmentFixups
= DF
->getFixups();
110 for (auto &Fixup
: Fixups
) {
111 Fixup
.setOffset(Fixup
.getOffset() + ContentsSize
);
112 DataFragmentFixups
.push_back(Fixup
);
115 DF
->setHasInstructions(STI
);
116 DF
->getContents().append(Code
.begin(), Code
.end());
119 MCStreamer
*llvm::createXCOFFStreamer(MCContext
&Context
,
120 std::unique_ptr
<MCAsmBackend
> &&MAB
,
121 std::unique_ptr
<MCObjectWriter
> &&OW
,
122 std::unique_ptr
<MCCodeEmitter
> &&CE
,
124 MCXCOFFStreamer
*S
= new MCXCOFFStreamer(Context
, std::move(MAB
),
125 std::move(OW
), std::move(CE
));
127 S
->getAssembler().setRelaxAll(true);
131 void MCXCOFFStreamer::emitXCOFFLocalCommonSymbol(MCSymbol
*LabelSym
,
134 unsigned ByteAlignment
) {
135 emitCommonSymbol(CsectSym
, Size
, ByteAlignment
);