MCSectionXCOFF.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===- lib/MC/MCSectionXCOFF.cpp - XCOFF 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/MCSectionXCOFF.h"
  9. #include "llvm/MC/MCAsmInfo.h"
  10. #include "llvm/MC/MCExpr.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "llvm/Support/Format.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. using namespace llvm;
  15. MCSectionXCOFF::~MCSectionXCOFF() = default;
  16. void MCSectionXCOFF::printCsectDirective(raw_ostream &OS) const {
  17. OS << "\t.csect " << QualName->getName() << "," << Log2_32(getAlignment())
  18. << '\n';
  19. }
  20. void MCSectionXCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
  21. raw_ostream &OS,
  22. const MCExpr *Subsection) const {
  23. if (getKind().isText()) {
  24. if (getMappingClass() != XCOFF::XMC_PR)
  25. report_fatal_error("Unhandled storage-mapping class for .text csect");
  26. printCsectDirective(OS);
  27. return;
  28. }
  29. if (getKind().isReadOnly()) {
  30. if (getMappingClass() != XCOFF::XMC_RO &&
  31. getMappingClass() != XCOFF::XMC_TD)
  32. report_fatal_error("Unhandled storage-mapping class for .rodata csect.");
  33. printCsectDirective(OS);
  34. return;
  35. }
  36. // Initialized TLS data.
  37. if (getKind().isThreadData()) {
  38. // We only expect XMC_TL here for initialized TLS data.
  39. if (getMappingClass() != XCOFF::XMC_TL)
  40. report_fatal_error("Unhandled storage-mapping class for .tdata csect.");
  41. printCsectDirective(OS);
  42. return;
  43. }
  44. if (getKind().isData()) {
  45. switch (getMappingClass()) {
  46. case XCOFF::XMC_RW:
  47. case XCOFF::XMC_DS:
  48. case XCOFF::XMC_TD:
  49. printCsectDirective(OS);
  50. break;
  51. case XCOFF::XMC_TC:
  52. case XCOFF::XMC_TE:
  53. break;
  54. case XCOFF::XMC_TC0:
  55. OS << "\t.toc\n";
  56. break;
  57. default:
  58. report_fatal_error(
  59. "Unhandled storage-mapping class for .data csect.");
  60. }
  61. return;
  62. }
  63. if (isCsect() && getMappingClass() == XCOFF::XMC_TD) {
  64. assert((getKind().isBSSExtern() || getKind().isBSSLocal() ||
  65. getKind().isReadOnlyWithRel()) &&
  66. "Unexepected section kind for toc-data");
  67. printCsectDirective(OS);
  68. return;
  69. }
  70. // Common csect type (uninitialized storage) does not have to print csect
  71. // directive for section switching.
  72. if (isCsect() && getCSectType() == XCOFF::XTY_CM) {
  73. assert((getMappingClass() == XCOFF::XMC_RW ||
  74. getMappingClass() == XCOFF::XMC_BS ||
  75. getMappingClass() == XCOFF::XMC_UL) &&
  76. "Generated a storage-mapping class for a common/bss/tbss csect we "
  77. "don't "
  78. "understand how to switch to.");
  79. // Common symbols and local zero-initialized symbols for TLS and Non-TLS are
  80. // eligible for .bss/.tbss csect, getKind().isThreadBSS() is used to cover
  81. // TLS common and zero-initialized local symbols since linkage type (in the
  82. // GlobalVariable) is not accessible in this class.
  83. assert((getKind().isBSSLocal() || getKind().isCommon() ||
  84. getKind().isThreadBSS()) &&
  85. "wrong symbol type for .bss/.tbss csect");
  86. // Don't have to print a directive for switching to section for commons and
  87. // zero-initialized TLS data. The '.comm' and '.lcomm' directives of the
  88. // variable will create the needed csect.
  89. return;
  90. }
  91. // Zero-initialized TLS data with weak or external linkage are not eligible to
  92. // be put into common csect.
  93. if (getKind().isThreadBSS()) {
  94. printCsectDirective(OS);
  95. return;
  96. }
  97. // XCOFF debug sections.
  98. if (getKind().isMetadata() && isDwarfSect()) {
  99. OS << "\n\t.dwsect "
  100. << format("0x%" PRIx32, getDwarfSubtypeFlags().getValue()) << '\n';
  101. OS << MAI.getPrivateLabelPrefix() << getName() << ':' << '\n';
  102. return;
  103. }
  104. report_fatal_error("Printing for this SectionKind is unimplemented.");
  105. }
  106. bool MCSectionXCOFF::UseCodeAlign() const { return getKind().isText(); }
  107. bool MCSectionXCOFF::isVirtualSection() const {
  108. // DWARF sections are always not virtual.
  109. if (isDwarfSect())
  110. return false;
  111. assert(isCsect() &&
  112. "Handling for isVirtualSection not implemented for this section!");
  113. return XCOFF::XTY_CM == CsectProp->Type;
  114. }