LLVM 22.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
35 return static_cast<XCOFFObjectWriter &>(getAssembler().getWriter());
36}
37
39 MCObjectStreamer::changeSection(Section, Subsection);
40 auto *Sec = static_cast<const MCSectionXCOFF *>(Section);
41 // We might miss calculating the symbols difference as absolute value before
42 // adding fixups when symbol_A without the fragment set is the csect itself
43 // and symbol_B is in it.
44 // TODO: Currently we only set the fragment for XMC_PR csects and DWARF
45 // sections because we don't have other cases that hit this problem yet.
46 // if (IsDwarfSec || CsectProp->MappingClass == XCOFF::XMC_PR)
47 // QualName->setFragment(F);
48 if (Sec->isDwarfSect() || Sec->getMappingClass() == XCOFF::XMC_PR)
50}
51
54 auto *Symbol = static_cast<MCSymbolXCOFF *>(Sym);
56
57 switch (Attribute) {
58 // XCOFF doesn't support the cold feature.
59 case MCSA_Cold:
60 return false;
61
62 case MCSA_Global:
63 case MCSA_Extern:
64 Symbol->setStorageClass(XCOFF::C_EXT);
65 Symbol->setExternal(true);
66 break;
67 case MCSA_LGlobal:
68 Symbol->setStorageClass(XCOFF::C_HIDEXT);
69 Symbol->setExternal(true);
70 break;
71 case llvm::MCSA_Weak:
72 Symbol->setStorageClass(XCOFF::C_WEAKEXT);
73 Symbol->setExternal(true);
74 break;
76 Symbol->setVisibilityType(XCOFF::SYM_V_HIDDEN);
77 break;
79 Symbol->setVisibilityType(XCOFF::SYM_V_PROTECTED);
80 break;
82 Symbol->setVisibilityType(XCOFF::SYM_V_EXPORTED);
83 break;
84 default:
85 report_fatal_error("Not implemented yet.");
86 }
87 return true;
88}
89
91 MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) {
92
93 emitSymbolAttribute(Symbol, Linkage);
94
95 // When the caller passes `MCSA_Invalid` for the visibility, do not emit one.
96 if (Visibility == MCSA_Invalid)
97 return;
98
99 emitSymbolAttribute(Symbol, Visibility);
100}
101
103 // Add a Fixup here to later record a relocation of type R_REF to prevent the
104 // ref symbol from being garbage collected (by the binder).
107}
108
110 StringRef Rename) {
111 auto *Symbol = static_cast<const MCSymbolXCOFF *>(Name);
112 if (!Symbol->hasRename())
113 report_fatal_error("Only explicit .rename is supported for XCOFF.");
114}
115
117 const MCSymbol *Trap,
118 unsigned Lang, unsigned Reason,
119 unsigned FunctionSize,
120 bool hasDebug) {
121 getWriter().addExceptionEntry(Symbol, Trap, Lang, Reason, FunctionSize,
122 hasDebug);
123}
124
127}
128
130 Align ByteAlignment) {
131 auto &Sym = static_cast<MCSymbolXCOFF &>(*Symbol);
132 getAssembler().registerSymbol(*Symbol);
133 Sym.setExternal(Sym.getStorageClass() != XCOFF::C_HIDEXT);
134 Symbol->setCommon(Size, ByteAlignment);
135
136 // Default csect align is 4, but common symbols have explicit alignment values
137 // and we should honor it.
138 Sym.getRepresentedCsect()->setAlignment(ByteAlignment);
139
140 // Emit the alignment and storage for the variable to the section.
141 emitValueToAlignment(ByteAlignment);
143}
144
147 MCSymbol *CsectSym,
148 Align Alignment) {
149 emitCommonSymbol(CsectSym, Size, Alignment);
150}
dxil DXContainer Global Emitter
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:179
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
Context object for machine code objects.
Definition: MCContext.h:83
Streaming object file generation interface.
void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
MCAssembler & getAssembler()
void addFixup(const MCExpr *Value, MCFixupKind Kind)
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
MCSymbolXCOFF * getQualNameSymbol() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
MCContext & getContext() const
Definition: MCStreamer.h:314
MCFragment * CurFrag
Definition: MCStreamer.h:267
void emitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:204
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
void setFragment(MCFragment *F) const
Mark the symbol as defined in the fragment F.
Definition: MCSymbol.h:257
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a common symbol.
XCOFFObjectWriter & getWriter()
void emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, uint64_t Size, MCSymbol *CsectSym, Align Alignment) override
Emits an lcomm directive with XCOFF csect information.
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 changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
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:63
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
virtual void addExceptionEntry(const MCSymbol *Symbol, const MCSymbol *Trap, unsigned LanguageCode, unsigned ReasonCode, unsigned FunctionSize, bool hasDebug)=0
virtual void addCInfoSymEntry(StringRef Name, StringRef Metadata)=0
@ R_REF
A non-relocating relocation.
Definition: XCOFF.h:294
@ SYM_V_PROTECTED
Definition: XCOFF.h:256
@ SYM_V_HIDDEN
Definition: XCOFF.h:255
@ SYM_V_EXPORTED
Definition: XCOFF.h:257
@ C_WEAKEXT
Definition: XCOFF.h:200
@ C_HIDEXT
Definition: XCOFF.h:207
@ XMC_PR
Program Code.
Definition: XCOFF.h:106
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
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:1886
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:856
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39