/src/keystone/llvm/lib/MC/MCSectionMachO.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
10 | | #include "llvm/MC/MCSectionMachO.h" |
11 | | #include "llvm/MC/MCContext.h" |
12 | | #include "llvm/Support/raw_ostream.h" |
13 | | #include <cctype> |
14 | | using namespace llvm_ks; |
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. |
19 | | static const struct { |
20 | | const char *AssemblerName, *EnumName; |
21 | | } SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE+1] = { |
22 | | { "regular", "S_REGULAR" }, // 0x00 |
23 | | { nullptr, "S_ZEROFILL" }, // 0x01 |
24 | | { "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 |
34 | | { nullptr, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C |
35 | | { "interposing", "S_INTERPOSING" }, // 0x0D |
36 | | { "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E |
37 | | { nullptr, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F |
38 | | { nullptr, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" }, // 0x10 |
39 | | { "thread_local_regular", "S_THREAD_LOCAL_REGULAR" }, // 0x11 |
40 | | { "thread_local_zerofill", "S_THREAD_LOCAL_ZEROFILL" }, // 0x12 |
41 | | { "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 |
46 | | }; |
47 | | |
48 | | |
49 | | /// SectionAttrDescriptors - This is an array of descriptors for section |
50 | | /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed |
51 | | /// by attribute, instead it is searched. |
52 | | static const struct { |
53 | | unsigned AttrFlag; |
54 | | const char *AssemblerName, *EnumName; |
55 | | } SectionAttrDescriptors[] = { |
56 | | #define ENTRY(ASMNAME, ENUM) \ |
57 | | { MachO::ENUM, ASMNAME, #ENUM }, |
58 | | ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS) |
59 | | ENTRY("no_toc", S_ATTR_NO_TOC) |
60 | | ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS) |
61 | | ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP) |
62 | | ENTRY("live_support", S_ATTR_LIVE_SUPPORT) |
63 | | ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE) |
64 | | ENTRY("debug", S_ATTR_DEBUG) |
65 | | ENTRY(nullptr /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS) |
66 | | ENTRY(nullptr /*FIXME*/, S_ATTR_EXT_RELOC) |
67 | | ENTRY(nullptr /*FIXME*/, S_ATTR_LOC_RELOC) |
68 | | #undef ENTRY |
69 | | { 0, "none", nullptr }, // used if section has no attributes but has a stub size |
70 | | }; |
71 | | |
72 | | MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section, |
73 | | unsigned TAA, unsigned reserved2, SectionKind K, |
74 | | MCSymbol *Begin) |
75 | 3.01k | : MCSection(SV_MachO, K, Begin), TypeAndAttributes(TAA), |
76 | 3.01k | Reserved2(reserved2) { |
77 | 3.01k | assert(Segment.size() <= 16 && Section.size() <= 16 && |
78 | 3.01k | "Segment or section string too long"); |
79 | 51.3k | for (unsigned i = 0; i != 16; ++i) { |
80 | 48.3k | if (i < Segment.size()) |
81 | 11.3k | SegmentName[i] = Segment[i]; |
82 | 37.0k | else |
83 | 37.0k | SegmentName[i] = 0; |
84 | | |
85 | 48.3k | if (i < Section.size()) |
86 | 20.6k | SectionName[i] = Section[i]; |
87 | 27.6k | else |
88 | 27.6k | SectionName[i] = 0; |
89 | 48.3k | } |
90 | 3.01k | } |
91 | | |
92 | | void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI, |
93 | | raw_ostream &OS, |
94 | 0 | const MCExpr *Subsection) const { |
95 | 0 | OS << "\t.section\t" << getSegmentName() << ',' << getSectionName(); |
96 | | |
97 | | // Get the section type and attributes. |
98 | 0 | unsigned TAA = getTypeAndAttributes(); |
99 | 0 | if (TAA == 0) { |
100 | 0 | OS << '\n'; |
101 | 0 | return; |
102 | 0 | } |
103 | | |
104 | 0 | MachO::SectionType SectionType = getType(); |
105 | 0 | assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE && |
106 | 0 | "Invalid SectionType specified!"); |
107 | | |
108 | 0 | if (SectionTypeDescriptors[SectionType].AssemblerName) { |
109 | 0 | OS << ','; |
110 | 0 | OS << SectionTypeDescriptors[SectionType].AssemblerName; |
111 | 0 | } else { |
112 | | // If we have no name for the attribute, stop here. |
113 | 0 | OS << '\n'; |
114 | 0 | return; |
115 | 0 | } |
116 | | |
117 | | // If we don't have any attributes, we're done. |
118 | 0 | unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES; |
119 | 0 | if (SectionAttrs == 0) { |
120 | | // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as |
121 | | // the attribute specifier. |
122 | 0 | if (Reserved2 != 0) |
123 | 0 | OS << ",none," << Reserved2; |
124 | 0 | OS << '\n'; |
125 | 0 | return; |
126 | 0 | } |
127 | | |
128 | | // Check each attribute to see if we have it. |
129 | 0 | char Separator = ','; |
130 | 0 | for (unsigned i = 0; |
131 | 0 | SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag; |
132 | 0 | ++i) { |
133 | | // Check to see if we have this attribute. |
134 | 0 | if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0) |
135 | 0 | continue; |
136 | | |
137 | | // Yep, clear it and print it. |
138 | 0 | SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag; |
139 | |
|
140 | 0 | OS << Separator; |
141 | 0 | if (SectionAttrDescriptors[i].AssemblerName) |
142 | 0 | OS << SectionAttrDescriptors[i].AssemblerName; |
143 | 0 | else |
144 | 0 | OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>"; |
145 | 0 | Separator = '+'; |
146 | 0 | } |
147 | |
|
148 | 0 | assert(SectionAttrs == 0 && "Unknown section attributes!"); |
149 | | |
150 | | // If we have a S_SYMBOL_STUBS size specified, print it. |
151 | 0 | if (Reserved2 != 0) |
152 | 0 | OS << ',' << Reserved2; |
153 | 0 | OS << '\n'; |
154 | 0 | } |
155 | | |
156 | 3.90k | bool MCSectionMachO::UseCodeAlign() const { |
157 | 3.90k | return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS); |
158 | 3.90k | } |
159 | | |
160 | 5.84k | bool MCSectionMachO::isVirtualSection() const { |
161 | 5.84k | return (getType() == MachO::S_ZEROFILL || |
162 | 5.84k | getType() == MachO::S_GB_ZEROFILL || |
163 | 5.84k | getType() == MachO::S_THREAD_LOCAL_ZEROFILL); |
164 | 5.84k | } |
165 | | |
166 | | /// 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. |
171 | | std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In. |
172 | | StringRef &Segment, // Out. |
173 | | StringRef &Section, // Out. |
174 | | unsigned &TAA, // Out. |
175 | | bool &TAAParsed, // Out. |
176 | 16.9k | unsigned &StubSize) { // Out. |
177 | 16.9k | TAAParsed = false; |
178 | | |
179 | 16.9k | SmallVector<StringRef, 5> SplitSpec; |
180 | 16.9k | Spec.split(SplitSpec, ','); |
181 | | // Remove leading and trailing whitespace. |
182 | 84.9k | auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef { |
183 | 84.9k | return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef(); |
184 | 84.9k | }; |
185 | 16.9k | Segment = GetEmptyOrTrim(0); |
186 | 16.9k | Section = GetEmptyOrTrim(1); |
187 | 16.9k | StringRef SectionType = GetEmptyOrTrim(2); |
188 | 16.9k | StringRef Attrs = GetEmptyOrTrim(3); |
189 | 16.9k | StringRef StubSizeStr = GetEmptyOrTrim(4); |
190 | | |
191 | | // Verify that the segment is present and not too long. |
192 | 16.9k | if (Segment.empty() || Segment.size() > 16) |
193 | 48 | return "mach-o section specifier requires a segment whose length is " |
194 | 48 | "between 1 and 16 characters"; |
195 | | |
196 | | // Verify that the section is present and not too long. |
197 | 16.9k | if (Section.empty()) |
198 | 117 | return "mach-o section specifier requires a segment and section " |
199 | 117 | "separated by a comma"; |
200 | | |
201 | 16.8k | if (Section.size() > 16) |
202 | 168 | return "mach-o section specifier requires a section whose length is " |
203 | 168 | "between 1 and 16 characters"; |
204 | | |
205 | | // If there is no comma after the section, we're done. |
206 | 16.6k | TAA = 0; |
207 | 16.6k | StubSize = 0; |
208 | 16.6k | if (SectionType.empty()) |
209 | 16.0k | return ""; |
210 | | |
211 | | // Figure out which section type it is. |
212 | 580 | auto TypeDescriptor = std::find_if( |
213 | 580 | std::begin(SectionTypeDescriptors), std::end(SectionTypeDescriptors), |
214 | 9.81k | [&](decltype(*SectionTypeDescriptors) &Descriptor) { |
215 | 9.81k | return Descriptor.AssemblerName && |
216 | 9.81k | SectionType == Descriptor.AssemblerName; |
217 | 9.81k | }); |
218 | | |
219 | | // If we didn't find the section type, reject it. |
220 | 580 | if (TypeDescriptor == std::end(SectionTypeDescriptors)) |
221 | 388 | return "mach-o section specifier uses an unknown section type"; |
222 | | |
223 | | // Remember the TypeID. |
224 | 192 | TAA = TypeDescriptor - std::begin(SectionTypeDescriptors); |
225 | 192 | TAAParsed = true; |
226 | | |
227 | | // If we have no comma after the section type, there are no attributes. |
228 | 192 | if (Attrs.empty()) { |
229 | | // S_SYMBOL_STUBS always require a symbol stub size specifier. |
230 | 4 | if (TAA == MachO::S_SYMBOL_STUBS) |
231 | 0 | return "mach-o section specifier of type 'symbol_stubs' requires a size " |
232 | 0 | "specifier"; |
233 | 4 | return ""; |
234 | 4 | } |
235 | | |
236 | | // The attribute list is a '+' separated list of attributes. |
237 | 188 | SmallVector<StringRef, 1> SectionAttrs; |
238 | 188 | Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
239 | | |
240 | 188 | for (StringRef &SectionAttr : SectionAttrs) { |
241 | 182 | auto AttrDescriptorI = std::find_if( |
242 | 182 | std::begin(SectionAttrDescriptors), std::end(SectionAttrDescriptors), |
243 | 2.00k | [&](decltype(*SectionAttrDescriptors) &Descriptor) { |
244 | 2.00k | return Descriptor.AssemblerName && |
245 | 2.00k | SectionAttr.trim() == Descriptor.AssemblerName; |
246 | 2.00k | }); |
247 | 182 | if (AttrDescriptorI == std::end(SectionAttrDescriptors)) |
248 | 182 | return "mach-o section specifier has invalid attribute"; |
249 | | |
250 | 0 | TAA |= AttrDescriptorI->AttrFlag; |
251 | 0 | } |
252 | | |
253 | | // Okay, we've parsed the section attributes, see if we have a stub size spec. |
254 | 6 | if (StubSizeStr.empty()) { |
255 | | // S_SYMBOL_STUBS always require a symbol stub size specifier. |
256 | 4 | if (TAA == MachO::S_SYMBOL_STUBS) |
257 | 0 | return "mach-o section specifier of type 'symbol_stubs' requires a size " |
258 | 0 | "specifier"; |
259 | 4 | return ""; |
260 | 4 | } |
261 | | |
262 | | // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS. |
263 | 2 | if ((TAA & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS) |
264 | 2 | return "mach-o section specifier cannot have a stub size specified because " |
265 | 2 | "it does not have type 'symbol_stubs'"; |
266 | | |
267 | | // Convert the stub size from a string to an integer. |
268 | 0 | if (StubSizeStr.getAsInteger(0, StubSize)) |
269 | 0 | return "mach-o section specifier has a malformed stub size"; |
270 | | |
271 | 0 | return ""; |
272 | 0 | } |