MCXCOFFStreamer.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/MCCodeEmitter.h"
  16. #include "llvm/MC/MCDirectives.h"
  17. #include "llvm/MC/MCObjectWriter.h"
  18. #include "llvm/MC/MCSectionXCOFF.h"
  19. #include "llvm/MC/MCSymbolXCOFF.h"
  20. #include "llvm/MC/TargetRegistry.h"
  21. using namespace llvm;
  22. MCXCOFFStreamer::MCXCOFFStreamer(MCContext &Context,
  23. std::unique_ptr<MCAsmBackend> MAB,
  24. std::unique_ptr<MCObjectWriter> OW,
  25. std::unique_ptr<MCCodeEmitter> Emitter)
  26. : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
  27. std::move(Emitter)) {}
  28. bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym,
  29. MCSymbolAttr Attribute) {
  30. auto *Symbol = cast<MCSymbolXCOFF>(Sym);
  31. getAssembler().registerSymbol(*Symbol);
  32. switch (Attribute) {
  33. case MCSA_Global:
  34. case MCSA_Extern:
  35. Symbol->setStorageClass(XCOFF::C_EXT);
  36. Symbol->setExternal(true);
  37. break;
  38. case MCSA_LGlobal:
  39. Symbol->setStorageClass(XCOFF::C_HIDEXT);
  40. Symbol->setExternal(true);
  41. break;
  42. case llvm::MCSA_Weak:
  43. Symbol->setStorageClass(XCOFF::C_WEAKEXT);
  44. Symbol->setExternal(true);
  45. break;
  46. case llvm::MCSA_Hidden:
  47. Symbol->setVisibilityType(XCOFF::SYM_V_HIDDEN);
  48. break;
  49. case llvm::MCSA_Protected:
  50. Symbol->setVisibilityType(XCOFF::SYM_V_PROTECTED);
  51. break;
  52. default:
  53. report_fatal_error("Not implemented yet.");
  54. }
  55. return true;
  56. }
  57. void MCXCOFFStreamer::emitXCOFFSymbolLinkageWithVisibility(
  58. MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) {
  59. emitSymbolAttribute(Symbol, Linkage);
  60. // When the caller passes `MCSA_Invalid` for the visibility, do not emit one.
  61. if (Visibility == MCSA_Invalid)
  62. return;
  63. emitSymbolAttribute(Symbol, Visibility);
  64. }
  65. void MCXCOFFStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  66. unsigned ByteAlignment) {
  67. getAssembler().registerSymbol(*Symbol);
  68. Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() !=
  69. XCOFF::C_HIDEXT);
  70. Symbol->setCommon(Size, ByteAlignment);
  71. // Default csect align is 4, but common symbols have explicit alignment values
  72. // and we should honor it.
  73. cast<MCSymbolXCOFF>(Symbol)->getRepresentedCsect()->setAlignment(
  74. Align(ByteAlignment));
  75. // Emit the alignment and storage for the variable to the section.
  76. emitValueToAlignment(ByteAlignment);
  77. emitZeros(Size);
  78. }
  79. void MCXCOFFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
  80. uint64_t Size, unsigned ByteAlignment,
  81. SMLoc Loc) {
  82. report_fatal_error("Zero fill not implemented for XCOFF.");
  83. }
  84. void MCXCOFFStreamer::emitInstToData(const MCInst &Inst,
  85. const MCSubtargetInfo &STI) {
  86. MCAssembler &Assembler = getAssembler();
  87. SmallVector<MCFixup, 4> Fixups;
  88. SmallString<256> Code;
  89. raw_svector_ostream VecOS(Code);
  90. Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
  91. // Add the fixups and data.
  92. MCDataFragment *DF = getOrCreateDataFragment(&STI);
  93. const size_t ContentsSize = DF->getContents().size();
  94. auto &DataFragmentFixups = DF->getFixups();
  95. for (auto &Fixup : Fixups) {
  96. Fixup.setOffset(Fixup.getOffset() + ContentsSize);
  97. DataFragmentFixups.push_back(Fixup);
  98. }
  99. DF->setHasInstructions(STI);
  100. DF->getContents().append(Code.begin(), Code.end());
  101. }
  102. MCStreamer *llvm::createXCOFFStreamer(MCContext &Context,
  103. std::unique_ptr<MCAsmBackend> &&MAB,
  104. std::unique_ptr<MCObjectWriter> &&OW,
  105. std::unique_ptr<MCCodeEmitter> &&CE,
  106. bool RelaxAll) {
  107. MCXCOFFStreamer *S = new MCXCOFFStreamer(Context, std::move(MAB),
  108. std::move(OW), std::move(CE));
  109. if (RelaxAll)
  110. S->getAssembler().setRelaxAll(true);
  111. return S;
  112. }
  113. void MCXCOFFStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym,
  114. uint64_t Size,
  115. MCSymbol *CsectSym,
  116. unsigned ByteAlignment) {
  117. emitCommonSymbol(CsectSym, Size, ByteAlignment);
  118. }