MCSectionELF.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //===- lib/MC/MCSectionELF.cpp - ELF 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/MCSectionELF.h"
  9. #include "llvm/ADT/Triple.h"
  10. #include "llvm/BinaryFormat/ELF.h"
  11. #include "llvm/MC/MCAsmInfo.h"
  12. #include "llvm/MC/MCExpr.h"
  13. #include "llvm/Support/ErrorHandling.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include <cassert>
  16. using namespace llvm;
  17. // Decides whether a '.section' directive
  18. // should be printed before the section name.
  19. bool MCSectionELF::shouldOmitSectionDirective(StringRef Name,
  20. const MCAsmInfo &MAI) const {
  21. if (isUnique())
  22. return false;
  23. return MAI.shouldOmitSectionDirective(Name);
  24. }
  25. static void printName(raw_ostream &OS, StringRef Name) {
  26. if (Name.find_first_not_of("0123456789_."
  27. "abcdefghijklmnopqrstuvwxyz"
  28. "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
  29. OS << Name;
  30. return;
  31. }
  32. OS << '"';
  33. for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
  34. if (*B == '"') // Unquoted "
  35. OS << "\\\"";
  36. else if (*B != '\\') // Neither " or backslash
  37. OS << *B;
  38. else if (B + 1 == E) // Trailing backslash
  39. OS << "\\\\";
  40. else {
  41. OS << B[0] << B[1]; // Quoted character
  42. ++B;
  43. }
  44. }
  45. OS << '"';
  46. }
  47. void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
  48. raw_ostream &OS,
  49. const MCExpr *Subsection) const {
  50. if (shouldOmitSectionDirective(getName(), MAI)) {
  51. OS << '\t' << getName();
  52. if (Subsection) {
  53. OS << '\t';
  54. Subsection->print(OS, &MAI);
  55. }
  56. OS << '\n';
  57. return;
  58. }
  59. OS << "\t.section\t";
  60. printName(OS, getName());
  61. // Handle the weird solaris syntax if desired.
  62. if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
  63. !(Flags & ELF::SHF_MERGE)) {
  64. if (Flags & ELF::SHF_ALLOC)
  65. OS << ",#alloc";
  66. if (Flags & ELF::SHF_EXECINSTR)
  67. OS << ",#execinstr";
  68. if (Flags & ELF::SHF_WRITE)
  69. OS << ",#write";
  70. if (Flags & ELF::SHF_EXCLUDE)
  71. OS << ",#exclude";
  72. if (Flags & ELF::SHF_TLS)
  73. OS << ",#tls";
  74. OS << '\n';
  75. return;
  76. }
  77. OS << ",\"";
  78. if (Flags & ELF::SHF_ALLOC)
  79. OS << 'a';
  80. if (Flags & ELF::SHF_EXCLUDE)
  81. OS << 'e';
  82. if (Flags & ELF::SHF_EXECINSTR)
  83. OS << 'x';
  84. if (Flags & ELF::SHF_GROUP)
  85. OS << 'G';
  86. if (Flags & ELF::SHF_WRITE)
  87. OS << 'w';
  88. if (Flags & ELF::SHF_MERGE)
  89. OS << 'M';
  90. if (Flags & ELF::SHF_STRINGS)
  91. OS << 'S';
  92. if (Flags & ELF::SHF_TLS)
  93. OS << 'T';
  94. if (Flags & ELF::SHF_LINK_ORDER)
  95. OS << 'o';
  96. if (Flags & ELF::SHF_GNU_RETAIN)
  97. OS << 'R';
  98. // If there are os-specific flags, print them.
  99. if (T.isOSSolaris())
  100. if (Flags & ELF::SHF_SUNW_NODISCARD)
  101. OS << 'R';
  102. // If there are target-specific flags, print them.
  103. Triple::ArchType Arch = T.getArch();
  104. if (Arch == Triple::xcore) {
  105. if (Flags & ELF::XCORE_SHF_CP_SECTION)
  106. OS << 'c';
  107. if (Flags & ELF::XCORE_SHF_DP_SECTION)
  108. OS << 'd';
  109. } else if (T.isARM() || T.isThumb()) {
  110. if (Flags & ELF::SHF_ARM_PURECODE)
  111. OS << 'y';
  112. } else if (Arch == Triple::hexagon) {
  113. if (Flags & ELF::SHF_HEX_GPREL)
  114. OS << 's';
  115. }
  116. OS << '"';
  117. OS << ',';
  118. // If comment string is '@', e.g. as on ARM - use '%' instead
  119. if (MAI.getCommentString()[0] == '@')
  120. OS << '%';
  121. else
  122. OS << '@';
  123. if (Type == ELF::SHT_INIT_ARRAY)
  124. OS << "init_array";
  125. else if (Type == ELF::SHT_FINI_ARRAY)
  126. OS << "fini_array";
  127. else if (Type == ELF::SHT_PREINIT_ARRAY)
  128. OS << "preinit_array";
  129. else if (Type == ELF::SHT_NOBITS)
  130. OS << "nobits";
  131. else if (Type == ELF::SHT_NOTE)
  132. OS << "note";
  133. else if (Type == ELF::SHT_PROGBITS)
  134. OS << "progbits";
  135. else if (Type == ELF::SHT_X86_64_UNWIND)
  136. OS << "unwind";
  137. else if (Type == ELF::SHT_MIPS_DWARF)
  138. // Print hex value of the flag while we do not have
  139. // any standard symbolic representation of the flag.
  140. OS << "0x7000001e";
  141. else if (Type == ELF::SHT_LLVM_ODRTAB)
  142. OS << "llvm_odrtab";
  143. else if (Type == ELF::SHT_LLVM_LINKER_OPTIONS)
  144. OS << "llvm_linker_options";
  145. else if (Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
  146. OS << "llvm_call_graph_profile";
  147. else if (Type == ELF::SHT_LLVM_DEPENDENT_LIBRARIES)
  148. OS << "llvm_dependent_libraries";
  149. else if (Type == ELF::SHT_LLVM_SYMPART)
  150. OS << "llvm_sympart";
  151. else if (Type == ELF::SHT_LLVM_BB_ADDR_MAP)
  152. OS << "llvm_bb_addr_map";
  153. else if (Type == ELF::SHT_LLVM_BB_ADDR_MAP_V0)
  154. OS << "llvm_bb_addr_map_v0";
  155. else if (Type == ELF::SHT_LLVM_OFFLOADING)
  156. OS << "llvm_offloading";
  157. else
  158. report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
  159. " for section " + getName());
  160. if (EntrySize) {
  161. assert(Flags & ELF::SHF_MERGE);
  162. OS << "," << EntrySize;
  163. }
  164. if (Flags & ELF::SHF_GROUP) {
  165. OS << ",";
  166. printName(OS, Group.getPointer()->getName());
  167. if (isComdat())
  168. OS << ",comdat";
  169. }
  170. if (Flags & ELF::SHF_LINK_ORDER) {
  171. OS << ",";
  172. if (LinkedToSym)
  173. printName(OS, LinkedToSym->getName());
  174. else
  175. OS << '0';
  176. }
  177. if (isUnique())
  178. OS << ",unique," << UniqueID;
  179. OS << '\n';
  180. if (Subsection) {
  181. OS << "\t.subsection\t";
  182. Subsection->print(OS, &MAI);
  183. OS << '\n';
  184. }
  185. }
  186. bool MCSectionELF::useCodeAlign() const {
  187. return getFlags() & ELF::SHF_EXECINSTR;
  188. }
  189. bool MCSectionELF::isVirtualSection() const {
  190. return getType() == ELF::SHT_NOBITS;
  191. }
  192. StringRef MCSectionELF::getVirtualSectionKind() const { return "SHT_NOBITS"; }