MCSectionELF.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/MC/MCSectionELF.h"
  10. #include "llvm/MC/MCAsmInfo.h"
  11. #include "llvm/MC/MCContext.h"
  12. #include "llvm/MC/MCExpr.h"
  13. #include "llvm/MC/MCSymbol.h"
  14. #include "llvm/Support/ELF.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. using namespace llvm;
  17. MCSectionELF::~MCSectionELF() {} // anchor.
  18. // Decides whether a '.section' directive
  19. // should be printed before the section name.
  20. bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
  21. const MCAsmInfo &MAI) const {
  22. if (isUnique())
  23. return false;
  24. // FIXME: Does .section .bss/.data/.text work everywhere??
  25. if (Name == ".text" || Name == ".data" ||
  26. (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
  27. return true;
  28. return false;
  29. }
  30. static void printName(raw_ostream &OS, StringRef Name) {
  31. if (Name.find_first_not_of("0123456789_."
  32. "abcdefghijklmnopqrstuvwxyz"
  33. "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
  34. OS << Name;
  35. return;
  36. }
  37. OS << '"';
  38. for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
  39. if (*B == '"') // Unquoted "
  40. OS << "\\\"";
  41. else if (*B != '\\') // Neither " or backslash
  42. OS << *B;
  43. else if (B + 1 == E) // Trailing backslash
  44. OS << "\\\\";
  45. else {
  46. OS << B[0] << B[1]; // Quoted character
  47. ++B;
  48. }
  49. }
  50. OS << '"';
  51. }
  52. void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
  53. raw_ostream &OS,
  54. const MCExpr *Subsection) const {
  55. if (ShouldOmitSectionDirective(SectionName, MAI)) {
  56. OS << '\t' << getSectionName();
  57. if (Subsection) {
  58. OS << '\t';
  59. Subsection->print(OS, &MAI);
  60. }
  61. OS << '\n';
  62. return;
  63. }
  64. OS << "\t.section\t";
  65. printName(OS, getSectionName());
  66. // Handle the weird solaris syntax if desired.
  67. if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
  68. !(Flags & ELF::SHF_MERGE)) {
  69. if (Flags & ELF::SHF_ALLOC)
  70. OS << ",#alloc";
  71. if (Flags & ELF::SHF_EXECINSTR)
  72. OS << ",#execinstr";
  73. if (Flags & ELF::SHF_WRITE)
  74. OS << ",#write";
  75. if (Flags & ELF::SHF_EXCLUDE)
  76. OS << ",#exclude";
  77. if (Flags & ELF::SHF_TLS)
  78. OS << ",#tls";
  79. OS << '\n';
  80. return;
  81. }
  82. OS << ",\"";
  83. if (Flags & ELF::SHF_ALLOC)
  84. OS << 'a';
  85. if (Flags & ELF::SHF_EXCLUDE)
  86. OS << 'e';
  87. if (Flags & ELF::SHF_EXECINSTR)
  88. OS << 'x';
  89. if (Flags & ELF::SHF_GROUP)
  90. OS << 'G';
  91. if (Flags & ELF::SHF_WRITE)
  92. OS << 'w';
  93. if (Flags & ELF::SHF_MERGE)
  94. OS << 'M';
  95. if (Flags & ELF::SHF_STRINGS)
  96. OS << 'S';
  97. if (Flags & ELF::SHF_TLS)
  98. OS << 'T';
  99. // If there are target-specific flags, print them.
  100. if (Flags & ELF::XCORE_SHF_CP_SECTION)
  101. OS << 'c';
  102. if (Flags & ELF::XCORE_SHF_DP_SECTION)
  103. OS << 'd';
  104. OS << '"';
  105. OS << ',';
  106. // If comment string is '@', e.g. as on ARM - use '%' instead
  107. if (MAI.getCommentString()[0] == '@')
  108. OS << '%';
  109. else
  110. OS << '@';
  111. if (Type == ELF::SHT_INIT_ARRAY)
  112. OS << "init_array";
  113. else if (Type == ELF::SHT_FINI_ARRAY)
  114. OS << "fini_array";
  115. else if (Type == ELF::SHT_PREINIT_ARRAY)
  116. OS << "preinit_array";
  117. else if (Type == ELF::SHT_NOBITS)
  118. OS << "nobits";
  119. else if (Type == ELF::SHT_NOTE)
  120. OS << "note";
  121. else if (Type == ELF::SHT_PROGBITS)
  122. OS << "progbits";
  123. if (EntrySize) {
  124. assert(Flags & ELF::SHF_MERGE);
  125. OS << "," << EntrySize;
  126. }
  127. if (Flags & ELF::SHF_GROUP) {
  128. OS << ",";
  129. printName(OS, Group->getName());
  130. OS << ",comdat";
  131. }
  132. if (isUnique())
  133. OS << ",unique," << UniqueID;
  134. OS << '\n';
  135. if (Subsection) {
  136. OS << "\t.subsection\t";
  137. Subsection->print(OS, &MAI);
  138. OS << '\n';
  139. }
  140. }
  141. bool MCSectionELF::UseCodeAlign() const {
  142. return getFlags() & ELF::SHF_EXECINSTR;
  143. }
  144. bool MCSectionELF::isVirtualSection() const {
  145. return getType() == ELF::SHT_NOBITS;
  146. }