LLVM 19.0.0git
MCXCOFFStreamer.cpp
Go to the documentation of this file.
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
16#include "llvm/MC/MCAssembler.h"
24
25using namespace llvm;
26
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
36 auto *Symbol = cast<MCSymbolXCOFF>(Sym);
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;
58 Symbol->setVisibilityType(XCOFF::SYM_V_HIDDEN);
59 break;
61 Symbol->setVisibilityType(XCOFF::SYM_V_PROTECTED);
62 break;
64 Symbol->setVisibilityType(XCOFF::SYM_V_EXPORTED);
65 break;
66 default:
67 report_fatal_error("Not implemented yet.");
68 }
69 return true;
70}
71
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
85 // Add a Fixup here to later record a relocation of type R_REF to prevent the
86 // ref symbol from being garbage collected (by the binder).
89 std::optional<MCFixupKind> MaybeKind =
91 if (!MaybeKind)
92 report_fatal_error("failed to get fixup kind for R_REF relocation");
93
94 MCFixupKind Kind = *MaybeKind;
95 MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, Kind);
96 DF->getFixups().push_back(Fixup);
97}
98
100 StringRef Rename) {
101 const MCSymbolXCOFF *Symbol = cast<const MCSymbolXCOFF>(Name);
102 if (!Symbol->hasRename())
103 report_fatal_error("Only explicit .rename is supported for XCOFF.");
104}
105
107 const MCSymbol *Trap,
108 unsigned Lang, unsigned Reason,
109 unsigned FunctionSize,
110 bool hasDebug) {
111 getAssembler().getWriter().addExceptionEntry(Symbol, Trap, Lang, Reason,
112 FunctionSize, hasDebug);
113}
114
117}
118
120 Align ByteAlignment) {
121 getAssembler().registerSymbol(*Symbol);
122 Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() !=
124 Symbol->setCommon(Size, ByteAlignment);
125
126 // Default csect align is 4, but common symbols have explicit alignment values
127 // and we should honor it.
128 cast<MCSymbolXCOFF>(Symbol)->getRepresentedCsect()->setAlignment(
129 ByteAlignment);
130
131 // Emit the alignment and storage for the variable to the section.
132 emitValueToAlignment(ByteAlignment);
134}
135
137 uint64_t Size, Align ByteAlignment,
138 SMLoc Loc) {
139 report_fatal_error("Zero fill not implemented for XCOFF.");
140}
141
143 const MCSubtargetInfo &STI) {
144 MCAssembler &Assembler = getAssembler();
146 SmallString<256> Code;
147 Assembler.getEmitter().encodeInstruction(Inst, Code, Fixups, STI);
148
149 // Add the fixups and data.
151 const size_t ContentsSize = DF->getContents().size();
152 auto &DataFragmentFixups = DF->getFixups();
153 for (auto &Fixup : Fixups) {
154 Fixup.setOffset(Fixup.getOffset() + ContentsSize);
155 DataFragmentFixups.push_back(Fixup);
156 }
157
158 DF->setHasInstructions(STI);
159 DF->getContents().append(Code.begin(), Code.end());
160}
161
163 std::unique_ptr<MCAsmBackend> &&MAB,
164 std::unique_ptr<MCObjectWriter> &&OW,
165 std::unique_ptr<MCCodeEmitter> &&CE,
166 bool RelaxAll) {
167 MCXCOFFStreamer *S = new MCXCOFFStreamer(Context, std::move(MAB),
168 std::move(OW), std::move(CE));
169 if (RelaxAll)
170 S->getAssembler().setRelaxAll(true);
171 return S;
172}
173
176 MCSymbol *CsectSym,
177 Align Alignment) {
178 emitCommonSymbol(CsectSym, Size, Alignment);
179}
dxil DXContainer Global Emitter
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
LLVMContext & Context
PowerPC TLS Dynamic Call Fixup
virtual std::optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:338
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:336
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:334
bool registerSymbol(const MCSymbol &Symbol)
void setRelaxAll(bool Value)
Definition: MCAssembler.h:363
virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
Encode the given Inst to bytes and append to CB.
Context object for machine code objects.
Definition: MCContext.h:81
Fragment for data and encoded instructions.
Definition: MCFragment.h:242
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Streaming object file generation interface.
void emitValueToAlignment(Align Alignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
MCDataFragment * getOrCreateDataFragment(const MCSubtargetInfo *STI=nullptr)
Get a data fragment to write into, creating a new one if the current fragment is not a data fragment.
MCAssembler & getAssembler()
virtual void addExceptionEntry(const MCSymbol *Symbol, const MCSymbol *Trap, unsigned LanguageCode, unsigned ReasonCode, unsigned FunctionSize, bool hasDebug)
virtual void addCInfoSymEntry(StringRef Name, StringRef Metadata)
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
Streaming machine code generation interface.
Definition: MCStreamer.h:212
MCContext & getContext() const
Definition: MCStreamer.h:297
void emitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:230
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:397
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a common symbol.
void emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, uint64_t Size, MCSymbol *CsectSym, Align Alignment) override
Emits an lcomm directive with XCOFF csect information.
void emitZerofill(MCSection *Section, MCSymbol *Symbol=nullptr, uint64_t Size=0, Align ByteAlignment=Align(1), SMLoc Loc=SMLoc()) override
Emit the zerofill section and an optional symbol.
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override
void emitXCOFFRefDirective(const MCSymbol *Symbol) override
Emit a XCOFF .ref directive which creates R_REF type entry in the relocation table for one or more sy...
MCXCOFFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
void emitXCOFFExceptDirective(const MCSymbol *Symbol, const MCSymbol *Trap, unsigned Lang, unsigned Reason, unsigned FunctionSize, bool hasDebug) override
Emit an XCOFF .except directive which adds information about a trap instruction to the object file ex...
void emitXCOFFRenameDirective(const MCSymbol *Name, StringRef Rename) override
Emit a XCOFF .rename directive which creates a synonym for an illegal or undesirable name.
void emitXCOFFCInfoSym(StringRef Name, StringRef Metadata) override
Emit a C_INFO symbol with XCOFF embedded metadata to the .info section.
void emitXCOFFSymbolLinkageWithVisibility(MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) override
Emit a symbol's linkage and visibility with a linkage directive for XCOFF.
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
Root of the metadata hierarchy.
Definition: Metadata.h:62
Represents a location in source code.
Definition: SMLoc.h:23
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
@ SYM_V_PROTECTED
Definition: XCOFF.h:255
@ SYM_V_HIDDEN
Definition: XCOFF.h:254
@ SYM_V_EXPORTED
Definition: XCOFF.h:256
@ C_WEAKEXT
Definition: XCOFF.h:199
@ C_HIDEXT
Definition: XCOFF.h:206
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
MCStreamer * createXCOFFStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE, bool RelaxAll)
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
MCSymbolAttr
Definition: MCDirectives.h:18
@ MCSA_Protected
.protected (ELF)
Definition: MCDirectives.h:43
@ MCSA_Exported
.globl _foo, exported (XCOFF)
Definition: MCDirectives.h:34
@ MCSA_Weak
.weak
Definition: MCDirectives.h:45
@ MCSA_Global
.type _foo, @gnu_unique_object
Definition: MCDirectives.h:30
@ MCSA_Extern
.extern (XCOFF)
Definition: MCDirectives.h:32
@ MCSA_Cold
.cold (MachO)
Definition: MCDirectives.h:22
@ MCSA_Hidden
.hidden (ELF)
Definition: MCDirectives.h:33
@ MCSA_LGlobal
.lglobl (XCOFF)
Definition: MCDirectives.h:31
@ MCSA_Invalid
Not a valid directive.
Definition: MCDirectives.h:19
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39