MCSectionMachO.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //===- lib/MC/MCSectionMachO.cpp - MachO 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/MCSectionMachO.h"
  9. #include "llvm/MC/MCContext.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. #include <cctype>
  12. using namespace llvm;
  13. /// SectionTypeDescriptors - These are strings that describe the various section
  14. /// types. This *must* be kept in order with and stay synchronized with the
  15. /// section type list.
  16. static constexpr struct {
  17. StringLiteral AssemblerName, EnumName;
  18. } SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE + 1] = {
  19. {StringLiteral("regular"), StringLiteral("S_REGULAR")}, // 0x00
  20. {StringLiteral(""), StringLiteral("S_ZEROFILL")}, // 0x01
  21. {StringLiteral("cstring_literals"),
  22. StringLiteral("S_CSTRING_LITERALS")}, // 0x02
  23. {StringLiteral("4byte_literals"),
  24. StringLiteral("S_4BYTE_LITERALS")}, // 0x03
  25. {StringLiteral("8byte_literals"),
  26. StringLiteral("S_8BYTE_LITERALS")}, // 0x04
  27. {StringLiteral("literal_pointers"),
  28. StringLiteral("S_LITERAL_POINTERS")}, // 0x05
  29. {StringLiteral("non_lazy_symbol_pointers"),
  30. StringLiteral("S_NON_LAZY_SYMBOL_POINTERS")}, // 0x06
  31. {StringLiteral("lazy_symbol_pointers"),
  32. StringLiteral("S_LAZY_SYMBOL_POINTERS")}, // 0x07
  33. {StringLiteral("symbol_stubs"), StringLiteral("S_SYMBOL_STUBS")}, // 0x08
  34. {StringLiteral("mod_init_funcs"),
  35. StringLiteral("S_MOD_INIT_FUNC_POINTERS")}, // 0x09
  36. {StringLiteral("mod_term_funcs"),
  37. StringLiteral("S_MOD_TERM_FUNC_POINTERS")}, // 0x0A
  38. {StringLiteral("coalesced"), StringLiteral("S_COALESCED")}, // 0x0B
  39. {StringLiteral("") /*FIXME??*/, StringLiteral("S_GB_ZEROFILL")}, // 0x0C
  40. {StringLiteral("interposing"), StringLiteral("S_INTERPOSING")}, // 0x0D
  41. {StringLiteral("16byte_literals"),
  42. StringLiteral("S_16BYTE_LITERALS")}, // 0x0E
  43. {StringLiteral("") /*FIXME??*/, StringLiteral("S_DTRACE_DOF")}, // 0x0F
  44. {StringLiteral("") /*FIXME??*/,
  45. StringLiteral("S_LAZY_DYLIB_SYMBOL_POINTERS")}, // 0x10
  46. {StringLiteral("thread_local_regular"),
  47. StringLiteral("S_THREAD_LOCAL_REGULAR")}, // 0x11
  48. {StringLiteral("thread_local_zerofill"),
  49. StringLiteral("S_THREAD_LOCAL_ZEROFILL")}, // 0x12
  50. {StringLiteral("thread_local_variables"),
  51. StringLiteral("S_THREAD_LOCAL_VARIABLES")}, // 0x13
  52. {StringLiteral("thread_local_variable_pointers"),
  53. StringLiteral("S_THREAD_LOCAL_VARIABLE_POINTERS")}, // 0x14
  54. {StringLiteral("thread_local_init_function_pointers"),
  55. StringLiteral("S_THREAD_LOCAL_INIT_FUNCTION_POINTERS")}, // 0x15
  56. };
  57. /// SectionAttrDescriptors - This is an array of descriptors for section
  58. /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
  59. /// by attribute, instead it is searched.
  60. static constexpr struct {
  61. unsigned AttrFlag;
  62. StringLiteral AssemblerName, EnumName;
  63. } SectionAttrDescriptors[] = {
  64. #define ENTRY(ASMNAME, ENUM) \
  65. { MachO::ENUM, StringLiteral(ASMNAME), StringLiteral(#ENUM) },
  66. ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
  67. ENTRY("no_toc", S_ATTR_NO_TOC)
  68. ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
  69. ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
  70. ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
  71. ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
  72. ENTRY("debug", S_ATTR_DEBUG)
  73. ENTRY("" /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
  74. ENTRY("" /*FIXME*/, S_ATTR_EXT_RELOC)
  75. ENTRY("" /*FIXME*/, S_ATTR_LOC_RELOC)
  76. #undef ENTRY
  77. { 0, StringLiteral("none"), StringLiteral("") }, // used if section has no attributes but has a stub size
  78. };
  79. MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
  80. unsigned TAA, unsigned reserved2, SectionKind K,
  81. MCSymbol *Begin)
  82. : MCSection(SV_MachO, Section, K, Begin), TypeAndAttributes(TAA),
  83. Reserved2(reserved2) {
  84. assert(Segment.size() <= 16 && Section.size() <= 16 &&
  85. "Segment or section string too long");
  86. for (unsigned i = 0; i != 16; ++i) {
  87. if (i < Segment.size())
  88. SegmentName[i] = Segment[i];
  89. else
  90. SegmentName[i] = 0;
  91. }
  92. }
  93. void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
  94. raw_ostream &OS,
  95. const MCExpr *Subsection) const {
  96. OS << "\t.section\t" << getSegmentName() << ',' << getName();
  97. // Get the section type and attributes.
  98. unsigned TAA = getTypeAndAttributes();
  99. if (TAA == 0) {
  100. OS << '\n';
  101. return;
  102. }
  103. MachO::SectionType SectionType = getType();
  104. assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE &&
  105. "Invalid SectionType specified!");
  106. if (!SectionTypeDescriptors[SectionType].AssemblerName.empty()) {
  107. OS << ',';
  108. OS << SectionTypeDescriptors[SectionType].AssemblerName;
  109. } else {
  110. // If we have no name for the attribute, stop here.
  111. OS << '\n';
  112. return;
  113. }
  114. // If we don't have any attributes, we're done.
  115. unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES;
  116. if (SectionAttrs == 0) {
  117. // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
  118. // the attribute specifier.
  119. if (Reserved2 != 0)
  120. OS << ",none," << Reserved2;
  121. OS << '\n';
  122. return;
  123. }
  124. // Check each attribute to see if we have it.
  125. char Separator = ',';
  126. for (unsigned i = 0;
  127. SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
  128. ++i) {
  129. // Check to see if we have this attribute.
  130. if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
  131. continue;
  132. // Yep, clear it and print it.
  133. SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
  134. OS << Separator;
  135. if (!SectionAttrDescriptors[i].AssemblerName.empty())
  136. OS << SectionAttrDescriptors[i].AssemblerName;
  137. else
  138. OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
  139. Separator = '+';
  140. }
  141. assert(SectionAttrs == 0 && "Unknown section attributes!");
  142. // If we have a S_SYMBOL_STUBS size specified, print it.
  143. if (Reserved2 != 0)
  144. OS << ',' << Reserved2;
  145. OS << '\n';
  146. }
  147. bool MCSectionMachO::UseCodeAlign() const {
  148. return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS);
  149. }
  150. bool MCSectionMachO::isVirtualSection() const {
  151. return (getType() == MachO::S_ZEROFILL ||
  152. getType() == MachO::S_GB_ZEROFILL ||
  153. getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
  154. }
  155. /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
  156. /// This is a string that can appear after a .section directive in a mach-o
  157. /// flavored .s file. If successful, this fills in the specified Out
  158. /// parameters and returns an empty string. When an invalid section
  159. /// specifier is present, this returns a string indicating the problem.
  160. Error MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
  161. StringRef &Segment, // Out.
  162. StringRef &Section, // Out.
  163. unsigned &TAA, // Out.
  164. bool &TAAParsed, // Out.
  165. unsigned &StubSize) { // Out.
  166. TAAParsed = false;
  167. SmallVector<StringRef, 5> SplitSpec;
  168. Spec.split(SplitSpec, ',');
  169. // Remove leading and trailing whitespace.
  170. auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef {
  171. return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef();
  172. };
  173. Segment = GetEmptyOrTrim(0);
  174. Section = GetEmptyOrTrim(1);
  175. StringRef SectionType = GetEmptyOrTrim(2);
  176. StringRef Attrs = GetEmptyOrTrim(3);
  177. StringRef StubSizeStr = GetEmptyOrTrim(4);
  178. // Verify that the section is present.
  179. if (Section.empty())
  180. return createStringError(inconvertibleErrorCode(),
  181. "mach-o section specifier requires a segment "
  182. "and section separated by a comma");
  183. // Verify that the section is not too long.
  184. if (Section.size() > 16)
  185. return createStringError(inconvertibleErrorCode(),
  186. "mach-o section specifier requires a section "
  187. "whose length is between 1 and 16 characters");
  188. // If there is no comma after the section, we're done.
  189. TAA = 0;
  190. StubSize = 0;
  191. if (SectionType.empty())
  192. return Error::success();
  193. // Figure out which section type it is.
  194. auto TypeDescriptor =
  195. llvm::find_if(SectionTypeDescriptors,
  196. [&](decltype(*SectionTypeDescriptors) &Descriptor) {
  197. return SectionType == Descriptor.AssemblerName;
  198. });
  199. // If we didn't find the section type, reject it.
  200. if (TypeDescriptor == std::end(SectionTypeDescriptors))
  201. return createStringError(inconvertibleErrorCode(),
  202. "mach-o section specifier uses an unknown "
  203. "section type");
  204. // Remember the TypeID.
  205. TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
  206. TAAParsed = true;
  207. // If we have no comma after the section type, there are no attributes.
  208. if (Attrs.empty()) {
  209. // S_SYMBOL_STUBS always require a symbol stub size specifier.
  210. if (TAA == MachO::S_SYMBOL_STUBS)
  211. return createStringError(inconvertibleErrorCode(),
  212. "mach-o section specifier of type "
  213. "'symbol_stubs' requires a size specifier");
  214. return Error::success();
  215. }
  216. // The attribute list is a '+' separated list of attributes.
  217. SmallVector<StringRef, 1> SectionAttrs;
  218. Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
  219. for (StringRef &SectionAttr : SectionAttrs) {
  220. auto AttrDescriptorI =
  221. llvm::find_if(SectionAttrDescriptors,
  222. [&](decltype(*SectionAttrDescriptors) &Descriptor) {
  223. return SectionAttr.trim() == Descriptor.AssemblerName;
  224. });
  225. if (AttrDescriptorI == std::end(SectionAttrDescriptors))
  226. return createStringError(inconvertibleErrorCode(),
  227. "mach-o section specifier has invalid "
  228. "attribute");
  229. TAA |= AttrDescriptorI->AttrFlag;
  230. }
  231. // Okay, we've parsed the section attributes, see if we have a stub size spec.
  232. if (StubSizeStr.empty()) {
  233. // S_SYMBOL_STUBS always require a symbol stub size specifier.
  234. if (TAA == MachO::S_SYMBOL_STUBS)
  235. return createStringError(inconvertibleErrorCode(),
  236. "mach-o section specifier of type "
  237. "'symbol_stubs' requires a size specifier");
  238. return Error::success();
  239. }
  240. // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
  241. if ((TAA & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS)
  242. return createStringError(inconvertibleErrorCode(),
  243. "mach-o section specifier cannot have a stub "
  244. "size specified because it does not have type "
  245. "'symbol_stubs'");
  246. // Convert the stub size from a string to an integer.
  247. if (StubSizeStr.getAsInteger(0, StubSize))
  248. return createStringError(inconvertibleErrorCode(),
  249. "mach-o section specifier has a malformed "
  250. "stub size");
  251. return Error::success();
  252. }