blob: 879c6e5ff9323d0964b97108c3973a91d892419e [file] [log] [blame]
Chris Lattner6c203912009-08-10 18:15:01 +00001//===- 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
10#include "llvm/MC/MCSectionMachO.h"
11#include "llvm/MC/MCContext.h"
12#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000013#include <cctype>
Chris Lattner6c203912009-08-10 18:15:01 +000014using namespace llvm;
15
16/// SectionTypeDescriptors - These are strings that describe the various section
17/// types. This *must* be kept in order with and stay synchronized with the
18/// section type list.
19static const struct {
20 const char *AssemblerName, *EnumName;
David Majnemer7b583052014-03-07 07:36:05 +000021} SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE+1] = {
Chris Lattner6c203912009-08-10 18:15:01 +000022 { "regular", "S_REGULAR" }, // 0x00
Craig Topperbb694de2014-04-13 04:57:38 +000023 { nullptr, "S_ZEROFILL" }, // 0x01
Chris Lattner6c203912009-08-10 18:15:01 +000024 { "cstring_literals", "S_CSTRING_LITERALS" }, // 0x02
25 { "4byte_literals", "S_4BYTE_LITERALS" }, // 0x03
26 { "8byte_literals", "S_8BYTE_LITERALS" }, // 0x04
27 { "literal_pointers", "S_LITERAL_POINTERS" }, // 0x05
28 { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" }, // 0x06
29 { "lazy_symbol_pointers", "S_LAZY_SYMBOL_POINTERS" }, // 0x07
30 { "symbol_stubs", "S_SYMBOL_STUBS" }, // 0x08
31 { "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, // 0x09
32 { "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, // 0x0A
33 { "coalesced", "S_COALESCED" }, // 0x0B
Craig Topperbb694de2014-04-13 04:57:38 +000034 { nullptr, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C
Chris Lattner6c203912009-08-10 18:15:01 +000035 { "interposing", "S_INTERPOSING" }, // 0x0D
36 { "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E
Craig Topperbb694de2014-04-13 04:57:38 +000037 { nullptr, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F
38 { nullptr, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" }, // 0x10
Eric Christopherbf792382010-05-17 21:02:07 +000039 { "thread_local_regular", "S_THREAD_LOCAL_REGULAR" }, // 0x11
Eric Christopher3dca28d2010-05-21 21:08:52 +000040 { "thread_local_zerofill", "S_THREAD_LOCAL_ZEROFILL" }, // 0x12
Eric Christopherbf792382010-05-17 21:02:07 +000041 { "thread_local_variables", "S_THREAD_LOCAL_VARIABLES" }, // 0x13
42 { "thread_local_variable_pointers",
43 "S_THREAD_LOCAL_VARIABLE_POINTERS" }, // 0x14
44 { "thread_local_init_function_pointers",
45 "S_THREAD_LOCAL_INIT_FUNCTION_POINTERS"}, // 0x15
Chris Lattner6c203912009-08-10 18:15:01 +000046};
47
48
49/// SectionAttrDescriptors - This is an array of descriptors for section
50/// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
David Majnemere500ec12014-03-10 00:55:07 +000051/// by attribute, instead it is searched.
Chris Lattner6c203912009-08-10 18:15:01 +000052static const struct {
53 unsigned AttrFlag;
54 const char *AssemblerName, *EnumName;
55} SectionAttrDescriptors[] = {
56#define ENTRY(ASMNAME, ENUM) \
David Majnemer7b583052014-03-07 07:36:05 +000057 { MachO::ENUM, ASMNAME, #ENUM },
Chris Lattner6c203912009-08-10 18:15:01 +000058ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
59ENTRY("no_toc", S_ATTR_NO_TOC)
60ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
61ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
62ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
63ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
64ENTRY("debug", S_ATTR_DEBUG)
Craig Topperbb694de2014-04-13 04:57:38 +000065ENTRY(nullptr /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
66ENTRY(nullptr /*FIXME*/, S_ATTR_EXT_RELOC)
67ENTRY(nullptr /*FIXME*/, S_ATTR_LOC_RELOC)
Chris Lattner6c203912009-08-10 18:15:01 +000068#undef ENTRY
Craig Topperbb694de2014-04-13 04:57:38 +000069 { 0, "none", nullptr }, // used if section has no attributes but has a stub size
Chris Lattner6c203912009-08-10 18:15:01 +000070};
71
Chris Lattner5418dd52010-04-08 21:26:26 +000072MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
Rafael Espindola6b9998b2015-03-10 22:00:25 +000073 unsigned TAA, unsigned reserved2, SectionKind K,
74 MCSymbol *Begin)
Rafael Espindola8ca44f02015-04-04 18:02:01 +000075 : MCSection(SV_MachO, K, Begin), TypeAndAttributes(TAA),
Rafael Espindola6b9998b2015-03-10 22:00:25 +000076 Reserved2(reserved2) {
Chris Lattner5418dd52010-04-08 21:26:26 +000077 assert(Segment.size() <= 16 && Section.size() <= 16 &&
78 "Segment or section string too long");
79 for (unsigned i = 0; i != 16; ++i) {
80 if (i < Segment.size())
81 SegmentName[i] = Segment[i];
82 else
83 SegmentName[i] = 0;
Jim Grosbach90600d32010-10-21 22:04:05 +000084
Chris Lattner5418dd52010-04-08 21:26:26 +000085 if (i < Section.size())
86 SectionName[i] = Section[i];
87 else
88 SectionName[i] = 0;
Jim Grosbach90600d32010-10-21 22:04:05 +000089 }
Chris Lattner6c203912009-08-10 18:15:01 +000090}
91
Chris Lattnere9a75a62009-08-22 21:43:10 +000092void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
Peter Collingbourne2f495b92013-04-17 21:18:16 +000093 raw_ostream &OS,
94 const MCExpr *Subsection) const {
Chris Lattner6c203912009-08-10 18:15:01 +000095 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
Jim Grosbach90600d32010-10-21 22:04:05 +000096
Chris Lattner6c203912009-08-10 18:15:01 +000097 // Get the section type and attributes.
98 unsigned TAA = getTypeAndAttributes();
99 if (TAA == 0) {
100 OS << '\n';
101 return;
102 }
103
David Majnemercd481d32014-03-07 18:49:54 +0000104 MachO::SectionType SectionType = getType();
David Majnemer7b583052014-03-07 07:36:05 +0000105 assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE &&
Chris Lattner6c203912009-08-10 18:15:01 +0000106 "Invalid SectionType specified!");
107
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000108 if (SectionTypeDescriptors[SectionType].AssemblerName) {
109 OS << ',';
Chris Lattner6c203912009-08-10 18:15:01 +0000110 OS << SectionTypeDescriptors[SectionType].AssemblerName;
Stuart Hastingsd7927e02011-02-21 21:07:07 +0000111 } else {
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000112 // If we have no name for the attribute, stop here.
Stuart Hastingsd7927e02011-02-21 21:07:07 +0000113 OS << '\n';
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000114 return;
Stuart Hastingsd7927e02011-02-21 21:07:07 +0000115 }
Jim Grosbach90600d32010-10-21 22:04:05 +0000116
Chris Lattner6c203912009-08-10 18:15:01 +0000117 // If we don't have any attributes, we're done.
David Majnemer7b583052014-03-07 07:36:05 +0000118 unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES;
Chris Lattner6c203912009-08-10 18:15:01 +0000119 if (SectionAttrs == 0) {
120 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
121 // the attribute specifier.
122 if (Reserved2 != 0)
123 OS << ",none," << Reserved2;
124 OS << '\n';
125 return;
126 }
127
128 // Check each attribute to see if we have it.
129 char Separator = ',';
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000130 for (unsigned i = 0;
131 SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
132 ++i) {
Chris Lattner6c203912009-08-10 18:15:01 +0000133 // Check to see if we have this attribute.
134 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
135 continue;
Jim Grosbach90600d32010-10-21 22:04:05 +0000136
Chris Lattner6c203912009-08-10 18:15:01 +0000137 // Yep, clear it and print it.
138 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
Jim Grosbach90600d32010-10-21 22:04:05 +0000139
Chris Lattner6c203912009-08-10 18:15:01 +0000140 OS << Separator;
141 if (SectionAttrDescriptors[i].AssemblerName)
142 OS << SectionAttrDescriptors[i].AssemblerName;
143 else
144 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
145 Separator = '+';
146 }
Jim Grosbach90600d32010-10-21 22:04:05 +0000147
Chris Lattner6c203912009-08-10 18:15:01 +0000148 assert(SectionAttrs == 0 && "Unknown section attributes!");
Jim Grosbach90600d32010-10-21 22:04:05 +0000149
Chris Lattner6c203912009-08-10 18:15:01 +0000150 // If we have a S_SYMBOL_STUBS size specified, print it.
151 if (Reserved2 != 0)
152 OS << ',' << Reserved2;
153 OS << '\n';
154}
155
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000156bool MCSectionMachO::UseCodeAlign() const {
David Majnemer7b583052014-03-07 07:36:05 +0000157 return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS);
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000158}
159
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000160bool MCSectionMachO::isVirtualSection() const {
David Majnemer7b583052014-03-07 07:36:05 +0000161 return (getType() == MachO::S_ZEROFILL ||
162 getType() == MachO::S_GB_ZEROFILL ||
163 getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000164}
165
Chris Lattner6c203912009-08-10 18:15:01 +0000166/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
167/// This is a string that can appear after a .section directive in a mach-o
168/// flavored .s file. If successful, this fills in the specified Out
169/// parameters and returns an empty string. When an invalid section
170/// specifier is present, this returns a string indicating the problem.
171std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
172 StringRef &Segment, // Out.
173 StringRef &Section, // Out.
174 unsigned &TAA, // Out.
Stuart Hastings12d53122011-03-19 02:42:31 +0000175 bool &TAAParsed, // Out.
Chris Lattner6c203912009-08-10 18:15:01 +0000176 unsigned &StubSize) { // Out.
Stuart Hastings12d53122011-03-19 02:42:31 +0000177 TAAParsed = false;
Jim Grosbach90600d32010-10-21 22:04:05 +0000178
David Majnemere500ec12014-03-10 00:55:07 +0000179 SmallVector<StringRef, 5> SplitSpec;
Chandler Carruthe4405e92015-09-10 06:12:31 +0000180 Spec.split(SplitSpec, ',');
David Majnemere500ec12014-03-10 00:55:07 +0000181 // Remove leading and trailing whitespace.
182 auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef {
183 return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef();
184 };
185 Segment = GetEmptyOrTrim(0);
186 Section = GetEmptyOrTrim(1);
187 StringRef SectionType = GetEmptyOrTrim(2);
188 StringRef Attrs = GetEmptyOrTrim(3);
189 StringRef StubSizeStr = GetEmptyOrTrim(4);
Chris Lattner6c203912009-08-10 18:15:01 +0000190
191 // Verify that the segment is present and not too long.
192 if (Segment.empty() || Segment.size() > 16)
193 return "mach-o section specifier requires a segment whose length is "
194 "between 1 and 16 characters";
Jim Grosbach90600d32010-10-21 22:04:05 +0000195
Chris Lattner6c203912009-08-10 18:15:01 +0000196 // Verify that the section is present and not too long.
David Majnemere500ec12014-03-10 00:55:07 +0000197 if (Section.empty())
198 return "mach-o section specifier requires a segment and section "
199 "separated by a comma";
200
201 if (Section.size() > 16)
Chris Lattner6c203912009-08-10 18:15:01 +0000202 return "mach-o section specifier requires a section whose length is "
203 "between 1 and 16 characters";
204
205 // If there is no comma after the section, we're done.
Stuart Hastings12d53122011-03-19 02:42:31 +0000206 TAA = 0;
Chris Lattner6c203912009-08-10 18:15:01 +0000207 StubSize = 0;
David Majnemere500ec12014-03-10 00:55:07 +0000208 if (SectionType.empty())
Chris Lattner6c203912009-08-10 18:15:01 +0000209 return "";
Jim Grosbach90600d32010-10-21 22:04:05 +0000210
Chris Lattner6c203912009-08-10 18:15:01 +0000211 // Figure out which section type it is.
David Majnemere500ec12014-03-10 00:55:07 +0000212 auto TypeDescriptor = std::find_if(
213 std::begin(SectionTypeDescriptors), std::end(SectionTypeDescriptors),
David Majnemer596587f2014-03-10 01:04:18 +0000214 [&](decltype(*SectionTypeDescriptors) &Descriptor) {
David Majnemere500ec12014-03-10 00:55:07 +0000215 return Descriptor.AssemblerName &&
216 SectionType == Descriptor.AssemblerName;
217 });
Jim Grosbach90600d32010-10-21 22:04:05 +0000218
Chris Lattner6c203912009-08-10 18:15:01 +0000219 // If we didn't find the section type, reject it.
David Majnemere500ec12014-03-10 00:55:07 +0000220 if (TypeDescriptor == std::end(SectionTypeDescriptors))
Chris Lattner6c203912009-08-10 18:15:01 +0000221 return "mach-o section specifier uses an unknown section type";
Jim Grosbach90600d32010-10-21 22:04:05 +0000222
Chris Lattner6c203912009-08-10 18:15:01 +0000223 // Remember the TypeID.
David Majnemere500ec12014-03-10 00:55:07 +0000224 TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
Stuart Hastings12d53122011-03-19 02:42:31 +0000225 TAAParsed = true;
Chris Lattner6c203912009-08-10 18:15:01 +0000226
227 // If we have no comma after the section type, there are no attributes.
David Majnemere500ec12014-03-10 00:55:07 +0000228 if (Attrs.empty()) {
Chris Lattner6c203912009-08-10 18:15:01 +0000229 // S_SYMBOL_STUBS always require a symbol stub size specifier.
David Majnemer7b583052014-03-07 07:36:05 +0000230 if (TAA == MachO::S_SYMBOL_STUBS)
Chris Lattner6c203912009-08-10 18:15:01 +0000231 return "mach-o section specifier of type 'symbol_stubs' requires a size "
232 "specifier";
233 return "";
234 }
235
Chris Lattner6c203912009-08-10 18:15:01 +0000236 // The attribute list is a '+' separated list of attributes.
David Majnemere500ec12014-03-10 00:55:07 +0000237 SmallVector<StringRef, 1> SectionAttrs;
Chandler Carruthe4405e92015-09-10 06:12:31 +0000238 Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Jim Grosbach90600d32010-10-21 22:04:05 +0000239
David Majnemere500ec12014-03-10 00:55:07 +0000240 for (StringRef &SectionAttr : SectionAttrs) {
241 auto AttrDescriptorI = std::find_if(
242 std::begin(SectionAttrDescriptors), std::end(SectionAttrDescriptors),
David Majnemer596587f2014-03-10 01:04:18 +0000243 [&](decltype(*SectionAttrDescriptors) &Descriptor) {
David Majnemere500ec12014-03-10 00:55:07 +0000244 return Descriptor.AssemblerName &&
245 SectionAttr.trim() == Descriptor.AssemblerName;
246 });
247 if (AttrDescriptorI == std::end(SectionAttrDescriptors))
248 return "mach-o section specifier has invalid attribute";
Chris Lattner6c203912009-08-10 18:15:01 +0000249
David Majnemere500ec12014-03-10 00:55:07 +0000250 TAA |= AttrDescriptorI->AttrFlag;
251 }
Chris Lattner6c203912009-08-10 18:15:01 +0000252
253 // Okay, we've parsed the section attributes, see if we have a stub size spec.
David Majnemere500ec12014-03-10 00:55:07 +0000254 if (StubSizeStr.empty()) {
Chris Lattner6c203912009-08-10 18:15:01 +0000255 // S_SYMBOL_STUBS always require a symbol stub size specifier.
David Majnemer7b583052014-03-07 07:36:05 +0000256 if (TAA == MachO::S_SYMBOL_STUBS)
Chris Lattner6c203912009-08-10 18:15:01 +0000257 return "mach-o section specifier of type 'symbol_stubs' requires a size "
258 "specifier";
259 return "";
260 }
261
262 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
David Majnemer7b583052014-03-07 07:36:05 +0000263 if ((TAA & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS)
Chris Lattner6c203912009-08-10 18:15:01 +0000264 return "mach-o section specifier cannot have a stub size specified because "
265 "it does not have type 'symbol_stubs'";
Jim Grosbach90600d32010-10-21 22:04:05 +0000266
Chris Lattnera15f0042009-09-20 06:58:54 +0000267 // Convert the stub size from a string to an integer.
268 if (StubSizeStr.getAsInteger(0, StubSize))
Chris Lattner6c203912009-08-10 18:15:01 +0000269 return "mach-o section specifier has a malformed stub size";
Jim Grosbach90600d32010-10-21 22:04:05 +0000270
Chris Lattner6c203912009-08-10 18:15:01 +0000271 return "";
272}