MCSectionMachO.cpp 12 KB

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