MCXCOFFStreamer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include "llvm/MC/MCXCOFFStreamer.h"
  13. #include "llvm/BinaryFormat/XCOFF.h"
  14. #include "llvm/MC/MCAsmBackend.h"
  15. #include "llvm/MC/MCAssembler.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/MC/TargetRegistry.h"
  22. #include "llvm/Support/Casting.h"
  23. using namespace llvm;
  24. MCXCOFFStreamer::MCXCOFFStreamer(MCContext &Context,
  25. std::unique_ptr<MCAsmBackend> MAB,
  26. std::unique_ptr<MCObjectWriter> OW,
  27. std::unique_ptr<MCCodeEmitter> Emitter)
  28. : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
  29. std::move(Emitter)) {}
  30. bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym,
  31. MCSymbolAttr Attribute) {
  32. auto *Symbol = cast<MCSymbolXCOFF>(Sym);
  33. getAssembler().registerSymbol(*Symbol);
  34. switch (Attribute) {
  35. // XCOFF doesn't support the cold feature.
  36. case MCSA_Cold:
  37. return false;
  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;
  54. case llvm::MCSA_Protected:
  55. Symbol->setVisibilityType(XCOFF::SYM_V_PROTECTED);
  56. break;
  57. case llvm::MCSA_Exported:
  58. Symbol->setVisibilityType(XCOFF::SYM_V_EXPORTED);
  59. break;
  60. default:
  61. report_fatal_error("Not implemented yet.");
  62. }
  63. return true;
  64. }
  65. void MCXCOFFStreamer::emitXCOFFSymbolLinkageWithVisibility(
  66. MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) {
  67. emitSymbolAttribute(Symbol, Linkage);
  68. // When the caller passes `MCSA_Invalid` for the visibility, do not emit one.
  69. if (Visibility == MCSA_Invalid)
  70. return;
  71. emitSymbolAttribute(Symbol, Visibility);
  72. }
  73. void MCXCOFFStreamer::emitXCOFFExceptDirective(const MCSymbol *Symbol,
  74. const MCSymbol *Trap,
  75. unsigned Lang, unsigned Reason,
  76. unsigned FunctionSize,
  77. bool hasDebug) {
  78. getAssembler().getWriter().addExceptionEntry(Symbol, Trap, Lang, Reason,
  79. FunctionSize, hasDebug);
  80. }
  81. void MCXCOFFStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  82. Align ByteAlignment) {
  83. getAssembler().registerSymbol(*Symbol);
  84. Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() !=
  85. XCOFF::C_HIDEXT);
  86. Symbol->setCommon(Size, ByteAlignment);
  87. // Default csect align is 4, but common symbols have explicit alignment values
  88. // and we should honor it.
  89. cast<MCSymbolXCOFF>(Symbol)->getRepresentedCsect()->setAlignment(
  90. ByteAlignment);
  91. // Emit the alignment and storage for the variable to the section.
  92. emitValueToAlignment(ByteAlignment);
  93. emitZeros(Size);
  94. }
  95. void MCXCOFFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
  96. uint64_t Size, Align ByteAlignment,
  97. SMLoc Loc) {
  98. report_fatal_error("Zero fill not implemented for XCOFF.");
  99. }
  100. void MCXCOFFStreamer::emitInstToData(const MCInst &Inst,
  101. const MCSubtargetInfo &STI) {
  102. MCAssembler &Assembler = getAssembler();
  103. SmallVector<MCFixup, 4> Fixups;
  104. SmallString<256> Code;
  105. raw_svector_ostream VecOS(Code);
  106. Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
  107. // Add the fixups and data.
  108. MCDataFragment *DF = getOrCreateDataFragment(&STI);
  109. const size_t ContentsSize = DF->getContents().size();
  110. auto &DataFragmentFixups = DF->getFixups();
  111. for (auto &Fixup : Fixups) {
  112. Fixup.setOffset(Fixup.getOffset() + ContentsSize);
  113. DataFragmentFixups.push_back(Fixup);
  114. }
  115. DF->setHasInstructions(STI);
  116. DF->getContents().append(Code.begin(), Code.end());
  117. }
  118. MCStreamer *llvm::createXCOFFStreamer(MCContext &Context,
  119. std::unique_ptr<MCAsmBackend> &&MAB,
  120. std::unique_ptr<MCObjectWriter> &&OW,
  121. std::unique_ptr<MCCodeEmitter> &&CE,
  122. bool RelaxAll) {
  123. MCXCOFFStreamer *S = new MCXCOFFStreamer(Context, std::move(MAB),
  124. std::move(OW), std::move(CE));
  125. if (RelaxAll)
  126. S->getAssembler().setRelaxAll(true);
  127. return S;
  128. }
  129. void MCXCOFFStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym,
  130. uint64_t Size,
  131. MCSymbol *CsectSym,
  132. Align Alignment) {
  133. emitCommonSymbol(CsectSym, Size, Alignment);
  134. }