LLVM  11.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/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"
22 
23 using namespace llvm;
24 
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)) {}
31 
34  auto *Symbol = cast<MCSymbolXCOFF>(Sym);
36 
37  switch (Attribute) {
38  case MCSA_Global:
39  case MCSA_Extern:
40  Symbol->setStorageClass(XCOFF::C_EXT);
41  Symbol->setExternal(true);
42  break;
43  case MCSA_LGlobal:
44  Symbol->setStorageClass(XCOFF::C_HIDEXT);
45  Symbol->setExternal(true);
46  break;
47  case llvm::MCSA_Weak:
48  Symbol->setStorageClass(XCOFF::C_WEAKEXT);
49  Symbol->setExternal(true);
50  break;
51  case llvm::MCSA_Hidden:
52  Symbol->setVisibilityType(XCOFF::SYM_V_HIDDEN);
53  break;
55  Symbol->setVisibilityType(XCOFF::SYM_V_PROTECTED);
56  break;
57  default:
58  report_fatal_error("Not implemented yet.");
59  }
60  return true;
61 }
62 
65 
66  emitSymbolAttribute(Symbol, Linkage);
67 
68  // When the caller passes `MCSA_Invalid` for the visibility, do not emit one.
69  if (Visibility == MCSA_Invalid)
70  return;
71 
72  emitSymbolAttribute(Symbol, Visibility);
73 }
74 
76  unsigned ByteAlignment) {
77  getAssembler().registerSymbol(*Symbol);
78  Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() !=
80  Symbol->setCommon(Size, ByteAlignment);
81 
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));
86 
87  // Emit the alignment and storage for the variable to the section.
88  emitValueToAlignment(ByteAlignment);
89  emitZeros(Size);
90 }
91 
93  uint64_t Size, unsigned ByteAlignment,
94  SMLoc Loc) {
95  report_fatal_error("Zero fill not implemented for XCOFF.");
96 }
97 
99  const MCSubtargetInfo &STI) {
100  MCAssembler &Assembler = getAssembler();
102  SmallString<256> Code;
103  raw_svector_ostream VecOS(Code);
104  Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
105 
106  // Add the fixups and data.
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);
113  }
114 
115  DF->setHasInstructions(STI);
116  DF->getContents().append(Code.begin(), Code.end());
117 }
118 
120  std::unique_ptr<MCAsmBackend> &&MAB,
121  std::unique_ptr<MCObjectWriter> &&OW,
122  std::unique_ptr<MCCodeEmitter> &&CE,
123  bool RelaxAll) {
124  MCXCOFFStreamer *S = new MCXCOFFStreamer(Context, std::move(MAB),
125  std::move(OW), std::move(CE));
126  if (RelaxAll)
127  S->getAssembler().setRelaxAll(true);
128  return S;
129 }
130 
132  uint64_t Size,
133  MCSymbol *CsectSym,
134  unsigned ByteAlignment) {
135  emitCommonSymbol(CsectSym, Size, ByteAlignment);
136 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
LLVMContext & Context
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void setCommon(uint64_t Size, unsigned Align, bool Target=false)
Mark this symbol as being &#39;common&#39;.
Definition: MCSymbol.h:341
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Not a valid directive.
Definition: MCDirectives.h:19
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:550
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
Definition: BitVector.h:959
void setExternal(bool Value) const
Definition: MCSymbol.h:398
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:294
MCStreamer * createXCOFFStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE, bool RelaxAll)
void emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, uint64_t Size, MCSymbol *CsectSym, unsigned ByteAlign) override
Emits an lcomm directive with XCOFF csect information.
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.
void setRelaxAll(bool Value)
Definition: MCAssembler.h:321
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.
Context object for machine code objects.
Definition: MCContext.h:66
.lglobl (XCOFF)
Definition: MCDirectives.h:31
Streaming object file generation interface.
.extern (XCOFF)
Definition: MCDirectives.h:32
.protected (ELF)
Definition: MCDirectives.h:42
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:187
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
.hidden (ELF)
Definition: MCDirectives.h:33
Streaming machine code generation interface.
Definition: MCStreamer.h:196
MCAssembler & getAssembler()
void setHasInstructions(const MCSubtargetInfo &STI)
Record that the fragment contains instructions with the MCSubtargetInfo in effect when the instructio...
Definition: MCFragment.h:167
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:213
void emitXCOFFSymbolLinkageWithVisibility(MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) override
Emit a symbol&#39;s linkage and visibilty with a linkage directive for XCOFF.
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
PowerPC TLS Dynamic Call Fixup
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
MCSymbolAttr
Definition: MCDirectives.h:18
void emitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:206
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) override
Emit a common symbol.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:433
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...
.type _foo,
Definition: MCDirectives.h:30
Generic base class for all target subtargets.
uint32_t Size
Definition: Profile.cpp:46
size_t size() const
Definition: SmallVector.h:66
Fragment for data and encoded instructions.
Definition: MCFragment.h:232
void emitValueToAlignment(unsigned ByteAlignment, 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.
MCXCOFFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
Represents a location in source code.
Definition: SMLoc.h:23
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override