MCSectionXCOFF.cpp 4.3 KB

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