MCSectionMachO.cpp 11 KB

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