blob: f40237231a2f7a162c0e3bb80b3bff0c810c4684 [file] [log] [blame]
Chris Lattnerf9bdedd2009-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 Lewycky476b2422010-12-19 20:43:38 +000013#include <cctype>
Chris Lattnerf9bdedd2009-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.
Benjamin Kramerb49b7502017-01-30 18:46:26 +000019static constexpr struct {
20 StringLiteral AssemblerName, EnumName;
Benjamin Kramer4337d892017-01-30 19:05:09 +000021} SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE + 1] = {
22 {StringLiteral("regular"), StringLiteral("S_REGULAR")}, // 0x00
23 {StringLiteral(""), StringLiteral("S_ZEROFILL")}, // 0x01
24 {StringLiteral("cstring_literals"),
25 StringLiteral("S_CSTRING_LITERALS")}, // 0x02
26 {StringLiteral("4byte_literals"),
27 StringLiteral("S_4BYTE_LITERALS")}, // 0x03
28 {StringLiteral("8byte_literals"),
29 StringLiteral("S_8BYTE_LITERALS")}, // 0x04
30 {StringLiteral("literal_pointers"),
31 StringLiteral("S_LITERAL_POINTERS")}, // 0x05
32 {StringLiteral("non_lazy_symbol_pointers"),
33 StringLiteral("S_NON_LAZY_SYMBOL_POINTERS")}, // 0x06
34 {StringLiteral("lazy_symbol_pointers"),
35 StringLiteral("S_LAZY_SYMBOL_POINTERS")}, // 0x07
36 {StringLiteral("symbol_stubs"), StringLiteral("S_SYMBOL_STUBS")}, // 0x08
37 {StringLiteral("mod_init_funcs"),
38 StringLiteral("S_MOD_INIT_FUNC_POINTERS")}, // 0x09
39 {StringLiteral("mod_term_funcs"),
40 StringLiteral("S_MOD_TERM_FUNC_POINTERS")}, // 0x0A
41 {StringLiteral("coalesced"), StringLiteral("S_COALESCED")}, // 0x0B
42 {StringLiteral("") /*FIXME??*/, StringLiteral("S_GB_ZEROFILL")}, // 0x0C
43 {StringLiteral("interposing"), StringLiteral("S_INTERPOSING")}, // 0x0D
44 {StringLiteral("16byte_literals"),
45 StringLiteral("S_16BYTE_LITERALS")}, // 0x0E
46 {StringLiteral("") /*FIXME??*/, StringLiteral("S_DTRACE_DOF")}, // 0x0F
47 {StringLiteral("") /*FIXME??*/,
48 StringLiteral("S_LAZY_DYLIB_SYMBOL_POINTERS")}, // 0x10
49 {StringLiteral("thread_local_regular"),
50 StringLiteral("S_THREAD_LOCAL_REGULAR")}, // 0x11
51 {StringLiteral("thread_local_zerofill"),
52 StringLiteral("S_THREAD_LOCAL_ZEROFILL")}, // 0x12
53 {StringLiteral("thread_local_variables"),
54 StringLiteral("S_THREAD_LOCAL_VARIABLES")}, // 0x13
55 {StringLiteral("thread_local_variable_pointers"),
56 StringLiteral("S_THREAD_LOCAL_VARIABLE_POINTERS")}, // 0x14
57 {StringLiteral("thread_local_init_function_pointers"),
58 StringLiteral("S_THREAD_LOCAL_INIT_FUNCTION_POINTERS")}, // 0x15
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000059};
60
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000061/// SectionAttrDescriptors - This is an array of descriptors for section
62/// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
David Majnemer5d0bfc82014-03-10 00:55:07 +000063/// by attribute, instead it is searched.
Benjamin Kramerb49b7502017-01-30 18:46:26 +000064static constexpr struct {
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000065 unsigned AttrFlag;
Benjamin Kramerb49b7502017-01-30 18:46:26 +000066 StringLiteral AssemblerName, EnumName;
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000067} SectionAttrDescriptors[] = {
68#define ENTRY(ASMNAME, ENUM) \
Benjamin Kramer4337d892017-01-30 19:05:09 +000069 { MachO::ENUM, StringLiteral(ASMNAME), StringLiteral(#ENUM) },
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000070ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
71ENTRY("no_toc", S_ATTR_NO_TOC)
72ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
73ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
74ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
75ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
76ENTRY("debug", S_ATTR_DEBUG)
Benjamin Kramerb49b7502017-01-30 18:46:26 +000077ENTRY("" /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
78ENTRY("" /*FIXME*/, S_ATTR_EXT_RELOC)
79ENTRY("" /*FIXME*/, S_ATTR_LOC_RELOC)
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000080#undef ENTRY
Benjamin Kramer4337d892017-01-30 19:05:09 +000081 { 0, StringLiteral("none"), StringLiteral("") }, // used if section has no attributes but has a stub size
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000082};
83
Chris Lattner74aae472010-04-08 21:26:26 +000084MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
Rafael Espindolaf3d745c2015-03-10 22:00:25 +000085 unsigned TAA, unsigned reserved2, SectionKind K,
86 MCSymbol *Begin)
Rafael Espindola903f4a22015-04-04 18:02:01 +000087 : MCSection(SV_MachO, K, Begin), TypeAndAttributes(TAA),
Rafael Espindolaf3d745c2015-03-10 22:00:25 +000088 Reserved2(reserved2) {
Chris Lattner74aae472010-04-08 21:26:26 +000089 assert(Segment.size() <= 16 && Section.size() <= 16 &&
90 "Segment or section string too long");
91 for (unsigned i = 0; i != 16; ++i) {
92 if (i < Segment.size())
93 SegmentName[i] = Segment[i];
94 else
95 SegmentName[i] = 0;
Jim Grosbachfe40f952010-10-21 22:04:05 +000096
Chris Lattner74aae472010-04-08 21:26:26 +000097 if (i < Section.size())
98 SectionName[i] = Section[i];
99 else
100 SectionName[i] = 0;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000101 }
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000102}
103
Rafael Espindolad5702b72017-01-30 15:38:43 +0000104void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
Peter Collingbournedf39be62013-04-17 21:18:16 +0000105 raw_ostream &OS,
106 const MCExpr *Subsection) const {
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000107 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
Jim Grosbachfe40f952010-10-21 22:04:05 +0000108
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000109 // Get the section type and attributes.
110 unsigned TAA = getTypeAndAttributes();
111 if (TAA == 0) {
112 OS << '\n';
113 return;
114 }
115
David Majnemerf5955c72014-03-07 18:49:54 +0000116 MachO::SectionType SectionType = getType();
David Majnemer508e0c42014-03-07 07:36:05 +0000117 assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE &&
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000118 "Invalid SectionType specified!");
119
Mehdi Amini3eff5cd2016-10-05 01:02:34 +0000120 if (!SectionTypeDescriptors[SectionType].AssemblerName.empty()) {
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000121 OS << ',';
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000122 OS << SectionTypeDescriptors[SectionType].AssemblerName;
Stuart Hastings37c79c52011-02-21 21:07:07 +0000123 } else {
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000124 // If we have no name for the attribute, stop here.
Stuart Hastings37c79c52011-02-21 21:07:07 +0000125 OS << '\n';
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000126 return;
Stuart Hastings37c79c52011-02-21 21:07:07 +0000127 }
Jim Grosbachfe40f952010-10-21 22:04:05 +0000128
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000129 // If we don't have any attributes, we're done.
David Majnemer508e0c42014-03-07 07:36:05 +0000130 unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES;
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000131 if (SectionAttrs == 0) {
132 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
133 // the attribute specifier.
134 if (Reserved2 != 0)
135 OS << ",none," << Reserved2;
136 OS << '\n';
137 return;
138 }
139
140 // Check each attribute to see if we have it.
141 char Separator = ',';
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000142 for (unsigned i = 0;
143 SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
144 ++i) {
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000145 // Check to see if we have this attribute.
146 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
147 continue;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000148
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000149 // Yep, clear it and print it.
150 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000151
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000152 OS << Separator;
Mehdi Amini3eff5cd2016-10-05 01:02:34 +0000153 if (!SectionAttrDescriptors[i].AssemblerName.empty())
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000154 OS << SectionAttrDescriptors[i].AssemblerName;
155 else
156 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
157 Separator = '+';
158 }
Jim Grosbachfe40f952010-10-21 22:04:05 +0000159
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000160 assert(SectionAttrs == 0 && "Unknown section attributes!");
Jim Grosbachfe40f952010-10-21 22:04:05 +0000161
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000162 // If we have a S_SYMBOL_STUBS size specified, print it.
163 if (Reserved2 != 0)
164 OS << ',' << Reserved2;
165 OS << '\n';
166}
167
Jan Wen Voung083cf152010-10-04 17:32:41 +0000168bool MCSectionMachO::UseCodeAlign() const {
David Majnemer508e0c42014-03-07 07:36:05 +0000169 return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS);
Jan Wen Voung083cf152010-10-04 17:32:41 +0000170}
171
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000172bool MCSectionMachO::isVirtualSection() const {
David Majnemer508e0c42014-03-07 07:36:05 +0000173 return (getType() == MachO::S_ZEROFILL ||
174 getType() == MachO::S_GB_ZEROFILL ||
175 getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000176}
177
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000178/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
179/// This is a string that can appear after a .section directive in a mach-o
180/// flavored .s file. If successful, this fills in the specified Out
181/// parameters and returns an empty string. When an invalid section
182/// specifier is present, this returns a string indicating the problem.
183std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
184 StringRef &Segment, // Out.
185 StringRef &Section, // Out.
186 unsigned &TAA, // Out.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000187 bool &TAAParsed, // Out.
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000188 unsigned &StubSize) { // Out.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000189 TAAParsed = false;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000190
David Majnemer5d0bfc82014-03-10 00:55:07 +0000191 SmallVector<StringRef, 5> SplitSpec;
Chandler Carruth6aaf0a62015-09-10 06:12:31 +0000192 Spec.split(SplitSpec, ',');
David Majnemer5d0bfc82014-03-10 00:55:07 +0000193 // Remove leading and trailing whitespace.
194 auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef {
195 return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef();
196 };
197 Segment = GetEmptyOrTrim(0);
198 Section = GetEmptyOrTrim(1);
199 StringRef SectionType = GetEmptyOrTrim(2);
200 StringRef Attrs = GetEmptyOrTrim(3);
201 StringRef StubSizeStr = GetEmptyOrTrim(4);
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000202
203 // Verify that the segment is present and not too long.
204 if (Segment.empty() || Segment.size() > 16)
205 return "mach-o section specifier requires a segment whose length is "
206 "between 1 and 16 characters";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000207
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000208 // Verify that the section is present and not too long.
David Majnemer5d0bfc82014-03-10 00:55:07 +0000209 if (Section.empty())
210 return "mach-o section specifier requires a segment and section "
211 "separated by a comma";
212
213 if (Section.size() > 16)
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000214 return "mach-o section specifier requires a section whose length is "
215 "between 1 and 16 characters";
216
217 // If there is no comma after the section, we're done.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000218 TAA = 0;
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000219 StubSize = 0;
David Majnemer5d0bfc82014-03-10 00:55:07 +0000220 if (SectionType.empty())
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000221 return "";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000222
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000223 // Figure out which section type it is.
David Majnemer5d0bfc82014-03-10 00:55:07 +0000224 auto TypeDescriptor = std::find_if(
225 std::begin(SectionTypeDescriptors), std::end(SectionTypeDescriptors),
David Majnemer4cd770f2014-03-10 01:04:18 +0000226 [&](decltype(*SectionTypeDescriptors) &Descriptor) {
Mehdi Amini3eff5cd2016-10-05 01:02:34 +0000227 return SectionType == Descriptor.AssemblerName;
David Majnemer5d0bfc82014-03-10 00:55:07 +0000228 });
Jim Grosbachfe40f952010-10-21 22:04:05 +0000229
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000230 // If we didn't find the section type, reject it.
David Majnemer5d0bfc82014-03-10 00:55:07 +0000231 if (TypeDescriptor == std::end(SectionTypeDescriptors))
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000232 return "mach-o section specifier uses an unknown section type";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000233
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000234 // Remember the TypeID.
David Majnemer5d0bfc82014-03-10 00:55:07 +0000235 TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000236 TAAParsed = true;
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000237
238 // If we have no comma after the section type, there are no attributes.
David Majnemer5d0bfc82014-03-10 00:55:07 +0000239 if (Attrs.empty()) {
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000240 // S_SYMBOL_STUBS always require a symbol stub size specifier.
David Majnemer508e0c42014-03-07 07:36:05 +0000241 if (TAA == MachO::S_SYMBOL_STUBS)
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000242 return "mach-o section specifier of type 'symbol_stubs' requires a size "
243 "specifier";
244 return "";
245 }
246
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000247 // The attribute list is a '+' separated list of attributes.
David Majnemer5d0bfc82014-03-10 00:55:07 +0000248 SmallVector<StringRef, 1> SectionAttrs;
Chandler Carruth6aaf0a62015-09-10 06:12:31 +0000249 Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Jim Grosbachfe40f952010-10-21 22:04:05 +0000250
David Majnemer5d0bfc82014-03-10 00:55:07 +0000251 for (StringRef &SectionAttr : SectionAttrs) {
252 auto AttrDescriptorI = std::find_if(
253 std::begin(SectionAttrDescriptors), std::end(SectionAttrDescriptors),
David Majnemer4cd770f2014-03-10 01:04:18 +0000254 [&](decltype(*SectionAttrDescriptors) &Descriptor) {
Mehdi Amini3eff5cd2016-10-05 01:02:34 +0000255 return SectionAttr.trim() == Descriptor.AssemblerName;
David Majnemer5d0bfc82014-03-10 00:55:07 +0000256 });
257 if (AttrDescriptorI == std::end(SectionAttrDescriptors))
258 return "mach-o section specifier has invalid attribute";
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000259
David Majnemer5d0bfc82014-03-10 00:55:07 +0000260 TAA |= AttrDescriptorI->AttrFlag;
261 }
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000262
263 // Okay, we've parsed the section attributes, see if we have a stub size spec.
David Majnemer5d0bfc82014-03-10 00:55:07 +0000264 if (StubSizeStr.empty()) {
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000265 // S_SYMBOL_STUBS always require a symbol stub size specifier.
David Majnemer508e0c42014-03-07 07:36:05 +0000266 if (TAA == MachO::S_SYMBOL_STUBS)
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000267 return "mach-o section specifier of type 'symbol_stubs' requires a size "
268 "specifier";
269 return "";
270 }
271
272 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
David Majnemer508e0c42014-03-07 07:36:05 +0000273 if ((TAA & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS)
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000274 return "mach-o section specifier cannot have a stub size specified because "
275 "it does not have type 'symbol_stubs'";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000276
Chris Lattnerd9221d72009-09-20 06:58:54 +0000277 // Convert the stub size from a string to an integer.
278 if (StubSizeStr.getAsInteger(0, StubSize))
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000279 return "mach-o section specifier has a malformed stub size";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000280
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000281 return "";
282}