LLVM  16.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 
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 
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);
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;
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  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 
94  unsigned ByteAlignment) {
95  getAssembler().registerSymbol(*Symbol);
96  Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() !=
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(
104 
105  // Emit the alignment and storage for the variable to the section.
107  emitZeros(Size);
108 }
109 
111  uint64_t Size, unsigned ByteAlignment,
112  SMLoc Loc) {
113  report_fatal_error("Zero fill not implemented for XCOFF.");
114 }
115 
117  const MCSubtargetInfo &STI) {
118  MCAssembler &Assembler = getAssembler();
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.
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 
138  std::unique_ptr<MCAsmBackend> &&MAB,
139  std::unique_ptr<MCObjectWriter> &&OW,
140  std::unique_ptr<MCCodeEmitter> &&CE,
141  bool RelaxAll) {
143  std::move(OW), std::move(CE));
144  if (RelaxAll)
145  S->getAssembler().setRelaxAll(true);
146  return S;
147 }
148 
150  uint64_t Size,
151  MCSymbol *CsectSym,
152  Align Alignment) {
153  emitCommonSymbol(CsectSym, Size, Alignment.value());
154 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::MCXCOFFStreamer::emitXCOFFSymbolLinkageWithVisibility
void emitXCOFFSymbolLinkageWithVisibility(MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) override
Emit a symbol's linkage and visibility with a linkage directive for XCOFF.
Definition: MCXCOFFStreamer.cpp:72
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCAssembler::registerSymbol
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
Definition: MCAssembler.cpp:467
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
llvm::Attribute
Definition: Attributes.h:66
MCCodeEmitter.h
MCDirectives.h
llvm::MCSA_Invalid
@ MCSA_Invalid
Not a valid directive.
Definition: MCDirectives.h:19
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::MCXCOFFStreamer::emitCommonSymbol
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) override
Emit a common symbol.
Definition: MCXCOFFStreamer.cpp:93
llvm::MCXCOFFStreamer::emitSymbolAttribute
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
Definition: MCXCOFFStreamer.cpp:34
MCAssembler.h
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::XCOFF::SYM_V_PROTECTED
@ SYM_V_PROTECTED
Definition: XCOFF.h:242
llvm::MCXCOFFStreamer::emitXCOFFExceptDirective
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...
Definition: MCXCOFFStreamer.cpp:84
llvm::MCSymbolAttr
MCSymbolAttr
Definition: MCDirectives.h:18
llvm::MCSA_Cold
@ MCSA_Cold
.cold (MachO)
Definition: MCDirectives.h:22
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
XCOFF.h
llvm::MCXCOFFStreamer::emitZerofill
void emitZerofill(MCSection *Section, MCSymbol *Symbol=nullptr, uint64_t Size=0, unsigned ByteAlignment=0, SMLoc Loc=SMLoc()) override
Emit the zerofill section and an optional symbol.
Definition: MCXCOFFStreamer.cpp:110
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:212
Emitter
dxil DXContainer Global Emitter
Definition: DXContainerGlobals.cpp:67
MCAsmBackend.h
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::MCSA_Extern
@ MCSA_Extern
.extern (XCOFF)
Definition: MCDirectives.h:32
llvm::MCSA_Protected
@ MCSA_Protected
.protected (ELF)
Definition: MCDirectives.h:43
llvm::MCSA_LGlobal
@ MCSA_LGlobal
.lglobl (XCOFF)
Definition: MCDirectives.h:31
llvm::MCObjectStreamer
Streaming object file generation interface.
Definition: MCObjectStreamer.h:42
llvm::AArch64::Fixups
Fixups
Definition: AArch64FixupKinds.h:17
llvm::MCObjectStreamer::emitValueToAlignment
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.
Definition: MCObjectStreamer.cpp:644
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145
llvm::MCSA_Hidden
@ MCSA_Hidden
.hidden (ELF)
Definition: MCDirectives.h:33
Align
uint64_t Align
Definition: ELFObjHandler.cpp:82
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
MCSectionXCOFF.h
DF
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
llvm::SmallString< 256 >
llvm::MCSA_Exported
@ MCSA_Exported
.globl _foo, exported (XCOFF)
Definition: MCDirectives.h:34
llvm::XCOFF::C_EXT
@ C_EXT
Definition: XCOFF.h:185
llvm::XCOFF::SYM_V_EXPORTED
@ SYM_V_EXPORTED
Definition: XCOFF.h:243
llvm::MCAssembler
Definition: MCAssembler.h:73
llvm::MCXCOFFStreamer::MCXCOFFStreamer
MCXCOFFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
Definition: MCXCOFFStreamer.cpp:27
llvm::MCAssembler::getEmitter
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:331
llvm::createXCOFFStreamer
MCStreamer * createXCOFFStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE, bool RelaxAll)
Definition: MCXCOFFStreamer.cpp:137
uint64_t
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
MCSymbolXCOFF.h
llvm::MCObjectWriter::addExceptionEntry
virtual void addExceptionEntry(const MCSymbol *Symbol, const MCSymbol *Trap, unsigned LanguageCode, unsigned ReasonCode, unsigned FunctionSize, bool hasDebug)
Definition: MCObjectWriter.h:108
llvm::XCOFF::C_HIDEXT
@ C_HIDEXT
Definition: XCOFF.h:193
llvm::move
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:1836
llvm::MCXCOFFStreamer::emitXCOFFLocalCommonSymbol
void emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, uint64_t Size, MCSymbol *CsectSym, Align Alignment) override
Emits an lcomm directive with XCOFF csect information.
Definition: MCXCOFFStreamer.cpp:149
llvm::MCObjectStreamer::getAssembler
MCAssembler & getAssembler()
Definition: MCObjectStreamer.h:128
llvm::MCXCOFFStreamer::emitInstToData
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override
Definition: MCXCOFFStreamer.cpp:116
llvm::MCObjectStreamer::getOrCreateDataFragment
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.
Definition: MCObjectStreamer.cpp:225
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:215
llvm::XCOFF::C_WEAKEXT
@ C_WEAKEXT
Definition: XCOFF.h:186
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::XCOFF::SYM_V_HIDDEN
@ SYM_V_HIDDEN
Definition: XCOFF.h:241
llvm::MCXCOFFStreamer
Definition: MCXCOFFStreamer.h:16
MCObjectWriter.h
std
Definition: BitVector.h:851
llvm::Align::value
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
Casting.h
llvm::MCSA_Global
@ MCSA_Global
.type _foo, @gnu_unique_object
Definition: MCDirectives.h:30
llvm::LCOMM::ByteAlignment
@ ByteAlignment
Definition: MCAsmInfo.h:50
llvm::MCSA_Weak
@ MCSA_Weak
.weak
Definition: MCDirectives.h:45
MCXCOFFStreamer.h
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:659
llvm::MCDataFragment
Fragment for data and encoded instructions.
Definition: MCFragment.h:241
TargetRegistry.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:76
llvm::MCCodeEmitter::encodeInstruction
virtual void encodeInstruction(const MCInst &Inst, raw_ostream &OS, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
EncodeInstruction - Encode the given Inst to bytes on the output stream OS.
llvm::MCStreamer::emitZeros
void emitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:227
llvm::MCAssembler::getWriter
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:333