MCSectionCOFF.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //===- lib/MC/MCSectionCOFF.cpp - COFF Code Section Representation --------===//
  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. #include "llvm/MC/MCSectionCOFF.h"
  9. #include "llvm/BinaryFormat/COFF.h"
  10. #include "llvm/MC/MCSymbol.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include <cassert>
  13. using namespace llvm;
  14. // shouldOmitSectionDirective - Decides whether a '.section' directive
  15. // should be printed before the section name
  16. bool MCSectionCOFF::shouldOmitSectionDirective(StringRef Name,
  17. const MCAsmInfo &MAI) const {
  18. if (COMDATSymbol)
  19. return false;
  20. // FIXME: Does .section .bss/.data/.text work everywhere??
  21. if (Name == ".text" || Name == ".data" || Name == ".bss")
  22. return true;
  23. return false;
  24. }
  25. void MCSectionCOFF::setSelection(int Selection) const {
  26. assert(Selection != 0 && "invalid COMDAT selection type");
  27. this->Selection = Selection;
  28. Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
  29. }
  30. void MCSectionCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
  31. raw_ostream &OS,
  32. const MCExpr *Subsection) const {
  33. // standard sections don't require the '.section'
  34. if (shouldOmitSectionDirective(getName(), MAI)) {
  35. OS << '\t' << getName() << '\n';
  36. return;
  37. }
  38. OS << "\t.section\t" << getName() << ",\"";
  39. if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
  40. OS << 'd';
  41. if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
  42. OS << 'b';
  43. if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE)
  44. OS << 'x';
  45. if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE)
  46. OS << 'w';
  47. else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ)
  48. OS << 'r';
  49. else
  50. OS << 'y';
  51. if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE)
  52. OS << 'n';
  53. if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
  54. OS << 's';
  55. if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) &&
  56. !isImplicitlyDiscardable(getName()))
  57. OS << 'D';
  58. if (getCharacteristics() & COFF::IMAGE_SCN_LNK_INFO)
  59. OS << 'i';
  60. OS << '"';
  61. if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
  62. if (COMDATSymbol)
  63. OS << ",";
  64. else
  65. OS << "\n\t.linkonce\t";
  66. switch (Selection) {
  67. case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
  68. OS << "one_only";
  69. break;
  70. case COFF::IMAGE_COMDAT_SELECT_ANY:
  71. OS << "discard";
  72. break;
  73. case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
  74. OS << "same_size";
  75. break;
  76. case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
  77. OS << "same_contents";
  78. break;
  79. case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
  80. OS << "associative";
  81. break;
  82. case COFF::IMAGE_COMDAT_SELECT_LARGEST:
  83. OS << "largest";
  84. break;
  85. case COFF::IMAGE_COMDAT_SELECT_NEWEST:
  86. OS << "newest";
  87. break;
  88. default:
  89. assert(false && "unsupported COFF selection type");
  90. break;
  91. }
  92. if (COMDATSymbol) {
  93. OS << ",";
  94. COMDATSymbol->print(OS, &MAI);
  95. }
  96. }
  97. OS << '\n';
  98. }
  99. bool MCSectionCOFF::useCodeAlign() const { return getKind().isText(); }
  100. bool MCSectionCOFF::isVirtualSection() const {
  101. return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
  102. }
  103. StringRef MCSectionCOFF::getVirtualSectionKind() const {
  104. return "IMAGE_SCN_CNT_UNINITIALIZED_DATA";
  105. }